Skip to content

Commit

Permalink
Merge pull request #97 from swiftwasm/katei/remove-legacy-types
Browse files Browse the repository at this point in the history
Remove unused types and functions
  • Loading branch information
kateinoigakukun committed Jul 1, 2024
2 parents 3ece12e + 39cef2f commit 4b70f1b
Show file tree
Hide file tree
Showing 8 changed files with 1 addition and 153 deletions.
2 changes: 0 additions & 2 deletions Sources/WasmKit/Execution/Instructions/Control.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 0 additions & 15 deletions Sources/WasmKit/Execution/Instructions/Expression.swift
Original file line number Diff line number Diff line change
@@ -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<Instruction>

Expand Down Expand Up @@ -52,8 +42,3 @@ struct ExpressionRef: Equatable {
self._relativeOffset = UInt32(relativeOffset)
}
}

/// > Note:
/// <https://webassembly.github.io/spec/core/syntax/instructions.html#expressions>

typealias Expression = [WasmParser.Instruction]
61 changes: 0 additions & 61 deletions Sources/WasmKit/Execution/Instructions/InstructionSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,38 +17,6 @@ extension Instruction {
}
}

struct LegacyBrTable: Equatable {
private let bufferBase: UnsafePointer<LabelIndex>
private let bufferCount: UInt32
var labelIndices: UnsafeBufferPointer<LabelIndex> {
UnsafeBufferPointer(start: bufferBase, count: Int(bufferCount - 1))
}
var defaultIndex: LabelIndex {
bufferBase[Int(bufferCount - 1)]
}

init(labelIndices: [LabelIndex], defaultIndex: LabelIndex) {
let buffer = UnsafeMutableBufferPointer<LabelIndex>.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))
Expand Down
6 changes: 0 additions & 6 deletions Sources/WasmKit/Execution/Runtime/ExecutionState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
8 changes: 0 additions & 8 deletions Sources/WasmKit/Execution/Runtime/Stack.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
/// > Note:
/// <https://webassembly.github.io/spec/core/exec/runtime.html#stack>
struct Stack {
enum Element: Equatable {
case value(Value)
case frame(Frame)
}

private var limit: UInt16 { UInt16.max }
private var valueStack: ValueStack
private var numberOfValues: Int { valueStack.count }
private var frames: FixedSizeStack<Frame>
var currentFrame: Frame!

var isEmpty: Bool {
self.frames.isEmpty && self.numberOfValues == 0
}

init() {
let limit = UInt16.max
self.valueStack = ValueStack(capacity: Int(limit))
Expand Down
54 changes: 0 additions & 54 deletions Sources/WasmKit/Execution/Types/Value.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions Sources/WasmKit/Translator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
2 changes: 1 addition & 1 deletion Tests/WasmKitTests/Execution/HostModuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class HostModuleTests: XCTestCase {
type: 0, locals: [],
body: {
[
.control(.call(functionIndex: 1))
.call(functionIndex: 1)
]
}),
],
Expand Down

0 comments on commit 4b70f1b

Please sign in to comment.