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