This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/H/YapHandles.h

154 lines
4.4 KiB
C
Raw Normal View History

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
#include "Regs.h"
2013-11-20 22:20:51 +00:00
/**
@group term_t_slots
2013-11-20 22:20:51 +00: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
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
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
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
- by SWI's PL_foreign_frame() function,
2013-11-20 22:20:51 +00: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
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
*************************************************************************************************/
/// @brief start a new set of slots, linking them to the last active slots (who may, or not, be active).
static inline yhandle_t
2013-11-20 22:20:51 +00:00
Yap_StartSlots( USES_REGS1 ) {
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;
}
/// @brief reset slots to a well-known position in the stack
static inline void
Yap_CloseSlots( yhandle_t slot USES_REGS ) {
LOCAL_CurSlot = slot;
}
/// @brief report the current position of the slots, assuming that they occupy the top of the stack.
static inline yhandle_t
2013-11-20 22:20:51 +00:00
Yap_CurrentSlot( USES_REGS1 ) {
return IntOfTerm(ASP[0]);
}
/// @brief read from a slot.
2013-11-20 22:20:51 +00:00
static inline Term
Yap_GetFromSlot(yhandle_t slot USES_REGS)
2013-11-20 22:20:51 +00:00
{
return(Deref(LCL0[slot]));
}
/// @brief read from a slot. but does not try to dereference the slot.
2013-11-20 22:20:51 +00:00
static inline Term
Yap_GetDerefedFromSlot(yhandle_t slot USES_REGS)
2013-11-20 22:20:51 +00:00
{
return LCL0[slot];
}
/// @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
Yap_GetPtrFromSlot(yhandle_t slot USES_REGS)
2013-11-20 22:20:51 +00:00
{
return(LCL0[slot]);
}
/// @brief get the memory address of a slot
2013-11-20 22:20:51 +00:00
static inline Term *
Yap_AddressFromSlot(yhandle_t slot USES_REGS)
2013-11-20 22:20:51 +00:00
{
return(LCL0+slot);
}
/// @brief store term in a slot
2013-11-20 22:20:51 +00:00
static inline void
Yap_PutInSlot(yhandle_t slot, Term t USES_REGS)
2013-11-20 22:20:51 +00:00
{
LCL0[slot] = t;
}
/// @brief allocate n empty new slots
static inline yhandle_t
2013-11-20 22:20:51 +00:00
Yap_NewSlots(int n USES_REGS)
{
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);
}
/// @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)
{
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);
}
/// @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
Yap_RecoverSlots(int n, yhandle_t topSlot USES_REGS)
2013-11-20 22:20:51 +00:00
{
yhandle_t old_slots = IntOfTerm(ASP[0]);
if (old_slots < n) {
2013-11-20 22:20:51 +00:00
return FALSE;
}
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