]> Repositorios git - scryer-prolog.git/commitdiff
remove need for RefCell wrapping on CodeDir
authorMark Thom <[email protected]>
Fri, 5 Oct 2018 03:43:49 +0000 (21:43 -0600)
committerMark Thom <[email protected]>
Fri, 5 Oct 2018 03:43:49 +0000 (21:43 -0600)
src/prolog/compile.rs
src/prolog/machine/machine_state.rs
src/prolog/machine/machine_state_impl.rs
src/prolog/machine/mod.rs
src/prolog/machine/system_calls.rs
src/prolog/machine/term_expansion.rs
src/prolog/macros.rs
src/prolog/read.rs
src/prolog/toplevel.rs

index 3784296d586ebe9916acf482259c3e39e092c806..343a364bd5bbff08c26c000bde7a621fbeb40e1b 100644 (file)
@@ -98,13 +98,10 @@ fn compile_query(terms: Vec<QueryTerm>, queue: Vec<TopLevel>, flags: MachineFlag
 }
 
 fn package_term(wam: &mut Machine, term: Term) -> Result<TopLevelPacket, ParserError> {
-    let code_dir = wam.code_dir.clone();
-    let indices = machine_code_indices!(wam.atom_tbl.clone(),
-                                        &mut CodeDir::new(),
-                                        &mut wam.op_dir,
-                                        &mut wam.modules);
-
-    consume_term(code_dir, term, indices)
+    let mut code_dir = wam.take_code_dir();
+    let packet = consume_term(&mut code_dir, term, &mut wam.indices)?;    
+    wam.swap_code_dir(&mut code_dir); 
+    Ok(packet)
 }
 
 pub fn compile_term(wam: &mut Machine, term: Term) -> EvalSession
@@ -119,7 +116,7 @@ pub fn compile_term(wam: &mut Machine, term: Term) -> EvalSession
             },
         TopLevelPacket::Decl(TopLevel::Declaration(decl), _) => {
             let mut compiler = ListingCompiler::new();
-            let mut indices = default_machine_code_indices!(wam.atom_tbl.clone());
+            let mut indices = default_index_store!(wam.indices.atom_tbl.clone());
 
             try_eval_session!(compiler.process_decl(decl, wam, &mut indices));
             try_eval_session!(compiler.add_code(wam, vec![], indices));
@@ -143,7 +140,7 @@ impl ListingCompiler {
         }
     }
 
