Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CASM serialization #2679

Merged
merged 36 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
87ebbaa
language & compute label info
lukaszcz Mar 4, 2024
e4ec79e
translation CASM to binary Cairo
lukaszcz Mar 5, 2024
1fbc9b2
fix relative jump offsets for case compilation
lukaszcz Mar 5, 2024
6910f21
serialization
lukaszcz Mar 5, 2024
97590c0
json serialization
lukaszcz Mar 7, 2024
0ebd16c
cairo compile target
lukaszcz Mar 7, 2024
a3722ca
fix default target
lukaszcz Mar 7, 2024
9d765bd
Cairo VM proof mode compatibility
lukaszcz Mar 8, 2024
20518f3
gen_stone_params.py
lukaszcz Mar 8, 2024
c4299e6
gen_stone_params.py
lukaszcz Mar 8, 2024
5af3f0d
scripts to run Cairo VM and Stone prover
lukaszcz Mar 8, 2024
db48a30
relativize calls & jumps
lukaszcz Mar 11, 2024
bb490bb
output
lukaszcz Mar 11, 2024
be2ed5f
relativize closures
lukaszcz Mar 11, 2024
c7803c1
wip
lukaszcz Mar 11, 2024
92a05e0
fix extend_closure
lukaszcz Mar 12, 2024
350e6fd
call_closure
lukaszcz Mar 12, 2024
d3a5b1e
test suite
lukaszcz Mar 12, 2024
ff070cf
Reg to Cairo tests
lukaszcz Mar 19, 2024
ae9eb20
temporarily turn off tests
lukaszcz Mar 19, 2024
0bde6f9
fix runtime & tests
lukaszcz Mar 19, 2024
f1eb4b1
tests
lukaszcz Mar 20, 2024
2b9bf52
update CI
lukaszcz Mar 20, 2024
dd64bdc
update CI
lukaszcz Mar 20, 2024
2ac7390
update CI
lukaszcz Mar 20, 2024
41e9ac7
update CI
lukaszcz Mar 20, 2024
b8a78b4
update CI
lukaszcz Mar 20, 2024
338c6f9
close file
lukaszcz Mar 20, 2024
44eda7f
update MacOS CLI
lukaszcz Mar 20, 2024
b4a74fd
update CI
lukaszcz Mar 20, 2024
1142332
macOS CI
lukaszcz Mar 21, 2024
d32c969
change bash path
lukaszcz Mar 21, 2024
e25bc7c
fix compilation after rebase
lukaszcz Mar 21, 2024
690c2f6
Merge branch 'main' into casm-to-cairo
lukaszcz Mar 26, 2024
b3a41c4
fix compilation
lukaszcz Mar 26, 2024
2515f5a
style changes
lukaszcz Mar 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ jobs:

- name: Add ~/.local/bin to PATH
run: |
mkdir -p "$HOME/.local/bin"
echo "$HOME/.local/bin" >> $GITHUB_PATH

- name: Install the latest Wasmer version
Expand All @@ -118,6 +119,19 @@ jobs:
run: |
vamp-ir --version

- name: Install Rust toolchain
uses: actions-rust-lang/setup-rust-toolchain@v1

- name: Install Cairo VM
shell: bash
run: |
git clone https://github.com/lambdaclass/cairo-vm.git
cd cairo-vm
cargo build --release
cp target/release/cairo-vm-cli $HOME/.local/bin/cairo-vm-cli
cd -
cp main/scripts/run_cairo_vm.sh $HOME/.local/bin/run_cairo_vm.sh

- name: Make runtime
run: |
cd main
Expand Down Expand Up @@ -274,6 +288,26 @@ jobs:
echo "CC=$(brew --prefix llvm@15)/bin/clang" >> $GITHUB_ENV
echo "LIBTOOL=$(brew --prefix llvm@15)/bin/llvm-ar" >> $GITHUB_ENV

- name: Add ~/.local/bin to PATH
run: |
mkdir -p "$HOME/.local/bin"
echo "$HOME/.local/bin" >> $GITHUB_PATH

- name: Install Rust toolchain
uses: actions-rust-lang/setup-rust-toolchain@v1

- name: Install Cairo VM
shell: bash
run: |
git clone https://github.com/lambdaclass/cairo-vm.git
cd cairo-vm
cargo build --release
cp -a target/release/cairo-vm-cli $HOME/.local/bin/cairo-vm-cli
chmod a+x $HOME/.local/bin/cairo-vm-cli
cd -
cp -a main/scripts/run_cairo_vm.sh $HOME/.local/bin/run_cairo_vm.sh
chmod a+x $HOME/.local/bin/run_cairo_vm.sh

