Skip to content

Commit 01a575c

Browse files
gtreptarv-auditor
andauthored
--start-symbol for kmir run (#446)
This adds a `<start-symbol>` cell to the configuration with a configuration variable for the symbol to begin execution from. It also changes the expected output in the `exec-smir` integration tests to only check the contents of the K cell. The semantics are going to be very unstable right now, and without this change we will probably be spending time updating the test vectors on nearly every pull request. --------- Co-authored-by: devops <[email protected]>
1 parent b5adfdc commit 01a575c

File tree

10 files changed

+51
-22
lines changed

10 files changed

+51
-22
lines changed

kmir/Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,9 @@ SRC_FILES := $(shell find src -type f -name '*.py')
9090

9191
pyupgrade: poetry-install
9292
$(POETRY_RUN) pyupgrade --py310-plus $(SRC_FILES)
93+
94+
# Update smir exec tests
95+
96+
.PHONY: update-exec-smir
97+
update-exec-smir: poetry-install
98+
UPDATE_EXEC_SMIR=true $(POETRY_RUN) pytest -k test_exec_smir

kmir/pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
44

55
[tool.poetry]
66
name = "kmir"
7-
version = "0.3.70"
7+
version = "0.3.71"
88
description = ""
99
authors = [
1010
"Runtime Verification, Inc. <[email protected]>",

kmir/src/kmir/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from typing import Final
22

3-
VERSION: Final = '0.3.70'
3+
VERSION: Final = '0.3.71'

kmir/src/kmir/__main__.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from pathlib import Path
77
from typing import TYPE_CHECKING
88

9-
from pyk.kast.inner import KSort, Subst
10-
119
from kmir.build import semantics
1210
from kmir.convert_from_definition.v2parser import parse_json
1311

@@ -23,6 +21,7 @@ class KMirOpts: ...
2321
class RunOpts(KMirOpts):
2422
input_file: Path
2523
depth: int
24+
start_symbol: str
2625

2726

2827
def _kmir_run(opts: RunOpts) -> None:
@@ -35,10 +34,7 @@ def _kmir_run(opts: RunOpts) -> None:
3534

3635
kmir_kast, _ = parse_result
3736

38-
subst = Subst({'$PGM': kmir_kast})
39-
init_config = subst.apply(tools.definition.init_config(KSort('GeneratedTopCell')))
40-
init_kore = tools.krun.kast_to_kore(init_config, KSort('GeneratedTopCell'))
41-
result = tools.krun.run_pattern(init_kore, depth=opts.depth)
37+
result = tools.run_parsed(kmir_kast, opts.start_symbol, opts.depth)
4238

4339
print(tools.kprint.kore_to_pretty(result))
4440

@@ -60,6 +56,9 @@ def _arg_parser() -> ArgumentParser:
6056
run_parser = command_parser.add_parser('run', help='run stable MIR programs')
6157
run_parser.add_argument('input_file', metavar='FILE', help='MIR program to run')
6258
run_parser.add_argument('--depth', type=int, metavar='DEPTH', help='Depth to execute')
59+
run_parser.add_argument(
60+
'--start-symbol', type=str, metavar='SYMBOL', default='main', help='Symbol name to begin execution from'
61+
)
6362

6463
return parser
6564

@@ -69,7 +68,7 @@ def _parse_args(args: Sequence[str]) -> KMirOpts:
6968

7069
match ns.command:
7170
case 'run':
72-
return RunOpts(input_file=Path(ns.input_file).resolve(), depth=ns.depth)
71+
return RunOpts(input_file=Path(ns.input_file).resolve(), depth=ns.depth, start_symbol=ns.start_symbol)
7372
case _:
7473
raise AssertionError()
7574

kmir/src/kmir/kdist/mir-semantics/kmir.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module KMIR-SYNTAX
1616
imports INT-SYNTAX
1717
imports FLOAT-SYNTAX
1818
19-
syntax KItem ::= #init( Pgm, Symbol )
19+
syntax KItem ::= #init( Pgm )
2020
2121
////////////////////////////////////////////
2222
// FIXME things below related to memory and
@@ -73,7 +73,7 @@ module KMIR-CONFIGURATION
7373
locals:List) // return val, args, local variables
7474
7575
configuration <kmir>
76-
<k> #init($PGM:Pgm, symbol("main")) </k>
76+
<k> #init($PGM:Pgm) </k>
7777
<retVal> NoValue </retVal>
7878
<currentFunc> ty(-1) </currentFunc> // to retrieve caller
7979
// unpacking the top frame to avoid frequent stack read/write operations
@@ -92,6 +92,7 @@ module KMIR-CONFIGURATION
9292
// heap
9393
<memory> .Map </memory> // FIXME unclear how to model
9494
// FIXME where do we put the "GlobalAllocs"? in the heap, presumably?
95+
<start-symbol> symbol($STARTSYM:String) </start-symbol>
9596
</kmir>
9697
endmodule
9798
```
@@ -116,11 +117,12 @@ function map and the initial memory have to be set up.
116117

117118
```k
118119
// #init step, assuming a singleton in the K cell
119-
rule <k> #init(_Name:Symbol _Allocs:GlobalAllocs Functions:FunctionNames Items:MonoItems, FuncName)
120+
rule <k> #init(_Name:Symbol _Allocs:GlobalAllocs Functions:FunctionNames Items:MonoItems)
120121
=>
121122
#execFunction(#findItem(Items, FuncName), Functions)
122123
</k>
123124
<functions> _ => #mkFunctionMap(Functions, Items) </functions>
125+
<start-symbol> FuncName </start-symbol>
124126
```
125127

126128
The `Map` of `functions` is constructed from the lookup table of `FunctionNames`,

kmir/src/kmir/tools.py

+15
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
from typing import TYPE_CHECKING
44

5+
from pyk.kast.inner import KSort, Subst
56
from pyk.kast.outer import read_kast_definition
67
from pyk.ktool.krun import KRun
8+
from pyk.prelude.string import stringToken
79

810
from .kparse import KParse
911

1012
if TYPE_CHECKING:
1113
from pathlib import Path
1214

15+
from pyk.kast import KInner
1316
from pyk.kast.outer import KDefinition
17+
from pyk.kore.syntax import Pattern
1418
from pyk.ktool.kprint import KPrint
1519

1620

@@ -42,3 +46,14 @@ def krun(self) -> KRun:
4246
@property
4347
def definition(self) -> KDefinition:
4448
return self.__definition
49+
50+
def run_parsed(self, parsed_smir: KInner, start_symbol: KInner | str = 'main', depth: int | None = None) -> Pattern:
51+
if isinstance(start_symbol, str):
52+
start_symbol = stringToken(start_symbol)
53+
54+
subst = Subst({'$PGM': parsed_smir, '$STARTSYM': start_symbol})
55+
init_config = subst.apply(self.definition.init_config(KSort('GeneratedTopCell')))
56+
init_kore = self.krun.kast_to_kore(init_config, KSort('GeneratedTopCell'))
57+
result = self.krun.run_pattern(init_kore, depth=depth)
58+
59+
return result

kmir/src/tests/integration/data/exec-smir/main-a-b-c/main-a-b-c.15.state

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,7 @@
4747
<memory>
4848
.Map
4949
</memory>
50-
</kmir>
51-
50+
<start-symbol>
51+
symbol ( "main" )
52+
</start-symbol>
53+
</kmir>

kmir/src/tests/integration/data/exec-smir/main-a-b-c/main-a-b-c.run.state

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,7 @@
4646
<memory>
4747
.Map
4848
</memory>
49-
</kmir>
49+
<start-symbol>
50+
symbol ( "main" )
51+
</start-symbol>
52+
</kmir>

kmir/src/tests/integration/test_integration.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from __future__ import annotations
22

33
import json
4+
import os
45
from pathlib import Path
56
from typing import TYPE_CHECKING
67

78
import pytest
8-
from pyk.kast.inner import KApply, KSort, KToken, Subst
9+
from pyk.kast.inner import KApply, KSort, KToken
910

1011
from kmir.convert_from_definition.v2parser import Parser
1112

@@ -187,14 +188,15 @@ def test_exec_smir(
187188
assert parsed is not None
188189
kmir_kast, _ = parsed
189190

190-
subst = Subst({'$PGM': kmir_kast})
191-
init_config = subst.apply(tools.definition.init_config(KSort('GeneratedTopCell')))
192-
init_kore = tools.krun.kast_to_kore(init_config, KSort('GeneratedTopCell'))
193-
result = tools.krun.run_pattern(init_kore, depth=depth)
191+
result = tools.run_parsed(kmir_kast, depth=depth)
194192

195193
with output_kast.open('r') as f:
196194
expected = f.read().rstrip()
197195

198196
result_pretty = tools.kprint.kore_to_pretty(result).rstrip()
199197

200-
assert result_pretty == expected
198+
if os.getenv('UPDATE_EXEC_SMIR') is None:
199+
assert result_pretty == expected
200+
else:
201+
with output_kast.open('w') as f:
202+
f.write(result_pretty)

package/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.3.70
1+
0.3.71

0 commit comments

Comments
 (0)