Skip to content

Commit

Permalink
Remove ifThenElse instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Apr 7, 2024
1 parent de39caa commit 2a5d6f9
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 31 deletions.
14 changes: 2 additions & 12 deletions Sources/WasmKit/Execution/Instructions/Control.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,15 @@ extension ExecutionState {

typealias BlockType = Instruction.BlockType

mutating func ifThen(runtime: Runtime, stack: inout Stack, endRef: ExpressionRef, type: BlockType) {
mutating func ifThen(runtime: Runtime, stack: inout Stack, elseOrEndRef: ExpressionRef, type: BlockType) {
let isTrue = stack.popValue().i32 != 0
if isTrue {
programCounter += 1
} else {
programCounter += endRef.relativeOffset
programCounter += elseOrEndRef.relativeOffset
}
}

mutating func ifThenElse(runtime: Runtime, stack: inout Stack, elseRef: ExpressionRef, type: BlockType) {
let isTrue = stack.popValue().i32 != 0
let addendToPC: Int
if isTrue {
addendToPC = 1
} else {
addendToPC = elseRef.relativeOffset
}
programCounter += addendToPC
}
mutating func end(runtime: Runtime, stack: inout Stack) {
fatalError()
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/WasmKit/Execution/Instructions/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ enum Instruction: Equatable {
case globalSet(index: GlobalIndex)
case unreachable
case nop
case ifThen(endRef: ExpressionRef, type: BlockType)
case ifThenElse(elseRef: ExpressionRef, type: BlockType)
case ifThen(elseOrEndRef: ExpressionRef, type: BlockType)
case end
case `else`(endRef: ExpressionRef)
case br(offset: Int32, copyCount: UInt32, popCount: UInt32)
Expand Down
8 changes: 2 additions & 6 deletions Sources/WasmKit/Execution/Runtime/InstDispatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ extension ExecutionState {
case .nop:
try self.nop(runtime: runtime, stack: &stack)
return true
case .ifThen(let endRef, let type):
self.ifThen(runtime: runtime, stack: &stack, endRef: endRef, type: type)
return true
case .ifThenElse(let elseRef, let type):
self.ifThenElse(runtime: runtime, stack: &stack, elseRef: elseRef, type: type)
case .ifThen(let elseOrEndRef, let type):
self.ifThen(runtime: runtime, stack: &stack, elseOrEndRef: elseOrEndRef, type: type)
return true
case .end:
self.end(runtime: runtime, stack: &stack)
Expand Down Expand Up @@ -254,7 +251,6 @@ extension Instruction {
case .unreachable: return "unreachable"
case .nop: return "nop"
case .ifThen: return "ifThen"
case .ifThenElse: return "ifThenElse"
case .end: return "end"
case .`else`: return "`else`"
case .br: return "br"
Expand Down
10 changes: 4 additions & 6 deletions Sources/WasmKit/Translator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -417,15 +417,13 @@ struct InstructionTranslator: InstructionVisitor {
)
let selfPC = iseqBuilder.insertingPC
iseqBuilder.emitWithLabel(endLabel) { iseqBuilder, endPC in
let endRef = ExpressionRef(from: selfPC, to: endPC)
let elseOrEndRef: ExpressionRef
if let elsePC = iseqBuilder.resolveLabel(elseLabel) {
return .ifThenElse(
elseRef: ExpressionRef(from: selfPC, to: elsePC),
type: Instruction.BlockType(blockType)
)
elseOrEndRef = ExpressionRef(from: selfPC, to: elsePC)
} else {
return .ifThen(endRef: endRef, type: Instruction.BlockType(blockType))
elseOrEndRef = ExpressionRef(from: selfPC, to: endPC)
}
return .ifThen(elseOrEndRef: elseOrEndRef, type: Instruction.BlockType(blockType))
}
}

Expand Down
7 changes: 2 additions & 5 deletions Utilities/generate_inst_dispatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,8 @@ let instructions: [Instruction] = [
Instruction(name: "unreachable", isControl: true, mayThrow: true, immediates: []),
Instruction(name: "nop", isControl: true, mayThrow: true, immediates: []),
Instruction(name: "ifThen", isControl: true, immediates: [
Immediate(name: "endRef", type: "ExpressionRef"),
Immediate(name: "type", type: "BlockType")
]),
Instruction(name: "ifThenElse", isControl: true, immediates: [
Immediate(name: "elseRef", type: "ExpressionRef"),
// elseRef for if-then-else-end sequence, endRef for if-then-end sequence
Immediate(name: "elseOrEndRef", type: "ExpressionRef"),
Immediate(name: "type", type: "BlockType")
]),
Instruction(name: "end", isControl: true, immediates: []),
Expand Down

0 comments on commit 2a5d6f9

Please sign in to comment.