Skip to content

Commit

Permalink
Contract initialization without args and simple calls
Browse files Browse the repository at this point in the history
  • Loading branch information
virgil-serbanuta committed Sep 12, 2024
1 parent 4eac1c1 commit 5783250
Show file tree
Hide file tree
Showing 47 changed files with 762 additions and 140 deletions.
46 changes: 43 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ MX_RUST_TESTING_OUTPUT_DIR ::= .build/mx-rust/output
MX_RUST_TESTING_INPUTS ::= $(wildcard $(MX_RUST_TESTING_INPUT_DIR)/*.run)
MX_RUST_TESTING_OUTPUTS ::= $(patsubst $(MX_RUST_TESTING_INPUT_DIR)/%,$(MX_RUST_TESTING_OUTPUT_DIR)/%.executed.kore,$(MX_RUST_TESTING_INPUTS))

.PHONY: clean build test syntax-test preprocessing-test execution-test mx-test mx-rust-test
MX_RUST_CONTRACT_TESTING_SEMANTICS_FILES ::= $(shell find mx-rust-semantics/ -type f -a '(' -name '*.md' -or -name '*.k' ')')
MX_RUST_CONTRACT_TESTING_KOMPILED ::= .build/mx-rust-contract-testing-kompiled
MX_RUST_CONTRACT_TESTING_TIMESTAMP ::= $(MX_RUST_CONTRACT_TESTING_KOMPILED)/timestamp
MX_RUST_CONTRACT_TESTING_INPUT_DIR ::= tests/mx-rust-contracts
MX_RUST_CONTRACT_TESTING_OUTPUT_DIR ::= .build/mx-rust-contarcts/output
MX_RUST_CONTRACT_TESTING_INPUTS ::= $(wildcard $(MX_RUST_CONTRACT_TESTING_INPUT_DIR)/*.run)
MX_RUST_CONTRACT_TESTING_OUTPUTS ::= $(patsubst $(MX_RUST_CONTRACT_TESTING_INPUT_DIR)/%,$(MX_RUST_CONTRACT_TESTING_OUTPUT_DIR)/%.executed.kore,$(MX_RUST_CONTRACT_TESTING_INPUTS))

.PHONY: clean build test syntax-test preprocessing-test execution-test mx-test mx-rust-test mx-rust-contract-test

clean:
rm -r .build
Expand All @@ -48,9 +56,10 @@ build: $(RUST_PREPROCESSING_TIMESTAMP) \
$(RUST_EXECUTION_TIMESTAMP) \
$(MX_TESTING_TIMESTAMP) \
$(MX_RUST_TIMESTAMP) \
$(MX_RUST_TESTING_TIMESTAMP)
$(MX_RUST_TESTING_TIMESTAMP) \
$(MX_RUST_CONTRACT_TESTING_TIMESTAMP)

test: build syntax-test preprocessing-test execution-test mx-test mx-rust-test
test: build syntax-test preprocessing-test execution-test mx-test mx-rust-test mx-rust-contract-test

syntax-test: $(SYNTAX_OUTPUTS)

Expand All @@ -62,6 +71,8 @@ mx-test: $(MX_TESTING_OUTPUTS)

mx-rust-test: $(MX_RUST_TESTING_OUTPUTS)

mx-rust-contract-test: $(MX_RUST_CONTRACT_TESTING_OUTPUTS)

$(RUST_PREPROCESSING_TIMESTAMP): $(RUST_SEMANTICS_FILES)
# Workaround for https://github.com/runtimeverification/k/issues/4141
-rm -r $(RUST_PREPROCESSING_KOMPILED)
Expand Down Expand Up @@ -93,6 +104,15 @@ $(MX_RUST_TESTING_TIMESTAMP): $(MX_SEMANTICS_FILES) $(RUST_SEMANTICS_FILES) $(MX
-I . \
--debug


$(MX_RUST_CONTRACT_TESTING_TIMESTAMP): $(MX_SEMANTICS_FILES) $(RUST_SEMANTICS_FILES) $(MX_RUST_SEMANTICS_FILES)
# Workaround for https://github.com/runtimeverification/k/issues/4141
-rm -r $(MX_RUST_CONTRACT_TESTING_KOMPILED)
$$(which kompile) mx-rust-semantics/targets/contract-testing/mx-rust.md \
-o $(MX_RUST_CONTRACT_TESTING_KOMPILED) \
-I . \
--debug

$(RUST_SYNTAX_OUTPUT_DIR)/%.rs-parsed: $(RUST_SYNTAX_INPUT_DIR)/%.rs $(RUST_PREPROCESSING_TIMESTAMP)
mkdir -p $(RUST_SYNTAX_OUTPUT_DIR)
kast --definition $(RUST_PREPROCESSING_KOMPILED) $< --sort Crate > $@.tmp && mv -f $@.tmp $@
Expand Down Expand Up @@ -154,3 +174,23 @@ $(MX_RUST_TESTING_OUTPUT_DIR)/%.run.executed.kore: \
-pTEST=$(CURDIR)/parse-mx-rust-test.sh
cat $@.tmp | grep -q "Lbl'-LT-'k'-GT-'{}(dotk{}())"
mv -f $@.tmp $@


# TODO: Add $(shell echo "$<" | sed 's/\.[^.]*.run$$//').rs
# as a dependency
$(MX_RUST_CONTRACT_TESTING_OUTPUT_DIR)/%.run.executed.kore: \
$(MX_RUST_CONTRACT_TESTING_INPUT_DIR)/%.run \
$(MX_RUST_CONTRACT_TESTING_TIMESTAMP) \
parse-mx-rust-contract.sh \
parse-mx-rust-contract-test.sh
mkdir -p $(MX_RUST_CONTRACT_TESTING_OUTPUT_DIR)
krun \
"$(shell echo "$<" | sed 's/\.[^.]*.run$$//').rs" \
--definition $(MX_RUST_CONTRACT_TESTING_KOMPILED) \
--parser $(CURDIR)/parse-mx-rust-contract.sh \
--output kore \
--output-file $@.tmp \
-cTEST='$(shell cat $<)' \
-pTEST=$(CURDIR)/parse-mx-rust-contract-test.sh
cat $@.tmp | grep -q "Lbl'-LT-'k'-GT-'{}(dotk{}())"
mv -f $@.tmp $@
9 changes: 9 additions & 0 deletions mx-rust-semantics/main/calls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```k
requires "calls/implementation.md"
module MX-RUST-CALLS
imports private MX-RUST-CALLS-IMPLEMENTATION
endmodule
```
17 changes: 17 additions & 0 deletions mx-rust-semantics/main/calls/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
```k
module MX-RUST-CALLS-CONFIGURATION
imports LIST
imports RUST-SHARED-SYNTAX
configuration
<mx-rust-calls>
<rust-execution-state-stack> .List </rust-execution-state-stack>
<mx-rust-endpoint-to-function> .Map </mx-rust-endpoint-to-function> // String to Identifier
// Valid only while a contract call is being prepared
<mx-rust-last-trait-name> (#token("no#path", "Identifier"):Identifier):TypePath </mx-rust-last-trait-name>
</mx-rust-calls>
endmodule
```
92 changes: 92 additions & 0 deletions mx-rust-semantics/main/calls/implementation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
```k
module MX-RUST-CALLS-IMPLEMENTATION
imports private COMMON-K-CELL
imports private MX-CALL-CONFIGURATION
imports private MX-COMMON-SYNTAX
imports private MX-RUST-CALLS-CONFIGURATION
imports private MX-RUST-REPRESENTATION
imports private RUST-EXECUTION-CONFIGURATION
imports private RUST-PREPROCESSING-CONFIGURATION
imports private RUST-REPRESENTATION
imports private RUST-SHARED-SYNTAX
rule (.K => rustValueToMx(V))
~> rustValuesToMx((V:Value , L:ValueList => L), _:MxValueList)
rule (V:MxValue => .K)
~> rustValuesToMx(_:ValueList, (L:MxValueList => V , L))
rule rustValuesToMx(.ValueList, ReversedArgs:MxValueList) ~> Hook:MxHookName
=> Hook(reverse(ReversedArgs, .MxValueList))
rule
<k>
host.pushCallState => .K
...
</k>
Execution:ExecutionCell
<rust-execution-state-stack> .List => ListItem(Execution) ... </rust-execution-state-stack>
rule
<k>
host.popCallState => .K
...
</k>
(_:ExecutionCell => Execution)
<rust-execution-state-stack> ListItem(Execution:ExecutionCell) => .List ... </rust-execution-state-stack>
rule
<k>
host.resetCallState => .K
...
</k>
(_:ExecutionCell => <execution>... <values> .Map </values> </execution>)
rule
<k>
host.newEnvironment
( rustCode
( EndpointToFunction:Map
, TraitName:TypePath
, Preprocessed:PreprocessedCell
)
)
=> .K
...
</k>
<mx-rust-last-trait-name> _ => TraitName </mx-rust-last-trait-name>
<mx-rust-endpoint-to-function> _ => EndpointToFunction </mx-rust-endpoint-to-function>
(_:PreprocessedCell => Preprocessed)
rule
<k>
host.mkCall(FunctionName:String)
=> MxRust#newContractObject(TraitName)
~> MxRust#partialMethodCall(Endpoint, mxArgsToRustArgs(Args))
...
</k>
<mx-rust-endpoint-to-function>
FunctionName |-> Endpoint:Identifier ...
</mx-rust-endpoint-to-function>
<mx-rust-last-trait-name>
TraitName:TypePath
=> (#token("no#path", "Identifier"):Identifier):TypePath
</mx-rust-last-trait-name>
<mx-call-args> Args:MxValueList </mx-call-args>
rule ptrValue(...) #as SelfValue:Expression
~> MxRust#partialMethodCall(Method:Identifier, Params:CallParamsList)
=> methodCall(... self: SelfValue, method: Method, params: Params)
syntax MxRustInstruction ::= "MxRust#newContractObject" "(" TypePath ")"
rule MxRust#newContractObject(P:TypePath) => Rust#newStruct(P, .Map)
syntax MxRustInstruction ::= "MxRust#partialMethodCall" "(" Identifier "," CallParamsList ")"
syntax CallParamsList ::= mxArgsToRustArgs(MxValueList) [function, total]
rule mxArgsToRustArgs(.MxValueList) => .CallParamsList
rule (ptrValue(_, V:Value) => rustValueToMx(V)) ~> endCall
endmodule
```
3 changes: 3 additions & 0 deletions mx-rust-semantics/main/configuration.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
```k
requires "calls/configuration.md"
requires "mx-semantics/main/configuration.md"
requires "rust-semantics/config.md"
module MX-RUST-COMMON-CONFIGURATION
imports MX-COMMON-CONFIGURATION
imports MX-RUST-CALLS-CONFIGURATION
imports RUST-CONFIGURATION
configuration
<mx-rust>
<mx-common/>
<rust/>
<mx-rust-calls/>
</mx-rust>
endmodule
Expand Down
84 changes: 47 additions & 37 deletions mx-rust-semantics/main/glue.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,60 @@
```k
module MX-RUST-GLUE
imports private COMMON-K-CELL
imports private MX-COMMON-SYNTAX
imports private RUST-EXECUTION-CONFIGURATION
imports private RUST-VALUE-SYNTAX
imports private MX-RUST-REPRESENTATION
rule
<k>
storeHostValue
(... destination: rustDestination(ValueId, _:MxRustType)
, value: wrappedRust(V:Value)
)
=> .K
...
</k>
<values> Values:Map => Values[ValueId <- V] </values>
requires ValueId >=Int 0
rule
(.K => mxRustEmptyValue(T))
~> storeHostValue
(... destination: rustDestination(_, T:MxRustType)
, value: mxWrappedEmpty
)
rule
(ptrValue(_, V:Value) => .K)
~> storeHostValue
(... value: mxWrappedEmpty => wrappedRust(V)
)
rule
<k>
mxRustLoadPtr(P:Int) => ptrValue(ptr(P), V)
...
</k>
<values> P |-> V:Value ... </values>
imports private COMMON-K-CELL
imports private K-EQUAL-SYNTAX
imports private MX-COMMON-SYNTAX
imports private MX-RUST-REPRESENTATION
imports private RUST-EXECUTION-CONFIGURATION
imports private RUST-VALUE-SYNTAX
rule (.K => mxValueToRust(T, V))
~> storeHostValue
(... destination: rustDestination(_ValueId, rustType(T):MxRustType)
, value: V:MxValue
)
requires notBool isMxEmptyValue(V)
rule
<k>
ptrValue(_, V:Value)
~> storeHostValue
(... destination: rustDestination(ValueId, _:MxRustType)
, value: _:MxValue
)
=> .K
...
</k>
<values> Values:Map => Values[ValueId <- V] </values>
requires ValueId >=Int 0
rule
(.K => mxRustEmptyValue(T))
~> storeHostValue
(... destination: rustDestination(_, T:MxRustType)
, value: mxEmptyValue
)
rule
<k>
mxRustLoadPtr(P:Int) => ptrValue(ptr(P), V)
...
</k>
<values> P |-> V:Value ... </values>
rule
<k> mxRustNewValue(V:Value) => ptrValue(ptr(NextId), V) ... </k>
<next-value-id> NextId:Int => NextId +Int 1 </next-value-id>
<values> Values:Map => Values[NextId <- V] </values>
rule mxIntValue(I:Int) ~> mxValueToRust(T:Type)
rule V:MxValue ~> mxValueToRust(T:Type)
=> mxValueToRust(T, V)
rule mxValueToRust(T:Type, mxIntValue(I:Int))
=> mxRustNewValue(integerToValue(I, T))
requires
(T ==K i32 orBool T ==K u32)
orBool (T ==K i64 orBool T ==K u64)
endmodule
```
30 changes: 29 additions & 1 deletion mx-rust-semantics/main/modules/biguint.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,36 @@ module MX-RUST-MODULES-BIGUINT
)
)
rule mxRustEmptyValue(BigUint) => mxRustBigIntNew(0)
rule mxRustEmptyValue(rustType(#token("BigUint", "Identifier")))
=> mxRustBigIntNew(0)
rule mxValueToRust(#token("BigUint", "Identifier"), mxIntValue(I:Int))
=> mxRustBigIntNew(I)
rule rustValueToMx
( struct
( #token("BigUint", "Identifier"):Identifier
, _:Map
) #as S:Value
)
=> mxRustGetBigIntFromStruct(S)
rule
<k>
mxRustGetBigIntFromStruct
( struct
( #token("BigUint", "Identifier"):Identifier
, #token("mx_biguint_id", "Identifier"):Identifier |-> BigUintIdId:Int
_:Map
)
)
=> mxGetBigInt(MInt2Unsigned(BigUintId))
...
</k>
<values>
BigUintIdId |-> i32(BigUintId:MInt{32})
...
</values>
endmodule
```
10 changes: 6 additions & 4 deletions mx-rust-semantics/main/modules/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module MX-RUST-MODULES-STORAGE
, .NormalizedCallParams
)
)
=> MX#storageStore(mxStringValue(StorageKey), wrappedRust(V))
=> rustValuesToMx((V, .ValueList), (mxStringValue(StorageKey), .MxValueList)) ~> MX#storageStore
...
</k>
<values>
Expand All @@ -61,7 +61,8 @@ module MX-RUST-MODULES-STORAGE
)
=> MX#storageLoad(mxStringValue(StorageKey), rustDestination(-1, noType))
~> setIfEmpty
~> MX#storageStore(mxStringValue(StorageKey), wrappedRust(V))
~> rustValuesToMx((V, .ValueList), (mxStringValue(StorageKey), .MxValueList))
~> MX#storageStore
...
</k>
<values>
Expand All @@ -77,9 +78,10 @@ module MX-RUST-MODULES-STORAGE
rule mxRustEmptyValue(noType) ~> storeHostValue(...) ~> setIfEmpty
=> .K
rule storeHostValue(... value: wrappedRust(_))
~> setIfEmpty ~> MX#storageStore(_)
rule storeHostValue(... value: V:MxValue)
~> setIfEmpty ~> _:KItem ~> MX#storageStore
=> .K
requires notBool isMxEmptyValue(V)
rule
<k>
Expand Down
Loading

0 comments on commit 5783250

Please sign in to comment.