1  /* Target definitions for x86 running Darwin.
       2     Copyright (C) 2001-2023 Free Software Foundation, Inc.
       3     Contributed by Apple Computer Inc.
       4  
       5  This file is part of GCC.
       6  
       7  GCC is free software; you can redistribute it and/or modify
       8  it under the terms of the GNU General Public License as published by
       9  the Free Software Foundation; either version 3, or (at your option)
      10  any later version.
      11  
      12  GCC is distributed in the hope that it will be useful,
      13  but WITHOUT ANY WARRANTY; without even the implied warranty of
      14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15  GNU General Public License for more details.
      16  
      17  You should have received a copy of the GNU General Public License
      18  along with GCC; see the file COPYING3.  If not see
      19  <http://www.gnu.org/licenses/>.  */
      20  
      21  /* Enable Mach-O bits in generic x86 code.  */
      22  #undef TARGET_MACHO
      23  #define TARGET_MACHO 1
      24  
      25  #undef DARWIN_X86
      26  #define DARWIN_X86 1
      27  
      28  /* Size of the Obj-C jump buffer.  */
      29  #define OBJC_JBLEN ((TARGET_64BIT) ? ((9 * 2) + 3 + 16) : (18))
      30  
      31  #undef TARGET_FPMATH_DEFAULT
      32  #define TARGET_FPMATH_DEFAULT (TARGET_SSE ? FPMATH_SSE : FPMATH_387)
      33  #undef TARGET_FPMATH_DEFAULT_P
      34  #define TARGET_FPMATH_DEFAULT_P(x) \
      35    (TARGET_SSE_P(x) ? FPMATH_SSE : FPMATH_387)
      36  
      37  #define TARGET_OS_CPP_BUILTINS()                \
      38    do {						\
      39      builtin_define ("__LITTLE_ENDIAN__");	\
      40      darwin_cpp_builtins (pfile);		\
      41    } while (0)
      42  
      43  #undef PTRDIFF_TYPE
      44  #define PTRDIFF_TYPE (TARGET_64BIT ? "long int" : "int")
      45  
      46  #undef WCHAR_TYPE
      47  #define WCHAR_TYPE "int"
      48  
      49  #undef WCHAR_TYPE_SIZE
      50  #define WCHAR_TYPE_SIZE 32
      51  
      52  /* Generate pic symbol indirection stubs if this is true.  */
      53  #undef TARGET_MACHO_SYMBOL_STUBS
      54  #define TARGET_MACHO_SYMBOL_STUBS (darwin_symbol_stubs)
      55  
      56  /* For compatibility with OSX system tools, use the new style of pic stub
      57     if this is set (default).  */
      58  #undef  MACHOPIC_ATT_STUB
      59  #define MACHOPIC_ATT_STUB (darwin_macho_att_stub)
      60  
      61  #undef MAX_BITS_PER_WORD
      62  #define MAX_BITS_PER_WORD 64
      63  
      64  #undef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
      65  #define FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN (0)
      66  
      67  #undef TARGET_KEEPS_VECTOR_ALIGNED_STACK
      68  #define TARGET_KEEPS_VECTOR_ALIGNED_STACK 1
      69  
      70  /* On Darwin, the stack is 128-bit aligned at the point of every call.
      71     Failure to ensure this will lead to a crash in the system libraries
      72     or dynamic loader.  */
      73  
      74  #undef MAIN_STACK_BOUNDARY
      75  #define MAIN_STACK_BOUNDARY 128
      76  
      77  /* Since we'll never want a stack boundary less aligned than 128 bits
      78     we need the extra work here otherwise bits of gcc get very grumpy
      79     when we ask for lower alignment.  We could just reject values less
      80     than 128 bits for Darwin, but it's easier to up the alignment if
      81     it's below the minimum.  */
      82  #undef PREFERRED_STACK_BOUNDARY
      83  #define PREFERRED_STACK_BOUNDARY \
      84    MAX (128, ix86_preferred_stack_boundary)
      85  
      86  /* We want -fPIC by default, unless we're using -static to compile for
      87     the kernel or some such.  */
      88  
      89  #undef CC1_SPEC
      90  #define CC1_SPEC "%(cc1_cpu) \
      91    %{!mkernel:%{!static:%{!mdynamic-no-pic:-fPIC}}} \
      92    %{g: %{!fno-eliminate-unused-debug-symbols: -feliminate-unused-debug-symbols }} \
      93    %{mx32:%eDarwin is not an mx32 platform} \
      94    %{mfentry*:%eDarwin does not support -mfentry or associated options}" \
      95    DARWIN_CC1_SPEC
      96  
      97  /* This is a workaround for a tool bug: see PR100340.  */
      98  
      99  #ifdef HAVE_AS_MLLVM_X86_PAD_FOR_ALIGN
     100  #define EXTRA_ASM_OPTS " -mllvm -x86-pad-for-align=false "
     101  #else
     102  #define EXTRA_ASM_OPTS ""
     103  #endif
     104  
     105  #undef ASM_SPEC
     106  #define ASM_SPEC \
     107  "%{static} -arch %(darwin_arch) " \
     108   ASM_OPTIONS ASM_MMACOSX_VERSION_MIN_SPEC EXTRA_ASM_OPTS \
     109  "%{!force_cpusubtype_ALL:-force_cpusubtype_ALL} "
     110  
     111  #undef ENDFILE_SPEC
     112  #define ENDFILE_SPEC \
     113    "%{mdaz-ftz:crtfastmath.o%s;Ofast|ffast-math|funsafe-math-optimizations:%{!shared:%{!mno-daz-ftz:crtfastmath.o%s}}} \
     114     %{mpc32:crtprec32.o%s} \
     115     %{mpc64:crtprec64.o%s} \
     116     %{mpc80:crtprec80.o%s}" TM_DESTRUCTOR
     117  
     118  #ifndef DARWIN_ARCH_SPEC
     119  /* We default to x86_64 for single-arch builds, bi-arch overrides.  */
     120  #define DARWIN_ARCH_SPEC "x86_64"
     121  #define DARWIN_SUBARCH_SPEC DARWIN_ARCH_SPEC
     122  #endif
     123  
     124  #undef SUBTARGET_EXTRA_SPECS
     125  #define SUBTARGET_EXTRA_SPECS                                   \
     126    DARWIN_EXTRA_SPECS                                            \
     127    { "darwin_arch", DARWIN_ARCH_SPEC },				\
     128    { "darwin_crt2", "" },                                        \
     129    { "darwin_subarch", DARWIN_ARCH_SPEC },
     130  
     131  /* The Darwin assembler mostly follows AT&T syntax.  */
     132  #undef ASSEMBLER_DIALECT
     133  #define ASSEMBLER_DIALECT ASM_ATT
     134  
     135  /* Define macro used to output shift-double opcodes when the shift
     136     count is in %cl.  Some assemblers require %cl as an argument;
     137     some don't.  This macro controls what to do: by default, don't
     138     print %cl.  */
     139  
     140  #define SHIFT_DOUBLE_OMITS_COUNT 0
     141  
     142  #undef TARGET_ASM_FILE_END
     143  #define TARGET_ASM_FILE_END darwin_file_end
     144  
     145  /* Define the syntax of pseudo-ops, labels and comments.  */
     146  
     147  /* String containing the assembler's comment-starter.  */
     148  
     149  #define ASM_COMMENT_START "#"
     150  
     151  /* By default, target has a 80387, uses IEEE compatible arithmetic,
     152     and returns float values in the 387.  */
     153  
     154  #undef TARGET_SUBTARGET_DEFAULT
     155  #define TARGET_SUBTARGET_DEFAULT \
     156    (MASK_80387 | MASK_IEEE_FP | MASK_FLOAT_RETURNS | MASK_128BIT_LONG_DOUBLE)
     157  
     158  /* For darwin we want to target specific processor features as a minimum,
     159     but these unfortunately don't correspond to a specific processor.  */
     160  #undef TARGET_SUBTARGET32_ISA_DEFAULT
     161  #define TARGET_SUBTARGET32_ISA_DEFAULT			\
     162    (OPTION_MASK_ISA_MMX | OPTION_MASK_ISA_SSE		\
     163     | OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE3)
     164  
     165  #undef TARGET_SUBTARGET64_ISA_DEFAULT
     166  #define TARGET_SUBTARGET64_ISA_DEFAULT TARGET_SUBTARGET32_ISA_DEFAULT
     167  
     168  #undef GOT_SYMBOL_NAME
     169  #define GOT_SYMBOL_NAME MACHOPIC_FUNCTION_BASE_NAME
     170  
     171  /* Define the syntax of pseudo-ops, labels and comments.  */
     172  
     173  #define LPREFIX "L"
     174  
     175  /* Assembler pseudos to introduce constants of various size.  */
     176  
     177  #define ASM_BYTE "\t.byte\t"
     178  #define ASM_SHORT "\t.word\t"
     179  #define ASM_LONG "\t.long\t"
     180  #define ASM_QUAD "\t.quad\t"
     181  
     182  #define SUBTARGET_ENCODE_SECTION_INFO  darwin_encode_section_info
     183  
     184  #undef ASM_OUTPUT_ALIGN
     185  #define ASM_OUTPUT_ALIGN(FILE,LOG)				   \
     186    do {								   \
     187      if ((LOG) != 0)						   \
     188        {								   \
     189  	if (in_section == text_section)				   \
     190  	  fprintf (FILE, "\t%s %d,0x90\n", ALIGN_ASM_OP, (LOG));   \
     191  	else							   \
     192  	  fprintf (FILE, "\t%s %d\n", ALIGN_ASM_OP, (LOG));	   \
     193        }								   \
     194    } while (0)
     195  
     196  /* Darwin x86 assemblers support the .ident directive.  */
     197  
     198  #undef TARGET_ASM_OUTPUT_IDENT
     199  #define TARGET_ASM_OUTPUT_IDENT default_asm_output_ident_directive
     200  
     201  /* We always want jump tables in the text section:
     202     * for PIC code, we need the subtracted symbol to be defined at
     203       assembly-time.
     204     * for mdynamic-no-pic, we cannot support jump tables in the .const
     205       section for weak functions, this looks to ld64 like direct access
     206       to the weak symbol from an anonymous atom.  */
     207  
     208  #undef JUMP_TABLES_IN_TEXT_SECTION
     209  #define JUMP_TABLES_IN_TEXT_SECTION 1
     210  
     211  /* Darwin profiling -- call mcount.
     212     If we need a stub, then we unconditionally mark it as used.  */
     213  #undef FUNCTION_PROFILER
     214  #define FUNCTION_PROFILER(FILE, LABELNO)				\
     215    do {									\
     216      if (TARGET_MACHO_SYMBOL_STUBS 					\
     217  	&& MACHOPIC_INDIRECT && !TARGET_64BIT)				\
     218        {									\
     219  	const char *name = machopic_mcount_stub_name ();		\
     220  	fprintf (FILE, "\tcall %s\n", name+1);  /*  skip '&'  */	\
     221        }									\
     222      else fprintf (FILE, "\tcall mcount\n");				\
     223    } while (0)
     224  
     225  #define C_COMMON_OVERRIDE_OPTIONS					\
     226    do {									\
     227      SUBTARGET_C_COMMON_OVERRIDE_OPTIONS;				\
     228    } while (0)
     229  
     230  #undef SUBTARGET_OVERRIDE_OPTIONS
     231  #define SUBTARGET_OVERRIDE_OPTIONS					\
     232    do {									\
     233      if (TARGET_64BIT && MACHO_DYNAMIC_NO_PIC_P)				\
     234        target_flags &= ~MASK_MACHO_DYNAMIC_NO_PIC;			\
     235    } while (0)
     236  
     237  /* Darwin uses the standard DWARF register numbers but the default
     238     register numbers for STABS.  Fortunately for 64-bit code the
     239     default and the standard are the same.  */
     240  #undef DEBUGGER_REGNO
     241  #define DEBUGGER_REGNO(n) 					\
     242    (TARGET_64BIT ? debugger64_register_map[n]				\
     243     : dwarf_debuginfo_p () ? svr4_debugger_register_map[n]		\
     244     : debugger_register_map[n])
     245  
     246  /* Unfortunately, the 32-bit EH information also doesn't use the standard
     247     DWARF register numbers.  */
     248  #define DWARF2_FRAME_REG_OUT(n, for_eh)					\
     249    (! (for_eh) || !dwarf_debuginfo_p () || TARGET_64BIT ? (n)	\
     250     : (n) == 5 ? 4							\
     251     : (n) == 4 ? 5							\
     252     : (n) >= 11 && (n) <= 18 ? (n) + 1					\
     253     : (n))
     254  
     255  #undef REGISTER_SUBTARGET_PRAGMAS
     256  #define REGISTER_SUBTARGET_PRAGMAS() DARWIN_REGISTER_TARGET_PRAGMAS()
     257  
     258  #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
     259  #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES darwin_set_default_type_attributes
     260  
     261  /* For 64-bit, we need to add 4 because @GOTPCREL is relative to the
     262     end of the instruction, but without the 4 we'd only have the right
     263     address for the start of the instruction.  */
     264  #undef ASM_MAYBE_OUTPUT_ENCODED_ADDR_RTX
     265  #define ASM_MAYBE_OUTPUT_ENCODED_ADDR_RTX(FILE, ENCODING, SIZE, ADDR, DONE) \
     266    if (TARGET_64BIT)							\
     267      {									\
     268        if ((SIZE) == 4 && ((ENCODING) & 0x70) == DW_EH_PE_pcrel)		\
     269  	{								\
     270  	  fputs (ASM_LONG, FILE);					\
     271  	  assemble_name (FILE, XSTR (ADDR, 0));				\
     272  	  fputs ("+4@GOTPCREL", FILE);					\
     273  	  goto DONE;							\
     274  	}								\
     275      }									\
     276    else									\
     277      {									\
     278        if (ENCODING == ASM_PREFERRED_EH_DATA_FORMAT (2, 1))		\
     279          {								\
     280            darwin_non_lazy_pcrel (FILE, ADDR);				\
     281            goto DONE;							\
     282          }								\
     283      }
     284  
     285  /* First available SYMBOL flag bit for use by subtargets.  */
     286  #define SYMBOL_FLAG_SUBT_DEP (SYMBOL_FLAG_MACH_DEP << 5)
     287  
     288  #undef MACHOPIC_NL_SYMBOL_PTR_SECTION
     289  #define MACHOPIC_NL_SYMBOL_PTR_SECTION \
     290  		".section __IMPORT,__pointers,non_lazy_symbol_pointers"
     291  
     292  #define SUBTARGET32_DEFAULT_CPU "i686"
     293  
     294  #undef  SUBTARGET_INIT_BUILTINS
     295  #define SUBTARGET_INIT_BUILTINS						\
     296    do {									\
     297      ix86_builtins[(int) IX86_BUILTIN_CFSTRING]				\
     298        = darwin_init_cfstring_builtins ((unsigned) (IX86_BUILTIN_CFSTRING)); \
     299      darwin_rename_builtins ();						\
     300    } while(0)
     301  
     302  /* Define the shadow offset for asan.  */
     303  #undef SUBTARGET_SHADOW_OFFSET
     304  #define SUBTARGET_SHADOW_OFFSET	\
     305    (TARGET_LP64 ? HOST_WIDE_INT_1 << 44 : HOST_WIDE_INT_1 << 29)
     306  
     307  #undef CLEAR_INSN_CACHE
     308  #define CLEAR_INSN_CACHE(beg, end)				\
     309    extern void sys_icache_invalidate(void *start, size_t len);	\
     310    sys_icache_invalidate ((beg), (size_t)((end)-(beg)))