new slot implementation

This commit is contained in:
Vítor Santos Costa 2015-02-03 02:36:51 +00:00
parent bf215e68a2
commit 5a668febd9
12 changed files with 143 additions and 122 deletions

View File

@ -1230,14 +1230,14 @@ X_API Term *
YAP_AddressFromSlot(Int slot)
{
CACHE_REGS
return Yap_AddressFromSlot(slot PASS_REGS);
return Yap_AddressFromSlot(slot);
}
X_API Term *
YAP_AddressOfTermInSlot(Int slot)
{
CACHE_REGS
Term *b = Yap_AddressFromSlot(slot PASS_REGS);
Term *b = Yap_AddressFromSlot(slot);
Term a = *b;
restart:
if (!IsVarTerm(a)) {
@ -1512,7 +1512,7 @@ YAP_Execute(PredEntry *pe, CPredicate exec_code)
}
PP = NULL;
// check for junk: open frames, etc */
Yap_CloseSlots(CurSlot PASS_REGS);
Yap_CloseSlots(CurSlot);
if (ret)
complete_exit(((choiceptr)(LCL0-OASP)), FALSE, FALSE PASS_REGS);
else
@ -1542,7 +1542,7 @@ YAP_ExecuteFirst(PredEntry *pe, CPredicate exec_code)
CACHE_REGS
CELL ocp = LCL0-(CELL *)B;
/* for slots to work */
Int CurSlot = Yap_StartSlots( PASS_REGS1 );
Int CurSlot = Yap_StartSlots( );
if (pe->PredFlags & (SWIEnvPredFlag|CArgsPredFlag|ModuleTransparentPredFlag)) {
uintptr_t val;
CPredicateV codev = (CPredicateV)exec_code;
@ -1557,7 +1557,7 @@ YAP_ExecuteFirst(PredEntry *pe, CPredicate exec_code)
} else {
val = codev(Yap_InitSlots(pe->ArityOfPE, &ARG1),0,ctx);
}
Yap_CloseSlots(CurSlot PASS_REGS);
Yap_CloseSlots(CurSlot);
PP = NULL;
if (val == 0) {
Term t;
@ -1583,7 +1583,7 @@ YAP_ExecuteFirst(PredEntry *pe, CPredicate exec_code)
}
} else {
Int ret = (exec_code)( PASS_REGS1 );
Yap_CloseSlots(CurSlot PASS_REGS);
Yap_CloseSlots(CurSlot);
if (!ret) {
Term t;
@ -1622,7 +1622,7 @@ YAP_ExecuteOnCut(PredEntry *pe, CPredicate exec_code, struct cut_c_str *top)
} else {
val = codev(Yap_InitSlots(pe->ArityOfPE, args),0,ctx);
}
Yap_CloseSlots(CurSlot PASS_REGS);
Yap_CloseSlots(CurSlot);
PP = NULL;
// B = LCL0-(CELL*)oB;
@ -1644,9 +1644,9 @@ YAP_ExecuteOnCut(PredEntry *pe, CPredicate exec_code, struct cut_c_str *top)
Int ret, CurSlot;
B = oB;
/* for slots to work */
CurSlot = Yap_StartSlots( PASS_REGS1 );
CurSlot = Yap_StartSlots( );
ret = (exec_code)( PASS_REGS1 );
Yap_CloseSlots(CurSlot PASS_REGS);
Yap_CloseSlots(CurSlot);
if (!ret) {
Term t;
@ -1667,7 +1667,7 @@ YAP_ExecuteNext(PredEntry *pe, CPredicate exec_code)
{
CACHE_REGS
/* for slots to work */
Yap_StartSlots( PASS_REGS1 );
Yap_StartSlots( );
UInt ocp = LCL0-(CELL *)B;
yhandle_t CurSlot = Yap_StartSlots();
if (pe->PredFlags & (SWIEnvPredFlag|CArgsPredFlag)) {
@ -1682,7 +1682,7 @@ YAP_ExecuteNext(PredEntry *pe, CPredicate exec_code)
} else {
val = codev(Yap_InitSlots(pe->ArityOfPE, &ARG1),0,ctx);
}
Yap_CloseSlots(CurSlot PASS_REGS);
Yap_CloseSlots(CurSlot);
/* we are below the original choice point ?? */
/* make sure we clean up the frames left by the user */
PP = NULL;
@ -1711,7 +1711,7 @@ YAP_ExecuteNext(PredEntry *pe, CPredicate exec_code)
return complete_exit(((choiceptr)(LCL0-ocp)), FALSE, FALSE PASS_REGS);
} else {
Int ret = (exec_code)( PASS_REGS1 );
Yap_CloseSlots(CurSlot PASS_REGS);
Yap_CloseSlots(CurSlot);
if (!ret) {
Term t;
@ -2214,7 +2214,7 @@ YAP_EnterGoal(PredEntry *pe, Term *ptr, YAP_dogoalinfo *dgi)
RECOVER_MACHINE_REGS();
if (out) {
dgi->EndSlot = LOCAL_CurSlot;
Yap_StartSlots( PASS_REGS1 );
Yap_StartSlots( );
} else {
LOCAL_CurSlot = dgi->CurSlot; // ignore any slots created within the called goal
}
@ -2243,7 +2243,7 @@ YAP_RetryGoal(YAP_dogoalinfo *dgi)
RECOVER_MACHINE_REGS();
if (out) {
LOCAL_CurSlot = dgi->CurSlot;
Yap_StartSlots( PASS_REGS1 );
Yap_StartSlots( );
}
return out;
}
@ -2324,7 +2324,7 @@ YAP_RunGoal(Term t)
CP = old_CP;
LOCAL_AllowRestart = TRUE;
// we are back to user code again, need slots */
Yap_StartSlots( PASS_REGS1 );
Yap_StartSlots( );
} else {
ENV = B->cp_env;
ENV = (CELL *)ENV[E_E];
@ -2784,7 +2784,7 @@ static char BootFile[] = "boot.yap";
/* do initial boot by consulting the file boot.yap */
static void
do_bootfile (char *bootfilename)
do_bootfile (char *bootfilename USES_REGS)
{
Term t;
Term term_end_of_file = MkAtomTerm(AtomEof);
@ -2807,9 +2807,9 @@ do_bootfile (char *bootfilename)
while (!eof_found)
{
CACHE_REGS
Yap_StartSlots( PASS_REGS1 );
Yap_StartSlots( );
t = YAP_Read(bootfile);
Yap_CloseSlots(CurSlot PASS_REGS);
Yap_CloseSlots(CurSlot);
if (eof_found) {
break;
}
@ -3076,7 +3076,7 @@ YAP_Init(YAP_init_args *yap_init)
construct_init_file(boot_file, BootFile);
yap_init->YapPrologBootFile = boot_file;
}
do_bootfile (yap_init->YapPrologBootFile ? yap_init->YapPrologBootFile : BootFile);
do_bootfile (yap_init->YapPrologBootFile ? yap_init->YapPrologBootFile : BootFile PASS_REGS);
/* initialise the top-level */
if (!do_bootstrap) {
char init_file[256];
@ -3201,7 +3201,7 @@ YAP_Reset(yap_reset_t mode)
/* always have an empty slots for people to use */
P = CP = YESCODE;
// ensure that we have slots where we need them
Yap_CloseSlots(1 PASS_REGS);
Yap_CloseSlots(1);
RECOVER_MACHINE_REGS();
return res;
}
@ -3802,7 +3802,7 @@ X_API void
YAP_SlotsToArgs(int n, yhandle_t slot)
{
CACHE_REGS
CELL *ptr0 = Yap_AddressFromSlot(slot PASS_REGS), *ptr1=&ARG1;
CELL *ptr0 = Yap_AddressFromSlot(slot), *ptr1=&ARG1;
while (n--) {
*ptr1++ = *ptr0++;
}

View File

@ -6516,7 +6516,7 @@ p_nth_instance( USES_REGS1 )
UNLOCK(pe->PELock);
return FALSE;
}
CurSlot = Yap_StartSlots( PASS_REGS1 );
CurSlot = Yap_StartSlots( );
/* I have pe and n */
sl4 = Yap_InitSlot( ARG4 PASS_REGS );
/* in case we have to index or to expand code */

View File

@ -1581,7 +1581,7 @@ mark_environments(CELL_PTR gc_ENV, size_t size, CELL *pvbmap USES_REGS)
//fprintf(stderr,"ENV %p %ld\n", gc_ENV, size);
#ifdef DEBUG
if (size < 0 || size > 512)
if (/* size < 0 || */ size > 512)
fprintf(stderr,"OOPS in GC: env size for %p is " UInt_FORMAT "\n", gc_ENV, (CELL)size);
#endif
mark_db_fixed((CELL *)gc_ENV[E_CP] PASS_REGS);

View File

@ -1234,7 +1234,7 @@ InitScratchPad(int wid)
REMOTE_ScratchPad(wid).msz = SCRATCH_START_SIZE;
}
CELL *
static CELL *
InitHandles(int wid) {
size_t initial_slots = 1024;
CELL *handles;

View File

@ -82,9 +82,9 @@ p_load_foreign( USES_REGS1 )
/* call the OS specific function for dynamic loading */
if(Yap_LoadForeign(ofiles,libs,InitProcName,&InitProc)==LOAD_SUCCEEDED) {
Yap_StartSlots( PASS_REGS1 );
Yap_StartSlots( );
(*InitProc)();
Yap_CloseSlots(CurSlot PASS_REGS);
Yap_CloseSlots(CurSlot);
returncode = TRUE;
}

View File

@ -862,7 +862,7 @@ Yap_TermToString(Term t, char *s, size_t sz, size_t *length, int *encoding, int
Int myASP = LCL0-ASP;
yhandle_t CurSlot = Yap_StartSlots();
Yap_StartSlots( PASS_REGS1 );
Yap_StartSlots( );
l = Yap_InitSlot(t PASS_REGS );
{ IOENC encodings[3];
@ -901,14 +901,14 @@ Yap_TermToString(Term t, char *s, size_t sz, size_t *length, int *encoding, int
if (r == buf) {
char *bf = malloc(*length+1);
if (!bf) {
Yap_CloseSlots(CurSlot PASS_REGS);
Yap_CloseSlots(CurSlot);
ASP = LCL0-myASP;
return NULL;
}
strncpy(bf,buf,*length+1);
r = bf;
}
Yap_CloseSlots(CurSlot PASS_REGS);
Yap_CloseSlots(CurSlot);
ASP = LCL0-myASP;
return r;
} else
@ -920,7 +920,7 @@ Yap_TermToString(Term t, char *s, size_t sz, size_t *length, int *encoding, int
Sfree(r);
}
}
Yap_CloseSlots(CurSlot PASS_REGS);
Yap_CloseSlots(CurSlot);
ASP = LCL0-myASP;
return NULL;
}

View File

@ -657,9 +657,9 @@ p_save2( USES_REGS1 )
if (!Yap_unify(ARG2,MkIntTerm(1)))
return FALSE;
which_save = 2;
Yap_StartSlots( PASS_REGS1 );
CurSlot = Yap_StartSlots();
res = do_save(DO_EVERYTHING PASS_REGS);
Yap_CloseSlots(CurSlot PASS_REGS);
Yap_CloseSlots(CurSlot);
return res;
}

View File

@ -1742,6 +1742,15 @@ static Int p_has_eam(USES_REGS1) {
#endif
}
static Int p_has_jit(USES_REGS1) {
#ifdef HAS_JIT
return (TRUE);
#else
return (FALSE);
#endif
}
static Int p_set_yap_flags(USES_REGS1) {
Term tflag = Deref(ARG1);
Term tvalue = Deref(ARG2);
@ -1996,7 +2005,7 @@ typedef void (*Proc)(void);
Proc E_Modules[] = {/* init_fc,*/ (Proc)0};
#ifndef YAPOR
#ifdef YAPOR
static Int p_parallel_mode(USES_REGS1) { return FALSE; }
static Int p_yapor_workers(USES_REGS1) { return FALSE; }
@ -2084,16 +2093,15 @@ void Yap_InitCPreds(void) {
Yap_InitCPred("$set_flag", 4, p_set_flag, SyncPredFlag);
Yap_InitCPred("$has_yap_or", 0, p_has_yap_or, SafePredFlag | SyncPredFlag);
Yap_InitCPred("$has_eam", 0, p_has_eam, SafePredFlag | SyncPredFlag);
Yap_InitCPred("$has_jit", 0, p_has_jit, SafePredFlag | SyncPredFlag);
#ifdef YAPOR
Yap_InitCPred("parallel_mode", 1, p_parallel_mode,
SafePredFlag | SyncPredFlag);
Yap_InitCPred("$c_yapor_workers", 1, p_yapor_workers,
SafePredFlag | SyncPredFlag);
#endif /* YAPOR */
#endif
#ifdef YAP_JIT
Yap_InitCPred("init", 1, p_parallel_mode,
SafePredFlag | SyncPredFlag);
Yap_InitCPred("$c_yapor_workers", 1, p_yapor_workers,
Yap_InitCPred("$jit_init", 1, p_jit,
SafePredFlag | SyncPredFlag);
#endif /* YAPOR */
#ifdef INES

View File

@ -395,7 +395,6 @@ read_Text( void *buf, seq_tv_t *inp, encoding_t *enc, int *minimal, size_t *leng
{
char *s;
wchar_t *ws;
yhandle_t CurSlot = Yap_StartSlots();
/* we know what the term is */
switch (inp->type & YAP_TYPE_MASK) {
@ -412,8 +411,9 @@ read_Text( void *buf, seq_tv_t *inp, encoding_t *enc, int *minimal, size_t *leng
return 0L;
}
s = StringOfTerm( inp->val.t );
if ( s == NULL )
if ( s == NULL ) {
return 0L;
}
// this is a term, extract the UTF8 representation
*enc = YAP_UTF8;
*minimal = FALSE;
@ -426,7 +426,9 @@ read_Text( void *buf, seq_tv_t *inp, encoding_t *enc, int *minimal, size_t *leng
{
int wide = FALSE;
s = Yap_ListOfCodesToBuffer( buf, inp->val.t, inp, &wide, lengp PASS_REGS);
if (!s) return NULL;
if (!s) {
return NULL;
}
*enc = ( wide ? YAP_WCHAR : YAP_CHAR );
}
return s;
@ -447,7 +449,9 @@ read_Text( void *buf, seq_tv_t *inp, encoding_t *enc, int *minimal, size_t *leng
{
int wide = FALSE;
s = Yap_ListToBuffer( buf, inp->val.t, inp, &wide, lengp PASS_REGS);
if (!s) return NULL;
if (!s) {
return NULL;
}
*enc = ( wide ? YAP_WCHAR : YAP_CHAR );
}
return s;
@ -524,24 +528,23 @@ read_Text( void *buf, seq_tv_t *inp, encoding_t *enc, int *minimal, size_t *leng
return (void *)inp->val.w;
case YAP_STRING_LITERAL:
{
Yap_StartSlots( PASS_REGS1 );
yhandle_t CurSlot = Yap_StartSlots( );
if (buf) s = buf;
else s = Yap_PreAllocCodeSpace();
size_t sz = LOCAL_MAX_SIZE-1;
IOSTREAM *fd;
AUX_ERROR( inp->val.t, LOCAL_MAX_SIZE, s, char);
CurSlot = Yap_StartSlots();
fd = Sopenmem(&s, &sz, "w");
fd->encoding = ENC_UTF8;
if ( ! PL_write_term(fd, Yap_InitSlot(inp->val.t PASS_REGS), 1200, 0) ||
Sputcode(EOS, fd) < 0 ||
Sflush(fd) < 0 ) {
Yap_CloseSlots(CurSlot PASS_REGS);
AUX_ERROR( inp->val.t, LOCAL_MAX_SIZE, s, char);
} else {
Yap_CloseSlots(CurSlot PASS_REGS);
}
*enc = YAP_UTF8;
*lengp = strlen(s);
Yap_CloseSlots(CurSlot);
return s;
}
default:

View File

@ -401,7 +401,11 @@ IsStringTerm (Term t)
#include <stdio.h>
#ifdef __cplusplus
#include <gmpxx.h>
#else
#include <gmp.h>
#endif
#else

View File

@ -24,7 +24,7 @@
Also known as term handles, slots are offsets to entries in the local stack. YAP never compresses the local stack, so slots are respected by the garbage collector,
hence providing a way to access terms without being exposed to stack shifts or garbage-collection.
Space is released when the function terminataes. Thus, slots will be automatically released at the end
Space is released when the function terminates. Thus, slots will be automatically released at the end
of a function. Hence, slots should always be used as local variables.
Slots are organized as follows:
@ -48,11 +48,12 @@ This section lists the main internal functions for slot management. These functi
*************************************************************************************************/
/// @brief start a new set of slots, linking them to the last active slots (who may, or not, be active).
/// Also states how many slots we had when we entered a segment of code.
static inline yhandle_t
/// @brief declares a new set of slots.
/// Used to tell how many slots we had when we entered a segment of code.
#define Yap_StartSlots() Yap_StartSlots__( PASS_REGS1 )
Yap_StartSlots( USES_REGS1 ) {
static inline yhandle_t
Yap_StartSlots__( USES_REGS1 ) {
// fprintf( stderr, " StartSlots = %ld", LOCAL_CurSlot);
if (LOCAL_CurSlot < 0) {
Yap_Error( SYSTEM_ERROR, 0L, " StartSlots = %ld", LOCAL_CurSlot);
@ -62,8 +63,10 @@ if (LOCAL_CurSlot < 0) {
/// @brief reset slots to a well-known position in the stack
#define Yap_CloseSlots( slot ) Yap_CloseSlots__( slot PASS_REGS )
static inline void
Yap_CloseSlots( yhandle_t slot USES_REGS ) {
Yap_CloseSlots__( yhandle_t slot USES_REGS ) {
LOCAL_CurSlot = slot;
}
@ -94,9 +97,12 @@ Yap_GetPtrFromSlot(yhandle_t slot USES_REGS)
return LOCAL_SlotBase[slot];
}
#define Yap_AddressFromSlot( slot ) Yap_AddressFromSlot__(slot PASS_REGS)
/// @brief get the memory address of a slot
static inline Term *
Yap_AddressFromSlot(yhandle_t slot USES_REGS)
Yap_AddressFromSlot__(yhandle_t slot USES_REGS)
{
return LOCAL_SlotBase+slot;
}
@ -111,7 +117,7 @@ Yap_PutInSlot(yhandle_t slot, Term t USES_REGS)
#define max(X,Y) ( X > Y ? X : Y )
static inline void
ensure_slots(int N)
ensure_slots(int N USES_REGS)
{
if (LOCAL_CurSlot+N >= LOCAL_NSlots) {
size_t inc = max(16*1024, LOCAL_NSlots/2); // measured in cells
@ -129,7 +135,7 @@ Yap_InitSlot(Term t USES_REGS)
{
yhandle_t old_slots = LOCAL_CurSlot;
ensure_slots( 1 );
ensure_slots( 1 PASS_REGS);
LOCAL_SlotBase[old_slots] = t;
LOCAL_CurSlot++;
return old_slots;
@ -142,7 +148,7 @@ Yap_NewSlots(int n USES_REGS)
yhandle_t old_slots = LOCAL_CurSlot;
int i;
ensure_slots(n);
ensure_slots(n PASS_REGS);
for (i = 0; i< n; i++) {
RESET_VARIABLE(Yap_AddressFromSlot(old_slots+i) );
}
@ -159,7 +165,7 @@ Yap_InitSlots__(int n, Term *ts USES_REGS)
yhandle_t old_slots = LOCAL_CurSlot;
int i;
ensure_slots( n );
ensure_slots( n PASS_REGS);
for (i=0; i< n; i++)
LOCAL_SlotBase[old_slots+i] = ts[i];
LOCAL_CurSlot += n;

View File

@ -40,7 +40,7 @@ extern void Yap_WriteAtom(IOSTREAM *s, Atom atom);
extern atom_t codeToAtom(int chrcode);
#define valTermRef(t) ((Word)Yap_AddressFromSlot(t PASS_REGS))
#define valTermRef(t) ((Word)Yap_AddressFromSlot(t))
#include "pl-codelist.h"