bracket verify_attributes/3 interrupts with allocation and deallocation of stack frames.
This commit is contained in:
@@ -8,6 +8,7 @@ pub struct Frame {
|
|||||||
pub global_index: usize,
|
pub global_index: usize,
|
||||||
pub e: usize,
|
pub e: usize,
|
||||||
pub cp: LocalCodePtr,
|
pub cp: LocalCodePtr,
|
||||||
|
pub special_form_cp: LocalCodePtr,
|
||||||
perms: Vec<Addr>
|
perms: Vec<Addr>
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,9 +18,15 @@ impl Frame {
|
|||||||
global_index,
|
global_index,
|
||||||
e: e,
|
e: e,
|
||||||
cp: cp,
|
cp: cp,
|
||||||
|
special_form_cp: LocalCodePtr::default(),
|
||||||
perms: (1 .. n+1).map(|i| Addr::StackCell(fr, i)).collect()
|
perms: (1 .. n+1).map(|i| Addr::StackCell(fr, i)).collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.perms.len()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AndStack(Vec<Frame>);
|
pub struct AndStack(Vec<Frame>);
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ impl DebrayAllocator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn is_in_use(&self, r: usize) -> bool {
|
fn is_in_use(&self, r: usize) -> bool {
|
||||||
let in_use_range = r <= self.arity && r >= self.arg_c;
|
let in_use_range = r <= self.arity && r >= self.arg_c;
|
||||||
in_use_range || self.in_use.contains(&r)
|
in_use_range || self.in_use.contains(&r)
|
||||||
|
|||||||
@@ -4,10 +4,9 @@ pub static VERIFY_ATTRS: &str = include_str!("attributed_variables.pl");
|
|||||||
|
|
||||||
pub(super) type Bindings = Vec<(usize, Addr)>;
|
pub(super) type Bindings = Vec<(usize, Addr)>;
|
||||||
|
|
||||||
pub(super) struct AttrVarInitializer {
|
pub(super) struct AttrVarInitializer {
|
||||||
pub(super) bindings: Bindings,
|
pub(super) bindings: Bindings,
|
||||||
cp_stack: Vec<CodePtr>,
|
pub(super) cp: LocalCodePtr,
|
||||||
pub(super) registers: Registers,
|
|
||||||
pub(super) verify_attrs_loc: usize
|
pub(super) verify_attrs_loc: usize
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -15,20 +14,13 @@ impl AttrVarInitializer {
|
|||||||
pub(super) fn new(p: usize) -> Self {
|
pub(super) fn new(p: usize) -> Self {
|
||||||
AttrVarInitializer {
|
AttrVarInitializer {
|
||||||
bindings: vec![],
|
bindings: vec![],
|
||||||
|
cp: LocalCodePtr::default(),
|
||||||
verify_attrs_loc: p,
|
verify_attrs_loc: p,
|
||||||
cp_stack: vec![],
|
|
||||||
registers: vec![Addr::HeapCell(0); MAX_ARITY + 1]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(super) fn pop_code_ptr(&mut self) -> CodePtr {
|
|
||||||
self.cp_stack.pop().unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(super) fn reset(&mut self) {
|
pub(super) fn reset(&mut self) {
|
||||||
self.cp_stack.clear();
|
|
||||||
self.bindings.clear();
|
self.bindings.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -37,7 +29,7 @@ impl MachineState {
|
|||||||
pub(super) fn push_attr_var_binding(&mut self, h: usize, addr: Addr)
|
pub(super) fn push_attr_var_binding(&mut self, h: usize, addr: Addr)
|
||||||
{
|
{
|
||||||
if self.attr_var_init.bindings.is_empty() {
|
if self.attr_var_init.bindings.is_empty() {
|
||||||
self.attr_var_init.cp_stack.push(self.p.clone() + 1);
|
self.attr_var_init.cp = self.p.local() + 1;
|
||||||
self.p = CodePtr::VerifyAttrInterrupt(self.attr_var_init.verify_attrs_loc);
|
self.p = CodePtr::VerifyAttrInterrupt(self.attr_var_init.verify_attrs_loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,18 +46,32 @@ impl MachineState {
|
|||||||
(var_list_addr, value_list_addr)
|
(var_list_addr, value_list_addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super)
|
||||||
|
fn calculate_register_threshold(&self) -> usize {
|
||||||
|
let mut count = 0;
|
||||||
|
|
||||||
|
for r in 1 .. MAX_ARITY + 1 {
|
||||||
|
if let &Addr::HeapCell(0) = &self[RegType::Temp(r)] {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
count
|
||||||
|
}
|
||||||
|
|
||||||
pub(super)
|
pub(super)
|
||||||
fn verify_attributes(&mut self)
|
fn verify_attributes(&mut self)
|
||||||
{
|
{
|
||||||
/* STEP 1: Undo bindings in machine.
|
/* STEP 1: Undo bindings in machine.
|
||||||
STEP 2: Write the list of bindings to two lists in the heap, one for vars, one for values.
|
STEP 2: Write the list of bindings to two lists in the heap, one for vars, one for values.
|
||||||
STEP 3: Swap the machine's Registers for attr_var_init's Registers.
|
STEP 3: Pass the addresses of the lists to iterate in the attr_vars special form.
|
||||||
STEP 4: Pass the addresses of the lists to iterate in the attr_vars special form.
|
|
||||||
Call verify_attributes/3 wherever applicable.
|
Call verify_attributes/3 wherever applicable.
|
||||||
STEP 5: Redo the bindings.
|
STEP 4: Redo the bindings.
|
||||||
STEP 6: Call the goals.
|
STEP 5: Call the goals.
|
||||||
STEP 7: Pop the top of AttrVarInitializer::cp_stack to self.p.
|
STEP 6: Pop the top of AttrVarInitializer::cp_stack to self.p.
|
||||||
STEP 8: Swap the AttrVarInitializer's Registers back for the machine's Registers.
|
STEP 7: Swap the AttrVarInitializer's Registers back for the machine's Registers.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// STEP 1.
|
// STEP 1.
|
||||||
@@ -75,10 +81,8 @@ impl MachineState {
|
|||||||
|
|
||||||
// STEP 2.
|
// STEP 2.
|
||||||
let (var_list_addr, value_list_addr) = self.populate_var_and_value_lists();
|
let (var_list_addr, value_list_addr) = self.populate_var_and_value_lists();
|
||||||
// STEP 3.
|
|
||||||
mem::swap(&mut self.registers, &mut self.attr_var_init.registers);
|
|
||||||
|
|
||||||
// STEP 4.
|
// STEP 3.
|
||||||
self[temp_v!(1)] = var_list_addr;
|
self[temp_v!(1)] = var_list_addr;
|
||||||
self[temp_v!(2)] = value_list_addr;
|
self[temp_v!(2)] = value_list_addr;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2175,7 +2175,7 @@ impl MachineState {
|
|||||||
self.e = self.and_stack.len() - 1;
|
self.e = self.and_stack.len() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deallocate(&mut self) {
|
pub(super) fn deallocate(&mut self) {
|
||||||
let e = self.e;
|
let e = self.e;
|
||||||
|
|
||||||
self.cp = self.and_stack[e].cp.clone();
|
self.cp = self.and_stack[e].cp.clone();
|
||||||
|
|||||||
@@ -586,6 +586,17 @@ impl MachineState {
|
|||||||
CodePtr::Local(_) =>
|
CodePtr::Local(_) =>
|
||||||
break,
|
break,
|
||||||
CodePtr::VerifyAttrInterrupt(p) => {
|
CodePtr::VerifyAttrInterrupt(p) => {
|
||||||
|
let rs = self.calculate_register_threshold();
|
||||||
|
|
||||||
|
self.allocate(rs); // store temp vars in perm vars slots.
|
||||||
|
|
||||||
|
let e = self.e;
|
||||||
|
self.and_stack[e].special_form_cp = self.attr_var_init.cp;
|
||||||
|
|
||||||
|
for i in 1 .. self.and_stack[e].len() + 1 {
|
||||||
|
self.and_stack[e][i] = self[RegType::Temp(i)].clone();
|
||||||
|
}
|
||||||
|
|
||||||
self.verify_attributes();
|
self.verify_attributes();
|
||||||
|
|
||||||
self.num_of_args = 2;
|
self.num_of_args = 2;
|
||||||
|
|||||||
@@ -450,9 +450,15 @@ impl MachineState {
|
|||||||
CWILCallPolicy.")
|
CWILCallPolicy.")
|
||||||
},
|
},
|
||||||
&SystemClauseType::RestoreCodePtrFromSpecialFormCP => {
|
&SystemClauseType::RestoreCodePtrFromSpecialFormCP => {
|
||||||
self.p = self.attr_var_init.pop_code_ptr();
|
let e = self.e;
|
||||||
mem::swap(&mut self.registers, &mut self.attr_var_init.registers);
|
|
||||||
|
for i in 1 .. self.and_stack[e].len() + 1 {
|
||||||
|
self[RegType::Temp(i)] = self.and_stack[e][i].clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
self.p = CodePtr::Local(self.and_stack[e].special_form_cp);
|
||||||
|
self.deallocate();
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
},
|
},
|
||||||
&SystemClauseType::RestoreCutPolicy => {
|
&SystemClauseType::RestoreCutPolicy => {
|
||||||
|
|||||||
@@ -13,6 +13,23 @@ fn error_string(e: &String) -> String {
|
|||||||
format!("error: exception thrown: {}", e)
|
format!("error: exception thrown: {}", e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for LocalCodePtr {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
LocalCodePtr::DirEntry(p) =>
|
||||||
|
write!(f, "LocalCodePtr::DirEntry({})", p),
|
||||||
|
LocalCodePtr::InSituDirEntry(p) =>
|
||||||
|
write!(f, "LocalCodePtr::InSituDirEntry({})", p),
|
||||||
|
LocalCodePtr::TopLevel(cn, p) =>
|
||||||
|
write!(f, "LocalCodePtr::TopLevel({}, {})", cn, p),
|
||||||
|
LocalCodePtr::UserGoalExpansion(p) =>
|
||||||
|
write!(f, "LocalCodePtr::UserGoalExpansion({})", p),
|
||||||
|
LocalCodePtr::UserTermExpansion(p) =>
|
||||||
|
write!(f, "LocalCodePtr::UserTermExpansion({})", p),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for IndexPtr {
|
impl fmt::Display for IndexPtr {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
|
|||||||
Reference in New Issue
Block a user