From 6bcdc31a12de27b2b6239c17218938449b2aac28 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 2 Sep 2025 12:32:36 +0900 Subject: [PATCH 1/2] NFC: Stop passing ValueStack to instruction closures in popPushEmit --- Sources/WasmKit/Translator.swift | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Sources/WasmKit/Translator.swift b/Sources/WasmKit/Translator.swift index 55db6715..72b2552d 100644 --- a/Sources/WasmKit/Translator.swift +++ b/Sources/WasmKit/Translator.swift @@ -1709,15 +1709,15 @@ struct InstructionTranslator: 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) }) } } @@ -1776,7 +1776,7 @@ struct InstructionTranslator: 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, @@ -1849,7 +1849,7 @@ struct InstructionTranslator: 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 @@ -1893,7 +1893,7 @@ struct InstructionTranslator: 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))) } } @@ -1917,12 +1917,12 @@ struct InstructionTranslator: 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))) } } @@ -2018,7 +2018,7 @@ struct InstructionTranslator: 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))) } } @@ -2217,7 +2217,7 @@ struct InstructionTranslator: InstructionVisitor { try popPushEmit( module.addressType(tableIndex: table), .ref(type.elementType) - ) { index, result, stack in + ) { index, result in return .tableGet( Instruction.TableGetOperand( index: index, From b6413783e0b259163237eca93eaf5a405602d584 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 7 Sep 2025 15:16:57 +0900 Subject: [PATCH 2/2] NFC: Remove `instance` parameter from `InstructionTranslator.translate` --- Sources/WasmKit/Execution/Function.swift | 2 +- Sources/WasmKit/Translator.swift | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/Sources/WasmKit/Execution/Function.swift b/Sources/WasmKit/Execution/Function.swift index e87f8b8d..52fc06f2 100644 --- a/Sources/WasmKit/Execution/Function.swift +++ b/Sources/WasmKit/Execution/Function.swift @@ -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 diff --git a/Sources/WasmKit/Translator.swift b/Sources/WasmKit/Translator.swift index 72b2552d..0dc26adb 100644 --- a/Sources/WasmKit/Translator.swift +++ b/Sources/WasmKit/Translator.swift @@ -1115,10 +1115,7 @@ struct InstructionTranslator: 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))