transition to MachineCodeIndices internally
This commit is contained in:
@@ -111,6 +111,7 @@ impl MachineError {
|
||||
};
|
||||
|
||||
stub.extend(err.into_iter());
|
||||
|
||||
MachineError { stub, from: ErrorProvenance::Constructed }
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ use prolog::instructions::*;
|
||||
use prolog::and_stack::*;
|
||||
use prolog::copier::*;
|
||||
use prolog::heap_print::*;
|
||||
use prolog::machine::MachineCodeIndices;
|
||||
use prolog::machine::machine_errors::*;
|
||||
use prolog::num::{BigInt, BigUint, Zero, One};
|
||||
use prolog::or_stack::*;
|
||||
@@ -46,19 +47,6 @@ impl<'a> CodeDirs<'a> {
|
||||
CodeDirs { code_dir, op_dir, modules }
|
||||
}
|
||||
|
||||
pub(super) fn get(&self, name: ClauseName, arity: usize, in_mod: ClauseName) -> Option<CodeIndex>
|
||||
{
|
||||
match in_mod.as_str() {
|
||||
"user" | "builtin" => self.code_dir.get(&(name, arity)).cloned(),
|
||||
_ =>
|
||||
match self.modules.get(&in_mod) {
|
||||
Some(&Module { ref code_dir, .. }) =>
|
||||
code_dir.get(&(name, arity)).cloned().map(CodeIndex::from),
|
||||
None => None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_internal(&self, name: ClauseName, arity: usize, in_mod: ClauseName) -> Option<ModuleCodeIndex> {
|
||||
self.modules.get(&in_mod)
|
||||
.and_then(|ref module| module.code_dir.get(&(name, arity)))
|
||||
@@ -90,9 +78,34 @@ pub trait CodeDirsAdapter<'a> {
|
||||
fn op_dir(&self) -> &OpDir;
|
||||
}
|
||||
|
||||
fn get_code_index(code_dir: &CodeDir, modules: &ModuleDir, key: PredicateKey, module: ClauseName)
|
||||
-> Option<CodeIndex>
|
||||
{
|
||||
match module.as_str() {
|
||||
"user" | "builtin" => code_dir.get(&key).cloned(),
|
||||
_ => modules.get(&module).and_then(|ref module| {
|
||||
module.code_dir.get(&key).cloned().map(CodeIndex::from)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> CodeDirsAdapter<'a> for MachineCodeIndices<'a> {
|
||||
fn get_code_index(&self, key: PredicateKey, module: ClauseName) -> Option<CodeIndex> {
|
||||
get_code_index(&self.code_dir, &self.modules, key, module)
|
||||
}
|
||||
|
||||
fn get_op(&self, key: OpDirKey) -> Option<(Specifier, usize, ClauseName)> {
|
||||
self.op_dir.get(&key).cloned()
|
||||
}
|
||||
|
||||
fn op_dir(&self) -> &OpDir {
|
||||
&self.op_dir
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> CodeDirsAdapter<'a> for CodeDirs<'a> {
|
||||
fn get_code_index(&self, key: PredicateKey, module: ClauseName) -> Option<CodeIndex> {
|
||||
self.get(key.0, key.1, module)
|
||||
get_code_index(&self.code_dir, &self.modules, key, module)
|
||||
}
|
||||
|
||||
fn get_op(&self, key: OpDirKey) -> Option<(Specifier, usize, ClauseName)> {
|
||||
@@ -433,18 +446,18 @@ pub(crate) trait CallPolicy: Any {
|
||||
}
|
||||
|
||||
fn context_call(&mut self, machine_st: &mut MachineState, name: ClauseName, arity: usize,
|
||||
idx: CodeIndex, code_dirs: CodeDirs)
|
||||
-> CallResult
|
||||
idx: CodeIndex, indices: MachineCodeIndices)
|
||||
-> CallResult
|
||||
{
|
||||
if machine_st.last_call {
|
||||
self.try_execute(machine_st, name, arity, idx, code_dirs)
|
||||
self.try_execute(machine_st, name, arity, idx, indices)
|
||||
} else {
|
||||
self.try_call(machine_st, name, arity, idx, code_dirs)
|
||||
self.try_call(machine_st, name, arity, idx, indices)
|
||||
}
|
||||
}
|
||||
|
||||
fn try_call<'a>(&mut self, machine_st: &mut MachineState, name: ClauseName, arity: usize,
|
||||
idx: CodeIndex, code_dirs: CodeDirs)
|
||||
idx: CodeIndex, indices: MachineCodeIndices<'a>)
|
||||
-> CallResult
|
||||
{
|
||||
match idx.0.borrow().0 {
|
||||
@@ -453,7 +466,7 @@ pub(crate) trait CallPolicy: Any {
|
||||
let module_name = idx.0.borrow().1.clone();
|
||||
let h = machine_st.heap.h;
|
||||
|
||||
if let Some(ref idx) = code_dirs.get_code_index((name.clone(), arity), module_name.clone())
|
||||
if let Some(ref idx) = indices.get_code_index((name.clone(), arity), module_name.clone())
|
||||
{
|
||||
if let IndexPtr::Index(compiled_tl_index) = idx.0.borrow().0 {
|
||||
call_at_index(machine_st, module_name, arity, compiled_tl_index);
|
||||
@@ -481,7 +494,7 @@ pub(crate) trait CallPolicy: Any {
|
||||
}
|
||||
|
||||
fn try_execute<'a>(&mut self, machine_st: &mut MachineState, name: ClauseName,
|
||||
arity: usize, idx: CodeIndex, code_dirs: CodeDirs)
|
||||
arity: usize, idx: CodeIndex, indices: MachineCodeIndices<'a>)
|
||||
-> CallResult
|
||||
{
|
||||
match idx.0.borrow().0 {
|
||||
@@ -490,7 +503,7 @@ pub(crate) trait CallPolicy: Any {
|
||||
let module_name = idx.0.borrow().1.clone();
|
||||
let h = machine_st.heap.h;
|
||||
|
||||
if let Some(ref idx) = code_dirs.get_code_index((name.clone(), arity), module_name.clone())
|
||||
if let Some(ref idx) = indices.get_code_index((name.clone(), arity), module_name.clone())
|
||||
{
|
||||
if let IndexPtr::Index(compiled_tl_index) = idx.0.borrow().0 {
|
||||
execute_at_index(machine_st, module_name, arity, compiled_tl_index);
|
||||
@@ -518,10 +531,10 @@ pub(crate) trait CallPolicy: Any {
|
||||
}
|
||||
|
||||
fn call_builtin<'a>(&mut self, machine_st: &mut MachineState, ct: &BuiltInClauseType,
|
||||
code_dirs: CodeDirs)
|
||||
indices: MachineCodeIndices<'a>) //code_dirs: CodeDirs)
|
||||
-> CallResult
|
||||
{
|
||||
match ct {
|
||||
match ct {
|
||||
&BuiltInClauseType::AcyclicTerm => {
|
||||
let addr = machine_st[temp_v!(1)].clone();
|
||||
machine_st.fail = machine_st.is_cyclic_term(addr);
|
||||
@@ -564,7 +577,7 @@ pub(crate) trait CallPolicy: Any {
|
||||
&BuiltInClauseType::Read => {
|
||||
let mut reader = Reader::new(machine_st);
|
||||
|
||||
match reader.read_stdin(code_dirs.op_dir()) {
|
||||
match reader.read_stdin(&indices.op_dir) {
|
||||
Ok(offset) => {
|
||||
let addr = reader.machine_st[temp_v!(1)].clone();
|
||||
reader.machine_st.unify(addr, Addr::HeapCell(offset));
|
||||
@@ -574,7 +587,7 @@ pub(crate) trait CallPolicy: Any {
|
||||
let stub = MachineError::functor_stub(clause_name!("read"), 1);
|
||||
let err = MachineError::syntax_error(h, e);
|
||||
let err = reader.machine_st.error_form(err, stub);
|
||||
|
||||
|
||||
return Err(err);
|
||||
}
|
||||
};
|
||||
@@ -614,7 +627,7 @@ pub(crate) trait CallPolicy: Any {
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
|
||||
return_from_clause!(machine_st.last_call, machine_st)
|
||||
},
|
||||
&BuiltInClauseType::PartialString => {
|
||||
@@ -679,7 +692,7 @@ pub(crate) trait CallPolicy: Any {
|
||||
}
|
||||
|
||||
fn call_n<'a>(&mut self, machine_st: &mut MachineState, arity: usize,
|
||||
code_dirs: CodeDirs)
|
||||
indices: MachineCodeIndices<'a>) //code_dirs: CodeDirs)
|
||||
-> CallResult
|
||||
{
|
||||
if let Some((name, arity)) = machine_st.setup_call_n(arity) {
|
||||
@@ -697,13 +710,13 @@ pub(crate) trait CallPolicy: Any {
|
||||
},
|
||||
ClauseType::BuiltIn(built_in) => {
|
||||
machine_st.setup_built_in_call(built_in.clone());
|
||||
self.call_builtin(machine_st, &built_in, code_dirs)?;
|
||||
self.call_builtin(machine_st, &built_in, indices)?;
|
||||
},
|
||||
ClauseType::Inlined(inlined) =>
|
||||
machine_st.execute_inlined(&inlined),
|
||||
ClauseType::Op(..) | ClauseType::Named(..) =>
|
||||
if let Some(idx) = code_dirs.get_code_index((name.clone(), arity), user) {
|
||||
self.context_call(machine_st, name, arity, idx, code_dirs)?;
|
||||
if let Some(idx) = indices.get_code_index((name.clone(), arity), user) {
|
||||
self.context_call(machine_st, name, arity, idx, indices)?;
|
||||
} else {
|
||||
let h = machine_st.heap.h;
|
||||
let stub = MachineError::functor_stub(clause_name!("call"), arity + 1);
|
||||
@@ -727,10 +740,10 @@ pub(crate) trait CallPolicy: Any {
|
||||
|
||||
impl CallPolicy for CWILCallPolicy {
|
||||
fn context_call<'a>(&mut self, machine_st: &mut MachineState, name: ClauseName,
|
||||
arity: usize, idx: CodeIndex, code_dirs: CodeDirs)
|
||||
arity: usize, idx: CodeIndex, indices: MachineCodeIndices<'a>)
|
||||
-> CallResult
|
||||
{
|
||||
self.prev_policy.context_call(machine_st, name, arity, idx, code_dirs)?;
|
||||
self.prev_policy.context_call(machine_st, name, arity, idx, indices)?;
|
||||
self.increment(machine_st)
|
||||
}
|
||||
|
||||
@@ -759,18 +772,17 @@ impl CallPolicy for CWILCallPolicy {
|
||||
}
|
||||
|
||||
fn call_builtin<'a>(&mut self, machine_st: &mut MachineState, ct: &BuiltInClauseType,
|
||||
code_dirs: CodeDirs)
|
||||
indices: MachineCodeIndices<'a>)
|
||||
-> CallResult
|
||||
{
|
||||
self.prev_policy.call_builtin(machine_st, ct, code_dirs)?;
|
||||
self.prev_policy.call_builtin(machine_st, ct, indices)?;
|
||||
self.increment(machine_st)
|
||||
}
|
||||
|
||||
fn call_n<'a>(&mut self, machine_st: &mut MachineState, arity: usize,
|
||||
code_dirs: CodeDirs)
|
||||
fn call_n<'a>(&mut self, machine_st: &mut MachineState, arity: usize, indices: MachineCodeIndices<'a>)
|
||||
-> CallResult
|
||||
{
|
||||
self.prev_policy.call_n(machine_st, arity, code_dirs)?;
|
||||
self.prev_policy.call_n(machine_st, arity, indices)?;
|
||||
self.increment(machine_st)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ use prolog::and_stack::*;
|
||||
use prolog::copier::*;
|
||||
use prolog::heap_iter::*;
|
||||
use prolog::heap_print::*;
|
||||
use prolog::machine::MachineCodeIndices;
|
||||
use prolog::machine::machine_errors::*;
|
||||
use prolog::machine::machine_state::*;
|
||||
use prolog::num::{Integer, Signed, ToPrimitive, Zero};
|
||||
@@ -2045,7 +2046,7 @@ impl MachineState {
|
||||
self.p += 1;
|
||||
}
|
||||
|
||||
fn handle_call_clause<'a>(&mut self, code_dirs: CodeDirs<'a>,
|
||||
fn handle_call_clause<'a>(&mut self, indices: MachineCodeIndices<'a>,
|
||||
call_policy: &mut Box<CallPolicy>,
|
||||
cut_policy: &mut Box<CutPolicy>,
|
||||
ct: &ClauseType,
|
||||
@@ -2064,20 +2065,20 @@ impl MachineState {
|
||||
|
||||
match ct {
|
||||
&ClauseType::BuiltIn(ref ct) =>
|
||||
try_or_fail!(self, call_policy.call_builtin(self, ct, code_dirs)),
|
||||
try_or_fail!(self, call_policy.call_builtin(self, ct, indices)),
|
||||
&ClauseType::CallN =>
|
||||
try_or_fail!(self, call_policy.call_n(self, arity, code_dirs)),
|
||||
try_or_fail!(self, call_policy.call_n(self, arity, indices)),
|
||||
&ClauseType::Inlined(ref ct) =>
|
||||
self.execute_inlined(ct),
|
||||
&ClauseType::Named(ref name, ref idx) | &ClauseType::Op(ref name, _, ref idx) =>
|
||||
try_or_fail!(self, call_policy.context_call(self, name.clone(), arity, idx.clone(),
|
||||
code_dirs)),
|
||||
indices)),
|
||||
&ClauseType::System(ref ct) =>
|
||||
try_or_fail!(self, self.system_call(ct, code_dirs, call_policy, cut_policy))
|
||||
try_or_fail!(self, self.system_call(ct, indices, call_policy, cut_policy))
|
||||
};
|
||||
}
|
||||
|
||||
pub(super) fn execute_ctrl_instr<'a>(&mut self, code_dirs: CodeDirs<'a>,
|
||||
pub(super) fn execute_ctrl_instr<'a>(&mut self, indices: MachineCodeIndices<'a>,
|
||||
call_policy: &mut Box<CallPolicy>,
|
||||
cut_policy: &mut Box<CutPolicy>,
|
||||
instr: &ControlInstruction)
|
||||
@@ -2086,7 +2087,7 @@ impl MachineState {
|
||||
&ControlInstruction::Allocate(num_cells) =>
|
||||
self.allocate(num_cells),
|
||||
&ControlInstruction::CallClause(ref ct, arity, _, lco, use_default_cp) =>
|
||||
self.handle_call_clause(code_dirs, call_policy, cut_policy, ct, arity, lco,
|
||||
self.handle_call_clause(indices, call_policy, cut_policy, ct, arity, lco,
|
||||
use_default_cp),
|
||||
&ControlInstruction::Deallocate => self.deallocate(),
|
||||
&ControlInstruction::JmpBy(arity, offset, _, lco) => {
|
||||
|
||||
@@ -23,6 +23,7 @@ static BUILTINS: &str = include_str!("../lib/builtins.pl");
|
||||
pub struct MachineCodeIndices<'a> {
|
||||
pub(super) code_dir: &'a mut CodeDir,
|
||||
pub(super) op_dir: &'a mut OpDir,
|
||||
pub(super) modules: &'a mut ModuleDir
|
||||
}
|
||||
|
||||
impl<'a> MachineCodeIndices<'a> {
|
||||
@@ -45,6 +46,13 @@ impl<'a> MachineCodeIndices<'a> {
|
||||
ct => ct
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(super) fn to_code_dirs(self) -> CodeDirs<'a> {
|
||||
CodeDirs { code_dir: self.code_dir,
|
||||
op_dir: self.op_dir,
|
||||
modules: self.modules }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Machine {
|
||||
@@ -107,13 +115,15 @@ impl Machine {
|
||||
cut_policy: Box::new(DefaultCutPolicy {}),
|
||||
code: Code::new(),
|
||||
code_dir: CodeDir::new(),
|
||||
term_dir: TermDir::new(),
|
||||
op_dir: default_op_dir(),
|
||||
term_dir: TermDir::new(),
|
||||
modules: HashMap::new(),
|
||||
cached_query: None
|
||||
};
|
||||
|
||||
let indices = machine_code_indices!(&mut CodeDir::new(), &mut default_op_dir());
|
||||
let indices = machine_code_indices!(&mut CodeDir::new(), &mut default_op_dir(),
|
||||
&mut HashMap::new());
|
||||
|
||||
compile_listing(&mut wam, BUILTINS, indices);
|
||||
|
||||
compile_user_module(&mut wam, LISTS);
|
||||
@@ -194,7 +204,8 @@ impl Machine {
|
||||
|
||||
if let Some(mut module) = self.modules.remove(&name) {
|
||||
let result = {
|
||||
let mut indices = machine_code_indices!(&mut self.code_dir, &mut self.op_dir);
|
||||
let mut indices = machine_code_indices!(&mut self.code_dir, &mut self.op_dir,
|
||||
&mut self.modules);
|
||||
indices.use_qualified_module(&mut module, &exports)
|
||||
};
|
||||
|
||||
@@ -211,7 +222,8 @@ impl Machine {
|
||||
|
||||
if let Some(mut module) = self.modules.remove(&name) {
|
||||
let result = {
|
||||
let mut indices = machine_code_indices!(&mut self.code_dir, &mut self.op_dir);
|
||||
let mut indices = machine_code_indices!(&mut self.code_dir, &mut self.op_dir,
|
||||
&mut self.modules);
|
||||
indices.use_module(&mut module)
|
||||
};
|
||||
|
||||
@@ -308,9 +320,11 @@ impl Machine {
|
||||
Line::Cut(ref cut_instr) =>
|
||||
self.ms.execute_cut_instr(cut_instr, &mut self.cut_policy),
|
||||
Line::Control(ref control_instr) => {
|
||||
let code_dirs = CodeDirs::new(&self.code_dir, &self.op_dir,
|
||||
&self.modules);
|
||||
self.ms.execute_ctrl_instr(code_dirs, &mut self.call_policy,
|
||||
let indices = machine_code_indices!(&mut self.code_dir, &mut self.op_dir,
|
||||
&mut self.modules);
|
||||
// let code_dirs = CodeDirs::new(&self.code_dir, &self.op_dir,
|
||||
// &self.modules);
|
||||
self.ms.execute_ctrl_instr(indices, &mut self.call_policy,
|
||||
&mut self.cut_policy, control_instr)
|
||||
},
|
||||
Line::Fact(ref fact) => {
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
use prolog_parser::ast::*;
|
||||
|
||||
use prolog::instructions::*;
|
||||
use prolog::machine::MachineCodeIndices;
|
||||
use prolog::machine::machine_errors::*;
|
||||
use prolog::machine::machine_state::*;
|
||||
use prolog::num::{ToPrimitive, Zero};
|
||||
use prolog::num::bigint::{BigInt};
|
||||
//use prolog::term_writer::*;
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
@@ -164,6 +166,7 @@ impl MachineState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn install_new_block(&mut self, r: RegType) -> usize {
|
||||
self.block = self.b;
|
||||
|
||||
@@ -174,6 +177,7 @@ impl MachineState {
|
||||
self.block
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_p(&mut self) {
|
||||
if self.last_call {
|
||||
self.p = CodePtr::Local(self.cp.clone());
|
||||
@@ -183,7 +187,7 @@ impl MachineState {
|
||||
}
|
||||
|
||||
pub(super) fn system_call<'a>(&mut self, ct: &SystemClauseType,
|
||||
code_dirs: CodeDirs<'a>,
|
||||
indices: MachineCodeIndices<'a>,
|
||||
call_policy: &mut Box<CallPolicy>,
|
||||
cut_policy: &mut Box<CutPolicy>,)
|
||||
-> CallResult
|
||||
@@ -237,7 +241,7 @@ impl MachineState {
|
||||
let prev_block = self.block;
|
||||
|
||||
if cut_policy.downcast_ref::<SCCCutPolicy>().is_err() {
|
||||
let (r_c_w_h, r_c_wo_h) = code_dirs.get_cleaner_sites();
|
||||
let (r_c_w_h, r_c_wo_h) = indices.to_code_dirs().get_cleaner_sites();
|
||||
*cut_policy = Box::new(SCCCutPolicy::new(r_c_w_h, r_c_wo_h));
|
||||
}
|
||||
|
||||
@@ -431,7 +435,14 @@ impl MachineState {
|
||||
return Err(err);
|
||||
},
|
||||
&SystemClauseType::Succeed => {},
|
||||
&SystemClauseType::UnwindStack => self.unwind_stack()
|
||||
&SystemClauseType::UnwindStack => self.unwind_stack(),
|
||||
&SystemClauseType::CompileAndRunQuery => {}
|
||||
/* let addr = self[temp_v!(1)].clone();
|
||||
|
||||
match term_write(&self, addr) {
|
||||
Err(e) => machine_error
|
||||
Ok(term) =>
|
||||
}*/
|
||||
};
|
||||
|
||||
self.set_p();
|
||||
|
||||
Reference in New Issue
Block a user