Skip to content

Commit 0eb69a5

Browse files
committed
Generalise entry point to an arbitrary named function item
1 parent c8c6f7f commit 0eb69a5

File tree

1 file changed

+39
-25
lines changed
  • kmir/src/kmir/kdist/mir-semantics

1 file changed

+39
-25
lines changed

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

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ requires "kmir-ast.md"
88

99
The MIR syntax is largely defined in [KMIR-AST](./kmir-ast.md) and its
1010
submodules. The execution is initialised based on a loaded `Pgm` read
11-
from a json format of stable-MIR.
11+
from a json format of stable-MIR, and the name of the function to execute.
1212

1313
```k
1414
module KMIR-SYNTAX
1515
imports KMIR-AST
1616
imports INT-SYNTAX
1717
imports FLOAT-SYNTAX
1818
19-
syntax KItem ::= #init( Pgm )
19+
syntax KItem ::= #init( Pgm, Symbol )
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) </k>
76+
<k> #init($PGM:Pgm, symbol("main")) </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
@@ -116,9 +116,9 @@ function map and the initial memory have to be set up.
116116

117117
```k
118118
// #init step, assuming a singleton in the K cell
119-
rule <k> #init(_Name:Symbol _Allocs:GlobalAllocs Functions:FunctionNames Items:MonoItems)
119+
rule <k> #init(_Name:Symbol _Allocs:GlobalAllocs Functions:FunctionNames Items:MonoItems, FuncName)
120120
=>
121-
#execMain(#findMainItem(Items)) // TODO could execute a different function
121+
#execFunction(#findItem(Items, FuncName), Functions)
122122
</k>
123123
<functions> _ => #mkFunctionMap(Functions, Items) </functions>
124124
```
@@ -138,11 +138,10 @@ The function _names_ and _ids_ are not relevant for calls and therefore dropped.
138138
rule #mkFunctionMap(Functions, Items)
139139
=>
140140
#accumFunctions(#mainIsMinusOne(Items), #accumItems(.Map, Items), Functions)
141-
// ^^^^^^^^^^^^^^^^^^^^^^ Adds "main" as function with ty(-1)
142-
141+
//////////////////// ^^^^^^^^^^^^^^^^^^^^^^ HACK Adds "main" as function with ty(-1)
143142
syntax Map ::= #mainIsMinusOne(MonoItems) [function]
144-
145-
rule #mainIsMinusOne(ITEMS) => ty(-1) |-> #findMainItem(ITEMS)
143+
rule #mainIsMinusOne(ITEMS) => ty(-1) |-> #findItem(ITEMS, symbol("main"))
144+
////////////////////////////////////////////////////////////////
146145
147146
// accumulate map of symbol_name -> function (MonoItemFn), discarding duplicate IDs
148147
rule #accumItems(Acc, .MonoItems) => Acc
@@ -181,42 +180,57 @@ The function _names_ and _ids_ are not relevant for calls and therefore dropped.
181180
182181
```
183182

184-
Executing the `main` function means to create the `currentFrame` data
183+
Executing a given named function means to create the `currentFrame` data
185184
structure from its function body and then execute the first basic
186-
block of the body.
185+
block of the body. The function's `Ty` index in the `functions` map must
186+
be known to populate the `currentFunc` field.
187187

188188
```k
189-
// `main` is found through its MonoItemFn.name
190-
syntax MonoItemKind ::= #findMainItem ( MonoItems ) [ function ]
189+
// find function through its MonoItemFn.name
190+
syntax MonoItem ::= #findItem ( MonoItems, Symbol ) [ function ]
191191
192-
rule #findMainItem( monoItem(_, monoItemFn(NAME, ID, BODY)) _ )
192+
rule #findItem( (monoItem(_, monoItemFn(N, _, _)) #as ITEM) _REST, NAME )
193193
=>
194-
monoItemFn(NAME, ID, BODY)
195-
requires NAME ==K symbol("main")
196-
rule #findMainItem( _:MonoItem Rest:MonoItems )
194+
ITEM
195+
requires N ==K NAME
196+
rule #findItem( _:MonoItem REST:MonoItems, NAME )
197197
=>
198-
#findMainItem(Rest)
198+
#findItem(REST, NAME)
199199
[owise]
200-
// rule #findMainItem( .MonoItems ) => error!
200+
// rule #findItem( .MonoItems, _NAME) => error!
201201
202-
syntax KItem ::= #execMain ( MonoItemKind )
202+
syntax KItem ::= #execFunction ( MonoItem, FunctionNames )
203203
204-
// NB differs from arbitrary function execution only by not pushing a stack frame
205-
rule <k> #execMain(monoItemFn(_, _, body(FIRST:BasicBlock _ #as BLOCKS, LOCALS, _, _, _, _) .Bodies))
204+
rule <k> #execFunction(
205+
monoItem(
206+
SYMNAME,
207+
monoItemFn(_, _, body(FIRST:BasicBlock _ #as BLOCKS,LOCALS, _, _, _, _) .Bodies)
208+
),
209+
FUNCTIONNAMES
210+
)
206211
=>
207212
#execBlock(FIRST)
208213
...
209214
</k>
210-
<currentFunc> _ => ty(-1) </currentFunc> // Index of main is not known
215+
<currentFunc> _ => #tyFromName(SYMNAME, FUNCTIONNAMES) </currentFunc>
211216
<currentFrame>
212217
<currentBody> _ => toKList(BLOCKS) </currentBody>
213-
<caller> _ => ty(-1) </caller> // no caller of main
218+
<caller> _ => ty(-1) </caller> // no caller
214219
<dest> _ => place(local(-1), .ProjectionElems)</dest>
215220
<target> _ => noBasicBlockIdx </target>
216-
<unwind> _ => unwindActionUnreachable </unwind> // FIXME
221+
<unwind> _ => unwindActionUnreachable </unwind>
217222
<locals> _ => #reserveFor(LOCALS) </locals>
218223
</currentFrame>
219224
225+
syntax Ty ::= #tyFromName( Symbol, FunctionNames ) [function]
226+
227+
rule #tyFromName(NAME, ListItem(functionName(TY, FNAME)) _) => TY
228+
requires NAME ==K FNAME
229+
rule #tyFromName(NAME, ListItem(_) REST:List) => #tyFromName(NAME, REST)
230+
[owise]
231+
232+
rule #tyFromName(_, .List) => ty(-1) // HACK see #mainIsMinusOne above
233+
220234
syntax List ::= toKList(BasicBlocks) [function, total]
221235
222236
rule toKList( .BasicBlocks ) => .List

0 commit comments

Comments
 (0)