about summary refs log tree commit diff
path: root/tvix/eval/src/disassembler.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tvix/eval/src/disassembler.rs')
-rw-r--r--tvix/eval/src/disassembler.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/tvix/eval/src/disassembler.rs b/tvix/eval/src/disassembler.rs
index 98a6dac9afc4..e5f6df525ad4 100644
--- a/tvix/eval/src/disassembler.rs
+++ b/tvix/eval/src/disassembler.rs
@@ -4,6 +4,7 @@
 use std::io::{Stderr, Write};
 use tabwriter::TabWriter;
 
+use crate::chunk::Chunk;
 use crate::opcode::OpCode;
 use crate::value::Value;
 
@@ -35,3 +36,33 @@ impl Drop for Tracer {
         self.0.flush().ok();
     }
 }
+
+fn disassemble_op(tw: &mut TabWriter<Stderr>, chunk: &Chunk, offset: usize) {
+    let code_width = format!("{}", chunk.code.len()).len();
+    write!(tw, "{:0width$}\t ", width = code_width).ok();
+
+    match chunk.code[offset] {
+        OpCode::OpConstant(idx) => write!(tw, "OpConstant({})\n", chunk.constant(idx)).ok(),
+
+        op => write!(tw, "{:?}\n", op).ok(),
+    };
+}
+
+/// Disassemble a chunk of code, printing out the operations in a
+/// reasonable, human-readable format.
+pub fn disassemble_chunk(chunk: &Chunk) {
+    let mut tw = TabWriter::new(std::io::stderr());
+
+    write!(
+        &mut tw,
+        "=== compiled bytecode ({} operations) ===\n",
+        chunk.code.len()
+    )
+    .ok();
+
+    for (idx, _) in chunk.code.iter().enumerate() {
+        disassemble_op(&mut tw, chunk, idx);
+    }
+
+    tw.flush().ok();
+}