From: Mark Thom Date: Fri, 22 Jul 2022 19:10:12 +0000 (-0600) Subject: print index_ptr offset (#1534) X-Git-Tag: v0.9.1^2~53 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=ec9c7632118c6cc4599264997a50914f8b33e6b2;p=scryer-prolog.git print index_ptr offset (#1534) --- diff --git a/build/static_string_indexing.rs b/build/static_string_indexing.rs index 6f57b37f..ca5ac765 100644 --- a/build/static_string_indexing.rs +++ b/build/static_string_indexing.rs @@ -66,7 +66,7 @@ impl<'ast> Visit<'ast> for StaticStrVisitor { if let Some(Lit::Str(string)) = m.parse_body::().ok() { self.static_strs.insert(string.value()); } - } else if path.is_ident("read_heap_cell") { + } else if path.is_ident("read_heap_cell") || path.is_ident("match_untyped_arena_ptr") { if let Some(m) = m.parse_body::().ok() { self.visit_expr(&m.expr); diff --git a/src/heap_print.rs b/src/heap_print.rs index 28c2e372..54f52cd6 100644 --- a/src/heap_print.rs +++ b/src/heap_print.rs @@ -762,7 +762,6 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { false } - // TODO: remove ClauseType here? fn format_clause( &mut self, max_depth: usize, @@ -1376,6 +1375,31 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { } } + fn print_index_ptr(&mut self, index_ptr: IndexPtr, max_depth: usize) { + if self.format_struct(max_depth, 1, atom!("$index_ptr")) { + let atom = self.state_stack.pop().unwrap(); + + self.state_stack.pop(); + self.state_stack.pop(); + + let offset = if index_ptr.is_undefined() || index_ptr.is_dynamic_undefined() { + TokenOrRedirect::Atom(atom!("undefined")) + } else { + let idx = index_ptr.p() as i64; + + TokenOrRedirect::NumberFocus( + max_depth, + NumberFocus::Unfocused(Number::Fixnum(Fixnum::build_with(idx))), + None, + ) + }; + + self.state_stack.push(offset); + self.state_stack.push(TokenOrRedirect::Open); + self.state_stack.push(atom); + } + } + fn print_stream(&mut self, stream: Stream, max_depth: usize) { if let Some(alias) = stream.options().get_alias() { self.print_atom(alias); @@ -1523,13 +1547,13 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { self.print_stream(stream, max_depth); } (ArenaHeaderTag::OssifiedOpDir, _op_dir) => { - append_str!(self, "'$ossified_op_dir'"); + self.print_atom(atom!("$ossified_op_dir")); } (ArenaHeaderTag::Dropped, _value) => { - append_str!(self, "'$dropped_value'"); + self.print_atom(atom!("$dropped_value")); } - (ArenaHeaderTag::IndexPtr, _index_ptr) => { - append_str!(self, "'$index_ptr'"); + (ArenaHeaderTag::IndexPtr, index_ptr) => { + self.print_index_ptr(*index_ptr, max_depth); } _ => { } diff --git a/src/machine/machine_indices.rs b/src/machine/machine_indices.rs index 26776d6f..1fcd41e4 100644 --- a/src/machine/machine_indices.rs +++ b/src/machine/machine_indices.rs @@ -92,6 +92,7 @@ pub struct IndexPtr { } impl IndexPtr { + #[inline(always)] pub(crate) fn dynamic_undefined() -> Self { IndexPtr::new() .with_p(0) @@ -99,6 +100,7 @@ impl IndexPtr { .with_tag(IndexPtrTag::DynamicUndefined) } + #[inline(always)] pub(crate) fn undefined() -> Self { IndexPtr::new() .with_p(0) @@ -106,6 +108,7 @@ impl IndexPtr { .with_tag(IndexPtrTag::Undefined) } + #[inline(always)] pub(crate) fn dynamic_index(p: usize) -> Self { IndexPtr::new() .with_p(p as u64) @@ -113,12 +116,29 @@ impl IndexPtr { .with_tag(IndexPtrTag::DynamicIndex) } + #[inline(always)] pub(crate) fn index(p: usize) -> Self { IndexPtr::new() .with_p(p as u64) .with_m(false) .with_tag(IndexPtrTag::Index) } + + #[inline(always)] + pub(crate) fn is_undefined(&self) -> bool { + match self.tag() { + IndexPtrTag::Undefined => true, + _ => false, + } + } + + #[inline(always)] + pub(crate) fn is_dynamic_undefined(&self) -> bool { + match self.tag() { + IndexPtrTag::DynamicUndefined => true, + _ => false, + } + } } #[derive(Debug, Clone, Copy, Ord, Hash, PartialOrd, Eq, PartialEq)] @@ -126,6 +146,22 @@ pub struct CodeIndex(TypedArenaPtr); const_assert!(std::mem::align_of::() == 8); +impl Deref for CodeIndex { + type Target = TypedArenaPtr; + + #[inline(always)] + fn deref(&self) -> &TypedArenaPtr { + &self.0 + } +} + +impl DerefMut for CodeIndex { + #[inline(always)] + fn deref_mut(&mut self) -> &mut TypedArenaPtr { + &mut self.0 + } +} + impl From for UntypedArenaPtr { #[inline(always)] fn from(ptr: CodeIndex) -> UntypedArenaPtr { @@ -158,14 +194,6 @@ impl CodeIndex { CodeIndex::new(IndexPtr::undefined(), arena) } - #[inline(always)] - pub(crate) fn is_undefined(&self) -> bool { - match self.0.tag() { - IndexPtrTag::Undefined => true, // | &IndexPtr::DynamicUndefined => true, - _ => false, - } - } - pub(crate) fn local(&self) -> Option { match self.0.tag() { IndexPtrTag::Index => Some(self.0.p() as usize),