prep for debray allocation

This commit is contained in:
Mark Thom
2017-04-29 16:50:53 -06:00
parent 2e7aa6423c
commit a6304d3f96
12 changed files with 815 additions and 710 deletions

2
Cargo.lock generated
View File

@@ -1,6 +1,6 @@
[root] [root]
name = "rusty-wam" name = "rusty-wam"
version = "0.5.12" version = "0.5.7"
dependencies = [ dependencies = [
"lalrpop 0.12.5 (registry+https://github.com/rust-lang/crates.io-index)", "lalrpop 0.12.5 (registry+https://github.com/rust-lang/crates.io-index)",
"lalrpop-util 0.12.5 (registry+https://github.com/rust-lang/crates.io-index)", "lalrpop-util 0.12.5 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "rusty-wam" name = "rusty-wam"
version = "0.5.12" version = "0.5.7"
authors = ["Mark Thom"] authors = ["Mark Thom"]
build = "build.rs" build = "build.rs"

View File

@@ -200,7 +200,7 @@ mod tests {
// test shallow cuts. // test shallow cuts.
submit(&mut wam, "memberchk(X, [X|_]) :- !. submit(&mut wam, "memberchk(X, [X|_]) :- !.
memberchk(X, [_|Xs]) :- !, memberchk(X, Xs)."); memberchk(X, [_|Xs]) :- memberchk(X, Xs).");
assert_eq!(submit(&mut wam, "?- memberchk(X, [a,b,c]).").failed_query(), false); assert_eq!(submit(&mut wam, "?- memberchk(X, [a,b,c]).").failed_query(), false);
assert_eq!(submit(&mut wam, "?- memberchk([X,X], [a,b,c,[d,e],[d,d]]).").failed_query(), false); assert_eq!(submit(&mut wam, "?- memberchk([X,X], [a,b,c,[d,e],[d,d]]).").failed_query(), false);

View File

@@ -7,6 +7,11 @@ pub type Var = String;
pub type Atom = String; pub type Atom = String;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum GenContext {
Head, Mid(usize), Last(usize) // Mid/Last: chunk_num
}
pub enum PredicateClause { pub enum PredicateClause {
Fact(Term), Fact(Term),
Rule(Rule) Rule(Rule)
@@ -90,13 +95,6 @@ impl VarReg {
pub fn is_temp(self) -> bool { pub fn is_temp(self) -> bool {
!self.norm().is_perm() !self.norm().is_perm()
} }
pub fn root_register(self) -> usize {
match self {
VarReg::ArgAndNorm(_, root) => root,
VarReg::Norm(root) => root.reg_num()
}
}
} }
impl Default for VarReg { impl Default for VarReg {
@@ -147,6 +145,7 @@ impl Rule {
} }
} }
#[derive(Clone, Copy)]
pub enum TermRef<'a> { pub enum TermRef<'a> {
AnonVar(Level), AnonVar(Level),
Cons(Level, &'a Cell<RegType>, &'a Term, &'a Term), Cons(Level, &'a Cell<RegType>, &'a Term, &'a Term),
@@ -155,6 +154,22 @@ pub enum TermRef<'a> {
Var(Level, &'a Cell<VarReg>, &'a Var) Var(Level, &'a Cell<VarReg>, &'a Var)
} }
impl<'a> TermRef<'a> {
pub fn level(self) -> Level {
match self {
TermRef::AnonVar(lvl)
| TermRef::Cons(lvl, _, _, _)
| TermRef::Constant(lvl, _, _)
| TermRef::Clause(lvl, _, _, _)
| TermRef::Var(lvl, _, _) => lvl
}
}
}
pub enum TermOrCutRef<'a> {
Cut, Term(&'a Term)
}
pub enum ChoiceInstruction { pub enum ChoiceInstruction {
RetryMeElse(usize), RetryMeElse(usize),
TrustMe, TrustMe,
@@ -410,6 +425,14 @@ impl Term {
} }
} }
pub fn is_callable(&self) -> bool {
match self {
&Term::Clause(_, _, _) | &Term::Constant(_, Constant::Atom(_)) =>
true,
_ => false
}
}
pub fn subterms(&self) -> usize { pub fn subterms(&self) -> usize {
match self { match self {
&Term::Clause(_, _, ref terms) => terms.len(), &Term::Clause(_, _, ref terms) => terms.len(),

File diff suppressed because it is too large Load Diff

247
src/prolog/indexing.rs Normal file
View File

@@ -0,0 +1,247 @@
use prolog::ast::*;
use std::collections::{HashMap, VecDeque};
use std::hash::Hash;
#[derive(Clone, Copy)]
enum IntIndex {
External(usize), Fail, Internal(usize)
}
pub struct CodeOffsets {
pub constants: HashMap<Constant, ThirdLevelIndex>,
pub lists: ThirdLevelIndex,
pub structures: HashMap<(Atom, usize), ThirdLevelIndex>
}
impl CodeOffsets {
pub fn new() -> Self {
CodeOffsets {
constants: HashMap::new(),
lists: Vec::new(),
structures: HashMap::new()
}
}
fn cap_choice_seq_with_trust(prelude: &mut ThirdLevelIndex) {
prelude.last_mut().map(|instr| {
match instr {
&mut IndexedChoiceInstruction::Retry(i) =>
*instr = IndexedChoiceInstruction::Trust(i),
_ => {}
};
});
}
fn add_index(is_first_index: bool, index: usize) -> IndexedChoiceInstruction {
if is_first_index {
IndexedChoiceInstruction::Try(index)
} else {
IndexedChoiceInstruction::Retry(index)
}
}
pub fn index_term(&mut self, first_arg: &Term, index: usize)
{
match first_arg {
&Term::Clause(_, ref name, ref terms) => {
let code = self.structures.entry((name.clone(), terms.len()))
.or_insert(Vec::new());
let is_initial_index = code.is_empty();
code.push(Self::add_index(is_initial_index, index));
},
&Term::Cons(_, _, _) => {
let is_initial_index = self.lists.is_empty();
self.lists.push(Self::add_index(is_initial_index, index));
},
&Term::Constant(_, ref constant) => {
let code = self.constants.entry(constant.clone())
.or_insert(Vec::new());
let is_initial_index = code.is_empty();
code.push(Self::add_index(is_initial_index, index));
},
_ => {}
};
}
fn second_level_index<Index>(indices: HashMap<Index, ThirdLevelIndex>,
prelude: &mut CodeDeque)
-> HashMap<Index, IntIndex>
where Index: Eq + Hash
{
let mut index_locs = HashMap::new();
for (key, mut code) in indices.into_iter() {
if code.len() > 1 {
index_locs.insert(key, IntIndex::Internal(prelude.len()));
Self::cap_choice_seq_with_trust(&mut code);
prelude.extend(code.into_iter().map(|code| Line::from(code)));
} else {
code.first().map(|i| {
index_locs.insert(key, IntIndex::External(i.offset()));
});
}
}
index_locs
}
fn no_indices(&self) -> bool {
let no_constants = self.constants.is_empty();
let no_structures = self.structures.is_empty();
let no_lists = self.lists.is_empty();
no_constants && no_structures && no_lists
}
fn flatten_index<Index>(index: HashMap<Index, IntIndex>, len: usize)
-> HashMap<Index, usize>
where Index: Eq + Hash
{
let mut flattened_index = HashMap::new();
for (key, int_index) in index.into_iter() {
match int_index {
IntIndex::External(offset) => {
flattened_index.insert(key, offset + len + 1);
},
IntIndex::Internal(offset) => {
flattened_index.insert(key, offset + 1);
},
_ => {}
};
}
flattened_index
}
fn switch_on_constant(con_ind: HashMap<Constant, ThirdLevelIndex>,
prelude: &mut CodeDeque)
-> IntIndex
{
let con_ind = Self::second_level_index(con_ind, prelude);
if con_ind.len() > 1 {
let index = Self::flatten_index(con_ind, prelude.len());
let instr = IndexingInstruction::SwitchOnConstant(index.len(), index);
prelude.push_front(Line::from(instr));
IntIndex::Internal(1)
} else {
con_ind.values().next()
.map(|i| *i)
.unwrap_or(IntIndex::Fail)
}
}
fn switch_on_list(mut lists: ThirdLevelIndex, prelude: &mut CodeDeque) -> IntIndex
{
if lists.len() > 1 {
Self::cap_choice_seq_with_trust(&mut lists);
prelude.extend(lists.into_iter().map(|i| Line::from(i)));
IntIndex::Internal(0)
} else {
lists.first()
.map(|i| IntIndex::External(i.offset()))
.unwrap_or(IntIndex::Fail)
}
}
fn switch_on_structure(str_ind: HashMap<(Atom, usize), ThirdLevelIndex>,
prelude: &mut CodeDeque)
-> IntIndex
{
let str_ind = Self::second_level_index(str_ind, prelude);
if str_ind.len() > 1 {
let index = Self::flatten_index(str_ind, prelude.len());
let instr = IndexingInstruction::SwitchOnStructure(index.len(), index);
prelude.push_front(Line::from(instr));
IntIndex::Internal(1)
} else {
str_ind.values().next()
.map(|i| *i)
.unwrap_or(IntIndex::Fail)
}
}
fn switch_on_str_offset_from(str_loc: IntIndex, prelude_len: usize, con_loc: IntIndex)
-> usize
{
match str_loc {
IntIndex::External(o) => o + prelude_len + 1,
IntIndex::Fail => 0,
IntIndex::Internal(_) => match con_loc {
IntIndex::Internal(_) => 2,
_ => 1
}
}
}
fn switch_on_con_offset_from(con_loc: IntIndex, prelude_len: usize) -> usize
{
match con_loc {
IntIndex::External(offset) => offset + prelude_len + 1,
IntIndex::Fail => 0,
IntIndex::Internal(offset) => offset,
}
}
fn switch_on_lst_offset_from(lst_loc: IntIndex, prelude_len: usize, lst_offset: usize)
-> usize
{
match lst_loc {
IntIndex::External(o) => o + prelude_len + 1,
IntIndex::Fail => 0,
IntIndex::Internal(_) => prelude_len - lst_offset + 1
}
}
pub fn add_indices(self, code: &mut Code, mut code_body: Code)
{
if self.no_indices() {
*code = code_body;
return;
}
let mut prelude = VecDeque::new();
let lst_loc = Self::switch_on_list(self.lists, &mut prelude);
let lst_offset = prelude.len();
let str_loc = Self::switch_on_structure(self.structures, &mut prelude);
let con_loc = Self::switch_on_constant(self.constants, &mut prelude);
let prelude_length = prelude.len();
for (index, line) in prelude.iter_mut().enumerate() {
match line {
&mut Line::IndexedChoice(IndexedChoiceInstruction::Try(ref mut i))
| &mut Line::IndexedChoice(IndexedChoiceInstruction::Retry(ref mut i))
| &mut Line::IndexedChoice(IndexedChoiceInstruction::Trust(ref mut i)) =>
*i += prelude_length - index,
_ => {}
}
}
let str_loc = Self::switch_on_str_offset_from(str_loc, prelude.len(), con_loc);
let con_loc = Self::switch_on_con_offset_from(con_loc, prelude.len());
let lst_loc = Self::switch_on_lst_offset_from(lst_loc, prelude.len(), lst_offset);
let switch_instr = IndexingInstruction::SwitchOnTerm(prelude.len() + 1,
con_loc,
lst_loc,
str_loc);
prelude.push_front(Line::from(switch_instr));
*code = Vec::from(prelude);
code.append(&mut code_body);
}
}

View File

@@ -2,6 +2,7 @@ use prolog::ast::*;
use std::cell::Cell; use std::cell::Cell;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::iter::*;
use std::vec::Vec; use std::vec::Vec;
enum IteratorState<'a> { enum IteratorState<'a> {
@@ -207,3 +208,120 @@ impl Term {
FactIterator::new(self) FactIterator::new(self)
} }
} }
pub struct ChunkedIterator<'a>
{
at_head: bool,
iter: Box<Iterator<Item=TermOrCutRef<'a>> + 'a>,
deep_cut_encountered: bool
}
impl<'a> ChunkedIterator<'a>
{
pub fn from_term(term: &'a Term, at_head: bool) -> Self
{
let inner_iter: Box<Iterator<Item=TermOrCutRef<'a>>> =
Box::new(once(TermOrCutRef::Term(term)));
ChunkedIterator {
at_head: at_head,
iter: inner_iter,
deep_cut_encountered: false
}
}
pub fn from_term_sequence(terms: &'a Vec<TermOrCut>) -> Self
{
let iter = terms.iter().map(|c| {
match c {
&TermOrCut::Cut => TermOrCutRef::Cut,
&TermOrCut::Term(ref term) => TermOrCutRef::Term(term)
}
});
ChunkedIterator {
at_head: false,
iter: Box::new(iter),
deep_cut_encountered: false
}
}
pub fn from_rule(rule: &'a Rule) -> Self
{
let &Rule { head: (ref p0, ref p1), ref clauses } = rule;
let iter = once(TermOrCutRef::Term(p0));
let inner_iter : Box<Iterator<Item=TermOrCutRef<'a>>> = match p1 {
&TermOrCut::Term(ref p1) => Box::new(once(TermOrCutRef::Term(p1))),
_ => Box::new(empty())
};
let iter = iter.chain(inner_iter.chain(clauses.iter().map(|c| {
match c {
&TermOrCut::Cut => TermOrCutRef::Cut,
&TermOrCut::Term(ref term) => TermOrCutRef::Term(term)
}
})));
ChunkedIterator {
at_head: true,
iter: Box::new(iter),
deep_cut_encountered: false
}
}
pub fn contains_deep_cut(&self) -> bool {
self.deep_cut_encountered
}
pub fn at_head(&self) -> bool {
self.at_head
}
fn take_chunk(&mut self, term: TermOrCutRef<'a>) -> (usize, Vec<TermOrCutRef<'a>>)
{
let mut result = vec![term];
let mut arity = 0;
while let Some(term) = self.iter.next() {
match term {
TermOrCutRef::Term(inner_term) => {
result.push(term);
if inner_term.is_callable() {
arity = inner_term.arity();
break;
}
},
_ => {
result.push(term);
self.deep_cut_encountered = true;
}
};
}
(arity, result)
}
}
impl<'a> Iterator for ChunkedIterator<'a>
{
// the last term arity, and the reference.
type Item = (usize, Vec<TermOrCutRef<'a>>);
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.iter.next() {
None => return None,
Some(TermOrCutRef::Term(term)) if self.at_head => {
self.at_head = false;
return Some(self.take_chunk(TermOrCutRef::Term(term)));
},
Some(TermOrCutRef::Term(term)) if term.is_callable() =>
return Some((term.arity(), vec![TermOrCutRef::Term(term)])),
Some(term_or_cut_ref) =>
return Some(self.take_chunk(term_or_cut_ref))
}
}
}
}

View File

@@ -251,8 +251,9 @@ impl Machine {
} }
if succeeded { if succeeded {
for (var, vr) in cg.vars() { for (var, var_status) in cg.vars() {
let addr = self.ms.registers[vr.root_register()].clone(); let r = var_status.as_reg_type().reg_num();
let addr = self.ms.registers[r].clone();
heap_locs.insert((*var).clone(), addr); heap_locs.insert((*var).clone(), addr);
} }

View File

@@ -1,9 +1,13 @@
pub mod and_stack; pub mod and_stack;
pub mod ast; pub mod ast;
pub mod codegen; pub mod codegen;
pub mod fixtures;
pub mod heapview; pub mod heapview;
pub mod indexing;
pub mod io; pub mod io;
pub mod iterators; pub mod iterators;
pub mod naive_allocator;
pub mod prolog_parser; pub mod prolog_parser;
pub mod machine; pub mod machine;
pub mod or_stack; pub mod or_stack;
pub mod targets;

View File

@@ -0,0 +1,163 @@
use prolog::ast::*;
use prolog::fixtures::*;
use std::cell::Cell;
use std::cmp::max;
use std::collections::{BTreeSet, HashMap};
pub struct TermMarker<'a> {
pub bindings: HashMap<&'a Var, VarData>,
arg_c: usize,
temp_c: usize,
contents: HashMap<usize, &'a Var>,
in_use: BTreeSet<usize>,
}
impl<'a> TermMarker<'a> {
pub fn new() -> TermMarker<'a> {
TermMarker {
arg_c: 1,
temp_c: 1,
bindings: HashMap::new(),
contents: HashMap::new(),
in_use: BTreeSet::new()
}
}
pub fn drain_var_data(&mut self, vs: VariableFixtures<'a>) -> VariableFixtures<'a>
{
let mut perm_vs = VariableFixtures::new();
for (var, (var_status, cells)) in vs.into_iter() {
match var_status {
VarStatus::Temp(chunk_num, tvd) => {
self.bindings.insert(var, VarData::Temp(chunk_num, 0, tvd));
},
VarStatus::Perm(_) => {
self.bindings.insert(var, VarData::Perm(0));
perm_vs.insert(var, (var_status, cells));
}
};
}
perm_vs
}
fn get(&self, var: &'a Var) -> RegType {
self.bindings.get(var).unwrap().as_reg_type()
}
pub fn contains_var(&self, var: &'a Var) -> bool {
self.bindings.contains_key(var)
}
pub fn marked_var(&self, var: &'a Var) -> bool {
self.get(var).reg_num() != 0
}
fn record_register(&mut self, var: &'a Var, r: RegType) {
match self.bindings.get_mut(var).unwrap() {
&mut VarData::Temp(_, ref mut s, _) => *s = r.reg_num(),
&mut VarData::Perm(ref mut s) => *s = r.reg_num()
}
}
pub fn mark_non_var(&mut self, lvl: Level, cell: &Cell<RegType>) {
let reg_type = cell.get();
if reg_type.reg_num() == 0 {
match lvl {
Level::Deep if !reg_type.is_perm() => {
let temp = self.temp_c;
self.temp_c += 1;
cell.set(RegType::Temp(temp));
},
Level::Shallow if !reg_type.is_perm() => {
let arg = self.arg_c;
self.arg_c += 1;
cell.set(RegType::Temp(arg));
},
_ => {}
};
}
}
pub fn mark_old_var(&mut self, lvl: Level, var: &'a Var) -> VarReg
{
let inner_reg = self.get(var);
match lvl {
Level::Deep => VarReg::Norm(inner_reg),
Level::Shallow => {
let reg = VarReg::ArgAndNorm(inner_reg, self.arg_c);
self.arg_c += 1;
reg
}
}
}
pub fn mark_new_var(&mut self, lvl: Level, var: &'a Var, reg: RegType) -> VarReg
{
let inner_reg = if !reg.is_perm() {
let temp = self.temp_c;
self.temp_c += 1;
RegType::Temp(temp)
} else {
reg
};
let reg = match lvl {
Level::Deep => VarReg::Norm(inner_reg),
Level::Shallow => {
let reg = VarReg::ArgAndNorm(inner_reg, self.arg_c);
self.arg_c += 1;
reg
}
};
self.record_register(var, inner_reg);
reg
}
pub fn mark_anon_var(&mut self, lvl: Level) -> VarReg {
let inner_reg = {
let temp = self.temp_c;
self.temp_c += 1;
RegType::Temp(temp)
};
match lvl {
Level::Deep => VarReg::Norm(inner_reg),
Level::Shallow => {
let reg = VarReg::ArgAndNorm(inner_reg, self.arg_c);
self.arg_c += 1;
reg
}
}
}
pub fn advance_arg(&mut self) {
self.arg_c += 1;
}
pub fn advance_at_head(&mut self, term: &'a Term) {
self.arg_c = 1;
self.temp_c = max(term.subterms() + 1, self.temp_c);
}
pub fn advance(&mut self, term: &'a Term) {
self.arg_c = 1;
self.temp_c = term.subterms() + 1;
}
pub fn reset(&mut self) {
self.bindings.clear();
self.contents.clear();
self.in_use.clear();
}
pub fn reset_contents(&mut self) {
self.contents.clear();
self.in_use.clear();
}
}

119
src/prolog/targets.rs Normal file
View File

@@ -0,0 +1,119 @@
use prolog::ast::*;
use prolog::iterators::*;
pub trait CompilationTarget<'a> {
type Iterator : Iterator<Item=TermRef<'a>>;
fn iter(&'a Term) -> Self::Iterator;
fn to_constant(Level, Constant, RegType) -> Self;
fn to_list(Level, RegType) -> Self;
fn to_structure(Level, Atom, usize, RegType) -> Self;
fn to_void(usize) -> Self;
fn constant_subterm(Constant) -> Self;
fn argument_to_variable(RegType, usize) -> Self;
fn argument_to_value(RegType, usize) -> Self;
fn subterm_to_variable(RegType) -> Self;
fn subterm_to_value(RegType) -> Self;
fn clause_arg_to_instr(RegType) -> Self;
}
impl<'a> CompilationTarget<'a> for FactInstruction {
type Iterator = FactIterator<'a>;
fn iter(term: &'a Term) -> Self::Iterator {
term.breadth_first_iter()
}
fn to_constant(lvl: Level, constant: Constant, reg: RegType) -> Self {
FactInstruction::GetConstant(lvl, constant, reg)
}
fn to_structure(lvl: Level, atom: Atom, arity: usize, reg: RegType) -> Self {
FactInstruction::GetStructure(lvl, atom, arity, reg)
}
fn to_list(lvl: Level, reg: RegType) -> Self {
FactInstruction::GetList(lvl, reg)
}
fn to_void(subterms: usize) -> Self {
FactInstruction::UnifyVoid(subterms)
}
fn constant_subterm(constant: Constant) -> Self {
FactInstruction::UnifyConstant(constant)
}
fn argument_to_variable(arg: RegType, val: usize) -> Self {
FactInstruction::GetVariable(arg, val)
}
fn argument_to_value(arg: RegType, val: usize) -> Self {
FactInstruction::GetValue(arg, val)
}
fn subterm_to_variable(val: RegType) -> Self {
FactInstruction::UnifyVariable(val)
}
fn subterm_to_value(val: RegType) -> Self {
FactInstruction::UnifyValue(val)
}
fn clause_arg_to_instr(val: RegType) -> Self {
FactInstruction::UnifyVariable(val)
}
}
impl<'a> CompilationTarget<'a> for QueryInstruction {
type Iterator = QueryIterator<'a>;
fn iter(term: &'a Term) -> Self::Iterator {
term.post_order_iter()
}
fn to_structure(lvl: Level, atom: Atom, arity: usize, reg: RegType) -> Self {
QueryInstruction::PutStructure(lvl, atom, arity, reg)
}
fn to_constant(lvl: Level, constant: Constant, reg: RegType) -> Self {
QueryInstruction::PutConstant(lvl, constant, reg)
}
fn to_list(lvl: Level, reg: RegType) -> Self {
QueryInstruction::PutList(lvl, reg)
}
fn to_void(subterms: usize) -> Self {
QueryInstruction::SetVoid(subterms)
}
fn constant_subterm(constant: Constant) -> Self {
QueryInstruction::SetConstant(constant)
}
fn argument_to_variable(arg: RegType, val: usize) -> Self {
QueryInstruction::PutVariable(arg, val)
}
fn argument_to_value(arg: RegType, val: usize) -> Self {
QueryInstruction::PutValue(arg, val)
}
fn subterm_to_variable(val: RegType) -> Self {
QueryInstruction::SetVariable(val)
}
fn subterm_to_value(val: RegType) -> Self {
QueryInstruction::SetValue(val)
}
fn clause_arg_to_instr(val: RegType) -> Self {
QueryInstruction::SetValue(val)
}
}