1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-14 17:32:00 +03:00
juvix/app/Commands/Dev/Asm/Compile.hs

53 lines
2.1 KiB
Haskell
Raw Normal View History

2022-12-06 13:33:20 +03:00
module Commands.Dev.Asm.Compile where
import Commands.Base
import Commands.Dev.Asm.Compile.Options
import Commands.Extra.Compile qualified as Compile
import Data.Text.IO qualified as TIO
import Juvix.Compiler.Asm.Translation.FromSource qualified as Asm
import Juvix.Compiler.Backend qualified as Backend
import Juvix.Compiler.Backend.C qualified as C
runCommand :: forall r. (Members '[Embed IO, App] r) => AsmCompileOptions -> Sem r ()
2022-12-06 13:33:20 +03:00
runCommand opts = do
2022-12-20 15:05:40 +03:00
file <- getFile
ep <- getEntryPoint (AppPath (preFileFromAbs file) True)
tgt <- getTarget (opts ^. compileTarget)
let entryPoint :: EntryPoint
entryPoint =
ep
{ _entryPointTarget = tgt,
_entryPointDebug = opts ^. compileDebug
}
2022-12-20 15:05:40 +03:00
s <- embed (readFile (toFilePath file))
case Asm.runParser (toFilePath file) s of
2022-12-06 13:33:20 +03:00
Left err -> exitJuvixError (JuvixError err)
Right tab -> do
case run $ runReader entryPoint $ runError $ asmToMiniC tab of
Left err -> exitJuvixError err
Right C.MiniCResult {..} -> do
buildDir <- askBuildDir
ensureDir buildDir
cFile <- inputCFile file
embed $ TIO.writeFile (toFilePath cFile) _resultCCode
Compile.runCommand opts {_compileInputFile = AppPath (preFileFromAbs cFile) False}
2022-12-06 13:33:20 +03:00
where
2022-12-20 15:05:40 +03:00
getFile :: Sem r (Path Abs File)
getFile = fromAppPathFile (opts ^. compileInputFile)
2022-12-06 13:33:20 +03:00
getTarget :: CompileTarget -> Sem r Backend.Target
getTarget = \case
TargetWasm32Wasi -> return Backend.TargetCWasm32Wasi
TargetNative64 -> return Backend.TargetCNative64
TargetGeb -> exitMsg (ExitFailure 1) "error: GEB target not supported for JuvixAsm"
TargetCore -> exitMsg (ExitFailure 1) "error: JuvixCore target not supported for JuvixAsm"
TargetAsm -> exitMsg (ExitFailure 1) "error: JuvixAsm target not supported for JuvixAsm"
2022-12-06 13:33:20 +03:00
inputCFile :: (Members '[App] r) => Path Abs File -> Sem r (Path Abs File)
2022-12-20 15:05:40 +03:00
inputCFile inputFileCompile = do
buildDir <- askBuildDir
return (buildDir <//> outputMiniCFile)
2022-12-06 13:33:20 +03:00
where
2022-12-20 15:05:40 +03:00
outputMiniCFile :: Path Rel File
outputMiniCFile = replaceExtension' ".c" (filename inputFileCompile)