Skip to content

WasmParser/WAT: Add support for the tail-call proposal #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/WAT/BinaryInstructionEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ extension BinaryInstructionEncoder {
try encodeInstruction([0x11])
try encodeImmediates(typeIndex: typeIndex, tableIndex: tableIndex)
}
mutating func visitReturnCall(functionIndex: UInt32) throws {
try encodeInstruction([0x12])
try encodeImmediates(functionIndex: functionIndex)
}
mutating func visitReturnCallIndirect(typeIndex: UInt32, tableIndex: UInt32) throws {
try encodeInstruction([0x13])
try encodeImmediates(typeIndex: typeIndex, tableIndex: tableIndex)
}
mutating func visitDrop() throws { try encodeInstruction([0x1A]) }
mutating func visitSelect() throws { try encodeInstruction([0x1B]) }
mutating func visitTypedSelect(type: ValueType) throws {
Expand Down
6 changes: 6 additions & 0 deletions Sources/WAT/ParseTextInstruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ func parseTextInstruction<V: InstructionVisitor>(keyword: String, expressionPars
case "call_indirect":
let (typeIndex, tableIndex) = try expressionParser.visitCallIndirect(wat: &wat)
return { return try $0.visitCallIndirect(typeIndex: typeIndex, tableIndex: tableIndex) }
case "return_call":
let (functionIndex) = try expressionParser.visitReturnCall(wat: &wat)
return { return try $0.visitReturnCall(functionIndex: functionIndex) }
case "return_call_indirect":
let (typeIndex, tableIndex) = try expressionParser.visitReturnCallIndirect(wat: &wat)
return { return try $0.visitReturnCallIndirect(typeIndex: typeIndex, tableIndex: tableIndex) }
case "drop": return { return try $0.visitDrop() }
case "select": return { return try $0.visitSelect() }
case "local.get":
Expand Down
6 changes: 6 additions & 0 deletions Sources/WAT/Parser/ExpressionParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,12 @@ extension ExpressionParser {
let (_, typeIndex) = try wat.types.resolve(use: typeUse)
return (UInt32(typeIndex), tableIndex)
}
mutating func visitReturnCall(wat: inout Wat) throws -> UInt32 {
return try visitCall(wat: &wat)
}
mutating func visitReturnCallIndirect(wat: inout Wat) throws -> (typeIndex: UInt32, tableIndex: UInt32) {
return try visitCallIndirect(wat: &wat)
}
mutating func visitTypedSelect(wat: inout Wat) throws -> ValueType {
fatalError("unreachable because Instruction.json does not define the name of typed select and it is handled in parseTextInstruction() manually")
}
Expand Down
10 changes: 10 additions & 0 deletions Sources/WasmParser/BinaryInstructionDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ protocol BinaryInstructionDecoder {
@inlinable mutating func visitCall() throws -> UInt32
/// Decode `call_indirect` immediates
@inlinable mutating func visitCallIndirect() throws -> (typeIndex: UInt32, tableIndex: UInt32)
/// Decode `return_call` immediates
@inlinable mutating func visitReturnCall() throws -> UInt32
/// Decode `return_call_indirect` immediates
@inlinable mutating func visitReturnCallIndirect() throws -> (typeIndex: UInt32, tableIndex: UInt32)
/// Decode `typedSelect` immediates
@inlinable mutating func visitTypedSelect() throws -> ValueType
/// Decode `local.get` immediates
Expand Down Expand Up @@ -122,6 +126,12 @@ func parseBinaryInstruction<V: InstructionVisitor, D: BinaryInstructionDecoder>(
case 0x11:
let (typeIndex, tableIndex) = try decoder.visitCallIndirect()
try visitor.visitCallIndirect(typeIndex: typeIndex, tableIndex: tableIndex)
case 0x12:
let (functionIndex) = try decoder.visitReturnCall()
try visitor.visitReturnCall(functionIndex: functionIndex)
case 0x13:
let (typeIndex, tableIndex) = try decoder.visitReturnCallIndirect()
try visitor.visitReturnCallIndirect(typeIndex: typeIndex, tableIndex: tableIndex)
case 0x1A:
try visitor.visitDrop()
case 0x1B:
Expand Down
12 changes: 12 additions & 0 deletions Sources/WasmParser/InstructionVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ public enum Instruction: Equatable {
case `return`
case `call`(functionIndex: UInt32)
case `callIndirect`(typeIndex: UInt32, tableIndex: UInt32)
case `returnCall`(functionIndex: UInt32)
case `returnCallIndirect`(typeIndex: UInt32, tableIndex: UInt32)
case `drop`
case `select`
case `typedSelect`(type: ValueType)
Expand Down Expand Up @@ -246,6 +248,8 @@ extension AnyInstructionVisitor {
public mutating func visitReturn() throws { return try self.visit(.return) }
public mutating func visitCall(functionIndex: UInt32) throws { return try self.visit(.call(functionIndex: functionIndex)) }
public mutating func visitCallIndirect(typeIndex: UInt32, tableIndex: UInt32) throws { return try self.visit(.callIndirect(typeIndex: typeIndex, tableIndex: tableIndex)) }
public mutating func visitReturnCall(functionIndex: UInt32) throws { return try self.visit(.returnCall(functionIndex: functionIndex)) }
public mutating func visitReturnCallIndirect(typeIndex: UInt32, tableIndex: UInt32) throws { return try self.visit(.returnCallIndirect(typeIndex: typeIndex, tableIndex: tableIndex)) }
public mutating func visitDrop() throws { return try self.visit(.drop) }
public mutating func visitSelect() throws { return try self.visit(.select) }
public mutating func visitTypedSelect(type: ValueType) throws { return try self.visit(.typedSelect(type: type)) }
Expand Down Expand Up @@ -316,6 +320,10 @@ public protocol InstructionVisitor {
mutating func visitCall(functionIndex: UInt32) throws
/// Visiting `call_indirect` instruction.
mutating func visitCallIndirect(typeIndex: UInt32, tableIndex: UInt32) throws
/// Visiting `return_call` instruction.
mutating func visitReturnCall(functionIndex: UInt32) throws
/// Visiting `return_call_indirect` instruction.
mutating func visitReturnCallIndirect(typeIndex: UInt32, tableIndex: UInt32) throws
/// Visiting `drop` instruction.
mutating func visitDrop() throws
/// Visiting `select` instruction.
Expand Down Expand Up @@ -409,6 +417,8 @@ extension InstructionVisitor {
case .return: return try visitReturn()
case let .call(functionIndex): return try visitCall(functionIndex: functionIndex)
case let .callIndirect(typeIndex, tableIndex): return try visitCallIndirect(typeIndex: typeIndex, tableIndex: tableIndex)
case let .returnCall(functionIndex): return try visitReturnCall(functionIndex: functionIndex)
case let .returnCallIndirect(typeIndex, tableIndex): return try visitReturnCallIndirect(typeIndex: typeIndex, tableIndex: tableIndex)
case .drop: return try visitDrop()
case .select: return try visitSelect()
case let .typedSelect(type): return try visitTypedSelect(type: type)
Expand Down Expand Up @@ -465,6 +475,8 @@ extension InstructionVisitor {
public mutating func visitReturn() throws {}
public mutating func visitCall(functionIndex: UInt32) throws {}
public mutating func visitCallIndirect(typeIndex: UInt32, tableIndex: UInt32) throws {}
public mutating func visitReturnCall(functionIndex: UInt32) throws {}
public mutating func visitReturnCallIndirect(typeIndex: UInt32, tableIndex: UInt32) throws {}
public mutating func visitDrop() throws {}
public mutating func visitSelect() throws {}
public mutating func visitTypedSelect(type: ValueType) throws {}
Expand Down
15 changes: 14 additions & 1 deletion Sources/WasmParser/WasmParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,14 @@ public struct WasmFeatureSet: OptionSet {
/// The WebAssembly threads proposal
@_alwaysEmitIntoClient
public static var threads: WasmFeatureSet { WasmFeatureSet(rawValue: 1 << 2) }
/// The WebAssembly tail-call proposal
@_alwaysEmitIntoClient
public static var tailCall: WasmFeatureSet { WasmFeatureSet(rawValue: 1 << 3) }

/// The default feature set
public static let `default`: WasmFeatureSet = [.referenceTypes]
/// The feature set with all features enabled
public static let all: WasmFeatureSet = [.memory64, .referenceTypes, .threads]
public static let all: WasmFeatureSet = [.memory64, .referenceTypes, .threads, .tailCall]
}

/// An error that occurs during parsing of a WebAssembly binary
Expand Down Expand Up @@ -629,6 +632,16 @@ extension Parser: BinaryInstructionDecoder {
return (typeIndex, tableIndex)
}

@inlinable mutating func visitReturnCall() throws -> UInt32 {
try parseUnsigned()
}

@inlinable mutating func visitReturnCallIndirect() throws -> (typeIndex: UInt32, tableIndex: UInt32) {
let typeIndex: TypeIndex = try parseUnsigned()
let tableIndex: TableIndex = try parseUnsigned()
return (typeIndex, tableIndex)
}

@inlinable mutating func visitTypedSelect() throws -> WasmTypes.ValueType {
let results = try parseVector { try parseValueType() }
guard results.count == 1 else {
Expand Down
7 changes: 6 additions & 1 deletion Tests/WATTests/EncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ class EncoderTests: XCTestCase {

let wast2jsonProcess = try Process.run(
wast2json,
arguments: [wastFile.path, "-o", json.path]
arguments: [
wastFile.path,
"--enable-memory64",
"--enable-tail-call",
"-o", json.path,
]
)
wast2jsonProcess.waitUntilExit()

Expand Down
1 change: 1 addition & 0 deletions Tests/WATTests/Spectest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ enum Spectest {
var allFiles = [
testsuitePath,
testsuitePath.appendingPathComponent("proposals/memory64"),
testsuitePath.appendingPathComponent("proposals/tail-call"),
rootDirectory.appendingPathComponent("Tests/WasmKitTests/ExtraSuite"),
].flatMap {
try! FileManager.default.contentsOfDirectory(at: $0, includingPropertiesForKeys: nil)
Expand Down
2 changes: 2 additions & 0 deletions Utilities/Instructions.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
["mvp" , "return" , ["0x0F"] , [] , null ],
["mvp" , "call" , ["0x10"] , [["functionIndex", "UInt32"]] , null ],
["mvp" , "call_indirect" , ["0x11"] , [["typeIndex", "UInt32"], ["tableIndex", "UInt32"]], null ],
["tailCall" , "return_call" , ["0x12"] , [["functionIndex", "UInt32"]] , null ],
["tailCall" , "return_call_indirect" , ["0x13"] , [["typeIndex", "UInt32"], ["tableIndex", "UInt32"]], null ],
["mvp" , "drop" , ["0x1A"] , [] , null ],
["mvp" , "select" , ["0x1B"] , [] , null ],
["referenceTypes" , {"enumCase": "typedSelect"}, ["0x1C"] , [["type", "ValueType"]] , null ],
Expand Down
Loading