use in situ code directory from metacall if conventional lookup fails (#238)

This commit is contained in:
Mark Thom
2019-11-27 00:52:30 -07:00
parent 0a665b79f2
commit 1dec482e22
4 changed files with 30 additions and 39 deletions

View File

@@ -873,13 +873,7 @@ pub(crate) trait CallPolicy: Any {
if let Some(idx) = indices.get_code_index((name.clone(), arity), module) { if let Some(idx) = indices.get_code_index((name.clone(), arity), module) {
self.context_call(machine_st, name, arity, idx, indices)?; self.context_call(machine_st, name, arity, idx, indices)?;
} else { } else {
let h = machine_st.heap.h; try_in_situ(machine_st, name, arity, indices, machine_st.last_call)?;
let stub = MachineError::functor_stub(clause_name!("call"), arity + 1);
let key = ExistenceError::Procedure(name, arity);
return Err(
machine_st.error_form(MachineError::existence_error(h, key), stub)
);
} }
} }
ClauseType::Hook(_) | ClauseType::System(_) => { ClauseType::Hook(_) | ClauseType::System(_) => {

View File

@@ -107,13 +107,13 @@ impl SubModuleUser for IndexStore {
&mut self.op_dir &mut self.op_dir
} }
fn get_code_index(&self, key: PredicateKey, module: ClauseName) -> Option<CodeIndex> { fn get_code_index(&self, key: PredicateKey, module_name: ClauseName) -> Option<CodeIndex> {
match module.as_str() { match module_name.as_str() {
"user" | "builtin" => self.code_dir.get(&key).cloned(), "user" | "builtin" => self.code_dir.get(&key).cloned(),
_ => self _ => self
.modules .modules
.get(&module) .get(&module_name)
.and_then(|ref module| module.code_dir.get(&key).cloned().map(CodeIndex::from)), .and_then(|ref module| module.code_dir.get(&key).cloned().map(CodeIndex::from))
} }
} }
@@ -132,7 +132,7 @@ impl SubModuleUser for IndexStore {
return; return;
} }
self.code_dir.insert((name, arity), idx); self.code_dir.insert((name.clone(), arity), idx.clone());
} }
fn use_qualified_module( fn use_qualified_module(
@@ -241,7 +241,7 @@ impl Machine {
pub fn run_init_code(&mut self, code: Code) -> bool { pub fn run_init_code(&mut self, code: Code) -> bool {
let old_machine_st = self.sink_to_snapshot(); let old_machine_st = self.sink_to_snapshot();
self.machine_st.reset(); self.machine_st.reset();
self.code_repo.cached_query = code; self.code_repo.cached_query = code;
self.run_query(); self.run_query();
@@ -418,14 +418,14 @@ impl Machine {
_ => _ =>
unreachable!() unreachable!()
}; };
let arity = match &self.machine_st.heap[s+2] { let arity = match &self.machine_st.heap[s+2] {
&HeapCellValue::Addr(Addr::Con(Constant::Integer(ref arity))) => &HeapCellValue::Addr(Addr::Con(Constant::Integer(ref arity))) =>
arity.to_usize().unwrap(), arity.to_usize().unwrap(),
_ => _ =>
unreachable!() unreachable!()
}; };
exports.push(ModuleExport::PredicateKey((name, arity))); exports.push(ModuleExport::PredicateKey((name, arity)));
} }
HeapCellValue::NamedStr(arity, ref name, _) HeapCellValue::NamedStr(arity, ref name, _)
@@ -443,7 +443,7 @@ impl Machine {
_ => _ =>
unreachable!() unreachable!()
}; };
let prec = match &self.machine_st.heap[s+1] { let prec = match &self.machine_st.heap[s+1] {
&HeapCellValue::Addr(Addr::Con(Constant::Integer(ref arity))) => &HeapCellValue::Addr(Addr::Con(Constant::Integer(ref arity))) =>
arity.to_usize().unwrap(), arity.to_usize().unwrap(),
@@ -593,7 +593,7 @@ impl Machine {
fn sink_to_snapshot(&mut self) -> MachineState { fn sink_to_snapshot(&mut self) -> MachineState {
let mut snapshot = MachineState::with_capacity(0); let mut snapshot = MachineState::with_capacity(0);
snapshot.hb = self.machine_st.hb; snapshot.hb = self.machine_st.hb;
snapshot.e = self.machine_st.e; snapshot.e = self.machine_st.e;
snapshot.b = self.machine_st.b; snapshot.b = self.machine_st.b;
@@ -619,7 +619,7 @@ impl Machine {
snapshot snapshot
} }
fn absorb_snapshot(&mut self, mut snapshot: MachineState) { fn absorb_snapshot(&mut self, mut snapshot: MachineState) {
self.machine_st.hb = snapshot.hb; self.machine_st.hb = snapshot.hb;
self.machine_st.e = snapshot.e; self.machine_st.e = snapshot.e;
self.machine_st.b = snapshot.b; self.machine_st.b = snapshot.b;
@@ -667,7 +667,7 @@ impl Machine {
// so hold onto it locally and restore it after the compiler has finished. // so hold onto it locally and restore it after the compiler has finished.
self.machine_st.fail = false; self.machine_st.fail = false;
let cached_query = mem::replace(&mut self.code_repo.cached_query, vec![]); let cached_query = mem::replace(&mut self.code_repo.cached_query, vec![]);
self.dynamic_transaction(trans_type, p); self.dynamic_transaction(trans_type, p);
self.code_repo.cached_query = cached_query; self.code_repo.cached_query = cached_query;

View File

@@ -161,7 +161,7 @@ impl<'a, R: Read> TermStream<'a, R> {
pub fn col_num(&self) -> usize { pub fn col_num(&self) -> usize {
self.parser.col_num() self.parser.col_num()
} }
#[inline] #[inline]
pub fn update_expansion_lens(&mut self) { pub fn update_expansion_lens(&mut self) {
let te_key = (clause_name!("term_expansion"), 2); let te_key = (clause_name!("term_expansion"), 2);
@@ -267,7 +267,7 @@ impl<'a, R: Read> TermStream<'a, R> {
let line_num = self.line_num(); let line_num = self.line_num();
let col_num = self.col_num(); let col_num = self.col_num();
let term = self.parser.read_term(composite_op!( let term = self.parser.read_term(composite_op!(
self.in_module, self.in_module,
&self.wam.indices.op_dir, &self.wam.indices.op_dir,
@@ -308,7 +308,7 @@ impl<'a, R: Read> TermStream<'a, R> {
initial_term, initial_term,
clause_name!(","), clause_name!(","),
))); )));
Ok(Term::Clause(cell, name, terms, arity)) Ok(Term::Clause(cell, name, terms, arity))
} }
_ => Ok(term), _ => Ok(term),
@@ -324,7 +324,7 @@ impl<'a, R: Read> TermStream<'a, R> {
) -> Result<Vec<Term>, ParserError> { ) -> Result<Vec<Term>, ParserError> {
let mut results = vec![]; let mut results = vec![];
while let Some(term) = terms.pop_front() { while let Some(term) = terms.pop_front() {
match machine_st.try_expand_term(self.wam, &term, CompileTimeHook::GoalExpansion) { match machine_st.try_expand_term(self.wam, &term, CompileTimeHook::GoalExpansion) {
Some(term_string) => { Some(term_string) => {
let term = self.parse_expansion_output(term_string.as_str(), op_dir)?; let term = self.parse_expansion_output(term_string.as_str(), op_dir)?;
@@ -382,14 +382,14 @@ impl MachineState {
) -> Option<String> { ) -> Option<String> {
let term_write_result = write_term_to_heap(term, self); let term_write_result = write_term_to_heap(term, self);
let h = self.heap.h; let h = self.heap.h;
self[temp_v!(1)] = Addr::HeapCell(term_write_result.heap_loc); self[temp_v!(1)] = Addr::HeapCell(term_write_result.heap_loc);
self.heap.push(HeapCellValue::Addr(Addr::HeapCell(h))); self.heap.push(HeapCellValue::Addr(Addr::HeapCell(h)));
self[temp_v!(2)] = Addr::HeapCell(h); self[temp_v!(2)] = Addr::HeapCell(h);
let code = vec![call_clause!(ClauseType::Hook(hook), 2, 0, true)]; let code = vec![call_clause!(ClauseType::Hook(hook), 2, 0, true)];
wam.code_repo.cached_query = code; wam.code_repo.cached_query = code;
self.query_stepper( self.query_stepper(
&mut wam.indices, &mut wam.indices,
&mut wam.policies, &mut wam.policies,

View File

@@ -60,8 +60,8 @@ impl<'a, 'b, 'c, R: Read> CompositeIndices<'a, 'b, 'c, R> {
IndexSource::TermStream => &mut self.term_stream.wam.indices.code_dir, IndexSource::TermStream => &mut self.term_stream.wam.indices.code_dir,
IndexSource::Local(ref mut indices) => &mut indices.code_dir, IndexSource::Local(ref mut indices) => &mut indices.code_dir,
} }
} }
fn static_code_dir(&self) -> Option<&CodeDir> { fn static_code_dir(&self) -> Option<&CodeDir> {
match self.static_code_dir { match self.static_code_dir {
Some(IndexSource::TermStream) => Some(&self.term_stream.wam.indices.code_dir), Some(IndexSource::TermStream) => Some(&self.term_stream.wam.indices.code_dir),
@@ -81,11 +81,11 @@ impl<'a, 'b, 'c, R: Read> CompositeIndices<'a, 'b, 'c, R> {
}; };
if let Some(idx) = idx_opt { if let Some(idx) = idx_opt {
self.local_code_dir().insert((name, arity), idx.clone()); self.local_code_dir().insert((name.clone(), arity), idx.clone());
idx idx
} else { } else {
let idx = CodeIndex::default(); let idx = CodeIndex::default();
self.local_code_dir().insert((name, arity), idx.clone()); self.local_code_dir().insert((name.clone(), arity), idx.clone());
idx idx
} }
} }
@@ -594,7 +594,7 @@ impl RelationWorker {
fn fabricate_disjunct(&self, body_term: Term) -> (JumpStub, VecDeque<Term>) { fn fabricate_disjunct(&self, body_term: Term) -> (JumpStub, VecDeque<Term>) {
let vars = self.compute_head(&body_term); let vars = self.compute_head(&body_term);
let clauses: Vec<_> = unfold_by_str(body_term, ";") let results = unfold_by_str(body_term, ";")
.into_iter() .into_iter()
.map(|term| { .map(|term| {
let mut subterms = unfold_by_str(term, ","); let mut subterms = unfold_by_str(term, ",");
@@ -603,13 +603,10 @@ impl RelationWorker {
check_for_internal_if_then(&mut subterms); check_for_internal_if_then(&mut subterms);
let term = subterms.pop().unwrap(); let term = subterms.pop().unwrap();
fold_by_str(subterms.into_iter(), term, clause_name!(",")) let clause = fold_by_str(subterms.into_iter(), term, clause_name!(","));
})
.collect();
let results = clauses self.fabricate_rule_body(&vars, clause)
.into_iter() })
.map(|clause| self.fabricate_rule_body(&vars, clause))
.collect(); .collect();
(vars, results) (vars, results)
@@ -984,7 +981,7 @@ impl<'a, R: Read> TopLevelBatchWorker<'a, R> {
if self.in_module { None } else { Some(IndexSource::TermStream) } if self.in_module { None } else { Some(IndexSource::TermStream) }
); );
let queue = self.rel_worker.parse_queue(&mut indices)?; let queue = self.rel_worker.parse_queue(&mut indices)?;
let result = (append_preds(preds), queue); let result = (append_preds(preds), queue);
let in_situ_code_dir = &mut indices.term_stream.wam.indices.in_situ_code_dir; let in_situ_code_dir = &mut indices.term_stream.wam.indices.in_situ_code_dir;