Skip to content

Commit 508e518

Browse files
Add more detailed documentation to the resizeFrameHeader instruction
1 parent 7783332 commit 508e518

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

Sources/WasmKit/Execution/Execution.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -538,17 +538,15 @@ extension Execution {
538538
func tailInvoke(
539539
function: InternalFunction,
540540
callerInstance: InternalInstance?,
541-
spAddend: VReg,
542541
sp: Sp, pc: Pc, md: inout Md, ms: inout Ms
543542
) throws -> (Pc, Sp) {
544543
if function.isWasm {
545544
return try tailInvokeWasmFunction(
546545
function: function.wasm, callerInstance: callerInstance,
547-
spAddend: spAddend,
548546
sp: sp, md: &md, ms: &ms
549547
)
550548
} else {
551-
try invokeHostFunction(function: function.host, sp: sp, spAddend: spAddend)
549+
try invokeHostFunction(function: function.host, sp: sp, spAddend: 0)
552550
return (pc, sp)
553551
}
554552
}
@@ -561,21 +559,19 @@ extension Execution {
561559
private func tailInvokeWasmFunction(
562560
function: EntityHandle<WasmFunctionEntity>,
563561
callerInstance: InternalInstance?,
564-
spAddend: VReg,
565562
sp: Sp, md: inout Md, ms: inout Ms
566563
) throws -> (Pc, Sp) {
567564
let iseq = try function.ensureCompiled(store: store)
568-
let newSp = sp.advanced(by: Int(spAddend))
569-
try checkStackBoundary(newSp.advanced(by: iseq.maxStackHeight))
570-
newSp.currentFunction = function
565+
try checkStackBoundary(sp.advanced(by: iseq.maxStackHeight))
566+
sp.currentFunction = function
571567

572-
initializeConstSlots(sp: newSp, iseq: iseq, numberOfNonParameterLocals: function.numberOfNonParameterLocals)
568+
initializeConstSlots(sp: sp, iseq: iseq, numberOfNonParameterLocals: function.numberOfNonParameterLocals)
573569

574570
Execution.CurrentMemory.mayUpdateCurrentInstance(
575571
instance: function.instance,
576572
from: callerInstance, md: &md, ms: &ms
577573
)
578-
return (iseq.baseAddress, newSp)
574+
return (iseq.baseAddress, sp)
579575
}
580576

581577
/// Executes the given WebAssembly function.

Sources/WasmKit/Execution/Instructions/Control.swift

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,12 @@ extension Execution {
151151
)
152152
return pc.next()
153153
}
154-
154+
155155
mutating func returnCall(sp: inout Sp, pc: Pc, md: inout Md, ms: inout Ms, immediate: Instruction.ReturnCallOperand) throws -> (Pc, CodeSlot) {
156156
var pc = pc
157157
(pc, sp) = try tailInvoke(
158158
function: immediate.callee,
159159
callerInstance: currentInstance(sp: sp),
160-
spAddend: 0,
161160
sp: sp, pc: pc, md: &md, ms: &ms
162161
)
163162
return pc.next()
@@ -172,13 +171,38 @@ extension Execution {
172171
(pc, sp) = try tailInvoke(
173172
function: function,
174173
callerInstance: callerInstance,
175-
spAddend: 0,
176174
sp: sp, pc: pc, md: &md, ms: &ms
177175
)
178176
return pc.next()
179177
}
180178

181179
mutating func resizeFrameHeader(sp: inout Sp, immediate: Instruction.ResizeFrameHeaderOperand) throws {
180+
// The params/results space are resized by `delta` slots and the rest of the
181+
// frame is copied to the new location. See the following diagram for the
182+
// layout of the frame before and after the resize operation:
183+
//
184+
//
185+
// |--------BEFORE-------| |--------AFTER--------|
186+
// | Params | Results | | Params | Results |
187+
// | ... | ... | | ... | ... |
188+
// Old Header ->|---------------------|\ | ... | ... | -+
189+
// | Sp | \ | ... | ... | | delta
190+
// |---------------------| \|---------------------|<- New Header -+ -+
191+
// | Pc | | Sp | |
192+
// |---------------------| |---------------------| |
193+
// | Current Func | C | Pc | |
194+
// Old Sp ->|---------------------| O |---------------------| |
195+
// | Locals | P | Current Func | |
196+
// | ... | Y |---------------------|<- New Sp |
197+
// |---------------------| | Locals | | sizeToCopy
198+
// | Consts | | ... | |
199+
// | ... | |---------------------| |
200+
// |---------------------| | Consts | |
201+
// | Value Stack | | ... | |
202+
// | ... | |---------------------| |
203+
// |---------------------|\ | Value Stack | |
204+
// \ | ... | |
205+
// \|---------------------| -+
182206
let newSp = sp.advanced(by: Int(immediate.delta))
183207
try checkStackBoundary(newSp)
184208
let oldFrameHeader = sp.advanced(by: -FrameHeaderLayout.numberOfSavingSlots)

Sources/WasmKit/Execution/Instructions/Instruction.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ enum Instruction: Equatable {
2525
case internalCall(Instruction.CallOperand)
2626
/// WebAssembly Core Instruction `call_indirect`
2727
case callIndirect(Instruction.CallIndirectOperand)
28-
/// Resize the frame header
28+
/// Resize the frame header by increasing param/result slots and copying `sizeToCopy`
29+
/// slots placed after the header
2930
case resizeFrameHeader(Instruction.ResizeFrameHeaderOperand)
3031
/// WebAssembly Core Instruction `return_call`
3132
case returnCall(Instruction.ReturnCallOperand)

Sources/WasmKit/Translator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private struct MetaProgramCounter {
190190
/// )
191191
/// ```
192192
///
193-
/// Then the stack frame layout looks like:
193+
/// Then the stack frame layout looks like:
194194
///
195195
/// ```
196196
/// | Offset | Description |

Utilities/Sources/VMSpec/Instruction.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,10 @@ extension VMGen {
543543
$0.field(name: "index", type: .VReg)
544544
$0.field(name: "spAddend", type: .VReg)
545545
},
546-
Instruction(name: "resizeFrameHeader", documentation: "Resize the frame header",
546+
Instruction(name: "resizeFrameHeader", documentation: """
547+
Resize the frame header by increasing param/result slots and copying `sizeToCopy`
548+
slots placed after the header
549+
""",
547550
mayThrow: true, mayUpdateFrame: true) {
548551
$0.field(name: "delta", type: .VReg)
549552
$0.field(name: "sizeToCopy", type: .VReg)

0 commit comments

Comments
 (0)