Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/WasmKit/Execution/Function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ struct WasmFunctionEntity {
intercepting: engine.interceptor != nil
)
let iseq = try code.withValue { code in
try translator.translate(code: code, instance: instance)
try translator.translate(code: code)
}
self.code = .compiled(iseq)
return iseq
Expand Down
27 changes: 12 additions & 15 deletions Sources/WasmKit/Translator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1115,10 +1115,7 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
// MARK: Main entry point

/// Translate a Wasm expression into a sequence of instructions.
mutating func translate(
code: Code,
instance: InternalInstance
) throws -> InstructionSequence {
mutating func translate(code: Code) throws -> InstructionSequence {
if intercepting {
// Emit `onEnter` instruction at the beginning of the function
emit(.onEnter(functionIndex))
Expand Down Expand Up @@ -1709,15 +1706,15 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
private mutating func popPushEmit(
_ pop: ValueType,
_ push: ValueType,
_ instruction: @escaping (_ popped: VReg, _ result: VReg, ValueStack) -> Instruction
_ instruction: @escaping (_ popped: VReg, _ result: VReg) -> Instruction
) throws {
let value = try popVRegOperand(pop)
let result = valueStack.push(push)
if let value = value {
emit(
instruction(value, result, valueStack),
resultRelink: { [valueStack] newResult in
instruction(value, newResult, valueStack)
instruction(value, result),
resultRelink: { newResult in
instruction(value, newResult)
})
}
}
Expand Down Expand Up @@ -1776,7 +1773,7 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
) throws {
let isMemory64 = try module.isMemory64(memoryIndex: 0)
try validator.validateMemArg(memarg, naturalAlignment: naturalAlignment)
try popPushEmit(.address(isMemory64: isMemory64), type) { value, result, stack in
try popPushEmit(.address(isMemory64: isMemory64), type) { value, result in
let loadOperand = Instruction.LoadOperand(
offset: memarg.offset,
pointer: value,
Expand Down Expand Up @@ -1849,7 +1846,7 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
let isMemory64 = try module.isMemory64(memoryIndex: memory)
let sizeType = ValueType.address(isMemory64: isMemory64)
// Just pop/push the same type (i64 or i32) value
try popPushEmit(sizeType, sizeType) { value, result, stack in
try popPushEmit(sizeType, sizeType) { value, result in
.memoryGrow(
Instruction.MemoryGrowOperand(
result: result, delta: value, memory: memory
Expand Down Expand Up @@ -1893,7 +1890,7 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
}

private mutating func visitUnary(_ operand: ValueType, _ instruction: @escaping (Instruction.UnaryOperand) -> Instruction) throws {
try popPushEmit(operand, operand) { value, result, stack in
try popPushEmit(operand, operand) { value, result in
return instruction(Instruction.UnaryOperand(result: LVReg(result), input: LVReg(value)))
}
}
Expand All @@ -1917,12 +1914,12 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
try visitBinary(operand, .i32, instruction)
}
private mutating func visitConversion(_ from: ValueType, _ to: ValueType, _ instruction: @escaping (Instruction.UnaryOperand) -> Instruction) throws {
try popPushEmit(from, to) { value, result, stack in
try popPushEmit(from, to) { value, result in
return instruction(Instruction.UnaryOperand(result: LVReg(result), input: LVReg(value)))
}
}
mutating func visitI32Eqz() throws -> Output {
try popPushEmit(.i32, .i32) { value, result, stack in
try popPushEmit(.i32, .i32) { value, result in
.i32Eqz(Instruction.UnaryOperand(result: LVReg(result), input: LVReg(value)))
}
}
Expand Down Expand Up @@ -2018,7 +2015,7 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
try visitBinary(operand, result, instruction)
}
mutating func visitI64Eqz() throws -> Output {
try popPushEmit(.i64, .i32) { value, result, stack in
try popPushEmit(.i64, .i32) { value, result in
.i64Eqz(Instruction.UnaryOperand(result: LVReg(result), input: LVReg(value)))
}
}
Expand Down Expand Up @@ -2217,7 +2214,7 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
try popPushEmit(
module.addressType(tableIndex: table),
.ref(type.elementType)
) { index, result, stack in
) { index, result in
return .tableGet(
Instruction.TableGetOperand(
index: index,
Expand Down