From d3421c1cb9fc52a583b888e5040ea6d60a4d02ac Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 3 Sep 2022 14:50:50 +0300 Subject: fix(tvix/eval): ensure disassembler prints continous lines correctly There can be different spans on the same line, so the previous implementation would duplicate line numbers unnecessarily. Change-Id: I8d8db77177aee0d834a6ec3584641e1bd5f31c3e Reviewed-on: https://cl.tvl.fyi/c/depot/+/6434 Reviewed-by: sterni Tested-by: BuildkiteCI --- tvix/eval/src/chunk.rs | 9 +++++++++ tvix/eval/src/disassembler.rs | 7 +++---- 2 files changed, 12 insertions(+), 4 deletions(-) (limited to 'tvix') diff --git a/tvix/eval/src/chunk.rs b/tvix/eval/src/chunk.rs index defa409f18d6..c89e6ef84810 100644 --- a/tvix/eval/src/chunk.rs +++ b/tvix/eval/src/chunk.rs @@ -77,4 +77,13 @@ impl Chunk { panic!("compiler error: chunk missing span for offset {}", offset.0); } + + /// Retrieve the line from which the instruction at `offset` was + /// compiled. Only available when the chunk carries a codemap, + /// i.e. when the disassembler is enabled. + #[cfg(feature = "disassembler")] + pub fn get_line(&self, offset: CodeIdx) -> usize { + let span = self.get_span(offset); + self.codemap.look_up_span(span).begin.line + 1 + } } diff --git a/tvix/eval/src/disassembler.rs b/tvix/eval/src/disassembler.rs index b089797f8ab1..7948977e4b55 100644 --- a/tvix/eval/src/disassembler.rs +++ b/tvix/eval/src/disassembler.rs @@ -44,13 +44,12 @@ impl Drop for Tracer { fn disassemble_op(tw: &mut TabWriter, chunk: &Chunk, width: usize, offset: usize) { let _ = write!(tw, "{:0width$}\t ", offset, width = width); - let span = chunk.get_span(CodeIdx(offset)); + let line = chunk.get_line(CodeIdx(offset)); - if offset > 0 && chunk.get_span(CodeIdx(offset - 1)) == span { + if offset > 0 && chunk.get_line(CodeIdx(offset - 1)) == line { write!(tw, " |\t").unwrap(); } else { - let loc = chunk.codemap.look_up_span(span); - write!(tw, "{:4}\t", loc.begin.line + 1).unwrap(); + write!(tw, "{:4}\t", line).unwrap(); } let _ = match chunk.code[offset] { -- cgit 1.4.1