PR target/19398
[official-gcc.git] / gcc / config / darwin.c
blob587a327c87009524a6623a85681af3699134d3b9
1 /* Functions for generic Darwin as target machine for GNU C compiler.
2 Copyright (C) 1989, 1990, 1991, 1992, 1993, 2000, 2001, 2002, 2003, 2004,
3 2005
4 Free Software Foundation, Inc.
5 Contributed by Apple Computer Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "insn-flags.h"
35 #include "output.h"
36 #include "insn-attr.h"
37 #include "flags.h"
38 #include "tree.h"
39 #include "expr.h"
40 #include "reload.h"
41 #include "function.h"
42 #include "ggc.h"
43 #include "langhooks.h"
44 #include "target.h"
45 #include "tm_p.h"
46 #include "toplev.h"
47 #include "hashtab.h"
49 /* Darwin supports a feature called fix-and-continue, which is used
50 for rapid turn around debugging. When code is compiled with the
51 -mfix-and-continue flag, two changes are made to the generated code
52 that allow the system to do things that it would normally not be
53 able to do easily. These changes allow gdb to load in
54 recompilation of a translation unit that has been changed into a
55 running program and replace existing functions and methods of that
56 translation unit with versions of those functions and methods
57 from the newly compiled translation unit. The new functions access
58 the existing static symbols from the old translation unit, if the
59 symbol existed in the unit to be replaced, and from the new
60 translation unit, otherwise.
62 The changes are to insert 5 nops at the beginning of all functions
63 and to use indirection to get at static symbols. The 5 nops
64 are required by consumers of the generated code. Currently, gdb
65 uses this to patch in a jump to the overriding function, this
66 allows all uses of the old name to forward to the replacement,
67 including existing function pointers and virtual methods. See
68 rs6000_emit_prologue for the code that handles the nop insertions.
70 The added indirection allows gdb to redirect accesses to static
71 symbols from the newly loaded translation unit to the existing
72 symbol, if any. @code{static} symbols are special and are handled by
73 setting the second word in the .non_lazy_symbol_pointer data
74 structure to symbol. See indirect_data for the code that handles
75 the extra indirection, and machopic_output_indirection and its use
76 of MACHO_SYMBOL_STATIC for the code that handles @code{static}
77 symbol indirection. */
79 /* Section names. */
80 section * darwin_sections[NUM_DARWIN_SECTIONS];
82 /* True if we're setting __attribute__ ((ms_struct)). */
83 int darwin_ms_struct = false;
85 /* A get_unnamed_section callback used to switch to an ObjC section.
86 DIRECTIVE is as for output_section_asm_op. */
88 static void
89 output_objc_section_asm_op (const void *directive)
91 static bool been_here = false;
93 if (! been_here)
95 static const enum darwin_section_enum tomark[] =
97 /* written, cold -> hot */
98 objc_cat_cls_meth_section,
99 objc_cat_inst_meth_section,
100 objc_string_object_section,
101 objc_constant_string_object_section,
102 objc_selector_refs_section,
103 objc_selector_fixup_section,
104 objc_cls_refs_section,
105 objc_class_section,
106 objc_meta_class_section,
107 /* shared, hot -> cold */
108 objc_cls_meth_section,
109 objc_inst_meth_section,
110 objc_protocol_section,
111 objc_class_names_section,
112 objc_meth_var_types_section,
113 objc_meth_var_names_section,
114 objc_category_section,
115 objc_class_vars_section,
116 objc_instance_vars_section,
117 objc_module_info_section,
118 objc_symbols_section
120 size_t i;
122 been_here = true;
123 for (i = 0; i < ARRAY_SIZE (tomark); i++)
124 switch_to_section (darwin_sections[tomark[i]]);
126 output_section_asm_op (directive);
129 /* Implement TARGET_ASM_INIT_SECTIONS. */
131 void
132 darwin_init_sections (void)
134 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
135 darwin_sections[NAME] = \
136 get_unnamed_section (FLAGS, (OBJC \
137 ? output_objc_section_asm_op \
138 : output_section_asm_op), \
139 "\t" DIRECTIVE);
140 #include "config/darwin-sections.def"
141 #undef DEF_SECTION
143 readonly_data_section = darwin_sections[const_section];
144 exception_section = darwin_sections[darwin_exception_section];
145 eh_frame_section = darwin_sections[darwin_eh_frame_section];
149 name_needs_quotes (const char *name)
151 int c;
152 while ((c = *name++) != '\0')
153 if (! ISIDNUM (c) && c != '.' && c != '$')
154 return 1;
155 return 0;
158 /* Return true if SYM_REF can be used without an indirection. */
159 static int
160 machopic_symbol_defined_p (rtx sym_ref)
162 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
163 return true;
165 /* If a symbol references local and is not an extern to this
166 file, then the symbol might be able to declared as defined. */
167 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
169 /* If the symbol references a variable and the variable is a
170 common symbol, then this symbol is not defined. */
171 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
173 tree decl = SYMBOL_REF_DECL (sym_ref);
174 if (!decl)
175 return true;
176 if (DECL_COMMON (decl))
177 return false;
179 return true;
181 return false;
184 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
185 reference, which will not be changed. */
187 enum machopic_addr_class
188 machopic_classify_symbol (rtx sym_ref)
190 int flags;
191 bool function_p;
193 flags = SYMBOL_REF_FLAGS (sym_ref);
194 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
195 if (machopic_symbol_defined_p (sym_ref))
196 return (function_p
197 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
198 else
199 return (function_p
200 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
203 #ifndef TARGET_FIX_AND_CONTINUE
204 #define TARGET_FIX_AND_CONTINUE 0
205 #endif
207 /* Indicate when fix-and-continue style code generation is being used
208 and when a reference to data should be indirected so that it can be
209 rebound in a new translation unit to reference the original instance
210 of that data. Symbol names that are for code generation local to
211 the translation unit are bound to the new translation unit;
212 currently this means symbols that begin with L or _OBJC_;
213 otherwise, we indicate that an indirect reference should be made to
214 permit the runtime to rebind new instances of the translation unit
215 to the original instance of the data. */
217 static int
218 indirect_data (rtx sym_ref)
220 int lprefix;
221 const char *name;
223 /* If we aren't generating fix-and-continue code, don't do anything special. */
224 if (TARGET_FIX_AND_CONTINUE == 0)
225 return 0;
227 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
228 are indirected. Symbols that begin with L and _OBJC_ are always
229 bound to the current translation unit as they are used for
230 generated local data of the translation unit. */
232 name = XSTR (sym_ref, 0);
234 lprefix = (((name[0] == '*' || name[0] == '&')
235 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
236 || (strncmp (name, "_OBJC_", 6) == 0));
238 return ! lprefix;
242 static int
243 machopic_data_defined_p (rtx sym_ref)
245 if (indirect_data (sym_ref))
246 return 0;
248 switch (machopic_classify_symbol (sym_ref))
250 case MACHOPIC_DEFINED_DATA:
251 case MACHOPIC_DEFINED_FUNCTION:
252 return 1;
253 default:
254 return 0;
258 void
259 machopic_define_symbol (rtx mem)
261 rtx sym_ref;
263 gcc_assert (GET_CODE (mem) == MEM);
264 sym_ref = XEXP (mem, 0);
265 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
268 static GTY(()) char * function_base;
270 const char *
271 machopic_function_base_name (void)
273 /* if dynamic-no-pic is on, we should not get here */
274 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
276 if (function_base == NULL)
277 function_base =
278 (char *) ggc_alloc_string ("<pic base>", sizeof ("<pic base>"));
280 current_function_uses_pic_offset_table = 1;
282 return function_base;
285 /* Return a SYMBOL_REF for the PIC function base. */
288 machopic_function_base_sym (void)
290 rtx sym_ref;
292 sym_ref = gen_rtx_SYMBOL_REF (Pmode, machopic_function_base_name ());
293 SYMBOL_REF_FLAGS (sym_ref)
294 |= (MACHO_SYMBOL_FLAG_VARIABLE | MACHO_SYMBOL_FLAG_DEFINED);
295 return sym_ref;
298 /* Return either ORIG or (const:P (minus:P ORIG PIC_BASE)), depending
299 on whether pic_base is NULL or not. */
300 static inline rtx
301 gen_pic_offset (rtx orig, rtx pic_base)
303 if (!pic_base)
304 return orig;
305 else
306 return gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, orig, pic_base));
309 static GTY(()) const char * function_base_func_name;
310 static GTY(()) int current_pic_label_num;
312 void
313 machopic_output_function_base_name (FILE *file)
315 const char *current_name;
317 /* If dynamic-no-pic is on, we should not get here. */
318 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
319 current_name =
320 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
321 if (function_base_func_name != current_name)
323 ++current_pic_label_num;
324 function_base_func_name = current_name;
326 fprintf (file, "\"L%011d$pb\"", current_pic_label_num);
329 /* The suffix attached to non-lazy pointer symbols. */
330 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
331 /* The suffix attached to stub symbols. */
332 #define STUB_SUFFIX "$stub"
334 typedef struct machopic_indirection GTY (())
336 /* The SYMBOL_REF for the entity referenced. */
337 rtx symbol;
338 /* The name of the stub or non-lazy pointer. */
339 const char * ptr_name;
340 /* True iff this entry is for a stub (as opposed to a non-lazy
341 pointer). */
342 bool stub_p;
343 /* True iff this stub or pointer pointer has been referenced. */
344 bool used;
345 } machopic_indirection;
347 /* A table mapping stub names and non-lazy pointer names to
348 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
350 static GTY ((param_is (struct machopic_indirection))) htab_t
351 machopic_indirections;
353 /* Return a hash value for a SLOT in the indirections hash table. */
355 static hashval_t
356 machopic_indirection_hash (const void *slot)
358 const machopic_indirection *p = (const machopic_indirection *) slot;
359 return htab_hash_string (p->ptr_name);
362 /* Returns true if the KEY is the same as that associated with
363 SLOT. */
365 static int
366 machopic_indirection_eq (const void *slot, const void *key)
368 return strcmp (((const machopic_indirection *) slot)->ptr_name, key) == 0;
371 /* Return the name of the non-lazy pointer (if STUB_P is false) or
372 stub (if STUB_B is true) corresponding to the given name. */
374 const char *
375 machopic_indirection_name (rtx sym_ref, bool stub_p)
377 char *buffer;
378 const char *name = XSTR (sym_ref, 0);
379 size_t namelen = strlen (name);
380 machopic_indirection *p;
381 void ** slot;
382 bool saw_star = false;
383 bool needs_quotes;
384 const char *suffix;
385 const char *prefix = user_label_prefix;
386 const char *quote = "";
387 tree id;
389 id = maybe_get_identifier (name);
390 if (id)
392 tree id_orig = id;
394 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
395 id = TREE_CHAIN (id);
396 if (id != id_orig)
398 name = IDENTIFIER_POINTER (id);
399 namelen = strlen (name);
403 if (name[0] == '*')
405 saw_star = true;
406 prefix = "";
407 ++name;
408 --namelen;
411 needs_quotes = name_needs_quotes (name);
412 if (needs_quotes)
414 quote = "\"";
417 if (stub_p)
418 suffix = STUB_SUFFIX;
419 else
420 suffix = NON_LAZY_POINTER_SUFFIX;
422 buffer = alloca (strlen ("&L")
423 + strlen (prefix)
424 + namelen
425 + strlen (suffix)
426 + 2 * strlen (quote)
427 + 1 /* '\0' */);
429 /* Construct the name of the non-lazy pointer or stub. */
430 sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
432 if (!machopic_indirections)
433 machopic_indirections = htab_create_ggc (37,
434 machopic_indirection_hash,
435 machopic_indirection_eq,
436 /*htab_del=*/NULL);
438 slot = htab_find_slot_with_hash (machopic_indirections, buffer,
439 htab_hash_string (buffer), INSERT);
440 if (*slot)
442 p = (machopic_indirection *) *slot;
444 else
446 p = (machopic_indirection *) ggc_alloc (sizeof (machopic_indirection));
447 p->symbol = sym_ref;
448 p->ptr_name = xstrdup (buffer);
449 p->stub_p = stub_p;
450 p->used = false;
451 *slot = p;
454 return p->ptr_name;
457 /* Return the name of the stub for the mcount function. */
459 const char*
460 machopic_mcount_stub_name (void)
462 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
463 return machopic_indirection_name (symbol, /*stub_p=*/true);
466 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
467 or non-lazy pointer as used -- and mark the object to which the
468 pointer/stub refers as used as well, since the pointer/stub will
469 emit a reference to it. */
471 void
472 machopic_validate_stub_or_non_lazy_ptr (const char *name)
474 machopic_indirection *p;
476 p = ((machopic_indirection *)
477 (htab_find_with_hash (machopic_indirections, name,
478 htab_hash_string (name))));
479 if (p && ! p->used)
481 const char *real_name;
482 tree id;
484 p->used = true;
486 /* Do what output_addr_const will do when we actually call it. */
487 if (SYMBOL_REF_DECL (p->symbol))
488 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
490 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
492 id = maybe_get_identifier (real_name);
493 if (id)
494 mark_referenced (id);
498 /* Transform ORIG, which may be any data source, to the corresponding
499 source using indirections. */
502 machopic_indirect_data_reference (rtx orig, rtx reg)
504 rtx ptr_ref = orig;
506 if (! MACHOPIC_INDIRECT)
507 return orig;
509 if (GET_CODE (orig) == SYMBOL_REF)
511 int defined = machopic_data_defined_p (orig);
513 if (defined && MACHO_DYNAMIC_NO_PIC_P)
515 #if defined (TARGET_TOC)
516 /* Create a new register for CSE opportunities. */
517 rtx hi_reg = (no_new_pseudos ? reg : gen_reg_rtx (Pmode));
518 emit_insn (gen_macho_high (hi_reg, orig));
519 emit_insn (gen_macho_low (reg, hi_reg, orig));
520 #else
521 /* some other cpu -- writeme! */
522 gcc_unreachable ();
523 #endif
524 return reg;
526 else if (defined)
528 #if defined (TARGET_TOC) || defined (HAVE_lo_sum)
529 rtx pic_base = machopic_function_base_sym ();
530 rtx offset = gen_pic_offset (orig, pic_base);
531 #endif
533 #if defined (TARGET_TOC) /* i.e., PowerPC */
534 rtx hi_sum_reg = (no_new_pseudos ? reg : gen_reg_rtx (Pmode));
536 gcc_assert (reg);
538 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
539 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
540 gen_rtx_HIGH (Pmode, offset))));
541 emit_insn (gen_rtx_SET (Pmode, reg,
542 gen_rtx_LO_SUM (Pmode, hi_sum_reg, offset)));
544 orig = reg;
545 #else
546 #if defined (HAVE_lo_sum)
547 gcc_assert (reg);
549 emit_insn (gen_rtx_SET (VOIDmode, reg,
550 gen_rtx_HIGH (Pmode, offset)));
551 emit_insn (gen_rtx_SET (VOIDmode, reg,
552 gen_rtx_LO_SUM (Pmode, reg, offset)));
553 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
555 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
556 #endif
557 #endif
558 return orig;
561 ptr_ref = (gen_rtx_SYMBOL_REF
562 (Pmode,
563 machopic_indirection_name (orig, /*stub_p=*/false)));
565 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
567 ptr_ref = gen_const_mem (Pmode, ptr_ref);
568 machopic_define_symbol (ptr_ref);
570 return ptr_ref;
572 else if (GET_CODE (orig) == CONST)
574 rtx base, result;
576 /* legitimize both operands of the PLUS */
577 if (GET_CODE (XEXP (orig, 0)) == PLUS)
579 base = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 0),
580 reg);
581 orig = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 1),
582 (base == reg ? 0 : reg));
584 else
585 return orig;
587 if (MACHOPIC_PURE && GET_CODE (orig) == CONST_INT)
588 result = plus_constant (base, INTVAL (orig));
589 else
590 result = gen_rtx_PLUS (Pmode, base, orig);
592 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
594 if (reg)
596 emit_move_insn (reg, result);
597 result = reg;
599 else
601 result = force_reg (GET_MODE (result), result);
605 return result;
608 else if (GET_CODE (orig) == MEM)
609 XEXP (ptr_ref, 0) = machopic_indirect_data_reference (XEXP (orig, 0), reg);
610 /* When the target is i386, this code prevents crashes due to the
611 compiler's ignorance on how to move the PIC base register to
612 other registers. (The reload phase sometimes introduces such
613 insns.) */
614 else if (GET_CODE (orig) == PLUS
615 && GET_CODE (XEXP (orig, 0)) == REG
616 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
617 #ifdef I386
618 /* Prevent the same register from being erroneously used
619 as both the base and index registers. */
620 && GET_CODE (XEXP (orig, 1)) == CONST
621 #endif
622 && reg)
624 emit_move_insn (reg, XEXP (orig, 0));
625 XEXP (ptr_ref, 0) = reg;
627 return ptr_ref;
630 /* Transform TARGET (a MEM), which is a function call target, to the
631 corresponding symbol_stub if necessary. Return a new MEM. */
634 machopic_indirect_call_target (rtx target)
636 if (GET_CODE (target) != MEM)
637 return target;
639 if (MACHOPIC_INDIRECT
640 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
641 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
642 & MACHO_SYMBOL_FLAG_DEFINED))
644 rtx sym_ref = XEXP (target, 0);
645 const char *stub_name = machopic_indirection_name (sym_ref,
646 /*stub_p=*/true);
647 enum machine_mode mode = GET_MODE (sym_ref);
649 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
650 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
651 MEM_READONLY_P (target) = 1;
652 MEM_NOTRAP_P (target) = 1;
655 return target;
659 machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
661 rtx pic_ref = orig;
663 if (! MACHOPIC_INDIRECT)
664 return orig;
666 /* First handle a simple SYMBOL_REF or LABEL_REF */
667 if (GET_CODE (orig) == LABEL_REF
668 || (GET_CODE (orig) == SYMBOL_REF
671 /* addr(foo) = &func+(foo-func) */
672 rtx pic_base;
674 orig = machopic_indirect_data_reference (orig, reg);
676 if (GET_CODE (orig) == PLUS
677 && GET_CODE (XEXP (orig, 0)) == REG)
679 if (reg == 0)
680 return force_reg (mode, orig);
682 emit_move_insn (reg, orig);
683 return reg;
686 /* if dynamic-no-pic we don't have a pic base */
687 if (MACHO_DYNAMIC_NO_PIC_P)
688 pic_base = NULL;
689 else
690 pic_base = machopic_function_base_sym ();
692 if (GET_CODE (orig) == MEM)
694 if (reg == 0)
696 gcc_assert (!reload_in_progress);
697 reg = gen_reg_rtx (Pmode);
700 #ifdef HAVE_lo_sum
701 if (MACHO_DYNAMIC_NO_PIC_P
702 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
703 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
705 #if defined (TARGET_TOC) /* ppc */
706 rtx temp_reg = (no_new_pseudos) ? reg : gen_reg_rtx (Pmode);
707 rtx asym = XEXP (orig, 0);
708 rtx mem;
710 emit_insn (gen_macho_high (temp_reg, asym));
711 mem = gen_const_mem (GET_MODE (orig),
712 gen_rtx_LO_SUM (Pmode, temp_reg, asym));
713 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
714 #else
715 /* Some other CPU -- WriteMe! but right now there are no other platform that can use dynamic-no-pic */
716 gcc_unreachable ();
717 #endif
718 pic_ref = reg;
720 else
721 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
722 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
724 rtx offset = gen_pic_offset (XEXP (orig, 0), pic_base);
725 #if defined (TARGET_TOC) /* i.e., PowerPC */
726 /* Generating a new reg may expose opportunities for
727 common subexpression elimination. */
728 rtx hi_sum_reg = no_new_pseudos ? reg : gen_reg_rtx (Pmode);
729 rtx mem;
730 rtx insn;
731 rtx sum;
733 sum = gen_rtx_HIGH (Pmode, offset);
734 if (! MACHO_DYNAMIC_NO_PIC_P)
735 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
737 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
739 mem = gen_const_mem (GET_MODE (orig),
740 gen_rtx_LO_SUM (Pmode,
741 hi_sum_reg, offset));
742 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
743 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, pic_ref,
744 REG_NOTES (insn));
746 pic_ref = reg;
747 #else
748 emit_insn (gen_rtx_USE (VOIDmode,
749 gen_rtx_REG (Pmode,
750 PIC_OFFSET_TABLE_REGNUM)));
752 emit_insn (gen_rtx_SET (VOIDmode, reg,
753 gen_rtx_HIGH (Pmode,
754 gen_rtx_CONST (Pmode,
755 offset))));
756 emit_insn (gen_rtx_SET (VOIDmode, reg,
757 gen_rtx_LO_SUM (Pmode, reg,
758 gen_rtx_CONST (Pmode, offset))));
759 pic_ref = gen_rtx_PLUS (Pmode,
760 pic_offset_table_rtx, reg);
761 #endif
763 else
764 #endif /* HAVE_lo_sum */
766 rtx pic = pic_offset_table_rtx;
767 if (GET_CODE (pic) != REG)
769 emit_move_insn (reg, pic);
770 pic = reg;
772 #if 0
773 emit_insn (gen_rtx_USE (VOIDmode,
774 gen_rtx_REG (Pmode,
775 PIC_OFFSET_TABLE_REGNUM)));
776 #endif
778 if (reload_in_progress)
779 regs_ever_live[REGNO (pic)] = 1;
780 pic_ref = gen_rtx_PLUS (Pmode, pic,
781 gen_pic_offset (XEXP (orig, 0),
782 pic_base));
785 #if !defined (TARGET_TOC)
786 emit_move_insn (reg, pic_ref);
787 pic_ref = gen_const_mem (GET_MODE (orig), reg);
788 #endif
790 else
793 #ifdef HAVE_lo_sum
794 if (GET_CODE (orig) == SYMBOL_REF
795 || GET_CODE (orig) == LABEL_REF)
797 rtx offset = gen_pic_offset (orig, pic_base);
798 #if defined (TARGET_TOC) /* i.e., PowerPC */
799 rtx hi_sum_reg;
801 if (reg == 0)
803 gcc_assert (!reload_in_progress);
804 reg = gen_reg_rtx (Pmode);
807 hi_sum_reg = reg;
809 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
810 (MACHO_DYNAMIC_NO_PIC_P)
811 ? gen_rtx_HIGH (Pmode, offset)
812 : gen_rtx_PLUS (Pmode,
813 pic_offset_table_rtx,
814 gen_rtx_HIGH (Pmode,
815 offset))));
816 emit_insn (gen_rtx_SET (VOIDmode, reg,
817 gen_rtx_LO_SUM (Pmode,
818 hi_sum_reg, offset)));
819 pic_ref = reg;
820 #else
821 emit_insn (gen_rtx_SET (VOIDmode, reg,
822 gen_rtx_HIGH (Pmode, offset)));
823 emit_insn (gen_rtx_SET (VOIDmode, reg,
824 gen_rtx_LO_SUM (Pmode, reg, offset)));
825 pic_ref = gen_rtx_PLUS (Pmode,
826 pic_offset_table_rtx, reg);
827 #endif
829 else
830 #endif /* HAVE_lo_sum */
832 if (REG_P (orig)
833 || GET_CODE (orig) == SUBREG)
835 return orig;
837 else
839 rtx pic = pic_offset_table_rtx;
840 if (GET_CODE (pic) != REG)
842 emit_move_insn (reg, pic);
843 pic = reg;
845 #if 0
846 emit_insn (gen_rtx_USE (VOIDmode,
847 pic_offset_table_rtx));
848 #endif
849 if (reload_in_progress)
850 regs_ever_live[REGNO (pic)] = 1;
851 pic_ref = gen_rtx_PLUS (Pmode,
852 pic,
853 gen_pic_offset (orig, pic_base));
858 if (GET_CODE (pic_ref) != REG)
860 if (reg != 0)
862 emit_move_insn (reg, pic_ref);
863 return reg;
865 else
867 return force_reg (mode, pic_ref);
870 else
872 return pic_ref;
876 else if (GET_CODE (orig) == SYMBOL_REF)
877 return orig;
879 else if (GET_CODE (orig) == PLUS
880 && (GET_CODE (XEXP (orig, 0)) == MEM
881 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
882 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
883 && XEXP (orig, 0) != pic_offset_table_rtx
884 && GET_CODE (XEXP (orig, 1)) != REG)
887 rtx base;
888 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
890 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
891 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
892 Pmode, (base == reg ? 0 : reg));
893 if (GET_CODE (orig) == CONST_INT)
895 pic_ref = plus_constant (base, INTVAL (orig));
896 is_complex = 1;
898 else
899 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
901 if (reg && is_complex)
903 emit_move_insn (reg, pic_ref);
904 pic_ref = reg;
906 /* Likewise, should we set special REG_NOTEs here? */
909 else if (GET_CODE (orig) == CONST)
911 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
914 else if (GET_CODE (orig) == MEM
915 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
917 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
918 addr = replace_equiv_address (orig, addr);
919 emit_move_insn (reg, addr);
920 pic_ref = reg;
923 return pic_ref;
926 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
927 DATA is the FILE* for assembly output. Called from
928 htab_traverse. */
930 static int
931 machopic_output_indirection (void **slot, void *data)
933 machopic_indirection *p = *((machopic_indirection **) slot);
934 FILE *asm_out_file = (FILE *) data;
935 rtx symbol;
936 const char *sym_name;
937 const char *ptr_name;
939 if (!p->used)
940 return 1;
942 symbol = p->symbol;
943 sym_name = XSTR (symbol, 0);
944 ptr_name = p->ptr_name;
946 if (p->stub_p)
948 char *sym;
949 char *stub;
950 tree id;
952 id = maybe_get_identifier (sym_name);
953 if (id)
955 tree id_orig = id;
957 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
958 id = TREE_CHAIN (id);
959 if (id != id_orig)
960 sym_name = IDENTIFIER_POINTER (id);
963 sym = alloca (strlen (sym_name) + 2);
964 if (sym_name[0] == '*' || sym_name[0] == '&')
965 strcpy (sym, sym_name + 1);
966 else if (sym_name[0] == '-' || sym_name[0] == '+')
967 strcpy (sym, sym_name);
968 else
969 sprintf (sym, "%s%s", user_label_prefix, sym_name);
971 stub = alloca (strlen (ptr_name) + 2);
972 if (ptr_name[0] == '*' || ptr_name[0] == '&')
973 strcpy (stub, ptr_name + 1);
974 else
975 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
977 machopic_output_stub (asm_out_file, sym, stub);
979 else if (! indirect_data (symbol)
980 && (machopic_symbol_defined_p (symbol)
981 || SYMBOL_REF_LOCAL_P (symbol)))
983 switch_to_section (data_section);
984 assemble_align (GET_MODE_ALIGNMENT (Pmode));
985 assemble_label (ptr_name);
986 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
987 GET_MODE_SIZE (Pmode),
988 GET_MODE_ALIGNMENT (Pmode), 1);
990 else
992 rtx init = const0_rtx;
994 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
995 assemble_name (asm_out_file, ptr_name);
996 fprintf (asm_out_file, ":\n");
998 fprintf (asm_out_file, "\t.indirect_symbol ");
999 assemble_name (asm_out_file, sym_name);
1000 fprintf (asm_out_file, "\n");
1002 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1003 have their symbol name instead of 0 in the second entry of
1004 the non-lazy symbol pointer data structure when they are
1005 defined. This allows the runtime to rebind newer instances
1006 of the translation unit with the original instance of the
1007 symbol. */
1009 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1010 && machopic_symbol_defined_p (symbol))
1011 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1013 assemble_integer (init, GET_MODE_SIZE (Pmode),
1014 GET_MODE_ALIGNMENT (Pmode), 1);
1017 return 1;
1020 void
1021 machopic_finish (FILE *asm_out_file)
1023 if (machopic_indirections)
1024 htab_traverse_noresize (machopic_indirections,
1025 machopic_output_indirection,
1026 asm_out_file);
1030 machopic_operand_p (rtx op)
1032 if (MACHOPIC_JUST_INDIRECT)
1034 while (GET_CODE (op) == CONST)
1035 op = XEXP (op, 0);
1037 if (GET_CODE (op) == SYMBOL_REF)
1038 return machopic_symbol_defined_p (op);
1039 else
1040 return 0;
1043 while (GET_CODE (op) == CONST)
1044 op = XEXP (op, 0);
1046 if (GET_CODE (op) == MINUS
1047 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
1048 && GET_CODE (XEXP (op, 1)) == SYMBOL_REF
1049 && machopic_symbol_defined_p (XEXP (op, 0))
1050 && machopic_symbol_defined_p (XEXP (op, 1)))
1051 return 1;
1053 return 0;
1056 /* This function records whether a given name corresponds to a defined
1057 or undefined function or variable, for machopic_classify_ident to
1058 use later. */
1060 void
1061 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
1063 rtx sym_ref;
1065 /* Do the standard encoding things first. */
1066 default_encode_section_info (decl, rtl, first);
1068 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1069 return;
1071 sym_ref = XEXP (rtl, 0);
1072 if (TREE_CODE (decl) == VAR_DECL)
1073 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1075 if (!DECL_EXTERNAL (decl)
1076 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1077 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1078 && ((TREE_STATIC (decl)
1079 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1080 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1081 && DECL_INITIAL (decl) != error_mark_node)))
1082 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1084 if (! TREE_PUBLIC (decl))
1085 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
1088 void
1089 darwin_mark_decl_preserved (const char *name)
1091 fprintf (asm_out_file, ".no_dead_strip ");
1092 assemble_name (asm_out_file, name);
1093 fputc ('\n', asm_out_file);
1096 section *
1097 machopic_select_section (tree exp, int reloc,
1098 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1100 section *base_section;
1101 bool weak_p = (DECL_P (exp) && DECL_WEAK (exp)
1102 && (lookup_attribute ("weak", DECL_ATTRIBUTES (exp))
1103 || ! lookup_attribute ("weak_import",
1104 DECL_ATTRIBUTES (exp))));
1106 if (TREE_CODE (exp) == FUNCTION_DECL)
1108 if (reloc == 1)
1109 base_section = (weak_p
1110 ? darwin_sections[text_unlikely_coal_section]
1111 : unlikely_text_section ());
1112 else
1113 base_section = weak_p ? darwin_sections[text_coal_section] : text_section;
1115 else if (decl_readonly_section_1 (exp, reloc, MACHOPIC_INDIRECT))
1116 base_section = weak_p ? darwin_sections[const_coal_section] : darwin_sections[const_section];
1117 else if (TREE_READONLY (exp) || TREE_CONSTANT (exp))
1118 base_section = weak_p ? darwin_sections[const_data_coal_section] : darwin_sections[const_data_section];
1119 else
1120 base_section = weak_p ? darwin_sections[data_coal_section] : data_section;
1122 if (TREE_CODE (exp) == STRING_CST
1123 && ((size_t) TREE_STRING_LENGTH (exp)
1124 == strlen (TREE_STRING_POINTER (exp)) + 1))
1125 return darwin_sections[cstring_section];
1126 else if ((TREE_CODE (exp) == INTEGER_CST || TREE_CODE (exp) == REAL_CST)
1127 && flag_merge_constants)
1129 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1131 if (TREE_CODE (size) == INTEGER_CST &&
1132 TREE_INT_CST_LOW (size) == 4 &&
1133 TREE_INT_CST_HIGH (size) == 0)
1134 return darwin_sections[literal4_section];
1135 else if (TREE_CODE (size) == INTEGER_CST &&
1136 TREE_INT_CST_LOW (size) == 8 &&
1137 TREE_INT_CST_HIGH (size) == 0)
1138 return darwin_sections[literal8_section];
1139 else if (TARGET_64BIT
1140 && TREE_CODE (size) == INTEGER_CST
1141 && TREE_INT_CST_LOW (size) == 16
1142 && TREE_INT_CST_HIGH (size) == 0)
1143 return darwin_sections[literal16_section];
1144 else
1145 return base_section;
1147 else if (TREE_CODE (exp) == CONSTRUCTOR
1148 && TREE_TYPE (exp)
1149 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
1150 && TYPE_NAME (TREE_TYPE (exp)))
1152 tree name = TYPE_NAME (TREE_TYPE (exp));
1153 if (TREE_CODE (name) == TYPE_DECL)
1154 name = DECL_NAME (name);
1156 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1158 if (flag_next_runtime)
1159 return darwin_sections[objc_constant_string_object_section];
1160 else
1161 return darwin_sections[objc_string_object_section];
1163 else
1164 return base_section;
1166 else if (TREE_CODE (exp) == VAR_DECL &&
1167 DECL_NAME (exp) &&
1168 TREE_CODE (DECL_NAME (exp)) == IDENTIFIER_NODE &&
1169 IDENTIFIER_POINTER (DECL_NAME (exp)) &&
1170 !strncmp (IDENTIFIER_POINTER (DECL_NAME (exp)), "_OBJC_", 6))
1172 const char *name = IDENTIFIER_POINTER (DECL_NAME (exp));
1174 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1175 return darwin_sections[objc_cls_meth_section];
1176 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1177 return darwin_sections[objc_inst_meth_section];
1178 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 20))
1179 return darwin_sections[objc_cat_cls_meth_section];
1180 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 23))
1181 return darwin_sections[objc_cat_inst_meth_section];
1182 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1183 return darwin_sections[objc_class_vars_section];
1184 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1185 return darwin_sections[objc_instance_vars_section];
1186 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1187 return darwin_sections[objc_cat_cls_meth_section];
1188 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1189 return darwin_sections[objc_class_names_section];
1190 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1191 return darwin_sections[objc_meth_var_names_section];
1192 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1193 return darwin_sections[objc_meth_var_types_section];
1194 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1195 return darwin_sections[objc_cls_refs_section];
1196 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1197 return darwin_sections[objc_class_section];
1198 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1199 return darwin_sections[objc_meta_class_section];
1200 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1201 return darwin_sections[objc_category_section];
1202 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1203 return darwin_sections[objc_selector_refs_section];
1204 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1205 return darwin_sections[objc_selector_fixup_section];
1206 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1207 return darwin_sections[objc_symbols_section];
1208 else if (!strncmp (name, "_OBJC_MODULES", 13))
1209 return darwin_sections[objc_module_info_section];
1210 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1211 return darwin_sections[objc_image_info_section];
1212 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1213 return darwin_sections[objc_cat_inst_meth_section];
1214 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1215 return darwin_sections[objc_cat_cls_meth_section];
1216 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1217 return darwin_sections[objc_cat_cls_meth_section];
1218 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1219 return darwin_sections[objc_protocol_section];
1220 else
1221 return base_section;
1223 else
1224 return base_section;
1227 /* This can be called with address expressions as "rtx".
1228 They must go in "const". */
1230 section *
1231 machopic_select_rtx_section (enum machine_mode mode, rtx x,
1232 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1234 if (GET_MODE_SIZE (mode) == 8
1235 && (GET_CODE (x) == CONST_INT
1236 || GET_CODE (x) == CONST_DOUBLE))
1237 return darwin_sections[literal8_section];
1238 else if (GET_MODE_SIZE (mode) == 4
1239 && (GET_CODE (x) == CONST_INT
1240 || GET_CODE (x) == CONST_DOUBLE))
1241 return darwin_sections[literal4_section];
1242 else if (TARGET_64BIT
1243 && GET_MODE_SIZE (mode) == 16
1244 && (GET_CODE (x) == CONST_INT
1245 || GET_CODE (x) == CONST_DOUBLE
1246 || GET_CODE (x) == CONST_VECTOR))
1247 return darwin_sections[literal16_section];
1248 else if (MACHOPIC_INDIRECT
1249 && (GET_CODE (x) == SYMBOL_REF
1250 || GET_CODE (x) == CONST
1251 || GET_CODE (x) == LABEL_REF))
1252 return darwin_sections[const_data_section];
1253 else
1254 return darwin_sections[const_section];
1257 void
1258 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1260 if (MACHOPIC_INDIRECT)
1261 switch_to_section (darwin_sections[mod_init_section]);
1262 else
1263 switch_to_section (darwin_sections[constructor_section]);
1264 assemble_align (POINTER_SIZE);
1265 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1267 if (! MACHOPIC_INDIRECT)
1268 fprintf (asm_out_file, ".reference .constructors_used\n");
1271 void
1272 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1274 if (MACHOPIC_INDIRECT)
1275 switch_to_section (darwin_sections[mod_term_section]);
1276 else
1277 switch_to_section (darwin_sections[destructor_section]);
1278 assemble_align (POINTER_SIZE);
1279 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1281 if (! MACHOPIC_INDIRECT)
1282 fprintf (asm_out_file, ".reference .destructors_used\n");
1285 void
1286 darwin_globalize_label (FILE *stream, const char *name)
1288 if (!!strncmp (name, "_OBJC_", 6))
1289 default_globalize_label (stream, name);
1292 void
1293 darwin_asm_named_section (const char *name,
1294 unsigned int flags ATTRIBUTE_UNUSED,
1295 tree decl ATTRIBUTE_UNUSED)
1297 fprintf (asm_out_file, "\t.section %s\n", name);
1300 void
1301 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
1303 /* Darwin does not use unique sections. */
1306 /* Handle __attribute__ ((apple_kext_compatibility)).
1307 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
1308 vtable for classes with this attribute (and their descendants) by not
1309 outputting the new 3.0 nondeleting destructor. This means that such
1310 objects CANNOT be allocated on the stack or as globals UNLESS they have
1311 a completely empty `operator delete'.
1312 Luckily, this fits in with the Darwin kext model.
1314 This attribute also disables gcc3's potential overlaying of derived
1315 class data members on the padding at the end of the base class. */
1317 tree
1318 darwin_handle_kext_attribute (tree *node, tree name,
1319 tree args ATTRIBUTE_UNUSED,
1320 int flags ATTRIBUTE_UNUSED,
1321 bool *no_add_attrs)
1323 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
1324 if (! TARGET_KEXTABI)
1326 warning (0, "%<%s%> 2.95 vtable-compatability attribute applies "
1327 "only when compiling a kext", IDENTIFIER_POINTER (name));
1329 *no_add_attrs = true;
1331 else if (TREE_CODE (*node) != RECORD_TYPE)
1333 warning (0, "%<%s%> 2.95 vtable-compatability attribute applies "
1334 "only to C++ classes", IDENTIFIER_POINTER (name));
1336 *no_add_attrs = true;
1339 return NULL_TREE;
1342 /* Handle a "weak_import" attribute; arguments as in
1343 struct attribute_spec.handler. */
1345 tree
1346 darwin_handle_weak_import_attribute (tree *node, tree name,
1347 tree ARG_UNUSED (args),
1348 int ARG_UNUSED (flags),
1349 bool * no_add_attrs)
1351 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
1353 warning (OPT_Wattributes, "%qs attribute ignored",
1354 IDENTIFIER_POINTER (name));
1355 *no_add_attrs = true;
1357 else
1358 declare_weak (*node);
1360 return NULL_TREE;
1363 static void
1364 no_dead_strip (FILE *file, const char *lab)
1366 fprintf (file, ".no_dead_strip %s\n", lab);
1369 /* Emit a label for an FDE, making it global and/or weak if appropriate.
1370 The third parameter is nonzero if this is for exception handling.
1371 The fourth parameter is nonzero if this is just a placeholder for an
1372 FDE that we are omitting. */
1374 void
1375 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
1377 const char *base;
1378 char *lab;
1379 bool need_quotes;
1381 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1382 base = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1383 else
1384 base = IDENTIFIER_POINTER (DECL_NAME (decl));
1386 base = targetm.strip_name_encoding (base);
1387 need_quotes = name_needs_quotes (base);
1389 if (! for_eh)
1390 return;
1392 lab = concat (need_quotes ? "\"" : "", user_label_prefix, base, ".eh",
1393 need_quotes ? "\"" : "", NULL);
1395 if (TREE_PUBLIC (decl))
1396 fprintf (file, "\t%s %s\n",
1397 (DECL_VISIBILITY (decl) != VISIBILITY_HIDDEN
1398 ? ".globl"
1399 : ".private_extern"),
1400 lab);
1402 if (DECL_WEAK (decl))
1403 fprintf (file, "\t.weak_definition %s\n", lab);
1405 if (empty)
1407 fprintf (file, "%s = 0\n", lab);
1409 /* Mark the absolute .eh and .eh1 style labels as needed to
1410 ensure that we don't dead code strip them and keep such
1411 labels from another instantiation point until we can fix this
1412 properly with group comdat support. */
1413 no_dead_strip (file, lab);
1415 else
1416 fprintf (file, "%s:\n", lab);
1418 free (lab);
1421 static GTY(()) unsigned long except_table_label_num;
1423 void
1424 darwin_emit_except_table_label (FILE *file)
1426 char section_start_label[30];
1428 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
1429 except_table_label_num++);
1430 ASM_OUTPUT_LABEL (file, section_start_label);
1432 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
1434 void
1435 darwin_non_lazy_pcrel (FILE *file, rtx addr)
1437 const char *nlp_name;
1439 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
1441 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
1442 fputs ("\t.long\t", file);
1443 ASM_OUTPUT_LABELREF (file, nlp_name);
1444 fputs ("-.", file);
1447 /* Emit an assembler directive to set visibility for a symbol. The
1448 only supported visibilities are VISIBILITY_DEFAULT and
1449 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
1450 extern". There is no MACH-O equivalent of ELF's
1451 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
1453 void
1454 darwin_assemble_visibility (tree decl, int vis)
1456 if (vis == VISIBILITY_DEFAULT)
1458 else if (vis == VISIBILITY_HIDDEN)
1460 fputs ("\t.private_extern ", asm_out_file);
1461 assemble_name (asm_out_file,
1462 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
1463 fputs ("\n", asm_out_file);
1465 else
1466 warning (OPT_Wattributes, "internal and protected visibility attributes "
1467 "not supported in this configuration; ignored");
1470 /* Output a difference of two labels that will be an assembly time
1471 constant if the two labels are local. (.long lab1-lab2 will be
1472 very different if lab1 is at the boundary between two sections; it
1473 will be relocated according to the second section, not the first,
1474 so one ends up with a difference between labels in different
1475 sections, which is bad in the dwarf2 eh context for instance.) */
1477 static int darwin_dwarf_label_counter;
1479 void
1480 darwin_asm_output_dwarf_delta (FILE *file, int size,
1481 const char *lab1, const char *lab2)
1483 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
1484 && lab2[0] == '*' && lab2[1] == 'L');
1485 const char *directive = (size == 8 ? ".quad" : ".long");
1487 if (islocaldiff)
1488 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
1489 else
1490 fprintf (file, "\t%s\t", directive);
1491 assemble_name_raw (file, lab1);
1492 fprintf (file, "-");
1493 assemble_name_raw (file, lab2);
1494 if (islocaldiff)
1495 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
1498 /* Output labels for the start of the DWARF sections if necessary. */
1499 void
1500 darwin_file_start (void)
1502 if (write_symbols == DWARF2_DEBUG)
1504 static const char * const debugnames[] =
1506 DEBUG_FRAME_SECTION,
1507 DEBUG_INFO_SECTION,
1508 DEBUG_ABBREV_SECTION,
1509 DEBUG_ARANGES_SECTION,
1510 DEBUG_MACINFO_SECTION,
1511 DEBUG_LINE_SECTION,
1512 DEBUG_LOC_SECTION,
1513 DEBUG_PUBNAMES_SECTION,
1514 DEBUG_STR_SECTION,
1515 DEBUG_RANGES_SECTION
1517 size_t i;
1519 for (i = 0; i < ARRAY_SIZE (debugnames); i++)
1521 int namelen;
1523 switch_to_section (get_section (debugnames[i], SECTION_DEBUG, NULL));
1525 gcc_assert (strncmp (debugnames[i], "__DWARF,", 8) == 0);
1526 gcc_assert (strchr (debugnames[i] + 8, ','));
1528 namelen = strchr (debugnames[i] + 8, ',') - (debugnames[i] + 8);
1529 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, debugnames[i] + 8);
1534 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
1535 offsets are not represented using relocs in .o files; either the
1536 section never leaves the .o file, or the linker or other tool is
1537 responsible for parsing the DWARF and updating the offsets. */
1539 void
1540 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
1541 section *base)
1543 char sname[64];
1544 int namelen;
1546 gcc_assert (base->common.flags & SECTION_NAMED);
1547 gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
1548 gcc_assert (strchr (base->named.name + 8, ','));
1550 namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
1551 sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
1552 darwin_asm_output_dwarf_delta (file, size, lab, sname);
1555 void
1556 darwin_file_end (void)
1558 machopic_finish (asm_out_file);
1559 if (strcmp (lang_hooks.name, "GNU C++") == 0)
1561 switch_to_section (darwin_sections[constructor_section]);
1562 switch_to_section (darwin_sections[destructor_section]);
1563 ASM_OUTPUT_ALIGN (asm_out_file, 1);
1565 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
1568 /* TODO: Add a language hook for identifying if a decl is a vtable. */
1569 #define DARWIN_VTABLE_P(DECL) 0
1571 /* Cross-module name binding. Darwin does not support overriding
1572 functions at dynamic-link time, except for vtables in kexts. */
1574 bool
1575 darwin_binds_local_p (tree decl)
1577 return default_binds_local_p_1 (decl,
1578 TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
1581 #if 0
1582 /* See TARGET_ASM_OUTPUT_ANCHOR for why we can't do this yet. */
1583 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
1584 anchor relative to ".", the current section position. We cannot use
1585 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
1587 void
1588 darwin_asm_output_anchor (rtx symbol)
1590 fprintf (asm_out_file, "\t.set\t");
1591 assemble_name (asm_out_file, XSTR (symbol, 0));
1592 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
1593 SYMBOL_REF_BLOCK_OFFSET (symbol));
1595 #endif
1597 /* Set the darwin specific attributes on TYPE. */
1598 void
1599 darwin_set_default_type_attributes (tree type)
1601 if (darwin_ms_struct
1602 && TREE_CODE (type) == RECORD_TYPE)
1603 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
1604 NULL_TREE,
1605 TYPE_ATTRIBUTES (type));
1608 /* True, iff we're generating code for loadable kernel extentions. */
1610 bool
1611 darwin_kextabi_p (void) {
1612 return flag_apple_kext;
1615 void
1616 darwin_override_options (void)
1618 if (flag_apple_kext && strcmp (lang_hooks.name, "GNU C++") != 0)
1620 warning (0, "command line option %<-fapple-kext%> is only valid for C++");
1621 flag_apple_kext = 0;
1623 if (flag_mkernel || flag_apple_kext)
1625 /* -mkernel implies -fapple-kext for C++ */
1626 if (strcmp (lang_hooks.name, "GNU C++") == 0)
1627 flag_apple_kext = 1;
1629 flag_no_common = 1;
1631 /* No EH in kexts. */
1632 flag_exceptions = 0;
1633 /* No -fnon-call-exceptions data in kexts. */
1634 flag_non_call_exceptions = 0;
1638 #include "gt-darwin.h"