From f620e6b916ee420c691c4414b0fac9975d080a9e Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 1 Jul 2024 09:25:00 +0900 Subject: [PATCH 1/2] Remove legacy types `WasmKit.BlockType` and `WasmKit.LegacyBrTable` `WasmKit.BlockType` is now replaced with `WasmParser.BlockType` and `WasmKit.LegacyBrTable` is now replaced with `Wasm.BrTable`. --- .../Execution/Instructions/Control.swift | 2 - .../Instructions/InstructionSupport.swift | 61 ------------------- Sources/WasmKit/Translator.swift | 6 -- 3 files changed, 69 deletions(-) diff --git a/Sources/WasmKit/Execution/Instructions/Control.swift b/Sources/WasmKit/Execution/Instructions/Control.swift index f9e82db4..f4cd7f37 100644 --- a/Sources/WasmKit/Execution/Instructions/Control.swift +++ b/Sources/WasmKit/Execution/Instructions/Control.swift @@ -8,8 +8,6 @@ extension ExecutionState { programCounter += 1 } - typealias BlockType = Instruction.BlockType - mutating func ifThen(runtime: Runtime, stack: inout Stack, elseOrEndRef: ExpressionRef) { let isTrue = stack.popValue().i32 != 0 if isTrue { diff --git a/Sources/WasmKit/Execution/Instructions/InstructionSupport.swift b/Sources/WasmKit/Execution/Instructions/InstructionSupport.swift index cf88fc84..160c1ab4 100644 --- a/Sources/WasmKit/Execution/Instructions/InstructionSupport.swift +++ b/Sources/WasmKit/Execution/Instructions/InstructionSupport.swift @@ -3,35 +3,6 @@ import WasmParser extension Instruction { typealias Memarg = MemArg - struct BlockType: Equatable { - let parameters: UInt16 - let results: UInt16 - - init(parameters: UInt16, results: UInt16) { - self.parameters = parameters - self.results = results - } - - init(blockType: WasmParser.BlockType, typeSection: [FunctionType]) throws { - switch blockType { - case .type: - self = Instruction.BlockType(parameters: 0, results: 1) - case .empty: - self = Instruction.BlockType(parameters: 0, results: 0) - case let .funcType(typeIndex): - let typeIndex = Int(typeIndex) - guard typeIndex < typeSection.count else { - throw WasmParserError.invalidTypeSectionReference - } - let funcType = typeSection[typeIndex] - self = Instruction.BlockType( - parameters: UInt16(funcType.parameters.count), - results: UInt16(funcType.results.count) - ) - } - } - } - struct BrTable: Equatable { struct Entry { var labelIndex: LabelIndex @@ -46,38 +17,6 @@ extension Instruction { } } - struct LegacyBrTable: Equatable { - private let bufferBase: UnsafePointer - private let bufferCount: UInt32 - var labelIndices: UnsafeBufferPointer { - UnsafeBufferPointer(start: bufferBase, count: Int(bufferCount - 1)) - } - var defaultIndex: LabelIndex { - bufferBase[Int(bufferCount - 1)] - } - - init(labelIndices: [LabelIndex], defaultIndex: LabelIndex) { - let buffer = UnsafeMutableBufferPointer.allocate(capacity: labelIndices.count + 1) - for (index, labelindex) in labelIndices.enumerated() { - buffer[index] = labelindex - } - buffer[labelIndices.count] = defaultIndex - self.bufferBase = UnsafePointer(buffer.baseAddress!) - self.bufferCount = UInt32(buffer.count) - } - - static func == (lhs: Instruction.LegacyBrTable, rhs: Instruction.LegacyBrTable) -> Bool { - lhs.labelIndices.baseAddress == rhs.labelIndices.baseAddress - } - } - - // Just for migration purpose - static func control(_ x: Instruction) -> Instruction { x } - static func numeric(_ x: Instruction) -> Instruction { x } - static func parametric(_ x: Instruction) -> Instruction { x } - static func variable(_ x: Instruction) -> Instruction { x } - static func reference(_ x: Instruction) -> Instruction { x } - static let f32Lt: Instruction = .numericFloatBinary(.lt(.f32)) static let f32Gt: Instruction = .numericFloatBinary(.gt(.f32)) static let f32Le: Instruction = .numericFloatBinary(.le(.f32)) diff --git a/Sources/WasmKit/Translator.swift b/Sources/WasmKit/Translator.swift index b4fbb1e2..5f3c657f 100644 --- a/Sources/WasmKit/Translator.swift +++ b/Sources/WasmKit/Translator.swift @@ -970,9 +970,3 @@ fileprivate extension FunctionType { } } } - -fileprivate extension Instruction.BlockType { - init(_ functionType: FunctionType) { - self.init(parameters: UInt16(functionType.parameters.count), results: UInt16(functionType.results.count)) - } -} From 39cef2f7d2580a824a58d28fb91bf26cd866e958 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 1 Jul 2024 09:34:24 +0900 Subject: [PATCH 2/2] Remove unused types and functions --- .../Execution/Instructions/Expression.swift | 15 ------ .../Execution/Runtime/ExecutionState.swift | 6 --- Sources/WasmKit/Execution/Runtime/Stack.swift | 8 --- Sources/WasmKit/Execution/Types/Value.swift | 54 ------------------- .../Execution/HostModuleTests.swift | 2 +- 5 files changed, 1 insertion(+), 84 deletions(-) diff --git a/Sources/WasmKit/Execution/Instructions/Expression.swift b/Sources/WasmKit/Execution/Instructions/Expression.swift index d79344b8..a5907b28 100644 --- a/Sources/WasmKit/Execution/Instructions/Expression.swift +++ b/Sources/WasmKit/Execution/Instructions/Expression.swift @@ -1,15 +1,5 @@ import WasmParser -/// See spec's -/// [definition](https://webassembly.github.io/spec/core/text/instructions.html?highlight=pseudo#control-instructions). -/// > The `block`, `loop` and `if` instructions are structured instructions. They bracket nested sequences of -/// > instructions, called blocks, terminated with, or separated by, end or else pseudo-instructions. As the -/// > grammar prescribes, they must be well-nested. -enum PseudoInstruction { - case `else` - case end -} - struct InstructionSequence: Equatable { let instructions: UnsafeBufferPointer @@ -52,8 +42,3 @@ struct ExpressionRef: Equatable { self._relativeOffset = UInt32(relativeOffset) } } - -/// > Note: -/// - -typealias Expression = [WasmParser.Instruction] diff --git a/Sources/WasmKit/Execution/Runtime/ExecutionState.swift b/Sources/WasmKit/Execution/Runtime/ExecutionState.swift index 1c0fc103..38681f07 100644 --- a/Sources/WasmKit/Execution/Runtime/ExecutionState.swift +++ b/Sources/WasmKit/Execution/Runtime/ExecutionState.swift @@ -82,9 +82,3 @@ extension ExecutionState { store.module(address: stack.currentFrame.module) } } - -extension ExecutionState { - mutating func pseudo(runtime: Runtime, pseudoInstruction: PseudoInstruction) throws { - fatalError("Unimplemented instruction: pseudo") - } -} diff --git a/Sources/WasmKit/Execution/Runtime/Stack.swift b/Sources/WasmKit/Execution/Runtime/Stack.swift index a7f96a66..a73626c4 100644 --- a/Sources/WasmKit/Execution/Runtime/Stack.swift +++ b/Sources/WasmKit/Execution/Runtime/Stack.swift @@ -1,10 +1,6 @@ /// > Note: /// struct Stack { - enum Element: Equatable { - case value(Value) - case frame(Frame) - } private var limit: UInt16 { UInt16.max } private var valueStack: ValueStack @@ -12,10 +8,6 @@ struct Stack { private var frames: FixedSizeStack var currentFrame: Frame! - var isEmpty: Bool { - self.frames.isEmpty && self.numberOfValues == 0 - } - init() { let limit = UInt16.max self.valueStack = ValueStack(capacity: Int(limit)) diff --git a/Sources/WasmKit/Execution/Types/Value.swift b/Sources/WasmKit/Execution/Types/Value.swift index 85bf3807..95596b59 100644 --- a/Sources/WasmKit/Execution/Types/Value.swift +++ b/Sources/WasmKit/Execution/Types/Value.swift @@ -445,30 +445,6 @@ extension Value { } } - var leadingZeroBitCount: Value { - switch self { - case let .i32(rawValue): return .i32(UInt32(rawValue.leadingZeroBitCount)) - case let .i64(rawValue): return .i64(UInt64(rawValue.leadingZeroBitCount)) - default: fatalError("Invalid type \(type) for `Value.\(#function)` implementation") - } - } - - var trailingZeroBitCount: Value { - switch self { - case let .i32(rawValue): return .i32(UInt32(rawValue.trailingZeroBitCount)) - case let .i64(rawValue): return .i64(UInt64(rawValue.trailingZeroBitCount)) - default: fatalError("Invalid type \(type) for `Value.\(#function)` implementation") - } - } - - var nonzeroBitCount: Value { - switch self { - case let .i32(rawValue): return .i32(UInt32(rawValue.nonzeroBitCount)) - case let .i64(rawValue): return .i64(UInt64(rawValue.nonzeroBitCount)) - default: fatalError("Invalid type \(type) for `Value.\(#function)` implementation") - } - } - func rotl(_ l: Self) -> Self { switch (self, l) { case let (.i32(rawValue), .i32(l)): @@ -527,36 +503,6 @@ extension Value { } } - static func + (lhs: Self, rhs: Self) -> Self { - switch (lhs, rhs) { - case let (.i32(lhs), .i32(rhs)): return .i32(lhs &+ rhs) - case let (.i64(lhs), .i64(rhs)): return .i64(lhs &+ rhs) - case let (.f32(lhs), .f32(rhs)): return .f32((Float32(bitPattern: lhs) + Float32(bitPattern: rhs)).bitPattern) - case let (.f64(lhs), .f64(rhs)): return .f64((Float64(bitPattern: lhs) + Float64(bitPattern: rhs)).bitPattern) - default: fatalError("Invalid types \(lhs.type) and \(rhs.type) for `Value.\(#function)` implementation") - } - } - - static func - (lhs: Self, rhs: Self) -> Self { - switch (lhs, rhs) { - case let (.i32(lhs), .i32(rhs)): return .i32(lhs &- rhs) - case let (.i64(lhs), .i64(rhs)): return .i64(lhs &- rhs) - case let (.f32(lhs), .f32(rhs)): return .f32((Float32(bitPattern: lhs) - Float32(bitPattern: rhs)).bitPattern) - case let (.f64(lhs), .f64(rhs)): return .f64((Float64(bitPattern: lhs) - Float64(bitPattern: rhs)).bitPattern) - default: fatalError("Invalid types \(lhs.type) and \(rhs.type) for `Value.\(#function)` implementation") - } - } - - static func * (lhs: Self, rhs: Self) -> Self { - switch (lhs, rhs) { - case let (.i32(lhs), .i32(rhs)): return .i32(lhs &* rhs) - case let (.i64(lhs), .i64(rhs)): return .i64(lhs &* rhs) - case let (.f32(lhs), .f32(rhs)): return .f32((Float32(bitPattern: lhs) * Float32(bitPattern: rhs)).bitPattern) - case let (.f64(lhs), .f64(rhs)): return .f64((Float64(bitPattern: lhs) * Float64(bitPattern: rhs)).bitPattern) - default: fatalError("Invalid types \(lhs.type) and \(rhs.type) for `Value.\(#function)` implementation") - } - } - static func / (lhs: Self, rhs: Self) -> Self { switch (lhs, rhs) { case let (.f32(lhs), .f32(rhs)): return .f32((Float32(bitPattern: lhs) / Float32(bitPattern: rhs)).bitPattern) diff --git a/Tests/WasmKitTests/Execution/HostModuleTests.swift b/Tests/WasmKitTests/Execution/HostModuleTests.swift index 5992bd02..b9bbc058 100644 --- a/Tests/WasmKitTests/Execution/HostModuleTests.swift +++ b/Tests/WasmKitTests/Execution/HostModuleTests.swift @@ -43,7 +43,7 @@ final class HostModuleTests: XCTestCase { type: 0, locals: [], body: { [ - .control(.call(functionIndex: 1)) + .call(functionIndex: 1) ] }), ],