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
|
|
|
|
|
2011-08-31 21:59:30 +01:00
|
|
|
#include "SWI-Stream.h"
|
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
|
|
|
|
2009-05-02 20:06:24 +01:00
|
|
|
static char StartUpFile[] = "startup.yss";
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
static char end_msg[256] ="*** End of YAP saved state *****";
|
|
|
|
|
2011-02-15 19:24:15 +00:00
|
|
|
/* SWI IO, must be restarted after restore */
|
|
|
|
void initIO(void);
|
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));
|
2010-03-22 16:56:58 +00:00
|
|
|
STATIC_PROTO(Int mywrite, (int, char *, Int));
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(int open_file, (char *, int));
|
2010-03-22 16:56:58 +00:00
|
|
|
STATIC_PROTO(int close_file, (void));
|
|
|
|
STATIC_PROTO(Int putout, (CELL));
|
|
|
|
STATIC_PROTO(Int putcellptr, (CELL *));
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(CELL get_cell, (void));
|
|
|
|
STATIC_PROTO(CELL *get_cellptr, ( /* CELL * */ void));
|
2011-03-07 16:02:55 +00:00
|
|
|
STATIC_PROTO(int put_info, (int, int CACHE_TYPE));
|
|
|
|
STATIC_PROTO(int save_regs, (int CACHE_TYPE));
|
2010-03-22 16:56:58 +00:00
|
|
|
STATIC_PROTO(int save_code_info, (void));
|
|
|
|
STATIC_PROTO(int save_heap, (void));
|
2011-03-07 16:02:55 +00:00
|
|
|
STATIC_PROTO(int save_stacks, (int CACHE_TYPE));
|
2010-03-22 16:56:58 +00:00
|
|
|
STATIC_PROTO(int save_crc, (void));
|
2011-03-07 16:02:55 +00:00
|
|
|
STATIC_PROTO(Int do_save, (int CACHE_TYPE));
|
|
|
|
STATIC_PROTO(Int p_save2, ( CACHE_TYPE1 ));
|
|
|
|
STATIC_PROTO(Int p_save_program, ( CACHE_TYPE1 ));
|
|
|
|
STATIC_PROTO(int check_header, (CELL *, CELL *, CELL *, CELL * CACHE_TYPE));
|
|
|
|
STATIC_PROTO(int get_heap_info, (CACHE_TYPE1));
|
|
|
|
STATIC_PROTO(int get_regs, (int CACHE_TYPE));
|
2005-03-02 18:35:49 +00:00
|
|
|
STATIC_PROTO(int get_insts, (OPCODE []));
|
|
|
|
STATIC_PROTO(int get_hash, (void));
|
2011-03-07 16:02:55 +00:00
|
|
|
STATIC_PROTO(int CopyCode, ( CACHE_TYPE1 ));
|
|
|
|
STATIC_PROTO(int CopyStacks, ( CACHE_TYPE1 ));
|
|
|
|
STATIC_PROTO(int get_coded, (int, OPCODE [] CACHE_TYPE));
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(void restore_codes, (void));
|
2011-03-07 16:02:55 +00:00
|
|
|
STATIC_PROTO(void RestoreDB, (DBEntry * CACHE_TYPE));
|
|
|
|
STATIC_PROTO(void RestoreDBTerm, (DBTerm *, int CACHE_TYPE));
|
|
|
|
STATIC_PROTO(void CleanClauses, (yamop *, yamop *,PredEntry * CACHE_TYPE));
|
|
|
|
STATIC_PROTO(void rehash, (CELL *, int, int CACHE_TYPE));
|
|
|
|
STATIC_PROTO(void CleanCode, (PredEntry * CACHE_TYPE));
|
|
|
|
STATIC_PROTO(void RestoreEntries, (PropEntry *, int CACHE_TYPE));
|
|
|
|
STATIC_PROTO(void RestoreFreeSpace, ( CACHE_TYPE1 ));
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(void restore_heap, (void));
|
|
|
|
#ifdef DEBUG_RESTORE3
|
|
|
|
STATIC_PROTO(void ShowAtoms, (void));
|
|
|
|
STATIC_PROTO(void ShowEntries, (PropEntry *));
|
|
|
|
#endif
|
2011-08-31 21:59:30 +01:00
|
|
|
STATIC_PROTO(int OpenRestore, (char *, char *, CELL *, CELL *, CELL *, CELL *, IOSTREAM **));
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(void CloseRestore, (void));
|
2009-11-23 16:01:41 +00:00
|
|
|
#ifndef _WIN32
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(int check_opcodes, (OPCODE []));
|
2009-11-23 16:01:41 +00:00
|
|
|
#endif
|
2011-03-07 16:02:55 +00:00
|
|
|
STATIC_PROTO(void RestoreHeap, (OPCODE [] CACHE_TYPE));
|
|
|
|
STATIC_PROTO(Int p_restore, ( CACHE_TYPE1 ));
|
|
|
|
STATIC_PROTO(void restore_heap_regs, ( CACHE_TYPE1 ));
|
|
|
|
STATIC_PROTO(void restore_regs, (int CACHE_TYPE));
|
2001-04-09 20:54:03 +01:00
|
|
|
#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 */
|
|
|
|
|
2010-03-22 16:56:58 +00:00
|
|
|
static Int
|
2005-03-02 18:35:49 +00:00
|
|
|
do_system_error(yap_error_number etype, const char *msg)
|
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2005-03-02 18:35:49 +00:00
|
|
|
#if HAVE_SNPRINTF
|
|
|
|
#if HAVE_STRERROR
|
2011-05-23 16:19:47 +01:00
|
|
|
snprintf(LOCAL_ErrorSay,MAX_ERROR_MSG_SIZE,"%s (%s when reading %s)", msg, strerror(errno), LOCAL_FileNameBuf);
|
2005-03-02 18:35:49 +00:00
|
|
|
#else
|
2011-05-23 16:19:47 +01:00
|
|
|
snprintf(LOCAL_ErrorSay,MAX_ERROR_MSG_SIZE,"%s, (system error %d when reading %s)",msg,errno,LOCAL_FileNameBuf);
|
2005-03-02 18:35:49 +00:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#if HAVE_STRERROR
|
2011-05-23 16:19:47 +01:00
|
|
|
sprintf(LOCAL_ErrorSay,"%s, (%s when reading %s)",msg,strerror(errno),LOCAL_FileNameBuf);
|
2005-03-02 18:35:49 +00:00
|
|
|
#else
|
2011-05-23 16:19:47 +01:00
|
|
|
sprintf(LOCAL_ErrorSay,"%s, (system error %d when reading %s)",msg,errno,LOCAL_FileNameBuf);
|
2005-03-02 18:35:49 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
|
|
|
LOCAL_Error_TYPE = etype;
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
inline static
|
2005-03-02 18:35:49 +00:00
|
|
|
int myread(int fd, char *buffer, Int len) {
|
2010-03-22 16:56:58 +00:00
|
|
|
ssize_t nread;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
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
|
2010-03-22 16:56:58 +00:00
|
|
|
Int
|
|
|
|
mywrite(int fd, char *buff, Int len) {
|
|
|
|
ssize_t nwritten;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
while (len > 0) {
|
2010-03-22 16:56:58 +00:00
|
|
|
nwritten = write(fd, buff, (size_t)len);
|
2005-03-02 18:35:49 +00:00
|
|
|
if (nwritten < 0) {
|
2010-03-22 16:56:58 +00:00
|
|
|
return do_system_error(SYSTEM_ERROR,"bad write on saved state");
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
buff += nwritten;
|
|
|
|
len -= nwritten;
|
|
|
|
}
|
2010-03-22 16:56:58 +00:00
|
|
|
return len;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
2011-05-25 16:40:36 +01:00
|
|
|
#define errout GLOBAL_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
|
|
|
}
|
|
|
|
|
2010-03-22 16:56:58 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
close_file(void)
|
|
|
|
{
|
2002-10-30 17:27:19 +00:00
|
|
|
if (splfild == 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return 0;
|
|
|
|
if (close(splfild) < 0)
|
|
|
|
return do_system_error(SYSTEM_ERROR,"bad close on saved state");
|
2001-04-09 20:54:03 +01:00
|
|
|
splfild = 0;
|
2010-03-22 16:56:58 +00:00
|
|
|
return 1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* stores a cell in a file */
|
2010-03-22 16:56:58 +00:00
|
|
|
static Int
|
2001-04-09 20:54:03 +01:00
|
|
|
putout(CELL l)
|
|
|
|
{
|
2010-03-22 16:56:58 +00:00
|
|
|
return mywrite(splfild, (char *) &l, sizeof(CELL));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* stores a pointer to a cell in a file */
|
2010-03-22 16:56:58 +00:00
|
|
|
static Int
|
2001-04-09 20:54:03 +01:00
|
|
|
putcellptr(CELL *l)
|
|
|
|
{
|
2010-03-22 16:56:58 +00:00
|
|
|
return 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;
|
|
|
|
|
2010-03-22 16:56:58 +00:00
|
|
|
if (myread(splfild, (char *) &l, Unsigned(sizeof(CELLPOINTER))) < 0)
|
|
|
|
return NULL;
|
2002-10-30 17:27:19 +00:00
|
|
|
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
|
|
|
|
*/
|
2010-03-22 16:56:58 +00:00
|
|
|
static int
|
2011-03-07 16:02:55 +00:00
|
|
|
put_info(int info, int mode USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
char msg[256];
|
|
|
|
|
2009-10-23 09:09:16 +01:00
|
|
|
sprintf(msg, "#!/bin/sh\nexec_dir=${YAPBINDIR:-%s}\nexec $exec_dir/yap $0 \"$@\"\n%cYAP-%s", YAP_BINDIR, 1, YAP_SVERSION);
|
2010-03-22 16:56:58 +00:00
|
|
|
if (mywrite(splfild, msg, strlen(msg) + 1))
|
|
|
|
return -1;
|
|
|
|
if (putout(Unsigned(info)) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* say whether we just saved the heap or everything */
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putout(mode) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* current state of stacks, to be used by SavedInfo */
|
|
|
|
/* space available in heap area */
|
2011-05-23 16:19:47 +01:00
|
|
|
if (putout(Unsigned(LOCAL_GlobalBase)-Unsigned(Yap_HeapBase)) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* space available for stacks */
|
2011-05-23 16:19:47 +01:00
|
|
|
if (putout(Unsigned(LOCAL_LocalBase)-Unsigned(LOCAL_GlobalBase)) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* space available for trail */
|
2011-05-23 16:19:47 +01:00
|
|
|
if (putout(Unsigned(LOCAL_TrailTop)-Unsigned(LOCAL_TrailBase)) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Space used in heap area */
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putout(Unsigned(HeapTop)-Unsigned(Yap_HeapBase)) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Space used for local stack */
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putout(Unsigned(LCL0)-Unsigned(ASP)) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Space used for global stack */
|
2011-05-23 16:19:47 +01:00
|
|
|
if (putout(Unsigned(H) - Unsigned(LOCAL_GlobalBase)) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Space used for trail */
|
2011-05-23 16:19:47 +01:00
|
|
|
if (putout(Unsigned(TR) - Unsigned(LOCAL_TrailBase)) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
|
|
|
return 0;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 16:56:58 +00:00
|
|
|
static int
|
2011-03-07 16:02:55 +00:00
|
|
|
save_regs(int mode USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
/* save all registers */
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putout((CELL)compile_arrays) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (mode == DO_EVERYTHING) {
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putcellptr((CELL *)CP) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putcellptr(ENV) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putcellptr(ASP) < 0)
|
|
|
|
return -1;
|
|
|
|
/* if (putout((CELL)N) < 0)
|
|
|
|
return -1; */
|
|
|
|
if (putcellptr(H0) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putcellptr(LCL0) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putcellptr(H) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putcellptr(HB) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putcellptr((CELL *)B) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putcellptr((CELL *)TR) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putcellptr(YENV) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putcellptr(S) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putcellptr((CELL *)P) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putout(CreepFlag) < 0)
|
|
|
|
return -1;
|
2010-07-27 23:29:55 +01:00
|
|
|
if (putcellptr((CELL *)EX) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
2011-03-30 15:32:59 +01:00
|
|
|
#if defined(YAPOR_SBA) || defined(TABLING)
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putcellptr(H_FZ) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putcellptr((CELL *)B_FZ) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putcellptr((CELL *)TR_FZ) < 0)
|
|
|
|
return -1;
|
2011-03-30 15:32:59 +01:00
|
|
|
#endif /* YAPOR_SBA || TABLING */
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putout(CurrentModule) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (mode == DO_EVERYTHING) {
|
|
|
|
#ifdef COROUTINING
|
2011-05-04 10:11:41 +01:00
|
|
|
if (putout(LOCAL_WokenGoals) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
#ifdef DEPTH_LIMIT
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putout(DEPTH) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2011-05-04 10:11:41 +01:00
|
|
|
if (putout(LOCAL_GcGeneration) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
2011-05-04 10:11:41 +01:00
|
|
|
if (putout(LOCAL_GcPhase) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
2011-05-04 10:11:41 +01:00
|
|
|
if (putout(LOCAL_GcCurrentPhase) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
/* The operand base */
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putcellptr(CellPtr(XREGS)) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putout(which_save) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Now start by saving the code */
|
|
|
|
/* the heap boundaries */
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putcellptr(CellPtr(Yap_HeapBase)) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putcellptr(CellPtr(HeapTop)) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* and the space it ocuppies */
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putout(Unsigned(Yap_heap_regs->heap_used)) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Then the start of the free code */
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putcellptr(CellPtr(FreeBlocks)) < 0)
|
|
|
|
return -1;
|
2010-04-22 12:08:51 +01:00
|
|
|
if (putcellptr(CellPtr(AuxBase)) < 0)
|
|
|
|
return -1;
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putcellptr(AuxSp) < 0)
|
|
|
|
return -1;
|
|
|
|
if (putcellptr(CellPtr(AuxTop)) < 0)
|
|
|
|
return -1;
|
2011-05-04 10:11:41 +01:00
|
|
|
if (putcellptr(CellPtr(LOCAL_ScratchPad.ptr)) < 0)
|
2010-04-22 12:08:51 +01:00
|
|
|
return -1;
|
2011-05-04 10:11:41 +01:00
|
|
|
if (putout(LOCAL_ScratchPad.sz) < 0)
|
2010-04-22 12:08:51 +01:00
|
|
|
return -1;
|
2011-05-04 10:11:41 +01:00
|
|
|
if (putout(LOCAL_ScratchPad.msz) < 0)
|
2010-04-22 12:08:51 +01:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (mode == DO_EVERYTHING) {
|
|
|
|
/* put the old trail base, just in case it moves again */
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putout(ARG1) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (which_save == 2) {
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putout(ARG2) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
if (putcellptr(CellPtr(LOCAL_TrailBase)) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2010-03-22 16:56:58 +00:00
|
|
|
return 0;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 16:56:58 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
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);
|
2010-03-22 16:56:58 +00:00
|
|
|
if (mywrite(splfild, (char *)my_ops, sizeof(OPCODE)*(_std_top+1)) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
/* and the current character codes */
|
2010-03-22 16:56:58 +00:00
|
|
|
if (mywrite(splfild, Yap_chtype, NUMBER_OF_CHARS) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 16:56:58 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
save_heap(void)
|
|
|
|
{
|
2010-01-22 22:29:07 +00:00
|
|
|
#ifdef USE_SYSTEM_MALLOC
|
2010-03-30 09:16:07 +01:00
|
|
|
return -1;
|
2010-01-22 22:29:07 +00:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
int j;
|
|
|
|
/* Then save the whole heap */
|
2010-03-21 22:12:42 +00:00
|
|
|
Yap_ResetConsultStack();
|
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 */
|
2010-03-22 16:56:58 +00:00
|
|
|
if (mywrite(splfild, (char *) Yap_HeapBase, j) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 16:56:58 +00:00
|
|
|
static int
|
2011-03-07 16:02:55 +00:00
|
|
|
save_stacks(int mode USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case DO_EVERYTHING:
|
|
|
|
/* Now, go and save the state */
|
|
|
|
/* Save the local stack */
|
|
|
|
j = Unsigned(LCL0) - Unsigned(ASP);
|
2010-03-22 16:56:58 +00:00
|
|
|
if (mywrite(splfild, (char *) ASP, j) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Save the global stack */
|
2011-05-23 16:19:47 +01:00
|
|
|
j = Unsigned(H) - Unsigned(LOCAL_GlobalBase);
|
|
|
|
if (mywrite(splfild, (char *) LOCAL_GlobalBase, j) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Save the trail */
|
2011-05-23 16:19:47 +01:00
|
|
|
j = Unsigned(TR) - Unsigned(LOCAL_TrailBase);
|
|
|
|
if (mywrite(splfild, (char *) LOCAL_TrailBase, j) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
case DO_ONLY_CODE:
|
|
|
|
{
|
|
|
|
tr_fr_ptr tr_ptr = TR;
|
2011-05-23 16:19:47 +01:00
|
|
|
while (tr_ptr != (tr_fr_ptr)LOCAL_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);
|
2010-03-22 16:56:58 +00:00
|
|
|
if (d1 < (CELL *)HeapTop) {
|
|
|
|
if (putout(val) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2001-12-17 18:31:11 +00:00
|
|
|
} else if (IsPairTerm(val)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
CELL *d1 = RepPair(val);
|
2010-03-22 16:56:58 +00:00
|
|
|
if (d1 < (CELL *)HeapTop) {
|
|
|
|
if (putout(val) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
tr_ptr--;
|
|
|
|
}
|
|
|
|
}
|
2010-03-22 16:56:58 +00:00
|
|
|
if (putcellptr(NULL) < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-03-22 16:56:58 +00:00
|
|
|
return 0;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 16:56:58 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
save_crc(void)
|
|
|
|
{
|
|
|
|
/* Save a CRC */
|
2010-03-22 16:56:58 +00:00
|
|
|
return mywrite(splfild, end_msg, 256);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
do_save(int mode USES_REGS) {
|
2011-03-28 22:55:28 +01:00
|
|
|
extern void Scleanup(void);
|
2001-04-09 20:54:03 +01:00
|
|
|
Term t1 = Deref(ARG1);
|
2006-05-19 15:31:32 +01:00
|
|
|
|
|
|
|
if (Yap_HoleSize) {
|
2011-05-23 16:19:47 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR,MkAtomTerm(Yap_LookupAtom(LOCAL_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;
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
if (!Yap_GetName(LOCAL_FileNameBuf, YAP_FILENAME_MAX, t1)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
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
|
|
|
}
|
2011-03-28 22:55:28 +01:00
|
|
|
Scleanup();
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_CloseStreams(TRUE);
|
2011-05-23 16:19:47 +01:00
|
|
|
if ((splfild = open_file(LOCAL_FileNameBuf, O_WRONLY | O_CREAT)) < 0) {
|
|
|
|
Yap_Error(SYSTEM_ERROR,MkAtomTerm(Yap_LookupAtom(LOCAL_FileNameBuf)),
|
2001-04-09 20:54:03 +01:00
|
|
|
"restore/1, open(%s)", strerror(errno));
|
|
|
|
return(FALSE);
|
|
|
|
}
|
2011-03-07 16:02:55 +00:00
|
|
|
if (put_info(FullSaved, mode PASS_REGS) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
2011-03-07 16:02:55 +00:00
|
|
|
if (save_regs(mode PASS_REGS) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
|
|
|
if (save_code_info() < 0)
|
|
|
|
return -1;
|
|
|
|
if (save_heap() < 0)
|
|
|
|
return -1;
|
2011-03-07 16:02:55 +00:00
|
|
|
if (save_stacks(mode PASS_REGS) < 0)
|
2010-03-22 16:56:58 +00:00
|
|
|
return -1;
|
|
|
|
if (save_crc() < 0)
|
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
close_file();
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Saves a complete prolog environment */
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_save2( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2010-11-25 16:33:25 +00:00
|
|
|
Int res;
|
|
|
|
|
2010-11-29 18:08:11 +00:00
|
|
|
Term t;
|
2011-04-29 14:59:17 +01:00
|
|
|
#ifdef YAPOR
|
2011-05-10 11:47:18 +01:00
|
|
|
if (GLOBAL_number_workers != 1) {
|
2010-10-15 01:15:45 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR,TermNil,
|
|
|
|
"cannot perform save: more than a worker/thread running");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
2011-04-29 14:59:17 +01:00
|
|
|
#endif /* YAPOR */
|
|
|
|
#ifdef THREADS
|
2011-05-10 10:06:51 +01:00
|
|
|
if (GLOBAL_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);
|
|
|
|
}
|
2011-04-29 14:59:17 +01:00
|
|
|
#endif /* THREADS */
|
2010-11-29 18:08:11 +00:00
|
|
|
/* avoid double saves */
|
|
|
|
if (IsNonVarTerm(t = Deref(ARG2)))
|
|
|
|
return TRUE;
|
|
|
|
if (!Yap_unify(ARG2,MkIntTerm(1)))
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
which_save = 2;
|
2011-03-07 16:02:55 +00:00
|
|
|
Yap_StartSlots( PASS_REGS1 );
|
|
|
|
res = do_save(DO_EVERYTHING PASS_REGS);
|
|
|
|
Yap_CloseSlots( PASS_REGS1 );
|
2010-11-29 18:08:11 +00:00
|
|
|
return res;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Just save the program, not the stacks */
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_save_program( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
which_save = 0;
|
2011-03-07 16:02:55 +00:00
|
|
|
return do_save(DO_ONLY_CODE PASS_REGS);
|
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
|
2011-03-07 16:02:55 +00:00
|
|
|
check_header(CELL *info, CELL *ATrail, CELL *AStack, CELL *AHeap USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2010-11-07 19:32:40 +00:00
|
|
|
char pp[256];
|
2001-04-09 20:54:03 +01:00
|
|
|
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 */
|
2009-10-23 09:09:16 +01:00
|
|
|
sprintf(msg, "YAP-%s", YAP_SVERSION);
|
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;
|
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
if (strcmp(pp, msg) != 0) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
|
|
|
strncpy(LOCAL_ErrorMessage, "saved state ", MAX_ERROR_MSG_SIZE);
|
|
|
|
strncat(LOCAL_ErrorMessage, LOCAL_FileNameBuf, MAX_ERROR_MSG_SIZE);
|
|
|
|
strncat(LOCAL_ErrorMessage, " failed to match version ID", MAX_ERROR_MSG_SIZE);
|
|
|
|
LOCAL_Error_TYPE = CONSISTENCY_ERROR;
|
2005-03-02 18:35:49 +00:00
|
|
|
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();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_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();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_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();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage) {
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
|
|
|
}
|
2002-10-30 17:27:19 +00:00
|
|
|
*AStack = get_header_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage) {
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
|
|
|
}
|
2002-10-30 17:27:19 +00:00
|
|
|
*ATrail = get_header_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage) {
|
2005-03-02 18:35:49 +00:00
|
|
|
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();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_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();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2002-10-30 17:27:19 +00:00
|
|
|
gb_size=get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2011-05-23 16:19:47 +01:00
|
|
|
if (Yap_HeapBase != NULL && lc_size+gb_size > Unsigned(LOCAL_LocalBase) - Unsigned(LOCAL_GlobalBase)) {
|
|
|
|
if (LOCAL_ErrorMessage != NULL)
|
|
|
|
LOCAL_ErrorMessage = "could not allocate enough stack space";
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
if (Yap_HeapBase != NULL && (tr_size = get_cell()) > Unsigned(LOCAL_TrailTop) - Unsigned(LOCAL_TrailBase)) {
|
|
|
|
if (LOCAL_ErrorMessage != NULL)
|
|
|
|
LOCAL_ErrorMessage = "could not allocate enough trail space";
|
2005-03-02 18:35:49 +00:00
|
|
|
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();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2002-10-30 17:27:19 +00:00
|
|
|
get_header_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2002-10-30 17:27:19 +00:00
|
|
|
get_header_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_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
|
2011-03-07 16:02:55 +00:00
|
|
|
get_heap_info(USES_REGS1)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_OldHeapBase = (ADDR) get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_OldHeapTop = (ADDR) get_cellptr();
|
2010-10-15 01:15:45 +01:00
|
|
|
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
OldHeapUsed = (Int) get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
FreeBlocks = (BlockHeader *) get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2010-04-22 12:08:51 +01:00
|
|
|
return -1;
|
|
|
|
AuxBase = (ADDR)get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2004-10-28 21:12:23 +01:00
|
|
|
AuxSp = get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2004-10-28 21:12:23 +01:00
|
|
|
AuxTop = (ADDR)get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2010-04-22 12:08:51 +01:00
|
|
|
return -1;
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_ScratchPad.ptr = (ADDR)get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2010-04-22 12:08:51 +01:00
|
|
|
return -1;
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_ScratchPad.sz = get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2010-04-22 12:08:51 +01:00
|
|
|
return -1;
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_ScratchPad.msz = get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_HDiff = Unsigned(Yap_HeapBase) - Unsigned(LOCAL_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
|
2011-03-07 16:02:55 +00:00
|
|
|
get_regs(int flag USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-05-23 16:19:47 +01:00
|
|
|
CELL *NewGlobalBase = (CELL *)LOCAL_GlobalBase;
|
2001-04-09 20:54:03 +01:00
|
|
|
CELL *NewLCL0 = LCL0;
|
|
|
|
CELL *OldXREGS;
|
|
|
|
|
|
|
|
/* Get regs */
|
|
|
|
compile_arrays = (int)get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (flag == DO_EVERYTHING) {
|
|
|
|
CP = (yamop *)get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
ENV = get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
ASP = get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* N = get_cell(); */
|
|
|
|
H0 = get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
LCL0 = get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
H = get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
HB = get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
B = (choiceptr)get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
TR = (tr_fr_ptr)get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
YENV = get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
S = get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
CreepFlag = get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2010-07-27 23:29:55 +01:00
|
|
|
EX = (struct DB_TERM *)get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-08-22 05:07:20 +01:00
|
|
|
return -1;
|
2011-03-30 15:32:59 +01:00
|
|
|
#if defined(YAPOR_SBA) || defined(TABLING)
|
2005-10-31 12:27:54 +00:00
|
|
|
H_FZ = get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-10-31 12:27:54 +00:00
|
|
|
return -1;
|
|
|
|
B_FZ = (choiceptr)get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-10-31 12:27:54 +00:00
|
|
|
return -1;
|
|
|
|
TR_FZ = (tr_fr_ptr)get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-10-31 12:27:54 +00:00
|
|
|
return -1;
|
2011-03-30 15:32:59 +01:00
|
|
|
#endif /* YAPOR_SBA || TABLING */
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-05-14 19:22:42 +01:00
|
|
|
CurrentModule = get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (flag == DO_EVERYTHING) {
|
|
|
|
#ifdef COROUTINING
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_WokenGoals = get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
#ifdef DEPTH_LIMIT
|
|
|
|
DEPTH = get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_GcGeneration = get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-09-21 04:49:33 +01:00
|
|
|
return -1;
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_GcPhase = get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2006-03-06 14:04:57 +00:00
|
|
|
return -1;
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_GcCurrentPhase = get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2006-03-06 14:04:57 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
/* Get the old bases */
|
|
|
|
OldXREGS = get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
which_save = get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_XDiff = (CELL)XREGS - (CELL)OldXREGS;
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2011-03-07 16:02:55 +00:00
|
|
|
if (get_heap_info( PASS_REGS1 ) < 0)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (flag == DO_EVERYTHING) {
|
|
|
|
ARG1 = get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (which_save == 2) {
|
|
|
|
ARG2 = get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
/* get old trail base */
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_OldTrailBase = (ADDR)get_cellptr();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Save the old register where we can easily access them */
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_OldASP = ASP;
|
|
|
|
LOCAL_OldLCL0 = LCL0;
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_OldGlobalBase = (CELL *)LOCAL_GlobalBase;
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_OldH = H;
|
|
|
|
LOCAL_OldTR = TR;
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_GDiff = Unsigned(NewGlobalBase) - Unsigned(LOCAL_GlobalBase);
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_GDiff0 = 0;
|
|
|
|
LOCAL_LDiff = Unsigned(NewLCL0) - Unsigned(LCL0);
|
|
|
|
LOCAL_TrDiff = LOCAL_LDiff;
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_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
|
2011-03-07 16:02:55 +00:00
|
|
|
CopyCode( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-05-04 10:11:41 +01:00
|
|
|
if (myread(splfild, (char *) Yap_HeapBase, (Unsigned(LOCAL_OldHeapTop) - Unsigned(LOCAL_OldHeapBase))) < 0) {
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2010-10-15 01:15:45 +01:00
|
|
|
}
|
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
|
2011-03-07 16:02:55 +00:00
|
|
|
CopyStacks( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
Int j;
|
|
|
|
char *NewASP;
|
|
|
|
|
2011-05-04 10:11:41 +01:00
|
|
|
j = Unsigned(LOCAL_OldLCL0) - Unsigned(ASP);
|
|
|
|
NewASP = (char *) (Unsigned(ASP) + (Unsigned(LCL0) - Unsigned(LOCAL_OldLCL0)));
|
2005-03-02 18:35:49 +00:00
|
|
|
if (myread(splfild, (char *) NewASP, j) < 0)
|
|
|
|
return -1;
|
2011-05-04 10:11:41 +01:00
|
|
|
j = Unsigned(H) - Unsigned(LOCAL_OldGlobalBase);
|
2011-05-23 16:19:47 +01:00
|
|
|
if (myread(splfild, (char *) LOCAL_GlobalBase, j) < 0)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2011-05-04 10:11:41 +01:00
|
|
|
j = Unsigned(TR) - Unsigned(LOCAL_OldTrailBase);
|
2011-05-23 16:19:47 +01:00
|
|
|
if (myread(splfild, LOCAL_TrailBase, j))
|
2005-03-02 18:35:49 +00:00
|
|
|
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
|
2011-03-07 16:02:55 +00:00
|
|
|
CopyTrailEntries( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
CELL entry, *Entries;
|
|
|
|
|
2011-05-23 16:19:47 +01:00
|
|
|
Entries = (CELL *)LOCAL_TrailBase;
|
2001-04-09 20:54:03 +01:00
|
|
|
do {
|
|
|
|
*Entries++ = entry = get_cell();
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage)
|
2005-03-02 18:35:49 +00:00
|
|
|
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
|
2011-03-07 16:02:55 +00:00
|
|
|
get_coded(int flag, OPCODE old_ops[] USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
char my_end_msg[256];
|
2010-10-15 01:15:45 +01:00
|
|
|
|
2011-03-07 16:02:55 +00:00
|
|
|
if (get_regs(flag PASS_REGS) < 0)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
|
|
|
if (get_insts(old_ops) < 0)
|
|
|
|
return -1;
|
|
|
|
if (get_hash() < 0)
|
|
|
|
return -1;
|
2011-03-07 16:02:55 +00:00
|
|
|
if (CopyCode( PASS_REGS1 ) < 0)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
switch (flag) {
|
|
|
|
case DO_EVERYTHING:
|
2011-03-07 16:02:55 +00:00
|
|
|
if (CopyStacks( PASS_REGS1 ) < 0)
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
case DO_ONLY_CODE:
|
2011-03-07 16:02:55 +00:00
|
|
|
if (CopyTrailEntries( PASS_REGS1 ) < 0)
|
2005-03-02 18:35:49 +00:00
|
|
|
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) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = "bad trailing CRC in saved state";
|
2005-03-02 18:35:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* restore some heap registers */
|
|
|
|
static void
|
2011-03-07 16:02:55 +00:00
|
|
|
restore_heap_regs( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
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;
|
2011-05-23 16:19:47 +01:00
|
|
|
HeapLim = LOCAL_GlobalBase;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* adjust abstract machine registers */
|
|
|
|
static void
|
2011-03-07 16:02:55 +00:00
|
|
|
restore_regs(int flag USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
restore_heap_regs( PASS_REGS1 );
|
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);
|
2010-07-27 23:29:55 +01:00
|
|
|
if (EX) {
|
|
|
|
EX = DBTermAdjust(EX);
|
2011-03-07 16:02:55 +00:00
|
|
|
RestoreDBTerm(EX, TRUE PASS_REGS);
|
2010-07-27 23:29:55 +01:00
|
|
|
}
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_WokenGoals = AbsAppl(PtoGloAdjust(RepAppl(LOCAL_WokenGoals)));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2011-03-07 16:02:55 +00:00
|
|
|
rehash(CELL *oldcode, int NOfE, int KindOfEntries USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
register CELL *savep, *basep;
|
|
|
|
CELL *oldp = oldcode;
|
|
|
|
int TableSize = NOfE - 1, NOfEntries;
|
|
|
|
register int i;
|
|
|
|
int hash;
|
|
|
|
CELL WorkTerm, failplace = 0;
|
|
|
|
CELL *Base = oldcode;
|
|
|
|
|
2011-05-04 10:11:41 +01:00
|
|
|
if (LOCAL_HDiff == 0)
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
basep = H;
|
|
|
|
if (H + (NOfE*2) > ASP) {
|
|
|
|
basep = (CELL *)TR;
|
2011-05-23 16:19:47 +01:00
|
|
|
if (basep + (NOfE*2) > (CELL *)LOCAL_TrailTop) {
|
|
|
|
if (!Yap_growtrail((ADDR)(basep + (NOfE*2))-LOCAL_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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-03 14:26:56 +01:00
|
|
|
static void
|
|
|
|
RestoreSWIHash(void)
|
|
|
|
{
|
|
|
|
Yap_InitSWIHash();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-04 19:21:55 +01:00
|
|
|
#include "rheap.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
|
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
|
2011-03-07 16:02:55 +00:00
|
|
|
RestoreFreeSpace( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
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();
|
2009-05-23 00:35:24 +01:00
|
|
|
if (AuxSp != NULL) {
|
2011-05-04 10:11:41 +01:00
|
|
|
if (AuxBase < LOCAL_OldHeapBase || AuxBase > LOCAL_OldHeapTop) {
|
2009-05-23 00:35:24 +01:00
|
|
|
AuxSp = NULL;
|
|
|
|
AuxBase = NULL;
|
|
|
|
AuxTop = NULL;
|
|
|
|
} else {
|
|
|
|
AuxSp = PtoHeapCellAdjust(AuxSp);
|
|
|
|
AuxBase = AddrAdjust(AuxBase);
|
|
|
|
AuxTop = AddrAdjust(AuxTop);
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_ScratchPad.ptr = AddrAdjust(LOCAL_ScratchPad.ptr);
|
2009-05-23 00:35:24 +01:00
|
|
|
}
|
|
|
|
}
|
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
|
2011-03-07 16:02:55 +00:00
|
|
|
RestoreAtomList(Atom atm USES_REGS)
|
2002-06-04 19:21:55 +01:00
|
|
|
{
|
|
|
|
AtomEntry *at;
|
|
|
|
|
|
|
|
at = RepAtom(atm);
|
|
|
|
if (EndOfPAEntr(at))
|
|
|
|
return;
|
|
|
|
do {
|
2011-03-07 16:02:55 +00:00
|
|
|
RestoreAtom(at PASS_REGS);
|
2008-05-12 23:31:37 +01:00
|
|
|
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
|
2011-03-07 16:02:55 +00:00
|
|
|
RestoreHashPreds( USES_REGS1 )
|
2007-09-29 00:18:17 +01:00
|
|
|
{
|
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;
|
2009-10-28 13:11:35 +00:00
|
|
|
PredEntry **oldp = 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;
|
2011-03-07 16:02:55 +00:00
|
|
|
CleanCode(p PASS_REGS);
|
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)
|
|
|
|
{
|
2009-10-30 23:59:00 +00:00
|
|
|
restore_codes();
|
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)) {
|
2011-05-25 16:40:36 +01:00
|
|
|
fprintf(GLOBAL_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 {
|
2011-05-25 16:40:36 +01:00
|
|
|
fprintf(GLOBAL_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 {
|
2011-05-25 16:40:36 +01:00
|
|
|
fprintf(GLOBAL_stderr,"Passei ao %s em %x\n", at->StrOfAE, at);
|
2006-11-27 17:42:03 +00:00
|
|
|
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) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
int mode;
|
|
|
|
|
2011-03-07 16:02:55 +00:00
|
|
|
if ((mode = check_header(Astate,ATrail,AStack,AHeap PASS_REGS)) == FAIL_RESTORE)
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FAIL_RESTORE);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = BootMode;
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_HeapBase) {
|
2011-03-28 22:55:28 +01:00
|
|
|
extern void Scleanup(void);
|
2008-03-13 18:41:52 +00:00
|
|
|
if (!yap_flags[HALT_AFTER_CONSULT_FLAG] && !yap_flags[QUIET_MODE_FLAG]) {
|
2011-05-23 16:19:47 +01:00
|
|
|
Yap_TrueFileName(s,LOCAL_FileNameBuf2, YAP_FILENAME_MAX);
|
|
|
|
fprintf(stderr, "%% Restoring file %s\n", LOCAL_FileNameBuf2);
|
2002-10-30 17:27:19 +00:00
|
|
|
}
|
2011-03-28 22:55:28 +01:00
|
|
|
Scleanup();
|
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
|
|
|
|
*/
|
2011-05-25 16:40:36 +01:00
|
|
|
errout = GLOBAL_stderr;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2010-08-17 13:19:17 +01:00
|
|
|
return mode;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
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
|
2008-11-26 09:56:18 +00:00
|
|
|
strncat(s, name, max_length-1);
|
2002-10-30 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 21:59:30 +01:00
|
|
|
static int try_open(char *inpf, CELL *Astate, CELL *ATrail, CELL *AStack, CELL *AHeap, char *buf, IOSTREAM **streamp) {
|
2010-08-17 13:19:17 +01:00
|
|
|
int mode;
|
|
|
|
|
2011-08-31 21:59:30 +01:00
|
|
|
if (streamp) {
|
2011-09-01 14:11:38 +01:00
|
|
|
if ((*streamp = Sopen_file(inpf, "rb"))) {
|
|
|
|
return DO_ONLY_CODE;
|
|
|
|
}
|
|
|
|
return FAIL_RESTORE;
|
2011-08-31 21:59:30 +01:00
|
|
|
}
|
2010-08-17 13:19:17 +01:00
|
|
|
if ((splfild = open_file(inpf, O_RDONLY)) < 0) {
|
|
|
|
return FAIL_RESTORE;
|
|
|
|
}
|
|
|
|
if (buf[0] == '\0')
|
|
|
|
strncpy(buf, inpf, YAP_FILENAME_MAX);
|
|
|
|
if ((mode = commit_to_saved_state(inpf,Astate,ATrail,AStack,AHeap)) != FAIL_RESTORE) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = NULL;
|
2010-08-17 13:19:17 +01:00
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2002-10-30 17:27:19 +00:00
|
|
|
static int
|
2011-08-31 21:59:30 +01:00
|
|
|
OpenRestore(char *inpf, char *YapLibDir, CELL *Astate, CELL *ATrail, CELL *AStack, CELL *AHeap, IOSTREAM **streamp)
|
2002-10-30 17:27:19 +00:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2002-10-30 17:27:19 +00:00
|
|
|
int mode = FAIL_RESTORE;
|
2010-08-17 13:19:17 +01:00
|
|
|
char save_buffer[YAP_FILENAME_MAX+1];
|
2011-08-23 18:26:59 +01:00
|
|
|
|
|
|
|
save_buffer[0] = '\0';
|
2011-05-23 16:19:47 +01:00
|
|
|
// LOCAL_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;
|
|
|
|
}
|
2010-08-17 13:19:17 +01:00
|
|
|
/* careful it starts from the root */
|
|
|
|
if (inpf[0] != '/') {
|
2005-03-02 18:35:49 +00:00
|
|
|
#if __simplescalar__
|
2010-08-17 13:19:17 +01:00
|
|
|
/* does not implement getcwd */
|
2011-05-25 16:40:36 +01:00
|
|
|
strncpy(LOCAL_FileNameBuf,GLOBAL_pwd,YAP_FILENAME_MAX);
|
2005-03-02 18:35:49 +00:00
|
|
|
#elif HAVE_GETCWD
|
2011-05-23 16:19:47 +01:00
|
|
|
if (getcwd (LOCAL_FileNameBuf, YAP_FILENAME_MAX) == NULL)
|
|
|
|
LOCAL_FileNameBuf[0] = '\0';
|
2005-03-02 18:35:49 +00:00
|
|
|
#else
|
2011-05-23 16:19:47 +01:00
|
|
|
if (getwd (LOCAL_FileNameBuf) == NULL)
|
|
|
|
LOCAL_FileNameBuf[0] = '\0';
|
2005-03-02 18:35:49 +00:00
|
|
|
#endif
|
2011-05-23 16:19:47 +01:00
|
|
|
strncat(LOCAL_FileNameBuf, "/", YAP_FILENAME_MAX-1);
|
|
|
|
strncat(LOCAL_FileNameBuf, inpf, YAP_FILENAME_MAX-1);
|
2010-08-17 13:19:17 +01:00
|
|
|
} else {
|
2011-05-23 16:19:47 +01:00
|
|
|
strncat(LOCAL_FileNameBuf, inpf, YAP_FILENAME_MAX-1);
|
2010-08-17 13:19:17 +01:00
|
|
|
}
|
2002-11-11 17:38:10 +00:00
|
|
|
if (inpf != NULL && (splfild = open_file(inpf, O_RDONLY)) > 0) {
|
2011-08-31 21:59:30 +01:00
|
|
|
if ((mode = try_open(inpf,Astate,ATrail,AStack,AHeap,save_buffer,streamp)) != FAIL_RESTORE) {
|
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) {
|
2011-05-23 16:19:47 +01:00
|
|
|
cat_file_name(LOCAL_FileNameBuf, Yap_LibDir, inpf, YAP_FILENAME_MAX);
|
2011-08-31 21:59:30 +01:00
|
|
|
if ((mode = try_open(LOCAL_FileNameBuf,Astate,ATrail,AStack,AHeap,save_buffer,streamp)) != FAIL_RESTORE) {
|
2010-08-17 13:19:17 +01:00
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
} else {
|
2011-08-31 21:59:30 +01:00
|
|
|
if ((mode = try_open(LOCAL_FileNameBuf,Astate,ATrail,AStack,AHeap,save_buffer,streamp)) != FAIL_RESTORE) {
|
2010-08-17 13:19:17 +01:00
|
|
|
return mode;
|
2002-10-30 17:27:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#if HAVE_GETENV
|
|
|
|
{
|
|
|
|
char *yap_env = getenv("YAPLIBDIR");
|
|
|
|
if (yap_env != NULL) {
|
2011-05-23 16:19:47 +01:00
|
|
|
cat_file_name(LOCAL_FileNameBuf, yap_env, inpf, YAP_FILENAME_MAX);
|
2011-08-31 21:59:30 +01:00
|
|
|
if ((mode = try_open(LOCAL_FileNameBuf,Astate,ATrail,AStack,AHeap,save_buffer,streamp)) != FAIL_RESTORE) {
|
2010-08-17 13:19:17 +01:00
|
|
|
return mode;
|
2002-10-30 17:27:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2009-10-23 09:09:16 +01:00
|
|
|
if (YAP_LIBDIR != NULL) {
|
2011-05-23 16:19:47 +01:00
|
|
|
cat_file_name(LOCAL_FileNameBuf, YAP_LIBDIR, inpf, YAP_FILENAME_MAX);
|
|
|
|
if ((splfild = open_file(LOCAL_FileNameBuf, O_RDONLY)) > 0) {
|
2011-08-31 21:59:30 +01:00
|
|
|
if ((mode = try_open(LOCAL_FileNameBuf,Astate,ATrail,AStack,AHeap,save_buffer,streamp)) != FAIL_RESTORE) {
|
2010-08-17 13:19:17 +01: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 */
|
2011-05-23 16:19:47 +01:00
|
|
|
if ((fatts = GetFileAttributes(LOCAL_FileNameBuf)) == 0xFFFFFFFFL ||
|
2006-08-30 02:06:30 +01:00
|
|
|
!(fatts & FILE_ATTRIBUTE_DIRECTORY)) {
|
|
|
|
/* couldn't find it where it was supposed to be,
|
|
|
|
let's try using the executable */
|
2011-05-23 16:19:47 +01:00
|
|
|
if (!GetModuleFileNameEx( GetCurrentProcess(), NULL, LOCAL_FileNameBuf, YAP_FILENAME_MAX)) {
|
2006-08-30 02:06:30 +01:00
|
|
|
/* do nothing */
|
|
|
|
goto end;
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
buflen = strlen(LOCAL_FileNameBuf);
|
|
|
|
pt = LOCAL_FileNameBuf+strlen(LOCAL_FileNameBuf);
|
2006-08-30 02:06:30 +01:00
|
|
|
while (*--pt != '\\') {
|
|
|
|
/* skip executable */
|
2011-05-23 16:19:47 +01:00
|
|
|
if (pt == LOCAL_FileNameBuf) {
|
2006-08-30 02:06:30 +01:00
|
|
|
/* do nothing */
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (*--pt != '\\') {
|
|
|
|
/* skip parent directory "bin\\" */
|
2011-05-23 16:19:47 +01:00
|
|
|
if (pt == LOCAL_FileNameBuf) {
|
2006-08-30 02:06:30 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* now, this is a possible location for the ROOT_DIR, let's look for a share directory here */
|
|
|
|
pt[1] = '\0';
|
2011-05-23 16:19:47 +01:00
|
|
|
strncat(LOCAL_FileNameBuf,"lib/Yap/startup.yss",YAP_FILENAME_MAX);
|
2006-08-30 02:06:30 +01:00
|
|
|
}
|
2011-08-31 21:59:30 +01:00
|
|
|
if ((mode = try_open(LOCAL_FileNameBuf,Astate,ATrail,AStack,AHeap,save_buffer,streamp)) != FAIL_RESTORE) {
|
2010-08-17 13:19:17 +01:00
|
|
|
return mode;
|
2006-08-30 02:06:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end:
|
|
|
|
#endif
|
|
|
|
/* try to open from current directory */
|
2003-01-08 16:45:35 +00:00
|
|
|
/* could not open file */
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage == NULL) {
|
2010-08-17 13:19:17 +01:00
|
|
|
if (save_buffer[0]) {
|
2011-05-23 16:19:47 +01:00
|
|
|
strncpy(LOCAL_FileNameBuf, save_buffer, YAP_FILENAME_MAX-1);
|
2010-08-17 13:19:17 +01:00
|
|
|
do_system_error(PERMISSION_ERROR_OPEN_SOURCE_SINK,"incorrect saved state");
|
|
|
|
} else {
|
2011-05-23 16:19:47 +01:00
|
|
|
strncpy(LOCAL_FileNameBuf, inpf, YAP_FILENAME_MAX-1);
|
2010-08-17 13:19:17 +01: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
|
|
|
}
|
|
|
|
|
2011-08-31 21:59:30 +01:00
|
|
|
IOSTREAM *
|
|
|
|
Yap_OpenRestore(char *inpf, char *YapLibDir)
|
|
|
|
{
|
|
|
|
IOSTREAM *stream = NULL;
|
|
|
|
|
|
|
|
OpenRestore(inpf, YapLibDir, NULL, NULL, NULL, NULL, &stream);
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
|
|
|
CloseRestore(void)
|
|
|
|
{
|
2011-05-25 16:40:36 +01:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef DEBUG_RESTORE3
|
|
|
|
ShowAtoms();
|
|
|
|
#endif
|
|
|
|
close_file();
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_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
|
2011-03-07 16:02:55 +00:00
|
|
|
RestoreHeap(OPCODE old_ops[] USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-05-04 10:11:41 +01:00
|
|
|
int heap_moved = (LOCAL_OldHeapBase != Yap_HeapBase ||
|
|
|
|
LOCAL_XDiff), 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;
|
2011-03-07 16:02:55 +00:00
|
|
|
RestoreFreeSpace( PASS_REGS1 );
|
2005-11-23 03:01:33 +00:00
|
|
|
}
|
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
|
|
|
{
|
2011-08-31 21:59:30 +01:00
|
|
|
return DO_ONLY_CODE;
|
|
|
|
|
2002-10-30 17:27:19 +00:00
|
|
|
CELL MyTrail, MyStack, MyHeap, MyState;
|
|
|
|
int mode;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-08-31 21:59:30 +01:00
|
|
|
mode = OpenRestore(FileName, YapLibDir, &MyState, &MyTrail, &MyStack, &MyHeap, NULL);
|
2002-10-30 17:27:19 +00:00
|
|
|
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
|
2011-03-07 16:02:55 +00:00
|
|
|
UnmarkTrEntries( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
CELL entry, *Entries;
|
|
|
|
|
|
|
|
/* initialise a choice point */
|
|
|
|
B = (choiceptr)LCL0;
|
|
|
|
B--;
|
2002-05-14 19:22:42 +01:00
|
|
|
B->cp_ap = NOCODE;
|
2011-05-23 16:19:47 +01:00
|
|
|
Entries = (CELL *)LOCAL_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;
|
|
|
|
|
2011-01-03 03:16:29 +00:00
|
|
|
/* cleanup any records we had in the saved state. They are now inaccessible */
|
|
|
|
static void
|
|
|
|
FreeRecords(void) {
|
|
|
|
struct record_list *ptr;
|
|
|
|
|
|
|
|
ptr = Yap_Records;
|
|
|
|
Yap_Records = NULL;
|
|
|
|
while (ptr) {
|
|
|
|
struct record_list *optr = ptr;
|
|
|
|
Yap_ReleaseTermFromDB(ptr->dbrecord);
|
|
|
|
ptr = ptr->next_rec;
|
|
|
|
Yap_FreeCodeSpace((void *)optr);
|
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called when wanting only to restore the heap and
|
|
|
|
* associated registers
|
|
|
|
*/
|
2002-11-11 17:38:10 +00:00
|
|
|
static int
|
2011-03-07 16:02:55 +00:00
|
|
|
Restore(char *s, char *lib_dir USES_REGS)
|
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
|
|
|
|
2011-08-31 21:59:30 +01:00
|
|
|
if ((restore_mode = OpenRestore(s, lib_dir, &MyState, &MyTrail, &MyStack, &MyHeap, NULL)) == 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;
|
2011-03-07 16:02:55 +00:00
|
|
|
if (get_coded(restore_mode, old_ops PASS_REGS) < 0)
|
2005-03-02 18:35:49 +00:00
|
|
|
return FAIL_RESTORE;
|
2011-03-07 16:02:55 +00:00
|
|
|
restore_regs(restore_mode PASS_REGS);
|
2001-04-09 20:54:03 +01:00
|
|
|
in_limbo = FALSE;
|
|
|
|
/*#endif*/
|
2011-03-07 16:02:55 +00:00
|
|
|
RestoreHeap(old_ops PASS_REGS);
|
2001-04-09 20:54:03 +01:00
|
|
|
switch(restore_mode) {
|
|
|
|
case DO_EVERYTHING:
|
2011-05-04 10:11:41 +01:00
|
|
|
if (LOCAL_OldHeapBase != Yap_HeapBase ||
|
|
|
|
LOCAL_OldLCL0 != LCL0 ||
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_OldGlobalBase != (CELL *)LOCAL_GlobalBase ||
|
|
|
|
LOCAL_OldTrailBase != LOCAL_TrailBase) {
|
2002-11-18 18:18:05 +00:00
|
|
|
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:
|
2011-03-07 16:02:55 +00:00
|
|
|
UnmarkTrEntries( PASS_REGS1 );
|
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();
|
2011-04-04 14:36:06 +01:00
|
|
|
FreeRecords();
|
|
|
|
/* restart IO */
|
|
|
|
initIO();
|
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();
|
2009-05-23 00:35:24 +01:00
|
|
|
#if USE_DL_MALLOC || USE_SYSTEM_MALLOC
|
|
|
|
if (!AuxSp) {
|
|
|
|
Yap_InitPreAllocCodeSpace();
|
|
|
|
}
|
|
|
|
#endif
|
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
|
2011-08-31 21:59:30 +01:00
|
|
|
Yap_SavedStateRestore(char *s, char *lib_dir)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
|
|
|
return Restore(s, lib_dir PASS_REGS);
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_restore( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
int mode;
|
2010-11-29 18:08:11 +00:00
|
|
|
char s[YAP_FILENAME_MAX+1];
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
Term t1 = Deref(ARG1);
|
2011-04-29 14:59:17 +01:00
|
|
|
#ifdef YAPOR
|
2011-05-10 11:47:18 +01:00
|
|
|
if (GLOBAL_number_workers != 1) {
|
2010-10-15 01:15:45 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR,TermNil,"cannot perform save: more than a worker/thread running");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
2011-04-29 14:59:17 +01:00
|
|
|
#endif /* YAPOR */
|
|
|
|
#ifdef THREADS
|
2011-05-10 10:06:51 +01:00
|
|
|
if (GLOBAL_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);
|
|
|
|
}
|
2011-04-29 14:59:17 +01:00
|
|
|
#endif /* THREADS */
|
2010-11-29 18:08:11 +00:00
|
|
|
if (!Yap_GetName(s, YAP_FILENAME_MAX, t1)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_LIST,t1,"restore/1");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
2011-03-07 16:02:55 +00:00
|
|
|
if ((mode = Restore(s, NULL PASS_REGS)) == 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 */
|
2011-05-23 16:19:47 +01:00
|
|
|
siglongjmp(LOCAL_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
|
|
|
{
|
2010-11-26 18:02:44 +00:00
|
|
|
Yap_InitCPred("$save", 2, p_save2, SyncPredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$save_program", 1, p_save_program, SyncPredFlag|HiddenPredFlag);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$restore", 1, p_restore, SyncPredFlag|HiddenPredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|