2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: save.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: saving and restoring a Prolog computation *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "@(#)save.c 1.3 3/15/90";
|
|
|
|
#endif
|
|
|
|
|
2002-06-01 02:46:06 +01:00
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
|
|
|
#include <windows.h>
|
2006-08-30 02:06:30 +01:00
|
|
|
#include <psapi.h>
|
2002-06-01 02:46:06 +01:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
#include "absmi.h"
|
|
|
|
#include "alloc.h"
|
2004-12-07 06:01:55 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
#include "dlmalloc.h"
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
#include "yapio.h"
|
|
|
|
#include "sshift.h"
|
|
|
|
#include "Foreign.h"
|
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
#if !HAVE_STRNCAT
|
|
|
|
#define strncat(X,Y,Z) strcat(X,Y)
|
|
|
|
#endif
|
|
|
|
#if !HAVE_STRNCPY
|
|
|
|
#define strncpy(X,Y,Z) strcpy(X,Y)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2001-05-28 20:54:53 +01:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2002-06-01 02:46:06 +01:00
|
|
|
#include "iopreds.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-06-01 02:46:06 +01:00
|
|
|
/********* hack for accesing several kinds of terms. Should be cleaned **/
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-10-30 17:27:19 +00:00
|
|
|
static char StartUpFile[] = "startup";
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
static char end_msg[256] ="*** End of YAP saved state *****";
|
|
|
|
|
2002-05-14 19:22:42 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
/*
|
2002-05-28 17:26:00 +01:00
|
|
|
*
|
|
|
|
#FOR DEBUGGING define DEBUG_RESTORE0 to check the file stuff,
|
|
|
|
#define DEBUG_RESTORE1 to see if it is able to prepare the chain,
|
|
|
|
#define DEBUG_RESTORE2 to see how things are going,
|
|
|
|
#define DEBUG_RESTORE3 to check if the atom chain is still a working chain,
|
2001-04-09 20:54:03 +01:00
|
|
|
* define DEBUG_RESTORE4 if you want to set the output for some
|
|
|
|
* particular file,
|
|
|
|
* define DEBUG_RESTORE5 if you want to see how the stacks are being
|
|
|
|
* cleaned up,
|
|
|
|
* define DEBUG_RESTORE6 if you want to follow the execution in
|
|
|
|
*
|
|
|
|
* Also a file is defined where you can write things, by default stderr
|
|
|
|
*
|
|
|
|
* Good Luck
|
|
|
|
*/
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2005-03-02 18:35:49 +00:00
|
|
|
STATIC_PROTO(int myread, (int, char *, Int));
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(void mywrite, (int, char *, Int));
|
|
|
|
STATIC_PROTO(int open_file, (char *, int));
|
|
|
|
STATIC_PROTO(void close_file, (void));
|
|
|
|
STATIC_PROTO(void putout, (CELL));
|
|
|
|
STATIC_PROTO(void putcellptr, (CELL *));
|
|
|
|
STATIC_PROTO(CELL get_cell, (void));
|
|
|
|
STATIC_PROTO(CELL *get_cellptr, ( /* CELL * */ void));
|
|
|
|
STATIC_PROTO(void put_info, (int, int));
|
|
|
|
STATIC_PROTO(void save_regs, (int));
|
|
|
|
STATIC_PROTO(void save_code_info, (void));
|
|
|
|
STATIC_PROTO(void save_heap, (void));
|
|
|
|
STATIC_PROTO(void save_stacks, (int));
|
|
|
|
STATIC_PROTO(void save_crc, (void));
|
|
|
|
STATIC_PROTO(Int do_save, (int));
|
|
|
|
STATIC_PROTO(Int p_save, (void));
|
|
|
|
STATIC_PROTO(Int p_save_program, (void));
|
2002-10-30 17:27:19 +00:00
|
|
|
STATIC_PROTO(int check_header, (CELL *, CELL *, CELL *, CELL *));
|
2005-03-02 18:35:49 +00:00
|
|
|
STATIC_PROTO(int get_heap_info, (void));
|
|
|
|
STATIC_PROTO(int get_regs, (int));
|
|
|
|
STATIC_PROTO(int get_insts, (OPCODE []));
|
|
|
|
STATIC_PROTO(int get_hash, (void));
|
|
|
|
STATIC_PROTO(int CopyCode, (void));
|
|
|
|
STATIC_PROTO(int CopyStacks, (void));
|
|
|
|
STATIC_PROTO(int get_coded, (int, OPCODE []));
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(void restore_codes, (void));
|
2004-02-06 02:26:23 +00:00
|
|
|
STATIC_PROTO(Term AdjustDBTerm, (Term, Term *));
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(void RestoreDB, (DBEntry *));
|
2003-04-30 18:46:05 +01:00
|
|
|
STATIC_PROTO(void CleanClauses, (yamop *, yamop *,PredEntry *));
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(void rehash, (CELL *, int, int));
|
|
|
|
STATIC_PROTO(void CleanCode, (PredEntry *));
|
2008-04-03 12:34:47 +01:00
|
|
|
STATIC_PROTO(void RestoreEntries, (PropEntry *, int));
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(void RestoreFreeSpace, (void));
|
|
|
|
STATIC_PROTO(void restore_heap, (void));
|
|
|
|
#ifdef DEBUG_RESTORE3
|
|
|
|
STATIC_PROTO(void ShowAtoms, (void));
|
|
|
|
STATIC_PROTO(void ShowEntries, (PropEntry *));
|
|
|
|
#endif
|
2002-10-30 17:27:19 +00:00
|
|
|
STATIC_PROTO(int OpenRestore, (char *, char *, CELL *, CELL *, CELL *, CELL *));
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(void CloseRestore, (void));
|
|
|
|
STATIC_PROTO(int check_opcodes, (OPCODE []));
|
2004-02-06 02:26:23 +00:00
|
|
|
STATIC_PROTO(void RestoreHeap, (OPCODE []));
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(Int p_restore, (void));
|
|
|
|
STATIC_PROTO(void restore_heap_regs, (void));
|
|
|
|
STATIC_PROTO(void restore_regs, (int));
|
|
|
|
#ifdef MACYAP
|
|
|
|
STATIC_PROTO(void NewFileInfo, (long, long));
|
|
|
|
extern int DefVol;
|
|
|
|
#endif
|
|
|
|
|
2008-06-05 17:24:08 +01:00
|
|
|
#ifdef _WIN32
|
2001-05-28 20:54:53 +01:00
|
|
|
#if HAVE_IO_H
|
2001-04-09 20:54:03 +01:00
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2008-06-05 17:24:08 +01:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#ifdef LIGHT
|
|
|
|
|
|
|
|
#include <unix.h>
|
|
|
|
#include <strings.h>
|
|
|
|
|
|
|
|
void
|
|
|
|
LightBug(char *);
|
|
|
|
|
|
|
|
static void
|
|
|
|
LightBug(s)
|
|
|
|
char *s;
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* LIGHT */
|
|
|
|
|
2005-03-02 18:35:49 +00:00
|
|
|
static int
|
|
|
|
do_system_error(yap_error_number etype, const char *msg)
|
|
|
|
{
|
|
|
|
#if HAVE_SNPRINTF
|
|
|
|
#if HAVE_STRERROR
|
|
|
|
snprintf(Yap_ErrorSay,MAX_ERROR_MSG_SIZE,"%s (%s when reading %s)", msg, strerror(errno), Yap_FileNameBuf);
|
|
|
|
#else
|
|
|
|
snprintf(Yap_ErrorSay,MAX_ERROR_MSG_SIZE,"%s, (system error %d when reading %s)",msg,errno,Yap_FileNameBuf);
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#if HAVE_STRERROR
|
|
|
|
sprintf(Yap_ErrorSay,"%s, (%s when reading %s)",msg,strerror(errno),Yap_FileNameBuf);
|
|
|
|
#else
|
|
|
|
sprintf(Yap_ErrorSay,"%s, (system error %d when reading %s)",msg,errno,Yap_FileNameBuf);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
Yap_Error_TYPE = etype;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#if SHORT_INTS
|
|
|
|
|
|
|
|
#ifdef M_WILLIAMS
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2005-03-02 18:35:49 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
myread(int fd, char *buff, Int len)
|
|
|
|
{
|
2005-03-02 18:35:49 +00:00
|
|
|
while (len > 0) {
|
|
|
|
int nchars = read(fd, buff, len);
|
2006-06-02 16:21:35 +01:00
|
|
|
if (nchars <= 0) {
|
2005-03-07 17:49:16 +00:00
|
|
|
return do_system_error(PERMISSION_ERROR_INPUT_PAST_END_OF_STREAM, "bad read on saved state");
|
2005-03-02 18:35:49 +00:00
|
|
|
}
|
|
|
|
len -= nchars;
|
|
|
|
buff += nchars;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-03-02 18:35:49 +00:00
|
|
|
return len;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mywrite(int fd, char *buff, Int len)
|
|
|
|
{
|
|
|
|
while (len > 16000) {
|
|
|
|
write(fd, buff, 16000);
|
|
|
|
len -= 16000;
|
|
|
|
buff += 16000;
|
|
|
|
}
|
|
|
|
write(fd, buff, (unsigned) len);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* SHORT_INTS */
|
|
|
|
|
|
|
|
inline static
|
2005-03-02 18:35:49 +00:00
|
|
|
int myread(int fd, char *buffer, Int len) {
|
2001-04-09 20:54:03 +01:00
|
|
|
int nread;
|
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
nread = read(fd, buffer, (int)len);
|
|
|
|
if (nread < 1) {
|
2005-03-02 18:35:49 +00:00
|
|
|
return do_system_error(PERMISSION_ERROR_INPUT_PAST_END_OF_STREAM,"bad read on saved state");
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
buffer += nread;
|
|
|
|
len -= nread;
|
|
|
|
}
|
2005-03-02 18:35:49 +00:00
|
|
|
return len;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline static
|
|
|
|
void mywrite(int fd, char *buff, Int len) {
|
|
|
|
Int nwritten;
|
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
nwritten = (Int)write(fd, buff, (int)len);
|
2005-03-02 18:35:49 +00:00
|
|
|
if (nwritten < 0) {
|
|
|
|
Yap_ErrorMessage = "bad write on saved state";
|
|
|
|
Yap_Error(SYSTEM_ERROR,TermNil,Yap_ErrorMessage);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
buff += nwritten;
|
|
|
|
len -= nwritten;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* SHORT_INTS */
|
|
|
|
|
|
|
|
#define FullSaved 1
|
|
|
|
|
|
|
|
/* Where the code was before */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef CELL *CELLPOINTER;
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static int splfild = 0;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
#ifdef DEBUG_RESTORE4
|
|
|
|
static FILE *errout;
|
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
#define errout Yap_stderr
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
static Int OldHeapUsed;
|
|
|
|
|
|
|
|
static CELL which_save;
|
|
|
|
|
|
|
|
/* Open a file to read or to write */
|
|
|
|
static int
|
2002-11-11 17:38:10 +00:00
|
|
|
open_file(char *my_file, int flag)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
int splfild;
|
|
|
|
|
|
|
|
#ifdef M_WILLIAMS
|
|
|
|
if (flag & O_CREAT)
|
2002-11-11 17:38:10 +00:00
|
|
|
splfild = creat(my_file, flag);
|
2001-04-09 20:54:03 +01:00
|
|
|
else
|
2002-11-11 17:38:10 +00:00
|
|
|
splfild = open(my_file, flag);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (splfild < 0) {
|
|
|
|
#else
|
|
|
|
#ifdef O_BINARY
|
|
|
|
#if _MSC_VER
|
2002-11-11 17:38:10 +00:00
|
|
|
if ((splfild = _open(my_file, flag | O_BINARY), _S_IREAD | _S_IWRITE) < 0)
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-11-11 17:38:10 +00:00
|
|
|
if ((splfild = open(my_file, flag | O_BINARY, 0775)) < 0)
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
#else /* O_BINARY */
|
2002-11-11 17:38:10 +00:00
|
|
|
if ((splfild = open(my_file, flag, 0755)) < 0)
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* O_BINARY */
|
|
|
|
#endif /* M_WILLIAMS */
|
|
|
|
{
|
|
|
|
splfild = 0; /* We do not have an open file */
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#ifdef undf0
|
2002-11-11 17:38:10 +00:00
|
|
|
fprintf(errout, "Opened file %s\n", my_file);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2005-03-02 18:35:49 +00:00
|
|
|
return splfild;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
close_file(void)
|
|
|
|
{
|
2002-10-30 17:27:19 +00:00
|
|
|
if (splfild == 0)
|
|
|
|
return;
|
2001-04-09 20:54:03 +01:00
|
|
|
close(splfild);
|
|
|
|
splfild = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stores a cell in a file */
|
|
|
|
static void
|
|
|
|
putout(CELL l)
|
|
|
|
{
|
2002-10-30 17:27:19 +00:00
|
|
|
mywrite(splfild, (char *) &l, sizeof(CELL));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* stores a pointer to a cell in a file */
|
|
|
|
static void
|
|
|
|
putcellptr(CELL *l)
|
|
|
|
{
|
2002-10-30 17:27:19 +00:00
|
|
|
mywrite(splfild, (char *) &l, sizeof(CELLPOINTER));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* gets a cell from a file */
|
|
|
|
static CELL
|
|
|
|
get_cell(void)
|
|
|
|
{
|
2002-10-30 17:27:19 +00:00
|
|
|
CELL l;
|
|
|
|
myread(splfild, (char *) &l, Unsigned(sizeof(CELL)));
|
|
|
|
return (l);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* gets a cell from a file */
|
|
|
|
static CELL
|
|
|
|
get_header_cell(void)
|
|
|
|
{
|
|
|
|
CELL l;
|
|
|
|
int count = 0, n;
|
|
|
|
while (count < sizeof(CELL)) {
|
|
|
|
if ((n = read(splfild, &l, sizeof(CELL)-count)) < 0) {
|
2005-03-02 18:35:49 +00:00
|
|
|
do_system_error(PERMISSION_ERROR_INPUT_PAST_END_OF_STREAM,"failed to read saved state header");
|
|
|
|
return 0L;
|
2002-10-30 17:27:19 +00:00
|
|
|
}
|
|
|
|
count += n;
|
|
|
|
}
|
2005-03-02 18:35:49 +00:00
|
|
|
return l;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* gets a pointer to cell from a file */
|
|
|
|
static CELL *
|
|
|
|
get_cellptr(void)
|
|
|
|
{
|
2002-10-30 17:27:19 +00:00
|
|
|
CELL *l;
|
|
|
|
|
|
|
|
myread(splfild, (char *) &l, Unsigned(sizeof(CELLPOINTER)));
|
|
|
|
return (l);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* writes the header (at the moment YAPV*), info about what kind of saved
|
|
|
|
* set, the work size, and the space ocuppied
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
put_info(int info, int mode)
|
|
|
|
{
|
|
|
|
char msg[256];
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
sprintf(msg, "#!/bin/sh\nexec_dir=${YAPBINDIR:-%s}\nexec $exec_dir/yap $0 \"$@\"\n%cYAPV%s", BIN_DIR, 1, YAP_VERSION);
|
2001-04-09 20:54:03 +01:00
|
|
|
mywrite(splfild, msg, strlen(msg) + 1);
|
|
|
|
putout(Unsigned(info));
|
|
|
|
/* say whether we just saved the heap or everything */
|
|
|
|
putout(mode);
|
|
|
|
/* current state of stacks, to be used by SavedInfo */
|
|
|
|
/* space available in heap area */
|
2002-11-18 18:18:05 +00:00
|
|
|
putout(Unsigned(Yap_GlobalBase)-Unsigned(Yap_HeapBase));
|
2001-04-09 20:54:03 +01:00
|
|
|
/* space available for stacks */
|
2002-11-18 18:18:05 +00:00
|
|
|
putout(Unsigned(Yap_LocalBase)-Unsigned(Yap_GlobalBase));
|
2001-04-09 20:54:03 +01:00
|
|
|
/* space available for trail */
|
2002-11-18 18:18:05 +00:00
|
|
|
putout(Unsigned(Yap_TrailTop)-Unsigned(Yap_TrailBase));
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Space used in heap area */
|
2002-11-18 18:18:05 +00:00
|
|
|
putout(Unsigned(HeapTop)-Unsigned(Yap_HeapBase));
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Space used for local stack */
|
|
|
|
putout(Unsigned(LCL0)-Unsigned(ASP));
|
|
|
|
/* Space used for global stack */
|
2002-11-18 18:18:05 +00:00
|
|
|
putout(Unsigned(H) - Unsigned(Yap_GlobalBase));
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Space used for trail */
|
2002-11-18 18:18:05 +00:00
|
|
|
putout(Unsigned(TR) - Unsigned(Yap_TrailBase));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
save_regs(int mode)
|
|
|
|
{
|
|
|
|
/* save all registers */
|
|
|
|
putout((CELL)compile_arrays);
|
|
|
|
if (mode == DO_EVERYTHING) {
|
|
|
|
putcellptr((CELL *)CP);
|
|
|
|
putcellptr(ENV);
|
|
|
|
putcellptr(ASP);
|
|
|
|
/* putout((CELL)N); */
|
|
|
|
putcellptr(H0);
|
|
|
|
putcellptr(LCL0);
|
|
|
|
putcellptr(H);
|
|
|
|
putcellptr(HB);
|
|
|
|
putcellptr((CELL *)B);
|
|
|
|
putcellptr((CELL *)TR);
|
|
|
|
putcellptr(YENV);
|
|
|
|
putcellptr(S);
|
|
|
|
putcellptr((CELL *)P);
|
|
|
|
putout(CreepFlag);
|
2002-05-14 19:22:42 +01:00
|
|
|
putout(EX);
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef COROUTINING
|
|
|
|
putout(DelayedVars);
|
|
|
|
#endif
|
2005-10-31 12:27:54 +00:00
|
|
|
#if defined(SBA) || defined(TABLING)
|
|
|
|
putcellptr(H_FZ);
|
|
|
|
putcellptr((CELL *)B_FZ);
|
|
|
|
putcellptr((CELL *)TR_FZ);
|
|
|
|
#endif /* SBA || TABLING */
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-05-14 19:22:42 +01:00
|
|
|
putout(CurrentModule);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (mode == DO_EVERYTHING) {
|
|
|
|
#ifdef COROUTINING
|
|
|
|
putout(WokenGoals);
|
|
|
|
#endif
|
|
|
|
#ifdef DEPTH_LIMIT
|
|
|
|
putout(DEPTH);
|
|
|
|
#endif
|
2005-09-21 04:49:33 +01:00
|
|
|
putout(GcGeneration);
|
2006-03-06 14:04:57 +00:00
|
|
|
putout(GcPhase);
|
|
|
|
putout(GcCurrentPhase);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
/* The operand base */
|
|
|
|
putcellptr(CellPtr(XREGS));
|
|
|
|
putout(which_save);
|
|
|
|
/* Now start by saving the code */
|
|
|
|
/* the heap boundaries */
|
2002-11-18 18:18:05 +00:00
|
|
|
putcellptr(CellPtr(Yap_HeapBase));
|
2001-04-09 20:54:03 +01:00
|
|
|
putcellptr(CellPtr(HeapTop));
|
|
|
|
/* and the space it ocuppies */
|
2006-05-18 17:33:05 +01:00
|
|
|
putout(Unsigned(Yap_heap_regs->heap_used));
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Then the start of the free code */
|
|
|
|
putcellptr(CellPtr(FreeBlocks));
|
2004-10-28 21:12:23 +01:00
|
|
|
putcellptr(AuxSp);
|
|
|
|
putcellptr(CellPtr(AuxTop));
|
2001-04-09 20:54:03 +01:00
|
|
|
if (mode == DO_EVERYTHING) {
|
|
|
|
/* put the old trail base, just in case it moves again */
|
|
|
|
putout(ARG1);
|
|
|
|
if (which_save == 2) {
|
|
|
|
putout(ARG2);
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
putcellptr(CellPtr(Yap_TrailBase));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
save_code_info(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* First the instructions */
|
|
|
|
{
|
|
|
|
op_numbers i;
|
|
|
|
|
|
|
|
OPCODE my_ops[_std_top+1];
|
|
|
|
for (i = _Ystop; i <= _std_top; ++i)
|
2002-11-18 18:18:05 +00:00
|
|
|
my_ops[i] = Yap_opcode(i);
|
2001-04-09 20:54:03 +01:00
|
|
|
mywrite(splfild, (char *)my_ops, sizeof(OPCODE)*(_std_top+1));
|
|
|
|
}
|
|
|
|
/* and the current character codes */
|
2002-11-18 18:18:05 +00:00
|
|
|
mywrite(splfild, Yap_chtype, NUMBER_OF_CHARS);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
save_heap(void)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
/* Then save the whole heap */
|
2008-04-11 17:30:28 +01:00
|
|
|
#if defined(YAPOR) || (defined(TABLING) && !defined(YAP_MEMORY_ALLOC_SCHEME))
|
2001-04-09 20:54:03 +01:00
|
|
|
/* skip the local and global data structures */
|
2002-11-18 18:18:05 +00:00
|
|
|
j = Unsigned(&GLOBAL) - Unsigned(Yap_HeapBase);
|
2001-04-09 20:54:03 +01:00
|
|
|
putout(j);
|
2002-11-18 18:18:05 +00:00
|
|
|
mywrite(splfild, (char *) Yap_HeapBase, j);
|
2006-04-29 16:54:56 +01:00
|
|
|
j = Unsigned(HeapTop) - Unsigned(REMOTE+MAX_WORKERS);
|
2001-04-09 20:54:03 +01:00
|
|
|
putout(j);
|
2006-04-29 16:54:56 +01:00
|
|
|
mywrite(splfild, (char *) &(REMOTE[MAX_WORKERS]), j);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
j = Unsigned(HeapTop) - Unsigned(Yap_HeapBase);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* store 10 more cells because of the memory manager */
|
2002-11-18 18:18:05 +00:00
|
|
|
mywrite(splfild, (char *) Yap_HeapBase, j);
|
2008-04-11 17:30:28 +01:00
|
|
|
#endif /* YAPOR || (TABLING && !YAP_MEMORY_ALLOC_SCHEME) */
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
save_stacks(int mode)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case DO_EVERYTHING:
|
|
|
|
/* Now, go and save the state */
|
|
|
|
/* Save the local stack */
|
|
|
|
j = Unsigned(LCL0) - Unsigned(ASP);
|
|
|
|
mywrite(splfild, (char *) ASP, j);
|
|
|
|
/* Save the global stack */
|
2002-11-18 18:18:05 +00:00
|
|
|
j = Unsigned(H) - Unsigned(Yap_GlobalBase);
|
|
|
|
mywrite(splfild, (char *) Yap_GlobalBase, j);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Save the trail */
|
2002-11-18 18:18:05 +00:00
|
|
|
j = Unsigned(TR) - Unsigned(Yap_TrailBase);
|
|
|
|
mywrite(splfild, (char *) Yap_TrailBase, j);
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
case DO_ONLY_CODE:
|
|
|
|
{
|
|
|
|
tr_fr_ptr tr_ptr = TR;
|
2002-11-18 18:18:05 +00:00
|
|
|
while (tr_ptr != (tr_fr_ptr)Yap_TrailBase) {
|
2001-04-09 20:54:03 +01:00
|
|
|
CELL val = TrailTerm(tr_ptr-1);
|
2001-12-17 18:31:11 +00:00
|
|
|
if (IsVarTerm(val)) {
|
|
|
|
CELL *d1 = VarOfTerm(val);
|
|
|
|
if (d1 < (CELL *)HeapTop)
|
|
|
|
putout(val);
|
|
|
|
} else if (IsPairTerm(val)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
CELL *d1 = RepPair(val);
|
|
|
|
if (d1 < (CELL *)HeapTop)
|
2001-12-17 18:31:11 +00:00
|
|
|
putout(val);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
tr_ptr--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
putcellptr(NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
save_crc(void)
|
|
|
|
{
|
|
|
|
/* Save a CRC */
|
|
|
|
mywrite(splfild, end_msg, 256);
|
|
|
|
#ifdef MACYAP
|
|
|
|
NewFileInfo('TEXT', 'MYap');
|
|
|
|
if (DefVol)
|
|
|
|
SetVol(0l, DefVol);
|
|
|
|
DefVol = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
do_save(int mode) {
|
|
|
|
#ifdef MACYAP
|
|
|
|
NewFileInfo('YAPS', 'MYap');
|
|
|
|
#endif
|
|
|
|
Term t1 = Deref(ARG1);
|
2006-05-19 15:31:32 +01:00
|
|
|
|
|
|
|
if (Yap_HoleSize) {
|
|
|
|
Yap_Error(SYSTEM_ERROR,MkAtomTerm(Yap_LookupAtom(Yap_FileNameBuf)),
|
2006-08-30 02:06:30 +01:00
|
|
|
"restore/1: address space has holes of size %ld, cannot save", (long int)Yap_HoleSize);
|
2006-05-19 15:31:32 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
if (!Yap_GetName(Yap_FileNameBuf, YAP_FILENAME_MAX, t1)) {
|
|
|
|
Yap_Error(TYPE_ERROR_LIST,t1,"save/1");
|
2005-03-02 18:35:49 +00:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_CloseStreams(TRUE);
|
|
|
|
if ((splfild = open_file(Yap_FileNameBuf, O_WRONLY | O_CREAT)) < 0) {
|
|
|
|
Yap_Error(SYSTEM_ERROR,MkAtomTerm(Yap_LookupAtom(Yap_FileNameBuf)),
|
2001-04-09 20:54:03 +01:00
|
|
|
"restore/1, open(%s)", strerror(errno));
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
put_info(FullSaved, mode);
|
|
|
|
save_regs(mode);
|
|
|
|
save_code_info();
|
|
|
|
save_heap();
|
|
|
|
save_stacks(mode);
|
|
|
|
save_crc();
|
|
|
|
close_file();
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Saves a complete prolog environment */
|
|
|
|
static Int
|
|
|
|
p_save(void)
|
|
|
|
{
|
2002-10-22 04:45:24 +01:00
|
|
|
#if defined(YAPOR) || defined(THREADS)
|
2002-10-21 23:52:36 +01:00
|
|
|
if (NOfThreads != 1) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR,TermNil,"cannot perform save: more than a worker/thread running");
|
2002-10-21 23:52:36 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
2002-10-22 04:45:24 +01:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
which_save = 1;
|
|
|
|
return(do_save(DO_EVERYTHING));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Saves a complete prolog environment */
|
|
|
|
static Int
|
|
|
|
p_save2(void)
|
|
|
|
{
|
2002-10-22 04:45:24 +01:00
|
|
|
#if defined(YAPOR) || defined(THREADS)
|
2002-10-21 23:52:36 +01:00
|
|
|
if (NOfThreads != 1) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR,TermNil,
|
2002-11-11 17:38:10 +00:00
|
|
|
"cannot perform save: more than a worker/thread running");
|
2002-10-21 23:52:36 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
2002-10-22 04:45:24 +01:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
which_save = 2;
|
2002-11-18 18:18:05 +00:00
|
|
|
return(do_save(DO_EVERYTHING) && Yap_unify(ARG2,MkIntTerm(1)));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Just save the program, not the stacks */
|
|
|
|
static Int
|
|
|
|
p_save_program(void)
|
|
|
|
{
|
|
|
|
which_save = 0;
|
2005-03-02 18:35:49 +00:00
|
|
|
return do_save(DO_ONLY_CODE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, to restore the saved code */
|
|
|
|
|
|
|
|
/* First check out if we are dealing with a valid file */
|
|
|
|
static int
|
2002-10-30 17:27:19 +00:00
|
|
|
check_header(CELL *info, CELL *ATrail, CELL *AStack, CELL *AHeap)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
char pp[80];
|
|
|
|
char msg[256];
|
2002-12-27 16:53:09 +00:00
|
|
|
CELL hp_size, gb_size, lc_size, tr_size, mode;
|
2006-06-02 16:21:35 +01:00
|
|
|
int n;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-10-30 17:27:19 +00:00
|
|
|
/* make sure we always check if there are enough bytes */
|
2001-04-09 20:54:03 +01:00
|
|
|
/* skip the first line */
|
2006-06-02 16:21:35 +01:00
|
|
|
pp[0] = '\0';
|
2001-04-09 20:54:03 +01:00
|
|
|
do {
|
2006-06-02 16:21:35 +01:00
|
|
|
if ((n = read(splfild, pp, 1)) <= 0) {
|
2005-03-02 18:35:49 +00:00
|
|
|
do_system_error(PERMISSION_ERROR_INPUT_PAST_END_OF_STREAM,"failed to scan first line from saved state");
|
|
|
|
return FAIL_RESTORE;
|
2002-10-30 17:27:19 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
} while (pp[0] != 1);
|
|
|
|
/* now check the version */
|
2002-11-11 17:38:10 +00:00
|
|
|
sprintf(msg, "YAPV%s", YAP_VERSION);
|
2002-10-30 17:27:19 +00:00
|
|
|
{
|
|
|
|
int count = 0, n, to_read = Unsigned(strlen(msg) + 1);
|
|
|
|
while (count < to_read) {
|
2006-06-02 16:21:35 +01:00
|
|
|
if ((n = read(splfild, pp, to_read-count)) <= 0) {
|
2005-03-02 18:35:49 +00:00
|
|
|
do_system_error(PERMISSION_ERROR_INPUT_PAST_END_OF_STREAM,"failed to scan version info from saved state");
|
|
|
|
return FAIL_RESTORE;
|
2002-10-30 17:27:19 +00:00
|
|
|
}
|
|
|
|
count += n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pp[0] != 'Y' && pp[1] != 'A' && pp[0] != 'P') {
|
2005-03-02 18:35:49 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
strncpy(Yap_ErrorMessage, "saved state ", MAX_ERROR_MSG_SIZE);
|
|
|
|
strncat(Yap_ErrorMessage, Yap_FileNameBuf, MAX_ERROR_MSG_SIZE);
|
|
|
|
strncat(Yap_ErrorMessage, " failed to match `YAP\'", MAX_ERROR_MSG_SIZE);
|
|
|
|
Yap_Error_TYPE = CONSISTENCY_ERROR;
|
|
|
|
return FAIL_RESTORE;
|
2002-10-30 17:27:19 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
if (strcmp(pp, msg) != 0) {
|
2005-03-02 18:35:49 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
strncpy(Yap_ErrorMessage, "saved state ", MAX_ERROR_MSG_SIZE);
|
|
|
|
strncat(Yap_ErrorMessage, Yap_FileNameBuf, MAX_ERROR_MSG_SIZE);
|
|
|
|
strncat(Yap_ErrorMessage, " failed to match version ID", MAX_ERROR_MSG_SIZE);
|
|
|
|
Yap_Error_TYPE = CONSISTENCY_ERROR;
|
|
|
|
return FAIL_RESTORE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
/* check info on header */
|
|
|
|
/* ignore info on saved state */
|
2002-10-30 17:27:19 +00:00
|
|
|
*info = get_header_cell();
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* check the restore mode */
|
2002-10-30 17:27:19 +00:00
|
|
|
mode = get_header_cell();
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2002-10-30 17:27:19 +00:00
|
|
|
if (mode != DO_EVERYTHING && mode != DO_ONLY_CODE) {
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
/* ignore info on stacks size */
|
2002-10-30 17:27:19 +00:00
|
|
|
*AHeap = get_header_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage) {
|
|
|
|
return FAIL_RESTORE;
|
|
|
|
}
|
2002-10-30 17:27:19 +00:00
|
|
|
*AStack = get_header_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage) {
|
|
|
|
return FAIL_RESTORE;
|
|
|
|
}
|
2002-10-30 17:27:19 +00:00
|
|
|
*ATrail = get_header_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage) {
|
|
|
|
return FAIL_RESTORE;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
/* now, check whether we got enough enough space to load the
|
|
|
|
saved space */
|
2002-09-09 18:40:12 +01:00
|
|
|
hp_size = get_cell();
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2004-01-23 02:23:51 +00:00
|
|
|
while (Yap_HeapBase != NULL &&
|
|
|
|
hp_size > Unsigned(HeapLim) - Unsigned(Yap_HeapBase)) {
|
|
|
|
if(!Yap_growheap(FALSE, hp_size, NULL)) {
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2002-10-09 05:26:48 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (mode == DO_EVERYTHING) {
|
2002-10-30 17:27:19 +00:00
|
|
|
lc_size = get_cell();
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2002-10-30 17:27:19 +00:00
|
|
|
gb_size=get_cell();
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_HeapBase != NULL && lc_size+gb_size > Unsigned(Yap_LocalBase) - Unsigned(Yap_GlobalBase)) {
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage != NULL)
|
|
|
|
Yap_ErrorMessage = "could not allocate enough stack space";
|
|
|
|
return FAIL_RESTORE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_HeapBase != NULL && (tr_size = get_cell()) > Unsigned(Yap_TrailTop) - Unsigned(Yap_TrailBase)) {
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage != NULL)
|
|
|
|
Yap_ErrorMessage = "could not allocate enough trail space";
|
|
|
|
return FAIL_RESTORE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* skip cell size */
|
2002-10-30 17:27:19 +00:00
|
|
|
get_header_cell();
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2002-10-30 17:27:19 +00:00
|
|
|
get_header_cell();
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2002-10-30 17:27:19 +00:00
|
|
|
get_header_cell();
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
return(mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Gets the state of the heap, and evaluates the related variables */
|
2005-03-02 18:35:49 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
get_heap_info(void)
|
|
|
|
{
|
|
|
|
OldHeapBase = (ADDR) get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
OldHeapTop = (ADDR) get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
OldHeapUsed = (Int) get_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
FreeBlocks = (BlockHeader *) get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2004-10-28 21:12:23 +01:00
|
|
|
AuxSp = get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2004-10-28 21:12:23 +01:00
|
|
|
AuxTop = (ADDR)get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2002-11-18 18:18:05 +00:00
|
|
|
HDiff = Unsigned(Yap_HeapBase) - Unsigned(OldHeapBase);
|
2005-03-02 18:35:49 +00:00
|
|
|
return 1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Gets the register array */
|
|
|
|
/* Saves the old bases for the work areas */
|
|
|
|
/* and evaluates the difference from the old areas to the new ones */
|
2005-03-02 18:35:49 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
get_regs(int flag)
|
|
|
|
{
|
2002-11-18 18:18:05 +00:00
|
|
|
CELL *NewGlobalBase = (CELL *)Yap_GlobalBase;
|
2001-04-09 20:54:03 +01:00
|
|
|
CELL *NewLCL0 = LCL0;
|
|
|
|
CELL *OldXREGS;
|
|
|
|
|
|
|
|
/* Get regs */
|
|
|
|
compile_arrays = (int)get_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (flag == DO_EVERYTHING) {
|
|
|
|
CP = (yamop *)get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
ENV = get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
ASP = get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* N = get_cell(); */
|
|
|
|
H0 = get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
LCL0 = get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
H = get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
HB = get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
B = (choiceptr)get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
TR = (tr_fr_ptr)get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
YENV = get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
S = get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
CreepFlag = get_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2005-08-22 05:07:20 +01:00
|
|
|
EX = get_cell();
|
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef COROUTINING
|
|
|
|
DelayedVars = get_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2005-10-31 12:27:54 +00:00
|
|
|
#if defined(SBA) || defined(TABLING)
|
|
|
|
H_FZ = get_cellptr();
|
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
|
|
|
B_FZ = (choiceptr)get_cellptr();
|
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
|
|
|
TR_FZ = (tr_fr_ptr)get_cellptr();
|
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
|
|
|
#endif /* SBA || TABLING */
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-05-14 19:22:42 +01:00
|
|
|
CurrentModule = get_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (flag == DO_EVERYTHING) {
|
|
|
|
#ifdef COROUTINING
|
|
|
|
WokenGoals = get_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
#ifdef DEPTH_LIMIT
|
|
|
|
DEPTH = get_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2005-09-21 04:49:33 +01:00
|
|
|
GcGeneration = get_cell();
|
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2006-03-06 14:04:57 +00:00
|
|
|
GcPhase = get_cell();
|
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
|
|
|
GcCurrentPhase = get_cell();
|
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
/* Get the old bases */
|
|
|
|
OldXREGS = get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
which_save = get_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
XDiff = (CELL)XREGS - (CELL)OldXREGS;
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
|
|
|
if (get_heap_info() < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (flag == DO_EVERYTHING) {
|
|
|
|
ARG1 = get_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (which_save == 2) {
|
|
|
|
ARG2 = get_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
/* get old trail base */
|
|
|
|
OldTrailBase = (ADDR)get_cellptr();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Save the old register where we can easily access them */
|
|
|
|
OldASP = ASP;
|
|
|
|
OldLCL0 = LCL0;
|
2002-11-18 18:18:05 +00:00
|
|
|
OldGlobalBase = (CELL *)Yap_GlobalBase;
|
2001-04-09 20:54:03 +01:00
|
|
|
OldH = H;
|
|
|
|
OldTR = TR;
|
2002-11-18 18:18:05 +00:00
|
|
|
GDiff = Unsigned(NewGlobalBase) - Unsigned(Yap_GlobalBase);
|
2006-08-22 17:12:46 +01:00
|
|
|
GDiff0 = 0;
|
2001-04-09 20:54:03 +01:00
|
|
|
LDiff = Unsigned(NewLCL0) - Unsigned(LCL0);
|
|
|
|
TrDiff = LDiff;
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_GlobalBase = (ADDR)NewGlobalBase;
|
2001-04-09 20:54:03 +01:00
|
|
|
LCL0 = NewLCL0;
|
|
|
|
}
|
2005-03-02 18:35:49 +00:00
|
|
|
return 1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the old opcodes and place them in a hash table */
|
2005-03-02 18:35:49 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
get_insts(OPCODE old_ops[])
|
|
|
|
{
|
2005-03-02 18:35:49 +00:00
|
|
|
return myread(splfild, (char *)old_ops, sizeof(OPCODE)*(_std_top+1));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the old atoms hash table */
|
2005-03-02 18:35:49 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
get_hash(void)
|
|
|
|
{
|
2005-03-02 18:35:49 +00:00
|
|
|
return myread(splfild, Yap_chtype , NUMBER_OF_CHARS);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy all of the old code to the new Heap */
|
2005-03-02 18:35:49 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
CopyCode(void)
|
|
|
|
{
|
2008-04-11 17:30:28 +01:00
|
|
|
#if defined(YAPOR) || (defined(TABLING) && !defined(YAP_MEMORY_ALLOC_SCHEME))
|
2001-04-09 20:54:03 +01:00
|
|
|
/* skip the local and global data structures */
|
|
|
|
CELL j = get_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
2006-06-02 16:21:35 +01:00
|
|
|
return -1;
|
2002-11-18 18:18:05 +00:00
|
|
|
if (j != Unsigned(&GLOBAL) - Unsigned(Yap_HeapBase)) {
|
2005-03-02 18:35:49 +00:00
|
|
|
Yap_ErrorMessage = "code space size does not match saved state";
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-03-02 18:35:49 +00:00
|
|
|
if (myread(splfild, (char *) Yap_HeapBase, j) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
j = get_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2006-04-29 16:54:56 +01:00
|
|
|
if (myread(splfild, (char *) &(REMOTE[MAX_WORKERS]), j) < 0)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2005-03-02 18:35:49 +00:00
|
|
|
if (myread(splfild, (char *) Yap_HeapBase,
|
|
|
|
(Unsigned(OldHeapTop) - Unsigned(OldHeapBase))) < 0)
|
|
|
|
return -1;
|
2008-04-11 17:30:28 +01:00
|
|
|
#endif /* YAPOR || (TABLING && !YAP_MEMORY_ALLOC_SCHEME) */
|
2005-03-02 18:35:49 +00:00
|
|
|
return 1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the local and global stack and also the trail to their new home */
|
|
|
|
/* In REGS we still have nonadjusted values !! */
|
2005-03-02 18:35:49 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
CopyStacks(void)
|
|
|
|
{
|
|
|
|
Int j;
|
|
|
|
char *NewASP;
|
|
|
|
|
|
|
|
j = Unsigned(OldLCL0) - Unsigned(ASP);
|
|
|
|
NewASP = (char *) (Unsigned(ASP) + (Unsigned(LCL0) - Unsigned(OldLCL0)));
|
2005-03-02 18:35:49 +00:00
|
|
|
if (myread(splfild, (char *) NewASP, j) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
j = Unsigned(H) - Unsigned(OldGlobalBase);
|
2005-03-02 18:35:49 +00:00
|
|
|
if (myread(splfild, (char *) Yap_GlobalBase, j) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
j = Unsigned(TR) - Unsigned(OldTrailBase);
|
2005-03-02 18:35:49 +00:00
|
|
|
if (myread(splfild, Yap_TrailBase, j))
|
|
|
|
return -1;
|
|
|
|
return 1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the local and global stack and also the trail to their new home */
|
|
|
|
/* In REGS we still have nonadjusted values !! */
|
2005-03-02 18:35:49 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
CopyTrailEntries(void)
|
|
|
|
{
|
|
|
|
CELL entry, *Entries;
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
Entries = (CELL *)Yap_TrailBase;
|
2001-04-09 20:54:03 +01:00
|
|
|
do {
|
|
|
|
*Entries++ = entry = get_cell();
|
2005-03-02 18:35:49 +00:00
|
|
|
if (Yap_ErrorMessage)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
} while ((CODEADDR)entry != NULL);
|
2005-03-02 18:35:49 +00:00
|
|
|
return 1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get things which are saved in the file */
|
2005-03-02 18:35:49 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
get_coded(int flag, OPCODE old_ops[])
|
|
|
|
{
|
|
|
|
char my_end_msg[256];
|
|
|
|
|
2005-03-02 18:35:49 +00:00
|
|
|
if (get_regs(flag) < 0)
|
|
|
|
return -1;
|
|
|
|
if (get_insts(old_ops) < 0)
|
|
|
|
return -1;
|
|
|
|
if (get_hash() < 0)
|
|
|
|
return -1;
|
|
|
|
if (CopyCode() < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
switch (flag) {
|
|
|
|
case DO_EVERYTHING:
|
2005-03-02 18:35:49 +00:00
|
|
|
if (CopyStacks() < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
case DO_ONLY_CODE:
|
2005-03-02 18:35:49 +00:00
|
|
|
if (CopyTrailEntries() < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Check CRC */
|
2005-03-02 18:35:49 +00:00
|
|
|
if (myread(splfild, my_end_msg, 256) < 0)
|
|
|
|
return -1;
|
|
|
|
if (strcmp(end_msg,my_end_msg) != 0) {
|
|
|
|
Yap_ErrorMessage = "bad trailing CRC in saved state";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* restore some heap registers */
|
|
|
|
static void
|
|
|
|
restore_heap_regs(void)
|
|
|
|
{
|
2008-02-15 12:41:33 +00:00
|
|
|
if (HeapTop) {
|
|
|
|
HeapTop = AddrAdjust(HeapTop);
|
|
|
|
*((YAP_SEG_SIZE *) HeapTop) = InUseFlag;
|
|
|
|
}
|
2006-05-18 17:33:05 +01:00
|
|
|
HeapMax = Yap_heap_regs->heap_used = OldHeapUsed;
|
2008-04-01 16:31:43 +01:00
|
|
|
HeapLim = Yap_GlobalBase;
|
2001-04-09 20:54:03 +01:00
|
|
|
restore_codes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adjust abstract machine registers */
|
|
|
|
static void
|
|
|
|
restore_regs(int flag)
|
|
|
|
{
|
|
|
|
restore_heap_regs();
|
2005-11-23 03:01:33 +00:00
|
|
|
if (CurrentModule) {
|
|
|
|
CurrentModule = AtomTermAdjust(CurrentModule);;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
if (flag == DO_EVERYTHING) {
|
|
|
|
CP = PtoOpAdjust(CP);
|
|
|
|
ENV = PtoLocAdjust(ENV);
|
|
|
|
ASP = PtoLocAdjust(ASP);
|
|
|
|
H = PtoGloAdjust(H);
|
|
|
|
B = (choiceptr)PtoLocAdjust(CellPtr(B));
|
|
|
|
TR = PtoTRAdjust(TR);
|
|
|
|
P = PtoOpAdjust(P);
|
|
|
|
HB = PtoLocAdjust(HB);
|
|
|
|
YENV = PtoLocAdjust(YENV);
|
|
|
|
S = PtoGloAdjust(S);
|
2005-08-23 22:28:22 +01:00
|
|
|
if (EX)
|
|
|
|
EX = AbsAppl(PtoGloAdjust(RepAppl(EX)));
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef COROUTINING
|
|
|
|
DelayedVars = AbsAppl(PtoGloAdjust(RepAppl(DelayedVars)));
|
|
|
|
#ifdef MULTI_ASSIGNMENT_VARIABLES
|
|
|
|
WokenGoals = AbsAppl(PtoGloAdjust(RepAppl(WokenGoals)));
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
recompute_mask(DBRef dbr)
|
|
|
|
{
|
|
|
|
if (dbr->Flags & DBNoVars) {
|
2003-08-27 14:37:10 +01:00
|
|
|
dbr->Mask = Yap_EvalMasks((Term) dbr->DBT.Entry, &(dbr->Key));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (dbr->Flags & DBComplex) {
|
|
|
|
/* This is quite nasty, we want to recalculate the mask but
|
|
|
|
we don't want to rebuild the whole term. We'll just build whatever we
|
|
|
|
need to recompute the mask.
|
|
|
|
*/
|
2002-06-04 19:21:55 +01:00
|
|
|
CELL *x = (CELL *)HeapTop, *tp;
|
|
|
|
unsigned int Arity, i;
|
|
|
|
Term out;
|
2003-08-27 14:37:10 +01:00
|
|
|
char *tbase = CharP(dbr->DBT.Contents-1);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2003-08-27 14:37:10 +01:00
|
|
|
if (IsPairTerm(dbr->DBT.Entry)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-06-04 19:21:55 +01:00
|
|
|
out = AbsPair(x);
|
|
|
|
Arity = 2;
|
2003-08-27 14:37:10 +01:00
|
|
|
tp = (CELL *)(tbase + (CELL) RepPair(dbr->DBT.Entry));
|
2002-06-04 19:21:55 +01:00
|
|
|
} else {
|
|
|
|
Functor f;
|
|
|
|
|
2003-08-27 14:37:10 +01:00
|
|
|
tp = (CELL *)(tbase + (CELL) RepAppl(dbr->DBT.Entry));
|
2002-06-04 19:21:55 +01:00
|
|
|
f = (Functor)(*tp++);
|
|
|
|
out = AbsAppl(x);
|
|
|
|
Arity = ArityOfFunctor(f);
|
|
|
|
*x++ = (CELL)f;
|
|
|
|
if (Arity > 3) Arity = 3;
|
|
|
|
}
|
|
|
|
for (i = 0; i < Arity; i++) {
|
|
|
|
register Term tw = *tp++;
|
|
|
|
if (IsVarTerm(tw)) {
|
|
|
|
RESET_VARIABLE(x);
|
|
|
|
} else if (IsApplTerm(tw)) {
|
|
|
|
/* just fetch the functor from where it is in the data-base.
|
|
|
|
This guarantees we have access to references and friends. */
|
|
|
|
CELL offset = (CELL)RepAppl(tw);
|
2003-08-27 14:37:10 +01:00
|
|
|
if (offset > dbr->DBT.NOfCells*sizeof(CELL))
|
2002-06-04 19:21:55 +01:00
|
|
|
*x = tw;
|
|
|
|
else
|
|
|
|
*x = AbsAppl((CELL *)(tbase + offset));
|
|
|
|
} else if (IsAtomicTerm(tw)) {
|
|
|
|
*x = tw;
|
|
|
|
} else if (IsPairTerm(tw)) {
|
|
|
|
*x = AbsPair(x);
|
|
|
|
}
|
|
|
|
x++;
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
dbr->Mask = Yap_EvalMasks(out, &(dbr->Key));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HASH_SHIFT 6
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is used to make an hash table correct, after displacing its elements,
|
|
|
|
* HCEnd should point to an area of free space, usually in the heap. The
|
|
|
|
* routine is very dependent on the hash function used, and it destroys the
|
|
|
|
* previous "hit" order
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
rehash(CELL *oldcode, int NOfE, int KindOfEntries)
|
|
|
|
{
|
|
|
|
register CELL *savep, *basep;
|
|
|
|
CELL *oldp = oldcode;
|
|
|
|
int TableSize = NOfE - 1, NOfEntries;
|
|
|
|
register int i;
|
|
|
|
int hash;
|
|
|
|
CELL WorkTerm, failplace = 0;
|
|
|
|
CELL *Base = oldcode;
|
|
|
|
|
|
|
|
if (HDiff == 0)
|
|
|
|
return;
|
|
|
|
basep = H;
|
|
|
|
if (H + (NOfE*2) > ASP) {
|
|
|
|
basep = (CELL *)TR;
|
2002-11-18 18:18:05 +00:00
|
|
|
if (basep + (NOfE*2) > (CELL *)Yap_TrailTop) {
|
2004-12-28 22:20:37 +00:00
|
|
|
if (!Yap_growtrail((ADDR)(basep + (NOfE*2))-Yap_TrailTop, TRUE)) {
|
2004-11-19 22:08:43 +00:00
|
|
|
Yap_Error(OUT_OF_TRAIL_ERROR, TermNil,
|
2001-04-09 20:54:03 +01:00
|
|
|
"not enough space to restore hash tables for indexing");
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_exit(1);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < NOfE; ++i) {
|
|
|
|
if (*oldp == 0) {
|
|
|
|
failplace = oldp[1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
oldp += 2;
|
|
|
|
}
|
|
|
|
savep = basep;
|
|
|
|
oldp = oldcode;
|
|
|
|
for (i = 0; i < NOfE; ++i) {
|
|
|
|
if (*oldp != 0) {
|
|
|
|
savep[0] = oldp[0];
|
|
|
|
savep[1] = oldp[1];
|
|
|
|
oldp[0] = 0;
|
|
|
|
oldp[1] = failplace;
|
|
|
|
savep += 2;
|
|
|
|
}
|
|
|
|
oldp += 2;
|
|
|
|
}
|
|
|
|
NOfEntries = (savep - basep)/2;
|
|
|
|
savep = basep;
|
|
|
|
for (i = 0; i < NOfEntries; ++i) {
|
|
|
|
register Int d;
|
|
|
|
CELL *hentry;
|
|
|
|
|
|
|
|
WorkTerm = savep[i*2];
|
|
|
|
hash = (Unsigned(WorkTerm) >> HASH_SHIFT) & TableSize;
|
|
|
|
hentry = Base + hash * 2;
|
|
|
|
d = TableSize & (Unsigned(WorkTerm) | 1);
|
|
|
|
while (*hentry) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
#ifdef CLASHES
|
|
|
|
++clashes;
|
|
|
|
#endif /* CLASHES */
|
|
|
|
#endif /* DEBUG */
|
|
|
|
hash = (hash + d) & TableSize;
|
|
|
|
hentry = Base + hash * 2;
|
|
|
|
}
|
|
|
|
hentry[0] = WorkTerm;
|
|
|
|
hentry[1] = savep[i*2+1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-04 19:21:55 +01:00
|
|
|
#include "rheap.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/* restore the atom entries which are invisible for the user */
|
|
|
|
static void
|
|
|
|
RestoreForeignCodeStructure(void)
|
|
|
|
{
|
|
|
|
ForeignObj *f_code;
|
|
|
|
|
|
|
|
if (ForeignCodeLoaded != NULL)
|
|
|
|
ForeignCodeLoaded = (void *)AddrAdjust((ADDR)ForeignCodeLoaded);
|
|
|
|
f_code = ForeignCodeLoaded;
|
|
|
|
while (f_code != NULL) {
|
|
|
|
StringList objs, libs;
|
|
|
|
if (f_code->objs != NULL)
|
|
|
|
f_code->objs = (StringList)AddrAdjust((ADDR)f_code->objs);
|
|
|
|
objs = f_code->objs;
|
|
|
|
while (objs != NULL) {
|
|
|
|
if (objs->next != NULL)
|
|
|
|
objs->next = (StringList)AddrAdjust((ADDR)objs->next);
|
|
|
|
if (objs->s != NULL)
|
|
|
|
objs->s = (char *)AddrAdjust((ADDR)objs->s);
|
|
|
|
objs = objs->next;
|
|
|
|
}
|
|
|
|
if (f_code->libs != NULL)
|
|
|
|
f_code->libs = (StringList)AddrAdjust((ADDR)f_code->libs);
|
|
|
|
libs = f_code->libs;
|
|
|
|
while (libs != NULL) {
|
|
|
|
if (libs->next != NULL)
|
|
|
|
libs->next = (StringList)AddrAdjust((ADDR)libs->next);
|
|
|
|
if (libs->s != NULL)
|
|
|
|
libs->s = (char *)AddrAdjust((ADDR)libs->s);
|
|
|
|
libs = libs->next;
|
|
|
|
}
|
|
|
|
if (f_code->f != NULL)
|
|
|
|
f_code->f = (char *)AddrAdjust((ADDR)f_code->f);
|
|
|
|
if (f_code->next != NULL)
|
|
|
|
f_code->next = (ForeignObj *)AddrAdjust((ADDR)f_code->f);
|
|
|
|
f_code = f_code->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-28 17:26:00 +01:00
|
|
|
/* restore the atom entries which are invisible for the user */
|
|
|
|
static void
|
|
|
|
RestoreIOStructures(void)
|
|
|
|
{
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitStdStreams();
|
2002-05-28 17:26:00 +01:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
|
|
|
RestoreFreeSpace(void)
|
|
|
|
{
|
2004-12-07 06:01:55 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
Yap_av = (struct malloc_state *)AddrAdjust((ADDR)Yap_av);
|
2005-11-23 03:01:33 +00:00
|
|
|
Yap_RestoreDLMalloc();
|
|
|
|
if (AuxSp != NULL)
|
|
|
|
AuxSp = PtoHeapCellAdjust(AuxSp);
|
|
|
|
if (AuxTop != NULL)
|
|
|
|
AuxTop = AddrAdjust(AuxTop);
|
2004-12-07 06:01:55 +00:00
|
|
|
#else
|
|
|
|
/* restores the list of free space, with its curious structure */
|
2005-11-23 03:01:33 +00:00
|
|
|
BlockHeader *bpt, *bsz;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
if (FreeBlocks != NULL)
|
|
|
|
FreeBlocks = BlockAdjust(FreeBlocks);
|
|
|
|
bpt = FreeBlocks;
|
2004-10-28 21:12:23 +01:00
|
|
|
if (AuxSp != NULL)
|
|
|
|
AuxSp = CellPtoHeapAdjust(AuxSp);
|
|
|
|
if (AuxTop != NULL)
|
|
|
|
AuxTop = AddrAdjust(AuxTop);
|
2001-04-09 20:54:03 +01:00
|
|
|
while (bpt != NULL) {
|
|
|
|
if (bpt->b_next != NULL) {
|
|
|
|
bsz = bpt->b_next = BlockAdjust(bpt->b_next);
|
|
|
|
while (bsz != NULL) {
|
|
|
|
if (bsz->b_next_size != NULL)
|
|
|
|
bsz->b_next_size = BlockAdjust(bsz->b_next_size);
|
|
|
|
if (bsz->b_next != NULL)
|
|
|
|
bsz->b_next = BlockAdjust(bsz->b_next);
|
|
|
|
bsz = bsz->b_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bpt->b_next_size != NULL)
|
|
|
|
bpt->b_next_size = BlockAdjust(bpt->b_next_size);
|
|
|
|
bpt = bpt->b_next_size;
|
|
|
|
}
|
|
|
|
*((YAP_SEG_SIZE *) HeapTop) = InUseFlag;
|
2004-12-07 06:01:55 +00:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2007-02-18 00:26:36 +00:00
|
|
|
static void
|
|
|
|
RestoreAtomList(Atom atm)
|
2002-06-04 19:21:55 +01:00
|
|
|
{
|
|
|
|
AtomEntry *at;
|
|
|
|
|
|
|
|
at = RepAtom(atm);
|
|
|
|
if (EndOfPAEntr(at))
|
|
|
|
return;
|
|
|
|
do {
|
2008-05-12 23:31:37 +01:00
|
|
|
RestoreAtom(at);
|
|
|
|
at = RepAtom(at->NextOfAE);
|
|
|
|
} while (!EndOfPAEntr(at));
|
2002-06-04 19:21:55 +01:00
|
|
|
}
|
|
|
|
|
2007-02-18 00:26:36 +00:00
|
|
|
|
2007-09-29 00:18:17 +01:00
|
|
|
static void
|
|
|
|
RestoreHashPreds(void)
|
|
|
|
{
|
2008-04-01 15:09:43 +01:00
|
|
|
UInt size = PredHashTableSize;
|
|
|
|
int malloced = FALSE;
|
|
|
|
PredEntry **np;
|
2007-09-29 00:18:17 +01:00
|
|
|
UInt i;
|
2008-04-01 15:09:43 +01:00
|
|
|
PredEntry **oldp = PredHash = (PredEntry **)AddrAdjust((ADDR)PredHash);
|
2007-09-29 00:18:17 +01:00
|
|
|
|
2008-04-01 15:09:43 +01:00
|
|
|
np = (PredEntry **) Yap_AllocAtomSpace(sizeof(PredEntry **)*size);
|
2007-09-29 00:18:17 +01:00
|
|
|
if (!np) {
|
2008-04-01 15:09:43 +01:00
|
|
|
if (!(np = (PredEntry **) malloc(sizeof(PredEntry **)*size))) {
|
|
|
|
Yap_Error(FATAL_ERROR,TermNil,"Could not allocate space for pred table");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
malloced = TRUE;
|
2007-09-29 00:18:17 +01:00
|
|
|
}
|
2008-04-01 15:09:43 +01:00
|
|
|
for (i = 0; i < size; i++) {
|
2007-09-29 00:18:17 +01:00
|
|
|
np[i] = NULL;
|
|
|
|
}
|
|
|
|
for (i = 0; i < PredHashTableSize; i++) {
|
2008-04-01 09:42:46 +01:00
|
|
|
PredEntry *p = oldp[i];
|
2007-09-29 00:18:17 +01:00
|
|
|
|
2008-04-01 09:42:46 +01:00
|
|
|
if (p)
|
|
|
|
p = PredEntryAdjust(p);
|
2007-09-29 00:18:17 +01:00
|
|
|
while (p) {
|
2008-04-01 09:42:46 +01:00
|
|
|
Prop nextp;
|
2007-09-29 00:18:17 +01:00
|
|
|
UInt hsh;
|
|
|
|
|
2008-04-01 09:42:46 +01:00
|
|
|
if (p->NextOfPE)
|
|
|
|
p->NextOfPE = PropAdjust(p->NextOfPE);
|
|
|
|
nextp = p->NextOfPE;
|
2007-09-29 00:18:17 +01:00
|
|
|
CleanCode(p);
|
2008-04-01 15:09:43 +01:00
|
|
|
hsh = PRED_HASH(p->FunctorOfPred, p->ModuleOfPred, size);
|
2007-09-29 00:18:17 +01:00
|
|
|
p->NextOfPE = AbsPredProp(np[hsh]);
|
|
|
|
np[hsh] = p;
|
|
|
|
p = RepPredProp(nextp);
|
|
|
|
}
|
|
|
|
}
|
2008-04-01 15:09:43 +01:00
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
PredHash[i] = np[i];
|
|
|
|
}
|
|
|
|
if (malloced)
|
|
|
|
free((ADDR)np);
|
|
|
|
else
|
|
|
|
Yap_FreeAtomSpace((ADDR)np);
|
2007-09-29 00:18:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/*
|
|
|
|
* This is the really tough part, to restore the whole of the heap
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
restore_heap(void)
|
|
|
|
{
|
|
|
|
AtomHashEntry *HashPtr = HashChain;
|
|
|
|
register int i;
|
2003-10-28 01:16:03 +00:00
|
|
|
for (i = 0; i < AtomHashTableSize; ++i) {
|
2007-02-18 00:26:36 +00:00
|
|
|
HashPtr->Entry = AtomAdjust(HashPtr->Entry);
|
|
|
|
RestoreAtomList(HashPtr->Entry);
|
2006-11-27 17:42:03 +00:00
|
|
|
HashPtr++;
|
|
|
|
}
|
|
|
|
HashPtr = WideHashChain;
|
|
|
|
for (i = 0; i < WideAtomHashTableSize; ++i) {
|
2007-02-18 00:26:36 +00:00
|
|
|
HashPtr->Entry = AtomAdjust(HashPtr->Entry);
|
|
|
|
RestoreAtomList(HashPtr->Entry);
|
2001-04-09 20:54:03 +01:00
|
|
|
HashPtr++;
|
|
|
|
}
|
2007-02-18 00:26:36 +00:00
|
|
|
INVISIBLECHAIN.Entry = AtomAdjust(INVISIBLECHAIN.Entry);
|
|
|
|
RestoreAtomList(INVISIBLECHAIN.Entry);
|
2008-05-12 23:31:37 +01:00
|
|
|
RestoreAtom(RepAtom(AtomFoundVar));
|
|
|
|
RestoreAtom(RepAtom(AtomFreeTerm));
|
2007-09-29 00:18:17 +01:00
|
|
|
RestoreHashPreds();
|
2001-04-09 20:54:03 +01:00
|
|
|
RestoreForeignCodeStructure();
|
2002-05-28 17:26:00 +01:00
|
|
|
RestoreIOStructures();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_RESTORE3
|
|
|
|
static void
|
|
|
|
ShowEntries(pp)
|
|
|
|
PropEntry *pp;
|
|
|
|
{
|
|
|
|
while (!EndOfPAEntr(pp)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
fprintf(Yap_stderr,"Estou a ver a prop %x em %x\n", pp->KindOfPE, pp);
|
2001-04-09 20:54:03 +01:00
|
|
|
pp = RepProp(pp->NextOfPE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ShowAtoms()
|
|
|
|
{
|
|
|
|
AtomHashEntry *HashPtr = HashChain;
|
|
|
|
register int i;
|
2003-10-28 01:16:03 +00:00
|
|
|
for (i = 0; i < AtomHashTableSize; ++i) {
|
2001-04-09 20:54:03 +01:00
|
|
|
if (HashPtr->Entry != NIL) {
|
|
|
|
AtomEntry *at;
|
|
|
|
at = RepAtom(HashPtr->Entry);
|
|
|
|
do {
|
2002-11-18 18:18:05 +00:00
|
|
|
fprintf(Yap_stderr,"Passei ao %s em %x\n", at->StrOfAE, at);
|
2001-10-30 16:42:05 +00:00
|
|
|
ShowEntries(RepProp(at->PropsOfAE));
|
2001-04-09 20:54:03 +01:00
|
|
|
} while (!EndOfPAEntr(at = RepAtom(at->NextOfAE)));
|
|
|
|
}
|
|
|
|
HashPtr++;
|
|
|
|
}
|
2006-11-27 17:42:03 +00:00
|
|
|
HashPtr = WideHashChain;
|
|
|
|
for (i = 0; i < WideAtomHashTableSize; ++i) {
|
|
|
|
if (HashPtr->Entry != NIL) {
|
|
|
|
AtomEntry *at;
|
|
|
|
at = RepAtom(HashPtr->Entry);
|
|
|
|
do {
|
|
|
|
fprintf(Yap_stderr,"Passei ao %s em %x\n", at->StrOfAE, at);
|
|
|
|
ShowEntries(RepProp(at->PropsOfAE));
|
|
|
|
} while (!EndOfPAEntr(at = RepAtom(at->NextOfAE)));
|
|
|
|
}
|
|
|
|
HashPtr++;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* DEBUG_RESTORE3 */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2002-10-30 17:27:19 +00:00
|
|
|
static int
|
|
|
|
commit_to_saved_state(char *s, CELL *Astate, CELL *ATrail, CELL *AStack, CELL *AHeap) {
|
2001-04-09 20:54:03 +01:00
|
|
|
int mode;
|
|
|
|
|
2002-10-30 17:27:19 +00:00
|
|
|
if ((mode = check_header(Astate,ATrail,AStack,AHeap)) == FAIL_RESTORE)
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FAIL_RESTORE);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = BootMode;
|
|
|
|
if (Yap_HeapBase) {
|
2008-03-13 18:41:52 +00:00
|
|
|
if (!yap_flags[HALT_AFTER_CONSULT_FLAG] && !yap_flags[QUIET_MODE_FLAG]) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_TrueFileName(s,Yap_FileNameBuf2, YAP_FILENAME_MAX);
|
2004-06-23 18:24:20 +01:00
|
|
|
fprintf(stderr, "%% Restoring file %s\n", Yap_FileNameBuf2);
|
2002-10-30 17:27:19 +00:00
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_CloseStreams(TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#ifdef DEBUG_RESTORE4
|
|
|
|
/*
|
|
|
|
* This should be another file, like the log file
|
|
|
|
*/
|
2002-11-18 18:18:05 +00:00
|
|
|
errout = Yap_stderr;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
return(mode);
|
|
|
|
}
|
|
|
|
|
2002-10-30 17:27:19 +00:00
|
|
|
static void
|
|
|
|
cat_file_name(char *s, char *prefix, char *name, unsigned int max_length)
|
|
|
|
{
|
|
|
|
strncpy(s, prefix, max_length);
|
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
|
|
|
strncat(s,"\\", max_length);
|
|
|
|
#else
|
|
|
|
strncat(s,"/", max_length);
|
|
|
|
#endif
|
|
|
|
strncat(s, name, max_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-11-11 17:38:10 +00:00
|
|
|
OpenRestore(char *inpf, char *YapLibDir, CELL *Astate, CELL *ATrail, CELL *AStack, CELL *AHeap)
|
2002-10-30 17:27:19 +00:00
|
|
|
{
|
|
|
|
int mode = FAIL_RESTORE;
|
|
|
|
|
2007-10-29 22:48:54 +00:00
|
|
|
// Yap_ErrorMessage = NULL;
|
2008-03-27 00:41:33 +00:00
|
|
|
if (inpf == NULL) {
|
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
|
|
|
if (!(inpf = Yap_RegistryGetString("startup")))
|
|
|
|
#endif
|
|
|
|
inpf = StartUpFile;
|
|
|
|
}
|
2005-03-02 18:35:49 +00:00
|
|
|
#if __simplescalar__
|
|
|
|
/* does not implement getcwd */
|
|
|
|
strncpy(Yap_FileNameBuf,yap_pwd,YAP_FILENAME_MAX);
|
|
|
|
#elif HAVE_GETCWD
|
|
|
|
if (getcwd (Yap_FileNameBuf, YAP_FILENAME_MAX) == NULL)
|
|
|
|
Yap_FileNameBuf[0] = '\0';
|
|
|
|
#else
|
|
|
|
if (getwd (Yap_FileNameBuf) == NULL)
|
|
|
|
Yap_FileNameBuf[0] = '\0';
|
|
|
|
#endif
|
|
|
|
strncat(Yap_FileNameBuf, "/", YAP_FILENAME_MAX);
|
|
|
|
strncat(Yap_FileNameBuf, inpf, YAP_FILENAME_MAX);
|
2002-11-11 17:38:10 +00:00
|
|
|
if (inpf != NULL && (splfild = open_file(inpf, O_RDONLY)) > 0) {
|
2002-12-10 19:08:25 +00:00
|
|
|
if ((mode = commit_to_saved_state(inpf,Astate,ATrail,AStack,AHeap)) != FAIL_RESTORE) {
|
|
|
|
Yap_ErrorMessage = NULL;
|
2005-03-02 18:35:49 +00:00
|
|
|
return mode;
|
2002-12-10 19:08:25 +00:00
|
|
|
}
|
2002-10-30 17:27:19 +00:00
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
if (!Yap_dir_separator(inpf[0]) && !Yap_volume_header(inpf)) {
|
2002-10-30 17:27:19 +00:00
|
|
|
/*
|
|
|
|
we have a relative path for the file, try to do somewhat better
|
|
|
|
using YAPLIBDIR or friends.
|
|
|
|
*/
|
|
|
|
if (YapLibDir != NULL) {
|
2002-11-18 18:18:05 +00:00
|
|
|
cat_file_name(Yap_FileNameBuf, Yap_LibDir, inpf, YAP_FILENAME_MAX);
|
|
|
|
if ((splfild = open_file(Yap_FileNameBuf, O_RDONLY)) > 0) {
|
2002-12-10 19:08:25 +00:00
|
|
|
if ((mode = commit_to_saved_state(Yap_FileNameBuf,Astate,ATrail,AStack,AHeap)) != FAIL_RESTORE) {
|
|
|
|
Yap_ErrorMessage = NULL;
|
2005-03-02 18:35:49 +00:00
|
|
|
return mode;
|
2002-12-10 19:08:25 +00:00
|
|
|
}
|
2002-10-30 17:27:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#if HAVE_GETENV
|
|
|
|
{
|
|
|
|
char *yap_env = getenv("YAPLIBDIR");
|
|
|
|
if (yap_env != NULL) {
|
2002-11-18 18:18:05 +00:00
|
|
|
cat_file_name(Yap_FileNameBuf, yap_env, inpf, YAP_FILENAME_MAX);
|
|
|
|
if ((splfild = open_file(Yap_FileNameBuf, O_RDONLY)) > 0) {
|
2002-12-10 19:08:25 +00:00
|
|
|
if ((mode = commit_to_saved_state(Yap_FileNameBuf,Astate,ATrail,AStack,AHeap)) != FAIL_RESTORE) {
|
|
|
|
Yap_ErrorMessage = NULL;
|
2005-03-02 18:35:49 +00:00
|
|
|
return mode;
|
2002-12-10 19:08:25 +00:00
|
|
|
}
|
2002-10-30 17:27:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (LIB_DIR != NULL) {
|
2002-11-18 18:18:05 +00:00
|
|
|
cat_file_name(Yap_FileNameBuf, LIB_DIR, inpf, YAP_FILENAME_MAX);
|
|
|
|
if ((splfild = open_file(Yap_FileNameBuf, O_RDONLY)) > 0) {
|
2002-12-10 19:08:25 +00:00
|
|
|
if ((mode = commit_to_saved_state(Yap_FileNameBuf,Astate,ATrail,AStack,AHeap)) != FAIL_RESTORE) {
|
|
|
|
Yap_ErrorMessage = NULL;
|
2002-10-30 17:27:19 +00:00
|
|
|
return(mode);
|
2002-12-10 19:08:25 +00:00
|
|
|
}
|
2002-10-30 17:27:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-08-30 02:06:30 +01:00
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
|
|
|
{
|
|
|
|
DWORD fatts;
|
|
|
|
int buflen;
|
|
|
|
char *pt;
|
|
|
|
|
|
|
|
/* try to get it from current executable */
|
|
|
|
if ((fatts = GetFileAttributes(Yap_FileNameBuf)) == 0xFFFFFFFFL ||
|
|
|
|
!(fatts & FILE_ATTRIBUTE_DIRECTORY)) {
|
|
|
|
/* couldn't find it where it was supposed to be,
|
|
|
|
let's try using the executable */
|
|
|
|
if (!GetModuleFileNameEx( GetCurrentProcess(), NULL, Yap_FileNameBuf, YAP_FILENAME_MAX)) {
|
|
|
|
/* do nothing */
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
buflen = strlen(Yap_FileNameBuf);
|
|
|
|
pt = Yap_FileNameBuf+strlen(Yap_FileNameBuf);
|
|
|
|
while (*--pt != '\\') {
|
|
|
|
/* skip executable */
|
|
|
|
if (pt == Yap_FileNameBuf) {
|
|
|
|
/* do nothing */
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (*--pt != '\\') {
|
|
|
|
/* skip parent directory "bin\\" */
|
|
|
|
if (pt == Yap_FileNameBuf) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* now, this is a possible location for the ROOT_DIR, let's look for a share directory here */
|
|
|
|
pt[1] = '\0';
|
|
|
|
strncat(Yap_FileNameBuf,"lib/Yap/startup",YAP_FILENAME_MAX);
|
|
|
|
}
|
|
|
|
if ((splfild = open_file(Yap_FileNameBuf, O_RDONLY)) > 0) {
|
|
|
|
if ((mode = commit_to_saved_state(Yap_FileNameBuf,Astate,ATrail,AStack,AHeap)) != FAIL_RESTORE) {
|
|
|
|
Yap_ErrorMessage = NULL;
|
|
|
|
return(mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end:
|
|
|
|
#endif
|
|
|
|
/* try to open from current directory */
|
2003-01-08 16:45:35 +00:00
|
|
|
/* could not open file */
|
|
|
|
if (Yap_ErrorMessage == NULL) {
|
2005-03-02 18:38:20 +00:00
|
|
|
#if __simplescalar__
|
|
|
|
/* does not implement getcwd */
|
|
|
|
strncpy(Yap_FileNameBuf,yap_pwd,YAP_FILENAME_MAX);
|
|
|
|
#elif HAVE_GETCWD
|
|
|
|
if (getcwd (Yap_FileNameBuf, YAP_FILENAME_MAX) == NULL)
|
|
|
|
Yap_FileNameBuf[0] = '\0';
|
|
|
|
#else
|
|
|
|
if (getwd (Yap_FileNameBuf) == NULL)
|
|
|
|
Yap_FileNameBuf[0] = '\0';
|
|
|
|
#endif
|
|
|
|
strncat(Yap_FileNameBuf, "/", YAP_FILENAME_MAX);
|
|
|
|
strncat(Yap_FileNameBuf, inpf, YAP_FILENAME_MAX);
|
2005-03-02 18:35:49 +00:00
|
|
|
do_system_error(PERMISSION_ERROR_OPEN_SOURCE_SINK,"could not open saved state");
|
2003-01-08 16:45:35 +00:00
|
|
|
}
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2002-10-30 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
|
|
|
CloseRestore(void)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_RESTORE3
|
|
|
|
ShowAtoms();
|
|
|
|
#endif
|
|
|
|
close_file();
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = UserMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2006-05-16 19:37:31 +01:00
|
|
|
#if !defined(_WIN32)
|
2001-04-09 20:54:03 +01:00
|
|
|
static int
|
|
|
|
check_opcodes(OPCODE old_ops[])
|
|
|
|
{
|
|
|
|
#if USE_THREADED_CODE
|
|
|
|
int have_shifted = FALSE;
|
|
|
|
op_numbers op = _Ystop;
|
|
|
|
for (op = _Ystop; op < _std_top; op++) {
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_opcode(op) != old_ops[op]) {
|
2001-04-09 20:54:03 +01:00
|
|
|
have_shifted = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-05-16 19:37:31 +01:00
|
|
|
return have_shifted;
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2006-05-16 19:37:31 +01:00
|
|
|
/* be conservative */
|
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
}
|
2006-05-16 19:37:31 +01:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
static void
|
2004-02-06 02:26:23 +00:00
|
|
|
RestoreHeap(OPCODE old_ops[])
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2002-11-18 18:18:05 +00:00
|
|
|
int heap_moved = (OldHeapBase != Yap_HeapBase), opcodes_moved;
|
2004-07-15 16:47:08 +01:00
|
|
|
Term mod = CurrentModule;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2004-07-15 16:47:08 +01:00
|
|
|
CurrentModule = PROLOG_MODULE;
|
2006-05-16 19:37:31 +01:00
|
|
|
#if defined(_WIN32)
|
|
|
|
/* It seems that under WIN32 opcodes may not have moved but the
|
|
|
|
remaining code may have bmoved */
|
|
|
|
opcodes_moved = TRUE;
|
|
|
|
#else
|
2001-04-09 20:54:03 +01:00
|
|
|
opcodes_moved = check_opcodes(old_ops);
|
2006-05-16 19:37:31 +01:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
/* opcodes_moved has side-effects and should be tried first */
|
2005-11-23 03:01:33 +00:00
|
|
|
if (heap_moved) {
|
2008-05-12 15:04:23 +01:00
|
|
|
opcodes_moved = TRUE;
|
2005-11-23 03:01:33 +00:00
|
|
|
RestoreFreeSpace();
|
|
|
|
}
|
2004-11-23 21:17:47 +00:00
|
|
|
if (heap_moved || opcodes_moved) {
|
2001-04-09 20:54:03 +01:00
|
|
|
restore_heap();
|
|
|
|
}
|
|
|
|
/* This must be done after restore_heap */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitAbsmi();
|
2004-02-06 02:26:23 +00:00
|
|
|
if (opcodes_moved) {
|
|
|
|
Yap_InitCPreds();
|
|
|
|
Yap_InitBackCPreds();
|
|
|
|
}
|
|
|
|
if (!(Yap_ReInitConstExps() &&
|
|
|
|
Yap_ReInitUnaryExps() &&
|
|
|
|
Yap_ReInitBinaryExps())) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR, TermNil, "arithmetic operator not in saved state");
|
2004-02-06 02:26:23 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef DEBUG_RESTORE1
|
2002-11-11 17:38:10 +00:00
|
|
|
fprintf(errout, "phase 1 done\n");
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2004-07-15 16:47:08 +01:00
|
|
|
CurrentModule = mod;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called to know about the parameters of the last saved
|
|
|
|
* state
|
|
|
|
*/
|
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_SavedInfo(char *FileName, char *YapLibDir, CELL *ATrail, CELL *AStack, CELL *AHeap)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2002-10-30 17:27:19 +00:00
|
|
|
CELL MyTrail, MyStack, MyHeap, MyState;
|
|
|
|
int mode;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-10-30 17:27:19 +00:00
|
|
|
mode = OpenRestore(FileName, YapLibDir, &MyState, &MyTrail, &MyStack, &MyHeap);
|
|
|
|
if (mode == FAIL_RESTORE) {
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-03-02 18:35:49 +00:00
|
|
|
close_file();
|
2002-10-30 17:27:19 +00:00
|
|
|
if (! *AHeap)
|
|
|
|
*AHeap = MyHeap / 1024;
|
|
|
|
if (mode != DO_ONLY_CODE && *AStack)
|
|
|
|
*AStack = MyStack / 1024;
|
|
|
|
if (mode != DO_ONLY_CODE && *ATrail)
|
|
|
|
*ATrail = MyTrail / 1024;
|
|
|
|
return (MyState);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
UnmarkTrEntries(void)
|
|
|
|
{
|
|
|
|
CELL entry, *Entries;
|
|
|
|
|
|
|
|
/* initialise a choice point */
|
|
|
|
B = (choiceptr)LCL0;
|
|
|
|
B--;
|
2002-05-14 19:22:42 +01:00
|
|
|
B->cp_ap = NOCODE;
|
2002-11-18 18:18:05 +00:00
|
|
|
Entries = (CELL *)Yap_TrailBase;
|
2001-12-17 18:31:11 +00:00
|
|
|
while ((entry = *Entries++) != (CELL)NULL) {
|
2008-04-01 09:42:46 +01:00
|
|
|
if (!IsVarTerm(entry)) {
|
|
|
|
if(IsPairTerm(entry)) {
|
|
|
|
CELL *ent = CellPtoHeapAdjust(RepPair(entry));
|
|
|
|
register CELL flags;
|
|
|
|
|
|
|
|
flags = *ent;
|
|
|
|
ResetFlag(InUseMask, flags);
|
|
|
|
*ent = flags;
|
|
|
|
if (FlagOn((DirtyMask|ErasedMask), flags)) {
|
|
|
|
if (FlagOn(DBClMask, flags)) {
|
|
|
|
Yap_ErDBE(DBStructFlagsToDBStruct(ent));
|
|
|
|
} else {
|
|
|
|
if (flags & LogUpdMask) {
|
|
|
|
if (flags & IndexMask) {
|
|
|
|
if (FlagOn(ErasedMask, flags))
|
|
|
|
Yap_ErLogUpdIndex(ClauseFlagsToLogUpdIndex(ent));
|
|
|
|
else
|
|
|
|
Yap_CleanUpIndex(ClauseFlagsToLogUpdIndex(ent));
|
|
|
|
} else {
|
|
|
|
Yap_ErLogUpdCl(ClauseFlagsToLogUpdClause(ent));
|
|
|
|
}
|
2004-09-27 21:45:04 +01:00
|
|
|
} else {
|
2008-04-01 09:42:46 +01:00
|
|
|
Yap_ErCl(ClauseFlagsToDynamicClause(ent));
|
2004-09-27 21:45:04 +01:00
|
|
|
}
|
2003-04-30 18:46:05 +01:00
|
|
|
}
|
2001-12-17 18:31:11 +00:00
|
|
|
}
|
2004-09-27 21:45:04 +01:00
|
|
|
#ifdef MULTI_ASSIGNMENT_VARIABLES
|
2008-04-01 09:42:46 +01:00
|
|
|
} else /* if (IsApplTerm(d1)) */ {
|
|
|
|
Entries += 2;
|
2004-09-27 21:45:04 +01:00
|
|
|
#endif
|
2008-04-01 09:42:46 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
B = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int in_limbo = FALSE;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called when wanting only to restore the heap and
|
|
|
|
* associated registers
|
|
|
|
*/
|
2002-11-11 17:38:10 +00:00
|
|
|
static int
|
2002-10-30 17:27:19 +00:00
|
|
|
Restore(char *s, char *lib_dir)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
int restore_mode;
|
|
|
|
|
|
|
|
OPCODE old_ops[_std_top+1];
|
2002-10-30 17:27:19 +00:00
|
|
|
CELL MyTrail, MyStack, MyHeap, MyState;
|
2008-03-22 23:35:00 +00:00
|
|
|
|
2002-10-30 17:27:19 +00:00
|
|
|
if ((restore_mode = OpenRestore(s, lib_dir, &MyState, &MyTrail, &MyStack, &MyHeap)) == FAIL_RESTORE)
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ShutdownLoadForeign();
|
2001-04-09 20:54:03 +01:00
|
|
|
in_limbo = TRUE;
|
2005-03-02 18:35:49 +00:00
|
|
|
if (get_coded(restore_mode, old_ops) < 0)
|
|
|
|
return FAIL_RESTORE;
|
2001-04-09 20:54:03 +01:00
|
|
|
restore_regs(restore_mode);
|
|
|
|
in_limbo = FALSE;
|
|
|
|
/*#endif*/
|
2004-02-06 02:26:23 +00:00
|
|
|
RestoreHeap(old_ops);
|
2001-04-09 20:54:03 +01:00
|
|
|
switch(restore_mode) {
|
|
|
|
case DO_EVERYTHING:
|
2002-11-18 18:18:05 +00:00
|
|
|
if (OldHeapBase != Yap_HeapBase ||
|
2001-04-09 20:54:03 +01:00
|
|
|
OldLCL0 != LCL0 ||
|
2002-11-18 18:18:05 +00:00
|
|
|
OldGlobalBase != (CELL *)Yap_GlobalBase ||
|
|
|
|
OldTrailBase != Yap_TrailBase) {
|
|
|
|
Yap_AdjustStacksAndTrail();
|
2001-04-09 20:54:03 +01:00
|
|
|
if (which_save == 2) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_AdjustRegs(2);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_AdjustRegs(1);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifdef DEBUG_RESTORE2
|
2002-11-11 17:38:10 +00:00
|
|
|
fprintf(errout, "phase 2 done\n");
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DO_ONLY_CODE:
|
|
|
|
UnmarkTrEntries();
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitYaamRegs();
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
}
|
2004-05-13 21:54:58 +01:00
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ReOpenLoadForeign();
|
|
|
|
Yap_InitPlIO();
|
2001-04-09 20:54:03 +01:00
|
|
|
/* reset time */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ReInitWallTime();
|
2006-08-30 02:06:30 +01:00
|
|
|
Yap_InitSysPath();
|
2001-04-09 20:54:03 +01:00
|
|
|
CloseRestore();
|
|
|
|
if (which_save == 2) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_unify(ARG2, MkIntTerm(0));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-03-02 18:35:49 +00:00
|
|
|
return restore_mode;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Restore(char *s, char *lib_dir)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
return Restore(s, lib_dir);
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Int
|
|
|
|
p_restore(void)
|
|
|
|
{
|
|
|
|
int mode;
|
|
|
|
|
|
|
|
Term t1 = Deref(ARG1);
|
2002-10-22 04:45:24 +01:00
|
|
|
#if defined(YAPOR) || defined(THREADS)
|
2002-10-21 23:52:36 +01:00
|
|
|
if (NOfThreads != 1) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR,TermNil,"cannot perform save: more than a worker/thread running");
|
2002-10-21 23:52:36 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
2002-10-22 04:45:24 +01:00
|
|
|
#endif
|
2002-11-18 18:18:05 +00:00
|
|
|
if (!Yap_GetName(Yap_FileNameBuf, YAP_FILENAME_MAX, t1)) {
|
|
|
|
Yap_Error(TYPE_ERROR_LIST,t1,"restore/1");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
if ((mode = Restore(Yap_FileNameBuf, NULL)) == DO_ONLY_CODE) {
|
2001-04-09 20:54:03 +01:00
|
|
|
#if PUSH_REGS
|
2002-11-18 18:18:05 +00:00
|
|
|
restore_absmi_regs(&Yap_standard_regs);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
/* back to the top level we go */
|
2002-11-18 18:18:05 +00:00
|
|
|
siglongjmp(Yap_RestartEnv,3);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
return(mode != FAIL_RESTORE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitSavePreds(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$save", 1, p_save, SafePredFlag|SyncPredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$save", 2, p_save2, SafePredFlag|SyncPredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$save_program", 1, p_save_program, SafePredFlag|SyncPredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$restore", 1, p_restore, SyncPredFlag|HiddenPredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|