Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rebis-dev: Fix Heap::compute_pstr_size being off by one cell #2759

Open
wants to merge 13 commits into
base: rebis-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benches/setup.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::BTreeMap, fs, path::Path};

use maplit::btreemap;
use scryer_prolog::{Machine, QueryResolution, Value};
use scryer_prolog::{QueryResolution, Value, Machine};

pub fn prolog_benches() -> BTreeMap<&'static str, PrologBenchmark> {
[
Expand Down
396 changes: 158 additions & 238 deletions build/instructions_template.rs

Large diffs are not rendered by default.

39 changes: 33 additions & 6 deletions build/static_string_indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ impl<'ast> Visit<'ast> for StaticStrVisitor {
}
}

const INLINED_ATOM_MAX_LEN: usize = 6;

fn static_string_index(string: &str, index: usize) -> u64 {
if 0 < string.len() && string.len() <= INLINED_ATOM_MAX_LEN {
let mut string_buf: [u8; 8] = [0u8; 8];
string_buf[.. string.len()].copy_from_slice(string.as_bytes());
(u64::from_le_bytes(string_buf) << 1) | 1
} else {
(index << 1) as u64
}
}

pub fn index_static_strings(instruction_rs_path: &std::path::Path) -> TokenStream {
use quote::*;

Expand Down Expand Up @@ -149,11 +161,26 @@ pub fn index_static_strings(instruction_rs_path: &std::path::Path) -> TokenStrea
visitor.visit_file(&syntax)
}

let indices = (0..visitor.static_strs.len()).map(|i| (i << 3) as u64);
let indices_iter = indices.clone();
let mut static_str_keys = vec![];
let mut static_strs = vec![];
let mut static_str_indices = vec![];

let indices: Vec<u64> = visitor.static_strs.iter().map(|string| {
let index = static_string_index(string, static_strs.len());

static_str_keys.push(string);

if index & 1 == 1 {
index
} else {
static_str_indices.push(index);
static_strs.push(string);
index
}
}).collect();

let static_strs_len = visitor.static_strs.len();
let static_strs: &Vec<_> = &visitor.static_strs.into_iter().collect();
let static_strs_len = static_strs.len(); // visitor.static_strs.len();
//let static_strs: &Vec<_> = &visitor.static_strs.into_iter().collect();

quote! {
static STRINGS: [&str; #static_strs_len] = [
Expand All @@ -163,11 +190,11 @@ pub fn index_static_strings(instruction_rs_path: &std::path::Path) -> TokenStrea
];

macro_rules! atom {
#((#static_strs) => { Atom { index: #indices_iter } };)*
#((#static_str_keys) => { Atom { index: #indices } };)*
}

pub static STATIC_ATOMS_MAP: phf::Map<&'static str, Atom> = phf::phf_map! {
#(#static_strs => { Atom { index: #indices } },)*
#(#static_strs => { Atom { index: #static_str_indices } },)*
};
}
}
19 changes: 8 additions & 11 deletions src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use crate::parser::ast::*;

use crate::forms::*;
use crate::instructions::*;
use crate::machine::heap::Heap;
use crate::targets::*;

use std::cell::Cell;

pub(crate) trait Allocator {
fn new() -> Self;

Expand All @@ -14,42 +13,40 @@ pub(crate) trait Allocator {
lvl: Level,
context: GenContext,
code: &mut CodeDeque,
);
) -> RegType;

fn mark_non_var<'a, Target: CompilationTarget<'a>>(
&mut self,
lvl: Level,
heap_loc: usize,
context: GenContext,
cell: &'a Cell<RegType>,
code: &mut CodeDeque,
);
) -> RegType;

#[allow(clippy::too_many_arguments)]
fn mark_reserved_var<'a, Target: CompilationTarget<'a>>(
&mut self,
var_num: usize,
lvl: Level,
cell: &Cell<VarReg>,
term_loc: GenContext,
context: GenContext,
code: &mut CodeDeque,
r: RegType,
is_new_var: bool,
);
) -> RegType;

fn mark_cut_var(&mut self, var_num: usize, chunk_num: usize) -> RegType;

fn mark_var<'a, Target: CompilationTarget<'a>>(
&mut self,
var_num: usize,
lvl: Level,
cell: &Cell<VarReg>,
context: GenContext,
code: &mut CodeDeque,
);
) -> RegType;

fn reset(&mut self);
fn reset_arg(&mut self, arg_num: usize);
fn reset_at_head(&mut self, args: &[Term]);
fn reset_at_head(&mut self, heap: &mut Heap, head_loc: usize);
fn reset_contents(&mut self);

fn advance_arg(&mut self);
Expand Down
37 changes: 6 additions & 31 deletions src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ mod tests {
use crate::arena::*;
use crate::atom_table::*;
use crate::machine::mock_wam::*;
use crate::machine::partial_string::*;
use crate::types::*;

use crate::parser::dashu::{Integer, Rational};
use ordered_float::OrderedFloat;
Expand Down Expand Up @@ -980,7 +980,7 @@ mod tests {

assert!(!big_int_ptr.as_ptr().is_null());

let cell = HeapCellValue::from(Literal::Integer(big_int_ptr));
let cell = HeapCellValue::from(big_int_ptr);
assert_eq!(cell.get_tag(), HeapCellValueTag::Cons);

let untyped_arena_ptr = match cell.to_untyped_arena_ptr() {
Expand Down Expand Up @@ -1086,31 +1086,6 @@ mod tests {
_ => { unreachable!() }
);

// complete string

let pstr_var_cell =
put_partial_string(&mut wam.machine_st.heap, "ronan", &wam.machine_st.atom_tbl);
let pstr_cell = wam.machine_st.heap[pstr_var_cell.get_value() as usize];

assert_eq!(pstr_cell.get_tag(), HeapCellValueTag::PStr);

match pstr_cell.to_pstr() {
Some(pstr) => {
assert_eq!(&*pstr.as_str_from(0), "ronan");
}
None => {
unreachable!();
}
}

read_heap_cell!(pstr_cell,
(HeapCellValueTag::PStr, pstr_atom) => {
let pstr = PartialString::from(pstr_atom);
assert_eq!(&*pstr.as_str_from(0), "ronan");
}
_ => { unreachable!() }
);

// fixnum

let fixnum_cell = fixnum_as_cell!(Fixnum::build_with(3));
Expand Down Expand Up @@ -1188,8 +1163,8 @@ mod tests {
let char_cell = char_as_cell!(c);

read_heap_cell!(char_cell,
(HeapCellValueTag::Char, c) => {
assert_eq!(c, 'c');
(HeapCellValueTag::Atom, (c, _arity)) => {
assert_eq!(&*c.as_str(), "c");
}
_ => { unreachable!() }
);
Expand All @@ -1198,8 +1173,8 @@ mod tests {
let cyrillic_char_cell = char_as_cell!(c);

read_heap_cell!(cyrillic_char_cell,
(HeapCellValueTag::Char, c) => {
assert_eq!(c, 'Ћ');
(HeapCellValueTag::Atom, (c, _arity)) => {
assert_eq!(&*c.as_str(), "Ћ");
}
_ => { unreachable!() }
);
Expand Down
Loading
Loading