1 /* Functions for generic Darwin as target machine for GNU C compiler.
2 Copyright (C) 1989, 1990, 1991, 1992, 1993, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Contributed by Apple Computer Inc.
6 This file is part of GNU CC.
8 GNU CC 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)
13 GNU CC 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 GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
25 #include "coretypes.h"
29 #include "hard-reg-set.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-flags.h"
35 #include "insn-attr.h"
42 #include "langhooks.h"
45 static int machopic_data_defined_p PARAMS ((const char *));
46 static void update_non_lazy_ptrs PARAMS ((const char *));
47 static void update_stubs PARAMS ((const char *));
50 name_needs_quotes (name)
54 while ((c = *name++) != '\0')
61 * flag_pic = 1 ... generate only indirections
62 * flag_pic = 2 ... generate indirections and pure code
65 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
66 reference, which will not be changed. */
68 static GTY(()) tree machopic_defined_list;
70 enum machopic_addr_class
71 machopic_classify_ident (ident)
74 const char *name = IDENTIFIER_POINTER (ident);
75 int lprefix = (((name[0] == '*' || name[0] == '&')
76 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
85 /* The PIC base symbol is always defined. */
86 if (! strcmp (name, "<pic base>"))
87 return MACHOPIC_DEFINED_DATA;
91 /* Here if no special encoding to be found. */
94 const char *name = IDENTIFIER_POINTER (ident);
95 int len = strlen (name);
97 if ((len > 5 && !strcmp (name + len - 5, "$stub"))
98 || (len > 6 && !strcmp (name + len - 6, "$stub\"")))
99 return MACHOPIC_DEFINED_FUNCTION;
100 return MACHOPIC_DEFINED_DATA;
103 for (temp = machopic_defined_list;
105 temp = TREE_CHAIN (temp))
107 if (ident == TREE_VALUE (temp))
108 return MACHOPIC_DEFINED_DATA;
111 if (TREE_ASM_WRITTEN (ident))
112 return MACHOPIC_DEFINED_DATA;
114 return MACHOPIC_UNDEFINED;
117 else if (name[1] == 'D')
118 return MACHOPIC_DEFINED_DATA;
120 else if (name[1] == 'T')
121 return MACHOPIC_DEFINED_FUNCTION;
123 /* It is possible that someone is holding a "stale" name, which has
124 since been defined. See if there is a "defined" name (i.e,
125 different from NAME only in having a '!D_' or a '!T_' instead of
126 a '!d_' or '!t_' prefix) in the identifier hash tables. If so, say
127 that this identifier is defined. */
128 else if (name[1] == 'd' || name[1] == 't')
131 new_name = (char *)alloca (strlen (name) + 1);
132 strcpy (new_name, name);
133 new_name[1] = (name[1] == 'd') ? 'D' : 'T';
134 if (maybe_get_identifier (new_name) != NULL)
135 return (name[1] == 'd') ? MACHOPIC_DEFINED_DATA
136 : MACHOPIC_DEFINED_FUNCTION;
139 for (temp = machopic_defined_list; temp != NULL_TREE; temp = TREE_CHAIN (temp))
141 if (ident == TREE_VALUE (temp))
144 return MACHOPIC_DEFINED_FUNCTION;
146 return MACHOPIC_DEFINED_DATA;
150 if (name[1] == 't' || name[1] == 'T')
153 return MACHOPIC_DEFINED_FUNCTION;
155 return MACHOPIC_UNDEFINED_FUNCTION;
160 return MACHOPIC_DEFINED_DATA;
162 return MACHOPIC_UNDEFINED_DATA;
167 enum machopic_addr_class
168 machopic_classify_name (name)
171 return machopic_classify_ident (get_identifier (name));
175 machopic_ident_defined_p (ident)
178 switch (machopic_classify_ident (ident))
180 case MACHOPIC_UNDEFINED:
181 case MACHOPIC_UNDEFINED_DATA:
182 case MACHOPIC_UNDEFINED_FUNCTION:
190 machopic_data_defined_p (name)
193 switch (machopic_classify_ident (get_identifier (name)))
195 case MACHOPIC_DEFINED_DATA:
203 machopic_name_defined_p (name)
206 return machopic_ident_defined_p (get_identifier (name));
210 machopic_define_ident (ident)
213 if (!machopic_ident_defined_p (ident))
214 machopic_defined_list =
215 tree_cons (NULL_TREE, ident, machopic_defined_list);
219 machopic_define_name (name)
222 machopic_define_ident (get_identifier (name));
225 static GTY(()) char * function_base;
228 machopic_function_base_name ()
230 const char *current_name;
231 /* if dynamic-no-pic is on, we should not get here */
232 if (MACHO_DYNAMIC_NO_PIC_P)
235 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
237 if (function_base == NULL)
239 (char *) ggc_alloc_string ("<pic base>", sizeof ("<pic base>"));
241 current_function_uses_pic_offset_table = 1;
243 return function_base;
246 static GTY(()) const char * function_base_func_name;
247 static GTY(()) int current_pic_label_num;
250 machopic_output_function_base_name (FILE *file)
252 const char *current_name;
254 /* If dynamic-no-pic is on, we should not get here. */
255 if (MACHO_DYNAMIC_NO_PIC_P)
258 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
259 if (function_base_func_name != current_name)
261 ++current_pic_label_num;
262 function_base_func_name = current_name;
264 fprintf (file, "\"L%011d$pb\"", current_pic_label_num);
267 static GTY(()) tree machopic_non_lazy_pointers;
269 /* Return a non-lazy pointer name corresponding to the given name,
270 either by finding it in our list of pointer names, or by generating
274 machopic_non_lazy_ptr_name (name)
277 const char *temp_name;
278 tree temp, ident = get_identifier (name);
280 for (temp = machopic_non_lazy_pointers;
282 temp = TREE_CHAIN (temp))
284 if (ident == TREE_VALUE (temp))
285 return IDENTIFIER_POINTER (TREE_PURPOSE (temp));
288 name = darwin_strip_name_encoding (name);
290 /* Try again, but comparing names this time. */
291 for (temp = machopic_non_lazy_pointers;
293 temp = TREE_CHAIN (temp))
295 if (TREE_VALUE (temp))
297 temp_name = IDENTIFIER_POINTER (TREE_VALUE (temp));
298 temp_name = darwin_strip_name_encoding (temp_name);
299 if (strcmp (name, temp_name) == 0)
300 return IDENTIFIER_POINTER (TREE_PURPOSE (temp));
308 buffer = alloca (strlen (name) + 20);
310 strcpy (buffer, "&L");
312 strcat (buffer, name+1);
315 strcat (buffer, "_");
316 strcat (buffer, name);
319 strcat (buffer, "$non_lazy_ptr");
320 ptr_name = get_identifier (buffer);
322 machopic_non_lazy_pointers
323 = tree_cons (ptr_name, ident, machopic_non_lazy_pointers);
325 TREE_USED (machopic_non_lazy_pointers) = 0;
327 return IDENTIFIER_POINTER (ptr_name);
331 static GTY(()) tree machopic_stubs;
333 /* Return the name of the stub corresponding to the given name,
334 generating a new stub name if necessary. */
337 machopic_stub_name (name)
340 tree temp, ident = get_identifier (name);
343 for (temp = machopic_stubs;
345 temp = TREE_CHAIN (temp))
347 if (ident == TREE_VALUE (temp))
348 return IDENTIFIER_POINTER (TREE_PURPOSE (temp));
349 tname = IDENTIFIER_POINTER (TREE_VALUE (temp));
350 if (strcmp (name, tname) == 0)
351 return IDENTIFIER_POINTER (TREE_PURPOSE (temp));
352 /* A library call name might not be section-encoded yet, so try
353 it against a stripped name. */
356 && strcmp (name, tname + 4) == 0)
357 return IDENTIFIER_POINTER (TREE_PURPOSE (temp));
360 name = darwin_strip_name_encoding (name);
365 int needs_quotes = name_needs_quotes (name);
367 buffer = alloca (strlen (name) + 20);
370 strcpy (buffer, "&\"L");
372 strcpy (buffer, "&L");
375 strcat (buffer, name+1);
379 strcat (buffer, "_");
380 strcat (buffer, name);
384 strcat (buffer, "$stub\"");
386 strcat (buffer, "$stub");
387 ptr_name = get_identifier (buffer);
389 machopic_stubs = tree_cons (ptr_name, ident, machopic_stubs);
390 TREE_USED (machopic_stubs) = 0;
392 return IDENTIFIER_POINTER (ptr_name);
397 machopic_validate_stub_or_non_lazy_ptr (name, validate_stub)
401 const char *real_name;
402 tree temp, ident = get_identifier (name), id2;
404 for (temp = (validate_stub ? machopic_stubs : machopic_non_lazy_pointers);
406 temp = TREE_CHAIN (temp))
407 if (ident == TREE_PURPOSE (temp))
409 /* Mark both the stub or non-lazy pointer as well as the
410 original symbol as being referenced. */
411 TREE_USED (temp) = 1;
412 if (TREE_CODE (TREE_VALUE (temp)) == IDENTIFIER_NODE)
413 TREE_SYMBOL_REFERENCED (TREE_VALUE (temp)) = 1;
414 real_name = IDENTIFIER_POINTER (TREE_VALUE (temp));
415 real_name = darwin_strip_name_encoding (real_name);
416 id2 = maybe_get_identifier (real_name);
418 TREE_SYMBOL_REFERENCED (id2) = 1;
422 /* Transform ORIG, which may be any data source, to the corresponding
423 source using indirections. */
426 machopic_indirect_data_reference (orig, reg)
431 if (! MACHOPIC_INDIRECT)
434 if (GET_CODE (orig) == SYMBOL_REF)
436 const char *name = XSTR (orig, 0);
438 int defined = machopic_data_defined_p (name);
440 if (defined && MACHO_DYNAMIC_NO_PIC_P)
442 #if defined (TARGET_TOC)
443 emit_insn (gen_macho_high (reg, orig));
444 emit_insn (gen_macho_low (reg, reg, orig));
446 /* some other cpu -- writeme! */
453 #if defined (TARGET_TOC) || defined (HAVE_lo_sum)
454 rtx pic_base = gen_rtx (SYMBOL_REF, Pmode,
455 machopic_function_base_name ());
456 rtx offset = gen_rtx (CONST, Pmode,
457 gen_rtx (MINUS, Pmode, orig, pic_base));
460 #if defined (TARGET_TOC) /* i.e., PowerPC */
461 rtx hi_sum_reg = reg;
466 emit_insn (gen_rtx (SET, Pmode, hi_sum_reg,
467 gen_rtx (PLUS, Pmode, pic_offset_table_rtx,
468 gen_rtx (HIGH, Pmode, offset))));
469 emit_insn (gen_rtx (SET, Pmode, reg,
470 gen_rtx (LO_SUM, Pmode, hi_sum_reg, offset)));
474 #if defined (HAVE_lo_sum)
475 if (reg == 0) abort ();
477 emit_insn (gen_rtx (SET, VOIDmode, reg,
478 gen_rtx (HIGH, Pmode, offset)));
479 emit_insn (gen_rtx (SET, VOIDmode, reg,
480 gen_rtx (LO_SUM, Pmode, reg, offset)));
481 emit_insn (gen_rtx (USE, VOIDmode,
482 gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM)));
484 orig = gen_rtx (PLUS, Pmode, pic_offset_table_rtx, reg);
490 ptr_ref = gen_rtx (SYMBOL_REF, Pmode,
491 machopic_non_lazy_ptr_name (name));
493 ptr_ref = gen_rtx_MEM (Pmode, ptr_ref);
494 RTX_UNCHANGING_P (ptr_ref) = 1;
498 else if (GET_CODE (orig) == CONST)
502 /* legitimize both operands of the PLUS */
503 if (GET_CODE (XEXP (orig, 0)) == PLUS)
505 base = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 0),
507 orig = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 1),
508 (base == reg ? 0 : reg));
513 if (MACHOPIC_PURE && GET_CODE (orig) == CONST_INT)
514 result = plus_constant (base, INTVAL (orig));
516 result = gen_rtx (PLUS, Pmode, base, orig);
518 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
522 emit_move_insn (reg, result);
527 result = force_reg (GET_MODE (result), result);
534 else if (GET_CODE (orig) == MEM)
535 XEXP (ptr_ref, 0) = machopic_indirect_data_reference (XEXP (orig, 0), reg);
536 /* When the target is i386, this code prevents crashes due to the
537 compiler's ignorance on how to move the PIC base register to
538 other registers. (The reload phase sometimes introduces such
540 else if (GET_CODE (orig) == PLUS
541 && GET_CODE (XEXP (orig, 0)) == REG
542 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
544 /* Prevent the same register from being erroneously used
545 as both the base and index registers. */
546 && GET_CODE (XEXP (orig, 1)) == CONST
550 emit_move_insn (reg, XEXP (orig, 0));
551 XEXP (ptr_ref, 0) = reg;
556 /* Transform TARGET (a MEM), which is a function call target, to the
557 corresponding symbol_stub if necessary. Return a new MEM. */
560 machopic_indirect_call_target (target)
563 if (GET_CODE (target) != MEM)
566 if (MACHOPIC_INDIRECT && GET_CODE (XEXP (target, 0)) == SYMBOL_REF)
568 enum machine_mode mode = GET_MODE (XEXP (target, 0));
569 const char *name = XSTR (XEXP (target, 0), 0);
571 /* If the name is already defined, we need do nothing. */
572 if (name[0] == '!' && name[1] == 'T')
575 if (!machopic_name_defined_p (name))
577 const char *stub_name = machopic_stub_name (name);
579 XEXP (target, 0) = gen_rtx (SYMBOL_REF, mode, stub_name);
580 RTX_UNCHANGING_P (target) = 1;
588 machopic_legitimize_pic_address (orig, mode, reg)
590 enum machine_mode mode;
594 if (! MACHOPIC_INDIRECT)
597 /* First handle a simple SYMBOL_REF or LABEL_REF */
598 if (GET_CODE (orig) == LABEL_REF
599 || (GET_CODE (orig) == SYMBOL_REF
602 /* addr(foo) = &func+(foo-func) */
605 orig = machopic_indirect_data_reference (orig, reg);
607 if (GET_CODE (orig) == PLUS
608 && GET_CODE (XEXP (orig, 0)) == REG)
611 return force_reg (mode, orig);
613 emit_move_insn (reg, orig);
617 /* if dynamic-no-pic then use 0 as the pic base */
618 if (MACHO_DYNAMIC_NO_PIC_P)
619 pic_base = CONST0_RTX (Pmode);
621 pic_base = gen_rtx (SYMBOL_REF, Pmode, machopic_function_base_name ());
623 if (GET_CODE (orig) == MEM)
627 if (reload_in_progress)
630 reg = gen_reg_rtx (Pmode);
634 if (MACHO_DYNAMIC_NO_PIC_P
635 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
636 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
638 #if defined (TARGET_TOC) /* ppc */
639 rtx temp_reg = (no_new_pseudos) ? reg : gen_reg_rtx (Pmode);
640 rtx asym = XEXP (orig, 0);
643 emit_insn (gen_macho_high (temp_reg, asym));
644 mem = gen_rtx_MEM (GET_MODE (orig),
645 gen_rtx (LO_SUM, Pmode, temp_reg, asym));
646 RTX_UNCHANGING_P (mem) = 1;
647 emit_insn (gen_rtx (SET, VOIDmode, reg, mem));
649 /* Some other CPU -- WriteMe! but right now there are no other platform that can use dynamic-no-pic */
655 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
656 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
658 rtx offset = gen_rtx (CONST, Pmode,
659 gen_rtx (MINUS, Pmode,
660 XEXP (orig, 0), pic_base));
661 #if defined (TARGET_TOC) /* i.e., PowerPC */
662 /* Generating a new reg may expose opportunities for
663 common subexpression elimination. */
665 (reload_in_progress ? reg : gen_reg_rtx (SImode));
667 emit_insn (gen_rtx (SET, Pmode, hi_sum_reg,
668 gen_rtx (PLUS, Pmode,
669 pic_offset_table_rtx,
670 gen_rtx (HIGH, Pmode, offset))));
671 emit_insn (gen_rtx (SET, VOIDmode, reg,
672 gen_rtx (MEM, GET_MODE (orig),
673 gen_rtx (LO_SUM, Pmode,
674 hi_sum_reg, offset))));
678 emit_insn (gen_rtx (USE, VOIDmode,
679 gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM)));
681 emit_insn (gen_rtx (SET, VOIDmode, reg,
682 gen_rtx (HIGH, Pmode,
683 gen_rtx (CONST, Pmode, offset))));
684 emit_insn (gen_rtx (SET, VOIDmode, reg,
685 gen_rtx (LO_SUM, Pmode, reg,
686 gen_rtx (CONST, Pmode, offset))));
687 pic_ref = gen_rtx (PLUS, Pmode,
688 pic_offset_table_rtx, reg);
692 #endif /* HAVE_lo_sum */
694 rtx pic = pic_offset_table_rtx;
695 if (GET_CODE (pic) != REG)
697 emit_move_insn (reg, pic);
701 emit_insn (gen_rtx (USE, VOIDmode,
702 gen_rtx (REG, Pmode, PIC_OFFSET_TABLE_REGNUM)));
705 pic_ref = gen_rtx (PLUS, Pmode,
707 gen_rtx (CONST, Pmode,
708 gen_rtx (MINUS, Pmode,
713 #if !defined (TARGET_TOC)
714 emit_move_insn (reg, pic_ref);
715 pic_ref = gen_rtx (MEM, GET_MODE (orig), reg);
717 RTX_UNCHANGING_P (pic_ref) = 1;
723 if (GET_CODE (orig) == SYMBOL_REF
724 || GET_CODE (orig) == LABEL_REF)
726 rtx offset = gen_rtx (CONST, Pmode,
727 gen_rtx (MINUS, Pmode, orig, pic_base));
728 #if defined (TARGET_TOC) /* i.e., PowerPC */
733 if (reload_in_progress)
736 reg = gen_reg_rtx (SImode);
741 emit_insn (gen_rtx (SET, Pmode, hi_sum_reg,
742 (MACHO_DYNAMIC_NO_PIC_P)
743 ? gen_rtx (HIGH, Pmode, offset)
744 : gen_rtx (PLUS, Pmode,
745 pic_offset_table_rtx,
746 gen_rtx (HIGH, Pmode, offset))));
747 emit_insn (gen_rtx (SET, VOIDmode, reg,
748 gen_rtx (LO_SUM, Pmode,
749 hi_sum_reg, offset)));
751 RTX_UNCHANGING_P (pic_ref) = 1;
753 emit_insn (gen_rtx (SET, VOIDmode, reg,
754 gen_rtx (HIGH, Pmode, offset)));
755 emit_insn (gen_rtx (SET, VOIDmode, reg,
756 gen_rtx (LO_SUM, Pmode, reg, offset)));
757 pic_ref = gen_rtx (PLUS, Pmode,
758 pic_offset_table_rtx, reg);
759 RTX_UNCHANGING_P (pic_ref) = 1;
763 #endif /* HAVE_lo_sum */
765 if (GET_CODE (orig) == REG)
771 rtx pic = pic_offset_table_rtx;
772 if (GET_CODE (pic) != REG)
774 emit_move_insn (reg, pic);
778 emit_insn (gen_rtx (USE, VOIDmode,
779 pic_offset_table_rtx));
781 pic_ref = gen_rtx (PLUS, Pmode,
783 gen_rtx (CONST, Pmode,
784 gen_rtx (MINUS, Pmode,
790 if (GET_CODE (pic_ref) != REG)
794 emit_move_insn (reg, pic_ref);
799 return force_reg (mode, pic_ref);
808 else if (GET_CODE (orig) == SYMBOL_REF)
811 else if (GET_CODE (orig) == PLUS
812 && (GET_CODE (XEXP (orig, 0)) == MEM
813 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
814 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
815 && XEXP (orig, 0) != pic_offset_table_rtx
816 && GET_CODE (XEXP (orig, 1)) != REG)
820 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
822 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
823 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
824 Pmode, (base == reg ? 0 : reg));
825 if (GET_CODE (orig) == CONST_INT)
827 pic_ref = plus_constant (base, INTVAL (orig));
831 pic_ref = gen_rtx (PLUS, Pmode, base, orig);
833 if (RTX_UNCHANGING_P (base) && RTX_UNCHANGING_P (orig))
834 RTX_UNCHANGING_P (pic_ref) = 1;
836 if (reg && is_complex)
838 emit_move_insn (reg, pic_ref);
841 /* Likewise, should we set special REG_NOTEs here? */
844 else if (GET_CODE (orig) == CONST)
846 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
849 else if (GET_CODE (orig) == MEM
850 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
852 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
854 addr = gen_rtx (MEM, GET_MODE (orig), addr);
855 RTX_UNCHANGING_P (addr) = RTX_UNCHANGING_P (orig);
856 emit_move_insn (reg, addr);
865 machopic_finish (asm_out_file)
870 for (temp = machopic_stubs;
872 temp = TREE_CHAIN (temp))
874 const char *sym_name = IDENTIFIER_POINTER (TREE_VALUE (temp));
875 const char *stub_name = IDENTIFIER_POINTER (TREE_PURPOSE (temp));
879 if (! TREE_USED (temp))
882 /* If the symbol is actually defined, we don't need a stub. */
883 if (sym_name[0] == '!' && sym_name[1] == 'T')
886 sym_name = darwin_strip_name_encoding (sym_name);
888 sym = alloca (strlen (sym_name) + 2);
889 if (sym_name[0] == '*' || sym_name[0] == '&')
890 strcpy (sym, sym_name + 1);
891 else if (sym_name[0] == '-' || sym_name[0] == '+')
892 strcpy (sym, sym_name);
894 sym[0] = '_', strcpy (sym + 1, sym_name);
896 stub = alloca (strlen (stub_name) + 2);
897 if (stub_name[0] == '*' || stub_name[0] == '&')
898 strcpy (stub, stub_name + 1);
900 stub[0] = '_', strcpy (stub + 1, stub_name);
902 machopic_output_stub (asm_out_file, sym, stub);
905 for (temp = machopic_non_lazy_pointers;
907 temp = TREE_CHAIN (temp))
909 const char *const sym_name = IDENTIFIER_POINTER (TREE_VALUE (temp));
910 const char *const lazy_name = IDENTIFIER_POINTER (TREE_PURPOSE (temp));
912 if (! TREE_USED (temp))
915 if (machopic_ident_defined_p (TREE_VALUE (temp)))
918 assemble_align (GET_MODE_ALIGNMENT (Pmode));
919 assemble_label (lazy_name);
920 assemble_integer (gen_rtx (SYMBOL_REF, Pmode, sym_name),
921 GET_MODE_SIZE (Pmode),
922 GET_MODE_ALIGNMENT (Pmode), 1);
926 machopic_nl_symbol_ptr_section ();
927 assemble_name (asm_out_file, lazy_name);
928 fprintf (asm_out_file, ":\n");
930 fprintf (asm_out_file, "\t.indirect_symbol ");
931 assemble_name (asm_out_file, sym_name);
932 fprintf (asm_out_file, "\n");
934 assemble_integer (const0_rtx, GET_MODE_SIZE (Pmode),
935 GET_MODE_ALIGNMENT (Pmode), 1);
941 machopic_operand_p (op)
944 if (MACHOPIC_JUST_INDIRECT)
946 while (GET_CODE (op) == CONST)
949 if (GET_CODE (op) == SYMBOL_REF)
950 return machopic_name_defined_p (XSTR (op, 0));
955 while (GET_CODE (op) == CONST)
958 if (GET_CODE (op) == MINUS
959 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
960 && GET_CODE (XEXP (op, 1)) == SYMBOL_REF
961 && machopic_name_defined_p (XSTR (XEXP (op, 0), 0))
962 && machopic_name_defined_p (XSTR (XEXP (op, 1), 0)))
968 /* This function records whether a given name corresponds to a defined
969 or undefined function or variable, for machopic_classify_ident to
973 darwin_encode_section_info (decl, rtl, first)
976 int first ATTRIBUTE_UNUSED;
981 const char *orig_str;
985 /* Do the standard encoding things first. */
986 default_encode_section_info (decl, rtl, first);
988 /* With the introduction of symbol_ref flags, some of the following
989 code has become redundant and should be removed at some point. */
991 if ((TREE_CODE (decl) == FUNCTION_DECL
992 || TREE_CODE (decl) == VAR_DECL)
993 && !DECL_EXTERNAL (decl)
994 && ((TREE_STATIC (decl)
995 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
996 || (DECL_INITIAL (decl)
997 && DECL_INITIAL (decl) != error_mark_node)))
1000 if (TREE_CODE (decl) == FUNCTION_DECL)
1001 code = (defined ? 'T' : 't');
1002 else if (TREE_CODE (decl) == VAR_DECL)
1003 code = (defined ? 'D' : 'd');
1008 sym_ref = XEXP (rtl, 0);
1009 orig_str = XSTR (sym_ref, 0);
1010 len = strlen (orig_str) + 1;
1012 if (orig_str[0] == '!')
1014 /* Already encoded; see if we need to change it. */
1015 if (code == orig_str[1])
1017 /* Yes, tweak a copy of the name and put it in a new string. */
1018 new_str = alloca (len);
1019 memcpy (new_str, orig_str, len);
1021 XSTR (sym_ref, 0) = ggc_alloc_string (new_str, len);
1025 /* Add the encoding. */
1027 new_str = alloca (new_len);
1032 memcpy (new_str + 4, orig_str, len);
1033 XSTR (sym_ref, 0) = ggc_alloc_string (new_str, new_len);
1035 /* The non-lazy pointer list may have captured references to the
1036 old encoded name, change them. */
1037 if (TREE_CODE (decl) == VAR_DECL)
1038 update_non_lazy_ptrs (XSTR (sym_ref, 0));
1040 update_stubs (XSTR (sym_ref, 0));
1043 /* Undo the effects of the above. */
1046 darwin_strip_name_encoding (str)
1049 return str[0] == '!' ? str + 4 : str;
1052 /* Scan the list of non-lazy pointers and update any recorded names whose
1053 stripped name matches the argument. */
1056 update_non_lazy_ptrs (name)
1059 const char *name1, *name2;
1062 name1 = darwin_strip_name_encoding (name);
1064 for (temp = machopic_non_lazy_pointers;
1066 temp = TREE_CHAIN (temp))
1068 const char *sym_name = IDENTIFIER_POINTER (TREE_VALUE (temp));
1070 if (*sym_name == '!')
1072 name2 = darwin_strip_name_encoding (sym_name);
1073 if (strcmp (name1, name2) == 0)
1075 /* FIXME: This breaks the identifier hash table. */
1076 IDENTIFIER_NODE_CHECK (TREE_VALUE (temp))->identifier.id.str
1077 = (unsigned char *) name;
1084 /* Function NAME is being defined, and its label has just been output.
1085 If there's already a reference to a stub for this function, we can
1086 just emit the stub label now and we don't bother emitting the stub later. */
1089 machopic_output_possible_stub_label (file, name)
1096 /* Ensure we're looking at a section-encoded name. */
1097 if (name[0] != '!' || (name[1] != 't' && name[1] != 'T'))
1100 for (temp = machopic_stubs;
1102 temp = TREE_CHAIN (temp))
1104 const char *sym_name;
1106 sym_name = IDENTIFIER_POINTER (TREE_VALUE (temp));
1107 if (sym_name[0] == '!' && sym_name[1] == 'T'
1108 && ! strcmp (name+2, sym_name+2))
1110 ASM_OUTPUT_LABEL (file, IDENTIFIER_POINTER (TREE_PURPOSE (temp)));
1111 /* Avoid generating a stub for this. */
1112 TREE_USED (temp) = 0;
1118 /* Scan the list of stubs and update any recorded names whose
1119 stripped name matches the argument. */
1125 const char *name1, *name2;
1128 name1 = darwin_strip_name_encoding (name);
1130 for (temp = machopic_stubs;
1132 temp = TREE_CHAIN (temp))
1134 const char *sym_name = IDENTIFIER_POINTER (TREE_VALUE (temp));
1136 if (*sym_name == '!')
1138 name2 = darwin_strip_name_encoding (sym_name);
1139 if (strcmp (name1, name2) == 0)
1141 /* FIXME: This breaks the identifier hash table. */
1142 IDENTIFIER_NODE_CHECK (TREE_VALUE (temp))->identifier.id.str
1143 = (unsigned char *) name;
1151 machopic_select_section (exp, reloc, align)
1154 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
1156 void (*base_function)(void);
1158 if (decl_readonly_section_1 (exp, reloc, MACHOPIC_INDIRECT))
1159 base_function = readonly_data_section;
1160 else if (TREE_READONLY (exp) || TREE_CONSTANT (exp))
1161 base_function = const_data_section;
1163 base_function = data_section;
1165 if (TREE_CODE (exp) == STRING_CST
1166 && ((size_t) TREE_STRING_LENGTH (exp)
1167 == strlen (TREE_STRING_POINTER (exp)) + 1)
1168 && ! flag_writable_strings)
1170 else if ((TREE_CODE (exp) == INTEGER_CST || TREE_CODE (exp) == REAL_CST)
1171 && flag_merge_constants)
1173 tree size = TYPE_SIZE (TREE_TYPE (exp));
1175 if (TREE_CODE (size) == INTEGER_CST &&
1176 TREE_INT_CST_LOW (size) == 4 &&
1177 TREE_INT_CST_HIGH (size) == 0)
1178 literal4_section ();
1179 else if (TREE_CODE (size) == INTEGER_CST &&
1180 TREE_INT_CST_LOW (size) == 8 &&
1181 TREE_INT_CST_HIGH (size) == 0)
1182 literal8_section ();
1186 else if (TREE_CODE (exp) == CONSTRUCTOR
1188 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
1189 && TYPE_NAME (TREE_TYPE (exp)))
1191 tree name = TYPE_NAME (TREE_TYPE (exp));
1192 if (TREE_CODE (name) == TYPE_DECL)
1193 name = DECL_NAME (name);
1194 if (!strcmp (IDENTIFIER_POINTER (name), "NSConstantString"))
1195 objc_constant_string_object_section ();
1196 else if (!strcmp (IDENTIFIER_POINTER (name), "NXConstantString"))
1197 objc_string_object_section ();
1201 else if (TREE_CODE (exp) == VAR_DECL &&
1203 TREE_CODE (DECL_NAME (exp)) == IDENTIFIER_NODE &&
1204 IDENTIFIER_POINTER (DECL_NAME (exp)) &&
1205 !strncmp (IDENTIFIER_POINTER (DECL_NAME (exp)), "_OBJC_", 6))
1207 const char *name = IDENTIFIER_POINTER (DECL_NAME (exp));
1209 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1210 objc_cls_meth_section ();
1211 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1212 objc_inst_meth_section ();
1213 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 20))
1214 objc_cat_cls_meth_section ();
1215 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 23))
1216 objc_cat_inst_meth_section ();
1217 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1218 objc_class_vars_section ();
1219 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1220 objc_instance_vars_section ();
1221 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1222 objc_cat_cls_meth_section ();
1223 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1224 objc_class_names_section ();
1225 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1226 objc_meth_var_names_section ();
1227 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1228 objc_meth_var_types_section ();
1229 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1230 objc_cls_refs_section ();
1231 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1232 objc_class_section ();
1233 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1234 objc_meta_class_section ();
1235 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1236 objc_category_section ();
1237 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1238 objc_selector_refs_section ();
1239 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1240 objc_selector_fixup_section ();
1241 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1242 objc_symbols_section ();
1243 else if (!strncmp (name, "_OBJC_MODULES", 13))
1244 objc_module_info_section ();
1245 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1246 objc_cat_inst_meth_section ();
1247 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1248 objc_cat_cls_meth_section ();
1249 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1250 objc_cat_cls_meth_section ();
1251 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1252 objc_protocol_section ();
1260 /* This can be called with address expressions as "rtx".
1261 They must go in "const". */
1264 machopic_select_rtx_section (mode, x, align)
1265 enum machine_mode mode;
1267 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
1269 if (GET_MODE_SIZE (mode) == 8)
1270 literal8_section ();
1271 else if (GET_MODE_SIZE (mode) == 4
1272 && (GET_CODE (x) == CONST_INT
1273 || GET_CODE (x) == CONST_DOUBLE))
1274 literal4_section ();
1280 machopic_asm_out_constructor (symbol, priority)
1282 int priority ATTRIBUTE_UNUSED;
1285 if (MACHOPIC_INDIRECT)
1286 mod_init_section ();
1288 constructor_section ();
1289 assemble_align (POINTER_SIZE);
1290 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1293 if (! MACHOPIC_INDIRECT)
1294 fprintf (asm_out_file, ".reference .constructors_used\n");
1298 machopic_asm_out_destructor (symbol, priority)
1300 int priority ATTRIBUTE_UNUSED;
1303 if (MACHOPIC_INDIRECT)
1304 mod_term_section ();
1306 destructor_section ();
1307 assemble_align (POINTER_SIZE);
1308 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1310 if (! MACHOPIC_INDIRECT)
1311 fprintf (asm_out_file, ".reference .destructors_used\n");
1315 darwin_globalize_label (stream, name)
1319 if (!!strncmp (name, "_OBJC_", 6))
1320 default_globalize_label (stream, name);
1323 /* Output a difference of two labels that will be an assembly time
1324 constant if the two labels are local. (.long lab1-lab2 will be
1325 very different if lab1 is at the boundary between two sections; it
1326 will be relocated according to the second section, not the first,
1327 so one ends up with a difference between labels in different
1328 sections, which is bad in the dwarf2 eh context for instance.) */
1330 static int darwin_dwarf_label_counter;
1333 darwin_asm_output_dwarf_delta (file, size, lab1, lab2)
1335 int size ATTRIBUTE_UNUSED;
1336 const char *lab1, *lab2;
1338 const char *p = lab1 + (lab1[0] == '*');
1339 int islocaldiff = (p[0] == 'L');
1342 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
1344 fprintf (file, "\t%s\t", ".long");
1345 assemble_name (file, lab1);
1346 fprintf (file, "-");
1347 assemble_name (file, lab2);
1349 fprintf (file, "\n\t.long L$set$%d", darwin_dwarf_label_counter++);
1355 machopic_finish (asm_out_file);
1356 if (strcmp (lang_hooks.name, "GNU C++") == 0)
1358 constructor_section ();
1359 destructor_section ();
1360 ASM_OUTPUT_ALIGN (asm_out_file, 1);
1364 #include "gt-darwin.h"