Skip to content

Commit

Permalink
Add an execution target; execute an empty method (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: Virgil Serbanuta <Virgil Serbanuta>
  • Loading branch information
virgil-serbanuta authored Aug 26, 2024
1 parent d66be36 commit 6db7c6f
Show file tree
Hide file tree
Showing 20 changed files with 347 additions and 9 deletions.
36 changes: 31 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
SEMANTICS_FILES ::= $(shell find rust-semantics/ -type f -a '(' -name '*.md' -or -name '*.k' ')')
RUST_PREPROCESSING_KOMPILED ::= .build/rust-preprocessing-kompiled
RUST_PREPROCESSING_TIMESTAMP ::= $(RUST_PREPROCESSING_KOMPILED)/timestamp
RUST_EXECUTION_KOMPILED ::= .build/rust-execution-kompiled
RUST_EXECUTION_TIMESTAMP ::= $(RUST_EXECUTION_KOMPILED)/timestamp
SYNTAX_INPUT_DIR ::= tests/syntax
SYNTAX_OUTPUT_DIR ::= .build/syntax-output
PREPROCESSING_INPUT_DIR ::= tests/preprocessing
PREPROCESSING_OUTPUT_DIR ::= .build/preprocessing-output
EXECUTION_INPUT_DIR ::= tests/execution
EXECUTION_OUTPUT_DIR ::= .build/execution-output

SYNTAX_INPUTS ::= $(wildcard $(SYNTAX_INPUT_DIR)/*.rs)
SYNTAX_OUTPUTS ::= $(patsubst $(SYNTAX_INPUT_DIR)/%.rs,$(SYNTAX_OUTPUT_DIR)/%.rs-parsed,$(SYNTAX_INPUTS))

PREPROCESSING_INPUTS ::= $(wildcard $(PREPROCESSING_INPUT_DIR)/*.rs)
PREPROCESSING_OUTPUTS ::= $(patsubst $(PREPROCESSING_INPUT_DIR)/%.rs,$(PREPROCESSING_OUTPUT_DIR)/%.rs.preprocessed.kore,$(PREPROCESSING_INPUTS))
PREPROCESSING_STATUSES ::= $(patsubst %.rs.preprocessed.kore,%.rs.status,$(PREPROCESSING_OUTPUTS))
PREPROCESSING_OUTPUTS ::= $(patsubst $(PREPROCESSING_INPUT_DIR)/%,$(PREPROCESSING_OUTPUT_DIR)/%.preprocessed.kore,$(PREPROCESSING_INPUTS))
PREPROCESSING_STATUSES ::= $(patsubst %.preprocessed.kore,%.status,$(PREPROCESSING_OUTPUTS))

.PHONY: clean build test syntax-test preprocessing-test
EXECUTION_INPUTS ::= $(wildcard $(EXECUTION_INPUT_DIR)/*.*.run)
EXECUTION_OUTPUTS ::= $(patsubst $(EXECUTION_INPUT_DIR)/%,$(EXECUTION_OUTPUT_DIR)/%.executed.kore,$(EXECUTION_INPUTS))
EXECUTION_STATUSES ::= $(patsubst %.executed.kore,%.status,$(EXECUTION_OUTPUTS))

.PHONY: clean build test syntax-test preprocessing-test execution-test

clean:
rm -r .build

build: $(RUST_PREPROCESSING_TIMESTAMP)
build: $(RUST_PREPROCESSING_TIMESTAMP) $(RUST_EXECUTION_TIMESTAMP)

test: syntax-test preprocessing-test
test: syntax-test preprocessing-test execution-test

syntax-test: $(SYNTAX_OUTPUTS)

preprocessing-test: $(PREPROCESSING_OUTPUTS)

execution-test: $(EXECUTION_OUTPUTS)

$(RUST_PREPROCESSING_TIMESTAMP): $(SEMANTICS_FILES)
$$(which kompile) rust-semantics/targets/preprocessing/rust.md -o $(RUST_PREPROCESSING_KOMPILED)

$(RUST_EXECUTION_TIMESTAMP): $(SEMANTICS_FILES)
$$(which kompile) rust-semantics/targets/execution/rust.md -o $(RUST_EXECUTION_KOMPILED)

$(SYNTAX_OUTPUT_DIR)/%.rs-parsed: $(SYNTAX_INPUT_DIR)/%.rs $(RUST_PREPROCESSING_TIMESTAMP)
mkdir -p $(SYNTAX_OUTPUT_DIR)
kast --definition $(RUST_PREPROCESSING_KOMPILED) $< --sort Crate > $@.tmp && mv -f $@.tmp $@
Expand All @@ -43,3 +56,16 @@ $(PREPROCESSING_OUTPUT_DIR)/%.rs.preprocessed.kore: $(PREPROCESSING_INPUT_DIR)/%
cat $@.tmp | grep -q "Lbl'-LT-'k'-GT-'{}(dotk{}())"
mv -f $@.tmp $@

# TODO: Add $(shell echo "$<" | sed 's/\.[^.]*.run$$//').rs
# as a dependency
$(EXECUTION_OUTPUT_DIR)/%.run.executed.kore: $(EXECUTION_INPUT_DIR)/%.run $(RUST_EXECUTION_TIMESTAMP)
mkdir -p $(EXECUTION_OUTPUT_DIR)
krun \
"$(shell echo "$<" | sed 's/\.[^.]*.run$$//').rs" \
--definition $(RUST_EXECUTION_KOMPILED) \
--output kore \
--output-file $@.tmp \
-cTEST="$(shell cat $<)" \
-pTEST=$(CURDIR)/run-test.sh
cat $@.tmp | grep -q "Lbl'-LT-'k'-GT-'{}(dotk{}())"
mv -f $@.tmp $@
8 changes: 8 additions & 0 deletions run-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /bin/bash

kast \
--output kore \
--definition .build/rust-execution-kompiled \
--module RUST-SYNTAX \
--sort ExecutionTest \
$1
2 changes: 2 additions & 0 deletions rust-semantics/config.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
```k
module RUST-CONFIGURATION
imports RUST-EXECUTION-CONFIGURATION
imports RUST-PREPROCESSING-CONFIGURATION
configuration
<rust>
<preprocessed/>
<execution/>
</rust>
endmodule
Expand Down
14 changes: 14 additions & 0 deletions rust-semantics/execution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
```k
requires "execution/configuration.md"
requires "execution/block.md"
requires "execution/calls.md"
requires "execution/stack.md"
module RUST-EXECUTION
imports RUST-BLOCK
imports RUST-CALLS
imports RUST-STACK
endmodule
```
17 changes: 17 additions & 0 deletions rust-semantics/execution/block.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
```k
module RUST-BLOCK
imports RUST-SHARED-SYNTAX
imports RUST-STACK
// https://doc.rust-lang.org/stable/reference/expressions/block-expr.html
// https://doc.rust-lang.org/stable/reference/names/scopes.html
rule {.InnerAttributes}:BlockExpression => .K
// Pushin and popping the local state (without cleaing) should help with
// variable shadowing
rule {.InnerAttributes S:Statements}:BlockExpression
=> pushLocalState ~> S ~> popLocalState
endmodule
```
56 changes: 56 additions & 0 deletions rust-semantics/execution/calls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
```k
module RUST-CALLS
imports BOOL
imports RUST-STACK
imports RUST-HELPERS
imports RUST-REPRESENTATION
imports RUST-RUNNING-CONFIGURATION
// https://doc.rust-lang.org/stable/reference/expressions/method-call-expr.html
syntax Instruction ::= "clearLocalState"
| setArgs(NormalizedCallParams, NormalizedFunctionParameterList)
rule
<k>
normalizedMethodCall(
TraitName:TypePath,
MethodName:Identifier,
Args:NormalizedCallParams
) => pushLocalState
~> clearLocalState
~> setArgs(Args, Params)
~> FunctionBody
~> popLocalState
...
</k>
<trait-path> TraitName </trait-path>
<method-name> MethodName </method-name>
<method-params> Params:NormalizedFunctionParameterList </method-params>
<method-implementation> block(FunctionBody) </method-implementation>
rule
<k>
clearLocalState => .K
...
</k>
<locals> _ => .Map </locals>
rule
<k>
setArgs(
(ValueId:Int , Ids:NormalizedCallParams) => Ids,
((Name:Identifier : T:Type) , Ps:NormalizedFunctionParameterList) => Ps
)
...
</k>
<locals> Locals:Map => Locals[Name <- ValueId] </locals>
<values> ValueId |-> Value ... </values>
requires notBool Name in_keys(Locals) andBool isSameType(Value, T)
rule setArgs(.NormalizedCallParams, .NormalizedFunctionParameterList) => .K
endmodule
```
17 changes: 17 additions & 0 deletions rust-semantics/execution/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
```k
module RUST-EXECUTION-CONFIGURATION
imports INT
imports LIST
imports MAP
configuration
<execution>
<values> .Map </values> // Map from ValueId:Int |-> Value
<locals> .Map </locals> // Map from Identifier |-> ValueId:Int
<stack> .List </stack> // list of locals map.
<next-value-id> 0 </next-value-id>
</execution>
endmodule
```
28 changes: 28 additions & 0 deletions rust-semantics/execution/stack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
```k
module RUST-STACK
imports LIST
imports RUST-RUNNING-CONFIGURATION
syntax Instruction ::= "pushLocalState"
| "popLocalState"
rule
<k>
pushLocalState => .K
...
</k>
<locals> Locals </locals>
<stack> .List => ListItem(Locals) ... </stack>
rule
<k>
popLocalState => .K
...
</k>
<locals> _ => Locals </locals>
<stack> ListItem(Locals) => .List ... </stack>
endmodule
```
19 changes: 19 additions & 0 deletions rust-semantics/helpers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
```k
module RUST-HELPERS
imports BOOL
imports private RUST-REPRESENTATION
syntax Int ::= paramsLength(NormalizedFunctionParameterList) [function, total]
rule paramsLength(.NormalizedFunctionParameterList) => 0
rule paramsLength(_P:NormalizedFunctionParameter , Ps:NormalizedFunctionParameterList)
=> 1 +Int paramsLength(Ps)
syntax Bool ::= isSameType(Value, Type) [function, total]
rule isSameType(_, _) => false [owise]
rule isSameType(_, $selftype) => true
rule isSameType(i64(_), i64) => true
rule isSameType(u64(_), u64) => true
rule isSameType(i32(_), i32) => true
rule isSameType(u32(_), u32) => true
endmodule
```
8 changes: 7 additions & 1 deletion rust-semantics/representation.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module RUST-REPRESENTATION
imports INT
imports LIST // for filling the second argument of `error`.
imports MAP
imports MINT
imports RUST-SHARED-SYNTAX
Expand All @@ -20,6 +21,10 @@ module RUST-REPRESENTATION
syntax NormalizedFunctionParameter ::= Identifier ":" Type
syntax NormalizedFunctionParameterList ::= List{NormalizedFunctionParameter, ","}
syntax NormalizedCallParams ::=List{Int, ","}
syntax Instruction ::= normalizedMethodCall(TypePath, Identifier, NormalizedCallParams)
syntax NormalizedFunctionParameterListOrError ::= NormalizedFunctionParameterList | SemanticsError
syntax Value ::= i32(MInt{32})
Expand All @@ -28,6 +33,7 @@ module RUST-REPRESENTATION
| u64(MInt{64})
| u128(MInt{128})
| tuple(ValueList)
| struct(TypePath, Map) // Map from field name (Identifier) to value ID (Int)
syntax ValueList ::= List{Value, ","}
syntax Expression ::= Value
syntax KResult ::= Value
Expand All @@ -44,4 +50,4 @@ module RUST-REPRESENTATION
endmodule
```
```
8 changes: 5 additions & 3 deletions rust-semantics/rust-common.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
```k
requires "config.md"
requires "execution.md"
requires "expression.md"
requires "helpers.md"
requires "preprocessing.md"
requires "representation.md"
requires "rust-common-syntax.md"
module RUST-COMMON
imports RUST-RUNNING-CONFIGURATION
imports RUST-EXECUTION
imports RUST-EXPRESSION
imports RUST-HELPERS
imports RUST-PREPROCESSING
imports RUST-REPRESENTATION
imports RUST-RUNNING-CONFIGURATION
imports RUST-SHARED-SYNTAX
endmodule
```
17 changes: 17 additions & 0 deletions rust-semantics/targets/execution/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
```k
module RUST-RUNNING-CONFIGURATION
imports private RUST-PREPROCESSING-SYNTAX
imports private RUST-EXECUTION-TEST-PARSING-SYNTAX
imports RUST-CONFIGURATION
imports RUST-EXECUTION-TEST-CONFIGURATION
configuration
<rust-mx>
<k> crateParser($PGM:Crate) ~> $TEST:ExecutionTest </k>
<rust/>
<rust-test/>
</rust-mx>
endmodule
```
18 changes: 18 additions & 0 deletions rust-semantics/targets/execution/rust.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
```k
requires "configuration.md"
requires "../../rust-common.md"
requires "../../rust-common-syntax.md"
requires "../../test.md"
module RUST-SYNTAX
imports RUST-COMMON-SYNTAX
imports RUST-EXECUTION-TEST-PARSING-SYNTAX
endmodule
module RUST
imports RUST-COMMON
imports RUST-EXECUTION-TEST
endmodule
```
3 changes: 3 additions & 0 deletions rust-semantics/targets/preprocessing/configuration.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
```k
module RUST-RUNNING-CONFIGURATION
imports private RUST-PREPROCESSING-SYNTAX
imports RUST-CONFIGURATION
configuration
<rust-mx>
<k> crateParser($PGM:Crate) </k>
<rust/>
</rust-mx>
endmodule
```
2 changes: 2 additions & 0 deletions rust-semantics/targets/preprocessing/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
requires "configuration.md"
requires "../../rust-common.md"
requires "../../rust-common-syntax.md"
module RUST-SYNTAX
imports RUST-COMMON-SYNTAX
endmodule
module RUST
imports RUST-COMMON
endmodule
Expand Down
10 changes: 10 additions & 0 deletions rust-semantics/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
```k
requires "test/configuration.md"
requires "test/execution.md"
module RUST-TEST
imports RUST-EXECUTION-TEST
endmodule
```
12 changes: 12 additions & 0 deletions rust-semantics/test/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
```k
module RUST-EXECUTION-TEST-CONFIGURATION
imports LIST
configuration
<rust-test>
<test-stack> .List </test-stack>
</rust-test>
endmodule
```
Loading

0 comments on commit 6db7c6f

Please sign in to comment.