about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-09-09T07·34+0300
committertazjin <tazjin@tvl.su>2022-09-11T12·26+0000
commit675bd36ea52feaed945e2fb39aff79c16d575468 (patch)
treeeaee1db578d0c9aff0f34bac1c703b053d82095b
parenta0acbfa47070b3fac9fb2c72ece57edb2c9faa10 (diff)
feat(tvix/eval): add Chunk::pop_op method r/4812
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 <sternenseemann@systemli.org>
-rw-r--r--tvix/eval/src/chunk.rs22
1 files changed, 22 insertions, 0 deletions
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);