- name: Make runtime
run: |
cd main
Expand Down Expand Up @@ -325,10 +359,6 @@ jobs:
make CC=$CC LIBTOOL=$LIBTOOL install
make CC=$CC LIBTOOL=$LIBTOOL test

- name: Add ~/.local/bin to PATH
run: |
echo "$HOME/.local/bin" >> $GITHUB_PATH

- name: Typecheck and format Juvix examples
if: ${{ success() }}
shell: bash
Expand Down
1 change: 1 addition & 0 deletions app/Commands/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ runCommand opts@CompileOptions {..} = do
TargetReg -> Compile.runRegPipeline arg
TargetAnoma -> Compile.runAnomaPipeline arg
TargetCasm -> Compile.runCasmPipeline arg
TargetCairo -> Compile.runCairoPipeline arg

writeCoreFile :: (Members '[EmbedIO, App, TaggedLock] r) => Compile.PipelineArg -> Sem r ()
writeCoreFile [email protected] {..} = do
Expand Down
11 changes: 11 additions & 0 deletions app/Commands/Dev/Asm/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Commands.Dev.Asm.Compile where
import Commands.Base
import Commands.Dev.Asm.Compile.Options
import Commands.Extra.Compile qualified as Compile
import Data.Aeson qualified as JSON
import Juvix.Compiler.Asm.Translation.FromSource qualified as Asm
import Juvix.Compiler.Backend qualified as Backend
import Juvix.Compiler.Backend.C qualified as C
Expand Down Expand Up @@ -46,6 +47,15 @@ runCommand opts = do
$ tab
Casm.Result {..} <- getRight r
writeFileEnsureLn casmFile (toPlainText $ Casm.ppProgram _resultCode)
TargetCairo -> do
cairoFile <- Compile.outputFile opts file
r <-
runReader entryPoint
. runError @JuvixError
. asmToCairo
$ tab
res <- getRight r
liftIO $ JSON.encodeFile (toFilePath cairoFile) res
_ ->
case run $ runReader entryPoint $ runError $ asmToMiniC tab of
Left err -> exitJuvixError err
Expand All @@ -70,6 +80,7 @@ runCommand opts = do
TargetNative64 -> return Backend.TargetCNative64
TargetReg -> return Backend.TargetReg
TargetCasm -> return Backend.TargetCairo
TargetCairo -> return Backend.TargetCairo
TargetAnoma -> err "Anoma"
TargetTree -> err "JuvixTree"
TargetGeb -> err "GEB"
Expand Down
3 changes: 2 additions & 1 deletion app/Commands/Dev/Asm/Compile/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ asmSupportedTargets =
[ TargetWasm32Wasi,
TargetNative64,
TargetReg,
TargetCasm
TargetCasm,
TargetCairo
]

parseAsmCompileOptions :: Parser AsmCompileOptions
Expand Down
2 changes: 2 additions & 0 deletions app/Commands/Dev/Casm.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module Commands.Dev.Casm where

import Commands.Base
import Commands.Dev.Casm.Compile as Compile
import Commands.Dev.Casm.Options
import Commands.Dev.Casm.Read as Read
import Commands.Dev.Casm.Run as Run

runCommand :: forall r. (Members '[EmbedIO, App, TaggedLock] r) => CasmCommand -> Sem r ()
runCommand = \case
Compile opts -> Compile.runCommand opts
Run opts -> Run.runCommand opts
Read opts -> Read.runCommand opts
57 changes: 57 additions & 0 deletions app/Commands/Dev/Casm/Compile.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module Commands.Dev.Casm.Compile where

import Commands.Base
import Commands.Dev.Casm.Compile.Options
import Commands.Extra.Compile qualified as Compile
import Data.Aeson qualified as JSON
import Juvix.Compiler.Backend qualified as Backend
import Juvix.Compiler.Casm.Data.Result qualified as Casm
import Juvix.Compiler.Casm.Translation.FromSource qualified as Casm
import Juvix.Compiler.Casm.Validate qualified as Casm

runCommand :: forall r. (Members '[EmbedIO, App, TaggedLock] r) => CompileOptions -> Sem r ()
runCommand opts = do
file <- getFile
s <- readFile file
case Casm.runParser file s of
Left err -> exitJuvixError (JuvixError err)
Right (labi, code) ->
case Casm.validate labi code of
Left err -> exitJuvixError (JuvixError err)
Right () -> do
ep <- getEntryPoint (AppPath (preFileFromAbs file) True)
tgt <- getTarget (opts ^. compileTarget)
let entryPoint :: EntryPoint
entryPoint =
ep
{ _entryPointTarget = tgt,
_entryPointDebug = opts ^. compileDebug
}
cairoFile <- Compile.outputFile opts file
r <-
runReader entryPoint
. runError @JuvixError
. casmToCairo
$ Casm.Result labi code
res <- getRight r
liftIO $ JSON.encodeFile (toFilePath cairoFile) res
where
getFile :: Sem r (Path Abs File)
getFile = getMainFile (opts ^. compileInputFile)

getTarget :: CompileTarget -> Sem r Backend.Target
getTarget = \case
TargetCairo -> return Backend.TargetCairo
TargetWasm32Wasi -> err "WASM"
TargetNative64 -> err "native"
TargetCasm -> err "CASM"
TargetReg -> err "JuvixReg"
TargetAnoma -> err "Anoma"
TargetTree -> err "JuvixTree"
TargetGeb -> err "GEB"
TargetVampIR -> err "VampIR"
TargetCore -> err "JuvixCore"
TargetAsm -> err "JuvixAsm"
where
err :: Text -> Sem r a
err tgt = exitMsg (ExitFailure 1) ("error: " <> tgt <> " target not supported for CASM")
21 changes: 21 additions & 0 deletions app/Commands/Dev/Casm/Compile/Options.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Commands.Dev.Casm.Compile.Options
( module Commands.Dev.Casm.Compile.Options,
module Commands.Extra.Compile.Options,
)
where

import Commands.Extra.Compile.Options
import CommonOptions
import Data.List.NonEmpty qualified as NonEmpty

casmSupportedTargets :: NonEmpty CompileTarget
casmSupportedTargets =
NonEmpty.fromList
[ TargetCairo
]

parseCasmCompileOptions :: Parser CompileOptions
parseCasmCompileOptions =
parseCompileOptions
casmSupportedTargets
(parseInputFile FileExtCasm)
16 changes: 14 additions & 2 deletions app/Commands/Dev/Casm/Options.hs
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
module Commands.Dev.Casm.Options where

import Commands.Dev.Casm.Compile.Options
import Commands.Dev.Casm.Read.Options
import Commands.Dev.Casm.Run.Options
import CommonOptions

data CasmCommand
= Run CasmRunOptions
= Compile CompileOptions
| Run CasmRunOptions
| Read CasmReadOptions
deriving stock (Data)

parseCasmCommand :: Parser CasmCommand
parseCasmCommand =
hsubparser $
mconcat
[ commandRun,
[ commandCompile,
commandRun,
commandRead
]
where
commandCompile :: Mod CommandFields CasmCommand
commandCompile = command "compile" compileInfo

commandRun :: Mod CommandFields CasmCommand
commandRun = command "run" runInfo

commandRead :: Mod CommandFields CasmCommand
commandRead = command "read" readInfo

compileInfo :: ParserInfo CasmCommand
compileInfo =
info
(Compile <$> parseCasmCompileOptions)
(progDesc "Compile a CASM file")

runInfo :: ParserInfo CasmCommand
runInfo =
info
Expand Down
1 change: 1 addition & 0 deletions app/Commands/Dev/Core/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ runCommand opts = do
TargetTree -> runTreePipeline arg
TargetAnoma -> runAnomaPipeline arg
TargetCasm -> runCasmPipeline arg
TargetCairo -> runCairoPipeline arg
where
getFile :: Sem r (Path Abs File)
getFile = getMainFile (opts ^. compileInputFile)
14 changes: 14 additions & 0 deletions app/Commands/Dev/Core/Compile/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Commands.Base
import Commands.Dev.Core.Compile.Options
import Commands.Dev.Tree.Compile.Base (outputAnomaResult)
import Commands.Extra.Compile qualified as Compile
import Data.Aeson qualified as JSON
import Juvix.Compiler.Asm.Pretty qualified as Asm
import Juvix.Compiler.Backend qualified as Backend
import Juvix.Compiler.Backend.C qualified as C
Expand Down Expand Up @@ -48,6 +49,7 @@ getEntry PipelineArg {..} = do
TargetTree -> Backend.TargetTree
TargetAnoma -> Backend.TargetAnoma
TargetCasm -> Backend.TargetCairo
TargetCairo -> Backend.TargetCairo

defaultOptLevel :: Int
defaultOptLevel
Expand Down Expand Up @@ -169,3 +171,15 @@ runCasmPipeline pa@PipelineArg {..} = do
$ _pipelineArgModule
Casm.Result {..} <- getRight r
writeFileEnsureLn casmFile (toPlainText $ Casm.ppProgram _resultCode)

runCairoPipeline :: (Members '[EmbedIO, App, TaggedLock] r) => PipelineArg -> Sem r ()
runCairoPipeline pa@PipelineArg {..} = do
entryPoint <- getEntry pa
cairoFile <- Compile.outputFile _pipelineArgOptions _pipelineArgFile
r <-
runReader entryPoint
. runError @JuvixError
. coreToCairo
$ _pipelineArgModule
res <- getRight r
liftIO $ JSON.encodeFile (toFilePath cairoFile) res
7 changes: 4 additions & 3 deletions app/Commands/Dev/Core/Compile/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ type CoreCompileOptions = CompileOptions
coreSupportedTargets :: NonEmpty CompileTarget
coreSupportedTargets =
NonEmpty.fromList
[ TargetWasm32Wasi,
TargetNative64,
[ TargetNative64,
TargetWasm32Wasi,
TargetGeb,
TargetVampIR,
TargetTree,
TargetAsm,
TargetReg,
TargetCasm
TargetCasm,
TargetCairo
]

parseCoreCompileOptions :: Parser CoreCompileOptions
Expand Down
11 changes: 11 additions & 0 deletions app/Commands/Dev/Reg/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Commands.Dev.Reg.Compile where
import Commands.Base
import Commands.Dev.Reg.Compile.Options
import Commands.Extra.Compile qualified as Compile
import Data.Aeson qualified as JSON
import Juvix.Compiler.Backend qualified as Backend
import Juvix.Compiler.Backend.C qualified as C
import Juvix.Compiler.Casm.Data.Result qualified as Casm
Expand Down Expand Up @@ -35,6 +36,15 @@ runCommand opts = do
$ tab
Casm.Result {..} <- getRight r
writeFileEnsureLn casmFile (toPlainText $ Casm.ppProgram _resultCode)
TargetCairo -> do
cairoFile <- Compile.outputFile opts file
r <-
runReader entryPoint
. runError @JuvixError
. regToCairo
$ tab
res <- getRight r
liftIO $ JSON.encodeFile (toFilePath cairoFile) res
_ ->
case run $ runReader entryPoint $ runError $ regToMiniC tab of
Left err -> exitJuvixError err
Expand All @@ -58,6 +68,7 @@ runCommand opts = do
TargetWasm32Wasi -> return Backend.TargetCWasm32Wasi
TargetNative64 -> return Backend.TargetCNative64
TargetCasm -> return Backend.TargetCairo
TargetCairo -> return Backend.TargetCairo
TargetReg -> err "JuvixReg"
TargetAnoma -> err "Anoma"
TargetTree -> err "JuvixTree"
Expand Down
7 changes: 4 additions & 3 deletions app/Commands/Dev/Reg/Compile/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import Data.List.NonEmpty qualified as NonEmpty
regSupportedTargets :: NonEmpty CompileTarget
regSupportedTargets =
NonEmpty.fromList
[ TargetWasm32Wasi,
TargetNative64,
TargetCasm
[ TargetNative64,
TargetWasm32Wasi,
TargetCasm,
TargetCairo
]

parseRegCompileOptions :: Parser CompileOptions
Expand Down
4 changes: 2 additions & 2 deletions app/Commands/Dev/Runtime/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ newtype RuntimeCommand
runtimeSupportedTargets :: NonEmpty CompileTarget
runtimeSupportedTargets =
NonEmpty.fromList
[ TargetWasm32Wasi,
TargetNative64
[ TargetNative64,
TargetWasm32Wasi
]

parseRuntimeOptions :: Parser CompileOptions
Expand Down
1 change: 1 addition & 0 deletions app/Commands/Dev/Tree/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ runCommand opts = do
TargetTree -> return ()
TargetAnoma -> runAnomaPipeline arg
TargetCasm -> runCasmPipeline arg
TargetCairo -> runCairoPipeline arg
where
getFile :: Sem r (Path Abs File)
getFile = getMainFile (opts ^. compileInputFile)
Loading
Loading