summaryrefslogtreecommitdiff
path: root/vendor/github.com/tetratelabs/wazero/internal/engine/interpreter/compiler.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/tetratelabs/wazero/internal/engine/interpreter/compiler.go')
-rw-r--r--vendor/github.com/tetratelabs/wazero/internal/engine/interpreter/compiler.go45
1 files changed, 44 insertions, 1 deletions
diff --git a/vendor/github.com/tetratelabs/wazero/internal/engine/interpreter/compiler.go b/vendor/github.com/tetratelabs/wazero/internal/engine/interpreter/compiler.go
index 4e20e4b2c..4269d237b 100644
--- a/vendor/github.com/tetratelabs/wazero/internal/engine/interpreter/compiler.go
+++ b/vendor/github.com/tetratelabs/wazero/internal/engine/interpreter/compiler.go
@@ -814,6 +814,7 @@ operatorSwitch:
c.emit(
newOperationCallIndirect(typeIndex, tableIndex),
)
+
case wasm.OpcodeDrop:
r := inclusiveRange{Start: 0, End: 0}
if peekValueType == unsignedTypeV128 {
@@ -3423,6 +3424,45 @@ operatorSwitch:
default:
return fmt.Errorf("unsupported atomic instruction in interpreterir: %s", wasm.AtomicInstructionName(atomicOp))
}
+
+ case wasm.OpcodeTailCallReturnCall:
+ fdef := c.module.FunctionDefinition(index)
+ functionFrame := c.controlFrames.functionFrame()
+ // Currently we do not support imported functions, we treat them as regular calls.
+ // For details, see internal/engine/RATIONALE.md
+ if _, _, isImport := fdef.Import(); isImport {
+ c.emit(newOperationCall(index))
+ dropOp := newOperationDrop(c.getFrameDropRange(functionFrame, false))
+
+ // Cleanup the stack and then jmp to function frame's continuation (meaning return).
+ c.emit(dropOp)
+ c.emit(newOperationBr(functionFrame.asLabel()))
+ } else {
+ c.emit(newOperationTailCallReturnCall(index))
+ }
+
+ // Return operation is stack-polymorphic, and mark the state as unreachable.
+ // That means subsequent instructions in the current control frame are "unreachable"
+ // and can be safely removed.
+ c.markUnreachable()
+
+ case wasm.OpcodeTailCallReturnCallIndirect:
+ typeIndex := index
+ tableIndex, n, err := leb128.LoadUint32(c.body[c.pc+1:])
+ if err != nil {
+ return fmt.Errorf("read target for br_table: %w", err)
+ }
+ c.pc += n
+
+ functionFrame := c.controlFrames.functionFrame()
+ dropRange := c.getFrameDropRange(functionFrame, false)
+ c.emit(newOperationTailCallReturnCallIndirect(typeIndex, tableIndex, dropRange, functionFrame.asLabel()))
+
+ // Return operation is stack-polymorphic, and mark the state as unreachable.
+ // That means subsequent instructions in the current control frame are "unreachable"
+ // and can be safely removed.
+ c.markUnreachable()
+
default:
return fmt.Errorf("unsupported instruction in interpreterir: 0x%x", op)
}
@@ -3449,7 +3489,10 @@ func (c *compiler) applyToStack(opcode wasm.Opcode) (index uint32, err error) {
wasm.OpcodeLocalSet,
wasm.OpcodeLocalTee,
wasm.OpcodeGlobalGet,
- wasm.OpcodeGlobalSet:
+ wasm.OpcodeGlobalSet,
+ // tail-call proposal
+ wasm.OpcodeTailCallReturnCall,
+ wasm.OpcodeTailCallReturnCallIndirect:
// Assumes that we are at the opcode now so skip it before read immediates.
v, num, err := leb128.LoadUint32(c.body[c.pc+1:])
if err != nil {