From ffdbb302967a147550113251e47b9d98a7affdc0 Mon Sep 17 00:00:00 2001
From: "wolfgang.thaller@gmx.net" <unknown>
Date: Mon, 27 Nov 2006 13:06:02 +0000
Subject: [PATCH] Initial support for x86_64-darwin

Basic -fvia-C code generation is there, not much testing.
---
 aclocal.m4                  |  4 ++++
 configure.ac                |  9 +++++++++
 distrib/configure-bin.ac    |  2 ++
 driver/mangler/ghc-asm.lprl | 40 +++++++++++++++++++++++++++++++++++++
 rts/StgCRun.c               | 12 ++++++++---
 5 files changed, 64 insertions(+), 3 deletions(-)

diff --git a/aclocal.m4 b/aclocal.m4
index 9d1e1cab9b02..86fdeaf76f3d 100644
--- a/aclocal.m4
+++ b/aclocal.m4
@@ -168,6 +168,10 @@ case $HostPlatform in
 alpha-dec-osf*) fptools_cv_leading_underscore=no;;
 *cygwin32) fptools_cv_leading_underscore=yes;;
 *mingw32) fptools_cv_leading_underscore=yes;;
+
+    # HACK: Apple doesn't seem to provide nlist in the 64-bit-libraries
+x86_64-apple-darwin*) fptools_cv_leading_underscore=yes;;
+
 *) AC_RUN_IFELSE([AC_LANG_SOURCE([[#ifdef HAVE_NLIST_H
 #include <nlist.h>
 struct nlist xYzzY1[] = {{"xYzzY1", 0},{0}};
diff --git a/configure.ac b/configure.ac
index 7f977cd65430..8b00e5af0200 100644
--- a/configure.ac
+++ b/configure.ac
@@ -317,6 +317,15 @@ x86_64-*-linux*)
         HostVendor_CPP='unknown'
         HostOS_CPP='linux'
         ;;
+x86_64-apple-darwin*)
+        HostPlatform=x86_64-apple-darwin
+        TargetPlatform=x86_64-apple-darwin
+        BuildPlatform=x86_64-apple-darwin
+        HostPlatform_CPP='x86_64_apple_darwin'
+        HostArch_CPP='x86_64'
+        HostVendor_CPP='apple'
+        HostOS_CPP='darwin'
+        ;;
 m68k-*-linux*)
         HostPlatform=m68k-unknown-linux # hack again
         TargetPlatform=m68k-unknown-linux
diff --git a/distrib/configure-bin.ac b/distrib/configure-bin.ac
index a852ec9ba91f..cda2cffecf7c 100644
--- a/distrib/configure-bin.ac
+++ b/distrib/configure-bin.ac
@@ -96,6 +96,8 @@ x86_64-*-openbsd*)
 	TargetPlatform=x86_64-unknown-openbsd;;
 x86_64-*-netbsd*)
 	TargetPlatform=x86_64-unknown-netbsd;;
+x86_64-apple-darwin*)
+	TargetPlatform=x86_64-apple-darwin;;
 *)
         echo "Unrecognised platform: $TargetPlatform"
         exit 1
