From 675bd36ea52feaed945e2fb39aff79c16d575468 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 9 Sep 2022 10:34:29 +0300 Subject: feat(tvix/eval): add Chunk::pop_op method This is used to drop an already emitted operation from a chunk again and clean up its span tracking. This is required in cases where the compiler has to backtrack. Change-Id: I8112da9427688bb2dec96a2ddd12390f6e9734c3 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6499 Tested-by: BuildkiteCI Reviewed-by: sterni --- tvix/eval/src/chunk.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tvix/eval/src/chunk.rs b/tvix/eval/src/chunk.rs index 7c6e08638a65..052bf0bf69de 100644 --- a/tvix/eval/src/chunk.rs +++ b/tvix/eval/src/chunk.rs @@ -60,6 +60,28 @@ impl Chunk { CodeIdx(idx) } + /// Pop the last operation from the chunk and clean up its tracked + /// span. Used when the compiler backtracks. + pub fn pop_op(&mut self) { + // Simply drop the last op. + self.code.pop(); + + // If the last span only had this op, drop it, otherwise + // decrease its operation counter. + match self.spans.last_mut() { + // If the last span had more than one op, decrease the + // counter. + Some(span) if span.count > 1 => span.count -= 1, + + // Otherwise, drop it. + Some(_) => { + self.spans.pop(); + } + + None => unreachable!(), + } + } + pub fn push_constant(&mut self, data: Value) -> ConstantIdx { let idx = self.constants.len(); self.constants.push(data); -- cgit 1.4.1