binding.h

00001                                 /* File:      binding.h
00002 ** Author(s): Jiyang Xu, Terrance Swift, Kostis Sagonas, Baoqiu Cui
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
00006 ** Copyright (C) ECRC, Germany, 1990
00007 ** 
00008 ** XSB is free software; you can redistribute it and/or modify it under the
00009 ** terms of the GNU Library General Public License as published by the Free
00010 ** Software Foundation; either version 2 of the License, or (at your option)
00011 ** any later version.
00012 ** 
00013 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00014 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00015 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00016 ** more details.
00017 ** 
00018 ** You should have received a copy of the GNU Library General Public License
00019 ** along with XSB; if not, write to the Free Software Foundation,
00020 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00021 **
00022 ** $Id: binding.h,v 1.19 2005/07/22 15:42:12 crojo Exp $
00023 ** 
00024 */
00025 
00026 /*
00027  * Structures Of Different Trail Frames:
00028  *
00029  * In SLG-WAM, trreg and trfreg always point to the trail_parent field of
00030  * a trail frame, and the trail stack may contain both 3-word trail frames 
00031  * and 4-word ones.  After untrailing a trail frame, trreg can be reset by 
00032  * following the trail_parent pointer.
00033  *
00034  * 1) Regular (forward) trail frame in SLG-WAM (3-word trail frame)
00035  * ----------------------------------------------------------------
00036  *    low memory
00037  *     +-----+
00038  *     |     | trail_variable : Address of the trailed variable
00039  *     +-----+
00040  *     |     | trail_value    : New value to which the trailed variable is
00041  *     +-----+                  to be bound
00042  *     |     | trail_parent   : Pointer to the trail_parent cell of the
00043  *     +-----+                  previous trail frame)
00044  *    high memory
00045  *
00046  * 2) Pre-image trail frame in SLG-WAM (4-word trail frame)
00047  * --------------------------------------------------------
00048  *    low memory
00049  *     +-----+
00050  *     |     | trail_pre_image: Old value of the trailed variable (cell)
00051  *     +---+-+
00052  *     |   |1| trail_variable : Address of the trailed variable
00053  *     +---+-+                  (with PRE_IMAGE_MARK)
00054  *     |     | trail_value    : New value to which the trailed variable is
00055  *     +-----+                  to be bound
00056  *     |     | trail_parent   : Pointer to the trail_parent cell of the
00057  *     +-----+                  previous trail frame)
00058  *    high memory
00059  */
00060 
00061 #include "xsb_config.h"
00062 #include "basictypes.h"
00063 #include "box_defines.h"
00064 
00065 
00066 #define PRE_IMAGE_TRAIL
00067 
00068 #define PRE_IMAGE_MARK   1
00069 
00070 #define TRAIL_FRAME_SIZE  4
00071 
00072 #define pushtrail0(addr,val)  \
00073    if (trfreg > trreg) {\
00074      if ((char *)trfreg > \
00075          ((char *)(top_of_cpstack) - (TRAIL_FRAME_SIZE*sizeof(CPtr)))) {\
00076        handle_tcpstack_overflow(CTXT);\
00077      }\
00078      *(trfreg+3) = (CPtr) trreg;\
00079      trreg = trfreg + 3;\
00080      *(trreg-1) = (CPtr) val;\
00081      *(trreg-2) = addr;\
00082    }\
00083    else {\
00084      if ((char *)trreg > \
00085          ((char *)(top_of_cpstack) - (TRAIL_FRAME_SIZE*sizeof(CPtr)))) {\
00086        handle_tcpstack_overflow(CTXT);\
00087      }\
00088      trreg = trreg+3;\
00089      *trreg = (CPtr) trreg-3;\
00090      *(trreg-1) = (CPtr) val;\
00091      *(trreg-2) = addr;\
00092    }
00093 
00094 #ifdef PRE_IMAGE_TRAIL
00095 #define push_pre_image_trail0(addr, new_value)                  \
00096   if (trfreg > trreg) {                                         \
00097     if ((char *)trfreg > ((char *)(top_of_cpstack) -            \
00098                           TRAIL_FRAME_SIZE*sizeof(CPtr))) {     \
00099       handle_tcpstack_overflow(CTXT);                           \
00100     }                                                           \
00101     *(trfreg + 4) = (CPtr) trreg;                               \
00102     trreg = trfreg + 4;                                         \
00103     *(trreg - 1) = (CPtr) (new_value);                          \
00104     *(trreg - 2) = (CPtr) ((Cell) (addr) | PRE_IMAGE_MARK);     \
00105     *(trreg - 3) = (CPtr) (cell(addr));                         \
00106   }                                                             \
00107   else {                                                        \
00108     if ((char *)trreg > ((char *)(top_of_cpstack) -             \
00109                           TRAIL_FRAME_SIZE*sizeof(CPtr))) {     \
00110       handle_tcpstack_overflow(CTXT);                           \
00111     }                                                           \
00112     trreg = trreg + 4;                                          \
00113     *trreg = (CPtr) trreg - 4;                                  \
00114     *(trreg - 1) = (CPtr) (new_value);                          \
00115     *(trreg - 2) = (CPtr) ((Cell) (addr) | PRE_IMAGE_MARK);     \
00116     *(trreg - 3) = (CPtr) (cell(addr));                         \
00117   }
00118 #endif /* PRE_IMAGE_TRAIL */
00119 
00120 #define conditional(a)  ( ((a) >= ebreg || (a) >= efreg) || \
00121                           ((a) < hbreg  || (a) < hfreg) )
00122 
00123 #define pushtrail(a,v)  if (conditional(a)) { pushtrail0(a,v); }
00124 #define dpushtrail(a,v) pushtrail0(a,v)
00125 
00126 #ifdef PRE_IMAGE_TRAIL
00127 #define push_pre_image_trail(a, new_v)                  \
00128   if (conditional(a)) {push_pre_image_trail0(a, new_v)}
00129 #endif /* PRE_IMAGE_TRAIL */
00130 
00131 /* --- binding -------------------------------------------------------- */
00132 
00133 #define bind_int_tagged(addr, val)      pushtrail(addr, (Cell) val); \
00134                                         bld_int_tagged(addr, val)
00135 
00136 #define bind_float_tagged(addr, val) pushtrail(addr, (Cell) val);\
00137                         bld_float_tagged(addr, val)
00138                         
00139 #define bind_int(addr, val)     pushtrail(addr, makeint(val));\
00140                                 bld_int(addr, val)
00141 
00142 #define bind_float(addr, val) pushtrail(addr, makefloat(val)); bld_float(addr, val)
00143 //below is old boxedint code. Retained in case new boxedint is faulty
00144 /*#define bind_boxedint(addr, val)                               \
00145      {Cell temp = makecs(hreg);                                  \
00146       new_heap_functor(hreg,box_psc);                            \
00147       bld_int(hreg,1); hreg++;                                   \
00148       bld_int(hreg,(((unsigned long)(val)) >> 24)); hreg++;      \
00149       bld_int(hreg,((val) & 0xffffff)); hreg++;                  \
00150       pushtrail(addr, temp);                                     \
00151       cell(addr) = temp;} */
00152 
00153 #define bind_boxedint(addr, value)                                                  \
00154      {Cell binttemp = makecs(hreg);         \
00155       new_heap_functor(hreg,box_psc);                                           \
00156       bld_int(hreg,((ID_BOXED_INT << BOX_ID_OFFSET ) )); hreg++;        \
00157       bld_int(hreg,INT_LOW_24_BITS(value)); hreg++;                     \
00158       bld_int(hreg,((value) & LOW_24_BITS_MASK)); hreg++;                   \
00159       pushtrail(addr, binttemp);                                            \
00160       cell(addr) = binttemp;}
00161 
00162 
00163 #ifndef FAST_FLOATS
00164 //Below, bind_boxedfloat builds a boxedfloat, from Float 'value', on the heap.
00165 //It also creates a Cell pointing to it, and inserts it at addr. 
00166 //Pre-condition: value must be a 64-bit floating point value
00167 /*#define bind_boxedfloat(addr, value)                                      \
00168      {Float tempFloat = value;                          \
00169       Cell bfloattemp = makecs(hreg);                                   \
00170       new_heap_functor(hreg,box_psc);                                   \
00171       bld_int(hreg,(ID_BOXED_FLOAT << BOX_ID_OFFSET ) | FLOAT_HIGH_16_BITS(tempFloat) ); \
00172       hreg++;                                               \
00173       bld_int(hreg,FLOAT_MIDDLE_24_BITS(tempFloat)); hreg++;    \
00174       bld_int(hreg,FLOAT_LOW_24_BITS(tempFloat)); hreg++;               \
00175       pushtrail(addr, bfloattemp);                                          \
00176       cell(addr) = bfloattemp;}
00177       
00178       */
00179       
00180 #define bind_boxedfloat(addr, value)   \
00181     {   pushtrail(addr, (Cell)makecs(hreg)); \
00182         bld_boxedfloat(CTXTc addr, value); }                                   
00183 #else
00184 #define bind_boxedfloat(addr, value) bind_float(addr, value)
00185 #endif
00186 
00187 
00188 #define bind_oint(addr, val)                \
00189      if (int_overflow(val)) {               \
00190         bind_boxedint(addr, val);                               \
00191       } else {bind_int(addr, val);}
00192 
00193 
00194 #define bind_ref(addr, val)     pushtrail(addr, val);\
00195                                 bld_ref(addr, val)
00196 
00197 #define dbind_ref(addr, val)    dpushtrail(addr, val);\
00198                                 bld_ref(addr, val)
00199 
00200 #define bind_cs(addr, str)      pushtrail(addr, makecs(str));\
00201                                 bld_cs(addr, str)
00202 
00203 #define bind_string(addr, str)  pushtrail(addr, makestring(str));\
00204                                 bld_string(addr,str)
00205 
00206 #define bind_nil(addr)          pushtrail(addr, makenil);\
00207                                 bld_nil(addr)
00208 
00209 #define bind_list(addr, list)   pushtrail(addr, makelist(list));\
00210                                 bld_list(addr, list)
00211 
00212 #define bind_attv(addr, attv)   pushtrail(addr, makeattv(attv));\
00213                                 bld_attv(addr, attv)
00214 
00215 #define bind_copy(addr, val)    pushtrail(addr, val); *(addr) = val
00216 
00217 /* value trail MUST be used because first CP cell has cp_trreg = 0 !!!! */
00218 
00219 #define untrail(addr) bld_free(addr)
00220 
00221 #ifdef PRE_IMAGE_TRAIL  /* untrail2 is for pre_image trail. */
00222 
00223 #define untrail2(trail_ptr, addr)               \
00224   if ((addr) & PRE_IMAGE_MARK) {                \
00225     bld_copy0((CPtr)((addr) - PRE_IMAGE_MARK),  \
00226               cell((CPtr)trail_ptr - 3));       \
00227   }                                             \
00228   else                                          \
00229     bld_free((CPtr)(addr))
00230 
00231 #endif /* PRE_IMAGE_TRAIL */
00232 
00233 
00234 /* --- testing location of variable ----------------------------------- */
00235 
00236 #define ereg_on_top(t_ereg)     t_ereg < ebreg
00237 
00238 #define efreg_on_top(t_ereg) efreg < ebreg  && efreg < t_ereg
00239 
00240 /* --- for building vals on the heap ---------------------------------- */
00241 
00242 #define nbldval(OP1) {                                                    \
00243    XSB_Deref(OP1);                                                                \
00244    if ( isnonvar(OP1) ||                                                  \
00245         ( /* (CPtr)(OP1) >= glstack.low && */                             \
00246           (CPtr)(OP1) <= top_of_heap ) ) {                                \
00247      new_heap_node(hreg, OP1);                                            \
00248    }                                                                      \
00249    else {  /* local stack vars point to heap vars and not vice-versa! */  \
00250      bind_ref((CPtr)(OP1), hreg);                                         \
00251      new_heap_free(hreg);                                                 \
00252    }                                                                      \
00253 }

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