-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the
juvix dev tree compile
command (#2590)
* Adds the `juvix dev tree compile` CLI command. * Depends on #2589 * Depends on #2587 * Depends on #2583
- Loading branch information
Showing
6 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Commands.Dev.Tree.Compile where | ||
|
||
import Commands.Base | ||
import Commands.Dev.Tree.Compile.Base | ||
import Commands.Dev.Tree.Compile.Options | ||
import Juvix.Compiler.Tree.Translation.FromSource qualified as Tree | ||
|
||
runCommand :: forall r. (Members '[Embed IO, App, TaggedLock] r) => CompileOptions -> Sem r () | ||
runCommand opts = do | ||
file <- getFile | ||
s <- readFile (toFilePath file) | ||
tab <- getRight (mapLeft JuvixError (Tree.runParser (toFilePath file) s)) | ||
let arg = PipelineArg opts file tab | ||
case opts ^. compileTarget of | ||
TargetWasm32Wasi -> runCPipeline arg | ||
TargetNative64 -> runCPipeline arg | ||
TargetGeb -> return () | ||
TargetVampIR -> return () | ||
TargetCore -> return () | ||
TargetAsm -> runAsmPipeline arg | ||
TargetTree -> return () | ||
TargetNockma -> runNockmaPipeline arg | ||
where | ||
getFile :: Sem r (Path Abs File) | ||
getFile = getMainFile (opts ^. compileInputFile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
module Commands.Dev.Tree.Compile.Base where | ||
|
||
import Commands.Base | ||
import Commands.Dev.Tree.Compile.Options | ||
import Commands.Extra.Compile qualified as Compile | ||
import Juvix.Compiler.Asm.Pretty qualified as Asm | ||
import Juvix.Compiler.Backend qualified as Backend | ||
import Juvix.Compiler.Backend.C qualified as C | ||
import Juvix.Compiler.Nockma.Pretty qualified as Nockma | ||
import Juvix.Compiler.Tree.Data.InfoTable qualified as Tree | ||
|
||
data PipelineArg = PipelineArg | ||
{ _pipelineArgOptions :: CompileOptions, | ||
_pipelineArgFile :: Path Abs File, | ||
_pipelineArgTable :: Tree.InfoTable | ||
} | ||
|
||
getEntry :: (Members '[Embed IO, App, TaggedLock] r) => PipelineArg -> Sem r EntryPoint | ||
getEntry PipelineArg {..} = do | ||
ep <- getEntryPoint (AppPath (preFileFromAbs _pipelineArgFile) True) | ||
return $ | ||
ep | ||
{ _entryPointTarget = getTarget (_pipelineArgOptions ^. compileTarget), | ||
_entryPointDebug = _pipelineArgOptions ^. compileDebug, | ||
_entryPointUnsafe = _pipelineArgOptions ^. compileUnsafe, | ||
_entryPointOptimizationLevel = fromMaybe defaultOptLevel (_pipelineArgOptions ^. compileOptimizationLevel), | ||
_entryPointInliningDepth = _pipelineArgOptions ^. compileInliningDepth | ||
} | ||
where | ||
getTarget :: CompileTarget -> Backend.Target | ||
getTarget = \case | ||
TargetWasm32Wasi -> Backend.TargetCWasm32Wasi | ||
TargetNative64 -> Backend.TargetCNative64 | ||
TargetGeb -> Backend.TargetGeb | ||
TargetVampIR -> Backend.TargetVampIR | ||
TargetCore -> Backend.TargetCore | ||
TargetAsm -> Backend.TargetAsm | ||
TargetTree -> Backend.TargetTree | ||
TargetNockma -> Backend.TargetNockma | ||
|
||
defaultOptLevel :: Int | ||
defaultOptLevel | ||
| _pipelineArgOptions ^. compileDebug = 0 | ||
| otherwise = defaultOptimizationLevel | ||
|
||
runCPipeline :: | ||
forall r. | ||
(Members '[Embed IO, App, TaggedLock] r) => | ||
PipelineArg -> | ||
Sem r () | ||
runCPipeline pa@PipelineArg {..} = do | ||
entryPoint <- getEntry pa | ||
C.MiniCResult {..} <- | ||
getRight | ||
. run | ||
. runReader entryPoint | ||
. runError @JuvixError | ||
$ treeToMiniC _pipelineArgTable | ||
cFile <- inputCFile _pipelineArgFile | ||
embed @IO (writeFile (toFilePath cFile) _resultCCode) | ||
outfile <- Compile.outputFile _pipelineArgOptions _pipelineArgFile | ||
Compile.runCommand | ||
_pipelineArgOptions | ||
{ _compileInputFile = Just (AppPath (preFileFromAbs cFile) False), | ||
_compileOutputFile = Just (AppPath (preFileFromAbs outfile) False) | ||
} | ||
where | ||
inputCFile :: Path Abs File -> Sem r (Path Abs File) | ||
inputCFile inputFileCompile = do | ||
buildDir <- askBuildDir | ||
ensureDir buildDir | ||
return (buildDir <//> replaceExtension' ".c" (filename inputFileCompile)) | ||
|
||
runAsmPipeline :: (Members '[Embed IO, App, TaggedLock] r) => PipelineArg -> Sem r () | ||
runAsmPipeline pa@PipelineArg {..} = do | ||
entryPoint <- getEntry pa | ||
asmFile <- Compile.outputFile _pipelineArgOptions _pipelineArgFile | ||
r <- | ||
runReader entryPoint | ||
. runError @JuvixError | ||
. treeToAsm | ||
$ _pipelineArgTable | ||
tab' <- getRight r | ||
let code = Asm.ppPrint tab' tab' | ||
embed @IO (writeFile (toFilePath asmFile) code) | ||
|
||
runNockmaPipeline :: (Members '[Embed IO, App, TaggedLock] r) => PipelineArg -> Sem r () | ||
runNockmaPipeline pa@PipelineArg {..} = do | ||
entryPoint <- getEntry pa | ||
nockmaFile <- Compile.outputFile _pipelineArgOptions _pipelineArgFile | ||
r <- | ||
runReader entryPoint | ||
. runError @JuvixError | ||
. treeToNockma | ||
$ _pipelineArgTable | ||
tab' <- getRight r | ||
let code = Nockma.ppSerialize tab' | ||
embed @IO (writeFile (toFilePath nockmaFile) code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Commands.Dev.Tree.Compile.Options | ||
( module Commands.Dev.Tree.Compile.Options, | ||
module Commands.Extra.Compile.Options, | ||
) | ||
where | ||
|
||
import Commands.Extra.Compile.Options | ||
import CommonOptions | ||
|
||
treeSupportedTargets :: NonEmpty CompileTarget | ||
treeSupportedTargets = | ||
nonEmpty' | ||
[ TargetWasm32Wasi, | ||
TargetNative64, | ||
TargetAsm | ||
] | ||
|
||
parseTreeCompileOptions :: Parser CompileOptions | ||
parseTreeCompileOptions = | ||
parseCompileOptions | ||
treeSupportedTargets | ||
(parseInputFile FileExtJuvixTree) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters