Skip to content

Commit

Permalink
VMGen: Overhaul the instruction generation system
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Sep 22, 2024
1 parent a9587a2 commit d279ac0
Show file tree
Hide file tree
Showing 27 changed files with 543 additions and 505 deletions.
28 changes: 14 additions & 14 deletions Sources/WasmKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ add_wasmkit_library(WasmKit
Execution/Instructions/InstructionSupport.swift
Execution/Instructions/Numeric.swift
Execution/Instructions/Variable.swift
Execution/Types/Instances.swift
Execution/Types/Errors.swift
Execution/Types/Value.swift
Execution/Types/UntypedValue.swift
Execution/Runtime/InstDispatch.swift
Execution/Runtime/Runtime.swift
Execution/Runtime/RuntimeInterceptor.swift
Execution/Runtime/ExecutionState.swift
Execution/Runtime/Profiler.swift
Execution/Runtime/NameRegistry.swift
Execution/Runtime/Store.swift
Execution/Runtime/StoreAllocator.swift
Execution/Runtime/SignpostTracer.swift
Execution/Runtime/Function.swift
Execution/DispatchInstruction.swift
Execution/Errors.swift
Execution/Execution.swift
Execution/Function.swift
Execution/Instances.swift
Execution/NameRegistry.swift
Execution/Profiler.swift
Execution/Runtime.swift
Execution/RuntimeInterceptor.swift
Execution/SignpostTracer.swift
Execution/Store.swift
Execution/StoreAllocator.swift
Execution/UntypedValue.swift
Execution/Value.swift
)

target_link_wasmkit_libraries(WasmKit PUBLIC
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// This file is generated by Utilities/generate_inst_dispatch.swift
extension ExecutionState {
//// Automatically generated by Utilities/Sources/VMGen.swift
//// DO NOT EDIT DIRECTLY
extension Execution {
@inline(__always)
mutating func doExecute(_ instruction: UInt64, sp: inout Sp, pc: inout Pc, md: inout Md, ms: inout Ms) throws {
switch instruction {
Expand Down Expand Up @@ -409,7 +410,7 @@ extension Instruction {
}


extension ExecutionState {
extension Execution {
@inline(__always) mutating func i32Add(sp: Sp, binaryOperand: Instruction.BinaryOperand) {
sp[i32: binaryOperand.result] = sp[i32: binaryOperand.lhs].add(sp[i32: binaryOperand.rhs])
}
Expand Down Expand Up @@ -890,7 +891,7 @@ extension ExecutionState {
}


extension ExecutionState {
extension Execution {
@_silgen_name("wasmkit_execute_copyStack") @inline(__always)
mutating func execute_copyStack(sp: UnsafeMutablePointer<Sp>, pc: UnsafeMutablePointer<Pc>, md: UnsafeMutablePointer<Md>, ms: UnsafeMutablePointer<Ms>) {
let copyStackOperand = Instruction.CopyStackOperand.load(from: &pc.pointee)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import _CWasmKit

/// An execution state of an invocation of exported function.
///
/// Each new invocation through exported function has a separate ``ExecutionState``
/// Each new invocation through exported function has a separate ``Execution``
/// even though the invocation happens during another invocation.
struct ExecutionState {
struct Execution {
/// The reference to the ``Runtime`` associated with the execution.
let runtime: RuntimeRef
/// The end of the VM stack space.
Expand All @@ -17,14 +17,14 @@ struct ExecutionState {
/// the given ``Runtime`` instance.
static func with<T>(
runtime: RuntimeRef,
body: (inout ExecutionState, Sp) throws -> T
body: (inout Execution, Sp) throws -> T
) rethrows -> T {
let limit = Int(UInt16.max)
let valueStack = UnsafeMutablePointer<StackSlot>.allocate(capacity: limit)
defer {
valueStack.deallocate()
}
var context = ExecutionState(runtime: runtime, stackEnd: valueStack.advanced(by: limit))
var context = Execution(runtime: runtime, stackEnd: valueStack.advanced(by: limit))
return try body(&context, valueStack)
}

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

extension ExecutionState {
extension Execution {
/// A namespace for the "current memory" (Md and Ms) management.
enum CurrentMemory {
/// Assigns the current memory to the given internal memory.
Expand Down Expand Up @@ -309,7 +309,7 @@ extension ExecutionState {
returnPC: pc,
spAddend: callLike.spAddend
)
ExecutionState.CurrentMemory.mayUpdateCurrentInstance(
Execution.CurrentMemory.mayUpdateCurrentInstance(
instance: function.instance,
from: callerInstance, md: &md, ms: &ms
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ struct WasmFunctionEntity {
self.index = index
}

mutating func ensureCompiled(context: inout ExecutionState) throws -> InstructionSequence {
mutating func ensureCompiled(context: inout Execution) throws -> InstructionSequence {
try ensureCompiled(runtime: context.runtime)
}

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion Sources/WasmKit/Execution/Instructions/Control.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// > Note:
/// <https://webassembly.github.io/spec/core/exec/instructions.html#control-instructions>
extension ExecutionState {
extension Execution {
func unreachable(sp: Sp, pc: Pc) throws -> Pc {
throw Trap.unreachable
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/WasmKit/Execution/Instructions/Instruction.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//// Automatically generated by Utilities/Sources/VMGen.swift
//// DO NOT EDIT DIRECTLY
enum Instruction: Equatable {
case copyStack(Instruction.CopyStackOperand)
case globalGet(Instruction.GlobalGetOperand)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ extension Int32: InstructionImmediate {
}

extension Instruction {
/// size = 6, alignment = 2
struct BinaryOperand: Equatable, InstructionImmediate {
let result: LVReg
let lhs: VReg
Expand All @@ -94,7 +93,6 @@ extension Instruction {
}
}

/// size = 4, alignment = 2
struct UnaryOperand: Equatable, InstructionImmediate {
let result: LVReg
let input: LVReg
Expand Down Expand Up @@ -125,7 +123,6 @@ extension Instruction {
}
}

/// size = 4, alignment = 8
struct LoadOperand: Equatable, InstructionImmediate {
let offset: UInt64
let pointer: VReg
Expand Down
2 changes: 1 addition & 1 deletion Sources/WasmKit/Execution/Instructions/Memory.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// > Note:
/// <https://webassembly.github.io/spec/core/exec/instructions.html#memory-instructions>
extension ExecutionState {
extension Execution {
@inline(never) func throwOutOfBoundsMemoryAccess() throws -> Never {
throw Trap.outOfBoundsMemoryAccess
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/WasmKit/Execution/Instructions/Numeric.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// > Note:
/// <https://webassembly.github.io/spec/core/exec/instructions.html#numeric-instructions>
extension ExecutionState {
extension Execution {
@inline(__always)
mutating func const32(sp: Sp, const32Operand: Instruction.Const32Operand) {
sp[const32Operand.result] = UntypedValue(storage32: const32Operand.value)
Expand Down
2 changes: 1 addition & 1 deletion Sources/WasmKit/Execution/Instructions/Parametric.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// > Note:
/// <https://webassembly.github.io/spec/core/exec/instructions.html#parametric-instructions>
extension ExecutionState {
extension Execution {
mutating func select(sp: Sp, selectOperand: Instruction.SelectOperand) {
let flag = sp[i32: selectOperand.condition]
let selected = flag != 0 ? selectOperand.onTrue : selectOperand.onFalse
Expand Down
2 changes: 1 addition & 1 deletion Sources/WasmKit/Execution/Instructions/Reference.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// > Note:
/// <https://webassembly.github.io/spec/core/exec/instructions.html#reference-instructions>
extension ExecutionState {
extension Execution {
mutating func refNull(sp: Sp, refNullOperand: Instruction.RefNullOperand) {
let value: Value
switch refNullOperand.type {
Expand Down
4 changes: 2 additions & 2 deletions Sources/WasmKit/Execution/Instructions/Table.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// <https://webassembly.github.io/spec/core/exec/instructions.html#table-instructions>

import WasmParser
extension ExecutionState {
extension Execution {
mutating func tableGet(sp: Sp, tableGetOperand: Instruction.TableGetOperand) throws {
let runtime = runtime.value
let table = getTable(tableGetOperand.tableIndex, sp: sp, store: runtime.store)
Expand Down Expand Up @@ -131,7 +131,7 @@ extension ExecutionState {
}
}

extension ExecutionState {
extension Execution {
fileprivate func getTable(_ tableIndex: UInt32, sp: Sp, store: Store) -> InternalTable {
return currentInstance(sp: sp).tables[Int(tableIndex)]
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/WasmKit/Execution/Instructions/Variable.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// > Note:
/// <https://webassembly.github.io/spec/core/syntax/instructions.html#variable-instructions>
extension ExecutionState {
extension Execution {
mutating func globalGet(sp: Sp, globalGetOperand: Instruction.GlobalGetOperand) {
globalGetOperand.global.withValue{
sp[globalGetOperand.reg] = $0.rawValue
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ extension Runtime {
}

extension Runtime {
@available(*, unavailable, message: "Runtime doesn't manage execution state anymore. Use ExecutionState.step instead")
@available(*, unavailable, message: "Runtime doesn't manage execution state anymore. Use Execution.step instead")
public func step() throws {
fatalError()
}

@available(*, unavailable, message: "Runtime doesn't manage execution state anymore. Use ExecutionState.step instead")
@available(*, unavailable, message: "Runtime doesn't manage execution state anymore. Use Execution.step instead")
public func run() throws {
fatalError()
}
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions Sources/_CWasmKit/include/DirectThreadedCode.inc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//// Automatically generated by Utilities/Sources/VMGen.swift
//// DO NOT EDIT DIRECTLY
SWIFT_CC(swiftasync) static inline void wasmkit_tc_copyStack(Sp sp, Pc pc, Md md, Ms ms, SWIFT_CONTEXT void *state) {
SWIFT_CC(swift) void wasmkit_execute_copyStack(Sp *sp, Pc *pc, Md *md, Ms *ms, SWIFT_CONTEXT void *state, SWIFT_ERROR_RESULT void **error);
void * _Nullable error = NULL;
Expand Down
Loading

0 comments on commit d279ac0

Please sign in to comment.