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