[AArch64] [Neon types 2/10] Update Current type attributes to new Neon Types.
[official-gcc.git] / gcc / config / darwin.c
blobcb1bc38a3be598db33e3c06fc59db481ff4a8504
1 /* Functions for generic Darwin as target machine for GNU C compiler.
2 Copyright (C) 1989-2013 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 "expr.h"
36 #include "reload.h"
37 #include "function.h"
38 #include "ggc.h"
39 #include "langhooks.h"
40 #include "target.h"
41 #include "tm_p.h"
42 #include "diagnostic-core.h"
43 #include "toplev.h"
44 #include "hashtab.h"
45 #include "df.h"
46 #include "debug.h"
47 #include "obstack.h"
48 #include "lto-streamer.h"
50 /* Darwin supports a feature called fix-and-continue, which is used
51 for rapid turn around debugging. When code is compiled with the
52 -mfix-and-continue flag, two changes are made to the generated code
53 that allow the system to do things that it would normally not be
54 able to do easily. These changes allow gdb to load in
55 recompilation of a translation unit that has been changed into a
56 running program and replace existing functions and methods of that
57 translation unit with versions of those functions and methods
58 from the newly compiled translation unit. The new functions access
59 the existing static symbols from the old translation unit, if the
60 symbol existed in the unit to be replaced, and from the new
61 translation unit, otherwise.
63 The changes are to insert 5 nops at the beginning of all functions
64 and to use indirection to get at static symbols. The 5 nops
65 are required by consumers of the generated code. Currently, gdb
66 uses this to patch in a jump to the overriding function, this
67 allows all uses of the old name to forward to the replacement,
68 including existing function pointers and virtual methods. See
69 rs6000_emit_prologue for the code that handles the nop insertions.
71 The added indirection allows gdb to redirect accesses to static
72 symbols from the newly loaded translation unit to the existing
73 symbol, if any. @code{static} symbols are special and are handled by
74 setting the second word in the .non_lazy_symbol_pointer data
75 structure to symbol. See indirect_data for the code that handles
76 the extra indirection, and machopic_output_indirection and its use
77 of MACHO_SYMBOL_STATIC for the code that handles @code{static}
78 symbol indirection. */
80 /* For darwin >= 9 (OSX 10.5) the linker is capable of making the necessary
81 branch islands and we no longer need to emit darwin stubs.
82 However, if we are generating code for earlier systems (or for use in the
83 kernel) the stubs might still be required, and this will be set true. */
84 int darwin_emit_branch_islands = false;
86 typedef struct GTY(()) cdtor_record {
87 rtx symbol;
88 int priority; /* [con/de]structor priority */
89 int position; /* original position */
90 } cdtor_record;
92 static GTY(()) vec<cdtor_record, va_gc> *ctors = NULL;
93 static GTY(()) vec<cdtor_record, va_gc> *dtors = NULL;
95 /* A flag to determine whether we are running c++ or obj-c++. This has to be
96 settable from non-c-family contexts too (i.e. we can't use the c_dialect_
97 functions). */
98 int darwin_running_cxx;
100 /* Some code-gen now depends on OS major version numbers (at least). */
101 int generating_for_darwin_version ;
103 /* Section names. */
104 section * darwin_sections[NUM_DARWIN_SECTIONS];
106 /* While we transition to using in-tests instead of ifdef'd code. */
107 #ifndef HAVE_lo_sum
108 #define HAVE_lo_sum 0
109 #define gen_macho_high(a,b) (a)
110 #define gen_macho_low(a,b,c) (a)
111 #endif
113 /* True if we're setting __attribute__ ((ms_struct)). */
114 int darwin_ms_struct = false;
116 /* Earlier versions of Darwin as do not recognize an alignment field in
117 .comm directives, this should be set for versions that allow it. */
118 int emit_aligned_common = false;
120 /* A get_unnamed_section callback used to switch to an ObjC section.
121 DIRECTIVE is as for output_section_asm_op. */
123 static void
124 output_objc_section_asm_op (const void *directive)
126 static bool been_here = false;
128 /* The NeXT ObjC Runtime requires these sections to be present and in
129 order in the object. The code below implements this by emitting
130 a section header for each ObjC section the first time that an ObjC
131 section is requested. */
132 if (! been_here)
134 section *saved_in_section = in_section;
135 static const enum darwin_section_enum tomark[] =
137 /* written, cold -> hot */
138 objc_cat_cls_meth_section,
139 objc_cat_inst_meth_section,
140 objc_string_object_section,
141 objc_constant_string_object_section,
142 objc_selector_refs_section,
143 objc_selector_fixup_section,
144 objc_cls_refs_section,
145 objc_class_section,
146 objc_meta_class_section,
147 /* shared, hot -> cold */
148 objc_cls_meth_section,
149 objc_inst_meth_section,
150 objc_protocol_section,
151 objc_class_names_section,
152 objc_meth_var_types_section,
153 objc_meth_var_names_section,
154 objc_category_section,
155 objc_class_vars_section,
156 objc_instance_vars_section,
157 objc_module_info_section,
158 objc_symbols_section,
160 /* ABI=1 */
161 static const enum darwin_section_enum tomarkv1[] =
163 objc1_protocol_ext_section,
164 objc1_class_ext_section,
165 objc1_prop_list_section
167 /* ABI=2 */
168 static const enum darwin_section_enum tomarkv2[] =
170 objc2_message_refs_section,
171 objc2_classdefs_section,
172 objc2_metadata_section,
173 objc2_classrefs_section,
174 objc2_classlist_section,
175 objc2_categorylist_section,
176 objc2_selector_refs_section,
177 objc2_nonlazy_class_section,
178 objc2_nonlazy_category_section,
179 objc2_protocollist_section,
180 objc2_protocolrefs_section,
181 objc2_super_classrefs_section,
182 objc2_image_info_section,
183 objc2_constant_string_object_section
185 size_t i;
187 been_here = true;
188 if (flag_objc_abi < 2)
190 for (i = 0; i < ARRAY_SIZE (tomark); i++)
191 switch_to_section (darwin_sections[tomark[i]]);
192 if (flag_objc_abi == 1)
193 for (i = 0; i < ARRAY_SIZE (tomarkv1); i++)
194 switch_to_section (darwin_sections[tomarkv1[i]]);
196 else
197 for (i = 0; i < ARRAY_SIZE (tomarkv2); i++)
198 switch_to_section (darwin_sections[tomarkv2[i]]);
199 /* Make sure we don't get varasm.c out of sync with us. */
200 switch_to_section (saved_in_section);
202 output_section_asm_op (directive);
206 /* Private flag applied to disable section-anchors in a particular section. */
207 #define SECTION_NO_ANCHOR SECTION_MACH_DEP
210 /* Implement TARGET_ASM_INIT_SECTIONS. */
212 void
213 darwin_init_sections (void)
215 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
216 darwin_sections[NAME] = \
217 get_unnamed_section (FLAGS, (OBJC \
218 ? output_objc_section_asm_op \
219 : output_section_asm_op), \
220 "\t" DIRECTIVE);
221 #include "config/darwin-sections.def"
222 #undef DEF_SECTION
224 readonly_data_section = darwin_sections[const_section];
225 exception_section = darwin_sections[darwin_exception_section];
226 eh_frame_section = darwin_sections[darwin_eh_frame_section];
230 name_needs_quotes (const char *name)
232 int c;
233 while ((c = *name++) != '\0')
234 if (! ISIDNUM (c)
235 && c != '.' && c != '$' && c != '_' )
236 return 1;
237 return 0;
240 /* Return true if SYM_REF can be used without an indirection. */
242 machopic_symbol_defined_p (rtx sym_ref)
244 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
245 return true;
247 /* If a symbol references local and is not an extern to this
248 file, then the symbol might be able to declared as defined. */
249 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
251 /* If the symbol references a variable and the variable is a
252 common symbol, then this symbol is not defined. */
253 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
255 tree decl = SYMBOL_REF_DECL (sym_ref);
256 if (!decl)
257 return true;
258 if (DECL_COMMON (decl))
259 return false;
261 return true;
263 return false;
266 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
267 reference, which will not be changed. */
269 enum machopic_addr_class
270 machopic_classify_symbol (rtx sym_ref)
272 bool function_p;
274 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
275 if (machopic_symbol_defined_p (sym_ref))
276 return (function_p
277 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
278 else
279 return (function_p
280 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
283 #ifndef TARGET_FIX_AND_CONTINUE
284 #define TARGET_FIX_AND_CONTINUE 0
285 #endif
287 /* Indicate when fix-and-continue style code generation is being used
288 and when a reference to data should be indirected so that it can be
289 rebound in a new translation unit to reference the original instance
290 of that data. Symbol names that are for code generation local to
291 the translation unit are bound to the new translation unit;
292 currently this means symbols that begin with L or _OBJC_;
293 otherwise, we indicate that an indirect reference should be made to
294 permit the runtime to rebind new instances of the translation unit
295 to the original instance of the data. */
297 static int
298 indirect_data (rtx sym_ref)
300 int lprefix;
301 const char *name;
303 /* If we aren't generating fix-and-continue code, don't do anything
304 special. */
305 if (TARGET_FIX_AND_CONTINUE == 0)
306 return 0;
308 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
309 are indirected. Symbols that begin with L and _OBJC_ are always
310 bound to the current translation unit as they are used for
311 generated local data of the translation unit. */
313 name = XSTR (sym_ref, 0);
315 lprefix = (((name[0] == '*' || name[0] == '&')
316 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
317 || (strncmp (name, "_OBJC_", 6) == 0));
319 return ! lprefix;
322 static int
323 machopic_data_defined_p (rtx sym_ref)
325 if (indirect_data (sym_ref))
326 return 0;
328 switch (machopic_classify_symbol (sym_ref))
330 case MACHOPIC_DEFINED_DATA:
331 case MACHOPIC_DEFINED_FUNCTION:
332 return 1;
333 default:
334 return 0;
338 void
339 machopic_define_symbol (rtx mem)
341 rtx sym_ref;
343 gcc_assert (GET_CODE (mem) == MEM);
344 sym_ref = XEXP (mem, 0);
345 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
348 /* Return either ORIG or:
350 (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
352 depending on MACHO_DYNAMIC_NO_PIC_P. */
354 machopic_gen_offset (rtx orig)
356 if (MACHO_DYNAMIC_NO_PIC_P)
357 return orig;
358 else
360 /* Play games to avoid marking the function as needing pic if we
361 are being called as part of the cost-estimation process. */
362 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
363 crtl->uses_pic_offset_table = 1;
364 orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
365 UNSPEC_MACHOPIC_OFFSET);
366 return gen_rtx_CONST (Pmode, orig);
370 static GTY(()) const char * function_base_func_name;
371 static GTY(()) int current_pic_label_num;
372 static GTY(()) int emitted_pic_label_num;
374 static void
375 update_pic_label_number_if_needed (void)
377 const char *current_name;
379 /* When we are generating _get_pc thunks within stubs, there is no current
380 function. */
381 if (current_function_decl)
383 current_name =
384 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
385 if (function_base_func_name != current_name)
387 ++current_pic_label_num;
388 function_base_func_name = current_name;
391 else
393 ++current_pic_label_num;
394 function_base_func_name = "L_machopic_stub_dummy";
398 void
399 machopic_output_function_base_name (FILE *file)
401 /* If dynamic-no-pic is on, we should not get here. */
402 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
404 update_pic_label_number_if_needed ();
405 fprintf (file, "L%d$pb", current_pic_label_num);
408 char curr_picbasename[32];
410 const char *
411 machopic_get_function_picbase (void)
413 /* If dynamic-no-pic is on, we should not get here. */
414 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
416 update_pic_label_number_if_needed ();
417 snprintf (curr_picbasename, 32, "L%d$pb", current_pic_label_num);
418 return (const char *) curr_picbasename;
421 bool
422 machopic_should_output_picbase_label (void)
424 update_pic_label_number_if_needed ();
426 if (current_pic_label_num == emitted_pic_label_num)
427 return false;
429 emitted_pic_label_num = current_pic_label_num;
430 return true;
433 /* The suffix attached to non-lazy pointer symbols. */
434 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
435 /* The suffix attached to stub symbols. */
436 #define STUB_SUFFIX "$stub"
438 typedef struct GTY (()) machopic_indirection
440 /* The SYMBOL_REF for the entity referenced. */
441 rtx symbol;
442 /* The name of the stub or non-lazy pointer. */
443 const char * ptr_name;
444 /* True iff this entry is for a stub (as opposed to a non-lazy
445 pointer). */
446 bool stub_p;
447 /* True iff this stub or pointer pointer has been referenced. */
448 bool used;
449 } machopic_indirection;
451 /* A table mapping stub names and non-lazy pointer names to
452 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
454 static GTY ((param_is (struct machopic_indirection))) htab_t
455 machopic_indirections;
457 /* Return a hash value for a SLOT in the indirections hash table. */
459 static hashval_t
460 machopic_indirection_hash (const void *slot)
462 const machopic_indirection *p = (const machopic_indirection *) slot;
463 return htab_hash_string (p->ptr_name);
466 /* Returns true if the KEY is the same as that associated with
467 SLOT. */
469 static int
470 machopic_indirection_eq (const void *slot, const void *key)
472 return strcmp (((const machopic_indirection *) slot)->ptr_name,
473 (const char *) key) == 0;
476 /* Return the name of the non-lazy pointer (if STUB_P is false) or
477 stub (if STUB_B is true) corresponding to the given name. */
479 const char *
480 machopic_indirection_name (rtx sym_ref, bool stub_p)
482 char *buffer;
483 const char *name = XSTR (sym_ref, 0);
484 size_t namelen = strlen (name);
485 machopic_indirection *p;
486 void ** slot;
487 bool needs_quotes;
488 const char *suffix;
489 const char *prefix = user_label_prefix;
490 const char *quote = "";
491 tree id;
493 id = maybe_get_identifier (name);
494 if (id)
496 tree id_orig = id;
498 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
499 id = TREE_CHAIN (id);
500 if (id != id_orig)
502 name = IDENTIFIER_POINTER (id);
503 namelen = strlen (name);
507 if (name[0] == '*')
509 prefix = "";
510 ++name;
511 --namelen;
514 needs_quotes = name_needs_quotes (name);
515 if (needs_quotes)
517 quote = "\"";
520 if (stub_p)
521 suffix = STUB_SUFFIX;
522 else
523 suffix = NON_LAZY_POINTER_SUFFIX;
525 buffer = XALLOCAVEC (char, strlen ("&L")
526 + strlen (prefix)
527 + namelen
528 + strlen (suffix)
529 + 2 * strlen (quote)
530 + 1 /* '\0' */);
532 /* Construct the name of the non-lazy pointer or stub. */
533 sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
535 if (!machopic_indirections)
536 machopic_indirections = htab_create_ggc (37,
537 machopic_indirection_hash,
538 machopic_indirection_eq,
539 /*htab_del=*/NULL);
541 slot = htab_find_slot_with_hash (machopic_indirections, buffer,
542 htab_hash_string (buffer), INSERT);
543 if (*slot)
545 p = (machopic_indirection *) *slot;
547 else
549 p = ggc_alloc_machopic_indirection ();
550 p->symbol = sym_ref;
551 p->ptr_name = xstrdup (buffer);
552 p->stub_p = stub_p;
553 p->used = false;
554 *slot = p;
557 return p->ptr_name;
560 /* Return the name of the stub for the mcount function. */
562 const char*
563 machopic_mcount_stub_name (void)
565 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
566 return machopic_indirection_name (symbol, /*stub_p=*/true);
569 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
570 or non-lazy pointer as used -- and mark the object to which the
571 pointer/stub refers as used as well, since the pointer/stub will
572 emit a reference to it. */
574 void
575 machopic_validate_stub_or_non_lazy_ptr (const char *name)
577 machopic_indirection *p;
579 p = ((machopic_indirection *)
580 (htab_find_with_hash (machopic_indirections, name,
581 htab_hash_string (name))));
582 if (p && ! p->used)
584 const char *real_name;
585 tree id;
587 p->used = true;
589 /* Do what output_addr_const will do when we actually call it. */
590 if (SYMBOL_REF_DECL (p->symbol))
591 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
593 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
595 id = maybe_get_identifier (real_name);
596 if (id)
597 mark_referenced (id);
601 /* Transform ORIG, which may be any data source, to the corresponding
602 source using indirections. */
605 machopic_indirect_data_reference (rtx orig, rtx reg)
607 rtx ptr_ref = orig;
609 if (! MACHOPIC_INDIRECT)
610 return orig;
612 if (GET_CODE (orig) == SYMBOL_REF)
614 int defined = machopic_data_defined_p (orig);
616 if (defined && MACHO_DYNAMIC_NO_PIC_P)
618 if (DARWIN_PPC)
620 /* Create a new register for CSE opportunities. */
621 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
622 emit_insn (gen_macho_high (hi_reg, orig));
623 emit_insn (gen_macho_low (reg, hi_reg, orig));
624 return reg;
626 else if (DARWIN_X86)
627 return orig;
628 else
629 /* some other cpu -- writeme! */
630 gcc_unreachable ();
632 else if (defined)
634 rtx offset = NULL;
635 if (DARWIN_PPC || HAVE_lo_sum)
636 offset = machopic_gen_offset (orig);
638 if (DARWIN_PPC)
640 rtx hi_sum_reg = (!can_create_pseudo_p ()
641 ? reg
642 : gen_reg_rtx (Pmode));
644 gcc_assert (reg);
646 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
647 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
648 gen_rtx_HIGH (Pmode, offset))));
649 emit_insn (gen_rtx_SET (Pmode, reg,
650 gen_rtx_LO_SUM (Pmode, hi_sum_reg,
651 copy_rtx (offset))));
653 orig = reg;
655 else if (HAVE_lo_sum)
657 gcc_assert (reg);
659 emit_insn (gen_rtx_SET (VOIDmode, reg,
660 gen_rtx_HIGH (Pmode, offset)));
661 emit_insn (gen_rtx_SET (VOIDmode, reg,
662 gen_rtx_LO_SUM (Pmode, reg,
663 copy_rtx (offset))));
664 emit_use (pic_offset_table_rtx);
666 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
668 return orig;
671 ptr_ref = (gen_rtx_SYMBOL_REF
672 (Pmode,
673 machopic_indirection_name (orig, /*stub_p=*/false)));
675 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
677 ptr_ref = gen_const_mem (Pmode, ptr_ref);
678 machopic_define_symbol (ptr_ref);
680 if (DARWIN_X86
681 && reg
682 && MACHO_DYNAMIC_NO_PIC_P)
684 emit_insn (gen_rtx_SET (Pmode, reg, ptr_ref));
685 ptr_ref = reg;
688 return ptr_ref;
690 else if (GET_CODE (orig) == CONST)
692 /* If "(const (plus ...", walk the PLUS and return that result.
693 PLUS processing (below) will restore the "(const ..." if
694 appropriate. */
695 if (GET_CODE (XEXP (orig, 0)) == PLUS)
696 return machopic_indirect_data_reference (XEXP (orig, 0), reg);
697 else
698 return orig;
700 else if (GET_CODE (orig) == MEM)
702 XEXP (ptr_ref, 0) =
703 machopic_indirect_data_reference (XEXP (orig, 0), reg);
704 return ptr_ref;
706 else if (GET_CODE (orig) == PLUS)
708 rtx base, result;
709 /* When the target is i386, this code prevents crashes due to the
710 compiler's ignorance on how to move the PIC base register to
711 other registers. (The reload phase sometimes introduces such
712 insns.) */
713 if (GET_CODE (XEXP (orig, 0)) == REG
714 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
715 /* Prevent the same register from being erroneously used
716 as both the base and index registers. */
717 && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST))
718 && reg)
720 emit_move_insn (reg, XEXP (orig, 0));
721 XEXP (ptr_ref, 0) = reg;
722 return ptr_ref;
725 /* Legitimize both operands of the PLUS. */
726 base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
727 orig = machopic_indirect_data_reference (XEXP (orig, 1),
728 (base == reg ? 0 : reg));
729 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
730 result = plus_constant (Pmode, base, INTVAL (orig));
731 else
732 result = gen_rtx_PLUS (Pmode, base, orig);
734 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
736 if (reg)
738 emit_move_insn (reg, result);
739 result = reg;
741 else
743 result = force_reg (GET_MODE (result), result);
747 return result;
749 return ptr_ref;
752 /* Transform TARGET (a MEM), which is a function call target, to the
753 corresponding symbol_stub if necessary. Return a new MEM. */
756 machopic_indirect_call_target (rtx target)
758 if (! darwin_emit_branch_islands)
759 return target;
761 if (GET_CODE (target) != MEM)
762 return target;
764 if (MACHOPIC_INDIRECT
765 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
766 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
767 & MACHO_SYMBOL_FLAG_DEFINED))
769 rtx sym_ref = XEXP (target, 0);
770 const char *stub_name = machopic_indirection_name (sym_ref,
771 /*stub_p=*/true);
772 enum machine_mode mode = GET_MODE (sym_ref);
774 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
775 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
776 MEM_READONLY_P (target) = 1;
777 MEM_NOTRAP_P (target) = 1;
780 return target;
784 machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
786 rtx pic_ref = orig;
788 if (! MACHOPIC_INDIRECT)
789 return orig;
791 /* First handle a simple SYMBOL_REF or LABEL_REF */
792 if (GET_CODE (orig) == LABEL_REF
793 || (GET_CODE (orig) == SYMBOL_REF
796 /* addr(foo) = &func+(foo-func) */
797 orig = machopic_indirect_data_reference (orig, reg);
799 if (GET_CODE (orig) == PLUS
800 && GET_CODE (XEXP (orig, 0)) == REG)
802 if (reg == 0)
803 return force_reg (mode, orig);
805 emit_move_insn (reg, orig);
806 return reg;
809 if (GET_CODE (orig) == MEM)
811 if (reg == 0)
813 gcc_assert (!reload_in_progress);
814 reg = gen_reg_rtx (Pmode);
817 #if HAVE_lo_sum
818 if (MACHO_DYNAMIC_NO_PIC_P
819 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
820 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
822 #if defined (TARGET_TOC) /* ppc */
823 rtx temp_reg = (!can_create_pseudo_p ()
824 ? reg :
825 gen_reg_rtx (Pmode));
826 rtx asym = XEXP (orig, 0);
827 rtx mem;
829 emit_insn (gen_macho_high (temp_reg, asym));
830 mem = gen_const_mem (GET_MODE (orig),
831 gen_rtx_LO_SUM (Pmode, temp_reg,
832 copy_rtx (asym)));
833 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
834 #else
835 /* Some other CPU -- WriteMe! but right now there are no other
836 platforms that can use dynamic-no-pic */
837 gcc_unreachable ();
838 #endif
839 pic_ref = reg;
841 else
842 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
843 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
845 rtx offset = machopic_gen_offset (XEXP (orig, 0));
846 #if defined (TARGET_TOC) /* i.e., PowerPC */
847 /* Generating a new reg may expose opportunities for
848 common subexpression elimination. */
849 rtx hi_sum_reg = (!can_create_pseudo_p ()
850 ? reg
851 : gen_reg_rtx (Pmode));
852 rtx mem;
853 rtx insn;
854 rtx sum;
856 sum = gen_rtx_HIGH (Pmode, offset);
857 if (! MACHO_DYNAMIC_NO_PIC_P)
858 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
860 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
862 mem = gen_const_mem (GET_MODE (orig),
863 gen_rtx_LO_SUM (Pmode,
864 hi_sum_reg,
865 copy_rtx (offset)));
866 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
867 set_unique_reg_note (insn, REG_EQUAL, pic_ref);
869 pic_ref = reg;
870 #else
871 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
873 emit_insn (gen_rtx_SET (VOIDmode, reg,
874 gen_rtx_HIGH (Pmode,
875 gen_rtx_CONST (Pmode,
876 offset))));
877 emit_insn (gen_rtx_SET (VOIDmode, reg,
878 gen_rtx_LO_SUM (Pmode, reg,
879 gen_rtx_CONST (Pmode,
880 copy_rtx (offset)))));
881 pic_ref = gen_rtx_PLUS (Pmode,
882 pic_offset_table_rtx, reg);
883 #endif
885 else
886 #endif /* HAVE_lo_sum */
888 rtx pic = pic_offset_table_rtx;
889 if (GET_CODE (pic) != REG)
891 emit_move_insn (reg, pic);
892 pic = reg;
894 #if 0
895 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
896 #endif
898 if (reload_in_progress)
899 df_set_regs_ever_live (REGNO (pic), true);
900 pic_ref = gen_rtx_PLUS (Pmode, pic,
901 machopic_gen_offset (XEXP (orig, 0)));
904 #if !defined (TARGET_TOC)
905 emit_move_insn (reg, pic_ref);
906 pic_ref = gen_const_mem (GET_MODE (orig), reg);
907 #endif
909 else
912 #if HAVE_lo_sum
913 if (GET_CODE (orig) == SYMBOL_REF
914 || GET_CODE (orig) == LABEL_REF)
916 rtx offset = machopic_gen_offset (orig);
917 #if defined (TARGET_TOC) /* i.e., PowerPC */
918 rtx hi_sum_reg;
920 if (reg == 0)
922 gcc_assert (!reload_in_progress);
923 reg = gen_reg_rtx (Pmode);
926 hi_sum_reg = reg;
928 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
929 (MACHO_DYNAMIC_NO_PIC_P)
930 ? gen_rtx_HIGH (Pmode, offset)
931 : gen_rtx_PLUS (Pmode,
932 pic_offset_table_rtx,
933 gen_rtx_HIGH (Pmode,
934 offset))));
935 emit_insn (gen_rtx_SET (VOIDmode, reg,
936 gen_rtx_LO_SUM (Pmode,
937 hi_sum_reg,
938 copy_rtx (offset))));
939 pic_ref = reg;
940 #else
941 emit_insn (gen_rtx_SET (VOIDmode, reg,
942 gen_rtx_HIGH (Pmode, offset)));
943 emit_insn (gen_rtx_SET (VOIDmode, reg,
944 gen_rtx_LO_SUM (Pmode, reg,
945 copy_rtx (offset))));
946 pic_ref = gen_rtx_PLUS (Pmode,
947 pic_offset_table_rtx, reg);
948 #endif
950 else
951 #endif /* HAVE_lo_sum */
953 if (REG_P (orig)
954 || GET_CODE (orig) == SUBREG)
956 return orig;
958 else
960 rtx pic = pic_offset_table_rtx;
961 if (GET_CODE (pic) != REG)
963 emit_move_insn (reg, pic);
964 pic = reg;
966 #if 0
967 emit_use (pic_offset_table_rtx);
968 #endif
969 if (reload_in_progress)
970 df_set_regs_ever_live (REGNO (pic), true);
971 pic_ref = gen_rtx_PLUS (Pmode,
972 pic,
973 machopic_gen_offset (orig));
978 if (GET_CODE (pic_ref) != REG)
980 if (reg != 0)
982 emit_move_insn (reg, pic_ref);
983 return reg;
985 else
987 return force_reg (mode, pic_ref);
990 else
992 return pic_ref;
996 else if (GET_CODE (orig) == SYMBOL_REF)
997 return orig;
999 else if (GET_CODE (orig) == PLUS
1000 && (GET_CODE (XEXP (orig, 0)) == MEM
1001 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
1002 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
1003 && XEXP (orig, 0) != pic_offset_table_rtx
1004 && GET_CODE (XEXP (orig, 1)) != REG)
1007 rtx base;
1008 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
1010 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1011 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
1012 Pmode, (base == reg ? 0 : reg));
1013 if (GET_CODE (orig) == CONST_INT)
1015 pic_ref = plus_constant (Pmode, base, INTVAL (orig));
1016 is_complex = 1;
1018 else
1019 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
1021 if (reg && is_complex)
1023 emit_move_insn (reg, pic_ref);
1024 pic_ref = reg;
1026 /* Likewise, should we set special REG_NOTEs here? */
1029 else if (GET_CODE (orig) == CONST)
1031 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1034 else if (GET_CODE (orig) == MEM
1035 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
1037 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1038 addr = replace_equiv_address (orig, addr);
1039 emit_move_insn (reg, addr);
1040 pic_ref = reg;
1043 return pic_ref;
1046 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
1047 DATA is the FILE* for assembly output. Called from
1048 htab_traverse. */
1050 static int
1051 machopic_output_indirection (void **slot, void *data)
1053 machopic_indirection *p = *((machopic_indirection **) slot);
1054 FILE *asm_out_file = (FILE *) data;
1055 rtx symbol;
1056 const char *sym_name;
1057 const char *ptr_name;
1059 if (!p->used)
1060 return 1;
1062 symbol = p->symbol;
1063 sym_name = XSTR (symbol, 0);
1064 ptr_name = p->ptr_name;
1066 if (p->stub_p)
1068 char *sym;
1069 char *stub;
1070 tree id;
1072 id = maybe_get_identifier (sym_name);
1073 if (id)
1075 tree id_orig = id;
1077 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
1078 id = TREE_CHAIN (id);
1079 if (id != id_orig)
1080 sym_name = IDENTIFIER_POINTER (id);
1083 sym = XALLOCAVEC (char, strlen (sym_name) + 2);
1084 if (sym_name[0] == '*' || sym_name[0] == '&')
1085 strcpy (sym, sym_name + 1);
1086 else if (sym_name[0] == '-' || sym_name[0] == '+')
1087 strcpy (sym, sym_name);
1088 else
1089 sprintf (sym, "%s%s", user_label_prefix, sym_name);
1091 stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
1092 if (ptr_name[0] == '*' || ptr_name[0] == '&')
1093 strcpy (stub, ptr_name + 1);
1094 else
1095 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
1097 machopic_output_stub (asm_out_file, sym, stub);
1099 else if (! indirect_data (symbol)
1100 && (machopic_symbol_defined_p (symbol)
1101 || SYMBOL_REF_LOCAL_P (symbol)))
1103 switch_to_section (data_section);
1104 assemble_align (GET_MODE_ALIGNMENT (Pmode));
1105 assemble_label (asm_out_file, ptr_name);
1106 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
1107 GET_MODE_SIZE (Pmode),
1108 GET_MODE_ALIGNMENT (Pmode), 1);
1110 else
1112 rtx init = const0_rtx;
1114 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
1116 /* Mach-O symbols are passed around in code through indirect
1117 references and the original symbol_ref hasn't passed through
1118 the generic handling and reference-catching in
1119 output_operand, so we need to manually mark weak references
1120 as such. */
1121 if (SYMBOL_REF_WEAK (symbol))
1123 tree decl = SYMBOL_REF_DECL (symbol);
1124 gcc_assert (DECL_P (decl));
1126 if (decl != NULL_TREE
1127 && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
1128 /* Handle only actual external-only definitions, not
1129 e.g. extern inline code or variables for which
1130 storage has been allocated. */
1131 && !TREE_STATIC (decl))
1133 fputs ("\t.weak_reference ", asm_out_file);
1134 assemble_name (asm_out_file, sym_name);
1135 fputc ('\n', asm_out_file);
1139 assemble_name (asm_out_file, ptr_name);
1140 fprintf (asm_out_file, ":\n");
1142 fprintf (asm_out_file, "\t.indirect_symbol ");
1143 assemble_name (asm_out_file, sym_name);
1144 fprintf (asm_out_file, "\n");
1146 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1147 have their symbol name instead of 0 in the second entry of
1148 the non-lazy symbol pointer data structure when they are
1149 defined. This allows the runtime to rebind newer instances
1150 of the translation unit with the original instance of the
1151 symbol. */
1153 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1154 && machopic_symbol_defined_p (symbol))
1155 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1157 assemble_integer (init, GET_MODE_SIZE (Pmode),
1158 GET_MODE_ALIGNMENT (Pmode), 1);
1161 return 1;
1164 void
1165 machopic_finish (FILE *asm_out_file)
1167 if (machopic_indirections)
1168 htab_traverse_noresize (machopic_indirections,
1169 machopic_output_indirection,
1170 asm_out_file);
1174 machopic_operand_p (rtx op)
1176 if (MACHOPIC_JUST_INDIRECT)
1177 return (GET_CODE (op) == SYMBOL_REF
1178 && machopic_symbol_defined_p (op));
1179 else
1180 return (GET_CODE (op) == CONST
1181 && GET_CODE (XEXP (op, 0)) == UNSPEC
1182 && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
1185 /* This function records whether a given name corresponds to a defined
1186 or undefined function or variable, for machopic_classify_ident to
1187 use later. */
1189 void
1190 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
1192 rtx sym_ref;
1194 /* Do the standard encoding things first. */
1195 default_encode_section_info (decl, rtl, first);
1197 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1198 return;
1200 sym_ref = XEXP (rtl, 0);
1201 if (TREE_CODE (decl) == VAR_DECL)
1202 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1204 if (!DECL_EXTERNAL (decl)
1205 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1206 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1207 && ((TREE_STATIC (decl)
1208 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1209 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1210 && DECL_INITIAL (decl) != error_mark_node)))
1211 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1213 if (! TREE_PUBLIC (decl))
1214 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
1217 void
1218 darwin_mark_decl_preserved (const char *name)
1220 fprintf (asm_out_file, "\t.no_dead_strip ");
1221 assemble_name (asm_out_file, name);
1222 fputc ('\n', asm_out_file);
1225 static section *
1226 darwin_rodata_section (int weak, bool zsize)
1228 return (weak
1229 ? darwin_sections[const_coal_section]
1230 : (zsize ? darwin_sections[zobj_const_section]
1231 : darwin_sections[const_section]));
1234 static section *
1235 darwin_mergeable_string_section (tree exp,
1236 unsigned HOST_WIDE_INT align)
1238 /* Darwin's ld expects to see non-writable string literals in the .cstring
1239 section. Later versions of ld check and complain when CFStrings are
1240 enabled. Therefore we shall force the strings into .cstring since we
1241 don't support writable ones anyway. */
1242 if ((darwin_constant_cfstrings || flag_merge_constants)
1243 && TREE_CODE (exp) == STRING_CST
1244 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1245 && align <= 256
1246 && (int_size_in_bytes (TREE_TYPE (exp))
1247 == TREE_STRING_LENGTH (exp))
1248 && ((size_t) TREE_STRING_LENGTH (exp)
1249 == strlen (TREE_STRING_POINTER (exp)) + 1))
1250 return darwin_sections[cstring_section];
1252 if (DARWIN_SECTION_ANCHORS && flag_section_anchors
1253 && TREE_CODE (exp) == STRING_CST
1254 && TREE_STRING_LENGTH (exp) == 0)
1255 return darwin_sections[zobj_const_section];
1257 return readonly_data_section;
1260 #ifndef HAVE_GAS_LITERAL16
1261 #define HAVE_GAS_LITERAL16 0
1262 #endif
1264 static section *
1265 darwin_mergeable_constant_section (tree exp,
1266 unsigned HOST_WIDE_INT align,
1267 bool zsize)
1269 enum machine_mode mode = DECL_MODE (exp);
1270 unsigned int modesize = GET_MODE_BITSIZE (mode);
1272 if (DARWIN_SECTION_ANCHORS
1273 && flag_section_anchors
1274 && zsize)
1275 return darwin_sections[zobj_const_section];
1277 if (flag_merge_constants
1278 && mode != VOIDmode
1279 && mode != BLKmode
1280 && modesize <= align
1281 && align >= 8
1282 && align <= 256
1283 && (align & (align -1)) == 0)
1285 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1287 if (TREE_CODE (size) == INTEGER_CST
1288 && TREE_INT_CST_LOW (size) == 4
1289 && TREE_INT_CST_HIGH (size) == 0)
1290 return darwin_sections[literal4_section];
1291 else if (TREE_CODE (size) == INTEGER_CST
1292 && TREE_INT_CST_LOW (size) == 8
1293 && TREE_INT_CST_HIGH (size) == 0)
1294 return darwin_sections[literal8_section];
1295 else if (HAVE_GAS_LITERAL16
1296 && TARGET_64BIT
1297 && TREE_CODE (size) == INTEGER_CST
1298 && TREE_INT_CST_LOW (size) == 16
1299 && TREE_INT_CST_HIGH (size) == 0)
1300 return darwin_sections[literal16_section];
1301 else
1302 return readonly_data_section;
1305 return readonly_data_section;
1308 section *
1309 darwin_tm_clone_table_section (void)
1311 return get_named_section (NULL,
1312 "__DATA,__tm_clone_table,regular,no_dead_strip",
1317 machopic_reloc_rw_mask (void)
1319 return MACHOPIC_INDIRECT ? 3 : 0;
1322 /* We have to deal with ObjC/C++ metadata section placement in the common
1323 code, since it will also be called from LTO.
1325 Return metadata attributes, if present (searching for ABI=2 first)
1326 Return NULL_TREE if no such attributes are found. */
1328 static tree
1329 is_objc_metadata (tree decl)
1331 if (DECL_P (decl)
1332 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1333 && DECL_ATTRIBUTES (decl))
1335 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1336 if (meta)
1337 return meta;
1338 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1339 if (meta)
1340 return meta;
1342 return NULL_TREE;
1345 static int classes_seen;
1346 static int objc_metadata_seen;
1348 /* Return the section required for Objective C ABI 2 metadata. */
1349 static section *
1350 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1352 const char *p;
1353 tree ident = TREE_VALUE (meta);
1354 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1355 p = IDENTIFIER_POINTER (ident);
1357 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi == 2);
1359 objc_metadata_seen = 1;
1361 if (base == data_section)
1362 base = darwin_sections[objc2_metadata_section];
1364 /* Most of the OBJC2 META-data end up in the base section, so check it
1365 first. */
1366 if (!strncmp (p, "V2_BASE", 7))
1367 return base;
1368 else if (!strncmp (p, "V2_STRG", 7))
1369 return darwin_sections[cstring_section];
1371 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
1372 return darwin_sections[objc2_classdefs_section];
1373 else if (!strncmp (p, "V2_MREF", 7))
1374 return darwin_sections[objc2_message_refs_section];
1375 else if (!strncmp (p, "V2_CLRF", 7))
1376 return darwin_sections[objc2_classrefs_section];
1377 else if (!strncmp (p, "V2_SURF", 7))
1378 return darwin_sections[objc2_super_classrefs_section];
1379 else if (!strncmp (p, "V2_NLCL", 7))
1380 return darwin_sections[objc2_nonlazy_class_section];
1381 else if (!strncmp (p, "V2_CLAB", 7))
1383 classes_seen = 1;
1384 return darwin_sections[objc2_classlist_section];
1386 else if (!strncmp (p, "V2_SRFS", 7))
1387 return darwin_sections[objc2_selector_refs_section];
1388 else if (!strncmp (p, "V2_NLCA", 7))
1389 return darwin_sections[objc2_nonlazy_category_section];
1390 else if (!strncmp (p, "V2_CALA", 7))
1391 return darwin_sections[objc2_categorylist_section];
1393 else if (!strncmp (p, "V2_PLST", 7))
1394 return darwin_sections[objc2_protocollist_section];
1395 else if (!strncmp (p, "V2_PRFS", 7))
1396 return darwin_sections[objc2_protocolrefs_section];
1398 else if (!strncmp (p, "V2_INFO", 7))
1399 return darwin_sections[objc2_image_info_section];
1401 else if (!strncmp (p, "V2_EHTY", 7))
1402 return darwin_sections[data_coal_section];
1404 else if (!strncmp (p, "V2_CSTR", 7))
1405 return darwin_sections[objc2_constant_string_object_section];
1407 /* Not recognized, default. */
1408 return base;
1411 /* Return the section required for Objective C ABI 0/1 metadata. */
1412 static section *
1413 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1415 const char *p;
1416 tree ident = TREE_VALUE (meta);
1417 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1418 p = IDENTIFIER_POINTER (ident);
1420 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi < 2);
1422 objc_metadata_seen = 1;
1424 /* String sections first, cos there are lots of strings. */
1425 if (!strncmp (p, "V1_STRG", 7))
1426 return darwin_sections[cstring_section];
1427 else if (!strncmp (p, "V1_CLSN", 7))
1428 return darwin_sections[objc_class_names_section];
1429 else if (!strncmp (p, "V1_METN", 7))
1430 return darwin_sections[objc_meth_var_names_section];
1431 else if (!strncmp (p, "V1_METT", 7))
1432 return darwin_sections[objc_meth_var_types_section];
1434 else if (!strncmp (p, "V1_CLAS", 7))
1436 classes_seen = 1;
1437 return darwin_sections[objc_class_section];
1439 else if (!strncmp (p, "V1_META", 7))
1440 return darwin_sections[objc_meta_class_section];
1441 else if (!strncmp (p, "V1_CATG", 7))
1442 return darwin_sections[objc_category_section];
1443 else if (!strncmp (p, "V1_PROT", 7))
1444 return darwin_sections[objc_protocol_section];
1446 else if (!strncmp (p, "V1_CLCV", 7))
1447 return darwin_sections[objc_class_vars_section];
1448 else if (!strncmp (p, "V1_CLIV", 7))
1449 return darwin_sections[objc_instance_vars_section];
1451 else if (!strncmp (p, "V1_CLCM", 7))
1452 return darwin_sections[objc_cls_meth_section];
1453 else if (!strncmp (p, "V1_CLIM", 7))
1454 return darwin_sections[objc_inst_meth_section];
1455 else if (!strncmp (p, "V1_CACM", 7))
1456 return darwin_sections[objc_cat_cls_meth_section];
1457 else if (!strncmp (p, "V1_CAIM", 7))
1458 return darwin_sections[objc_cat_inst_meth_section];
1459 else if (!strncmp (p, "V1_PNSM", 7))
1460 return darwin_sections[objc_cat_inst_meth_section];
1461 else if (!strncmp (p, "V1_PCLM", 7))
1462 return darwin_sections[objc_cat_cls_meth_section];
1464 else if (!strncmp (p, "V1_CLPR", 7))
1465 return darwin_sections[objc_cat_cls_meth_section];
1466 else if (!strncmp (p, "V1_CAPR", 7))
1467 return darwin_sections[objc_category_section]; /* ??? CHECK me. */
1469 else if (!strncmp (p, "V1_PRFS", 7))
1470 return darwin_sections[objc_cat_cls_meth_section];
1471 else if (!strncmp (p, "V1_CLRF", 7))
1472 return darwin_sections[objc_cls_refs_section];
1473 else if (!strncmp (p, "V1_SRFS", 7))
1474 return darwin_sections[objc_selector_refs_section];
1476 else if (!strncmp (p, "V1_MODU", 7))
1477 return darwin_sections[objc_module_info_section];
1478 else if (!strncmp (p, "V1_SYMT", 7))
1479 return darwin_sections[objc_symbols_section];
1480 else if (!strncmp (p, "V1_INFO", 7))
1481 return darwin_sections[objc_image_info_section];
1483 else if (!strncmp (p, "V1_PLST", 7))
1484 return darwin_sections[objc1_prop_list_section];
1485 else if (!strncmp (p, "V1_PEXT", 7))
1486 return darwin_sections[objc1_protocol_ext_section];
1487 else if (!strncmp (p, "V1_CEXT", 7))
1488 return darwin_sections[objc1_class_ext_section];
1490 else if (!strncmp (p, "V2_CSTR", 7))
1491 return darwin_sections[objc_constant_string_object_section];
1493 return base;
1496 section *
1497 machopic_select_section (tree decl,
1498 int reloc,
1499 unsigned HOST_WIDE_INT align)
1501 bool zsize, one, weak, ro;
1502 section *base_section = NULL;
1504 weak = (DECL_P (decl)
1505 && DECL_WEAK (decl)
1506 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
1508 zsize = (DECL_P (decl)
1509 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1510 && tree_low_cst (DECL_SIZE_UNIT (decl), 1) == 0);
1512 one = DECL_P (decl)
1513 && TREE_CODE (decl) == VAR_DECL
1514 && DECL_ONE_ONLY (decl);
1516 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
1518 switch (categorize_decl_for_section (decl, reloc))
1520 case SECCAT_TEXT:
1521 gcc_unreachable ();
1522 break;
1524 case SECCAT_RODATA:
1525 case SECCAT_SRODATA:
1526 base_section = darwin_rodata_section (weak, zsize);
1527 break;
1529 case SECCAT_RODATA_MERGE_STR:
1530 base_section = darwin_mergeable_string_section (decl, align);
1531 break;
1533 case SECCAT_RODATA_MERGE_STR_INIT:
1534 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1535 break;
1537 case SECCAT_RODATA_MERGE_CONST:
1538 base_section = darwin_mergeable_constant_section (decl, align, zsize);
1539 break;
1541 case SECCAT_DATA:
1542 case SECCAT_DATA_REL:
1543 case SECCAT_DATA_REL_LOCAL:
1544 case SECCAT_DATA_REL_RO:
1545 case SECCAT_DATA_REL_RO_LOCAL:
1546 case SECCAT_SDATA:
1547 case SECCAT_TDATA:
1548 if (weak || one)
1550 if (ro)
1551 base_section = darwin_sections[const_data_coal_section];
1552 else
1553 base_section = darwin_sections[data_coal_section];
1555 else if (DARWIN_SECTION_ANCHORS
1556 && flag_section_anchors
1557 && zsize)
1559 /* If we're doing section anchors, then punt zero-sized objects into
1560 their own sections so that they don't interfere with offset
1561 computation for the remaining vars. This does not need to be done
1562 for stuff in mergeable sections, since these are ineligible for
1563 anchors. */
1564 if (ro)
1565 base_section = darwin_sections[zobj_const_data_section];
1566 else
1567 base_section = darwin_sections[zobj_data_section];
1569 else if (ro)
1570 base_section = darwin_sections[const_data_section];
1571 else
1572 base_section = data_section;
1573 break;
1574 case SECCAT_BSS:
1575 case SECCAT_SBSS:
1576 case SECCAT_TBSS:
1577 if (weak || one)
1578 base_section = darwin_sections[data_coal_section];
1579 else
1581 if (!TREE_PUBLIC (decl))
1582 base_section = lcomm_section;
1583 else if (bss_noswitch_section)
1584 base_section = bss_noswitch_section;
1585 else
1586 base_section = data_section;
1588 break;
1590 default:
1591 gcc_unreachable ();
1594 /* Darwin weird special cases.
1595 a) OBJC Meta-data. */
1596 if (DECL_P (decl)
1597 && (TREE_CODE (decl) == VAR_DECL
1598 || TREE_CODE (decl) == CONST_DECL)
1599 && DECL_ATTRIBUTES (decl))
1601 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1602 if (meta)
1603 return darwin_objc2_section (decl, meta, base_section);
1604 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1605 if (meta)
1606 return darwin_objc1_section (decl, meta, base_section);
1607 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
1608 if (meta)
1609 return base_section; /* GNU runtime is happy with it all in one pot. */
1612 /* b) Constant string objects. */
1613 if (TREE_CODE (decl) == CONSTRUCTOR
1614 && TREE_TYPE (decl)
1615 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1616 && TYPE_NAME (TREE_TYPE (decl)))
1618 tree name = TYPE_NAME (TREE_TYPE (decl));
1619 if (TREE_CODE (name) == TYPE_DECL)
1620 name = DECL_NAME (name);
1622 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1624 if (flag_next_runtime)
1626 if (flag_objc_abi == 2)
1627 return darwin_sections[objc2_constant_string_object_section];
1628 else
1629 return darwin_sections[objc_constant_string_object_section];
1631 else
1632 return darwin_sections[objc_string_object_section];
1634 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
1635 return darwin_sections[cfstring_constant_object_section];
1636 else
1637 return base_section;
1639 /* c) legacy meta-data selection. */
1640 else if (TREE_CODE (decl) == VAR_DECL
1641 && DECL_NAME (decl)
1642 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1643 && IDENTIFIER_POINTER (DECL_NAME (decl))
1644 && flag_next_runtime
1645 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1647 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1648 static bool warned_objc_46 = false;
1649 /* We shall assert that zero-sized objects are an error in ObjC
1650 meta-data. */
1651 gcc_assert (tree_low_cst (DECL_SIZE_UNIT (decl), 1) != 0);
1653 /* ??? This mechanism for determining the metadata section is
1654 broken when LTO is in use, since the frontend that generated
1655 the data is not identified. We will keep the capability for
1656 the short term - in case any non-Objective-C programs are using
1657 it to place data in specified sections. */
1658 if (!warned_objc_46)
1660 location_t loc = DECL_SOURCE_LOCATION (decl);
1661 warning_at (loc, 0, "the use of _OBJC_-prefixed variable names"
1662 " to select meta-data sections is deprecated at 4.6"
1663 " and will be removed in 4.7");
1664 warned_objc_46 = true;
1667 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1668 return darwin_sections[objc_cls_meth_section];
1669 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1670 return darwin_sections[objc_inst_meth_section];
1671 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29))
1672 return darwin_sections[objc_cat_cls_meth_section];
1673 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32))
1674 return darwin_sections[objc_cat_inst_meth_section];
1675 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1676 return darwin_sections[objc_class_vars_section];
1677 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1678 return darwin_sections[objc_instance_vars_section];
1679 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1680 return darwin_sections[objc_cat_cls_meth_section];
1681 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1682 return darwin_sections[objc_class_names_section];
1683 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1684 return darwin_sections[objc_meth_var_names_section];
1685 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1686 return darwin_sections[objc_meth_var_types_section];
1687 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1688 return darwin_sections[objc_cls_refs_section];
1689 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1690 return darwin_sections[objc_class_section];
1691 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1692 return darwin_sections[objc_meta_class_section];
1693 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1694 return darwin_sections[objc_category_section];
1695 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1696 return darwin_sections[objc_selector_refs_section];
1697 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1698 return darwin_sections[objc_selector_fixup_section];
1699 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1700 return darwin_sections[objc_symbols_section];
1701 else if (!strncmp (name, "_OBJC_MODULES", 13))
1702 return darwin_sections[objc_module_info_section];
1703 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1704 return darwin_sections[objc_image_info_section];
1705 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1706 return darwin_sections[objc_cat_inst_meth_section];
1707 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1708 return darwin_sections[objc_cat_cls_meth_section];
1709 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1710 return darwin_sections[objc_cat_cls_meth_section];
1711 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1712 return darwin_sections[objc_protocol_section];
1713 else
1714 return base_section;
1717 return base_section;
1720 /* This can be called with address expressions as "rtx".
1721 They must go in "const". */
1723 section *
1724 machopic_select_rtx_section (enum machine_mode mode, rtx x,
1725 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1727 if (GET_MODE_SIZE (mode) == 8
1728 && (GET_CODE (x) == CONST_INT
1729 || GET_CODE (x) == CONST_DOUBLE))
1730 return darwin_sections[literal8_section];
1731 else if (GET_MODE_SIZE (mode) == 4
1732 && (GET_CODE (x) == CONST_INT
1733 || GET_CODE (x) == CONST_DOUBLE))
1734 return darwin_sections[literal4_section];
1735 else if (HAVE_GAS_LITERAL16
1736 && TARGET_64BIT
1737 && GET_MODE_SIZE (mode) == 16
1738 && (GET_CODE (x) == CONST_INT
1739 || GET_CODE (x) == CONST_DOUBLE
1740 || GET_CODE (x) == CONST_VECTOR))
1741 return darwin_sections[literal16_section];
1742 else if (MACHOPIC_INDIRECT
1743 && (GET_CODE (x) == SYMBOL_REF
1744 || GET_CODE (x) == CONST
1745 || GET_CODE (x) == LABEL_REF))
1746 return darwin_sections[const_data_section];
1747 else
1748 return darwin_sections[const_section];
1751 void
1752 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1754 cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)};
1756 vec_safe_push (ctors, new_elt);
1758 if (! MACHOPIC_INDIRECT)
1759 fprintf (asm_out_file, ".reference .constructors_used\n");
1762 void
1763 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1765 cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)};
1767 vec_safe_push (dtors, new_elt);
1769 if (! MACHOPIC_INDIRECT)
1770 fprintf (asm_out_file, ".reference .destructors_used\n");
1773 static int
1774 sort_cdtor_records (const void * a, const void * b)
1776 const cdtor_record *cda = (const cdtor_record *)a;
1777 const cdtor_record *cdb = (const cdtor_record *)b;
1778 if (cda->priority > cdb->priority)
1779 return 1;
1780 if (cda->priority < cdb->priority)
1781 return -1;
1782 if (cda->position > cdb->position)
1783 return 1;
1784 if (cda->position < cdb->position)
1785 return -1;
1786 return 0;
1789 static void
1790 finalize_ctors ()
1792 unsigned int i;
1793 cdtor_record *elt;
1795 if (MACHOPIC_INDIRECT)
1796 switch_to_section (darwin_sections[mod_init_section]);
1797 else
1798 switch_to_section (darwin_sections[constructor_section]);
1800 if (vec_safe_length (ctors) > 1)
1801 ctors->qsort (sort_cdtor_records);
1802 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
1804 assemble_align (POINTER_SIZE);
1805 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1809 static void
1810 finalize_dtors ()
1812 unsigned int i;
1813 cdtor_record *elt;
1815 if (MACHOPIC_INDIRECT)
1816 switch_to_section (darwin_sections[mod_term_section]);
1817 else
1818 switch_to_section (darwin_sections[destructor_section]);
1820 if (vec_safe_length (dtors) > 1)
1821 dtors->qsort (sort_cdtor_records);
1822 FOR_EACH_VEC_SAFE_ELT (dtors, i, elt)
1824 assemble_align (POINTER_SIZE);
1825 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1829 void
1830 darwin_globalize_label (FILE *stream, const char *name)
1832 if (!!strncmp (name, "_OBJC_", 6))
1833 default_globalize_label (stream, name);
1836 /* This routine returns non-zero if 'name' starts with the special objective-c
1837 anonymous file-scope static name. It accommodates c++'s mangling of such
1838 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
1840 int
1841 darwin_label_is_anonymous_local_objc_name (const char *name)
1843 const unsigned char *p = (const unsigned char *) name;
1844 if (*p != '_')
1845 return 0;
1846 if (p[1] == 'Z' && p[2] == 'L')
1848 p += 3;
1849 while (*p >= '0' && *p <= '9')
1850 p++;
1852 return (!strncmp ((const char *)p, "_OBJC_", 6));
1855 /* LTO support for Mach-O.
1857 This version uses three mach-o sections to encapsulate the (unlimited
1858 number of) lto sections.
1860 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
1861 __GNU_LTO, __section_names contains the GNU LTO section names.
1862 __GNU_LTO, __section_index contains an array of values that index these.
1864 Indexed thus:
1865 <section offset from the start of __GNU_LTO, __lto_sections>,
1866 <section length>
1867 <name offset from the start of __GNU_LTO, __section_names,
1868 <name length>.
1870 At present, for both m32 and m64 mach-o files each of these fields is
1871 represented by a uint32_t. This is because, AFAICT, a mach-o object
1872 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1874 uint32_t offset;
1875 "offset An integer specifying the offset to this section in the file." */
1877 /* Count lto section numbers. */
1878 static unsigned int lto_section_num = 0;
1880 /* A vector of information about LTO sections, at present, we only have
1881 the name. TODO: see if we can get the data length somehow. */
1882 typedef struct GTY (()) darwin_lto_section_e {
1883 const char *sectname;
1884 } darwin_lto_section_e ;
1886 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
1888 /* Segment for LTO data. */
1889 #define LTO_SEGMENT_NAME "__GNU_LTO"
1891 /* Section wrapper scheme (used here to wrap the unlimited number of LTO
1892 sections into three Mach-O ones).
1893 NOTE: These names MUST be kept in sync with those in
1894 libiberty/simple-object-mach-o. */
1895 #define LTO_SECTS_SECTION "__wrapper_sects"
1896 #define LTO_NAMES_SECTION "__wrapper_names"
1897 #define LTO_INDEX_SECTION "__wrapper_index"
1899 /* File to temporarily store LTO data. This is appended to asm_out_file
1900 in darwin_end_file. */
1901 static FILE *lto_asm_out_file, *saved_asm_out_file;
1902 static char *lto_asm_out_name;
1904 /* Prepare asm_out_file for LTO output. For darwin, this means hiding
1905 asm_out_file and switching to an alternative output file. */
1906 void
1907 darwin_asm_lto_start (void)
1909 gcc_assert (! saved_asm_out_file);
1910 saved_asm_out_file = asm_out_file;
1911 if (! lto_asm_out_name)
1912 lto_asm_out_name = make_temp_file (".lto.s");
1913 lto_asm_out_file = fopen (lto_asm_out_name, "a");
1914 if (lto_asm_out_file == NULL)
1915 fatal_error ("failed to open temporary file %s for LTO output",
1916 lto_asm_out_name);
1917 asm_out_file = lto_asm_out_file;
1920 /* Restore asm_out_file. */
1921 void
1922 darwin_asm_lto_end (void)
1924 gcc_assert (saved_asm_out_file);
1925 fclose (lto_asm_out_file);
1926 asm_out_file = saved_asm_out_file;
1927 saved_asm_out_file = NULL;
1930 static void
1931 darwin_asm_dwarf_section (const char *name, unsigned int flags, tree decl);
1933 /* Called for the TARGET_ASM_NAMED_SECTION hook. */
1935 void
1936 darwin_asm_named_section (const char *name,
1937 unsigned int flags,
1938 tree decl ATTRIBUTE_UNUSED)
1940 /* LTO sections go in a special section that encapsulates the (unlimited)
1941 number of GNU LTO sections within a single mach-o one. */
1942 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1943 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1945 darwin_lto_section_e e;
1946 /* We expect certain flags to be set... */
1947 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1948 == (SECTION_DEBUG | SECTION_NAMED));
1950 /* Switch to our combined section. */
1951 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
1952 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
1953 /* Output a label for the start of this sub-section. */
1954 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
1955 lto_section_num, name);
1956 /* We have to jump through hoops to get the values of the intra-section
1957 offsets... */
1958 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
1959 lto_section_num, lto_section_num);
1960 fprintf (asm_out_file,
1961 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
1962 lto_section_num, lto_section_num+1, lto_section_num);
1963 lto_section_num++;
1964 e.sectname = xstrdup (name);
1965 /* Keep the names, we'll need to make a table later.
1966 TODO: check that we do not revisit sections, that would break
1967 the assumption of how this is done. */
1968 if (lto_section_names == NULL)
1969 vec_alloc (lto_section_names, 16);
1970 vec_safe_push (lto_section_names, e);
1972 else if (strncmp (name, "__DWARF,", 8) == 0)
1973 darwin_asm_dwarf_section (name, flags, decl);
1974 else
1975 fprintf (asm_out_file, "\t.section %s\n", name);
1978 void
1979 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
1981 /* Darwin does not use unique sections. */
1984 /* Handle __attribute__ ((apple_kext_compatibility)).
1985 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
1986 vtable for classes with this attribute (and their descendants) by not
1987 outputting the new 3.0 nondeleting destructor. This means that such
1988 objects CANNOT be allocated on the stack or as globals UNLESS they have
1989 a completely empty `operator delete'.
1990 Luckily, this fits in with the Darwin kext model.
1992 This attribute also disables gcc3's potential overlaying of derived
1993 class data members on the padding at the end of the base class. */
1995 tree
1996 darwin_handle_kext_attribute (tree *node, tree name,
1997 tree args ATTRIBUTE_UNUSED,
1998 int flags ATTRIBUTE_UNUSED,
1999 bool *no_add_attrs)
2001 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
2002 if (! TARGET_KEXTABI)
2004 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2005 "only when compiling a kext", name);
2007 *no_add_attrs = true;
2009 else if (TREE_CODE (*node) != RECORD_TYPE)
2011 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2012 "only to C++ classes", name);
2014 *no_add_attrs = true;
2017 return NULL_TREE;
2020 /* Handle a "weak_import" attribute; arguments as in
2021 struct attribute_spec.handler. */
2023 tree
2024 darwin_handle_weak_import_attribute (tree *node, tree name,
2025 tree ARG_UNUSED (args),
2026 int ARG_UNUSED (flags),
2027 bool * no_add_attrs)
2029 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
2031 warning (OPT_Wattributes, "%qE attribute ignored",
2032 name);
2033 *no_add_attrs = true;
2035 else
2036 declare_weak (*node);
2038 return NULL_TREE;
2041 /* Emit a label for an FDE, making it global and/or weak if appropriate.
2042 The third parameter is nonzero if this is for exception handling.
2043 The fourth parameter is nonzero if this is just a placeholder for an
2044 FDE that we are omitting. */
2046 void
2047 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
2049 char *lab ;
2050 char buf[32];
2051 static int invok_count = 0;
2052 static tree last_fun_decl = NULL_TREE;
2054 /* We use the linker to emit the .eh labels for Darwin 9 and above. */
2055 if (! for_eh || generating_for_darwin_version >= 9)
2056 return;
2058 /* FIXME: This only works when the eh for all sections of a function is
2059 emitted at the same time. If that changes, we would need to use a lookup
2060 table of some form to determine what to do. Also, we should emit the
2061 unadorned label for the partition containing the public label for a
2062 function. This is of limited use, probably, since we do not currently
2063 enable partitioning. */
2064 strcpy (buf, ".eh");
2065 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
2067 if (decl == last_fun_decl)
2069 invok_count++;
2070 snprintf (buf, 31, "$$part$$%d.eh", invok_count);
2072 else
2074 last_fun_decl = decl;
2075 invok_count = 0;
2079 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
2081 if (TREE_PUBLIC (decl))
2083 targetm.asm_out.globalize_label (file, lab);
2084 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
2086 fputs ("\t.private_extern ", file);
2087 assemble_name (file, lab);
2088 fputc ('\n', file);
2092 if (DECL_WEAK (decl))
2094 fputs ("\t.weak_definition ", file);
2095 assemble_name (file, lab);
2096 fputc ('\n', file);
2099 assemble_name (file, lab);
2100 if (empty)
2102 fputs (" = 0\n", file);
2104 /* Mark the absolute .eh and .eh1 style labels as needed to
2105 ensure that we don't dead code strip them and keep such
2106 labels from another instantiation point until we can fix this
2107 properly with group comdat support. */
2108 darwin_mark_decl_preserved (lab);
2110 else
2111 fputs (":\n", file);
2113 free (lab);
2116 static GTY(()) unsigned long except_table_label_num;
2118 void
2119 darwin_emit_except_table_label (FILE *file)
2121 char section_start_label[30];
2123 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
2124 except_table_label_num++);
2125 ASM_OUTPUT_LABEL (file, section_start_label);
2127 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
2129 void
2130 darwin_non_lazy_pcrel (FILE *file, rtx addr)
2132 const char *nlp_name;
2134 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2136 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2137 fputs ("\t.long\t", file);
2138 ASM_OUTPUT_LABELREF (file, nlp_name);
2139 fputs ("-.", file);
2142 /* If this is uncommented, details of each allocation will be printed
2143 in the asm right before the actual code. WARNING - this will cause some
2144 test-suite fails (since the printout will contain items that some tests
2145 are not expecting) -- so don't leave it on by default (it bloats the
2146 asm too). */
2147 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
2149 /* The first two of these routines are ostensibly just intended to put
2150 names into the asm. However, they are both hijacked in order to ensure
2151 that zero-sized items do not make their way into the output. Consequently,
2152 we also need to make these participate in provisions for dealing with
2153 such items in section anchors. */
2155 /* The implementation of ASM_DECLARE_OBJECT_NAME. */
2156 /* The RTTI data (e.g., __ti4name) is common and public (and static),
2157 but it does need to be referenced via indirect PIC data pointers.
2158 The machopic_define_symbol calls are telling the machopic subsystem
2159 that the name *is* defined in this module, so it doesn't need to
2160 make them indirect. */
2161 void
2162 darwin_asm_declare_object_name (FILE *file,
2163 const char *nam, tree decl)
2165 const char *xname = nam;
2166 unsigned HOST_WIDE_INT size;
2167 bool local_def, weak;
2169 weak = (DECL_P (decl)
2170 && DECL_WEAK (decl)
2171 && !lookup_attribute ("weak_import",
2172 DECL_ATTRIBUTES (decl)));
2174 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
2175 && (!DECL_COMMON (decl)
2176 || !TREE_PUBLIC (decl)));
2178 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
2179 xname = IDENTIFIER_POINTER (DECL_NAME (decl));
2181 if (local_def)
2183 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2184 if (!weak)
2185 machopic_define_symbol (DECL_RTL (decl));
2188 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
2190 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2191 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
2192 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
2193 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
2194 (unsigned long long)size, DECL_ALIGN (decl), local_def,
2195 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2196 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
2197 (unsigned long)DECL_INITIAL (decl));
2198 #endif
2200 /* Darwin needs help to support local zero-sized objects.
2201 They must be made at least one byte, and the section containing must be
2202 marked as unsuitable for section-anchors (see storage allocators below).
2204 For non-zero objects this output is handled by varasm.c.
2206 if (!size)
2208 unsigned int l2align = 0;
2210 /* The align must be honored, even for zero-sized. */
2211 if (DECL_ALIGN (decl))
2213 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
2214 fprintf (file, "\t.align\t%u\n", l2align);
2217 ASM_OUTPUT_LABEL (file, xname);
2218 size = 1;
2219 fprintf (file, "\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2221 /* Check that we've correctly picked up the zero-sized item and placed it
2222 properly. */
2223 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2224 || (in_section
2225 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2227 else
2228 ASM_OUTPUT_LABEL (file, xname);
2231 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
2232 void
2233 darwin_asm_declare_constant_name (FILE *file, const char *name,
2234 const_tree exp ATTRIBUTE_UNUSED,
2235 HOST_WIDE_INT size)
2237 assemble_label (file, name);
2238 /* As for other items, we need at least one byte. */
2239 if (!size)
2241 fputs ("\t.space\t1\n", file);
2242 /* Check that we've correctly picked up the zero-sized item and placed it
2243 properly. */
2244 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2245 || (in_section
2246 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2250 /* Darwin storage allocators.
2252 Zerofill sections are desirable for large blank data since, otherwise, these
2253 data bloat objects (PR33210).
2255 However, section anchors don't work in .zerofill sections (one cannot switch
2256 to a zerofill section). Ergo, for Darwin targets using section anchors we need
2257 to put (at least some) data into 'normal' switchable sections.
2259 Here we set a relatively arbitrary value for the size of an object to trigger
2260 zerofill when section anchors are enabled (anything bigger than a page for
2261 current Darwin implementations). FIXME: there ought to be some objective way
2262 to make this choice.
2264 When section anchor are off this is ignored anyway. */
2266 #define BYTES_ZFILL 4096
2268 /* Emit a chunk of data for items coalesced by the linker. */
2269 static void
2270 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
2271 unsigned HOST_WIDE_INT size,
2272 unsigned int align)
2274 /* Since the sections used here are coalesed, they will not be eligible
2275 for section anchors, and therefore we don't need to break that out. */
2276 if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
2277 switch_to_section (darwin_sections[const_data_coal_section]);
2278 else
2279 switch_to_section (darwin_sections[data_coal_section]);
2281 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
2282 the align info for zero-sized items... but do it here otherwise. */
2283 if (size && align)
2284 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2286 if (TREE_PUBLIC (decl))
2287 darwin_globalize_label (fp, name);
2289 /* ... and we let it deal with outputting one byte of zero for them too. */
2290 darwin_asm_declare_object_name (fp, name, decl);
2291 if (size)
2292 assemble_zeros (size);
2295 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
2296 static void
2297 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
2298 unsigned HOST_WIDE_INT size,
2299 unsigned int align, tree meta)
2301 section *ocs = data_section;
2303 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
2304 ocs = darwin_objc2_section (decl, meta, ocs);
2305 else
2306 ocs = darwin_objc1_section (decl, meta, ocs);
2308 switch_to_section (ocs);
2310 /* We shall declare that zero-sized meta-data are not valid (yet). */
2311 gcc_assert (size);
2312 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2314 /* ... and we let it deal with outputting one byte of zero for them too. */
2315 darwin_asm_declare_object_name (fp, name, decl);
2316 assemble_zeros (size);
2319 /* This routine emits 'local' storage:
2321 When Section Anchors are off this routine emits .zerofill commands in
2322 sections named for their alignment.
2324 When Section Anchors are on, smaller (non-zero-sized) items are placed in
2325 the .static_data section so that the section anchoring system can see them.
2326 Larger items are still placed in .zerofill sections, addressing PR33210.
2327 The routine has no checking - it is all assumed to be done by the caller.
2329 static void
2330 darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
2331 unsigned HOST_WIDE_INT size,
2332 unsigned int l2align)
2334 /* FIXME: We have a fudge to make this work with Java even when the target does
2335 not use sections anchors -- Java seems to need at least one small item in a
2336 non-zerofill segment. */
2337 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2338 || (size && size <= 2))
2340 /* Put smaller objects in _static_data, where the section anchors system
2341 can get them.
2342 However, if they are zero-sized punt them to yet a different section
2343 (that is not allowed to participate in anchoring). */
2344 if (!size)
2346 fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
2347 in_section = darwin_sections[zobj_bss_section];
2348 size = 1;
2350 else
2352 fputs ("\t.static_data\n", fp);
2353 in_section = darwin_sections[static_data_section];
2356 if (l2align)
2357 fprintf (fp, "\t.align\t%u\n", l2align);
2359 assemble_name (fp, name);
2360 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2362 else
2364 /* When we are on a non-section anchor target, we can get zero-sized
2365 items here. However, all we need to do is to bump them to one byte
2366 and the section alignment will take care of the rest. */
2367 char secnam[64];
2368 unsigned int flags ;
2369 snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"),
2370 (unsigned) l2align);
2371 /* We can't anchor (yet, if ever) in zerofill sections, because we can't
2372 switch to them and emit a label. */
2373 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2374 in_section = get_section (secnam, flags, NULL);
2375 fprintf (fp, "\t.zerofill %s,", secnam);
2376 assemble_name (fp, name);
2377 if (!size)
2378 size = 1;
2380 if (l2align)
2381 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2382 size, (unsigned) l2align);
2383 else
2384 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2387 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2388 /* This is defined as a file-scope var, so we know to notify machopic. */
2389 machopic_define_symbol (DECL_RTL (decl));
2392 /* Emit a chunk of common. */
2393 static void
2394 darwin_emit_common (FILE *fp, const char *name,
2395 unsigned HOST_WIDE_INT size, unsigned int align)
2397 unsigned HOST_WIDE_INT rounded;
2398 unsigned int l2align;
2400 /* Earlier systems complain if the alignment exceeds the page size.
2401 The magic number is 4096 * 8 - hard-coded for legacy systems. */
2402 if (!emit_aligned_common && (align > 32768UL))
2403 align = 4096UL; /* In units. */
2404 else
2405 align /= BITS_PER_UNIT;
2407 /* Make sure we have a meaningful align. */
2408 if (!align)
2409 align = 1;
2411 /* For earlier toolchains, we need to emit the var as a rounded size to
2412 tell ld the alignment. */
2413 if (size < align)
2414 rounded = align;
2415 else
2416 rounded = (size + (align-1)) & ~(align-1);
2418 l2align = floor_log2 (align);
2419 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2421 in_section = comm_section;
2422 /* We mustn't allow multiple public symbols to share an address when using
2423 the normal OSX toolchain. */
2424 if (!size)
2426 /* Put at least one byte. */
2427 size = 1;
2428 /* This section can no longer participate in section anchoring. */
2429 comm_section->common.flags |= SECTION_NO_ANCHOR;
2432 fputs ("\t.comm\t", fp);
2433 assemble_name (fp, name);
2434 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
2435 emit_aligned_common?size:rounded);
2436 if (l2align && emit_aligned_common)
2437 fprintf (fp, ",%u", l2align);
2438 fputs ("\n", fp);
2441 /* Output a var which is all zero - into aligned BSS sections, common, lcomm
2442 or coalescable data sections (for weak or comdat) as appropriate. */
2444 void
2445 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
2446 unsigned HOST_WIDE_INT size, unsigned int align)
2448 unsigned int l2align;
2449 bool one, pub, weak;
2450 tree meta;
2452 pub = TREE_PUBLIC (decl);
2453 one = DECL_ONE_ONLY (decl);
2454 weak = (DECL_P (decl)
2455 && DECL_WEAK (decl)
2456 && !lookup_attribute ("weak_import",
2457 DECL_ATTRIBUTES (decl)));
2459 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2460 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
2461 " pub %d weak %d one %d init %lx\n",
2462 name, (long long)size, (int)align, TREE_READONLY (decl),
2463 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2464 pub, weak, one, (unsigned long)DECL_INITIAL (decl));
2465 #endif
2467 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2468 before the target has a chance to comment. */
2469 if ((meta = is_objc_metadata (decl)))
2471 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2472 return;
2475 /* Check that any initializer is valid. */
2476 gcc_assert ((DECL_INITIAL (decl) == NULL)
2477 || (DECL_INITIAL (decl) == error_mark_node)
2478 || initializer_zerop (DECL_INITIAL (decl)));
2480 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2481 gcc_assert (!DECL_COMMON (decl));
2483 /* Pick up the correct alignment. */
2484 if (!size || !align)
2485 align = DECL_ALIGN (decl);
2487 l2align = floor_log2 (align / BITS_PER_UNIT);
2488 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2490 last_assemble_variable_decl = decl;
2492 /* We would rather not have to check this here - but it seems that we might
2493 be passed a decl that should be in coalesced space. */
2494 if (one || weak)
2496 /* Weak or COMDAT objects are put in mergeable sections. */
2497 darwin_emit_weak_or_comdat (fp, decl, name, size,
2498 DECL_ALIGN (decl));
2499 return;
2502 /* If this is not public, then emit according to local rules. */
2503 if (!pub)
2505 darwin_emit_local_bss (fp, decl, name, size, l2align);
2506 return;
2509 /* So we have a public symbol (small item fudge for Java, see above). */
2510 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2511 || (size && size <= 2))
2513 /* Put smaller objects in data, where the section anchors system can get
2514 them. However, if they are zero-sized punt them to yet a different
2515 section (that is not allowed to participate in anchoring). */
2516 if (!size)
2518 fputs ("\t.section\t__DATA,__zobj_data\n", fp);
2519 in_section = darwin_sections[zobj_data_section];
2520 size = 1;
2522 else
2524 fputs ("\t.data\n", fp);
2525 in_section = data_section;
2528 if (l2align)
2529 fprintf (fp, "\t.align\t%u\n", l2align);
2531 assemble_name (fp, name);
2532 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2534 else
2536 char secnam[64];
2537 unsigned int flags ;
2538 /* When we are on a non-section anchor target, we can get zero-sized
2539 items here. However, all we need to do is to bump them to one byte
2540 and the section alignment will take care of the rest. */
2541 snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align);
2543 /* We can't anchor in zerofill sections, because we can't switch
2544 to them and emit a label. */
2545 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2546 in_section = get_section (secnam, flags, NULL);
2547 fprintf (fp, "\t.zerofill %s,", secnam);
2548 assemble_name (fp, name);
2549 if (!size)
2550 size = 1;
2552 if (l2align)
2553 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
2554 else
2555 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2557 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2560 /* Output a chunk of common, with alignment specified (where the target
2561 supports this). */
2562 void
2563 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
2564 unsigned HOST_WIDE_INT size,
2565 unsigned int align)
2567 unsigned int l2align;
2568 bool one, weak;
2569 tree meta;
2571 /* No corresponding var. */
2572 if (decl==NULL)
2574 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2575 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
2576 #endif
2577 darwin_emit_common (fp, name, size, align);
2578 return;
2581 one = DECL_ONE_ONLY (decl);
2582 weak = (DECL_P (decl)
2583 && DECL_WEAK (decl)
2584 && !lookup_attribute ("weak_import",
2585 DECL_ATTRIBUTES (decl)));
2587 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2588 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
2589 " weak %d one %d init %lx\n",
2590 name, (long long)size, (int)align, TREE_READONLY (decl),
2591 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2592 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
2593 #endif
2595 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2596 before the target has a chance to comment. */
2597 if ((meta = is_objc_metadata (decl)))
2599 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2600 return;
2603 /* We shouldn't be messing with this if the decl has a section name. */
2604 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2606 /* We would rather not have to check this here - but it seems that we might
2607 be passed a decl that should be in coalesced space. */
2608 if (one || weak)
2610 /* Weak or COMDAT objects are put in mergable sections. */
2611 darwin_emit_weak_or_comdat (fp, decl, name, size,
2612 DECL_ALIGN (decl));
2613 return;
2616 /* We should only get here for DECL_COMMON, with a zero init (and, in
2617 principle, only for public symbols too - although we deal with local
2618 ones below). */
2620 /* Check the initializer is OK. */
2621 gcc_assert (DECL_COMMON (decl)
2622 && ((DECL_INITIAL (decl) == NULL)
2623 || (DECL_INITIAL (decl) == error_mark_node)
2624 || initializer_zerop (DECL_INITIAL (decl))));
2626 last_assemble_variable_decl = decl;
2628 if (!size || !align)
2629 align = DECL_ALIGN (decl);
2631 l2align = floor_log2 (align / BITS_PER_UNIT);
2632 /* Check we aren't asking for more aligment than the platform allows. */
2633 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2635 if (TREE_PUBLIC (decl) != 0)
2636 darwin_emit_common (fp, name, size, align);
2637 else
2638 darwin_emit_local_bss (fp, decl, name, size, l2align);
2641 /* Output a chunk of BSS with alignment specfied. */
2642 void
2643 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
2644 unsigned HOST_WIDE_INT size,
2645 unsigned int align)
2647 unsigned long l2align;
2648 bool one, weak;
2649 tree meta;
2651 one = DECL_ONE_ONLY (decl);
2652 weak = (DECL_P (decl)
2653 && DECL_WEAK (decl)
2654 && !lookup_attribute ("weak_import",
2655 DECL_ATTRIBUTES (decl)));
2657 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2658 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
2659 " weak %d init %lx\n",
2660 name, (long long)size, (int)align, TREE_READONLY (decl),
2661 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
2662 weak , (unsigned long)DECL_INITIAL (decl));
2663 #endif
2665 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2666 before the target has a chance to comment. */
2667 if ((meta = is_objc_metadata (decl)))
2669 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2670 return;
2673 /* We shouldn't be messing with this if the decl has a section name. */
2674 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2676 /* We would rather not have to check this here - but it seems that we might
2677 be passed a decl that should be in coalesced space. */
2678 if (one || weak)
2680 /* Weak or COMDAT objects are put in mergable sections. */
2681 darwin_emit_weak_or_comdat (fp, decl, name, size,
2682 DECL_ALIGN (decl));
2683 return;
2686 /* .. and it should be suitable for placement in local mem. */
2687 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
2688 /* .. and any initializer must be all-zero. */
2689 gcc_assert ((DECL_INITIAL (decl) == NULL)
2690 || (DECL_INITIAL (decl) == error_mark_node)
2691 || initializer_zerop (DECL_INITIAL (decl)));
2693 last_assemble_variable_decl = decl;
2695 if (!size || !align)
2696 align = DECL_ALIGN (decl);
2698 l2align = floor_log2 (align / BITS_PER_UNIT);
2699 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2701 darwin_emit_local_bss (fp, decl, name, size, l2align);
2704 /* Emit an assembler directive to set visibility for a symbol. The
2705 only supported visibilities are VISIBILITY_DEFAULT and
2706 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
2707 extern". There is no MACH-O equivalent of ELF's
2708 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
2710 void
2711 darwin_assemble_visibility (tree decl, int vis)
2713 if (vis == VISIBILITY_DEFAULT)
2715 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
2717 fputs ("\t.private_extern ", asm_out_file);
2718 assemble_name (asm_out_file,
2719 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
2720 fputs ("\n", asm_out_file);
2722 else
2723 warning (OPT_Wattributes, "protected visibility attribute "
2724 "not supported in this configuration; ignored");
2727 /* vec used by darwin_asm_dwarf_section.
2728 Maybe a hash tab would be better here - but the intention is that this is
2729 a very short list (fewer than 16 items) and each entry should (ideally,
2730 eventually) only be presented once.
2732 A structure to hold a dwarf debug section used entry. */
2734 typedef struct GTY(()) dwarf_sect_used_entry {
2735 const char *name;
2736 unsigned count;
2738 dwarf_sect_used_entry;
2741 /* A list of used __DWARF sections. */
2742 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
2744 /* This is called when we are asked to assemble a named section and the
2745 name begins with __DWARF,. We keep a list of the section names (without
2746 the __DWARF, prefix) and use this to emit our required start label on the
2747 first switch to each section. */
2749 static void
2750 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2751 tree ARG_UNUSED (decl))
2753 unsigned i;
2754 int namelen;
2755 const char * sname;
2756 dwarf_sect_used_entry *ref;
2757 bool found = false;
2758 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2759 == (SECTION_DEBUG | SECTION_NAMED));
2760 /* We know that the name starts with __DWARF, */
2761 sname = name + 8;
2762 namelen = strchr (sname, ',') - sname;
2763 gcc_assert (namelen);
2764 if (dwarf_sect_names_table == NULL)
2765 vec_alloc (dwarf_sect_names_table, 16);
2766 else
2767 for (i = 0;
2768 dwarf_sect_names_table->iterate (i, &ref);
2769 i++)
2771 if (!ref)
2772 break;
2773 if (!strcmp (ref->name, sname))
2775 found = true;
2776 ref->count++;
2777 break;
2781 fprintf (asm_out_file, "\t.section %s\n", name);
2782 if (!found)
2784 dwarf_sect_used_entry e;
2785 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
2786 e.count = 1;
2787 e.name = xstrdup (sname);
2788 vec_safe_push (dwarf_sect_names_table, e);
2792 /* Output a difference of two labels that will be an assembly time
2793 constant if the two labels are local. (.long lab1-lab2 will be
2794 very different if lab1 is at the boundary between two sections; it
2795 will be relocated according to the second section, not the first,
2796 so one ends up with a difference between labels in different
2797 sections, which is bad in the dwarf2 eh context for instance.) */
2799 static int darwin_dwarf_label_counter;
2801 void
2802 darwin_asm_output_dwarf_delta (FILE *file, int size,
2803 const char *lab1, const char *lab2)
2805 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
2806 && lab2[0] == '*' && lab2[1] == 'L');
2807 const char *directive = (size == 8 ? ".quad" : ".long");
2809 if (islocaldiff)
2810 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
2811 else
2812 fprintf (file, "\t%s\t", directive);
2814 assemble_name_raw (file, lab1);
2815 fprintf (file, "-");
2816 assemble_name_raw (file, lab2);
2817 if (islocaldiff)
2818 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
2821 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
2822 offsets are not represented using relocs in .o files; either the
2823 section never leaves the .o file, or the linker or other tool is
2824 responsible for parsing the DWARF and updating the offsets. */
2826 void
2827 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
2828 section *base)
2830 char sname[64];
2831 int namelen;
2833 gcc_assert (base->common.flags & SECTION_NAMED);
2834 gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
2835 gcc_assert (strchr (base->named.name + 8, ','));
2837 namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
2838 sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
2839 darwin_asm_output_dwarf_delta (file, size, lab, sname);
2842 /* Called from the within the TARGET_ASM_FILE_START for each target. */
2844 void
2845 darwin_file_start (void)
2847 /* Nothing to do. */
2850 /* Called for the TARGET_ASM_FILE_END hook.
2851 Emit the mach-o pic indirection data, the lto data and, finally a flag
2852 to tell the linker that it can break the file object into sections and
2853 move those around for efficiency. */
2855 void
2856 darwin_file_end (void)
2858 if (!vec_safe_is_empty (ctors))
2859 finalize_ctors ();
2860 if (!vec_safe_is_empty (dtors))
2861 finalize_dtors ();
2863 /* If we are expecting to output NeXT ObjC meta-data, (and we actually see
2864 some) then we output the fix-and-continue marker (Image Info).
2865 This applies to Objective C, Objective C++ and LTO with either language
2866 as part of the input. */
2867 if (flag_next_runtime && objc_metadata_seen)
2869 unsigned int flags = 0;
2870 if (flag_objc_abi >= 2)
2872 flags = 16;
2873 output_section_asm_op
2874 (darwin_sections[objc2_image_info_section]->unnamed.data);
2876 else
2877 output_section_asm_op
2878 (darwin_sections[objc_image_info_section]->unnamed.data);
2880 ASM_OUTPUT_ALIGN (asm_out_file, 2);
2881 fputs ("L_OBJC_ImageInfo:\n", asm_out_file);
2883 flags |= (flag_replace_objc_classes && classes_seen) ? 1 : 0;
2884 flags |= flag_objc_gc ? 2 : 0;
2886 fprintf (asm_out_file, "\t.long\t0\n\t.long\t%u\n", flags);
2889 machopic_finish (asm_out_file);
2890 if (strcmp (lang_hooks.name, "GNU C++") == 0)
2892 switch_to_section (darwin_sections[constructor_section]);
2893 switch_to_section (darwin_sections[destructor_section]);
2894 ASM_OUTPUT_ALIGN (asm_out_file, 1);
2897 /* If there was LTO assembler output, append it to asm_out_file. */
2898 if (lto_asm_out_name)
2900 int n;
2901 char *buf, *lto_asm_txt;
2903 /* Shouldn't be here if we failed to switch back. */
2904 gcc_assert (! saved_asm_out_file);
2906 lto_asm_out_file = fopen (lto_asm_out_name, "r");
2907 if (lto_asm_out_file == NULL)
2908 fatal_error ("failed to open temporary file %s with LTO output",
2909 lto_asm_out_name);
2910 fseek (lto_asm_out_file, 0, SEEK_END);
2911 n = ftell (lto_asm_out_file);
2912 if (n > 0)
2914 fseek (lto_asm_out_file, 0, SEEK_SET);
2915 lto_asm_txt = buf = (char *) xmalloc (n + 1);
2916 while (fgets (lto_asm_txt, n, lto_asm_out_file))
2917 fputs (lto_asm_txt, asm_out_file);
2918 /* Put a termination label. */
2919 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2920 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2921 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
2922 lto_section_num);
2923 /* Make sure our termination label stays in this section. */
2924 fputs ("\t.space\t1\n", asm_out_file);
2927 /* Remove the temporary file. */
2928 fclose (lto_asm_out_file);
2929 unlink_if_ordinary (lto_asm_out_name);
2930 free (lto_asm_out_name);
2933 /* Output the names and indices. */
2934 if (lto_section_names && lto_section_names->length ())
2936 int count;
2937 darwin_lto_section_e *ref;
2938 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
2939 the latter up ourselves. */
2940 const char *op = integer_asm_op (4,0);
2942 /* Emit the names. */
2943 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2944 LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
2945 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2947 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
2948 /* We have to jump through hoops to get the values of the intra-section
2949 offsets... */
2950 fprintf (asm_out_file,
2951 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
2952 count, count);
2953 fprintf (asm_out_file,
2954 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
2955 count, count+1, count);
2956 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
2958 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
2959 /* make sure our termination label stays in this section. */
2960 fputs ("\t.space\t1\n", asm_out_file);
2962 /* Emit the Index. */
2963 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2964 LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
2965 fputs ("\t.align\t2\n", asm_out_file);
2966 fputs ("# Section offset, Section length, Name offset, Name length\n",
2967 asm_out_file);
2968 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2970 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
2971 op, count, ref->sectname);
2972 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
2973 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
2974 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
2978 /* If we have section anchors, then we must prevent the linker from
2979 re-arranging data. */
2980 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2981 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
2984 /* TODO: Add a language hook for identifying if a decl is a vtable. */
2985 #define DARWIN_VTABLE_P(DECL) 0
2987 /* Cross-module name binding. Darwin does not support overriding
2988 functions at dynamic-link time, except for vtables in kexts. */
2990 bool
2991 darwin_binds_local_p (const_tree decl)
2993 return default_binds_local_p_1 (decl,
2994 TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
2997 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
2998 anchor relative to ".", the current section position. We cannot use
2999 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
3000 void
3001 darwin_asm_output_anchor (rtx symbol)
3003 fprintf (asm_out_file, "\t.set\t");
3004 assemble_name (asm_out_file, XSTR (symbol, 0));
3005 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
3006 SYMBOL_REF_BLOCK_OFFSET (symbol));
3009 /* Disable section anchoring on any section containing a zero-sized
3010 object. */
3011 bool
3012 darwin_use_anchors_for_symbol_p (const_rtx symbol)
3014 if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
3016 section *sect;
3017 /* If the section contains a zero-sized object it's ineligible. */
3018 sect = SYMBOL_REF_BLOCK (symbol)->sect;
3019 /* This should have the effect of disabling anchors for vars that follow
3020 any zero-sized one, in a given section. */
3021 if (sect->common.flags & SECTION_NO_ANCHOR)
3022 return false;
3024 /* Also check the normal reasons for suppressing. */
3025 return default_use_anchors_for_symbol_p (symbol);
3027 else
3028 return false;
3031 /* Set the darwin specific attributes on TYPE. */
3032 void
3033 darwin_set_default_type_attributes (tree type)
3035 if (darwin_ms_struct
3036 && TREE_CODE (type) == RECORD_TYPE)
3037 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
3038 NULL_TREE,
3039 TYPE_ATTRIBUTES (type));
3042 /* True, iff we're generating code for loadable kernel extensions. */
3044 bool
3045 darwin_kextabi_p (void) {
3046 return flag_apple_kext;
3049 void
3050 darwin_override_options (void)
3052 /* Keep track of which (major) version we're generating code for. */
3053 if (darwin_macosx_version_min)
3055 if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
3056 generating_for_darwin_version = 10;
3057 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
3058 generating_for_darwin_version = 9;
3060 /* Earlier versions are not specifically accounted, until required. */
3063 /* In principle, this should be c-family only. However, we really need to
3064 set sensible defaults for LTO as well, since the section selection stuff
3065 should check for correctness re. the ABI. TODO: check and provide the
3066 flags (runtime & ABI) from the lto wrapper). */
3068 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
3069 if (!global_options_set.x_flag_objc_abi)
3070 global_options.x_flag_objc_abi
3071 = (!flag_next_runtime)
3073 : (TARGET_64BIT ? 2
3074 : (generating_for_darwin_version >= 9) ? 1
3075 : 0);
3077 /* Objective-C family ABI 2 is only valid for next/m64 at present. */
3078 if (global_options_set.x_flag_objc_abi && flag_next_runtime)
3080 if (TARGET_64BIT && global_options.x_flag_objc_abi < 2)
3081 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 must be"
3082 " used for %<-m64%> targets with"
3083 " %<-fnext-runtime%>");
3084 if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
3085 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 is not"
3086 " supported on %<-m32%> targets with"
3087 " %<-fnext-runtime%>");
3090 /* Don't emit DWARF3/4 unless specifically selected. This is a
3091 workaround for tool bugs. */
3092 if (!global_options_set.x_dwarf_strict)
3093 dwarf_strict = 1;
3094 if (!global_options_set.x_dwarf_version)
3095 dwarf_version = 2;
3097 /* Do not allow unwind tables to be generated by default for m32.
3098 fnon-call-exceptions will override this, regardless of what we do. */
3099 if (generating_for_darwin_version < 10
3100 && !global_options_set.x_flag_asynchronous_unwind_tables
3101 && !TARGET_64BIT)
3102 global_options.x_flag_asynchronous_unwind_tables = 0;
3104 /* Disable -freorder-blocks-and-partition when unwind tables are being
3105 emitted for Darwin < 9 (OSX 10.5).
3106 The strategy is, "Unless the User has specifically set/unset an unwind
3107 flag we will switch off -freorder-blocks-and-partition when unwind tables
3108 will be generated". If the User specifically sets flags... we assume
3109 (s)he knows why... */
3110 if (generating_for_darwin_version < 9
3111 && global_options_set.x_flag_reorder_blocks_and_partition
3112 && ((global_options.x_flag_exceptions /* User, c++, java */
3113 && !global_options_set.x_flag_exceptions) /* User specified... */
3114 || (global_options.x_flag_unwind_tables
3115 && !global_options_set.x_flag_unwind_tables)
3116 || (global_options.x_flag_non_call_exceptions
3117 && !global_options_set.x_flag_non_call_exceptions)
3118 || (global_options.x_flag_asynchronous_unwind_tables
3119 && !global_options_set.x_flag_asynchronous_unwind_tables)))
3121 inform (input_location,
3122 "-freorder-blocks-and-partition does not work with exceptions "
3123 "on this architecture");
3124 flag_reorder_blocks_and_partition = 0;
3125 flag_reorder_blocks = 1;
3128 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
3129 one valid choice of exception scheme for each runtime. */
3130 if (!global_options_set.x_flag_objc_sjlj_exceptions)
3131 global_options.x_flag_objc_sjlj_exceptions =
3132 flag_next_runtime && !TARGET_64BIT;
3134 /* FIXME: and this could be eliminated then too. */
3135 if (!global_options_set.x_flag_exceptions
3136 && flag_objc_exceptions
3137 && TARGET_64BIT)
3138 flag_exceptions = 1;
3140 if (flag_mkernel || flag_apple_kext)
3142 /* -mkernel implies -fapple-kext for C++ */
3143 if (strcmp (lang_hooks.name, "GNU C++") == 0)
3144 flag_apple_kext = 1;
3146 flag_no_common = 1;
3148 /* No EH in kexts. */
3149 flag_exceptions = 0;
3150 /* No -fnon-call-exceptions data in kexts. */
3151 flag_non_call_exceptions = 0;
3152 /* so no tables either.. */
3153 flag_unwind_tables = 0;
3154 flag_asynchronous_unwind_tables = 0;
3155 /* We still need to emit branch islands for kernel context. */
3156 darwin_emit_branch_islands = true;
3159 if (flag_var_tracking_uninit == 0
3160 && generating_for_darwin_version >= 9
3161 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
3162 : (debug_info_level >= DINFO_LEVEL_NORMAL))
3163 && write_symbols == DWARF2_DEBUG)
3164 flag_var_tracking_uninit = flag_var_tracking;
3166 if (MACHO_DYNAMIC_NO_PIC_P)
3168 if (flag_pic)
3169 warning_at (UNKNOWN_LOCATION, 0,
3170 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
3171 " %<-fpie%> or %<-fPIE%>");
3172 flag_pic = 0;
3174 else if (flag_pic == 1)
3176 /* Darwin's -fpic is -fPIC. */
3177 flag_pic = 2;
3180 /* It is assumed that branch island stubs are needed for earlier systems. */
3181 if (generating_for_darwin_version < 9)
3182 darwin_emit_branch_islands = true;
3183 else
3184 emit_aligned_common = true; /* Later systems can support aligned common. */
3186 /* The c_dialect...() macros are not available to us here. */
3187 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3190 #if DARWIN_PPC
3191 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
3193 static void
3194 darwin_patch_builtin (enum built_in_function fncode)
3196 tree fn = builtin_decl_explicit (fncode);
3197 tree sym;
3198 char *newname;
3200 if (!fn)
3201 return;
3203 sym = DECL_ASSEMBLER_NAME (fn);
3204 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
3206 set_user_assembler_name (fn, newname);
3208 fn = builtin_decl_implicit (fncode);
3209 if (fn)
3210 set_user_assembler_name (fn, newname);
3213 void
3214 darwin_patch_builtins (void)
3216 if (LONG_DOUBLE_TYPE_SIZE != 128)
3217 return;
3219 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
3220 #define PATCH_BUILTIN_NO64(fncode) \
3221 if (!TARGET_64BIT) \
3222 darwin_patch_builtin (fncode);
3223 #define PATCH_BUILTIN_VARIADIC(fncode) \
3224 if (!TARGET_64BIT \
3225 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
3226 darwin_patch_builtin (fncode);
3227 #include "darwin-ppc-ldouble-patch.def"
3228 #undef PATCH_BUILTIN
3229 #undef PATCH_BUILTIN_NO64
3230 #undef PATCH_BUILTIN_VARIADIC
3232 #endif
3234 /* CFStrings implementation. */
3235 static GTY(()) tree cfstring_class_reference = NULL_TREE;
3236 static GTY(()) tree cfstring_type_node = NULL_TREE;
3237 static GTY(()) tree ccfstring_type_node = NULL_TREE;
3238 static GTY(()) tree pccfstring_type_node = NULL_TREE;
3239 static GTY(()) tree pcint_type_node = NULL_TREE;
3240 static GTY(()) tree pcchar_type_node = NULL_TREE;
3242 static enum built_in_function darwin_builtin_cfstring;
3244 /* Store all constructed constant CFStrings in a hash table so that
3245 they get uniqued properly. */
3247 typedef struct GTY (()) cfstring_descriptor {
3248 /* The string literal. */
3249 tree literal;
3250 /* The resulting constant CFString. */
3251 tree constructor;
3252 } cfstring_descriptor;
3254 static GTY ((param_is (struct cfstring_descriptor))) htab_t cfstring_htab;
3256 static hashval_t cfstring_hash (const void *);
3257 static int cfstring_eq (const void *, const void *);
3259 static tree
3260 add_builtin_field_decl (tree type, const char *name, tree **chain)
3262 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
3263 get_identifier (name), type);
3265 if (*chain != NULL)
3266 **chain = field;
3267 *chain = &DECL_CHAIN (field);
3269 return field;
3272 tree
3273 darwin_init_cfstring_builtins (unsigned builtin_cfstring)
3275 tree cfsfun, fields, pccfstring_ftype_pcchar;
3276 tree *chain = NULL;
3278 darwin_builtin_cfstring =
3279 (enum built_in_function) builtin_cfstring;
3281 /* struct __builtin_CFString {
3282 const int *isa; (will point at
3283 int flags; __CFConstantStringClassReference)
3284 const char *str;
3285 long length;
3286 }; */
3288 pcint_type_node = build_pointer_type
3289 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
3291 pcchar_type_node = build_pointer_type
3292 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3294 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
3296 /* Have to build backwards for finish struct. */
3297 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
3298 add_builtin_field_decl (pcchar_type_node, "str", &chain);
3299 add_builtin_field_decl (integer_type_node, "flags", &chain);
3300 add_builtin_field_decl (pcint_type_node, "isa", &chain);
3301 finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
3302 fields, NULL_TREE);
3304 /* const struct __builtin_CFstring *
3305 __builtin___CFStringMakeConstantString (const char *); */
3307 ccfstring_type_node = build_qualified_type
3308 (cfstring_type_node, TYPE_QUAL_CONST);
3309 pccfstring_type_node = build_pointer_type (ccfstring_type_node);
3310 pccfstring_ftype_pcchar = build_function_type_list
3311 (pccfstring_type_node, pcchar_type_node, NULL_TREE);
3313 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
3314 get_identifier ("__builtin___CFStringMakeConstantString"),
3315 pccfstring_ftype_pcchar);
3317 TREE_PUBLIC (cfsfun) = 1;
3318 DECL_EXTERNAL (cfsfun) = 1;
3319 DECL_ARTIFICIAL (cfsfun) = 1;
3320 /* Make a lang-specific section - dup_lang_specific_decl makes a new node
3321 in place of the existing, which may be NULL. */
3322 DECL_LANG_SPECIFIC (cfsfun) = NULL;
3323 (*lang_hooks.dup_lang_specific_decl) (cfsfun);
3324 DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD;
3325 DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring;
3326 lang_hooks.builtin_function (cfsfun);
3328 /* extern int __CFConstantStringClassReference[]; */
3329 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
3330 get_identifier ("__CFConstantStringClassReference"),
3331 build_array_type (integer_type_node, NULL_TREE));
3333 TREE_PUBLIC (cfstring_class_reference) = 1;
3334 DECL_ARTIFICIAL (cfstring_class_reference) = 1;
3335 (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
3336 DECL_EXTERNAL (cfstring_class_reference) = 1;
3337 rest_of_decl_compilation (cfstring_class_reference, 0, 0);
3339 /* Initialize the hash table used to hold the constant CFString objects. */
3340 cfstring_htab = htab_create_ggc (31, cfstring_hash, cfstring_eq, NULL);
3342 return cfstring_type_node;
3345 tree
3346 darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
3347 bool ARG_UNUSED (ignore))
3349 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3351 if (fcode == darwin_builtin_cfstring)
3353 if (!darwin_constant_cfstrings)
3355 error ("built-in function %qD requires the"
3356 " %<-mconstant-cfstrings%> flag", fndecl);
3357 return error_mark_node;
3360 if (n_args != 1)
3362 error ("built-in function %qD takes one argument only", fndecl);
3363 return error_mark_node;
3366 return darwin_build_constant_cfstring (*argp);
3369 return NULL_TREE;
3372 void
3373 darwin_rename_builtins (void)
3375 /* The system ___divdc3 routine in libSystem on darwin10 is not
3376 accurate to 1ulp, ours is, so we avoid ever using the system name
3377 for this routine and instead install a non-conflicting name that
3378 is accurate.
3380 When -ffast-math or -funsafe-math-optimizations is given, we can
3381 use the faster version. */
3382 if (!flag_unsafe_math_optimizations)
3384 enum built_in_function dcode
3385 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
3386 + DCmode - MIN_MODE_COMPLEX_FLOAT);
3387 tree fn = builtin_decl_explicit (dcode);
3388 /* Fortran and c call TARGET_INIT_BUILTINS and
3389 TARGET_INIT_LIBFUNCS at different times, so we have to put a
3390 call into each to ensure that at least one of them is called
3391 after build_common_builtin_nodes. A better fix is to add a
3392 new hook to run after build_common_builtin_nodes runs. */
3393 if (fn)
3394 set_user_assembler_name (fn, "___ieee_divdc3");
3395 fn = builtin_decl_implicit (dcode);
3396 if (fn)
3397 set_user_assembler_name (fn, "___ieee_divdc3");
3401 bool
3402 darwin_libc_has_function (enum function_class fn_class)
3404 if (fn_class == function_sincos)
3405 return false;
3406 if (fn_class == function_c99_math_complex
3407 || fn_class == function_c99_misc)
3408 return (TARGET_64BIT
3409 || strverscmp (darwin_macosx_version_min, "10.3") >= 0);
3411 return true;
3414 static hashval_t
3415 cfstring_hash (const void *ptr)
3417 tree str = ((const struct cfstring_descriptor *)ptr)->literal;
3418 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3419 int i, len = TREE_STRING_LENGTH (str);
3420 hashval_t h = len;
3422 for (i = 0; i < len; i++)
3423 h = ((h * 613) + p[i]);
3425 return h;
3428 static int
3429 cfstring_eq (const void *ptr1, const void *ptr2)
3431 tree str1 = ((const struct cfstring_descriptor *)ptr1)->literal;
3432 tree str2 = ((const struct cfstring_descriptor *)ptr2)->literal;
3433 int len1 = TREE_STRING_LENGTH (str1);
3435 return (len1 == TREE_STRING_LENGTH (str2)
3436 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3437 len1));
3440 tree
3441 darwin_build_constant_cfstring (tree str)
3443 struct cfstring_descriptor *desc, key;
3444 void **loc;
3445 tree addr;
3447 if (!str)
3449 error ("CFString literal is missing");
3450 return error_mark_node;
3453 STRIP_NOPS (str);
3455 if (TREE_CODE (str) == ADDR_EXPR)
3456 str = TREE_OPERAND (str, 0);
3458 if (TREE_CODE (str) != STRING_CST)
3460 error ("CFString literal expression is not a string constant");
3461 return error_mark_node;
3464 /* Perhaps we already constructed a constant CFString just like this one? */
3465 key.literal = str;
3466 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3467 desc = (struct cfstring_descriptor *) *loc;
3469 if (!desc)
3471 tree var, constructor, field;
3472 vec<constructor_elt, va_gc> *v = NULL;
3473 int length = TREE_STRING_LENGTH (str) - 1;
3475 if (darwin_warn_nonportable_cfstrings)
3477 const char *s = TREE_STRING_POINTER (str);
3478 int l = 0;
3480 for (l = 0; l < length; l++)
3481 if (!s[l] || !isascii (s[l]))
3483 warning (darwin_warn_nonportable_cfstrings, "%s in CFString literal",
3484 s[l] ? "non-ASCII character" : "embedded NUL");
3485 break;
3489 *loc = desc = ggc_alloc_cleared_cfstring_descriptor ();
3490 desc->literal = str;
3492 /* isa *. */
3493 field = TYPE_FIELDS (ccfstring_type_node);
3494 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3495 build1 (ADDR_EXPR, TREE_TYPE (field),
3496 cfstring_class_reference));
3497 /* flags */
3498 field = DECL_CHAIN (field);
3499 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3500 build_int_cst (TREE_TYPE (field), 0x000007c8));
3501 /* string *. */
3502 field = DECL_CHAIN (field);
3503 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3504 build1 (ADDR_EXPR, TREE_TYPE (field), str));
3505 /* length */
3506 field = DECL_CHAIN (field);
3507 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3508 build_int_cst (TREE_TYPE (field), length));
3510 constructor = build_constructor (ccfstring_type_node, v);
3511 TREE_READONLY (constructor) = 1;
3512 TREE_CONSTANT (constructor) = 1;
3513 TREE_STATIC (constructor) = 1;
3515 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
3516 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
3517 being built without any knowledge of C++ tree accessors; hence, we shall
3518 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
3519 if (darwin_running_cxx)
3520 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
3522 /* Create an anonymous global variable for this CFString. */
3523 var = build_decl (input_location, CONST_DECL,
3524 NULL, TREE_TYPE (constructor));
3525 DECL_ARTIFICIAL (var) = 1;
3526 TREE_STATIC (var) = 1;
3527 DECL_INITIAL (var) = constructor;
3528 /* FIXME: This should use a translation_unit_decl to indicate file scope. */
3529 DECL_CONTEXT (var) = NULL_TREE;
3530 desc->constructor = var;
3533 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
3534 TREE_CONSTANT (addr) = 1;
3536 return addr;
3539 bool
3540 darwin_cfstring_p (tree str)
3542 struct cfstring_descriptor key;
3543 void **loc;
3545 if (!str)
3546 return false;
3548 STRIP_NOPS (str);
3550 if (TREE_CODE (str) == ADDR_EXPR)
3551 str = TREE_OPERAND (str, 0);
3553 if (TREE_CODE (str) != STRING_CST)
3554 return false;
3556 key.literal = str;
3557 loc = htab_find_slot (cfstring_htab, &key, NO_INSERT);
3559 if (loc)
3560 return true;
3562 return false;
3565 void
3566 darwin_enter_string_into_cfstring_table (tree str)
3568 struct cfstring_descriptor key;
3569 void **loc;
3571 key.literal = str;
3572 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3574 if (!*loc)
3576 *loc = ggc_alloc_cleared_cfstring_descriptor ();
3577 ((struct cfstring_descriptor *)*loc)->literal = str;
3581 /* Choose named function section based on its frequency. */
3583 section *
3584 darwin_function_section (tree decl, enum node_frequency freq,
3585 bool startup, bool exit)
3587 /* Decide if we need to put this in a coalescable section. */
3588 bool weak = (decl
3589 && DECL_WEAK (decl)
3590 && (!DECL_ATTRIBUTES (decl)
3591 || !lookup_attribute ("weak_import",
3592 DECL_ATTRIBUTES (decl))));
3594 /* If there is a specified section name, we should not be trying to
3595 override. */
3596 if (decl && DECL_SECTION_NAME (decl) != NULL_TREE)
3597 return get_named_section (decl, NULL, 0);
3599 /* Default when there is no function re-ordering. */
3600 if (!flag_reorder_functions)
3601 return (weak)
3602 ? darwin_sections[text_coal_section]
3603 : text_section;
3605 /* Startup code should go to startup subsection unless it is
3606 unlikely executed (this happens especially with function splitting
3607 where we can split away unnecessary parts of static constructors). */
3608 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
3609 return (weak)
3610 ? darwin_sections[text_startup_coal_section]
3611 : darwin_sections[text_startup_section];
3613 /* Similarly for exit. */
3614 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
3615 return (weak)
3616 ? darwin_sections[text_exit_coal_section]
3617 : darwin_sections[text_exit_section];
3619 /* Group cold functions together, similarly for hot code. */
3620 switch (freq)
3622 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
3623 return (weak)
3624 ? darwin_sections[text_cold_coal_section]
3625 : darwin_sections[text_cold_section];
3626 break;
3627 case NODE_FREQUENCY_HOT:
3628 return (weak)
3629 ? darwin_sections[text_hot_coal_section]
3630 : darwin_sections[text_hot_section];
3631 break;
3632 default:
3633 return (weak)
3634 ? darwin_sections[text_coal_section]
3635 : text_section;
3636 break;
3640 /* When a function is partitioned between sections, we need to insert a label
3641 at the start of each new chunk - so that it may become a valid 'atom' for
3642 eh and debug purposes. Without this the linker will emit warnings if one
3643 tries to add line location information (since the switched fragment will
3644 be anonymous). */
3646 void
3647 darwin_function_switched_text_sections (FILE *fp, tree decl, bool new_is_cold)
3649 char buf[128];
3650 snprintf (buf, 128, "%s%s",new_is_cold?"__cold_sect_of_":"__hot_sect_of_",
3651 IDENTIFIER_POINTER (DECL_NAME (decl)));
3652 /* Make sure we pick up all the relevant quotes etc. */
3653 assemble_name_raw (fp, (const char *) buf);
3654 fputs (":\n", fp);
3657 #include "gt-darwin.h"