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.3 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:
---- Offset of next pointer in chain (tagged as an Int)
---- Number of entries (tagged as Int), in the example TAG(INT,4)
2013-11-20 22:20:51 +00:00
Entry
Entry
Entry
Entry
---- Number of entries (tagged as Int), 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).
2013-11-20 22:20:51 +00:00
static inline Int
Yap_StartSlots( USES_REGS1 ) {
Int CurSlot = LOCAL_CurSlot;
// 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( Int slot USES_REGS ) {
LOCAL_CurSlot = slot;
}
/// @brief report the current position of the slots, assuming that they occupy the top of the stack.
2013-11-20 22:20:51 +00:00
static inline Int
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(Int slot USES_REGS)
{
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(Int slot USES_REGS)
{
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(Int slot USES_REGS)
{
return(LCL0[slot]);
}
/// @brief get the memory address of a slot
2013-11-20 22:20:51 +00:00
static inline Term *
Yap_AddressFromSlot(Int slot USES_REGS)
{
return(LCL0+slot);
}
/// @brief store term in a slot
2013-11-20 22:20:51 +00:00
static inline void
Yap_PutInSlot(Int slot, Term t USES_REGS)
{
LCL0[slot] = t;
}
/// @brief allocate n empty new slots
2013-11-20 22:20:51 +00:00
static inline Int
Yap_NewSlots(int n USES_REGS)
{
Int old_slots = IntOfTerm(ASP[0]), oldn = n;
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)
{
Int old_slots = IntOfTerm(ASP[0]);
*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, Int topSlot USES_REGS)
2013-11-20 22:20:51 +00:00
{
Int 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