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