-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support compilation of Anoma transactions in nockma backend (#2693)
When we first implemented the Nockma backend we wrongly assumed that the only entry point for Juvix compiled Nockma modules would be the main function. Using this assumption we could add a setup step in the main function that put the Anoma stdlib and compiled functions from the Juvix module in a static place in the Nockma subject. References to the Anoma stdlib and functions in the module could then be resolved statically. However, one of the use cases for Juvix -> Nockma compilation is for Anoma to run logic functions that are fields of a transaction. So the user writes a Juvix program with main function that returns a transaction. The result of the main function is passed to Anoma. When Anoma calls the logic function on a field of the transaction, the setup part of the main function is not run so the subject is not in the required state. In fact, the logic function is not even callable by Anoma because non-main functions in the Juvix module use a calling convention that assumes the subject has a particular shape. This PR solves the problem by making all functions in the Juvix module use the Anoma calling convention. We make all compiled closures (including, for example, the logic functions stored on resources in a transaction) self contained, i.e they contain the functions library and anoma standard library. Modules that contain many closures produce large nockma output files which slows down the evaluator. This will need to be fixed in the future either with Nockma compression ([jam serialization](https://developers.urbit.org/reference/hoon/stdlib/2p)) or otherwise. But it does not block the compilation and execution of Anoma transactions. Other fixes / additions: * Extra tracing. You can now annotate output cells with a tag that will be displayed in the output * Unittests for listToTuple, appendRights helper functions * Fixes for the nockma parser when parsing 'pretty nockma', specifically stdlib calls, tags and functions_library atom. * Adds `juvix dev nock run` command that can run a program output with the `anoma` target. * Remove the `nockma` target. As described above we always use the Anoma calling convention so there's no need for a separate target for the 'juvix calling convention' * Adds a `--profile` flag to `juvix dev nock run` which outputs a count of Nockma ops used in the evaluation * In tests we no longer serialise the compiled program to force full evaluation of the compiled code. We added a negative test to check that strings are not allowed in Nockma/Anoma programs, it is output in a file `OUTPUT.profile` and has the following form: ``` quote : 15077 apply : 0 isCell : 0 suc : 0 = : 4517 if : 5086 seq : 5086 push : 0 call : 4896 replace : 1 hint : 8 scry : 0 trace : 0 ``` --------- Co-authored-by: Jan Mas Rovira <[email protected]>
- Loading branch information
1 parent
2e00642
commit b981405
Showing
37 changed files
with
752 additions
and
354 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
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
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
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
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,37 @@ | ||
module Commands.Dev.Nockma.Run where | ||
|
||
import Commands.Base hiding (Atom) | ||
import Commands.Dev.Nockma.Run.Options | ||
import Juvix.Compiler.Nockma.Anoma | ||
import Juvix.Compiler.Nockma.EvalCompiled | ||
import Juvix.Compiler.Nockma.Evaluator | ||
import Juvix.Compiler.Nockma.Pretty | ||
import Juvix.Compiler.Nockma.Translation.FromSource qualified as Nockma | ||
import Juvix.Parser.Error | ||
|
||
runCommand :: forall r. (Members '[EmbedIO, App] r) => NockmaRunOptions -> Sem r () | ||
runCommand opts = do | ||
afile <- fromAppPathFile inputFile | ||
argsFile <- mapM fromAppPathFile (opts ^. nockmaRunArgs) | ||
parsedArgs <- mapM (Nockma.parseTermFile >=> checkParsed) argsFile | ||
parsedTerm <- Nockma.parseTermFile afile >>= checkParsed | ||
case parsedTerm of | ||
t@(TermCell {}) -> do | ||
let formula = anomaCallTuple parsedArgs | ||
(counts, res) <- | ||
runOpCounts | ||
. runReader defaultEvalOptions | ||
. runOutputSem @(Term Natural) (say . ppTrace) | ||
$ evalCompiledNock' t formula | ||
putStrLn (ppPrint res) | ||
let statsFile = replaceExtension' ".profile" afile | ||
writeFileEnsureLn statsFile (prettyText counts) | ||
TermAtom {} -> exitFailMsg "Expected nockma input to be a cell" | ||
where | ||
inputFile :: AppPath File | ||
inputFile = opts ^. nockmaRunFile | ||
|
||
checkParsed :: Either MegaparsecError (Term Natural) -> Sem r (Term Natural) | ||
checkParsed = \case | ||
Left err -> exitJuvixError (JuvixError err) | ||
Right tm -> return tm |
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,32 @@ | ||
module Commands.Dev.Nockma.Run.Options where | ||
|
||
import CommonOptions | ||
|
||
data NockmaRunOptions = NockmaRunOptions | ||
{ _nockmaRunFile :: AppPath File, | ||
_nockmaRunProfile :: Bool, | ||
_nockmaRunArgs :: Maybe (AppPath File) | ||
} | ||
deriving stock (Data) | ||
|
||
makeLenses ''NockmaRunOptions | ||
|
||
parseNockmaRunOptions :: Parser NockmaRunOptions | ||
parseNockmaRunOptions = do | ||
_nockmaRunFile <- parseInputFile FileExtNockma | ||
_nockmaRunArgs <- optional $ do | ||
_pathPath <- | ||
option | ||
somePreFileOpt | ||
( long "args" | ||
<> metavar "ARGS_FILE" | ||
<> help "Path to file containing args" | ||
<> action "file" | ||
) | ||
pure AppPath {_pathIsInput = True, ..} | ||
_nockmaRunProfile <- | ||
switch | ||
( long "profile" | ||
<> help "Report evaluator profiling statistics" | ||
) | ||
pure NockmaRunOptions {..} |
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
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
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
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
Oops, something went wrong.