From 2e018a50a74040d8db5c0dfeae5f829a5c7c0cf2 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 4 Sep 2022 23:16:59 +0300 Subject: feat(tvix/eval): implement OpTailCall If the last operation within a chunk is a function call, the call can be executed in the same call frame without increasing the depth of the call stack. To enable this, a new OpTailCall instruction (similar to OpCall) is introduced, but not yet emitted by the compiler. Change-Id: I9ffbd7da6d2d6a8ec7a724646435dc6ee89712f2 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6457 Tested-by: BuildkiteCI Reviewed-by: sterni --- tvix/eval/src/vm.rs | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) (limited to 'tvix/eval/src/vm.rs') diff --git a/tvix/eval/src/vm.rs b/tvix/eval/src/vm.rs index b65a37582f..a7b9bf3e92 100644 --- a/tvix/eval/src/vm.rs +++ b/tvix/eval/src/vm.rs @@ -9,7 +9,7 @@ use crate::{ observer::Observer, opcode::{CodeIdx, ConstantIdx, Count, JumpOffset, OpCode, StackIdx, UpvalueIdx}, upvalues::UpvalueCarrier, - value::{Closure, Lambda, NixAttrs, NixList, Thunk, Value}, + value::{Builtin, Closure, Lambda, NixAttrs, NixList, Thunk, Value}, }; struct CallFrame { @@ -447,19 +447,33 @@ impl<'o> VM<'o> { self.push(result) } - Value::Builtin(builtin) => { - let builtin_name = builtin.name(); - self.observer.observe_enter_builtin(builtin_name); + Value::Builtin(builtin) => self.call_builtin(builtin)?, - let arg = self.pop(); - let result = fallible!(self, builtin.apply(self, arg)); + _ => return Err(self.error(ErrorKind::NotCallable)), + }; + } - self.observer.observe_exit_builtin(builtin_name); + OpCode::OpTailCall => { + let callable = self.pop(); - self.push(result); + match callable { + Value::Builtin(builtin) => self.call_builtin(builtin)?, + + Value::Closure(closure) => { + let lambda = closure.lambda(); + self.observer.observe_tail_call(self.frames.len(), &lambda); + + // Replace the current call frames + // internals with that of the tail-called + // closure. + let mut frame = self.frame_mut(); + frame.lambda = lambda; + frame.upvalues = closure.upvalues().to_vec(); + frame.ip = 0; // reset instruction pointer to beginning } + _ => return Err(self.error(ErrorKind::NotCallable)), - }; + } } OpCode::OpGetUpvalue(upv_idx) => { @@ -699,6 +713,20 @@ impl<'o> VM<'o> { _ => Ok(()), } } + + fn call_builtin(&mut self, builtin: Builtin) -> EvalResult<()> { + let builtin_name = builtin.name(); + self.observer.observe_enter_builtin(builtin_name); + + let arg = self.pop(); + let result = fallible!(self, builtin.apply(self, arg)); + + self.observer.observe_exit_builtin(builtin_name); + + self.push(result); + + Ok(()) + } } // TODO: use Rc::unwrap_or_clone once it is stabilised. -- cgit 1.4.1