PR/56490
[official-gcc.git] / gcc / config / darwin.c
bloba049a5d079623647d2a0262080ee3efa4d31f552
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;
373 void
374 machopic_output_function_base_name (FILE *file)
376 const char *current_name;
378 /* If dynamic-no-pic is on, we should not get here. */
379 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
380 /* When we are generating _get_pc thunks within stubs, there is no current
381 function. */
382 if (current_function_decl)
384 current_name =
385 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
386 if (function_base_func_name != current_name)
388 ++current_pic_label_num;
389 function_base_func_name = current_name;
392 else
394 ++current_pic_label_num;
395 function_base_func_name = "L_machopic_stub_dummy";
397 fprintf (file, "L%011d$pb", current_pic_label_num);
400 /* The suffix attached to non-lazy pointer symbols. */
401 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
402 /* The suffix attached to stub symbols. */
403 #define STUB_SUFFIX "$stub"
405 typedef struct GTY (()) machopic_indirection
407 /* The SYMBOL_REF for the entity referenced. */
408 rtx symbol;
409 /* The name of the stub or non-lazy pointer. */
410 const char * ptr_name;
411 /* True iff this entry is for a stub (as opposed to a non-lazy
412 pointer). */
413 bool stub_p;
414 /* True iff this stub or pointer pointer has been referenced. */
415 bool used;
416 } machopic_indirection;
418 /* A table mapping stub names and non-lazy pointer names to
419 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
421 static GTY ((param_is (struct machopic_indirection))) htab_t
422 machopic_indirections;
424 /* Return a hash value for a SLOT in the indirections hash table. */
426 static hashval_t
427 machopic_indirection_hash (const void *slot)
429 const machopic_indirection *p = (const machopic_indirection *) slot;
430 return htab_hash_string (p->ptr_name);
433 /* Returns true if the KEY is the same as that associated with
434 SLOT. */
436 static int
437 machopic_indirection_eq (const void *slot, const void *key)
439 return strcmp (((const machopic_indirection *) slot)->ptr_name,
440 (const char *) key) == 0;
443 /* Return the name of the non-lazy pointer (if STUB_P is false) or
444 stub (if STUB_B is true) corresponding to the given name. */
446 const char *
447 machopic_indirection_name (rtx sym_ref, bool stub_p)
449 char *buffer;
450 const char *name = XSTR (sym_ref, 0);
451 size_t namelen = strlen (name);
452 machopic_indirection *p;
453 void ** slot;
454 bool needs_quotes;
455 const char *suffix;
456 const char *prefix = user_label_prefix;
457 const char *quote = "";
458 tree id;
460 id = maybe_get_identifier (name);
461 if (id)
463 tree id_orig = id;
465 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
466 id = TREE_CHAIN (id);
467 if (id != id_orig)
469 name = IDENTIFIER_POINTER (id);
470 namelen = strlen (name);
474 if (name[0] == '*')
476 prefix = "";
477 ++name;
478 --namelen;
481 needs_quotes = name_needs_quotes (name);
482 if (needs_quotes)
484 quote = "\"";
487 if (stub_p)
488 suffix = STUB_SUFFIX;
489 else
490 suffix = NON_LAZY_POINTER_SUFFIX;
492 buffer = XALLOCAVEC (char, strlen ("&L")
493 + strlen (prefix)
494 + namelen
495 + strlen (suffix)
496 + 2 * strlen (quote)
497 + 1 /* '\0' */);
499 /* Construct the name of the non-lazy pointer or stub. */
500 sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
502 if (!machopic_indirections)
503 machopic_indirections = htab_create_ggc (37,
504 machopic_indirection_hash,
505 machopic_indirection_eq,
506 /*htab_del=*/NULL);
508 slot = htab_find_slot_with_hash (machopic_indirections, buffer,
509 htab_hash_string (buffer), INSERT);
510 if (*slot)
512 p = (machopic_indirection *) *slot;
514 else
516 p = ggc_alloc_machopic_indirection ();
517 p->symbol = sym_ref;
518 p->ptr_name = xstrdup (buffer);
519 p->stub_p = stub_p;
520 p->used = false;
521 *slot = p;
524 return p->ptr_name;
527 /* Return the name of the stub for the mcount function. */
529 const char*
530 machopic_mcount_stub_name (void)
532 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
533 return machopic_indirection_name (symbol, /*stub_p=*/true);
536 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
537 or non-lazy pointer as used -- and mark the object to which the
538 pointer/stub refers as used as well, since the pointer/stub will
539 emit a reference to it. */
541 void
542 machopic_validate_stub_or_non_lazy_ptr (const char *name)
544 machopic_indirection *p;
546 p = ((machopic_indirection *)
547 (htab_find_with_hash (machopic_indirections, name,
548 htab_hash_string (name))));
549 if (p && ! p->used)
551 const char *real_name;
552 tree id;
554 p->used = true;
556 /* Do what output_addr_const will do when we actually call it. */
557 if (SYMBOL_REF_DECL (p->symbol))
558 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
560 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
562 id = maybe_get_identifier (real_name);
563 if (id)
564 mark_referenced (id);
568 /* Transform ORIG, which may be any data source, to the corresponding
569 source using indirections. */
572 machopic_indirect_data_reference (rtx orig, rtx reg)
574 rtx ptr_ref = orig;
576 if (! MACHOPIC_INDIRECT)
577 return orig;
579 if (GET_CODE (orig) == SYMBOL_REF)
581 int defined = machopic_data_defined_p (orig);
583 if (defined && MACHO_DYNAMIC_NO_PIC_P)
585 if (DARWIN_PPC)
587 /* Create a new register for CSE opportunities. */
588 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
589 emit_insn (gen_macho_high (hi_reg, orig));
590 emit_insn (gen_macho_low (reg, hi_reg, orig));
591 return reg;
593 else if (DARWIN_X86)
594 return orig;
595 else
596 /* some other cpu -- writeme! */
597 gcc_unreachable ();
599 else if (defined)
601 rtx offset = NULL;
602 if (DARWIN_PPC || HAVE_lo_sum)
603 offset = machopic_gen_offset (orig);
605 if (DARWIN_PPC)
607 rtx hi_sum_reg = (!can_create_pseudo_p ()
608 ? reg
609 : gen_reg_rtx (Pmode));
611 gcc_assert (reg);
613 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
614 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
615 gen_rtx_HIGH (Pmode, offset))));
616 emit_insn (gen_rtx_SET (Pmode, reg,
617 gen_rtx_LO_SUM (Pmode, hi_sum_reg,
618 copy_rtx (offset))));
620 orig = reg;
622 else if (HAVE_lo_sum)
624 gcc_assert (reg);
626 emit_insn (gen_rtx_SET (VOIDmode, reg,
627 gen_rtx_HIGH (Pmode, offset)));
628 emit_insn (gen_rtx_SET (VOIDmode, reg,
629 gen_rtx_LO_SUM (Pmode, reg,
630 copy_rtx (offset))));
631 emit_use (pic_offset_table_rtx);
633 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
635 return orig;
638 ptr_ref = (gen_rtx_SYMBOL_REF
639 (Pmode,
640 machopic_indirection_name (orig, /*stub_p=*/false)));
642 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
644 ptr_ref = gen_const_mem (Pmode, ptr_ref);
645 machopic_define_symbol (ptr_ref);
647 if (DARWIN_X86
648 && reg
649 && MACHO_DYNAMIC_NO_PIC_P)
651 emit_insn (gen_rtx_SET (Pmode, reg, ptr_ref));
652 ptr_ref = reg;
655 return ptr_ref;
657 else if (GET_CODE (orig) == CONST)
659 /* If "(const (plus ...", walk the PLUS and return that result.
660 PLUS processing (below) will restore the "(const ..." if
661 appropriate. */
662 if (GET_CODE (XEXP (orig, 0)) == PLUS)
663 return machopic_indirect_data_reference (XEXP (orig, 0), reg);
664 else
665 return orig;
667 else if (GET_CODE (orig) == MEM)
669 XEXP (ptr_ref, 0) =
670 machopic_indirect_data_reference (XEXP (orig, 0), reg);
671 return ptr_ref;
673 else if (GET_CODE (orig) == PLUS)
675 rtx base, result;
676 /* When the target is i386, this code prevents crashes due to the
677 compiler's ignorance on how to move the PIC base register to
678 other registers. (The reload phase sometimes introduces such
679 insns.) */
680 if (GET_CODE (XEXP (orig, 0)) == REG
681 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
682 /* Prevent the same register from being erroneously used
683 as both the base and index registers. */
684 && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST))
685 && reg)
687 emit_move_insn (reg, XEXP (orig, 0));
688 XEXP (ptr_ref, 0) = reg;
689 return ptr_ref;
692 /* Legitimize both operands of the PLUS. */
693 base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
694 orig = machopic_indirect_data_reference (XEXP (orig, 1),
695 (base == reg ? 0 : reg));
696 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
697 result = plus_constant (Pmode, base, INTVAL (orig));
698 else
699 result = gen_rtx_PLUS (Pmode, base, orig);
701 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
703 if (reg)
705 emit_move_insn (reg, result);
706 result = reg;
708 else
710 result = force_reg (GET_MODE (result), result);
714 return result;
716 return ptr_ref;
719 /* Transform TARGET (a MEM), which is a function call target, to the
720 corresponding symbol_stub if necessary. Return a new MEM. */
723 machopic_indirect_call_target (rtx target)
725 if (! darwin_emit_branch_islands)
726 return target;
728 if (GET_CODE (target) != MEM)
729 return target;
731 if (MACHOPIC_INDIRECT
732 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
733 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
734 & MACHO_SYMBOL_FLAG_DEFINED))
736 rtx sym_ref = XEXP (target, 0);
737 const char *stub_name = machopic_indirection_name (sym_ref,
738 /*stub_p=*/true);
739 enum machine_mode mode = GET_MODE (sym_ref);
741 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
742 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
743 MEM_READONLY_P (target) = 1;
744 MEM_NOTRAP_P (target) = 1;
747 return target;
751 machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
753 rtx pic_ref = orig;
755 if (! MACHOPIC_INDIRECT)
756 return orig;
758 /* First handle a simple SYMBOL_REF or LABEL_REF */
759 if (GET_CODE (orig) == LABEL_REF
760 || (GET_CODE (orig) == SYMBOL_REF
763 /* addr(foo) = &func+(foo-func) */
764 orig = machopic_indirect_data_reference (orig, reg);
766 if (GET_CODE (orig) == PLUS
767 && GET_CODE (XEXP (orig, 0)) == REG)
769 if (reg == 0)
770 return force_reg (mode, orig);
772 emit_move_insn (reg, orig);
773 return reg;
776 if (GET_CODE (orig) == MEM)
778 if (reg == 0)
780 gcc_assert (!reload_in_progress);
781 reg = gen_reg_rtx (Pmode);
784 #if HAVE_lo_sum
785 if (MACHO_DYNAMIC_NO_PIC_P
786 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
787 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
789 #if defined (TARGET_TOC) /* ppc */
790 rtx temp_reg = (!can_create_pseudo_p ()
791 ? reg :
792 gen_reg_rtx (Pmode));
793 rtx asym = XEXP (orig, 0);
794 rtx mem;
796 emit_insn (gen_macho_high (temp_reg, asym));
797 mem = gen_const_mem (GET_MODE (orig),
798 gen_rtx_LO_SUM (Pmode, temp_reg,
799 copy_rtx (asym)));
800 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
801 #else
802 /* Some other CPU -- WriteMe! but right now there are no other
803 platforms that can use dynamic-no-pic */
804 gcc_unreachable ();
805 #endif
806 pic_ref = reg;
808 else
809 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
810 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
812 rtx offset = machopic_gen_offset (XEXP (orig, 0));
813 #if defined (TARGET_TOC) /* i.e., PowerPC */
814 /* Generating a new reg may expose opportunities for
815 common subexpression elimination. */
816 rtx hi_sum_reg = (!can_create_pseudo_p ()
817 ? reg
818 : gen_reg_rtx (Pmode));
819 rtx mem;
820 rtx insn;
821 rtx sum;
823 sum = gen_rtx_HIGH (Pmode, offset);
824 if (! MACHO_DYNAMIC_NO_PIC_P)
825 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
827 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
829 mem = gen_const_mem (GET_MODE (orig),
830 gen_rtx_LO_SUM (Pmode,
831 hi_sum_reg,
832 copy_rtx (offset)));
833 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
834 set_unique_reg_note (insn, REG_EQUAL, pic_ref);
836 pic_ref = reg;
837 #else
838 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
840 emit_insn (gen_rtx_SET (VOIDmode, reg,
841 gen_rtx_HIGH (Pmode,
842 gen_rtx_CONST (Pmode,
843 offset))));
844 emit_insn (gen_rtx_SET (VOIDmode, reg,
845 gen_rtx_LO_SUM (Pmode, reg,
846 gen_rtx_CONST (Pmode,
847 copy_rtx (offset)))));
848 pic_ref = gen_rtx_PLUS (Pmode,
849 pic_offset_table_rtx, reg);
850 #endif
852 else
853 #endif /* HAVE_lo_sum */
855 rtx pic = pic_offset_table_rtx;
856 if (GET_CODE (pic) != REG)
858 emit_move_insn (reg, pic);
859 pic = reg;
861 #if 0
862 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
863 #endif
865 if (reload_in_progress)
866 df_set_regs_ever_live (REGNO (pic), true);
867 pic_ref = gen_rtx_PLUS (Pmode, pic,
868 machopic_gen_offset (XEXP (orig, 0)));
871 #if !defined (TARGET_TOC)
872 emit_move_insn (reg, pic_ref);
873 pic_ref = gen_const_mem (GET_MODE (orig), reg);
874 #endif
876 else
879 #if HAVE_lo_sum
880 if (GET_CODE (orig) == SYMBOL_REF
881 || GET_CODE (orig) == LABEL_REF)
883 rtx offset = machopic_gen_offset (orig);
884 #if defined (TARGET_TOC) /* i.e., PowerPC */
885 rtx hi_sum_reg;
887 if (reg == 0)
889 gcc_assert (!reload_in_progress);
890 reg = gen_reg_rtx (Pmode);
893 hi_sum_reg = reg;
895 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
896 (MACHO_DYNAMIC_NO_PIC_P)
897 ? gen_rtx_HIGH (Pmode, offset)
898 : gen_rtx_PLUS (Pmode,
899 pic_offset_table_rtx,
900 gen_rtx_HIGH (Pmode,
901 offset))));
902 emit_insn (gen_rtx_SET (VOIDmode, reg,
903 gen_rtx_LO_SUM (Pmode,
904 hi_sum_reg,
905 copy_rtx (offset))));
906 pic_ref = reg;
907 #else
908 emit_insn (gen_rtx_SET (VOIDmode, reg,
909 gen_rtx_HIGH (Pmode, offset)));
910 emit_insn (gen_rtx_SET (VOIDmode, reg,
911 gen_rtx_LO_SUM (Pmode, reg,
912 copy_rtx (offset))));
913 pic_ref = gen_rtx_PLUS (Pmode,
914 pic_offset_table_rtx, reg);
915 #endif
917 else
918 #endif /* HAVE_lo_sum */
920 if (REG_P (orig)
921 || GET_CODE (orig) == SUBREG)
923 return orig;
925 else
927 rtx pic = pic_offset_table_rtx;
928 if (GET_CODE (pic) != REG)
930 emit_move_insn (reg, pic);
931 pic = reg;
933 #if 0
934 emit_use (pic_offset_table_rtx);
935 #endif
936 if (reload_in_progress)
937 df_set_regs_ever_live (REGNO (pic), true);
938 pic_ref = gen_rtx_PLUS (Pmode,
939 pic,
940 machopic_gen_offset (orig));
945 if (GET_CODE (pic_ref) != REG)
947 if (reg != 0)
949 emit_move_insn (reg, pic_ref);
950 return reg;
952 else
954 return force_reg (mode, pic_ref);
957 else
959 return pic_ref;
963 else if (GET_CODE (orig) == SYMBOL_REF)
964 return orig;
966 else if (GET_CODE (orig) == PLUS
967 && (GET_CODE (XEXP (orig, 0)) == MEM
968 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
969 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
970 && XEXP (orig, 0) != pic_offset_table_rtx
971 && GET_CODE (XEXP (orig, 1)) != REG)
974 rtx base;
975 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
977 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
978 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
979 Pmode, (base == reg ? 0 : reg));
980 if (GET_CODE (orig) == CONST_INT)
982 pic_ref = plus_constant (Pmode, base, INTVAL (orig));
983 is_complex = 1;
985 else
986 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
988 if (reg && is_complex)
990 emit_move_insn (reg, pic_ref);
991 pic_ref = reg;
993 /* Likewise, should we set special REG_NOTEs here? */
996 else if (GET_CODE (orig) == CONST)
998 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1001 else if (GET_CODE (orig) == MEM
1002 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
1004 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1005 addr = replace_equiv_address (orig, addr);
1006 emit_move_insn (reg, addr);
1007 pic_ref = reg;
1010 return pic_ref;
1013 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
1014 DATA is the FILE* for assembly output. Called from
1015 htab_traverse. */
1017 static int
1018 machopic_output_indirection (void **slot, void *data)
1020 machopic_indirection *p = *((machopic_indirection **) slot);
1021 FILE *asm_out_file = (FILE *) data;
1022 rtx symbol;
1023 const char *sym_name;
1024 const char *ptr_name;
1026 if (!p->used)
1027 return 1;
1029 symbol = p->symbol;
1030 sym_name = XSTR (symbol, 0);
1031 ptr_name = p->ptr_name;
1033 if (p->stub_p)
1035 char *sym;
1036 char *stub;
1037 tree id;
1039 id = maybe_get_identifier (sym_name);
1040 if (id)
1042 tree id_orig = id;
1044 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
1045 id = TREE_CHAIN (id);
1046 if (id != id_orig)
1047 sym_name = IDENTIFIER_POINTER (id);
1050 sym = XALLOCAVEC (char, strlen (sym_name) + 2);
1051 if (sym_name[0] == '*' || sym_name[0] == '&')
1052 strcpy (sym, sym_name + 1);
1053 else if (sym_name[0] == '-' || sym_name[0] == '+')
1054 strcpy (sym, sym_name);
1055 else
1056 sprintf (sym, "%s%s", user_label_prefix, sym_name);
1058 stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
1059 if (ptr_name[0] == '*' || ptr_name[0] == '&')
1060 strcpy (stub, ptr_name + 1);
1061 else
1062 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
1064 machopic_output_stub (asm_out_file, sym, stub);
1066 else if (! indirect_data (symbol)
1067 && (machopic_symbol_defined_p (symbol)
1068 || SYMBOL_REF_LOCAL_P (symbol)))
1070 switch_to_section (data_section);
1071 assemble_align (GET_MODE_ALIGNMENT (Pmode));
1072 assemble_label (asm_out_file, ptr_name);
1073 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
1074 GET_MODE_SIZE (Pmode),
1075 GET_MODE_ALIGNMENT (Pmode), 1);
1077 else
1079 rtx init = const0_rtx;
1081 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
1083 /* Mach-O symbols are passed around in code through indirect
1084 references and the original symbol_ref hasn't passed through
1085 the generic handling and reference-catching in
1086 output_operand, so we need to manually mark weak references
1087 as such. */
1088 if (SYMBOL_REF_WEAK (symbol))
1090 tree decl = SYMBOL_REF_DECL (symbol);
1091 gcc_assert (DECL_P (decl));
1093 if (decl != NULL_TREE
1094 && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
1095 /* Handle only actual external-only definitions, not
1096 e.g. extern inline code or variables for which
1097 storage has been allocated. */
1098 && !TREE_STATIC (decl))
1100 fputs ("\t.weak_reference ", asm_out_file);
1101 assemble_name (asm_out_file, sym_name);
1102 fputc ('\n', asm_out_file);
1106 assemble_name (asm_out_file, ptr_name);
1107 fprintf (asm_out_file, ":\n");
1109 fprintf (asm_out_file, "\t.indirect_symbol ");
1110 assemble_name (asm_out_file, sym_name);
1111 fprintf (asm_out_file, "\n");
1113 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1114 have their symbol name instead of 0 in the second entry of
1115 the non-lazy symbol pointer data structure when they are
1116 defined. This allows the runtime to rebind newer instances
1117 of the translation unit with the original instance of the
1118 symbol. */
1120 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1121 && machopic_symbol_defined_p (symbol))
1122 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1124 assemble_integer (init, GET_MODE_SIZE (Pmode),
1125 GET_MODE_ALIGNMENT (Pmode), 1);
1128 return 1;
1131 void
1132 machopic_finish (FILE *asm_out_file)
1134 if (machopic_indirections)
1135 htab_traverse_noresize (machopic_indirections,
1136 machopic_output_indirection,
1137 asm_out_file);
1141 machopic_operand_p (rtx op)
1143 if (MACHOPIC_JUST_INDIRECT)
1144 return (GET_CODE (op) == SYMBOL_REF
1145 && machopic_symbol_defined_p (op));
1146 else
1147 return (GET_CODE (op) == CONST
1148 && GET_CODE (XEXP (op, 0)) == UNSPEC
1149 && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
1152 /* This function records whether a given name corresponds to a defined
1153 or undefined function or variable, for machopic_classify_ident to
1154 use later. */
1156 void
1157 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
1159 rtx sym_ref;
1161 /* Do the standard encoding things first. */
1162 default_encode_section_info (decl, rtl, first);
1164 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1165 return;
1167 sym_ref = XEXP (rtl, 0);
1168 if (TREE_CODE (decl) == VAR_DECL)
1169 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1171 if (!DECL_EXTERNAL (decl)
1172 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1173 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1174 && ((TREE_STATIC (decl)
1175 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1176 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1177 && DECL_INITIAL (decl) != error_mark_node)))
1178 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1180 if (! TREE_PUBLIC (decl))
1181 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
1184 void
1185 darwin_mark_decl_preserved (const char *name)
1187 fprintf (asm_out_file, "\t.no_dead_strip ");
1188 assemble_name (asm_out_file, name);
1189 fputc ('\n', asm_out_file);
1192 static section *
1193 darwin_rodata_section (int weak, bool zsize)
1195 return (weak
1196 ? darwin_sections[const_coal_section]
1197 : (zsize ? darwin_sections[zobj_const_section]
1198 : darwin_sections[const_section]));
1201 static section *
1202 darwin_mergeable_string_section (tree exp,
1203 unsigned HOST_WIDE_INT align)
1205 /* Darwin's ld expects to see non-writable string literals in the .cstring
1206 section. Later versions of ld check and complain when CFStrings are
1207 enabled. Therefore we shall force the strings into .cstring since we
1208 don't support writable ones anyway. */
1209 if ((darwin_constant_cfstrings || flag_merge_constants)
1210 && TREE_CODE (exp) == STRING_CST
1211 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1212 && align <= 256
1213 && (int_size_in_bytes (TREE_TYPE (exp))
1214 == TREE_STRING_LENGTH (exp))
1215 && ((size_t) TREE_STRING_LENGTH (exp)
1216 == strlen (TREE_STRING_POINTER (exp)) + 1))
1217 return darwin_sections[cstring_section];
1219 if (DARWIN_SECTION_ANCHORS && flag_section_anchors
1220 && TREE_CODE (exp) == STRING_CST
1221 && TREE_STRING_LENGTH (exp) == 0)
1222 return darwin_sections[zobj_const_section];
1224 return readonly_data_section;
1227 #ifndef HAVE_GAS_LITERAL16
1228 #define HAVE_GAS_LITERAL16 0
1229 #endif
1231 static section *
1232 darwin_mergeable_constant_section (tree exp,
1233 unsigned HOST_WIDE_INT align,
1234 bool zsize)
1236 enum machine_mode mode = DECL_MODE (exp);
1237 unsigned int modesize = GET_MODE_BITSIZE (mode);
1239 if (DARWIN_SECTION_ANCHORS
1240 && flag_section_anchors
1241 && zsize)
1242 return darwin_sections[zobj_const_section];
1244 if (flag_merge_constants
1245 && mode != VOIDmode
1246 && mode != BLKmode
1247 && modesize <= align
1248 && align >= 8
1249 && align <= 256
1250 && (align & (align -1)) == 0)
1252 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1254 if (TREE_CODE (size) == INTEGER_CST
1255 && TREE_INT_CST_LOW (size) == 4
1256 && TREE_INT_CST_HIGH (size) == 0)
1257 return darwin_sections[literal4_section];
1258 else if (TREE_CODE (size) == INTEGER_CST
1259 && TREE_INT_CST_LOW (size) == 8
1260 && TREE_INT_CST_HIGH (size) == 0)
1261 return darwin_sections[literal8_section];
1262 else if (HAVE_GAS_LITERAL16
1263 && TARGET_64BIT
1264 && TREE_CODE (size) == INTEGER_CST
1265 && TREE_INT_CST_LOW (size) == 16
1266 && TREE_INT_CST_HIGH (size) == 0)
1267 return darwin_sections[literal16_section];
1268 else
1269 return readonly_data_section;
1272 return readonly_data_section;
1275 section *
1276 darwin_tm_clone_table_section (void)
1278 return get_named_section (NULL,
1279 "__DATA,__tm_clone_table,regular,no_dead_strip",
1284 machopic_reloc_rw_mask (void)
1286 return MACHOPIC_INDIRECT ? 3 : 0;
1289 /* We have to deal with ObjC/C++ metadata section placement in the common
1290 code, since it will also be called from LTO.
1292 Return metadata attributes, if present (searching for ABI=2 first)
1293 Return NULL_TREE if no such attributes are found. */
1295 static tree
1296 is_objc_metadata (tree decl)
1298 if (DECL_P (decl)
1299 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1300 && DECL_ATTRIBUTES (decl))
1302 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1303 if (meta)
1304 return meta;
1305 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1306 if (meta)
1307 return meta;
1309 return NULL_TREE;
1312 /* Return the section required for Objective C ABI 2 metadata. */
1313 static section *
1314 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1316 const char *p;
1317 tree ident = TREE_VALUE (meta);
1318 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1319 p = IDENTIFIER_POINTER (ident);
1321 /* If we are in LTO, then we don't know the state of flag_next_runtime
1322 or flag_objc_abi when the code was generated. We set these from the
1323 meta-data - which is needed to deal with const string constructors. */
1325 flag_next_runtime = 1;
1326 flag_objc_abi = 2;
1328 if (base == data_section)
1329 base = darwin_sections[objc2_metadata_section];
1331 /* Most of the OBJC2 META-data end up in the base section, so check it
1332 first. */
1333 if (!strncmp (p, "V2_BASE", 7))
1334 return base;
1335 else if (!strncmp (p, "V2_STRG", 7))
1336 return darwin_sections[cstring_section];
1338 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
1339 return darwin_sections[objc2_classdefs_section];
1340 else if (!strncmp (p, "V2_MREF", 7))
1341 return darwin_sections[objc2_message_refs_section];
1342 else if (!strncmp (p, "V2_CLRF", 7))
1343 return darwin_sections[objc2_classrefs_section];
1344 else if (!strncmp (p, "V2_SURF", 7))
1345 return darwin_sections[objc2_super_classrefs_section];
1346 else if (!strncmp (p, "V2_NLCL", 7))
1347 return darwin_sections[objc2_nonlazy_class_section];
1348 else if (!strncmp (p, "V2_CLAB", 7))
1349 return darwin_sections[objc2_classlist_section];
1350 else if (!strncmp (p, "V2_SRFS", 7))
1351 return darwin_sections[objc2_selector_refs_section];
1352 else if (!strncmp (p, "V2_NLCA", 7))
1353 return darwin_sections[objc2_nonlazy_category_section];
1354 else if (!strncmp (p, "V2_CALA", 7))
1355 return darwin_sections[objc2_categorylist_section];
1357 else if (!strncmp (p, "V2_PLST", 7))
1358 return darwin_sections[objc2_protocollist_section];
1359 else if (!strncmp (p, "V2_PRFS", 7))
1360 return darwin_sections[objc2_protocolrefs_section];
1362 else if (!strncmp (p, "V2_INFO", 7))
1363 return darwin_sections[objc2_image_info_section];
1365 else if (!strncmp (p, "V2_EHTY", 7))
1366 return darwin_sections[data_coal_section];
1368 else if (!strncmp (p, "V2_CSTR", 7))
1369 return darwin_sections[objc2_constant_string_object_section];
1371 /* Not recognized, default. */
1372 return base;
1375 /* Return the section required for Objective C ABI 0/1 metadata. */
1376 static section *
1377 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1379 const char *p;
1380 tree ident = TREE_VALUE (meta);
1381 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1382 p = IDENTIFIER_POINTER (ident);
1384 /* If we are in LTO, then we don't know the state of flag_next_runtime
1385 or flag_objc_abi when the code was generated. We set these from the
1386 meta-data - which is needed to deal with const string constructors. */
1387 flag_next_runtime = 1;
1388 if (!global_options_set.x_flag_objc_abi)
1389 flag_objc_abi = 1;
1391 /* String sections first, cos there are lots of strings. */
1392 if (!strncmp (p, "V1_STRG", 7))
1393 return darwin_sections[cstring_section];
1394 else if (!strncmp (p, "V1_CLSN", 7))
1395 return darwin_sections[objc_class_names_section];
1396 else if (!strncmp (p, "V1_METN", 7))
1397 return darwin_sections[objc_meth_var_names_section];
1398 else if (!strncmp (p, "V1_METT", 7))
1399 return darwin_sections[objc_meth_var_types_section];
1401 else if (!strncmp (p, "V1_CLAS", 7))
1402 return darwin_sections[objc_class_section];
1403 else if (!strncmp (p, "V1_META", 7))
1404 return darwin_sections[objc_meta_class_section];
1405 else if (!strncmp (p, "V1_CATG", 7))
1406 return darwin_sections[objc_category_section];
1407 else if (!strncmp (p, "V1_PROT", 7))
1408 return darwin_sections[objc_protocol_section];
1410 else if (!strncmp (p, "V1_CLCV", 7))
1411 return darwin_sections[objc_class_vars_section];
1412 else if (!strncmp (p, "V1_CLIV", 7))
1413 return darwin_sections[objc_instance_vars_section];
1415 else if (!strncmp (p, "V1_CLCM", 7))
1416 return darwin_sections[objc_cls_meth_section];
1417 else if (!strncmp (p, "V1_CLIM", 7))
1418 return darwin_sections[objc_inst_meth_section];
1419 else if (!strncmp (p, "V1_CACM", 7))
1420 return darwin_sections[objc_cat_cls_meth_section];
1421 else if (!strncmp (p, "V1_CAIM", 7))
1422 return darwin_sections[objc_cat_inst_meth_section];
1423 else if (!strncmp (p, "V1_PNSM", 7))
1424 return darwin_sections[objc_cat_inst_meth_section];
1425 else if (!strncmp (p, "V1_PCLM", 7))
1426 return darwin_sections[objc_cat_cls_meth_section];
1428 else if (!strncmp (p, "V1_CLPR", 7))
1429 return darwin_sections[objc_cat_cls_meth_section];
1430 else if (!strncmp (p, "V1_CAPR", 7))
1431 return darwin_sections[objc_category_section]; /* ??? CHECK me. */
1433 else if (!strncmp (p, "V1_PRFS", 7))
1434 return darwin_sections[objc_cat_cls_meth_section];
1435 else if (!strncmp (p, "V1_CLRF", 7))
1436 return darwin_sections[objc_cls_refs_section];
1437 else if (!strncmp (p, "V1_SRFS", 7))
1438 return darwin_sections[objc_selector_refs_section];
1440 else if (!strncmp (p, "V1_MODU", 7))
1441 return darwin_sections[objc_module_info_section];
1442 else if (!strncmp (p, "V1_SYMT", 7))
1443 return darwin_sections[objc_symbols_section];
1444 else if (!strncmp (p, "V1_INFO", 7))
1445 return darwin_sections[objc_image_info_section];
1447 else if (!strncmp (p, "V1_PLST", 7))
1448 return darwin_sections[objc1_prop_list_section];
1449 else if (!strncmp (p, "V1_PEXT", 7))
1450 return darwin_sections[objc1_protocol_ext_section];
1451 else if (!strncmp (p, "V1_CEXT", 7))
1452 return darwin_sections[objc1_class_ext_section];
1454 else if (!strncmp (p, "V2_CSTR", 7))
1455 return darwin_sections[objc_constant_string_object_section];
1457 return base;
1460 section *
1461 machopic_select_section (tree decl,
1462 int reloc,
1463 unsigned HOST_WIDE_INT align)
1465 bool zsize, one, weak, ro;
1466 section *base_section = NULL;
1468 weak = (DECL_P (decl)
1469 && DECL_WEAK (decl)
1470 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
1472 zsize = (DECL_P (decl)
1473 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1474 && tree_low_cst (DECL_SIZE_UNIT (decl), 1) == 0);
1476 one = DECL_P (decl)
1477 && TREE_CODE (decl) == VAR_DECL
1478 && DECL_ONE_ONLY (decl);
1480 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
1482 switch (categorize_decl_for_section (decl, reloc))
1484 case SECCAT_TEXT:
1485 gcc_unreachable ();
1486 break;
1488 case SECCAT_RODATA:
1489 case SECCAT_SRODATA:
1490 base_section = darwin_rodata_section (weak, zsize);
1491 break;
1493 case SECCAT_RODATA_MERGE_STR:
1494 base_section = darwin_mergeable_string_section (decl, align);
1495 break;
1497 case SECCAT_RODATA_MERGE_STR_INIT:
1498 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1499 break;
1501 case SECCAT_RODATA_MERGE_CONST:
1502 base_section = darwin_mergeable_constant_section (decl, align, zsize);
1503 break;
1505 case SECCAT_DATA:
1506 case SECCAT_DATA_REL:
1507 case SECCAT_DATA_REL_LOCAL:
1508 case SECCAT_DATA_REL_RO:
1509 case SECCAT_DATA_REL_RO_LOCAL:
1510 case SECCAT_SDATA:
1511 case SECCAT_TDATA:
1512 if (weak || one)
1514 if (ro)
1515 base_section = darwin_sections[const_data_coal_section];
1516 else
1517 base_section = darwin_sections[data_coal_section];
1519 else if (DARWIN_SECTION_ANCHORS
1520 && flag_section_anchors
1521 && zsize)
1523 /* If we're doing section anchors, then punt zero-sized objects into
1524 their own sections so that they don't interfere with offset
1525 computation for the remaining vars. This does not need to be done
1526 for stuff in mergeable sections, since these are ineligible for
1527 anchors. */
1528 if (ro)
1529 base_section = darwin_sections[zobj_const_data_section];
1530 else
1531 base_section = darwin_sections[zobj_data_section];
1533 else if (ro)
1534 base_section = darwin_sections[const_data_section];
1535 else
1536 base_section = data_section;
1537 break;
1538 case SECCAT_BSS:
1539 case SECCAT_SBSS:
1540 case SECCAT_TBSS:
1541 if (weak || one)
1542 base_section = darwin_sections[data_coal_section];
1543 else
1545 if (!TREE_PUBLIC (decl))
1546 base_section = lcomm_section;
1547 else if (bss_noswitch_section)
1548 base_section = bss_noswitch_section;
1549 else
1550 base_section = data_section;
1552 break;
1554 default:
1555 gcc_unreachable ();
1558 /* Darwin weird special cases.
1559 a) OBJC Meta-data. */
1560 if (DECL_P (decl)
1561 && (TREE_CODE (decl) == VAR_DECL
1562 || TREE_CODE (decl) == CONST_DECL)
1563 && DECL_ATTRIBUTES (decl))
1565 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1566 if (meta)
1567 return darwin_objc2_section (decl, meta, base_section);
1568 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1569 if (meta)
1570 return darwin_objc1_section (decl, meta, base_section);
1571 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
1572 if (meta)
1573 return base_section; /* GNU runtime is happy with it all in one pot. */
1576 /* b) Constant string objects. */
1577 if (TREE_CODE (decl) == CONSTRUCTOR
1578 && TREE_TYPE (decl)
1579 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1580 && TYPE_NAME (TREE_TYPE (decl)))
1582 tree name = TYPE_NAME (TREE_TYPE (decl));
1583 if (TREE_CODE (name) == TYPE_DECL)
1584 name = DECL_NAME (name);
1586 /* FIXME: This is unsatisfactory for LTO, since it relies on other
1587 metadata determining the source FE. */
1588 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1590 if (flag_next_runtime)
1592 if (flag_objc_abi == 2)
1593 return darwin_sections[objc2_constant_string_object_section];
1594 else
1595 return darwin_sections[objc_constant_string_object_section];
1597 else
1598 return darwin_sections[objc_string_object_section];
1600 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
1601 return darwin_sections[cfstring_constant_object_section];
1602 else
1603 return base_section;
1605 /* c) legacy meta-data selection. */
1606 else if (TREE_CODE (decl) == VAR_DECL
1607 && DECL_NAME (decl)
1608 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1609 && IDENTIFIER_POINTER (DECL_NAME (decl))
1610 && flag_next_runtime
1611 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1613 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1614 static bool warned_objc_46 = false;
1615 /* We shall assert that zero-sized objects are an error in ObjC
1616 meta-data. */
1617 gcc_assert (tree_low_cst (DECL_SIZE_UNIT (decl), 1) != 0);
1619 /* ??? This mechanism for determining the metadata section is
1620 broken when LTO is in use, since the frontend that generated
1621 the data is not identified. We will keep the capability for
1622 the short term - in case any non-Objective-C programs are using
1623 it to place data in specified sections. */
1624 if (!warned_objc_46)
1626 location_t loc = DECL_SOURCE_LOCATION (decl);
1627 warning_at (loc, 0, "the use of _OBJC_-prefixed variable names"
1628 " to select meta-data sections is deprecated at 4.6"
1629 " and will be removed in 4.7");
1630 warned_objc_46 = true;
1633 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1634 return darwin_sections[objc_cls_meth_section];
1635 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1636 return darwin_sections[objc_inst_meth_section];
1637 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29))
1638 return darwin_sections[objc_cat_cls_meth_section];
1639 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32))
1640 return darwin_sections[objc_cat_inst_meth_section];
1641 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1642 return darwin_sections[objc_class_vars_section];
1643 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1644 return darwin_sections[objc_instance_vars_section];
1645 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1646 return darwin_sections[objc_cat_cls_meth_section];
1647 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1648 return darwin_sections[objc_class_names_section];
1649 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1650 return darwin_sections[objc_meth_var_names_section];
1651 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1652 return darwin_sections[objc_meth_var_types_section];
1653 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1654 return darwin_sections[objc_cls_refs_section];
1655 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1656 return darwin_sections[objc_class_section];
1657 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1658 return darwin_sections[objc_meta_class_section];
1659 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1660 return darwin_sections[objc_category_section];
1661 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1662 return darwin_sections[objc_selector_refs_section];
1663 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1664 return darwin_sections[objc_selector_fixup_section];
1665 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1666 return darwin_sections[objc_symbols_section];
1667 else if (!strncmp (name, "_OBJC_MODULES", 13))
1668 return darwin_sections[objc_module_info_section];
1669 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1670 return darwin_sections[objc_image_info_section];
1671 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1672 return darwin_sections[objc_cat_inst_meth_section];
1673 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1674 return darwin_sections[objc_cat_cls_meth_section];
1675 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1676 return darwin_sections[objc_cat_cls_meth_section];
1677 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1678 return darwin_sections[objc_protocol_section];
1679 else
1680 return base_section;
1683 return base_section;
1686 /* This can be called with address expressions as "rtx".
1687 They must go in "const". */
1689 section *
1690 machopic_select_rtx_section (enum machine_mode mode, rtx x,
1691 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1693 if (GET_MODE_SIZE (mode) == 8
1694 && (GET_CODE (x) == CONST_INT
1695 || GET_CODE (x) == CONST_DOUBLE))
1696 return darwin_sections[literal8_section];
1697 else if (GET_MODE_SIZE (mode) == 4
1698 && (GET_CODE (x) == CONST_INT
1699 || GET_CODE (x) == CONST_DOUBLE))
1700 return darwin_sections[literal4_section];
1701 else if (HAVE_GAS_LITERAL16
1702 && TARGET_64BIT
1703 && GET_MODE_SIZE (mode) == 16
1704 && (GET_CODE (x) == CONST_INT
1705 || GET_CODE (x) == CONST_DOUBLE
1706 || GET_CODE (x) == CONST_VECTOR))
1707 return darwin_sections[literal16_section];
1708 else if (MACHOPIC_INDIRECT
1709 && (GET_CODE (x) == SYMBOL_REF
1710 || GET_CODE (x) == CONST
1711 || GET_CODE (x) == LABEL_REF))
1712 return darwin_sections[const_data_section];
1713 else
1714 return darwin_sections[const_section];
1717 void
1718 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1720 cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)};
1722 vec_safe_push (ctors, new_elt);
1724 if (! MACHOPIC_INDIRECT)
1725 fprintf (asm_out_file, ".reference .constructors_used\n");
1728 void
1729 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1731 cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)};
1733 vec_safe_push (dtors, new_elt);
1735 if (! MACHOPIC_INDIRECT)
1736 fprintf (asm_out_file, ".reference .destructors_used\n");
1739 static int
1740 sort_cdtor_records (const void * a, const void * b)
1742 const cdtor_record *cda = (const cdtor_record *)a;
1743 const cdtor_record *cdb = (const cdtor_record *)b;
1744 if (cda->priority > cdb->priority)
1745 return 1;
1746 if (cda->priority < cdb->priority)
1747 return -1;
1748 if (cda->position > cdb->position)
1749 return 1;
1750 if (cda->position < cdb->position)
1751 return -1;
1752 return 0;
1755 static void
1756 finalize_ctors ()
1758 unsigned int i;
1759 cdtor_record *elt;
1761 if (MACHOPIC_INDIRECT)
1762 switch_to_section (darwin_sections[mod_init_section]);
1763 else
1764 switch_to_section (darwin_sections[constructor_section]);
1766 if (vec_safe_length (ctors) > 1)
1767 ctors->qsort (sort_cdtor_records);
1768 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
1770 assemble_align (POINTER_SIZE);
1771 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1775 static void
1776 finalize_dtors ()
1778 unsigned int i;
1779 cdtor_record *elt;
1781 if (MACHOPIC_INDIRECT)
1782 switch_to_section (darwin_sections[mod_term_section]);
1783 else
1784 switch_to_section (darwin_sections[destructor_section]);
1786 if (vec_safe_length (dtors) > 1)
1787 dtors->qsort (sort_cdtor_records);
1788 FOR_EACH_VEC_SAFE_ELT (dtors, i, elt)
1790 assemble_align (POINTER_SIZE);
1791 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1795 void
1796 darwin_globalize_label (FILE *stream, const char *name)
1798 if (!!strncmp (name, "_OBJC_", 6))
1799 default_globalize_label (stream, name);
1802 /* This routine returns non-zero if 'name' starts with the special objective-c
1803 anonymous file-scope static name. It accommodates c++'s mangling of such
1804 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
1806 int
1807 darwin_label_is_anonymous_local_objc_name (const char *name)
1809 const unsigned char *p = (const unsigned char *) name;
1810 if (*p != '_')
1811 return 0;
1812 if (p[1] == 'Z' && p[2] == 'L')
1814 p += 3;
1815 while (*p >= '0' && *p <= '9')
1816 p++;
1818 return (!strncmp ((const char *)p, "_OBJC_", 6));
1821 /* LTO support for Mach-O.
1823 This version uses three mach-o sections to encapsulate the (unlimited
1824 number of) lto sections.
1826 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
1827 __GNU_LTO, __section_names contains the GNU LTO section names.
1828 __GNU_LTO, __section_index contains an array of values that index these.
1830 Indexed thus:
1831 <section offset from the start of __GNU_LTO, __lto_sections>,
1832 <section length>
1833 <name offset from the start of __GNU_LTO, __section_names,
1834 <name length>.
1836 At present, for both m32 and m64 mach-o files each of these fields is
1837 represented by a uint32_t. This is because, AFAICT, a mach-o object
1838 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1840 uint32_t offset;
1841 "offset An integer specifying the offset to this section in the file." */
1843 /* Count lto section numbers. */
1844 static unsigned int lto_section_num = 0;
1846 /* A vector of information about LTO sections, at present, we only have
1847 the name. TODO: see if we can get the data length somehow. */
1848 typedef struct GTY (()) darwin_lto_section_e {
1849 const char *sectname;
1850 } darwin_lto_section_e ;
1852 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
1854 /* Segment for LTO data. */
1855 #define LTO_SEGMENT_NAME "__GNU_LTO"
1857 /* Section wrapper scheme (used here to wrap the unlimited number of LTO
1858 sections into three Mach-O ones).
1859 NOTE: These names MUST be kept in sync with those in
1860 libiberty/simple-object-mach-o. */
1861 #define LTO_SECTS_SECTION "__wrapper_sects"
1862 #define LTO_NAMES_SECTION "__wrapper_names"
1863 #define LTO_INDEX_SECTION "__wrapper_index"
1865 /* File to temporarily store LTO data. This is appended to asm_out_file
1866 in darwin_end_file. */
1867 static FILE *lto_asm_out_file, *saved_asm_out_file;
1868 static char *lto_asm_out_name;
1870 /* Prepare asm_out_file for LTO output. For darwin, this means hiding
1871 asm_out_file and switching to an alternative output file. */
1872 void
1873 darwin_asm_lto_start (void)
1875 gcc_assert (! saved_asm_out_file);
1876 saved_asm_out_file = asm_out_file;
1877 if (! lto_asm_out_name)
1878 lto_asm_out_name = make_temp_file (".lto.s");
1879 lto_asm_out_file = fopen (lto_asm_out_name, "a");
1880 if (lto_asm_out_file == NULL)
1881 fatal_error ("failed to open temporary file %s for LTO output",
1882 lto_asm_out_name);
1883 asm_out_file = lto_asm_out_file;
1886 /* Restore asm_out_file. */
1887 void
1888 darwin_asm_lto_end (void)
1890 gcc_assert (saved_asm_out_file);
1891 fclose (lto_asm_out_file);
1892 asm_out_file = saved_asm_out_file;
1893 saved_asm_out_file = NULL;
1896 static void
1897 darwin_asm_dwarf_section (const char *name, unsigned int flags, tree decl);
1899 /* Called for the TARGET_ASM_NAMED_SECTION hook. */
1901 void
1902 darwin_asm_named_section (const char *name,
1903 unsigned int flags,
1904 tree decl ATTRIBUTE_UNUSED)
1906 /* LTO sections go in a special section that encapsulates the (unlimited)
1907 number of GNU LTO sections within a single mach-o one. */
1908 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1909 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1911 darwin_lto_section_e e;
1912 /* We expect certain flags to be set... */
1913 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1914 == (SECTION_DEBUG | SECTION_NAMED));
1916 /* Switch to our combined section. */
1917 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
1918 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
1919 /* Output a label for the start of this sub-section. */
1920 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
1921 lto_section_num, name);
1922 /* We have to jump through hoops to get the values of the intra-section
1923 offsets... */
1924 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
1925 lto_section_num, lto_section_num);
1926 fprintf (asm_out_file,
1927 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
1928 lto_section_num, lto_section_num+1, lto_section_num);
1929 lto_section_num++;
1930 e.sectname = xstrdup (name);
1931 /* Keep the names, we'll need to make a table later.
1932 TODO: check that we do not revisit sections, that would break
1933 the assumption of how this is done. */
1934 if (lto_section_names == NULL)
1935 vec_alloc (lto_section_names, 16);
1936 vec_safe_push (lto_section_names, e);
1938 else if (strncmp (name, "__DWARF,", 8) == 0)
1939 darwin_asm_dwarf_section (name, flags, decl);
1940 else
1941 fprintf (asm_out_file, "\t.section %s\n", name);
1944 void
1945 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
1947 /* Darwin does not use unique sections. */
1950 /* Handle __attribute__ ((apple_kext_compatibility)).
1951 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
1952 vtable for classes with this attribute (and their descendants) by not
1953 outputting the new 3.0 nondeleting destructor. This means that such
1954 objects CANNOT be allocated on the stack or as globals UNLESS they have
1955 a completely empty `operator delete'.
1956 Luckily, this fits in with the Darwin kext model.
1958 This attribute also disables gcc3's potential overlaying of derived
1959 class data members on the padding at the end of the base class. */
1961 tree
1962 darwin_handle_kext_attribute (tree *node, tree name,
1963 tree args ATTRIBUTE_UNUSED,
1964 int flags ATTRIBUTE_UNUSED,
1965 bool *no_add_attrs)
1967 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
1968 if (! TARGET_KEXTABI)
1970 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
1971 "only when compiling a kext", name);
1973 *no_add_attrs = true;
1975 else if (TREE_CODE (*node) != RECORD_TYPE)
1977 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
1978 "only to C++ classes", name);
1980 *no_add_attrs = true;
1983 return NULL_TREE;
1986 /* Handle a "weak_import" attribute; arguments as in
1987 struct attribute_spec.handler. */
1989 tree
1990 darwin_handle_weak_import_attribute (tree *node, tree name,
1991 tree ARG_UNUSED (args),
1992 int ARG_UNUSED (flags),
1993 bool * no_add_attrs)
1995 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
1997 warning (OPT_Wattributes, "%qE attribute ignored",
1998 name);
1999 *no_add_attrs = true;
2001 else
2002 declare_weak (*node);
2004 return NULL_TREE;
2007 /* Emit a label for an FDE, making it global and/or weak if appropriate.
2008 The third parameter is nonzero if this is for exception handling.
2009 The fourth parameter is nonzero if this is just a placeholder for an
2010 FDE that we are omitting. */
2012 void
2013 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
2015 char *lab ;
2016 char buf[32];
2017 static int invok_count = 0;
2018 static tree last_fun_decl = NULL_TREE;
2020 /* We use the linker to emit the .eh labels for Darwin 9 and above. */
2021 if (! for_eh || generating_for_darwin_version >= 9)
2022 return;
2024 /* FIXME: This only works when the eh for all sections of a function is
2025 emitted at the same time. If that changes, we would need to use a lookup
2026 table of some form to determine what to do. Also, we should emit the
2027 unadorned label for the partition containing the public label for a
2028 function. This is of limited use, probably, since we do not currently
2029 enable partitioning. */
2030 strcpy (buf, ".eh");
2031 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
2033 if (decl == last_fun_decl)
2035 invok_count++;
2036 snprintf (buf, 31, "$$part$$%d.eh", invok_count);
2038 else
2040 last_fun_decl = decl;
2041 invok_count = 0;
2045 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
2047 if (TREE_PUBLIC (decl))
2049 targetm.asm_out.globalize_label (file, lab);
2050 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
2052 fputs ("\t.private_extern ", file);
2053 assemble_name (file, lab);
2054 fputc ('\n', file);
2058 if (DECL_WEAK (decl))
2060 fputs ("\t.weak_definition ", file);
2061 assemble_name (file, lab);
2062 fputc ('\n', file);
2065 assemble_name (file, lab);
2066 if (empty)
2068 fputs (" = 0\n", file);
2070 /* Mark the absolute .eh and .eh1 style labels as needed to
2071 ensure that we don't dead code strip them and keep such
2072 labels from another instantiation point until we can fix this
2073 properly with group comdat support. */
2074 darwin_mark_decl_preserved (lab);
2076 else
2077 fputs (":\n", file);
2079 free (lab);
2082 static GTY(()) unsigned long except_table_label_num;
2084 void
2085 darwin_emit_except_table_label (FILE *file)
2087 char section_start_label[30];
2089 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
2090 except_table_label_num++);
2091 ASM_OUTPUT_LABEL (file, section_start_label);
2093 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
2095 void
2096 darwin_non_lazy_pcrel (FILE *file, rtx addr)
2098 const char *nlp_name;
2100 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2102 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2103 fputs ("\t.long\t", file);
2104 ASM_OUTPUT_LABELREF (file, nlp_name);
2105 fputs ("-.", file);
2108 /* If this is uncommented, details of each allocation will be printed
2109 in the asm right before the actual code. WARNING - this will cause some
2110 test-suite fails (since the printout will contain items that some tests
2111 are not expecting) -- so don't leave it on by default (it bloats the
2112 asm too). */
2113 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
2115 /* The first two of these routines are ostensibly just intended to put
2116 names into the asm. However, they are both hijacked in order to ensure
2117 that zero-sized items do not make their way into the output. Consequently,
2118 we also need to make these participate in provisions for dealing with
2119 such items in section anchors. */
2121 /* The implementation of ASM_DECLARE_OBJECT_NAME. */
2122 /* The RTTI data (e.g., __ti4name) is common and public (and static),
2123 but it does need to be referenced via indirect PIC data pointers.
2124 The machopic_define_symbol calls are telling the machopic subsystem
2125 that the name *is* defined in this module, so it doesn't need to
2126 make them indirect. */
2127 void
2128 darwin_asm_declare_object_name (FILE *file,
2129 const char *nam, tree decl)
2131 const char *xname = nam;
2132 unsigned HOST_WIDE_INT size;
2133 bool local_def, weak;
2135 weak = (DECL_P (decl)
2136 && DECL_WEAK (decl)
2137 && !lookup_attribute ("weak_import",
2138 DECL_ATTRIBUTES (decl)));
2140 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
2141 && (!DECL_COMMON (decl)
2142 || !TREE_PUBLIC (decl)));
2144 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
2145 xname = IDENTIFIER_POINTER (DECL_NAME (decl));
2147 if (local_def)
2149 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2150 if (!weak)
2151 machopic_define_symbol (DECL_RTL (decl));
2154 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
2156 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2157 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
2158 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
2159 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
2160 (unsigned long long)size, DECL_ALIGN (decl), local_def,
2161 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2162 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
2163 (unsigned long)DECL_INITIAL (decl));
2164 #endif
2166 /* Darwin needs help to support local zero-sized objects.
2167 They must be made at least one byte, and the section containing must be
2168 marked as unsuitable for section-anchors (see storage allocators below).
2170 For non-zero objects this output is handled by varasm.c.
2172 if (!size)
2174 unsigned int l2align = 0;
2176 /* The align must be honored, even for zero-sized. */
2177 if (DECL_ALIGN (decl))
2179 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
2180 fprintf (file, "\t.align\t%u\n", l2align);
2183 ASM_OUTPUT_LABEL (file, xname);
2184 size = 1;
2185 fprintf (file, "\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2187 /* Check that we've correctly picked up the zero-sized item and placed it
2188 properly. */
2189 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2190 || (in_section
2191 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2193 else
2194 ASM_OUTPUT_LABEL (file, xname);
2197 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
2198 void
2199 darwin_asm_declare_constant_name (FILE *file, const char *name,
2200 const_tree exp ATTRIBUTE_UNUSED,
2201 HOST_WIDE_INT size)
2203 assemble_label (file, name);
2204 /* As for other items, we need at least one byte. */
2205 if (!size)
2207 fputs ("\t.space\t1\n", file);
2208 /* Check that we've correctly picked up the zero-sized item and placed it
2209 properly. */
2210 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2211 || (in_section
2212 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2216 /* Darwin storage allocators.
2218 Zerofill sections are desirable for large blank data since, otherwise, these
2219 data bloat objects (PR33210).
2221 However, section anchors don't work in .zerofill sections (one cannot switch
2222 to a zerofill section). Ergo, for Darwin targets using section anchors we need
2223 to put (at least some) data into 'normal' switchable sections.
2225 Here we set a relatively arbitrary value for the size of an object to trigger
2226 zerofill when section anchors are enabled (anything bigger than a page for
2227 current Darwin implementations). FIXME: there ought to be some objective way
2228 to make this choice.
2230 When section anchor are off this is ignored anyway. */
2232 #define BYTES_ZFILL 4096
2234 /* Emit a chunk of data for items coalesced by the linker. */
2235 static void
2236 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
2237 unsigned HOST_WIDE_INT size,
2238 unsigned int align)
2240 /* Since the sections used here are coalesed, they will not be eligible
2241 for section anchors, and therefore we don't need to break that out. */
2242 if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
2243 switch_to_section (darwin_sections[const_data_coal_section]);
2244 else
2245 switch_to_section (darwin_sections[data_coal_section]);
2247 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
2248 the align info for zero-sized items... but do it here otherwise. */
2249 if (size && align)
2250 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2252 if (TREE_PUBLIC (decl))
2253 darwin_globalize_label (fp, name);
2255 /* ... and we let it deal with outputting one byte of zero for them too. */
2256 darwin_asm_declare_object_name (fp, name, decl);
2257 if (size)
2258 assemble_zeros (size);
2261 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
2262 static void
2263 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
2264 unsigned HOST_WIDE_INT size,
2265 unsigned int align, tree meta)
2267 section *ocs = data_section;
2269 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
2270 ocs = darwin_objc2_section (decl, meta, ocs);
2271 else
2272 ocs = darwin_objc1_section (decl, meta, ocs);
2274 switch_to_section (ocs);
2276 /* We shall declare that zero-sized meta-data are not valid (yet). */
2277 gcc_assert (size);
2278 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2280 /* ... and we let it deal with outputting one byte of zero for them too. */
2281 darwin_asm_declare_object_name (fp, name, decl);
2282 assemble_zeros (size);
2285 /* This routine emits 'local' storage:
2287 When Section Anchors are off this routine emits .zerofill commands in
2288 sections named for their alignment.
2290 When Section Anchors are on, smaller (non-zero-sized) items are placed in
2291 the .static_data section so that the section anchoring system can see them.
2292 Larger items are still placed in .zerofill sections, addressing PR33210.
2293 The routine has no checking - it is all assumed to be done by the caller.
2295 static void
2296 darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
2297 unsigned HOST_WIDE_INT size,
2298 unsigned int l2align)
2300 /* FIXME: We have a fudge to make this work with Java even when the target does
2301 not use sections anchors -- Java seems to need at least one small item in a
2302 non-zerofill segment. */
2303 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2304 || (size && size <= 2))
2306 /* Put smaller objects in _static_data, where the section anchors system
2307 can get them.
2308 However, if they are zero-sized punt them to yet a different section
2309 (that is not allowed to participate in anchoring). */
2310 if (!size)
2312 fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
2313 in_section = darwin_sections[zobj_bss_section];
2314 size = 1;
2316 else
2318 fputs ("\t.static_data\n", fp);
2319 in_section = darwin_sections[static_data_section];
2322 if (l2align)
2323 fprintf (fp, "\t.align\t%u\n", l2align);
2325 assemble_name (fp, name);
2326 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2328 else
2330 /* When we are on a non-section anchor target, we can get zero-sized
2331 items here. However, all we need to do is to bump them to one byte
2332 and the section alignment will take care of the rest. */
2333 char secnam[64];
2334 unsigned int flags ;
2335 snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"),
2336 (unsigned) l2align);
2337 /* We can't anchor (yet, if ever) in zerofill sections, because we can't
2338 switch to them and emit a label. */
2339 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2340 in_section = get_section (secnam, flags, NULL);
2341 fprintf (fp, "\t.zerofill %s,", secnam);
2342 assemble_name (fp, name);
2343 if (!size)
2344 size = 1;
2346 if (l2align)
2347 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2348 size, (unsigned) l2align);
2349 else
2350 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2353 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2354 /* This is defined as a file-scope var, so we know to notify machopic. */
2355 machopic_define_symbol (DECL_RTL (decl));
2358 /* Emit a chunk of common. */
2359 static void
2360 darwin_emit_common (FILE *fp, const char *name,
2361 unsigned HOST_WIDE_INT size, unsigned int align)
2363 unsigned HOST_WIDE_INT rounded;
2364 unsigned int l2align;
2366 /* Earlier systems complain if the alignment exceeds the page size.
2367 The magic number is 4096 * 8 - hard-coded for legacy systems. */
2368 if (!emit_aligned_common && (align > 32768UL))
2369 align = 4096UL; /* In units. */
2370 else
2371 align /= BITS_PER_UNIT;
2373 /* Make sure we have a meaningful align. */
2374 if (!align)
2375 align = 1;
2377 /* For earlier toolchains, we need to emit the var as a rounded size to
2378 tell ld the alignment. */
2379 if (size < align)
2380 rounded = align;
2381 else
2382 rounded = (size + (align-1)) & ~(align-1);
2384 l2align = floor_log2 (align);
2385 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2387 in_section = comm_section;
2388 /* We mustn't allow multiple public symbols to share an address when using
2389 the normal OSX toolchain. */
2390 if (!size)
2392 /* Put at least one byte. */
2393 size = 1;
2394 /* This section can no longer participate in section anchoring. */
2395 comm_section->common.flags |= SECTION_NO_ANCHOR;
2398 fputs ("\t.comm\t", fp);
2399 assemble_name (fp, name);
2400 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
2401 emit_aligned_common?size:rounded);
2402 if (l2align && emit_aligned_common)
2403 fprintf (fp, ",%u", l2align);
2404 fputs ("\n", fp);
2407 /* Output a var which is all zero - into aligned BSS sections, common, lcomm
2408 or coalescable data sections (for weak or comdat) as appropriate. */
2410 void
2411 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
2412 unsigned HOST_WIDE_INT size, unsigned int align)
2414 unsigned int l2align;
2415 bool one, pub, weak;
2416 tree meta;
2418 pub = TREE_PUBLIC (decl);
2419 one = DECL_ONE_ONLY (decl);
2420 weak = (DECL_P (decl)
2421 && DECL_WEAK (decl)
2422 && !lookup_attribute ("weak_import",
2423 DECL_ATTRIBUTES (decl)));
2425 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2426 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
2427 " pub %d weak %d one %d init %lx\n",
2428 name, (long long)size, (int)align, TREE_READONLY (decl),
2429 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2430 pub, weak, one, (unsigned long)DECL_INITIAL (decl));
2431 #endif
2433 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2434 before the target has a chance to comment. */
2435 if ((meta = is_objc_metadata (decl)))
2437 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2438 return;
2441 /* Check that any initializer is valid. */
2442 gcc_assert ((DECL_INITIAL (decl) == NULL)
2443 || (DECL_INITIAL (decl) == error_mark_node)
2444 || initializer_zerop (DECL_INITIAL (decl)));
2446 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2447 gcc_assert (!DECL_COMMON (decl));
2449 /* Pick up the correct alignment. */
2450 if (!size || !align)
2451 align = DECL_ALIGN (decl);
2453 l2align = floor_log2 (align / BITS_PER_UNIT);
2454 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2456 last_assemble_variable_decl = decl;
2458 /* We would rather not have to check this here - but it seems that we might
2459 be passed a decl that should be in coalesced space. */
2460 if (one || weak)
2462 /* Weak or COMDAT objects are put in mergeable sections. */
2463 darwin_emit_weak_or_comdat (fp, decl, name, size,
2464 DECL_ALIGN (decl));
2465 return;
2468 /* If this is not public, then emit according to local rules. */
2469 if (!pub)
2471 darwin_emit_local_bss (fp, decl, name, size, l2align);
2472 return;
2475 /* So we have a public symbol (small item fudge for Java, see above). */
2476 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2477 || (size && size <= 2))
2479 /* Put smaller objects in data, where the section anchors system can get
2480 them. However, if they are zero-sized punt them to yet a different
2481 section (that is not allowed to participate in anchoring). */
2482 if (!size)
2484 fputs ("\t.section\t__DATA,__zobj_data\n", fp);
2485 in_section = darwin_sections[zobj_data_section];
2486 size = 1;
2488 else
2490 fputs ("\t.data\n", fp);
2491 in_section = data_section;
2494 if (l2align)
2495 fprintf (fp, "\t.align\t%u\n", l2align);
2497 assemble_name (fp, name);
2498 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2500 else
2502 char secnam[64];
2503 unsigned int flags ;
2504 /* When we are on a non-section anchor target, we can get zero-sized
2505 items here. However, all we need to do is to bump them to one byte
2506 and the section alignment will take care of the rest. */
2507 snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align);
2509 /* We can't anchor in zerofill sections, because we can't switch
2510 to them and emit a label. */
2511 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2512 in_section = get_section (secnam, flags, NULL);
2513 fprintf (fp, "\t.zerofill %s,", secnam);
2514 assemble_name (fp, name);
2515 if (!size)
2516 size = 1;
2518 if (l2align)
2519 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
2520 else
2521 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2523 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2526 /* Output a chunk of common, with alignment specified (where the target
2527 supports this). */
2528 void
2529 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
2530 unsigned HOST_WIDE_INT size,
2531 unsigned int align)
2533 unsigned int l2align;
2534 bool one, weak;
2535 tree meta;
2537 /* No corresponding var. */
2538 if (decl==NULL)
2540 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2541 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
2542 #endif
2543 darwin_emit_common (fp, name, size, align);
2544 return;
2547 one = DECL_ONE_ONLY (decl);
2548 weak = (DECL_P (decl)
2549 && DECL_WEAK (decl)
2550 && !lookup_attribute ("weak_import",
2551 DECL_ATTRIBUTES (decl)));
2553 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2554 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
2555 " weak %d one %d init %lx\n",
2556 name, (long long)size, (int)align, TREE_READONLY (decl),
2557 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2558 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
2559 #endif
2561 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2562 before the target has a chance to comment. */
2563 if ((meta = is_objc_metadata (decl)))
2565 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2566 return;
2569 /* We shouldn't be messing with this if the decl has a section name. */
2570 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2572 /* We would rather not have to check this here - but it seems that we might
2573 be passed a decl that should be in coalesced space. */
2574 if (one || weak)
2576 /* Weak or COMDAT objects are put in mergable sections. */
2577 darwin_emit_weak_or_comdat (fp, decl, name, size,
2578 DECL_ALIGN (decl));
2579 return;
2582 /* We should only get here for DECL_COMMON, with a zero init (and, in
2583 principle, only for public symbols too - although we deal with local
2584 ones below). */
2586 /* Check the initializer is OK. */
2587 gcc_assert (DECL_COMMON (decl)
2588 && ((DECL_INITIAL (decl) == NULL)
2589 || (DECL_INITIAL (decl) == error_mark_node)
2590 || initializer_zerop (DECL_INITIAL (decl))));
2592 last_assemble_variable_decl = decl;
2594 if (!size || !align)
2595 align = DECL_ALIGN (decl);
2597 l2align = floor_log2 (align / BITS_PER_UNIT);
2598 /* Check we aren't asking for more aligment than the platform allows. */
2599 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2601 if (TREE_PUBLIC (decl) != 0)
2602 darwin_emit_common (fp, name, size, align);
2603 else
2604 darwin_emit_local_bss (fp, decl, name, size, l2align);
2607 /* Output a chunk of BSS with alignment specfied. */
2608 void
2609 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
2610 unsigned HOST_WIDE_INT size,
2611 unsigned int align)
2613 unsigned long l2align;
2614 bool one, weak;
2615 tree meta;
2617 one = DECL_ONE_ONLY (decl);
2618 weak = (DECL_P (decl)
2619 && DECL_WEAK (decl)
2620 && !lookup_attribute ("weak_import",
2621 DECL_ATTRIBUTES (decl)));
2623 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2624 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
2625 " weak %d init %lx\n",
2626 name, (long long)size, (int)align, TREE_READONLY (decl),
2627 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
2628 weak , (unsigned long)DECL_INITIAL (decl));
2629 #endif
2631 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2632 before the target has a chance to comment. */
2633 if ((meta = is_objc_metadata (decl)))
2635 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2636 return;
2639 /* We shouldn't be messing with this if the decl has a section name. */
2640 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2642 /* We would rather not have to check this here - but it seems that we might
2643 be passed a decl that should be in coalesced space. */
2644 if (one || weak)
2646 /* Weak or COMDAT objects are put in mergable sections. */
2647 darwin_emit_weak_or_comdat (fp, decl, name, size,
2648 DECL_ALIGN (decl));
2649 return;
2652 /* .. and it should be suitable for placement in local mem. */
2653 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
2654 /* .. and any initializer must be all-zero. */
2655 gcc_assert ((DECL_INITIAL (decl) == NULL)
2656 || (DECL_INITIAL (decl) == error_mark_node)
2657 || initializer_zerop (DECL_INITIAL (decl)));
2659 last_assemble_variable_decl = decl;
2661 if (!size || !align)
2662 align = DECL_ALIGN (decl);
2664 l2align = floor_log2 (align / BITS_PER_UNIT);
2665 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2667 darwin_emit_local_bss (fp, decl, name, size, l2align);
2670 /* Emit an assembler directive to set visibility for a symbol. The
2671 only supported visibilities are VISIBILITY_DEFAULT and
2672 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
2673 extern". There is no MACH-O equivalent of ELF's
2674 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
2676 void
2677 darwin_assemble_visibility (tree decl, int vis)
2679 if (vis == VISIBILITY_DEFAULT)
2681 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
2683 fputs ("\t.private_extern ", asm_out_file);
2684 assemble_name (asm_out_file,
2685 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
2686 fputs ("\n", asm_out_file);
2688 else
2689 warning (OPT_Wattributes, "protected visibility attribute "
2690 "not supported in this configuration; ignored");
2693 /* vec used by darwin_asm_dwarf_section.
2694 Maybe a hash tab would be better here - but the intention is that this is
2695 a very short list (fewer than 16 items) and each entry should (ideally,
2696 eventually) only be presented once.
2698 A structure to hold a dwarf debug section used entry. */
2700 typedef struct GTY(()) dwarf_sect_used_entry {
2701 const char *name;
2702 unsigned count;
2704 dwarf_sect_used_entry;
2707 /* A list of used __DWARF sections. */
2708 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
2710 /* This is called when we are asked to assemble a named section and the
2711 name begins with __DWARF,. We keep a list of the section names (without
2712 the __DWARF, prefix) and use this to emit our required start label on the
2713 first switch to each section. */
2715 static void
2716 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2717 tree ARG_UNUSED (decl))
2719 unsigned i;
2720 int namelen;
2721 const char * sname;
2722 dwarf_sect_used_entry *ref;
2723 bool found = false;
2724 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2725 == (SECTION_DEBUG | SECTION_NAMED));
2726 /* We know that the name starts with __DWARF, */
2727 sname = name + 8;
2728 namelen = strchr (sname, ',') - sname;
2729 gcc_assert (namelen);
2730 if (dwarf_sect_names_table == NULL)
2731 vec_alloc (dwarf_sect_names_table, 16);
2732 else
2733 for (i = 0;
2734 dwarf_sect_names_table->iterate (i, &ref);
2735 i++)
2737 if (!ref)
2738 break;
2739 if (!strcmp (ref->name, sname))
2741 found = true;
2742 ref->count++;
2743 break;
2747 fprintf (asm_out_file, "\t.section %s\n", name);
2748 if (!found)
2750 dwarf_sect_used_entry e;
2751 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
2752 e.count = 1;
2753 e.name = xstrdup (sname);
2754 vec_safe_push (dwarf_sect_names_table, e);
2758 /* Output a difference of two labels that will be an assembly time
2759 constant if the two labels are local. (.long lab1-lab2 will be
2760 very different if lab1 is at the boundary between two sections; it
2761 will be relocated according to the second section, not the first,
2762 so one ends up with a difference between labels in different
2763 sections, which is bad in the dwarf2 eh context for instance.) */
2765 static int darwin_dwarf_label_counter;
2767 void
2768 darwin_asm_output_dwarf_delta (FILE *file, int size,
2769 const char *lab1, const char *lab2)
2771 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
2772 && lab2[0] == '*' && lab2[1] == 'L');
2773 const char *directive = (size == 8 ? ".quad" : ".long");
2775 if (islocaldiff)
2776 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
2777 else
2778 fprintf (file, "\t%s\t", directive);
2780 assemble_name_raw (file, lab1);
2781 fprintf (file, "-");
2782 assemble_name_raw (file, lab2);
2783 if (islocaldiff)
2784 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
2787 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
2788 offsets are not represented using relocs in .o files; either the
2789 section never leaves the .o file, or the linker or other tool is
2790 responsible for parsing the DWARF and updating the offsets. */
2792 void
2793 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
2794 section *base)
2796 char sname[64];
2797 int namelen;
2799 gcc_assert (base->common.flags & SECTION_NAMED);
2800 gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
2801 gcc_assert (strchr (base->named.name + 8, ','));
2803 namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
2804 sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
2805 darwin_asm_output_dwarf_delta (file, size, lab, sname);
2808 /* Called from the within the TARGET_ASM_FILE_START for each target. */
2810 void
2811 darwin_file_start (void)
2813 /* Nothing to do. */
2816 /* Called for the TARGET_ASM_FILE_END hook.
2817 Emit the mach-o pic indirection data, the lto data and, finally a flag
2818 to tell the linker that it can break the file object into sections and
2819 move those around for efficiency. */
2821 void
2822 darwin_file_end (void)
2824 if (!vec_safe_is_empty (ctors))
2825 finalize_ctors ();
2826 if (!vec_safe_is_empty (dtors))
2827 finalize_dtors ();
2828 machopic_finish (asm_out_file);
2829 if (strcmp (lang_hooks.name, "GNU C++") == 0)
2831 switch_to_section (darwin_sections[constructor_section]);
2832 switch_to_section (darwin_sections[destructor_section]);
2833 ASM_OUTPUT_ALIGN (asm_out_file, 1);
2836 /* If there was LTO assembler output, append it to asm_out_file. */
2837 if (lto_asm_out_name)
2839 int n;
2840 char *buf, *lto_asm_txt;
2842 /* Shouldn't be here if we failed to switch back. */
2843 gcc_assert (! saved_asm_out_file);
2845 lto_asm_out_file = fopen (lto_asm_out_name, "r");
2846 if (lto_asm_out_file == NULL)
2847 fatal_error ("failed to open temporary file %s with LTO output",
2848 lto_asm_out_name);
2849 fseek (lto_asm_out_file, 0, SEEK_END);
2850 n = ftell (lto_asm_out_file);
2851 if (n > 0)
2853 fseek (lto_asm_out_file, 0, SEEK_SET);
2854 lto_asm_txt = buf = (char *) xmalloc (n + 1);
2855 while (fgets (lto_asm_txt, n, lto_asm_out_file))
2856 fputs (lto_asm_txt, asm_out_file);
2857 /* Put a termination label. */
2858 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2859 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2860 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
2861 lto_section_num);
2862 /* Make sure our termination label stays in this section. */
2863 fputs ("\t.space\t1\n", asm_out_file);
2866 /* Remove the temporary file. */
2867 fclose (lto_asm_out_file);
2868 unlink_if_ordinary (lto_asm_out_name);
2869 free (lto_asm_out_name);
2872 /* Output the names and indices. */
2873 if (lto_section_names && lto_section_names->length ())
2875 int count;
2876 darwin_lto_section_e *ref;
2877 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
2878 the latter up ourselves. */
2879 const char *op = integer_asm_op (4,0);
2881 /* Emit the names. */
2882 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2883 LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
2884 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2886 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
2887 /* We have to jump through hoops to get the values of the intra-section
2888 offsets... */
2889 fprintf (asm_out_file,
2890 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
2891 count, count);
2892 fprintf (asm_out_file,
2893 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
2894 count, count+1, count);
2895 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
2897 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
2898 /* make sure our termination label stays in this section. */
2899 fputs ("\t.space\t1\n", asm_out_file);
2901 /* Emit the Index. */
2902 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2903 LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
2904 fputs ("\t.align\t2\n", asm_out_file);
2905 fputs ("# Section offset, Section length, Name offset, Name length\n",
2906 asm_out_file);
2907 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2909 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
2910 op, count, ref->sectname);
2911 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
2912 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
2913 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
2917 /* If we have section anchors, then we must prevent the linker from
2918 re-arranging data. */
2919 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2920 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
2923 /* TODO: Add a language hook for identifying if a decl is a vtable. */
2924 #define DARWIN_VTABLE_P(DECL) 0
2926 /* Cross-module name binding. Darwin does not support overriding
2927 functions at dynamic-link time, except for vtables in kexts. */
2929 bool
2930 darwin_binds_local_p (const_tree decl)
2932 return default_binds_local_p_1 (decl,
2933 TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
2936 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
2937 anchor relative to ".", the current section position. We cannot use
2938 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
2939 void
2940 darwin_asm_output_anchor (rtx symbol)
2942 fprintf (asm_out_file, "\t.set\t");
2943 assemble_name (asm_out_file, XSTR (symbol, 0));
2944 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
2945 SYMBOL_REF_BLOCK_OFFSET (symbol));
2948 /* Disable section anchoring on any section containing a zero-sized
2949 object. */
2950 bool
2951 darwin_use_anchors_for_symbol_p (const_rtx symbol)
2953 if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
2955 section *sect;
2956 /* If the section contains a zero-sized object it's ineligible. */
2957 sect = SYMBOL_REF_BLOCK (symbol)->sect;
2958 /* This should have the effect of disabling anchors for vars that follow
2959 any zero-sized one, in a given section. */
2960 if (sect->common.flags & SECTION_NO_ANCHOR)
2961 return false;
2963 /* Also check the normal reasons for suppressing. */
2964 return default_use_anchors_for_symbol_p (symbol);
2966 else
2967 return false;
2970 /* Set the darwin specific attributes on TYPE. */
2971 void
2972 darwin_set_default_type_attributes (tree type)
2974 if (darwin_ms_struct
2975 && TREE_CODE (type) == RECORD_TYPE)
2976 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
2977 NULL_TREE,
2978 TYPE_ATTRIBUTES (type));
2981 /* True, iff we're generating code for loadable kernel extensions. */
2983 bool
2984 darwin_kextabi_p (void) {
2985 return flag_apple_kext;
2988 void
2989 darwin_override_options (void)
2991 /* Keep track of which (major) version we're generating code for. */
2992 if (darwin_macosx_version_min)
2994 if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
2995 generating_for_darwin_version = 10;
2996 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
2997 generating_for_darwin_version = 9;
2999 /* Earlier versions are not specifically accounted, until required. */
3002 /* In principle, this should be c-family only. However, we really need to
3003 set sensible defaults for LTO as well, since the section selection stuff
3004 should check for correctness re. the ABI. TODO: check and provide the
3005 flags (runtime & ABI) from the lto wrapper). */
3007 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
3008 if (!global_options_set.x_flag_objc_abi)
3009 global_options.x_flag_objc_abi
3010 = (!flag_next_runtime)
3012 : (TARGET_64BIT ? 2
3013 : (generating_for_darwin_version >= 9) ? 1
3014 : 0);
3016 /* Objective-C family ABI 2 is only valid for next/m64 at present. */
3017 if (global_options_set.x_flag_objc_abi && flag_next_runtime)
3019 if (TARGET_64BIT && global_options.x_flag_objc_abi < 2)
3020 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 must be"
3021 " used for %<-m64%> targets with"
3022 " %<-fnext-runtime%>");
3023 if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
3024 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 is not"
3025 " supported on %<-m32%> targets with"
3026 " %<-fnext-runtime%>");
3029 /* Don't emit DWARF3/4 unless specifically selected. This is a
3030 workaround for tool bugs. */
3031 if (!global_options_set.x_dwarf_strict)
3032 dwarf_strict = 1;
3033 if (!global_options_set.x_dwarf_version)
3034 dwarf_version = 2;
3036 /* Do not allow unwind tables to be generated by default for m32.
3037 fnon-call-exceptions will override this, regardless of what we do. */
3038 if (generating_for_darwin_version < 10
3039 && !global_options_set.x_flag_asynchronous_unwind_tables
3040 && !TARGET_64BIT)
3041 global_options.x_flag_asynchronous_unwind_tables = 0;
3043 /* Disable -freorder-blocks-and-partition when unwind tables are being
3044 emitted for Darwin < 9 (OSX 10.5).
3045 The strategy is, "Unless the User has specifically set/unset an unwind
3046 flag we will switch off -freorder-blocks-and-partition when unwind tables
3047 will be generated". If the User specifically sets flags... we assume
3048 (s)he knows why... */
3049 if (generating_for_darwin_version < 9
3050 && global_options_set.x_flag_reorder_blocks_and_partition
3051 && ((global_options.x_flag_exceptions /* User, c++, java */
3052 && !global_options_set.x_flag_exceptions) /* User specified... */
3053 || (global_options.x_flag_unwind_tables
3054 && !global_options_set.x_flag_unwind_tables)
3055 || (global_options.x_flag_non_call_exceptions
3056 && !global_options_set.x_flag_non_call_exceptions)
3057 || (global_options.x_flag_asynchronous_unwind_tables
3058 && !global_options_set.x_flag_asynchronous_unwind_tables)))
3060 inform (input_location,
3061 "-freorder-blocks-and-partition does not work with exceptions "
3062 "on this architecture");
3063 flag_reorder_blocks_and_partition = 0;
3064 flag_reorder_blocks = 1;
3067 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
3068 one valid choice of exception scheme for each runtime. */
3069 if (!global_options_set.x_flag_objc_sjlj_exceptions)
3070 global_options.x_flag_objc_sjlj_exceptions =
3071 flag_next_runtime && !TARGET_64BIT;
3073 /* FIXME: and this could be eliminated then too. */
3074 if (!global_options_set.x_flag_exceptions
3075 && flag_objc_exceptions
3076 && TARGET_64BIT)
3077 flag_exceptions = 1;
3079 if (flag_mkernel || flag_apple_kext)
3081 /* -mkernel implies -fapple-kext for C++ */
3082 if (strcmp (lang_hooks.name, "GNU C++") == 0)
3083 flag_apple_kext = 1;
3085 flag_no_common = 1;
3087 /* No EH in kexts. */
3088 flag_exceptions = 0;
3089 /* No -fnon-call-exceptions data in kexts. */
3090 flag_non_call_exceptions = 0;
3091 /* so no tables either.. */
3092 flag_unwind_tables = 0;
3093 flag_asynchronous_unwind_tables = 0;
3094 /* We still need to emit branch islands for kernel context. */
3095 darwin_emit_branch_islands = true;
3098 if (flag_var_tracking_uninit == 0
3099 && generating_for_darwin_version >= 9
3100 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
3101 : (debug_info_level >= DINFO_LEVEL_NORMAL))
3102 && write_symbols == DWARF2_DEBUG)
3103 flag_var_tracking_uninit = flag_var_tracking;
3105 if (MACHO_DYNAMIC_NO_PIC_P)
3107 if (flag_pic)
3108 warning_at (UNKNOWN_LOCATION, 0,
3109 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
3110 " %<-fpie%> or %<-fPIE%>");
3111 flag_pic = 0;
3113 else if (flag_pic == 1)
3115 /* Darwin's -fpic is -fPIC. */
3116 flag_pic = 2;
3119 /* It is assumed that branch island stubs are needed for earlier systems. */
3120 if (generating_for_darwin_version < 9)
3121 darwin_emit_branch_islands = true;
3122 else
3123 emit_aligned_common = true; /* Later systems can support aligned common. */
3125 /* The c_dialect...() macros are not available to us here. */
3126 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3129 #if DARWIN_PPC
3130 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
3132 static void
3133 darwin_patch_builtin (enum built_in_function fncode)
3135 tree fn = builtin_decl_explicit (fncode);
3136 tree sym;
3137 char *newname;
3139 if (!fn)
3140 return;
3142 sym = DECL_ASSEMBLER_NAME (fn);
3143 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
3145 set_user_assembler_name (fn, newname);
3147 fn = builtin_decl_implicit (fncode);
3148 if (fn)
3149 set_user_assembler_name (fn, newname);
3152 void
3153 darwin_patch_builtins (void)
3155 if (LONG_DOUBLE_TYPE_SIZE != 128)
3156 return;
3158 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
3159 #define PATCH_BUILTIN_NO64(fncode) \
3160 if (!TARGET_64BIT) \
3161 darwin_patch_builtin (fncode);
3162 #define PATCH_BUILTIN_VARIADIC(fncode) \
3163 if (!TARGET_64BIT \
3164 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
3165 darwin_patch_builtin (fncode);
3166 #include "darwin-ppc-ldouble-patch.def"
3167 #undef PATCH_BUILTIN
3168 #undef PATCH_BUILTIN_NO64
3169 #undef PATCH_BUILTIN_VARIADIC
3171 #endif
3173 /* CFStrings implementation. */
3174 static GTY(()) tree cfstring_class_reference = NULL_TREE;
3175 static GTY(()) tree cfstring_type_node = NULL_TREE;
3176 static GTY(()) tree ccfstring_type_node = NULL_TREE;
3177 static GTY(()) tree pccfstring_type_node = NULL_TREE;
3178 static GTY(()) tree pcint_type_node = NULL_TREE;
3179 static GTY(()) tree pcchar_type_node = NULL_TREE;
3181 static enum built_in_function darwin_builtin_cfstring;
3183 /* Store all constructed constant CFStrings in a hash table so that
3184 they get uniqued properly. */
3186 typedef struct GTY (()) cfstring_descriptor {
3187 /* The string literal. */
3188 tree literal;
3189 /* The resulting constant CFString. */
3190 tree constructor;
3191 } cfstring_descriptor;
3193 static GTY ((param_is (struct cfstring_descriptor))) htab_t cfstring_htab;
3195 static hashval_t cfstring_hash (const void *);
3196 static int cfstring_eq (const void *, const void *);
3198 static tree
3199 add_builtin_field_decl (tree type, const char *name, tree **chain)
3201 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
3202 get_identifier (name), type);
3204 if (*chain != NULL)
3205 **chain = field;
3206 *chain = &DECL_CHAIN (field);
3208 return field;
3211 tree
3212 darwin_init_cfstring_builtins (unsigned builtin_cfstring)
3214 tree cfsfun, fields, pccfstring_ftype_pcchar;
3215 tree *chain = NULL;
3217 darwin_builtin_cfstring =
3218 (enum built_in_function) builtin_cfstring;
3220 /* struct __builtin_CFString {
3221 const int *isa; (will point at
3222 int flags; __CFConstantStringClassReference)
3223 const char *str;
3224 long length;
3225 }; */
3227 pcint_type_node = build_pointer_type
3228 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
3230 pcchar_type_node = build_pointer_type
3231 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3233 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
3235 /* Have to build backwards for finish struct. */
3236 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
3237 add_builtin_field_decl (pcchar_type_node, "str", &chain);
3238 add_builtin_field_decl (integer_type_node, "flags", &chain);
3239 add_builtin_field_decl (pcint_type_node, "isa", &chain);
3240 finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
3241 fields, NULL_TREE);
3243 /* const struct __builtin_CFstring *
3244 __builtin___CFStringMakeConstantString (const char *); */
3246 ccfstring_type_node = build_qualified_type
3247 (cfstring_type_node, TYPE_QUAL_CONST);
3248 pccfstring_type_node = build_pointer_type (ccfstring_type_node);
3249 pccfstring_ftype_pcchar = build_function_type_list
3250 (pccfstring_type_node, pcchar_type_node, NULL_TREE);
3252 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
3253 get_identifier ("__builtin___CFStringMakeConstantString"),
3254 pccfstring_ftype_pcchar);
3256 TREE_PUBLIC (cfsfun) = 1;
3257 DECL_EXTERNAL (cfsfun) = 1;
3258 DECL_ARTIFICIAL (cfsfun) = 1;
3259 /* Make a lang-specific section - dup_lang_specific_decl makes a new node
3260 in place of the existing, which may be NULL. */
3261 DECL_LANG_SPECIFIC (cfsfun) = NULL;
3262 (*lang_hooks.dup_lang_specific_decl) (cfsfun);
3263 DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD;
3264 DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring;
3265 lang_hooks.builtin_function (cfsfun);
3267 /* extern int __CFConstantStringClassReference[]; */
3268 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
3269 get_identifier ("__CFConstantStringClassReference"),
3270 build_array_type (integer_type_node, NULL_TREE));
3272 TREE_PUBLIC (cfstring_class_reference) = 1;
3273 DECL_ARTIFICIAL (cfstring_class_reference) = 1;
3274 (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
3275 DECL_EXTERNAL (cfstring_class_reference) = 1;
3276 rest_of_decl_compilation (cfstring_class_reference, 0, 0);
3278 /* Initialize the hash table used to hold the constant CFString objects. */
3279 cfstring_htab = htab_create_ggc (31, cfstring_hash, cfstring_eq, NULL);
3281 return cfstring_type_node;
3284 tree
3285 darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
3286 bool ARG_UNUSED (ignore))
3288 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3290 if (fcode == darwin_builtin_cfstring)
3292 if (!darwin_constant_cfstrings)
3294 error ("built-in function %qD requires the"
3295 " %<-mconstant-cfstrings%> flag", fndecl);
3296 return error_mark_node;
3299 if (n_args != 1)
3301 error ("built-in function %qD takes one argument only", fndecl);
3302 return error_mark_node;
3305 return darwin_build_constant_cfstring (*argp);
3308 return NULL_TREE;
3311 void
3312 darwin_rename_builtins (void)
3314 /* The system ___divdc3 routine in libSystem on darwin10 is not
3315 accurate to 1ulp, ours is, so we avoid ever using the system name
3316 for this routine and instead install a non-conflicting name that
3317 is accurate.
3319 When -ffast-math or -funsafe-math-optimizations is given, we can
3320 use the faster version. */
3321 if (!flag_unsafe_math_optimizations)
3323 enum built_in_function dcode
3324 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
3325 + DCmode - MIN_MODE_COMPLEX_FLOAT);
3326 tree fn = builtin_decl_explicit (dcode);
3327 /* Fortran and c call TARGET_INIT_BUILTINS and
3328 TARGET_INIT_LIBFUNCS at different times, so we have to put a
3329 call into each to ensure that at least one of them is called
3330 after build_common_builtin_nodes. A better fix is to add a
3331 new hook to run after build_common_builtin_nodes runs. */
3332 if (fn)
3333 set_user_assembler_name (fn, "___ieee_divdc3");
3334 fn = builtin_decl_implicit (dcode);
3335 if (fn)
3336 set_user_assembler_name (fn, "___ieee_divdc3");
3340 static hashval_t
3341 cfstring_hash (const void *ptr)
3343 tree str = ((const struct cfstring_descriptor *)ptr)->literal;
3344 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3345 int i, len = TREE_STRING_LENGTH (str);
3346 hashval_t h = len;
3348 for (i = 0; i < len; i++)
3349 h = ((h * 613) + p[i]);
3351 return h;
3354 static int
3355 cfstring_eq (const void *ptr1, const void *ptr2)
3357 tree str1 = ((const struct cfstring_descriptor *)ptr1)->literal;
3358 tree str2 = ((const struct cfstring_descriptor *)ptr2)->literal;
3359 int len1 = TREE_STRING_LENGTH (str1);
3361 return (len1 == TREE_STRING_LENGTH (str2)
3362 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3363 len1));
3366 tree
3367 darwin_build_constant_cfstring (tree str)
3369 struct cfstring_descriptor *desc, key;
3370 void **loc;
3371 tree addr;
3373 if (!str)
3375 error ("CFString literal is missing");
3376 return error_mark_node;
3379 STRIP_NOPS (str);
3381 if (TREE_CODE (str) == ADDR_EXPR)
3382 str = TREE_OPERAND (str, 0);
3384 if (TREE_CODE (str) != STRING_CST)
3386 error ("CFString literal expression is not a string constant");
3387 return error_mark_node;
3390 /* Perhaps we already constructed a constant CFString just like this one? */
3391 key.literal = str;
3392 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3393 desc = (struct cfstring_descriptor *) *loc;
3395 if (!desc)
3397 tree var, constructor, field;
3398 vec<constructor_elt, va_gc> *v = NULL;
3399 int length = TREE_STRING_LENGTH (str) - 1;
3401 if (darwin_warn_nonportable_cfstrings)
3403 const char *s = TREE_STRING_POINTER (str);
3404 int l = 0;
3406 for (l = 0; l < length; l++)
3407 if (!s[l] || !isascii (s[l]))
3409 warning (darwin_warn_nonportable_cfstrings, "%s in CFString literal",
3410 s[l] ? "non-ASCII character" : "embedded NUL");
3411 break;
3415 *loc = desc = ggc_alloc_cleared_cfstring_descriptor ();
3416 desc->literal = str;
3418 /* isa *. */
3419 field = TYPE_FIELDS (ccfstring_type_node);
3420 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3421 build1 (ADDR_EXPR, TREE_TYPE (field),
3422 cfstring_class_reference));
3423 /* flags */
3424 field = DECL_CHAIN (field);
3425 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3426 build_int_cst (TREE_TYPE (field), 0x000007c8));
3427 /* string *. */
3428 field = DECL_CHAIN (field);
3429 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3430 build1 (ADDR_EXPR, TREE_TYPE (field), str));
3431 /* length */
3432 field = DECL_CHAIN (field);
3433 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3434 build_int_cst (TREE_TYPE (field), length));
3436 constructor = build_constructor (ccfstring_type_node, v);
3437 TREE_READONLY (constructor) = 1;
3438 TREE_CONSTANT (constructor) = 1;
3439 TREE_STATIC (constructor) = 1;
3441 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
3442 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
3443 being built without any knowledge of C++ tree accessors; hence, we shall
3444 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
3445 if (darwin_running_cxx)
3446 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
3448 /* Create an anonymous global variable for this CFString. */
3449 var = build_decl (input_location, CONST_DECL,
3450 NULL, TREE_TYPE (constructor));
3451 DECL_ARTIFICIAL (var) = 1;
3452 TREE_STATIC (var) = 1;
3453 DECL_INITIAL (var) = constructor;
3454 /* FIXME: This should use a translation_unit_decl to indicate file scope. */
3455 DECL_CONTEXT (var) = NULL_TREE;
3456 desc->constructor = var;
3459 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
3460 TREE_CONSTANT (addr) = 1;
3462 return addr;
3465 bool
3466 darwin_cfstring_p (tree str)
3468 struct cfstring_descriptor key;
3469 void **loc;
3471 if (!str)
3472 return false;
3474 STRIP_NOPS (str);
3476 if (TREE_CODE (str) == ADDR_EXPR)
3477 str = TREE_OPERAND (str, 0);
3479 if (TREE_CODE (str) != STRING_CST)
3480 return false;
3482 key.literal = str;
3483 loc = htab_find_slot (cfstring_htab, &key, NO_INSERT);
3485 if (loc)
3486 return true;
3488 return false;
3491 void
3492 darwin_enter_string_into_cfstring_table (tree str)
3494 struct cfstring_descriptor key;
3495 void **loc;
3497 key.literal = str;
3498 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3500 if (!*loc)
3502 *loc = ggc_alloc_cleared_cfstring_descriptor ();
3503 ((struct cfstring_descriptor *)*loc)->literal = str;
3507 /* Choose named function section based on its frequency. */
3509 section *
3510 darwin_function_section (tree decl, enum node_frequency freq,
3511 bool startup, bool exit)
3513 /* Decide if we need to put this in a coalescable section. */
3514 bool weak = (decl
3515 && DECL_WEAK (decl)
3516 && (!DECL_ATTRIBUTES (decl)
3517 || !lookup_attribute ("weak_import",
3518 DECL_ATTRIBUTES (decl))));
3520 /* If there is a specified section name, we should not be trying to
3521 override. */
3522 if (decl && DECL_SECTION_NAME (decl) != NULL_TREE)
3523 return get_named_section (decl, NULL, 0);
3525 /* Default when there is no function re-ordering. */
3526 if (!flag_reorder_functions)
3527 return (weak)
3528 ? darwin_sections[text_coal_section]
3529 : text_section;
3531 /* Startup code should go to startup subsection unless it is
3532 unlikely executed (this happens especially with function splitting
3533 where we can split away unnecessary parts of static constructors). */
3534 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
3535 return (weak)
3536 ? darwin_sections[text_startup_coal_section]
3537 : darwin_sections[text_startup_section];
3539 /* Similarly for exit. */
3540 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
3541 return (weak)
3542 ? darwin_sections[text_exit_coal_section]
3543 : darwin_sections[text_exit_section];
3545 /* Group cold functions together, similarly for hot code. */
3546 switch (freq)
3548 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
3549 return (weak)
3550 ? darwin_sections[text_cold_coal_section]
3551 : darwin_sections[text_cold_section];
3552 break;
3553 case NODE_FREQUENCY_HOT:
3554 return (weak)
3555 ? darwin_sections[text_hot_coal_section]
3556 : darwin_sections[text_hot_section];
3557 break;
3558 default:
3559 return (weak)
3560 ? darwin_sections[text_coal_section]
3561 : text_section;
3562 break;
3566 /* When a function is partitioned between sections, we need to insert a label
3567 at the start of each new chunk - so that it may become a valid 'atom' for
3568 eh and debug purposes. Without this the linker will emit warnings if one
3569 tries to add line location information (since the switched fragment will
3570 be anonymous). */
3572 void
3573 darwin_function_switched_text_sections (FILE *fp, tree decl, bool new_is_cold)
3575 char buf[128];
3576 snprintf (buf, 128, "%s%s",new_is_cold?"__cold_sect_of_":"__hot_sect_of_",
3577 IDENTIFIER_POINTER (DECL_NAME (decl)));
3578 /* Make sure we pick up all the relevant quotes etc. */
3579 assemble_name_raw (fp, (const char *) buf);
3580 fputs (":\n", fp);
3583 #include "gt-darwin.h"