tst_aux.h

00001 /* File:      tst_aux.h
00002 ** Author(s): Ernie Johnson
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: tst_aux.h,v 1.9 2005/11/18 21:09:37 tswift Exp $
00022 ** 
00023 */
00024 
00025 
00026 #ifndef PRIVATE_TST_DEFS
00027 
00028 #define PRIVATE_TST_DEFS
00029 
00030 #include "debugs/debug_tables.h"
00031 #include "debugs/debug_tries.h"
00032 #include "dynamic_stack.h"
00033 
00034 
00035 /*===========================================================================*/
00036 
00037 /*
00038  *                     Auxiliary Data Structures
00039  *                     =========================
00040  *
00041  *  This file contains typedefs of and macros for using some objects
00042  *  universally needed by the subsumptive components of the engine,
00043  *  i.e. the operations:
00044  *
00045  *  i) Subsumptive Call Check/Insert
00046  *  ii) Variant TST Answer Check/Insert
00047  *  iii) Relevant Answer Identification
00048  *  iv) Answer Consumption
00049  */
00050 
00051 /*---------------------------------------------------------------------------*/
00052 
00053 /*
00054  *  tstTermStack
00055  *  ------------
00056  *  For flattening a heap term during processing.
00057  */
00058 
00059 #ifndef MULTI_THREAD
00060 extern DynamicStack tstTermStack;
00061 #endif
00062 #define TST_TERMSTACK_INITSIZE    25
00063 
00064 #define TermStack_Top           ((CPtr)DynStk_Top(tstTermStack))
00065 #define TermStack_Base          ((CPtr)DynStk_Base(tstTermStack))
00066 #define TermStack_NumTerms      DynStk_NumFrames(tstTermStack)
00067 #define TermStack_ResetTOS      DynStk_ResetTOS(tstTermStack)
00068 #define TermStack_IsEmpty       DynStk_IsEmpty(tstTermStack)
00069 
00070 #define TermStack_SetTOS(Index)                 \
00071    DynStk_Top(tstTermStack) = TermStack_Base + Index
00072 
00073 #define TermStack_Push(Term) {                  \
00074    CPtr nextFrame;                              \
00075    DynStk_Push(tstTermStack,nextFrame);         \
00076    *nextFrame = Term;                           \
00077  }
00078 
00079 #define TermStack_BlindPush(Term) {             \
00080    CPtr nextFrame;                              \
00081    DynStk_BlindPush(tstTermStack,nextFrame);    \
00082    *nextFrame = Term;                           \
00083  }
00084 
00085 #define TermStack_Pop(Term) {                   \
00086    CPtr curFrame;                               \
00087    DynStk_BlindPop(tstTermStack,curFrame);      \
00088    Term = *curFrame;                            \
00089  }
00090 
00091 #define TermStack_Peek(Term) {                  \
00092    CPtr curFrame;                               \
00093    DynStk_BlindPeek(tstTermStack,curFrame);     \
00094    Term = *curFrame;                            \
00095  }
00096 
00097 /* Specialty Pushing Macros
00098    ------------------------ */
00099 #define TermStack_NOOP      /* Nothing to push when constants match */
00100 
00101 #define TermStack_PushFunctorArgs(CS_Cell)                   \
00102    TermStack_PushLowToHighVector( clref_val(CS_Cell) + 1,    \
00103                                   get_arity((Psc)*clref_val(CS_Cell)) )
00104 
00105 #define TermStack_PushListArgs(LIST_Cell) {     \
00106    CPtr pListHeadCell = clref_val(LIST_Cell);   \
00107    DynStk_ExpandIfOverflow(tstTermStack,2);     \
00108    TermStack_BlindPush( *(pListHeadCell + 1) ); \
00109    TermStack_BlindPush( *(pListHeadCell) );     \
00110  }
00111 
00112 /*
00113  * The following macros enable the movement of an argument vector to
00114  * the TermStack.  Two versions are supplied depending on whether the
00115  * vector is arranged from high-to-low memory, such as an answer
00116  * template, or from low-to-high memory, such as the arguments of a
00117  * compound heap term.  The vector pointer is assumed to reference the
00118  * first element of the vector.
00119  */
00120 
00121 #define TermStack_PushLowToHighVector(pVectorLow,Magnitude) {   \
00122    int i, numElements;                                          \
00123    CPtr pElement;                                               \
00124                                                                 \
00125    numElements = Magnitude;                                     \
00126    pElement = pVectorLow + numElements;                         \
00127    DynStk_ExpandIfOverflow(tstTermStack,numElements);           \
00128    for (i = 0; i < numElements; i++) {                          \
00129      pElement--;                                                \
00130      TermStack_BlindPush(*pElement);                            \
00131    }                                                            \
00132  }
00133    
00134 #define TermStack_PushHighToLowVector(pVectorHigh,Magnitude) {  \
00135    int i, numElements;                                          \
00136    CPtr pElement;                                               \
00137                                                                 \
00138    numElements = Magnitude;                                     \
00139    pElement = pVectorHigh - numElements;                        \
00140    DynStk_ExpandIfOverflow(tstTermStack,numElements);           \
00141    for (i = 0; i < numElements; i++) {                          \
00142      pElement++;                                                \
00143      TermStack_BlindPush(*pElement);                            \
00144    }                                                            \
00145  }
00146 
00147 /*
00148  * This macro copies an array of terms onto the TermStack, checking for
00149  * overflow only once at the beginning, rather than with each push.  The
00150  * elements to be pushed are assumed to exist in array elements
00151  * 0..(NumElements-1).
00152  */
00153 
00154 #define TermStack_PushArray(Array,NumElements) {        \
00155    counter i;                                           \
00156    DynStk_ExpandIfOverflow(tstTermStack,NumElements);   \
00157    for ( i = 0;  i < NumElements;  i++ )                \
00158      TermStack_BlindPush(Array[i]);                     \
00159  }
00160 
00161 /* ------------------------------------------------------------------------- */
00162 
00163 /*
00164  *  tstTermStackLog
00165  *  ---------------
00166  *  For noting the changes made to the tstTermStack during processing
00167  *  where backtracking is required.  Only the changes necessary to
00168  *  transform the tstTermStack from its current state to a prior state
00169  *  are logged.  Therefore, we only need record values popped from the
00170  *  tstTermStack.
00171  *
00172  *  Each frame of the log consists of the index of a tstTermStack
00173  *  frame and the value that was stored there.  tstTermStack values
00174  *  are reinstated as a side effect of a tstTermStackLog_Pop.
00175  */
00176 
00177 typedef struct {
00178   int index;             /* location within the tstTermStack... */
00179   Cell value;            /* where this value appeared. */
00180 } tstLogFrame;
00181 typedef tstLogFrame *pLogFrame;
00182 
00183 #define LogFrame_Index(Frame)   ( (Frame)->index )
00184 #define LogFrame_Value(Frame)   ( (Frame)->value )
00185 
00186 
00187 #ifndef MULTI_THREAD
00188 extern DynamicStack tstTermStackLog;
00189 #endif
00190 #define TST_TERMSTACKLOG_INITSIZE    20
00191 
00192 #define TermStackLog_Top        ((pLogFrame)DynStk_Top(tstTermStackLog))
00193 #define TermStackLog_Base       ((pLogFrame)DynStk_Base(tstTermStackLog))
00194 #define TermStackLog_ResetTOS   DynStk_ResetTOS(tstTermStackLog)
00195 
00196 /*
00197  * This doesn't always immediately follow a pop of the TermStack (see
00198  * tst_retrv.c).  Therefore the subterm is obtained from the TermStack
00199  * directly before more terms are pushed onto it.
00200  */
00201 #define TermStackLog_PushFrame {                                \
00202    pLogFrame nextFrame;                                         \
00203    DynStk_Push(tstTermStackLog,nextFrame);                      \
00204    LogFrame_Index(nextFrame) = TermStack_Top - TermStack_Base;  \
00205    LogFrame_Value(nextFrame) = *(TermStack_Top);                \
00206  }
00207 
00208 #define TermStackLog_PopAndReset {                                      \
00209    pLogFrame curFrame;                                                  \
00210    DynStk_BlindPop(tstTermStackLog,curFrame)                            \
00211    TermStack_Base[LogFrame_Index(curFrame)] = LogFrame_Value(curFrame); \
00212  }
00213 
00214 /*
00215  * Reset the TermStack elements down to and including the Index-th
00216  * entry in the Log.
00217  */
00218 #define TermStackLog_Unwind(Index) {                    \
00219    pLogFrame unwindBase = TermStackLog_Base + Index;    \
00220    while ( TermStackLog_Top > unwindBase )              \
00221      TermStackLog_PopAndReset;                          \
00222  }
00223 
00224 /* ------------------------------------------------------------------------- */
00225 
00226 /*
00227  *  tstSymbolStack
00228  *  ---------------
00229  *  For constructing terms from the symbols stored along a path in the trie.
00230  */
00231 
00232 #ifndef MULTI_THREAD
00233 extern DynamicStack tstSymbolStack;
00234 #endif
00235 #define TST_SYMBOLSTACK_INITSIZE   25
00236 
00237 #define SymbolStack_Top           ((CPtr)DynStk_Top(tstSymbolStack))
00238 #define SymbolStack_Base          ((CPtr)DynStk_Base(tstSymbolStack))
00239 #define SymbolStack_NumSymbols    (SymbolStack_Top - SymbolStack_Base)
00240 #define SymbolStack_ResetTOS      DynStk_ResetTOS(tstSymbolStack)
00241 #define SymbolStack_IsEmpty       DynStk_IsEmpty(tstSymbolStack)
00242 
00243 #define SymbolStack_Push(Symbol) {              \
00244    CPtr nextFrame;                              \
00245    DynStk_Push(tstSymbolStack,nextFrame);       \
00246    *nextFrame = Symbol;                         \
00247  }
00248 
00249 #define SymbolStack_Pop(Symbol) {               \
00250    CPtr curFrame;                               \
00251    DynStk_BlindPop(tstSymbolStack,curFrame);    \
00252    Symbol = *curFrame;                          \
00253 }
00254 
00255 #define SymbolStack_Peek(Symbol) {              \
00256    CPtr curFrame;                               \
00257    DynStk_BlindPeek(tstSymbolStack,curFrame);   \
00258    Symbol = *curFrame;                          \
00259 }
00260 
00261 #define SymbolStack_PushPathRoot(Leaf,Root) {   \
00262    BTNptr btn = (BTNptr)Leaf;                   \
00263    while ( ! IsTrieRoot(btn) ) {                \
00264      SymbolStack_Push(BTN_Symbol(btn));         \
00265      btn = BTN_Parent(btn);                     \
00266    }                                            \
00267    Root = (void *)btn;                          \
00268  }
00269 
00270 #define SymbolStack_PushPath(Leaf) {            \
00271    BTNptr root;                                 \
00272    SymbolStack_PushPathRoot(Leaf,root);         \
00273  }
00274 
00275 /* ------------------------------------------------------------------------- */
00276 
00277 /*
00278  *  tstTrail
00279  *  ---------
00280  *  For recording bindings made during processing.  This Trail performs
00281  *  simple WAM trailing -- it saves address locations only.
00282  */
00283 
00284 #ifndef MULTI_THREAD
00285 extern DynamicStack tstTrail;
00286 #endif
00287 #define TST_TRAIL_INITSIZE    20
00288 
00289 #define Trail_Top               ((CPtr *)DynStk_Top(tstTrail))
00290 #define Trail_Base              ((CPtr *)DynStk_Base(tstTrail))
00291 #define Trail_NumBindings       DynStk_NumFrames(tstTrail)
00292 #define Trail_ResetTOS          DynStk_ResetTOS(tstTrail)
00293 
00294 #define Trail_Push(Addr) {                      \
00295    CPtr *nextFrame;                             \
00296    DynStk_Push(tstTrail,nextFrame);             \
00297    *nextFrame = (CPtr)(Addr);                   \
00298  }
00299 
00300 #define Trail_PopAndReset {                     \
00301    CPtr *curFrame;                              \
00302    DynStk_BlindPop(tstTrail,curFrame);          \
00303    bld_free(*curFrame);                         \
00304  }
00305 
00306 #define Trail_Unwind_All        Trail_Unwind(0)
00307 
00308 /*
00309  * Untrail down to and including the Index-th element.
00310  */
00311 #define Trail_Unwind(Index) {                   \
00312    CPtr *unwindBase = Trail_Base + Index;       \
00313    while ( Trail_Top > unwindBase )             \
00314      Trail_PopAndReset;                         \
00315  }
00316 
00317 /*=========================================================================*/
00318 
00319 /*
00320  *                      Using the Trie Stacks
00321  *                      =====================
00322  */
00323 
00324 #define ProcessNextSubtermFromTrieStacks(Symbol,StdVarNum) {    \
00325                                                                 \
00326    Cell subterm;                                                \
00327                                                                 \
00328    TermStack_Pop(subterm);                                      \
00329    XSB_Deref(subterm);                                          \
00330    switch ( cell_tag(subterm) ) {                               \
00331    case XSB_REF:                                                \
00332    case XSB_REF1:                                               \
00333      if ( ! IsStandardizedVariable(subterm) ) {                 \
00334        StandardizeVariable(subterm, StdVarNum);                 \
00335        Trail_Push(subterm);                                     \
00336        Symbol = EncodeNewTrieVar(StdVarNum);                    \
00337        StdVarNum++;                                             \
00338      }                                                          \
00339      else                                                       \
00340        Symbol = EncodeTrieVar(IndexOfStdVar(subterm));          \
00341      break;                                                     \
00342    case XSB_STRING:                                             \
00343    case XSB_INT:                                                \
00344    case XSB_FLOAT:                                              \
00345      Symbol = EncodeTrieConstant(subterm);                      \
00346      break;                                                     \
00347    case XSB_STRUCT:                                             \
00348      Symbol = EncodeTrieFunctor(subterm);                       \
00349      TermStack_PushFunctorArgs(subterm);                        \
00350      break;                                                     \
00351    case XSB_LIST:                                               \
00352      Symbol = EncodeTrieList(subterm);                          \
00353      TermStack_PushListArgs(subterm);                           \
00354      break;                                                     \
00355    case XSB_ATTV:                                               \
00356      xsb_table_error(CTXTc                                      \
00357         "Attributed variables not yet implemented in answers for subsumptive tables."); \
00358       break;                                                    \
00359    default:                                                     \
00360      Symbol = 0;  /* avoid "uninitialized" compiler warning */  \
00361      TrieError_UnknownSubtermTag(subterm);                      \
00362    }                                                            \
00363  }
00364 
00365 /*=========================================================================*/
00366 
00367 
00368 #endif

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