xref: /freebsd-12.1/contrib/gcc/varasm.c (revision 5bfc7db4)
1 /* Output variables, constants and external declarations, for GNU compiler.
2    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
3    1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4    Free Software Foundation, Inc.
5 
6 This file is part of GCC.
7 
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12 
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.  */
22 
23 
24 /* This file handles generation of all the assembler code
25    *except* the instructions of a function.
26    This includes declarations of variables and their initial values.
27 
28    We also output the assembler code for constants stored in memory
29    and are responsible for combining constants with the same value.  */
30 
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "rtl.h"
36 #include "tree.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "expr.h"
40 #include "hard-reg-set.h"
41 #include "regs.h"
42 #include "real.h"
43 #include "output.h"
44 #include "toplev.h"
45 #include "hashtab.h"
46 #include "c-pragma.h"
47 #include "ggc.h"
48 #include "langhooks.h"
49 #include "tm_p.h"
50 #include "debug.h"
51 #include "target.h"
52 #include "tree-mudflap.h"
53 #include "cgraph.h"
54 #include "cfglayout.h"
55 #include "basic-block.h"
56 
57 #ifdef XCOFF_DEBUGGING_INFO
58 #include "xcoffout.h"		/* Needed for external data
59 				   declarations for e.g. AIX 4.x.  */
60 #endif
61 
62 /* The (assembler) name of the first globally-visible object output.  */
63 extern GTY(()) const char *first_global_object_name;
64 extern GTY(()) const char *weak_global_object_name;
65 
66 const char *first_global_object_name;
67 const char *weak_global_object_name;
68 
69 struct addr_const;
70 struct constant_descriptor_rtx;
71 struct rtx_constant_pool;
72 
73 struct varasm_status GTY(())
74 {
75   /* If we're using a per-function constant pool, this is it.  */
76   struct rtx_constant_pool *pool;
77 
78   /* Number of tree-constants deferred during the expansion of this
79      function.  */
80   unsigned int deferred_constants;
81 };
82 
83 #define n_deferred_constants (cfun->varasm->deferred_constants)
84 
85 /* Number for making the label on the next
86    constant that is stored in memory.  */
87 
88 static GTY(()) int const_labelno;
89 
90 /* Carry information from ASM_DECLARE_OBJECT_NAME
91    to ASM_FINISH_DECLARE_OBJECT.  */
92 
93 int size_directive_output;
94 
95 /* The last decl for which assemble_variable was called,
96    if it did ASM_DECLARE_OBJECT_NAME.
97    If the last call to assemble_variable didn't do that,
98    this holds 0.  */
99 
100 tree last_assemble_variable_decl;
101 
102 /* The following global variable indicates if the first basic block
103    in a function belongs to the cold partition or not.  */
104 
105 bool first_function_block_is_cold;
106 
107 /* We give all constants their own alias set.  Perhaps redundant with
108    MEM_READONLY_P, but pre-dates it.  */
109 
110 static HOST_WIDE_INT const_alias_set;
111 
112 static const char *strip_reg_name (const char *);
113 static int contains_pointers_p (tree);
114 #ifdef ASM_OUTPUT_EXTERNAL
115 static bool incorporeal_function_p (tree);
116 #endif
117 static void decode_addr_const (tree, struct addr_const *);
118 static hashval_t const_desc_hash (const void *);
119 static int const_desc_eq (const void *, const void *);
120 static hashval_t const_hash_1 (const tree);
121 static int compare_constant (const tree, const tree);
122 static tree copy_constant (tree);
123 static void output_constant_def_contents (rtx);
124 static void output_addressed_constants (tree);
125 static unsigned HOST_WIDE_INT array_size_for_constructor (tree);
126 static unsigned min_align (unsigned, unsigned);
127 static void output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int);
128 static void globalize_decl (tree);
129 #ifdef BSS_SECTION_ASM_OP
130 #ifdef ASM_OUTPUT_BSS
131 static void asm_output_bss (FILE *, tree, const char *,
132 			    unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
133 #endif
134 #ifdef ASM_OUTPUT_ALIGNED_BSS
135 static void asm_output_aligned_bss (FILE *, tree, const char *,
136 				    unsigned HOST_WIDE_INT, int)
137      ATTRIBUTE_UNUSED;
138 #endif
139 #endif /* BSS_SECTION_ASM_OP */
140 static void mark_weak (tree);
141 static void output_constant_pool (const char *, tree);
142 
143 /* Well-known sections, each one associated with some sort of *_ASM_OP.  */
144 section *text_section;
145 section *data_section;
146 section *readonly_data_section;
147 section *sdata_section;
148 section *ctors_section;
149 section *dtors_section;
150 section *bss_section;
151 section *sbss_section;
152 
153 /* Various forms of common section.  All are guaranteed to be nonnull.  */
154 section *tls_comm_section;
155 section *comm_section;
156 section *lcomm_section;
157 
158 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
159    May be null.  */
160 section *bss_noswitch_section;
161 
162 /* The section that holds the main exception table, when known.  The section
163    is set either by the target's init_sections hook or by the first call to
164    switch_to_exception_section.  */
165 section *exception_section;
166 
167 /* The section that holds the DWARF2 frame unwind information, when known.
168    The section is set either by the target's init_sections hook or by the
169    first call to switch_to_eh_frame_section.  */
170 section *eh_frame_section;
171 
172 /* asm_out_file's current section.  This is NULL if no section has yet
173    been selected or if we lose track of what the current section is.  */
174 section *in_section;
175 
176 /* True if code for the current function is currently being directed
177    at the cold section.  */
178 bool in_cold_section_p;
179 
180 /* A linked list of all the unnamed sections.  */
181 static GTY(()) section *unnamed_sections;
182 
183 /* Return a nonzero value if DECL has a section attribute.  */
184 #ifndef IN_NAMED_SECTION
185 #define IN_NAMED_SECTION(DECL) \
186   ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
187    && DECL_SECTION_NAME (DECL) != NULL_TREE)
188 #endif
189 
190 /* Hash table of named sections.  */
191 static GTY((param_is (section))) htab_t section_htab;
192 
193 /* A table of object_blocks, indexed by section.  */
194 static GTY((param_is (struct object_block))) htab_t object_block_htab;
195 
196 /* The next number to use for internal anchor labels.  */
197 static GTY(()) int anchor_labelno;
198 
199 /* A pool of constants that can be shared between functions.  */
200 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
201 
202 /* Helper routines for maintaining section_htab.  */
203 
204 static int
section_entry_eq(const void * p1,const void * p2)205 section_entry_eq (const void *p1, const void *p2)
206 {
207   const section *old = p1;
208   const char *new = p2;
209 
210   return strcmp (old->named.name, new) == 0;
211 }
212 
213 static hashval_t
section_entry_hash(const void * p)214 section_entry_hash (const void *p)
215 {
216   const section *old = p;
217   return htab_hash_string (old->named.name);
218 }
219 
220 /* Return a hash value for section SECT.  */
221 
222 static hashval_t
hash_section(section * sect)223 hash_section (section *sect)
224 {
225   if (sect->common.flags & SECTION_NAMED)
226     return htab_hash_string (sect->named.name);
227   return sect->common.flags;
228 }
229 
230 /* Helper routines for maintaining object_block_htab.  */
231 
232 static int
object_block_entry_eq(const void * p1,const void * p2)233 object_block_entry_eq (const void *p1, const void *p2)
234 {
235   const struct object_block *old = p1;
236   const section *new = p2;
237 
238   return old->sect == new;
239 }
240 
241 static hashval_t
object_block_entry_hash(const void * p)242 object_block_entry_hash (const void *p)
243 {
244   const struct object_block *old = p;
245   return hash_section (old->sect);
246 }
247 
248 /* Return a new unnamed section with the given fields.  */
249 
250 section *
get_unnamed_section(unsigned int flags,void (* callback)(const void *),const void * data)251 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
252 		     const void *data)
253 {
254   section *sect;
255 
256   sect = ggc_alloc (sizeof (struct unnamed_section));
257   sect->unnamed.common.flags = flags | SECTION_UNNAMED;
258   sect->unnamed.callback = callback;
259   sect->unnamed.data = data;
260   sect->unnamed.next = unnamed_sections;
261 
262   unnamed_sections = sect;
263   return sect;
264 }
265 
266 /* Return a SECTION_NOSWITCH section with the given fields.  */
267 
268 static section *
get_noswitch_section(unsigned int flags,noswitch_section_callback callback)269 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
270 {
271   section *sect;
272 
273   sect = ggc_alloc (sizeof (struct unnamed_section));
274   sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
275   sect->noswitch.callback = callback;
276 
277   return sect;
278 }
279 
280 /* Return the named section structure associated with NAME.  Create
281    a new section with the given fields if no such structure exists.  */
282 
283 section *
get_section(const char * name,unsigned int flags,tree decl)284 get_section (const char *name, unsigned int flags, tree decl)
285 {
286   section *sect, **slot;
287 
288   slot = (section **)
289     htab_find_slot_with_hash (section_htab, name,
290 			      htab_hash_string (name), INSERT);
291   flags |= SECTION_NAMED;
292   if (*slot == NULL)
293     {
294       sect = ggc_alloc (sizeof (struct named_section));
295       sect->named.common.flags = flags;
296       sect->named.name = ggc_strdup (name);
297       sect->named.decl = decl;
298       *slot = sect;
299     }
300   else
301     {
302       sect = *slot;
303       if ((sect->common.flags & ~SECTION_DECLARED) != flags
304 	  && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
305 	{
306 	  /* Sanity check user variables for flag changes.  */
307 	  if (decl == 0)
308 	    decl = sect->named.decl;
309 	  gcc_assert (decl);
310 	  error ("%+D causes a section type conflict", decl);
311 	}
312     }
313   return sect;
314 }
315 
316 /* Return true if the current compilation mode benefits from having
317    objects grouped into blocks.  */
318 
319 static bool
use_object_blocks_p(void)320 use_object_blocks_p (void)
321 {
322   return flag_section_anchors;
323 }
324 
325 /* Return the object_block structure for section SECT.  Create a new
326    structure if we haven't created one already.  Return null if SECT
327    itself is null.  */
328 
329 static struct object_block *
get_block_for_section(section * sect)330 get_block_for_section (section *sect)
331 {
332   struct object_block *block;
333   void **slot;
334 
335   if (sect == NULL)
336     return NULL;
337 
338   slot = htab_find_slot_with_hash (object_block_htab, sect,
339 				   hash_section (sect), INSERT);
340   block = (struct object_block *) *slot;
341   if (block == NULL)
342     {
343       block = (struct object_block *)
344 	ggc_alloc_cleared (sizeof (struct object_block));
345       block->sect = sect;
346       *slot = block;
347     }
348   return block;
349 }
350 
351 /* Create a symbol with label LABEL and place it at byte offset
352    OFFSET in BLOCK.  OFFSET can be negative if the symbol's offset
353    is not yet known.  LABEL must be a garbage-collected string.  */
354 
355 static rtx
create_block_symbol(const char * label,struct object_block * block,HOST_WIDE_INT offset)356 create_block_symbol (const char *label, struct object_block *block,
357 		     HOST_WIDE_INT offset)
358 {
359   rtx symbol;
360   unsigned int size;
361 
362   /* Create the extended SYMBOL_REF.  */
363   size = RTX_HDR_SIZE + sizeof (struct block_symbol);
364   symbol = ggc_alloc_zone (size, &rtl_zone);
365 
366   /* Initialize the normal SYMBOL_REF fields.  */
367   memset (symbol, 0, size);
368   PUT_CODE (symbol, SYMBOL_REF);
369   PUT_MODE (symbol, Pmode);
370   XSTR (symbol, 0) = label;
371   SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
372 
373   /* Initialize the block_symbol stuff.  */
374   SYMBOL_REF_BLOCK (symbol) = block;
375   SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
376 
377   return symbol;
378 }
379 
380 static void
initialize_cold_section_name(void)381 initialize_cold_section_name (void)
382 {
383   const char *stripped_name;
384   char *name, *buffer;
385   tree dsn;
386 
387   gcc_assert (cfun && current_function_decl);
388   if (cfun->unlikely_text_section_name)
389     return;
390 
391   dsn = DECL_SECTION_NAME (current_function_decl);
392   if (flag_function_sections && dsn)
393     {
394       name = alloca (TREE_STRING_LENGTH (dsn) + 1);
395       memcpy (name, TREE_STRING_POINTER (dsn), TREE_STRING_LENGTH (dsn) + 1);
396 
397       stripped_name = targetm.strip_name_encoding (name);
398 
399       buffer = ACONCAT ((stripped_name, "_unlikely", NULL));
400       cfun->unlikely_text_section_name = ggc_strdup (buffer);
401     }
402   else
403     cfun->unlikely_text_section_name =  UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
404 }
405 
406 /* Tell assembler to switch to unlikely-to-be-executed text section.  */
407 
408 section *
unlikely_text_section(void)409 unlikely_text_section (void)
410 {
411   if (cfun)
412     {
413       if (!cfun->unlikely_text_section_name)
414 	initialize_cold_section_name ();
415 
416       return get_named_section (NULL, cfun->unlikely_text_section_name, 0);
417     }
418   else
419     return get_named_section (NULL, UNLIKELY_EXECUTED_TEXT_SECTION_NAME, 0);
420 }
421 
422 /* When called within a function context, return true if the function
423    has been assigned a cold text section and if SECT is that section.
424    When called outside a function context, return true if SECT is the
425    default cold section.  */
426 
427 bool
unlikely_text_section_p(section * sect)428 unlikely_text_section_p (section *sect)
429 {
430   const char *name;
431 
432   if (cfun)
433     name = cfun->unlikely_text_section_name;
434   else
435     name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
436 
437   return (name
438 	  && sect
439 	  && SECTION_STYLE (sect) == SECTION_NAMED
440 	  && strcmp (name, sect->named.name) == 0);
441 }
442 
443 /* Return a section with a particular name and with whatever SECTION_*
444    flags section_type_flags deems appropriate.  The name of the section
445    is taken from NAME if nonnull, otherwise it is taken from DECL's
446    DECL_SECTION_NAME.  DECL is the decl associated with the section
447    (see the section comment for details) and RELOC is as for
448    section_type_flags.  */
449 
450 section *
get_named_section(tree decl,const char * name,int reloc)451 get_named_section (tree decl, const char *name, int reloc)
452 {
453   unsigned int flags;
454 
455   gcc_assert (!decl || DECL_P (decl));
456   if (name == NULL)
457     name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
458 
459   flags = targetm.section_type_flags (decl, name, reloc);
460 
461   return get_section (name, flags, decl);
462 }
463 
464 /* If required, set DECL_SECTION_NAME to a unique name.  */
465 
466 void
resolve_unique_section(tree decl,int reloc ATTRIBUTE_UNUSED,int flag_function_or_data_sections)467 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
468 			int flag_function_or_data_sections)
469 {
470   if (DECL_SECTION_NAME (decl) == NULL_TREE
471       && targetm.have_named_sections
472       && (flag_function_or_data_sections
473 	  || DECL_ONE_ONLY (decl)))
474     targetm.asm_out.unique_section (decl, reloc);
475 }
476 
477 #ifdef BSS_SECTION_ASM_OP
478 
479 #ifdef ASM_OUTPUT_BSS
480 
481 /* Utility function for ASM_OUTPUT_BSS for targets to use if
482    they don't support alignments in .bss.
483    ??? It is believed that this function will work in most cases so such
484    support is localized here.  */
485 
486 static void
asm_output_bss(FILE * file,tree decl ATTRIBUTE_UNUSED,const char * name,unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,unsigned HOST_WIDE_INT rounded)487 asm_output_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
488 		const char *name,
489 		unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
490 		unsigned HOST_WIDE_INT rounded)
491 {
492   targetm.asm_out.globalize_label (file, name);
493   switch_to_section (bss_section);
494 #ifdef ASM_DECLARE_OBJECT_NAME
495   last_assemble_variable_decl = decl;
496   ASM_DECLARE_OBJECT_NAME (file, name, decl);
497 #else
498   /* Standard thing is just output label for the object.  */
499   ASM_OUTPUT_LABEL (file, name);
500 #endif /* ASM_DECLARE_OBJECT_NAME */
501   ASM_OUTPUT_SKIP (file, rounded ? rounded : 1);
502 }
503 
504 #endif
505 
506 #ifdef ASM_OUTPUT_ALIGNED_BSS
507 
508 /* Utility function for targets to use in implementing
509    ASM_OUTPUT_ALIGNED_BSS.
510    ??? It is believed that this function will work in most cases so such
511    support is localized here.  */
512 
513 static void
asm_output_aligned_bss(FILE * file,tree decl ATTRIBUTE_UNUSED,const char * name,unsigned HOST_WIDE_INT size,int align)514 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
515 			const char *name, unsigned HOST_WIDE_INT size,
516 			int align)
517 {
518   switch_to_section (bss_section);
519   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
520 #ifdef ASM_DECLARE_OBJECT_NAME
521   last_assemble_variable_decl = decl;
522   ASM_DECLARE_OBJECT_NAME (file, name, decl);
523 #else
524   /* Standard thing is just output label for the object.  */
525   ASM_OUTPUT_LABEL (file, name);
526 #endif /* ASM_DECLARE_OBJECT_NAME */
527   ASM_OUTPUT_SKIP (file, size ? size : 1);
528 }
529 
530 #endif
531 
532 #endif /* BSS_SECTION_ASM_OP */
533 
534 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
535 /* Return the hot section for function DECL.  Return text_section for
536    null DECLs.  */
537 
538 static section *
hot_function_section(tree decl)539 hot_function_section (tree decl)
540 {
541   if (decl != NULL_TREE
542       && DECL_SECTION_NAME (decl) != NULL_TREE
543       && targetm.have_named_sections)
544     return get_named_section (decl, NULL, 0);
545   else
546     return text_section;
547 }
548 #endif
549 
550 /* Return the section for function DECL.
551 
552    If DECL is NULL_TREE, return the text section.  We can be passed
553    NULL_TREE under some circumstances by dbxout.c at least.  */
554 
555 section *
function_section(tree decl)556 function_section (tree decl)
557 {
558   int reloc = 0;
559 
560   if (first_function_block_is_cold)
561     reloc = 1;
562 
563 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
564   if (decl != NULL_TREE
565       && DECL_SECTION_NAME (decl) != NULL_TREE)
566     return reloc ? unlikely_text_section ()
567 		 : get_named_section (decl, NULL, 0);
568   else
569     return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
570 #else
571   return reloc ? unlikely_text_section () : hot_function_section (decl);
572 #endif
573 }
574 
575 section *
current_function_section(void)576 current_function_section (void)
577 {
578 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
579   if (current_function_decl != NULL_TREE
580       && DECL_SECTION_NAME (current_function_decl) != NULL_TREE)
581     return in_cold_section_p ? unlikely_text_section ()
582 			     : get_named_section (current_function_decl,
583 						  NULL, 0);
584   else
585     return targetm.asm_out.select_section (current_function_decl,
586 					   in_cold_section_p,
587 					   DECL_ALIGN (current_function_decl));
588 #else
589   return (in_cold_section_p
590 	  ? unlikely_text_section ()
591 	  : hot_function_section (current_function_decl));
592 #endif
593 }
594 
595 /* Return the read-only data section associated with function DECL.  */
596 
597 section *
default_function_rodata_section(tree decl)598 default_function_rodata_section (tree decl)
599 {
600   if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
601     {
602       const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
603 
604       if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
605         {
606 	  size_t len = strlen (name) + 3;
607 	  char* rname = alloca (len);
608 
609 	  strcpy (rname, ".rodata");
610 	  strcat (rname, name + 5);
611 	  return get_section (rname, SECTION_LINKONCE, decl);
612 	}
613       /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo.  */
614       else if (DECL_ONE_ONLY (decl)
615 	       && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
616 	{
617 	  size_t len = strlen (name) + 1;
618 	  char *rname = alloca (len);
619 
620 	  memcpy (rname, name, len);
621 	  rname[14] = 'r';
622 	  return get_section (rname, SECTION_LINKONCE, decl);
623 	}
624       /* For .text.foo we want to use .rodata.foo.  */
625       else if (flag_function_sections && flag_data_sections
626 	       && strncmp (name, ".text.", 6) == 0)
627 	{
628 	  size_t len = strlen (name) + 1;
629 	  char *rname = alloca (len + 2);
630 
631 	  memcpy (rname, ".rodata", 7);
632 	  memcpy (rname + 7, name + 5, len - 5);
633 	  return get_section (rname, 0, decl);
634 	}
635     }
636 
637   return readonly_data_section;
638 }
639 
640 /* Return the read-only data section associated with function DECL
641    for targets where that section should be always the single
642    readonly data section.  */
643 
644 section *
default_no_function_rodata_section(tree decl ATTRIBUTE_UNUSED)645 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
646 {
647   return readonly_data_section;
648 }
649 
650 /* Return the section to use for string merging.  */
651 
652 static section *
mergeable_string_section(tree decl ATTRIBUTE_UNUSED,unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,unsigned int flags ATTRIBUTE_UNUSED)653 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
654 			  unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
655 			  unsigned int flags ATTRIBUTE_UNUSED)
656 {
657   HOST_WIDE_INT len;
658 
659   if (HAVE_GAS_SHF_MERGE && flag_merge_constants
660       && TREE_CODE (decl) == STRING_CST
661       && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
662       && align <= 256
663       && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
664       && TREE_STRING_LENGTH (decl) >= len)
665     {
666       enum machine_mode mode;
667       unsigned int modesize;
668       const char *str;
669       HOST_WIDE_INT i;
670       int j, unit;
671       char name[30];
672 
673       mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
674       modesize = GET_MODE_BITSIZE (mode);
675       if (modesize >= 8 && modesize <= 256
676 	  && (modesize & (modesize - 1)) == 0)
677 	{
678 	  if (align < modesize)
679 	    align = modesize;
680 
681 	  str = TREE_STRING_POINTER (decl);
682 	  unit = GET_MODE_SIZE (mode);
683 
684 	  /* Check for embedded NUL characters.  */
685 	  for (i = 0; i < len; i += unit)
686 	    {
687 	      for (j = 0; j < unit; j++)
688 		if (str[i + j] != '\0')
689 		  break;
690 	      if (j == unit)
691 		break;
692 	    }
693 	  if (i == len - unit)
694 	    {
695 	      sprintf (name, ".rodata.str%d.%d", modesize / 8,
696 		       (int) (align / 8));
697 	      flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
698 	      return get_section (name, flags, NULL);
699 	    }
700 	}
701     }
702 
703   return readonly_data_section;
704 }
705 
706 /* Return the section to use for constant merging.  */
707 
708 section *
mergeable_constant_section(enum machine_mode mode ATTRIBUTE_UNUSED,unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,unsigned int flags ATTRIBUTE_UNUSED)709 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
710 			    unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
711 			    unsigned int flags ATTRIBUTE_UNUSED)
712 {
713   unsigned int modesize = GET_MODE_BITSIZE (mode);
714 
715   if (HAVE_GAS_SHF_MERGE && flag_merge_constants
716       && mode != VOIDmode
717       && mode != BLKmode
718       && modesize <= align
719       && align >= 8
720       && align <= 256
721       && (align & (align - 1)) == 0)
722     {
723       char name[24];
724 
725       sprintf (name, ".rodata.cst%d", (int) (align / 8));
726       flags |= (align / 8) | SECTION_MERGE;
727       return get_section (name, flags, NULL);
728     }
729   return readonly_data_section;
730 }
731 
732 /* Given NAME, a putative register name, discard any customary prefixes.  */
733 
734 static const char *
strip_reg_name(const char * name)735 strip_reg_name (const char *name)
736 {
737 #ifdef REGISTER_PREFIX
738   if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
739     name += strlen (REGISTER_PREFIX);
740 #endif
741   if (name[0] == '%' || name[0] == '#')
742     name++;
743   return name;
744 }
745 
746 /* The user has asked for a DECL to have a particular name.  Set (or
747    change) it in such a way that we don't prefix an underscore to
748    it.  */
749 void
set_user_assembler_name(tree decl,const char * name)750 set_user_assembler_name (tree decl, const char *name)
751 {
752   char *starred = alloca (strlen (name) + 2);
753   starred[0] = '*';
754   strcpy (starred + 1, name);
755   change_decl_assembler_name (decl, get_identifier (starred));
756   SET_DECL_RTL (decl, NULL_RTX);
757 }
758 
759 /* Decode an `asm' spec for a declaration as a register name.
760    Return the register number, or -1 if nothing specified,
761    or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
762    or -3 if ASMSPEC is `cc' and is not recognized,
763    or -4 if ASMSPEC is `memory' and is not recognized.
764    Accept an exact spelling or a decimal number.
765    Prefixes such as % are optional.  */
766 
767 int
decode_reg_name(const char * asmspec)768 decode_reg_name (const char *asmspec)
769 {
770   if (asmspec != 0)
771     {
772       int i;
773 
774       /* Get rid of confusing prefixes.  */
775       asmspec = strip_reg_name (asmspec);
776 
777       /* Allow a decimal number as a "register name".  */
778       for (i = strlen (asmspec) - 1; i >= 0; i--)
779 	if (! ISDIGIT (asmspec[i]))
780 	  break;
781       if (asmspec[0] != 0 && i < 0)
782 	{
783 	  i = atoi (asmspec);
784 	  if (i < FIRST_PSEUDO_REGISTER && i >= 0)
785 	    return i;
786 	  else
787 	    return -2;
788 	}
789 
790       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
791 	if (reg_names[i][0]
792 	    && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
793 	  return i;
794 
795 #ifdef ADDITIONAL_REGISTER_NAMES
796       {
797 	static const struct { const char *const name; const int number; } table[]
798 	  = ADDITIONAL_REGISTER_NAMES;
799 
800 	for (i = 0; i < (int) ARRAY_SIZE (table); i++)
801 	  if (table[i].name[0]
802 	      && ! strcmp (asmspec, table[i].name))
803 	    return table[i].number;
804       }
805 #endif /* ADDITIONAL_REGISTER_NAMES */
806 
807       if (!strcmp (asmspec, "memory"))
808 	return -4;
809 
810       if (!strcmp (asmspec, "cc"))
811 	return -3;
812 
813       return -2;
814     }
815 
816   return -1;
817 }
818 
819 /* Return true if DECL's initializer is suitable for a BSS section.  */
820 
821 static bool
bss_initializer_p(tree decl)822 bss_initializer_p (tree decl)
823 {
824   return (DECL_INITIAL (decl) == NULL
825 	  || DECL_INITIAL (decl) == error_mark_node
826 	  || (flag_zero_initialized_in_bss
827 	      /* Leave constant zeroes in .rodata so they
828 		 can be shared.  */
829 	      && !TREE_READONLY (decl)
830 	      && initializer_zerop (DECL_INITIAL (decl))));
831 }
832 
833 /* Compute the alignment of variable specified by DECL.
834    DONT_OUTPUT_DATA is from assemble_variable.  */
835 
836 void
align_variable(tree decl,bool dont_output_data)837 align_variable (tree decl, bool dont_output_data)
838 {
839   unsigned int align = DECL_ALIGN (decl);
840 
841   /* In the case for initialing an array whose length isn't specified,
842      where we have not yet been able to do the layout,
843      figure out the proper alignment now.  */
844   if (dont_output_data && DECL_SIZE (decl) == 0
845       && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
846     align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
847 
848   /* Some object file formats have a maximum alignment which they support.
849      In particular, a.out format supports a maximum alignment of 4.  */
850   if (align > MAX_OFILE_ALIGNMENT)
851     {
852       warning (0, "alignment of %q+D is greater than maximum object "
853                "file alignment.  Using %d", decl,
854 	       MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
855       align = MAX_OFILE_ALIGNMENT;
856     }
857 
858   /* On some machines, it is good to increase alignment sometimes.  */
859   if (! DECL_USER_ALIGN (decl))
860     {
861 #ifdef DATA_ALIGNMENT
862       align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
863 #endif
864 #ifdef CONSTANT_ALIGNMENT
865       if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
866 	align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
867 #endif
868     }
869 
870   /* Reset the alignment in case we have made it tighter, so we can benefit
871      from it in get_pointer_alignment.  */
872   DECL_ALIGN (decl) = align;
873 }
874 
875 /* Return the section into which the given VAR_DECL or CONST_DECL
876    should be placed.  PREFER_NOSWITCH_P is true if a noswitch
877    section should be used wherever possible.  */
878 
879 static section *
get_variable_section(tree decl,bool prefer_noswitch_p)880 get_variable_section (tree decl, bool prefer_noswitch_p)
881 {
882   int reloc;
883 
884   /* If the decl has been given an explicit section name, then it
885      isn't common, and shouldn't be handled as such.  */
886   if (DECL_COMMON (decl) && DECL_SECTION_NAME (decl) == NULL)
887     {
888       if (DECL_THREAD_LOCAL_P (decl))
889 	return tls_comm_section;
890       if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
891 	return comm_section;
892     }
893 
894   if (DECL_INITIAL (decl) == error_mark_node)
895     reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
896   else if (DECL_INITIAL (decl))
897     reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
898   else
899     reloc = 0;
900 
901   resolve_unique_section (decl, reloc, flag_data_sections);
902   if (IN_NAMED_SECTION (decl))
903     return get_named_section (decl, NULL, reloc);
904 
905   if (!DECL_THREAD_LOCAL_P (decl)
906       && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
907       && bss_initializer_p (decl))
908     {
909       if (!TREE_PUBLIC (decl))
910 	return lcomm_section;
911       if (bss_noswitch_section)
912 	return bss_noswitch_section;
913     }
914 
915   return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
916 }
917 
918 /* Return the block into which object_block DECL should be placed.  */
919 
920 static struct object_block *
get_block_for_decl(tree decl)921 get_block_for_decl (tree decl)
922 {
923   section *sect;
924 
925   if (TREE_CODE (decl) == VAR_DECL)
926     {
927       /* The object must be defined in this translation unit.  */
928       if (DECL_EXTERNAL (decl))
929 	return NULL;
930 
931       /* There's no point using object blocks for something that is
932 	 isolated by definition.  */
933       if (DECL_ONE_ONLY (decl))
934 	return NULL;
935     }
936 
937   /* We can only calculate block offsets if the decl has a known
938      constant size.  */
939   if (DECL_SIZE_UNIT (decl) == NULL)
940     return NULL;
941   if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
942     return NULL;
943 
944   /* Find out which section should contain DECL.  We cannot put it into
945      an object block if it requires a standalone definition.  */
946   if (TREE_CODE (decl) == VAR_DECL)
947       align_variable (decl, 0);
948   sect = get_variable_section (decl, true);
949   if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
950     return NULL;
951 
952   return get_block_for_section (sect);
953 }
954 
955 /* Make sure block symbol SYMBOL is in block BLOCK.  */
956 
957 static void
change_symbol_block(rtx symbol,struct object_block * block)958 change_symbol_block (rtx symbol, struct object_block *block)
959 {
960   if (block != SYMBOL_REF_BLOCK (symbol))
961     {
962       gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
963       SYMBOL_REF_BLOCK (symbol) = block;
964     }
965 }
966 
967 /* Return true if it is possible to put DECL in an object_block.  */
968 
969 static bool
use_blocks_for_decl_p(tree decl)970 use_blocks_for_decl_p (tree decl)
971 {
972   /* Only data DECLs can be placed into object blocks.  */
973   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
974     return false;
975 
976   /* Detect decls created by dw2_force_const_mem.  Such decls are
977      special because DECL_INITIAL doesn't specify the decl's true value.
978      dw2_output_indirect_constants will instead call assemble_variable
979      with dont_output_data set to 1 and then print the contents itself.  */
980   if (DECL_INITIAL (decl) == decl)
981     return false;
982 
983   /* If this decl is an alias, then we don't want to emit a definition.  */
984   if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
985     return false;
986 
987   return true;
988 }
989 
990 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL.  DECL should
991    have static storage duration.  In other words, it should not be an
992    automatic variable, including PARM_DECLs.
993 
994    There is, however, one exception: this function handles variables
995    explicitly placed in a particular register by the user.
996 
997    This is never called for PARM_DECL nodes.  */
998 
999 void
make_decl_rtl(tree decl)1000 make_decl_rtl (tree decl)
1001 {
1002   const char *name = 0;
1003   int reg_number;
1004   rtx x;
1005 
1006   /* Check that we are not being given an automatic variable.  */
1007   gcc_assert (TREE_CODE (decl) != PARM_DECL
1008 	      && TREE_CODE (decl) != RESULT_DECL);
1009 
1010   /* A weak alias has TREE_PUBLIC set but not the other bits.  */
1011   gcc_assert (TREE_CODE (decl) != VAR_DECL
1012 	      || TREE_STATIC (decl)
1013 	      || TREE_PUBLIC (decl)
1014 	      || DECL_EXTERNAL (decl)
1015 	      || DECL_REGISTER (decl));
1016 
1017   /* And that we were not given a type or a label.  */
1018   gcc_assert (TREE_CODE (decl) != TYPE_DECL
1019 	      && TREE_CODE (decl) != LABEL_DECL);
1020 
1021   /* For a duplicate declaration, we can be called twice on the
1022      same DECL node.  Don't discard the RTL already made.  */
1023   if (DECL_RTL_SET_P (decl))
1024     {
1025       /* If the old RTL had the wrong mode, fix the mode.  */
1026       x = DECL_RTL (decl);
1027       if (GET_MODE (x) != DECL_MODE (decl))
1028 	SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1029 
1030       if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1031 	return;
1032 
1033       /* ??? Another way to do this would be to maintain a hashed
1034 	 table of such critters.  Instead of adding stuff to a DECL
1035 	 to give certain attributes to it, we could use an external
1036 	 hash map from DECL to set of attributes.  */
1037 
1038       /* Let the target reassign the RTL if it wants.
1039 	 This is necessary, for example, when one machine specific
1040 	 decl attribute overrides another.  */
1041       targetm.encode_section_info (decl, DECL_RTL (decl), false);
1042 
1043       /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1044 	 on the new decl information.  */
1045       if (MEM_P (x)
1046 	  && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1047 	  && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1048 	change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1049 
1050       /* Make this function static known to the mudflap runtime.  */
1051       if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1052 	mudflap_enqueue_decl (decl);
1053 
1054       return;
1055     }
1056 
1057   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1058 
1059   if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1060       && DECL_REGISTER (decl))
1061     {
1062       error ("register name not specified for %q+D", decl);
1063     }
1064   else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1065     {
1066       const char *asmspec = name+1;
1067       reg_number = decode_reg_name (asmspec);
1068       /* First detect errors in declaring global registers.  */
1069       if (reg_number == -1)
1070 	error ("register name not specified for %q+D", decl);
1071       else if (reg_number < 0)
1072 	error ("invalid register name for %q+D", decl);
1073       else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
1074 	error ("data type of %q+D isn%'t suitable for a register",
1075 	       decl);
1076       else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
1077 	error ("register specified for %q+D isn%'t suitable for data type",
1078                decl);
1079       /* Now handle properly declared static register variables.  */
1080       else
1081 	{
1082 	  int nregs;
1083 
1084 	  if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1085 	    {
1086 	      DECL_INITIAL (decl) = 0;
1087 	      error ("global register variable has initial value");
1088 	    }
1089 	  if (TREE_THIS_VOLATILE (decl))
1090 	    warning (OPT_Wvolatile_register_var,
1091 		     "optimization may eliminate reads and/or "
1092 		     "writes to register variables");
1093 
1094 	  /* If the user specified one of the eliminables registers here,
1095 	     e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1096 	     confused with that register and be eliminated.  This usage is
1097 	     somewhat suspect...  */
1098 
1099 	  SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
1100 	  ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1101 	  REG_USERVAR_P (DECL_RTL (decl)) = 1;
1102 
1103 	  if (TREE_STATIC (decl))
1104 	    {
1105 	      /* Make this register global, so not usable for anything
1106 		 else.  */
1107 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1108 	      name = IDENTIFIER_POINTER (DECL_NAME (decl));
1109 	      ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1110 #endif
1111 	      nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
1112 	      while (nregs > 0)
1113 		globalize_reg (reg_number + --nregs);
1114 	    }
1115 
1116 	  /* As a register variable, it has no section.  */
1117 	  return;
1118 	}
1119     }
1120   /* Now handle ordinary static variables and functions (in memory).
1121      Also handle vars declared register invalidly.  */
1122   else if (name[0] == '*')
1123   {
1124 #ifdef REGISTER_PREFIX
1125     if (strlen (REGISTER_PREFIX) != 0)
1126       {
1127 	reg_number = decode_reg_name (name);
1128 	if (reg_number >= 0 || reg_number == -3)
1129 	  error ("register name given for non-register variable %q+D", decl);
1130       }
1131 #endif
1132   }
1133 
1134   /* Specifying a section attribute on a variable forces it into a
1135      non-.bss section, and thus it cannot be common.  */
1136   if (TREE_CODE (decl) == VAR_DECL
1137       && DECL_SECTION_NAME (decl) != NULL_TREE
1138       && DECL_INITIAL (decl) == NULL_TREE
1139       && DECL_COMMON (decl))
1140     DECL_COMMON (decl) = 0;
1141 
1142   /* Variables can't be both common and weak.  */
1143   if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1144     DECL_COMMON (decl) = 0;
1145 
1146   if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1147     x = create_block_symbol (name, get_block_for_decl (decl), -1);
1148   else
1149     x = gen_rtx_SYMBOL_REF (Pmode, name);
1150   SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1151   SET_SYMBOL_REF_DECL (x, decl);
1152 
1153   x = gen_rtx_MEM (DECL_MODE (decl), x);
1154   if (TREE_CODE (decl) != FUNCTION_DECL)
1155     set_mem_attributes (x, decl, 1);
1156   SET_DECL_RTL (decl, x);
1157 
1158   /* Optionally set flags or add text to the name to record information
1159      such as that it is a function name.
1160      If the name is changed, the macro ASM_OUTPUT_LABELREF
1161      will have to know how to strip this information.  */
1162   targetm.encode_section_info (decl, DECL_RTL (decl), true);
1163 
1164   /* Make this function static known to the mudflap runtime.  */
1165   if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1166     mudflap_enqueue_decl (decl);
1167 }
1168 
1169 /* Output a string of literal assembler code
1170    for an `asm' keyword used between functions.  */
1171 
1172 void
assemble_asm(tree string)1173 assemble_asm (tree string)
1174 {
1175   app_enable ();
1176 
1177   if (TREE_CODE (string) == ADDR_EXPR)
1178     string = TREE_OPERAND (string, 0);
1179 
1180   fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
1181 }
1182 
1183 /* Record an element in the table of global destructors.  SYMBOL is
1184    a SYMBOL_REF of the function to be called; PRIORITY is a number
1185    between 0 and MAX_INIT_PRIORITY.  */
1186 
1187 void
default_stabs_asm_out_destructor(rtx symbol ATTRIBUTE_UNUSED,int priority ATTRIBUTE_UNUSED)1188 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
1189 				  int priority ATTRIBUTE_UNUSED)
1190 {
1191 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1192   /* Tell GNU LD that this is part of the static destructor set.
1193      This will work for any system that uses stabs, most usefully
1194      aout systems.  */
1195   dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
1196   dbxout_stab_value_label (XSTR (symbol, 0));
1197 #else
1198   sorry ("global destructors not supported on this target");
1199 #endif
1200 }
1201 
1202 void
default_named_section_asm_out_destructor(rtx symbol,int priority)1203 default_named_section_asm_out_destructor (rtx symbol, int priority)
1204 {
1205   const char *section = ".dtors";
1206   char buf[16];
1207 
1208   /* ??? This only works reliably with the GNU linker.  */
1209   if (priority != DEFAULT_INIT_PRIORITY)
1210     {
1211       sprintf (buf, ".dtors.%.5u",
1212 	       /* Invert the numbering so the linker puts us in the proper
1213 		  order; constructors are run from right to left, and the
1214 		  linker sorts in increasing order.  */
1215 	       MAX_INIT_PRIORITY - priority);
1216       section = buf;
1217     }
1218 
1219   switch_to_section (get_section (section, SECTION_WRITE, NULL));
1220   assemble_align (POINTER_SIZE);
1221   assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1222 }
1223 
1224 #ifdef DTORS_SECTION_ASM_OP
1225 void
default_dtor_section_asm_out_destructor(rtx symbol,int priority ATTRIBUTE_UNUSED)1226 default_dtor_section_asm_out_destructor (rtx symbol,
1227 					 int priority ATTRIBUTE_UNUSED)
1228 {
1229   switch_to_section (dtors_section);
1230   assemble_align (POINTER_SIZE);
1231   assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1232 }
1233 #endif
1234 
1235 /* Likewise for global constructors.  */
1236 
1237 void
default_stabs_asm_out_constructor(rtx symbol ATTRIBUTE_UNUSED,int priority ATTRIBUTE_UNUSED)1238 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
1239 				   int priority ATTRIBUTE_UNUSED)
1240 {
1241 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1242   /* Tell GNU LD that this is part of the static destructor set.
1243      This will work for any system that uses stabs, most usefully
1244      aout systems.  */
1245   dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
1246   dbxout_stab_value_label (XSTR (symbol, 0));
1247 #else
1248   sorry ("global constructors not supported on this target");
1249 #endif
1250 }
1251 
1252 void
default_named_section_asm_out_constructor(rtx symbol,int priority)1253 default_named_section_asm_out_constructor (rtx symbol, int priority)
1254 {
1255   const char *section = ".ctors";
1256   char buf[16];
1257 
1258   /* ??? This only works reliably with the GNU linker.  */
1259   if (priority != DEFAULT_INIT_PRIORITY)
1260     {
1261       sprintf (buf, ".ctors.%.5u",
1262 	       /* Invert the numbering so the linker puts us in the proper
1263 		  order; constructors are run from right to left, and the
1264 		  linker sorts in increasing order.  */
1265 	       MAX_INIT_PRIORITY - priority);
1266       section = buf;
1267     }
1268 
1269   switch_to_section (get_section (section, SECTION_WRITE, NULL));
1270   assemble_align (POINTER_SIZE);
1271   assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1272 }
1273 
1274 #ifdef CTORS_SECTION_ASM_OP
1275 void
default_ctor_section_asm_out_constructor(rtx symbol,int priority ATTRIBUTE_UNUSED)1276 default_ctor_section_asm_out_constructor (rtx symbol,
1277 					  int priority ATTRIBUTE_UNUSED)
1278 {
1279   switch_to_section (ctors_section);
1280   assemble_align (POINTER_SIZE);
1281   assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1282 }
1283 #endif
1284 
1285 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1286    a nonzero value if the constant pool should be output before the
1287    start of the function, or a zero value if the pool should output
1288    after the end of the function.  The default is to put it before the
1289    start.  */
1290 
1291 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1292 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1293 #endif
1294 
1295 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1296    to be output to assembler.
1297    Set first_global_object_name and weak_global_object_name as appropriate.  */
1298 
1299 void
notice_global_symbol(tree decl)1300 notice_global_symbol (tree decl)
1301 {
1302   const char **type = &first_global_object_name;
1303 
1304   if (first_global_object_name
1305       || !TREE_PUBLIC (decl)
1306       || DECL_EXTERNAL (decl)
1307       || !DECL_NAME (decl)
1308       || (TREE_CODE (decl) != FUNCTION_DECL
1309 	  && (TREE_CODE (decl) != VAR_DECL
1310 	      || (DECL_COMMON (decl)
1311 		  && (DECL_INITIAL (decl) == 0
1312 		      || DECL_INITIAL (decl) == error_mark_node))))
1313       || !MEM_P (DECL_RTL (decl)))
1314     return;
1315 
1316   /* We win when global object is found, but it is useful to know about weak
1317      symbol as well so we can produce nicer unique names.  */
1318   if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl))
1319     type = &weak_global_object_name;
1320 
1321   if (!*type)
1322     {
1323       const char *p;
1324       const char *name;
1325       rtx decl_rtl = DECL_RTL (decl);
1326 
1327       p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1328       name = ggc_strdup (p);
1329 
1330       *type = name;
1331     }
1332 }
1333 
1334 /* Output assembler code for the constant pool of a function and associated
1335    with defining the name of the function.  DECL describes the function.
1336    NAME is the function's name.  For the constant pool, we use the current
1337    constant pool data.  */
1338 
1339 void
assemble_start_function(tree decl,const char * fnname)1340 assemble_start_function (tree decl, const char *fnname)
1341 {
1342   int align;
1343   char tmp_label[100];
1344   bool hot_label_written = false;
1345 
1346   cfun->unlikely_text_section_name = NULL;
1347 
1348   first_function_block_is_cold = false;
1349   if (flag_reorder_blocks_and_partition)
1350     {
1351       ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1352       cfun->hot_section_label = ggc_strdup (tmp_label);
1353       ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1354       cfun->cold_section_label = ggc_strdup (tmp_label);
1355       ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1356       cfun->hot_section_end_label = ggc_strdup (tmp_label);
1357       ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1358       cfun->cold_section_end_label = ggc_strdup (tmp_label);
1359       const_labelno++;
1360     }
1361   else
1362     {
1363       cfun->hot_section_label = NULL;
1364       cfun->cold_section_label = NULL;
1365       cfun->hot_section_end_label = NULL;
1366       cfun->cold_section_end_label = NULL;
1367     }
1368 
1369   /* The following code does not need preprocessing in the assembler.  */
1370 
1371   app_disable ();
1372 
1373   if (CONSTANT_POOL_BEFORE_FUNCTION)
1374     output_constant_pool (fnname, decl);
1375 
1376   resolve_unique_section (decl, 0, flag_function_sections);
1377 
1378   /* Make sure the not and cold text (code) sections are properly
1379      aligned.  This is necessary here in the case where the function
1380      has both hot and cold sections, because we don't want to re-set
1381      the alignment when the section switch happens mid-function.  */
1382 
1383   if (flag_reorder_blocks_and_partition)
1384     {
1385       switch_to_section (unlikely_text_section ());
1386       assemble_align (DECL_ALIGN (decl));
1387       ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_label);
1388 
1389       /* When the function starts with a cold section, we need to explicitly
1390 	 align the hot section and write out the hot section label.
1391 	 But if the current function is a thunk, we do not have a CFG.  */
1392       if (!current_function_is_thunk
1393 	  && BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1394 	{
1395 	  switch_to_section (text_section);
1396 	  assemble_align (DECL_ALIGN (decl));
1397 	  ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1398 	  hot_label_written = true;
1399 	  first_function_block_is_cold = true;
1400 	}
1401     }
1402   else if (DECL_SECTION_NAME (decl))
1403     {
1404       /* Calls to function_section rely on first_function_block_is_cold
1405 	 being accurate.  The first block may be cold even if we aren't
1406 	 doing partitioning, if the entire function was decided by
1407 	 choose_function_section (predict.c) to be cold.  */
1408 
1409       initialize_cold_section_name ();
1410 
1411       if (cfun->unlikely_text_section_name
1412 	  && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
1413 		     cfun->unlikely_text_section_name) == 0)
1414 	first_function_block_is_cold = true;
1415     }
1416 
1417   in_cold_section_p = first_function_block_is_cold;
1418 
1419   /* Switch to the correct text section for the start of the function.  */
1420 
1421   switch_to_section (function_section (decl));
1422   if (flag_reorder_blocks_and_partition
1423       && !hot_label_written)
1424     ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1425 
1426   /* Tell assembler to move to target machine's alignment for functions.  */
1427   align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1428   if (align > 0)
1429     {
1430       ASM_OUTPUT_ALIGN (asm_out_file, align);
1431     }
1432 
1433   /* Handle a user-specified function alignment.
1434      Note that we still need to align to DECL_ALIGN, as above,
1435      because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all.  */
1436   if (! DECL_USER_ALIGN (decl)
1437       && align_functions_log > align
1438       && cfun->function_frequency != FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1439     {
1440 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1441       ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1442 				 align_functions_log, align_functions - 1);
1443 #else
1444       ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1445 #endif
1446     }
1447 
1448 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1449   ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1450 #endif
1451 
1452   (*debug_hooks->begin_function) (decl);
1453 
1454   /* Make function name accessible from other files, if appropriate.  */
1455 
1456   if (TREE_PUBLIC (decl))
1457     {
1458       notice_global_symbol (decl);
1459 
1460       globalize_decl (decl);
1461 
1462       maybe_assemble_visibility (decl);
1463     }
1464 
1465   if (DECL_PRESERVE_P (decl))
1466     targetm.asm_out.mark_decl_preserved (fnname);
1467 
1468   /* Do any machine/system dependent processing of the function name.  */
1469 #ifdef ASM_DECLARE_FUNCTION_NAME
1470   ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1471 #else
1472   /* Standard thing is just output label for the function.  */
1473   ASM_OUTPUT_LABEL (asm_out_file, fnname);
1474 #endif /* ASM_DECLARE_FUNCTION_NAME */
1475 }
1476 
1477 /* Output assembler code associated with defining the size of the
1478    function.  DECL describes the function.  NAME is the function's name.  */
1479 
1480 void
assemble_end_function(tree decl,const char * fnname ATTRIBUTE_UNUSED)1481 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1482 {
1483 #ifdef ASM_DECLARE_FUNCTION_SIZE
1484   /* We could have switched section in the middle of the function.  */
1485   if (flag_reorder_blocks_and_partition)
1486     switch_to_section (function_section (decl));
1487   ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1488 #endif
1489   if (! CONSTANT_POOL_BEFORE_FUNCTION)
1490     {
1491       output_constant_pool (fnname, decl);
1492       switch_to_section (function_section (decl)); /* need to switch back */
1493     }
1494   /* Output labels for end of hot/cold text sections (to be used by
1495      debug info.)  */
1496   if (flag_reorder_blocks_and_partition)
1497     {
1498       section *save_text_section;
1499 
1500       save_text_section = in_section;
1501       switch_to_section (unlikely_text_section ());
1502       ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_end_label);
1503       if (first_function_block_is_cold)
1504 	switch_to_section (text_section);
1505       else
1506 	switch_to_section (function_section (decl));
1507       ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_end_label);
1508       switch_to_section (save_text_section);
1509     }
1510 }
1511 
1512 /* Assemble code to leave SIZE bytes of zeros.  */
1513 
1514 void
assemble_zeros(unsigned HOST_WIDE_INT size)1515 assemble_zeros (unsigned HOST_WIDE_INT size)
1516 {
1517   /* Do no output if -fsyntax-only.  */
1518   if (flag_syntax_only)
1519     return;
1520 
1521 #ifdef ASM_NO_SKIP_IN_TEXT
1522   /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1523      so we must output 0s explicitly in the text section.  */
1524   if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1525     {
1526       unsigned HOST_WIDE_INT i;
1527       for (i = 0; i < size; i++)
1528 	assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1529     }
1530   else
1531 #endif
1532     if (size > 0)
1533       ASM_OUTPUT_SKIP (asm_out_file, size);
1534 }
1535 
1536 /* Assemble an alignment pseudo op for an ALIGN-bit boundary.  */
1537 
1538 void
assemble_align(int align)1539 assemble_align (int align)
1540 {
1541   if (align > BITS_PER_UNIT)
1542     {
1543       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1544     }
1545 }
1546 
1547 /* Assemble a string constant with the specified C string as contents.  */
1548 
1549 void
assemble_string(const char * p,int size)1550 assemble_string (const char *p, int size)
1551 {
1552   int pos = 0;
1553   int maximum = 2000;
1554 
1555   /* If the string is very long, split it up.  */
1556 
1557   while (pos < size)
1558     {
1559       int thissize = size - pos;
1560       if (thissize > maximum)
1561 	thissize = maximum;
1562 
1563       ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1564 
1565       pos += thissize;
1566       p += thissize;
1567     }
1568 }
1569 
1570 
1571 /* A noswitch_section_callback for lcomm_section.  */
1572 
1573 static bool
emit_local(tree decl ATTRIBUTE_UNUSED,const char * name ATTRIBUTE_UNUSED,unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)1574 emit_local (tree decl ATTRIBUTE_UNUSED,
1575 	    const char *name ATTRIBUTE_UNUSED,
1576 	    unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1577 	    unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1578 {
1579 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1580   ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1581 				 size, DECL_ALIGN (decl));
1582   return true;
1583 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1584   ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1585   return true;
1586 #else
1587   ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1588   return false;
1589 #endif
1590 }
1591 
1592 /* A noswitch_section_callback for bss_noswitch_section.  */
1593 
1594 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
1595 static bool
emit_bss(tree decl ATTRIBUTE_UNUSED,const char * name ATTRIBUTE_UNUSED,unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)1596 emit_bss (tree decl ATTRIBUTE_UNUSED,
1597 	  const char *name ATTRIBUTE_UNUSED,
1598 	  unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1599 	  unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1600 {
1601 #if defined ASM_OUTPUT_ALIGNED_BSS
1602   ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl));
1603   return true;
1604 #else
1605   ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded);
1606   return false;
1607 #endif
1608 }
1609 #endif
1610 
1611 /* A noswitch_section_callback for comm_section.  */
1612 
1613 static bool
emit_common(tree decl ATTRIBUTE_UNUSED,const char * name ATTRIBUTE_UNUSED,unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)1614 emit_common (tree decl ATTRIBUTE_UNUSED,
1615 	     const char *name ATTRIBUTE_UNUSED,
1616 	     unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1617 	     unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1618 {
1619 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1620   ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1621 				  size, DECL_ALIGN (decl));
1622   return true;
1623 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1624   ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl));
1625   return true;
1626 #else
1627   ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1628   return false;
1629 #endif
1630 }
1631 
1632 /* A noswitch_section_callback for tls_comm_section.  */
1633 
1634 static bool
emit_tls_common(tree decl ATTRIBUTE_UNUSED,const char * name ATTRIBUTE_UNUSED,unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)1635 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1636 		 const char *name ATTRIBUTE_UNUSED,
1637 		 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1638 		 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1639 {
1640 #ifdef ASM_OUTPUT_TLS_COMMON
1641   ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1642   return true;
1643 #else
1644   sorry ("thread-local COMMON data not implemented");
1645   return true;
1646 #endif
1647 }
1648 
1649 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1650    NAME is the name of DECL's SYMBOL_REF.  */
1651 
1652 static void
assemble_noswitch_variable(tree decl,const char * name,section * sect)1653 assemble_noswitch_variable (tree decl, const char *name, section *sect)
1654 {
1655   unsigned HOST_WIDE_INT size, rounded;
1656 
1657   size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1658   rounded = size;
1659 
1660   /* Don't allocate zero bytes of common,
1661      since that means "undefined external" in the linker.  */
1662   if (size == 0)
1663     rounded = 1;
1664 
1665   /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1666      so that each uninitialized object starts on such a boundary.  */
1667   rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1668   rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1669 	     * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1670 
1671   if (!sect->noswitch.callback (decl, name, size, rounded)
1672       && (unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1673     warning (0, "requested alignment for %q+D is greater than "
1674 	     "implemented alignment of %wu", decl, rounded);
1675 }
1676 
1677 /* A subroutine of assemble_variable.  Output the label and contents of
1678    DECL, whose address is a SYMBOL_REF with name NAME.  DONT_OUTPUT_DATA
1679    is as for assemble_variable.  */
1680 
1681 static void
assemble_variable_contents(tree decl,const char * name,bool dont_output_data)1682 assemble_variable_contents (tree decl, const char *name,
1683 			    bool dont_output_data)
1684 {
1685   /* Do any machine/system dependent processing of the object.  */
1686 #ifdef ASM_DECLARE_OBJECT_NAME
1687   last_assemble_variable_decl = decl;
1688   ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1689 #else
1690   /* Standard thing is just output label for the object.  */
1691   ASM_OUTPUT_LABEL (asm_out_file, name);
1692 #endif /* ASM_DECLARE_OBJECT_NAME */
1693 
1694   if (!dont_output_data)
1695     {
1696       if (DECL_INITIAL (decl)
1697 	  && DECL_INITIAL (decl) != error_mark_node
1698 	  && !initializer_zerop (DECL_INITIAL (decl)))
1699 	/* Output the actual data.  */
1700 	output_constant (DECL_INITIAL (decl),
1701 			 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1702 			 DECL_ALIGN (decl));
1703       else
1704 	/* Leave space for it.  */
1705 	assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1706     }
1707 }
1708 
1709 /* Assemble everything that is needed for a variable or function declaration.
1710    Not used for automatic variables, and not used for function definitions.
1711    Should not be called for variables of incomplete structure type.
1712 
1713    TOP_LEVEL is nonzero if this variable has file scope.
1714    AT_END is nonzero if this is the special handling, at end of compilation,
1715    to define things that have had only tentative definitions.
1716    DONT_OUTPUT_DATA if nonzero means don't actually output the
1717    initial value (that will be done by the caller).  */
1718 
1719 void
assemble_variable(tree decl,int top_level ATTRIBUTE_UNUSED,int at_end ATTRIBUTE_UNUSED,int dont_output_data)1720 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1721 		   int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1722 {
1723   const char *name;
1724   rtx decl_rtl, symbol;
1725   section *sect;
1726 
1727   if (lang_hooks.decls.prepare_assemble_variable)
1728     lang_hooks.decls.prepare_assemble_variable (decl);
1729 
1730   last_assemble_variable_decl = 0;
1731 
1732   /* Normally no need to say anything here for external references,
1733      since assemble_external is called by the language-specific code
1734      when a declaration is first seen.  */
1735 
1736   if (DECL_EXTERNAL (decl))
1737     return;
1738 
1739   /* Output no assembler code for a function declaration.
1740      Only definitions of functions output anything.  */
1741 
1742   if (TREE_CODE (decl) == FUNCTION_DECL)
1743     return;
1744 
1745   /* Do nothing for global register variables.  */
1746   if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1747     {
1748       TREE_ASM_WRITTEN (decl) = 1;
1749       return;
1750     }
1751 
1752   /* If type was incomplete when the variable was declared,
1753      see if it is complete now.  */
1754 
1755   if (DECL_SIZE (decl) == 0)
1756     layout_decl (decl, 0);
1757 
1758   /* Still incomplete => don't allocate it; treat the tentative defn
1759      (which is what it must have been) as an `extern' reference.  */
1760 
1761   if (!dont_output_data && DECL_SIZE (decl) == 0)
1762     {
1763       error ("storage size of %q+D isn%'t known", decl);
1764       TREE_ASM_WRITTEN (decl) = 1;
1765       return;
1766     }
1767 
1768   /* The first declaration of a variable that comes through this function
1769      decides whether it is global (in C, has external linkage)
1770      or local (in C, has internal linkage).  So do nothing more
1771      if this function has already run.  */
1772 
1773   if (TREE_ASM_WRITTEN (decl))
1774     return;
1775 
1776   /* Make sure targetm.encode_section_info is invoked before we set
1777      ASM_WRITTEN.  */
1778   decl_rtl = DECL_RTL (decl);
1779 
1780   TREE_ASM_WRITTEN (decl) = 1;
1781 
1782   /* Do no output if -fsyntax-only.  */
1783   if (flag_syntax_only)
1784     return;
1785 
1786   app_disable ();
1787 
1788   if (! dont_output_data
1789       && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1790     {
1791       error ("size of variable %q+D is too large", decl);
1792       return;
1793     }
1794 
1795   gcc_assert (MEM_P (decl_rtl));
1796   gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
1797   symbol = XEXP (decl_rtl, 0);
1798   name = XSTR (symbol, 0);
1799   if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1800     notice_global_symbol (decl);
1801 
1802   /* Compute the alignment of this data.  */
1803 
1804   align_variable (decl, dont_output_data);
1805   set_mem_align (decl_rtl, DECL_ALIGN (decl));
1806 
1807   if (TREE_PUBLIC (decl))
1808     maybe_assemble_visibility (decl);
1809 
1810   if (DECL_PRESERVE_P (decl))
1811     targetm.asm_out.mark_decl_preserved (name);
1812 
1813   /* First make the assembler name(s) global if appropriate.  */
1814   sect = get_variable_section (decl, false);
1815   if (TREE_PUBLIC (decl)
1816       && DECL_NAME (decl)
1817       && (sect->common.flags & SECTION_COMMON) == 0)
1818     globalize_decl (decl);
1819 
1820   /* Output any data that we will need to use the address of.  */
1821   if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
1822     output_addressed_constants (DECL_INITIAL (decl));
1823 
1824   /* dbxout.c needs to know this.  */
1825   if (sect && (sect->common.flags & SECTION_CODE) != 0)
1826     DECL_IN_TEXT_SECTION (decl) = 1;
1827 
1828   /* If the decl is part of an object_block, make sure that the decl
1829      has been positioned within its block, but do not write out its
1830      definition yet.  output_object_blocks will do that later.  */
1831   if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
1832     {
1833       gcc_assert (!dont_output_data);
1834       place_block_symbol (symbol);
1835     }
1836   else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1837     assemble_noswitch_variable (decl, name, sect);
1838   else
1839     {
1840       switch_to_section (sect);
1841       if (DECL_ALIGN (decl) > BITS_PER_UNIT)
1842 	ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
1843       assemble_variable_contents (decl, name, dont_output_data);
1844     }
1845 }
1846 
1847 /* Return 1 if type TYPE contains any pointers.  */
1848 
1849 static int
contains_pointers_p(tree type)1850 contains_pointers_p (tree type)
1851 {
1852   switch (TREE_CODE (type))
1853     {
1854     case POINTER_TYPE:
1855     case REFERENCE_TYPE:
1856       /* I'm not sure whether OFFSET_TYPE needs this treatment,
1857 	 so I'll play safe and return 1.  */
1858     case OFFSET_TYPE:
1859       return 1;
1860 
1861     case RECORD_TYPE:
1862     case UNION_TYPE:
1863     case QUAL_UNION_TYPE:
1864       {
1865 	tree fields;
1866 	/* For a type that has fields, see if the fields have pointers.  */
1867 	for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
1868 	  if (TREE_CODE (fields) == FIELD_DECL
1869 	      && contains_pointers_p (TREE_TYPE (fields)))
1870 	    return 1;
1871 	return 0;
1872       }
1873 
1874     case ARRAY_TYPE:
1875       /* An array type contains pointers if its element type does.  */
1876       return contains_pointers_p (TREE_TYPE (type));
1877 
1878     default:
1879       return 0;
1880     }
1881 }
1882 
1883 /* In unit-at-a-time mode, we delay assemble_external processing until
1884    the compilation unit is finalized.  This is the best we can do for
1885    right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
1886    it all the way to final.  See PR 17982 for further discussion.  */
1887 static GTY(()) tree pending_assemble_externals;
1888 
1889 #ifdef ASM_OUTPUT_EXTERNAL
1890 /* True if DECL is a function decl for which no out-of-line copy exists.
1891    It is assumed that DECL's assembler name has been set.  */
1892 
1893 static bool
incorporeal_function_p(tree decl)1894 incorporeal_function_p (tree decl)
1895 {
1896   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1897     {
1898       const char *name;
1899 
1900       if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1901 	  && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
1902 	return true;
1903 
1904       name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1905       if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
1906 	return true;
1907     }
1908   return false;
1909 }
1910 
1911 /* Actually do the tests to determine if this is necessary, and invoke
1912    ASM_OUTPUT_EXTERNAL.  */
1913 static void
assemble_external_real(tree decl)1914 assemble_external_real (tree decl)
1915 {
1916   rtx rtl = DECL_RTL (decl);
1917 
1918   if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
1919       && !SYMBOL_REF_USED (XEXP (rtl, 0))
1920       && !incorporeal_function_p (decl))
1921     {
1922       /* Some systems do require some output.  */
1923       SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
1924       ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
1925     }
1926 }
1927 #endif
1928 
1929 void
process_pending_assemble_externals(void)1930 process_pending_assemble_externals (void)
1931 {
1932 #ifdef ASM_OUTPUT_EXTERNAL
1933   tree list;
1934   for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
1935     assemble_external_real (TREE_VALUE (list));
1936 
1937   pending_assemble_externals = 0;
1938 #endif
1939 }
1940 
1941 /* Output something to declare an external symbol to the assembler.
1942    (Most assemblers don't need this, so we normally output nothing.)
1943    Do nothing if DECL is not external.  */
1944 
1945 void
assemble_external(tree decl ATTRIBUTE_UNUSED)1946 assemble_external (tree decl ATTRIBUTE_UNUSED)
1947 {
1948   /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
1949      main body of this code is only rarely exercised.  To provide some
1950      testing, on all platforms, we make sure that the ASM_OUT_FILE is
1951      open.  If it's not, we should not be calling this function.  */
1952   gcc_assert (asm_out_file);
1953 
1954 #ifdef ASM_OUTPUT_EXTERNAL
1955   if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
1956     return;
1957 
1958   /* We want to output external symbols at very last to check if they
1959      are references or not.  */
1960   pending_assemble_externals = tree_cons (0, decl,
1961 					  pending_assemble_externals);
1962 #endif
1963 }
1964 
1965 /* Similar, for calling a library function FUN.  */
1966 
1967 void
assemble_external_libcall(rtx fun)1968 assemble_external_libcall (rtx fun)
1969 {
1970   /* Declare library function name external when first used, if nec.  */
1971   if (! SYMBOL_REF_USED (fun))
1972     {
1973       SYMBOL_REF_USED (fun) = 1;
1974       targetm.asm_out.external_libcall (fun);
1975     }
1976 }
1977 
1978 /* Assemble a label named NAME.  */
1979 
1980 void
assemble_label(const char * name)1981 assemble_label (const char *name)
1982 {
1983   ASM_OUTPUT_LABEL (asm_out_file, name);
1984 }
1985 
1986 /* Set the symbol_referenced flag for ID.  */
1987 void
mark_referenced(tree id)1988 mark_referenced (tree id)
1989 {
1990   TREE_SYMBOL_REFERENCED (id) = 1;
1991 }
1992 
1993 /* Set the symbol_referenced flag for DECL and notify callgraph.  */
1994 void
mark_decl_referenced(tree decl)1995 mark_decl_referenced (tree decl)
1996 {
1997   if (TREE_CODE (decl) == FUNCTION_DECL)
1998     {
1999       /* Extern inline functions don't become needed when referenced.
2000 	 If we know a method will be emitted in other TU and no new
2001 	 functions can be marked reachable, just use the external
2002 	 definition.  */
2003       struct cgraph_node *node = cgraph_node (decl);
2004       if (!DECL_EXTERNAL (decl)
2005 	  && (!node->local.vtable_method || !cgraph_global_info_ready
2006 	      || !node->local.finalized))
2007 	cgraph_mark_needed_node (node);
2008     }
2009   else if (TREE_CODE (decl) == VAR_DECL)
2010     {
2011       struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
2012       cgraph_varpool_mark_needed_node (node);
2013       /* C++ frontend use mark_decl_references to force COMDAT variables
2014          to be output that might appear dead otherwise.  */
2015       node->force_output = true;
2016     }
2017   /* else do nothing - we can get various sorts of CST nodes here,
2018      which do not need to be marked.  */
2019 }
2020 
2021 
2022 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2023    until we find an identifier that is not itself a transparent alias.
2024    Modify the alias passed to it by reference (and all aliases on the
2025    way to the ultimate target), such that they do not have to be
2026    followed again, and return the ultimate target of the alias
2027    chain.  */
2028 
2029 static inline tree
ultimate_transparent_alias_target(tree * alias)2030 ultimate_transparent_alias_target (tree *alias)
2031 {
2032   tree target = *alias;
2033 
2034   if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2035     {
2036       gcc_assert (TREE_CHAIN (target));
2037       target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2038       gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2039 		  && ! TREE_CHAIN (target));
2040       *alias = target;
2041     }
2042 
2043   return target;
2044 }
2045 
2046 /* Output to FILE (an assembly file) a reference to NAME.  If NAME
2047    starts with a *, the rest of NAME is output verbatim.  Otherwise
2048    NAME is transformed in a target-specific way (usually by the
2049    addition of an underscore).  */
2050 
2051 void
assemble_name_raw(FILE * file,const char * name)2052 assemble_name_raw (FILE *file, const char *name)
2053 {
2054   if (name[0] == '*')
2055     fputs (&name[1], file);
2056   else
2057     ASM_OUTPUT_LABELREF (file, name);
2058 }
2059 
2060 /* Like assemble_name_raw, but should be used when NAME might refer to
2061    an entity that is also represented as a tree (like a function or
2062    variable).  If NAME does refer to such an entity, that entity will
2063    be marked as referenced.  */
2064 
2065 void
assemble_name(FILE * file,const char * name)2066 assemble_name (FILE *file, const char *name)
2067 {
2068   const char *real_name;
2069   tree id;
2070 
2071   real_name = targetm.strip_name_encoding (name);
2072 
2073   id = maybe_get_identifier (real_name);
2074   if (id)
2075     {
2076       tree id_orig = id;
2077 
2078       mark_referenced (id);
2079       ultimate_transparent_alias_target (&id);
2080       if (id != id_orig)
2081 	name = IDENTIFIER_POINTER (id);
2082       gcc_assert (! TREE_CHAIN (id));
2083     }
2084 
2085   assemble_name_raw (file, name);
2086 }
2087 
2088 /* Allocate SIZE bytes writable static space with a gensym name
2089    and return an RTX to refer to its address.  */
2090 
2091 rtx
assemble_static_space(unsigned HOST_WIDE_INT size)2092 assemble_static_space (unsigned HOST_WIDE_INT size)
2093 {
2094   char name[12];
2095   const char *namestring;
2096   rtx x;
2097 
2098   ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2099   ++const_labelno;
2100   namestring = ggc_strdup (name);
2101 
2102   x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2103   SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2104 
2105 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2106   ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2107 				 BIGGEST_ALIGNMENT);
2108 #else
2109 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2110   ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2111 #else
2112   {
2113     /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2114        so that each uninitialized object starts on such a boundary.  */
2115     /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL.  */
2116     unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2117       = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2118 	 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2119 	 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2120     ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2121   }
2122 #endif
2123 #endif
2124   return x;
2125 }
2126 
2127 /* Assemble the static constant template for function entry trampolines.
2128    This is done at most once per compilation.
2129    Returns an RTX for the address of the template.  */
2130 
2131 static GTY(()) rtx initial_trampoline;
2132 
2133 #ifdef TRAMPOLINE_TEMPLATE
2134 rtx
assemble_trampoline_template(void)2135 assemble_trampoline_template (void)
2136 {
2137   char label[256];
2138   const char *name;
2139   int align;
2140   rtx symbol;
2141 
2142   if (initial_trampoline)
2143     return initial_trampoline;
2144 
2145   /* By default, put trampoline templates in read-only data section.  */
2146 
2147 #ifdef TRAMPOLINE_SECTION
2148   switch_to_section (TRAMPOLINE_SECTION);
2149 #else
2150   switch_to_section (readonly_data_section);
2151 #endif
2152 
2153   /* Write the assembler code to define one.  */
2154   align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2155   if (align > 0)
2156     {
2157       ASM_OUTPUT_ALIGN (asm_out_file, align);
2158     }
2159 
2160   targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2161   TRAMPOLINE_TEMPLATE (asm_out_file);
2162 
2163   /* Record the rtl to refer to it.  */
2164   ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2165   name = ggc_strdup (label);
2166   symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2167   SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2168 
2169   initial_trampoline = gen_rtx_MEM (BLKmode, symbol);
2170   set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2171 
2172   return initial_trampoline;
2173 }
2174 #endif
2175 
2176 /* A and B are either alignments or offsets.  Return the minimum alignment
2177    that may be assumed after adding the two together.  */
2178 
2179 static inline unsigned
min_align(unsigned int a,unsigned int b)2180 min_align (unsigned int a, unsigned int b)
2181 {
2182   return (a | b) & -(a | b);
2183 }
2184 
2185 /* Return the assembler directive for creating a given kind of integer
2186    object.  SIZE is the number of bytes in the object and ALIGNED_P
2187    indicates whether it is known to be aligned.  Return NULL if the
2188    assembly dialect has no such directive.
2189 
2190    The returned string should be printed at the start of a new line and
2191    be followed immediately by the object's initial value.  */
2192 
2193 const char *
integer_asm_op(int size,int aligned_p)2194 integer_asm_op (int size, int aligned_p)
2195 {
2196   struct asm_int_op *ops;
2197 
2198   if (aligned_p)
2199     ops = &targetm.asm_out.aligned_op;
2200   else
2201     ops = &targetm.asm_out.unaligned_op;
2202 
2203   switch (size)
2204     {
2205     case 1:
2206       return targetm.asm_out.byte_op;
2207     case 2:
2208       return ops->hi;
2209     case 4:
2210       return ops->si;
2211     case 8:
2212       return ops->di;
2213     case 16:
2214       return ops->ti;
2215     default:
2216       return NULL;
2217     }
2218 }
2219 
2220 /* Use directive OP to assemble an integer object X.  Print OP at the
2221    start of the line, followed immediately by the value of X.  */
2222 
2223 void
assemble_integer_with_op(const char * op,rtx x)2224 assemble_integer_with_op (const char *op, rtx x)
2225 {
2226   fputs (op, asm_out_file);
2227   output_addr_const (asm_out_file, x);
2228   fputc ('\n', asm_out_file);
2229 }
2230 
2231 /* The default implementation of the asm_out.integer target hook.  */
2232 
2233 bool
default_assemble_integer(rtx x ATTRIBUTE_UNUSED,unsigned int size ATTRIBUTE_UNUSED,int aligned_p ATTRIBUTE_UNUSED)2234 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2235 			  unsigned int size ATTRIBUTE_UNUSED,
2236 			  int aligned_p ATTRIBUTE_UNUSED)
2237 {
2238   const char *op = integer_asm_op (size, aligned_p);
2239   /* Avoid GAS bugs for large values.  Specifically negative values whose
2240      absolute value fits in a bfd_vma, but not in a bfd_signed_vma.  */
2241   if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2242     return false;
2243   return op && (assemble_integer_with_op (op, x), true);
2244 }
2245 
2246 /* Assemble the integer constant X into an object of SIZE bytes.  ALIGN is
2247    the alignment of the integer in bits.  Return 1 if we were able to output
2248    the constant, otherwise 0.  We must be able to output the constant,
2249    if FORCE is nonzero.  */
2250 
2251 bool
assemble_integer(rtx x,unsigned int size,unsigned int align,int force)2252 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2253 {
2254   int aligned_p;
2255 
2256   aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2257 
2258   /* See if the target hook can handle this kind of object.  */
2259   if (targetm.asm_out.integer (x, size, aligned_p))
2260     return true;
2261 
2262   /* If the object is a multi-byte one, try splitting it up.  Split
2263      it into words it if is multi-word, otherwise split it into bytes.  */
2264   if (size > 1)
2265     {
2266       enum machine_mode omode, imode;
2267       unsigned int subalign;
2268       unsigned int subsize, i;
2269 
2270       subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2271       subalign = MIN (align, subsize * BITS_PER_UNIT);
2272       omode = mode_for_size (subsize * BITS_PER_UNIT, MODE_INT, 0);
2273       imode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2274 
2275       for (i = 0; i < size; i += subsize)
2276 	{
2277 	  rtx partial = simplify_subreg (omode, x, imode, i);
2278 	  if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2279 	    break;
2280 	}
2281       if (i == size)
2282 	return true;
2283 
2284       /* If we've printed some of it, but not all of it, there's no going
2285 	 back now.  */
2286       gcc_assert (!i);
2287     }
2288 
2289   gcc_assert (!force);
2290 
2291   return false;
2292 }
2293 
2294 void
assemble_real(REAL_VALUE_TYPE d,enum machine_mode mode,unsigned int align)2295 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2296 {
2297   long data[4] = {0, 0, 0, 0};
2298   int i;
2299   int bitsize, nelts, nunits, units_per;
2300 
2301   /* This is hairy.  We have a quantity of known size.  real_to_target
2302      will put it into an array of *host* longs, 32 bits per element
2303      (even if long is more than 32 bits).  We need to determine the
2304      number of array elements that are occupied (nelts) and the number
2305      of *target* min-addressable units that will be occupied in the
2306      object file (nunits).  We cannot assume that 32 divides the
2307      mode's bitsize (size * BITS_PER_UNIT) evenly.
2308 
2309      size * BITS_PER_UNIT is used here to make sure that padding bits
2310      (which might appear at either end of the value; real_to_target
2311      will include the padding bits in its output array) are included.  */
2312 
2313   nunits = GET_MODE_SIZE (mode);
2314   bitsize = nunits * BITS_PER_UNIT;
2315   nelts = CEIL (bitsize, 32);
2316   units_per = 32 / BITS_PER_UNIT;
2317 
2318   real_to_target (data, &d, mode);
2319 
2320   /* Put out the first word with the specified alignment.  */
2321   assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2322   nunits -= units_per;
2323 
2324   /* Subsequent words need only 32-bit alignment.  */
2325   align = min_align (align, 32);
2326 
2327   for (i = 1; i < nelts; i++)
2328     {
2329       assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2330       nunits -= units_per;
2331     }
2332 }
2333 
2334 /* Given an expression EXP with a constant value,
2335    reduce it to the sum of an assembler symbol and an integer.
2336    Store them both in the structure *VALUE.
2337    EXP must be reducible.  */
2338 
2339 struct addr_const GTY(())
2340 {
2341   rtx base;
2342   HOST_WIDE_INT offset;
2343 };
2344 
2345 static void
decode_addr_const(tree exp,struct addr_const * value)2346 decode_addr_const (tree exp, struct addr_const *value)
2347 {
2348   tree target = TREE_OPERAND (exp, 0);
2349   int offset = 0;
2350   rtx x;
2351 
2352   while (1)
2353     {
2354       if (TREE_CODE (target) == COMPONENT_REF
2355 	  && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2356 
2357 	{
2358 	  offset += int_byte_position (TREE_OPERAND (target, 1));
2359 	  target = TREE_OPERAND (target, 0);
2360 	}
2361       else if (TREE_CODE (target) == ARRAY_REF
2362 	       || TREE_CODE (target) == ARRAY_RANGE_REF)
2363 	{
2364 	  offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2365 		     * tree_low_cst (TREE_OPERAND (target, 1), 0));
2366 	  target = TREE_OPERAND (target, 0);
2367 	}
2368       else
2369 	break;
2370     }
2371 
2372   switch (TREE_CODE (target))
2373     {
2374     case VAR_DECL:
2375     case FUNCTION_DECL:
2376       x = DECL_RTL (target);
2377       break;
2378 
2379     case LABEL_DECL:
2380       x = gen_rtx_MEM (FUNCTION_MODE,
2381 		       gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2382       break;
2383 
2384     case REAL_CST:
2385     case STRING_CST:
2386     case COMPLEX_CST:
2387     case CONSTRUCTOR:
2388     case INTEGER_CST:
2389       x = output_constant_def (target, 1);
2390       break;
2391 
2392     default:
2393       gcc_unreachable ();
2394     }
2395 
2396   gcc_assert (MEM_P (x));
2397   x = XEXP (x, 0);
2398 
2399   value->base = x;
2400   value->offset = offset;
2401 }
2402 
2403 /* Uniquize all constants that appear in memory.
2404    Each constant in memory thus far output is recorded
2405    in `const_desc_table'.  */
2406 
2407 struct constant_descriptor_tree GTY(())
2408 {
2409   /* A MEM for the constant.  */
2410   rtx rtl;
2411 
2412   /* The value of the constant.  */
2413   tree value;
2414 
2415   /* Hash of value.  Computing the hash from value each time
2416      hashfn is called can't work properly, as that means recursive
2417      use of the hash table during hash table expansion.  */
2418   hashval_t hash;
2419 };
2420 
2421 static GTY((param_is (struct constant_descriptor_tree)))
2422      htab_t const_desc_htab;
2423 
2424 static struct constant_descriptor_tree * build_constant_desc (tree);
2425 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2426 
2427 /* Compute a hash code for a constant expression.  */
2428 
2429 static hashval_t
const_desc_hash(const void * ptr)2430 const_desc_hash (const void *ptr)
2431 {
2432   return ((struct constant_descriptor_tree *)ptr)->hash;
2433 }
2434 
2435 static hashval_t
const_hash_1(const tree exp)2436 const_hash_1 (const tree exp)
2437 {
2438   const char *p;
2439   hashval_t hi;
2440   int len, i;
2441   enum tree_code code = TREE_CODE (exp);
2442 
2443   /* Either set P and LEN to the address and len of something to hash and
2444      exit the switch or return a value.  */
2445 
2446   switch (code)
2447     {
2448     case INTEGER_CST:
2449       p = (char *) &TREE_INT_CST (exp);
2450       len = sizeof TREE_INT_CST (exp);
2451       break;
2452 
2453     case REAL_CST:
2454       return real_hash (TREE_REAL_CST_PTR (exp));
2455 
2456     case STRING_CST:
2457       p = TREE_STRING_POINTER (exp);
2458       len = TREE_STRING_LENGTH (exp);
2459       break;
2460 
2461     case COMPLEX_CST:
2462       return (const_hash_1 (TREE_REALPART (exp)) * 5
2463 	      + const_hash_1 (TREE_IMAGPART (exp)));
2464 
2465     case CONSTRUCTOR:
2466       {
2467 	unsigned HOST_WIDE_INT idx;
2468 	tree value;
2469 
2470 	hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2471 
2472 	FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2473 	  if (value)
2474 	    hi = hi * 603 + const_hash_1 (value);
2475 
2476 	return hi;
2477       }
2478 
2479     case ADDR_EXPR:
2480     case FDESC_EXPR:
2481       {
2482 	struct addr_const value;
2483 
2484 	decode_addr_const (exp, &value);
2485 	switch (GET_CODE (value.base))
2486 	  {
2487 	  case SYMBOL_REF:
2488 	    /* Don't hash the address of the SYMBOL_REF;
2489 	       only use the offset and the symbol name.  */
2490 	    hi = value.offset;
2491 	    p = XSTR (value.base, 0);
2492 	    for (i = 0; p[i] != 0; i++)
2493 	      hi = ((hi * 613) + (unsigned) (p[i]));
2494 	    break;
2495 
2496 	  case LABEL_REF:
2497 	    hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2498 	    break;
2499 
2500 	  default:
2501 	    gcc_unreachable ();
2502 	  }
2503       }
2504       return hi;
2505 
2506     case PLUS_EXPR:
2507     case MINUS_EXPR:
2508       return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2509 	      + const_hash_1 (TREE_OPERAND (exp, 1)));
2510 
2511     case NOP_EXPR:
2512     case CONVERT_EXPR:
2513     case NON_LVALUE_EXPR:
2514       return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2515 
2516     default:
2517       /* A language specific constant. Just hash the code.  */
2518       return code;
2519     }
2520 
2521   /* Compute hashing function.  */
2522   hi = len;
2523   for (i = 0; i < len; i++)
2524     hi = ((hi * 613) + (unsigned) (p[i]));
2525 
2526   return hi;
2527 }
2528 
2529 /* Wrapper of compare_constant, for the htab interface.  */
2530 static int
const_desc_eq(const void * p1,const void * p2)2531 const_desc_eq (const void *p1, const void *p2)
2532 {
2533   const struct constant_descriptor_tree *c1 = p1;
2534   const struct constant_descriptor_tree *c2 = p2;
2535   if (c1->hash != c2->hash)
2536     return 0;
2537   return compare_constant (c1->value, c2->value);
2538 }
2539 
2540 /* Compare t1 and t2, and return 1 only if they are known to result in
2541    the same bit pattern on output.  */
2542 
2543 static int
compare_constant(const tree t1,const tree t2)2544 compare_constant (const tree t1, const tree t2)
2545 {
2546   enum tree_code typecode;
2547 
2548   if (t1 == NULL_TREE)
2549     return t2 == NULL_TREE;
2550   if (t2 == NULL_TREE)
2551     return 0;
2552 
2553   if (TREE_CODE (t1) != TREE_CODE (t2))
2554     return 0;
2555 
2556   switch (TREE_CODE (t1))
2557     {
2558     case INTEGER_CST:
2559       /* Integer constants are the same only if the same width of type.  */
2560       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2561 	return 0;
2562       if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2563 	return 0;
2564       return tree_int_cst_equal (t1, t2);
2565 
2566     case REAL_CST:
2567       /* Real constants are the same only if the same width of type.  */
2568       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2569 	return 0;
2570 
2571       return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2572 
2573     case STRING_CST:
2574       if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2575 	return 0;
2576 
2577       return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2578 	      && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2579 			 TREE_STRING_LENGTH (t1)));
2580 
2581     case COMPLEX_CST:
2582       return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2583 	      && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2584 
2585     case CONSTRUCTOR:
2586       {
2587 	VEC(constructor_elt, gc) *v1, *v2;
2588 	unsigned HOST_WIDE_INT idx;
2589 
2590 	typecode = TREE_CODE (TREE_TYPE (t1));
2591 	if (typecode != TREE_CODE (TREE_TYPE (t2)))
2592 	  return 0;
2593 
2594 	if (typecode == ARRAY_TYPE)
2595 	  {
2596 	    HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2597 	    /* For arrays, check that the sizes all match.  */
2598 	    if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2599 		|| size_1 == -1
2600 		|| size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2601 	      return 0;
2602 	  }
2603 	else
2604 	  {
2605 	    /* For record and union constructors, require exact type
2606                equality.  */
2607 	    if (TREE_TYPE (t1) != TREE_TYPE (t2))
2608 	      return 0;
2609 	  }
2610 
2611 	v1 = CONSTRUCTOR_ELTS (t1);
2612 	v2 = CONSTRUCTOR_ELTS (t2);
2613 	if (VEC_length (constructor_elt, v1)
2614 	    != VEC_length (constructor_elt, v2))
2615 	    return 0;
2616 
2617 	for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
2618 	  {
2619 	    constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
2620 	    constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
2621 
2622 	    /* Check that each value is the same...  */
2623 	    if (!compare_constant (c1->value, c2->value))
2624 	      return 0;
2625 	    /* ... and that they apply to the same fields!  */
2626 	    if (typecode == ARRAY_TYPE)
2627 	      {
2628 		if (!compare_constant (c1->index, c2->index))
2629 		  return 0;
2630 	      }
2631 	    else
2632 	      {
2633 		if (c1->index != c2->index)
2634 		  return 0;
2635 	      }
2636 	  }
2637 
2638 	return 1;
2639       }
2640 
2641     case ADDR_EXPR:
2642     case FDESC_EXPR:
2643       {
2644 	struct addr_const value1, value2;
2645 
2646 	decode_addr_const (t1, &value1);
2647 	decode_addr_const (t2, &value2);
2648 	return (value1.offset == value2.offset
2649 		&& strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2650       }
2651 
2652     case PLUS_EXPR:
2653     case MINUS_EXPR:
2654     case RANGE_EXPR:
2655       return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2656 	      && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2657 
2658     case NOP_EXPR:
2659     case CONVERT_EXPR:
2660     case NON_LVALUE_EXPR:
2661     case VIEW_CONVERT_EXPR:
2662       return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2663 
2664     default:
2665       {
2666 	tree nt1, nt2;
2667 	nt1 = lang_hooks.expand_constant (t1);
2668 	nt2 = lang_hooks.expand_constant (t2);
2669 	if (nt1 != t1 || nt2 != t2)
2670 	  return compare_constant (nt1, nt2);
2671 	else
2672 	  return 0;
2673       }
2674     }
2675 
2676   gcc_unreachable ();
2677 }
2678 
2679 /* Make a copy of the whole tree structure for a constant.  This
2680    handles the same types of nodes that compare_constant handles.  */
2681 
2682 static tree
copy_constant(tree exp)2683 copy_constant (tree exp)
2684 {
2685   switch (TREE_CODE (exp))
2686     {
2687     case ADDR_EXPR:
2688       /* For ADDR_EXPR, we do not want to copy the decl whose address
2689 	 is requested.  We do want to copy constants though.  */
2690       if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
2691 	return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2692 		       copy_constant (TREE_OPERAND (exp, 0)));
2693       else
2694 	return copy_node (exp);
2695 
2696     case INTEGER_CST:
2697     case REAL_CST:
2698     case STRING_CST:
2699       return copy_node (exp);
2700 
2701     case COMPLEX_CST:
2702       return build_complex (TREE_TYPE (exp),
2703 			    copy_constant (TREE_REALPART (exp)),
2704 			    copy_constant (TREE_IMAGPART (exp)));
2705 
2706     case PLUS_EXPR:
2707     case MINUS_EXPR:
2708       return build2 (TREE_CODE (exp), TREE_TYPE (exp),
2709 		     copy_constant (TREE_OPERAND (exp, 0)),
2710 		     copy_constant (TREE_OPERAND (exp, 1)));
2711 
2712     case NOP_EXPR:
2713     case CONVERT_EXPR:
2714     case NON_LVALUE_EXPR:
2715     case VIEW_CONVERT_EXPR:
2716       return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2717 		     copy_constant (TREE_OPERAND (exp, 0)));
2718 
2719     case CONSTRUCTOR:
2720       {
2721 	tree copy = copy_node (exp);
2722 	VEC(constructor_elt, gc) *v;
2723 	unsigned HOST_WIDE_INT idx;
2724 	tree purpose, value;
2725 
2726 	v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
2727 						      CONSTRUCTOR_ELTS (exp)));
2728 	FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
2729 	  {
2730 	    constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
2731 	    ce->index = purpose;
2732 	    ce->value = copy_constant (value);
2733 	  }
2734 	CONSTRUCTOR_ELTS (copy) = v;
2735 	return copy;
2736       }
2737 
2738     default:
2739       {
2740 	tree t = lang_hooks.expand_constant (exp);
2741 
2742 	gcc_assert (t != exp);
2743 	return copy_constant (t);
2744       }
2745     }
2746 }
2747 
2748 /* Return the alignment of constant EXP in bits.  */
2749 
2750 static unsigned int
get_constant_alignment(tree exp)2751 get_constant_alignment (tree exp)
2752 {
2753   unsigned int align;
2754 
2755   align = TYPE_ALIGN (TREE_TYPE (exp));
2756 #ifdef CONSTANT_ALIGNMENT
2757   align = CONSTANT_ALIGNMENT (exp, align);
2758 #endif
2759   return align;
2760 }
2761 
2762 /* Return the section into which constant EXP should be placed.  */
2763 
2764 static section *
get_constant_section(tree exp)2765 get_constant_section (tree exp)
2766 {
2767   if (IN_NAMED_SECTION (exp))
2768     return get_named_section (exp, NULL, compute_reloc_for_constant (exp));
2769   else
2770     return targetm.asm_out.select_section (exp,
2771 					   compute_reloc_for_constant (exp),
2772 					   get_constant_alignment (exp));
2773 }
2774 
2775 /* Return the size of constant EXP in bytes.  */
2776 
2777 static HOST_WIDE_INT
get_constant_size(tree exp)2778 get_constant_size (tree exp)
2779 {
2780   HOST_WIDE_INT size;
2781 
2782   size = int_size_in_bytes (TREE_TYPE (exp));
2783   if (TREE_CODE (exp) == STRING_CST)
2784     size = MAX (TREE_STRING_LENGTH (exp), size);
2785   return size;
2786 }
2787 
2788 /* Subroutine of output_constant_def:
2789    No constant equal to EXP is known to have been output.
2790    Make a constant descriptor to enter EXP in the hash table.
2791    Assign the label number and construct RTL to refer to the
2792    constant's location in memory.
2793    Caller is responsible for updating the hash table.  */
2794 
2795 static struct constant_descriptor_tree *
build_constant_desc(tree exp)2796 build_constant_desc (tree exp)
2797 {
2798   rtx symbol;
2799   rtx rtl;
2800   char label[256];
2801   int labelno;
2802   struct constant_descriptor_tree *desc;
2803 
2804   desc = ggc_alloc (sizeof (*desc));
2805   desc->value = copy_constant (exp);
2806 
2807   /* Propagate marked-ness to copied constant.  */
2808   if (flag_mudflap && mf_marked_p (exp))
2809     mf_mark (desc->value);
2810 
2811   /* Create a string containing the label name, in LABEL.  */
2812   labelno = const_labelno++;
2813   ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
2814 
2815   /* We have a symbol name; construct the SYMBOL_REF and the MEM.  */
2816   if (use_object_blocks_p ())
2817     {
2818       section *sect = get_constant_section (exp);
2819       symbol = create_block_symbol (ggc_strdup (label),
2820 				    get_block_for_section (sect), -1);
2821     }
2822   else
2823     symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2824   SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
2825   SET_SYMBOL_REF_DECL (symbol, desc->value);
2826   TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2827 
2828   rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
2829   set_mem_attributes (rtl, exp, 1);
2830   set_mem_alias_set (rtl, 0);
2831   set_mem_alias_set (rtl, const_alias_set);
2832 
2833   /* Set flags or add text to the name to record information, such as
2834      that it is a local symbol.  If the name is changed, the macro
2835      ASM_OUTPUT_LABELREF will have to know how to strip this
2836      information.  This call might invalidate our local variable
2837      SYMBOL; we can't use it afterward.  */
2838 
2839   targetm.encode_section_info (exp, rtl, true);
2840 
2841   desc->rtl = rtl;
2842 
2843   return desc;
2844 }
2845 
2846 /* Return an rtx representing a reference to constant data in memory
2847    for the constant expression EXP.
2848 
2849    If assembler code for such a constant has already been output,
2850    return an rtx to refer to it.
2851    Otherwise, output such a constant in memory
2852    and generate an rtx for it.
2853 
2854    If DEFER is nonzero, this constant can be deferred and output only
2855    if referenced in the function after all optimizations.
2856 
2857    `const_desc_table' records which constants already have label strings.  */
2858 
2859 rtx
output_constant_def(tree exp,int defer)2860 output_constant_def (tree exp, int defer)
2861 {
2862   struct constant_descriptor_tree *desc;
2863   struct constant_descriptor_tree key;
2864   void **loc;
2865 
2866   /* Look up EXP in the table of constant descriptors.  If we didn't find
2867      it, create a new one.  */
2868   key.value = exp;
2869   key.hash = const_hash_1 (exp);
2870   loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
2871 
2872   desc = *loc;
2873   if (desc == 0)
2874     {
2875       desc = build_constant_desc (exp);
2876       desc->hash = key.hash;
2877       *loc = desc;
2878     }
2879 
2880   maybe_output_constant_def_contents (desc, defer);
2881   return desc->rtl;
2882 }
2883 
2884 /* Subroutine of output_constant_def: Decide whether or not we need to
2885    output the constant DESC now, and if so, do it.  */
2886 static void
maybe_output_constant_def_contents(struct constant_descriptor_tree * desc,int defer)2887 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
2888 				    int defer)
2889 {
2890   rtx symbol = XEXP (desc->rtl, 0);
2891   tree exp = desc->value;
2892 
2893   if (flag_syntax_only)
2894     return;
2895 
2896   if (TREE_ASM_WRITTEN (exp))
2897     /* Already output; don't do it again.  */
2898     return;
2899 
2900   /* We can always defer constants as long as the context allows
2901      doing so.  */
2902   if (defer)
2903     {
2904       /* Increment n_deferred_constants if it exists.  It needs to be at
2905 	 least as large as the number of constants actually referred to
2906 	 by the function.  If it's too small we'll stop looking too early
2907 	 and fail to emit constants; if it's too large we'll only look
2908 	 through the entire function when we could have stopped earlier.  */
2909       if (cfun)
2910 	n_deferred_constants++;
2911       return;
2912     }
2913 
2914   output_constant_def_contents (symbol);
2915 }
2916 
2917 /* Subroutine of output_constant_def_contents.  Output the definition
2918    of constant EXP, which is pointed to by label LABEL.  ALIGN is the
2919    constant's alignment in bits.  */
2920 
2921 static void
assemble_constant_contents(tree exp,const char * label,unsigned int align)2922 assemble_constant_contents (tree exp, const char *label, unsigned int align)
2923 {
2924   HOST_WIDE_INT size;
2925 
2926   size = get_constant_size (exp);
2927 
2928   /* Do any machine/system dependent processing of the constant.  */
2929 #ifdef ASM_DECLARE_CONSTANT_NAME
2930   ASM_DECLARE_CONSTANT_NAME (asm_out_file, label, exp, size);
2931 #else
2932   /* Standard thing is just output label for the constant.  */
2933   ASM_OUTPUT_LABEL (asm_out_file, label);
2934 #endif /* ASM_DECLARE_CONSTANT_NAME */
2935 
2936   /* Output the value of EXP.  */
2937   output_constant (exp, size, align);
2938 }
2939 
2940 /* We must output the constant data referred to by SYMBOL; do so.  */
2941 
2942 static void
output_constant_def_contents(rtx symbol)2943 output_constant_def_contents (rtx symbol)
2944 {
2945   tree exp = SYMBOL_REF_DECL (symbol);
2946   unsigned int align;
2947 
2948   /* Make sure any other constants whose addresses appear in EXP
2949      are assigned label numbers.  */
2950   output_addressed_constants (exp);
2951 
2952   /* We are no longer deferring this constant.  */
2953   TREE_ASM_WRITTEN (exp) = 1;
2954 
2955   /* If the constant is part of an object block, make sure that the
2956      decl has been positioned within its block, but do not write out
2957      its definition yet.  output_object_blocks will do that later.  */
2958   if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2959     place_block_symbol (symbol);
2960   else
2961     {
2962       switch_to_section (get_constant_section (exp));
2963       align = get_constant_alignment (exp);
2964       if (align > BITS_PER_UNIT)
2965 	ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2966       assemble_constant_contents (exp, XSTR (symbol, 0), align);
2967     }
2968   if (flag_mudflap)
2969     mudflap_enqueue_constant (exp);
2970 }
2971 
2972 /* Look up EXP in the table of constant descriptors.  Return the rtl
2973    if it has been emitted, else null.  */
2974 
2975 rtx
lookup_constant_def(tree exp)2976 lookup_constant_def (tree exp)
2977 {
2978   struct constant_descriptor_tree *desc;
2979   struct constant_descriptor_tree key;
2980 
2981   key.value = exp;
2982   key.hash = const_hash_1 (exp);
2983   desc = htab_find_with_hash (const_desc_htab, &key, key.hash);
2984 
2985   return (desc ? desc->rtl : NULL_RTX);
2986 }
2987 
2988 /* Used in the hash tables to avoid outputting the same constant
2989    twice.  Unlike 'struct constant_descriptor_tree', RTX constants
2990    are output once per function, not once per file.  */
2991 /* ??? Only a few targets need per-function constant pools.  Most
2992    can use one per-file pool.  Should add a targetm bit to tell the
2993    difference.  */
2994 
2995 struct rtx_constant_pool GTY(())
2996 {
2997   /* Pointers to first and last constant in pool, as ordered by offset.  */
2998   struct constant_descriptor_rtx *first;
2999   struct constant_descriptor_rtx *last;
3000 
3001   /* Hash facility for making memory-constants from constant rtl-expressions.
3002      It is used on RISC machines where immediate integer arguments and
3003      constant addresses are restricted so that such constants must be stored
3004      in memory.  */
3005   htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3006 
3007   /* Current offset in constant pool (does not include any
3008      machine-specific header).  */
3009   HOST_WIDE_INT offset;
3010 };
3011 
3012 struct constant_descriptor_rtx GTY((chain_next ("%h.next")))
3013 {
3014   struct constant_descriptor_rtx *next;
3015   rtx mem;
3016   rtx sym;
3017   rtx constant;
3018   HOST_WIDE_INT offset;
3019   hashval_t hash;
3020   enum machine_mode mode;
3021   unsigned int align;
3022   int labelno;
3023   int mark;
3024 };
3025 
3026 /* Hash and compare functions for const_rtx_htab.  */
3027 
3028 static hashval_t
const_desc_rtx_hash(const void * ptr)3029 const_desc_rtx_hash (const void *ptr)
3030 {
3031   const struct constant_descriptor_rtx *desc = ptr;
3032   return desc->hash;
3033 }
3034 
3035 static int
const_desc_rtx_eq(const void * a,const void * b)3036 const_desc_rtx_eq (const void *a, const void *b)
3037 {
3038   const struct constant_descriptor_rtx *x = a;
3039   const struct constant_descriptor_rtx *y = b;
3040 
3041   if (x->mode != y->mode)
3042     return 0;
3043   return rtx_equal_p (x->constant, y->constant);
3044 }
3045 
3046 /* This is the worker function for const_rtx_hash, called via for_each_rtx.  */
3047 
3048 static int
const_rtx_hash_1(rtx * xp,void * data)3049 const_rtx_hash_1 (rtx *xp, void *data)
3050 {
3051   unsigned HOST_WIDE_INT hwi;
3052   enum machine_mode mode;
3053   enum rtx_code code;
3054   hashval_t h, *hp;
3055   rtx x;
3056 
3057   x = *xp;
3058   code = GET_CODE (x);
3059   mode = GET_MODE (x);
3060   h = (hashval_t) code * 1048573 + mode;
3061 
3062   switch (code)
3063     {
3064     case CONST_INT:
3065       hwi = INTVAL (x);
3066     fold_hwi:
3067       {
3068 	const int shift = sizeof (hashval_t) * CHAR_BIT;
3069 	const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3070 	int i;
3071 
3072 	h ^= (hashval_t) hwi;
3073 	for (i = 1; i < n; ++i)
3074 	  {
3075 	    hwi >>= shift;
3076 	    h ^= (hashval_t) hwi;
3077 	  }
3078       }
3079       break;
3080 
3081     case CONST_DOUBLE:
3082       if (mode == VOIDmode)
3083 	{
3084 	  hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3085 	  goto fold_hwi;
3086 	}
3087       else
3088 	h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3089       break;
3090 
3091     case CONST_VECTOR:
3092       {
3093 	int i;
3094 	for (i = XVECLEN (x, 0); i-- > 0; )
3095 	  h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3096       }
3097       break;
3098 
3099     case SYMBOL_REF:
3100       h ^= htab_hash_string (XSTR (x, 0));
3101       break;
3102 
3103     case LABEL_REF:
3104       h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3105       break;
3106 
3107     case UNSPEC:
3108     case UNSPEC_VOLATILE:
3109       h = h * 251 + XINT (x, 1);
3110       break;
3111 
3112     default:
3113       break;
3114     }
3115 
3116   hp = data;
3117   *hp = *hp * 509 + h;
3118   return 0;
3119 }
3120 
3121 /* Compute a hash value for X, which should be a constant.  */
3122 
3123 static hashval_t
const_rtx_hash(rtx x)3124 const_rtx_hash (rtx x)
3125 {
3126   hashval_t h = 0;
3127   for_each_rtx (&x, const_rtx_hash_1, &h);
3128   return h;
3129 }
3130 
3131 
3132 /* Create and return a new rtx constant pool.  */
3133 
3134 static struct rtx_constant_pool *
create_constant_pool(void)3135 create_constant_pool (void)
3136 {
3137   struct rtx_constant_pool *pool;
3138 
3139   pool = ggc_alloc (sizeof (struct rtx_constant_pool));
3140   pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3141 					  const_desc_rtx_eq, NULL);
3142   pool->first = NULL;
3143   pool->last = NULL;
3144   pool->offset = 0;
3145   return pool;
3146 }
3147 
3148 /* Initialize constant pool hashing for a new function.  */
3149 
3150 void
init_varasm_status(struct function * f)3151 init_varasm_status (struct function *f)
3152 {
3153   struct varasm_status *p;
3154 
3155   p = ggc_alloc (sizeof (struct varasm_status));
3156   f->varasm = p;
3157 
3158   p->pool = create_constant_pool ();
3159   p->deferred_constants = 0;
3160 }
3161 
3162 /* Given a MINUS expression, simplify it if both sides
3163    include the same symbol.  */
3164 
3165 rtx
simplify_subtraction(rtx x)3166 simplify_subtraction (rtx x)
3167 {
3168   rtx r = simplify_rtx (x);
3169   return r ? r : x;
3170 }
3171 
3172 /* Given a constant rtx X, make (or find) a memory constant for its value
3173    and return a MEM rtx to refer to it in memory.  */
3174 
3175 rtx
force_const_mem(enum machine_mode mode,rtx x)3176 force_const_mem (enum machine_mode mode, rtx x)
3177 {
3178   struct constant_descriptor_rtx *desc, tmp;
3179   struct rtx_constant_pool *pool;
3180   char label[256];
3181   rtx def, symbol;
3182   hashval_t hash;
3183   unsigned int align;
3184   void **slot;
3185 
3186   /* If we're not allowed to drop X into the constant pool, don't.  */
3187   if (targetm.cannot_force_const_mem (x))
3188     return NULL_RTX;
3189 
3190   /* Record that this function has used a constant pool entry.  */
3191   current_function_uses_const_pool = 1;
3192 
3193   /* Decide which pool to use.  */
3194   pool = (targetm.use_blocks_for_constant_p (mode, x)
3195 	  ? shared_constant_pool
3196 	  : cfun->varasm->pool);
3197 
3198   /* Lookup the value in the hashtable.  */
3199   tmp.constant = x;
3200   tmp.mode = mode;
3201   hash = const_rtx_hash (x);
3202   slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3203   desc = *slot;
3204 
3205   /* If the constant was already present, return its memory.  */
3206   if (desc)
3207     return copy_rtx (desc->mem);
3208 
3209   /* Otherwise, create a new descriptor.  */
3210   desc = ggc_alloc (sizeof (*desc));
3211   *slot = desc;
3212 
3213   /* Align the location counter as required by EXP's data type.  */
3214   align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3215 #ifdef CONSTANT_ALIGNMENT
3216   {
3217     tree type = lang_hooks.types.type_for_mode (mode, 0);
3218     if (type != NULL_TREE)
3219       align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3220   }
3221 #endif
3222 
3223   pool->offset += (align / BITS_PER_UNIT) - 1;
3224   pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3225 
3226   desc->next = NULL;
3227   desc->constant = tmp.constant;
3228   desc->offset = pool->offset;
3229   desc->hash = hash;
3230   desc->mode = mode;
3231   desc->align = align;
3232   desc->labelno = const_labelno;
3233   desc->mark = 0;
3234 
3235   pool->offset += GET_MODE_SIZE (mode);
3236   if (pool->last)
3237     pool->last->next = desc;
3238   else
3239     pool->first = pool->last = desc;
3240   pool->last = desc;
3241 
3242   /* Create a string containing the label name, in LABEL.  */
3243   ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3244   ++const_labelno;
3245 
3246   /* Construct the SYMBOL_REF.  Make sure to mark it as belonging to
3247      the constants pool.  */
3248   if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3249     {
3250       section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3251       symbol = create_block_symbol (ggc_strdup (label),
3252 				    get_block_for_section (sect), -1);
3253     }
3254   else
3255     symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3256   desc->sym = symbol;
3257   SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3258   CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3259   SET_SYMBOL_REF_CONSTANT (symbol, desc);
3260 
3261   /* Construct the MEM.  */
3262   desc->mem = def = gen_const_mem (mode, symbol);
3263   set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3264   set_mem_align (def, align);
3265 
3266   /* If we're dropping a label to the constant pool, make sure we
3267      don't delete it.  */
3268   if (GET_CODE (x) == LABEL_REF)
3269     LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3270 
3271   return copy_rtx (def);
3272 }
3273 
3274 /* Given a constant pool SYMBOL_REF, return the corresponding constant.  */
3275 
3276 rtx
get_pool_constant(rtx addr)3277 get_pool_constant (rtx addr)
3278 {
3279   return SYMBOL_REF_CONSTANT (addr)->constant;
3280 }
3281 
3282 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3283    and whether it has been output or not.  */
3284 
3285 rtx
get_pool_constant_mark(rtx addr,bool * pmarked)3286 get_pool_constant_mark (rtx addr, bool *pmarked)
3287 {
3288   struct constant_descriptor_rtx *desc;
3289 
3290   desc = SYMBOL_REF_CONSTANT (addr);
3291   *pmarked = (desc->mark != 0);
3292   return desc->constant;
3293 }
3294 
3295 /* Similar, return the mode.  */
3296 
3297 enum machine_mode
get_pool_mode(rtx addr)3298 get_pool_mode (rtx addr)
3299 {
3300   return SYMBOL_REF_CONSTANT (addr)->mode;
3301 }
3302 
3303 /* Return the size of the constant pool.  */
3304 
3305 int
get_pool_size(void)3306 get_pool_size (void)
3307 {
3308   return cfun->varasm->pool->offset;
3309 }
3310 
3311 /* Worker function for output_constant_pool_1.  Emit assembly for X
3312    in MODE with known alignment ALIGN.  */
3313 
3314 static void
output_constant_pool_2(enum machine_mode mode,rtx x,unsigned int align)3315 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3316 {
3317   switch (GET_MODE_CLASS (mode))
3318     {
3319     case MODE_FLOAT:
3320     case MODE_DECIMAL_FLOAT:
3321       {
3322 	REAL_VALUE_TYPE r;
3323 
3324 	gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3325 	REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3326 	assemble_real (r, mode, align);
3327 	break;
3328       }
3329 
3330     case MODE_INT:
3331     case MODE_PARTIAL_INT:
3332       assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3333       break;
3334 
3335     case MODE_VECTOR_FLOAT:
3336     case MODE_VECTOR_INT:
3337       {
3338 	int i, units;
3339         enum machine_mode submode = GET_MODE_INNER (mode);
3340 	unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3341 
3342 	gcc_assert (GET_CODE (x) == CONST_VECTOR);
3343 	units = CONST_VECTOR_NUNITS (x);
3344 
3345 	for (i = 0; i < units; i++)
3346 	  {
3347 	    rtx elt = CONST_VECTOR_ELT (x, i);
3348 	    output_constant_pool_2 (submode, elt, i ? subalign : align);
3349 	  }
3350       }
3351       break;
3352 
3353     default:
3354       gcc_unreachable ();
3355     }
3356 }
3357 
3358 /* Worker function for output_constant_pool.  Emit constant DESC,
3359    giving it ALIGN bits of alignment.  */
3360 
3361 static void
output_constant_pool_1(struct constant_descriptor_rtx * desc,unsigned int align)3362 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3363 			unsigned int align)
3364 {
3365   rtx x, tmp;
3366 
3367   x = desc->constant;
3368 
3369   /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3370      whose CODE_LABEL has been deleted.  This can occur if a jump table
3371      is eliminated by optimization.  If so, write a constant of zero
3372      instead.  Note that this can also happen by turning the
3373      CODE_LABEL into a NOTE.  */
3374   /* ??? This seems completely and utterly wrong.  Certainly it's
3375      not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3376      functioning even with INSN_DELETED_P and friends.  */
3377 
3378   tmp = x;
3379   switch (GET_CODE (x))
3380     {
3381     case CONST:
3382       if (GET_CODE (XEXP (x, 0)) != PLUS
3383 	  || GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
3384 	break;
3385       tmp = XEXP (XEXP (x, 0), 0);
3386       /* FALLTHRU  */
3387 
3388     case LABEL_REF:
3389       tmp = XEXP (x, 0);
3390       gcc_assert (!INSN_DELETED_P (tmp));
3391       gcc_assert (!NOTE_P (tmp)
3392 		  || NOTE_LINE_NUMBER (tmp) != NOTE_INSN_DELETED);
3393       break;
3394 
3395     default:
3396       break;
3397     }
3398 
3399 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3400   ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3401 				 align, desc->labelno, done);
3402 #endif
3403 
3404   assemble_align (align);
3405 
3406   /* Output the label.  */
3407   targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3408 
3409   /* Output the data.  */
3410   output_constant_pool_2 (desc->mode, x, align);
3411 
3412   /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3413      sections have proper size.  */
3414   if (align > GET_MODE_BITSIZE (desc->mode)
3415       && in_section
3416       && (in_section->common.flags & SECTION_MERGE))
3417     assemble_align (align);
3418 
3419 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3420  done:
3421 #endif
3422   return;
3423 }
3424 
3425 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3426    to as used.  Emit referenced deferred strings.  This function can
3427    be used with for_each_rtx to mark all SYMBOL_REFs in an rtx.  */
3428 
3429 static int
mark_constant(rtx * current_rtx,void * data ATTRIBUTE_UNUSED)3430 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3431 {
3432   rtx x = *current_rtx;
3433 
3434   if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3435     return 0;
3436 
3437   if (CONSTANT_POOL_ADDRESS_P (x))
3438     {
3439       struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3440       if (desc->mark == 0)
3441 	{
3442 	  desc->mark = 1;
3443 	  for_each_rtx (&desc->constant, mark_constant, NULL);
3444 	}
3445     }
3446   else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3447     {
3448       tree exp = SYMBOL_REF_DECL (x);
3449       if (!TREE_ASM_WRITTEN (exp))
3450 	{
3451 	  n_deferred_constants--;
3452 	  output_constant_def_contents (x);
3453 	}
3454     }
3455 
3456   return -1;
3457 }
3458 
3459 /* Look through appropriate parts of INSN, marking all entries in the
3460    constant pool which are actually being used.  Entries that are only
3461    referenced by other constants are also marked as used.  Emit
3462    deferred strings that are used.  */
3463 
3464 static void
mark_constants(rtx insn)3465 mark_constants (rtx insn)
3466 {
3467   if (!INSN_P (insn))
3468     return;
3469 
3470   /* Insns may appear inside a SEQUENCE.  Only check the patterns of
3471      insns, not any notes that may be attached.  We don't want to mark
3472      a constant just because it happens to appear in a REG_EQUIV note.  */
3473   if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3474     {
3475       rtx seq = PATTERN (insn);
3476       int i, n = XVECLEN (seq, 0);
3477       for (i = 0; i < n; ++i)
3478 	{
3479 	  rtx subinsn = XVECEXP (seq, 0, i);
3480 	  if (INSN_P (subinsn))
3481 	    for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
3482 	}
3483     }
3484   else
3485     for_each_rtx (&PATTERN (insn), mark_constant, NULL);
3486 }
3487 
3488 /* Look through the instructions for this function, and mark all the
3489    entries in POOL which are actually being used.  Emit deferred constants
3490    which have indeed been used.  */
3491 
3492 static void
mark_constant_pool(void)3493 mark_constant_pool (void)
3494 {
3495   rtx insn, link;
3496 
3497   if (!current_function_uses_const_pool && n_deferred_constants == 0)
3498     return;
3499 
3500   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3501     mark_constants (insn);
3502 
3503   for (link = current_function_epilogue_delay_list;
3504        link;
3505        link = XEXP (link, 1))
3506     mark_constants (XEXP (link, 0));
3507 }
3508 
3509 /* Write all the constants in POOL.  */
3510 
3511 static void
output_constant_pool_contents(struct rtx_constant_pool * pool)3512 output_constant_pool_contents (struct rtx_constant_pool *pool)
3513 {
3514   struct constant_descriptor_rtx *desc;
3515 
3516   for (desc = pool->first; desc ; desc = desc->next)
3517     if (desc->mark)
3518       {
3519 	/* If the constant is part of an object_block, make sure that
3520 	   the constant has been positioned within its block, but do not
3521 	   write out its definition yet.  output_object_blocks will do
3522 	   that later.  */
3523 	if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3524 	    && SYMBOL_REF_BLOCK (desc->sym))
3525 	  place_block_symbol (desc->sym);
3526 	else
3527 	  {
3528 	    switch_to_section (targetm.asm_out.select_rtx_section
3529 			       (desc->mode, desc->constant, desc->align));
3530 	    output_constant_pool_1 (desc, desc->align);
3531 	  }
3532       }
3533 }
3534 
3535 /* Mark all constants that are used in the current function, then write
3536    out the function's private constant pool.  */
3537 
3538 static void
output_constant_pool(const char * fnname ATTRIBUTE_UNUSED,tree fndecl ATTRIBUTE_UNUSED)3539 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3540 		      tree fndecl ATTRIBUTE_UNUSED)
3541 {
3542   struct rtx_constant_pool *pool = cfun->varasm->pool;
3543 
3544   /* It is possible for gcc to call force_const_mem and then to later
3545      discard the instructions which refer to the constant.  In such a
3546      case we do not need to output the constant.  */
3547   mark_constant_pool ();
3548 
3549 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3550   ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3551 #endif
3552 
3553   output_constant_pool_contents (pool);
3554 
3555 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3556   ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3557 #endif
3558 }
3559 
3560 /* Write the contents of the shared constant pool.  */
3561 
3562 void
output_shared_constant_pool(void)3563 output_shared_constant_pool (void)
3564 {
3565   output_constant_pool_contents (shared_constant_pool);
3566 }
3567 
3568 /* Determine what kind of relocations EXP may need.  */
3569 
3570 int
compute_reloc_for_constant(tree exp)3571 compute_reloc_for_constant (tree exp)
3572 {
3573   int reloc = 0, reloc2;
3574   tree tem;
3575 
3576   /* Give the front-end a chance to convert VALUE to something that
3577      looks more like a constant to the back-end.  */
3578   exp = lang_hooks.expand_constant (exp);
3579 
3580   switch (TREE_CODE (exp))
3581     {
3582     case ADDR_EXPR:
3583     case FDESC_EXPR:
3584       /* Go inside any operations that get_inner_reference can handle and see
3585 	 if what's inside is a constant: no need to do anything here for
3586 	 addresses of variables or functions.  */
3587       for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3588 	   tem = TREE_OPERAND (tem, 0))
3589 	;
3590 
3591       if (TREE_PUBLIC (tem))
3592 	reloc |= 2;
3593       else
3594 	reloc |= 1;
3595       break;
3596 
3597     case PLUS_EXPR:
3598       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3599       reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3600       break;
3601 
3602     case MINUS_EXPR:
3603       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3604       reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3605       /* The difference of two local labels is computable at link time.  */
3606       if (reloc == 1 && reloc2 == 1)
3607 	reloc = 0;
3608       else
3609 	reloc |= reloc2;
3610       break;
3611 
3612     case NOP_EXPR:
3613     case CONVERT_EXPR:
3614     case NON_LVALUE_EXPR:
3615     case VIEW_CONVERT_EXPR:
3616       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3617       break;
3618 
3619     case CONSTRUCTOR:
3620       {
3621 	unsigned HOST_WIDE_INT idx;
3622 	FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3623 	  if (tem != 0)
3624 	    reloc |= compute_reloc_for_constant (tem);
3625       }
3626       break;
3627 
3628     default:
3629       break;
3630     }
3631   return reloc;
3632 }
3633 
3634 /* Find all the constants whose addresses are referenced inside of EXP,
3635    and make sure assembler code with a label has been output for each one.
3636    Indicate whether an ADDR_EXPR has been encountered.  */
3637 
3638 static void
output_addressed_constants(tree exp)3639 output_addressed_constants (tree exp)
3640 {
3641   tree tem;
3642 
3643   /* Give the front-end a chance to convert VALUE to something that
3644      looks more like a constant to the back-end.  */
3645   exp = lang_hooks.expand_constant (exp);
3646 
3647   switch (TREE_CODE (exp))
3648     {
3649     case ADDR_EXPR:
3650     case FDESC_EXPR:
3651       /* Go inside any operations that get_inner_reference can handle and see
3652 	 if what's inside is a constant: no need to do anything here for
3653 	 addresses of variables or functions.  */
3654       for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3655 	   tem = TREE_OPERAND (tem, 0))
3656 	;
3657 
3658       /* If we have an initialized CONST_DECL, retrieve the initializer.  */
3659       if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
3660 	tem = DECL_INITIAL (tem);
3661 
3662       if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3663 	output_constant_def (tem, 0);
3664       break;
3665 
3666     case PLUS_EXPR:
3667     case MINUS_EXPR:
3668       output_addressed_constants (TREE_OPERAND (exp, 1));
3669       /* Fall through.  */
3670 
3671     case NOP_EXPR:
3672     case CONVERT_EXPR:
3673     case NON_LVALUE_EXPR:
3674     case VIEW_CONVERT_EXPR:
3675       output_addressed_constants (TREE_OPERAND (exp, 0));
3676       break;
3677 
3678     case CONSTRUCTOR:
3679       {
3680 	unsigned HOST_WIDE_INT idx;
3681 	FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3682 	  if (tem != 0)
3683 	    output_addressed_constants (tem);
3684       }
3685       break;
3686 
3687     default:
3688       break;
3689     }
3690 }
3691 
3692 /* Whether a constructor CTOR is a valid static constant initializer if all
3693    its elements are.  This used to be internal to initializer_constant_valid_p
3694    and has been exposed to let other functions like categorize_ctor_elements
3695    evaluate the property while walking a constructor for other purposes.  */
3696 
3697 bool
constructor_static_from_elts_p(tree ctor)3698 constructor_static_from_elts_p (tree ctor)
3699 {
3700   return (TREE_CONSTANT (ctor)
3701 	  && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
3702 	      || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE)
3703 	  && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (ctor)));
3704 }
3705 
3706 /* Return nonzero if VALUE is a valid constant-valued expression
3707    for use in initializing a static variable; one that can be an
3708    element of a "constant" initializer.
3709 
3710    Return null_pointer_node if the value is absolute;
3711    if it is relocatable, return the variable that determines the relocation.
3712    We assume that VALUE has been folded as much as possible;
3713    therefore, we do not need to check for such things as
3714    arithmetic-combinations of integers.  */
3715 
3716 tree
initializer_constant_valid_p(tree value,tree endtype)3717 initializer_constant_valid_p (tree value, tree endtype)
3718 {
3719   /* Give the front-end a chance to convert VALUE to something that
3720      looks more like a constant to the back-end.  */
3721   value = lang_hooks.expand_constant (value);
3722 
3723   switch (TREE_CODE (value))
3724     {
3725     case CONSTRUCTOR:
3726       if (constructor_static_from_elts_p (value))
3727 	{
3728 	  unsigned HOST_WIDE_INT idx;
3729 	  tree elt;
3730 	  bool absolute = true;
3731 
3732 	  FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
3733 	    {
3734 	      tree reloc;
3735 	      reloc = initializer_constant_valid_p (elt, TREE_TYPE (elt));
3736 	      if (!reloc)
3737 		return NULL_TREE;
3738 	      if (reloc != null_pointer_node)
3739 		absolute = false;
3740 	    }
3741 	  /* For a non-absolute relocation, there is no single
3742 	     variable that can be "the variable that determines the
3743 	     relocation."  */
3744 	  return absolute ? null_pointer_node : error_mark_node;
3745 	}
3746 
3747       return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
3748 
3749     case INTEGER_CST:
3750     case VECTOR_CST:
3751     case REAL_CST:
3752     case STRING_CST:
3753     case COMPLEX_CST:
3754       return null_pointer_node;
3755 
3756     case ADDR_EXPR:
3757     case FDESC_EXPR:
3758       value = staticp (TREE_OPERAND (value, 0));
3759       if (value)
3760 	{
3761 	  /* "&(*a).f" is like unto pointer arithmetic.  If "a" turns out to
3762 	     be a constant, this is old-skool offsetof-like nonsense.  */
3763 	  if (TREE_CODE (value) == INDIRECT_REF
3764 	      && TREE_CONSTANT (TREE_OPERAND (value, 0)))
3765 	    return null_pointer_node;
3766 	  /* Taking the address of a nested function involves a trampoline.  */
3767 	  if (TREE_CODE (value) == FUNCTION_DECL
3768 	      && ((decl_function_context (value)
3769 		   && !DECL_NO_STATIC_CHAIN (value))
3770 		  || DECL_DLLIMPORT_P (value)))
3771 	    return NULL_TREE;
3772 	  /* "&{...}" requires a temporary to hold the constructed
3773 	     object.  */
3774 	  if (TREE_CODE (value) == CONSTRUCTOR)
3775 	    return NULL_TREE;
3776 	}
3777       return value;
3778 
3779     case VIEW_CONVERT_EXPR:
3780     case NON_LVALUE_EXPR:
3781       return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3782 
3783     case CONVERT_EXPR:
3784     case NOP_EXPR:
3785       {
3786 	tree src;
3787 	tree src_type;
3788 	tree dest_type;
3789 
3790 	src = TREE_OPERAND (value, 0);
3791 	src_type = TREE_TYPE (src);
3792 	dest_type = TREE_TYPE (value);
3793 
3794 	/* Allow conversions between pointer types, floating-point
3795 	   types, and offset types.  */
3796 	if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
3797 	    || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
3798 	    || (TREE_CODE (dest_type) == OFFSET_TYPE
3799 		&& TREE_CODE (src_type) == OFFSET_TYPE))
3800 	  return initializer_constant_valid_p (src, endtype);
3801 
3802 	/* Allow length-preserving conversions between integer types.  */
3803 	if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
3804 	    && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
3805 	  return initializer_constant_valid_p (src, endtype);
3806 
3807 	/* Allow conversions between other integer types only if
3808 	   explicit value.  */
3809 	if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
3810 	  {
3811 	    tree inner = initializer_constant_valid_p (src, endtype);
3812 	    if (inner == null_pointer_node)
3813 	      return null_pointer_node;
3814 	    break;
3815 	  }
3816 
3817 	/* Allow (int) &foo provided int is as wide as a pointer.  */
3818 	if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
3819 	    && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
3820 	  return initializer_constant_valid_p (src, endtype);
3821 
3822 	/* Likewise conversions from int to pointers, but also allow
3823 	   conversions from 0.  */
3824 	if ((POINTER_TYPE_P (dest_type)
3825 	     || TREE_CODE (dest_type) == OFFSET_TYPE)
3826 	    && INTEGRAL_TYPE_P (src_type))
3827 	  {
3828 	    if (TREE_CODE (src) == INTEGER_CST
3829 		&& TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
3830 	      return null_pointer_node;
3831 	    if (integer_zerop (src))
3832 	      return null_pointer_node;
3833 	    else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
3834 	      return initializer_constant_valid_p (src, endtype);
3835 	  }
3836 
3837 	/* Allow conversions to struct or union types if the value
3838 	   inside is okay.  */
3839 	if (TREE_CODE (dest_type) == RECORD_TYPE
3840 	    || TREE_CODE (dest_type) == UNION_TYPE)
3841 	  return initializer_constant_valid_p (src, endtype);
3842       }
3843       break;
3844 
3845     case PLUS_EXPR:
3846       if (! INTEGRAL_TYPE_P (endtype)
3847 	  || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3848 	{
3849 	  tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3850 						      endtype);
3851 	  tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3852 						      endtype);
3853 	  /* If either term is absolute, use the other terms relocation.  */
3854 	  if (valid0 == null_pointer_node)
3855 	    return valid1;
3856 	  if (valid1 == null_pointer_node)
3857 	    return valid0;
3858 	}
3859       break;
3860 
3861     case MINUS_EXPR:
3862       if (! INTEGRAL_TYPE_P (endtype)
3863 	  || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3864 	{
3865 	  tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3866 						      endtype);
3867 	  tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3868 						      endtype);
3869 	  /* Win if second argument is absolute.  */
3870 	  if (valid1 == null_pointer_node)
3871 	    return valid0;
3872 	  /* Win if both arguments have the same relocation.
3873 	     Then the value is absolute.  */
3874 	  if (valid0 == valid1 && valid0 != 0)
3875 	    return null_pointer_node;
3876 
3877 	  /* Since GCC guarantees that string constants are unique in the
3878 	     generated code, a subtraction between two copies of the same
3879 	     constant string is absolute.  */
3880 	  if (valid0 && TREE_CODE (valid0) == STRING_CST
3881 	      && valid1 && TREE_CODE (valid1) == STRING_CST
3882 	      && operand_equal_p (valid0, valid1, 1))
3883 	    return null_pointer_node;
3884 	}
3885 
3886       /* Support narrowing differences.  */
3887       if (INTEGRAL_TYPE_P (endtype))
3888 	{
3889 	  tree op0, op1;
3890 
3891 	  op0 = TREE_OPERAND (value, 0);
3892 	  op1 = TREE_OPERAND (value, 1);
3893 
3894 	  /* Like STRIP_NOPS except allow the operand mode to widen.
3895 	     This works around a feature of fold that simplifies
3896 	     (int)(p1 - p2) to ((int)p1 - (int)p2) under the theory
3897 	     that the narrower operation is cheaper.  */
3898 
3899 	  while (TREE_CODE (op0) == NOP_EXPR
3900 		 || TREE_CODE (op0) == CONVERT_EXPR
3901 		 || TREE_CODE (op0) == NON_LVALUE_EXPR)
3902 	    {
3903 	      tree inner = TREE_OPERAND (op0, 0);
3904 	      if (inner == error_mark_node
3905 	          || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3906 		  || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
3907 		      > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3908 		break;
3909 	      op0 = inner;
3910 	    }
3911 
3912 	  while (TREE_CODE (op1) == NOP_EXPR
3913 		 || TREE_CODE (op1) == CONVERT_EXPR
3914 		 || TREE_CODE (op1) == NON_LVALUE_EXPR)
3915 	    {
3916 	      tree inner = TREE_OPERAND (op1, 0);
3917 	      if (inner == error_mark_node
3918 	          || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3919 		  || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
3920 		      > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3921 		break;
3922 	      op1 = inner;
3923 	    }
3924 
3925 	  op0 = initializer_constant_valid_p (op0, endtype);
3926 	  op1 = initializer_constant_valid_p (op1, endtype);
3927 
3928 	  /* Both initializers must be known.  */
3929 	  if (op0 && op1)
3930 	    {
3931 	      if (op0 == op1)
3932 		return null_pointer_node;
3933 
3934 	      /* Support differences between labels.  */
3935 	      if (TREE_CODE (op0) == LABEL_DECL
3936 		  && TREE_CODE (op1) == LABEL_DECL)
3937 		return null_pointer_node;
3938 
3939 	      if (TREE_CODE (op0) == STRING_CST
3940 		  && TREE_CODE (op1) == STRING_CST
3941 		  && operand_equal_p (op0, op1, 1))
3942 		return null_pointer_node;
3943 	    }
3944 	}
3945       break;
3946 
3947     default:
3948       break;
3949     }
3950 
3951   return 0;
3952 }
3953 
3954 /* Output assembler code for constant EXP to FILE, with no label.
3955    This includes the pseudo-op such as ".int" or ".byte", and a newline.
3956    Assumes output_addressed_constants has been done on EXP already.
3957 
3958    Generate exactly SIZE bytes of assembler data, padding at the end
3959    with zeros if necessary.  SIZE must always be specified.
3960 
3961    SIZE is important for structure constructors,
3962    since trailing members may have been omitted from the constructor.
3963    It is also important for initialization of arrays from string constants
3964    since the full length of the string constant might not be wanted.
3965    It is also needed for initialization of unions, where the initializer's
3966    type is just one member, and that may not be as long as the union.
3967 
3968    There a case in which we would fail to output exactly SIZE bytes:
3969    for a structure constructor that wants to produce more than SIZE bytes.
3970    But such constructors will never be generated for any possible input.
3971 
3972    ALIGN is the alignment of the data in bits.  */
3973 
3974 void
output_constant(tree exp,unsigned HOST_WIDE_INT size,unsigned int align)3975 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
3976 {
3977   enum tree_code code;
3978   unsigned HOST_WIDE_INT thissize;
3979 
3980   /* Some front-ends use constants other than the standard language-independent
3981      varieties, but which may still be output directly.  Give the front-end a
3982      chance to convert EXP to a language-independent representation.  */
3983   exp = lang_hooks.expand_constant (exp);
3984 
3985   if (size == 0 || flag_syntax_only)
3986     return;
3987 
3988   /* See if we're trying to initialize a pointer in a non-default mode
3989      to the address of some declaration somewhere.  If the target says
3990      the mode is valid for pointers, assume the target has a way of
3991      resolving it.  */
3992   if (TREE_CODE (exp) == NOP_EXPR
3993       && POINTER_TYPE_P (TREE_TYPE (exp))
3994       && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
3995     {
3996       tree saved_type = TREE_TYPE (exp);
3997 
3998       /* Peel off any intermediate conversions-to-pointer for valid
3999 	 pointer modes.  */
4000       while (TREE_CODE (exp) == NOP_EXPR
4001 	     && POINTER_TYPE_P (TREE_TYPE (exp))
4002 	     && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
4003 	exp = TREE_OPERAND (exp, 0);
4004 
4005       /* If what we're left with is the address of something, we can
4006 	 convert the address to the final type and output it that
4007 	 way.  */
4008       if (TREE_CODE (exp) == ADDR_EXPR)
4009 	exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4010       /* Likewise for constant ints.  */
4011       else if (TREE_CODE (exp) == INTEGER_CST)
4012 	exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4013 				  TREE_INT_CST_HIGH (exp));
4014 
4015     }
4016 
4017   /* Eliminate any conversions since we'll be outputting the underlying
4018      constant.  */
4019   while (TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
4020 	 || TREE_CODE (exp) == NON_LVALUE_EXPR
4021 	 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4022     {
4023       HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4024       HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4025 
4026       /* Make sure eliminating the conversion is really a no-op, except with
4027 	 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4028 	 union types to allow for Ada unchecked unions.  */
4029       if (type_size > op_size
4030 	  && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4031 	  && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4032 	/* Keep the conversion. */
4033 	break;
4034       else
4035 	exp = TREE_OPERAND (exp, 0);
4036     }
4037 
4038   code = TREE_CODE (TREE_TYPE (exp));
4039   thissize = int_size_in_bytes (TREE_TYPE (exp));
4040 
4041   /* Give the front end another chance to expand constants.  */
4042   exp = lang_hooks.expand_constant (exp);
4043 
4044   /* Allow a constructor with no elements for any data type.
4045      This means to fill the space with zeros.  */
4046   if (TREE_CODE (exp) == CONSTRUCTOR
4047       && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
4048     {
4049       assemble_zeros (size);
4050       return;
4051     }
4052 
4053   if (TREE_CODE (exp) == FDESC_EXPR)
4054     {
4055 #ifdef ASM_OUTPUT_FDESC
4056       HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
4057       tree decl = TREE_OPERAND (exp, 0);
4058       ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4059 #else
4060       gcc_unreachable ();
4061 #endif
4062       return;
4063     }
4064 
4065   /* Now output the underlying data.  If we've handling the padding, return.
4066      Otherwise, break and ensure SIZE is the size written.  */
4067   switch (code)
4068     {
4069     case BOOLEAN_TYPE:
4070     case INTEGER_TYPE:
4071     case ENUMERAL_TYPE:
4072     case POINTER_TYPE:
4073     case REFERENCE_TYPE:
4074     /* APPLE LOCAL radar 5822844 */
4075     case BLOCK_POINTER_TYPE:
4076     case OFFSET_TYPE:
4077       if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4078 					   EXPAND_INITIALIZER),
4079 			      MIN (size, thissize), align, 0))
4080 	error ("initializer for integer value is too complicated");
4081       break;
4082 
4083     case REAL_TYPE:
4084       if (TREE_CODE (exp) != REAL_CST)
4085 	error ("initializer for floating value is not a floating constant");
4086 
4087       assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4088       break;
4089 
4090     case COMPLEX_TYPE:
4091       output_constant (TREE_REALPART (exp), thissize / 2, align);
4092       output_constant (TREE_IMAGPART (exp), thissize / 2,
4093 		       min_align (align, BITS_PER_UNIT * (thissize / 2)));
4094       break;
4095 
4096     case ARRAY_TYPE:
4097     case VECTOR_TYPE:
4098       switch (TREE_CODE (exp))
4099 	{
4100 	case CONSTRUCTOR:
4101 	  output_constructor (exp, size, align);
4102 	  return;
4103 	case STRING_CST:
4104 	  thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
4105 			  size);
4106 	  assemble_string (TREE_STRING_POINTER (exp), thissize);
4107 	  break;
4108 
4109 	case VECTOR_CST:
4110 	  {
4111 	    int elt_size;
4112 	    tree link;
4113 	    unsigned int nalign;
4114 	    enum machine_mode inner;
4115 
4116 	    inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4117 	    nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4118 
4119 	    elt_size = GET_MODE_SIZE (inner);
4120 
4121 	    link = TREE_VECTOR_CST_ELTS (exp);
4122 	    output_constant (TREE_VALUE (link), elt_size, align);
4123 	    thissize = elt_size;
4124 	    while ((link = TREE_CHAIN (link)) != NULL)
4125 	      {
4126 		output_constant (TREE_VALUE (link), elt_size, nalign);
4127 		thissize += elt_size;
4128 	      }
4129 	    break;
4130 	  }
4131 	default:
4132 	  gcc_unreachable ();
4133 	}
4134       break;
4135 
4136     case RECORD_TYPE:
4137     case UNION_TYPE:
4138       gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4139       output_constructor (exp, size, align);
4140       return;
4141 
4142     case ERROR_MARK:
4143       return;
4144 
4145     default:
4146       gcc_unreachable ();
4147     }
4148 
4149   if (size > thissize)
4150     assemble_zeros (size - thissize);
4151 }
4152 
4153 
4154 /* Subroutine of output_constructor, used for computing the size of
4155    arrays of unspecified length.  VAL must be a CONSTRUCTOR of an array
4156    type with an unspecified upper bound.  */
4157 
4158 static unsigned HOST_WIDE_INT
array_size_for_constructor(tree val)4159 array_size_for_constructor (tree val)
4160 {
4161   tree max_index, i;
4162   unsigned HOST_WIDE_INT cnt;
4163   tree index, value, tmp;
4164 
4165   /* This code used to attempt to handle string constants that are not
4166      arrays of single-bytes, but nothing else does, so there's no point in
4167      doing it here.  */
4168   if (TREE_CODE (val) == STRING_CST)
4169     return TREE_STRING_LENGTH (val);
4170 
4171   max_index = NULL_TREE;
4172   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4173     {
4174       if (TREE_CODE (index) == RANGE_EXPR)
4175 	index = TREE_OPERAND (index, 1);
4176       if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4177 	max_index = index;
4178     }
4179 
4180   if (max_index == NULL_TREE)
4181     return 0;
4182 
4183   /* Compute the total number of array elements.  */
4184   tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4185   i = size_binop (MINUS_EXPR, fold_convert (sizetype, max_index),
4186 		  fold_convert (sizetype, tmp));
4187   i = size_binop (PLUS_EXPR, i, build_int_cst (sizetype, 1));
4188 
4189   /* Multiply by the array element unit size to find number of bytes.  */
4190   i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4191 
4192   return tree_low_cst (i, 1);
4193 }
4194 
4195 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
4196    Generate at least SIZE bytes, padding if necessary.  */
4197 
4198 static void
output_constructor(tree exp,unsigned HOST_WIDE_INT size,unsigned int align)4199 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
4200 		    unsigned int align)
4201 {
4202   tree type = TREE_TYPE (exp);
4203   tree field = 0;
4204   tree min_index = 0;
4205   /* Number of bytes output or skipped so far.
4206      In other words, current position within the constructor.  */
4207   HOST_WIDE_INT total_bytes = 0;
4208   /* Nonzero means BYTE contains part of a byte, to be output.  */
4209   int byte_buffer_in_use = 0;
4210   int byte = 0;
4211   unsigned HOST_WIDE_INT cnt;
4212   constructor_elt *ce;
4213 
4214   gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
4215 
4216   if (TREE_CODE (type) == RECORD_TYPE)
4217     field = TYPE_FIELDS (type);
4218 
4219   if (TREE_CODE (type) == ARRAY_TYPE
4220       && TYPE_DOMAIN (type) != 0)
4221     min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
4222 
4223   /* As LINK goes through the elements of the constant,
4224      FIELD goes through the structure fields, if the constant is a structure.
4225      if the constant is a union, then we override this,
4226      by getting the field from the TREE_LIST element.
4227      But the constant could also be an array.  Then FIELD is zero.
4228 
4229      There is always a maximum of one element in the chain LINK for unions
4230      (even if the initializer in a source program incorrectly contains
4231      more one).  */
4232   for (cnt = 0;
4233        VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
4234        cnt++, field = field ? TREE_CHAIN (field) : 0)
4235     {
4236       tree val = ce->value;
4237       tree index = 0;
4238 
4239       /* The element in a union constructor specifies the proper field
4240 	 or index.  */
4241       if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
4242 	   || TREE_CODE (type) == QUAL_UNION_TYPE)
4243 	  && ce->index != 0)
4244 	field = ce->index;
4245 
4246       else if (TREE_CODE (type) == ARRAY_TYPE)
4247 	index = ce->index;
4248 
4249 #ifdef ASM_COMMENT_START
4250       if (field && flag_verbose_asm)
4251 	fprintf (asm_out_file, "%s %s:\n",
4252 		 ASM_COMMENT_START,
4253 		 DECL_NAME (field)
4254 		 ? IDENTIFIER_POINTER (DECL_NAME (field))
4255 		 : "<anonymous>");
4256 #endif
4257 
4258       /* Eliminate the marker that makes a cast not be an lvalue.  */
4259       if (val != 0)
4260 	STRIP_NOPS (val);
4261 
4262       if (index && TREE_CODE (index) == RANGE_EXPR)
4263 	{
4264 	  unsigned HOST_WIDE_INT fieldsize
4265 	    = int_size_in_bytes (TREE_TYPE (type));
4266 	  HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
4267 	  HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
4268 	  HOST_WIDE_INT index;
4269 	  unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
4270 
4271 	  for (index = lo_index; index <= hi_index; index++)
4272 	    {
4273 	      /* Output the element's initial value.  */
4274 	      if (val == 0)
4275 		assemble_zeros (fieldsize);
4276 	      else
4277 		output_constant (val, fieldsize, align2);
4278 
4279 	      /* Count its size.  */
4280 	      total_bytes += fieldsize;
4281 	    }
4282 	}
4283       else if (field == 0 || !DECL_BIT_FIELD (field))
4284 	{
4285 	  /* An element that is not a bit-field.  */
4286 
4287 	  unsigned HOST_WIDE_INT fieldsize;
4288 	  /* Since this structure is static,
4289 	     we know the positions are constant.  */
4290 	  HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
4291 	  unsigned int align2;
4292 
4293 	  if (index != 0)
4294 	    pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
4295 		   * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
4296 
4297 	  /* Output any buffered-up bit-fields preceding this element.  */
4298 	  if (byte_buffer_in_use)
4299 	    {
4300 	      assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4301 	      total_bytes++;
4302 	      byte_buffer_in_use = 0;
4303 	    }
4304 
4305 	  /* Advance to offset of this element.
4306 	     Note no alignment needed in an array, since that is guaranteed
4307 	     if each element has the proper size.  */
4308 	  if ((field != 0 || index != 0) && pos != total_bytes)
4309 	    {
4310 	      gcc_assert (pos >= total_bytes);
4311 	      assemble_zeros (pos - total_bytes);
4312 	      total_bytes = pos;
4313 	    }
4314 
4315 	  /* Find the alignment of this element.  */
4316 	  align2 = min_align (align, BITS_PER_UNIT * pos);
4317 
4318 	  /* Determine size this element should occupy.  */
4319 	  if (field)
4320 	    {
4321 	      fieldsize = 0;
4322 
4323 	      /* If this is an array with an unspecified upper bound,
4324 		 the initializer determines the size.  */
4325 	      /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4326 		 but we cannot do this until the deprecated support for
4327 		 initializing zero-length array members is removed.  */
4328 	      if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
4329 		  && TYPE_DOMAIN (TREE_TYPE (field))
4330 		  && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
4331 		{
4332 		  fieldsize = array_size_for_constructor (val);
4333 		  /* Given a non-empty initialization, this field had
4334 		     better be last.  */
4335 		  gcc_assert (!fieldsize || !TREE_CHAIN (field));
4336 		}
4337 	      else if (DECL_SIZE_UNIT (field))
4338 		{
4339 		  /* ??? This can't be right.  If the decl size overflows
4340 		     a host integer we will silently emit no data.  */
4341 		  if (host_integerp (DECL_SIZE_UNIT (field), 1))
4342 		    fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
4343 		}
4344 	    }
4345 	  else
4346 	    fieldsize = int_size_in_bytes (TREE_TYPE (type));
4347 
4348 	  /* Output the element's initial value.  */
4349 	  if (val == 0)
4350 	    assemble_zeros (fieldsize);
4351 	  else
4352 	    output_constant (val, fieldsize, align2);
4353 
4354 	  /* Count its size.  */
4355 	  total_bytes += fieldsize;
4356 	}
4357       else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
4358 	error ("invalid initial value for member %qs",
4359 	       IDENTIFIER_POINTER (DECL_NAME (field)));
4360       else
4361 	{
4362 	  /* Element that is a bit-field.  */
4363 
4364 	  HOST_WIDE_INT next_offset = int_bit_position (field);
4365 	  HOST_WIDE_INT end_offset
4366 	    = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
4367 
4368 	  if (val == 0)
4369 	    val = integer_zero_node;
4370 
4371 	  /* If this field does not start in this (or, next) byte,
4372 	     skip some bytes.  */
4373 	  if (next_offset / BITS_PER_UNIT != total_bytes)
4374 	    {
4375 	      /* Output remnant of any bit field in previous bytes.  */
4376 	      if (byte_buffer_in_use)
4377 		{
4378 		  assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4379 		  total_bytes++;
4380 		  byte_buffer_in_use = 0;
4381 		}
4382 
4383 	      /* If still not at proper byte, advance to there.  */
4384 	      if (next_offset / BITS_PER_UNIT != total_bytes)
4385 		{
4386 		  gcc_assert (next_offset / BITS_PER_UNIT >= total_bytes);
4387 		  assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
4388 		  total_bytes = next_offset / BITS_PER_UNIT;
4389 		}
4390 	    }
4391 
4392 	  if (! byte_buffer_in_use)
4393 	    byte = 0;
4394 
4395 	  /* We must split the element into pieces that fall within
4396 	     separate bytes, and combine each byte with previous or
4397 	     following bit-fields.  */
4398 
4399 	  /* next_offset is the offset n fbits from the beginning of
4400 	     the structure to the next bit of this element to be processed.
4401 	     end_offset is the offset of the first bit past the end of
4402 	     this element.  */
4403 	  while (next_offset < end_offset)
4404 	    {
4405 	      int this_time;
4406 	      int shift;
4407 	      HOST_WIDE_INT value;
4408 	      HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4409 	      HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4410 
4411 	      /* Advance from byte to byte
4412 		 within this element when necessary.  */
4413 	      while (next_byte != total_bytes)
4414 		{
4415 		  assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4416 		  total_bytes++;
4417 		  byte = 0;
4418 		}
4419 
4420 	      /* Number of bits we can process at once
4421 		 (all part of the same byte).  */
4422 	      this_time = MIN (end_offset - next_offset,
4423 			       BITS_PER_UNIT - next_bit);
4424 	      if (BYTES_BIG_ENDIAN)
4425 		{
4426 		  /* On big-endian machine, take the most significant bits
4427 		     first (of the bits that are significant)
4428 		     and put them into bytes from the most significant end.  */
4429 		  shift = end_offset - next_offset - this_time;
4430 
4431 		  /* Don't try to take a bunch of bits that cross
4432 		     the word boundary in the INTEGER_CST. We can
4433 		     only select bits from the LOW or HIGH part
4434 		     not from both.  */
4435 		  if (shift < HOST_BITS_PER_WIDE_INT
4436 		      && shift + this_time > HOST_BITS_PER_WIDE_INT)
4437 		    {
4438 		      this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4439 		      shift = HOST_BITS_PER_WIDE_INT;
4440 		    }
4441 
4442 		  /* Now get the bits from the appropriate constant word.  */
4443 		  if (shift < HOST_BITS_PER_WIDE_INT)
4444 		    value = TREE_INT_CST_LOW (val);
4445 		  else
4446 		    {
4447 		      gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4448 		      value = TREE_INT_CST_HIGH (val);
4449 		      shift -= HOST_BITS_PER_WIDE_INT;
4450 		    }
4451 
4452 		  /* Get the result. This works only when:
4453 		     1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
4454 		  byte |= (((value >> shift)
4455 			    & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4456 			   << (BITS_PER_UNIT - this_time - next_bit));
4457 		}
4458 	      else
4459 		{
4460 		  /* On little-endian machines,
4461 		     take first the least significant bits of the value
4462 		     and pack them starting at the least significant
4463 		     bits of the bytes.  */
4464 		  shift = next_offset - int_bit_position (field);
4465 
4466 		  /* Don't try to take a bunch of bits that cross
4467 		     the word boundary in the INTEGER_CST. We can
4468 		     only select bits from the LOW or HIGH part
4469 		     not from both.  */
4470 		  if (shift < HOST_BITS_PER_WIDE_INT
4471 		      && shift + this_time > HOST_BITS_PER_WIDE_INT)
4472 		    this_time = (HOST_BITS_PER_WIDE_INT - shift);
4473 
4474 		  /* Now get the bits from the appropriate constant word.  */
4475 		  if (shift < HOST_BITS_PER_WIDE_INT)
4476 		    value = TREE_INT_CST_LOW (val);
4477 		  else
4478 		    {
4479 		      gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4480 		      value = TREE_INT_CST_HIGH (val);
4481 		      shift -= HOST_BITS_PER_WIDE_INT;
4482 		    }
4483 
4484 		  /* Get the result. This works only when:
4485 		     1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
4486 		  byte |= (((value >> shift)
4487 			    & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4488 			   << next_bit);
4489 		}
4490 
4491 	      next_offset += this_time;
4492 	      byte_buffer_in_use = 1;
4493 	    }
4494 	}
4495     }
4496 
4497   if (byte_buffer_in_use)
4498     {
4499       assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4500       total_bytes++;
4501     }
4502 
4503   if ((unsigned HOST_WIDE_INT)total_bytes < size)
4504     assemble_zeros (size - total_bytes);
4505 }
4506 
4507 /* This TREE_LIST contains any weak symbol declarations waiting
4508    to be emitted.  */
4509 static GTY(()) tree weak_decls;
4510 
4511 /* Mark DECL as weak.  */
4512 
4513 static void
mark_weak(tree decl)4514 mark_weak (tree decl)
4515 {
4516   DECL_WEAK (decl) = 1;
4517 
4518   if (DECL_RTL_SET_P (decl)
4519       && MEM_P (DECL_RTL (decl))
4520       && XEXP (DECL_RTL (decl), 0)
4521       && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
4522     SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
4523 }
4524 
4525 /* Merge weak status between NEWDECL and OLDDECL.  */
4526 
4527 void
merge_weak(tree newdecl,tree olddecl)4528 merge_weak (tree newdecl, tree olddecl)
4529 {
4530   if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4531     {
4532       if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
4533         {
4534           tree *pwd;
4535           /* We put the NEWDECL on the weak_decls list at some point
4536              and OLDDECL as well.  Keep just OLDDECL on the list.  */
4537 	  for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
4538 	    if (TREE_VALUE (*pwd) == newdecl)
4539 	      {
4540 	        *pwd = TREE_CHAIN (*pwd);
4541 		break;
4542 	      }
4543         }
4544       return;
4545     }
4546 
4547   if (DECL_WEAK (newdecl))
4548     {
4549       tree wd;
4550 
4551       /* NEWDECL is weak, but OLDDECL is not.  */
4552 
4553       /* If we already output the OLDDECL, we're in trouble; we can't
4554 	 go back and make it weak.  This error cannot caught in
4555 	 declare_weak because the NEWDECL and OLDDECL was not yet
4556 	 been merged; therefore, TREE_ASM_WRITTEN was not set.  */
4557       if (TREE_ASM_WRITTEN (olddecl))
4558 	error ("weak declaration of %q+D must precede definition",
4559 	       newdecl);
4560 
4561       /* If we've already generated rtl referencing OLDDECL, we may
4562 	 have done so in a way that will not function properly with
4563 	 a weak symbol.  */
4564       else if (TREE_USED (olddecl)
4565 	       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4566 	warning (0, "weak declaration of %q+D after first use results "
4567                  "in unspecified behavior", newdecl);
4568 
4569       if (SUPPORTS_WEAK)
4570 	{
4571 	  /* We put the NEWDECL on the weak_decls list at some point.
4572 	     Replace it with the OLDDECL.  */
4573 	  for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
4574 	    if (TREE_VALUE (wd) == newdecl)
4575 	      {
4576 		TREE_VALUE (wd) = olddecl;
4577 		break;
4578 	      }
4579 	  /* We may not find the entry on the list.  If NEWDECL is a
4580 	     weak alias, then we will have already called
4581 	     globalize_decl to remove the entry; in that case, we do
4582 	     not need to do anything.  */
4583 	}
4584 
4585       /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping.  */
4586       mark_weak (olddecl);
4587     }
4588   else
4589     /* OLDDECL was weak, but NEWDECL was not explicitly marked as
4590        weak.  Just update NEWDECL to indicate that it's weak too.  */
4591     mark_weak (newdecl);
4592 }
4593 
4594 /* Declare DECL to be a weak symbol.  */
4595 
4596 void
declare_weak(tree decl)4597 declare_weak (tree decl)
4598 {
4599   if (! TREE_PUBLIC (decl))
4600     error ("weak declaration of %q+D must be public", decl);
4601   else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4602     error ("weak declaration of %q+D must precede definition", decl);
4603   else if (SUPPORTS_WEAK)
4604     {
4605       if (! DECL_WEAK (decl))
4606 	weak_decls = tree_cons (NULL, decl, weak_decls);
4607     }
4608   else
4609     warning (0, "weak declaration of %q+D not supported", decl);
4610 
4611   mark_weak (decl);
4612 }
4613 
4614 static void
weak_finish_1(tree decl)4615 weak_finish_1 (tree decl)
4616 {
4617 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
4618   const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4619 #endif
4620 
4621   if (! TREE_USED (decl))
4622     return;
4623 
4624 #ifdef ASM_WEAKEN_DECL
4625   ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
4626 #else
4627 #ifdef ASM_WEAKEN_LABEL
4628   ASM_WEAKEN_LABEL (asm_out_file, name);
4629 #else
4630 #ifdef ASM_OUTPUT_WEAK_ALIAS
4631   {
4632     static bool warn_once = 0;
4633     if (! warn_once)
4634       {
4635 	warning (0, "only weak aliases are supported in this configuration");
4636 	warn_once = 1;
4637       }
4638     return;
4639   }
4640 #endif
4641 #endif
4642 #endif
4643 }
4644 
4645 /* This TREE_LIST contains weakref targets.  */
4646 
4647 static GTY(()) tree weakref_targets;
4648 
4649 /* Forward declaration.  */
4650 static tree find_decl_and_mark_needed (tree decl, tree target);
4651 
4652 /* Emit any pending weak declarations.  */
4653 
4654 void
weak_finish(void)4655 weak_finish (void)
4656 {
4657   tree t;
4658 
4659   for (t = weakref_targets; t; t = TREE_CHAIN (t))
4660     {
4661       tree alias_decl = TREE_PURPOSE (t);
4662       tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
4663 
4664       if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
4665 	/* Remove alias_decl from the weak list, but leave entries for
4666 	   the target alone.  */
4667 	target = NULL_TREE;
4668 #ifndef ASM_OUTPUT_WEAKREF
4669       else if (! TREE_SYMBOL_REFERENCED (target))
4670 	{
4671 	  /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
4672 	     defined, otherwise we and weak_finish_1 would use a
4673 	     different macros.  */
4674 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
4675 	  ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
4676 # else
4677 	  tree decl = find_decl_and_mark_needed (alias_decl, target);
4678 
4679 	  if (! decl)
4680 	    {
4681 	      decl = build_decl (TREE_CODE (alias_decl), target,
4682 				 TREE_TYPE (alias_decl));
4683 
4684 	      DECL_EXTERNAL (decl) = 1;
4685 	      TREE_PUBLIC (decl) = 1;
4686 	      DECL_ARTIFICIAL (decl) = 1;
4687 	      TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
4688 	      TREE_USED (decl) = 1;
4689 	    }
4690 
4691 	  weak_finish_1 (decl);
4692 # endif
4693 	}
4694 #endif
4695 
4696       {
4697 	tree *p;
4698 	tree t2;
4699 
4700 	/* Remove the alias and the target from the pending weak list
4701 	   so that we do not emit any .weak directives for the former,
4702 	   nor multiple .weak directives for the latter.  */
4703 	for (p = &weak_decls; (t2 = *p) ; )
4704 	  {
4705 	    if (TREE_VALUE (t2) == alias_decl
4706 		|| target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
4707 	      *p = TREE_CHAIN (t2);
4708 	    else
4709 	      p = &TREE_CHAIN (t2);
4710 	  }
4711 
4712 	/* Remove other weakrefs to the same target, to speed things up.  */
4713 	for (p = &TREE_CHAIN (t); (t2 = *p) ; )
4714 	  {
4715 	    if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
4716 	      *p = TREE_CHAIN (t2);
4717 	    else
4718 	      p = &TREE_CHAIN (t2);
4719 	  }
4720       }
4721     }
4722 
4723   for (t = weak_decls; t; t = TREE_CHAIN (t))
4724     {
4725       tree decl = TREE_VALUE (t);
4726 
4727       weak_finish_1 (decl);
4728     }
4729 }
4730 
4731 /* Emit the assembly bits to indicate that DECL is globally visible.  */
4732 
4733 static void
globalize_decl(tree decl)4734 globalize_decl (tree decl)
4735 {
4736   const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
4737 
4738 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
4739   if (DECL_WEAK (decl))
4740     {
4741       tree *p, t;
4742 
4743 #ifdef ASM_WEAKEN_DECL
4744       ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
4745 #else
4746       ASM_WEAKEN_LABEL (asm_out_file, name);
4747 #endif
4748 
4749       /* Remove this function from the pending weak list so that
4750 	 we do not emit multiple .weak directives for it.  */
4751       for (p = &weak_decls; (t = *p) ; )
4752 	{
4753 	  if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4754 	    *p = TREE_CHAIN (t);
4755 	  else
4756 	    p = &TREE_CHAIN (t);
4757 	}
4758 
4759       /* Remove weakrefs to the same target from the pending weakref
4760 	 list, for the same reason.  */
4761       for (p = &weakref_targets; (t = *p) ; )
4762 	{
4763 	  if (DECL_ASSEMBLER_NAME (decl)
4764 	      == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4765 	    *p = TREE_CHAIN (t);
4766 	  else
4767 	    p = &TREE_CHAIN (t);
4768 	}
4769 
4770       return;
4771     }
4772 #elif defined(ASM_MAKE_LABEL_LINKONCE)
4773   if (DECL_ONE_ONLY (decl))
4774     ASM_MAKE_LABEL_LINKONCE (asm_out_file, name);
4775 #endif
4776 
4777   targetm.asm_out.globalize_label (asm_out_file, name);
4778 }
4779 
4780 /* We have to be able to tell cgraph about the needed-ness of the target
4781    of an alias.  This requires that the decl have been defined.  Aliases
4782    that precede their definition have to be queued for later processing.  */
4783 
4784 typedef struct alias_pair GTY(())
4785 {
4786   tree decl;
4787   tree target;
4788 } alias_pair;
4789 
4790 /* Define gc'd vector type.  */
4791 DEF_VEC_O(alias_pair);
4792 DEF_VEC_ALLOC_O(alias_pair,gc);
4793 
4794 static GTY(()) VEC(alias_pair,gc) *alias_pairs;
4795 
4796 /* Given an assembly name, find the decl it is associated with.  At the
4797    same time, mark it needed for cgraph.  */
4798 
4799 static tree
find_decl_and_mark_needed(tree decl,tree target)4800 find_decl_and_mark_needed (tree decl, tree target)
4801 {
4802   struct cgraph_node *fnode = NULL;
4803   struct cgraph_varpool_node *vnode = NULL;
4804 
4805   if (TREE_CODE (decl) == FUNCTION_DECL)
4806     {
4807       fnode = cgraph_node_for_asm (target);
4808       if (fnode == NULL)
4809 	vnode = cgraph_varpool_node_for_asm (target);
4810     }
4811   else
4812     {
4813       vnode = cgraph_varpool_node_for_asm (target);
4814       if (vnode == NULL)
4815 	fnode = cgraph_node_for_asm (target);
4816     }
4817 
4818   if (fnode)
4819     {
4820       /* We can't mark function nodes as used after cgraph global info
4821 	 is finished.  This wouldn't generally be necessary, but C++
4822 	 virtual table thunks are introduced late in the game and
4823 	 might seem like they need marking, although in fact they
4824 	 don't.  */
4825       if (! cgraph_global_info_ready)
4826 	cgraph_mark_needed_node (fnode);
4827       return fnode->decl;
4828     }
4829   else if (vnode)
4830     {
4831       cgraph_varpool_mark_needed_node (vnode);
4832       return vnode->decl;
4833     }
4834   else
4835     return NULL_TREE;
4836 }
4837 
4838 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
4839    or ASM_OUTPUT_DEF_FROM_DECLS.  The function defines the symbol whose
4840    tree node is DECL to have the value of the tree node TARGET.  */
4841 
4842 static void
do_assemble_alias(tree decl,tree target)4843 do_assemble_alias (tree decl, tree target)
4844 {
4845   if (TREE_ASM_WRITTEN (decl))
4846     return;
4847 
4848   TREE_ASM_WRITTEN (decl) = 1;
4849   TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4850 
4851   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4852     {
4853       ultimate_transparent_alias_target (&target);
4854 
4855       if (!TREE_SYMBOL_REFERENCED (target))
4856 	weakref_targets = tree_cons (decl, target, weakref_targets);
4857 
4858 #ifdef ASM_OUTPUT_WEAKREF
4859       ASM_OUTPUT_WEAKREF (asm_out_file, decl,
4860 			  IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4861 			  IDENTIFIER_POINTER (target));
4862 #else
4863       if (!SUPPORTS_WEAK)
4864 	{
4865 	  error ("%Jweakref is not supported in this configuration", decl);
4866 	  return;
4867 	}
4868 #endif
4869       return;
4870     }
4871 
4872 #ifdef ASM_OUTPUT_DEF
4873   /* Make name accessible from other files, if appropriate.  */
4874 
4875   if (TREE_PUBLIC (decl))
4876     {
4877       globalize_decl (decl);
4878       maybe_assemble_visibility (decl);
4879     }
4880 
4881 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
4882   ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4883 # else
4884   ASM_OUTPUT_DEF (asm_out_file,
4885 		  IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4886 		  IDENTIFIER_POINTER (target));
4887 # endif
4888 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4889   {
4890     const char *name;
4891     tree *p, t;
4892 
4893     name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4894 # ifdef ASM_WEAKEN_DECL
4895     ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4896 # else
4897     ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4898 # endif
4899     /* Remove this function from the pending weak list so that
4900        we do not emit multiple .weak directives for it.  */
4901     for (p = &weak_decls; (t = *p) ; )
4902       if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4903 	*p = TREE_CHAIN (t);
4904       else
4905 	p = &TREE_CHAIN (t);
4906 
4907     /* Remove weakrefs to the same target from the pending weakref
4908        list, for the same reason.  */
4909     for (p = &weakref_targets; (t = *p) ; )
4910       {
4911 	if (DECL_ASSEMBLER_NAME (decl)
4912 	    == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4913 	  *p = TREE_CHAIN (t);
4914 	else
4915 	  p = &TREE_CHAIN (t);
4916       }
4917   }
4918 #endif
4919 }
4920 
4921 /* First pass of completing pending aliases.  Make sure that cgraph knows
4922    which symbols will be required.  */
4923 
4924 void
finish_aliases_1(void)4925 finish_aliases_1 (void)
4926 {
4927   unsigned i;
4928   alias_pair *p;
4929 
4930   for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4931     {
4932       tree target_decl;
4933 
4934       target_decl = find_decl_and_mark_needed (p->decl, p->target);
4935       if (target_decl == NULL)
4936 	{
4937 	  if (! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4938 	    error ("%q+D aliased to undefined symbol %qs",
4939 		   p->decl, IDENTIFIER_POINTER (p->target));
4940 	}
4941       else if (DECL_EXTERNAL (target_decl)
4942 	       && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4943 	error ("%q+D aliased to external symbol %qs",
4944 	       p->decl, IDENTIFIER_POINTER (p->target));
4945     }
4946 }
4947 
4948 /* Second pass of completing pending aliases.  Emit the actual assembly.
4949    This happens at the end of compilation and thus it is assured that the
4950    target symbol has been emitted.  */
4951 
4952 void
finish_aliases_2(void)4953 finish_aliases_2 (void)
4954 {
4955   unsigned i;
4956   alias_pair *p;
4957 
4958   for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4959     do_assemble_alias (p->decl, p->target);
4960 
4961   VEC_truncate (alias_pair, alias_pairs, 0);
4962 }
4963 
4964 /* Emit an assembler directive to make the symbol for DECL an alias to
4965    the symbol for TARGET.  */
4966 
4967 void
assemble_alias(tree decl,tree target)4968 assemble_alias (tree decl, tree target)
4969 {
4970   tree target_decl;
4971   bool is_weakref = false;
4972 
4973   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4974     {
4975       tree alias = DECL_ASSEMBLER_NAME (decl);
4976 
4977       is_weakref = true;
4978 
4979       ultimate_transparent_alias_target (&target);
4980 
4981       if (alias == target)
4982 	error ("weakref %q+D ultimately targets itself", decl);
4983       else
4984 	{
4985 #ifndef ASM_OUTPUT_WEAKREF
4986 	  IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
4987 	  TREE_CHAIN (alias) = target;
4988 #endif
4989 	}
4990       if (TREE_PUBLIC (decl))
4991 	error ("weakref %q+D must have static linkage", decl);
4992     }
4993   else
4994     {
4995 #if !defined (ASM_OUTPUT_DEF)
4996 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
4997       error ("%Jalias definitions not supported in this configuration", decl);
4998       return;
4999 # else
5000       if (!DECL_WEAK (decl))
5001 	{
5002 	  error ("%Jonly weak aliases are supported in this configuration", decl);
5003 	  return;
5004 	}
5005 # endif
5006 #endif
5007     }
5008 
5009   /* We must force creation of DECL_RTL for debug info generation, even though
5010      we don't use it here.  */
5011   make_decl_rtl (decl);
5012   TREE_USED (decl) = 1;
5013 
5014   /* A quirk of the initial implementation of aliases required that the user
5015      add "extern" to all of them.  Which is silly, but now historical.  Do
5016      note that the symbol is in fact locally defined.  */
5017   if (! is_weakref)
5018     DECL_EXTERNAL (decl) = 0;
5019 
5020   /* Allow aliases to aliases.  */
5021   if (TREE_CODE (decl) == FUNCTION_DECL)
5022     cgraph_node (decl)->alias = true;
5023   else
5024     cgraph_varpool_node (decl)->alias = true;
5025 
5026   /* If the target has already been emitted, we don't have to queue the
5027      alias.  This saves a tad o memory.  */
5028   target_decl = find_decl_and_mark_needed (decl, target);
5029   if (target_decl && TREE_ASM_WRITTEN (target_decl))
5030     do_assemble_alias (decl, target);
5031   else
5032     {
5033       alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
5034       p->decl = decl;
5035       p->target = target;
5036     }
5037 }
5038 
5039 /* Emit an assembler directive to set symbol for DECL visibility to
5040    the visibility type VIS, which must not be VISIBILITY_DEFAULT.  */
5041 
5042 void
default_assemble_visibility(tree decl,int vis)5043 default_assemble_visibility (tree decl, int vis)
5044 {
5045   static const char * const visibility_types[] = {
5046     NULL, "protected", "hidden", "internal"
5047   };
5048 
5049   const char *name, *type;
5050 
5051   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5052   type = visibility_types[vis];
5053 
5054 #ifdef HAVE_GAS_HIDDEN
5055   fprintf (asm_out_file, "\t.%s\t", type);
5056   assemble_name (asm_out_file, name);
5057   fprintf (asm_out_file, "\n");
5058 #else
5059   warning (OPT_Wattributes, "visibility attribute not supported "
5060 	   "in this configuration; ignored");
5061 #endif
5062 }
5063 
5064 /* A helper function to call assemble_visibility when needed for a decl.  */
5065 
5066 int
maybe_assemble_visibility(tree decl)5067 maybe_assemble_visibility (tree decl)
5068 {
5069   enum symbol_visibility vis = DECL_VISIBILITY (decl);
5070 
5071   if (vis != VISIBILITY_DEFAULT)
5072     {
5073       targetm.asm_out.visibility (decl, vis);
5074       return 1;
5075     }
5076   else
5077     return 0;
5078 }
5079 
5080 /* Returns 1 if the target configuration supports defining public symbols
5081    so that one of them will be chosen at link time instead of generating a
5082    multiply-defined symbol error, whether through the use of weak symbols or
5083    a target-specific mechanism for having duplicates discarded.  */
5084 
5085 int
supports_one_only(void)5086 supports_one_only (void)
5087 {
5088   if (SUPPORTS_ONE_ONLY)
5089     return 1;
5090   return SUPPORTS_WEAK;
5091 }
5092 
5093 /* Set up DECL as a public symbol that can be defined in multiple
5094    translation units without generating a linker error.  */
5095 
5096 void
make_decl_one_only(tree decl)5097 make_decl_one_only (tree decl)
5098 {
5099   gcc_assert (TREE_CODE (decl) == VAR_DECL
5100 	      || TREE_CODE (decl) == FUNCTION_DECL);
5101 
5102   TREE_PUBLIC (decl) = 1;
5103 
5104   if (SUPPORTS_ONE_ONLY)
5105     {
5106 #ifdef MAKE_DECL_ONE_ONLY
5107       MAKE_DECL_ONE_ONLY (decl);
5108 #endif
5109       DECL_ONE_ONLY (decl) = 1;
5110     }
5111   else if (TREE_CODE (decl) == VAR_DECL
5112       && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5113     DECL_COMMON (decl) = 1;
5114   else
5115     {
5116       gcc_assert (SUPPORTS_WEAK);
5117       DECL_WEAK (decl) = 1;
5118     }
5119 }
5120 
5121 void
init_varasm_once(void)5122 init_varasm_once (void)
5123 {
5124   section_htab = htab_create_ggc (31, section_entry_hash,
5125 				  section_entry_eq, NULL);
5126   object_block_htab = htab_create_ggc (31, object_block_entry_hash,
5127 				       object_block_entry_eq, NULL);
5128   const_desc_htab = htab_create_ggc (1009, const_desc_hash,
5129 				     const_desc_eq, NULL);
5130 
5131   const_alias_set = new_alias_set ();
5132   shared_constant_pool = create_constant_pool ();
5133 
5134 #ifdef TEXT_SECTION_ASM_OP
5135   text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5136 				      TEXT_SECTION_ASM_OP);
5137 #endif
5138 
5139 #ifdef DATA_SECTION_ASM_OP
5140   data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5141 				      DATA_SECTION_ASM_OP);
5142 #endif
5143 
5144 #ifdef SDATA_SECTION_ASM_OP
5145   sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5146 				       SDATA_SECTION_ASM_OP);
5147 #endif
5148 
5149 #ifdef READONLY_DATA_SECTION_ASM_OP
5150   readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5151 					       READONLY_DATA_SECTION_ASM_OP);
5152 #endif
5153 
5154 #ifdef CTORS_SECTION_ASM_OP
5155   ctors_section = get_unnamed_section (0, output_section_asm_op,
5156 				       CTORS_SECTION_ASM_OP);
5157 #endif
5158 
5159 #ifdef DTORS_SECTION_ASM_OP
5160   dtors_section = get_unnamed_section (0, output_section_asm_op,
5161 				       DTORS_SECTION_ASM_OP);
5162 #endif
5163 
5164 #ifdef BSS_SECTION_ASM_OP
5165   bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5166 				     output_section_asm_op,
5167 				     BSS_SECTION_ASM_OP);
5168 #endif
5169 
5170 #ifdef SBSS_SECTION_ASM_OP
5171   sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5172 				      output_section_asm_op,
5173 				      SBSS_SECTION_ASM_OP);
5174 #endif
5175 
5176   tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5177 					   | SECTION_COMMON, emit_tls_common);
5178   lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5179 					| SECTION_COMMON, emit_local);
5180   comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5181 				       | SECTION_COMMON, emit_common);
5182 
5183 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
5184   bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5185 					       emit_bss);
5186 #endif
5187 
5188   targetm.asm_out.init_sections ();
5189 
5190   if (readonly_data_section == NULL)
5191     readonly_data_section = text_section;
5192 }
5193 
5194 enum tls_model
decl_default_tls_model(tree decl)5195 decl_default_tls_model (tree decl)
5196 {
5197   enum tls_model kind;
5198   bool is_local;
5199 
5200   is_local = targetm.binds_local_p (decl);
5201   if (!flag_shlib)
5202     {
5203       if (is_local)
5204 	kind = TLS_MODEL_LOCAL_EXEC;
5205       else
5206 	kind = TLS_MODEL_INITIAL_EXEC;
5207     }
5208 
5209   /* Local dynamic is inefficient when we're not combining the
5210      parts of the address.  */
5211   else if (optimize && is_local)
5212     kind = TLS_MODEL_LOCAL_DYNAMIC;
5213   else
5214     kind = TLS_MODEL_GLOBAL_DYNAMIC;
5215   if (kind < flag_tls_default)
5216     kind = flag_tls_default;
5217 
5218   return kind;
5219 }
5220 
5221 /* Select a set of attributes for section NAME based on the properties
5222    of DECL and whether or not RELOC indicates that DECL's initializer
5223    might contain runtime relocations.
5224 
5225    We make the section read-only and executable for a function decl,
5226    read-only for a const data decl, and writable for a non-const data decl.  */
5227 
5228 unsigned int
default_section_type_flags(tree decl,const char * name,int reloc)5229 default_section_type_flags (tree decl, const char *name, int reloc)
5230 {
5231   unsigned int flags;
5232 
5233   if (decl && TREE_CODE (decl) == FUNCTION_DECL)
5234     flags = SECTION_CODE;
5235   else if (decl && decl_readonly_section (decl, reloc))
5236     flags = 0;
5237   else if (current_function_decl
5238 	   && cfun
5239 	   && cfun->unlikely_text_section_name
5240 	   && strcmp (name, cfun->unlikely_text_section_name) == 0)
5241     flags = SECTION_CODE;
5242   else if (!decl
5243 	   && (!current_function_decl || !cfun)
5244 	   && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
5245     flags = SECTION_CODE;
5246   else
5247     flags = SECTION_WRITE;
5248 
5249   if (decl && DECL_ONE_ONLY (decl))
5250     flags |= SECTION_LINKONCE;
5251 
5252   if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5253     flags |= SECTION_TLS | SECTION_WRITE;
5254 
5255   if (strcmp (name, ".bss") == 0
5256       || strncmp (name, ".bss.", 5) == 0
5257       || strncmp (name, ".gnu.linkonce.b.", 16) == 0
5258       || strcmp (name, ".sbss") == 0
5259       || strncmp (name, ".sbss.", 6) == 0
5260       || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
5261     flags |= SECTION_BSS;
5262 
5263   if (strcmp (name, ".tdata") == 0
5264       || strncmp (name, ".tdata.", 7) == 0
5265       || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
5266     flags |= SECTION_TLS;
5267 
5268   if (strcmp (name, ".tbss") == 0
5269       || strncmp (name, ".tbss.", 6) == 0
5270       || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
5271     flags |= SECTION_TLS | SECTION_BSS;
5272 
5273   /* These three sections have special ELF types.  They are neither
5274      SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
5275      want to print a section type (@progbits or @nobits).  If someone
5276      is silly enough to emit code or TLS variables to one of these
5277      sections, then don't handle them specially.  */
5278   if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
5279       && (strcmp (name, ".init_array") == 0
5280 	  || strcmp (name, ".fini_array") == 0
5281 	  || strcmp (name, ".preinit_array") == 0))
5282     flags |= SECTION_NOTYPE;
5283 
5284   return flags;
5285 }
5286 
5287 /* Return true if the target supports some form of global BSS,
5288    either through bss_noswitch_section, or by selecting a BSS
5289    section in TARGET_ASM_SELECT_SECTION.  */
5290 
5291 bool
have_global_bss_p(void)5292 have_global_bss_p (void)
5293 {
5294   return bss_noswitch_section || targetm.have_switchable_bss_sections;
5295 }
5296 
5297 /* Output assembly to switch to section NAME with attribute FLAGS.
5298    Four variants for common object file formats.  */
5299 
5300 void
default_no_named_section(const char * name ATTRIBUTE_UNUSED,unsigned int flags ATTRIBUTE_UNUSED,tree decl ATTRIBUTE_UNUSED)5301 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
5302 			  unsigned int flags ATTRIBUTE_UNUSED,
5303 			  tree decl ATTRIBUTE_UNUSED)
5304 {
5305   /* Some object formats don't support named sections at all.  The
5306      front-end should already have flagged this as an error.  */
5307   gcc_unreachable ();
5308 }
5309 
5310 void
default_elf_asm_named_section(const char * name,unsigned int flags,tree decl ATTRIBUTE_UNUSED)5311 default_elf_asm_named_section (const char *name, unsigned int flags,
5312 			       tree decl ATTRIBUTE_UNUSED)
5313 {
5314   char flagchars[10], *f = flagchars;
5315 
5316   /* If we have already declared this section, we can use an
5317      abbreviated form to switch back to it -- unless this section is
5318      part of a COMDAT groups, in which case GAS requires the full
5319      declaration every time.  */
5320   if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5321       && (flags & SECTION_DECLARED))
5322     {
5323       fprintf (asm_out_file, "\t.section\t%s\n", name);
5324       return;
5325     }
5326 
5327   if (!(flags & SECTION_DEBUG))
5328     *f++ = 'a';
5329   if (flags & SECTION_WRITE)
5330     *f++ = 'w';
5331   if (flags & SECTION_CODE)
5332     *f++ = 'x';
5333   if (flags & SECTION_SMALL)
5334     *f++ = 's';
5335   if (flags & SECTION_MERGE)
5336     *f++ = 'M';
5337   if (flags & SECTION_STRINGS)
5338     *f++ = 'S';
5339   if (flags & SECTION_TLS)
5340     *f++ = 'T';
5341   if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5342     *f++ = 'G';
5343   *f = '\0';
5344 
5345   fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
5346 
5347   if (!(flags & SECTION_NOTYPE))
5348     {
5349       const char *type;
5350       const char *format;
5351 
5352       if (flags & SECTION_BSS)
5353 	type = "nobits";
5354       else
5355 	type = "progbits";
5356 
5357       format = ",@%s";
5358 #ifdef ASM_COMMENT_START
5359       /* On platforms that use "@" as the assembly comment character,
5360 	 use "%" instead.  */
5361       if (strcmp (ASM_COMMENT_START, "@") == 0)
5362 	format = ",%%%s";
5363 #endif
5364       fprintf (asm_out_file, format, type);
5365 
5366       if (flags & SECTION_ENTSIZE)
5367 	fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
5368       if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5369 	fprintf (asm_out_file, ",%s,comdat",
5370 		 lang_hooks.decls.comdat_group (decl));
5371     }
5372 
5373   putc ('\n', asm_out_file);
5374 }
5375 
5376 void
default_coff_asm_named_section(const char * name,unsigned int flags,tree decl ATTRIBUTE_UNUSED)5377 default_coff_asm_named_section (const char *name, unsigned int flags,
5378 				tree decl ATTRIBUTE_UNUSED)
5379 {
5380   char flagchars[8], *f = flagchars;
5381 
5382   if (flags & SECTION_WRITE)
5383     *f++ = 'w';
5384   if (flags & SECTION_CODE)
5385     *f++ = 'x';
5386   *f = '\0';
5387 
5388   fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
5389 }
5390 
5391 void
default_pe_asm_named_section(const char * name,unsigned int flags,tree decl)5392 default_pe_asm_named_section (const char *name, unsigned int flags,
5393 			      tree decl)
5394 {
5395   default_coff_asm_named_section (name, flags, decl);
5396 
5397   if (flags & SECTION_LINKONCE)
5398     {
5399       /* Functions may have been compiled at various levels of
5400          optimization so we can't use `same_size' here.
5401          Instead, have the linker pick one.  */
5402       fprintf (asm_out_file, "\t.linkonce %s\n",
5403 	       (flags & SECTION_CODE ? "discard" : "same_size"));
5404     }
5405 }
5406 
5407 /* The lame default section selector.  */
5408 
5409 section *
default_select_section(tree decl,int reloc,unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)5410 default_select_section (tree decl, int reloc,
5411 			unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5412 {
5413   if (DECL_P (decl))
5414     {
5415       if (decl_readonly_section (decl, reloc))
5416 	return readonly_data_section;
5417     }
5418   else if (TREE_CODE (decl) == CONSTRUCTOR)
5419     {
5420       if (! ((flag_pic && reloc)
5421 	     || !TREE_READONLY (decl)
5422 	     || TREE_SIDE_EFFECTS (decl)
5423 	     || !TREE_CONSTANT (decl)))
5424 	return readonly_data_section;
5425     }
5426   else if (TREE_CODE (decl) == STRING_CST)
5427     return readonly_data_section;
5428   else if (! (flag_pic && reloc))
5429     return readonly_data_section;
5430 
5431   return data_section;
5432 }
5433 
5434 enum section_category
categorize_decl_for_section(tree decl,int reloc)5435 categorize_decl_for_section (tree decl, int reloc)
5436 {
5437   enum section_category ret;
5438 
5439   if (TREE_CODE (decl) == FUNCTION_DECL)
5440     return SECCAT_TEXT;
5441   else if (TREE_CODE (decl) == STRING_CST)
5442     {
5443       if (flag_mudflap) /* or !flag_merge_constants */
5444         return SECCAT_RODATA;
5445       else
5446 	return SECCAT_RODATA_MERGE_STR;
5447     }
5448   else if (TREE_CODE (decl) == VAR_DECL)
5449     {
5450       if (bss_initializer_p (decl))
5451 	ret = SECCAT_BSS;
5452       else if (! TREE_READONLY (decl)
5453 	       || TREE_SIDE_EFFECTS (decl)
5454 	       || ! TREE_CONSTANT (DECL_INITIAL (decl)))
5455 	{
5456 	  /* Here the reloc_rw_mask is not testing whether the section should
5457 	     be read-only or not, but whether the dynamic link will have to
5458 	     do something.  If so, we wish to segregate the data in order to
5459 	     minimize cache misses inside the dynamic linker.  */
5460 	  if (reloc & targetm.asm_out.reloc_rw_mask ())
5461 	    ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
5462 	  else
5463 	    ret = SECCAT_DATA;
5464 	}
5465       else if (reloc & targetm.asm_out.reloc_rw_mask ())
5466 	ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
5467       else if (reloc || flag_merge_constants < 2)
5468 	/* C and C++ don't allow different variables to share the same
5469 	   location.  -fmerge-all-constants allows even that (at the
5470 	   expense of not conforming).  */
5471 	ret = SECCAT_RODATA;
5472       else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
5473 	ret = SECCAT_RODATA_MERGE_STR_INIT;
5474       else
5475 	ret = SECCAT_RODATA_MERGE_CONST;
5476     }
5477   else if (TREE_CODE (decl) == CONSTRUCTOR)
5478     {
5479       if ((reloc & targetm.asm_out.reloc_rw_mask ())
5480 	  || TREE_SIDE_EFFECTS (decl)
5481 	  || ! TREE_CONSTANT (decl))
5482 	ret = SECCAT_DATA;
5483       else
5484 	ret = SECCAT_RODATA;
5485     }
5486   else
5487     ret = SECCAT_RODATA;
5488 
5489   /* There are no read-only thread-local sections.  */
5490   if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5491     {
5492       /* Note that this would be *just* SECCAT_BSS, except that there's
5493 	 no concept of a read-only thread-local-data section.  */
5494       if (ret == SECCAT_BSS
5495 	  || (flag_zero_initialized_in_bss
5496 	      && initializer_zerop (DECL_INITIAL (decl))))
5497 	ret = SECCAT_TBSS;
5498       else
5499 	ret = SECCAT_TDATA;
5500     }
5501 
5502   /* If the target uses small data sections, select it.  */
5503   else if (targetm.in_small_data_p (decl))
5504     {
5505       if (ret == SECCAT_BSS)
5506 	ret = SECCAT_SBSS;
5507       else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
5508 	ret = SECCAT_SRODATA;
5509       else
5510 	ret = SECCAT_SDATA;
5511     }
5512 
5513   return ret;
5514 }
5515 
5516 bool
decl_readonly_section(tree decl,int reloc)5517 decl_readonly_section (tree decl, int reloc)
5518 {
5519   switch (categorize_decl_for_section (decl, reloc))
5520     {
5521     case SECCAT_RODATA:
5522     case SECCAT_RODATA_MERGE_STR:
5523     case SECCAT_RODATA_MERGE_STR_INIT:
5524     case SECCAT_RODATA_MERGE_CONST:
5525     case SECCAT_SRODATA:
5526       return true;
5527       break;
5528     default:
5529       return false;
5530       break;
5531     }
5532 }
5533 
5534 /* Select a section based on the above categorization.  */
5535 
5536 section *
default_elf_select_section(tree decl,int reloc,unsigned HOST_WIDE_INT align)5537 default_elf_select_section (tree decl, int reloc,
5538 			    unsigned HOST_WIDE_INT align)
5539 {
5540   const char *sname;
5541   switch (categorize_decl_for_section (decl, reloc))
5542     {
5543     case SECCAT_TEXT:
5544       /* We're not supposed to be called on FUNCTION_DECLs.  */
5545       gcc_unreachable ();
5546     case SECCAT_RODATA:
5547       return readonly_data_section;
5548     case SECCAT_RODATA_MERGE_STR:
5549       return mergeable_string_section (decl, align, 0);
5550     case SECCAT_RODATA_MERGE_STR_INIT:
5551       return mergeable_string_section (DECL_INITIAL (decl), align, 0);
5552     case SECCAT_RODATA_MERGE_CONST:
5553       return mergeable_constant_section (DECL_MODE (decl), align, 0);
5554     case SECCAT_SRODATA:
5555       sname = ".sdata2";
5556       break;
5557     case SECCAT_DATA:
5558       return data_section;
5559     case SECCAT_DATA_REL:
5560       sname = ".data.rel";
5561       break;
5562     case SECCAT_DATA_REL_LOCAL:
5563       sname = ".data.rel.local";
5564       break;
5565     case SECCAT_DATA_REL_RO:
5566       sname = ".data.rel.ro";
5567       break;
5568     case SECCAT_DATA_REL_RO_LOCAL:
5569       sname = ".data.rel.ro.local";
5570       break;
5571     case SECCAT_SDATA:
5572       sname = ".sdata";
5573       break;
5574     case SECCAT_TDATA:
5575       sname = ".tdata";
5576       break;
5577     case SECCAT_BSS:
5578       if (bss_section)
5579 	return bss_section;
5580       sname = ".bss";
5581       break;
5582     case SECCAT_SBSS:
5583       sname = ".sbss";
5584       break;
5585     case SECCAT_TBSS:
5586       sname = ".tbss";
5587       break;
5588     default:
5589       gcc_unreachable ();
5590     }
5591 
5592   if (!DECL_P (decl))
5593     decl = NULL_TREE;
5594   return get_named_section (decl, sname, reloc);
5595 }
5596 
5597 /* Construct a unique section name based on the decl name and the
5598    categorization performed above.  */
5599 
5600 void
default_unique_section(tree decl,int reloc)5601 default_unique_section (tree decl, int reloc)
5602 {
5603   /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
5604   bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
5605   const char *prefix, *name;
5606   size_t nlen, plen;
5607   char *string;
5608 
5609   switch (categorize_decl_for_section (decl, reloc))
5610     {
5611     case SECCAT_TEXT:
5612       prefix = one_only ? ".gnu.linkonce.t." : ".text.";
5613       break;
5614     case SECCAT_RODATA:
5615     case SECCAT_RODATA_MERGE_STR:
5616     case SECCAT_RODATA_MERGE_STR_INIT:
5617     case SECCAT_RODATA_MERGE_CONST:
5618       prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
5619       break;
5620     case SECCAT_SRODATA:
5621       prefix = one_only ? ".gnu.linkonce.s2." : ".sdata2.";
5622       break;
5623     case SECCAT_DATA:
5624       prefix = one_only ? ".gnu.linkonce.d." : ".data.";
5625       break;
5626     case SECCAT_DATA_REL:
5627       prefix = one_only ? ".gnu.linkonce.d.rel." : ".data.rel.";
5628       break;
5629     case SECCAT_DATA_REL_LOCAL:
5630       prefix = one_only ? ".gnu.linkonce.d.rel.local." : ".data.rel.local.";
5631       break;
5632     case SECCAT_DATA_REL_RO:
5633       prefix = one_only ? ".gnu.linkonce.d.rel.ro." : ".data.rel.ro.";
5634       break;
5635     case SECCAT_DATA_REL_RO_LOCAL:
5636       prefix = one_only ? ".gnu.linkonce.d.rel.ro.local."
5637 	       : ".data.rel.ro.local.";
5638       break;
5639     case SECCAT_SDATA:
5640       prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
5641       break;
5642     case SECCAT_BSS:
5643       prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
5644       break;
5645     case SECCAT_SBSS:
5646       prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
5647       break;
5648     case SECCAT_TDATA:
5649       prefix = one_only ? ".gnu.linkonce.td." : ".tdata.";
5650       break;
5651     case SECCAT_TBSS:
5652       prefix = one_only ? ".gnu.linkonce.tb." : ".tbss.";
5653       break;
5654     default:
5655       gcc_unreachable ();
5656     }
5657   plen = strlen (prefix);
5658 
5659   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5660   name = targetm.strip_name_encoding (name);
5661   nlen = strlen (name);
5662 
5663   string = alloca (nlen + plen + 1);
5664   memcpy (string, prefix, plen);
5665   memcpy (string + plen, name, nlen + 1);
5666 
5667   DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
5668 }
5669 
5670 /* Like compute_reloc_for_constant, except for an RTX.  The return value
5671    is a mask for which bit 1 indicates a global relocation, and bit 0
5672    indicates a local relocation.  */
5673 
5674 static int
compute_reloc_for_rtx_1(rtx * xp,void * data)5675 compute_reloc_for_rtx_1 (rtx *xp, void *data)
5676 {
5677   int *preloc = data;
5678   rtx x = *xp;
5679 
5680   switch (GET_CODE (x))
5681     {
5682     case SYMBOL_REF:
5683       *preloc |= SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
5684       break;
5685     case LABEL_REF:
5686       *preloc |= 1;
5687       break;
5688     default:
5689       break;
5690     }
5691 
5692   return 0;
5693 }
5694 
5695 static int
compute_reloc_for_rtx(rtx x)5696 compute_reloc_for_rtx (rtx x)
5697 {
5698   int reloc;
5699 
5700   switch (GET_CODE (x))
5701     {
5702     case CONST:
5703     case SYMBOL_REF:
5704     case LABEL_REF:
5705       reloc = 0;
5706       for_each_rtx (&x, compute_reloc_for_rtx_1, &reloc);
5707       return reloc;
5708 
5709     default:
5710       return 0;
5711     }
5712 }
5713 
5714 section *
default_select_rtx_section(enum machine_mode mode ATTRIBUTE_UNUSED,rtx x,unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)5715 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
5716 			    rtx x,
5717 			    unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5718 {
5719   if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
5720     return data_section;
5721   else
5722     return readonly_data_section;
5723 }
5724 
5725 section *
default_elf_select_rtx_section(enum machine_mode mode,rtx x,unsigned HOST_WIDE_INT align)5726 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
5727 				unsigned HOST_WIDE_INT align)
5728 {
5729   int reloc = compute_reloc_for_rtx (x);
5730 
5731   /* ??? Handle small data here somehow.  */
5732 
5733   if (reloc & targetm.asm_out.reloc_rw_mask ())
5734     {
5735       if (reloc == 1)
5736 	return get_named_section (NULL, ".data.rel.ro.local", 1);
5737       else
5738 	return get_named_section (NULL, ".data.rel.ro", 3);
5739     }
5740 
5741   return mergeable_constant_section (mode, align, 0);
5742 }
5743 
5744 /* Set the generally applicable flags on the SYMBOL_REF for EXP.  */
5745 
5746 void
default_encode_section_info(tree decl,rtx rtl,int first ATTRIBUTE_UNUSED)5747 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
5748 {
5749   rtx symbol;
5750   int flags;
5751 
5752   /* Careful not to prod global register variables.  */
5753   if (!MEM_P (rtl))
5754     return;
5755   symbol = XEXP (rtl, 0);
5756   if (GET_CODE (symbol) != SYMBOL_REF)
5757     return;
5758 
5759   flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
5760   if (TREE_CODE (decl) == FUNCTION_DECL)
5761     flags |= SYMBOL_FLAG_FUNCTION;
5762   if (targetm.binds_local_p (decl))
5763     flags |= SYMBOL_FLAG_LOCAL;
5764   if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5765     flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
5766   else if (targetm.in_small_data_p (decl))
5767     flags |= SYMBOL_FLAG_SMALL;
5768   /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names?  Without
5769      being PUBLIC, the thing *must* be defined in this translation unit.
5770      Prevent this buglet from being propagated into rtl code as well.  */
5771   if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
5772     flags |= SYMBOL_FLAG_EXTERNAL;
5773 
5774   SYMBOL_REF_FLAGS (symbol) = flags;
5775 }
5776 
5777 /* By default, we do nothing for encode_section_info, so we need not
5778    do anything but discard the '*' marker.  */
5779 
5780 const char *
default_strip_name_encoding(const char * str)5781 default_strip_name_encoding (const char *str)
5782 {
5783   return str + (*str == '*');
5784 }
5785 
5786 #ifdef ASM_OUTPUT_DEF
5787 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR.  Define the
5788    anchor relative to ".", the current section position.  */
5789 
5790 void
default_asm_output_anchor(rtx symbol)5791 default_asm_output_anchor (rtx symbol)
5792 {
5793   char buffer[100];
5794 
5795   sprintf (buffer, ". + " HOST_WIDE_INT_PRINT_DEC,
5796 	   SYMBOL_REF_BLOCK_OFFSET (symbol));
5797   ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
5798 }
5799 #endif
5800 
5801 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P.  */
5802 
5803 bool
default_use_anchors_for_symbol_p(rtx symbol)5804 default_use_anchors_for_symbol_p (rtx symbol)
5805 {
5806   section *sect;
5807   tree decl;
5808 
5809   /* Don't use anchors for mergeable sections.  The linker might move
5810      the objects around.  */
5811   sect = SYMBOL_REF_BLOCK (symbol)->sect;
5812   if (sect->common.flags & SECTION_MERGE)
5813     return false;
5814 
5815   /* Don't use anchors for small data sections.  The small data register
5816      acts as an anchor for such sections.  */
5817   if (sect->common.flags & SECTION_SMALL)
5818     return false;
5819 
5820   decl = SYMBOL_REF_DECL (symbol);
5821   if (decl && DECL_P (decl))
5822     {
5823       /* Don't use section anchors for decls that might be defined by
5824 	 other modules.  */
5825       if (!targetm.binds_local_p (decl))
5826 	return false;
5827 
5828       /* Don't use section anchors for decls that will be placed in a
5829 	 small data section.  */
5830       /* ??? Ideally, this check would be redundant with the SECTION_SMALL
5831 	 one above.  The problem is that we only use SECTION_SMALL for
5832 	 sections that should be marked as small in the section directive.  */
5833       if (targetm.in_small_data_p (decl))
5834 	return false;
5835     }
5836   return true;
5837 }
5838 
5839 /* Assume ELF-ish defaults, since that's pretty much the most liberal
5840    wrt cross-module name binding.  */
5841 
5842 bool
default_binds_local_p(tree exp)5843 default_binds_local_p (tree exp)
5844 {
5845   return default_binds_local_p_1 (exp, flag_shlib);
5846 }
5847 
5848 bool
default_binds_local_p_1(tree exp,int shlib)5849 default_binds_local_p_1 (tree exp, int shlib)
5850 {
5851   bool local_p;
5852 
5853   /* A non-decl is an entry in the constant pool.  */
5854   if (!DECL_P (exp))
5855     local_p = true;
5856   /* Weakrefs may not bind locally, even though the weakref itself is
5857      always static and therefore local.  */
5858   else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
5859     local_p = false;
5860   /* Static variables are always local.  */
5861   else if (! TREE_PUBLIC (exp))
5862     local_p = true;
5863   /* A variable is local if the user has said explicitly that it will
5864      be.  */
5865   else if (DECL_VISIBILITY_SPECIFIED (exp)
5866 	   && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5867     local_p = true;
5868   /* Variables defined outside this object might not be local.  */
5869   else if (DECL_EXTERNAL (exp))
5870     local_p = false;
5871   /* If defined in this object and visibility is not default, must be
5872      local.  */
5873   else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5874     local_p = true;
5875   /* Default visibility weak data can be overridden by a strong symbol
5876      in another module and so are not local.  */
5877   else if (DECL_WEAK (exp))
5878     local_p = false;
5879   /* If PIC, then assume that any global name can be overridden by
5880      symbols resolved from other modules, unless we are compiling with
5881      -fwhole-program, which assumes that names are local.  */
5882   else if (shlib)
5883     local_p = flag_whole_program;
5884   /* Uninitialized COMMON variable may be unified with symbols
5885      resolved from other modules.  */
5886   else if (DECL_COMMON (exp)
5887 	   && (DECL_INITIAL (exp) == NULL
5888 	       || DECL_INITIAL (exp) == error_mark_node))
5889     local_p = false;
5890   /* Otherwise we're left with initialized (or non-common) global data
5891      which is of necessity defined locally.  */
5892   else
5893     local_p = true;
5894 
5895   return local_p;
5896 }
5897 
5898 /* Determine whether or not a pointer mode is valid. Assume defaults
5899    of ptr_mode or Pmode - can be overridden.  */
5900 bool
default_valid_pointer_mode(enum machine_mode mode)5901 default_valid_pointer_mode (enum machine_mode mode)
5902 {
5903   return (mode == ptr_mode || mode == Pmode);
5904 }
5905 
5906 /* Default function to output code that will globalize a label.  A
5907    target must define GLOBAL_ASM_OP or provide its own function to
5908    globalize a label.  */
5909 #ifdef GLOBAL_ASM_OP
5910 void
default_globalize_label(FILE * stream,const char * name)5911 default_globalize_label (FILE * stream, const char *name)
5912 {
5913   fputs (GLOBAL_ASM_OP, stream);
5914   assemble_name (stream, name);
5915   putc ('\n', stream);
5916 }
5917 #endif /* GLOBAL_ASM_OP */
5918 
5919 /* Default function to output a label for unwind information.  The
5920    default is to do nothing.  A target that needs nonlocal labels for
5921    unwind information must provide its own function to do this.  */
5922 void
default_emit_unwind_label(FILE * stream ATTRIBUTE_UNUSED,tree decl ATTRIBUTE_UNUSED,int for_eh ATTRIBUTE_UNUSED,int empty ATTRIBUTE_UNUSED)5923 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
5924 			   tree decl ATTRIBUTE_UNUSED,
5925 			   int for_eh ATTRIBUTE_UNUSED,
5926 			   int empty ATTRIBUTE_UNUSED)
5927 {
5928 }
5929 
5930 /* Default function to output a label to divide up the exception table.
5931    The default is to do nothing.  A target that needs/wants to divide
5932    up the table must provide it's own function to do this.  */
5933 void
default_emit_except_table_label(FILE * stream ATTRIBUTE_UNUSED)5934 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
5935 {
5936 }
5937 
5938 /* This is how to output an internal numbered label where PREFIX is
5939    the class of label and LABELNO is the number within the class.  */
5940 
5941 void
default_internal_label(FILE * stream,const char * prefix,unsigned long labelno)5942 default_internal_label (FILE *stream, const char *prefix,
5943 			unsigned long labelno)
5944 {
5945   char *const buf = alloca (40 + strlen (prefix));
5946   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
5947   ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
5948 }
5949 
5950 /* This is the default behavior at the beginning of a file.  It's
5951    controlled by two other target-hook toggles.  */
5952 void
default_file_start(void)5953 default_file_start (void)
5954 {
5955   if (targetm.file_start_app_off && !flag_verbose_asm)
5956     fputs (ASM_APP_OFF, asm_out_file);
5957 
5958   if (targetm.file_start_file_directive)
5959     output_file_directive (asm_out_file, main_input_filename);
5960 }
5961 
5962 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
5963    which emits a special section directive used to indicate whether or
5964    not this object file needs an executable stack.  This is primarily
5965    a GNU extension to ELF but could be used on other targets.  */
5966 
5967 int trampolines_created;
5968 
5969 void
file_end_indicate_exec_stack(void)5970 file_end_indicate_exec_stack (void)
5971 {
5972   unsigned int flags = SECTION_DEBUG;
5973   if (trampolines_created)
5974     flags |= SECTION_CODE;
5975 
5976   switch_to_section (get_section (".note.GNU-stack", flags, NULL));
5977 }
5978 
5979 /* Output DIRECTIVE (a C string) followed by a newline.  This is used as
5980    a get_unnamed_section callback.  */
5981 
5982 void
output_section_asm_op(const void * directive)5983 output_section_asm_op (const void *directive)
5984 {
5985   fprintf (asm_out_file, "%s\n", (const char *) directive);
5986 }
5987 
5988 /* Emit assembly code to switch to section NEW_SECTION.  Do nothing if
5989    the current section is NEW_SECTION.  */
5990 
5991 void
switch_to_section(section * new_section)5992 switch_to_section (section *new_section)
5993 {
5994   if (in_section == new_section)
5995     return;
5996 
5997   if (new_section->common.flags & SECTION_FORGET)
5998     in_section = NULL;
5999   else
6000     in_section = new_section;
6001 
6002   switch (SECTION_STYLE (new_section))
6003     {
6004     case SECTION_NAMED:
6005       if (cfun
6006 	  && !cfun->unlikely_text_section_name
6007 	  && strcmp (new_section->named.name,
6008 		     UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
6009 	cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
6010 
6011       targetm.asm_out.named_section (new_section->named.name,
6012 				     new_section->named.common.flags,
6013 				     new_section->named.decl);
6014       break;
6015 
6016     case SECTION_UNNAMED:
6017       new_section->unnamed.callback (new_section->unnamed.data);
6018       break;
6019 
6020     case SECTION_NOSWITCH:
6021       gcc_unreachable ();
6022       break;
6023     }
6024 
6025   new_section->common.flags |= SECTION_DECLARED;
6026 }
6027 
6028 /* If block symbol SYMBOL has not yet been assigned an offset, place
6029    it at the end of its block.  */
6030 
6031 void
place_block_symbol(rtx symbol)6032 place_block_symbol (rtx symbol)
6033 {
6034   unsigned HOST_WIDE_INT size, mask, offset;
6035   struct constant_descriptor_rtx *desc;
6036   unsigned int alignment;
6037   struct object_block *block;
6038   tree decl;
6039 
6040   gcc_assert (SYMBOL_REF_BLOCK (symbol));
6041   if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
6042     return;
6043 
6044   /* Work out the symbol's size and alignment.  */
6045   if (CONSTANT_POOL_ADDRESS_P (symbol))
6046     {
6047       desc = SYMBOL_REF_CONSTANT (symbol);
6048       alignment = desc->align;
6049       size = GET_MODE_SIZE (desc->mode);
6050     }
6051   else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6052     {
6053       decl = SYMBOL_REF_DECL (symbol);
6054       alignment = get_constant_alignment (decl);
6055       size = get_constant_size (decl);
6056     }
6057   else
6058     {
6059       decl = SYMBOL_REF_DECL (symbol);
6060       alignment = DECL_ALIGN (decl);
6061       size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6062     }
6063 
6064   /* Calculate the object's offset from the start of the block.  */
6065   block = SYMBOL_REF_BLOCK (symbol);
6066   mask = alignment / BITS_PER_UNIT - 1;
6067   offset = (block->size + mask) & ~mask;
6068   SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
6069 
6070   /* Record the block's new alignment and size.  */
6071   block->alignment = MAX (block->alignment, alignment);
6072   block->size = offset + size;
6073 
6074   VEC_safe_push (rtx, gc, block->objects, symbol);
6075 }
6076 
6077 /* Return the anchor that should be used to address byte offset OFFSET
6078    from the first object in BLOCK.  MODEL is the TLS model used
6079    to access it.  */
6080 
6081 rtx
get_section_anchor(struct object_block * block,HOST_WIDE_INT offset,enum tls_model model)6082 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
6083 		    enum tls_model model)
6084 {
6085   char label[100];
6086   unsigned int begin, middle, end;
6087   unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
6088   rtx anchor;
6089 
6090   /* Work out the anchor's offset.  Use an offset of 0 for the first
6091      anchor so that we don't pessimize the case where we take the address
6092      of a variable at the beginning of the block.  This is particularly
6093      useful when a block has only one variable assigned to it.
6094 
6095      We try to place anchors RANGE bytes apart, so there can then be
6096      anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
6097      a ptr_mode offset.  With some target settings, the lowest such
6098      anchor might be out of range for the lowest ptr_mode offset;
6099      likewise the highest anchor for the highest offset.  Use anchors
6100      at the extreme ends of the ptr_mode range in such cases.
6101 
6102      All arithmetic uses unsigned integers in order to avoid
6103      signed overflow.  */
6104   max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
6105   min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
6106   range = max_offset - min_offset + 1;
6107   if (range == 0)
6108     offset = 0;
6109   else
6110     {
6111       bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
6112       if (offset < 0)
6113 	{
6114 	  delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
6115 	  delta -= delta % range;
6116 	  if (delta > bias)
6117 	    delta = bias;
6118 	  offset = (HOST_WIDE_INT) (-delta);
6119 	}
6120       else
6121 	{
6122 	  delta = (unsigned HOST_WIDE_INT) offset - min_offset;
6123 	  delta -= delta % range;
6124 	  if (delta > bias - 1)
6125 	    delta = bias - 1;
6126 	  offset = (HOST_WIDE_INT) delta;
6127 	}
6128     }
6129 
6130   /* Do a binary search to see if there's already an anchor we can use.
6131      Set BEGIN to the new anchor's index if not.  */
6132   begin = 0;
6133   end = VEC_length (rtx, block->anchors);
6134   while (begin != end)
6135     {
6136       middle = (end + begin) / 2;
6137       anchor = VEC_index (rtx, block->anchors, middle);
6138       if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
6139 	end = middle;
6140       else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
6141 	begin = middle + 1;
6142       else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
6143 	end = middle;
6144       else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
6145 	begin = middle + 1;
6146       else
6147 	return anchor;
6148     }
6149 
6150   /* Create a new anchor with a unique label.  */
6151   ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
6152   anchor = create_block_symbol (ggc_strdup (label), block, offset);
6153   SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
6154   SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
6155 
6156   /* Insert it at index BEGIN.  */
6157   VEC_safe_insert (rtx, gc, block->anchors, begin, anchor);
6158   return anchor;
6159 }
6160 
6161 /* Output the objects in BLOCK.  */
6162 
6163 static void
output_object_block(struct object_block * block)6164 output_object_block (struct object_block *block)
6165 {
6166   struct constant_descriptor_rtx *desc;
6167   unsigned int i;
6168   HOST_WIDE_INT offset;
6169   tree decl;
6170   rtx symbol;
6171 
6172   if (block->objects == NULL)
6173     return;
6174 
6175   /* Switch to the section and make sure that the first byte is
6176      suitably aligned.  */
6177   switch_to_section (block->sect);
6178   assemble_align (block->alignment);
6179 
6180   /* Define the values of all anchors relative to the current section
6181      position.  */
6182   for (i = 0; VEC_iterate (rtx, block->anchors, i, symbol); i++)
6183     targetm.asm_out.output_anchor (symbol);
6184 
6185   /* Output the objects themselves.  */
6186   offset = 0;
6187   for (i = 0; VEC_iterate (rtx, block->objects, i, symbol); i++)
6188     {
6189       /* Move to the object's offset, padding with zeros if necessary.  */
6190       assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
6191       offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
6192       if (CONSTANT_POOL_ADDRESS_P (symbol))
6193 	{
6194 	  desc = SYMBOL_REF_CONSTANT (symbol);
6195 	  output_constant_pool_1 (desc, 1);
6196 	  offset += GET_MODE_SIZE (desc->mode);
6197 	}
6198       else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6199 	{
6200 	  decl = SYMBOL_REF_DECL (symbol);
6201 	  assemble_constant_contents (decl, XSTR (symbol, 0),
6202 				      get_constant_alignment (decl));
6203 	  offset += get_constant_size (decl);
6204 	}
6205       else
6206 	{
6207 	  decl = SYMBOL_REF_DECL (symbol);
6208 	  assemble_variable_contents (decl, XSTR (symbol, 0), false);
6209 	  offset += tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6210 	}
6211     }
6212 }
6213 
6214 /* A htab_traverse callback used to call output_object_block for
6215    each member of object_block_htab.  */
6216 
6217 static int
output_object_block_htab(void ** slot,void * data ATTRIBUTE_UNUSED)6218 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
6219 {
6220   output_object_block ((struct object_block *) (*slot));
6221   return 1;
6222 }
6223 
6224 /* Output the definitions of all object_blocks.  */
6225 
6226 void
output_object_blocks(void)6227 output_object_blocks (void)
6228 {
6229   htab_traverse (object_block_htab, output_object_block_htab, NULL);
6230 }
6231 
6232 /* Emit text to declare externally defined symbols. It is needed to
6233    properly support non-default visibility.  */
6234 void
default_elf_asm_output_external(FILE * file ATTRIBUTE_UNUSED,tree decl,const char * name ATTRIBUTE_UNUSED)6235 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
6236 				 tree decl,
6237 				 const char *name ATTRIBUTE_UNUSED)
6238 {
6239   /* We output the name if and only if TREE_SYMBOL_REFERENCED is
6240      set in order to avoid putting out names that are never really
6241      used. */
6242   if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
6243       && targetm.binds_local_p (decl))
6244     maybe_assemble_visibility (decl);
6245 }
6246 
6247 #include "gt-varasm.h"
6248