forked from openhwgroup/corev-gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarasm.cc
8602 lines (7349 loc) · 250 KB
/
varasm.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Output variables, constants and external declarations, for GNU compiler.
Copyright (C) 1987-2023 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
/* This file handles generation of all the assembler code
*except* the instructions of a function.
This includes declarations of variables and their initial values.
We also output the assembler code for constants stored in memory
and are responsible for combining constants with the same value. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "target.h"
#include "rtl.h"
#include "tree.h"
#include "predict.h"
#include "memmodel.h"
#include "tm_p.h"
#include "stringpool.h"
#include "regs.h"
#include "emit-rtl.h"
#include "cgraph.h"
#include "diagnostic-core.h"
#include "fold-const.h"
#include "stor-layout.h"
#include "varasm.h"
#include "version.h"
#include "flags.h"
#include "stmt.h"
#include "expr.h"
#include "expmed.h"
#include "optabs.h"
#include "output.h"
#include "langhooks.h"
#include "debug.h"
#include "common/common-target.h"
#include "stringpool.h"
#include "attribs.h"
#include "asan.h"
#include "rtl-iter.h"
#include "file-prefix-map.h" /* remap_debug_filename() */
#include "alloc-pool.h"
#include "toplev.h"
#include "opts.h"
/* The (assembler) name of the first globally-visible object output. */
extern GTY(()) const char *first_global_object_name;
extern GTY(()) const char *weak_global_object_name;
const char *first_global_object_name;
const char *weak_global_object_name;
class addr_const;
class constant_descriptor_rtx;
struct rtx_constant_pool;
#define n_deferred_constants (crtl->varasm.deferred_constants)
/* Number for making the label on the next
constant that is stored in memory. */
static GTY(()) int const_labelno;
/* Carry information from ASM_DECLARE_OBJECT_NAME
to ASM_FINISH_DECLARE_OBJECT. */
int size_directive_output;
/* The last decl for which assemble_variable was called,
if it did ASM_DECLARE_OBJECT_NAME.
If the last call to assemble_variable didn't do that,
this holds 0. */
tree last_assemble_variable_decl;
/* The following global variable indicates if the first basic block
in a function belongs to the cold partition or not. */
bool first_function_block_is_cold;
/* Whether we saw any functions with no_split_stack. */
static bool saw_no_split_stack;
static const char *strip_reg_name (const char *);
static bool contains_pointers_p (tree);
#ifdef ASM_OUTPUT_EXTERNAL
static bool incorporeal_function_p (tree);
#endif
static void decode_addr_const (tree, class addr_const *);
static hashval_t const_hash_1 (const tree);
static bool compare_constant (const tree, const tree);
static void output_constant_def_contents (rtx);
static void output_addressed_constants (tree, int);
static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
unsigned int, bool, bool);
static void globalize_decl (tree);
static bool decl_readonly_section_1 (enum section_category);
#ifdef BSS_SECTION_ASM_OP
#ifdef ASM_OUTPUT_ALIGNED_BSS
static void asm_output_aligned_bss (FILE *, tree, const char *,
unsigned HOST_WIDE_INT, int)
ATTRIBUTE_UNUSED;
#endif
#endif /* BSS_SECTION_ASM_OP */
static void mark_weak (tree);
static void output_constant_pool (const char *, tree);
static void handle_vtv_comdat_section (section *, const_tree);
/* Well-known sections, each one associated with some sort of *_ASM_OP. */
section *text_section;
section *data_section;
section *readonly_data_section;
section *sdata_section;
section *ctors_section;
section *dtors_section;
section *bss_section;
section *sbss_section;
/* Various forms of common section. All are guaranteed to be nonnull. */
section *tls_comm_section;
section *comm_section;
section *lcomm_section;
/* A SECTION_NOSWITCH section used for declaring global BSS variables.
May be null. */
section *bss_noswitch_section;
/* The section that holds the main exception table, when known. The section
is set either by the target's init_sections hook or by the first call to
switch_to_exception_section. */
section *exception_section;
/* The section that holds the DWARF2 frame unwind information, when known.
The section is set either by the target's init_sections hook or by the
first call to switch_to_eh_frame_section. */
section *eh_frame_section;
/* asm_out_file's current section. This is NULL if no section has yet
been selected or if we lose track of what the current section is. */
section *in_section;
/* True if code for the current function is currently being directed
at the cold section. */
bool in_cold_section_p;
/* The following global holds the "function name" for the code in the
cold section of a function, if hot/cold function splitting is enabled
and there was actually code that went into the cold section. A
pseudo function name is needed for the cold section of code for some
debugging tools that perform symbolization. */
tree cold_function_name = NULL_TREE;
/* A linked list of all the unnamed sections. */
static GTY(()) section *unnamed_sections;
/* Return a nonzero value if DECL has a section attribute. */
#define IN_NAMED_SECTION(DECL) \
(VAR_OR_FUNCTION_DECL_P (DECL) && DECL_SECTION_NAME (DECL) != NULL)
struct section_hasher : ggc_ptr_hash<section>
{
typedef const char *compare_type;
static hashval_t hash (section *);
static bool equal (section *, const char *);
};
/* Hash table of named sections. */
static GTY(()) hash_table<section_hasher> *section_htab;
struct object_block_hasher : ggc_ptr_hash<object_block>
{
typedef const section *compare_type;
static hashval_t hash (object_block *);
static bool equal (object_block *, const section *);
};
/* A table of object_blocks, indexed by section. */
static GTY(()) hash_table<object_block_hasher> *object_block_htab;
/* The next number to use for internal anchor labels. */
static GTY(()) int anchor_labelno;
/* A pool of constants that can be shared between functions. */
static GTY(()) struct rtx_constant_pool *shared_constant_pool;
/* Helper routines for maintaining section_htab. */
bool
section_hasher::equal (section *old, const char *new_name)
{
return strcmp (old->named.name, new_name) == 0;
}
hashval_t
section_hasher::hash (section *old)
{
return htab_hash_string (old->named.name);
}
/* Return a hash value for section SECT. */
static hashval_t
hash_section (section *sect)
{
if (sect->common.flags & SECTION_NAMED)
return htab_hash_string (sect->named.name);
return sect->common.flags & ~SECTION_DECLARED;
}
/* Helper routines for maintaining object_block_htab. */
inline bool
object_block_hasher::equal (object_block *old, const section *new_section)
{
return old->sect == new_section;
}
hashval_t
object_block_hasher::hash (object_block *old)
{
return hash_section (old->sect);
}
/* Return a new unnamed section with the given fields. */
section *
get_unnamed_section (unsigned int flags, void (*callback) (const char *),
const char *data)
{
section *sect;
sect = ggc_alloc<section> ();
sect->unnamed.common.flags = flags | SECTION_UNNAMED;
sect->unnamed.callback = callback;
sect->unnamed.data = data;
sect->unnamed.next = unnamed_sections;
unnamed_sections = sect;
return sect;
}
/* Return a SECTION_NOSWITCH section with the given fields. */
static section *
get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
{
section *sect;
sect = ggc_alloc<section> ();
sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
sect->noswitch.callback = callback;
return sect;
}
/* Return the named section structure associated with NAME. Create
a new section with the given fields if no such structure exists.
When NOT_EXISTING, then fail if the section already exists. Return
the existing section if the SECTION_RETAIN bit doesn't match. Set
the SECTION_WRITE | SECTION_RELRO bits on the existing section
if one of the section flags is SECTION_WRITE | SECTION_RELRO and the
other has none of these flags in named sections and either the section
hasn't been declared yet or has been declared as writable. */
section *
get_section (const char *name, unsigned int flags, tree decl,
bool not_existing)
{
section *sect, **slot;
slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
INSERT);
flags |= SECTION_NAMED;
if (decl != nullptr
&& DECL_P (decl)
&& lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
flags |= SECTION_RETAIN;
if (*slot == NULL)
{
sect = ggc_alloc<section> ();
sect->named.common.flags = flags;
sect->named.name = ggc_strdup (name);
sect->named.decl = decl;
*slot = sect;
}
else
{
if (not_existing)
internal_error ("section already exists: %qs", name);
sect = *slot;
/* It is fine if one of the sections has SECTION_NOTYPE as long as
the other has none of the contrary flags (see the logic at the end
of default_section_type_flags, below). */
if (((sect->common.flags ^ flags) & SECTION_NOTYPE)
&& !((sect->common.flags | flags)
& (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE
| (HAVE_COMDAT_GROUP ? SECTION_LINKONCE : 0))))
{
sect->common.flags |= SECTION_NOTYPE;
flags |= SECTION_NOTYPE;
}
if ((sect->common.flags & ~SECTION_DECLARED) != flags
&& ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
{
/* It is fine if one of the section flags is
SECTION_WRITE | SECTION_RELRO and the other has none of these
flags (i.e. read-only) in named sections and either the
section hasn't been declared yet or has been declared as writable.
In that case just make sure the resulting flags are
SECTION_WRITE | SECTION_RELRO, ie. writable only because of
relocations. */
if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
== (SECTION_WRITE | SECTION_RELRO)
&& (sect->common.flags
& ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
== (flags & ~(SECTION_WRITE | SECTION_RELRO))
&& ((sect->common.flags & SECTION_DECLARED) == 0
|| (sect->common.flags & SECTION_WRITE)))
{
sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
return sect;
}
/* If the SECTION_RETAIN bit doesn't match, return and switch
to a new section later. */
if ((sect->common.flags & SECTION_RETAIN)
!= (flags & SECTION_RETAIN))
return sect;
/* Sanity check user variables for flag changes. */
if (sect->named.decl != NULL
&& DECL_P (sect->named.decl)
&& decl != sect->named.decl)
{
if (decl != NULL && DECL_P (decl))
error ("%+qD causes a section type conflict with %qD",
decl, sect->named.decl);
else
error ("section type conflict with %qD", sect->named.decl);
inform (DECL_SOURCE_LOCATION (sect->named.decl),
"%qD was declared here", sect->named.decl);
}
else if (decl != NULL && DECL_P (decl))
error ("%+qD causes a section type conflict", decl);
else
error ("section type conflict");
/* Make sure we don't error about one section multiple times. */
sect->common.flags |= SECTION_OVERRIDE;
}
}
return sect;
}
/* Return true if the current compilation mode benefits from having
objects grouped into blocks. */
static bool
use_object_blocks_p (void)
{
return flag_section_anchors;
}
/* Return the object_block structure for section SECT. Create a new
structure if we haven't created one already. Return null if SECT
itself is null. Return also null for mergeable sections since
section anchors can't be used in mergeable sections anyway,
because the linker might move objects around, and using the
object blocks infrastructure in that case is both a waste and a
maintenance burden. */
static struct object_block *
get_block_for_section (section *sect)
{
struct object_block *block;
if (sect == NULL)
return NULL;
if (sect->common.flags & SECTION_MERGE)
return NULL;
object_block **slot
= object_block_htab->find_slot_with_hash (sect, hash_section (sect),
INSERT);
block = *slot;
if (block == NULL)
{
block = ggc_cleared_alloc<object_block> ();
block->sect = sect;
*slot = block;
}
return block;
}
/* Create a symbol with label LABEL and place it at byte offset
OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
is not yet known. LABEL must be a garbage-collected string. */
static rtx
create_block_symbol (const char *label, struct object_block *block,
HOST_WIDE_INT offset)
{
rtx symbol;
unsigned int size;
/* Create the extended SYMBOL_REF. */
size = RTX_HDR_SIZE + sizeof (struct block_symbol);
symbol = (rtx) ggc_internal_alloc (size);
/* Initialize the normal SYMBOL_REF fields. */
memset (symbol, 0, size);
PUT_CODE (symbol, SYMBOL_REF);
PUT_MODE (symbol, Pmode);
XSTR (symbol, 0) = label;
SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
/* Initialize the block_symbol stuff. */
SYMBOL_REF_BLOCK (symbol) = block;
SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
return symbol;
}
/* Return a section with a particular name and with whatever SECTION_*
flags section_type_flags deems appropriate. The name of the section
is taken from NAME if nonnull, otherwise it is taken from DECL's
DECL_SECTION_NAME. DECL is the decl associated with the section
(see the section comment for details) and RELOC is as for
section_type_flags. */
section *
get_named_section (tree decl, const char *name, int reloc)
{
unsigned int flags;
if (name == NULL)
{
gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
name = DECL_SECTION_NAME (decl);
}
flags = targetm.section_type_flags (decl, name, reloc);
return get_section (name, flags, decl);
}
/* Worker for resolve_unique_section. */
static bool
set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
{
n->implicit_section = true;
return false;
}
/* If required, set DECL_SECTION_NAME to a unique name. */
void
resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
int flag_function_or_data_sections)
{
if (DECL_SECTION_NAME (decl) == NULL
&& targetm_common.have_named_sections
&& (flag_function_or_data_sections
|| lookup_attribute ("retain", DECL_ATTRIBUTES (decl))
|| DECL_COMDAT_GROUP (decl)))
{
targetm.asm_out.unique_section (decl, reloc);
if (DECL_SECTION_NAME (decl))
symtab_node::get (decl)->call_for_symbol_and_aliases
(set_implicit_section, NULL, true);
}
}
#ifdef BSS_SECTION_ASM_OP
#ifdef ASM_OUTPUT_ALIGNED_BSS
/* Utility function for targets to use in implementing
ASM_OUTPUT_ALIGNED_BSS.
??? It is believed that this function will work in most cases so such
support is localized here. */
static void
asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
const char *name, unsigned HOST_WIDE_INT size,
int align)
{
switch_to_section (bss_section);
ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
#ifdef ASM_DECLARE_OBJECT_NAME
last_assemble_variable_decl = decl;
ASM_DECLARE_OBJECT_NAME (file, name, decl);
#else
/* Standard thing is just output label for the object. */
ASM_OUTPUT_LABEL (file, name);
#endif /* ASM_DECLARE_OBJECT_NAME */
ASM_OUTPUT_SKIP (file, size ? size : 1);
}
#endif
#endif /* BSS_SECTION_ASM_OP */
#ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
/* Return the hot section for function DECL. Return text_section for
null DECLs. */
static section *
hot_function_section (tree decl)
{
if (decl != NULL_TREE
&& DECL_SECTION_NAME (decl) != NULL
&& targetm_common.have_named_sections)
return get_named_section (decl, NULL, 0);
else
return text_section;
}
#endif
/* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
is NULL.
When DECL_SECTION_NAME is non-NULL and it is implicit section and
NAMED_SECTION_SUFFIX is non-NULL, then produce section called
concatenate the name with NAMED_SECTION_SUFFIX.
Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
section *
get_named_text_section (tree decl,
const char *text_section_name,
const char *named_section_suffix)
{
if (decl && DECL_SECTION_NAME (decl))
{
if (named_section_suffix)
{
const char *dsn = DECL_SECTION_NAME (decl);
const char *stripped_name;
char *name, *buffer;
name = (char *) alloca (strlen (dsn) + 1);
memcpy (name, dsn,
strlen (dsn) + 1);
stripped_name = targetm.strip_name_encoding (name);
buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
return get_named_section (decl, buffer, 0);
}
else if (symtab_node::get (decl)->implicit_section)
{
const char *name;
/* Do not try to split gnu_linkonce functions. This gets somewhat
slipperly. */
if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
return NULL;
name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
name = targetm.strip_name_encoding (name);
return get_named_section (decl, ACONCAT ((text_section_name, ".",
name, NULL)), 0);
}
else
return NULL;
}
return get_named_section (decl, text_section_name, 0);
}
/* Choose named function section based on its frequency. */
section *
default_function_section (tree decl, enum node_frequency freq,
bool startup, bool exit)
{
#if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
/* Old GNU linkers have buggy --gc-section support, which sometimes
results in .gcc_except_table* sections being garbage collected. */
if (decl
&& symtab_node::get (decl)->implicit_section)
return NULL;
#endif
if (!flag_reorder_functions
|| !targetm_common.have_named_sections)
return NULL;
/* Startup code should go to startup subsection unless it is
unlikely executed (this happens especially with function splitting
where we can split away unnecessary parts of static constructors. */
if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
{
/* During LTO the tp_first_run profiling will naturally place all
initialization code first. Using separate section is counter-productive
because startup only code may call functions which are no longer
startup only. */
if (!in_lto_p
|| !cgraph_node::get (decl)->tp_first_run
|| !opt_for_fn (decl, flag_profile_reorder_functions))
return get_named_text_section (decl, ".text.startup", NULL);
else
return NULL;
}
/* Similarly for exit. */
if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
return get_named_text_section (decl, ".text.exit", NULL);
/* Group cold functions together, similarly for hot code. */
switch (freq)
{
case NODE_FREQUENCY_UNLIKELY_EXECUTED:
return get_named_text_section (decl, ".text.unlikely", NULL);
case NODE_FREQUENCY_HOT:
return get_named_text_section (decl, ".text.hot", NULL);
/* FALLTHRU */
default:
return NULL;
}
}
/* Return the section for function DECL.
If DECL is NULL_TREE, return the text section. We can be passed
NULL_TREE under some circumstances by dbxout.cc at least.
If FORCE_COLD is true, return cold function section ignoring
the frequency info of cgraph_node. */
static section *
function_section_1 (tree decl, bool force_cold)
{
section *section = NULL;
enum node_frequency freq = NODE_FREQUENCY_NORMAL;
bool startup = false, exit = false;
if (decl)
{
struct cgraph_node *node = cgraph_node::get (decl);
if (node)
{
freq = node->frequency;
startup = node->only_called_at_startup;
exit = node->only_called_at_exit;
}
}
if (force_cold)
freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
#ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
if (decl != NULL_TREE
&& DECL_SECTION_NAME (decl) != NULL)
{
if (targetm.asm_out.function_section)
section = targetm.asm_out.function_section (decl, freq,
startup, exit);
if (section)
return section;
return get_named_section (decl, NULL, 0);
}
else
return targetm.asm_out.select_section
(decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
symtab_node::get (decl)->definition_alignment ());
#else
if (targetm.asm_out.function_section)
section = targetm.asm_out.function_section (decl, freq, startup, exit);
if (section)
return section;
return hot_function_section (decl);
#endif
}
/* Return the section for function DECL.
If DECL is NULL_TREE, return the text section. We can be passed
NULL_TREE under some circumstances by dbxout.cc at least. */
section *
function_section (tree decl)
{
/* Handle cases where function splitting code decides
to put function entry point into unlikely executed section
despite the fact that the function itself is not cold
(i.e. it is called rarely but contains a hot loop that is
better to live in hot subsection for the code locality). */
return function_section_1 (decl,
first_function_block_is_cold);
}
/* Return the section for the current function, take IN_COLD_SECTION_P
into account. */
section *
current_function_section (void)
{
return function_section_1 (current_function_decl, in_cold_section_p);
}
/* Tell assembler to switch to unlikely-to-be-executed text section. */
section *
unlikely_text_section (void)
{
return function_section_1 (current_function_decl, true);
}
/* When called within a function context, return true if the function
has been assigned a cold text section and if SECT is that section.
When called outside a function context, return true if SECT is the
default cold section. */
bool
unlikely_text_section_p (section *sect)
{
return sect == function_section_1 (current_function_decl, true);
}
/* Switch to the other function partition (if inside of hot section
into cold section, otherwise into the hot section). */
void
switch_to_other_text_partition (void)
{
in_cold_section_p = !in_cold_section_p;
switch_to_section (current_function_section ());
}
/* Return the read-only or relocated read-only data section
associated with function DECL. */
section *
default_function_rodata_section (tree decl, bool relocatable)
{
const char* sname;
unsigned int flags;
flags = 0;
if (relocatable)
{
sname = ".data.rel.ro.local";
flags = (SECTION_WRITE | SECTION_RELRO);
}
else
sname = ".rodata";
if (decl && DECL_SECTION_NAME (decl))
{
const char *name = DECL_SECTION_NAME (decl);
if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
{
const char *dot;
size_t len;
char* rname;
dot = strchr (name + 1, '.');
if (!dot)
dot = name;
len = strlen (dot) + strlen (sname) + 1;
rname = (char *) alloca (len);
strcpy (rname, sname);
strcat (rname, dot);
return get_section (rname, (SECTION_LINKONCE | flags), decl);
}
/* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo or
.gnu.linkonce.d.rel.ro.local.foo if the jump table is relocatable. */
else if (DECL_COMDAT_GROUP (decl)
&& startswith (name, ".gnu.linkonce.t."))
{
size_t len;
char *rname;
if (relocatable)
{
len = strlen (name) + strlen (".rel.ro.local") + 1;
rname = (char *) alloca (len);
strcpy (rname, ".gnu.linkonce.d.rel.ro.local");
strcat (rname, name + 15);
}
else
{
len = strlen (name) + 1;
rname = (char *) alloca (len);
memcpy (rname, name, len);
rname[14] = 'r';
}
return get_section (rname, (SECTION_LINKONCE | flags), decl);
}
/* For .text.foo we want to use .rodata.foo. */
else if (flag_function_sections && flag_data_sections
&& startswith (name, ".text."))
{
size_t len = strlen (name) + 1;
char *rname = (char *) alloca (len + strlen (sname) - 5);
memcpy (rname, sname, strlen (sname));
memcpy (rname + strlen (sname), name + 5, len - 5);
return get_section (rname, flags, decl);
}
}
if (relocatable)
return get_section (sname, flags, decl);
else
return readonly_data_section;
}
/* Return the read-only data section associated with function DECL
for targets where that section should be always the single
readonly data section. */
section *
default_no_function_rodata_section (tree, bool)
{
return readonly_data_section;
}
/* A subroutine of mergeable_string_section and mergeable_constant_section. */
static const char *
function_mergeable_rodata_prefix (void)
{
section *s = targetm.asm_out.function_rodata_section (current_function_decl,
false);
if (SECTION_STYLE (s) == SECTION_NAMED)
return s->named.name;
else
return targetm.asm_out.mergeable_rodata_prefix;
}
/* Return the section to use for string merging. */
static section *
mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
unsigned int flags ATTRIBUTE_UNUSED)
{
HOST_WIDE_INT len;
if (HAVE_GAS_SHF_MERGE && flag_merge_constants
&& TREE_CODE (decl) == STRING_CST
&& TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
&& align <= 256
&& (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
&& TREE_STRING_LENGTH (decl) == len)
{
scalar_int_mode mode;
unsigned int modesize;
const char *str;
HOST_WIDE_INT i;
int j, unit;
const char *prefix = function_mergeable_rodata_prefix ();
char *name = (char *) alloca (strlen (prefix) + 30);
mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
modesize = GET_MODE_BITSIZE (mode);
if (modesize >= 8 && modesize <= 256
&& (modesize & (modesize - 1)) == 0)
{
if (align < modesize)
align = modesize;
if (!HAVE_LD_ALIGNED_SHF_MERGE && align > 8)
return readonly_data_section;
str = TREE_STRING_POINTER (decl);
unit = GET_MODE_SIZE (mode);
/* Check for embedded NUL characters. */
for (i = 0; i < len; i += unit)
{
for (j = 0; j < unit; j++)
if (str[i + j] != '\0')
break;
if (j == unit)
break;
}
if (i == len - unit || (unit == 1 && i == len))
{
sprintf (name, "%s.str%d.%d", prefix,
modesize / 8, (int) (align / 8));
flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
return get_section (name, flags, NULL);
}
}
}
return readonly_data_section;
}
/* Return the section to use for constant merging. */
section *
mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
unsigned int flags ATTRIBUTE_UNUSED)
{
if (HAVE_GAS_SHF_MERGE && flag_merge_constants
&& mode != VOIDmode
&& mode != BLKmode
&& known_le (GET_MODE_BITSIZE (mode), align)
&& align >= 8
&& align <= 256
&& (align & (align - 1)) == 0
&& (HAVE_LD_ALIGNED_SHF_MERGE ? 1 : align == 8))
{
const char *prefix = function_mergeable_rodata_prefix ();
char *name = (char *) alloca (strlen (prefix) + 30);
sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
flags |= (align / 8) | SECTION_MERGE;
return get_section (name, flags, NULL);
}
return readonly_data_section;
}
/* Given NAME, a putative register name, discard any customary prefixes. */
static const char *
strip_reg_name (const char *name)
{
#ifdef REGISTER_PREFIX
if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
name += strlen (REGISTER_PREFIX);
#endif
if (name[0] == '%' || name[0] == '#')
name++;
return name;
}
/* The user has asked for a DECL to have a particular name. Set (or
change) it in such a way that we don't prefix an underscore to
it. */
void
set_user_assembler_name (tree decl, const char *name)
{
char *starred = (char *) alloca (strlen (name) + 2);
starred[0] = '*';
strcpy (starred + 1, name);
symtab->change_decl_assembler_name (decl, get_identifier (starred));
SET_DECL_RTL (decl, NULL_RTX);
}
/* Decode an `asm' spec for a declaration as a register name.
Return the register number, or -1 if nothing specified,
or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
or -3 if ASMSPEC is `cc' and is not recognized,
or -4 if ASMSPEC is `memory' and is not recognized.
Accept an exact spelling or a decimal number.
Prefixes such as % are optional. */
int
decode_reg_name_and_count (const char *asmspec, int *pnregs)
{
/* Presume just one register is clobbered. */
*pnregs = 1;
if (asmspec != 0)
{
int i;
/* Get rid of confusing prefixes. */
asmspec = strip_reg_name (asmspec);
/* Allow a decimal number as a "register name". */
for (i = strlen (asmspec) - 1; i >= 0; i--)
if (! ISDIGIT (asmspec[i]))
break;
if (asmspec[0] != 0 && i < 0)
{
i = atoi (asmspec);
if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
return i;
else
return -2;