@@ -8,15 +8,15 @@ requires "kmir-ast.md"
8
8
9
9
The MIR syntax is largely defined in [ KMIR-AST] ( ./kmir-ast.md ) and its
10
10
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 .
12
12
13
13
``` k
14
14
module KMIR-SYNTAX
15
15
imports KMIR-AST
16
16
imports INT-SYNTAX
17
17
imports FLOAT-SYNTAX
18
18
19
- syntax KItem ::= #init( Pgm )
19
+ syntax KItem ::= #init( Pgm, Symbol )
20
20
21
21
////////////////////////////////////////////
22
22
// FIXME things below related to memory and
@@ -73,7 +73,7 @@ module KMIR-CONFIGURATION
73
73
locals:List) // return val, args, local variables
74
74
75
75
configuration <kmir>
76
- <k> #init($PGM:Pgm) </k>
76
+ <k> #init($PGM:Pgm, symbol("main") ) </k>
77
77
<retVal> NoValue </retVal>
78
78
<currentFunc> ty(-1) </currentFunc> // to retrieve caller
79
79
// 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.
116
116
117
117
``` k
118
118
// #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 )
120
120
=>
121
- #execMain(#findMainItem (Items)) // TODO could execute a different function
121
+ #execFunction(#findItem (Items, FuncName), Functions)
122
122
</k>
123
123
<functions> _ => #mkFunctionMap(Functions, Items) </functions>
124
124
```
@@ -138,11 +138,10 @@ The function _names_ and _ids_ are not relevant for calls and therefore dropped.
138
138
rule #mkFunctionMap(Functions, Items)
139
139
=>
140
140
#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)
143
142
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
+ ////////////////////////////////////////////////////////////////
146
145
147
146
// accumulate map of symbol_name -> function (MonoItemFn), discarding duplicate IDs
148
147
rule #accumItems(Acc, .MonoItems) => Acc
@@ -181,42 +180,57 @@ The function _names_ and _ids_ are not relevant for calls and therefore dropped.
181
180
182
181
```
183
182
184
- Executing the ` main ` function means to create the ` currentFrame ` data
183
+ Executing a given named function means to create the ` currentFrame ` data
185
184
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.
187
187
188
188
``` 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 ]
191
191
192
- rule #findMainItem( monoItem(_, monoItemFn(NAME, ID, BODY )) _ )
192
+ rule #findItem( ( monoItem(_, monoItemFn(N, _, _ )) #as ITEM) _REST, NAME )
193
193
=>
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 )
197
197
=>
198
- #findMainItem(Rest )
198
+ #findItem(REST, NAME )
199
199
[owise]
200
- // rule #findMainItem ( .MonoItems ) => error!
200
+ // rule #findItem ( .MonoItems, _NAME ) => error!
201
201
202
- syntax KItem ::= #execMain ( MonoItemKind )
202
+ syntax KItem ::= #execFunction ( MonoItem, FunctionNames )
203
203
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
+ )
206
211
=>
207
212
#execBlock(FIRST)
208
213
...
209
214
</k>
210
- <currentFunc> _ => ty(-1 ) </currentFunc> // Index of main is not known
215
+ <currentFunc> _ => #tyFromName(SYMNAME, FUNCTIONNAMES ) </currentFunc>
211
216
<currentFrame>
212
217
<currentBody> _ => toKList(BLOCKS) </currentBody>
213
- <caller> _ => ty(-1) </caller> // no caller of main
218
+ <caller> _ => ty(-1) </caller> // no caller
214
219
<dest> _ => place(local(-1), .ProjectionElems)</dest>
215
220
<target> _ => noBasicBlockIdx </target>
216
- <unwind> _ => unwindActionUnreachable </unwind> // FIXME
221
+ <unwind> _ => unwindActionUnreachable </unwind>
217
222
<locals> _ => #reserveFor(LOCALS) </locals>
218
223
</currentFrame>
219
224
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
+
220
234
syntax List ::= toKList(BasicBlocks) [function, total]
221
235
222
236
rule toKList( .BasicBlocks ) => .List
0 commit comments