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