about summary refs log tree commit diff
path: root/tvix/eval/src/chunk.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-09-04T15·53+0300
committertazjin <tazjin@tvl.su>2022-09-09T21·10+0000
commitcbf2d2d29293af56d60fa7e04ee1969c18b9845f (patch)
tree2958af3ff31f27791632956641c5f6bcec6d72a1 /tvix/eval/src/chunk.rs
parent3cf5c402091d1e10c26fddf6764f572495f38899 (diff)
refactor(tvix/eval): move `disassemble_op` to the Chunk structure r/4777
Change-Id: Ic6710c609ed647bfa47d673aaf22c4da96c0f319
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6451
Reviewed-by: sterni <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval/src/chunk.rs')
-rw-r--r--tvix/eval/src/chunk.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/tvix/eval/src/chunk.rs b/tvix/eval/src/chunk.rs
index 4d653c2b22..a085da5716 100644
--- a/tvix/eval/src/chunk.rs
+++ b/tvix/eval/src/chunk.rs
@@ -1,5 +1,8 @@
+use std::io::Write;
 use std::ops::Index;
 
+use codemap::CodeMap;
+
 use crate::opcode::{CodeIdx, ConstantIdx, OpCode};
 use crate::value::Value;
 
@@ -97,4 +100,32 @@ impl Chunk {
         // real line numbers
         codemap.look_up_span(span).begin.line + 1
     }
+
+    /// Write the disassembler representation of the operation at
+    /// `idx` to the specified writer.
+    pub fn disassemble_op<W: Write>(
+        &self,
+        writer: &mut W,
+        codemap: &CodeMap,
+        width: usize,
+        idx: CodeIdx,
+    ) -> Result<(), std::io::Error> {
+        write!(writer, "{:#width$x}\t ", idx.0, width = width)?;
+
+        // Print continuation character if the previous operation was at
+        // the same line, otherwise print the line.
+        let line = self.get_line(codemap, idx);
+        if idx.0 > 0 && self.get_line(codemap, CodeIdx(idx.0 - 1)) == line {
+            write!(writer, "   |\t")?;
+        } else {
+            write!(writer, "{:4}\t", line)?;
+        }
+
+        match self[idx] {
+            OpCode::OpConstant(idx) => writeln!(writer, "OpConstant({}@{})", self[idx], idx.0),
+            op => writeln!(writer, "{:?}", op),
+        }?;
+
+        Ok(())
+    }
 }