Add support for ASM foreign files (.s) in TH (#16180)
authorSylvain Henry <sylvain@haskus.fr>
Tue, 15 Jan 2019 12:14:45 +0000 (13:14 +0100)
committerMarge Bot <ben+marge-bot@smart-cactus.org>
Mon, 21 Jan 2019 02:35:20 +0000 (21:35 -0500)
compiler/main/DriverPipeline.hs
libraries/ghc-boot-th/GHC/ForeignSrcLang/Type.hs
libraries/template-haskell/Language/Haskell/TH/Syntax.hs
testsuite/tests/th/T16180.hs [new file with mode: 0644]
testsuite/tests/th/T16180.stdout [new file with mode: 0644]
testsuite/tests/th/all.T

index f1a5cb4..5fe2362 100644 (file)
@@ -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)
index 3106141..c40f6f9 100644 (file)
@@ -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)
index 705222a..3ff6393 100644 (file)
@@ -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 (file)
index 0000000..2a4b80c
--- /dev/null
@@ -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 (file)
index 0000000..a378710
--- /dev/null
@@ -0,0 +1,2 @@
+Hello world
+
index 6783bb6..9ddf283 100644 (file)
@@ -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, [''])