summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain Henry <sylvain@haskus.fr>2019-01-15 13:14:45 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2019-01-20 21:35:20 -0500
commitf035504b573b9684d53a6d035f8726672c1b0190 (patch)
tree5cc4037684d8967aa7b4808130cd9514d90a4501
parent6e7aa5e937b18a45ee39c8c67b7a2f7350b89a66 (diff)
downloadhaskell-f035504b573b9684d53a6d035f8726672c1b0190.tar.gz
Add support for ASM foreign files (.s) in TH (#16180)
-rw-r--r--compiler/main/DriverPipeline.hs9
-rw-r--r--libraries/ghc-boot-th/GHC/ForeignSrcLang/Type.hs8
-rw-r--r--libraries/template-haskell/Language/Haskell/TH/Syntax.hs9
-rw-r--r--testsuite/tests/th/T16180.hs25
-rw-r--r--testsuite/tests/th/T16180.stdout2
-rw-r--r--testsuite/tests/th/all.T1
6 files changed, 45 insertions, 9 deletions
diff --git a/compiler/main/DriverPipeline.hs b/compiler/main/DriverPipeline.hs
index f1a5cb46e0..5fe2362973 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 3106141de7..c40f6f91df 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 705222a2f9..3ff6393794 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 0000000000..2a4b80c976
--- /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/th/T16180.stdout b/testsuite/tests/th/T16180.stdout
new file mode 100644
index 0000000000..a378710ebf
--- /dev/null
+++ b/testsuite/tests/th/T16180.stdout
@@ -0,0 +1,2 @@
+Hello world
+
diff --git a/testsuite/tests/th/all.T b/testsuite/tests/th/all.T
index 6783bb6c78..9ddf28345e 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, [''])