Skip to content

Commit 93721a3

Browse files
committed
Add remaining instructions
1 parent 09396e1 commit 93721a3

File tree

10 files changed

+95
-8
lines changed

10 files changed

+95
-8
lines changed

Sources/WAT/BinaryInstructionEncoder.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,17 @@ extension BinaryInstructionEncoder {
396396
try encodeInstruction([0x14])
397397
try encodeImmediates(functionIndex: functionIndex)
398398
}
399+
mutating func visitReturnCallRef(functionIndex: UInt32) throws {
400+
try encodeInstruction([0x15])
401+
try encodeImmediates(functionIndex: functionIndex)
402+
}
403+
mutating func visitAsNonNull() throws { try encodeInstruction([0xD4]) }
404+
mutating func visitBrOnNull(functionIndex: UInt32) throws {
405+
try encodeInstruction([0xD5])
406+
try encodeImmediates(functionIndex: functionIndex)
407+
}
408+
mutating func visitBrOnNonNull(functionIndex: UInt32) throws {
409+
try encodeInstruction([0xD6])
410+
try encodeImmediates(functionIndex: functionIndex)
411+
}
399412
}

Sources/WAT/ParseTextInstruction.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,16 @@ func parseTextInstruction<V: InstructionVisitor>(keyword: String, expressionPars
335335
case "call_ref":
336336
let (functionIndex) = try expressionParser.visitCallRef(wat: &wat)
337337
return { return try $0.visitCallRef(functionIndex: functionIndex) }
338+
case "return_call_ref":
339+
let (functionIndex) = try expressionParser.visitReturnCallRef(wat: &wat)
340+
return { return try $0.visitReturnCallRef(functionIndex: functionIndex) }
341+
case "as_non_null": return { return try $0.visitAsNonNull() }
342+
case "br_on_null":
343+
let (functionIndex) = try expressionParser.visitBrOnNull(wat: &wat)
344+
return { return try $0.visitBrOnNull(functionIndex: functionIndex) }
345+
case "br_on_non_null":
346+
let (functionIndex) = try expressionParser.visitBrOnNonNull(wat: &wat)
347+
return { return try $0.visitBrOnNonNull(functionIndex: functionIndex) }
338348
default: return nil
339349
}
340350
}

