Skip to content

Commit c0f96a8

Browse files
WasmParser: Stop copying the expression bytes before parsing them
1 parent d279ac0 commit c0f96a8

File tree

21 files changed

+171
-162
lines changed

21 files changed

+171
-162
lines changed

Sources/WAT/Encoder.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import WasmParser
2+
import WasmTypes
23

34
struct Encoder {
45
var output: [UInt8] = []

Sources/WAT/NameMapping.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import WasmParser
2+
import WasmTypes
23

34
/// A module field declaration that may have its name
45
protocol NamedModuleFieldDecl {
@@ -126,7 +127,7 @@ struct TypesMap {
126127
return .empty
127128
}
128129
private mutating func resolveBlockType(
129-
signature: WasmParser.FunctionType,
130+
signature: WasmTypes.FunctionType,
130131
resolveSignatureIndex: (inout TypesMap) -> Int
131132
) throws -> BlockType {
132133
if signature.parameters.isEmpty {
@@ -146,7 +147,7 @@ struct TypesMap {
146147
}
147148

148149
/// Resolves a block type from a function type signature
149-
mutating func resolveBlockType(signature: WasmParser.FunctionType) throws -> BlockType {
150+
mutating func resolveBlockType(signature: WasmTypes.FunctionType) throws -> BlockType {
150151
return try resolveBlockType(
151152
signature: signature,
152153
resolveSignatureIndex: {
@@ -171,7 +172,7 @@ struct TypesMap {
171172
case let (indexOrId?, _):
172173
return try nameMapping.resolveIndex(use: indexOrId)
173174
case (nil, let inline):
174-
let inline = inline?.signature ?? WasmParser.FunctionType(parameters: [], results: [])
175+
let inline = inline?.signature ?? WasmTypes.FunctionType(parameters: [], results: [])
175176
return addAnonymousSignature(inline)
176177
}
177178
}
@@ -196,7 +197,7 @@ struct TypesMap {
196197
return (found, Int(index))
197198
case (nil, let inline):
198199
// If no index and no inline type, then it's a function type with no parameters or results
199-
let inline = inline ?? WatParser.FunctionType(signature: WasmParser.FunctionType(parameters: [], results: []), parameterNames: [])
200+
let inline = inline ?? WatParser.FunctionType(signature: WasmTypes.FunctionType(parameters: [], results: []), parameterNames: [])
200201
// Check if the inline type already exists
201202
if let index = indices[inline.signature] {
202203
return (inline, index)

Sources/WAT/ParseInstruction.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// swift-format-ignore-file
2-
// This file is generated by Utilities/generate_inst_visitor.swift
2+
//// Automatically generated by Utilities/Sources/WasmGen.swift
3+
//// DO NOT EDIT DIRECTLY
4+
35
import WasmParser
6+
import WasmTypes
47

58
/// Parses a text instruction, consuming immediate tokens as necessary.
69
/// - Parameters:

Sources/WAT/Parser/ExpressionParser.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import WasmParser
2+
import WasmTypes
23

34
struct ExpressionParser<Visitor: InstructionVisitor> {
45
typealias LocalsMap = NameMapping<WatParser.LocalDecl>

Sources/WAT/Parser/WatParser.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import WasmParser
2+
import WasmTypes
23

34
struct WatParser {
45
var parser: Parser
@@ -40,7 +41,7 @@ struct WatParser {
4041
}
4142

4243
struct FunctionType {
43-
let signature: WasmParser.FunctionType
44+
let signature: WasmTypes.FunctionType
4445
/// Names of the parameters. The number of names must match the number of parameters in `type`.
4546
let parameterNames: [String?]
4647
}
@@ -554,7 +555,7 @@ struct WatParser {
554555
let (params, names) = try params()
555556
let results = try results()
556557
try parser.expect(.rightParen)
557-
return FunctionType(signature: WasmParser.FunctionType(parameters: params, results: results), parameterNames: names)
558+
return FunctionType(signature: WasmTypes.FunctionType(parameters: params, results: results), parameterNames: names)
558559
}
559560

560561
mutating func optionalFunctionType() throws -> FunctionType? {
@@ -563,7 +564,7 @@ struct WatParser {
563564
if results.isEmpty, params.isEmpty {
564565
return nil
565566
}
566-
return FunctionType(signature: WasmParser.FunctionType(parameters: params, results: results), parameterNames: names)
567+
return FunctionType(signature: WasmTypes.FunctionType(parameters: params, results: results), parameterNames: names)
567568
}
568569

569570
mutating func params() throws -> ([ValueType], [String?]) {

Sources/WasmKit/Execution/DispatchInstruction.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//// Automatically generated by Utilities/Sources/VMGen.swift
22
//// DO NOT EDIT DIRECTLY
3+
34
extension Execution {
45
@inline(__always)
56
mutating func doExecute(_ instruction: UInt64, sp: inout Sp, pc: inout Pc, md: inout Md, ms: inout Ms) throws {

Sources/WasmKit/Execution/Errors.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import WasmParser
1+
import WasmTypes
22

33
public enum Trap: Error {
44
// FIXME: for debugging purposes, to be eventually deleted
@@ -10,7 +10,7 @@ public enum Trap: Error {
1010
/// Stack overflow
1111
case stackOverflow
1212
/// The stack value type does not match the expected type
13-
case stackValueTypesMismatch(expected: WasmParser.ValueType, actual: WasmParser.ValueType)
13+
case stackValueTypesMismatch(expected: WasmTypes.ValueType, actual: WasmTypes.ValueType)
1414
/// Too deep call stack
1515
case callStackExhausted
1616

Sources/WasmKit/Execution/Function.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,9 @@ struct WasmFunctionEntity {
191191
functionIndex: index,
192192
intercepting: runtime.value.interceptor != nil
193193
)
194-
let iseq = try translator.translate(
195-
expression: code.expression,
196-
instance: instance
197-
)
194+
let iseq = try code.withValue { code in
195+
try translator.translate(code: code, instance: instance)
196+
}
198197
self.code = .compiled(iseq)
199198
return iseq
200199
}

Sources/WasmKit/Execution/Instructions/Instruction.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//// Automatically generated by Utilities/Sources/VMGen.swift
22
//// DO NOT EDIT DIRECTLY
3+
34
enum Instruction: Equatable {
45
case copyStack(Instruction.CopyStackOperand)
56
case globalGet(Instruction.GlobalGetOperand)

Sources/WasmKit/Execution/Value.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import enum WasmParser.ReferenceType
2-
import enum WasmParser.ValueType
1+
import enum WasmTypes.ReferenceType
2+
import enum WasmTypes.ValueType
33

44
/// > Note:
55
/// <https://webassembly.github.io/spec/core/syntax/types.html#value-types>
66

77

8-
public typealias ReferenceType = WasmParser.ReferenceType
8+
public typealias ReferenceType = WasmTypes.ReferenceType
99

1010
extension Value {
1111
func maybeAddressOffset(_ isMemory64: Bool) -> UInt64? {

0 commit comments

Comments
 (0)