[commit: ghc] wip/ghc-8.8-merges: Add support for ASM foreign files (.s) in TH (#16180) (b3ee7e3)
git at git.haskell.org
git at git.haskell.org
Thu Feb 21 15:09:44 UTC 2019
Repository : ssh://git@git.haskell.org/ghc
On branch : wip/ghc-8.8-merges
Link : http://ghc.haskell.org/trac/ghc/changeset/b3ee7e330f7a98de36130f356bf32b95bc28859c/ghc
>---------------------------------------------------------------
commit b3ee7e330f7a98de36130f356bf32b95bc28859c
Author: Sylvain Henry <sylvain at haskus.fr>
Date: Tue Jan 15 13:14:45 2019 +0100
Add support for ASM foreign files (.s) in TH (#16180)
>---------------------------------------------------------------
b3ee7e330f7a98de36130f356bf32b95bc28859c
compiler/main/DriverPipeline.hs | 9 ++++----
libraries/ghc-boot-th/GHC/ForeignSrcLang/Type.hs | 8 ++++++-
.../template-haskell/Language/Haskell/TH/Syntax.hs | 9 ++++----
testsuite/tests/th/T16180.hs | 25 ++++++++++++++++++++++
.../objc/objcpp-hi.stdout => th/T16180.stdout} | 1 +
testsuite/tests/th/all.T | 1 +
6 files changed, 44 insertions(+), 9 deletions(-)
diff --git a/compiler/main/DriverPipeline.hs b/compiler/main/DriverPipeline.hs
index f1a5cb4..5fe2362 100644
--- a/compiler/main/DriverPipeline.hs
+++ b/compiler/main/DriverPipeline.hs
@@ -306,11 +306,12 @@ compileForeign :: HscEnv -> ForeignSrcLang -> FilePath -> IO FilePath
compileForeign _ RawObject object_file = return object_file
compileForeign hsc_env lang stub_c = do
let phase = case lang of
- LangC -> Cc
- LangCxx -> Ccxx
- LangObjc -> Cobjc
+ LangC -> Cc
+ LangCxx -> Ccxx
+ LangObjc -> Cobjc
LangObjcxx -> Cobjcxx
- RawObject -> panic "compileForeign: should be unreachable"
+ LangAsm -> As True -- allow CPP
+ RawObject -> panic "compileForeign: should be unreachable"
(_, stub_o) <- runPipeline StopLn hsc_env
(stub_c, Just (RealPhase phase))
Nothing (Temporary TFL_GhcSession)
diff --git a/libraries/ghc-boot-th/GHC/ForeignSrcLang/Type.hs b/libraries/ghc-boot-th/GHC/ForeignSrcLang/Type.hs
index 3106141..c40f6f9 100644
--- a/libraries/ghc-boot-th/GHC/ForeignSrcLang/Type.hs
+++ b/libraries/ghc-boot-th/GHC/ForeignSrcLang/Type.hs
@@ -6,6 +6,12 @@ module GHC.ForeignSrcLang.Type
import Prelude -- See note [Why do we import Prelude here?]
import GHC.Generics (Generic)
+-- | Foreign formats supported by GHC via TH
data ForeignSrcLang
- = LangC | LangCxx | LangObjc | LangObjcxx | RawObject
+ = LangC -- ^ C
+ | LangCxx -- ^ C++
+ | LangObjc -- ^ Objective C
+ | LangObjcxx -- ^ Objective C++
+ | LangAsm -- ^ Assembly language (.s)
+ | RawObject -- ^ Object (.o)
deriving (Eq, Show, Generic)
diff --git a/libraries/template-haskell/Language/Haskell/TH/Syntax.hs b/libraries/template-haskell/Language/Haskell/TH/Syntax.hs
index 705222a..3ff6393 100644
--- a/libraries/template-haskell/Language/Haskell/TH/Syntax.hs
+++ b/libraries/template-haskell/Language/Haskell/TH/Syntax.hs
@@ -503,11 +503,12 @@ addForeignFile = addForeignSource
addForeignSource :: ForeignSrcLang -> String -> Q ()
addForeignSource lang src = do
let suffix = case lang of
- LangC -> "c"
- LangCxx -> "cpp"
- LangObjc -> "m"
+ LangC -> "c"
+ LangCxx -> "cpp"
+ LangObjc -> "m"
LangObjcxx -> "mm"
- RawObject -> "a"
+ LangAsm -> "s"
+ RawObject -> "a"
path <- addTempFile suffix
runIO $ writeFile path src
addForeignFilePath lang path
diff --git a/testsuite/tests/th/T16180.hs b/testsuite/tests/th/T16180.hs
new file mode 100644
index 0000000..2a4b80c
--- /dev/null
+++ b/testsuite/tests/th/T16180.hs
@@ -0,0 +1,25 @@
+{-# LANGUAGE TemplateHaskell #-}
+
+module Main where
+
+import Language.Haskell.TH.Syntax
+import Foreign.C.String
+
+$(do
+ -- some architectures require a "_" symbol prefix...
+ -- GHC defines a LEADING_UNDERSCORE CPP constant to indicate this.
+ addForeignSource LangAsm
+ "#if defined(LEADING_UNDERSCORE)\n\
+ \.global \"_mydata\"\n\
+ \_mydata:\n\
+ \#else\n\
+ \.global \"mydata\"\n\
+ \mydata:\n\
+ \#endif\n\
+ \.ascii \"Hello world\\0\"\n"
+ return [])
+
+foreign import ccall "&mydata" mystring :: CString
+
+main :: IO ()
+main = putStrLn =<< peekCString mystring
diff --git a/testsuite/tests/driver/objc/objcpp-hi.stdout b/testsuite/tests/th/T16180.stdout
similarity index 92%
copy from testsuite/tests/driver/objc/objcpp-hi.stdout
copy to testsuite/tests/th/T16180.stdout
index 802992c..a378710 100644
--- a/testsuite/tests/driver/objc/objcpp-hi.stdout
+++ b/testsuite/tests/th/T16180.stdout
@@ -1 +1,2 @@
Hello world
+
diff --git a/testsuite/tests/th/all.T b/testsuite/tests/th/all.T
index 6783bb6..9ddf283 100644
--- a/testsuite/tests/th/all.T
+++ b/testsuite/tests/th/all.T
@@ -467,3 +467,4 @@ test('T15437', expect_broken(15437), multimod_compile,
test('T15985', normal, compile, [''])
test('T16133', normal, compile_fail, [''])
test('T15471', normal, multimod_compile, ['T15471.hs', '-v0'])
+test('T16180', normal, compile_and_run, [''])
More information about the ghc-commits
mailing list