Merge trunk version 195164 into gupc branch.
[official-gcc.git] / gcc / config / darwin.c
blobe3f7fdb66b4486456c84f681449b6fabf921aa19
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 /* A flag to determine whether we are running c++ or obj-c++. This has to be
87 settable from non-c-family contexts too (i.e. we can't use the c_dialect_
88 functions). */
89 int darwin_running_cxx;
91 /* Some code-gen now depends on OS major version numbers (at least). */
92 int generating_for_darwin_version ;
94 /* Section names. */
95 section * darwin_sections[NUM_DARWIN_SECTIONS];
97 /* While we transition to using in-tests instead of ifdef'd code. */
98 #ifndef HAVE_lo_sum
99 #define HAVE_lo_sum 0
100 #define gen_macho_high(a,b) (a)
101 #define gen_macho_low(a,b,c) (a)
102 #endif
104 /* True if we're setting __attribute__ ((ms_struct)). */
105 int darwin_ms_struct = false;
107 /* Earlier versions of Darwin as do not recognize an alignment field in
108 .comm directives, this should be set for versions that allow it. */
109 int emit_aligned_common = false;
111 /* A get_unnamed_section callback used to switch to an ObjC section.
112 DIRECTIVE is as for output_section_asm_op. */
114 static void
115 output_objc_section_asm_op (const void *directive)
117 static bool been_here = false;
119 /* The NeXT ObjC Runtime requires these sections to be present and in
120 order in the object. The code below implements this by emitting
121 a section header for each ObjC section the first time that an ObjC
122 section is requested. */
123 if (! been_here)
125 section *saved_in_section = in_section;
126 static const enum darwin_section_enum tomark[] =
128 /* written, cold -> hot */
129 objc_cat_cls_meth_section,
130 objc_cat_inst_meth_section,
131 objc_string_object_section,
132 objc_constant_string_object_section,
133 objc_selector_refs_section,
134 objc_selector_fixup_section,
135 objc_cls_refs_section,
136 objc_class_section,
137 objc_meta_class_section,
138 /* shared, hot -> cold */
139 objc_cls_meth_section,
140 objc_inst_meth_section,
141 objc_protocol_section,
142 objc_class_names_section,
143 objc_meth_var_types_section,
144 objc_meth_var_names_section,
145 objc_category_section,
146 objc_class_vars_section,
147 objc_instance_vars_section,
148 objc_module_info_section,
149 objc_symbols_section,
151 /* ABI=1 */
152 static const enum darwin_section_enum tomarkv1[] =
154 objc1_protocol_ext_section,
155 objc1_class_ext_section,
156 objc1_prop_list_section
158 /* ABI=2 */
159 static const enum darwin_section_enum tomarkv2[] =
161 objc2_message_refs_section,
162 objc2_classdefs_section,
163 objc2_metadata_section,
164 objc2_classrefs_section,
165 objc2_classlist_section,
166 objc2_categorylist_section,
167 objc2_selector_refs_section,
168 objc2_nonlazy_class_section,
169 objc2_nonlazy_category_section,
170 objc2_protocollist_section,
171 objc2_protocolrefs_section,
172 objc2_super_classrefs_section,
173 objc2_image_info_section,
174 objc2_constant_string_object_section
176 size_t i;
178 been_here = true;
179 if (flag_objc_abi < 2)
181 for (i = 0; i < ARRAY_SIZE (tomark); i++)
182 switch_to_section (darwin_sections[tomark[i]]);
183 if (flag_objc_abi == 1)
184 for (i = 0; i < ARRAY_SIZE (tomarkv1); i++)
185 switch_to_section (darwin_sections[tomarkv1[i]]);
187 else
188 for (i = 0; i < ARRAY_SIZE (tomarkv2); i++)
189 switch_to_section (darwin_sections[tomarkv2[i]]);
190 /* Make sure we don't get varasm.c out of sync with us. */
191 switch_to_section (saved_in_section);
193 output_section_asm_op (directive);
197 /* Private flag applied to disable section-anchors in a particular section. */
198 #define SECTION_NO_ANCHOR SECTION_MACH_DEP
201 /* Implement TARGET_ASM_INIT_SECTIONS. */
203 void
204 darwin_init_sections (void)
206 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
207 darwin_sections[NAME] = \
208 get_unnamed_section (FLAGS, (OBJC \
209 ? output_objc_section_asm_op \
210 : output_section_asm_op), \
211 "\t" DIRECTIVE);
212 #include "config/darwin-sections.def"
213 #undef DEF_SECTION
215 readonly_data_section = darwin_sections[const_section];
216 exception_section = darwin_sections[darwin_exception_section];
217 eh_frame_section = darwin_sections[darwin_eh_frame_section];
221 name_needs_quotes (const char *name)
223 int c;
224 while ((c = *name++) != '\0')
225 if (! ISIDNUM (c)
226 && c != '.' && c != '$' && c != '_' )
227 return 1;
228 return 0;
231 /* Return true if SYM_REF can be used without an indirection. */
233 machopic_symbol_defined_p (rtx sym_ref)
235 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
236 return true;
238 /* If a symbol references local and is not an extern to this
239 file, then the symbol might be able to declared as defined. */
240 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
242 /* If the symbol references a variable and the variable is a
243 common symbol, then this symbol is not defined. */
244 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
246 tree decl = SYMBOL_REF_DECL (sym_ref);
247 if (!decl)
248 return true;
249 if (DECL_COMMON (decl))
250 return false;
252 return true;
254 return false;
257 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
258 reference, which will not be changed. */
260 enum machopic_addr_class
261 machopic_classify_symbol (rtx sym_ref)
263 bool function_p;
265 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
266 if (machopic_symbol_defined_p (sym_ref))
267 return (function_p
268 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
269 else
270 return (function_p
271 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
274 #ifndef TARGET_FIX_AND_CONTINUE
275 #define TARGET_FIX_AND_CONTINUE 0
276 #endif
278 /* Indicate when fix-and-continue style code generation is being used
279 and when a reference to data should be indirected so that it can be
280 rebound in a new translation unit to reference the original instance
281 of that data. Symbol names that are for code generation local to
282 the translation unit are bound to the new translation unit;
283 currently this means symbols that begin with L or _OBJC_;
284 otherwise, we indicate that an indirect reference should be made to
285 permit the runtime to rebind new instances of the translation unit
286 to the original instance of the data. */
288 static int
289 indirect_data (rtx sym_ref)
291 int lprefix;
292 const char *name;
294 /* If we aren't generating fix-and-continue code, don't do anything
295 special. */
296 if (TARGET_FIX_AND_CONTINUE == 0)
297 return 0;
299 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
300 are indirected. Symbols that begin with L and _OBJC_ are always
301 bound to the current translation unit as they are used for
302 generated local data of the translation unit. */
304 name = XSTR (sym_ref, 0);
306 lprefix = (((name[0] == '*' || name[0] == '&')
307 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
308 || (strncmp (name, "_OBJC_", 6) == 0));
310 return ! lprefix;
313 static int
314 machopic_data_defined_p (rtx sym_ref)
316 if (indirect_data (sym_ref))
317 return 0;
319 switch (machopic_classify_symbol (sym_ref))
321 case MACHOPIC_DEFINED_DATA:
322 case MACHOPIC_DEFINED_FUNCTION:
323 return 1;
324 default:
325 return 0;
329 void
330 machopic_define_symbol (rtx mem)
332 rtx sym_ref;
334 gcc_assert (GET_CODE (mem) == MEM);
335 sym_ref = XEXP (mem, 0);
336 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
339 /* Return either ORIG or:
341 (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
343 depending on MACHO_DYNAMIC_NO_PIC_P. */
345 machopic_gen_offset (rtx orig)
347 if (MACHO_DYNAMIC_NO_PIC_P)
348 return orig;
349 else
351 /* Play games to avoid marking the function as needing pic if we
352 are being called as part of the cost-estimation process. */
353 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
354 crtl->uses_pic_offset_table = 1;
355 orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
356 UNSPEC_MACHOPIC_OFFSET);
357 return gen_rtx_CONST (Pmode, orig);
361 static GTY(()) const char * function_base_func_name;
362 static GTY(()) int current_pic_label_num;
364 void
365 machopic_output_function_base_name (FILE *file)
367 const char *current_name;
369 /* If dynamic-no-pic is on, we should not get here. */
370 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
371 /* When we are generating _get_pc thunks within stubs, there is no current
372 function. */
373 if (current_function_decl)
375 current_name =
376 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
377 if (function_base_func_name != current_name)
379 ++current_pic_label_num;
380 function_base_func_name = current_name;
383 else
385 ++current_pic_label_num;
386 function_base_func_name = "L_machopic_stub_dummy";
388 fprintf (file, "L%011d$pb", current_pic_label_num);
391 /* The suffix attached to non-lazy pointer symbols. */
392 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
393 /* The suffix attached to stub symbols. */
394 #define STUB_SUFFIX "$stub"
396 typedef struct GTY (()) machopic_indirection
398 /* The SYMBOL_REF for the entity referenced. */
399 rtx symbol;
400 /* The name of the stub or non-lazy pointer. */
401 const char * ptr_name;
402 /* True iff this entry is for a stub (as opposed to a non-lazy
403 pointer). */
404 bool stub_p;
405 /* True iff this stub or pointer pointer has been referenced. */
406 bool used;
407 } machopic_indirection;
409 /* A table mapping stub names and non-lazy pointer names to
410 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
412 static GTY ((param_is (struct machopic_indirection))) htab_t
413 machopic_indirections;
415 /* Return a hash value for a SLOT in the indirections hash table. */
417 static hashval_t
418 machopic_indirection_hash (const void *slot)
420 const machopic_indirection *p = (const machopic_indirection *) slot;
421 return htab_hash_string (p->ptr_name);
424 /* Returns true if the KEY is the same as that associated with
425 SLOT. */
427 static int
428 machopic_indirection_eq (const void *slot, const void *key)
430 return strcmp (((const machopic_indirection *) slot)->ptr_name,
431 (const char *) key) == 0;
434 /* Return the name of the non-lazy pointer (if STUB_P is false) or
435 stub (if STUB_B is true) corresponding to the given name. */
437 const char *
438 machopic_indirection_name (rtx sym_ref, bool stub_p)
440 char *buffer;
441 const char *name = XSTR (sym_ref, 0);
442 size_t namelen = strlen (name);
443 machopic_indirection *p;
444 void ** slot;
445 bool needs_quotes;
446 const char *suffix;
447 const char *prefix = user_label_prefix;
448 const char *quote = "";
449 tree id;
451 id = maybe_get_identifier (name);
452 if (id)
454 tree id_orig = id;
456 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
457 id = TREE_CHAIN (id);
458 if (id != id_orig)
460 name = IDENTIFIER_POINTER (id);
461 namelen = strlen (name);
465 if (name[0] == '*')
467 prefix = "";
468 ++name;
469 --namelen;
472 needs_quotes = name_needs_quotes (name);
473 if (needs_quotes)
475 quote = "\"";
478 if (stub_p)
479 suffix = STUB_SUFFIX;
480 else
481 suffix = NON_LAZY_POINTER_SUFFIX;
483 buffer = XALLOCAVEC (char, strlen ("&L")
484 + strlen (prefix)
485 + namelen
486 + strlen (suffix)
487 + 2 * strlen (quote)
488 + 1 /* '\0' */);
490 /* Construct the name of the non-lazy pointer or stub. */
491 sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
493 if (!machopic_indirections)
494 machopic_indirections = htab_create_ggc (37,
495 machopic_indirection_hash,
496 machopic_indirection_eq,
497 /*htab_del=*/NULL);
499 slot = htab_find_slot_with_hash (machopic_indirections, buffer,
500 htab_hash_string (buffer), INSERT);
501 if (*slot)
503 p = (machopic_indirection *) *slot;
505 else
507 p = ggc_alloc_machopic_indirection ();
508 p->symbol = sym_ref;
509 p->ptr_name = xstrdup (buffer);
510 p->stub_p = stub_p;
511 p->used = false;
512 *slot = p;
515 return p->ptr_name;
518 /* Return the name of the stub for the mcount function. */
520 const char*
521 machopic_mcount_stub_name (void)
523 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
524 return machopic_indirection_name (symbol, /*stub_p=*/true);
527 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
528 or non-lazy pointer as used -- and mark the object to which the
529 pointer/stub refers as used as well, since the pointer/stub will
530 emit a reference to it. */
532 void
533 machopic_validate_stub_or_non_lazy_ptr (const char *name)
535 machopic_indirection *p;
537 p = ((machopic_indirection *)
538 (htab_find_with_hash (machopic_indirections, name,
539 htab_hash_string (name))));
540 if (p && ! p->used)
542 const char *real_name;
543 tree id;
545 p->used = true;
547 /* Do what output_addr_const will do when we actually call it. */
548 if (SYMBOL_REF_DECL (p->symbol))
549 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
551 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
553 id = maybe_get_identifier (real_name);
554 if (id)
555 mark_referenced (id);
559 /* Transform ORIG, which may be any data source, to the corresponding
560 source using indirections. */
563 machopic_indirect_data_reference (rtx orig, rtx reg)
565 rtx ptr_ref = orig;
567 if (! MACHOPIC_INDIRECT)
568 return orig;
570 if (GET_CODE (orig) == SYMBOL_REF)
572 int defined = machopic_data_defined_p (orig);
574 if (defined && MACHO_DYNAMIC_NO_PIC_P)
576 if (DARWIN_PPC)
578 /* Create a new register for CSE opportunities. */
579 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
580 emit_insn (gen_macho_high (hi_reg, orig));
581 emit_insn (gen_macho_low (reg, hi_reg, orig));
582 return reg;
584 else if (DARWIN_X86)
585 return orig;
586 else
587 /* some other cpu -- writeme! */
588 gcc_unreachable ();
590 else if (defined)
592 rtx offset = NULL;
593 if (DARWIN_PPC || HAVE_lo_sum)
594 offset = machopic_gen_offset (orig);
596 if (DARWIN_PPC)
598 rtx hi_sum_reg = (!can_create_pseudo_p ()
599 ? reg
600 : gen_reg_rtx (Pmode));
602 gcc_assert (reg);
604 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
605 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
606 gen_rtx_HIGH (Pmode, offset))));
607 emit_insn (gen_rtx_SET (Pmode, reg,
608 gen_rtx_LO_SUM (Pmode, hi_sum_reg,
609 copy_rtx (offset))));
611 orig = reg;
613 else if (HAVE_lo_sum)
615 gcc_assert (reg);
617 emit_insn (gen_rtx_SET (VOIDmode, reg,
618 gen_rtx_HIGH (Pmode, offset)));
619 emit_insn (gen_rtx_SET (VOIDmode, reg,
620 gen_rtx_LO_SUM (Pmode, reg,
621 copy_rtx (offset))));
622 emit_use (pic_offset_table_rtx);
624 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
626 return orig;
629 ptr_ref = (gen_rtx_SYMBOL_REF
630 (Pmode,
631 machopic_indirection_name (orig, /*stub_p=*/false)));
633 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
635 ptr_ref = gen_const_mem (Pmode, ptr_ref);
636 machopic_define_symbol (ptr_ref);
638 if (DARWIN_X86
639 && reg
640 && MACHO_DYNAMIC_NO_PIC_P)
642 emit_insn (gen_rtx_SET (Pmode, reg, ptr_ref));
643 ptr_ref = reg;
646 return ptr_ref;
648 else if (GET_CODE (orig) == CONST)
650 /* If "(const (plus ...", walk the PLUS and return that result.
651 PLUS processing (below) will restore the "(const ..." if
652 appropriate. */
653 if (GET_CODE (XEXP (orig, 0)) == PLUS)
654 return machopic_indirect_data_reference (XEXP (orig, 0), reg);
655 else
656 return orig;
658 else if (GET_CODE (orig) == MEM)
660 XEXP (ptr_ref, 0) =
661 machopic_indirect_data_reference (XEXP (orig, 0), reg);
662 return ptr_ref;
664 else if (GET_CODE (orig) == PLUS)
666 rtx base, result;
667 /* When the target is i386, this code prevents crashes due to the
668 compiler's ignorance on how to move the PIC base register to
669 other registers. (The reload phase sometimes introduces such
670 insns.) */
671 if (GET_CODE (XEXP (orig, 0)) == REG
672 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
673 /* Prevent the same register from being erroneously used
674 as both the base and index registers. */
675 && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST))
676 && reg)
678 emit_move_insn (reg, XEXP (orig, 0));
679 XEXP (ptr_ref, 0) = reg;
680 return ptr_ref;
683 /* Legitimize both operands of the PLUS. */
684 base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
685 orig = machopic_indirect_data_reference (XEXP (orig, 1),
686 (base == reg ? 0 : reg));
687 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
688 result = plus_constant (Pmode, base, INTVAL (orig));
689 else
690 result = gen_rtx_PLUS (Pmode, base, orig);
692 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
694 if (reg)
696 emit_move_insn (reg, result);
697 result = reg;
699 else
701 result = force_reg (GET_MODE (result), result);
705 return result;
707 return ptr_ref;
710 /* Transform TARGET (a MEM), which is a function call target, to the
711 corresponding symbol_stub if necessary. Return a new MEM. */
714 machopic_indirect_call_target (rtx target)
716 if (! darwin_emit_branch_islands)
717 return target;
719 if (GET_CODE (target) != MEM)
720 return target;
722 if (MACHOPIC_INDIRECT
723 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
724 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
725 & MACHO_SYMBOL_FLAG_DEFINED))
727 rtx sym_ref = XEXP (target, 0);
728 const char *stub_name = machopic_indirection_name (sym_ref,
729 /*stub_p=*/true);
730 enum machine_mode mode = GET_MODE (sym_ref);
732 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
733 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
734 MEM_READONLY_P (target) = 1;
735 MEM_NOTRAP_P (target) = 1;
738 return target;
742 machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
744 rtx pic_ref = orig;
746 if (! MACHOPIC_INDIRECT)
747 return orig;
749 /* First handle a simple SYMBOL_REF or LABEL_REF */
750 if (GET_CODE (orig) == LABEL_REF
751 || (GET_CODE (orig) == SYMBOL_REF
754 /* addr(foo) = &func+(foo-func) */
755 orig = machopic_indirect_data_reference (orig, reg);
757 if (GET_CODE (orig) == PLUS
758 && GET_CODE (XEXP (orig, 0)) == REG)
760 if (reg == 0)
761 return force_reg (mode, orig);
763 emit_move_insn (reg, orig);
764 return reg;
767 if (GET_CODE (orig) == MEM)
769 if (reg == 0)
771 gcc_assert (!reload_in_progress);
772 reg = gen_reg_rtx (Pmode);
775 #if HAVE_lo_sum
776 if (MACHO_DYNAMIC_NO_PIC_P
777 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
778 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
780 #if defined (TARGET_TOC) /* ppc */
781 rtx temp_reg = (!can_create_pseudo_p ()
782 ? reg :
783 gen_reg_rtx (Pmode));
784 rtx asym = XEXP (orig, 0);
785 rtx mem;
787 emit_insn (gen_macho_high (temp_reg, asym));
788 mem = gen_const_mem (GET_MODE (orig),
789 gen_rtx_LO_SUM (Pmode, temp_reg,
790 copy_rtx (asym)));
791 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
792 #else
793 /* Some other CPU -- WriteMe! but right now there are no other
794 platforms that can use dynamic-no-pic */
795 gcc_unreachable ();
796 #endif
797 pic_ref = reg;
799 else
800 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
801 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
803 rtx offset = machopic_gen_offset (XEXP (orig, 0));
804 #if defined (TARGET_TOC) /* i.e., PowerPC */
805 /* Generating a new reg may expose opportunities for
806 common subexpression elimination. */
807 rtx hi_sum_reg = (!can_create_pseudo_p ()
808 ? reg
809 : gen_reg_rtx (Pmode));
810 rtx mem;
811 rtx insn;
812 rtx sum;
814 sum = gen_rtx_HIGH (Pmode, offset);
815 if (! MACHO_DYNAMIC_NO_PIC_P)
816 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
818 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
820 mem = gen_const_mem (GET_MODE (orig),
821 gen_rtx_LO_SUM (Pmode,
822 hi_sum_reg,
823 copy_rtx (offset)));
824 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
825 set_unique_reg_note (insn, REG_EQUAL, pic_ref);
827 pic_ref = reg;
828 #else
829 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
831 emit_insn (gen_rtx_SET (VOIDmode, reg,
832 gen_rtx_HIGH (Pmode,
833 gen_rtx_CONST (Pmode,
834 offset))));
835 emit_insn (gen_rtx_SET (VOIDmode, reg,
836 gen_rtx_LO_SUM (Pmode, reg,
837 gen_rtx_CONST (Pmode,
838 copy_rtx (offset)))));
839 pic_ref = gen_rtx_PLUS (Pmode,
840 pic_offset_table_rtx, reg);
841 #endif
843 else
844 #endif /* HAVE_lo_sum */
846 rtx pic = pic_offset_table_rtx;
847 if (GET_CODE (pic) != REG)
849 emit_move_insn (reg, pic);
850 pic = reg;
852 #if 0
853 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
854 #endif
856 if (reload_in_progress)
857 df_set_regs_ever_live (REGNO (pic), true);
858 pic_ref = gen_rtx_PLUS (Pmode, pic,
859 machopic_gen_offset (XEXP (orig, 0)));
862 #if !defined (TARGET_TOC)
863 emit_move_insn (reg, pic_ref);
864 pic_ref = gen_const_mem (GET_MODE (orig), reg);
865 #endif
867 else
870 #if HAVE_lo_sum
871 if (GET_CODE (orig) == SYMBOL_REF
872 || GET_CODE (orig) == LABEL_REF)
874 rtx offset = machopic_gen_offset (orig);
875 #if defined (TARGET_TOC) /* i.e., PowerPC */
876 rtx hi_sum_reg;
878 if (reg == 0)
880 gcc_assert (!reload_in_progress);
881 reg = gen_reg_rtx (Pmode);
884 hi_sum_reg = reg;
886 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
887 (MACHO_DYNAMIC_NO_PIC_P)
888 ? gen_rtx_HIGH (Pmode, offset)
889 : gen_rtx_PLUS (Pmode,
890 pic_offset_table_rtx,
891 gen_rtx_HIGH (Pmode,
892 offset))));
893 emit_insn (gen_rtx_SET (VOIDmode, reg,
894 gen_rtx_LO_SUM (Pmode,
895 hi_sum_reg,
896 copy_rtx (offset))));
897 pic_ref = reg;
898 #else
899 emit_insn (gen_rtx_SET (VOIDmode, reg,
900 gen_rtx_HIGH (Pmode, offset)));
901 emit_insn (gen_rtx_SET (VOIDmode, reg,
902 gen_rtx_LO_SUM (Pmode, reg,
903 copy_rtx (offset))));
904 pic_ref = gen_rtx_PLUS (Pmode,
905 pic_offset_table_rtx, reg);
906 #endif
908 else
909 #endif /* HAVE_lo_sum */
911 if (REG_P (orig)
912 || GET_CODE (orig) == SUBREG)
914 return orig;
916 else
918 rtx pic = pic_offset_table_rtx;
919 if (GET_CODE (pic) != REG)
921 emit_move_insn (reg, pic);
922 pic = reg;
924 #if 0
925 emit_use (pic_offset_table_rtx);
926 #endif
927 if (reload_in_progress)
928 df_set_regs_ever_live (REGNO (pic), true);
929 pic_ref = gen_rtx_PLUS (Pmode,
930 pic,
931 machopic_gen_offset (orig));
936 if (GET_CODE (pic_ref) != REG)
938 if (reg != 0)
940 emit_move_insn (reg, pic_ref);
941 return reg;
943 else
945 return force_reg (mode, pic_ref);
948 else
950 return pic_ref;
954 else if (GET_CODE (orig) == SYMBOL_REF)
955 return orig;
957 else if (GET_CODE (orig) == PLUS
958 && (GET_CODE (XEXP (orig, 0)) == MEM
959 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
960 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
961 && XEXP (orig, 0) != pic_offset_table_rtx
962 && GET_CODE (XEXP (orig, 1)) != REG)
965 rtx base;
966 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
968 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
969 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
970 Pmode, (base == reg ? 0 : reg));
971 if (GET_CODE (orig) == CONST_INT)
973 pic_ref = plus_constant (Pmode, base, INTVAL (orig));
974 is_complex = 1;
976 else
977 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
979 if (reg && is_complex)
981 emit_move_insn (reg, pic_ref);
982 pic_ref = reg;
984 /* Likewise, should we set special REG_NOTEs here? */
987 else if (GET_CODE (orig) == CONST)
989 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
992 else if (GET_CODE (orig) == MEM
993 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
995 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
996 addr = replace_equiv_address (orig, addr);
997 emit_move_insn (reg, addr);
998 pic_ref = reg;
1001 return pic_ref;
1004 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
1005 DATA is the FILE* for assembly output. Called from
1006 htab_traverse. */
1008 static int
1009 machopic_output_indirection (void **slot, void *data)
1011 machopic_indirection *p = *((machopic_indirection **) slot);
1012 FILE *asm_out_file = (FILE *) data;
1013 rtx symbol;
1014 const char *sym_name;
1015 const char *ptr_name;
1017 if (!p->used)
1018 return 1;
1020 symbol = p->symbol;
1021 sym_name = XSTR (symbol, 0);
1022 ptr_name = p->ptr_name;
1024 if (p->stub_p)
1026 char *sym;
1027 char *stub;
1028 tree id;
1030 id = maybe_get_identifier (sym_name);
1031 if (id)
1033 tree id_orig = id;
1035 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
1036 id = TREE_CHAIN (id);
1037 if (id != id_orig)
1038 sym_name = IDENTIFIER_POINTER (id);
1041 sym = XALLOCAVEC (char, strlen (sym_name) + 2);
1042 if (sym_name[0] == '*' || sym_name[0] == '&')
1043 strcpy (sym, sym_name + 1);
1044 else if (sym_name[0] == '-' || sym_name[0] == '+')
1045 strcpy (sym, sym_name);
1046 else
1047 sprintf (sym, "%s%s", user_label_prefix, sym_name);
1049 stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
1050 if (ptr_name[0] == '*' || ptr_name[0] == '&')
1051 strcpy (stub, ptr_name + 1);
1052 else
1053 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
1055 machopic_output_stub (asm_out_file, sym, stub);
1057 else if (! indirect_data (symbol)
1058 && (machopic_symbol_defined_p (symbol)
1059 || SYMBOL_REF_LOCAL_P (symbol)))
1061 switch_to_section (data_section);
1062 assemble_align (GET_MODE_ALIGNMENT (Pmode));
1063 assemble_label (asm_out_file, ptr_name);
1064 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
1065 GET_MODE_SIZE (Pmode),
1066 GET_MODE_ALIGNMENT (Pmode), 1);
1068 else
1070 rtx init = const0_rtx;
1072 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
1074 /* Mach-O symbols are passed around in code through indirect
1075 references and the original symbol_ref hasn't passed through
1076 the generic handling and reference-catching in
1077 output_operand, so we need to manually mark weak references
1078 as such. */
1079 if (SYMBOL_REF_WEAK (symbol))
1081 tree decl = SYMBOL_REF_DECL (symbol);
1082 gcc_assert (DECL_P (decl));
1084 if (decl != NULL_TREE
1085 && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
1086 /* Handle only actual external-only definitions, not
1087 e.g. extern inline code or variables for which
1088 storage has been allocated. */
1089 && !TREE_STATIC (decl))
1091 fputs ("\t.weak_reference ", asm_out_file);
1092 assemble_name (asm_out_file, sym_name);
1093 fputc ('\n', asm_out_file);
1097 assemble_name (asm_out_file, ptr_name);
1098 fprintf (asm_out_file, ":\n");
1100 fprintf (asm_out_file, "\t.indirect_symbol ");
1101 assemble_name (asm_out_file, sym_name);
1102 fprintf (asm_out_file, "\n");
1104 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1105 have their symbol name instead of 0 in the second entry of
1106 the non-lazy symbol pointer data structure when they are
1107 defined. This allows the runtime to rebind newer instances
1108 of the translation unit with the original instance of the
1109 symbol. */
1111 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1112 && machopic_symbol_defined_p (symbol))
1113 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1115 assemble_integer (init, GET_MODE_SIZE (Pmode),
1116 GET_MODE_ALIGNMENT (Pmode), 1);
1119 return 1;
1122 void
1123 machopic_finish (FILE *asm_out_file)
1125 if (machopic_indirections)
1126 htab_traverse_noresize (machopic_indirections,
1127 machopic_output_indirection,
1128 asm_out_file);
1132 machopic_operand_p (rtx op)
1134 if (MACHOPIC_JUST_INDIRECT)
1135 return (GET_CODE (op) == SYMBOL_REF
1136 && machopic_symbol_defined_p (op));
1137 else
1138 return (GET_CODE (op) == CONST
1139 && GET_CODE (XEXP (op, 0)) == UNSPEC
1140 && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
1143 /* This function records whether a given name corresponds to a defined
1144 or undefined function or variable, for machopic_classify_ident to
1145 use later. */
1147 void
1148 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
1150 rtx sym_ref;
1152 /* Do the standard encoding things first. */
1153 default_encode_section_info (decl, rtl, first);
1155 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1156 return;
1158 sym_ref = XEXP (rtl, 0);
1159 if (TREE_CODE (decl) == VAR_DECL)
1160 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1162 if (!DECL_EXTERNAL (decl)
1163 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1164 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1165 && ((TREE_STATIC (decl)
1166 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1167 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1168 && DECL_INITIAL (decl) != error_mark_node)))
1169 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1171 if (! TREE_PUBLIC (decl))
1172 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
1175 void
1176 darwin_mark_decl_preserved (const char *name)
1178 fprintf (asm_out_file, "\t.no_dead_strip ");
1179 assemble_name (asm_out_file, name);
1180 fputc ('\n', asm_out_file);
1183 static section *
1184 darwin_rodata_section (int weak, bool zsize)
1186 return (weak
1187 ? darwin_sections[const_coal_section]
1188 : (zsize ? darwin_sections[zobj_const_section]
1189 : darwin_sections[const_section]));
1192 static section *
1193 darwin_mergeable_string_section (tree exp,
1194 unsigned HOST_WIDE_INT align)
1196 /* Darwin's ld expects to see non-writable string literals in the .cstring
1197 section. Later versions of ld check and complain when CFStrings are
1198 enabled. Therefore we shall force the strings into .cstring since we
1199 don't support writable ones anyway. */
1200 if ((darwin_constant_cfstrings || flag_merge_constants)
1201 && TREE_CODE (exp) == STRING_CST
1202 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1203 && align <= 256
1204 && (int_size_in_bytes (TREE_TYPE (exp))
1205 == TREE_STRING_LENGTH (exp))
1206 && ((size_t) TREE_STRING_LENGTH (exp)
1207 == strlen (TREE_STRING_POINTER (exp)) + 1))
1208 return darwin_sections[cstring_section];
1210 if (DARWIN_SECTION_ANCHORS && flag_section_anchors
1211 && TREE_CODE (exp) == STRING_CST
1212 && TREE_STRING_LENGTH (exp) == 0)
1213 return darwin_sections[zobj_const_section];
1215 return readonly_data_section;
1218 #ifndef HAVE_GAS_LITERAL16
1219 #define HAVE_GAS_LITERAL16 0
1220 #endif
1222 static section *
1223 darwin_mergeable_constant_section (tree exp,
1224 unsigned HOST_WIDE_INT align,
1225 bool zsize)
1227 enum machine_mode mode = DECL_MODE (exp);
1228 unsigned int modesize = GET_MODE_BITSIZE (mode);
1230 if (DARWIN_SECTION_ANCHORS
1231 && flag_section_anchors
1232 && zsize)
1233 return darwin_sections[zobj_const_section];
1235 if (flag_merge_constants
1236 && mode != VOIDmode
1237 && mode != BLKmode
1238 && modesize <= align
1239 && align >= 8
1240 && align <= 256
1241 && (align & (align -1)) == 0)
1243 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1245 if (TREE_CODE (size) == INTEGER_CST
1246 && TREE_INT_CST_LOW (size) == 4
1247 && TREE_INT_CST_HIGH (size) == 0)
1248 return darwin_sections[literal4_section];
1249 else if (TREE_CODE (size) == INTEGER_CST
1250 && TREE_INT_CST_LOW (size) == 8
1251 && TREE_INT_CST_HIGH (size) == 0)
1252 return darwin_sections[literal8_section];
1253 else if (HAVE_GAS_LITERAL16
1254 && TARGET_64BIT
1255 && TREE_CODE (size) == INTEGER_CST
1256 && TREE_INT_CST_LOW (size) == 16
1257 && TREE_INT_CST_HIGH (size) == 0)
1258 return darwin_sections[literal16_section];
1259 else
1260 return readonly_data_section;
1263 return readonly_data_section;
1266 section *
1267 darwin_tm_clone_table_section (void)
1269 return get_named_section (NULL,
1270 "__DATA,__tm_clone_table,regular,no_dead_strip",
1275 machopic_reloc_rw_mask (void)
1277 return MACHOPIC_INDIRECT ? 3 : 0;
1280 /* We have to deal with ObjC/C++ metadata section placement in the common
1281 code, since it will also be called from LTO.
1283 Return metadata attributes, if present (searching for ABI=2 first)
1284 Return NULL_TREE if no such attributes are found. */
1286 static tree
1287 is_objc_metadata (tree decl)
1289 if (DECL_P (decl)
1290 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1291 && DECL_ATTRIBUTES (decl))
1293 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1294 if (meta)
1295 return meta;
1296 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1297 if (meta)
1298 return meta;
1300 return NULL_TREE;
1303 /* Return the section required for Objective C ABI 2 metadata. */
1304 static section *
1305 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1307 const char *p;
1308 tree ident = TREE_VALUE (meta);
1309 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1310 p = IDENTIFIER_POINTER (ident);
1312 /* If we are in LTO, then we don't know the state of flag_next_runtime
1313 or flag_objc_abi when the code was generated. We set these from the
1314 meta-data - which is needed to deal with const string constructors. */
1316 flag_next_runtime = 1;
1317 flag_objc_abi = 2;
1319 if (base == data_section)
1320 base = darwin_sections[objc2_metadata_section];
1322 /* Most of the OBJC2 META-data end up in the base section, so check it
1323 first. */
1324 if (!strncmp (p, "V2_BASE", 7))
1325 return base;
1326 else if (!strncmp (p, "V2_STRG", 7))
1327 return darwin_sections[cstring_section];
1329 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
1330 return darwin_sections[objc2_classdefs_section];
1331 else if (!strncmp (p, "V2_MREF", 7))
1332 return darwin_sections[objc2_message_refs_section];
1333 else if (!strncmp (p, "V2_CLRF", 7))
1334 return darwin_sections[objc2_classrefs_section];
1335 else if (!strncmp (p, "V2_SURF", 7))
1336 return darwin_sections[objc2_super_classrefs_section];
1337 else if (!strncmp (p, "V2_NLCL", 7))
1338 return darwin_sections[objc2_nonlazy_class_section];
1339 else if (!strncmp (p, "V2_CLAB", 7))
1340 return darwin_sections[objc2_classlist_section];
1341 else if (!strncmp (p, "V2_SRFS", 7))
1342 return darwin_sections[objc2_selector_refs_section];
1343 else if (!strncmp (p, "V2_NLCA", 7))
1344 return darwin_sections[objc2_nonlazy_category_section];
1345 else if (!strncmp (p, "V2_CALA", 7))
1346 return darwin_sections[objc2_categorylist_section];
1348 else if (!strncmp (p, "V2_PLST", 7))
1349 return darwin_sections[objc2_protocollist_section];
1350 else if (!strncmp (p, "V2_PRFS", 7))
1351 return darwin_sections[objc2_protocolrefs_section];
1353 else if (!strncmp (p, "V2_INFO", 7))
1354 return darwin_sections[objc2_image_info_section];
1356 else if (!strncmp (p, "V2_EHTY", 7))
1357 return darwin_sections[data_coal_section];
1359 else if (!strncmp (p, "V2_CSTR", 7))
1360 return darwin_sections[objc2_constant_string_object_section];
1362 /* Not recognized, default. */
1363 return base;
1366 /* Return the section required for Objective C ABI 0/1 metadata. */
1367 static section *
1368 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1370 const char *p;
1371 tree ident = TREE_VALUE (meta);
1372 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1373 p = IDENTIFIER_POINTER (ident);
1375 /* If we are in LTO, then we don't know the state of flag_next_runtime
1376 or flag_objc_abi when the code was generated. We set these from the
1377 meta-data - which is needed to deal with const string constructors. */
1378 flag_next_runtime = 1;
1379 if (!global_options_set.x_flag_objc_abi)
1380 flag_objc_abi = 1;
1382 /* String sections first, cos there are lots of strings. */
1383 if (!strncmp (p, "V1_STRG", 7))
1384 return darwin_sections[cstring_section];
1385 else if (!strncmp (p, "V1_CLSN", 7))
1386 return darwin_sections[objc_class_names_section];
1387 else if (!strncmp (p, "V1_METN", 7))
1388 return darwin_sections[objc_meth_var_names_section];
1389 else if (!strncmp (p, "V1_METT", 7))
1390 return darwin_sections[objc_meth_var_types_section];
1392 else if (!strncmp (p, "V1_CLAS", 7))
1393 return darwin_sections[objc_class_section];
1394 else if (!strncmp (p, "V1_META", 7))
1395 return darwin_sections[objc_meta_class_section];
1396 else if (!strncmp (p, "V1_CATG", 7))
1397 return darwin_sections[objc_category_section];
1398 else if (!strncmp (p, "V1_PROT", 7))
1399 return darwin_sections[objc_protocol_section];
1401 else if (!strncmp (p, "V1_CLCV", 7))
1402 return darwin_sections[objc_class_vars_section];
1403 else if (!strncmp (p, "V1_CLIV", 7))
1404 return darwin_sections[objc_instance_vars_section];
1406 else if (!strncmp (p, "V1_CLCM", 7))
1407 return darwin_sections[objc_cls_meth_section];
1408 else if (!strncmp (p, "V1_CLIM", 7))
1409 return darwin_sections[objc_inst_meth_section];
1410 else if (!strncmp (p, "V1_CACM", 7))
1411 return darwin_sections[objc_cat_cls_meth_section];
1412 else if (!strncmp (p, "V1_CAIM", 7))
1413 return darwin_sections[objc_cat_inst_meth_section];
1414 else if (!strncmp (p, "V1_PNSM", 7))
1415 return darwin_sections[objc_cat_inst_meth_section];
1416 else if (!strncmp (p, "V1_PCLM", 7))
1417 return darwin_sections[objc_cat_cls_meth_section];
1419 else if (!strncmp (p, "V1_CLPR", 7))
1420 return darwin_sections[objc_cat_cls_meth_section];
1421 else if (!strncmp (p, "V1_CAPR", 7))
1422 return darwin_sections[objc_category_section]; /* ??? CHECK me. */
1424 else if (!strncmp (p, "V1_PRFS", 7))
1425 return darwin_sections[objc_cat_cls_meth_section];
1426 else if (!strncmp (p, "V1_CLRF", 7))
1427 return darwin_sections[objc_cls_refs_section];
1428 else if (!strncmp (p, "V1_SRFS", 7))
1429 return darwin_sections[objc_selector_refs_section];
1431 else if (!strncmp (p, "V1_MODU", 7))
1432 return darwin_sections[objc_module_info_section];
1433 else if (!strncmp (p, "V1_SYMT", 7))
1434 return darwin_sections[objc_symbols_section];
1435 else if (!strncmp (p, "V1_INFO", 7))
1436 return darwin_sections[objc_image_info_section];
1438 else if (!strncmp (p, "V1_PLST", 7))
1439 return darwin_sections[objc1_prop_list_section];
1440 else if (!strncmp (p, "V1_PEXT", 7))
1441 return darwin_sections[objc1_protocol_ext_section];
1442 else if (!strncmp (p, "V1_CEXT", 7))
1443 return darwin_sections[objc1_class_ext_section];
1445 else if (!strncmp (p, "V2_CSTR", 7))
1446 return darwin_sections[objc_constant_string_object_section];
1448 return base;
1451 section *
1452 machopic_select_section (tree decl,
1453 int reloc,
1454 unsigned HOST_WIDE_INT align)
1456 bool zsize, one, weak, ro;
1457 section *base_section = NULL;
1459 weak = (DECL_P (decl)
1460 && DECL_WEAK (decl)
1461 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
1463 zsize = (DECL_P (decl)
1464 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1465 && tree_low_cst (DECL_SIZE_UNIT (decl), 1) == 0);
1467 one = DECL_P (decl)
1468 && TREE_CODE (decl) == VAR_DECL
1469 && DECL_ONE_ONLY (decl);
1471 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
1473 switch (categorize_decl_for_section (decl, reloc))
1475 case SECCAT_TEXT:
1476 gcc_unreachable ();
1477 break;
1479 case SECCAT_RODATA:
1480 case SECCAT_SRODATA:
1481 base_section = darwin_rodata_section (weak, zsize);
1482 break;
1484 case SECCAT_RODATA_MERGE_STR:
1485 base_section = darwin_mergeable_string_section (decl, align);
1486 break;
1488 case SECCAT_RODATA_MERGE_STR_INIT:
1489 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1490 break;
1492 case SECCAT_RODATA_MERGE_CONST:
1493 base_section = darwin_mergeable_constant_section (decl, align, zsize);
1494 break;
1496 case SECCAT_DATA:
1497 case SECCAT_DATA_REL:
1498 case SECCAT_DATA_REL_LOCAL:
1499 case SECCAT_DATA_REL_RO:
1500 case SECCAT_DATA_REL_RO_LOCAL:
1501 case SECCAT_SDATA:
1502 case SECCAT_TDATA:
1503 if (weak || one)
1505 if (ro)
1506 base_section = darwin_sections[const_data_coal_section];
1507 else
1508 base_section = darwin_sections[data_coal_section];
1510 else if (DARWIN_SECTION_ANCHORS
1511 && flag_section_anchors
1512 && zsize)
1514 /* If we're doing section anchors, then punt zero-sized objects into
1515 their own sections so that they don't interfere with offset
1516 computation for the remaining vars. This does not need to be done
1517 for stuff in mergeable sections, since these are ineligible for
1518 anchors. */
1519 if (ro)
1520 base_section = darwin_sections[zobj_const_data_section];
1521 else
1522 base_section = darwin_sections[zobj_data_section];
1524 else if (ro)
1525 base_section = darwin_sections[const_data_section];
1526 else
1527 base_section = data_section;
1528 break;
1529 case SECCAT_BSS:
1530 case SECCAT_SBSS:
1531 case SECCAT_TBSS:
1532 if (weak || one)
1533 base_section = darwin_sections[data_coal_section];
1534 else
1536 if (!TREE_PUBLIC (decl))
1537 base_section = lcomm_section;
1538 else if (bss_noswitch_section)
1539 base_section = bss_noswitch_section;
1540 else
1541 base_section = data_section;
1543 break;
1545 default:
1546 gcc_unreachable ();
1549 /* Darwin weird special cases.
1550 a) OBJC Meta-data. */
1551 if (DECL_P (decl)
1552 && (TREE_CODE (decl) == VAR_DECL
1553 || TREE_CODE (decl) == CONST_DECL)
1554 && DECL_ATTRIBUTES (decl))
1556 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1557 if (meta)
1558 return darwin_objc2_section (decl, meta, base_section);
1559 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1560 if (meta)
1561 return darwin_objc1_section (decl, meta, base_section);
1562 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
1563 if (meta)
1564 return base_section; /* GNU runtime is happy with it all in one pot. */
1567 /* b) Constant string objects. */
1568 if (TREE_CODE (decl) == CONSTRUCTOR
1569 && TREE_TYPE (decl)
1570 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1571 && TYPE_NAME (TREE_TYPE (decl)))
1573 tree name = TYPE_NAME (TREE_TYPE (decl));
1574 if (TREE_CODE (name) == TYPE_DECL)
1575 name = DECL_NAME (name);
1577 /* FIXME: This is unsatisfactory for LTO, since it relies on other
1578 metadata determining the source FE. */
1579 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1581 if (flag_next_runtime)
1583 if (flag_objc_abi == 2)
1584 return darwin_sections[objc2_constant_string_object_section];
1585 else
1586 return darwin_sections[objc_constant_string_object_section];
1588 else
1589 return darwin_sections[objc_string_object_section];
1591 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
1592 return darwin_sections[cfstring_constant_object_section];
1593 else
1594 return base_section;
1596 /* c) legacy meta-data selection. */
1597 else if (TREE_CODE (decl) == VAR_DECL
1598 && DECL_NAME (decl)
1599 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1600 && IDENTIFIER_POINTER (DECL_NAME (decl))
1601 && flag_next_runtime
1602 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1604 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1605 static bool warned_objc_46 = false;
1606 /* We shall assert that zero-sized objects are an error in ObjC
1607 meta-data. */
1608 gcc_assert (tree_low_cst (DECL_SIZE_UNIT (decl), 1) != 0);
1610 /* ??? This mechanism for determining the metadata section is
1611 broken when LTO is in use, since the frontend that generated
1612 the data is not identified. We will keep the capability for
1613 the short term - in case any non-Objective-C programs are using
1614 it to place data in specified sections. */
1615 if (!warned_objc_46)
1617 location_t loc = DECL_SOURCE_LOCATION (decl);
1618 warning_at (loc, 0, "the use of _OBJC_-prefixed variable names"
1619 " to select meta-data sections is deprecated at 4.6"
1620 " and will be removed in 4.7");
1621 warned_objc_46 = true;
1624 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1625 return darwin_sections[objc_cls_meth_section];
1626 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1627 return darwin_sections[objc_inst_meth_section];
1628 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29))
1629 return darwin_sections[objc_cat_cls_meth_section];
1630 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32))
1631 return darwin_sections[objc_cat_inst_meth_section];
1632 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1633 return darwin_sections[objc_class_vars_section];
1634 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1635 return darwin_sections[objc_instance_vars_section];
1636 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1637 return darwin_sections[objc_cat_cls_meth_section];
1638 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1639 return darwin_sections[objc_class_names_section];
1640 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1641 return darwin_sections[objc_meth_var_names_section];
1642 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1643 return darwin_sections[objc_meth_var_types_section];
1644 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1645 return darwin_sections[objc_cls_refs_section];
1646 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1647 return darwin_sections[objc_class_section];
1648 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1649 return darwin_sections[objc_meta_class_section];
1650 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1651 return darwin_sections[objc_category_section];
1652 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1653 return darwin_sections[objc_selector_refs_section];
1654 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1655 return darwin_sections[objc_selector_fixup_section];
1656 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1657 return darwin_sections[objc_symbols_section];
1658 else if (!strncmp (name, "_OBJC_MODULES", 13))
1659 return darwin_sections[objc_module_info_section];
1660 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1661 return darwin_sections[objc_image_info_section];
1662 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1663 return darwin_sections[objc_cat_inst_meth_section];
1664 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1665 return darwin_sections[objc_cat_cls_meth_section];
1666 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1667 return darwin_sections[objc_cat_cls_meth_section];
1668 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1669 return darwin_sections[objc_protocol_section];
1670 else
1671 return base_section;
1674 return base_section;
1677 /* This can be called with address expressions as "rtx".
1678 They must go in "const". */
1680 section *
1681 machopic_select_rtx_section (enum machine_mode mode, rtx x,
1682 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1684 if (GET_MODE_SIZE (mode) == 8
1685 && (GET_CODE (x) == CONST_INT
1686 || GET_CODE (x) == CONST_DOUBLE))
1687 return darwin_sections[literal8_section];
1688 else if (GET_MODE_SIZE (mode) == 4
1689 && (GET_CODE (x) == CONST_INT
1690 || GET_CODE (x) == CONST_DOUBLE))
1691 return darwin_sections[literal4_section];
1692 else if (HAVE_GAS_LITERAL16
1693 && TARGET_64BIT
1694 && GET_MODE_SIZE (mode) == 16
1695 && (GET_CODE (x) == CONST_INT
1696 || GET_CODE (x) == CONST_DOUBLE
1697 || GET_CODE (x) == CONST_VECTOR))
1698 return darwin_sections[literal16_section];
1699 else if (MACHOPIC_INDIRECT
1700 && (GET_CODE (x) == SYMBOL_REF
1701 || GET_CODE (x) == CONST
1702 || GET_CODE (x) == LABEL_REF))
1703 return darwin_sections[const_data_section];
1704 else
1705 return darwin_sections[const_section];
1708 void
1709 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1711 if (MACHOPIC_INDIRECT)
1712 switch_to_section (darwin_sections[mod_init_section]);
1713 else
1714 switch_to_section (darwin_sections[constructor_section]);
1715 assemble_align (POINTER_SIZE);
1716 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1718 if (! MACHOPIC_INDIRECT)
1719 fprintf (asm_out_file, ".reference .constructors_used\n");
1722 void
1723 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1725 if (MACHOPIC_INDIRECT)
1726 switch_to_section (darwin_sections[mod_term_section]);
1727 else
1728 switch_to_section (darwin_sections[destructor_section]);
1729 assemble_align (POINTER_SIZE);
1730 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1732 if (! MACHOPIC_INDIRECT)
1733 fprintf (asm_out_file, ".reference .destructors_used\n");
1736 void
1737 darwin_globalize_label (FILE *stream, const char *name)
1739 if (!!strncmp (name, "_OBJC_", 6))
1740 default_globalize_label (stream, name);
1743 /* This routine returns non-zero if 'name' starts with the special objective-c
1744 anonymous file-scope static name. It accommodates c++'s mangling of such
1745 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
1747 int
1748 darwin_label_is_anonymous_local_objc_name (const char *name)
1750 const unsigned char *p = (const unsigned char *) name;
1751 if (*p != '_')
1752 return 0;
1753 if (p[1] == 'Z' && p[2] == 'L')
1755 p += 3;
1756 while (*p >= '0' && *p <= '9')
1757 p++;
1759 return (!strncmp ((const char *)p, "_OBJC_", 6));
1762 /* LTO support for Mach-O.
1764 This version uses three mach-o sections to encapsulate the (unlimited
1765 number of) lto sections.
1767 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
1768 __GNU_LTO, __section_names contains the GNU LTO section names.
1769 __GNU_LTO, __section_index contains an array of values that index these.
1771 Indexed thus:
1772 <section offset from the start of __GNU_LTO, __lto_sections>,
1773 <section length>
1774 <name offset from the start of __GNU_LTO, __section_names,
1775 <name length>.
1777 At present, for both m32 and m64 mach-o files each of these fields is
1778 represented by a uint32_t. This is because, AFAICT, a mach-o object
1779 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1781 uint32_t offset;
1782 "offset An integer specifying the offset to this section in the file." */
1784 /* Count lto section numbers. */
1785 static unsigned int lto_section_num = 0;
1787 /* A vector of information about LTO sections, at present, we only have
1788 the name. TODO: see if we can get the data length somehow. */
1789 typedef struct GTY (()) darwin_lto_section_e {
1790 const char *sectname;
1791 } darwin_lto_section_e ;
1793 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
1795 /* Segment for LTO data. */
1796 #define LTO_SEGMENT_NAME "__GNU_LTO"
1798 /* Section wrapper scheme (used here to wrap the unlimited number of LTO
1799 sections into three Mach-O ones).
1800 NOTE: These names MUST be kept in sync with those in
1801 libiberty/simple-object-mach-o. */
1802 #define LTO_SECTS_SECTION "__wrapper_sects"
1803 #define LTO_NAMES_SECTION "__wrapper_names"
1804 #define LTO_INDEX_SECTION "__wrapper_index"
1806 /* File to temporarily store LTO data. This is appended to asm_out_file
1807 in darwin_end_file. */
1808 static FILE *lto_asm_out_file, *saved_asm_out_file;
1809 static char *lto_asm_out_name;
1811 /* Prepare asm_out_file for LTO output. For darwin, this means hiding
1812 asm_out_file and switching to an alternative output file. */
1813 void
1814 darwin_asm_lto_start (void)
1816 gcc_assert (! saved_asm_out_file);
1817 saved_asm_out_file = asm_out_file;
1818 if (! lto_asm_out_name)
1819 lto_asm_out_name = make_temp_file (".lto.s");
1820 lto_asm_out_file = fopen (lto_asm_out_name, "a");
1821 if (lto_asm_out_file == NULL)
1822 fatal_error ("failed to open temporary file %s for LTO output",
1823 lto_asm_out_name);
1824 asm_out_file = lto_asm_out_file;
1827 /* Restore asm_out_file. */
1828 void
1829 darwin_asm_lto_end (void)
1831 gcc_assert (saved_asm_out_file);
1832 fclose (lto_asm_out_file);
1833 asm_out_file = saved_asm_out_file;
1834 saved_asm_out_file = NULL;
1837 static void
1838 darwin_asm_dwarf_section (const char *name, unsigned int flags, tree decl);
1840 /* Called for the TARGET_ASM_NAMED_SECTION hook. */
1842 void
1843 darwin_asm_named_section (const char *name,
1844 unsigned int flags,
1845 tree decl ATTRIBUTE_UNUSED)
1847 /* LTO sections go in a special section that encapsulates the (unlimited)
1848 number of GNU LTO sections within a single mach-o one. */
1849 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1850 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1852 darwin_lto_section_e e;
1853 /* We expect certain flags to be set... */
1854 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1855 == (SECTION_DEBUG | SECTION_NAMED));
1857 /* Switch to our combined section. */
1858 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
1859 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
1860 /* Output a label for the start of this sub-section. */
1861 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
1862 lto_section_num, name);
1863 /* We have to jump through hoops to get the values of the intra-section
1864 offsets... */
1865 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
1866 lto_section_num, lto_section_num);
1867 fprintf (asm_out_file,
1868 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
1869 lto_section_num, lto_section_num+1, lto_section_num);
1870 lto_section_num++;
1871 e.sectname = xstrdup (name);
1872 /* Keep the names, we'll need to make a table later.
1873 TODO: check that we do not revisit sections, that would break
1874 the assumption of how this is done. */
1875 if (lto_section_names == NULL)
1876 vec_alloc (lto_section_names, 16);
1877 vec_safe_push (lto_section_names, e);
1879 else if (strncmp (name, "__DWARF,", 8) == 0)
1880 darwin_asm_dwarf_section (name, flags, decl);
1881 else
1882 fprintf (asm_out_file, "\t.section %s\n", name);
1885 void
1886 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
1888 /* Darwin does not use unique sections. */
1891 /* Handle __attribute__ ((apple_kext_compatibility)).
1892 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
1893 vtable for classes with this attribute (and their descendants) by not
1894 outputting the new 3.0 nondeleting destructor. This means that such
1895 objects CANNOT be allocated on the stack or as globals UNLESS they have
1896 a completely empty `operator delete'.
1897 Luckily, this fits in with the Darwin kext model.
1899 This attribute also disables gcc3's potential overlaying of derived
1900 class data members on the padding at the end of the base class. */
1902 tree
1903 darwin_handle_kext_attribute (tree *node, tree name,
1904 tree args ATTRIBUTE_UNUSED,
1905 int flags ATTRIBUTE_UNUSED,
1906 bool *no_add_attrs)
1908 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
1909 if (! TARGET_KEXTABI)
1911 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
1912 "only when compiling a kext", name);
1914 *no_add_attrs = true;
1916 else if (TREE_CODE (*node) != RECORD_TYPE)
1918 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
1919 "only to C++ classes", name);
1921 *no_add_attrs = true;
1924 return NULL_TREE;
1927 /* Handle a "weak_import" attribute; arguments as in
1928 struct attribute_spec.handler. */
1930 tree
1931 darwin_handle_weak_import_attribute (tree *node, tree name,
1932 tree ARG_UNUSED (args),
1933 int ARG_UNUSED (flags),
1934 bool * no_add_attrs)
1936 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
1938 warning (OPT_Wattributes, "%qE attribute ignored",
1939 name);
1940 *no_add_attrs = true;
1942 else
1943 declare_weak (*node);
1945 return NULL_TREE;
1948 /* Emit a label for an FDE, making it global and/or weak if appropriate.
1949 The third parameter is nonzero if this is for exception handling.
1950 The fourth parameter is nonzero if this is just a placeholder for an
1951 FDE that we are omitting. */
1953 void
1954 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
1956 char *lab ;
1957 char buf[32];
1958 static int invok_count = 0;
1959 static tree last_fun_decl = NULL_TREE;
1961 /* We use the linker to emit the .eh labels for Darwin 9 and above. */
1962 if (! for_eh || generating_for_darwin_version >= 9)
1963 return;
1965 /* FIXME: This only works when the eh for all sections of a function is
1966 emitted at the same time. If that changes, we would need to use a lookup
1967 table of some form to determine what to do. Also, we should emit the
1968 unadorned label for the partition containing the public label for a
1969 function. This is of limited use, probably, since we do not currently
1970 enable partitioning. */
1971 strcpy (buf, ".eh");
1972 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
1974 if (decl == last_fun_decl)
1976 invok_count++;
1977 snprintf (buf, 31, "$$part$$%d.eh", invok_count);
1979 else
1981 last_fun_decl = decl;
1982 invok_count = 0;
1986 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
1988 if (TREE_PUBLIC (decl))
1990 targetm.asm_out.globalize_label (file, lab);
1991 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
1993 fputs ("\t.private_extern ", file);
1994 assemble_name (file, lab);
1995 fputc ('\n', file);
1999 if (DECL_WEAK (decl))
2001 fputs ("\t.weak_definition ", file);
2002 assemble_name (file, lab);
2003 fputc ('\n', file);
2006 assemble_name (file, lab);
2007 if (empty)
2009 fputs (" = 0\n", file);
2011 /* Mark the absolute .eh and .eh1 style labels as needed to
2012 ensure that we don't dead code strip them and keep such
2013 labels from another instantiation point until we can fix this
2014 properly with group comdat support. */
2015 darwin_mark_decl_preserved (lab);
2017 else
2018 fputs (":\n", file);
2020 free (lab);
2023 static GTY(()) unsigned long except_table_label_num;
2025 void
2026 darwin_emit_except_table_label (FILE *file)
2028 char section_start_label[30];
2030 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
2031 except_table_label_num++);
2032 ASM_OUTPUT_LABEL (file, section_start_label);
2034 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
2036 void
2037 darwin_non_lazy_pcrel (FILE *file, rtx addr)
2039 const char *nlp_name;
2041 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2043 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2044 fputs ("\t.long\t", file);
2045 ASM_OUTPUT_LABELREF (file, nlp_name);
2046 fputs ("-.", file);
2049 /* If this is uncommented, details of each allocation will be printed
2050 in the asm right before the actual code. WARNING - this will cause some
2051 test-suite fails (since the printout will contain items that some tests
2052 are not expecting) -- so don't leave it on by default (it bloats the
2053 asm too). */
2054 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
2056 /* The first two of these routines are ostensibly just intended to put
2057 names into the asm. However, they are both hijacked in order to ensure
2058 that zero-sized items do not make their way into the output. Consequently,
2059 we also need to make these participate in provisions for dealing with
2060 such items in section anchors. */
2062 /* The implementation of ASM_DECLARE_OBJECT_NAME. */
2063 /* The RTTI data (e.g., __ti4name) is common and public (and static),
2064 but it does need to be referenced via indirect PIC data pointers.
2065 The machopic_define_symbol calls are telling the machopic subsystem
2066 that the name *is* defined in this module, so it doesn't need to
2067 make them indirect. */
2068 void
2069 darwin_asm_declare_object_name (FILE *file,
2070 const char *nam, tree decl)
2072 const char *xname = nam;
2073 unsigned HOST_WIDE_INT size;
2074 bool local_def, weak;
2076 weak = (DECL_P (decl)
2077 && DECL_WEAK (decl)
2078 && !lookup_attribute ("weak_import",
2079 DECL_ATTRIBUTES (decl)));
2081 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
2082 && (!DECL_COMMON (decl)
2083 || !TREE_PUBLIC (decl)));
2085 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
2086 xname = IDENTIFIER_POINTER (DECL_NAME (decl));
2088 if (local_def)
2090 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2091 if (!weak)
2092 machopic_define_symbol (DECL_RTL (decl));
2095 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
2097 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2098 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
2099 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
2100 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
2101 (unsigned long long)size, DECL_ALIGN (decl), local_def,
2102 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2103 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
2104 (unsigned long)DECL_INITIAL (decl));
2105 #endif
2107 /* Darwin needs help to support local zero-sized objects.
2108 They must be made at least one byte, and the section containing must be
2109 marked as unsuitable for section-anchors (see storage allocators below).
2111 For non-zero objects this output is handled by varasm.c.
2113 if (!size)
2115 unsigned int l2align = 0;
2117 /* The align must be honored, even for zero-sized. */
2118 if (DECL_ALIGN (decl))
2120 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
2121 fprintf (file, "\t.align\t%u\n", l2align);
2124 ASM_OUTPUT_LABEL (file, xname);
2125 size = 1;
2126 fprintf (file, "\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2128 /* Check that we've correctly picked up the zero-sized item and placed it
2129 properly. */
2130 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2131 || (in_section
2132 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2134 else
2135 ASM_OUTPUT_LABEL (file, xname);
2138 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
2139 void
2140 darwin_asm_declare_constant_name (FILE *file, const char *name,
2141 const_tree exp ATTRIBUTE_UNUSED,
2142 HOST_WIDE_INT size)
2144 assemble_label (file, name);
2145 /* As for other items, we need at least one byte. */
2146 if (!size)
2148 fputs ("\t.space\t1\n", file);
2149 /* Check that we've correctly picked up the zero-sized item and placed it
2150 properly. */
2151 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2152 || (in_section
2153 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2157 /* Darwin storage allocators.
2159 Zerofill sections are desirable for large blank data since, otherwise, these
2160 data bloat objects (PR33210).
2162 However, section anchors don't work in .zerofill sections (one cannot switch
2163 to a zerofill section). Ergo, for Darwin targets using section anchors we need
2164 to put (at least some) data into 'normal' switchable sections.
2166 Here we set a relatively arbitrary value for the size of an object to trigger
2167 zerofill when section anchors are enabled (anything bigger than a page for
2168 current Darwin implementations). FIXME: there ought to be some objective way
2169 to make this choice.
2171 When section anchor are off this is ignored anyway. */
2173 #define BYTES_ZFILL 4096
2175 /* Emit a chunk of data for items coalesced by the linker. */
2176 static void
2177 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
2178 unsigned HOST_WIDE_INT size,
2179 unsigned int align)
2181 /* Since the sections used here are coalesed, they will not be eligible
2182 for section anchors, and therefore we don't need to break that out. */
2183 if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
2184 switch_to_section (darwin_sections[const_data_coal_section]);
2185 else
2186 switch_to_section (darwin_sections[data_coal_section]);
2188 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
2189 the align info for zero-sized items... but do it here otherwise. */
2190 if (size && align)
2191 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2193 if (TREE_PUBLIC (decl))
2194 darwin_globalize_label (fp, name);
2196 /* ... and we let it deal with outputting one byte of zero for them too. */
2197 darwin_asm_declare_object_name (fp, name, decl);
2198 if (size)
2199 assemble_zeros (size);
2202 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
2203 static void
2204 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
2205 unsigned HOST_WIDE_INT size,
2206 unsigned int align, tree meta)
2208 section *ocs = data_section;
2210 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
2211 ocs = darwin_objc2_section (decl, meta, ocs);
2212 else
2213 ocs = darwin_objc1_section (decl, meta, ocs);
2215 switch_to_section (ocs);
2217 /* We shall declare that zero-sized meta-data are not valid (yet). */
2218 gcc_assert (size);
2219 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2221 /* ... and we let it deal with outputting one byte of zero for them too. */
2222 darwin_asm_declare_object_name (fp, name, decl);
2223 assemble_zeros (size);
2226 /* This routine emits 'local' storage:
2228 When Section Anchors are off this routine emits .zerofill commands in
2229 sections named for their alignment.
2231 When Section Anchors are on, smaller (non-zero-sized) items are placed in
2232 the .static_data section so that the section anchoring system can see them.
2233 Larger items are still placed in .zerofill sections, addressing PR33210.
2234 The routine has no checking - it is all assumed to be done by the caller.
2236 static void
2237 darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
2238 unsigned HOST_WIDE_INT size,
2239 unsigned int l2align)
2241 /* FIXME: We have a fudge to make this work with Java even when the target does
2242 not use sections anchors -- Java seems to need at least one small item in a
2243 non-zerofill segment. */
2244 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2245 || (size && size <= 2))
2247 /* Put smaller objects in _static_data, where the section anchors system
2248 can get them.
2249 However, if they are zero-sized punt them to yet a different section
2250 (that is not allowed to participate in anchoring). */
2251 if (!size)
2253 fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
2254 in_section = darwin_sections[zobj_bss_section];
2255 size = 1;
2257 else
2259 fputs ("\t.static_data\n", fp);
2260 in_section = darwin_sections[static_data_section];
2263 if (l2align)
2264 fprintf (fp, "\t.align\t%u\n", l2align);
2266 assemble_name (fp, name);
2267 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2269 else
2271 /* When we are on a non-section anchor target, we can get zero-sized
2272 items here. However, all we need to do is to bump them to one byte
2273 and the section alignment will take care of the rest. */
2274 char secnam[64];
2275 unsigned int flags ;
2276 snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"),
2277 (unsigned) l2align);
2278 /* We can't anchor (yet, if ever) in zerofill sections, because we can't
2279 switch to them and emit a label. */
2280 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2281 in_section = get_section (secnam, flags, NULL);
2282 fprintf (fp, "\t.zerofill %s,", secnam);
2283 assemble_name (fp, name);
2284 if (!size)
2285 size = 1;
2287 if (l2align)
2288 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2289 size, (unsigned) l2align);
2290 else
2291 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2294 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2295 /* This is defined as a file-scope var, so we know to notify machopic. */
2296 machopic_define_symbol (DECL_RTL (decl));
2299 /* Emit a chunk of common. */
2300 static void
2301 darwin_emit_common (FILE *fp, const char *name,
2302 unsigned HOST_WIDE_INT size, unsigned int align)
2304 unsigned HOST_WIDE_INT rounded;
2305 unsigned int l2align;
2307 /* Earlier systems complain if the alignment exceeds the page size.
2308 The magic number is 4096 * 8 - hard-coded for legacy systems. */
2309 if (!emit_aligned_common && (align > 32768UL))
2310 align = 4096UL; /* In units. */
2311 else
2312 align /= BITS_PER_UNIT;
2314 /* Make sure we have a meaningful align. */
2315 if (!align)
2316 align = 1;
2318 /* For earlier toolchains, we need to emit the var as a rounded size to
2319 tell ld the alignment. */
2320 if (size < align)
2321 rounded = align;
2322 else
2323 rounded = (size + (align-1)) & ~(align-1);
2325 l2align = floor_log2 (align);
2326 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2328 in_section = comm_section;
2329 /* We mustn't allow multiple public symbols to share an address when using
2330 the normal OSX toolchain. */
2331 if (!size)
2333 /* Put at least one byte. */
2334 size = 1;
2335 /* This section can no longer participate in section anchoring. */
2336 comm_section->common.flags |= SECTION_NO_ANCHOR;
2339 fputs ("\t.comm\t", fp);
2340 assemble_name (fp, name);
2341 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
2342 emit_aligned_common?size:rounded);
2343 if (l2align && emit_aligned_common)
2344 fprintf (fp, ",%u", l2align);
2345 fputs ("\n", fp);
2348 /* Output a var which is all zero - into aligned BSS sections, common, lcomm
2349 or coalescable data sections (for weak or comdat) as appropriate. */
2351 void
2352 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
2353 unsigned HOST_WIDE_INT size, unsigned int align)
2355 unsigned int l2align;
2356 bool one, pub, weak;
2357 tree meta;
2359 pub = TREE_PUBLIC (decl);
2360 one = DECL_ONE_ONLY (decl);
2361 weak = (DECL_P (decl)
2362 && DECL_WEAK (decl)
2363 && !lookup_attribute ("weak_import",
2364 DECL_ATTRIBUTES (decl)));
2366 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2367 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
2368 " pub %d weak %d one %d init %lx\n",
2369 name, (long long)size, (int)align, TREE_READONLY (decl),
2370 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2371 pub, weak, one, (unsigned long)DECL_INITIAL (decl));
2372 #endif
2374 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2375 before the target has a chance to comment. */
2376 if ((meta = is_objc_metadata (decl)))
2378 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2379 return;
2382 /* Check that any initializer is valid. */
2383 gcc_assert ((DECL_INITIAL (decl) == NULL)
2384 || (DECL_INITIAL (decl) == error_mark_node)
2385 || initializer_zerop (DECL_INITIAL (decl)));
2387 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2388 gcc_assert (!DECL_COMMON (decl));
2390 /* Pick up the correct alignment. */
2391 if (!size || !align)
2392 align = DECL_ALIGN (decl);
2394 l2align = floor_log2 (align / BITS_PER_UNIT);
2395 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2397 last_assemble_variable_decl = decl;
2399 /* We would rather not have to check this here - but it seems that we might
2400 be passed a decl that should be in coalesced space. */
2401 if (one || weak)
2403 /* Weak or COMDAT objects are put in mergeable sections. */
2404 darwin_emit_weak_or_comdat (fp, decl, name, size,
2405 DECL_ALIGN (decl));
2406 return;
2409 /* If this is not public, then emit according to local rules. */
2410 if (!pub)
2412 darwin_emit_local_bss (fp, decl, name, size, l2align);
2413 return;
2416 /* So we have a public symbol (small item fudge for Java, see above). */
2417 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2418 || (size && size <= 2))
2420 /* Put smaller objects in data, where the section anchors system can get
2421 them. However, if they are zero-sized punt them to yet a different
2422 section (that is not allowed to participate in anchoring). */
2423 if (!size)
2425 fputs ("\t.section\t__DATA,__zobj_data\n", fp);
2426 in_section = darwin_sections[zobj_data_section];
2427 size = 1;
2429 else
2431 fputs ("\t.data\n", fp);
2432 in_section = data_section;
2435 if (l2align)
2436 fprintf (fp, "\t.align\t%u\n", l2align);
2438 assemble_name (fp, name);
2439 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2441 else
2443 char secnam[64];
2444 unsigned int flags ;
2445 /* When we are on a non-section anchor target, we can get zero-sized
2446 items here. However, all we need to do is to bump them to one byte
2447 and the section alignment will take care of the rest. */
2448 snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align);
2450 /* We can't anchor in zerofill sections, because we can't switch
2451 to them and emit a label. */
2452 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2453 in_section = get_section (secnam, flags, NULL);
2454 fprintf (fp, "\t.zerofill %s,", secnam);
2455 assemble_name (fp, name);
2456 if (!size)
2457 size = 1;
2459 if (l2align)
2460 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
2461 else
2462 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2464 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2467 /* Output a chunk of common, with alignment specified (where the target
2468 supports this). */
2469 void
2470 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
2471 unsigned HOST_WIDE_INT size,
2472 unsigned int align)
2474 unsigned int l2align;
2475 bool one, weak;
2476 tree meta;
2478 /* No corresponding var. */
2479 if (decl==NULL)
2481 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2482 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
2483 #endif
2484 darwin_emit_common (fp, name, size, align);
2485 return;
2488 one = DECL_ONE_ONLY (decl);
2489 weak = (DECL_P (decl)
2490 && DECL_WEAK (decl)
2491 && !lookup_attribute ("weak_import",
2492 DECL_ATTRIBUTES (decl)));
2494 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2495 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
2496 " weak %d one %d init %lx\n",
2497 name, (long long)size, (int)align, TREE_READONLY (decl),
2498 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2499 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
2500 #endif
2502 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2503 before the target has a chance to comment. */
2504 if ((meta = is_objc_metadata (decl)))
2506 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2507 return;
2510 /* We shouldn't be messing with this if the decl has a section name. */
2511 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2513 /* We would rather not have to check this here - but it seems that we might
2514 be passed a decl that should be in coalesced space. */
2515 if (one || weak)
2517 /* Weak or COMDAT objects are put in mergable sections. */
2518 darwin_emit_weak_or_comdat (fp, decl, name, size,
2519 DECL_ALIGN (decl));
2520 return;
2523 /* We should only get here for DECL_COMMON, with a zero init (and, in
2524 principle, only for public symbols too - although we deal with local
2525 ones below). */
2527 /* Check the initializer is OK. */
2528 gcc_assert (DECL_COMMON (decl)
2529 && ((DECL_INITIAL (decl) == NULL)
2530 || (DECL_INITIAL (decl) == error_mark_node)
2531 || initializer_zerop (DECL_INITIAL (decl))));
2533 last_assemble_variable_decl = decl;
2535 if (!size || !align)
2536 align = DECL_ALIGN (decl);
2538 l2align = floor_log2 (align / BITS_PER_UNIT);
2539 /* Check we aren't asking for more aligment than the platform allows. */
2540 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2542 if (TREE_PUBLIC (decl) != 0)
2543 darwin_emit_common (fp, name, size, align);
2544 else
2545 darwin_emit_local_bss (fp, decl, name, size, l2align);
2548 /* Output a chunk of BSS with alignment specfied. */
2549 void
2550 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
2551 unsigned HOST_WIDE_INT size,
2552 unsigned int align)
2554 unsigned long l2align;
2555 bool one, weak;
2556 tree meta;
2558 one = DECL_ONE_ONLY (decl);
2559 weak = (DECL_P (decl)
2560 && DECL_WEAK (decl)
2561 && !lookup_attribute ("weak_import",
2562 DECL_ATTRIBUTES (decl)));
2564 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2565 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
2566 " weak %d init %lx\n",
2567 name, (long long)size, (int)align, TREE_READONLY (decl),
2568 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
2569 weak , (unsigned long)DECL_INITIAL (decl));
2570 #endif
2572 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2573 before the target has a chance to comment. */
2574 if ((meta = is_objc_metadata (decl)))
2576 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2577 return;
2580 /* We shouldn't be messing with this if the decl has a section name. */
2581 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2583 /* We would rather not have to check this here - but it seems that we might
2584 be passed a decl that should be in coalesced space. */
2585 if (one || weak)
2587 /* Weak or COMDAT objects are put in mergable sections. */
2588 darwin_emit_weak_or_comdat (fp, decl, name, size,
2589 DECL_ALIGN (decl));
2590 return;
2593 /* .. and it should be suitable for placement in local mem. */
2594 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
2595 /* .. and any initializer must be all-zero. */
2596 gcc_assert ((DECL_INITIAL (decl) == NULL)
2597 || (DECL_INITIAL (decl) == error_mark_node)
2598 || initializer_zerop (DECL_INITIAL (decl)));
2600 last_assemble_variable_decl = decl;
2602 if (!size || !align)
2603 align = DECL_ALIGN (decl);
2605 l2align = floor_log2 (align / BITS_PER_UNIT);
2606 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2608 darwin_emit_local_bss (fp, decl, name, size, l2align);
2611 /* Emit an assembler directive to set visibility for a symbol. The
2612 only supported visibilities are VISIBILITY_DEFAULT and
2613 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
2614 extern". There is no MACH-O equivalent of ELF's
2615 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
2617 void
2618 darwin_assemble_visibility (tree decl, int vis)
2620 if (vis == VISIBILITY_DEFAULT)
2622 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
2624 fputs ("\t.private_extern ", asm_out_file);
2625 assemble_name (asm_out_file,
2626 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
2627 fputs ("\n", asm_out_file);
2629 else
2630 warning (OPT_Wattributes, "protected visibility attribute "
2631 "not supported in this configuration; ignored");
2634 /* vec used by darwin_asm_dwarf_section.
2635 Maybe a hash tab would be better here - but the intention is that this is
2636 a very short list (fewer than 16 items) and each entry should (ideally,
2637 eventually) only be presented once.
2639 A structure to hold a dwarf debug section used entry. */
2641 typedef struct GTY(()) dwarf_sect_used_entry {
2642 const char *name;
2643 unsigned count;
2645 dwarf_sect_used_entry;
2648 /* A list of used __DWARF sections. */
2649 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
2651 /* This is called when we are asked to assemble a named section and the
2652 name begins with __DWARF,. We keep a list of the section names (without
2653 the __DWARF, prefix) and use this to emit our required start label on the
2654 first switch to each section. */
2656 static void
2657 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2658 tree ARG_UNUSED (decl))
2660 unsigned i;
2661 int namelen;
2662 const char * sname;
2663 dwarf_sect_used_entry *ref;
2664 bool found = false;
2665 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2666 == (SECTION_DEBUG | SECTION_NAMED));
2667 /* We know that the name starts with __DWARF, */
2668 sname = name + 8;
2669 namelen = strchr (sname, ',') - sname;
2670 gcc_assert (namelen);
2671 if (dwarf_sect_names_table == NULL)
2672 vec_alloc (dwarf_sect_names_table, 16);
2673 else
2674 for (i = 0;
2675 dwarf_sect_names_table->iterate (i, &ref);
2676 i++)
2678 if (!ref)
2679 break;
2680 if (!strcmp (ref->name, sname))
2682 found = true;
2683 ref->count++;
2684 break;
2688 fprintf (asm_out_file, "\t.section %s\n", name);
2689 if (!found)
2691 dwarf_sect_used_entry e;
2692 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
2693 e.count = 1;
2694 e.name = xstrdup (sname);
2695 vec_safe_push (dwarf_sect_names_table, e);
2699 /* Output a difference of two labels that will be an assembly time
2700 constant if the two labels are local. (.long lab1-lab2 will be
2701 very different if lab1 is at the boundary between two sections; it
2702 will be relocated according to the second section, not the first,
2703 so one ends up with a difference between labels in different
2704 sections, which is bad in the dwarf2 eh context for instance.) */
2706 static int darwin_dwarf_label_counter;
2708 void
2709 darwin_asm_output_dwarf_delta (FILE *file, int size,
2710 const char *lab1, const char *lab2)
2712 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
2713 && lab2[0] == '*' && lab2[1] == 'L');
2714 const char *directive = (size == 8 ? ".quad" : ".long");
2716 if (islocaldiff)
2717 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
2718 else
2719 fprintf (file, "\t%s\t", directive);
2721 assemble_name_raw (file, lab1);
2722 fprintf (file, "-");
2723 assemble_name_raw (file, lab2);
2724 if (islocaldiff)
2725 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
2728 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
2729 offsets are not represented using relocs in .o files; either the
2730 section never leaves the .o file, or the linker or other tool is
2731 responsible for parsing the DWARF and updating the offsets. */
2733 void
2734 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
2735 section *base)
2737 char sname[64];
2738 int namelen;
2740 gcc_assert (base->common.flags & SECTION_NAMED);
2741 gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
2742 gcc_assert (strchr (base->named.name + 8, ','));
2744 namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
2745 sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
2746 darwin_asm_output_dwarf_delta (file, size, lab, sname);
2749 /* Called from the within the TARGET_ASM_FILE_START for each target. */
2751 void
2752 darwin_file_start (void)
2754 /* Nothing to do. */
2757 /* Called for the TARGET_ASM_FILE_END hook.
2758 Emit the mach-o pic indirection data, the lto data and, finally a flag
2759 to tell the linker that it can break the file object into sections and
2760 move those around for efficiency. */
2762 void
2763 darwin_file_end (void)
2765 machopic_finish (asm_out_file);
2766 if (strcmp (lang_hooks.name, "GNU C++") == 0)
2768 switch_to_section (darwin_sections[constructor_section]);
2769 switch_to_section (darwin_sections[destructor_section]);
2770 ASM_OUTPUT_ALIGN (asm_out_file, 1);
2773 /* If there was LTO assembler output, append it to asm_out_file. */
2774 if (lto_asm_out_name)
2776 int n;
2777 char *buf, *lto_asm_txt;
2779 /* Shouldn't be here if we failed to switch back. */
2780 gcc_assert (! saved_asm_out_file);
2782 lto_asm_out_file = fopen (lto_asm_out_name, "r");
2783 if (lto_asm_out_file == NULL)
2784 fatal_error ("failed to open temporary file %s with LTO output",
2785 lto_asm_out_name);
2786 fseek (lto_asm_out_file, 0, SEEK_END);
2787 n = ftell (lto_asm_out_file);
2788 if (n > 0)
2790 fseek (lto_asm_out_file, 0, SEEK_SET);
2791 lto_asm_txt = buf = (char *) xmalloc (n + 1);
2792 while (fgets (lto_asm_txt, n, lto_asm_out_file))
2793 fputs (lto_asm_txt, asm_out_file);
2794 /* Put a termination label. */
2795 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2796 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2797 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
2798 lto_section_num);
2799 /* Make sure our termination label stays in this section. */
2800 fputs ("\t.space\t1\n", asm_out_file);
2803 /* Remove the temporary file. */
2804 fclose (lto_asm_out_file);
2805 unlink_if_ordinary (lto_asm_out_name);
2806 free (lto_asm_out_name);
2809 /* Output the names and indices. */
2810 if (lto_section_names && lto_section_names->length ())
2812 int count;
2813 darwin_lto_section_e *ref;
2814 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
2815 the latter up ourselves. */
2816 const char *op = integer_asm_op (4,0);
2818 /* Emit the names. */
2819 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2820 LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
2821 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2823 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
2824 /* We have to jump through hoops to get the values of the intra-section
2825 offsets... */
2826 fprintf (asm_out_file,
2827 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
2828 count, count);
2829 fprintf (asm_out_file,
2830 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
2831 count, count+1, count);
2832 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
2834 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
2835 /* make sure our termination label stays in this section. */
2836 fputs ("\t.space\t1\n", asm_out_file);
2838 /* Emit the Index. */
2839 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2840 LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
2841 fputs ("\t.align\t2\n", asm_out_file);
2842 fputs ("# Section offset, Section length, Name offset, Name length\n",
2843 asm_out_file);
2844 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2846 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
2847 op, count, ref->sectname);
2848 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
2849 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
2850 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
2854 /* If we have section anchors, then we must prevent the linker from
2855 re-arranging data. */
2856 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2857 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
2860 /* TODO: Add a language hook for identifying if a decl is a vtable. */
2861 #define DARWIN_VTABLE_P(DECL) 0
2863 /* Cross-module name binding. Darwin does not support overriding
2864 functions at dynamic-link time, except for vtables in kexts. */
2866 bool
2867 darwin_binds_local_p (const_tree decl)
2869 return default_binds_local_p_1 (decl,
2870 TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
2873 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
2874 anchor relative to ".", the current section position. We cannot use
2875 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
2876 void
2877 darwin_asm_output_anchor (rtx symbol)
2879 fprintf (asm_out_file, "\t.set\t");
2880 assemble_name (asm_out_file, XSTR (symbol, 0));
2881 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
2882 SYMBOL_REF_BLOCK_OFFSET (symbol));
2885 /* Disable section anchoring on any section containing a zero-sized
2886 object. */
2887 bool
2888 darwin_use_anchors_for_symbol_p (const_rtx symbol)
2890 if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
2892 section *sect;
2893 /* If the section contains a zero-sized object it's ineligible. */
2894 sect = SYMBOL_REF_BLOCK (symbol)->sect;
2895 /* This should have the effect of disabling anchors for vars that follow
2896 any zero-sized one, in a given section. */
2897 if (sect->common.flags & SECTION_NO_ANCHOR)
2898 return false;
2900 /* Also check the normal reasons for suppressing. */
2901 return default_use_anchors_for_symbol_p (symbol);
2903 else
2904 return false;
2907 /* Set the darwin specific attributes on TYPE. */
2908 void
2909 darwin_set_default_type_attributes (tree type)
2911 if (darwin_ms_struct
2912 && TREE_CODE (type) == RECORD_TYPE)
2913 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
2914 NULL_TREE,
2915 TYPE_ATTRIBUTES (type));
2918 /* True, iff we're generating code for loadable kernel extensions. */
2920 bool
2921 darwin_kextabi_p (void) {
2922 return flag_apple_kext;
2925 void
2926 darwin_override_options (void)
2928 /* Keep track of which (major) version we're generating code for. */
2929 if (darwin_macosx_version_min)
2931 if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
2932 generating_for_darwin_version = 10;
2933 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
2934 generating_for_darwin_version = 9;
2936 /* Earlier versions are not specifically accounted, until required. */
2939 /* In principle, this should be c-family only. However, we really need to
2940 set sensible defaults for LTO as well, since the section selection stuff
2941 should check for correctness re. the ABI. TODO: check and provide the
2942 flags (runtime & ABI) from the lto wrapper). */
2944 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
2945 if (!global_options_set.x_flag_objc_abi)
2946 global_options.x_flag_objc_abi
2947 = (!flag_next_runtime)
2949 : (TARGET_64BIT ? 2
2950 : (generating_for_darwin_version >= 9) ? 1
2951 : 0);
2953 /* Objective-C family ABI 2 is only valid for next/m64 at present. */
2954 if (global_options_set.x_flag_objc_abi && flag_next_runtime)
2956 if (TARGET_64BIT && global_options.x_flag_objc_abi < 2)
2957 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 must be"
2958 " used for %<-m64%> targets with"
2959 " %<-fnext-runtime%>");
2960 if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
2961 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 is not"
2962 " supported on %<-m32%> targets with"
2963 " %<-fnext-runtime%>");
2966 /* Don't emit DWARF3/4 unless specifically selected. This is a
2967 workaround for tool bugs. */
2968 if (!global_options_set.x_dwarf_strict)
2969 dwarf_strict = 1;
2970 if (!global_options_set.x_dwarf_version)
2971 dwarf_version = 2;
2973 /* Do not allow unwind tables to be generated by default for m32.
2974 fnon-call-exceptions will override this, regardless of what we do. */
2975 if (generating_for_darwin_version < 10
2976 && !global_options_set.x_flag_asynchronous_unwind_tables
2977 && !TARGET_64BIT)
2978 global_options.x_flag_asynchronous_unwind_tables = 0;
2980 /* Disable -freorder-blocks-and-partition when unwind tables are being
2981 emitted for Darwin < 9 (OSX 10.5).
2982 The strategy is, "Unless the User has specifically set/unset an unwind
2983 flag we will switch off -freorder-blocks-and-partition when unwind tables
2984 will be generated". If the User specifically sets flags... we assume
2985 (s)he knows why... */
2986 if (generating_for_darwin_version < 9
2987 && global_options_set.x_flag_reorder_blocks_and_partition
2988 && ((global_options.x_flag_exceptions /* User, c++, java */
2989 && !global_options_set.x_flag_exceptions) /* User specified... */
2990 || (global_options.x_flag_unwind_tables
2991 && !global_options_set.x_flag_unwind_tables)
2992 || (global_options.x_flag_non_call_exceptions
2993 && !global_options_set.x_flag_non_call_exceptions)
2994 || (global_options.x_flag_asynchronous_unwind_tables
2995 && !global_options_set.x_flag_asynchronous_unwind_tables)))
2997 inform (input_location,
2998 "-freorder-blocks-and-partition does not work with exceptions "
2999 "on this architecture");
3000 flag_reorder_blocks_and_partition = 0;
3001 flag_reorder_blocks = 1;
3004 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
3005 one valid choice of exception scheme for each runtime. */
3006 if (!global_options_set.x_flag_objc_sjlj_exceptions)
3007 global_options.x_flag_objc_sjlj_exceptions =
3008 flag_next_runtime && !TARGET_64BIT;
3010 /* FIXME: and this could be eliminated then too. */
3011 if (!global_options_set.x_flag_exceptions
3012 && flag_objc_exceptions
3013 && TARGET_64BIT)
3014 flag_exceptions = 1;
3016 if (flag_mkernel || flag_apple_kext)
3018 /* -mkernel implies -fapple-kext for C++ */
3019 if (strcmp (lang_hooks.name, "GNU C++") == 0)
3020 flag_apple_kext = 1;
3022 flag_no_common = 1;
3024 /* No EH in kexts. */
3025 flag_exceptions = 0;
3026 /* No -fnon-call-exceptions data in kexts. */
3027 flag_non_call_exceptions = 0;
3028 /* so no tables either.. */
3029 flag_unwind_tables = 0;
3030 flag_asynchronous_unwind_tables = 0;
3031 /* We still need to emit branch islands for kernel context. */
3032 darwin_emit_branch_islands = true;
3035 if (flag_var_tracking_uninit == 0
3036 && optimize >= 1
3037 && generating_for_darwin_version >= 9
3038 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
3039 : (debug_info_level >= DINFO_LEVEL_NORMAL))
3040 && write_symbols == DWARF2_DEBUG)
3041 flag_var_tracking_uninit = flag_var_tracking;
3043 if (MACHO_DYNAMIC_NO_PIC_P)
3045 if (flag_pic)
3046 warning_at (UNKNOWN_LOCATION, 0,
3047 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
3048 " %<-fpie%> or %<-fPIE%>");
3049 flag_pic = 0;
3051 else if (flag_pic == 1)
3053 /* Darwin's -fpic is -fPIC. */
3054 flag_pic = 2;
3057 /* It is assumed that branch island stubs are needed for earlier systems. */
3058 if (generating_for_darwin_version < 9)
3059 darwin_emit_branch_islands = true;
3060 else
3061 emit_aligned_common = true; /* Later systems can support aligned common. */
3063 /* The c_dialect...() macros are not available to us here. */
3064 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3067 #if DARWIN_PPC
3068 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
3070 static void
3071 darwin_patch_builtin (enum built_in_function fncode)
3073 tree fn = builtin_decl_explicit (fncode);
3074 tree sym;
3075 char *newname;
3077 if (!fn)
3078 return;
3080 sym = DECL_ASSEMBLER_NAME (fn);
3081 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
3083 set_user_assembler_name (fn, newname);
3085 fn = builtin_decl_implicit (fncode);
3086 if (fn)
3087 set_user_assembler_name (fn, newname);
3090 void
3091 darwin_patch_builtins (void)
3093 if (LONG_DOUBLE_TYPE_SIZE != 128)
3094 return;
3096 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
3097 #define PATCH_BUILTIN_NO64(fncode) \
3098 if (!TARGET_64BIT) \
3099 darwin_patch_builtin (fncode);
3100 #define PATCH_BUILTIN_VARIADIC(fncode) \
3101 if (!TARGET_64BIT \
3102 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
3103 darwin_patch_builtin (fncode);
3104 #include "darwin-ppc-ldouble-patch.def"
3105 #undef PATCH_BUILTIN
3106 #undef PATCH_BUILTIN_NO64
3107 #undef PATCH_BUILTIN_VARIADIC
3109 #endif
3111 /* CFStrings implementation. */
3112 static GTY(()) tree cfstring_class_reference = NULL_TREE;
3113 static GTY(()) tree cfstring_type_node = NULL_TREE;
3114 static GTY(()) tree ccfstring_type_node = NULL_TREE;
3115 static GTY(()) tree pccfstring_type_node = NULL_TREE;
3116 static GTY(()) tree pcint_type_node = NULL_TREE;
3117 static GTY(()) tree pcchar_type_node = NULL_TREE;
3119 static enum built_in_function darwin_builtin_cfstring;
3121 /* Store all constructed constant CFStrings in a hash table so that
3122 they get uniqued properly. */
3124 typedef struct GTY (()) cfstring_descriptor {
3125 /* The string literal. */
3126 tree literal;
3127 /* The resulting constant CFString. */
3128 tree constructor;
3129 } cfstring_descriptor;
3131 static GTY ((param_is (struct cfstring_descriptor))) htab_t cfstring_htab;
3133 static hashval_t cfstring_hash (const void *);
3134 static int cfstring_eq (const void *, const void *);
3136 static tree
3137 add_builtin_field_decl (tree type, const char *name, tree **chain)
3139 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
3140 get_identifier (name), type);
3142 if (*chain != NULL)
3143 **chain = field;
3144 *chain = &DECL_CHAIN (field);
3146 return field;
3149 tree
3150 darwin_init_cfstring_builtins (unsigned builtin_cfstring)
3152 tree cfsfun, fields, pccfstring_ftype_pcchar;
3153 tree *chain = NULL;
3155 darwin_builtin_cfstring =
3156 (enum built_in_function) builtin_cfstring;
3158 /* struct __builtin_CFString {
3159 const int *isa; (will point at
3160 int flags; __CFConstantStringClassReference)
3161 const char *str;
3162 long length;
3163 }; */
3165 pcint_type_node = build_pointer_type
3166 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
3168 pcchar_type_node = build_pointer_type
3169 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3171 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
3173 /* Have to build backwards for finish struct. */
3174 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
3175 add_builtin_field_decl (pcchar_type_node, "str", &chain);
3176 add_builtin_field_decl (integer_type_node, "flags", &chain);
3177 add_builtin_field_decl (pcint_type_node, "isa", &chain);
3178 finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
3179 fields, NULL_TREE);
3181 /* const struct __builtin_CFstring *
3182 __builtin___CFStringMakeConstantString (const char *); */
3184 ccfstring_type_node = build_qualified_type
3185 (cfstring_type_node, TYPE_QUAL_CONST);
3186 pccfstring_type_node = build_pointer_type (ccfstring_type_node);
3187 pccfstring_ftype_pcchar = build_function_type_list
3188 (pccfstring_type_node, pcchar_type_node, NULL_TREE);
3190 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
3191 get_identifier ("__builtin___CFStringMakeConstantString"),
3192 pccfstring_ftype_pcchar);
3194 TREE_PUBLIC (cfsfun) = 1;
3195 DECL_EXTERNAL (cfsfun) = 1;
3196 DECL_ARTIFICIAL (cfsfun) = 1;
3197 /* Make a lang-specific section - dup_lang_specific_decl makes a new node
3198 in place of the existing, which may be NULL. */
3199 DECL_LANG_SPECIFIC (cfsfun) = NULL;
3200 (*lang_hooks.dup_lang_specific_decl) (cfsfun);
3201 DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD;
3202 DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring;
3203 lang_hooks.builtin_function (cfsfun);
3205 /* extern int __CFConstantStringClassReference[]; */
3206 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
3207 get_identifier ("__CFConstantStringClassReference"),
3208 build_array_type (integer_type_node, NULL_TREE));
3210 TREE_PUBLIC (cfstring_class_reference) = 1;
3211 DECL_ARTIFICIAL (cfstring_class_reference) = 1;
3212 (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
3213 DECL_EXTERNAL (cfstring_class_reference) = 1;
3214 rest_of_decl_compilation (cfstring_class_reference, 0, 0);
3216 /* Initialize the hash table used to hold the constant CFString objects. */
3217 cfstring_htab = htab_create_ggc (31, cfstring_hash, cfstring_eq, NULL);
3219 return cfstring_type_node;
3222 tree
3223 darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
3224 bool ARG_UNUSED (ignore))
3226 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3228 if (fcode == darwin_builtin_cfstring)
3230 if (!darwin_constant_cfstrings)
3232 error ("built-in function %qD requires the"
3233 " %<-mconstant-cfstrings%> flag", fndecl);
3234 return error_mark_node;
3237 if (n_args != 1)
3239 error ("built-in function %qD takes one argument only", fndecl);
3240 return error_mark_node;
3243 return darwin_build_constant_cfstring (*argp);
3246 return NULL_TREE;
3249 void
3250 darwin_rename_builtins (void)
3252 /* The system ___divdc3 routine in libSystem on darwin10 is not
3253 accurate to 1ulp, ours is, so we avoid ever using the system name
3254 for this routine and instead install a non-conflicting name that
3255 is accurate.
3257 When -ffast-math or -funsafe-math-optimizations is given, we can
3258 use the faster version. */
3259 if (!flag_unsafe_math_optimizations)
3261 enum built_in_function dcode
3262 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
3263 + DCmode - MIN_MODE_COMPLEX_FLOAT);
3264 tree fn = builtin_decl_explicit (dcode);
3265 /* Fortran and c call TARGET_INIT_BUILTINS and
3266 TARGET_INIT_LIBFUNCS at different times, so we have to put a
3267 call into each to ensure that at least one of them is called
3268 after build_common_builtin_nodes. A better fix is to add a
3269 new hook to run after build_common_builtin_nodes runs. */
3270 if (fn)
3271 set_user_assembler_name (fn, "___ieee_divdc3");
3272 fn = builtin_decl_implicit (dcode);
3273 if (fn)
3274 set_user_assembler_name (fn, "___ieee_divdc3");
3278 static hashval_t
3279 cfstring_hash (const void *ptr)
3281 tree str = ((const struct cfstring_descriptor *)ptr)->literal;
3282 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3283 int i, len = TREE_STRING_LENGTH (str);
3284 hashval_t h = len;
3286 for (i = 0; i < len; i++)
3287 h = ((h * 613) + p[i]);
3289 return h;
3292 static int
3293 cfstring_eq (const void *ptr1, const void *ptr2)
3295 tree str1 = ((const struct cfstring_descriptor *)ptr1)->literal;
3296 tree str2 = ((const struct cfstring_descriptor *)ptr2)->literal;
3297 int len1 = TREE_STRING_LENGTH (str1);
3299 return (len1 == TREE_STRING_LENGTH (str2)
3300 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3301 len1));
3304 tree
3305 darwin_build_constant_cfstring (tree str)
3307 struct cfstring_descriptor *desc, key;
3308 void **loc;
3309 tree addr;
3311 if (!str)
3313 error ("CFString literal is missing");
3314 return error_mark_node;
3317 STRIP_NOPS (str);
3319 if (TREE_CODE (str) == ADDR_EXPR)
3320 str = TREE_OPERAND (str, 0);
3322 if (TREE_CODE (str) != STRING_CST)
3324 error ("CFString literal expression is not a string constant");
3325 return error_mark_node;
3328 /* Perhaps we already constructed a constant CFString just like this one? */
3329 key.literal = str;
3330 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3331 desc = (struct cfstring_descriptor *) *loc;
3333 if (!desc)
3335 tree var, constructor, field;
3336 vec<constructor_elt, va_gc> *v = NULL;
3337 int length = TREE_STRING_LENGTH (str) - 1;
3339 if (darwin_warn_nonportable_cfstrings)
3341 const char *s = TREE_STRING_POINTER (str);
3342 int l = 0;
3344 for (l = 0; l < length; l++)
3345 if (!s[l] || !isascii (s[l]))
3347 warning (darwin_warn_nonportable_cfstrings, "%s in CFString literal",
3348 s[l] ? "non-ASCII character" : "embedded NUL");
3349 break;
3353 *loc = desc = ggc_alloc_cleared_cfstring_descriptor ();
3354 desc->literal = str;
3356 /* isa *. */
3357 field = TYPE_FIELDS (ccfstring_type_node);
3358 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3359 build1 (ADDR_EXPR, TREE_TYPE (field),
3360 cfstring_class_reference));
3361 /* flags */
3362 field = DECL_CHAIN (field);
3363 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3364 build_int_cst (TREE_TYPE (field), 0x000007c8));
3365 /* string *. */
3366 field = DECL_CHAIN (field);
3367 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3368 build1 (ADDR_EXPR, TREE_TYPE (field), str));
3369 /* length */
3370 field = DECL_CHAIN (field);
3371 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3372 build_int_cst (TREE_TYPE (field), length));
3374 constructor = build_constructor (ccfstring_type_node, v);
3375 TREE_READONLY (constructor) = 1;
3376 TREE_CONSTANT (constructor) = 1;
3377 TREE_STATIC (constructor) = 1;
3379 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
3380 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
3381 being built without any knowledge of C++ tree accessors; hence, we shall
3382 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
3383 if (darwin_running_cxx)
3384 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
3386 /* Create an anonymous global variable for this CFString. */
3387 var = build_decl (input_location, CONST_DECL,
3388 NULL, TREE_TYPE (constructor));
3389 DECL_ARTIFICIAL (var) = 1;
3390 TREE_STATIC (var) = 1;
3391 DECL_INITIAL (var) = constructor;
3392 /* FIXME: This should use a translation_unit_decl to indicate file scope. */
3393 DECL_CONTEXT (var) = NULL_TREE;
3394 desc->constructor = var;
3397 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
3398 TREE_CONSTANT (addr) = 1;
3400 return addr;
3403 bool
3404 darwin_cfstring_p (tree str)
3406 struct cfstring_descriptor key;
3407 void **loc;
3409 if (!str)
3410 return false;
3412 STRIP_NOPS (str);
3414 if (TREE_CODE (str) == ADDR_EXPR)
3415 str = TREE_OPERAND (str, 0);
3417 if (TREE_CODE (str) != STRING_CST)
3418 return false;
3420 key.literal = str;
3421 loc = htab_find_slot (cfstring_htab, &key, NO_INSERT);
3423 if (loc)
3424 return true;
3426 return false;
3429 void
3430 darwin_enter_string_into_cfstring_table (tree str)
3432 struct cfstring_descriptor key;
3433 void **loc;
3435 key.literal = str;
3436 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3438 if (!*loc)
3440 *loc = ggc_alloc_cleared_cfstring_descriptor ();
3441 ((struct cfstring_descriptor *)*loc)->literal = str;
3445 /* Choose named function section based on its frequency. */
3447 section *
3448 darwin_function_section (tree decl, enum node_frequency freq,
3449 bool startup, bool exit)
3451 /* Decide if we need to put this in a coalescable section. */
3452 bool weak = (decl
3453 && DECL_WEAK (decl)
3454 && (!DECL_ATTRIBUTES (decl)
3455 || !lookup_attribute ("weak_import",
3456 DECL_ATTRIBUTES (decl))));
3458 /* If there is a specified section name, we should not be trying to
3459 override. */
3460 if (decl && DECL_SECTION_NAME (decl) != NULL_TREE)
3461 return get_named_section (decl, NULL, 0);
3463 /* Default when there is no function re-ordering. */
3464 if (!flag_reorder_functions)
3465 return (weak)
3466 ? darwin_sections[text_coal_section]
3467 : text_section;
3469 /* Startup code should go to startup subsection unless it is
3470 unlikely executed (this happens especially with function splitting
3471 where we can split away unnecessary parts of static constructors). */
3472 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
3473 return (weak)
3474 ? darwin_sections[text_startup_coal_section]
3475 : darwin_sections[text_startup_section];
3477 /* Similarly for exit. */
3478 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
3479 return (weak)
3480 ? darwin_sections[text_exit_coal_section]
3481 : darwin_sections[text_exit_section];
3483 /* Group cold functions together, similarly for hot code. */
3484 switch (freq)
3486 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
3487 return (weak)
3488 ? darwin_sections[text_cold_coal_section]
3489 : darwin_sections[text_cold_section];
3490 break;
3491 case NODE_FREQUENCY_HOT:
3492 return (weak)
3493 ? darwin_sections[text_hot_coal_section]
3494 : darwin_sections[text_hot_section];
3495 break;
3496 default:
3497 return (weak)
3498 ? darwin_sections[text_coal_section]
3499 : text_section;
3500 break;
3504 /* When a function is partitioned between sections, we need to insert a label
3505 at the start of each new chunk - so that it may become a valid 'atom' for
3506 eh and debug purposes. Without this the linker will emit warnings if one
3507 tries to add line location information (since the switched fragment will
3508 be anonymous). */
3510 void
3511 darwin_function_switched_text_sections (FILE *fp, tree decl, bool new_is_cold)
3513 char buf[128];
3514 snprintf (buf, 128, "%s%s",new_is_cold?"__cold_sect_of_":"__hot_sect_of_",
3515 IDENTIFIER_POINTER (DECL_NAME (decl)));
3516 /* Make sure we pick up all the relevant quotes etc. */
3517 assemble_name_raw (fp, (const char *) buf);
3518 fputs (":\n", fp);
3521 #include "gt-darwin.h"