Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook implementations and hook lookup fixes #45

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 88 additions & 6 deletions pykwasm/src/pykwasm/kdist/wasm-semantics/ulm-wasm.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,112 @@ infer that this must be the top configuration, so it will wrap it in
A special configuration cell is added in the local case to support VM initialization.

```local
<entry> $ENTRY:String </entry>
<entry> $ENTRY:WasmString </entry>
```


```k
</ulmWasm>
```

Implementing all unresolved imports as hooks
--------------------------------------------

```k

syntax String ::= wasmString2StringStripped ( WasmString ) [function]
| #stripQuotes ( String ) [function]
// ----------------------------------------------------
rule wasmString2StringStripped(WS) => #stripQuotes(#parseWasmString(WS))

rule #stripQuotes(S) => replaceAll(S, "\"", "")

syntax IdentifierToken ::= String2Identifier(String) [function, total, hook(STRING.string2token)]

```

First, we create an empty module for any import referencing a non-existing module.

```k

rule
<instrs> #import(MOD, _, _) ... </instrs>
<moduleRegistry> MR:Map => MR [ MOD <- NEXT ] </moduleRegistry>
<nextModuleIdx> NEXT => NEXT +Int 1 </nextModuleIdx>
<moduleInstances> ( .Bag => <moduleInst> <modIdx> NEXT </modIdx> ... </moduleInst>) ... </moduleInstances>
requires notBool MOD in_keys(MR)

```

Next, for each import referencing a non-existing function, we add a function
that just invokes a (non-wasm) `hostCall` instruction.

```k

syntax Instr ::= hostCall(String, String, FuncType)
// ---------------------------------------------------
rule
<instrs>
( .K
=> allocfunc
( HOSTMOD, NEXTADDR, TYPE, [ .ValTypes ]
, hostCall
( wasmString2StringStripped(MOD)
, wasmString2StringStripped(NAME)
, TYPE
)
.Instrs
, #meta
(... id: String2Identifier
( "$auto-alloc:"
+String #parseWasmString(MOD)
+String ":"
+String #parseWasmString(NAME)
)
, localIds: .Map
)
)
)
~> #import(MOD, NAME, #funcDesc(... type: TIDX))
...
</instrs>
<curModIdx> CUR </curModIdx>
<moduleInst>
<modIdx> CUR </modIdx>
<types> ... TIDX |-> TYPE ... </types>
...
</moduleInst>
<nextFuncAddr> NEXTADDR => NEXTADDR +Int 1 </nextFuncAddr>
<moduleRegistry> ... MOD |-> HOSTMOD ... </moduleRegistry>
<moduleInst>
<modIdx> HOSTMOD </modIdx>
<exports> EXPORTS => EXPORTS [NAME <- NEXTFUNC ] </exports>
<funcAddrs> FS => setExtend(FS, NEXTFUNC, NEXTADDR, -1) </funcAddrs>
<nextFuncIdx> NEXTFUNC => NEXTFUNC +Int 1 </nextFuncIdx>
<nextTypeIdx> NEXTTYPE => NEXTTYPE +Int 1 </nextTypeIdx>
<types> TYPES => TYPES [ NEXTTYPE <- TYPE ] </types>
...
</moduleInst>
requires notBool NAME in_keys(EXPORTS)

```

Obtaining the Entrypoint
------------------------

In the standalone semantics, the Wasm VM obtains an entrypoint from the configuration.

```local
syntax String ::= #getEntryPoint() [function, total]
syntax WasmString ::= #getEntryPoint() [function, total]
rule #getEntryPoint() => FUNCNAME
[[ <entry> FUNCNAME </entry> ]]
```

In the remote semantics, the Wasm VM has a fixed entrypoint.

```remote
syntax String ::= #getEntryPoint() [function, total]
rule #getEntryPoint() => "ulmDispatchCaller"
syntax WasmString ::= #getEntryPoint() [function, total]
rule #getEntryPoint() => #token("\"ulmDispatchCaller\"", "WasmStringToken")
```

Passing Control
Expand All @@ -123,8 +205,8 @@ Note that entrypoint resolution must occur _after_ the Wasm module has been load
This is ensured by requiring that the `<instrs>` cell is empty during resolution.

```k
syntax Initializer ::= #resolveCurModuleFuncExport(String)
| #resolveModuleFuncExport(Int, String)
syntax Initializer ::= #resolveCurModuleFuncExport(WasmString)
| #resolveModuleFuncExport(Int, WasmString)
| #resolveFunc(Int, ListInt)
// ----------------------------------------------
rule <k> #resolveCurModuleFuncExport(FUNCNAME) => #resolveModuleFuncExport(MODIDX, FUNCNAME) </k>
Expand Down
Loading