Skip to content

Commit bb3d7a6

Browse files
VMGen: Minor documentation improvements
1 parent b03fc02 commit bb3d7a6

File tree

6 files changed

+49
-37
lines changed

6 files changed

+49
-37
lines changed

Sources/WasmKit/Execution/DispatchInstruction.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
//// Automatically generated by Utilities/Sources/VMGen.swift
22
//// DO NOT EDIT DIRECTLY
33

4+
// Include the C inline code to codegen together with the Swift code.
5+
import _CWasmKit.InlineCode
6+
7+
// MARK: - Token Threaded Code
48
extension Execution {
9+
10+
/// Execute an instruction identified by the opcode.
11+
/// Note: This function is only used when using token threading model.
512
@inline(__always)
613
mutating func doExecute(_ opcode: OpcodeID, sp: inout Sp, pc: inout Pc, md: inout Md, ms: inout Ms) throws -> CodeSlot {
714
switch opcode {
@@ -207,6 +214,7 @@ extension Execution {
207214
}
208215
}
209216

217+
// MARK: - Direct Threaded Code
210218
extension Execution {
211219
@_silgen_name("wasmkit_execute_copyStack") @inline(__always)
212220
mutating func execute_copyStack(sp: UnsafeMutablePointer<Sp>, pc: UnsafeMutablePointer<Pc>, md: UnsafeMutablePointer<Md>, ms: UnsafeMutablePointer<Ms>) -> CodeSlot {
@@ -1763,14 +1771,12 @@ extension Execution {
17631771
}
17641772
}
17651773

1766-
1767-
import _CWasmKit.InlineCode
1768-
17691774
extension Instruction {
1770-
var handler: UInt64 {
1775+
/// The tail-calling execution handler for the instruction.
1776+
var handler: UInt {
17711777
return withUnsafePointer(to: wasmkit_tc_exec_handlers) {
17721778
let count = MemoryLayout.size(ofValue: wasmkit_tc_exec_handlers) / MemoryLayout<wasmkit_tc_exec>.size
1773-
return $0.withMemoryRebound(to: UInt64.self, capacity: count) {
1779+
return $0.withMemoryRebound(to: UInt.self, capacity: count) {
17741780
$0[Int(self.opcodeID)]
17751781
}
17761782
}

Sources/WasmKit/Execution/Execution.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,9 @@ func executeWasm(
234234
}
235235

236236
try withUnsafeTemporaryAllocation(of: CodeSlot.self, capacity: 2) { rootISeq in
237-
switch runtime.value.configuration.threadingModel {
238-
case .direct:
239-
rootISeq[0] = Instruction.endOfExecution.handler
240-
case .token:
241-
rootISeq[0] = UInt64(Instruction.endOfExecution.opcodeID)
242-
}
237+
rootISeq[0] = Instruction.endOfExecution.headSlot(
238+
threadingModel: runtime.value.configuration.threadingModel
239+
)
243240
try stack.execute(
244241
sp: sp,
245242
pc: rootISeq.baseAddress!,

Sources/WasmKit/Execution/Instructions/Control.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,11 @@ extension Execution {
100100
mutating func compilingCall(sp: inout Sp, pc: Pc, immediate: Instruction.CallOperand) throws -> (Pc, CodeSlot) {
101101
var pc = pc
102102
// NOTE: `CompilingCallOperand` consumes 2 slots, discriminator is at -3
103-
let discriminatorPc = pc.advanced(by: -3)
103+
let headSlotPc = pc.advanced(by: -3)
104104
let callee = immediate.callee
105105
try callee.ensureCompiled(runtime: runtime)
106106
let replaced = Instruction.internalCall(immediate)
107-
switch runtime.value.configuration.threadingModel {
108-
case .direct:
109-
discriminatorPc.pointee = replaced.handler
110-
case .token:
111-
discriminatorPc.pointee = UInt64(replaced.opcodeID)
112-
}
107+
headSlotPc.pointee = replaced.headSlot(threadingModel: runtime.value.configuration.threadingModel)
113108
try _internalCall(sp: &sp, pc: &pc, callee: callee, internalCallOperand: immediate)
114109
return pc.next()
115110
}

Sources/WasmKit/Execution/Instructions/InstructionSupport.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ extension UntypedValue {
188188
/// The type of an opcode identifier.
189189
typealias OpcodeID = UInt64
190190

191+
extension Instruction {
192+
func headSlot(threadingModel: RuntimeConfiguration.ThreadingModel) -> CodeSlot {
193+
switch threadingModel {
194+
case .direct:
195+
return CodeSlot(handler)
196+
case .token:
197+
return opcodeID
198+
}
199+
}
200+
}
201+
191202
// MARK: - Instruction printing support
192203

193204
extension InstructionSequence {

Sources/WasmKit/Translator.swift

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -585,13 +585,7 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
585585

586586
private mutating func assign(at index: Int, _ instruction: Instruction) {
587587
trace("assign: \(instruction)")
588-
let headSlot: CodeSlot
589-
switch runtimeConfiguration.threadingModel {
590-
case .direct:
591-
headSlot = instruction.handler
592-
case .token:
593-
headSlot = UInt64(instruction.opcodeID)
594-
}
588+
let headSlot = instruction.headSlot(threadingModel: runtimeConfiguration.threadingModel)
595589
trace(" [\(index)] = 0x\(String(headSlot, radix: 16))")
596590
self.instructions[index] = headSlot
597591
if let immediate = instruction.rawImmediate {
@@ -636,12 +630,7 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
636630
mutating func emit(_ instruction: Instruction, resultRelink: ResultRelink? = nil) {
637631
self.lastEmission = LastEmission(position: insertingPC, resultRelink: resultRelink)
638632
trace("emitInstruction: \(instruction)")
639-
switch runtimeConfiguration.threadingModel {
640-
case .direct:
641-
emitSlot(instruction.handler)
642-
case .token:
643-
emitSlot(UInt64(instruction.opcodeID))
644-
}
633+
emitSlot(instruction.headSlot(threadingModel: runtimeConfiguration.threadingModel))
645634
if let immediate = instruction.rawImmediate {
646635
var slots: [CodeSlot] = []
647636
immediate.emit(to: { slots.append($0) })

Utilities/Sources/VMGen.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ enum VMGen {
3434
+ ExecutionParameter.allCases.map { ($0.label, $0.type, true) }
3535
var output = """
3636
extension Execution {
37+
38+
/// Execute an instruction identified by the opcode.
39+
/// Note: This function is only used when using token threading model.
3740
@inline(__always)
3841
mutating func doExecute(_ \(doExecuteParams.map { "\($0.label): \($0.isInout ? "inout " : "")\($0.type)" }.joined(separator: ", "))) throws -> CodeSlot {
3942
switch opcode {
@@ -427,19 +430,30 @@ enum VMGen {
427430
let generatedFiles = [
428431
GeneratedFile(
429432
projectSources + ["WasmKit", "Execution", "DispatchInstruction.swift"],
430-
header + generateDispatcher(instructions: instructions)
431-
+ "\n\n"
432-
+ generateDirectThreadedCode(instructions: instructions, inlineImpls: inlineImpls)
433+
header
433434
+ """
435+
// Include the C inline code to codegen together with the Swift code.
436+
import _CWasmKit.InlineCode
434437
438+
// MARK: - Token Threaded Code
435439
436-
import _CWasmKit.InlineCode
440+
"""
441+
+ generateDispatcher(instructions: instructions)
442+
+ """
443+
444+
445+
// MARK: - Direct Threaded Code
446+
447+
"""
448+
+ generateDirectThreadedCode(instructions: instructions, inlineImpls: inlineImpls)
449+
+ """
437450
438451
extension Instruction {
439-
var handler: UInt64 {
452+
/// The tail-calling execution handler for the instruction.
453+
var handler: UInt {
440454
return withUnsafePointer(to: wasmkit_tc_exec_handlers) {
441455
let count = MemoryLayout.size(ofValue: wasmkit_tc_exec_handlers) / MemoryLayout<wasmkit_tc_exec>.size
442-
return $0.withMemoryRebound(to: UInt64.self, capacity: count) {
456+
return $0.withMemoryRebound(to: UInt.self, capacity: count) {
443457
$0[Int(self.opcodeID)]
444458
}
445459
}

0 commit comments

Comments
 (0)