Skip to content

Commit 52a7d69

Browse files
Rename rawIndex to opcodeID in the instruction model
1 parent b7a1aff commit 52a7d69

File tree

7 files changed

+36
-31
lines changed

7 files changed

+36
-31
lines changed

Sources/WasmKit/Execution/DispatchInstruction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,6 @@ extension Instruction {
22572257

22582258
@inline(never)
22592259
var handler: UInt64 {
2260-
return Self.handlers[rawIndex]
2260+
return Self.handlers[Int(self.opcodeID)]
22612261
}
22622262
}

Sources/WasmKit/Execution/Execution.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func executeWasm(
238238
case .direct:
239239
rootISeq[0] = Instruction.endOfExecution.handler
240240
case .token:
241-
rootISeq[0] = UInt64(Instruction.endOfExecution.rawIndex)
241+
rootISeq[0] = UInt64(Instruction.endOfExecution.opcodeID)
242242
}
243243
try stack.execute(
244244
sp: sp,
@@ -381,8 +381,8 @@ extension Execution {
381381
private var buffer = CircularBuffer<UInt64>(capacity: 3)
382382

383383
/// Tracks the given instruction index. This function is called for each instruction execution.
384-
mutating func track(_ rawIndex: UInt64) {
385-
buffer.append(rawIndex)
384+
mutating func track(_ opcode: UInt64) {
385+
buffer.append(opcode)
386386
if let a = buffer[0], let b = buffer[1], let c = buffer[2] {
387387
let trigram = Trigram(a: a, b: b, c: c)
388388
countByTrigram[trigram, default: 0] += 1
@@ -392,7 +392,7 @@ extension Execution {
392392
func dump<TargetStream: TextOutputStream>(target: inout TargetStream, limit: Int) {
393393
print("Instruction statistics:", to: &target)
394394
for (trigram, count) in countByTrigram.sorted(by: { $0.value > $1.value }).prefix(limit) {
395-
print(" \(Instruction.name(rawIndex: trigram.a)) -> \(Instruction.name(rawIndex: trigram.b)) -> \(Instruction.name(rawIndex: trigram.c)) = \(count)", to: &target)
395+
print(" \(Instruction.name(opcode: trigram.a)) -> \(Instruction.name(opcode: trigram.b)) -> \(Instruction.name(opcode: trigram.c)) = \(count)", to: &target)
396396
}
397397
}
398398

@@ -412,12 +412,12 @@ extension Execution {
412412
var stats = StatsCollector()
413413
defer { stats.dump() }
414414
#endif
415-
var inst = pc.read(UInt64.self)
415+
var opcode = pc.read(OpcodeID.self)
416416
while true {
417417
#if EngineStats
418418
stats.track(inst)
419419
#endif
420-
inst = try doExecute(inst, sp: &sp, pc: &pc, md: &md, ms: &ms)
420+
opcode = try doExecute(opcode, sp: &sp, pc: &pc, md: &md, ms: &ms)
421421
}
422422
}
423423

Sources/WasmKit/Execution/Instructions/Control.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ extension Execution {
108108
case .direct:
109109
discriminatorPc.pointee = replaced.handler
110110
case .token:
111-
discriminatorPc.pointee = UInt64(replaced.rawIndex)
111+
discriminatorPc.pointee = UInt64(replaced.opcodeID)
112112
}
113113
try _internalCall(sp: &sp, pc: &pc, callee: callee, internalCallOperand: immediate)
114114
return pc.next()

Sources/WasmKit/Execution/Instructions/Instruction.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,8 @@ extension Instruction {
10201020

10211021

10221022
extension Instruction {
1023-
var rawIndex: Int {
1023+
/// The opcode ID of the instruction.
1024+
var opcodeID: OpcodeID {
10241025
switch self {
10251026
case .copyStack: return 0
10261027
case .globalGet: return 1
@@ -1227,8 +1228,8 @@ extension Instruction {
12271228
/// - Returns: The instruction read from the program counter.
12281229
/// - Precondition: The instruction sequence must be compiled with token threading model.
12291230
static func load(from pc: inout Pc) -> Instruction {
1230-
let rawIndex = pc.read(UInt64.self)
1231-
switch rawIndex {
1231+
let opcode = pc.read(UInt64.self)
1232+
switch opcode {
12321233
case 0: return .copyStack(Instruction.CopyStackOperand.load(from: &pc))
12331234
case 1: return .globalGet(Instruction.GlobalAndVRegOperand.load(from: &pc))
12341235
case 2: return .globalSet(Instruction.GlobalAndVRegOperand.load(from: &pc))
@@ -1425,20 +1426,20 @@ extension Instruction {
14251426
case 193: return .tableElementDrop(Instruction.TableElementDropOperand.load(from: &pc))
14261427
case 194: return .onEnter(Instruction.OnEnterOperand.load(from: &pc))
14271428
case 195: return .onExit(Instruction.OnExitOperand.load(from: &pc))
1428-
default: fatalError("Unknown instruction index: \(rawIndex)")
1429+
default: fatalError("Unknown instruction opcode: \(opcode)")
14291430
}
14301431
}
14311432
}
14321433

14331434
#if EngineStats
14341435
extension Instruction {
14351436
/// The name of the instruction.
1436-
/// - Parameter rawIndex: The raw index of the instruction.
1437+
/// - Parameter opcode: The opcode ID of the instruction.
14371438
/// - Returns: The name of the instruction.
14381439
///
14391440
/// NOTE: This function is used for debugging purposes.
1440-
static func name(rawIndex: UInt64) -> String {
1441-
switch rawIndex {
1441+
static func name(opcode: OpcodeID) -> String {
1442+
switch opcode {
14421443
case 0: return "copyStack"
14431444
case 1: return "globalGet"
14441445
case 2: return "globalSet"
@@ -1635,7 +1636,7 @@ extension Instruction {
16351636
case 193: return "tableElementDrop"
16361637
case 194: return "onEnter"
16371638
case 195: return "onExit"
1638-
default: fatalError("Unknown instruction index: \(rawIndex)")
1639+
default: fatalError("Unknown instruction index: \(opcode)")
16391640
}
16401641
}
16411642
}

Sources/WasmKit/Execution/Instructions/InstructionSupport.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ extension Instruction {
210210
typealias OnExitOperand = FunctionIndex
211211
}
212212

213+
/// The type of an opcode identifier.
214+
typealias OpcodeID = UInt64
215+
213216
struct InstructionPrintingContext {
214217
let shouldColor: Bool
215218
let function: Function

Sources/WasmKit/Translator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
590590
case .direct:
591591
headSlot = instruction.handler
592592
case .token:
593-
headSlot = UInt64(instruction.rawIndex)
593+
headSlot = UInt64(instruction.opcodeID)
594594
}
595595
trace(" [\(index)] = 0x\(String(headSlot, radix: 16))")
596596
self.instructions[index] = headSlot
@@ -640,7 +640,7 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
640640
case .direct:
641641
emitSlot(instruction.handler)
642642
case .token:
643-
emitSlot(UInt64(instruction.rawIndex))
643+
emitSlot(UInt64(instruction.opcodeID))
644644
}
645645
if let immediate = instruction.rawImmediate {
646646
var slots: [CodeSlot] = []

Utilities/Sources/VMGen.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ enum VMGen {
3030

3131
static func generateDispatcher(instructions: [Instruction]) -> String {
3232
let doExecuteParams: [Instruction.Parameter] =
33-
[("instruction", "UInt64", false)]
33+
[("opcode", "OpcodeID", false)]
3434
+ ExecutionParameter.allCases.map { ($0.label, $0.type, true) }
3535
var output = """
3636
extension Execution {
3737
@inline(__always)
3838
mutating func doExecute(_ \(doExecuteParams.map { "\($0.label): \($0.isInout ? "inout " : "")\($0.type)" }.joined(separator: ", "))) throws -> CodeSlot {
39-
switch instruction {
39+
switch opcode {
4040
"""
4141

42-
for (index, inst) in instructions.enumerated() {
42+
for (opcode, inst) in instructions.enumerated() {
4343
let tryPrefix = inst.mayThrow ? "try " : ""
4444
let args = ExecutionParameter.allCases.map { "\($0.label): &\($0.label)" }
4545
output += """
4646
47-
case \(index): return \(tryPrefix)self.execute_\(inst.name)(\(args.joined(separator: ", ")))
47+
case \(opcode): return \(tryPrefix)self.execute_\(inst.name)(\(args.joined(separator: ", ")))
4848
"""
4949
}
5050
output += """
@@ -226,7 +226,8 @@ enum VMGen {
226226
output += "\n\n"
227227
output += """
228228
extension Instruction {
229-
var rawIndex: Int {
229+
/// The opcode ID of the instruction.
230+
var opcodeID: OpcodeID {
230231
switch self {
231232
232233
"""
@@ -247,8 +248,8 @@ enum VMGen {
247248
/// - Returns: The instruction read from the program counter.
248249
/// - Precondition: The instruction sequence must be compiled with token threading model.
249250
static func load(from pc: inout Pc) -> Instruction {
250-
let rawIndex = pc.read(UInt64.self)
251-
switch rawIndex {
251+
let opcode = pc.read(UInt64.self)
252+
switch opcode {
252253
253254
"""
254255
for (i, inst) in instructions.enumerated() {
@@ -260,7 +261,7 @@ enum VMGen {
260261
}
261262
}
262263
output += """
263-
default: fatalError("Unknown instruction index: \\(rawIndex)")
264+
default: fatalError("Unknown instruction opcode: \\(opcode)")
264265
}
265266
}
266267
}
@@ -271,12 +272,12 @@ enum VMGen {
271272
#if EngineStats
272273
extension Instruction {
273274
/// The name of the instruction.
274-
/// - Parameter rawIndex: The raw index of the instruction.
275+
/// - Parameter opcode: The opcode ID of the instruction.
275276
/// - Returns: The name of the instruction.
276277
///
277278
/// NOTE: This function is used for debugging purposes.
278-
static func name(rawIndex: UInt64) -> String {
279-
switch rawIndex {
279+
static func name(opcode: OpcodeID) -> String {
280+
switch opcode {
280281
"""
281282
for (i, inst) in instructions.enumerated() {
282283
output += """
@@ -286,7 +287,7 @@ enum VMGen {
286287
}
287288
output += """
288289
289-
default: fatalError("Unknown instruction index: \\(rawIndex)")
290+
default: fatalError("Unknown instruction index: \\(opcode)")
290291
}
291292
}
292293
}
@@ -464,7 +465,7 @@ enum VMGen {
464465
465466
@inline(never)
466467
var handler: UInt64 {
467-
return Self.handlers[rawIndex]
468+
return Self.handlers[Int(self.opcodeID)]
468469
}
469470
}
470471

0 commit comments

Comments
 (0)