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