Skip to content

Commit 5d938c5

Browse files
VMGen: Overhaul the instruction generation system
1 parent 0d23ada commit 5d938c5

27 files changed

+543
-505
lines changed

Sources/WasmKit/CMakeLists.txt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ add_wasmkit_library(WasmKit
1818
Execution/Instructions/InstructionSupport.swift
1919
Execution/Instructions/Numeric.swift
2020
Execution/Instructions/Variable.swift
21-
Execution/Types/Instances.swift
22-
Execution/Types/Errors.swift
23-
Execution/Types/Value.swift
24-
Execution/Types/UntypedValue.swift
25-
Execution/Runtime/InstDispatch.swift
26-
Execution/Runtime/Runtime.swift
27-
Execution/Runtime/RuntimeInterceptor.swift
28-
Execution/Runtime/ExecutionState.swift
29-
Execution/Runtime/Profiler.swift
30-
Execution/Runtime/NameRegistry.swift
31-
Execution/Runtime/Store.swift
32-
Execution/Runtime/StoreAllocator.swift
33-
Execution/Runtime/SignpostTracer.swift
34-
Execution/Runtime/Function.swift
21+
Execution/DispatchInstruction.swift
22+
Execution/Errors.swift
23+
Execution/Execution.swift
24+
Execution/Function.swift
25+
Execution/Instances.swift
26+
Execution/NameRegistry.swift
27+
Execution/Profiler.swift
28+
Execution/Runtime.swift
29+
Execution/RuntimeInterceptor.swift
30+
Execution/SignpostTracer.swift
31+
Execution/Store.swift
32+
Execution/StoreAllocator.swift
33+
Execution/UntypedValue.swift
34+
Execution/Value.swift
3535
)
3636

