2013-11-20 22:20:51 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog %W% %G% *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: YapHandles.h *
|
|
|
|
* mods: *
|
|
|
|
* comments: term handles for YAP: basic ops *
|
|
|
|
* version: $Id: Yap.h,v 1.38 2008-06-18 10:02:27 vsc Exp $ *
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#ifndef YAP_HANDLES_H
|
|
|
|
#define YAP_HANDLES_H 1
|
2014-05-14 17:59:08 +01:00
|
|
|
#include "Regs.h"
|
2013-11-20 22:20:51 +00:00
|
|
|
|
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
/**
|
2014-07-27 01:14:15 +01:00
|
|
|
@groupdef term_t_slots
|
2013-11-20 22:20:51 +00:00
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
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.
|
2013-11-20 22:20:51 +00:00
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
Space is released when the function terminataes. Thus, slots will be automatically released at the end
|
|
|
|
of a function. Hence, slots should always be used as local variables.
|
2013-11-20 22:20:51 +00:00
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
Slots are organized as follows:
|
2014-06-11 19:36:50 +01:00
|
|
|
---- Offset of next pointer in chain (tagged as an handle_t)
|
|
|
|
---- Number of entries (tagged as handle_t), in the example TAG(INT,4)
|
2013-11-20 22:20:51 +00:00
|
|
|
Entry
|
|
|
|
Entry
|
|
|
|
Entry
|
|
|
|
Entry
|
2014-06-11 19:36:50 +01:00
|
|
|
---- Number of entries (tagged as handle_t), in the example TAG(INT,4)
|
2013-11-20 22:20:51 +00:00
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
Slots are not known to the yaam. Instead, A new set of slots is created when the emulator calls user C-code.
|
|
|
|
(see YAP_Execute* functions). They are also created:
|
2013-11-20 22:20:51 +00:00
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
- by SWI's PL_foreign_frame() function,
|
2013-11-20 22:20:51 +00:00
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
- by the YAP_RunGoal() routines and friends, when they exit successfully. Notice that all handles created by c-goals within
|
|
|
|
a `Goal` execution should not be used afterwards.
|
2013-11-20 22:20:51 +00:00
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
This section lists the main internal functions for slot management. These functions are then exported through corresponding FLI C-functions
|
2013-11-20 22:20:51 +00:00
|
|
|
|
|
|
|
*************************************************************************************************/
|
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
/// @brief start a new set of slots, linking them to the last active slots (who may, or not, be active).
|
2014-06-16 14:41:54 +01:00
|
|
|
static inline yhandle_t
|
2013-11-20 22:20:51 +00:00
|
|
|
Yap_StartSlots( USES_REGS1 ) {
|
2014-06-16 14:41:54 +01:00
|
|
|
yhandle_t CurSlot = LOCAL_CurSlot;
|
2013-11-20 22:20:51 +00:00
|
|
|
// if (CurSlot == LCL0-(ASP+(IntOfTerm(ASP[0])+2)))
|
|
|
|
// return CurSlot;
|
|
|
|
/* new slot */
|
|
|
|
*--ASP = MkIntegerTerm(CurSlot);
|
|
|
|
LOCAL_CurSlot = LCL0-ASP;
|
|
|
|
*--ASP = MkIntTerm(0);
|
|
|
|
*--ASP = MkIntTerm(0);
|
|
|
|
return CurSlot;
|
|
|
|
}
|
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
|
|
|
|
/// @brief reset slots to a well-known position in the stack
|
2013-12-15 08:37:10 +00:00
|
|
|
static inline void
|
2014-06-16 14:41:54 +01:00
|
|
|
Yap_CloseSlots( yhandle_t slot USES_REGS ) {
|
2013-12-15 08:37:10 +00:00
|
|
|
LOCAL_CurSlot = slot;
|
|
|
|
}
|
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
/// @brief report the current position of the slots, assuming that they occupy the top of the stack.
|
2014-06-16 14:41:54 +01:00
|
|
|
static inline yhandle_t
|
2013-11-20 22:20:51 +00:00
|
|
|
Yap_CurrentSlot( USES_REGS1 ) {
|
|
|
|
return IntOfTerm(ASP[0]);
|
|
|
|
}
|
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
/// @brief read from a slot.
|
2013-11-20 22:20:51 +00:00
|
|
|
static inline Term
|
2014-06-16 14:41:54 +01:00
|
|
|
Yap_GetFromSlot(yhandle_t slot USES_REGS)
|
2013-11-20 22:20:51 +00:00
|
|
|
{
|
|
|
|
return(Deref(LCL0[slot]));
|
|
|
|
}
|
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
/// @brief read from a slot. but does not try to dereference the slot.
|
2013-11-20 22:20:51 +00:00
|
|
|
static inline Term
|
2014-06-16 14:41:54 +01:00
|
|
|
Yap_GetDerefedFromSlot(yhandle_t slot USES_REGS)
|
2013-11-20 22:20:51 +00:00
|
|
|
{
|
|
|
|
return LCL0[slot];
|
|
|
|
}
|
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
/// @brief read the object in a slot. but do not try to dereference the slot.
|
2013-11-20 22:20:51 +00:00
|
|
|
static inline Term
|
2014-06-16 14:41:54 +01:00
|
|
|
Yap_GetPtrFromSlot(yhandle_t slot USES_REGS)
|
2013-11-20 22:20:51 +00:00
|
|
|
{
|
|
|
|
return(LCL0[slot]);
|
|
|
|
}
|
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
/// @brief get the memory address of a slot
|
2013-11-20 22:20:51 +00:00
|
|
|
static inline Term *
|
2014-06-16 14:41:54 +01:00
|
|
|
Yap_AddressFromSlot(yhandle_t slot USES_REGS)
|
2013-11-20 22:20:51 +00:00
|
|
|
{
|
|
|
|
return(LCL0+slot);
|
|
|
|
}
|
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
/// @brief store term in a slot
|
2013-11-20 22:20:51 +00:00
|
|
|
static inline void
|
2014-06-16 14:41:54 +01:00
|
|
|
Yap_PutInSlot(yhandle_t slot, Term t USES_REGS)
|
2013-11-20 22:20:51 +00:00
|
|
|
{
|
|
|
|
LCL0[slot] = t;
|
|
|
|
}
|
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
/// @brief allocate n empty new slots
|
2014-06-16 14:41:54 +01:00
|
|
|
static inline yhandle_t
|
2013-11-20 22:20:51 +00:00
|
|
|
Yap_NewSlots(int n USES_REGS)
|
|
|
|
{
|
2014-06-16 14:41:54 +01:00
|
|
|
yhandle_t old_slots = IntOfTerm(ASP[0]), oldn = n;
|
2013-11-20 22:20:51 +00:00
|
|
|
while (n > 0) {
|
|
|
|
RESET_VARIABLE(ASP);
|
|
|
|
ASP--;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
ASP[old_slots+oldn+1] = ASP[0] = MkIntTerm(old_slots+oldn);
|
|
|
|
return((ASP+1)-LCL0);
|
|
|
|
}
|
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
/// @brief create a new slot with term t
|
2013-11-20 22:20:51 +00:00
|
|
|
static inline Int
|
|
|
|
Yap_InitSlot(Term t USES_REGS)
|
|
|
|
{
|
2014-06-16 14:41:54 +01:00
|
|
|
yhandle_t old_slots = IntOfTerm(ASP[0]);
|
2013-11-20 22:20:51 +00:00
|
|
|
*ASP = t;
|
|
|
|
ASP--;
|
|
|
|
ASP[old_slots+2] = ASP[0] = MkIntTerm(old_slots+1);
|
|
|
|
return((ASP+1)-LCL0);
|
|
|
|
}
|
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
/// @brief Succeeds if it is to recover the space allocated for $n$ contiguos slots starting at topSlot.
|
2013-11-20 22:20:51 +00:00
|
|
|
static inline int
|
2014-06-16 14:41:54 +01:00
|
|
|
Yap_RecoverSlots(int n, yhandle_t topSlot USES_REGS)
|
2013-11-20 22:20:51 +00:00
|
|
|
{
|
2014-06-16 14:41:54 +01:00
|
|
|
yhandle_t old_slots = IntOfTerm(ASP[0]);
|
2014-05-14 17:59:08 +01:00
|
|
|
if (old_slots < n) {
|
2013-11-20 22:20:51 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2014-05-14 17:59:08 +01:00
|
|
|
if (ASP+1 != LCL0+topSlot)
|
|
|
|
return FALSE;
|
2013-11-20 22:20:51 +00:00
|
|
|
ASP += n;
|
2014-05-15 01:11:26 +01:00
|
|
|
ASP[old_slots+(n-1)] = ASP[0] = MkIntTerm(old_slots-n);
|
2013-11-20 22:20:51 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|