@@ -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