Remove assert in get_def_bb_for_const
[official-gcc.git] / gcc / config / darwin.c
blob0055d8054415ffa229bc69b22e02b9678ecc2c56
1 /* Functions for generic Darwin as target machine for GNU C compiler.
2 Copyright (C) 1989-2016 Free Software Foundation, Inc.
3 Contributed by Apple Computer Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cfghooks.h"
30 #include "df.h"
31 #include "tm_p.h"
32 #include "stringpool.h"
33 #include "insn-config.h"
34 #include "emit-rtl.h"
35 #include "cgraph.h"
36 #include "lto-streamer.h"
37 #include "output.h"
38 #include "varasm.h"
39 #include "stor-layout.h"
40 #include "explow.h"
41 #include "expr.h"
42 #include "langhooks.h"
43 #include "toplev.h"
44 #include "lto-section-names.h"
46 /* Darwin supports a feature called fix-and-continue, which is used
47 for rapid turn around debugging. When code is compiled with the
48 -mfix-and-continue flag, two changes are made to the generated code
49 that allow the system to do things that it would normally not be
50 able to do easily. These changes allow gdb to load in
51 recompilation of a translation unit that has been changed into a
52 running program and replace existing functions and methods of that
53 translation unit with versions of those functions and methods
54 from the newly compiled translation unit. The new functions access
55 the existing static symbols from the old translation unit, if the
56 symbol existed in the unit to be replaced, and from the new
57 translation unit, otherwise.
59 The changes are to insert 5 nops at the beginning of all functions
60 and to use indirection to get at static symbols. The 5 nops
61 are required by consumers of the generated code. Currently, gdb
62 uses this to patch in a jump to the overriding function, this
63 allows all uses of the old name to forward to the replacement,
64 including existing function pointers and virtual methods. See
65 rs6000_emit_prologue for the code that handles the nop insertions.
67 The added indirection allows gdb to redirect accesses to static
68 symbols from the newly loaded translation unit to the existing
69 symbol, if any. @code{static} symbols are special and are handled by
70 setting the second word in the .non_lazy_symbol_pointer data
71 structure to symbol. See indirect_data for the code that handles
72 the extra indirection, and machopic_output_indirection and its use
73 of MACHO_SYMBOL_STATIC for the code that handles @code{static}
74 symbol indirection. */
76 /* For darwin >= 9 (OSX 10.5) the linker is capable of making the necessary
77 branch islands and we no longer need to emit darwin stubs.
78 However, if we are generating code for earlier systems (or for use in the
79 kernel) the stubs might still be required, and this will be set true. */
80 int darwin_emit_branch_islands = false;
82 typedef struct GTY(()) cdtor_record {
83 rtx symbol;
84 int priority; /* [con/de]structor priority */
85 int position; /* original position */
86 } cdtor_record;
88 static GTY(()) vec<cdtor_record, va_gc> *ctors = NULL;
89 static GTY(()) vec<cdtor_record, va_gc> *dtors = NULL;
91 /* A flag to determine whether we are running c++ or obj-c++. This has to be
92 settable from non-c-family contexts too (i.e. we can't use the c_dialect_
93 functions). */
94 int darwin_running_cxx;
96 /* Some code-gen now depends on OS major version numbers (at least). */
97 int generating_for_darwin_version ;
99 /* Section names. */
100 section * darwin_sections[NUM_DARWIN_SECTIONS];
102 /* While we transition to using in-tests instead of ifdef'd code. */
103 #if !HAVE_lo_sum
104 #define gen_macho_high(a,b) (a)
105 #define gen_macho_low(a,b,c) (a)
106 #endif
108 /* True if we're setting __attribute__ ((ms_struct)). */
109 int darwin_ms_struct = false;
111 /* Earlier versions of Darwin as do not recognize an alignment field in
112 .comm directives, this should be set for versions that allow it. */
113 int emit_aligned_common = false;
115 /* A get_unnamed_section callback used to switch to an ObjC section.
116 DIRECTIVE is as for output_section_asm_op. */
118 static void
119 output_objc_section_asm_op (const void *directive)
121 static bool been_here = false;
123 /* The NeXT ObjC Runtime requires these sections to be present and in
124 order in the object. The code below implements this by emitting
125 a section header for each ObjC section the first time that an ObjC
126 section is requested. */
127 if (! been_here)
129 section *saved_in_section = in_section;
130 static const enum darwin_section_enum tomark[] =
132 /* written, cold -> hot */
133 objc_cat_cls_meth_section,
134 objc_cat_inst_meth_section,
135 objc_string_object_section,
136 objc_constant_string_object_section,
137 objc_selector_refs_section,
138 objc_selector_fixup_section,
139 objc_cls_refs_section,
140 objc_class_section,
141 objc_meta_class_section,
142 /* shared, hot -> cold */
143 objc_cls_meth_section,
144 objc_inst_meth_section,
145 objc_protocol_section,
146 objc_class_names_section,
147 objc_meth_var_types_section,
148 objc_meth_var_names_section,
149 objc_category_section,
150 objc_class_vars_section,
151 objc_instance_vars_section,
152 objc_module_info_section,
153 objc_symbols_section,
155 /* ABI=1 */
156 static const enum darwin_section_enum tomarkv1[] =
158 objc1_protocol_ext_section,
159 objc1_class_ext_section,
160 objc1_prop_list_section
162 /* ABI=2 */
163 static const enum darwin_section_enum tomarkv2[] =
165 objc2_message_refs_section,
166 objc2_classdefs_section,
167 objc2_metadata_section,
168 objc2_classrefs_section,
169 objc2_classlist_section,
170 objc2_categorylist_section,
171 objc2_selector_refs_section,
172 objc2_nonlazy_class_section,
173 objc2_nonlazy_category_section,
174 objc2_protocollist_section,
175 objc2_protocolrefs_section,
176 objc2_super_classrefs_section,
177 objc2_image_info_section,
178 objc2_constant_string_object_section
180 size_t i;
182 been_here = true;
183 if (flag_objc_abi < 2)
185 for (i = 0; i < ARRAY_SIZE (tomark); i++)
186 switch_to_section (darwin_sections[tomark[i]]);
187 if (flag_objc_abi == 1)
188 for (i = 0; i < ARRAY_SIZE (tomarkv1); i++)
189 switch_to_section (darwin_sections[tomarkv1[i]]);
191 else
192 for (i = 0; i < ARRAY_SIZE (tomarkv2); i++)
193 switch_to_section (darwin_sections[tomarkv2[i]]);
194 /* Make sure we don't get varasm.c out of sync with us. */
195 switch_to_section (saved_in_section);
197 output_section_asm_op (directive);
201 /* Private flag applied to disable section-anchors in a particular section. */
202 #define SECTION_NO_ANCHOR SECTION_MACH_DEP
205 /* Implement TARGET_ASM_INIT_SECTIONS. */
207 void
208 darwin_init_sections (void)
210 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
211 darwin_sections[NAME] = \
212 get_unnamed_section (FLAGS, (OBJC \
213 ? output_objc_section_asm_op \
214 : output_section_asm_op), \
215 "\t" DIRECTIVE);
216 #include "config/darwin-sections.def"
217 #undef DEF_SECTION
219 readonly_data_section = darwin_sections[const_section];
220 exception_section = darwin_sections[darwin_exception_section];
221 eh_frame_section = darwin_sections[darwin_eh_frame_section];
225 name_needs_quotes (const char *name)
227 int c;
228 while ((c = *name++) != '\0')
229 if (! ISIDNUM (c)
230 && c != '.' && c != '$' && c != '_' )
231 return 1;
232 return 0;
235 /* Return true if SYM_REF can be used without an indirection. */
237 machopic_symbol_defined_p (rtx sym_ref)
239 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
240 return true;
242 /* If a symbol references local and is not an extern to this
243 file, then the symbol might be able to declared as defined. */
244 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
246 /* If the symbol references a variable and the variable is a
247 common symbol, then this symbol is not defined. */
248 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
250 tree decl = SYMBOL_REF_DECL (sym_ref);
251 if (!decl)
252 return true;
253 if (DECL_COMMON (decl))
254 return false;
256 return true;
258 return false;
261 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
262 reference, which will not be changed. */
264 enum machopic_addr_class
265 machopic_classify_symbol (rtx sym_ref)
267 bool function_p;
269 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
270 if (machopic_symbol_defined_p (sym_ref))
271 return (function_p
272 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
273 else
274 return (function_p
275 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
278 #ifndef TARGET_FIX_AND_CONTINUE
279 #define TARGET_FIX_AND_CONTINUE 0
280 #endif
282 /* Indicate when fix-and-continue style code generation is being used
283 and when a reference to data should be indirected so that it can be
284 rebound in a new translation unit to reference the original instance
285 of that data. Symbol names that are for code generation local to
286 the translation unit are bound to the new translation unit;
287 currently this means symbols that begin with L or _OBJC_;
288 otherwise, we indicate that an indirect reference should be made to
289 permit the runtime to rebind new instances of the translation unit
290 to the original instance of the data. */
292 static int
293 indirect_data (rtx sym_ref)
295 int lprefix;
296 const char *name;
298 /* If we aren't generating fix-and-continue code, don't do anything
299 special. */
300 if (TARGET_FIX_AND_CONTINUE == 0)
301 return 0;
303 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
304 are indirected. Symbols that begin with L and _OBJC_ are always
305 bound to the current translation unit as they are used for
306 generated local data of the translation unit. */
308 name = XSTR (sym_ref, 0);
310 lprefix = (((name[0] == '*' || name[0] == '&')
311 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
312 || (strncmp (name, "_OBJC_", 6) == 0));
314 return ! lprefix;
317 static int
318 machopic_data_defined_p (rtx sym_ref)
320 if (indirect_data (sym_ref))
321 return 0;
323 switch (machopic_classify_symbol (sym_ref))
325 case MACHOPIC_DEFINED_DATA:
326 case MACHOPIC_DEFINED_FUNCTION:
327 return 1;
328 default:
329 return 0;
333 void
334 machopic_define_symbol (rtx mem)
336 rtx sym_ref;
338 gcc_assert (GET_CODE (mem) == MEM);
339 sym_ref = XEXP (mem, 0);
340 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
343 /* Return either ORIG or:
345 (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
347 depending on MACHO_DYNAMIC_NO_PIC_P. */
349 machopic_gen_offset (rtx orig)
351 if (MACHO_DYNAMIC_NO_PIC_P)
352 return orig;
353 else
355 /* Play games to avoid marking the function as needing pic if we
356 are being called as part of the cost-estimation process. */
357 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
358 crtl->uses_pic_offset_table = 1;
359 orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
360 UNSPEC_MACHOPIC_OFFSET);
361 return gen_rtx_CONST (Pmode, orig);
365 static GTY(()) const char * function_base_func_name;
366 static GTY(()) int current_pic_label_num;
367 static GTY(()) int emitted_pic_label_num;
369 static void
370 update_pic_label_number_if_needed (void)
372 const char *current_name;
374 /* When we are generating _get_pc thunks within stubs, there is no current
375 function. */
376 if (current_function_decl)
378 current_name =
379 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
380 if (function_base_func_name != current_name)
382 ++current_pic_label_num;
383 function_base_func_name = current_name;
386 else
388 ++current_pic_label_num;
389 function_base_func_name = "L_machopic_stub_dummy";
393 void
394 machopic_output_function_base_name (FILE *file)
396 /* If dynamic-no-pic is on, we should not get here. */
397 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
399 update_pic_label_number_if_needed ();
400 fprintf (file, "L%d$pb", current_pic_label_num);
403 char curr_picbasename[32];
405 const char *
406 machopic_get_function_picbase (void)
408 /* If dynamic-no-pic is on, we should not get here. */
409 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
411 update_pic_label_number_if_needed ();
412 snprintf (curr_picbasename, 32, "L%d$pb", current_pic_label_num);
413 return (const char *) curr_picbasename;
416 bool
417 machopic_should_output_picbase_label (void)
419 update_pic_label_number_if_needed ();
421 if (current_pic_label_num == emitted_pic_label_num)
422 return false;
424 emitted_pic_label_num = current_pic_label_num;
425 return true;
428 /* The suffix attached to non-lazy pointer symbols. */
429 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
430 /* The suffix attached to stub symbols. */
431 #define STUB_SUFFIX "$stub"
433 typedef struct GTY ((for_user)) machopic_indirection
435 /* The SYMBOL_REF for the entity referenced. */
436 rtx symbol;
437 /* The name of the stub or non-lazy pointer. */
438 const char * ptr_name;
439 /* True iff this entry is for a stub (as opposed to a non-lazy
440 pointer). */
441 bool stub_p;
442 /* True iff this stub or pointer has been referenced. */
443 bool used;
444 } machopic_indirection;
446 struct indirection_hasher : ggc_ptr_hash<machopic_indirection>
448 typedef const char *compare_type;
449 static hashval_t hash (machopic_indirection *);
450 static bool equal (machopic_indirection *, const char *);
453 /* A table mapping stub names and non-lazy pointer names to
454 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
456 static GTY (()) hash_table<indirection_hasher> *machopic_indirections;
458 /* Return a hash value for a SLOT in the indirections hash table. */
460 hashval_t
461 indirection_hasher::hash (machopic_indirection *p)
463 return htab_hash_string (p->ptr_name);
466 /* Returns true if the KEY is the same as that associated with
467 SLOT. */
469 bool
470 indirection_hasher::equal (machopic_indirection *s, const char *k)
472 return strcmp (s->ptr_name, k) == 0;
475 /* Return the name of the non-lazy pointer (if STUB_P is false) or
476 stub (if STUB_B is true) corresponding to the given name. */
478 const char *
479 machopic_indirection_name (rtx sym_ref, bool stub_p)
481 char *buffer;
482 const char *name = XSTR (sym_ref, 0);
483 size_t namelen = strlen (name);
484 machopic_indirection *p;
485 bool needs_quotes;
486 const char *suffix;
487 const char *prefix = user_label_prefix;
488 const char *quote = "";
489 tree id;
491 id = maybe_get_identifier (name);
492 if (id)
494 tree id_orig = id;
496 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
497 id = TREE_CHAIN (id);
498 if (id != id_orig)
500 name = IDENTIFIER_POINTER (id);
501 namelen = strlen (name);
505 if (name[0] == '*')
507 prefix = "";
508 ++name;
509 --namelen;
512 needs_quotes = name_needs_quotes (name);
513 if (needs_quotes)
515 quote = "\"";
518 if (stub_p)
519 suffix = STUB_SUFFIX;
520 else
521 suffix = NON_LAZY_POINTER_SUFFIX;
523 buffer = XALLOCAVEC (char, strlen ("&L")
524 + strlen (prefix)
525 + namelen
526 + strlen (suffix)
527 + 2 * strlen (quote)
528 + 1 /* '\0' */);
530 /* Construct the name of the non-lazy pointer or stub. */
531 sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
533 if (!machopic_indirections)
534 machopic_indirections = hash_table<indirection_hasher>::create_ggc (37);
536 machopic_indirection **slot
537 = machopic_indirections->find_slot_with_hash (buffer,
538 htab_hash_string (buffer),
539 INSERT);
540 if (*slot)
542 p = *slot;
544 else
546 p = ggc_alloc<machopic_indirection> ();
547 p->symbol = sym_ref;
548 p->ptr_name = xstrdup (buffer);
549 p->stub_p = stub_p;
550 p->used = false;
551 *slot = p;
554 return p->ptr_name;
557 /* Return the name of the stub for the mcount function. */
559 const char*
560 machopic_mcount_stub_name (void)
562 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
563 return machopic_indirection_name (symbol, /*stub_p=*/true);
566 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
567 or non-lazy pointer as used -- and mark the object to which the
568 pointer/stub refers as used as well, since the pointer/stub will
569 emit a reference to it. */
571 void
572 machopic_validate_stub_or_non_lazy_ptr (const char *name)
574 machopic_indirection *p
575 = machopic_indirections->find_with_hash (name, htab_hash_string (name));
576 if (p && ! p->used)
578 const char *real_name;
579 tree id;
581 p->used = true;
583 /* Do what output_addr_const will do when we actually call it. */
584 if (SYMBOL_REF_DECL (p->symbol))
585 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
587 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
589 id = maybe_get_identifier (real_name);
590 if (id)
591 mark_referenced (id);
595 /* Transform ORIG, which may be any data source, to the corresponding
596 source using indirections. */
599 machopic_indirect_data_reference (rtx orig, rtx reg)
601 rtx ptr_ref = orig;
603 if (! MACHOPIC_INDIRECT)
604 return orig;
606 if (GET_CODE (orig) == SYMBOL_REF)
608 int defined = machopic_data_defined_p (orig);
610 if (defined && MACHO_DYNAMIC_NO_PIC_P)
612 if (DARWIN_PPC)
614 /* Create a new register for CSE opportunities. */
615 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
616 emit_insn (gen_macho_high (hi_reg, orig));
617 emit_insn (gen_macho_low (reg, hi_reg, orig));
618 return reg;
620 else if (DARWIN_X86)
621 return orig;
622 else
623 /* some other cpu -- writeme! */
624 gcc_unreachable ();
626 else if (defined)
628 rtx offset = NULL;
629 if (DARWIN_PPC || HAVE_lo_sum)
630 offset = machopic_gen_offset (orig);
632 if (DARWIN_PPC)
634 rtx hi_sum_reg = (!can_create_pseudo_p ()
635 ? reg
636 : gen_reg_rtx (Pmode));
638 gcc_assert (reg);
640 emit_insn (gen_rtx_SET (hi_sum_reg,
641 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
642 gen_rtx_HIGH (Pmode, offset))));
643 emit_insn (gen_rtx_SET (reg,
644 gen_rtx_LO_SUM (Pmode, hi_sum_reg,
645 copy_rtx (offset))));
647 orig = reg;
649 else if (HAVE_lo_sum)
651 gcc_assert (reg);
653 emit_insn (gen_rtx_SET (reg, gen_rtx_HIGH (Pmode, offset)));
654 emit_insn (gen_rtx_SET (reg, gen_rtx_LO_SUM (Pmode, reg,
655 copy_rtx (offset))));
656 emit_use (pic_offset_table_rtx);
658 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
660 return orig;
663 ptr_ref = (gen_rtx_SYMBOL_REF
664 (Pmode,
665 machopic_indirection_name (orig, /*stub_p=*/false)));
667 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
669 ptr_ref = gen_const_mem (Pmode, ptr_ref);
670 machopic_define_symbol (ptr_ref);
672 if (DARWIN_X86
673 && reg
674 && MACHO_DYNAMIC_NO_PIC_P)
676 emit_insn (gen_rtx_SET (reg, ptr_ref));
677 ptr_ref = reg;
680 return ptr_ref;
682 else if (GET_CODE (orig) == CONST)
684 /* If "(const (plus ...", walk the PLUS and return that result.
685 PLUS processing (below) will restore the "(const ..." if
686 appropriate. */
687 if (GET_CODE (XEXP (orig, 0)) == PLUS)
688 return machopic_indirect_data_reference (XEXP (orig, 0), reg);
689 else
690 return orig;
692 else if (GET_CODE (orig) == MEM)
694 XEXP (ptr_ref, 0) =
695 machopic_indirect_data_reference (XEXP (orig, 0), reg);
696 return ptr_ref;
698 else if (GET_CODE (orig) == PLUS)
700 rtx base, result;
701 /* When the target is i386, this code prevents crashes due to the
702 compiler's ignorance on how to move the PIC base register to
703 other registers. (The reload phase sometimes introduces such
704 insns.) */
705 if (GET_CODE (XEXP (orig, 0)) == REG
706 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
707 /* Prevent the same register from being erroneously used
708 as both the base and index registers. */
709 && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST))
710 && reg)
712 emit_move_insn (reg, XEXP (orig, 0));
713 XEXP (ptr_ref, 0) = reg;
714 return ptr_ref;
717 /* Legitimize both operands of the PLUS. */
718 base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
719 orig = machopic_indirect_data_reference (XEXP (orig, 1),
720 (base == reg ? 0 : reg));
721 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
722 result = plus_constant (Pmode, base, INTVAL (orig));
723 else
724 result = gen_rtx_PLUS (Pmode, base, orig);
726 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
728 if (reg)
730 emit_move_insn (reg, result);
731 result = reg;
733 else
735 result = force_reg (GET_MODE (result), result);
739 return result;
741 return ptr_ref;
744 /* Transform TARGET (a MEM), which is a function call target, to the
745 corresponding symbol_stub if necessary. Return a new MEM. */
748 machopic_indirect_call_target (rtx target)
750 if (! darwin_emit_branch_islands)
751 return target;
753 if (GET_CODE (target) != MEM)
754 return target;
756 if (MACHOPIC_INDIRECT
757 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
758 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
759 & MACHO_SYMBOL_FLAG_DEFINED))
761 rtx sym_ref = XEXP (target, 0);
762 const char *stub_name = machopic_indirection_name (sym_ref,
763 /*stub_p=*/true);
764 machine_mode mode = GET_MODE (sym_ref);
766 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
767 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
768 MEM_READONLY_P (target) = 1;
769 MEM_NOTRAP_P (target) = 1;
772 return target;
776 machopic_legitimize_pic_address (rtx orig, machine_mode mode, rtx reg)
778 rtx pic_ref = orig;
780 if (! MACHOPIC_INDIRECT)
781 return orig;
783 /* First handle a simple SYMBOL_REF or LABEL_REF */
784 if (GET_CODE (orig) == LABEL_REF
785 || (GET_CODE (orig) == SYMBOL_REF
788 /* addr(foo) = &func+(foo-func) */
789 orig = machopic_indirect_data_reference (orig, reg);
791 if (GET_CODE (orig) == PLUS
792 && GET_CODE (XEXP (orig, 0)) == REG)
794 if (reg == 0)
795 return force_reg (mode, orig);
797 emit_move_insn (reg, orig);
798 return reg;
801 if (GET_CODE (orig) == MEM)
803 if (reg == 0)
805 gcc_assert (!reload_in_progress);
806 reg = gen_reg_rtx (Pmode);
809 #if HAVE_lo_sum
810 if (MACHO_DYNAMIC_NO_PIC_P
811 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
812 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
814 #if defined (TARGET_TOC) /* ppc */
815 rtx temp_reg = (!can_create_pseudo_p ()
816 ? reg :
817 gen_reg_rtx (Pmode));
818 rtx asym = XEXP (orig, 0);
819 rtx mem;
821 emit_insn (gen_macho_high (temp_reg, asym));
822 mem = gen_const_mem (GET_MODE (orig),
823 gen_rtx_LO_SUM (Pmode, temp_reg,
824 copy_rtx (asym)));
825 emit_insn (gen_rtx_SET (reg, mem));
826 #else
827 /* Some other CPU -- WriteMe! but right now there are no other
828 platforms that can use dynamic-no-pic */
829 gcc_unreachable ();
830 #endif
831 pic_ref = reg;
833 else
834 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
835 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
837 rtx offset = machopic_gen_offset (XEXP (orig, 0));
838 #if defined (TARGET_TOC) /* i.e., PowerPC */
839 /* Generating a new reg may expose opportunities for
840 common subexpression elimination. */
841 rtx hi_sum_reg = (!can_create_pseudo_p ()
842 ? reg
843 : gen_reg_rtx (Pmode));
844 rtx mem;
845 rtx insn;
846 rtx sum;
848 sum = gen_rtx_HIGH (Pmode, offset);
849 if (! MACHO_DYNAMIC_NO_PIC_P)
850 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
852 emit_insn (gen_rtx_SET (hi_sum_reg, sum));
854 mem = gen_const_mem (GET_MODE (orig),
855 gen_rtx_LO_SUM (Pmode,
856 hi_sum_reg,
857 copy_rtx (offset)));
858 insn = emit_insn (gen_rtx_SET (reg, mem));
859 set_unique_reg_note (insn, REG_EQUAL, pic_ref);
861 pic_ref = reg;
862 #else
863 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
865 emit_insn (gen_rtx_SET (reg,
866 gen_rtx_HIGH (Pmode,
867 gen_rtx_CONST (Pmode,
868 offset))));
869 emit_insn (gen_rtx_SET (reg,
870 gen_rtx_LO_SUM (Pmode, reg,
871 gen_rtx_CONST (Pmode,
872 copy_rtx (offset)))));
873 pic_ref = gen_rtx_PLUS (Pmode,
874 pic_offset_table_rtx, reg);
875 #endif
877 else
878 #endif /* HAVE_lo_sum */
880 rtx pic = pic_offset_table_rtx;
881 if (GET_CODE (pic) != REG)
883 emit_move_insn (reg, pic);
884 pic = reg;
886 #if 0
887 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
888 #endif
890 if (reload_in_progress)
891 df_set_regs_ever_live (REGNO (pic), true);
892 pic_ref = gen_rtx_PLUS (Pmode, pic,
893 machopic_gen_offset (XEXP (orig, 0)));
896 #if !defined (TARGET_TOC)
897 emit_move_insn (reg, pic_ref);
898 pic_ref = gen_const_mem (GET_MODE (orig), reg);
899 #endif
901 else
904 #if HAVE_lo_sum
905 if (GET_CODE (orig) == SYMBOL_REF
906 || GET_CODE (orig) == LABEL_REF)
908 rtx offset = machopic_gen_offset (orig);
909 #if defined (TARGET_TOC) /* i.e., PowerPC */
910 rtx hi_sum_reg;
912 if (reg == 0)
914 gcc_assert (!reload_in_progress);
915 reg = gen_reg_rtx (Pmode);
918 hi_sum_reg = reg;
920 emit_insn (gen_rtx_SET (hi_sum_reg,
921 (MACHO_DYNAMIC_NO_PIC_P)
922 ? gen_rtx_HIGH (Pmode, offset)
923 : gen_rtx_PLUS (Pmode,
924 pic_offset_table_rtx,
925 gen_rtx_HIGH (Pmode,
926 offset))));
927 emit_insn (gen_rtx_SET (reg,
928 gen_rtx_LO_SUM (Pmode,
929 hi_sum_reg,
930 copy_rtx (offset))));
931 pic_ref = reg;
932 #else
933 emit_insn (gen_rtx_SET (reg, gen_rtx_HIGH (Pmode, offset)));
934 emit_insn (gen_rtx_SET (reg,
935 gen_rtx_LO_SUM (Pmode, reg,
936 copy_rtx (offset))));
937 pic_ref = gen_rtx_PLUS (Pmode,
938 pic_offset_table_rtx, reg);
939 #endif
941 else
942 #endif /* HAVE_lo_sum */
944 if (REG_P (orig)
945 || GET_CODE (orig) == SUBREG)
947 return orig;
949 else
951 rtx pic = pic_offset_table_rtx;
952 if (GET_CODE (pic) != REG)
954 emit_move_insn (reg, pic);
955 pic = reg;
957 #if 0
958 emit_use (pic_offset_table_rtx);
959 #endif
960 if (reload_in_progress)
961 df_set_regs_ever_live (REGNO (pic), true);
962 pic_ref = gen_rtx_PLUS (Pmode,
963 pic,
964 machopic_gen_offset (orig));
969 if (GET_CODE (pic_ref) != REG)
971 if (reg != 0)
973 emit_move_insn (reg, pic_ref);
974 return reg;
976 else
978 return force_reg (mode, pic_ref);
981 else
983 return pic_ref;
987 else if (GET_CODE (orig) == SYMBOL_REF)
988 return orig;
990 else if (GET_CODE (orig) == PLUS
991 && (GET_CODE (XEXP (orig, 0)) == MEM
992 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
993 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
994 && XEXP (orig, 0) != pic_offset_table_rtx
995 && GET_CODE (XEXP (orig, 1)) != REG)
998 rtx base;
999 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
1001 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1002 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
1003 Pmode, (base == reg ? 0 : reg));
1004 if (GET_CODE (orig) == CONST_INT)
1006 pic_ref = plus_constant (Pmode, base, INTVAL (orig));
1007 is_complex = 1;
1009 else
1010 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
1012 if (reg && is_complex)
1014 emit_move_insn (reg, pic_ref);
1015 pic_ref = reg;
1017 /* Likewise, should we set special REG_NOTEs here? */
1020 else if (GET_CODE (orig) == CONST)
1022 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1025 else if (GET_CODE (orig) == MEM
1026 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
1028 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1029 addr = replace_equiv_address (orig, addr);
1030 emit_move_insn (reg, addr);
1031 pic_ref = reg;
1034 return pic_ref;
1037 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
1038 DATA is the FILE* for assembly output. Called from
1039 htab_traverse. */
1042 machopic_output_indirection (machopic_indirection **slot, FILE *asm_out_file)
1044 machopic_indirection *p = *slot;
1045 rtx symbol;
1046 const char *sym_name;
1047 const char *ptr_name;
1049 if (!p->used)
1050 return 1;
1052 symbol = p->symbol;
1053 sym_name = XSTR (symbol, 0);
1054 ptr_name = p->ptr_name;
1056 if (p->stub_p)
1058 char *sym;
1059 char *stub;
1060 tree id;
1062 id = maybe_get_identifier (sym_name);
1063 if (id)
1065 tree id_orig = id;
1067 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
1068 id = TREE_CHAIN (id);
1069 if (id != id_orig)
1070 sym_name = IDENTIFIER_POINTER (id);
1073 sym = XALLOCAVEC (char, strlen (sym_name) + 2);
1074 if (sym_name[0] == '*' || sym_name[0] == '&')
1075 strcpy (sym, sym_name + 1);
1076 else if (sym_name[0] == '-' || sym_name[0] == '+')
1077 strcpy (sym, sym_name);
1078 else
1079 sprintf (sym, "%s%s", user_label_prefix, sym_name);
1081 stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
1082 if (ptr_name[0] == '*' || ptr_name[0] == '&')
1083 strcpy (stub, ptr_name + 1);
1084 else
1085 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
1087 machopic_output_stub (asm_out_file, sym, stub);
1089 else if (! indirect_data (symbol)
1090 && (machopic_symbol_defined_p (symbol)
1091 || SYMBOL_REF_LOCAL_P (symbol)))
1093 switch_to_section (data_section);
1094 assemble_align (GET_MODE_ALIGNMENT (Pmode));
1095 assemble_label (asm_out_file, ptr_name);
1096 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
1097 GET_MODE_SIZE (Pmode),
1098 GET_MODE_ALIGNMENT (Pmode), 1);
1100 else
1102 rtx init = const0_rtx;
1104 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
1106 /* Mach-O symbols are passed around in code through indirect
1107 references and the original symbol_ref hasn't passed through
1108 the generic handling and reference-catching in
1109 output_operand, so we need to manually mark weak references
1110 as such. */
1111 if (SYMBOL_REF_WEAK (symbol))
1113 tree decl = SYMBOL_REF_DECL (symbol);
1114 gcc_assert (DECL_P (decl));
1116 if (decl != NULL_TREE
1117 && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
1118 /* Handle only actual external-only definitions, not
1119 e.g. extern inline code or variables for which
1120 storage has been allocated. */
1121 && !TREE_STATIC (decl))
1123 fputs ("\t.weak_reference ", asm_out_file);
1124 assemble_name (asm_out_file, sym_name);
1125 fputc ('\n', asm_out_file);
1129 assemble_name (asm_out_file, ptr_name);
1130 fprintf (asm_out_file, ":\n");
1132 fprintf (asm_out_file, "\t.indirect_symbol ");
1133 assemble_name (asm_out_file, sym_name);
1134 fprintf (asm_out_file, "\n");
1136 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1137 have their symbol name instead of 0 in the second entry of
1138 the non-lazy symbol pointer data structure when they are
1139 defined. This allows the runtime to rebind newer instances
1140 of the translation unit with the original instance of the
1141 symbol. */
1143 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1144 && machopic_symbol_defined_p (symbol))
1145 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1147 assemble_integer (init, GET_MODE_SIZE (Pmode),
1148 GET_MODE_ALIGNMENT (Pmode), 1);
1151 return 1;
1154 void
1155 machopic_finish (FILE *asm_out_file)
1157 if (machopic_indirections)
1158 machopic_indirections
1159 ->traverse_noresize<FILE *, machopic_output_indirection> (asm_out_file);
1163 machopic_operand_p (rtx op)
1165 if (MACHOPIC_JUST_INDIRECT)
1166 return (GET_CODE (op) == SYMBOL_REF
1167 && machopic_symbol_defined_p (op));
1168 else
1169 return (GET_CODE (op) == CONST
1170 && GET_CODE (XEXP (op, 0)) == UNSPEC
1171 && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
1174 /* This function records whether a given name corresponds to a defined
1175 or undefined function or variable, for machopic_classify_ident to
1176 use later. */
1178 void
1179 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
1181 rtx sym_ref;
1183 /* Do the standard encoding things first. */
1184 default_encode_section_info (decl, rtl, first);
1186 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1187 return;
1189 sym_ref = XEXP (rtl, 0);
1190 if (TREE_CODE (decl) == VAR_DECL)
1191 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1193 if (!DECL_EXTERNAL (decl)
1194 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1195 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1196 && ((TREE_STATIC (decl)
1197 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1198 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1199 && DECL_INITIAL (decl) != error_mark_node)))
1200 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1202 if (! TREE_PUBLIC (decl))
1203 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
1206 void
1207 darwin_mark_decl_preserved (const char *name)
1209 /* Actually we shouldn't mark any local symbol this way, but for now
1210 this only happens with ObjC meta-data. */
1211 if (darwin_label_is_anonymous_local_objc_name (name))
1212 return;
1214 fprintf (asm_out_file, "\t.no_dead_strip ");
1215 assemble_name (asm_out_file, name);
1216 fputc ('\n', asm_out_file);
1219 static section *
1220 darwin_rodata_section (int weak, bool zsize)
1222 return (weak
1223 ? darwin_sections[const_coal_section]
1224 : (zsize ? darwin_sections[zobj_const_section]
1225 : darwin_sections[const_section]));
1228 static section *
1229 darwin_mergeable_string_section (tree exp,
1230 unsigned HOST_WIDE_INT align)
1232 /* Darwin's ld expects to see non-writable string literals in the .cstring
1233 section. Later versions of ld check and complain when CFStrings are
1234 enabled. Therefore we shall force the strings into .cstring since we
1235 don't support writable ones anyway. */
1236 if ((darwin_constant_cfstrings || flag_merge_constants)
1237 && TREE_CODE (exp) == STRING_CST
1238 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1239 && align <= 256
1240 && (int_size_in_bytes (TREE_TYPE (exp))
1241 == TREE_STRING_LENGTH (exp))
1242 && ((size_t) TREE_STRING_LENGTH (exp)
1243 == strlen (TREE_STRING_POINTER (exp)) + 1))
1244 return darwin_sections[cstring_section];
1246 if (DARWIN_SECTION_ANCHORS && flag_section_anchors
1247 && TREE_CODE (exp) == STRING_CST
1248 && TREE_STRING_LENGTH (exp) == 0)
1249 return darwin_sections[zobj_const_section];
1251 return readonly_data_section;
1254 #ifndef HAVE_GAS_LITERAL16
1255 #define HAVE_GAS_LITERAL16 0
1256 #endif
1258 static section *
1259 darwin_mergeable_constant_section (tree exp,
1260 unsigned HOST_WIDE_INT align,
1261 bool zsize)
1263 machine_mode mode = DECL_MODE (exp);
1264 unsigned int modesize = GET_MODE_BITSIZE (mode);
1266 if (DARWIN_SECTION_ANCHORS
1267 && flag_section_anchors
1268 && zsize)
1269 return darwin_sections[zobj_const_section];
1271 if (flag_merge_constants
1272 && mode != VOIDmode
1273 && mode != BLKmode
1274 && modesize <= align
1275 && align >= 8
1276 && align <= 256
1277 && (align & (align -1)) == 0)
1279 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1281 if (TREE_CODE (size) == INTEGER_CST)
1283 if (wi::eq_p (size, 4))
1284 return darwin_sections[literal4_section];
1285 else if (wi::eq_p (size, 8))
1286 return darwin_sections[literal8_section];
1287 else if (HAVE_GAS_LITERAL16
1288 && TARGET_64BIT
1289 && wi::eq_p (size, 16))
1290 return darwin_sections[literal16_section];
1294 return readonly_data_section;
1297 section *
1298 darwin_tm_clone_table_section (void)
1300 return get_named_section (NULL,
1301 "__DATA,__tm_clone_table,regular,no_dead_strip",
1306 machopic_reloc_rw_mask (void)
1308 return MACHOPIC_INDIRECT ? 3 : 0;
1311 /* We have to deal with ObjC/C++ metadata section placement in the common
1312 code, since it will also be called from LTO.
1314 Return metadata attributes, if present (searching for ABI=2 first)
1315 Return NULL_TREE if no such attributes are found. */
1317 static tree
1318 is_objc_metadata (tree decl)
1320 if (DECL_P (decl)
1321 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1322 && DECL_ATTRIBUTES (decl))
1324 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1325 if (meta)
1326 return meta;
1327 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1328 if (meta)
1329 return meta;
1331 return NULL_TREE;
1334 static int classes_seen;
1335 static int objc_metadata_seen;
1337 /* Return the section required for Objective C ABI 2 metadata. */
1338 static section *
1339 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1341 const char *p;
1342 tree ident = TREE_VALUE (meta);
1343 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1344 p = IDENTIFIER_POINTER (ident);
1346 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi == 2);
1348 objc_metadata_seen = 1;
1350 if (base == data_section)
1351 base = darwin_sections[objc2_metadata_section];
1353 /* Most of the OBJC2 META-data end up in the base section, so check it
1354 first. */
1355 if (!strncmp (p, "V2_BASE", 7))
1356 return base;
1357 else if (!strncmp (p, "V2_STRG", 7))
1358 return darwin_sections[cstring_section];
1360 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
1361 return darwin_sections[objc2_classdefs_section];
1362 else if (!strncmp (p, "V2_MREF", 7))
1363 return darwin_sections[objc2_message_refs_section];
1364 else if (!strncmp (p, "V2_CLRF", 7))
1365 return darwin_sections[objc2_classrefs_section];
1366 else if (!strncmp (p, "V2_SURF", 7))
1367 return darwin_sections[objc2_super_classrefs_section];
1368 else if (!strncmp (p, "V2_NLCL", 7))
1369 return darwin_sections[objc2_nonlazy_class_section];
1370 else if (!strncmp (p, "V2_CLAB", 7))
1372 classes_seen = 1;
1373 return darwin_sections[objc2_classlist_section];
1375 else if (!strncmp (p, "V2_SRFS", 7))
1376 return darwin_sections[objc2_selector_refs_section];
1377 else if (!strncmp (p, "V2_NLCA", 7))
1378 return darwin_sections[objc2_nonlazy_category_section];
1379 else if (!strncmp (p, "V2_CALA", 7))
1380 return darwin_sections[objc2_categorylist_section];
1382 else if (!strncmp (p, "V2_PLST", 7))
1383 return darwin_sections[objc2_protocollist_section];
1384 else if (!strncmp (p, "V2_PRFS", 7))
1385 return darwin_sections[objc2_protocolrefs_section];
1387 else if (!strncmp (p, "V2_INFO", 7))
1388 return darwin_sections[objc2_image_info_section];
1390 else if (!strncmp (p, "V2_EHTY", 7))
1391 return darwin_sections[data_coal_section];
1393 else if (!strncmp (p, "V2_CSTR", 7))
1394 return darwin_sections[objc2_constant_string_object_section];
1396 /* Not recognized, default. */
1397 return base;
1400 /* Return the section required for Objective C ABI 0/1 metadata. */
1401 static section *
1402 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1404 const char *p;
1405 tree ident = TREE_VALUE (meta);
1406 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1407 p = IDENTIFIER_POINTER (ident);
1409 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi < 2);
1411 objc_metadata_seen = 1;
1413 /* String sections first, cos there are lots of strings. */
1414 if (!strncmp (p, "V1_STRG", 7))
1415 return darwin_sections[cstring_section];
1416 else if (!strncmp (p, "V1_CLSN", 7))
1417 return darwin_sections[objc_class_names_section];
1418 else if (!strncmp (p, "V1_METN", 7))
1419 return darwin_sections[objc_meth_var_names_section];
1420 else if (!strncmp (p, "V1_METT", 7))
1421 return darwin_sections[objc_meth_var_types_section];
1423 else if (!strncmp (p, "V1_CLAS", 7))
1425 classes_seen = 1;
1426 return darwin_sections[objc_class_section];
1428 else if (!strncmp (p, "V1_META", 7))
1429 return darwin_sections[objc_meta_class_section];
1430 else if (!strncmp (p, "V1_CATG", 7))
1431 return darwin_sections[objc_category_section];
1432 else if (!strncmp (p, "V1_PROT", 7))
1433 return darwin_sections[objc_protocol_section];
1435 else if (!strncmp (p, "V1_CLCV", 7))
1436 return darwin_sections[objc_class_vars_section];
1437 else if (!strncmp (p, "V1_CLIV", 7))
1438 return darwin_sections[objc_instance_vars_section];
1440 else if (!strncmp (p, "V1_CLCM", 7))
1441 return darwin_sections[objc_cls_meth_section];
1442 else if (!strncmp (p, "V1_CLIM", 7))
1443 return darwin_sections[objc_inst_meth_section];
1444 else if (!strncmp (p, "V1_CACM", 7))
1445 return darwin_sections[objc_cat_cls_meth_section];
1446 else if (!strncmp (p, "V1_CAIM", 7))
1447 return darwin_sections[objc_cat_inst_meth_section];
1448 else if (!strncmp (p, "V1_PNSM", 7))
1449 return darwin_sections[objc_cat_inst_meth_section];
1450 else if (!strncmp (p, "V1_PCLM", 7))
1451 return darwin_sections[objc_cat_cls_meth_section];
1453 else if (!strncmp (p, "V1_CLPR", 7))
1454 return darwin_sections[objc_cat_cls_meth_section];
1455 else if (!strncmp (p, "V1_CAPR", 7))
1456 return darwin_sections[objc_category_section]; /* ??? CHECK me. */
1458 else if (!strncmp (p, "V1_PRFS", 7))
1459 return darwin_sections[objc_cat_cls_meth_section];
1460 else if (!strncmp (p, "V1_CLRF", 7))
1461 return darwin_sections[objc_cls_refs_section];
1462 else if (!strncmp (p, "V1_SRFS", 7))
1463 return darwin_sections[objc_selector_refs_section];
1465 else if (!strncmp (p, "V1_MODU", 7))
1466 return darwin_sections[objc_module_info_section];
1467 else if (!strncmp (p, "V1_SYMT", 7))
1468 return darwin_sections[objc_symbols_section];
1469 else if (!strncmp (p, "V1_INFO", 7))
1470 return darwin_sections[objc_image_info_section];
1472 else if (!strncmp (p, "V1_PLST", 7))
1473 return darwin_sections[objc1_prop_list_section];
1474 else if (!strncmp (p, "V1_PEXT", 7))
1475 return darwin_sections[objc1_protocol_ext_section];
1476 else if (!strncmp (p, "V1_CEXT", 7))
1477 return darwin_sections[objc1_class_ext_section];
1479 else if (!strncmp (p, "V2_CSTR", 7))
1480 return darwin_sections[objc_constant_string_object_section];
1482 return base;
1485 section *
1486 machopic_select_section (tree decl,
1487 int reloc,
1488 unsigned HOST_WIDE_INT align)
1490 bool zsize, one, weak, ro;
1491 section *base_section = NULL;
1493 weak = (DECL_P (decl)
1494 && DECL_WEAK (decl)
1495 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
1497 zsize = (DECL_P (decl)
1498 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1499 && tree_to_uhwi (DECL_SIZE_UNIT (decl)) == 0);
1501 one = DECL_P (decl)
1502 && TREE_CODE (decl) == VAR_DECL
1503 && DECL_COMDAT_GROUP (decl);
1505 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
1507 switch (categorize_decl_for_section (decl, reloc))
1509 case SECCAT_TEXT:
1510 gcc_unreachable ();
1511 break;
1513 case SECCAT_RODATA:
1514 case SECCAT_SRODATA:
1515 base_section = darwin_rodata_section (weak, zsize);
1516 break;
1518 case SECCAT_RODATA_MERGE_STR:
1519 base_section = darwin_mergeable_string_section (decl, align);
1520 break;
1522 case SECCAT_RODATA_MERGE_STR_INIT:
1523 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1524 break;
1526 case SECCAT_RODATA_MERGE_CONST:
1527 base_section = darwin_mergeable_constant_section (decl, align, zsize);
1528 break;
1530 case SECCAT_DATA:
1531 case SECCAT_DATA_REL:
1532 case SECCAT_DATA_REL_LOCAL:
1533 case SECCAT_DATA_REL_RO:
1534 case SECCAT_DATA_REL_RO_LOCAL:
1535 case SECCAT_SDATA:
1536 case SECCAT_TDATA:
1537 if (weak || one)
1539 if (ro)
1540 base_section = darwin_sections[const_data_coal_section];
1541 else
1542 base_section = darwin_sections[data_coal_section];
1544 else if (DARWIN_SECTION_ANCHORS
1545 && flag_section_anchors
1546 && zsize)
1548 /* If we're doing section anchors, then punt zero-sized objects into
1549 their own sections so that they don't interfere with offset
1550 computation for the remaining vars. This does not need to be done
1551 for stuff in mergeable sections, since these are ineligible for
1552 anchors. */
1553 if (ro)
1554 base_section = darwin_sections[zobj_const_data_section];
1555 else
1556 base_section = darwin_sections[zobj_data_section];
1558 else if (ro)
1559 base_section = darwin_sections[const_data_section];
1560 else
1561 base_section = data_section;
1562 break;
1563 case SECCAT_BSS:
1564 case SECCAT_SBSS:
1565 case SECCAT_TBSS:
1566 if (weak || one)
1567 base_section = darwin_sections[data_coal_section];
1568 else
1570 if (!TREE_PUBLIC (decl))
1571 base_section = lcomm_section;
1572 else if (bss_noswitch_section)
1573 base_section = bss_noswitch_section;
1574 else
1575 base_section = data_section;
1577 break;
1579 default:
1580 gcc_unreachable ();
1583 /* Darwin weird special cases.
1584 a) OBJC Meta-data. */
1585 if (DECL_P (decl)
1586 && (TREE_CODE (decl) == VAR_DECL
1587 || TREE_CODE (decl) == CONST_DECL)
1588 && DECL_ATTRIBUTES (decl))
1590 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1591 if (meta)
1592 return darwin_objc2_section (decl, meta, base_section);
1593 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1594 if (meta)
1595 return darwin_objc1_section (decl, meta, base_section);
1596 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
1597 if (meta)
1598 return base_section; /* GNU runtime is happy with it all in one pot. */
1601 /* b) Constant string objects. */
1602 if (TREE_CODE (decl) == CONSTRUCTOR
1603 && TREE_TYPE (decl)
1604 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1605 && TYPE_NAME (TREE_TYPE (decl)))
1607 tree name = TYPE_NAME (TREE_TYPE (decl));
1608 if (TREE_CODE (name) == TYPE_DECL)
1609 name = DECL_NAME (name);
1611 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1613 if (flag_next_runtime)
1615 if (flag_objc_abi == 2)
1616 return darwin_sections[objc2_constant_string_object_section];
1617 else
1618 return darwin_sections[objc_constant_string_object_section];
1620 else
1621 return darwin_sections[objc_string_object_section];
1623 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
1624 return darwin_sections[cfstring_constant_object_section];
1625 else
1626 return base_section;
1628 /* c) legacy meta-data selection. */
1629 else if (TREE_CODE (decl) == VAR_DECL
1630 && DECL_NAME (decl)
1631 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1632 && IDENTIFIER_POINTER (DECL_NAME (decl))
1633 && flag_next_runtime
1634 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1636 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1637 static bool warned_objc_46 = false;
1638 /* We shall assert that zero-sized objects are an error in ObjC
1639 meta-data. */
1640 gcc_assert (tree_to_uhwi (DECL_SIZE_UNIT (decl)) != 0);
1642 /* ??? This mechanism for determining the metadata section is
1643 broken when LTO is in use, since the frontend that generated
1644 the data is not identified. We will keep the capability for
1645 the short term - in case any non-Objective-C programs are using
1646 it to place data in specified sections. */
1647 if (!warned_objc_46)
1649 location_t loc = DECL_SOURCE_LOCATION (decl);
1650 warning_at (loc, 0, "the use of _OBJC_-prefixed variable names"
1651 " to select meta-data sections is deprecated at 4.6"
1652 " and will be removed in 4.7");
1653 warned_objc_46 = true;
1656 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1657 return darwin_sections[objc_cls_meth_section];
1658 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1659 return darwin_sections[objc_inst_meth_section];
1660 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29))
1661 return darwin_sections[objc_cat_cls_meth_section];
1662 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32))
1663 return darwin_sections[objc_cat_inst_meth_section];
1664 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1665 return darwin_sections[objc_class_vars_section];
1666 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1667 return darwin_sections[objc_instance_vars_section];
1668 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1669 return darwin_sections[objc_cat_cls_meth_section];
1670 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1671 return darwin_sections[objc_class_names_section];
1672 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1673 return darwin_sections[objc_meth_var_names_section];
1674 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1675 return darwin_sections[objc_meth_var_types_section];
1676 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1677 return darwin_sections[objc_cls_refs_section];
1678 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1679 return darwin_sections[objc_class_section];
1680 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1681 return darwin_sections[objc_meta_class_section];
1682 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1683 return darwin_sections[objc_category_section];
1684 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1685 return darwin_sections[objc_selector_refs_section];
1686 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1687 return darwin_sections[objc_selector_fixup_section];
1688 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1689 return darwin_sections[objc_symbols_section];
1690 else if (!strncmp (name, "_OBJC_MODULES", 13))
1691 return darwin_sections[objc_module_info_section];
1692 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1693 return darwin_sections[objc_image_info_section];
1694 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1695 return darwin_sections[objc_cat_inst_meth_section];
1696 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1697 return darwin_sections[objc_cat_cls_meth_section];
1698 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1699 return darwin_sections[objc_cat_cls_meth_section];
1700 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1701 return darwin_sections[objc_protocol_section];
1702 else
1703 return base_section;
1706 return base_section;
1709 /* This can be called with address expressions as "rtx".
1710 They must go in "const". */
1712 section *
1713 machopic_select_rtx_section (machine_mode mode, rtx x,
1714 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1716 if (GET_MODE_SIZE (mode) == 8
1717 && (GET_CODE (x) == CONST_INT
1718 || GET_CODE (x) == CONST_WIDE_INT
1719 || GET_CODE (x) == CONST_DOUBLE))
1720 return darwin_sections[literal8_section];
1721 else if (GET_MODE_SIZE (mode) == 4
1722 && (GET_CODE (x) == CONST_INT
1723 || GET_CODE (x) == CONST_WIDE_INT
1724 || GET_CODE (x) == CONST_DOUBLE))
1725 return darwin_sections[literal4_section];
1726 else if (HAVE_GAS_LITERAL16
1727 && TARGET_64BIT
1728 && GET_MODE_SIZE (mode) == 16
1729 && (GET_CODE (x) == CONST_INT
1730 || GET_CODE (x) == CONST_WIDE_INT
1731 || GET_CODE (x) == CONST_DOUBLE
1732 || GET_CODE (x) == CONST_VECTOR))
1733 return darwin_sections[literal16_section];
1734 else if (MACHOPIC_INDIRECT
1735 && (GET_CODE (x) == SYMBOL_REF
1736 || GET_CODE (x) == CONST
1737 || GET_CODE (x) == LABEL_REF))
1738 return darwin_sections[const_data_section];
1739 else
1740 return darwin_sections[const_section];
1743 void
1744 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1746 cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)};
1748 vec_safe_push (ctors, new_elt);
1750 if (! MACHOPIC_INDIRECT)
1751 fprintf (asm_out_file, ".reference .constructors_used\n");
1754 void
1755 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1757 cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)};
1759 vec_safe_push (dtors, new_elt);
1761 if (! MACHOPIC_INDIRECT)
1762 fprintf (asm_out_file, ".reference .destructors_used\n");
1765 static int
1766 sort_cdtor_records (const void * a, const void * b)
1768 const cdtor_record *cda = (const cdtor_record *)a;
1769 const cdtor_record *cdb = (const cdtor_record *)b;
1770 if (cda->priority > cdb->priority)
1771 return 1;
1772 if (cda->priority < cdb->priority)
1773 return -1;
1774 if (cda->position > cdb->position)
1775 return 1;
1776 if (cda->position < cdb->position)
1777 return -1;
1778 return 0;
1781 static void
1782 finalize_ctors ()
1784 unsigned int i;
1785 cdtor_record *elt;
1787 if (MACHOPIC_INDIRECT)
1788 switch_to_section (darwin_sections[mod_init_section]);
1789 else
1790 switch_to_section (darwin_sections[constructor_section]);
1792 if (vec_safe_length (ctors) > 1)
1793 ctors->qsort (sort_cdtor_records);
1794 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
1796 assemble_align (POINTER_SIZE);
1797 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1801 static void
1802 finalize_dtors ()
1804 unsigned int i;
1805 cdtor_record *elt;
1807 if (MACHOPIC_INDIRECT)
1808 switch_to_section (darwin_sections[mod_term_section]);
1809 else
1810 switch_to_section (darwin_sections[destructor_section]);
1812 if (vec_safe_length (dtors) > 1)
1813 dtors->qsort (sort_cdtor_records);
1814 FOR_EACH_VEC_SAFE_ELT (dtors, i, elt)
1816 assemble_align (POINTER_SIZE);
1817 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1821 void
1822 darwin_globalize_label (FILE *stream, const char *name)
1824 if (!!strncmp (name, "_OBJC_", 6))
1825 default_globalize_label (stream, name);
1828 /* This routine returns non-zero if 'name' starts with the special objective-c
1829 anonymous file-scope static name. It accommodates c++'s mangling of such
1830 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
1832 int
1833 darwin_label_is_anonymous_local_objc_name (const char *name)
1835 const unsigned char *p = (const unsigned char *) name;
1836 if (*p != '_')
1837 return 0;
1838 if (p[1] == 'Z' && p[2] == 'L')
1840 p += 3;
1841 while (*p >= '0' && *p <= '9')
1842 p++;
1844 return (!strncmp ((const char *)p, "_OBJC_", 6));
1847 /* LTO support for Mach-O.
1849 This version uses three mach-o sections to encapsulate the (unlimited
1850 number of) lto sections.
1852 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
1853 __GNU_LTO, __section_names contains the GNU LTO section names.
1854 __GNU_LTO, __section_index contains an array of values that index these.
1856 Indexed thus:
1857 <section offset from the start of __GNU_LTO, __lto_sections>,
1858 <section length>
1859 <name offset from the start of __GNU_LTO, __section_names,
1860 <name length>.
1862 At present, for both m32 and m64 mach-o files each of these fields is
1863 represented by a uint32_t. This is because, AFAICT, a mach-o object
1864 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1866 uint32_t offset;
1867 "offset An integer specifying the offset to this section in the file." */
1869 /* Count lto section numbers. */
1870 static unsigned int lto_section_num = 0;
1872 /* A vector of information about LTO sections, at present, we only have
1873 the name. TODO: see if we can get the data length somehow. */
1874 typedef struct GTY (()) darwin_lto_section_e {
1875 const char *sectname;
1876 } darwin_lto_section_e ;
1878 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
1880 /* Section wrapper scheme (used here to wrap the unlimited number of LTO
1881 sections into three Mach-O ones).
1882 NOTE: These names MUST be kept in sync with those in
1883 libiberty/simple-object-mach-o. */
1884 #define LTO_SECTS_SECTION "__wrapper_sects"
1885 #define LTO_NAMES_SECTION "__wrapper_names"
1886 #define LTO_INDEX_SECTION "__wrapper_index"
1888 /* File to temporarily store LTO data. This is appended to asm_out_file
1889 in darwin_end_file. */
1890 static FILE *lto_asm_out_file, *saved_asm_out_file;
1891 static char *lto_asm_out_name;
1893 /* Prepare asm_out_file for LTO output. For darwin, this means hiding
1894 asm_out_file and switching to an alternative output file. */
1895 void
1896 darwin_asm_lto_start (void)
1898 gcc_assert (! saved_asm_out_file);
1899 saved_asm_out_file = asm_out_file;
1900 if (! lto_asm_out_name)
1901 lto_asm_out_name = make_temp_file (".lto.s");
1902 lto_asm_out_file = fopen (lto_asm_out_name, "a");
1903 if (lto_asm_out_file == NULL)
1904 fatal_error (input_location,
1905 "failed to open temporary file %s for LTO output",
1906 lto_asm_out_name);
1907 asm_out_file = lto_asm_out_file;
1910 /* Restore asm_out_file. */
1911 void
1912 darwin_asm_lto_end (void)
1914 gcc_assert (saved_asm_out_file);
1915 fclose (lto_asm_out_file);
1916 asm_out_file = saved_asm_out_file;
1917 saved_asm_out_file = NULL;
1920 static void
1921 darwin_asm_dwarf_section (const char *name, unsigned int flags, tree decl);
1923 /* Called for the TARGET_ASM_NAMED_SECTION hook. */
1925 void
1926 darwin_asm_named_section (const char *name,
1927 unsigned int flags,
1928 tree decl ATTRIBUTE_UNUSED)
1930 /* LTO sections go in a special section that encapsulates the (unlimited)
1931 number of GNU LTO sections within a single mach-o one. */
1932 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1933 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1935 darwin_lto_section_e e;
1936 /* We expect certain flags to be set... */
1937 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1938 == (SECTION_DEBUG | SECTION_NAMED));
1940 /* Switch to our combined section. */
1941 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
1942 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
1943 /* Output a label for the start of this sub-section. */
1944 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
1945 lto_section_num, name);
1946 /* We have to jump through hoops to get the values of the intra-section
1947 offsets... */
1948 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
1949 lto_section_num, lto_section_num);
1950 fprintf (asm_out_file,
1951 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
1952 lto_section_num, lto_section_num+1, lto_section_num);
1953 lto_section_num++;
1954 e.sectname = xstrdup (name);
1955 /* Keep the names, we'll need to make a table later.
1956 TODO: check that we do not revisit sections, that would break
1957 the assumption of how this is done. */
1958 if (lto_section_names == NULL)
1959 vec_alloc (lto_section_names, 16);
1960 vec_safe_push (lto_section_names, e);
1962 else if (strncmp (name, "__DWARF,", 8) == 0)
1963 darwin_asm_dwarf_section (name, flags, decl);
1964 else
1965 fprintf (asm_out_file, "\t.section %s\n", name);
1968 void
1969 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
1971 /* Darwin does not use unique sections. */
1974 /* Handle __attribute__ ((apple_kext_compatibility)).
1975 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
1976 vtable for classes with this attribute (and their descendants) by not
1977 outputting the new 3.0 nondeleting destructor. This means that such
1978 objects CANNOT be allocated on the stack or as globals UNLESS they have
1979 a completely empty `operator delete'.
1980 Luckily, this fits in with the Darwin kext model.
1982 This attribute also disables gcc3's potential overlaying of derived
1983 class data members on the padding at the end of the base class. */
1985 tree
1986 darwin_handle_kext_attribute (tree *node, tree name,
1987 tree args ATTRIBUTE_UNUSED,
1988 int flags ATTRIBUTE_UNUSED,
1989 bool *no_add_attrs)
1991 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
1992 if (! TARGET_KEXTABI)
1994 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
1995 "only when compiling a kext", name);
1997 *no_add_attrs = true;
1999 else if (TREE_CODE (*node) != RECORD_TYPE)
2001 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2002 "only to C++ classes", name);
2004 *no_add_attrs = true;
2007 return NULL_TREE;
2010 /* Handle a "weak_import" attribute; arguments as in
2011 struct attribute_spec.handler. */
2013 tree
2014 darwin_handle_weak_import_attribute (tree *node, tree name,
2015 tree ARG_UNUSED (args),
2016 int ARG_UNUSED (flags),
2017 bool * no_add_attrs)
2019 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
2021 warning (OPT_Wattributes, "%qE attribute ignored",
2022 name);
2023 *no_add_attrs = true;
2025 else
2026 declare_weak (*node);
2028 return NULL_TREE;
2031 /* Emit a label for an FDE, making it global and/or weak if appropriate.
2032 The third parameter is nonzero if this is for exception handling.
2033 The fourth parameter is nonzero if this is just a placeholder for an
2034 FDE that we are omitting. */
2036 void
2037 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
2039 char *lab ;
2040 char buf[32];
2041 static int invok_count = 0;
2042 static tree last_fun_decl = NULL_TREE;
2044 /* We use the linker to emit the .eh labels for Darwin 9 and above. */
2045 if (! for_eh || generating_for_darwin_version >= 9)
2046 return;
2048 /* FIXME: This only works when the eh for all sections of a function is
2049 emitted at the same time. If that changes, we would need to use a lookup
2050 table of some form to determine what to do. Also, we should emit the
2051 unadorned label for the partition containing the public label for a
2052 function. This is of limited use, probably, since we do not currently
2053 enable partitioning. */
2054 strcpy (buf, ".eh");
2055 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
2057 if (decl == last_fun_decl)
2059 invok_count++;
2060 snprintf (buf, 31, "$$part$$%d.eh", invok_count);
2062 else
2064 last_fun_decl = decl;
2065 invok_count = 0;
2069 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
2071 if (TREE_PUBLIC (decl))
2073 targetm.asm_out.globalize_label (file, lab);
2074 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
2076 fputs ("\t.private_extern ", file);
2077 assemble_name (file, lab);
2078 fputc ('\n', file);
2082 if (DECL_WEAK (decl))
2084 fputs ("\t.weak_definition ", file);
2085 assemble_name (file, lab);
2086 fputc ('\n', file);
2089 assemble_name (file, lab);
2090 if (empty)
2092 fputs (" = 0\n", file);
2094 /* Mark the absolute .eh and .eh1 style labels as needed to
2095 ensure that we don't dead code strip them and keep such
2096 labels from another instantiation point until we can fix this
2097 properly with group comdat support. */
2098 darwin_mark_decl_preserved (lab);
2100 else
2101 fputs (":\n", file);
2103 free (lab);
2106 static GTY(()) unsigned long except_table_label_num;
2108 void
2109 darwin_emit_except_table_label (FILE *file)
2111 char section_start_label[30];
2113 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
2114 except_table_label_num++);
2115 ASM_OUTPUT_LABEL (file, section_start_label);
2117 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
2119 void
2120 darwin_non_lazy_pcrel (FILE *file, rtx addr)
2122 const char *nlp_name;
2124 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2126 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2127 fputs ("\t.long\t", file);
2128 ASM_OUTPUT_LABELREF (file, nlp_name);
2129 fputs ("-.", file);
2132 /* If this is uncommented, details of each allocation will be printed
2133 in the asm right before the actual code. WARNING - this will cause some
2134 test-suite fails (since the printout will contain items that some tests
2135 are not expecting) -- so don't leave it on by default (it bloats the
2136 asm too). */
2137 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
2139 /* The first two of these routines are ostensibly just intended to put
2140 names into the asm. However, they are both hijacked in order to ensure
2141 that zero-sized items do not make their way into the output. Consequently,
2142 we also need to make these participate in provisions for dealing with
2143 such items in section anchors. */
2145 /* The implementation of ASM_DECLARE_OBJECT_NAME. */
2146 /* The RTTI data (e.g., __ti4name) is common and public (and static),
2147 but it does need to be referenced via indirect PIC data pointers.
2148 The machopic_define_symbol calls are telling the machopic subsystem
2149 that the name *is* defined in this module, so it doesn't need to
2150 make them indirect. */
2151 void
2152 darwin_asm_declare_object_name (FILE *file,
2153 const char *nam, tree decl)
2155 const char *xname = nam;
2156 unsigned HOST_WIDE_INT size;
2157 bool local_def, weak;
2159 weak = (DECL_P (decl)
2160 && DECL_WEAK (decl)
2161 && !lookup_attribute ("weak_import",
2162 DECL_ATTRIBUTES (decl)));
2164 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
2165 && (!DECL_COMMON (decl)
2166 || !TREE_PUBLIC (decl)));
2168 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
2169 xname = IDENTIFIER_POINTER (DECL_NAME (decl));
2171 if (local_def)
2173 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2174 if (!weak)
2175 machopic_define_symbol (DECL_RTL (decl));
2178 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2180 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2181 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
2182 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
2183 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
2184 (unsigned long long)size, DECL_ALIGN (decl), local_def,
2185 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2186 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
2187 (unsigned long)DECL_INITIAL (decl));
2188 #endif
2190 /* Darwin needs help to support local zero-sized objects.
2191 They must be made at least one byte, and the section containing must be
2192 marked as unsuitable for section-anchors (see storage allocators below).
2194 For non-zero objects this output is handled by varasm.c.
2196 if (!size)
2198 unsigned int l2align = 0;
2200 /* The align must be honored, even for zero-sized. */
2201 if (DECL_ALIGN (decl))
2203 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
2204 fprintf (file, "\t.align\t%u\n", l2align);
2207 ASM_OUTPUT_LABEL (file, xname);
2208 size = 1;
2209 fprintf (file, "\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2211 /* Check that we've correctly picked up the zero-sized item and placed it
2212 properly. */
2213 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2214 || (in_section
2215 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2217 else
2218 ASM_OUTPUT_LABEL (file, xname);
2221 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
2222 void
2223 darwin_asm_declare_constant_name (FILE *file, const char *name,
2224 const_tree exp ATTRIBUTE_UNUSED,
2225 HOST_WIDE_INT size)
2227 assemble_label (file, name);
2228 /* As for other items, we need at least one byte. */
2229 if (!size)
2231 fputs ("\t.space\t1\n", file);
2232 /* Check that we've correctly picked up the zero-sized item and placed it
2233 properly. */
2234 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2235 || (in_section
2236 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2240 /* Darwin storage allocators.
2242 Zerofill sections are desirable for large blank data since, otherwise, these
2243 data bloat objects (PR33210).
2245 However, section anchors don't work in .zerofill sections (one cannot switch
2246 to a zerofill section). Ergo, for Darwin targets using section anchors we need
2247 to put (at least some) data into 'normal' switchable sections.
2249 Here we set a relatively arbitrary value for the size of an object to trigger
2250 zerofill when section anchors are enabled (anything bigger than a page for
2251 current Darwin implementations). FIXME: there ought to be some objective way
2252 to make this choice.
2254 When section anchor are off this is ignored anyway. */
2256 #define BYTES_ZFILL 4096
2258 /* Emit a chunk of data for items coalesced by the linker. */
2259 static void
2260 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
2261 unsigned HOST_WIDE_INT size,
2262 unsigned int align)
2264 /* Since the sections used here are coalesed, they will not be eligible
2265 for section anchors, and therefore we don't need to break that out. */
2266 if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
2267 switch_to_section (darwin_sections[const_data_coal_section]);
2268 else
2269 switch_to_section (darwin_sections[data_coal_section]);
2271 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
2272 the align info for zero-sized items... but do it here otherwise. */
2273 if (size && align)
2274 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2276 if (TREE_PUBLIC (decl))
2277 darwin_globalize_label (fp, name);
2279 /* ... and we let it deal with outputting one byte of zero for them too. */
2280 darwin_asm_declare_object_name (fp, name, decl);
2281 if (size)
2282 assemble_zeros (size);
2285 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
2286 static void
2287 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
2288 unsigned HOST_WIDE_INT size,
2289 unsigned int align, tree meta)
2291 section *ocs = data_section;
2293 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
2294 ocs = darwin_objc2_section (decl, meta, ocs);
2295 else
2296 ocs = darwin_objc1_section (decl, meta, ocs);
2298 switch_to_section (ocs);
2300 /* We shall declare that zero-sized meta-data are not valid (yet). */
2301 gcc_assert (size);
2302 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2304 /* ... and we let it deal with outputting one byte of zero for them too. */
2305 darwin_asm_declare_object_name (fp, name, decl);
2306 assemble_zeros (size);
2309 /* This routine emits 'local' storage:
2311 When Section Anchors are off this routine emits .zerofill commands in
2312 sections named for their alignment.
2314 When Section Anchors are on, smaller (non-zero-sized) items are placed in
2315 the .static_data section so that the section anchoring system can see them.
2316 Larger items are still placed in .zerofill sections, addressing PR33210.
2317 The routine has no checking - it is all assumed to be done by the caller.
2319 static void
2320 darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
2321 unsigned HOST_WIDE_INT size,
2322 unsigned int l2align)
2324 /* FIXME: We have a fudge to make this work with Java even when the target does
2325 not use sections anchors -- Java seems to need at least one small item in a
2326 non-zerofill segment. */
2327 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2328 || (size && size <= 2))
2330 /* Put smaller objects in _static_data, where the section anchors system
2331 can get them.
2332 However, if they are zero-sized punt them to yet a different section
2333 (that is not allowed to participate in anchoring). */
2334 if (!size)
2336 fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
2337 in_section = darwin_sections[zobj_bss_section];
2338 size = 1;
2340 else
2342 fputs ("\t.static_data\n", fp);
2343 in_section = darwin_sections[static_data_section];
2346 if (l2align)
2347 fprintf (fp, "\t.align\t%u\n", l2align);
2349 assemble_name (fp, name);
2350 fprintf (fp, ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2352 else
2354 /* When we are on a non-section anchor target, we can get zero-sized
2355 items here. However, all we need to do is to bump them to one byte
2356 and the section alignment will take care of the rest. */
2357 char secnam[64];
2358 unsigned int flags ;
2359 snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"),
2360 (unsigned) l2align);
2361 /* We can't anchor (yet, if ever) in zerofill sections, because we can't
2362 switch to them and emit a label. */
2363 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2364 in_section = get_section (secnam, flags, NULL);
2365 fprintf (fp, "\t.zerofill %s,", secnam);
2366 assemble_name (fp, name);
2367 if (!size)
2368 size = 1;
2370 if (l2align)
2371 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2372 size, (unsigned) l2align);
2373 else
2374 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2377 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2378 /* This is defined as a file-scope var, so we know to notify machopic. */
2379 machopic_define_symbol (DECL_RTL (decl));
2382 /* Emit a chunk of common. */
2383 static void
2384 darwin_emit_common (FILE *fp, const char *name,
2385 unsigned HOST_WIDE_INT size, unsigned int align)
2387 unsigned HOST_WIDE_INT rounded;
2388 unsigned int l2align;
2390 /* Earlier systems complain if the alignment exceeds the page size.
2391 The magic number is 4096 * 8 - hard-coded for legacy systems. */
2392 if (!emit_aligned_common && (align > 32768UL))
2393 align = 4096UL; /* In units. */
2394 else
2395 align /= BITS_PER_UNIT;
2397 /* Make sure we have a meaningful align. */
2398 if (!align)
2399 align = 1;
2401 /* For earlier toolchains, we need to emit the var as a rounded size to
2402 tell ld the alignment. */
2403 if (size < align)
2404 rounded = align;
2405 else
2406 rounded = (size + (align-1)) & ~(align-1);
2408 l2align = floor_log2 (align);
2409 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2411 in_section = comm_section;
2412 /* We mustn't allow multiple public symbols to share an address when using
2413 the normal OSX toolchain. */
2414 if (!size)
2416 /* Put at least one byte. */
2417 size = 1;
2418 /* This section can no longer participate in section anchoring. */
2419 comm_section->common.flags |= SECTION_NO_ANCHOR;
2422 fputs ("\t.comm\t", fp);
2423 assemble_name (fp, name);
2424 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
2425 emit_aligned_common?size:rounded);
2426 if (l2align && emit_aligned_common)
2427 fprintf (fp, ",%u", l2align);
2428 fputs ("\n", fp);
2431 /* Output a var which is all zero - into aligned BSS sections, common, lcomm
2432 or coalescable data sections (for weak or comdat) as appropriate. */
2434 void
2435 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
2436 unsigned HOST_WIDE_INT size, unsigned int align)
2438 unsigned int l2align;
2439 bool one, pub, weak;
2440 tree meta;
2442 pub = TREE_PUBLIC (decl);
2443 one = DECL_ONE_ONLY (decl);
2444 weak = (DECL_P (decl)
2445 && DECL_WEAK (decl)
2446 && !lookup_attribute ("weak_import",
2447 DECL_ATTRIBUTES (decl)));
2449 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2450 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
2451 " pub %d weak %d one %d init %lx\n",
2452 name, (long long)size, (int)align, TREE_READONLY (decl),
2453 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2454 pub, weak, one, (unsigned long)DECL_INITIAL (decl));
2455 #endif
2457 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2458 before the target has a chance to comment. */
2459 if ((meta = is_objc_metadata (decl)))
2461 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2462 return;
2465 /* Check that any initializer is valid. */
2466 gcc_assert ((DECL_INITIAL (decl) == NULL)
2467 || (DECL_INITIAL (decl) == error_mark_node)
2468 || initializer_zerop (DECL_INITIAL (decl)));
2470 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2471 gcc_assert (!DECL_COMMON (decl));
2473 /* Pick up the correct alignment. */
2474 if (!size || !align)
2475 align = DECL_ALIGN (decl);
2477 l2align = floor_log2 (align / BITS_PER_UNIT);
2478 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2480 last_assemble_variable_decl = decl;
2482 /* We would rather not have to check this here - but it seems that we might
2483 be passed a decl that should be in coalesced space. */
2484 if (one || weak)
2486 /* Weak or COMDAT objects are put in mergeable sections. */
2487 darwin_emit_weak_or_comdat (fp, decl, name, size,
2488 DECL_ALIGN (decl));
2489 return;
2492 /* If this is not public, then emit according to local rules. */
2493 if (!pub)
2495 darwin_emit_local_bss (fp, decl, name, size, l2align);
2496 return;
2499 /* So we have a public symbol (small item fudge for Java, see above). */
2500 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2501 || (size && size <= 2))
2503 /* Put smaller objects in data, where the section anchors system can get
2504 them. However, if they are zero-sized punt them to yet a different
2505 section (that is not allowed to participate in anchoring). */
2506 if (!size)
2508 fputs ("\t.section\t__DATA,__zobj_data\n", fp);
2509 in_section = darwin_sections[zobj_data_section];
2510 size = 1;
2512 else
2514 fputs ("\t.data\n", fp);
2515 in_section = data_section;
2518 if (l2align)
2519 fprintf (fp, "\t.align\t%u\n", l2align);
2521 assemble_name (fp, name);
2522 fprintf (fp, ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2524 else
2526 char secnam[64];
2527 unsigned int flags ;
2528 /* When we are on a non-section anchor target, we can get zero-sized
2529 items here. However, all we need to do is to bump them to one byte
2530 and the section alignment will take care of the rest. */
2531 snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align);
2533 /* We can't anchor in zerofill sections, because we can't switch
2534 to them and emit a label. */
2535 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2536 in_section = get_section (secnam, flags, NULL);
2537 fprintf (fp, "\t.zerofill %s,", secnam);
2538 assemble_name (fp, name);
2539 if (!size)
2540 size = 1;
2542 if (l2align)
2543 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
2544 else
2545 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2547 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2550 /* Output a chunk of common, with alignment specified (where the target
2551 supports this). */
2552 void
2553 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
2554 unsigned HOST_WIDE_INT size,
2555 unsigned int align)
2557 unsigned int l2align;
2558 bool one, weak;
2559 tree meta;
2561 /* No corresponding var. */
2562 if (decl==NULL)
2564 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2565 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
2566 #endif
2567 darwin_emit_common (fp, name, size, align);
2568 return;
2571 one = DECL_ONE_ONLY (decl);
2572 weak = (DECL_P (decl)
2573 && DECL_WEAK (decl)
2574 && !lookup_attribute ("weak_import",
2575 DECL_ATTRIBUTES (decl)));
2577 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2578 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
2579 " weak %d one %d init %lx\n",
2580 name, (long long)size, (int)align, TREE_READONLY (decl),
2581 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2582 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
2583 #endif
2585 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2586 before the target has a chance to comment. */
2587 if ((meta = is_objc_metadata (decl)))
2589 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2590 return;
2593 /* We shouldn't be messing with this if the decl has a section name. */
2594 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2596 /* We would rather not have to check this here - but it seems that we might
2597 be passed a decl that should be in coalesced space. */
2598 if (one || weak)
2600 /* Weak or COMDAT objects are put in mergable sections. */
2601 darwin_emit_weak_or_comdat (fp, decl, name, size,
2602 DECL_ALIGN (decl));
2603 return;
2606 /* We should only get here for DECL_COMMON, with a zero init (and, in
2607 principle, only for public symbols too - although we deal with local
2608 ones below). */
2610 /* Check the initializer is OK. */
2611 gcc_assert (DECL_COMMON (decl)
2612 && ((DECL_INITIAL (decl) == NULL)
2613 || (DECL_INITIAL (decl) == error_mark_node)
2614 || initializer_zerop (DECL_INITIAL (decl))));
2616 last_assemble_variable_decl = decl;
2618 if (!size || !align)
2619 align = DECL_ALIGN (decl);
2621 l2align = floor_log2 (align / BITS_PER_UNIT);
2622 /* Check we aren't asking for more aligment than the platform allows. */
2623 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2625 if (TREE_PUBLIC (decl) != 0)
2626 darwin_emit_common (fp, name, size, align);
2627 else
2628 darwin_emit_local_bss (fp, decl, name, size, l2align);
2631 /* Output a chunk of BSS with alignment specfied. */
2632 void
2633 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
2634 unsigned HOST_WIDE_INT size,
2635 unsigned int align)
2637 unsigned long l2align;
2638 bool one, weak;
2639 tree meta;
2641 one = DECL_ONE_ONLY (decl);
2642 weak = (DECL_P (decl)
2643 && DECL_WEAK (decl)
2644 && !lookup_attribute ("weak_import",
2645 DECL_ATTRIBUTES (decl)));
2647 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2648 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
2649 " weak %d init %lx\n",
2650 name, (long long)size, (int)align, TREE_READONLY (decl),
2651 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
2652 weak , (unsigned long)DECL_INITIAL (decl));
2653 #endif
2655 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2656 before the target has a chance to comment. */
2657 if ((meta = is_objc_metadata (decl)))
2659 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2660 return;
2663 /* We shouldn't be messing with this if the decl has a section name. */
2664 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2666 /* We would rather not have to check this here - but it seems that we might
2667 be passed a decl that should be in coalesced space. */
2668 if (one || weak)
2670 /* Weak or COMDAT objects are put in mergable sections. */
2671 darwin_emit_weak_or_comdat (fp, decl, name, size,
2672 DECL_ALIGN (decl));
2673 return;
2676 /* .. and it should be suitable for placement in local mem. */
2677 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
2678 /* .. and any initializer must be all-zero. */
2679 gcc_assert ((DECL_INITIAL (decl) == NULL)
2680 || (DECL_INITIAL (decl) == error_mark_node)
2681 || initializer_zerop (DECL_INITIAL (decl)));
2683 last_assemble_variable_decl = decl;
2685 if (!size || !align)
2686 align = DECL_ALIGN (decl);
2688 l2align = floor_log2 (align / BITS_PER_UNIT);
2689 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2691 darwin_emit_local_bss (fp, decl, name, size, l2align);
2694 /* Emit an assembler directive to set visibility for a symbol. The
2695 only supported visibilities are VISIBILITY_DEFAULT and
2696 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
2697 extern". There is no MACH-O equivalent of ELF's
2698 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
2700 void
2701 darwin_assemble_visibility (tree decl, int vis)
2703 if (vis == VISIBILITY_DEFAULT)
2705 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
2707 fputs ("\t.private_extern ", asm_out_file);
2708 assemble_name (asm_out_file,
2709 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
2710 fputs ("\n", asm_out_file);
2712 else
2713 warning (OPT_Wattributes, "protected visibility attribute "
2714 "not supported in this configuration; ignored");
2717 /* vec used by darwin_asm_dwarf_section.
2718 Maybe a hash tab would be better here - but the intention is that this is
2719 a very short list (fewer than 16 items) and each entry should (ideally,
2720 eventually) only be presented once.
2722 A structure to hold a dwarf debug section used entry. */
2724 typedef struct GTY(()) dwarf_sect_used_entry {
2725 const char *name;
2726 unsigned count;
2728 dwarf_sect_used_entry;
2731 /* A list of used __DWARF sections. */
2732 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
2734 /* This is called when we are asked to assemble a named section and the
2735 name begins with __DWARF,. We keep a list of the section names (without
2736 the __DWARF, prefix) and use this to emit our required start label on the
2737 first switch to each section. */
2739 static void
2740 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2741 tree ARG_UNUSED (decl))
2743 unsigned i;
2744 int namelen;
2745 const char * sname;
2746 dwarf_sect_used_entry *ref;
2747 bool found = false;
2748 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2749 == (SECTION_DEBUG | SECTION_NAMED));
2750 /* We know that the name starts with __DWARF, */
2751 sname = name + 8;
2752 namelen = strchr (sname, ',') - sname;
2753 gcc_assert (namelen);
2754 if (dwarf_sect_names_table == NULL)
2755 vec_alloc (dwarf_sect_names_table, 16);
2756 else
2757 for (i = 0;
2758 dwarf_sect_names_table->iterate (i, &ref);
2759 i++)
2761 if (!ref)
2762 break;
2763 if (!strcmp (ref->name, sname))
2765 found = true;
2766 ref->count++;
2767 break;
2771 fprintf (asm_out_file, "\t.section %s\n", name);
2772 if (!found)
2774 dwarf_sect_used_entry e;
2775 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
2776 e.count = 1;
2777 e.name = xstrdup (sname);
2778 vec_safe_push (dwarf_sect_names_table, e);
2782 /* Output a difference of two labels that will be an assembly time
2783 constant if the two labels are local. (.long lab1-lab2 will be
2784 very different if lab1 is at the boundary between two sections; it
2785 will be relocated according to the second section, not the first,
2786 so one ends up with a difference between labels in different
2787 sections, which is bad in the dwarf2 eh context for instance.) */
2789 static int darwin_dwarf_label_counter;
2791 void
2792 darwin_asm_output_dwarf_delta (FILE *file, int size,
2793 const char *lab1, const char *lab2)
2795 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
2796 && lab2[0] == '*' && lab2[1] == 'L');
2797 const char *directive = (size == 8 ? ".quad" : ".long");
2799 if (islocaldiff)
2800 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
2801 else
2802 fprintf (file, "\t%s\t", directive);
2804 assemble_name_raw (file, lab1);
2805 fprintf (file, "-");
2806 assemble_name_raw (file, lab2);
2807 if (islocaldiff)
2808 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
2811 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
2812 offsets are not represented using relocs in .o files; either the
2813 section never leaves the .o file, or the linker or other tool is
2814 responsible for parsing the DWARF and updating the offsets. */
2816 void
2817 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
2818 section *base)
2820 char sname[64];
2821 int namelen;
2823 gcc_assert (base->common.flags & SECTION_NAMED);
2824 gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
2825 gcc_assert (strchr (base->named.name + 8, ','));
2827 namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
2828 sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
2829 darwin_asm_output_dwarf_delta (file, size, lab, sname);
2832 /* Called from the within the TARGET_ASM_FILE_START for each target. */
2834 void
2835 darwin_file_start (void)
2837 /* Nothing to do. */
2840 /* Called for the TARGET_ASM_FILE_END hook.
2841 Emit the mach-o pic indirection data, the lto data and, finally a flag
2842 to tell the linker that it can break the file object into sections and
2843 move those around for efficiency. */
2845 void
2846 darwin_file_end (void)
2848 if (!vec_safe_is_empty (ctors))
2849 finalize_ctors ();
2850 if (!vec_safe_is_empty (dtors))
2851 finalize_dtors ();
2853 /* If we are expecting to output NeXT ObjC meta-data, (and we actually see
2854 some) then we output the fix-and-continue marker (Image Info).
2855 This applies to Objective C, Objective C++ and LTO with either language
2856 as part of the input. */
2857 if (flag_next_runtime && objc_metadata_seen)
2859 unsigned int flags = 0;
2860 if (flag_objc_abi >= 2)
2862 flags = 16;
2863 output_section_asm_op
2864 (darwin_sections[objc2_image_info_section]->unnamed.data);
2866 else
2867 output_section_asm_op
2868 (darwin_sections[objc_image_info_section]->unnamed.data);
2870 ASM_OUTPUT_ALIGN (asm_out_file, 2);
2871 fputs ("L_OBJC_ImageInfo:\n", asm_out_file);
2873 flags |= (flag_replace_objc_classes && classes_seen) ? 1 : 0;
2874 flags |= flag_objc_gc ? 2 : 0;
2876 fprintf (asm_out_file, "\t.long\t0\n\t.long\t%u\n", flags);
2879 machopic_finish (asm_out_file);
2880 if (lang_GNU_CXX ())
2882 switch_to_section (darwin_sections[constructor_section]);
2883 switch_to_section (darwin_sections[destructor_section]);
2884 ASM_OUTPUT_ALIGN (asm_out_file, 1);
2887 /* If there was LTO assembler output, append it to asm_out_file. */
2888 if (lto_asm_out_name)
2890 int n;
2891 char *buf, *lto_asm_txt;
2893 /* Shouldn't be here if we failed to switch back. */
2894 gcc_assert (! saved_asm_out_file);
2896 lto_asm_out_file = fopen (lto_asm_out_name, "r");
2897 if (lto_asm_out_file == NULL)
2898 fatal_error (input_location,
2899 "failed to open temporary file %s with LTO output",
2900 lto_asm_out_name);
2901 fseek (lto_asm_out_file, 0, SEEK_END);
2902 n = ftell (lto_asm_out_file);
2903 if (n > 0)
2905 fseek (lto_asm_out_file, 0, SEEK_SET);
2906 lto_asm_txt = buf = (char *) xmalloc (n + 1);
2907 while (fgets (lto_asm_txt, n, lto_asm_out_file))
2908 fputs (lto_asm_txt, asm_out_file);
2909 /* Put a termination label. */
2910 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2911 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2912 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
2913 lto_section_num);
2914 /* Make sure our termination label stays in this section. */
2915 fputs ("\t.space\t1\n", asm_out_file);
2918 /* Remove the temporary file. */
2919 fclose (lto_asm_out_file);
2920 unlink_if_ordinary (lto_asm_out_name);
2921 free (lto_asm_out_name);
2924 /* Output the names and indices. */
2925 if (lto_section_names && lto_section_names->length ())
2927 int count;
2928 darwin_lto_section_e *ref;
2929 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
2930 the latter up ourselves. */
2931 const char *op = integer_asm_op (4,0);
2933 /* Emit the names. */
2934 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2935 LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
2936 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2938 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
2939 /* We have to jump through hoops to get the values of the intra-section
2940 offsets... */
2941 fprintf (asm_out_file,
2942 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
2943 count, count);
2944 fprintf (asm_out_file,
2945 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
2946 count, count+1, count);
2947 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
2949 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
2950 /* make sure our termination label stays in this section. */
2951 fputs ("\t.space\t1\n", asm_out_file);
2953 /* Emit the Index. */
2954 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2955 LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
2956 fputs ("\t.align\t2\n", asm_out_file);
2957 fputs ("# Section offset, Section length, Name offset, Name length\n",
2958 asm_out_file);
2959 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2961 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
2962 op, count, ref->sectname);
2963 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
2964 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
2965 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
2969 /* If we have section anchors, then we must prevent the linker from
2970 re-arranging data. */
2971 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2972 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
2975 /* TODO: Add a language hook for identifying if a decl is a vtable. */
2976 #define DARWIN_VTABLE_P(DECL) 0
2978 /* Cross-module name binding. Darwin does not support overriding
2979 functions at dynamic-link time, except for vtables in kexts. */
2981 bool
2982 darwin_binds_local_p (const_tree decl)
2984 return default_binds_local_p_1 (decl,
2985 TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
2988 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
2989 anchor relative to ".", the current section position. We cannot use
2990 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
2991 void
2992 darwin_asm_output_anchor (rtx symbol)
2994 fprintf (asm_out_file, "\t.set\t");
2995 assemble_name (asm_out_file, XSTR (symbol, 0));
2996 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
2997 SYMBOL_REF_BLOCK_OFFSET (symbol));
3000 /* Disable section anchoring on any section containing a zero-sized
3001 object. */
3002 bool
3003 darwin_use_anchors_for_symbol_p (const_rtx symbol)
3005 if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
3007 section *sect;
3008 /* If the section contains a zero-sized object it's ineligible. */
3009 sect = SYMBOL_REF_BLOCK (symbol)->sect;
3010 /* This should have the effect of disabling anchors for vars that follow
3011 any zero-sized one, in a given section. */
3012 if (sect->common.flags & SECTION_NO_ANCHOR)
3013 return false;
3015 /* Also check the normal reasons for suppressing. */
3016 return default_use_anchors_for_symbol_p (symbol);
3018 else
3019 return false;
3022 /* Set the darwin specific attributes on TYPE. */
3023 void
3024 darwin_set_default_type_attributes (tree type)
3026 if (darwin_ms_struct
3027 && TREE_CODE (type) == RECORD_TYPE)
3028 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
3029 NULL_TREE,
3030 TYPE_ATTRIBUTES (type));
3033 /* True, iff we're generating code for loadable kernel extensions. */
3035 bool
3036 darwin_kextabi_p (void) {
3037 return flag_apple_kext;
3040 void
3041 darwin_override_options (void)
3043 /* Keep track of which (major) version we're generating code for. */
3044 if (darwin_macosx_version_min)
3046 if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
3047 generating_for_darwin_version = 10;
3048 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
3049 generating_for_darwin_version = 9;
3051 /* Earlier versions are not specifically accounted, until required. */
3054 /* In principle, this should be c-family only. However, we really need to
3055 set sensible defaults for LTO as well, since the section selection stuff
3056 should check for correctness re. the ABI. TODO: check and provide the
3057 flags (runtime & ABI) from the lto wrapper). */
3059 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
3060 if (!global_options_set.x_flag_objc_abi)
3061 global_options.x_flag_objc_abi
3062 = (!flag_next_runtime)
3064 : (TARGET_64BIT ? 2
3065 : (generating_for_darwin_version >= 9) ? 1
3066 : 0);
3068 /* Objective-C family ABI 2 is only valid for next/m64 at present. */
3069 if (global_options_set.x_flag_objc_abi && flag_next_runtime)
3071 if (TARGET_64BIT && global_options.x_flag_objc_abi < 2)
3072 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 must be"
3073 " used for %<-m64%> targets with"
3074 " %<-fnext-runtime%>");
3075 if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
3076 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 is not"
3077 " supported on %<-m32%> targets with"
3078 " %<-fnext-runtime%>");
3081 /* Don't emit DWARF3/4 unless specifically selected. This is a
3082 workaround for tool bugs. */
3083 if (!global_options_set.x_dwarf_strict)
3084 dwarf_strict = 1;
3085 if (!global_options_set.x_dwarf_version)
3086 dwarf_version = 2;
3088 /* Do not allow unwind tables to be generated by default for m32.
3089 fnon-call-exceptions will override this, regardless of what we do. */
3090 if (generating_for_darwin_version < 10
3091 && !global_options_set.x_flag_asynchronous_unwind_tables
3092 && !TARGET_64BIT)
3093 global_options.x_flag_asynchronous_unwind_tables = 0;
3095 /* Disable -freorder-blocks-and-partition when unwind tables are being
3096 emitted for Darwin < 9 (OSX 10.5).
3097 The strategy is, "Unless the User has specifically set/unset an unwind
3098 flag we will switch off -freorder-blocks-and-partition when unwind tables
3099 will be generated". If the User specifically sets flags... we assume
3100 (s)he knows why... */
3101 if (generating_for_darwin_version < 9
3102 && global_options_set.x_flag_reorder_blocks_and_partition
3103 && ((global_options.x_flag_exceptions /* User, c++, java */
3104 && !global_options_set.x_flag_exceptions) /* User specified... */
3105 || (global_options.x_flag_unwind_tables
3106 && !global_options_set.x_flag_unwind_tables)
3107 || (global_options.x_flag_non_call_exceptions
3108 && !global_options_set.x_flag_non_call_exceptions)
3109 || (global_options.x_flag_asynchronous_unwind_tables
3110 && !global_options_set.x_flag_asynchronous_unwind_tables)))
3112 inform (input_location,
3113 "-freorder-blocks-and-partition does not work with exceptions "
3114 "on this architecture");
3115 flag_reorder_blocks_and_partition = 0;
3116 flag_reorder_blocks = 1;
3119 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
3120 one valid choice of exception scheme for each runtime. */
3121 if (!global_options_set.x_flag_objc_sjlj_exceptions)
3122 global_options.x_flag_objc_sjlj_exceptions =
3123 flag_next_runtime && !TARGET_64BIT;
3125 /* FIXME: and this could be eliminated then too. */
3126 if (!global_options_set.x_flag_exceptions
3127 && flag_objc_exceptions
3128 && TARGET_64BIT)
3129 flag_exceptions = 1;
3131 if (flag_mkernel || flag_apple_kext)
3133 /* -mkernel implies -fapple-kext for C++ */
3134 if (lang_GNU_CXX ())
3135 flag_apple_kext = 1;
3137 flag_no_common = 1;
3139 /* No EH in kexts. */
3140 flag_exceptions = 0;
3141 /* No -fnon-call-exceptions data in kexts. */
3142 flag_non_call_exceptions = 0;
3143 /* so no tables either.. */
3144 flag_unwind_tables = 0;
3145 flag_asynchronous_unwind_tables = 0;
3146 /* We still need to emit branch islands for kernel context. */
3147 darwin_emit_branch_islands = true;
3150 if (flag_var_tracking_uninit == 0
3151 && generating_for_darwin_version >= 9
3152 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
3153 : (debug_info_level >= DINFO_LEVEL_NORMAL))
3154 && write_symbols == DWARF2_DEBUG)
3155 flag_var_tracking_uninit = flag_var_tracking;
3157 if (MACHO_DYNAMIC_NO_PIC_P)
3159 if (flag_pic)
3160 warning_at (UNKNOWN_LOCATION, 0,
3161 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
3162 " %<-fpie%> or %<-fPIE%>");
3163 flag_pic = 0;
3165 else if (flag_pic == 1)
3167 /* Darwin's -fpic is -fPIC. */
3168 flag_pic = 2;
3171 /* It is assumed that branch island stubs are needed for earlier systems. */
3172 if (generating_for_darwin_version < 9)
3173 darwin_emit_branch_islands = true;
3174 else
3175 emit_aligned_common = true; /* Later systems can support aligned common. */
3177 /* The c_dialect...() macros are not available to us here. */
3178 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3181 #if DARWIN_PPC
3182 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
3184 static void
3185 darwin_patch_builtin (enum built_in_function fncode)
3187 tree fn = builtin_decl_explicit (fncode);
3188 tree sym;
3189 char *newname;
3191 if (!fn)
3192 return;
3194 sym = DECL_ASSEMBLER_NAME (fn);
3195 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
3197 set_user_assembler_name (fn, newname);
3199 fn = builtin_decl_implicit (fncode);
3200 if (fn)
3201 set_user_assembler_name (fn, newname);
3204 void
3205 darwin_patch_builtins (void)
3207 if (LONG_DOUBLE_TYPE_SIZE != 128)
3208 return;
3210 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
3211 #define PATCH_BUILTIN_NO64(fncode) \
3212 if (!TARGET_64BIT) \
3213 darwin_patch_builtin (fncode);
3214 #define PATCH_BUILTIN_VARIADIC(fncode) \
3215 if (!TARGET_64BIT \
3216 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
3217 darwin_patch_builtin (fncode);
3218 #include "darwin-ppc-ldouble-patch.def"
3219 #undef PATCH_BUILTIN
3220 #undef PATCH_BUILTIN_NO64
3221 #undef PATCH_BUILTIN_VARIADIC
3223 #endif
3225 /* CFStrings implementation. */
3226 static GTY(()) tree cfstring_class_reference = NULL_TREE;
3227 static GTY(()) tree cfstring_type_node = NULL_TREE;
3228 static GTY(()) tree ccfstring_type_node = NULL_TREE;
3229 static GTY(()) tree pccfstring_type_node = NULL_TREE;
3230 static GTY(()) tree pcint_type_node = NULL_TREE;
3231 static GTY(()) tree pcchar_type_node = NULL_TREE;
3233 static enum built_in_function darwin_builtin_cfstring;
3235 /* Store all constructed constant CFStrings in a hash table so that
3236 they get uniqued properly. */
3238 typedef struct GTY ((for_user)) cfstring_descriptor {
3239 /* The string literal. */
3240 tree literal;
3241 /* The resulting constant CFString. */
3242 tree constructor;
3243 } cfstring_descriptor;
3245 struct cfstring_hasher : ggc_ptr_hash<cfstring_descriptor>
3247 static hashval_t hash (cfstring_descriptor *);
3248 static bool equal (cfstring_descriptor *, cfstring_descriptor *);
3251 static GTY (()) hash_table<cfstring_hasher> *cfstring_htab;
3253 static tree
3254 add_builtin_field_decl (tree type, const char *name, tree **chain)
3256 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
3257 get_identifier (name), type);
3259 if (*chain != NULL)
3260 **chain = field;
3261 *chain = &DECL_CHAIN (field);
3263 return field;
3266 tree
3267 darwin_init_cfstring_builtins (unsigned builtin_cfstring)
3269 tree cfsfun, fields, pccfstring_ftype_pcchar;
3270 tree *chain = NULL;
3272 darwin_builtin_cfstring =
3273 (enum built_in_function) builtin_cfstring;
3275 /* struct __builtin_CFString {
3276 const int *isa; (will point at
3277 int flags; __CFConstantStringClassReference)
3278 const char *str;
3279 long length;
3280 }; */
3282 pcint_type_node = build_pointer_type
3283 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
3285 pcchar_type_node = build_pointer_type
3286 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3288 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
3290 /* Have to build backwards for finish struct. */
3291 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
3292 add_builtin_field_decl (pcchar_type_node, "str", &chain);
3293 add_builtin_field_decl (integer_type_node, "flags", &chain);
3294 add_builtin_field_decl (pcint_type_node, "isa", &chain);
3295 finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
3296 fields, NULL_TREE);
3298 /* const struct __builtin_CFstring *
3299 __builtin___CFStringMakeConstantString (const char *); */
3301 ccfstring_type_node = build_qualified_type
3302 (cfstring_type_node, TYPE_QUAL_CONST);
3303 pccfstring_type_node = build_pointer_type (ccfstring_type_node);
3304 pccfstring_ftype_pcchar = build_function_type_list
3305 (pccfstring_type_node, pcchar_type_node, NULL_TREE);
3307 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
3308 get_identifier ("__builtin___CFStringMakeConstantString"),
3309 pccfstring_ftype_pcchar);
3311 TREE_PUBLIC (cfsfun) = 1;
3312 DECL_EXTERNAL (cfsfun) = 1;
3313 DECL_ARTIFICIAL (cfsfun) = 1;
3314 /* Make a lang-specific section - dup_lang_specific_decl makes a new node
3315 in place of the existing, which may be NULL. */
3316 DECL_LANG_SPECIFIC (cfsfun) = NULL;
3317 (*lang_hooks.dup_lang_specific_decl) (cfsfun);
3318 DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD;
3319 DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring;
3320 lang_hooks.builtin_function (cfsfun);
3322 /* extern int __CFConstantStringClassReference[]; */
3323 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
3324 get_identifier ("__CFConstantStringClassReference"),
3325 build_array_type (integer_type_node, NULL_TREE));
3327 TREE_PUBLIC (cfstring_class_reference) = 1;
3328 DECL_ARTIFICIAL (cfstring_class_reference) = 1;
3329 (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
3330 DECL_EXTERNAL (cfstring_class_reference) = 1;
3331 rest_of_decl_compilation (cfstring_class_reference, 0, 0);
3333 /* Initialize the hash table used to hold the constant CFString objects. */
3334 cfstring_htab = hash_table<cfstring_hasher>::create_ggc (31);
3336 return cfstring_type_node;
3339 tree
3340 darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
3341 bool ARG_UNUSED (ignore))
3343 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3345 if (fcode == darwin_builtin_cfstring)
3347 if (!darwin_constant_cfstrings)
3349 error ("built-in function %qD requires the"
3350 " %<-mconstant-cfstrings%> flag", fndecl);
3351 return error_mark_node;
3354 if (n_args != 1)
3356 error ("built-in function %qD takes one argument only", fndecl);
3357 return error_mark_node;
3360 return darwin_build_constant_cfstring (*argp);
3363 return NULL_TREE;
3366 void
3367 darwin_rename_builtins (void)
3369 /* The system ___divdc3 routine in libSystem on darwin10 is not
3370 accurate to 1ulp, ours is, so we avoid ever using the system name
3371 for this routine and instead install a non-conflicting name that
3372 is accurate.
3374 When -ffast-math or -funsafe-math-optimizations is given, we can
3375 use the faster version. */
3376 if (!flag_unsafe_math_optimizations)
3378 enum built_in_function dcode
3379 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
3380 + DCmode - MIN_MODE_COMPLEX_FLOAT);
3381 tree fn = builtin_decl_explicit (dcode);
3382 /* Fortran and c call TARGET_INIT_BUILTINS and
3383 TARGET_INIT_LIBFUNCS at different times, so we have to put a
3384 call into each to ensure that at least one of them is called
3385 after build_common_builtin_nodes. A better fix is to add a
3386 new hook to run after build_common_builtin_nodes runs. */
3387 if (fn)
3388 set_user_assembler_name (fn, "___ieee_divdc3");
3389 fn = builtin_decl_implicit (dcode);
3390 if (fn)
3391 set_user_assembler_name (fn, "___ieee_divdc3");
3395 bool
3396 darwin_libc_has_function (enum function_class fn_class)
3398 if (fn_class == function_sincos)
3399 return false;
3400 if (fn_class == function_c99_math_complex
3401 || fn_class == function_c99_misc)
3402 return (TARGET_64BIT
3403 || strverscmp (darwin_macosx_version_min, "10.3") >= 0);
3405 return true;
3408 hashval_t
3409 cfstring_hasher::hash (cfstring_descriptor *ptr)
3411 tree str = ptr->literal;
3412 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3413 int i, len = TREE_STRING_LENGTH (str);
3414 hashval_t h = len;
3416 for (i = 0; i < len; i++)
3417 h = ((h * 613) + p[i]);
3419 return h;
3422 bool
3423 cfstring_hasher::equal (cfstring_descriptor *ptr1, cfstring_descriptor *ptr2)
3425 tree str1 = ptr1->literal;
3426 tree str2 = ptr2->literal;
3427 int len1 = TREE_STRING_LENGTH (str1);
3429 return (len1 == TREE_STRING_LENGTH (str2)
3430 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3431 len1));
3434 tree
3435 darwin_build_constant_cfstring (tree str)
3437 struct cfstring_descriptor *desc, key;
3438 tree addr;
3440 if (!str)
3442 error ("CFString literal is missing");
3443 return error_mark_node;
3446 STRIP_NOPS (str);
3448 if (TREE_CODE (str) == ADDR_EXPR)
3449 str = TREE_OPERAND (str, 0);
3451 if (TREE_CODE (str) != STRING_CST)
3453 error ("CFString literal expression is not a string constant");
3454 return error_mark_node;
3457 /* Perhaps we already constructed a constant CFString just like this one? */
3458 key.literal = str;
3459 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
3460 desc = *loc;
3462 if (!desc)
3464 tree var, constructor, field;
3465 vec<constructor_elt, va_gc> *v = NULL;
3466 int length = TREE_STRING_LENGTH (str) - 1;
3468 if (darwin_warn_nonportable_cfstrings)
3470 const char *s = TREE_STRING_POINTER (str);
3471 int l = 0;
3473 for (l = 0; l < length; l++)
3474 if (!s[l] || !isascii (s[l]))
3476 warning (darwin_warn_nonportable_cfstrings, "%s in CFString literal",
3477 s[l] ? "non-ASCII character" : "embedded NUL");
3478 break;
3482 *loc = desc = ggc_cleared_alloc<cfstring_descriptor> ();
3483 desc->literal = str;
3485 /* isa *. */
3486 field = TYPE_FIELDS (ccfstring_type_node);
3487 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3488 build1 (ADDR_EXPR, TREE_TYPE (field),
3489 cfstring_class_reference));
3490 /* flags */
3491 field = DECL_CHAIN (field);
3492 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3493 build_int_cst (TREE_TYPE (field), 0x000007c8));
3494 /* string *. */
3495 field = DECL_CHAIN (field);
3496 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3497 build1 (ADDR_EXPR, TREE_TYPE (field), str));
3498 /* length */
3499 field = DECL_CHAIN (field);
3500 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3501 build_int_cst (TREE_TYPE (field), length));
3503 constructor = build_constructor (ccfstring_type_node, v);
3504 TREE_READONLY (constructor) = 1;
3505 TREE_CONSTANT (constructor) = 1;
3506 TREE_STATIC (constructor) = 1;
3508 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
3509 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
3510 being built without any knowledge of C++ tree accessors; hence, we shall
3511 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
3512 if (darwin_running_cxx)
3513 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
3515 /* Create an anonymous global variable for this CFString. */
3516 var = build_decl (input_location, CONST_DECL,
3517 NULL, TREE_TYPE (constructor));
3518 DECL_ARTIFICIAL (var) = 1;
3519 TREE_STATIC (var) = 1;
3520 DECL_INITIAL (var) = constructor;
3521 /* FIXME: This should use a translation_unit_decl to indicate file scope. */
3522 DECL_CONTEXT (var) = NULL_TREE;
3523 desc->constructor = var;
3526 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
3527 TREE_CONSTANT (addr) = 1;
3529 return addr;
3532 bool
3533 darwin_cfstring_p (tree str)
3535 struct cfstring_descriptor key;
3537 if (!str)
3538 return false;
3540 STRIP_NOPS (str);
3542 if (TREE_CODE (str) == ADDR_EXPR)
3543 str = TREE_OPERAND (str, 0);
3545 if (TREE_CODE (str) != STRING_CST)
3546 return false;
3548 key.literal = str;
3549 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, NO_INSERT);
3551 if (loc)
3552 return true;
3554 return false;
3557 void
3558 darwin_enter_string_into_cfstring_table (tree str)
3560 struct cfstring_descriptor key;
3562 key.literal = str;
3563 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
3565 if (!*loc)
3567 *loc = ggc_cleared_alloc<cfstring_descriptor> ();
3568 ((struct cfstring_descriptor *)*loc)->literal = str;
3572 /* Choose named function section based on its frequency. */
3574 section *
3575 darwin_function_section (tree decl, enum node_frequency freq,
3576 bool startup, bool exit)
3578 /* Decide if we need to put this in a coalescable section. */
3579 bool weak = (decl
3580 && DECL_WEAK (decl)
3581 && (!DECL_ATTRIBUTES (decl)
3582 || !lookup_attribute ("weak_import",
3583 DECL_ATTRIBUTES (decl))));
3585 /* If there is a specified section name, we should not be trying to
3586 override. */
3587 if (decl && DECL_SECTION_NAME (decl) != NULL)
3588 return get_named_section (decl, NULL, 0);
3590 /* We always put unlikely executed stuff in the cold section. */
3591 if (freq == NODE_FREQUENCY_UNLIKELY_EXECUTED)
3592 return (weak) ? darwin_sections[text_cold_coal_section]
3593 : darwin_sections[text_cold_section];
3595 /* If we have LTO *and* feedback information, then let LTO handle
3596 the function ordering, it makes a better job (for normal, hot,
3597 startup and exit - hence the bailout for cold above). */
3598 if (in_lto_p && flag_profile_values)
3599 goto default_function_sections;
3601 /* Non-cold startup code should go to startup subsection. */
3602 if (startup)
3603 return (weak) ? darwin_sections[text_startup_coal_section]
3604 : darwin_sections[text_startup_section];
3606 /* Similarly for exit. */
3607 if (exit)
3608 return (weak) ? darwin_sections[text_exit_coal_section]
3609 : darwin_sections[text_exit_section];
3611 /* Place hot code. */
3612 if (freq == NODE_FREQUENCY_HOT)
3613 return (weak) ? darwin_sections[text_hot_coal_section]
3614 : darwin_sections[text_hot_section];
3616 /* Otherwise, default to the 'normal' non-reordered sections. */
3617 default_function_sections:
3618 return (weak) ? darwin_sections[text_coal_section]
3619 : text_section;
3622 /* When a function is partitioned between sections, we need to insert a label
3623 at the start of each new chunk - so that it may become a valid 'atom' for
3624 eh and debug purposes. Without this the linker will emit warnings if one
3625 tries to add line location information (since the switched fragment will
3626 be anonymous). */
3628 void
3629 darwin_function_switched_text_sections (FILE *fp, tree decl, bool new_is_cold)
3631 char buf[128];
3632 snprintf (buf, 128, "%s%s",new_is_cold?"__cold_sect_of_":"__hot_sect_of_",
3633 IDENTIFIER_POINTER (DECL_NAME (decl)));
3634 /* Make sure we pick up all the relevant quotes etc. */
3635 assemble_name_raw (fp, (const char *) buf);
3636 fputs (":\n", fp);
3639 #include "gt-darwin.h"