3737
target_link_wasmkit_libraries(WasmKit PUBLIC

Sources/WasmKit/Execution/Runtime/InstDispatch.swift renamed to Sources/WasmKit/Execution/DispatchInstruction.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// This file is generated by Utilities/generate_inst_dispatch.swift
2-
extension ExecutionState {
1+
//// Automatically generated by Utilities/Sources/VMGen.swift
2+
//// DO NOT EDIT DIRECTLY
3+
extension Execution {
34
@inline(__always)
45
mutating func doExecute(_ instruction: UInt64, sp: inout Sp, pc: inout Pc, md: inout Md, ms: inout Ms) throws {
56
switch instruction {
@@ -409,7 +410,7 @@ extension Instruction {
409410
}
410411

411412

412-
extension ExecutionState {
413+
extension Execution {
413414
@inline(__always) mutating func i32Add(sp: Sp, binaryOperand: Instruction.BinaryOperand) {
414415
sp[i32: binaryOperand.result] = sp[i32: binaryOperand.lhs].add(sp[i32: binaryOperand.rhs])
415416
}
@@ -890,7 +891,7 @@ extension ExecutionState {
890891
}
891892

892893

893-
extension ExecutionState {
894+
extension Execution {
894895
@_silgen_name("wasmkit_execute_copyStack") @inline(__always)
895896
mutating func execute_copyStack(sp: UnsafeMutablePointer<Sp>, pc: UnsafeMutablePointer<Pc>, md: UnsafeMutablePointer<Md>, ms: UnsafeMutablePointer<Ms>) {
896897
let copyStackOperand = Instruction.CopyStackOperand.load(from: &pc.pointee)

Sources/WasmKit/Execution/Runtime/ExecutionState.swift renamed to Sources/WasmKit/Execution/Execution.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import _CWasmKit
22

33
/// An execution state of an invocation of exported function.
44
///
5-
/// Each new invocation through exported function has a separate ``ExecutionState``
5+
/// Each new invocation through exported function has a separate ``Execution``
66
/// even though the invocation happens during another invocation.
7-
struct ExecutionState {
7+
struct Execution {
88
/// The reference to the ``Runtime`` associated with the execution.
99
let runtime: RuntimeRef
1010
/// The end of the VM stack space.
@@ -17,14 +17,14 @@ struct ExecutionState {
1717
/// the given ``Runtime`` instance.
1818
static func with<T>(
1919
runtime: RuntimeRef,
20-
body: (inout ExecutionState, Sp) throws -> T
20+
body: (inout Execution, Sp) throws -> T
2121
) rethrows -> T {
2222
let limit = Int(UInt16.max)
2323
let valueStack = UnsafeMutablePointer<StackSlot>.allocate(capacity: limit)
2424
defer {
2525
valueStack.deallocate()
2626
}
27-
var context = ExecutionState(runtime: runtime, stackEnd: valueStack.advanced(by: limit))
27+
var context = Execution(runtime: runtime, stackEnd: valueStack.advanced(by: limit))
2828
return try body(&context, valueStack)
2929
}
3030

@@ -136,7 +136,7 @@ func executeWasm(
136136
) throws -> [Value] {
137137
// NOTE: `runtime` variable must not outlive this function
138138
let runtime = RuntimeRef(runtime)
139-
return try ExecutionState.with(runtime: runtime) { (stack, sp) in
139+
return try Execution.with(runtime: runtime) { (stack, sp) in
140140
// Advance the stack pointer to be able to reference negative indices
141141
// for saving slots.
142142
let sp = sp.advanced(by: FrameHeaderLayout.numberOfSavingSlots)
@@ -164,7 +164,7 @@ func executeWasm(
164164
}
165165
}
166166

167-
extension ExecutionState {
167+
extension Execution {
168168
/// A namespace for the "current memory" (Md and Ms) management.
169169
enum CurrentMemory {
170170
/// Assigns the current memory to the given internal memory.
@@ -309,7 +309,7 @@ extension ExecutionState {
309309
returnPC: pc,
310310
spAddend: callLike.spAddend
311311
)
312-
ExecutionState.CurrentMemory.mayUpdateCurrentInstance(
312+
Execution.CurrentMemory.mayUpdateCurrentInstance(
313313
instance: function.instance,
314314
from: callerInstance, md: &md, ms: &ms
315315
)

Sources/WasmKit/Execution/Runtime/Function.swift renamed to Sources/WasmKit/Execution/Function.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ struct WasmFunctionEntity {
165165
self.index = index
166166
}
167167

168-
mutating func ensureCompiled(context: inout ExecutionState) throws -> InstructionSequence {
168+
mutating func ensureCompiled(context: inout Execution) throws -> InstructionSequence {
169169
try ensureCompiled(runtime: context.runtime)
170170
}
171171

Sources/WasmKit/Execution/Instructions/Control.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// > Note:
22
/// <https://webassembly.github.io/spec/core/exec/instructions.html#control-instructions>
3-
extension ExecutionState {
3+
extension Execution {
44
func unreachable(sp: Sp, pc: Pc) throws -> Pc {
55
throw Trap.unreachable
66
}

Sources/WasmKit/Execution/Instructions/Instruction.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//// Automatically generated by Utilities/Sources/VMGen.swift
2+
//// DO NOT EDIT DIRECTLY
13
enum Instruction: Equatable {
24
case copyStack(Instruction.CopyStackOperand)
35
case globalGet(Instruction.GlobalGetOperand)

Sources/WasmKit/Execution/Instructions/InstructionSupport.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ extension Int32: InstructionImmediate {
8181
}
8282

8383
extension Instruction {
84-
/// size = 6, alignment = 2
8584
struct BinaryOperand: Equatable, InstructionImmediate {
8685
let result: LVReg
8786
let lhs: VReg
@@ -94,7 +93,6 @@ extension Instruction {
9493
}
9594
}
9695

97-
/// size = 4, alignment = 2
9896
struct UnaryOperand: Equatable, InstructionImmediate {
9997
let result: LVReg
10098
let input: LVReg
@@ -125,7 +123,6 @@ extension Instruction {
125123
}
126124
}
127125

128-
/// size = 4, alignment = 8
129126
struct LoadOperand: Equatable, InstructionImmediate {
130127
let offset: UInt64
131128
let pointer: VReg

Sources/WasmKit/Execution/Instructions/Memory.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// > Note:
22
/// <https://webassembly.github.io/spec/core/exec/instructions.html#memory-instructions>
3-
extension ExecutionState {
3+
extension Execution {
44
@inline(never) func throwOutOfBoundsMemoryAccess() throws -> Never {
55
throw Trap.outOfBoundsMemoryAccess
66
}

0 commit comments

Comments
 (0)