-    fn use_module(&mut self, submodule: Module, wam: &mut Machine, indices: &mut MachineCodeIndices)
+    fn use_module(&mut self, submodule: Module, wam: &mut Machine, indices: &mut IndexStore)
                   -> Result<(), SessionError>
     {
         let mod_name = self.get_module_name();
@@ -161,7 +158,7 @@ impl ListingCompiler {
     }
 
     fn use_qualified_module(&mut self, submodule: Module, wam: &mut Machine,
-                            exports: &Vec<PredicateKey>, indices: &mut MachineCodeIndices)
+                            exports: &Vec<PredicateKey>, indices: &mut IndexStore)
                             -> Result<(), SessionError>
     {
         let mod_name = self.get_module_name();
@@ -213,11 +210,11 @@ impl ListingCompiler {
         Ok(code)
     }
 
-    fn add_code(&mut self, wam: &mut Machine, code: Code, indices: MachineCodeIndices)
+    fn add_code(&mut self, wam: &mut Machine, code: Code, mut indices: IndexStore)
                 -> Result<(), SessionError>
     {
-        let code_dir = mem::replace(indices.code_dir, HashMap::new());
-        let op_dir   = mem::replace(indices.op_dir, HashMap::new());
+        let code_dir = mem::replace(&mut indices.code_dir, CodeDir::new());
+        let op_dir   = mem::replace(&mut indices.op_dir, OpDir::new());
 
         if let Some(mut module) = self.module.take() {
             module.code_dir.extend(as_module_code_dir(code_dir));
@@ -236,7 +233,7 @@ impl ListingCompiler {
         self.non_counted_bt_preds.insert((name, arity));
     }
 
-    fn process_decl(&mut self, decl: Declaration, wam: &mut Machine, indices: &mut MachineCodeIndices)
+    fn process_decl(&mut self, decl: Declaration, wam: &mut Machine, indices: &mut IndexStore)
                     -> Result<(), SessionError>
     {
         match decl {
@@ -272,13 +269,15 @@ impl ListingCompiler {
 }
 
 pub
-fn compile_listing<'a, R: Read>(wam: &mut Machine, src: R, mut indices: MachineCodeIndices<'a>,
-                                mut toplevel_indices: MachineCodeIndices<'a>)
-                                -> EvalSession
+fn compile_listing<R: Read>(wam: &mut Machine, src: R, mut indices: IndexStore,
+                            mut toplevel_indices: IndexStore)
+                            -> EvalSession
 {
-    let mut worker = TopLevelBatchWorker::new(src, wam.atom_tbl.clone(),
+    let code_dir = wam.take_code_dir();
+    let mut worker = TopLevelBatchWorker::new(src, wam.indices.atom_tbl.clone(),
                                               wam.machine_flags(),
-                                              wam.code_dir.clone());
+                                              code_dir);
+    
     let mut compiler = ListingCompiler::new();
     let mut toplevel_results = vec![];
 
@@ -298,6 +297,8 @@ fn compile_listing<'a, R: Read>(wam: &mut Machine, src: R, mut indices: MachineC
         }
     }
 
+    wam.swap_code_dir(&mut worker.static_code_dir);
+    
     let module_code = try_eval_session!(compiler.generate_code(worker.results, wam,
                                                                &mut indices.code_dir));
     let toplvl_code = try_eval_session!(compiler.generate_code(toplevel_results, wam,
@@ -309,8 +310,8 @@ fn compile_listing<'a, R: Read>(wam: &mut Machine, src: R, mut indices: MachineC
     EvalSession::EntrySuccess
 }
 
-fn setup_indices(wam: &Machine, indices: &mut MachineCodeIndices) -> Result<(), SessionError> {
-    if let Some(ref builtins) = wam.modules.get(&clause_name!("builtins")) {
+fn setup_indices(wam: &Machine, indices: &mut IndexStore) -> Result<(), SessionError> {
+    if let Some(ref builtins) = wam.indices.modules.get(&clause_name!("builtins")) {
         indices.use_module(builtins)
     } else {
         Err(SessionError::ModuleNotFound)
@@ -318,10 +319,10 @@ fn setup_indices(wam: &Machine, indices: &mut MachineCodeIndices) -> Result<(),
 }
 
 pub fn compile_user_module<R: Read>(wam: &mut Machine, src: R) -> EvalSession {
-    let atom_tbl = wam.atom_tbl.clone();    
-    let mut indices = default_machine_code_indices!(atom_tbl.clone());
+    let atom_tbl = wam.indices.atom_tbl.clone();    
+    let mut indices = default_index_store!(atom_tbl.clone());
     
     try_eval_session!(setup_indices(&wam, &mut indices));
     
-    compile_listing(wam, src, indices, default_machine_code_indices!(atom_tbl))
+    compile_listing(wam, src, indices, default_index_store!(atom_tbl))
 }
index 84cacb482b7f2f0db1fecd0ef927b638cc1fa9ef..a01fd6d94b94fac32521f98c5c1e67d15ddf890f 100644 (file)
@@ -5,7 +5,7 @@ use prolog::instructions::*;
 use prolog::and_stack::*;
 use prolog::copier::*;
 use prolog::heap_print::*;
-use prolog::machine::MachineCodeIndices;
+use prolog::machine::IndexStore;
 use prolog::machine::machine_errors::*;
 use prolog::num::{BigInt, BigUint, Zero, One};
 use prolog::or_stack::*;
@@ -34,39 +34,6 @@ impl Ball {
     }
 }
 
-#[derive(Clone, Copy)]
-pub(crate) struct CodeDirs<'a> {
-    pub code_dir: &'a CodeDir,
-    pub op_dir: &'a OpDir,
-    pub modules: &'a ModuleDir
-}
-
-impl<'a> CodeDirs<'a> {
-    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)))
-            .cloned()
-    }
-
-    pub(super) fn get_cleaner_sites(&self) -> (usize, usize) {
-        let r_w_h  = clause_name!("run_cleaners_with_handling");
-        let r_wo_h = clause_name!("run_cleaners_without_handling");
-
-        let builtins = clause_name!("builtins");
-
-        let r_w_h  = self.get_internal(r_w_h, 0, builtins.clone()).and_then(|item| item.local());
-        let r_wo_h = self.get_internal(r_wo_h, 1, builtins).and_then(|item| item.local());
-
-        if let Some(r_w_h) = r_w_h {
-            if let Some(r_wo_h) = r_wo_h {
-                return (r_w_h, r_wo_h);
-            }
-        }
-
-        return (0, 0);
-    }
-}
-
 pub(super) struct DuplicateTerm<'a> {
     state: &'a mut MachineState
 }
@@ -431,8 +398,8 @@ pub(crate) trait CallPolicy: Any {
         Ok(())
     }
 
-    fn context_call(&mut self, machine_st: &mut MachineState, name: ClauseName, arity: usize,
-                    idx: CodeIndex, indices: MachineCodeIndices)
+    fn context_call(&mut self, machine_st: &mut MachineState, name: ClauseName,
+                    arity: usize, idx: CodeIndex, indices: &mut IndexStore)
                     -> CallResult
     {
         if machine_st.last_call {
@@ -442,9 +409,9 @@ pub(crate) trait CallPolicy: Any {
         }
     }
 
-    fn try_call<'a>(&mut self, machine_st: &mut MachineState, name: ClauseName, arity: usize,
-                    idx: CodeIndex, indices: MachineCodeIndices<'a>)
-                    -> CallResult
+    fn try_call(&mut self, machine_st: &mut MachineState, name: ClauseName, arity: usize,
+                idx: CodeIndex, indices: &IndexStore)
+                -> CallResult
     {
         match idx.0.borrow().0 {
             IndexPtr::Module => {
@@ -477,9 +444,9 @@ pub(crate) trait CallPolicy: Any {
         Ok(())
     }
 
-    fn try_execute<'a>(&mut self, machine_st: &mut MachineState, name: ClauseName,
-                       arity: usize, idx: CodeIndex, indices: MachineCodeIndices<'a>)
-                       -> CallResult
+    fn try_execute(&mut self, machine_st: &mut MachineState, name: ClauseName,
+                   arity: usize, idx: CodeIndex, indices: &IndexStore)
+                   -> CallResult
     {
         match idx.0.borrow().0 {
             IndexPtr::Module => {
@@ -512,9 +479,9 @@ pub(crate) trait CallPolicy: Any {
         Ok(())
     }
 
-    fn call_builtin<'a>(&mut self, machine_st: &mut MachineState, ct: &BuiltInClauseType,
-                        indices: MachineCodeIndices<'a>) //code_dirs: CodeDirs)
-                        -> CallResult
+    fn call_builtin(&mut self, machine_st: &mut MachineState, ct: &BuiltInClauseType,
+                    indices: &mut IndexStore)
+                    -> CallResult
     {
         match ct {
             &BuiltInClauseType::AcyclicTerm => {
@@ -682,9 +649,8 @@ pub(crate) trait CallPolicy: Any {
         Ok(())
     }
 
-    fn call_n<'a>(&mut self, machine_st: &mut MachineState, arity: usize,
-                  indices: MachineCodeIndices<'a>)
-                  -> CallResult
+    fn call_n(&mut self, machine_st: &mut MachineState, arity: usize, indices: &mut IndexStore)
+              -> CallResult
     {
         if let Some((name, arity)) = machine_st.setup_call_n(arity) {
             match ClauseType::from(name.clone(), arity, None) {
@@ -731,9 +697,9 @@ 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, indices: MachineCodeIndices<'a>)
-                        -> CallResult
+    fn context_call(&mut self, machine_st: &mut MachineState, name: ClauseName,
+                    arity: usize, idx: CodeIndex, indices: &mut IndexStore)
+                    -> CallResult
     {
         self.prev_policy.context_call(machine_st, name, arity, idx, indices)?;
         self.increment(machine_st)
@@ -763,16 +729,16 @@ impl CallPolicy for CWILCallPolicy {
         self.increment(machine_st)
     }
 
-    fn call_builtin<'a>(&mut self, machine_st: &mut MachineState, ct: &BuiltInClauseType,
-                        indices: MachineCodeIndices<'a>)
-                        -> CallResult
+    fn call_builtin(&mut self, machine_st: &mut MachineState, ct: &BuiltInClauseType,
+                    indices: &mut IndexStore)
+                    -> CallResult
     {
         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, indices: MachineCodeIndices<'a>)
-                  -> CallResult
+    fn call_n(&mut self, machine_st: &mut MachineState, arity: usize, indices: &mut IndexStore)
+              -> CallResult
     {
         self.prev_policy.call_n(machine_st, arity, indices)?;
         self.increment(machine_st)
index e7f86b8b9cc4d982fc83a566e10bbcf60a406685..9465b3ab432d1f1f56cc0ab5be44e580b3cfe6d6 100644 (file)
@@ -6,7 +6,7 @@ use prolog::and_stack::*;
 use prolog::copier::*;
 use prolog::heap_iter::*;
 use prolog::heap_print::*;
-use prolog::machine::MachineCodeIndices;
+use prolog::machine::IndexStore;
 use prolog::machine::machine_errors::*;
 use prolog::machine::machine_state::*;
 use prolog::num::{Integer, Signed, ToPrimitive, Zero};
@@ -2126,13 +2126,13 @@ impl MachineState {
         self.p += 1;
     }
 
-    fn handle_call_clause<'a>(&mut self, indices: MachineCodeIndices<'a>,
-                              call_policy: &mut Box<CallPolicy>,
-                              cut_policy:  &mut Box<CutPolicy>,
-                              ct: &ClauseType,
-                              arity: usize,
-                              lco: bool,
-                              use_default_cp: bool)
+    fn handle_call_clause(&mut self, indices: &mut IndexStore,
+                          call_policy: &mut Box<CallPolicy>,
+                          cut_policy:  &mut Box<CutPolicy>,
+                          ct: &ClauseType,
+                          arity: usize,
+                          lco: bool,
+                          use_default_cp: bool)
     {
         let mut default_call_policy: Box<CallPolicy> = Box::new(DefaultCallPolicy {});
         let call_policy = if use_default_cp {
@@ -2160,10 +2160,10 @@ impl MachineState {
         };
     }
 
-    pub(super) fn execute_ctrl_instr<'a>(&mut self, indices: MachineCodeIndices<'a>,
-                                         call_policy: &mut Box<CallPolicy>,
-                                         cut_policy:  &mut Box<CutPolicy>,
-                                         instr: &ControlInstruction)
+    pub(super) fn execute_ctrl_instr(&mut self, indices: &mut IndexStore,
+                                     call_policy: &mut Box<CallPolicy>,
+                                     cut_policy:  &mut Box<CutPolicy>,
+                                     instr: &ControlInstruction)
     {
         match instr {
             &ControlInstruction::Allocate(num_cells) =>
index ac5bfe183a9f6c0727ab5f26fae205f50b230aa2..ea00097efc176ed65bb85cfe9a8ad7bb0464a68b 100644 (file)
@@ -16,37 +16,65 @@ mod system_calls;
 
 use prolog::machine::machine_state::*;
 
-use std::cell::RefCell;
 use std::collections::HashMap;
-use std::mem::swap;
+use std::mem;
 use std::ops::Index;
 use std::rc::Rc;
 
 static BUILTINS: &str = include_str!("../lib/builtins.pl");
 
-pub struct MachineCodeIndices<'a> {
+pub struct IndexStore {
     pub(super) atom_tbl: TabledData<Atom>,
-    pub(super) code_dir: &'a mut CodeDir,
-    pub(super) op_dir: &'a mut OpDir,
-    pub(super) modules: &'a mut ModuleDir
+    pub(super) code_dir: CodeDir,
+    pub(super) op_dir: OpDir,
+    pub(super) modules: ModuleDir
 }
 
-impl<'a> MachineCodeIndices<'a> {
+impl IndexStore {
     #[inline]
-    pub(super) fn copy_and_swap(&mut self, other: &mut MachineCodeIndices<'a>) {
-        *self.code_dir = other.code_dir.clone();
-        *self.op_dir = other.op_dir.clone();
-
-        swap(&mut self.code_dir, &mut other.code_dir);
-        swap(&mut self.op_dir, &mut other.op_dir);
-        swap(&mut self.modules, &mut other.modules);
+    pub(super) fn new() -> Self {
+        IndexStore {
+            atom_tbl: TabledData::new(Rc::new("user".to_string())),
+            code_dir: CodeDir::new(),
+            op_dir: default_op_dir(),
+            modules: ModuleDir::new()
+        }
     }
 
     #[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(super) fn copy_and_swap(&mut self, other: &mut IndexStore) {
+        self.code_dir = other.code_dir.clone();
+        self.op_dir = other.op_dir.clone();
+
+        mem::swap(&mut self.code_dir, &mut other.code_dir);
+        mem::swap(&mut self.op_dir, &mut other.op_dir);
+        mem::swap(&mut self.modules, &mut other.modules);
+    }
+
+    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)))
+            .cloned()
+    }
+
+    pub(super) fn get_cleaner_sites(&self) -> (usize, usize) {
+        let r_w_h  = clause_name!("run_cleaners_with_handling");
+        let r_wo_h = clause_name!("run_cleaners_without_handling");
+
+        let builtins = clause_name!("builtins");
+
+        let r_w_h  = self.get_internal(r_w_h, 0, builtins.clone()).and_then(|item| item.local());
+        let r_wo_h = self.get_internal(r_wo_h, 1, builtins).and_then(|item| item.local());
+
+        if let Some(r_w_h) = r_w_h {
+            if let Some(r_wo_h) = r_wo_h {
+                return (r_w_h, r_wo_h);
+            }
+        }
+
+        return (0, 0);
     }
 }
 
@@ -55,12 +83,9 @@ pub struct Machine {
     call_policy: Box<CallPolicy>,
     cut_policy: Box<CutPolicy>,
     code: Code,
-    pub(super) atom_tbl: TabledData<Atom>,
-    pub(super) code_dir: Rc<RefCell<CodeDir>>,
-    pub(super) op_dir: OpDir,
+    pub(super) indices: IndexStore,
     term_dir: TermDir,
     term_expanders: Code,
-    pub(super) modules: ModuleDir,
     cached_query: Option<Code>
 }
 
@@ -81,13 +106,13 @@ impl Index<LocalCodePtr> for Machine {
     }
 }
 
-impl<'a> SubModuleUser for MachineCodeIndices<'a> {
+impl SubModuleUser for IndexStore {
     fn atom_tbl(&self) -> TabledData<Atom> {
         self.atom_tbl.clone()
     }
     
     fn op_dir(&mut self) -> &mut OpDir {
-        self.op_dir
+        &mut self.op_dir
     }
 
     fn get_code_index(&self, key: PredicateKey, module: ClauseName) -> Option<CodeIndex>
@@ -132,20 +157,17 @@ impl Machine {
             call_policy: Box::new(DefaultCallPolicy {}),
             cut_policy: Box::new(DefaultCutPolicy {}),
             code: Code::new(),
-            atom_tbl: TabledData::new(Rc::new("user".to_string())),
-            code_dir: Rc::new(RefCell::new(CodeDir::new())),
-            op_dir: default_op_dir(),
+            indices: IndexStore::new(),
             term_dir: TermDir::new(),
             term_expanders: Code::new(),
-            modules: HashMap::new(),
             cached_query: None
         };
 
-        let atom_tbl = wam.atom_tbl.clone();
+        let atom_tbl = wam.indices.atom_tbl.clone();
         
         compile_listing(&mut wam, BUILTINS.as_bytes(),
-                        default_machine_code_indices!(atom_tbl.clone()),
-                        default_machine_code_indices!(atom_tbl));
+                        default_index_store!(atom_tbl.clone()),
+                        default_index_store!(atom_tbl));
 
         compile_user_module(&mut wam, LISTS.as_bytes());
         compile_user_module(&mut wam, CONTROL.as_bytes());
@@ -178,7 +200,7 @@ impl Machine {
                 }
             };
 
-            if let Some(ref existing_idx) = self.code_dir.borrow().get(&key) {
+            if let Some(ref existing_idx) = self.indices.code_dir.get(&key) {
                 // ensure we don't try to overwrite an existing predicate from a different module.
                 if !existing_idx.is_undefined() && !idx.is_undefined() {
                     // allow the overwriting of user-level predicates by all other predicates.
@@ -197,7 +219,7 @@ impl Machine {
 
         // error detection has finished, so update the master index of keys.
         for (key, idx) in code_dir {
-            if let Some(ref mut master_idx) = self.code_dir.borrow_mut().get_mut(&key) {
+            if let Some(ref mut master_idx) = self.indices.code_dir.get_mut(&key) {
                 // ensure we don't double borrow if master_idx == idx.
                 // we don't need to modify anything in that case.
                 if !Rc::ptr_eq(&master_idx.0, &idx.0) {
@@ -207,7 +229,7 @@ impl Machine {
                 continue;
             }
 
-            self.code_dir.borrow_mut().insert(key.clone(), idx.clone());
+            self.indices.code_dir.insert(key.clone(), idx.clone());
         }
 
         self.code.extend(code.into_iter());
@@ -216,31 +238,37 @@ impl Machine {
 
     #[inline]
     pub fn add_batched_ops(&mut self, op_dir: OpDir) {
-        self.op_dir.extend(op_dir.into_iter());
+        self.indices.op_dir.extend(op_dir.into_iter());
     }
 
     #[inline]
     pub fn remove_module(&mut self, module: &Module) {
-        let mut indices = machine_code_indices!(self.atom_tbl.clone(),
-                                                &mut self.code_dir.borrow_mut(),
-                                                &mut self.op_dir,
-                                                &mut self.modules);
-        indices.remove_module(clause_name!("user"), module);
+        self.indices.remove_module(clause_name!("user"), module);
     }
 
     #[inline]
     pub fn take_module(&mut self, name: ClauseName) -> Option<Module> {
-        self.modules.remove(&name)
+        self.indices.modules.remove(&name)
     }
 
+    #[inline]
+    pub fn take_code_dir(&mut self) -> CodeDir {
+        mem::replace(&mut self.indices.code_dir, CodeDir::new())
+    }
+    
+    #[inline]
+    pub fn swap_code_dir(&mut self, code_dir: &mut CodeDir) {
+        mem::swap(&mut self.indices.code_dir, code_dir);        
+    }
+    
     #[inline]
     pub fn insert_module(&mut self, module: Module) {
-        self.modules.insert(module.module_decl.name.clone(), module);
+        self.indices.modules.insert(module.module_decl.name.clone(), module);
     }
 
     #[inline]
     pub fn add_module(&mut self, module: Module, code: Code) {
-        self.modules.insert(module.module_decl.name.clone(), module);
+        self.indices.modules.insert(module.module_decl.name.clone(), module);
         self.code.extend(code.into_iter());
     }
 
@@ -300,8 +328,6 @@ impl Machine {
             None => return
         };
 
-        let atom_tbl = self.atom_tbl.clone();
-
         match instr {
             Line::Arithmetic(ref arith_instr) =>
                 self.ms.execute_arith_instr(arith_instr),
@@ -309,15 +335,9 @@ impl Machine {
                 self.ms.execute_choice_instr(choice_instr, &mut self.call_policy),
             Line::Cut(ref cut_instr) =>
                 self.ms.execute_cut_instr(cut_instr, &mut self.cut_policy),
-            Line::Control(ref control_instr) => {
-                let indices = machine_code_indices!(atom_tbl,
-                                                    &mut self.code_dir.borrow_mut(),
-                                                    &mut self.op_dir,
-                                                    &mut self.modules);
-
-                self.ms.execute_ctrl_instr(indices, &mut self.call_policy,
-                                           &mut self.cut_policy, control_instr)
-            },
+            Line::Control(ref control_instr) => 
+                self.ms.execute_ctrl_instr(&mut self.indices, &mut self.call_policy,
+                                           &mut self.cut_policy, control_instr),            
             Line::Fact(ref fact) => {
                 for fact_instr in fact {
                     if self.failed() {
@@ -515,7 +535,7 @@ impl Machine {
 
     pub fn clear(&mut self) {
         let mut machine = Machine::new();
-        swap(self, &mut machine);
+        mem::swap(self, &mut machine);
     }
 
     pub fn reset(&mut self) {
index 639f078d21586f6374aa3f13e35af5859cc8fe91..f193e034d3ad6dbc8df3c07938291a834e43ccd3 100644 (file)
@@ -2,7 +2,7 @@ use prolog_parser::ast::*;
 
 use prolog::heap_iter::*;
 use prolog::instructions::*;
-use prolog::machine::MachineCodeIndices;
+use prolog::machine::IndexStore;
 use prolog::machine::machine_errors::*;
 use prolog::machine::machine_state::*;
 use prolog::num::{ToPrimitive, Zero};
@@ -187,11 +187,11 @@ impl MachineState {
         }
     }
 
-    pub(super) fn system_call<'a>(&mut self, ct: &SystemClauseType,
-                                  indices: MachineCodeIndices<'a>,
-                                  call_policy: &mut Box<CallPolicy>,
-                                  cut_policy:  &mut Box<CutPolicy>,)
-                                  -> CallResult
+    pub(super) fn system_call(&mut self, ct: &SystemClauseType,
+                              indices: &IndexStore,
+                              call_policy: &mut Box<CallPolicy>,
+                              cut_policy:  &mut Box<CutPolicy>,)
+                              -> CallResult
     {
         match ct {
             &SystemClauseType::CheckCutPoint => {
@@ -242,7 +242,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) = indices.to_code_dirs().get_cleaner_sites();
+                    let (r_c_w_h, r_c_wo_h) = indices.get_cleaner_sites();
                     *cut_policy = Box::new(SCCCutPolicy::new(r_c_w_h, r_c_wo_h));
                 }
 
index 488763379970737c31b69a8691ed221dbbb45856..e4ee205679c71332a5e2bb558743476319fe0199 100644 (file)
@@ -77,7 +77,8 @@ impl<R: Read> TermStream<R> {
                 };
             }
 
-            let term = self.parser.read_term(composite_op!(self.in_module, &wam.op_dir, op_dir))?;
+            let term = self.parser.read_term(composite_op!(self.in_module, &wam.indices.op_dir,
+                                                           op_dir))?;
             self.stack.push(term);
         }
     }
index 2eb18bbaa82d932dff1fc8fee875fad342f6324e..dfd45cfd23e96f0ac88a0fa281b6398944c84ade 100644 (file)
@@ -209,21 +209,18 @@ macro_rules! set_code_index {
     }}
 }
 
-macro_rules! machine_code_indices {
+macro_rules! index_store {
     ($atom_tbl:expr, $code_dir:expr, $op_dir:expr, $modules:expr) => (
-        MachineCodeIndices { atom_tbl: $atom_tbl,
-                             code_dir: $code_dir,
-                             op_dir: $op_dir,
-                             modules: $modules }
+        IndexStore { atom_tbl: $atom_tbl,
+                     code_dir: $code_dir,
+                     op_dir: $op_dir,
+                     modules: $modules }
     )
 }
 
-macro_rules! default_machine_code_indices {
+macro_rules! default_index_store {
     ($atom_tbl:expr) => (
-        machine_code_indices!($atom_tbl,
-                              &mut CodeDir::new(),
-                              &mut default_op_dir(),
-                              &mut HashMap::new())
+        index_store!($atom_tbl, CodeDir::new(), default_op_dir(), HashMap::new())
     )
 }
 
index d2a42ff85391a29ca9c1fbf2d532ab48183a57b5..1b87b6f6a34ba97e61888bc5975b1449e2a5db10 100644 (file)
@@ -45,7 +45,7 @@ pub fn read_toplevel(wam: &mut Machine) -> Result<Input, ParserError> {
             Ok(Input::Batch)
         },
         _ => {
-            let mut term_stream = TermStream::new(stdin.lock(), wam.atom_tbl.clone(),
+            let mut term_stream = TermStream::new(stdin.lock(), wam.indices.atom_tbl.clone(),
                                                   wam.machine_flags());
             term_stream.add_to_top(buffer.as_str());
 
index 65c0ec8c50d3ff26718a87acb867aa850b051493..ab4096fa207b4aff2e1d66ba4e6b24b20de8fdbe 100644 (file)
@@ -13,9 +13,9 @@ use std::io::Read;
 use std::mem;
 use std::rc::Rc;
 
-struct CompositeIndices<'a, 'b : 'a> {
-    local: &'a mut MachineCodeIndices<'b>,
-    static_code_dir: Option<Rc<RefCell<CodeDir>>>
+struct CompositeIndices<'a, 'b> {
+    local: &'a mut IndexStore,
+    static_code_dir: Option<&'b mut CodeDir>
 }
 
 macro_rules! composite_indices {
@@ -32,15 +32,16 @@ macro_rules! composite_indices {
     )
 }
 
-impl<'a, 'b : 'a> CompositeIndices<'a, 'b>
+impl<'a, 'b> CompositeIndices<'a, 'b>
 {
     fn get_code_index(&mut self, name: ClauseName, arity: usize) -> CodeIndex {
-        let idx_opt = self.local.code_dir.get(&(name.clone(), arity)).cloned()
+        let idx_opt = self.local.code_dir.get(&(name.clone(), arity))
             .or_else(|| {
-                self.static_code_dir.clone().and_then(|code_dir| {
-                    code_dir.borrow().get(&(name.clone(), arity)).cloned()
-                })
-            });
+                match &self.static_code_dir {
+                    &Some(ref code_dir) => code_dir.get(&(name.clone(), arity)),
+                    _ => None
+                }
+            }).cloned();
 
         if let Some(idx) = idx_opt {
             self.local.code_dir.insert((name, arity), idx.clone());
@@ -714,17 +715,16 @@ pub fn parse_term<R: Read>(wam: &Machine, buf: R) -> Result<Term, ParserError>
 {
     use prolog_parser::parser::*;
 
-    let mut parser = Parser::new(buf, wam.atom_tbl.clone(), wam.machine_flags());
-    parser.read_term(composite_op!(&wam.op_dir))
+    let mut parser = Parser::new(buf, wam.indices.atom_tbl.clone(), wam.machine_flags());
+    parser.read_term(composite_op!(&wam.indices.op_dir))
 }
 
 pub
-fn consume_term<'a>(static_code_dir: Rc<RefCell<CodeDir>>, term: Term,
-                    mut indices: MachineCodeIndices<'a>)
-                    -> Result<TopLevelPacket, ParserError>
+fn consume_term(static_code_dir: &mut CodeDir, term: Term, indices: &mut IndexStore)
+                -> Result<TopLevelPacket, ParserError>
 {
     let mut rel_worker = RelationWorker::new();
-    let mut indices = composite_indices!(false, &mut indices, static_code_dir);
+    let mut indices = composite_indices!(false, indices, static_code_dir);
 
     let tl = rel_worker.try_term_to_tl(&mut indices, term, true)?;
     let results = rel_worker.parse_queue(&mut indices)?;
@@ -735,14 +735,13 @@ fn consume_term<'a>(static_code_dir: Rc<RefCell<CodeDir>>, term: Term,
 pub struct TopLevelBatchWorker<R: Read> {
     pub(crate) term_stream: TermStream<R>,
     rel_worker: RelationWorker,
-    static_code_dir: Rc<RefCell<CodeDir>>,
+    pub(super) static_code_dir: CodeDir,
     pub(crate) results: Vec<(Predicate, VecDeque<TopLevel>)>,
     pub(crate) in_module: bool
 }
 
 impl<R: Read> TopLevelBatchWorker<R> {
-    pub fn new(inner: R, atom_tbl: TabledData<Atom>, flags: MachineFlags,
-               static_code_dir: Rc<RefCell<CodeDir>>)
+    pub fn new(inner: R, atom_tbl: TabledData<Atom>, flags: MachineFlags, static_code_dir: CodeDir)
                -> Self
     {
         TopLevelBatchWorker { term_stream: TermStream::new(inner, atom_tbl, flags),
@@ -753,12 +752,12 @@ impl<R: Read> TopLevelBatchWorker<R> {
     }
 
     pub
-    fn consume<'a, 'b : 'a>(&mut self, wam: &mut Machine, indices: &'a mut MachineCodeIndices<'b>)
-                           -> Result<Option<Declaration>, SessionError>
+    fn consume(&mut self, wam: &mut Machine, indices: &mut IndexStore)
+               -> Result<Option<Declaration>, SessionError>
     {
         let mut preds = vec![];
         let mut indices = composite_indices!(self.in_module, indices,
-                                             self.static_code_dir.clone());
+                                             &mut self.static_code_dir);
 
         while !self.term_stream.eof()? {
             self.term_stream.empty_tokens(); // empty the parser stack of token descriptions.