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