From cbf2d2d29293af56d60fa7e04ee1969c18b9845f Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 4 Sep 2022 18:53:17 +0300 Subject: refactor(tvix/eval): move `disassemble_op` to the Chunk structure Change-Id: Ic6710c609ed647bfa47d673aaf22c4da96c0f319 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6451 Reviewed-by: sterni Tested-by: BuildkiteCI --- tvix/eval/src/chunk.rs | 31 +++++++++++++++++++++++++++++++ tvix/eval/src/disassembler.rs | 28 +--------------------------- tvix/eval/src/observer.rs | 3 +-- 3 files changed, 33 insertions(+), 29 deletions(-) (limited to 'tvix/eval/src') diff --git a/tvix/eval/src/chunk.rs b/tvix/eval/src/chunk.rs index 4d653c2b22fa..a085da571660 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( + &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(()) + } } diff --git a/tvix/eval/src/disassembler.rs b/tvix/eval/src/disassembler.rs index d7c9c8895c2e..51d39c3910a1 100644 --- a/tvix/eval/src/disassembler.rs +++ b/tvix/eval/src/disassembler.rs @@ -1,12 +1,10 @@ //! Implements methods for disassembling and printing a representation //! of compiled code, as well as tracing the runtime stack during //! execution. -use codemap::CodeMap; use std::io::{Stderr, Write}; use tabwriter::TabWriter; -use crate::chunk::Chunk; -use crate::opcode::{CodeIdx, OpCode}; +use crate::opcode::OpCode; use crate::value::Value; /// Helper struct to trace runtime values and automatically flush the @@ -41,27 +39,3 @@ impl Drop for Tracer { let _ = self.0.flush(); } } - -pub fn disassemble_op( - tw: &mut W, - codemap: &CodeMap, - chunk: &Chunk, - width: usize, - idx: CodeIdx, -) { - let _ = write!(tw, "{:#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 = chunk.get_line(codemap, idx); - if idx.0 > 0 && chunk.get_line(codemap, CodeIdx(idx.0 - 1)) == line { - write!(tw, " |\t").unwrap(); - } else { - write!(tw, "{:4}\t", line).unwrap(); - } - - let _ = match chunk[idx] { - OpCode::OpConstant(idx) => writeln!(tw, "OpConstant({}@{})", chunk[idx], idx.0), - op => writeln!(tw, "{:?}", op), - }; -} diff --git a/tvix/eval/src/observer.rs b/tvix/eval/src/observer.rs index 5aeb344ee954..62312a74ad36 100644 --- a/tvix/eval/src/observer.rs +++ b/tvix/eval/src/observer.rs @@ -10,7 +10,6 @@ use std::rc::Rc; use tabwriter::TabWriter; use crate::chunk::Chunk; -use crate::disassembler::disassemble_op; use crate::opcode::CodeIdx; use crate::value::Lambda; @@ -72,7 +71,7 @@ impl DisassemblingObserver { let width = format!("{:#x}", chunk.code.len() - 1).len(); for (idx, _) in chunk.code.iter().enumerate() { - disassemble_op(&mut self.writer, &self.codemap, chunk, width, CodeIdx(idx)); + let _ = chunk.disassemble_op(&mut self.writer, &self.codemap, width, CodeIdx(idx)); } } } -- cgit 1.4.1