From fb4c197b39910d3278cc5859eaffda0b8f6ffe88 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Wed, 22 Feb 2023 14:18:09 +0300 Subject: refactor(tvix/eval): enhance debug output for bytecode dumps This adds addresses of thunk and closure chunks to the debug output displayed when dumping bytecode. This makes it possible to see in the dump which thunks are referenced by constants in other thunks. Change-Id: I2c98de5227e7cb415666cd3134c947a56979dc80 Reviewed-on: https://cl.tvl.fyi/c/depot/+/8137 Autosubmit: tazjin Reviewed-by: flokli Tested-by: BuildkiteCI --- tvix/eval/src/chunk.rs | 10 +++++++++- tvix/eval/src/value/thunk.rs | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) (limited to 'tvix/eval') diff --git a/tvix/eval/src/chunk.rs b/tvix/eval/src/chunk.rs index 35fd7e78e674..04b58bde20fd 100644 --- a/tvix/eval/src/chunk.rs +++ b/tvix/eval/src/chunk.rs @@ -143,7 +143,15 @@ impl Chunk { } match self[idx] { - OpCode::OpConstant(idx) => writeln!(writer, "OpConstant({}@{})", self[idx], idx.0), + OpCode::OpConstant(idx) => { + let val_str = match &self[idx] { + Value::Thunk(t) => t.debug_repr(), + Value::Closure(c) => format!("closure({:p})", c.lambda), + val => format!("{}", val), + }; + + writeln!(writer, "OpConstant({}@{})", val_str, idx.0) + } op => writeln!(writer, "{:?}", op), }?; diff --git a/tvix/eval/src/value/thunk.rs b/tvix/eval/src/value/thunk.rs index e30d58e72849..747cd413e23e 100644 --- a/tvix/eval/src/value/thunk.rs +++ b/tvix/eval/src/value/thunk.rs @@ -73,6 +73,17 @@ enum ThunkRepr { Evaluated(Value), } +impl ThunkRepr { + fn debug_repr(&self) -> String { + match self { + ThunkRepr::Evaluated(v) => format!("thunk(val|{})", v), + ThunkRepr::Blackhole => "thunk(blackhole)".to_string(), + ThunkRepr::Native(_) => "thunk(native)".to_string(), + ThunkRepr::Suspended { lambda, .. } => format!("thunk({:p})", *lambda), + } + } +} + /// A thunk is created for any value which requires non-strict /// evaluation due to self-reference or lazy semantics (or both). /// Every reference cycle involving `Value`s will contain at least @@ -428,6 +439,11 @@ impl Thunk { _ => false, } } + + /// Helper function to format thunks in observer output. + pub(crate) fn debug_repr(&self) -> String { + self.0.borrow().debug_repr() + } } impl TotalDisplay for Thunk { -- cgit 1.4.1