diff --git a/driver/mangler/ghc-asm.lprl b/driver/mangler/ghc-asm.lprl
index ab43cdcd7d34..90b49751b60e 100644
--- a/driver/mangler/ghc-asm.lprl
+++ b/driver/mangler/ghc-asm.lprl
@@ -343,6 +343,32 @@ sub init_TARGET_STUFF {
     $T_HDR_entry    = "\t\.text\n\t\.align 2\n";
     $T_HDR_vector   = "\t\.text\n\t\.align 2\n";
 
+    #--------------------------------------------------------#
+    } elsif ( $TargetPlatform =~ /^x86_64-apple-darwin.*/ ) {
+				# Apple PowerPC Darwin/MacOS X.
+    $T_STABBY	    = 0; # 1 iff .stab things (usually if a.out format)
+    $T_US	    = '_'; # _ if symbols have an underscore on the front
+    $T_PRE_APP	    = 'DOESNT APPLY'; # regexp that says what comes before APP/NO_APP
+    $T_CONST_LBL    = '^\LC\d+:'; # regexp for what such a lbl looks like
+    $T_POST_LBL	    = ':';
+
+    $T_MOVE_DIRVS   = '^(\s*(\.align \d+|\.text|\.data|\.const_data|\.cstring|\.non_lazy_symbol_pointer|\.const|\.static_const|\.literal4|\.literal8|\.static_data|\.globl \S+|\.section .*|\.lcomm.*)\n)';
+    $T_COPY_DIRVS   = '\.(globl|lcomm)';
+
+    $T_DOT_WORD	    = '\.(quad|long|short|byte|fill|space)';
+    $T_DOT_GLOBAL   = '\.globl';
+    $T_HDR_toc      = "\.toc\n";
+    $T_HDR_literal16= "\t\.literal8\n\t\.align 4\n";
+    $T_HDR_literal  = "\t\.const\n\t\.align 4\n";
+    $T_HDR_misc	    = "\t\.text\n\t\.align 2\n";
+    $T_HDR_data	    = "\t\.data\n\t\.align 2\n";
+    $T_HDR_rodata   = "\t\.const\n\t\.align 2\n";
+    $T_HDR_relrodata= "\t\.const_data\n\t\.align 2\n";
+    $T_HDR_closure  = "\t\.data\n\t\.align 2\n";
+    $T_HDR_info	    = "\t\.text\n\t\.align 2\n";
+    $T_HDR_entry    = "\t\.text\n\t\.align 2\n";
+    $T_HDR_vector   = "\t\.text\n\t\.align 2\n";
+
     #--------------------------------------------------------#
     } elsif ( $TargetPlatform =~ /^powerpc-.*-linux/ ) {
 				# PowerPC Linux
@@ -1730,6 +1756,20 @@ sub rev_tbl {
 	}
     }
 
+    if ( $TargetPlatform =~ /x86_64-apple-darwin/ ) {
+        # Tack a label to the front of the info table, too.
+        # For now, this just serves to work around a crash in Apple's new
+        # 64-bit linker (it seems to assume that there is no data before the
+        # first label in a section).
+        
+        # The plan for the future is to do this on all Darwin platforms, and
+        # to add a reference to this label after the entry code, just as the
+        # NCG does, so we can enable dead-code-stripping in the linker without
+        # losing our info tables. (Hence the name _dsp, for dead-strip preventer)
+        
+        $before .= "\n${infoname}_dsp:\n";    
+    }
+
     $tbl = $before
 	 . (($TargetPlatform !~ /^hppa/) ? '' : join("\n", @imports) . "\n")
 	 . join("\n", @words) . "\n"
diff --git a/rts/StgCRun.c b/rts/StgCRun.c
index 9d80bdbf6414..51388510c633 100644
--- a/rts/StgCRun.c
+++ b/rts/StgCRun.c
@@ -106,6 +106,12 @@ StgFunPtr StgReturn(void)
 
 #else /* !USE_MINIINTERPRETER */
 
+#ifdef LEADING_UNDERSCORE
+#define STG_RUN "_StgRun"
+#else
+#define STG_RUN "StgRun"
+#endif
+
 #ifdef LEADING_UNDERSCORE
 #define STG_RETURN "_StgReturn"
 #else
@@ -207,8 +213,8 @@ StgRunIsImplementedInAssembler(void)
 	/*
 	 * save callee-saves registers on behalf of the STG code.
 	 */
-	".globl StgRun\n"
-	"StgRun:\n\t"
+	".globl " STG_RUN "\n"
+	STG_RUN ":\n\t"
 	"subq %0, %%rsp\n\t"
 	"movq %%rsp, %%rax\n\t"
 	"addq %0-48, %%rax\n\t"
@@ -228,7 +234,7 @@ StgRunIsImplementedInAssembler(void)
         "movq %%rdi,%%rax\n\t"
         "jmp *%%rax\n\t"
 
-	".global " STG_RETURN "\n"
+	".globl " STG_RETURN "\n"
        	STG_RETURN ":\n\t"
 
 	"movq %%rbx, %%rax\n\t"   /* Return value in R1  */
-- 
GitLab