tc_insts_xsb_i.h

00001 /* File:      tc_insts_xsb_i.h
00002 ** Author(s): Prasad Rao, Kostis Sagonas
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
00006 ** 
00007 ** XSB is free software; you can redistribute it and/or modify it under the
00008 ** terms of the GNU Library General Public License as published by the Free
00009 ** Software Foundation; either version 2 of the License, or (at your option)
00010 ** any later version.
00011 ** 
00012 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00013 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00015 ** more details.
00016 ** 
00017 ** You should have received a copy of the GNU Library General Public License
00018 ** along with XSB; if not, write to the Free Software Foundation,
00019 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020 **
00021 ** $Id: tc_insts_xsb_i.h,v 1.25 2005/12/12 18:44:53 dwarren Exp $
00022 ** 
00023 */
00024 
00025 #include "debugs/debug_tries.h"
00026 
00027 /*----------------------------------------------------------------------*/
00028 /* The following is the list of Trie-Code instructions.                 */
00029 /*----------------------------------------------------------------------*/
00030 
00031 XSB_Start_Instr(trie_no_cp_str,_trie_no_cp_str)
00032         TRIE_R_LOCK();
00033         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_no_cp_str"));
00034         NodePtr = (BTNptr) lpcreg;
00035         unify_with_trie_str;
00036         non_ftag_lpcreg;
00037 XSB_End_Instr()
00038 
00039 /* TLS: opfail below is actually the sibling trie node */
00040 XSB_Start_Instr(trie_try_str,_trie_try_str) 
00041         CPtr tbreg;
00042 #ifdef SLG_GC
00043         CPtr old_cptop;
00044 #endif
00045         TRIE_R_LOCK();
00046         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_try_str"));
00047         NodePtr = (BTNptr) lpcreg;
00048         save_find_locx(ereg);
00049         tbreg = top_of_cpstack;
00050 #ifdef SLG_GC
00051         old_cptop = tbreg;
00052 #endif
00053         save_trie_registers(tbreg);                        
00054         save_choicepoint(tbreg,ereg,(byte *)opfail,breg);  
00055 #ifdef SLG_GC                                              
00056         cp_prevtop(tbreg) = old_cptop;
00057 #endif                                                     
00058         breg = tbreg;
00059         hbreg = hreg;
00060         unify_with_trie_str;
00061         non_ftag_lpcreg;
00062 XSB_End_Instr()
00063 
00064 XSB_Start_Instr(trie_retry_str,_trie_retry_str) 
00065         CPtr tbreg;
00066         TRIE_R_LOCK();
00067         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_retry_str"));
00068         NodePtr = (BTNptr) lpcreg;
00069         tbreg = breg;
00070         restore_regs_and_vars(tbreg, CP_SIZE);
00071         cp_pcreg(breg) =  (byte *)opfail;
00072         unify_with_trie_str;
00073         non_ftag_lpcreg;
00074 XSB_End_Instr()
00075 
00076 XSB_Start_Instr(trie_trust_str,_trie_trust_str) 
00077         CPtr tbreg;
00078         TRIE_R_LOCK();
00079         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_trust_str"));
00080         NodePtr = (BTNptr) lpcreg;
00081         tbreg = breg;
00082         restore_regs_and_vars(tbreg, CP_SIZE);
00083         breg = cp_prevbreg(breg);       /* Remove this CP */
00084         restore_trail_condition_registers(breg);
00085         unify_with_trie_str;
00086         non_ftag_lpcreg;
00087 XSB_End_Instr()
00088 
00089 /*----------------------------------------------------------------------*/
00090 
00091 XSB_Start_Instr(trie_no_cp_numcon,_trie_no_cp_numcon)
00092         TRIE_R_LOCK();
00093         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_no_cp_numcon:"));
00094         NodePtr = (BTNptr) lpcreg;
00095         unify_with_trie_numcon;
00096         reg_arrayptr--;
00097         non_ftag_lpcreg;
00098 XSB_End_Instr()
00099 
00100 XSB_Start_Instr(trie_try_numcon,_trie_try_numcon) 
00101         CPtr tbreg;
00102 #ifdef SLG_GC
00103         CPtr old_cptop;
00104 #endif
00105         TRIE_R_LOCK();
00106         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_try_numcon"));
00107         NodePtr = (BTNptr) lpcreg;
00108         save_find_locx(ereg);
00109         tbreg = top_of_cpstack;
00110 #ifdef SLG_GC
00111         old_cptop = tbreg;
00112 #endif
00113         save_trie_registers(tbreg);
00114         save_choicepoint(tbreg,ereg,(byte *)opfail,breg);
00115 #ifdef SLG_GC
00116         cp_prevtop(tbreg) = old_cptop;
00117 #endif
00118         breg = tbreg;
00119         hbreg = hreg;
00120         unify_with_trie_numcon;
00121         reg_arrayptr--;
00122         non_ftag_lpcreg;
00123 XSB_End_Instr()
00124 
00125 XSB_Start_Instr(trie_retry_numcon,_trie_retry_numcon) 
00126         CPtr tbreg;
00127         TRIE_R_LOCK();
00128         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_retry_numcon"));
00129         NodePtr = (BTNptr) lpcreg;
00130         tbreg = breg;
00131         restore_regs_and_vars(tbreg, CP_SIZE);
00132         cp_pcreg(breg) = (byte *) opfail;
00133         unify_with_trie_numcon;
00134         reg_arrayptr--;
00135         non_ftag_lpcreg;
00136 XSB_End_Instr()
00137 
00138 XSB_Start_Instr(trie_trust_numcon,_trie_trust_numcon) 
00139         CPtr tbreg;
00140         TRIE_R_LOCK();
00141         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_trust_numcon"));
00142         NodePtr = (BTNptr) lpcreg;
00143         tbreg = breg;
00144         restore_regs_and_vars(tbreg, CP_SIZE);
00145         breg = cp_prevbreg(breg);
00146         restore_trail_condition_registers(breg);
00147         unify_with_trie_numcon;
00148         reg_arrayptr--;
00149         non_ftag_lpcreg;
00150 XSB_End_Instr()
00151 
00152 /*----------------------------------------------------------------------*/
00153 
00154 XSB_Start_Instr(trie_no_cp_numcon_succ,_trie_no_cp_numcon_succ)
00155         TRIE_R_LOCK();
00156         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_no_cp_numcon_succ"));
00157         NodePtr = (BTNptr) lpcreg;
00158         unify_with_trie_numcon;
00159         reg_arrayptr--;
00160         proceed_lpcreg;
00161 XSB_End_Instr()
00162 
00163 XSB_Start_Instr(trie_try_numcon_succ,_trie_try_numcon_succ) 
00164         CPtr tbreg;
00165 #ifdef SLG_GC
00166         CPtr old_cptop;
00167 #endif
00168         TRIE_R_LOCK();
00169         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_try_numcon_succ"));
00170         NodePtr = (BTNptr) lpcreg;
00171         save_find_locx(ereg);
00172         tbreg = top_of_cpstack;
00173 #ifdef SLG_GC
00174         old_cptop = tbreg;
00175 #endif
00176         save_trie_registers(tbreg);
00177         save_choicepoint(tbreg,ereg,(byte *)opfail,breg);
00178 #ifdef SLG_GC
00179         cp_prevtop(tbreg) = old_cptop;
00180 #endif
00181         breg = tbreg;
00182         hbreg = hreg;
00183         unify_with_trie_numcon;
00184         reg_arrayptr--;
00185         proceed_lpcreg;
00186 XSB_End_Instr()
00187 
00188 XSB_Start_Instr(trie_retry_numcon_succ,_trie_retry_numcon_succ) 
00189         CPtr tbreg;
00190         TRIE_R_LOCK();
00191         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_retry_numcon_succ"));
00192         NodePtr = (BTNptr) lpcreg;
00193         tbreg = breg;
00194         restore_regs_and_vars(tbreg, CP_SIZE);
00195         cp_pcreg(breg) = (byte *) opfail;
00196         unify_with_trie_numcon;
00197         reg_arrayptr--;
00198         proceed_lpcreg;
00199 XSB_End_Instr()
00200 
00201 XSB_Start_Instr(trie_trust_numcon_succ,_trie_trust_numcon_succ) 
00202         CPtr tbreg;
00203         TRIE_R_LOCK();
00204         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_trust_numcon_succ"));
00205         NodePtr = (BTNptr) lpcreg;
00206         tbreg = breg;
00207         restore_regs_and_vars(tbreg, CP_SIZE);
00208         breg = cp_prevbreg(breg);
00209         restore_trail_condition_registers(breg);
00210         unify_with_trie_numcon;
00211         reg_arrayptr--;
00212         proceed_lpcreg;
00213 XSB_End_Instr()
00214 
00215 /*----------------------------------------------------------------------*/
00216 
00217 XSB_Start_Instr(trie_no_cp_var,_trie_no_cp_var)
00218         TRIE_R_LOCK();
00219         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_no_cp_var"));
00220         NodePtr = (BTNptr) lpcreg;
00221         num_vars_in_var_regs = DecodeTrieVar(opatom);
00222         xsb_dbgmsg((LOG_TRIE_INSTR, "symbol number is %d\n",num_vars_in_var_regs));
00223         var_regs[num_vars_in_var_regs] = (CPtr) *reg_arrayptr;
00224 #ifdef DEBUG_ASSERTIONS
00225         { int i = num_vars_in_var_regs;
00226           if ((isref(var_regs[i])) &&
00227             ((var_regs[i] < (CPtr)glstack.low) || (var_regs[i] >= hreg)) &&
00228             ((var_regs[i] < top_of_localstk) || (var_regs[i] >= (CPtr) glstack.high))) {
00229             xsb_dbgmsg((LOG_DEBUG, "tc_insts_xsb_i.h (no_cp): var reg assigned bad 0x%p %d 0x%p",
00230                        hreg, i, var_regs[i])); }
00231         } 
00232 #endif
00233         reg_arrayptr--;
00234         next_lpcreg;
00235 XSB_End_Instr()
00236 
00237 XSB_Start_Instr(trie_try_var,_trie_try_var) 
00238         CPtr tbreg;
00239 #ifdef SLG_GC
00240         CPtr old_cptop;
00241 #endif
00242         TRIE_R_LOCK();
00243         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_try_var"));
00244         NodePtr = (BTNptr) lpcreg;
00245         save_find_locx(ereg);
00246         tbreg = top_of_cpstack;
00247         /*      save_find_locx(ereg);*/
00248 #ifdef SLG_GC
00249         old_cptop = tbreg;
00250 #endif
00251         save_trie_registers(tbreg);
00252         save_choicepoint(tbreg,ereg,(byte *)opfail,breg);
00253 #ifdef SLG_GC
00254         cp_prevtop(tbreg) = old_cptop;
00255 #endif
00256         breg = tbreg;
00257         hbreg = hreg;
00258         num_vars_in_var_regs = DecodeTrieVar(opatom);
00259         var_regs[num_vars_in_var_regs] = (CPtr) *reg_arrayptr;
00260 #ifdef DEBUG_ASSERTIONS
00261         { int i = num_vars_in_var_regs;
00262           if ((isref(var_regs[i])) &&
00263             ((var_regs[i] < (CPtr)glstack.low) || (var_regs[i] >= hreg)) &&
00264             ((var_regs[i] < top_of_localstk) || (var_regs[i] >= (CPtr) glstack.high))) {
00265             xsb_dbgmsg((LOG_DEBUG, "tc_insts_xsb_i.h (try): var reg assigned bad 0x%p %d 0x%p",
00266                        hreg, i, var_regs[i]));
00267           }
00268         } 
00269 #endif
00270         reg_arrayptr--;
00271         next_lpcreg;
00272 XSB_End_Instr()
00273 
00274 XSB_Start_Instr(trie_retry_var,_trie_retry_var) 
00275         CPtr tbreg;
00276         TRIE_R_LOCK();
00277         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_retry_var"));
00278         NodePtr = (BTNptr) lpcreg;
00279         tbreg = breg;
00280         restore_regs_and_vars(tbreg, CP_SIZE);
00281         cp_pcreg(breg) = (byte *) opfail;
00282         num_vars_in_var_regs = DecodeTrieVar(opatom);
00283         var_regs[num_vars_in_var_regs] = (CPtr) *reg_arrayptr;
00284 #ifdef DEBUG_ASSERTIONS
00285         { int i = num_vars_in_var_regs;
00286           if ((isref(var_regs[i])) &&
00287             ((var_regs[i] < (CPtr)glstack.low) || (var_regs[i] >= hreg)) &&
00288             ((var_regs[i] < top_of_localstk) || (var_regs[i] >= (CPtr) glstack.high))) {
00289             xsb_dbgmsg((LOG_DEBUG, "tc_insts_xsb_i.h (retry): var reg assigned bad 0x%p %d 0x%p",
00290                        hreg, i, var_regs[i]));
00291           }
00292         } 
00293 #endif
00294         reg_arrayptr--;
00295         next_lpcreg;
00296 XSB_End_Instr()
00297 
00298 XSB_Start_Instr(trie_trust_var,_trie_trust_var)  
00299         CPtr tbreg;
00300         TRIE_R_LOCK();
00301         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_trust_var"));
00302         NodePtr = (BTNptr) lpcreg;
00303         tbreg = breg;
00304         restore_regs_and_vars(tbreg, CP_SIZE);
00305         breg = cp_prevbreg(breg);       /* Remove this CP */
00306         restore_trail_condition_registers(breg);
00307         num_vars_in_var_regs = DecodeTrieVar(opatom);
00308         var_regs[num_vars_in_var_regs] = (CPtr) *reg_arrayptr;
00309 #ifdef DEBUG_ASSERTIONS
00310         { int i = num_vars_in_var_regs;
00311           if ((isref(var_regs[i])) &&
00312             ((var_regs[i] < (CPtr)glstack.low) || (var_regs[i] >= hreg)) &&
00313             ((var_regs[i] < top_of_localstk) || (var_regs[i] >= (CPtr) glstack.high))) {
00314              xsb_dbgmsg((LOG_DEBUG, "tc_insts_xsb_i.h (trust): var reg assigned bad 0x%p %d 0x%p",
00315                         hreg, i, var_regs[i]));
00316           }
00317         } 
00318 #endif
00319         reg_arrayptr--;
00320         next_lpcreg;
00321 XSB_End_Instr()
00322 
00323 /*----------------------------------------------------------------------*/
00324 
00325 /* Documentation on trie_xxx_val instructions (TLS).  Documentation
00326    refers to trie_no_cp_val below, but appllies also to
00327    unify_with_trie_val macro.
00328 
00329 These instructions can be somewhat confusing as they are, to some
00330 extent, overcoded.  They can most easily be explained using reasoning
00331 by cases.  First, note that tries are used in 3 main ways: a) variant
00332 tabling, b) subsumptive tabling, and c) asserted tries, and the
00333 behavior of the val instructions differs somewhat in each case.  
00334 
00335 a) Variant Tabling.  In this case, the routine variant_answer_search()
00336 distinguishes between usage 1) an attv of the call that is unchanged
00337 by the answer; usage 2) an attv of the call that is changed to another
00338 attv in the answer; usage 3) a variable in the call that is bound to
00339 an attv; usage 4) a variable in the call that is bound to another
00340 variable in the call by a binding in the answer.  The routine
00341 variant_answer_search() generates trie_xxx_val instructions only
00342 usages 1 and 4 above.  In usages 2 and 3 a trie_xxx_attv instruction
00343 is generated.  Thus in variant tabling *reg_arrayptr will dereference
00344 only to an attv iff trie_xxx_val dereferences to that same attv (case
00345 b.2 in the code below corresponding to usage 1 ); and reg_arrayptr
00346 will dereference to a ref vanilla variable iff the associated symbol
00347 is a ref (case a corresponding to usage 4)
00348 
00349 b) Subsumptive tabling: need to check
00350 
00351 c) Asserted tries.  For asserted tries, the trie_xxx_val instructions
00352 are generated only for variables (I think) -- but the corresponding
00353 position can be anything you like: a vanilla variable (case a), an
00354 attributed variable (case b), or a constant/structure/number/list
00355 (case c).  Case b.2 will never happen as this depends on the trie
00356 position being an attv, which, as stated, wont happen.
00357 
00358 */
00359 
00360 XSB_Start_Instr(trie_no_cp_val,_trie_no_cp_val)
00361   Def2ops
00362   TRIE_R_LOCK();
00363   xsb_dbgmsg((LOG_TRIE_INSTR, "trie_no_cp_val"));
00364   NodePtr = (BTNptr) lpcreg;
00365 {
00366   Cell cell2deref;                                                      
00367   XSB_Deref(*reg_arrayptr);                                             
00368   if (isref(*reg_arrayptr)) {                            // case a 
00369     cell2deref = (Cell)var_regs[(int)int_val(opatom)];                  
00370     XSB_Deref(cell2deref);                                              
00371     if (cell2deref != *reg_arrayptr)                                    
00372       bind_ref((CPtr) *reg_arrayptr, cell2deref);                       
00373   }                                                                     
00374   else if (isattv(*reg_arrayptr)) {                      // case b              
00375     xsb_dbgmsg((LOG_TRIE_INSTR, "symbol number is %d\n",(int)int_val(opatom)));
00376     cell2deref = (Cell) var_regs[(int)int_val(opatom)];                 
00377     XSB_Deref(cell2deref);                                              
00378     if (*reg_arrayptr != cell2deref) {                  // case b.1             
00379       /* Do not trigger attv interrupt! */                            
00380       
00381       bind_ref(clref_val(*reg_arrayptr), cell2deref);                   
00382     }                                                                   
00383     else {                                             // case b.2              
00384       attv_dbgmsg(">>>> keep old attr in unify_with_trie_val\n");       
00385     }                                                                   
00386   }                                                                     
00387   else {                                              // case c                 
00388     op1 = (Cell)*reg_arrayptr;                                          
00389     op2 = (Cell) var_regs[(int)int_val(opatom)];                        
00390     if (unify(CTXTc op1,op2) == FALSE) {                                        
00391       Fail1;                                                            
00392       XSB_Next_Instr();                                                 
00393     }                                                                   
00394   }                                                                     
00395   reg_arrayptr--;                                                       
00396 }
00397 /*   unify_with_trie_val; */
00398   next_lpcreg;
00399 XSB_End_Instr()
00400 
00401 XSB_Start_Instr(trie_try_val,_trie_try_val) 
00402   Def2ops
00403   CPtr tbreg;
00404 #ifdef SLG_GC
00405         CPtr old_cptop;
00406 #endif
00407   TRIE_R_LOCK();
00408   xsb_dbgmsg((LOG_TRIE_INSTR, "trie_try_val"));
00409   NodePtr = (BTNptr) lpcreg;
00410   save_find_locx(ereg);
00411   tbreg = top_of_cpstack;
00412 #ifdef SLG_GC
00413         old_cptop = tbreg;
00414 #endif
00415   save_trie_registers(tbreg);
00416   save_choicepoint(tbreg,ereg,(byte *)opfail,breg);
00417 #ifdef SLG_GC
00418         cp_prevtop(tbreg) = old_cptop;
00419 #endif
00420   breg = tbreg;
00421   hbreg = hreg;
00422   unify_with_trie_val;
00423   next_lpcreg;
00424 XSB_End_Instr()
00425 
00426 XSB_Start_Instr(trie_retry_val,_trie_retry_val) 
00427   Def2ops
00428   CPtr tbreg;
00429   TRIE_R_LOCK();
00430   xsb_dbgmsg((LOG_TRIE_INSTR, "trie_retry_val"));
00431   NodePtr = (BTNptr) lpcreg;
00432   tbreg = breg;
00433   restore_regs_and_vars(tbreg, CP_SIZE);
00434   cp_pcreg(breg) = (byte *) opfail;
00435   unify_with_trie_val;
00436   next_lpcreg;
00437 XSB_End_Instr()
00438 
00439 XSB_Start_Instr(trie_trust_val,_trie_trust_val) 
00440   Def2ops
00441   CPtr tbreg;
00442   TRIE_R_LOCK();
00443   xsb_dbgmsg((LOG_TRIE_INSTR, "trie_trust_val"));
00444   NodePtr = (BTNptr) lpcreg;
00445   tbreg = breg;
00446   restore_regs_and_vars(tbreg, CP_SIZE);
00447   breg = cp_prevbreg(breg);     /* Remove this CP */
00448   restore_trail_condition_registers(breg);
00449   unify_with_trie_val;
00450   next_lpcreg;
00451 XSB_End_Instr()
00452 
00453 /*----------------------------------------------------------------------*/
00454 
00455 XSB_Start_Instr(trie_no_cp_list,_trie_no_cp_list)
00456         TRIE_R_LOCK();
00457         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_no_cp_list"));
00458         NodePtr = (BTNptr) lpcreg;
00459         unify_with_trie_list;
00460         non_ftag_lpcreg;
00461 XSB_End_Instr()
00462 
00463 XSB_Start_Instr(trie_try_list,_trie_try_list) 
00464         CPtr tbreg;
00465 #ifdef SLG_GC
00466         CPtr old_cptop;
00467 #endif
00468         TRIE_R_LOCK();
00469         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_try_list"));
00470         NodePtr = (BTNptr) lpcreg;
00471         save_find_locx(ereg);
00472         tbreg = top_of_cpstack;
00473 #ifdef SLG_GC
00474         old_cptop = tbreg;
00475 #endif
00476         save_trie_registers(tbreg);
00477         save_choicepoint(tbreg,ereg,(byte *)opfail,breg);
00478 #ifdef SLG_GC
00479         cp_prevtop(tbreg) = old_cptop;
00480 #endif
00481         breg = tbreg;
00482         hbreg = hreg;
00483         unify_with_trie_list;
00484         non_ftag_lpcreg;
00485 XSB_End_Instr()
00486 
00487 XSB_Start_Instr(trie_retry_list,_trie_retry_list) 
00488         CPtr tbreg;
00489         TRIE_R_LOCK();
00490         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_retry_list:"));
00491         NodePtr = (BTNptr) lpcreg;
00492         tbreg = breg;
00493         restore_regs_and_vars(tbreg, CP_SIZE);
00494         cp_pcreg(breg) = (byte *) opfail;
00495         unify_with_trie_list;
00496         non_ftag_lpcreg;
00497 XSB_End_Instr()
00498 
00499 XSB_Start_Instr(trie_trust_list,_trie_trust_list) 
00500         CPtr tbreg;
00501         TRIE_R_LOCK();
00502         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_trust_list"));
00503         NodePtr = (BTNptr) lpcreg;
00504         tbreg = breg;
00505         restore_regs_and_vars(tbreg, CP_SIZE);
00506         breg = cp_prevbreg(breg);       /* Remove this CP */
00507         restore_trail_condition_registers(breg);
00508         unify_with_trie_list;
00509         non_ftag_lpcreg;
00510 XSB_End_Instr()
00511 
00512 /*----------------------------------------------------------------------*/
00513 /* fail insts for deleted nodes - reclaim deleted returns at completion */
00514 /*----------------------------------------------------------------------*/
00515 
00516 XSB_Start_Instr(trie_no_cp_fail,_trie_no_cp_fail)
00517         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_no_cp_fail"));
00518         lpcreg = (byte *) & fail_inst;
00519 XSB_End_Instr()
00520 
00521 XSB_Start_Instr(trie_trust_fail,_trie_trust_fail) 
00522         CPtr tbreg;
00523         TRIE_R_LOCK();
00524         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_trust_fail"));
00525         NodePtr = (BTNptr) lpcreg;
00526         tbreg = breg;
00527         restore_regs_and_vars(tbreg, CP_SIZE);
00528         breg = cp_prevbreg(breg);       /* Remove this CP */
00529         restore_trail_condition_registers(breg);
00530         lpcreg = (byte *) & fail_inst;
00531 XSB_End_Instr()
00532 
00533 XSB_Start_Instr(trie_try_fail,_trie_try_fail) 
00534         CPtr tbreg;
00535 #ifdef SLG_GC
00536         CPtr old_cptop;
00537 #endif
00538         TRIE_R_LOCK();
00539         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_try_fail"));
00540         NodePtr = (BTNptr) lpcreg;
00541         save_find_locx(ereg);
00542         tbreg = top_of_cpstack;
00543 #ifdef SLG_GC
00544         old_cptop = tbreg;
00545 #endif
00546         save_trie_registers(tbreg);
00547         save_choicepoint(tbreg,ereg,(byte *)opfail,breg);
00548 #ifdef SLG_GC
00549         cp_prevtop(tbreg) = old_cptop;
00550 #endif
00551         breg = tbreg;
00552         hbreg = hreg;
00553         lpcreg = (byte *) & fail_inst;
00554 XSB_End_Instr()
00555 
00556 XSB_Start_Instr(trie_retry_fail,_trie_retry_fail) 
00557         CPtr tbreg;
00558         TRIE_R_LOCK();
00559         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_retry_fail"));
00560         NodePtr = (BTNptr) lpcreg;
00561         tbreg = breg;
00562         restore_regs_and_vars(tbreg, CP_SIZE);
00563         cp_pcreg(breg) = (byte *) opfail;
00564         lpcreg = (byte *) & fail_inst;
00565 XSB_End_Instr()
00566 
00567 /*----------------------------------------------------------------------*/
00568 /* The following code, that handles hashing in coded tries, has been    */
00569 /* modified for garbage collection.  Choice points for hashes in tries, */
00570 /* besides the usual trie argument registers (see file tr_code.h), also */
00571 /* contain 3 fields with certain information about the hash bucket.     */
00572 /* The first and third of these fields are predefined integers and are  */
00573 /* now encoded as such.  The second field contains a malloc-ed address  */
00574 /* which is encoded as a STRING (that's how addresses are encoded in    */
00575 /* XSB -- see file cell_xsb.h) to prevent garbage collection from       */
00576 /* treating it as a reference either to a WAM stack or to a CHAT area.  */
00577 /*----------------------------------------------------------------------*/
00578 
00579 /* Structure of the CPF created by hash_opcode:
00580 
00581              +-------------+
00582              |             |   LOW MEMORY
00583              |    Trail    |
00584              |             |
00585              |      |      |
00586              |      V      |
00587              |             |
00588              |             |
00589              |      ^      |
00590              |      |      |
00591              |             |
00592              |  CP Stack   |
00593              |             |
00594              |             |
00595              |=============|
00596              | Rest of CPF |--- Basic CPF (no argument registers)
00597              |-------------|
00598              | HASH index  | - last bucket explored
00599              |  ht header  | - ptr to HashTable Header structure
00600              | HASH_IS flag| - var/nonvar status of topmost term
00601              |-------------|    (the next to be unified with the trie)
00602              |     n+1     |_
00603              |reg_array[n] | \
00604              |      .      |  |
00605              |      .      |  |- Subterms to be unified with trie
00606              |      .      |  |
00607              |reg_array[0] |_/
00608              |-------------|
00609              |      m      |_
00610              | var_regs[m] | \
00611              |      .      |  |
00612              |      .      |  |- Variables encountered so far along trie path
00613              |      .      |  |   (m is -1 if no variables were encountered)
00614              | var_regs[0] |_/
00615              |=============|
00616              |      .      |
00617              |      .      |
00618              |      .      |    HIGH MEMORY
00619              +-------------+
00620 */
00621 
00622 XSB_Start_Instr(hash_opcode,_hash_opcode) 
00623         CPtr tbreg, temp_ptr_for_hash;
00624 #ifdef SLG_GC
00625         CPtr old_cptop;
00626 #endif
00627         BTHTptr hash_header;
00628         BTHTptr *hash_base;
00629         int hashed_hash_offset;
00630 
00631         xsb_dbgmsg((LOG_TRIE_INSTR, "hash_opcode"));
00632    /*
00633     *  Under new trie structure, NodePtr is actually pointing at a
00634     *  Hash Table Header struct.
00635     */
00636     hash_header = (BTHTptr) lpcreg;
00637     hash_base = (BTHTptr *) BTHT_BucketArray(hash_header);
00638 
00639         temp_ptr_for_hash = (CPtr)*reg_arrayptr;
00640         XSB_CptrDeref(temp_ptr_for_hash);
00641         if (!isref(temp_ptr_for_hash) 
00642             && (*hash_base == NULL)){ 
00643             /* Ground call and no variables in hash table */
00644             hash_nonvar_subterm(temp_ptr_for_hash,hash_header,
00645                                 hashed_hash_offset);
00646             if(*(hash_base + hashed_hash_offset) == NULL)
00647               /* fail to previous CPF */
00648               lpcreg = (byte *) cp_pcreg(breg);
00649             else
00650               /* execute code of tries in this bucket */
00651               lpcreg = (byte *) *(hash_base + hashed_hash_offset);
00652         }
00653         else {  
00654           save_find_locx(ereg);
00655           tbreg = top_of_cpstack;
00656 #ifdef SLG_GC
00657           old_cptop = tbreg;
00658 #endif
00659           save_trie_registers(tbreg);
00660           if (isref(temp_ptr_for_hash))
00661             cell(--tbreg) = makeint(HASH_IS_FREE);
00662           else 
00663             cell(--tbreg) = makeint(HASH_IS_NOT_FREE);
00664     /*
00665      *  For normal trie nodes, this next CP value was given as the beginning
00666      *  of the hash table (bucket array).  With the new trie structure, I
00667      *  instead pass in the header, allowing access to all needed fields,
00668      *  including the bucket array.
00669      */
00670           //      printf("makeaddr: tc_insts_xsb_i.h: %p\n",hash_header); //dsw!!!
00671           cell(--tbreg) = makeaddr(hash_header); /* BUT NOT A STRING */
00672           cell(--tbreg) = makeint(FIRST_HASH_NODE);
00673           save_choicepoint(tbreg,ereg,(byte *)&hash_handle_inst,breg);
00674 #ifdef SLG_GC
00675           cp_prevtop(tbreg) = old_cptop;
00676 #endif
00677           breg = tbreg;
00678           hbreg = hreg;
00679           lpcreg = (byte *) &hash_handle_inst;
00680         } 
00681 XSB_End_Instr()
00682 
00683 /*
00684  *  Since this instruction is called immediately after 'hash_opcode' and
00685  *  is also backtracked to while exploring a bucket chain, a mechanism is
00686  *  needed to distinguish between the two cases.  Hence the use of the
00687  *  FIRST_HASH_NODE flag in the CPS.
00688  */
00689 XSB_Start_Instr(hash_handle,_hash_handle)
00690     CPtr    tbreg;
00691     BTHTptr hash_hdr, *hash_base;
00692     int     hash_offset, hashed_hash_offset;
00693 
00694     xsb_dbgmsg((LOG_TRIE_INSTR, "hash_handle"));
00695     hash_offset = int_val(cell(breg+CP_SIZE));
00696     hash_hdr = (BTHTptr) string_val(cell(breg+CP_SIZE+1));
00697     hash_base = (BTHTptr *) BTHT_BucketArray(hash_hdr);
00698 if ( int_val(cell(breg + CP_SIZE + 2)) == HASH_IS_NOT_FREE ) {
00699       /* Unify with nonvar */
00700       if ( (hash_offset != FIRST_HASH_NODE) &&
00701            (hash_offset != NO_MORE_IN_HASH) ) {
00702         tbreg = breg;
00703         restore_regs_and_vars(tbreg, CP_SIZE+3);
00704       }
00705       XSB_Deref(*reg_arrayptr);
00706       if (isref(*reg_arrayptr))   /* sanity check */
00707         xsb_exit("error_condition in hash_handle\n");
00708 
00709       hash_nonvar_subterm(*reg_arrayptr,hash_hdr,hashed_hash_offset);
00710       if (hash_offset == FIRST_HASH_NODE) {
00711         if (*hash_base == NULL) { /* No Variables in hash table */
00712           breg = cp_prevbreg(breg);   /* dealloc this CPF */
00713           if(*(hash_base + hashed_hash_offset) == NULL)
00714             /* fail to previous CPF */
00715             lpcreg = (byte *) cp_pcreg(breg);
00716           else
00717             /* execute code of tries in this bucket */
00718             lpcreg = (byte *) *(hash_base + hashed_hash_offset);
00719         }
00720         else {   /* save hashed-to bucket, explore bucket 0 */
00721           if ( (*(hash_base + hashed_hash_offset) == NULL) ||
00722                (hashed_hash_offset == 0) )
00723             breg = cp_prevbreg(breg);   /* dealloc this CPF */
00724           else
00725             cell(breg + CP_SIZE) = makeint(hashed_hash_offset);
00726           lpcreg = (byte *) *hash_base;
00727         }
00728       }
00729       else if (hash_offset == hashed_hash_offset) {
00730         /* explore hashed-to bucket */
00731         lpcreg = (byte *)*(hash_base + hash_offset);
00732         breg = cp_prevbreg(breg);
00733       }
00734       else {
00735         xsb_error("Hash Offset %d, HHO %d",
00736                   hash_offset, hashed_hash_offset);
00737         xsb_exit("error_condition in hash_handle\n");
00738       }
00739     }
00740     else {  /* unification of trie with variable term */
00741       find_next_nonempty_bucket(hash_hdr,hash_base,hash_offset);
00742       if (hash_offset == NO_MORE_IN_HASH) {
00743         breg = cp_prevbreg(breg);
00744         lpcreg = (byte *) cp_pcreg(breg);
00745       }
00746       else {
00747         if ( int_val(cell(breg+CP_SIZE)) != FIRST_HASH_NODE ) {
00748           tbreg = breg;
00749           restore_regs_and_vars(tbreg, CP_SIZE+3);
00750         }
00751         lpcreg = (byte *) *(hash_base + hash_offset);
00752         cell(breg+CP_SIZE) = makeint(hash_offset);
00753       }
00754     }
00755 XSB_End_Instr()
00756 
00757 /*----------------------------------------------------------------------*/
00758 
00759 XSB_Start_Instr(trie_proceed,_trie_proceed)     /* This is essentially a "proceed" */
00760         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_proceed:"));
00761         NodePtr = (BTNptr) lpcreg;
00762         proceed_lpcreg;
00763 XSB_End_Instr()
00764 
00765 XSB_Start_Instr(trie_root,_trie_root)      /* A no-op; begin processing with child */
00766         TRIE_R_LOCK()
00767         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_root:"));
00768         NodePtr = (BTNptr) lpcreg;
00769         lpcreg = (byte *) BTN_Child(NodePtr);
00770 XSB_End_Instr()
00771 
00772 XSB_Start_Instr(trie_fail_unlock,_trie_fail_unlock)
00773         xsb_dbgmsg((LOG_TRIE_INSTR, "trie_fail_unlock"));
00774         TRIE_R_UNLOCK()
00775         breg = cp_prevbreg(breg);       /* Remove this CP */
00776         restore_trail_condition_registers(breg);
00777         lpcreg = (byte *) & fail_inst;
00778 XSB_End_Instr()
00779 /*
00780  * This is the embedded-trie instruction which is placed in the root of
00781  * asserted tries.  It looks a lot like both "return_table_code", which
00782  * prepares the engine to walk an answer trie, and "get_calls", which
00783  * prepares the engine to walk a call trie.  Maybe there's a way to
00784  * "unify" these operations now that all tries contain root nodes.
00785  */
00786 XSB_Start_Instr(trie_assert_inst,_trie_assert_inst)
00787   Psc psc_ptr;
00788   int i;
00789 #ifdef MULTI_THREAD_RWL
00790   CPtr tbreg;
00791 #ifdef SLG_GC
00792   CPtr old_cptop;
00793 #endif
00794 #endif
00795 
00796   xsb_dbgmsg((LOG_TRIE_INSTR, "trie_assert"));
00797   NodePtr = (BTNptr) lpcreg;
00798   if (Child(NodePtr) != NULL) {
00799     TRIE_R_LOCK()
00800     psc_ptr = DecodeTrieFunctor(BTN_Symbol(NodePtr));
00801     reg_arrayptr = reg_array -1;
00802     num_vars_in_var_regs = -1;
00803     for (i = get_arity(psc_ptr); i >= 1; i--) { pushreg(*(rreg+i)); }
00804     lpcreg = (byte *) Child(NodePtr);
00805 #ifdef MULTI_THREAD_RWL
00806 /* save choice point for trie_unlock instruction */
00807     save_find_locx(ereg);
00808     tbreg = top_of_cpstack;
00809 #ifdef SLG_GC
00810     old_cptop = tbreg;
00811 #endif
00812     save_choicepoint(tbreg,ereg,(byte *)&trie_fail_unlock_inst,breg);
00813 #ifdef SLG_GC
00814     cp_prevtop(tbreg) = old_cptop;
00815 #endif
00816     breg = tbreg;
00817     hbreg = hreg;
00818 #endif
00819   }
00820   else
00821     lpcreg = (byte *) &fail_inst;
00822 XSB_End_Instr()
00823 
00824 XSB_Start_Instr(trie_no_cp_attv,_trie_no_cp_attv)
00825   TRIE_R_LOCK();
00826   xsb_dbgmsg((LOG_TRIE_INSTR, "trie_no_cp_attv"));
00827   NodePtr = (BTNptr) lpcreg;
00828   unify_with_trie_attv;
00829   next_lpcreg
00830 XSB_End_Instr()
00831 
00832 XSB_Start_Instr(trie_try_attv,_trie_try_attv)
00833   CPtr tbreg;
00834 #ifdef SLG_GC
00835         CPtr old_cptop;
00836 #endif
00837   TRIE_R_LOCK();
00838   xsb_dbgmsg((LOG_TRIE_INSTR, "trie_try_attv"));
00839   NodePtr = (BTNptr) lpcreg;
00840   save_find_locx(ereg);
00841   tbreg = top_of_cpstack;
00842 #ifdef SLG_GC
00843         old_cptop = tbreg;
00844 #endif
00845   save_trie_registers(tbreg);
00846   save_choicepoint(tbreg,ereg,(byte *)opfail,breg);
00847 #ifdef SLG_GC
00848         cp_prevtop(tbreg) = old_cptop;
00849 #endif
00850   breg = tbreg;
00851   hbreg = hreg;
00852   unify_with_trie_attv;
00853   next_lpcreg;
00854 XSB_End_Instr()
00855 
00856 XSB_Start_Instr(trie_retry_attv,_trie_retry_attv) 
00857   CPtr tbreg;
00858   TRIE_R_LOCK();
00859   xsb_dbgmsg((LOG_TRIE_INSTR, "trie_retry_attv:"));
00860   NodePtr = (BTNptr) lpcreg;
00861   tbreg = breg;
00862   restore_regs_and_vars(tbreg, CP_SIZE);
00863   cp_pcreg(breg) = (byte *) opfail;
00864   unify_with_trie_attv;
00865   next_lpcreg;
00866 XSB_End_Instr()
00867 
00868 XSB_Start_Instr(trie_trust_attv,_trie_trust_attv) 
00869   CPtr tbreg;
00870   TRIE_R_LOCK();
00871   xsb_dbgmsg((LOG_TRIE_INSTR, "trie_trust_attv"));
00872   NodePtr = (BTNptr) lpcreg;
00873   tbreg = breg;
00874   restore_regs_and_vars(tbreg, CP_SIZE);
00875   breg = cp_prevbreg(breg);     /* Remove this CP */
00876   restore_trail_condition_registers(breg);
00877   unify_with_trie_attv;
00878   next_lpcreg;
00879 XSB_End_Instr()
00880 
00881 /*----------------------------------------------------------------------*/

Generated on Wed Jul 26 13:30:42 2006 for XSB by  doxygen 1.4.5