* config/arm/arm.md (addsi3_cbranch_scratch): Correct constraints.
[official-gcc.git] / gcc / config / darwin.c
blobdd9c34d984ae075249ab6d72d96a8dc40161870a
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 Free Software Foundation, Inc.
4 Contributed by Apple Computer Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "real.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-flags.h"
34 #include "output.h"
35 #include "insn-attr.h"
36 #include "flags.h"
37 #include "tree.h"
38 #include "expr.h"
39 #include "reload.h"
40 #include "function.h"
41 #include "ggc.h"
42 #include "langhooks.h"
43 #include "target.h"
44 #include "tm_p.h"
45 #include "errors.h"
46 #include "hashtab.h"
48 /* Darwin supports a feature called fix-and-continue, which is used
49 for rapid turn around debugging. When code is compiled with the
50 -mfix-and-continue flag, two changes are made to the generated code
51 that allow the system to do things that it would normally not be
52 able to do easily. These changes allow gdb to load in
53 recompilation of a translation unit that has been changed into a
54 running program and replace existing functions and methods of that
55 translation unit with with versions of those functions and methods
56 from the newly compiled translation unit. The new functions access
57 the existing static data from the old translation unit, if the data
58 existed in the unit to be replaced, and from the new translation
59 unit, for new data.
61 The changes are to insert 4 nops at the beginning of all functions
62 and to use indirection to get at static duration data. The 4 nops
63 are required by consumers of the generated code. Currently, gdb
64 uses this to patch in a jump to the overriding function, this
65 allows all uses of the old name to forward to the replacement,
66 including existing function pointers and virtual methods. See
67 rs6000_emit_prologue for the code that handles the nop insertions.
69 The added indirection allows gdb to redirect accesses to static
70 duration data from the newly loaded translation unit to the
71 existing data, if any. @code{static} data is special and is
72 handled by setting the second word in the .non_lazy_symbol_pointer
73 data structure to the address of the data. See indirect_data for
74 the code that handles the extra indirection, and
75 machopic_output_indirection and its use of MACHO_SYMBOL_STATIC for
76 the code that handles @code{static} data indirection. */
79 /* Nonzero if the user passes the -mone-byte-bool switch, which forces
80 sizeof(bool) to be 1. */
81 const char *darwin_one_byte_bool = 0;
83 int
84 name_needs_quotes (const char *name)
86 int c;
87 while ((c = *name++) != '\0')
88 if (! ISIDNUM (c) && c != '.' && c != '$')
89 return 1;
90 return 0;
94 * flag_pic = 1 ... generate only indirections
95 * flag_pic = 2 ... generate indirections and pure code
98 static int
99 machopic_symbol_defined_p (rtx sym_ref)
101 return (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
102 || (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref));
105 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
106 reference, which will not be changed. */
108 enum machopic_addr_class
109 machopic_classify_symbol (rtx sym_ref)
111 int flags;
112 bool function_p;
114 flags = SYMBOL_REF_FLAGS (sym_ref);
115 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
116 if (machopic_symbol_defined_p (sym_ref))
117 return (function_p
118 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
119 else
120 return (function_p
121 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
124 #ifndef TARGET_FIX_AND_CONTINUE
125 #define TARGET_FIX_AND_CONTINUE 0
126 #endif
128 /* Indicate when fix-and-continue style code generation is being used
129 and when a reference to data should be indirected so that it can be
130 rebound in a new translation unit to refernce the original instance
131 of that data. Symbol names that are for code generation local to
132 the translation unit are bound to the new translation unit;
133 currently this means symbols that begin with L or _OBJC_;
134 otherwise, we indicate that an indirect reference should be made to
135 permit the runtime to rebind new instances of the translation unit
136 to the original instance of the data. */
138 static int
139 indirect_data (rtx sym_ref)
141 int lprefix;
142 const char *name;
144 /* If we aren't generating fix-and-continue code, don't do anything special. */
145 if (TARGET_FIX_AND_CONTINUE == 0)
146 return 0;
148 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
149 are indirected. Symbols that begin with L and _OBJC_ are always
150 bound to the current translation unit as they are used for
151 generated local data of the translation unit. */
153 name = XSTR (sym_ref, 0);
155 lprefix = (((name[0] == '*' || name[0] == '&')
156 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
157 || (strncmp (name, "_OBJC_", 6)));
159 return ! lprefix;
163 static int
164 machopic_data_defined_p (rtx sym_ref)
166 if (indirect_data (sym_ref))
167 return 0;
169 switch (machopic_classify_symbol (sym_ref))
171 case MACHOPIC_DEFINED_DATA:
172 return 1;
173 default:
174 return 0;
178 void
179 machopic_define_symbol (rtx mem)
181 rtx sym_ref;
182 if (GET_CODE (mem) != MEM)
183 abort ();
184 sym_ref = XEXP (mem, 0);
185 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
188 static GTY(()) char * function_base;
190 const char *
191 machopic_function_base_name (void)
193 /* if dynamic-no-pic is on, we should not get here */
194 if (MACHO_DYNAMIC_NO_PIC_P)
195 abort ();
197 if (function_base == NULL)
198 function_base =
199 (char *) ggc_alloc_string ("<pic base>", sizeof ("<pic base>"));
201 current_function_uses_pic_offset_table = 1;
203 return function_base;
206 /* Return a SYMBOL_REF for the PIC function base. */
209 machopic_function_base_sym (void)
211 rtx sym_ref;
213 sym_ref = gen_rtx_SYMBOL_REF (Pmode, machopic_function_base_name ());
214 SYMBOL_REF_FLAGS (sym_ref)
215 |= (MACHO_SYMBOL_FLAG_VARIABLE | MACHO_SYMBOL_FLAG_DEFINED);
216 return sym_ref;
219 static GTY(()) const char * function_base_func_name;
220 static GTY(()) int current_pic_label_num;
222 void
223 machopic_output_function_base_name (FILE *file)
225 const char *current_name;
227 /* If dynamic-no-pic is on, we should not get here. */
228 if (MACHO_DYNAMIC_NO_PIC_P)
229 abort ();
230 current_name =
231 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
232 if (function_base_func_name != current_name)
234 ++current_pic_label_num;
235 function_base_func_name = current_name;
237 fprintf (file, "\"L%011d$pb\"", current_pic_label_num);
240 /* The suffix attached to non-lazy pointer symbols. */
241 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
242 /* The suffix attached to stub symbols. */
243 #define STUB_SUFFIX "$stub"
245 typedef struct machopic_indirection GTY (())
247 /* The SYMBOL_REF for the entity referenced. */
248 rtx symbol;
249 /* The name of the stub or non-lazy pointer. */
250 const char * ptr_name;
251 /* True iff this entry is for a stub (as opposed to a non-lazy
252 pointer). */
253 bool stub_p;
254 /* True iff this stub or pointer pointer has been referenced. */
255 bool used;
256 } machopic_indirection;
258 /* A table mapping stub names and non-lazy pointer names to
259 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
261 static GTY ((param_is (struct machopic_indirection))) htab_t
262 machopic_indirections;
264 /* Return a hash value for a SLOT in the indirections hash table. */
266 static hashval_t
267 machopic_indirection_hash (const void *slot)
269 const machopic_indirection *p = (const machopic_indirection *) slot;
270 return htab_hash_string (p->ptr_name);
273 /* Returns true if the KEY is the same as that associated with
274 SLOT. */
276 static int
277 machopic_indirection_eq (const void *slot, const void *key)
279 return strcmp (((const machopic_indirection *) slot)->ptr_name, key) == 0;
282 /* Return the name of the non-lazy pointer (if STUB_P is false) or
283 stub (if STUB_B is true) corresponding to the given name. */
285 const char *
286 machopic_indirection_name (rtx sym_ref, bool stub_p)
288 char *buffer;
289 const char *name = XSTR (sym_ref, 0);
290 size_t namelen = strlen (name);
291 machopic_indirection *p;
292 void ** slot;
294 /* Construct the name of the non-lazy pointer or stub. */
295 if (stub_p)
297 int needs_quotes = name_needs_quotes (name);
298 buffer = alloca (strlen ("&L")
299 + namelen
300 + strlen (STUB_SUFFIX)
301 + 2 /* possible quotes */
302 + 1 /* '\0' */);
304 if (needs_quotes)
306 if (name[0] == '*')
307 sprintf (buffer, "&\"L%s" STUB_SUFFIX "\"", name + 1);
308 else
309 sprintf (buffer, "&\"L%s%s" STUB_SUFFIX "\"", user_label_prefix,
310 name);
312 else if (name[0] == '*')
313 sprintf (buffer, "&L%s" STUB_SUFFIX, name + 1);
314 else
315 sprintf (buffer, "&L%s%s" STUB_SUFFIX, user_label_prefix, name);
317 else
319 buffer = alloca (strlen ("&L")
320 + strlen (user_label_prefix)
321 + namelen
322 + strlen (NON_LAZY_POINTER_SUFFIX)
323 + 1 /* '\0' */);
324 if (name[0] == '*')
325 sprintf (buffer, "&L%s" NON_LAZY_POINTER_SUFFIX, name + 1);
326 else
327 sprintf (buffer, "&L%s%s" NON_LAZY_POINTER_SUFFIX,
328 user_label_prefix, name);
331 if (!machopic_indirections)
332 machopic_indirections = htab_create_ggc (37,
333 machopic_indirection_hash,
334 machopic_indirection_eq,
335 /*htab_del=*/NULL);
337 slot = htab_find_slot_with_hash (machopic_indirections, buffer,
338 htab_hash_string (buffer), INSERT);
339 if (*slot)
341 p = (machopic_indirection *) *slot;
343 else
345 p = (machopic_indirection *) ggc_alloc (sizeof (machopic_indirection));
346 p->symbol = sym_ref;
347 p->ptr_name = xstrdup (buffer);
348 p->stub_p = stub_p;
349 p->used = false;
350 *slot = p;
353 return p->ptr_name;
356 /* Return the name of the stub for the mcount function. */
358 const char*
359 machopic_mcount_stub_name (void)
361 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
362 return machopic_indirection_name (symbol, /*stub_p=*/true);
365 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
366 or non-lazy pointer as used -- and mark the object to which the
367 pointer/stub refers as used as well, since the pointer/stub will
368 emit a reference to it. */
370 void
371 machopic_validate_stub_or_non_lazy_ptr (const char *name)
373 machopic_indirection *p;
375 p = ((machopic_indirection *)
376 (htab_find_with_hash (machopic_indirections, name,
377 htab_hash_string (name))));
378 if (p && ! p->used)
380 const char *real_name;
381 tree id;
383 p->used = true;
385 /* Do what output_addr_const will do when we actually call it. */
386 if (SYMBOL_REF_DECL (p->symbol))
387 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
389 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
391 id = maybe_get_identifier (real_name);
392 if (id)
393 mark_referenced (id);
397 /* Transform ORIG, which may be any data source, to the corresponding
398 source using indirections. */
401 machopic_indirect_data_reference (rtx orig, rtx reg)
403 rtx ptr_ref = orig;
405 if (! MACHOPIC_INDIRECT)
406 return orig;
408 if (GET_CODE (orig) == SYMBOL_REF)
410 int defined = machopic_data_defined_p (orig);
412 if (defined && MACHO_DYNAMIC_NO_PIC_P)
414 #if defined (TARGET_TOC)
415 emit_insn (GET_MODE (orig) == DImode
416 ? gen_macho_high_di (reg, orig)
417 : gen_macho_high (reg, orig));
418 emit_insn (GET_MODE (orig) == DImode
419 ? gen_macho_low_di (reg, reg, orig)
420 : gen_macho_low (reg, reg, orig));
421 #else
422 /* some other cpu -- writeme! */
423 abort ();
424 #endif
425 return reg;
427 else if (defined)
429 #if defined (TARGET_TOC) || defined (HAVE_lo_sum)
430 rtx pic_base = machopic_function_base_sym ();
431 rtx offset = gen_rtx_CONST (Pmode,
432 gen_rtx_MINUS (Pmode, orig, pic_base));
433 #endif
435 #if defined (TARGET_TOC) /* i.e., PowerPC */
436 rtx hi_sum_reg = (no_new_pseudos ? reg : gen_reg_rtx (Pmode));
438 if (reg == NULL)
439 abort ();
441 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
442 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
443 gen_rtx_HIGH (Pmode, offset))));
444 emit_insn (gen_rtx_SET (Pmode, reg,
445 gen_rtx_LO_SUM (Pmode, hi_sum_reg, offset)));
447 orig = reg;
448 #else
449 #if defined (HAVE_lo_sum)
450 if (reg == 0) abort ();
452 emit_insn (gen_rtx_SET (VOIDmode, reg,
453 gen_rtx_HIGH (Pmode, offset)));
454 emit_insn (gen_rtx_SET (VOIDmode, reg,
455 gen_rtx_LO_SUM (Pmode, reg, offset)));
456 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
458 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
459 #endif
460 #endif
461 return orig;
464 ptr_ref = (gen_rtx_SYMBOL_REF
465 (Pmode,
466 machopic_indirection_name (orig, /*stub_p=*/false)));
468 SYMBOL_REF_DECL (ptr_ref) = SYMBOL_REF_DECL (orig);
470 ptr_ref = gen_const_mem (Pmode, ptr_ref);
471 machopic_define_symbol (ptr_ref);
473 return ptr_ref;
475 else if (GET_CODE (orig) == CONST)
477 rtx base, result;
479 /* legitimize both operands of the PLUS */
480 if (GET_CODE (XEXP (orig, 0)) == PLUS)
482 base = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 0),
483 reg);
484 orig = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 1),
485 (base == reg ? 0 : reg));
487 else
488 return orig;
490 if (MACHOPIC_PURE && GET_CODE (orig) == CONST_INT)
491 result = plus_constant (base, INTVAL (orig));
492 else
493 result = gen_rtx_PLUS (Pmode, base, orig);
495 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
497 if (reg)
499 emit_move_insn (reg, result);
500 result = reg;
502 else
504 result = force_reg (GET_MODE (result), result);
508 return result;
511 else if (GET_CODE (orig) == MEM)
512 XEXP (ptr_ref, 0) = machopic_indirect_data_reference (XEXP (orig, 0), reg);
513 /* When the target is i386, this code prevents crashes due to the
514 compiler's ignorance on how to move the PIC base register to
515 other registers. (The reload phase sometimes introduces such
516 insns.) */
517 else if (GET_CODE (orig) == PLUS
518 && GET_CODE (XEXP (orig, 0)) == REG
519 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
520 #ifdef I386
521 /* Prevent the same register from being erroneously used
522 as both the base and index registers. */
523 && GET_CODE (XEXP (orig, 1)) == CONST
524 #endif
525 && reg)
527 emit_move_insn (reg, XEXP (orig, 0));
528 XEXP (ptr_ref, 0) = reg;
530 return ptr_ref;
533 /* Transform TARGET (a MEM), which is a function call target, to the
534 corresponding symbol_stub if necessary. Return a new MEM. */
537 machopic_indirect_call_target (rtx target)
539 if (GET_CODE (target) != MEM)
540 return target;
542 if (MACHOPIC_INDIRECT
543 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
544 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
545 & MACHO_SYMBOL_FLAG_DEFINED))
547 rtx sym_ref = XEXP (target, 0);
548 const char *stub_name = machopic_indirection_name (sym_ref,
549 /*stub_p=*/true);
550 enum machine_mode mode = GET_MODE (sym_ref);
551 tree decl = SYMBOL_REF_DECL (sym_ref);
553 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
554 SYMBOL_REF_DECL (XEXP (target, 0)) = decl;
555 MEM_READONLY_P (target) = 1;
556 MEM_NOTRAP_P (target) = 1;
559 return target;
563 machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
565 rtx pic_ref = orig;
567 if (! MACHOPIC_INDIRECT)
568 return orig;
570 /* First handle a simple SYMBOL_REF or LABEL_REF */
571 if (GET_CODE (orig) == LABEL_REF
572 || (GET_CODE (orig) == SYMBOL_REF
575 /* addr(foo) = &func+(foo-func) */
576 rtx pic_base;
578 orig = machopic_indirect_data_reference (orig, reg);
580 if (GET_CODE (orig) == PLUS
581 && GET_CODE (XEXP (orig, 0)) == REG)
583 if (reg == 0)
584 return force_reg (mode, orig);
586 emit_move_insn (reg, orig);
587 return reg;
590 /* if dynamic-no-pic then use 0 as the pic base */
591 if (MACHO_DYNAMIC_NO_PIC_P)
592 pic_base = CONST0_RTX (Pmode);
593 else
594 pic_base = machopic_function_base_sym ();
596 if (GET_CODE (orig) == MEM)
598 if (reg == 0)
600 if (reload_in_progress)
601 abort ();
602 else
603 reg = gen_reg_rtx (Pmode);
606 #ifdef HAVE_lo_sum
607 if (MACHO_DYNAMIC_NO_PIC_P
608 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
609 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
611 #if defined (TARGET_TOC) /* ppc */
612 rtx temp_reg = (no_new_pseudos) ? reg : gen_reg_rtx (Pmode);
613 rtx asym = XEXP (orig, 0);
614 rtx mem;
616 emit_insn (mode == DImode
617 ? gen_macho_high_di (temp_reg, asym)
618 : gen_macho_high (temp_reg, asym));
619 mem = gen_const_mem (GET_MODE (orig),
620 gen_rtx_LO_SUM (Pmode, temp_reg, asym));
621 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
622 #else
623 /* Some other CPU -- WriteMe! but right now there are no other platform that can use dynamic-no-pic */
624 abort ();
625 #endif
626 pic_ref = reg;
628 else
629 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
630 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
632 rtx offset = gen_rtx_CONST (Pmode,
633 gen_rtx_MINUS (Pmode,
634 XEXP (orig, 0),
635 pic_base));
636 #if defined (TARGET_TOC) /* i.e., PowerPC */
637 /* Generating a new reg may expose opportunities for
638 common subexpression elimination. */
639 rtx hi_sum_reg = no_new_pseudos ? reg : gen_reg_rtx (Pmode);
640 rtx mem;
641 rtx insn;
642 rtx sum;
644 sum = gen_rtx_HIGH (Pmode, offset);
645 if (! MACHO_DYNAMIC_NO_PIC_P)
646 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
648 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
650 mem = gen_const_mem (GET_MODE (orig),
651 gen_rtx_LO_SUM (Pmode,
652 hi_sum_reg, offset));
653 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
654 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, pic_ref,
655 REG_NOTES (insn));
657 pic_ref = reg;
658 #else
659 emit_insn (gen_rtx_USE (VOIDmode,
660 gen_rtx_REG (Pmode,
661 PIC_OFFSET_TABLE_REGNUM)));
663 emit_insn (gen_rtx_SET (VOIDmode, reg,
664 gen_rtx_HIGH (Pmode,
665 gen_rtx_CONST (Pmode,
666 offset))));
667 emit_insn (gen_rtx_SET (VOIDmode, reg,
668 gen_rtx_LO_SUM (Pmode, reg,
669 gen_rtx_CONST (Pmode, offset))));
670 pic_ref = gen_rtx_PLUS (Pmode,
671 pic_offset_table_rtx, reg);
672 #endif
674 else
675 #endif /* HAVE_lo_sum */
677 rtx pic = pic_offset_table_rtx;
678 if (GET_CODE (pic) != REG)
680 emit_move_insn (reg, pic);
681 pic = reg;
683 #if 0
684 emit_insn (gen_rtx_USE (VOIDmode,
685 gen_rtx_REG (Pmode,
686 PIC_OFFSET_TABLE_REGNUM)));
687 #endif
689 pic_ref = gen_rtx_PLUS (Pmode,
690 pic,
691 gen_rtx_CONST (Pmode,
692 gen_rtx_MINUS (Pmode,
693 XEXP (orig, 0),
694 pic_base)));
697 #if !defined (TARGET_TOC)
698 emit_move_insn (reg, pic_ref);
699 pic_ref = gen_const_mem (GET_MODE (orig), reg);
700 #endif
702 else
705 #ifdef HAVE_lo_sum
706 if (GET_CODE (orig) == SYMBOL_REF
707 || GET_CODE (orig) == LABEL_REF)
709 rtx offset = gen_rtx_CONST (Pmode,
710 gen_rtx_MINUS (Pmode,
711 orig, pic_base));
712 #if defined (TARGET_TOC) /* i.e., PowerPC */
713 rtx hi_sum_reg;
715 if (reg == 0)
717 if (reload_in_progress)
718 abort ();
719 else
720 reg = gen_reg_rtx (Pmode);
723 hi_sum_reg = reg;
725 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
726 (MACHO_DYNAMIC_NO_PIC_P)
727 ? gen_rtx_HIGH (Pmode, offset)
728 : gen_rtx_PLUS (Pmode,
729 pic_offset_table_rtx,
730 gen_rtx_HIGH (Pmode,
731 offset))));
732 emit_insn (gen_rtx_SET (VOIDmode, reg,
733 gen_rtx_LO_SUM (Pmode,
734 hi_sum_reg, offset)));
735 pic_ref = reg;
736 #else
737 emit_insn (gen_rtx_SET (VOIDmode, reg,
738 gen_rtx_HIGH (Pmode, offset)));
739 emit_insn (gen_rtx_SET (VOIDmode, reg,
740 gen_rtx_LO_SUM (Pmode, reg, offset)));
741 pic_ref = gen_rtx_PLUS (Pmode,
742 pic_offset_table_rtx, reg);
743 #endif
745 else
746 #endif /* HAVE_lo_sum */
748 if (GET_CODE (orig) == REG)
750 return orig;
752 else
754 rtx pic = pic_offset_table_rtx;
755 if (GET_CODE (pic) != REG)
757 emit_move_insn (reg, pic);
758 pic = reg;
760 #if 0
761 emit_insn (gen_rtx_USE (VOIDmode,
762 pic_offset_table_rtx));
763 #endif
764 pic_ref = gen_rtx_PLUS (Pmode,
765 pic,
766 gen_rtx_CONST (Pmode,
767 gen_rtx_MINUS (Pmode,
768 orig, pic_base)));
773 if (GET_CODE (pic_ref) != REG)
775 if (reg != 0)
777 emit_move_insn (reg, pic_ref);
778 return reg;
780 else
782 return force_reg (mode, pic_ref);
785 else
787 return pic_ref;
791 else if (GET_CODE (orig) == SYMBOL_REF)
792 return orig;
794 else if (GET_CODE (orig) == PLUS
795 && (GET_CODE (XEXP (orig, 0)) == MEM
796 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
797 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
798 && XEXP (orig, 0) != pic_offset_table_rtx
799 && GET_CODE (XEXP (orig, 1)) != REG)
802 rtx base;
803 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
805 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
806 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
807 Pmode, (base == reg ? 0 : reg));
808 if (GET_CODE (orig) == CONST_INT)
810 pic_ref = plus_constant (base, INTVAL (orig));
811 is_complex = 1;
813 else
814 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
816 if (reg && is_complex)
818 emit_move_insn (reg, pic_ref);
819 pic_ref = reg;
821 /* Likewise, should we set special REG_NOTEs here? */
824 else if (GET_CODE (orig) == CONST)
826 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
829 else if (GET_CODE (orig) == MEM
830 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
832 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
833 addr = replace_equiv_address (orig, addr);
834 emit_move_insn (reg, addr);
835 pic_ref = reg;
838 return pic_ref;
841 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
842 DATA is the FILE* for assembly output. Called from
843 htab_traverse. */
845 static int
846 machopic_output_indirection (void **slot, void *data)
848 machopic_indirection *p = *((machopic_indirection **) slot);
849 FILE *asm_out_file = (FILE *) data;
850 rtx symbol;
851 const char *sym_name;
852 const char *ptr_name;
854 if (!p->used)
855 return 1;
857 symbol = p->symbol;
858 sym_name = XSTR (symbol, 0);
859 ptr_name = p->ptr_name;
861 if (p->stub_p)
863 char *sym;
864 char *stub;
866 sym = alloca (strlen (sym_name) + 2);
867 if (sym_name[0] == '*' || sym_name[0] == '&')
868 strcpy (sym, sym_name + 1);
869 else if (sym_name[0] == '-' || sym_name[0] == '+')
870 strcpy (sym, sym_name);
871 else
872 sprintf (sym, "%s%s", user_label_prefix, sym_name);
874 stub = alloca (strlen (ptr_name) + 2);
875 if (ptr_name[0] == '*' || ptr_name[0] == '&')
876 strcpy (stub, ptr_name + 1);
877 else
878 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
880 machopic_output_stub (asm_out_file, sym, stub);
882 else if (! indirect_data (symbol)
883 && (machopic_symbol_defined_p (symbol)
884 || SYMBOL_REF_LOCAL_P (symbol)))
886 data_section ();
887 assemble_align (GET_MODE_ALIGNMENT (Pmode));
888 assemble_label (ptr_name);
889 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
890 GET_MODE_SIZE (Pmode),
891 GET_MODE_ALIGNMENT (Pmode), 1);
893 else
895 rtx init = const0_rtx;
897 machopic_nl_symbol_ptr_section ();
898 assemble_name (asm_out_file, ptr_name);
899 fprintf (asm_out_file, ":\n");
901 fprintf (asm_out_file, "\t.indirect_symbol ");
902 assemble_name (asm_out_file, sym_name);
903 fprintf (asm_out_file, "\n");
905 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
906 have their symbol name instead of 0 in the second entry of
907 the non-lazy symbol pointer data structure when they are
908 defined. This allows the runtime to rebind newer instances
909 of the translation unit with the original instance of the
910 data. */
912 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
913 && machopic_symbol_defined_p (symbol))
914 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
916 assemble_integer (init, GET_MODE_SIZE (Pmode),
917 GET_MODE_ALIGNMENT (Pmode), 1);
920 return 1;
923 void
924 machopic_finish (FILE *asm_out_file)
926 if (machopic_indirections)
927 htab_traverse_noresize (machopic_indirections,
928 machopic_output_indirection,
929 asm_out_file);
933 machopic_operand_p (rtx op)
935 if (MACHOPIC_JUST_INDIRECT)
937 while (GET_CODE (op) == CONST)
938 op = XEXP (op, 0);
940 if (GET_CODE (op) == SYMBOL_REF)
941 return machopic_symbol_defined_p (op);
942 else
943 return 0;
946 while (GET_CODE (op) == CONST)
947 op = XEXP (op, 0);
949 if (GET_CODE (op) == MINUS
950 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
951 && GET_CODE (XEXP (op, 1)) == SYMBOL_REF
952 && machopic_symbol_defined_p (XEXP (op, 0))
953 && machopic_symbol_defined_p (XEXP (op, 1)))
954 return 1;
956 return 0;
959 /* This function records whether a given name corresponds to a defined
960 or undefined function or variable, for machopic_classify_ident to
961 use later. */
963 void
964 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
966 rtx sym_ref;
968 /* Do the standard encoding things first. */
969 default_encode_section_info (decl, rtl, first);
971 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
972 return;
974 sym_ref = XEXP (rtl, 0);
975 if (TREE_CODE (decl) == VAR_DECL)
976 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
978 if (!DECL_EXTERNAL (decl)
979 && (!TREE_PUBLIC (decl) || (!DECL_ONE_ONLY (decl) && !DECL_WEAK (decl)))
980 && ((TREE_STATIC (decl)
981 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
982 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
983 && DECL_INITIAL (decl) != error_mark_node)))
984 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
986 if (TREE_CODE (decl) == VAR_DECL
987 && indirect_data (sym_ref)
988 && ! TREE_PUBLIC (decl))
989 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
992 static GTY(()) tree textcoal_section = 0;
993 static GTY(()) tree datacoal_section = 0;
995 void
996 darwin_make_decl_one_only (tree decl)
998 tree sec = 0;
999 if (textcoal_section == 0)
1001 static const char *ts = "__TEXT,__textcoal_nt,coalesced";
1002 static const char *ds = "__DATA,__datacoal_nt,coalesced";
1003 textcoal_section = build_string (strlen (ts), ts);
1004 datacoal_section = build_string (strlen (ds), ds);
1007 sec = TREE_CODE (decl) == FUNCTION_DECL
1008 ? textcoal_section
1009 : datacoal_section;
1010 TREE_PUBLIC (decl) = 1;
1011 DECL_ONE_ONLY (decl) = 1;
1012 DECL_SECTION_NAME (decl) = sec;
1015 void
1016 darwin_mark_decl_preserved (const char *name)
1018 fprintf (asm_out_file, ".no_dead_strip ");
1019 assemble_name (asm_out_file, name);
1020 fputc ('\n', asm_out_file);
1023 void
1024 machopic_select_section (tree exp, int reloc,
1025 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1027 void (*base_function)(void);
1029 if (decl_readonly_section_1 (exp, reloc, MACHOPIC_INDIRECT))
1030 base_function = readonly_data_section;
1031 else if (TREE_READONLY (exp) || TREE_CONSTANT (exp))
1032 base_function = const_data_section;
1033 else
1034 base_function = data_section;
1036 if (TREE_CODE (exp) == STRING_CST
1037 && ((size_t) TREE_STRING_LENGTH (exp)
1038 == strlen (TREE_STRING_POINTER (exp)) + 1))
1039 cstring_section ();
1040 else if ((TREE_CODE (exp) == INTEGER_CST || TREE_CODE (exp) == REAL_CST)
1041 && flag_merge_constants)
1043 tree size = TYPE_SIZE (TREE_TYPE (exp));
1045 if (TREE_CODE (size) == INTEGER_CST &&
1046 TREE_INT_CST_LOW (size) == 4 &&
1047 TREE_INT_CST_HIGH (size) == 0)
1048 literal4_section ();
1049 else if (TREE_CODE (size) == INTEGER_CST &&
1050 TREE_INT_CST_LOW (size) == 8 &&
1051 TREE_INT_CST_HIGH (size) == 0)
1052 literal8_section ();
1053 else
1054 base_function ();
1056 else if (TREE_CODE (exp) == CONSTRUCTOR
1057 && TREE_TYPE (exp)
1058 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
1059 && TYPE_NAME (TREE_TYPE (exp)))
1061 tree name = TYPE_NAME (TREE_TYPE (exp));
1062 if (TREE_CODE (name) == TYPE_DECL)
1063 name = DECL_NAME (name);
1064 if (!strcmp (IDENTIFIER_POINTER (name), "NSConstantString"))
1065 objc_constant_string_object_section ();
1066 else if (!strcmp (IDENTIFIER_POINTER (name), "NXConstantString"))
1067 objc_string_object_section ();
1068 else
1069 base_function ();
1071 else if (TREE_CODE (exp) == VAR_DECL &&
1072 DECL_NAME (exp) &&
1073 TREE_CODE (DECL_NAME (exp)) == IDENTIFIER_NODE &&
1074 IDENTIFIER_POINTER (DECL_NAME (exp)) &&
1075 !strncmp (IDENTIFIER_POINTER (DECL_NAME (exp)), "_OBJC_", 6))
1077 const char *name = IDENTIFIER_POINTER (DECL_NAME (exp));
1079 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1080 objc_cls_meth_section ();
1081 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1082 objc_inst_meth_section ();
1083 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 20))
1084 objc_cat_cls_meth_section ();
1085 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 23))
1086 objc_cat_inst_meth_section ();
1087 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1088 objc_class_vars_section ();
1089 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1090 objc_instance_vars_section ();
1091 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1092 objc_cat_cls_meth_section ();
1093 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1094 objc_class_names_section ();
1095 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1096 objc_meth_var_names_section ();
1097 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1098 objc_meth_var_types_section ();
1099 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1100 objc_cls_refs_section ();
1101 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1102 objc_class_section ();
1103 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1104 objc_meta_class_section ();
1105 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1106 objc_category_section ();
1107 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1108 objc_selector_refs_section ();
1109 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1110 objc_selector_fixup_section ();
1111 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1112 objc_symbols_section ();
1113 else if (!strncmp (name, "_OBJC_MODULES", 13))
1114 objc_module_info_section ();
1115 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1116 objc_image_info_section ();
1117 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1118 objc_cat_inst_meth_section ();
1119 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1120 objc_cat_cls_meth_section ();
1121 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1122 objc_cat_cls_meth_section ();
1123 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1124 objc_protocol_section ();
1125 else
1126 base_function ();
1128 else
1129 base_function ();
1132 /* This can be called with address expressions as "rtx".
1133 They must go in "const". */
1135 void
1136 machopic_select_rtx_section (enum machine_mode mode, rtx x,
1137 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1139 if (GET_MODE_SIZE (mode) == 8)
1140 literal8_section ();
1141 else if (GET_MODE_SIZE (mode) == 4
1142 && (GET_CODE (x) == CONST_INT
1143 || GET_CODE (x) == CONST_DOUBLE))
1144 literal4_section ();
1145 else if (MACHOPIC_INDIRECT
1146 && (GET_CODE (x) == SYMBOL_REF
1147 || GET_CODE (x) == CONST
1148 || GET_CODE (x) == LABEL_REF))
1149 const_data_section ();
1150 else
1151 const_section ();
1154 void
1155 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1157 if (MACHOPIC_INDIRECT)
1158 mod_init_section ();
1159 else
1160 constructor_section ();
1161 assemble_align (POINTER_SIZE);
1162 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1164 if (! MACHOPIC_INDIRECT)
1165 fprintf (asm_out_file, ".reference .constructors_used\n");
1168 void
1169 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1171 if (MACHOPIC_INDIRECT)
1172 mod_term_section ();
1173 else
1174 destructor_section ();
1175 assemble_align (POINTER_SIZE);
1176 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1178 if (! MACHOPIC_INDIRECT)
1179 fprintf (asm_out_file, ".reference .destructors_used\n");
1182 void
1183 darwin_globalize_label (FILE *stream, const char *name)
1185 if (!!strncmp (name, "_OBJC_", 6))
1186 default_globalize_label (stream, name);
1189 void
1190 darwin_asm_named_section (const char *name,
1191 unsigned int flags ATTRIBUTE_UNUSED,
1192 tree decl ATTRIBUTE_UNUSED)
1194 fprintf (asm_out_file, ".section %s\n", name);
1197 unsigned int
1198 darwin_section_type_flags (tree decl, const char *name, int reloc)
1200 unsigned int flags = default_section_type_flags (decl, name, reloc);
1202 /* Weak or linkonce variables live in a writable section. */
1203 if (decl != 0 && TREE_CODE (decl) != FUNCTION_DECL
1204 && (DECL_WEAK (decl) || DECL_ONE_ONLY (decl)))
1205 flags |= SECTION_WRITE;
1207 return flags;
1210 void
1211 darwin_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
1213 /* Darwin does not use unique sections. However, the target's
1214 unique_section hook is called for linkonce symbols. We need
1215 to set an appropriate section for such symbols. */
1216 if (DECL_ONE_ONLY (decl) && !DECL_SECTION_NAME (decl))
1217 darwin_make_decl_one_only (decl);
1220 #define HAVE_DEAD_STRIP 0
1222 static void
1223 no_dead_strip (FILE *file, const char *lab)
1225 if (HAVE_DEAD_STRIP)
1226 fprintf (file, ".no_dead_strip %s\n", lab);
1229 /* Emit a label for an FDE, making it global and/or weak if appropriate.
1230 The third parameter is nonzero if this is for exception handling.
1231 The fourth parameter is nonzero if this is just a placeholder for an
1232 FDE that we are omitting. */
1234 void
1235 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
1237 tree id = DECL_ASSEMBLER_NAME (decl)
1238 ? DECL_ASSEMBLER_NAME (decl)
1239 : DECL_NAME (decl);
1241 const char *prefix = "_";
1242 const int prefix_len = 1;
1244 const char *base = IDENTIFIER_POINTER (id);
1245 unsigned int base_len = IDENTIFIER_LENGTH (id);
1247 const char *suffix = ".eh";
1249 int need_quotes = name_needs_quotes (base);
1250 int quotes_len = need_quotes ? 2 : 0;
1251 char *lab;
1253 if (! for_eh)
1254 suffix = ".eh1";
1256 lab = xmalloc (prefix_len + base_len + strlen (suffix) + quotes_len + 1);
1257 lab[0] = '\0';
1259 if (need_quotes)
1260 strcat(lab, "\"");
1261 strcat(lab, prefix);
1262 strcat(lab, base);
1263 strcat(lab, suffix);
1264 if (need_quotes)
1265 strcat(lab, "\"");
1267 if (TREE_PUBLIC (decl))
1268 fprintf (file, "%s %s\n",
1269 (DECL_VISIBILITY (decl) != VISIBILITY_HIDDEN
1270 ? ".globl"
1271 : ".private_extern"),
1272 lab);
1274 if (DECL_ONE_ONLY (decl) && TREE_PUBLIC (decl))
1275 fprintf (file, ".weak_definition %s\n", lab);
1277 if (empty)
1279 fprintf (file, "%s = 0\n", lab);
1281 /* Mark the absolute .eh and .eh1 style labels as needed to
1282 ensure that we don't dead code strip them and keep such
1283 labels from another instantiation point until we can fix this
1284 properly with group comdat support. */
1285 no_dead_strip (file, lab);
1287 else
1288 fprintf (file, "%s:\n", lab);
1290 free (lab);
1293 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
1295 void
1296 darwin_non_lazy_pcrel (FILE *file, rtx addr)
1298 const char *nlp_name;
1300 if (GET_CODE (addr) != SYMBOL_REF)
1301 abort ();
1303 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
1304 fputs ("\t.long\t", file);
1305 ASM_OUTPUT_LABELREF (file, nlp_name);
1306 fputs ("-.", file);
1309 /* Emit an assembler directive to set visibility for a symbol. The
1310 only supported visibilities are VISIBILITY_DEFAULT and
1311 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
1312 extern". There is no MACH-O equivalent of ELF's
1313 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
1315 void
1316 darwin_assemble_visibility (tree decl, int vis)
1318 if (vis == VISIBILITY_DEFAULT)
1320 else if (vis == VISIBILITY_HIDDEN)
1322 fputs ("\t.private_extern ", asm_out_file);
1323 assemble_name (asm_out_file,
1324 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
1325 fputs ("\n", asm_out_file);
1327 else
1328 warning ("internal and protected visibility attributes not supported"
1329 "in this configuration; ignored");
1332 /* Output a difference of two labels that will be an assembly time
1333 constant if the two labels are local. (.long lab1-lab2 will be
1334 very different if lab1 is at the boundary between two sections; it
1335 will be relocated according to the second section, not the first,
1336 so one ends up with a difference between labels in different
1337 sections, which is bad in the dwarf2 eh context for instance.) */
1339 static int darwin_dwarf_label_counter;
1341 void
1342 darwin_asm_output_dwarf_delta (FILE *file, int size ATTRIBUTE_UNUSED,
1343 const char *lab1, const char *lab2)
1345 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
1346 && lab2[0] == '*' && lab2[1] == 'L');
1348 if (islocaldiff)
1349 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
1350 else
1351 fprintf (file, "\t%s\t", ".long");
1352 assemble_name (file, lab1);
1353 fprintf (file, "-");
1354 assemble_name (file, lab2);
1355 if (islocaldiff)
1356 fprintf (file, "\n\t.long L$set$%d", darwin_dwarf_label_counter++);
1359 void
1360 darwin_file_end (void)
1362 machopic_finish (asm_out_file);
1363 if (strcmp (lang_hooks.name, "GNU C++") == 0)
1365 constructor_section ();
1366 destructor_section ();
1367 ASM_OUTPUT_ALIGN (asm_out_file, 1);
1369 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
1372 /* True, iff we're generating fast turn around debugging code. When
1373 true, we arrange for function prologues to start with 4 nops so
1374 that gdb may insert code to redirect them, and for data to accessed
1375 indirectly. The runtime uses this indirection to forward
1376 references for data to the original instance of that data. */
1378 int darwin_fix_and_continue;
1379 const char *darwin_fix_and_continue_switch;
1381 #include "gt-darwin.h"