Sources/WAT/Parser/ExpressionParser.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ struct ExpressionParser<Visitor: InstructionVisitor> {
363363

364364
private mutating func refKind() throws -> ReferenceType {
365365
if try parser.take(.id) {
366-
return .funcRef // not sure about this.
366+
return .funcRef // not sure about this.
367367
} else if try parser.takeKeyword("func") {
368368
return .funcRef
369369
} else if try parser.takeKeyword("extern") {
@@ -445,6 +445,15 @@ extension ExpressionParser {
445445
let use = try parser.expectIndexOrId()
446446
return UInt32(try wat.types.resolve(use: use).index)
447447
}
448+
mutating func visitReturnCallRef(wat: inout Wat) throws -> UInt32 {
449+
return 0
450+
}
451+
mutating func visitBrOnNull(wat: inout Wat) throws -> UInt32 {
452+
return 0
453+
}
454+
mutating func visitBrOnNonNull(wat: inout Wat) throws -> UInt32 {
455+
return 0
456+
}
448457
mutating func visitCallIndirect(wat: inout Wat) throws -> (typeIndex: UInt32, tableIndex: UInt32) {
449458
let tableIndex: UInt32
450459
if let tableId = try parser.takeIndexOrId() {

Sources/WasmKit/Execution/Instructions/InstructionSupport.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ extension Int32: InstructionImmediate {
105105

106106
extension Instruction.RefNullOperand {
107107
init(result: VReg, type: ReferenceType) {
108-
self.init(result: result, rawType: type.heapType.rawValue) // need to figure out rawType here
108+
self.init(result: result, rawType: type.heapType.rawValue) // need to figure out rawType here
109109
}
110110

111111
var type: ReferenceType {
112-
ReferenceType(isNullable: true, heapType: HeapType(rawValue: rawType)!) // is this still a valid conversion?
112+
ReferenceType(isNullable: true, heapType: HeapType(rawValue: rawType)!) // is this still a valid conversion?
113113
}
114114
}
115115

Sources/WasmParser/BinaryInstructionDecoder.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ protocol BinaryInstructionDecoder {
8888
@inlinable mutating func visitTableSize() throws -> UInt32
8989
/// Decode `call_ref` immediates
9090
@inlinable mutating func visitCallRef() throws -> UInt32
91+
/// Decode `return_call_ref` immediates
92+
@inlinable mutating func visitReturnCallRef() throws -> UInt32
93+
/// Decode `br_on_null` immediates
94+
@inlinable mutating func visitBrOnNull() throws -> UInt32
95+
/// Decode `br_on_non_null` immediates
96+
@inlinable mutating func visitBrOnNonNull() throws -> UInt32
9197
}
9298
@inlinable
9399
func parseBinaryInstruction<V: InstructionVisitor, D: BinaryInstructionDecoder>(visitor: inout V, decoder: inout D) throws -> Bool {
@@ -137,6 +143,9 @@ func parseBinaryInstruction<V: InstructionVisitor, D: BinaryInstructionDecoder>(
137143
case 0x14:
138144
let (functionIndex) = try decoder.visitCallRef()
139145
try visitor.visitCallRef(functionIndex: functionIndex)
146+
case 0x15:
147+
let (functionIndex) = try decoder.visitReturnCallRef()
148+
try visitor.visitReturnCallRef(functionIndex: functionIndex)
140149
case 0x1A:
141150
try visitor.visitDrop()
142151
case 0x1B:
@@ -516,6 +525,14 @@ func parseBinaryInstruction<V: InstructionVisitor, D: BinaryInstructionDecoder>(
516525
case 0xD2:
517526
let (functionIndex) = try decoder.visitRefFunc()
518527
try visitor.visitRefFunc(functionIndex: functionIndex)
528+
case 0xD4:
529+
try visitor.visitAsNonNull()
530+
case 0xD5:
531+
let (functionIndex) = try decoder.visitBrOnNull()
532+
try visitor.visitBrOnNull(functionIndex: functionIndex)
533+
case 0xD6:
534+
let (functionIndex) = try decoder.visitBrOnNonNull()
535+
try visitor.visitBrOnNonNull(functionIndex: functionIndex)
519536
case 0xFC:
520537

521538
let opcode1 = try decoder.claimNextByte()

Sources/WasmParser/InstructionVisitor.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ public enum Instruction: Equatable {
227227
case `tableGrow`(table: UInt32)
228228
case `tableSize`(table: UInt32)
229229
case `callRef`(functionIndex: UInt32)
230+
case `returnCallRef`(functionIndex: UInt32)
231+
case `asNonNull`
232+
case `brOnNull`(functionIndex: UInt32)
233+
case `brOnNonNull`(functionIndex: UInt32)
230234
}
231235

232236
/// A visitor that visits all instructions by a single visit method.
@@ -289,6 +293,10 @@ extension AnyInstructionVisitor {
289293
public mutating func visitTableGrow(table: UInt32) throws { return try self.visit(.tableGrow(table: table)) }
290294
public mutating func visitTableSize(table: UInt32) throws { return try self.visit(.tableSize(table: table)) }
291295
public mutating func visitCallRef(functionIndex: UInt32) throws { return try self.visit(.callRef(functionIndex: functionIndex)) }
296+
public mutating func visitReturnCallRef(functionIndex: UInt32) throws { return try self.visit(.returnCallRef(functionIndex: functionIndex)) }
297+
public mutating func visitAsNonNull() throws { return try self.visit(.asNonNull) }
298+
public mutating func visitBrOnNull(functionIndex: UInt32) throws { return try self.visit(.brOnNull(functionIndex: functionIndex)) }
299+
public mutating func visitBrOnNonNull(functionIndex: UInt32) throws { return try self.visit(.brOnNonNull(functionIndex: functionIndex)) }
292300
}
293301

294302
/// A visitor for WebAssembly instructions.
@@ -402,6 +410,14 @@ public protocol InstructionVisitor {
402410
mutating func visitTableSize(table: UInt32) throws
403411
/// Visiting `call_ref` instruction.
404412
mutating func visitCallRef(functionIndex: UInt32) throws
413+
/// Visiting `return_call_ref` instruction.
414+
mutating func visitReturnCallRef(functionIndex: UInt32) throws
415+
/// Visiting `as_non_null` instruction.
416+
mutating func visitAsNonNull() throws
417+
/// Visiting `br_on_null` instruction.
418+
mutating func visitBrOnNull(functionIndex: UInt32) throws
419+
/// Visiting `br_on_non_null` instruction.
420+
mutating func visitBrOnNonNull(functionIndex: UInt32) throws
405421
}
406422

407423
extension InstructionVisitor {
@@ -461,6 +477,10 @@ extension InstructionVisitor {
461477
case let .tableGrow(table): return try visitTableGrow(table: table)
462478
case let .tableSize(table): return try visitTableSize(table: table)
463479
case let .callRef(functionIndex): return try visitCallRef(functionIndex: functionIndex)
480+
case let .returnCallRef(functionIndex): return try visitReturnCallRef(functionIndex: functionIndex)
481+
case .asNonNull: return try visitAsNonNull()
482+
case let .brOnNull(functionIndex): return try visitBrOnNull(functionIndex: functionIndex)
483+
case let .brOnNonNull(functionIndex): return try visitBrOnNonNull(functionIndex: functionIndex)
464484
}
465485
}
466486
}
@@ -520,5 +540,9 @@ extension InstructionVisitor {
520540
public mutating func visitTableGrow(table: UInt32) throws {}
521541
public mutating func visitTableSize(table: UInt32) throws {}
522542
public mutating func visitCallRef(functionIndex: UInt32) throws {}
543+
public mutating func visitReturnCallRef(functionIndex: UInt32) throws {}
544+
public mutating func visitAsNonNull() throws {}
545+
public mutating func visitBrOnNull(functionIndex: UInt32) throws {}
546+
public mutating func visitBrOnNonNull(functionIndex: UInt32) throws {}
523547
}
524548

Sources/WasmParser/WasmParser.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,10 @@ extension Parser: BinaryInstructionDecoder {
648648
return (typeIndex, tableIndex)
649649
}
650650

651+
@inlinable mutating func visitReturnCallRef() throws -> UInt32 {
652+
return 0
653+
}
654+
651655
@inlinable mutating func visitTypedSelect() throws -> WasmTypes.ValueType {
652656
let results = try parseVector { try parseValueType() }
653657
guard results.count == 1 else {
@@ -692,6 +696,12 @@ extension Parser: BinaryInstructionDecoder {
692696
}
693697
return refType
694698
}
699+
@inlinable mutating func visitBrOnNull() throws -> UInt32 {
700+
return 0
701+
}
702+
@inlinable mutating func visitBrOnNonNull() throws -> UInt32 {
703+
return 0
704+
}
695705

696706
@inlinable mutating func visitRefFunc() throws -> UInt32 { try parseUnsigned() }
697707
@inlinable mutating func visitMemoryInit() throws -> UInt32 {

Sources/WasmTypes/WasmTypes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public struct FunctionType: Equatable, Hashable {
1616

1717
public enum HeapType: UInt8, Equatable, Hashable {
1818
/// A reference to any kind of function.
19-
case funcRef // -> to be renamed func
19+
case funcRef // -> to be renamed func
2020

2121
/// An external host data.
22-
case externRef // -> to be renamed extern
22+
case externRef // -> to be renamed extern
2323
}
2424

2525
/// Reference types

Tests/WasmKitTests/SpectestTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ final class SpectestTests: XCTestCase {
5151
let defaultConfig = EngineConfiguration()
5252
let result = try await spectestResult(
5353
path: Self.functionReferences,
54-
include: ["function-references/call_ref.wast"], // focusing on call_ref for now, but will update to run all function-references tests.
54+
include: ["function-references/call_ref.wast"], // focusing on call_ref for now, but will update to run all function-references tests.
5555
exclude: [],
5656
parallel: false,
5757
configuration: defaultConfig
@@ -116,7 +116,7 @@ final class SpectestTests: XCTestCase {
116116
"gc/type-rec.wast",
117117
"gc/type-subtyping.wast",
118118
"gc/unreached-invalid.wast",
119-
"gc/unreached-valid.wast"
119+
"gc/unreached-valid.wast",
120120
]
121121
)
122122
}

Utilities/Instructions.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,9 @@
202202
["saturatingFloatToInt", "i64.trunc_sat_f32_u" , ["0xFC", "0x05"], [] , "conversion"],
203203
["saturatingFloatToInt", "i64.trunc_sat_f64_s" , ["0xFC", "0x06"], [] , "conversion"],
204204
["saturatingFloatToInt", "i64.trunc_sat_f64_u" , ["0xFC", "0x07"], [] , "conversion"],
205-
["referenceTypes" , "call_ref" , ["0x14"] , [["functionIndex", "UInt32"]] , null ]
205+
["referenceTypes" , "call_ref" , ["0x14"] , [["functionIndex", "UInt32"]] , null ],
206+
["referenceTypes" , "return_call_ref" , ["0x15"] , [["functionIndex", "UInt32"]] , null ],
207+
["referenceTypes" , "as_non_null" , ["0xd4"] , [] , null ],
208+
["referenceTypes" , "br_on_null" , ["0xd5"] , [["functionIndex", "UInt32"]] , null ],
209+
["referenceTypes" , "br_on_non_null" , ["0xd6"] , [["functionIndex", "UInt32"]] , null ]
206210
]

0 commit comments

Comments
 (0)