From 69d8f17a26db50e7b76297dbf588aafc23425c71 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 27 Aug 2022 00:21:08 +0300 Subject: feat(tvix/eval): compile creation of closure objects Fully implements the instructions for compiling closure objects (without runtime handling yet). Closure (and thunk) objects are created at runtime by capturing all known upvalues. To represent this, the instructions for creating them need to have a variable number of arguments. Due to that, this commit introduces new variants in OpCode that are not actually operations, but data. If the VM is implemented correctly, the instruction pointer should never point at these. Due to this, the VM will panic if it sees a data operand during an execution run. Change-Id: Ic56b49b3a42736dc437751e76df0e89c8d0619c6 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6291 Tested-by: BuildkiteCI Reviewed-by: grfn --- tvix/eval/src/compiler/mod.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'tvix/eval/src/compiler/mod.rs') diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs index 3ade6831c286..0c41f06cbde1 100644 --- a/tvix/eval/src/compiler/mod.rs +++ b/tvix/eval/src/compiler/mod.rs @@ -856,9 +856,27 @@ impl Compiler { crate::disassembler::disassemble_chunk(&compiled.lambda.chunk); } - self.emit_constant(Value::Closure(Closure { - lambda: compiled.lambda, - })); + // If the function is not a closure, just emit it directly and + // move on. + if compiled.lambda.upvalue_count == 0 { + self.emit_constant(Value::Closure(Closure::new(compiled.lambda))); + return; + } + + // If the function is a closure, we need to emit the variable + // number of operands that allow the runtime to close over the + // upvalues. + let closure_idx = self + .chunk() + .push_constant(Value::Closure(Closure::new(compiled.lambda))); + + self.chunk().push_op(OpCode::OpClosure(closure_idx)); + for upvalue in compiled.scope.upvalues { + match upvalue { + Upvalue::Stack(idx) => self.chunk().push_op(OpCode::DataLocalIdx(idx)), + Upvalue::Upvalue(idx) => self.chunk().push_op(OpCode::DataUpvalueIdx(idx)), + }; + } } fn compile_apply(&mut self, node: ast::Apply) { -- cgit 1.4.1