PR preprocessor/63831
[official-gcc.git] / gcc / config / darwin.c
blob1785e85cf2d7b4362115ac698177277a75af6034
1 /* Functions for generic Darwin as target machine for GNU C compiler.
2 Copyright (C) 1989-2014 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 "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-flags.h"
31 #include "output.h"
32 #include "insn-attr.h"
33 #include "flags.h"
34 #include "tree.h"
35 #include "stringpool.h"
36 #include "varasm.h"
37 #include "stor-layout.h"
38 #include "expr.h"
39 #include "reload.h"
40 #include "hashtab.h"
41 #include "hash-set.h"
42 #include "vec.h"
43 #include "machmode.h"
44 #include "input.h"
45 #include "function.h"
46 #include "ggc.h"
47 #include "langhooks.h"
48 #include "target.h"
49 #include "tm_p.h"
50 #include "diagnostic-core.h"
51 #include "toplev.h"
52 #include "dominance.h"
53 #include "cfg.h"
54 #include "cfgrtl.h"
55 #include "cfganal.h"
56 #include "lcm.h"
57 #include "cfgbuild.h"
58 #include "cfgcleanup.h"
59 #include "predict.h"
60 #include "basic-block.h"
61 #include "df.h"
62 #include "debug.h"
63 #include "obstack.h"
64 #include "hash-table.h"
65 #include "tree-ssa-alias.h"
66 #include "internal-fn.h"
67 #include "gimple-fold.h"
68 #include "tree-eh.h"
69 #include "gimple-expr.h"
70 #include "is-a.h"
71 #include "gimple.h"
72 #include "gimplify.h"
73 #include "hash-map.h"
74 #include "plugin-api.h"
75 #include "ipa-ref.h"
76 #include "cgraph.h"
77 #include "lto-streamer.h"
78 #include "lto-section-names.h"
80 /* Darwin supports a feature called fix-and-continue, which is used
81 for rapid turn around debugging. When code is compiled with the
82 -mfix-and-continue flag, two changes are made to the generated code
83 that allow the system to do things that it would normally not be
84 able to do easily. These changes allow gdb to load in
85 recompilation of a translation unit that has been changed into a
86 running program and replace existing functions and methods of that
87 translation unit with versions of those functions and methods
88 from the newly compiled translation unit. The new functions access
89 the existing static symbols from the old translation unit, if the
90 symbol existed in the unit to be replaced, and from the new
91 translation unit, otherwise.
93 The changes are to insert 5 nops at the beginning of all functions
94 and to use indirection to get at static symbols. The 5 nops
95 are required by consumers of the generated code. Currently, gdb
96 uses this to patch in a jump to the overriding function, this
97 allows all uses of the old name to forward to the replacement,
98 including existing function pointers and virtual methods. See
99 rs6000_emit_prologue for the code that handles the nop insertions.
101 The added indirection allows gdb to redirect accesses to static
102 symbols from the newly loaded translation unit to the existing
103 symbol, if any. @code{static} symbols are special and are handled by
104 setting the second word in the .non_lazy_symbol_pointer data
105 structure to symbol. See indirect_data for the code that handles
106 the extra indirection, and machopic_output_indirection and its use
107 of MACHO_SYMBOL_STATIC for the code that handles @code{static}
108 symbol indirection. */
110 /* For darwin >= 9 (OSX 10.5) the linker is capable of making the necessary
111 branch islands and we no longer need to emit darwin stubs.
112 However, if we are generating code for earlier systems (or for use in the
113 kernel) the stubs might still be required, and this will be set true. */
114 int darwin_emit_branch_islands = false;
116 typedef struct GTY(()) cdtor_record {
117 rtx symbol;
118 int priority; /* [con/de]structor priority */
119 int position; /* original position */
120 } cdtor_record;
122 static GTY(()) vec<cdtor_record, va_gc> *ctors = NULL;
123 static GTY(()) vec<cdtor_record, va_gc> *dtors = NULL;
125 /* A flag to determine whether we are running c++ or obj-c++. This has to be
126 settable from non-c-family contexts too (i.e. we can't use the c_dialect_
127 functions). */
128 int darwin_running_cxx;
130 /* Some code-gen now depends on OS major version numbers (at least). */
131 int generating_for_darwin_version ;
133 /* Section names. */
134 section * darwin_sections[NUM_DARWIN_SECTIONS];
136 /* While we transition to using in-tests instead of ifdef'd code. */
137 #ifndef HAVE_lo_sum
138 #define HAVE_lo_sum 0
139 #define gen_macho_high(a,b) (a)
140 #define gen_macho_low(a,b,c) (a)
141 #endif
143 /* True if we're setting __attribute__ ((ms_struct)). */
144 int darwin_ms_struct = false;
146 /* Earlier versions of Darwin as do not recognize an alignment field in
147 .comm directives, this should be set for versions that allow it. */
148 int emit_aligned_common = false;
150 /* A get_unnamed_section callback used to switch to an ObjC section.
151 DIRECTIVE is as for output_section_asm_op. */
153 static void
154 output_objc_section_asm_op (const void *directive)
156 static bool been_here = false;
158 /* The NeXT ObjC Runtime requires these sections to be present and in
159 order in the object. The code below implements this by emitting
160 a section header for each ObjC section the first time that an ObjC
161 section is requested. */
162 if (! been_here)
164 section *saved_in_section = in_section;
165 static const enum darwin_section_enum tomark[] =
167 /* written, cold -> hot */
168 objc_cat_cls_meth_section,
169 objc_cat_inst_meth_section,
170 objc_string_object_section,
171 objc_constant_string_object_section,
172 objc_selector_refs_section,
173 objc_selector_fixup_section,
174 objc_cls_refs_section,
175 objc_class_section,
176 objc_meta_class_section,
177 /* shared, hot -> cold */
178 objc_cls_meth_section,
179 objc_inst_meth_section,
180 objc_protocol_section,
181 objc_class_names_section,
182 objc_meth_var_types_section,
183 objc_meth_var_names_section,
184 objc_category_section,
185 objc_class_vars_section,
186 objc_instance_vars_section,
187 objc_module_info_section,
188 objc_symbols_section,
190 /* ABI=1 */
191 static const enum darwin_section_enum tomarkv1[] =
193 objc1_protocol_ext_section,
194 objc1_class_ext_section,
195 objc1_prop_list_section
197 /* ABI=2 */
198 static const enum darwin_section_enum tomarkv2[] =
200 objc2_message_refs_section,
201 objc2_classdefs_section,
202 objc2_metadata_section,
203 objc2_classrefs_section,
204 objc2_classlist_section,
205 objc2_categorylist_section,
206 objc2_selector_refs_section,
207 objc2_nonlazy_class_section,
208 objc2_nonlazy_category_section,
209 objc2_protocollist_section,
210 objc2_protocolrefs_section,
211 objc2_super_classrefs_section,
212 objc2_image_info_section,
213 objc2_constant_string_object_section
215 size_t i;
217 been_here = true;
218 if (flag_objc_abi < 2)
220 for (i = 0; i < ARRAY_SIZE (tomark); i++)
221 switch_to_section (darwin_sections[tomark[i]]);
222 if (flag_objc_abi == 1)
223 for (i = 0; i < ARRAY_SIZE (tomarkv1); i++)
224 switch_to_section (darwin_sections[tomarkv1[i]]);
226 else
227 for (i = 0; i < ARRAY_SIZE (tomarkv2); i++)
228 switch_to_section (darwin_sections[tomarkv2[i]]);
229 /* Make sure we don't get varasm.c out of sync with us. */
230 switch_to_section (saved_in_section);
232 output_section_asm_op (directive);
236 /* Private flag applied to disable section-anchors in a particular section. */
237 #define SECTION_NO_ANCHOR SECTION_MACH_DEP
240 /* Implement TARGET_ASM_INIT_SECTIONS. */
242 void
243 darwin_init_sections (void)
245 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
246 darwin_sections[NAME] = \
247 get_unnamed_section (FLAGS, (OBJC \
248 ? output_objc_section_asm_op \
249 : output_section_asm_op), \
250 "\t" DIRECTIVE);
251 #include "config/darwin-sections.def"
252 #undef DEF_SECTION
254 readonly_data_section = darwin_sections[const_section];
255 exception_section = darwin_sections[darwin_exception_section];
256 eh_frame_section = darwin_sections[darwin_eh_frame_section];
260 name_needs_quotes (const char *name)
262 int c;
263 while ((c = *name++) != '\0')
264 if (! ISIDNUM (c)
265 && c != '.' && c != '$' && c != '_' )
266 return 1;
267 return 0;
270 /* Return true if SYM_REF can be used without an indirection. */
272 machopic_symbol_defined_p (rtx sym_ref)
274 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
275 return true;
277 /* If a symbol references local and is not an extern to this
278 file, then the symbol might be able to declared as defined. */
279 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
281 /* If the symbol references a variable and the variable is a
282 common symbol, then this symbol is not defined. */
283 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
285 tree decl = SYMBOL_REF_DECL (sym_ref);
286 if (!decl)
287 return true;
288 if (DECL_COMMON (decl))
289 return false;
291 return true;
293 return false;
296 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
297 reference, which will not be changed. */
299 enum machopic_addr_class
300 machopic_classify_symbol (rtx sym_ref)
302 bool function_p;
304 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
305 if (machopic_symbol_defined_p (sym_ref))
306 return (function_p
307 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
308 else
309 return (function_p
310 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
313 #ifndef TARGET_FIX_AND_CONTINUE
314 #define TARGET_FIX_AND_CONTINUE 0
315 #endif
317 /* Indicate when fix-and-continue style code generation is being used
318 and when a reference to data should be indirected so that it can be
319 rebound in a new translation unit to reference the original instance
320 of that data. Symbol names that are for code generation local to
321 the translation unit are bound to the new translation unit;
322 currently this means symbols that begin with L or _OBJC_;
323 otherwise, we indicate that an indirect reference should be made to
324 permit the runtime to rebind new instances of the translation unit
325 to the original instance of the data. */
327 static int
328 indirect_data (rtx sym_ref)
330 int lprefix;
331 const char *name;
333 /* If we aren't generating fix-and-continue code, don't do anything
334 special. */
335 if (TARGET_FIX_AND_CONTINUE == 0)
336 return 0;
338 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
339 are indirected. Symbols that begin with L and _OBJC_ are always
340 bound to the current translation unit as they are used for
341 generated local data of the translation unit. */
343 name = XSTR (sym_ref, 0);
345 lprefix = (((name[0] == '*' || name[0] == '&')
346 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
347 || (strncmp (name, "_OBJC_", 6) == 0));
349 return ! lprefix;
352 static int
353 machopic_data_defined_p (rtx sym_ref)
355 if (indirect_data (sym_ref))
356 return 0;
358 switch (machopic_classify_symbol (sym_ref))
360 case MACHOPIC_DEFINED_DATA:
361 case MACHOPIC_DEFINED_FUNCTION:
362 return 1;
363 default:
364 return 0;
368 void
369 machopic_define_symbol (rtx mem)
371 rtx sym_ref;
373 gcc_assert (GET_CODE (mem) == MEM);
374 sym_ref = XEXP (mem, 0);
375 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
378 /* Return either ORIG or:
380 (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
382 depending on MACHO_DYNAMIC_NO_PIC_P. */
384 machopic_gen_offset (rtx orig)
386 if (MACHO_DYNAMIC_NO_PIC_P)
387 return orig;
388 else
390 /* Play games to avoid marking the function as needing pic if we
391 are being called as part of the cost-estimation process. */
392 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
393 crtl->uses_pic_offset_table = 1;
394 orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
395 UNSPEC_MACHOPIC_OFFSET);
396 return gen_rtx_CONST (Pmode, orig);
400 static GTY(()) const char * function_base_func_name;
401 static GTY(()) int current_pic_label_num;
402 static GTY(()) int emitted_pic_label_num;
404 static void
405 update_pic_label_number_if_needed (void)
407 const char *current_name;
409 /* When we are generating _get_pc thunks within stubs, there is no current
410 function. */
411 if (current_function_decl)
413 current_name =
414 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
415 if (function_base_func_name != current_name)
417 ++current_pic_label_num;
418 function_base_func_name = current_name;
421 else
423 ++current_pic_label_num;
424 function_base_func_name = "L_machopic_stub_dummy";
428 void
429 machopic_output_function_base_name (FILE *file)
431 /* If dynamic-no-pic is on, we should not get here. */
432 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
434 update_pic_label_number_if_needed ();
435 fprintf (file, "L%d$pb", current_pic_label_num);
438 char curr_picbasename[32];
440 const char *
441 machopic_get_function_picbase (void)
443 /* If dynamic-no-pic is on, we should not get here. */
444 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
446 update_pic_label_number_if_needed ();
447 snprintf (curr_picbasename, 32, "L%d$pb", current_pic_label_num);
448 return (const char *) curr_picbasename;
451 bool
452 machopic_should_output_picbase_label (void)
454 update_pic_label_number_if_needed ();
456 if (current_pic_label_num == emitted_pic_label_num)
457 return false;
459 emitted_pic_label_num = current_pic_label_num;
460 return true;
463 /* The suffix attached to non-lazy pointer symbols. */
464 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
465 /* The suffix attached to stub symbols. */
466 #define STUB_SUFFIX "$stub"
468 typedef struct GTY ((for_user)) machopic_indirection
470 /* The SYMBOL_REF for the entity referenced. */
471 rtx symbol;
472 /* The name of the stub or non-lazy pointer. */
473 const char * ptr_name;
474 /* True iff this entry is for a stub (as opposed to a non-lazy
475 pointer). */
476 bool stub_p;
477 /* True iff this stub or pointer pointer has been referenced. */
478 bool used;
479 } machopic_indirection;
481 struct indirection_hasher : ggc_hasher<machopic_indirection *>
483 typedef const char *compare_type;
484 static hashval_t hash (machopic_indirection *);
485 static bool equal (machopic_indirection *, const char *);
488 /* A table mapping stub names and non-lazy pointer names to
489 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
491 static GTY (()) hash_table<indirection_hasher> *machopic_indirections;
493 /* Return a hash value for a SLOT in the indirections hash table. */
495 hashval_t
496 indirection_hasher::hash (machopic_indirection *p)
498 return htab_hash_string (p->ptr_name);
501 /* Returns true if the KEY is the same as that associated with
502 SLOT. */
504 bool
505 indirection_hasher::equal (machopic_indirection *s, const char *k)
507 return strcmp (s->ptr_name, k) == 0;
510 /* Return the name of the non-lazy pointer (if STUB_P is false) or
511 stub (if STUB_B is true) corresponding to the given name. */
513 const char *
514 machopic_indirection_name (rtx sym_ref, bool stub_p)
516 char *buffer;
517 const char *name = XSTR (sym_ref, 0);
518 size_t namelen = strlen (name);
519 machopic_indirection *p;
520 bool needs_quotes;
521 const char *suffix;
522 const char *prefix = user_label_prefix;
523 const char *quote = "";
524 tree id;
526 id = maybe_get_identifier (name);
527 if (id)
529 tree id_orig = id;
531 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
532 id = TREE_CHAIN (id);
533 if (id != id_orig)
535 name = IDENTIFIER_POINTER (id);
536 namelen = strlen (name);
540 if (name[0] == '*')
542 prefix = "";
543 ++name;
544 --namelen;
547 needs_quotes = name_needs_quotes (name);
548 if (needs_quotes)
550 quote = "\"";
553 if (stub_p)
554 suffix = STUB_SUFFIX;
555 else
556 suffix = NON_LAZY_POINTER_SUFFIX;
558 buffer = XALLOCAVEC (char, strlen ("&L")
559 + strlen (prefix)
560 + namelen
561 + strlen (suffix)
562 + 2 * strlen (quote)
563 + 1 /* '\0' */);
565 /* Construct the name of the non-lazy pointer or stub. */
566 sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
568 if (!machopic_indirections)
569 machopic_indirections = hash_table<indirection_hasher>::create_ggc (37);
571 machopic_indirection **slot
572 = machopic_indirections->find_slot_with_hash (buffer,
573 htab_hash_string (buffer),
574 INSERT);
575 if (*slot)
577 p = *slot;
579 else
581 p = ggc_alloc<machopic_indirection> ();
582 p->symbol = sym_ref;
583 p->ptr_name = xstrdup (buffer);
584 p->stub_p = stub_p;
585 p->used = false;
586 *slot = p;
589 return p->ptr_name;
592 /* Return the name of the stub for the mcount function. */
594 const char*
595 machopic_mcount_stub_name (void)
597 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
598 return machopic_indirection_name (symbol, /*stub_p=*/true);
601 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
602 or non-lazy pointer as used -- and mark the object to which the
603 pointer/stub refers as used as well, since the pointer/stub will
604 emit a reference to it. */
606 void
607 machopic_validate_stub_or_non_lazy_ptr (const char *name)
609 machopic_indirection *p
610 = machopic_indirections->find_with_hash (name, htab_hash_string (name));
611 if (p && ! p->used)
613 const char *real_name;
614 tree id;
616 p->used = true;
618 /* Do what output_addr_const will do when we actually call it. */
619 if (SYMBOL_REF_DECL (p->symbol))
620 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
622 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
624 id = maybe_get_identifier (real_name);
625 if (id)
626 mark_referenced (id);
630 /* Transform ORIG, which may be any data source, to the corresponding
631 source using indirections. */
634 machopic_indirect_data_reference (rtx orig, rtx reg)
636 rtx ptr_ref = orig;
638 if (! MACHOPIC_INDIRECT)
639 return orig;
641 if (GET_CODE (orig) == SYMBOL_REF)
643 int defined = machopic_data_defined_p (orig);
645 if (defined && MACHO_DYNAMIC_NO_PIC_P)
647 if (DARWIN_PPC)
649 /* Create a new register for CSE opportunities. */
650 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
651 emit_insn (gen_macho_high (hi_reg, orig));
652 emit_insn (gen_macho_low (reg, hi_reg, orig));
653 return reg;
655 else if (DARWIN_X86)
656 return orig;
657 else
658 /* some other cpu -- writeme! */
659 gcc_unreachable ();
661 else if (defined)
663 rtx offset = NULL;
664 if (DARWIN_PPC || HAVE_lo_sum)
665 offset = machopic_gen_offset (orig);
667 if (DARWIN_PPC)
669 rtx hi_sum_reg = (!can_create_pseudo_p ()
670 ? reg
671 : gen_reg_rtx (Pmode));
673 gcc_assert (reg);
675 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
676 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
677 gen_rtx_HIGH (Pmode, offset))));
678 emit_insn (gen_rtx_SET (Pmode, reg,
679 gen_rtx_LO_SUM (Pmode, hi_sum_reg,
680 copy_rtx (offset))));
682 orig = reg;
684 else if (HAVE_lo_sum)
686 gcc_assert (reg);
688 emit_insn (gen_rtx_SET (VOIDmode, reg,
689 gen_rtx_HIGH (Pmode, offset)));
690 emit_insn (gen_rtx_SET (VOIDmode, reg,
691 gen_rtx_LO_SUM (Pmode, reg,
692 copy_rtx (offset))));
693 emit_use (pic_offset_table_rtx);
695 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
697 return orig;
700 ptr_ref = (gen_rtx_SYMBOL_REF
701 (Pmode,
702 machopic_indirection_name (orig, /*stub_p=*/false)));
704 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
706 ptr_ref = gen_const_mem (Pmode, ptr_ref);
707 machopic_define_symbol (ptr_ref);
709 if (DARWIN_X86
710 && reg
711 && MACHO_DYNAMIC_NO_PIC_P)
713 emit_insn (gen_rtx_SET (Pmode, reg, ptr_ref));
714 ptr_ref = reg;
717 return ptr_ref;
719 else if (GET_CODE (orig) == CONST)
721 /* If "(const (plus ...", walk the PLUS and return that result.
722 PLUS processing (below) will restore the "(const ..." if
723 appropriate. */
724 if (GET_CODE (XEXP (orig, 0)) == PLUS)
725 return machopic_indirect_data_reference (XEXP (orig, 0), reg);
726 else
727 return orig;
729 else if (GET_CODE (orig) == MEM)
731 XEXP (ptr_ref, 0) =
732 machopic_indirect_data_reference (XEXP (orig, 0), reg);
733 return ptr_ref;
735 else if (GET_CODE (orig) == PLUS)
737 rtx base, result;
738 /* When the target is i386, this code prevents crashes due to the
739 compiler's ignorance on how to move the PIC base register to
740 other registers. (The reload phase sometimes introduces such
741 insns.) */
742 if (GET_CODE (XEXP (orig, 0)) == REG
743 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
744 /* Prevent the same register from being erroneously used
745 as both the base and index registers. */
746 && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST))
747 && reg)
749 emit_move_insn (reg, XEXP (orig, 0));
750 XEXP (ptr_ref, 0) = reg;
751 return ptr_ref;
754 /* Legitimize both operands of the PLUS. */
755 base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
756 orig = machopic_indirect_data_reference (XEXP (orig, 1),
757 (base == reg ? 0 : reg));
758 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
759 result = plus_constant (Pmode, base, INTVAL (orig));
760 else
761 result = gen_rtx_PLUS (Pmode, base, orig);
763 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
765 if (reg)
767 emit_move_insn (reg, result);
768 result = reg;
770 else
772 result = force_reg (GET_MODE (result), result);
776 return result;
778 return ptr_ref;
781 /* Transform TARGET (a MEM), which is a function call target, to the
782 corresponding symbol_stub if necessary. Return a new MEM. */
785 machopic_indirect_call_target (rtx target)
787 if (! darwin_emit_branch_islands)
788 return target;
790 if (GET_CODE (target) != MEM)
791 return target;
793 if (MACHOPIC_INDIRECT
794 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
795 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
796 & MACHO_SYMBOL_FLAG_DEFINED))
798 rtx sym_ref = XEXP (target, 0);
799 const char *stub_name = machopic_indirection_name (sym_ref,
800 /*stub_p=*/true);
801 machine_mode mode = GET_MODE (sym_ref);
803 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
804 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
805 MEM_READONLY_P (target) = 1;
806 MEM_NOTRAP_P (target) = 1;
809 return target;
813 machopic_legitimize_pic_address (rtx orig, machine_mode mode, rtx reg)
815 rtx pic_ref = orig;
817 if (! MACHOPIC_INDIRECT)
818 return orig;
820 /* First handle a simple SYMBOL_REF or LABEL_REF */
821 if (GET_CODE (orig) == LABEL_REF
822 || (GET_CODE (orig) == SYMBOL_REF
825 /* addr(foo) = &func+(foo-func) */
826 orig = machopic_indirect_data_reference (orig, reg);
828 if (GET_CODE (orig) == PLUS
829 && GET_CODE (XEXP (orig, 0)) == REG)
831 if (reg == 0)
832 return force_reg (mode, orig);
834 emit_move_insn (reg, orig);
835 return reg;
838 if (GET_CODE (orig) == MEM)
840 if (reg == 0)
842 gcc_assert (!reload_in_progress);
843 reg = gen_reg_rtx (Pmode);
846 #if HAVE_lo_sum
847 if (MACHO_DYNAMIC_NO_PIC_P
848 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
849 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
851 #if defined (TARGET_TOC) /* ppc */
852 rtx temp_reg = (!can_create_pseudo_p ()
853 ? reg :
854 gen_reg_rtx (Pmode));
855 rtx asym = XEXP (orig, 0);
856 rtx mem;
858 emit_insn (gen_macho_high (temp_reg, asym));
859 mem = gen_const_mem (GET_MODE (orig),
860 gen_rtx_LO_SUM (Pmode, temp_reg,
861 copy_rtx (asym)));
862 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
863 #else
864 /* Some other CPU -- WriteMe! but right now there are no other
865 platforms that can use dynamic-no-pic */
866 gcc_unreachable ();
867 #endif
868 pic_ref = reg;
870 else
871 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
872 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
874 rtx offset = machopic_gen_offset (XEXP (orig, 0));
875 #if defined (TARGET_TOC) /* i.e., PowerPC */
876 /* Generating a new reg may expose opportunities for
877 common subexpression elimination. */
878 rtx hi_sum_reg = (!can_create_pseudo_p ()
879 ? reg
880 : gen_reg_rtx (Pmode));
881 rtx mem;
882 rtx insn;
883 rtx sum;
885 sum = gen_rtx_HIGH (Pmode, offset);
886 if (! MACHO_DYNAMIC_NO_PIC_P)
887 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
889 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
891 mem = gen_const_mem (GET_MODE (orig),
892 gen_rtx_LO_SUM (Pmode,
893 hi_sum_reg,
894 copy_rtx (offset)));
895 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
896 set_unique_reg_note (insn, REG_EQUAL, pic_ref);
898 pic_ref = reg;
899 #else
900 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
902 emit_insn (gen_rtx_SET (VOIDmode, reg,
903 gen_rtx_HIGH (Pmode,
904 gen_rtx_CONST (Pmode,
905 offset))));
906 emit_insn (gen_rtx_SET (VOIDmode, reg,
907 gen_rtx_LO_SUM (Pmode, reg,
908 gen_rtx_CONST (Pmode,
909 copy_rtx (offset)))));
910 pic_ref = gen_rtx_PLUS (Pmode,
911 pic_offset_table_rtx, reg);
912 #endif
914 else
915 #endif /* HAVE_lo_sum */
917 rtx pic = pic_offset_table_rtx;
918 if (GET_CODE (pic) != REG)
920 emit_move_insn (reg, pic);
921 pic = reg;
923 #if 0
924 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
925 #endif
927 if (reload_in_progress)
928 df_set_regs_ever_live (REGNO (pic), true);
929 pic_ref = gen_rtx_PLUS (Pmode, pic,
930 machopic_gen_offset (XEXP (orig, 0)));
933 #if !defined (TARGET_TOC)
934 emit_move_insn (reg, pic_ref);
935 pic_ref = gen_const_mem (GET_MODE (orig), reg);
936 #endif
938 else
941 #if HAVE_lo_sum
942 if (GET_CODE (orig) == SYMBOL_REF
943 || GET_CODE (orig) == LABEL_REF)
945 rtx offset = machopic_gen_offset (orig);
946 #if defined (TARGET_TOC) /* i.e., PowerPC */
947 rtx hi_sum_reg;
949 if (reg == 0)
951 gcc_assert (!reload_in_progress);
952 reg = gen_reg_rtx (Pmode);
955 hi_sum_reg = reg;
957 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
958 (MACHO_DYNAMIC_NO_PIC_P)
959 ? gen_rtx_HIGH (Pmode, offset)
960 : gen_rtx_PLUS (Pmode,
961 pic_offset_table_rtx,
962 gen_rtx_HIGH (Pmode,
963 offset))));
964 emit_insn (gen_rtx_SET (VOIDmode, reg,
965 gen_rtx_LO_SUM (Pmode,
966 hi_sum_reg,
967 copy_rtx (offset))));
968 pic_ref = reg;
969 #else
970 emit_insn (gen_rtx_SET (VOIDmode, reg,
971 gen_rtx_HIGH (Pmode, offset)));
972 emit_insn (gen_rtx_SET (VOIDmode, reg,
973 gen_rtx_LO_SUM (Pmode, reg,
974 copy_rtx (offset))));
975 pic_ref = gen_rtx_PLUS (Pmode,
976 pic_offset_table_rtx, reg);
977 #endif
979 else
980 #endif /* HAVE_lo_sum */
982 if (REG_P (orig)
983 || GET_CODE (orig) == SUBREG)
985 return orig;
987 else
989 rtx pic = pic_offset_table_rtx;
990 if (GET_CODE (pic) != REG)
992 emit_move_insn (reg, pic);
993 pic = reg;
995 #if 0
996 emit_use (pic_offset_table_rtx);
997 #endif
998 if (reload_in_progress)
999 df_set_regs_ever_live (REGNO (pic), true);
1000 pic_ref = gen_rtx_PLUS (Pmode,
1001 pic,
1002 machopic_gen_offset (orig));
1007 if (GET_CODE (pic_ref) != REG)
1009 if (reg != 0)
1011 emit_move_insn (reg, pic_ref);
1012 return reg;
1014 else
1016 return force_reg (mode, pic_ref);
1019 else
1021 return pic_ref;
1025 else if (GET_CODE (orig) == SYMBOL_REF)
1026 return orig;
1028 else if (GET_CODE (orig) == PLUS
1029 && (GET_CODE (XEXP (orig, 0)) == MEM
1030 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
1031 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
1032 && XEXP (orig, 0) != pic_offset_table_rtx
1033 && GET_CODE (XEXP (orig, 1)) != REG)
1036 rtx base;
1037 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
1039 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1040 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
1041 Pmode, (base == reg ? 0 : reg));
1042 if (GET_CODE (orig) == CONST_INT)
1044 pic_ref = plus_constant (Pmode, base, INTVAL (orig));
1045 is_complex = 1;
1047 else
1048 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
1050 if (reg && is_complex)
1052 emit_move_insn (reg, pic_ref);
1053 pic_ref = reg;
1055 /* Likewise, should we set special REG_NOTEs here? */
1058 else if (GET_CODE (orig) == CONST)
1060 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1063 else if (GET_CODE (orig) == MEM
1064 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
1066 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1067 addr = replace_equiv_address (orig, addr);
1068 emit_move_insn (reg, addr);
1069 pic_ref = reg;
1072 return pic_ref;
1075 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
1076 DATA is the FILE* for assembly output. Called from
1077 htab_traverse. */
1080 machopic_output_indirection (machopic_indirection **slot, FILE *asm_out_file)
1082 machopic_indirection *p = *slot;
1083 rtx symbol;
1084 const char *sym_name;
1085 const char *ptr_name;
1087 if (!p->used)
1088 return 1;
1090 symbol = p->symbol;
1091 sym_name = XSTR (symbol, 0);
1092 ptr_name = p->ptr_name;
1094 if (p->stub_p)
1096 char *sym;
1097 char *stub;
1098 tree id;
1100 id = maybe_get_identifier (sym_name);
1101 if (id)
1103 tree id_orig = id;
1105 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
1106 id = TREE_CHAIN (id);
1107 if (id != id_orig)
1108 sym_name = IDENTIFIER_POINTER (id);
1111 sym = XALLOCAVEC (char, strlen (sym_name) + 2);
1112 if (sym_name[0] == '*' || sym_name[0] == '&')
1113 strcpy (sym, sym_name + 1);
1114 else if (sym_name[0] == '-' || sym_name[0] == '+')
1115 strcpy (sym, sym_name);
1116 else
1117 sprintf (sym, "%s%s", user_label_prefix, sym_name);
1119 stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
1120 if (ptr_name[0] == '*' || ptr_name[0] == '&')
1121 strcpy (stub, ptr_name + 1);
1122 else
1123 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
1125 machopic_output_stub (asm_out_file, sym, stub);
1127 else if (! indirect_data (symbol)
1128 && (machopic_symbol_defined_p (symbol)
1129 || SYMBOL_REF_LOCAL_P (symbol)))
1131 switch_to_section (data_section);
1132 assemble_align (GET_MODE_ALIGNMENT (Pmode));
1133 assemble_label (asm_out_file, ptr_name);
1134 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
1135 GET_MODE_SIZE (Pmode),
1136 GET_MODE_ALIGNMENT (Pmode), 1);
1138 else
1140 rtx init = const0_rtx;
1142 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
1144 /* Mach-O symbols are passed around in code through indirect
1145 references and the original symbol_ref hasn't passed through
1146 the generic handling and reference-catching in
1147 output_operand, so we need to manually mark weak references
1148 as such. */
1149 if (SYMBOL_REF_WEAK (symbol))
1151 tree decl = SYMBOL_REF_DECL (symbol);
1152 gcc_assert (DECL_P (decl));
1154 if (decl != NULL_TREE
1155 && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
1156 /* Handle only actual external-only definitions, not
1157 e.g. extern inline code or variables for which
1158 storage has been allocated. */
1159 && !TREE_STATIC (decl))
1161 fputs ("\t.weak_reference ", asm_out_file);
1162 assemble_name (asm_out_file, sym_name);
1163 fputc ('\n', asm_out_file);
1167 assemble_name (asm_out_file, ptr_name);
1168 fprintf (asm_out_file, ":\n");
1170 fprintf (asm_out_file, "\t.indirect_symbol ");
1171 assemble_name (asm_out_file, sym_name);
1172 fprintf (asm_out_file, "\n");
1174 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1175 have their symbol name instead of 0 in the second entry of
1176 the non-lazy symbol pointer data structure when they are
1177 defined. This allows the runtime to rebind newer instances
1178 of the translation unit with the original instance of the
1179 symbol. */
1181 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1182 && machopic_symbol_defined_p (symbol))
1183 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1185 assemble_integer (init, GET_MODE_SIZE (Pmode),
1186 GET_MODE_ALIGNMENT (Pmode), 1);
1189 return 1;
1192 void
1193 machopic_finish (FILE *asm_out_file)
1195 if (machopic_indirections)
1196 machopic_indirections
1197 ->traverse_noresize<FILE *, machopic_output_indirection> (asm_out_file);
1201 machopic_operand_p (rtx op)
1203 if (MACHOPIC_JUST_INDIRECT)
1204 return (GET_CODE (op) == SYMBOL_REF
1205 && machopic_symbol_defined_p (op));
1206 else
1207 return (GET_CODE (op) == CONST
1208 && GET_CODE (XEXP (op, 0)) == UNSPEC
1209 && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
1212 /* This function records whether a given name corresponds to a defined
1213 or undefined function or variable, for machopic_classify_ident to
1214 use later. */
1216 void
1217 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
1219 rtx sym_ref;
1221 /* Do the standard encoding things first. */
1222 default_encode_section_info (decl, rtl, first);
1224 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1225 return;
1227 sym_ref = XEXP (rtl, 0);
1228 if (TREE_CODE (decl) == VAR_DECL)
1229 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1231 if (!DECL_EXTERNAL (decl)
1232 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1233 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1234 && ((TREE_STATIC (decl)
1235 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1236 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1237 && DECL_INITIAL (decl) != error_mark_node)))
1238 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1240 if (! TREE_PUBLIC (decl))
1241 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
1244 void
1245 darwin_mark_decl_preserved (const char *name)
1247 fprintf (asm_out_file, "\t.no_dead_strip ");
1248 assemble_name (asm_out_file, name);
1249 fputc ('\n', asm_out_file);
1252 static section *
1253 darwin_rodata_section (int weak, bool zsize)
1255 return (weak
1256 ? darwin_sections[const_coal_section]
1257 : (zsize ? darwin_sections[zobj_const_section]
1258 : darwin_sections[const_section]));
1261 static section *
1262 darwin_mergeable_string_section (tree exp,
1263 unsigned HOST_WIDE_INT align)
1265 /* Darwin's ld expects to see non-writable string literals in the .cstring
1266 section. Later versions of ld check and complain when CFStrings are
1267 enabled. Therefore we shall force the strings into .cstring since we
1268 don't support writable ones anyway. */
1269 if ((darwin_constant_cfstrings || flag_merge_constants)
1270 && TREE_CODE (exp) == STRING_CST
1271 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1272 && align <= 256
1273 && (int_size_in_bytes (TREE_TYPE (exp))
1274 == TREE_STRING_LENGTH (exp))
1275 && ((size_t) TREE_STRING_LENGTH (exp)
1276 == strlen (TREE_STRING_POINTER (exp)) + 1))
1277 return darwin_sections[cstring_section];
1279 if (DARWIN_SECTION_ANCHORS && flag_section_anchors
1280 && TREE_CODE (exp) == STRING_CST
1281 && TREE_STRING_LENGTH (exp) == 0)
1282 return darwin_sections[zobj_const_section];
1284 return readonly_data_section;
1287 #ifndef HAVE_GAS_LITERAL16
1288 #define HAVE_GAS_LITERAL16 0
1289 #endif
1291 static section *
1292 darwin_mergeable_constant_section (tree exp,
1293 unsigned HOST_WIDE_INT align,
1294 bool zsize)
1296 machine_mode mode = DECL_MODE (exp);
1297 unsigned int modesize = GET_MODE_BITSIZE (mode);
1299 if (DARWIN_SECTION_ANCHORS
1300 && flag_section_anchors
1301 && zsize)
1302 return darwin_sections[zobj_const_section];
1304 if (flag_merge_constants
1305 && mode != VOIDmode
1306 && mode != BLKmode
1307 && modesize <= align
1308 && align >= 8
1309 && align <= 256
1310 && (align & (align -1)) == 0)
1312 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1314 if (TREE_CODE (size) == INTEGER_CST)
1316 if (wi::eq_p (size, 4))
1317 return darwin_sections[literal4_section];
1318 else if (wi::eq_p (size, 8))
1319 return darwin_sections[literal8_section];
1320 else if (HAVE_GAS_LITERAL16
1321 && TARGET_64BIT
1322 && wi::eq_p (size, 16))
1323 return darwin_sections[literal16_section];
1327 return readonly_data_section;
1330 section *
1331 darwin_tm_clone_table_section (void)
1333 return get_named_section (NULL,
1334 "__DATA,__tm_clone_table,regular,no_dead_strip",
1339 machopic_reloc_rw_mask (void)
1341 return MACHOPIC_INDIRECT ? 3 : 0;
1344 /* We have to deal with ObjC/C++ metadata section placement in the common
1345 code, since it will also be called from LTO.
1347 Return metadata attributes, if present (searching for ABI=2 first)
1348 Return NULL_TREE if no such attributes are found. */
1350 static tree
1351 is_objc_metadata (tree decl)
1353 if (DECL_P (decl)
1354 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1355 && DECL_ATTRIBUTES (decl))
1357 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1358 if (meta)
1359 return meta;
1360 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1361 if (meta)
1362 return meta;
1364 return NULL_TREE;
1367 static int classes_seen;
1368 static int objc_metadata_seen;
1370 /* Return the section required for Objective C ABI 2 metadata. */
1371 static section *
1372 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1374 const char *p;
1375 tree ident = TREE_VALUE (meta);
1376 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1377 p = IDENTIFIER_POINTER (ident);
1379 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi == 2);
1381 objc_metadata_seen = 1;
1383 if (base == data_section)
1384 base = darwin_sections[objc2_metadata_section];
1386 /* Most of the OBJC2 META-data end up in the base section, so check it
1387 first. */
1388 if (!strncmp (p, "V2_BASE", 7))
1389 return base;
1390 else if (!strncmp (p, "V2_STRG", 7))
1391 return darwin_sections[cstring_section];
1393 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
1394 return darwin_sections[objc2_classdefs_section];
1395 else if (!strncmp (p, "V2_MREF", 7))
1396 return darwin_sections[objc2_message_refs_section];
1397 else if (!strncmp (p, "V2_CLRF", 7))
1398 return darwin_sections[objc2_classrefs_section];
1399 else if (!strncmp (p, "V2_SURF", 7))
1400 return darwin_sections[objc2_super_classrefs_section];
1401 else if (!strncmp (p, "V2_NLCL", 7))
1402 return darwin_sections[objc2_nonlazy_class_section];
1403 else if (!strncmp (p, "V2_CLAB", 7))
1405 classes_seen = 1;
1406 return darwin_sections[objc2_classlist_section];
1408 else if (!strncmp (p, "V2_SRFS", 7))
1409 return darwin_sections[objc2_selector_refs_section];
1410 else if (!strncmp (p, "V2_NLCA", 7))
1411 return darwin_sections[objc2_nonlazy_category_section];
1412 else if (!strncmp (p, "V2_CALA", 7))
1413 return darwin_sections[objc2_categorylist_section];
1415 else if (!strncmp (p, "V2_PLST", 7))
1416 return darwin_sections[objc2_protocollist_section];
1417 else if (!strncmp (p, "V2_PRFS", 7))
1418 return darwin_sections[objc2_protocolrefs_section];
1420 else if (!strncmp (p, "V2_INFO", 7))
1421 return darwin_sections[objc2_image_info_section];
1423 else if (!strncmp (p, "V2_EHTY", 7))
1424 return darwin_sections[data_coal_section];
1426 else if (!strncmp (p, "V2_CSTR", 7))
1427 return darwin_sections[objc2_constant_string_object_section];
1429 /* Not recognized, default. */
1430 return base;
1433 /* Return the section required for Objective C ABI 0/1 metadata. */
1434 static section *
1435 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1437 const char *p;
1438 tree ident = TREE_VALUE (meta);
1439 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1440 p = IDENTIFIER_POINTER (ident);
1442 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi < 2);
1444 objc_metadata_seen = 1;
1446 /* String sections first, cos there are lots of strings. */
1447 if (!strncmp (p, "V1_STRG", 7))
1448 return darwin_sections[cstring_section];
1449 else if (!strncmp (p, "V1_CLSN", 7))
1450 return darwin_sections[objc_class_names_section];
1451 else if (!strncmp (p, "V1_METN", 7))
1452 return darwin_sections[objc_meth_var_names_section];
1453 else if (!strncmp (p, "V1_METT", 7))
1454 return darwin_sections[objc_meth_var_types_section];
1456 else if (!strncmp (p, "V1_CLAS", 7))
1458 classes_seen = 1;
1459 return darwin_sections[objc_class_section];
1461 else if (!strncmp (p, "V1_META", 7))
1462 return darwin_sections[objc_meta_class_section];
1463 else if (!strncmp (p, "V1_CATG", 7))
1464 return darwin_sections[objc_category_section];
1465 else if (!strncmp (p, "V1_PROT", 7))
1466 return darwin_sections[objc_protocol_section];
1468 else if (!strncmp (p, "V1_CLCV", 7))
1469 return darwin_sections[objc_class_vars_section];
1470 else if (!strncmp (p, "V1_CLIV", 7))
1471 return darwin_sections[objc_instance_vars_section];
1473 else if (!strncmp (p, "V1_CLCM", 7))
1474 return darwin_sections[objc_cls_meth_section];
1475 else if (!strncmp (p, "V1_CLIM", 7))
1476 return darwin_sections[objc_inst_meth_section];
1477 else if (!strncmp (p, "V1_CACM", 7))
1478 return darwin_sections[objc_cat_cls_meth_section];
1479 else if (!strncmp (p, "V1_CAIM", 7))
1480 return darwin_sections[objc_cat_inst_meth_section];
1481 else if (!strncmp (p, "V1_PNSM", 7))
1482 return darwin_sections[objc_cat_inst_meth_section];
1483 else if (!strncmp (p, "V1_PCLM", 7))
1484 return darwin_sections[objc_cat_cls_meth_section];
1486 else if (!strncmp (p, "V1_CLPR", 7))
1487 return darwin_sections[objc_cat_cls_meth_section];
1488 else if (!strncmp (p, "V1_CAPR", 7))
1489 return darwin_sections[objc_category_section]; /* ??? CHECK me. */
1491 else if (!strncmp (p, "V1_PRFS", 7))
1492 return darwin_sections[objc_cat_cls_meth_section];
1493 else if (!strncmp (p, "V1_CLRF", 7))
1494 return darwin_sections[objc_cls_refs_section];
1495 else if (!strncmp (p, "V1_SRFS", 7))
1496 return darwin_sections[objc_selector_refs_section];
1498 else if (!strncmp (p, "V1_MODU", 7))
1499 return darwin_sections[objc_module_info_section];
1500 else if (!strncmp (p, "V1_SYMT", 7))
1501 return darwin_sections[objc_symbols_section];
1502 else if (!strncmp (p, "V1_INFO", 7))
1503 return darwin_sections[objc_image_info_section];
1505 else if (!strncmp (p, "V1_PLST", 7))
1506 return darwin_sections[objc1_prop_list_section];
1507 else if (!strncmp (p, "V1_PEXT", 7))
1508 return darwin_sections[objc1_protocol_ext_section];
1509 else if (!strncmp (p, "V1_CEXT", 7))
1510 return darwin_sections[objc1_class_ext_section];
1512 else if (!strncmp (p, "V2_CSTR", 7))
1513 return darwin_sections[objc_constant_string_object_section];
1515 return base;
1518 section *
1519 machopic_select_section (tree decl,
1520 int reloc,
1521 unsigned HOST_WIDE_INT align)
1523 bool zsize, one, weak, ro;
1524 section *base_section = NULL;
1526 weak = (DECL_P (decl)
1527 && DECL_WEAK (decl)
1528 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
1530 zsize = (DECL_P (decl)
1531 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1532 && tree_to_uhwi (DECL_SIZE_UNIT (decl)) == 0);
1534 one = DECL_P (decl)
1535 && TREE_CODE (decl) == VAR_DECL
1536 && DECL_COMDAT_GROUP (decl);
1538 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
1540 switch (categorize_decl_for_section (decl, reloc))
1542 case SECCAT_TEXT:
1543 gcc_unreachable ();
1544 break;
1546 case SECCAT_RODATA:
1547 case SECCAT_SRODATA:
1548 base_section = darwin_rodata_section (weak, zsize);
1549 break;
1551 case SECCAT_RODATA_MERGE_STR:
1552 base_section = darwin_mergeable_string_section (decl, align);
1553 break;
1555 case SECCAT_RODATA_MERGE_STR_INIT:
1556 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1557 break;
1559 case SECCAT_RODATA_MERGE_CONST:
1560 base_section = darwin_mergeable_constant_section (decl, align, zsize);
1561 break;
1563 case SECCAT_DATA:
1564 case SECCAT_DATA_REL:
1565 case SECCAT_DATA_REL_LOCAL:
1566 case SECCAT_DATA_REL_RO:
1567 case SECCAT_DATA_REL_RO_LOCAL:
1568 case SECCAT_SDATA:
1569 case SECCAT_TDATA:
1570 if (weak || one)
1572 if (ro)
1573 base_section = darwin_sections[const_data_coal_section];
1574 else
1575 base_section = darwin_sections[data_coal_section];
1577 else if (DARWIN_SECTION_ANCHORS
1578 && flag_section_anchors
1579 && zsize)
1581 /* If we're doing section anchors, then punt zero-sized objects into
1582 their own sections so that they don't interfere with offset
1583 computation for the remaining vars. This does not need to be done
1584 for stuff in mergeable sections, since these are ineligible for
1585 anchors. */
1586 if (ro)
1587 base_section = darwin_sections[zobj_const_data_section];
1588 else
1589 base_section = darwin_sections[zobj_data_section];
1591 else if (ro)
1592 base_section = darwin_sections[const_data_section];
1593 else
1594 base_section = data_section;
1595 break;
1596 case SECCAT_BSS:
1597 case SECCAT_SBSS:
1598 case SECCAT_TBSS:
1599 if (weak || one)
1600 base_section = darwin_sections[data_coal_section];
1601 else
1603 if (!TREE_PUBLIC (decl))
1604 base_section = lcomm_section;
1605 else if (bss_noswitch_section)
1606 base_section = bss_noswitch_section;
1607 else
1608 base_section = data_section;
1610 break;
1612 default:
1613 gcc_unreachable ();
1616 /* Darwin weird special cases.
1617 a) OBJC Meta-data. */
1618 if (DECL_P (decl)
1619 && (TREE_CODE (decl) == VAR_DECL
1620 || TREE_CODE (decl) == CONST_DECL)
1621 && DECL_ATTRIBUTES (decl))
1623 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1624 if (meta)
1625 return darwin_objc2_section (decl, meta, base_section);
1626 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1627 if (meta)
1628 return darwin_objc1_section (decl, meta, base_section);
1629 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
1630 if (meta)
1631 return base_section; /* GNU runtime is happy with it all in one pot. */
1634 /* b) Constant string objects. */
1635 if (TREE_CODE (decl) == CONSTRUCTOR
1636 && TREE_TYPE (decl)
1637 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1638 && TYPE_NAME (TREE_TYPE (decl)))
1640 tree name = TYPE_NAME (TREE_TYPE (decl));
1641 if (TREE_CODE (name) == TYPE_DECL)
1642 name = DECL_NAME (name);
1644 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1646 if (flag_next_runtime)
1648 if (flag_objc_abi == 2)
1649 return darwin_sections[objc2_constant_string_object_section];
1650 else
1651 return darwin_sections[objc_constant_string_object_section];
1653 else
1654 return darwin_sections[objc_string_object_section];
1656 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
1657 return darwin_sections[cfstring_constant_object_section];
1658 else
1659 return base_section;
1661 /* c) legacy meta-data selection. */
1662 else if (TREE_CODE (decl) == VAR_DECL
1663 && DECL_NAME (decl)
1664 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1665 && IDENTIFIER_POINTER (DECL_NAME (decl))
1666 && flag_next_runtime
1667 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1669 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1670 static bool warned_objc_46 = false;
1671 /* We shall assert that zero-sized objects are an error in ObjC
1672 meta-data. */
1673 gcc_assert (tree_to_uhwi (DECL_SIZE_UNIT (decl)) != 0);
1675 /* ??? This mechanism for determining the metadata section is
1676 broken when LTO is in use, since the frontend that generated
1677 the data is not identified. We will keep the capability for
1678 the short term - in case any non-Objective-C programs are using
1679 it to place data in specified sections. */
1680 if (!warned_objc_46)
1682 location_t loc = DECL_SOURCE_LOCATION (decl);
1683 warning_at (loc, 0, "the use of _OBJC_-prefixed variable names"
1684 " to select meta-data sections is deprecated at 4.6"
1685 " and will be removed in 4.7");
1686 warned_objc_46 = true;
1689 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1690 return darwin_sections[objc_cls_meth_section];
1691 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1692 return darwin_sections[objc_inst_meth_section];
1693 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29))
1694 return darwin_sections[objc_cat_cls_meth_section];
1695 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32))
1696 return darwin_sections[objc_cat_inst_meth_section];
1697 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1698 return darwin_sections[objc_class_vars_section];
1699 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1700 return darwin_sections[objc_instance_vars_section];
1701 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1702 return darwin_sections[objc_cat_cls_meth_section];
1703 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1704 return darwin_sections[objc_class_names_section];
1705 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1706 return darwin_sections[objc_meth_var_names_section];
1707 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1708 return darwin_sections[objc_meth_var_types_section];
1709 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1710 return darwin_sections[objc_cls_refs_section];
1711 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1712 return darwin_sections[objc_class_section];
1713 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1714 return darwin_sections[objc_meta_class_section];
1715 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1716 return darwin_sections[objc_category_section];
1717 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1718 return darwin_sections[objc_selector_refs_section];
1719 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1720 return darwin_sections[objc_selector_fixup_section];
1721 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1722 return darwin_sections[objc_symbols_section];
1723 else if (!strncmp (name, "_OBJC_MODULES", 13))
1724 return darwin_sections[objc_module_info_section];
1725 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1726 return darwin_sections[objc_image_info_section];
1727 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1728 return darwin_sections[objc_cat_inst_meth_section];
1729 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1730 return darwin_sections[objc_cat_cls_meth_section];
1731 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1732 return darwin_sections[objc_cat_cls_meth_section];
1733 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1734 return darwin_sections[objc_protocol_section];
1735 else
1736 return base_section;
1739 return base_section;
1742 /* This can be called with address expressions as "rtx".
1743 They must go in "const". */
1745 section *
1746 machopic_select_rtx_section (machine_mode mode, rtx x,
1747 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1749 if (GET_MODE_SIZE (mode) == 8
1750 && (GET_CODE (x) == CONST_INT
1751 || GET_CODE (x) == CONST_WIDE_INT
1752 || GET_CODE (x) == CONST_DOUBLE))
1753 return darwin_sections[literal8_section];
1754 else if (GET_MODE_SIZE (mode) == 4
1755 && (GET_CODE (x) == CONST_INT
1756 || GET_CODE (x) == CONST_WIDE_INT
1757 || GET_CODE (x) == CONST_DOUBLE))
1758 return darwin_sections[literal4_section];
1759 else if (HAVE_GAS_LITERAL16
1760 && TARGET_64BIT
1761 && GET_MODE_SIZE (mode) == 16
1762 && (GET_CODE (x) == CONST_INT
1763 || GET_CODE (x) == CONST_WIDE_INT
1764 || GET_CODE (x) == CONST_DOUBLE
1765 || GET_CODE (x) == CONST_VECTOR))
1766 return darwin_sections[literal16_section];
1767 else if (MACHOPIC_INDIRECT
1768 && (GET_CODE (x) == SYMBOL_REF
1769 || GET_CODE (x) == CONST
1770 || GET_CODE (x) == LABEL_REF))
1771 return darwin_sections[const_data_section];
1772 else
1773 return darwin_sections[const_section];
1776 void
1777 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1779 cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)};
1781 vec_safe_push (ctors, new_elt);
1783 if (! MACHOPIC_INDIRECT)
1784 fprintf (asm_out_file, ".reference .constructors_used\n");
1787 void
1788 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1790 cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)};
1792 vec_safe_push (dtors, new_elt);
1794 if (! MACHOPIC_INDIRECT)
1795 fprintf (asm_out_file, ".reference .destructors_used\n");
1798 static int
1799 sort_cdtor_records (const void * a, const void * b)
1801 const cdtor_record *cda = (const cdtor_record *)a;
1802 const cdtor_record *cdb = (const cdtor_record *)b;
1803 if (cda->priority > cdb->priority)
1804 return 1;
1805 if (cda->priority < cdb->priority)
1806 return -1;
1807 if (cda->position > cdb->position)
1808 return 1;
1809 if (cda->position < cdb->position)
1810 return -1;
1811 return 0;
1814 static void
1815 finalize_ctors ()
1817 unsigned int i;
1818 cdtor_record *elt;
1820 if (MACHOPIC_INDIRECT)
1821 switch_to_section (darwin_sections[mod_init_section]);
1822 else
1823 switch_to_section (darwin_sections[constructor_section]);
1825 if (vec_safe_length (ctors) > 1)
1826 ctors->qsort (sort_cdtor_records);
1827 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
1829 assemble_align (POINTER_SIZE);
1830 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1834 static void
1835 finalize_dtors ()
1837 unsigned int i;
1838 cdtor_record *elt;
1840 if (MACHOPIC_INDIRECT)
1841 switch_to_section (darwin_sections[mod_term_section]);
1842 else
1843 switch_to_section (darwin_sections[destructor_section]);
1845 if (vec_safe_length (dtors) > 1)
1846 dtors->qsort (sort_cdtor_records);
1847 FOR_EACH_VEC_SAFE_ELT (dtors, i, elt)
1849 assemble_align (POINTER_SIZE);
1850 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1854 void
1855 darwin_globalize_label (FILE *stream, const char *name)
1857 if (!!strncmp (name, "_OBJC_", 6))
1858 default_globalize_label (stream, name);
1861 /* This routine returns non-zero if 'name' starts with the special objective-c
1862 anonymous file-scope static name. It accommodates c++'s mangling of such
1863 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
1865 int
1866 darwin_label_is_anonymous_local_objc_name (const char *name)
1868 const unsigned char *p = (const unsigned char *) name;
1869 if (*p != '_')
1870 return 0;
1871 if (p[1] == 'Z' && p[2] == 'L')
1873 p += 3;
1874 while (*p >= '0' && *p <= '9')
1875 p++;
1877 return (!strncmp ((const char *)p, "_OBJC_", 6));
1880 /* LTO support for Mach-O.
1882 This version uses three mach-o sections to encapsulate the (unlimited
1883 number of) lto sections.
1885 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
1886 __GNU_LTO, __section_names contains the GNU LTO section names.
1887 __GNU_LTO, __section_index contains an array of values that index these.
1889 Indexed thus:
1890 <section offset from the start of __GNU_LTO, __lto_sections>,
1891 <section length>
1892 <name offset from the start of __GNU_LTO, __section_names,
1893 <name length>.
1895 At present, for both m32 and m64 mach-o files each of these fields is
1896 represented by a uint32_t. This is because, AFAICT, a mach-o object
1897 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1899 uint32_t offset;
1900 "offset An integer specifying the offset to this section in the file." */
1902 /* Count lto section numbers. */
1903 static unsigned int lto_section_num = 0;
1905 /* A vector of information about LTO sections, at present, we only have
1906 the name. TODO: see if we can get the data length somehow. */
1907 typedef struct GTY (()) darwin_lto_section_e {
1908 const char *sectname;
1909 } darwin_lto_section_e ;
1911 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
1913 /* Section wrapper scheme (used here to wrap the unlimited number of LTO
1914 sections into three Mach-O ones).
1915 NOTE: These names MUST be kept in sync with those in
1916 libiberty/simple-object-mach-o. */
1917 #define LTO_SECTS_SECTION "__wrapper_sects"
1918 #define LTO_NAMES_SECTION "__wrapper_names"
1919 #define LTO_INDEX_SECTION "__wrapper_index"
1921 /* File to temporarily store LTO data. This is appended to asm_out_file
1922 in darwin_end_file. */
1923 static FILE *lto_asm_out_file, *saved_asm_out_file;
1924 static char *lto_asm_out_name;
1926 /* Prepare asm_out_file for LTO output. For darwin, this means hiding
1927 asm_out_file and switching to an alternative output file. */
1928 void
1929 darwin_asm_lto_start (void)
1931 gcc_assert (! saved_asm_out_file);
1932 saved_asm_out_file = asm_out_file;
1933 if (! lto_asm_out_name)
1934 lto_asm_out_name = make_temp_file (".lto.s");
1935 lto_asm_out_file = fopen (lto_asm_out_name, "a");
1936 if (lto_asm_out_file == NULL)
1937 fatal_error ("failed to open temporary file %s for LTO output",
1938 lto_asm_out_name);
1939 asm_out_file = lto_asm_out_file;
1942 /* Restore asm_out_file. */
1943 void
1944 darwin_asm_lto_end (void)
1946 gcc_assert (saved_asm_out_file);
1947 fclose (lto_asm_out_file);
1948 asm_out_file = saved_asm_out_file;
1949 saved_asm_out_file = NULL;
1952 static void
1953 darwin_asm_dwarf_section (const char *name, unsigned int flags, tree decl);
1955 /* Called for the TARGET_ASM_NAMED_SECTION hook. */
1957 void
1958 darwin_asm_named_section (const char *name,
1959 unsigned int flags,
1960 tree decl ATTRIBUTE_UNUSED)
1962 /* LTO sections go in a special section that encapsulates the (unlimited)
1963 number of GNU LTO sections within a single mach-o one. */
1964 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1965 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1967 darwin_lto_section_e e;
1968 /* We expect certain flags to be set... */
1969 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1970 == (SECTION_DEBUG | SECTION_NAMED));
1972 /* Switch to our combined section. */
1973 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
1974 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
1975 /* Output a label for the start of this sub-section. */
1976 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
1977 lto_section_num, name);
1978 /* We have to jump through hoops to get the values of the intra-section
1979 offsets... */
1980 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
1981 lto_section_num, lto_section_num);
1982 fprintf (asm_out_file,
1983 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
1984 lto_section_num, lto_section_num+1, lto_section_num);
1985 lto_section_num++;
1986 e.sectname = xstrdup (name);
1987 /* Keep the names, we'll need to make a table later.
1988 TODO: check that we do not revisit sections, that would break
1989 the assumption of how this is done. */
1990 if (lto_section_names == NULL)
1991 vec_alloc (lto_section_names, 16);
1992 vec_safe_push (lto_section_names, e);
1994 else if (strncmp (name, "__DWARF,", 8) == 0)
1995 darwin_asm_dwarf_section (name, flags, decl);
1996 else
1997 fprintf (asm_out_file, "\t.section %s\n", name);
2000 void
2001 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
2003 /* Darwin does not use unique sections. */
2006 /* Handle __attribute__ ((apple_kext_compatibility)).
2007 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
2008 vtable for classes with this attribute (and their descendants) by not
2009 outputting the new 3.0 nondeleting destructor. This means that such
2010 objects CANNOT be allocated on the stack or as globals UNLESS they have
2011 a completely empty `operator delete'.
2012 Luckily, this fits in with the Darwin kext model.
2014 This attribute also disables gcc3's potential overlaying of derived
2015 class data members on the padding at the end of the base class. */
2017 tree
2018 darwin_handle_kext_attribute (tree *node, tree name,
2019 tree args ATTRIBUTE_UNUSED,
2020 int flags ATTRIBUTE_UNUSED,
2021 bool *no_add_attrs)
2023 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
2024 if (! TARGET_KEXTABI)
2026 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2027 "only when compiling a kext", name);
2029 *no_add_attrs = true;
2031 else if (TREE_CODE (*node) != RECORD_TYPE)
2033 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2034 "only to C++ classes", name);
2036 *no_add_attrs = true;
2039 return NULL_TREE;
2042 /* Handle a "weak_import" attribute; arguments as in
2043 struct attribute_spec.handler. */
2045 tree
2046 darwin_handle_weak_import_attribute (tree *node, tree name,
2047 tree ARG_UNUSED (args),
2048 int ARG_UNUSED (flags),
2049 bool * no_add_attrs)
2051 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
2053 warning (OPT_Wattributes, "%qE attribute ignored",
2054 name);
2055 *no_add_attrs = true;
2057 else
2058 declare_weak (*node);
2060 return NULL_TREE;
2063 /* Emit a label for an FDE, making it global and/or weak if appropriate.
2064 The third parameter is nonzero if this is for exception handling.
2065 The fourth parameter is nonzero if this is just a placeholder for an
2066 FDE that we are omitting. */
2068 void
2069 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
2071 char *lab ;
2072 char buf[32];
2073 static int invok_count = 0;
2074 static tree last_fun_decl = NULL_TREE;
2076 /* We use the linker to emit the .eh labels for Darwin 9 and above. */
2077 if (! for_eh || generating_for_darwin_version >= 9)
2078 return;
2080 /* FIXME: This only works when the eh for all sections of a function is
2081 emitted at the same time. If that changes, we would need to use a lookup
2082 table of some form to determine what to do. Also, we should emit the
2083 unadorned label for the partition containing the public label for a
2084 function. This is of limited use, probably, since we do not currently
2085 enable partitioning. */
2086 strcpy (buf, ".eh");
2087 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
2089 if (decl == last_fun_decl)
2091 invok_count++;
2092 snprintf (buf, 31, "$$part$$%d.eh", invok_count);
2094 else
2096 last_fun_decl = decl;
2097 invok_count = 0;
2101 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
2103 if (TREE_PUBLIC (decl))
2105 targetm.asm_out.globalize_label (file, lab);
2106 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
2108 fputs ("\t.private_extern ", file);
2109 assemble_name (file, lab);
2110 fputc ('\n', file);
2114 if (DECL_WEAK (decl))
2116 fputs ("\t.weak_definition ", file);
2117 assemble_name (file, lab);
2118 fputc ('\n', file);
2121 assemble_name (file, lab);
2122 if (empty)
2124 fputs (" = 0\n", file);
2126 /* Mark the absolute .eh and .eh1 style labels as needed to
2127 ensure that we don't dead code strip them and keep such
2128 labels from another instantiation point until we can fix this
2129 properly with group comdat support. */
2130 darwin_mark_decl_preserved (lab);
2132 else
2133 fputs (":\n", file);
2135 free (lab);
2138 static GTY(()) unsigned long except_table_label_num;
2140 void
2141 darwin_emit_except_table_label (FILE *file)
2143 char section_start_label[30];
2145 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
2146 except_table_label_num++);
2147 ASM_OUTPUT_LABEL (file, section_start_label);
2149 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
2151 void
2152 darwin_non_lazy_pcrel (FILE *file, rtx addr)
2154 const char *nlp_name;
2156 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2158 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2159 fputs ("\t.long\t", file);
2160 ASM_OUTPUT_LABELREF (file, nlp_name);
2161 fputs ("-.", file);
2164 /* If this is uncommented, details of each allocation will be printed
2165 in the asm right before the actual code. WARNING - this will cause some
2166 test-suite fails (since the printout will contain items that some tests
2167 are not expecting) -- so don't leave it on by default (it bloats the
2168 asm too). */
2169 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
2171 /* The first two of these routines are ostensibly just intended to put
2172 names into the asm. However, they are both hijacked in order to ensure
2173 that zero-sized items do not make their way into the output. Consequently,
2174 we also need to make these participate in provisions for dealing with
2175 such items in section anchors. */
2177 /* The implementation of ASM_DECLARE_OBJECT_NAME. */
2178 /* The RTTI data (e.g., __ti4name) is common and public (and static),
2179 but it does need to be referenced via indirect PIC data pointers.
2180 The machopic_define_symbol calls are telling the machopic subsystem
2181 that the name *is* defined in this module, so it doesn't need to
2182 make them indirect. */
2183 void
2184 darwin_asm_declare_object_name (FILE *file,
2185 const char *nam, tree decl)
2187 const char *xname = nam;
2188 unsigned HOST_WIDE_INT size;
2189 bool local_def, weak;
2191 weak = (DECL_P (decl)
2192 && DECL_WEAK (decl)
2193 && !lookup_attribute ("weak_import",
2194 DECL_ATTRIBUTES (decl)));
2196 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
2197 && (!DECL_COMMON (decl)
2198 || !TREE_PUBLIC (decl)));
2200 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
2201 xname = IDENTIFIER_POINTER (DECL_NAME (decl));
2203 if (local_def)
2205 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2206 if (!weak)
2207 machopic_define_symbol (DECL_RTL (decl));
2210 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2212 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2213 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
2214 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
2215 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
2216 (unsigned long long)size, DECL_ALIGN (decl), local_def,
2217 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2218 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
2219 (unsigned long)DECL_INITIAL (decl));
2220 #endif
2222 /* Darwin needs help to support local zero-sized objects.
2223 They must be made at least one byte, and the section containing must be
2224 marked as unsuitable for section-anchors (see storage allocators below).
2226 For non-zero objects this output is handled by varasm.c.
2228 if (!size)
2230 unsigned int l2align = 0;
2232 /* The align must be honored, even for zero-sized. */
2233 if (DECL_ALIGN (decl))
2235 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
2236 fprintf (file, "\t.align\t%u\n", l2align);
2239 ASM_OUTPUT_LABEL (file, xname);
2240 size = 1;
2241 fprintf (file, "\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2243 /* Check that we've correctly picked up the zero-sized item and placed it
2244 properly. */
2245 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2246 || (in_section
2247 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2249 else
2250 ASM_OUTPUT_LABEL (file, xname);
2253 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
2254 void
2255 darwin_asm_declare_constant_name (FILE *file, const char *name,
2256 const_tree exp ATTRIBUTE_UNUSED,
2257 HOST_WIDE_INT size)
2259 assemble_label (file, name);
2260 /* As for other items, we need at least one byte. */
2261 if (!size)
2263 fputs ("\t.space\t1\n", file);
2264 /* Check that we've correctly picked up the zero-sized item and placed it
2265 properly. */
2266 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2267 || (in_section
2268 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2272 /* Darwin storage allocators.
2274 Zerofill sections are desirable for large blank data since, otherwise, these
2275 data bloat objects (PR33210).
2277 However, section anchors don't work in .zerofill sections (one cannot switch
2278 to a zerofill section). Ergo, for Darwin targets using section anchors we need
2279 to put (at least some) data into 'normal' switchable sections.
2281 Here we set a relatively arbitrary value for the size of an object to trigger
2282 zerofill when section anchors are enabled (anything bigger than a page for
2283 current Darwin implementations). FIXME: there ought to be some objective way
2284 to make this choice.
2286 When section anchor are off this is ignored anyway. */
2288 #define BYTES_ZFILL 4096
2290 /* Emit a chunk of data for items coalesced by the linker. */
2291 static void
2292 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
2293 unsigned HOST_WIDE_INT size,
2294 unsigned int align)
2296 /* Since the sections used here are coalesed, they will not be eligible
2297 for section anchors, and therefore we don't need to break that out. */
2298 if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
2299 switch_to_section (darwin_sections[const_data_coal_section]);
2300 else
2301 switch_to_section (darwin_sections[data_coal_section]);
2303 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
2304 the align info for zero-sized items... but do it here otherwise. */
2305 if (size && align)
2306 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2308 if (TREE_PUBLIC (decl))
2309 darwin_globalize_label (fp, name);
2311 /* ... and we let it deal with outputting one byte of zero for them too. */
2312 darwin_asm_declare_object_name (fp, name, decl);
2313 if (size)
2314 assemble_zeros (size);
2317 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
2318 static void
2319 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
2320 unsigned HOST_WIDE_INT size,
2321 unsigned int align, tree meta)
2323 section *ocs = data_section;
2325 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
2326 ocs = darwin_objc2_section (decl, meta, ocs);
2327 else
2328 ocs = darwin_objc1_section (decl, meta, ocs);
2330 switch_to_section (ocs);
2332 /* We shall declare that zero-sized meta-data are not valid (yet). */
2333 gcc_assert (size);
2334 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2336 /* ... and we let it deal with outputting one byte of zero for them too. */
2337 darwin_asm_declare_object_name (fp, name, decl);
2338 assemble_zeros (size);
2341 /* This routine emits 'local' storage:
2343 When Section Anchors are off this routine emits .zerofill commands in
2344 sections named for their alignment.
2346 When Section Anchors are on, smaller (non-zero-sized) items are placed in
2347 the .static_data section so that the section anchoring system can see them.
2348 Larger items are still placed in .zerofill sections, addressing PR33210.
2349 The routine has no checking - it is all assumed to be done by the caller.
2351 static void
2352 darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
2353 unsigned HOST_WIDE_INT size,
2354 unsigned int l2align)
2356 /* FIXME: We have a fudge to make this work with Java even when the target does
2357 not use sections anchors -- Java seems to need at least one small item in a
2358 non-zerofill segment. */
2359 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2360 || (size && size <= 2))
2362 /* Put smaller objects in _static_data, where the section anchors system
2363 can get them.
2364 However, if they are zero-sized punt them to yet a different section
2365 (that is not allowed to participate in anchoring). */
2366 if (!size)
2368 fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
2369 in_section = darwin_sections[zobj_bss_section];
2370 size = 1;
2372 else
2374 fputs ("\t.static_data\n", fp);
2375 in_section = darwin_sections[static_data_section];
2378 if (l2align)
2379 fprintf (fp, "\t.align\t%u\n", l2align);
2381 assemble_name (fp, name);
2382 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2384 else
2386 /* When we are on a non-section anchor target, we can get zero-sized
2387 items here. However, all we need to do is to bump them to one byte
2388 and the section alignment will take care of the rest. */
2389 char secnam[64];
2390 unsigned int flags ;
2391 snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"),
2392 (unsigned) l2align);
2393 /* We can't anchor (yet, if ever) in zerofill sections, because we can't
2394 switch to them and emit a label. */
2395 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2396 in_section = get_section (secnam, flags, NULL);
2397 fprintf (fp, "\t.zerofill %s,", secnam);
2398 assemble_name (fp, name);
2399 if (!size)
2400 size = 1;
2402 if (l2align)
2403 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2404 size, (unsigned) l2align);
2405 else
2406 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2409 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2410 /* This is defined as a file-scope var, so we know to notify machopic. */
2411 machopic_define_symbol (DECL_RTL (decl));
2414 /* Emit a chunk of common. */
2415 static void
2416 darwin_emit_common (FILE *fp, const char *name,
2417 unsigned HOST_WIDE_INT size, unsigned int align)
2419 unsigned HOST_WIDE_INT rounded;
2420 unsigned int l2align;
2422 /* Earlier systems complain if the alignment exceeds the page size.
2423 The magic number is 4096 * 8 - hard-coded for legacy systems. */
2424 if (!emit_aligned_common && (align > 32768UL))
2425 align = 4096UL; /* In units. */
2426 else
2427 align /= BITS_PER_UNIT;
2429 /* Make sure we have a meaningful align. */
2430 if (!align)
2431 align = 1;
2433 /* For earlier toolchains, we need to emit the var as a rounded size to
2434 tell ld the alignment. */
2435 if (size < align)
2436 rounded = align;
2437 else
2438 rounded = (size + (align-1)) & ~(align-1);
2440 l2align = floor_log2 (align);
2441 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2443 in_section = comm_section;
2444 /* We mustn't allow multiple public symbols to share an address when using
2445 the normal OSX toolchain. */
2446 if (!size)
2448 /* Put at least one byte. */
2449 size = 1;
2450 /* This section can no longer participate in section anchoring. */
2451 comm_section->common.flags |= SECTION_NO_ANCHOR;
2454 fputs ("\t.comm\t", fp);
2455 assemble_name (fp, name);
2456 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
2457 emit_aligned_common?size:rounded);
2458 if (l2align && emit_aligned_common)
2459 fprintf (fp, ",%u", l2align);
2460 fputs ("\n", fp);
2463 /* Output a var which is all zero - into aligned BSS sections, common, lcomm
2464 or coalescable data sections (for weak or comdat) as appropriate. */
2466 void
2467 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
2468 unsigned HOST_WIDE_INT size, unsigned int align)
2470 unsigned int l2align;
2471 bool one, pub, weak;
2472 tree meta;
2474 pub = TREE_PUBLIC (decl);
2475 one = DECL_ONE_ONLY (decl);
2476 weak = (DECL_P (decl)
2477 && DECL_WEAK (decl)
2478 && !lookup_attribute ("weak_import",
2479 DECL_ATTRIBUTES (decl)));
2481 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2482 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
2483 " pub %d weak %d one %d init %lx\n",
2484 name, (long long)size, (int)align, TREE_READONLY (decl),
2485 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2486 pub, weak, one, (unsigned long)DECL_INITIAL (decl));
2487 #endif
2489 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2490 before the target has a chance to comment. */
2491 if ((meta = is_objc_metadata (decl)))
2493 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2494 return;
2497 /* Check that any initializer is valid. */
2498 gcc_assert ((DECL_INITIAL (decl) == NULL)
2499 || (DECL_INITIAL (decl) == error_mark_node)
2500 || initializer_zerop (DECL_INITIAL (decl)));
2502 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2503 gcc_assert (!DECL_COMMON (decl));
2505 /* Pick up the correct alignment. */
2506 if (!size || !align)
2507 align = DECL_ALIGN (decl);
2509 l2align = floor_log2 (align / BITS_PER_UNIT);
2510 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2512 last_assemble_variable_decl = decl;
2514 /* We would rather not have to check this here - but it seems that we might
2515 be passed a decl that should be in coalesced space. */
2516 if (one || weak)
2518 /* Weak or COMDAT objects are put in mergeable sections. */
2519 darwin_emit_weak_or_comdat (fp, decl, name, size,
2520 DECL_ALIGN (decl));
2521 return;
2524 /* If this is not public, then emit according to local rules. */
2525 if (!pub)
2527 darwin_emit_local_bss (fp, decl, name, size, l2align);
2528 return;
2531 /* So we have a public symbol (small item fudge for Java, see above). */
2532 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2533 || (size && size <= 2))
2535 /* Put smaller objects in data, where the section anchors system can get
2536 them. However, if they are zero-sized punt them to yet a different
2537 section (that is not allowed to participate in anchoring). */
2538 if (!size)
2540 fputs ("\t.section\t__DATA,__zobj_data\n", fp);
2541 in_section = darwin_sections[zobj_data_section];
2542 size = 1;
2544 else
2546 fputs ("\t.data\n", fp);
2547 in_section = data_section;
2550 if (l2align)
2551 fprintf (fp, "\t.align\t%u\n", l2align);
2553 assemble_name (fp, name);
2554 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2556 else
2558 char secnam[64];
2559 unsigned int flags ;
2560 /* When we are on a non-section anchor target, we can get zero-sized
2561 items here. However, all we need to do is to bump them to one byte
2562 and the section alignment will take care of the rest. */
2563 snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align);
2565 /* We can't anchor in zerofill sections, because we can't switch
2566 to them and emit a label. */
2567 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2568 in_section = get_section (secnam, flags, NULL);
2569 fprintf (fp, "\t.zerofill %s,", secnam);
2570 assemble_name (fp, name);
2571 if (!size)
2572 size = 1;
2574 if (l2align)
2575 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
2576 else
2577 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2579 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2582 /* Output a chunk of common, with alignment specified (where the target
2583 supports this). */
2584 void
2585 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
2586 unsigned HOST_WIDE_INT size,
2587 unsigned int align)
2589 unsigned int l2align;
2590 bool one, weak;
2591 tree meta;
2593 /* No corresponding var. */
2594 if (decl==NULL)
2596 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2597 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
2598 #endif
2599 darwin_emit_common (fp, name, size, align);
2600 return;
2603 one = DECL_ONE_ONLY (decl);
2604 weak = (DECL_P (decl)
2605 && DECL_WEAK (decl)
2606 && !lookup_attribute ("weak_import",
2607 DECL_ATTRIBUTES (decl)));
2609 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2610 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
2611 " weak %d one %d init %lx\n",
2612 name, (long long)size, (int)align, TREE_READONLY (decl),
2613 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2614 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
2615 #endif
2617 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2618 before the target has a chance to comment. */
2619 if ((meta = is_objc_metadata (decl)))
2621 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2622 return;
2625 /* We shouldn't be messing with this if the decl has a section name. */
2626 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2628 /* We would rather not have to check this here - but it seems that we might
2629 be passed a decl that should be in coalesced space. */
2630 if (one || weak)
2632 /* Weak or COMDAT objects are put in mergable sections. */
2633 darwin_emit_weak_or_comdat (fp, decl, name, size,
2634 DECL_ALIGN (decl));
2635 return;
2638 /* We should only get here for DECL_COMMON, with a zero init (and, in
2639 principle, only for public symbols too - although we deal with local
2640 ones below). */
2642 /* Check the initializer is OK. */
2643 gcc_assert (DECL_COMMON (decl)
2644 && ((DECL_INITIAL (decl) == NULL)
2645 || (DECL_INITIAL (decl) == error_mark_node)
2646 || initializer_zerop (DECL_INITIAL (decl))));
2648 last_assemble_variable_decl = decl;
2650 if (!size || !align)
2651 align = DECL_ALIGN (decl);
2653 l2align = floor_log2 (align / BITS_PER_UNIT);
2654 /* Check we aren't asking for more aligment than the platform allows. */
2655 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2657 if (TREE_PUBLIC (decl) != 0)
2658 darwin_emit_common (fp, name, size, align);
2659 else
2660 darwin_emit_local_bss (fp, decl, name, size, l2align);
2663 /* Output a chunk of BSS with alignment specfied. */
2664 void
2665 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
2666 unsigned HOST_WIDE_INT size,
2667 unsigned int align)
2669 unsigned long l2align;
2670 bool one, weak;
2671 tree meta;
2673 one = DECL_ONE_ONLY (decl);
2674 weak = (DECL_P (decl)
2675 && DECL_WEAK (decl)
2676 && !lookup_attribute ("weak_import",
2677 DECL_ATTRIBUTES (decl)));
2679 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2680 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
2681 " weak %d init %lx\n",
2682 name, (long long)size, (int)align, TREE_READONLY (decl),
2683 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
2684 weak , (unsigned long)DECL_INITIAL (decl));
2685 #endif
2687 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2688 before the target has a chance to comment. */
2689 if ((meta = is_objc_metadata (decl)))
2691 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2692 return;
2695 /* We shouldn't be messing with this if the decl has a section name. */
2696 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2698 /* We would rather not have to check this here - but it seems that we might
2699 be passed a decl that should be in coalesced space. */
2700 if (one || weak)
2702 /* Weak or COMDAT objects are put in mergable sections. */
2703 darwin_emit_weak_or_comdat (fp, decl, name, size,
2704 DECL_ALIGN (decl));
2705 return;
2708 /* .. and it should be suitable for placement in local mem. */
2709 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
2710 /* .. and any initializer must be all-zero. */
2711 gcc_assert ((DECL_INITIAL (decl) == NULL)
2712 || (DECL_INITIAL (decl) == error_mark_node)
2713 || initializer_zerop (DECL_INITIAL (decl)));
2715 last_assemble_variable_decl = decl;
2717 if (!size || !align)
2718 align = DECL_ALIGN (decl);
2720 l2align = floor_log2 (align / BITS_PER_UNIT);
2721 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2723 darwin_emit_local_bss (fp, decl, name, size, l2align);
2726 /* Emit an assembler directive to set visibility for a symbol. The
2727 only supported visibilities are VISIBILITY_DEFAULT and
2728 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
2729 extern". There is no MACH-O equivalent of ELF's
2730 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
2732 void
2733 darwin_assemble_visibility (tree decl, int vis)
2735 if (vis == VISIBILITY_DEFAULT)
2737 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
2739 fputs ("\t.private_extern ", asm_out_file);
2740 assemble_name (asm_out_file,
2741 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
2742 fputs ("\n", asm_out_file);
2744 else
2745 warning (OPT_Wattributes, "protected visibility attribute "
2746 "not supported in this configuration; ignored");
2749 /* vec used by darwin_asm_dwarf_section.
2750 Maybe a hash tab would be better here - but the intention is that this is
2751 a very short list (fewer than 16 items) and each entry should (ideally,
2752 eventually) only be presented once.
2754 A structure to hold a dwarf debug section used entry. */
2756 typedef struct GTY(()) dwarf_sect_used_entry {
2757 const char *name;
2758 unsigned count;
2760 dwarf_sect_used_entry;
2763 /* A list of used __DWARF sections. */
2764 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
2766 /* This is called when we are asked to assemble a named section and the
2767 name begins with __DWARF,. We keep a list of the section names (without
2768 the __DWARF, prefix) and use this to emit our required start label on the
2769 first switch to each section. */
2771 static void
2772 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2773 tree ARG_UNUSED (decl))
2775 unsigned i;
2776 int namelen;
2777 const char * sname;
2778 dwarf_sect_used_entry *ref;
2779 bool found = false;
2780 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2781 == (SECTION_DEBUG | SECTION_NAMED));
2782 /* We know that the name starts with __DWARF, */
2783 sname = name + 8;
2784 namelen = strchr (sname, ',') - sname;
2785 gcc_assert (namelen);
2786 if (dwarf_sect_names_table == NULL)
2787 vec_alloc (dwarf_sect_names_table, 16);
2788 else
2789 for (i = 0;
2790 dwarf_sect_names_table->iterate (i, &ref);
2791 i++)
2793 if (!ref)
2794 break;
2795 if (!strcmp (ref->name, sname))
2797 found = true;
2798 ref->count++;
2799 break;
2803 fprintf (asm_out_file, "\t.section %s\n", name);
2804 if (!found)
2806 dwarf_sect_used_entry e;
2807 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
2808 e.count = 1;
2809 e.name = xstrdup (sname);
2810 vec_safe_push (dwarf_sect_names_table, e);
2814 /* Output a difference of two labels that will be an assembly time
2815 constant if the two labels are local. (.long lab1-lab2 will be
2816 very different if lab1 is at the boundary between two sections; it
2817 will be relocated according to the second section, not the first,
2818 so one ends up with a difference between labels in different
2819 sections, which is bad in the dwarf2 eh context for instance.) */
2821 static int darwin_dwarf_label_counter;
2823 void
2824 darwin_asm_output_dwarf_delta (FILE *file, int size,
2825 const char *lab1, const char *lab2)
2827 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
2828 && lab2[0] == '*' && lab2[1] == 'L');
2829 const char *directive = (size == 8 ? ".quad" : ".long");
2831 if (islocaldiff)
2832 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
2833 else
2834 fprintf (file, "\t%s\t", directive);
2836 assemble_name_raw (file, lab1);
2837 fprintf (file, "-");
2838 assemble_name_raw (file, lab2);
2839 if (islocaldiff)
2840 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
2843 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
2844 offsets are not represented using relocs in .o files; either the
2845 section never leaves the .o file, or the linker or other tool is
2846 responsible for parsing the DWARF and updating the offsets. */
2848 void
2849 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
2850 section *base)
2852 char sname[64];
2853 int namelen;
2855 gcc_assert (base->common.flags & SECTION_NAMED);
2856 gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
2857 gcc_assert (strchr (base->named.name + 8, ','));
2859 namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
2860 sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
2861 darwin_asm_output_dwarf_delta (file, size, lab, sname);
2864 /* Called from the within the TARGET_ASM_FILE_START for each target. */
2866 void
2867 darwin_file_start (void)
2869 /* Nothing to do. */
2872 /* Called for the TARGET_ASM_FILE_END hook.
2873 Emit the mach-o pic indirection data, the lto data and, finally a flag
2874 to tell the linker that it can break the file object into sections and
2875 move those around for efficiency. */
2877 void
2878 darwin_file_end (void)
2880 if (!vec_safe_is_empty (ctors))
2881 finalize_ctors ();
2882 if (!vec_safe_is_empty (dtors))
2883 finalize_dtors ();
2885 /* If we are expecting to output NeXT ObjC meta-data, (and we actually see
2886 some) then we output the fix-and-continue marker (Image Info).
2887 This applies to Objective C, Objective C++ and LTO with either language
2888 as part of the input. */
2889 if (flag_next_runtime && objc_metadata_seen)
2891 unsigned int flags = 0;
2892 if (flag_objc_abi >= 2)
2894 flags = 16;
2895 output_section_asm_op
2896 (darwin_sections[objc2_image_info_section]->unnamed.data);
2898 else
2899 output_section_asm_op
2900 (darwin_sections[objc_image_info_section]->unnamed.data);
2902 ASM_OUTPUT_ALIGN (asm_out_file, 2);
2903 fputs ("L_OBJC_ImageInfo:\n", asm_out_file);
2905 flags |= (flag_replace_objc_classes && classes_seen) ? 1 : 0;
2906 flags |= flag_objc_gc ? 2 : 0;
2908 fprintf (asm_out_file, "\t.long\t0\n\t.long\t%u\n", flags);
2911 machopic_finish (asm_out_file);
2912 if (lang_GNU_CXX ())
2914 switch_to_section (darwin_sections[constructor_section]);
2915 switch_to_section (darwin_sections[destructor_section]);
2916 ASM_OUTPUT_ALIGN (asm_out_file, 1);
2919 /* If there was LTO assembler output, append it to asm_out_file. */
2920 if (lto_asm_out_name)
2922 int n;
2923 char *buf, *lto_asm_txt;
2925 /* Shouldn't be here if we failed to switch back. */
2926 gcc_assert (! saved_asm_out_file);
2928 lto_asm_out_file = fopen (lto_asm_out_name, "r");
2929 if (lto_asm_out_file == NULL)
2930 fatal_error ("failed to open temporary file %s with LTO output",
2931 lto_asm_out_name);
2932 fseek (lto_asm_out_file, 0, SEEK_END);
2933 n = ftell (lto_asm_out_file);
2934 if (n > 0)
2936 fseek (lto_asm_out_file, 0, SEEK_SET);
2937 lto_asm_txt = buf = (char *) xmalloc (n + 1);
2938 while (fgets (lto_asm_txt, n, lto_asm_out_file))
2939 fputs (lto_asm_txt, asm_out_file);
2940 /* Put a termination label. */
2941 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2942 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2943 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
2944 lto_section_num);
2945 /* Make sure our termination label stays in this section. */
2946 fputs ("\t.space\t1\n", asm_out_file);
2949 /* Remove the temporary file. */
2950 fclose (lto_asm_out_file);
2951 unlink_if_ordinary (lto_asm_out_name);
2952 free (lto_asm_out_name);
2955 /* Output the names and indices. */
2956 if (lto_section_names && lto_section_names->length ())
2958 int count;
2959 darwin_lto_section_e *ref;
2960 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
2961 the latter up ourselves. */
2962 const char *op = integer_asm_op (4,0);
2964 /* Emit the names. */
2965 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2966 LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
2967 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2969 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
2970 /* We have to jump through hoops to get the values of the intra-section
2971 offsets... */
2972 fprintf (asm_out_file,
2973 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
2974 count, count);
2975 fprintf (asm_out_file,
2976 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
2977 count, count+1, count);
2978 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
2980 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
2981 /* make sure our termination label stays in this section. */
2982 fputs ("\t.space\t1\n", asm_out_file);
2984 /* Emit the Index. */
2985 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2986 LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
2987 fputs ("\t.align\t2\n", asm_out_file);
2988 fputs ("# Section offset, Section length, Name offset, Name length\n",
2989 asm_out_file);
2990 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2992 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
2993 op, count, ref->sectname);
2994 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
2995 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
2996 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
3000 /* If we have section anchors, then we must prevent the linker from
3001 re-arranging data. */
3002 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
3003 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
3006 /* TODO: Add a language hook for identifying if a decl is a vtable. */
3007 #define DARWIN_VTABLE_P(DECL) 0
3009 /* Cross-module name binding. Darwin does not support overriding
3010 functions at dynamic-link time, except for vtables in kexts. */
3012 bool
3013 darwin_binds_local_p (const_tree decl)
3015 return default_binds_local_p_1 (decl,
3016 TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
3019 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
3020 anchor relative to ".", the current section position. We cannot use
3021 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
3022 void
3023 darwin_asm_output_anchor (rtx symbol)
3025 fprintf (asm_out_file, "\t.set\t");
3026 assemble_name (asm_out_file, XSTR (symbol, 0));
3027 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
3028 SYMBOL_REF_BLOCK_OFFSET (symbol));
3031 /* Disable section anchoring on any section containing a zero-sized
3032 object. */
3033 bool
3034 darwin_use_anchors_for_symbol_p (const_rtx symbol)
3036 if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
3038 section *sect;
3039 /* If the section contains a zero-sized object it's ineligible. */
3040 sect = SYMBOL_REF_BLOCK (symbol)->sect;
3041 /* This should have the effect of disabling anchors for vars that follow
3042 any zero-sized one, in a given section. */
3043 if (sect->common.flags & SECTION_NO_ANCHOR)
3044 return false;
3046 /* Also check the normal reasons for suppressing. */
3047 return default_use_anchors_for_symbol_p (symbol);
3049 else
3050 return false;
3053 /* Set the darwin specific attributes on TYPE. */
3054 void
3055 darwin_set_default_type_attributes (tree type)
3057 if (darwin_ms_struct
3058 && TREE_CODE (type) == RECORD_TYPE)
3059 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
3060 NULL_TREE,
3061 TYPE_ATTRIBUTES (type));
3064 /* True, iff we're generating code for loadable kernel extensions. */
3066 bool
3067 darwin_kextabi_p (void) {
3068 return flag_apple_kext;
3071 void
3072 darwin_override_options (void)
3074 /* Keep track of which (major) version we're generating code for. */
3075 if (darwin_macosx_version_min)
3077 if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
3078 generating_for_darwin_version = 10;
3079 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
3080 generating_for_darwin_version = 9;
3082 /* Earlier versions are not specifically accounted, until required. */
3085 /* In principle, this should be c-family only. However, we really need to
3086 set sensible defaults for LTO as well, since the section selection stuff
3087 should check for correctness re. the ABI. TODO: check and provide the
3088 flags (runtime & ABI) from the lto wrapper). */
3090 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
3091 if (!global_options_set.x_flag_objc_abi)
3092 global_options.x_flag_objc_abi
3093 = (!flag_next_runtime)
3095 : (TARGET_64BIT ? 2
3096 : (generating_for_darwin_version >= 9) ? 1
3097 : 0);
3099 /* Objective-C family ABI 2 is only valid for next/m64 at present. */
3100 if (global_options_set.x_flag_objc_abi && flag_next_runtime)
3102 if (TARGET_64BIT && global_options.x_flag_objc_abi < 2)
3103 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 must be"
3104 " used for %<-m64%> targets with"
3105 " %<-fnext-runtime%>");
3106 if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
3107 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 is not"
3108 " supported on %<-m32%> targets with"
3109 " %<-fnext-runtime%>");
3112 /* Don't emit DWARF3/4 unless specifically selected. This is a
3113 workaround for tool bugs. */
3114 if (!global_options_set.x_dwarf_strict)
3115 dwarf_strict = 1;
3116 if (!global_options_set.x_dwarf_version)
3117 dwarf_version = 2;
3119 /* Do not allow unwind tables to be generated by default for m32.
3120 fnon-call-exceptions will override this, regardless of what we do. */
3121 if (generating_for_darwin_version < 10
3122 && !global_options_set.x_flag_asynchronous_unwind_tables
3123 && !TARGET_64BIT)
3124 global_options.x_flag_asynchronous_unwind_tables = 0;
3126 /* Disable -freorder-blocks-and-partition when unwind tables are being
3127 emitted for Darwin < 9 (OSX 10.5).
3128 The strategy is, "Unless the User has specifically set/unset an unwind
3129 flag we will switch off -freorder-blocks-and-partition when unwind tables
3130 will be generated". If the User specifically sets flags... we assume
3131 (s)he knows why... */
3132 if (generating_for_darwin_version < 9
3133 && global_options_set.x_flag_reorder_blocks_and_partition
3134 && ((global_options.x_flag_exceptions /* User, c++, java */
3135 && !global_options_set.x_flag_exceptions) /* User specified... */
3136 || (global_options.x_flag_unwind_tables
3137 && !global_options_set.x_flag_unwind_tables)
3138 || (global_options.x_flag_non_call_exceptions
3139 && !global_options_set.x_flag_non_call_exceptions)
3140 || (global_options.x_flag_asynchronous_unwind_tables
3141 && !global_options_set.x_flag_asynchronous_unwind_tables)))
3143 inform (input_location,
3144 "-freorder-blocks-and-partition does not work with exceptions "
3145 "on this architecture");
3146 flag_reorder_blocks_and_partition = 0;
3147 flag_reorder_blocks = 1;
3150 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
3151 one valid choice of exception scheme for each runtime. */
3152 if (!global_options_set.x_flag_objc_sjlj_exceptions)
3153 global_options.x_flag_objc_sjlj_exceptions =
3154 flag_next_runtime && !TARGET_64BIT;
3156 /* FIXME: and this could be eliminated then too. */
3157 if (!global_options_set.x_flag_exceptions
3158 && flag_objc_exceptions
3159 && TARGET_64BIT)
3160 flag_exceptions = 1;
3162 if (flag_mkernel || flag_apple_kext)
3164 /* -mkernel implies -fapple-kext for C++ */
3165 if (lang_GNU_CXX ())
3166 flag_apple_kext = 1;
3168 flag_no_common = 1;
3170 /* No EH in kexts. */
3171 flag_exceptions = 0;
3172 /* No -fnon-call-exceptions data in kexts. */
3173 flag_non_call_exceptions = 0;
3174 /* so no tables either.. */
3175 flag_unwind_tables = 0;
3176 flag_asynchronous_unwind_tables = 0;
3177 /* We still need to emit branch islands for kernel context. */
3178 darwin_emit_branch_islands = true;
3181 if (flag_var_tracking_uninit == 0
3182 && generating_for_darwin_version >= 9
3183 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
3184 : (debug_info_level >= DINFO_LEVEL_NORMAL))
3185 && write_symbols == DWARF2_DEBUG)
3186 flag_var_tracking_uninit = flag_var_tracking;
3188 if (MACHO_DYNAMIC_NO_PIC_P)
3190 if (flag_pic)
3191 warning_at (UNKNOWN_LOCATION, 0,
3192 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
3193 " %<-fpie%> or %<-fPIE%>");
3194 flag_pic = 0;
3196 else if (flag_pic == 1)
3198 /* Darwin's -fpic is -fPIC. */
3199 flag_pic = 2;
3202 /* It is assumed that branch island stubs are needed for earlier systems. */
3203 if (generating_for_darwin_version < 9)
3204 darwin_emit_branch_islands = true;
3205 else
3206 emit_aligned_common = true; /* Later systems can support aligned common. */
3208 /* The c_dialect...() macros are not available to us here. */
3209 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3212 #if DARWIN_PPC
3213 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
3215 static void
3216 darwin_patch_builtin (enum built_in_function fncode)
3218 tree fn = builtin_decl_explicit (fncode);
3219 tree sym;
3220 char *newname;
3222 if (!fn)
3223 return;
3225 sym = DECL_ASSEMBLER_NAME (fn);
3226 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
3228 set_user_assembler_name (fn, newname);
3230 fn = builtin_decl_implicit (fncode);
3231 if (fn)
3232 set_user_assembler_name (fn, newname);
3235 void
3236 darwin_patch_builtins (void)
3238 if (LONG_DOUBLE_TYPE_SIZE != 128)
3239 return;
3241 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
3242 #define PATCH_BUILTIN_NO64(fncode) \
3243 if (!TARGET_64BIT) \
3244 darwin_patch_builtin (fncode);
3245 #define PATCH_BUILTIN_VARIADIC(fncode) \
3246 if (!TARGET_64BIT \
3247 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
3248 darwin_patch_builtin (fncode);
3249 #include "darwin-ppc-ldouble-patch.def"
3250 #undef PATCH_BUILTIN
3251 #undef PATCH_BUILTIN_NO64
3252 #undef PATCH_BUILTIN_VARIADIC
3254 #endif
3256 /* CFStrings implementation. */
3257 static GTY(()) tree cfstring_class_reference = NULL_TREE;
3258 static GTY(()) tree cfstring_type_node = NULL_TREE;
3259 static GTY(()) tree ccfstring_type_node = NULL_TREE;
3260 static GTY(()) tree pccfstring_type_node = NULL_TREE;
3261 static GTY(()) tree pcint_type_node = NULL_TREE;
3262 static GTY(()) tree pcchar_type_node = NULL_TREE;
3264 static enum built_in_function darwin_builtin_cfstring;
3266 /* Store all constructed constant CFStrings in a hash table so that
3267 they get uniqued properly. */
3269 typedef struct GTY ((for_user)) cfstring_descriptor {
3270 /* The string literal. */
3271 tree literal;
3272 /* The resulting constant CFString. */
3273 tree constructor;
3274 } cfstring_descriptor;
3276 struct cfstring_hasher : ggc_hasher<cfstring_descriptor *>
3278 static hashval_t hash (cfstring_descriptor *);
3279 static bool equal (cfstring_descriptor *, cfstring_descriptor *);
3282 static GTY (()) hash_table<cfstring_hasher> *cfstring_htab;
3284 static tree
3285 add_builtin_field_decl (tree type, const char *name, tree **chain)
3287 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
3288 get_identifier (name), type);
3290 if (*chain != NULL)
3291 **chain = field;
3292 *chain = &DECL_CHAIN (field);
3294 return field;
3297 tree
3298 darwin_init_cfstring_builtins (unsigned builtin_cfstring)
3300 tree cfsfun, fields, pccfstring_ftype_pcchar;
3301 tree *chain = NULL;
3303 darwin_builtin_cfstring =
3304 (enum built_in_function) builtin_cfstring;
3306 /* struct __builtin_CFString {
3307 const int *isa; (will point at
3308 int flags; __CFConstantStringClassReference)
3309 const char *str;
3310 long length;
3311 }; */
3313 pcint_type_node = build_pointer_type
3314 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
3316 pcchar_type_node = build_pointer_type
3317 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3319 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
3321 /* Have to build backwards for finish struct. */
3322 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
3323 add_builtin_field_decl (pcchar_type_node, "str", &chain);
3324 add_builtin_field_decl (integer_type_node, "flags", &chain);
3325 add_builtin_field_decl (pcint_type_node, "isa", &chain);
3326 finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
3327 fields, NULL_TREE);
3329 /* const struct __builtin_CFstring *
3330 __builtin___CFStringMakeConstantString (const char *); */
3332 ccfstring_type_node = build_qualified_type
3333 (cfstring_type_node, TYPE_QUAL_CONST);
3334 pccfstring_type_node = build_pointer_type (ccfstring_type_node);
3335 pccfstring_ftype_pcchar = build_function_type_list
3336 (pccfstring_type_node, pcchar_type_node, NULL_TREE);
3338 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
3339 get_identifier ("__builtin___CFStringMakeConstantString"),
3340 pccfstring_ftype_pcchar);
3342 TREE_PUBLIC (cfsfun) = 1;
3343 DECL_EXTERNAL (cfsfun) = 1;
3344 DECL_ARTIFICIAL (cfsfun) = 1;
3345 /* Make a lang-specific section - dup_lang_specific_decl makes a new node
3346 in place of the existing, which may be NULL. */
3347 DECL_LANG_SPECIFIC (cfsfun) = NULL;
3348 (*lang_hooks.dup_lang_specific_decl) (cfsfun);
3349 DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD;
3350 DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring;
3351 lang_hooks.builtin_function (cfsfun);
3353 /* extern int __CFConstantStringClassReference[]; */
3354 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
3355 get_identifier ("__CFConstantStringClassReference"),
3356 build_array_type (integer_type_node, NULL_TREE));
3358 TREE_PUBLIC (cfstring_class_reference) = 1;
3359 DECL_ARTIFICIAL (cfstring_class_reference) = 1;
3360 (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
3361 DECL_EXTERNAL (cfstring_class_reference) = 1;
3362 rest_of_decl_compilation (cfstring_class_reference, 0, 0);
3364 /* Initialize the hash table used to hold the constant CFString objects. */
3365 cfstring_htab = hash_table<cfstring_hasher>::create_ggc (31);
3367 return cfstring_type_node;
3370 tree
3371 darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
3372 bool ARG_UNUSED (ignore))
3374 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3376 if (fcode == darwin_builtin_cfstring)
3378 if (!darwin_constant_cfstrings)
3380 error ("built-in function %qD requires the"
3381 " %<-mconstant-cfstrings%> flag", fndecl);
3382 return error_mark_node;
3385 if (n_args != 1)
3387 error ("built-in function %qD takes one argument only", fndecl);
3388 return error_mark_node;
3391 return darwin_build_constant_cfstring (*argp);
3394 return NULL_TREE;
3397 void
3398 darwin_rename_builtins (void)
3400 /* The system ___divdc3 routine in libSystem on darwin10 is not
3401 accurate to 1ulp, ours is, so we avoid ever using the system name
3402 for this routine and instead install a non-conflicting name that
3403 is accurate.
3405 When -ffast-math or -funsafe-math-optimizations is given, we can
3406 use the faster version. */
3407 if (!flag_unsafe_math_optimizations)
3409 enum built_in_function dcode
3410 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
3411 + DCmode - MIN_MODE_COMPLEX_FLOAT);
3412 tree fn = builtin_decl_explicit (dcode);
3413 /* Fortran and c call TARGET_INIT_BUILTINS and
3414 TARGET_INIT_LIBFUNCS at different times, so we have to put a
3415 call into each to ensure that at least one of them is called
3416 after build_common_builtin_nodes. A better fix is to add a
3417 new hook to run after build_common_builtin_nodes runs. */
3418 if (fn)
3419 set_user_assembler_name (fn, "___ieee_divdc3");
3420 fn = builtin_decl_implicit (dcode);
3421 if (fn)
3422 set_user_assembler_name (fn, "___ieee_divdc3");
3426 bool
3427 darwin_libc_has_function (enum function_class fn_class)
3429 if (fn_class == function_sincos)
3430 return false;
3431 if (fn_class == function_c99_math_complex
3432 || fn_class == function_c99_misc)
3433 return (TARGET_64BIT
3434 || strverscmp (darwin_macosx_version_min, "10.3") >= 0);
3436 return true;
3439 hashval_t
3440 cfstring_hasher::hash (cfstring_descriptor *ptr)
3442 tree str = ptr->literal;
3443 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3444 int i, len = TREE_STRING_LENGTH (str);
3445 hashval_t h = len;
3447 for (i = 0; i < len; i++)
3448 h = ((h * 613) + p[i]);
3450 return h;
3453 bool
3454 cfstring_hasher::equal (cfstring_descriptor *ptr1, cfstring_descriptor *ptr2)
3456 tree str1 = ptr1->literal;
3457 tree str2 = ptr2->literal;
3458 int len1 = TREE_STRING_LENGTH (str1);
3460 return (len1 == TREE_STRING_LENGTH (str2)
3461 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3462 len1));
3465 tree
3466 darwin_build_constant_cfstring (tree str)
3468 struct cfstring_descriptor *desc, key;
3469 tree addr;
3471 if (!str)
3473 error ("CFString literal is missing");
3474 return error_mark_node;
3477 STRIP_NOPS (str);
3479 if (TREE_CODE (str) == ADDR_EXPR)
3480 str = TREE_OPERAND (str, 0);
3482 if (TREE_CODE (str) != STRING_CST)
3484 error ("CFString literal expression is not a string constant");
3485 return error_mark_node;
3488 /* Perhaps we already constructed a constant CFString just like this one? */
3489 key.literal = str;
3490 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
3491 desc = *loc;
3493 if (!desc)
3495 tree var, constructor, field;
3496 vec<constructor_elt, va_gc> *v = NULL;
3497 int length = TREE_STRING_LENGTH (str) - 1;
3499 if (darwin_warn_nonportable_cfstrings)
3501 const char *s = TREE_STRING_POINTER (str);
3502 int l = 0;
3504 for (l = 0; l < length; l++)
3505 if (!s[l] || !isascii (s[l]))
3507 warning (darwin_warn_nonportable_cfstrings, "%s in CFString literal",
3508 s[l] ? "non-ASCII character" : "embedded NUL");
3509 break;
3513 *loc = desc = ggc_cleared_alloc<cfstring_descriptor> ();
3514 desc->literal = str;
3516 /* isa *. */
3517 field = TYPE_FIELDS (ccfstring_type_node);
3518 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3519 build1 (ADDR_EXPR, TREE_TYPE (field),
3520 cfstring_class_reference));
3521 /* flags */
3522 field = DECL_CHAIN (field);
3523 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3524 build_int_cst (TREE_TYPE (field), 0x000007c8));
3525 /* string *. */
3526 field = DECL_CHAIN (field);
3527 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3528 build1 (ADDR_EXPR, TREE_TYPE (field), str));
3529 /* length */
3530 field = DECL_CHAIN (field);
3531 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3532 build_int_cst (TREE_TYPE (field), length));
3534 constructor = build_constructor (ccfstring_type_node, v);
3535 TREE_READONLY (constructor) = 1;
3536 TREE_CONSTANT (constructor) = 1;
3537 TREE_STATIC (constructor) = 1;
3539 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
3540 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
3541 being built without any knowledge of C++ tree accessors; hence, we shall
3542 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
3543 if (darwin_running_cxx)
3544 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
3546 /* Create an anonymous global variable for this CFString. */
3547 var = build_decl (input_location, CONST_DECL,
3548 NULL, TREE_TYPE (constructor));
3549 DECL_ARTIFICIAL (var) = 1;
3550 TREE_STATIC (var) = 1;
3551 DECL_INITIAL (var) = constructor;
3552 /* FIXME: This should use a translation_unit_decl to indicate file scope. */
3553 DECL_CONTEXT (var) = NULL_TREE;
3554 desc->constructor = var;
3557 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
3558 TREE_CONSTANT (addr) = 1;
3560 return addr;
3563 bool
3564 darwin_cfstring_p (tree str)
3566 struct cfstring_descriptor key;
3568 if (!str)
3569 return false;
3571 STRIP_NOPS (str);
3573 if (TREE_CODE (str) == ADDR_EXPR)
3574 str = TREE_OPERAND (str, 0);
3576 if (TREE_CODE (str) != STRING_CST)
3577 return false;
3579 key.literal = str;
3580 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, NO_INSERT);
3582 if (loc)
3583 return true;
3585 return false;
3588 void
3589 darwin_enter_string_into_cfstring_table (tree str)
3591 struct cfstring_descriptor key;
3593 key.literal = str;
3594 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
3596 if (!*loc)
3598 *loc = ggc_cleared_alloc<cfstring_descriptor> ();
3599 ((struct cfstring_descriptor *)*loc)->literal = str;
3603 /* Choose named function section based on its frequency. */
3605 section *
3606 darwin_function_section (tree decl, enum node_frequency freq,
3607 bool startup, bool exit)
3609 /* Decide if we need to put this in a coalescable section. */
3610 bool weak = (decl
3611 && DECL_WEAK (decl)
3612 && (!DECL_ATTRIBUTES (decl)
3613 || !lookup_attribute ("weak_import",
3614 DECL_ATTRIBUTES (decl))));
3616 /* If there is a specified section name, we should not be trying to
3617 override. */
3618 if (decl && DECL_SECTION_NAME (decl) != NULL)
3619 return get_named_section (decl, NULL, 0);
3621 /* We always put unlikely executed stuff in the cold section. */
3622 if (freq == NODE_FREQUENCY_UNLIKELY_EXECUTED)
3623 return (weak) ? darwin_sections[text_cold_coal_section]
3624 : darwin_sections[text_cold_section];
3626 /* If we have LTO *and* feedback information, then let LTO handle
3627 the function ordering, it makes a better job (for normal, hot,
3628 startup and exit - hence the bailout for cold above). */
3629 if (in_lto_p && flag_profile_values)
3630 goto default_function_sections;
3632 /* Non-cold startup code should go to startup subsection. */
3633 if (startup)
3634 return (weak) ? darwin_sections[text_startup_coal_section]
3635 : darwin_sections[text_startup_section];
3637 /* Similarly for exit. */
3638 if (exit)
3639 return (weak) ? darwin_sections[text_exit_coal_section]
3640 : darwin_sections[text_exit_section];
3642 /* Place hot code. */
3643 if (freq == NODE_FREQUENCY_HOT)
3644 return (weak) ? darwin_sections[text_hot_coal_section]
3645 : darwin_sections[text_hot_section];
3647 /* Otherwise, default to the 'normal' non-reordered sections. */
3648 default_function_sections:
3649 return (weak) ? darwin_sections[text_coal_section]
3650 : text_section;
3653 /* When a function is partitioned between sections, we need to insert a label
3654 at the start of each new chunk - so that it may become a valid 'atom' for
3655 eh and debug purposes. Without this the linker will emit warnings if one
3656 tries to add line location information (since the switched fragment will
3657 be anonymous). */
3659 void
3660 darwin_function_switched_text_sections (FILE *fp, tree decl, bool new_is_cold)
3662 char buf[128];
3663 snprintf (buf, 128, "%s%s",new_is_cold?"__cold_sect_of_":"__hot_sect_of_",
3664 IDENTIFIER_POINTER (DECL_NAME (decl)));
3665 /* Make sure we pick up all the relevant quotes etc. */
3666 assemble_name_raw (fp, (const char *) buf);
3667 fputs (":\n", fp);
3670 #include "gt-darwin.h"