home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Vectronix 2
/
VECTRONIX2.iso
/
FILES_01
/
X_PROLOG.LZH
/
X_PROLOG
/
SOURCES
/
IMAGE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-08-13
|
8KB
|
360 lines
/*
* X PROLOG Vers. 2.0
*
*
* Written by : Andreas Toenne
* CS Dept. , IRB
* University of Dortmund, W-Germany
* <at@unido.uucp>
* <....!seismo!unido!at>
* <at@unido.bitnet>
*
* Copyright : This software is copyrighted by Andreas Toenne.
* Permission is granted hereby to copy the entire
* package including this copyright notice without fee.
*
*/
#include <stdio.h>
#include "prolog.h"
#include "error.h"
#include "extern.h"
#define REL(x) ((x) ? (long)(x) - (long)functorsp + 1 : (long)(x))
#define DEREL(x) ((x) ? (long)(x) + (long)functorsp - 1 : (long)(x))
#ifdef VAX
#define lmalloc malloc
#endif
#define EVEN(x) (((long)(x) & ~(1L)) + 2) /* make adress even */
extern char *malloc();
extern char *lmalloc();
/* make the areas relocatible */
void relocate()
{
functor *f;
clause *c;
term *t;
long i;
/* relocate all functors */
f = (functor *)functorsp;
while (f < functornext)
{
f->cp = (char *)REL(f->cp);
f->left = (functor *)REL(f->left);
f->right = (functor *)REL(f->right);
f = (functor *)((long)f + sizeof(functor) + strlen(f->name));
if ((long)f &0x1) /* odd */
f = (functor *)((long)f + 1);
}
/* relocate all clauses */
c = clausesp;
while (c < clausenext)
{
c->next = (clause *)REL(c->next);
if (!ISBUILTIN(c))
{
c->head = (term *)REL(c->head);
c->body = (term *)REL(c->body);
}
c++;
}
/* relocate all prototypes */
t = (term *)protostack;
while (t < protonext)
{
if (ISFREE(t)) /* relocate free list */
{
ARG(t,0) = (term *)REL(ARG(t,0));
t = (term *)((long)t+sizeof(term)+
(t->flags&0xff)*sizeof(term *));
continue;
}
if (ISINT(t) || ISVAR(t)) /* noth to do */
{
t++;
continue;
}
for (i=1; i<=ARITY(t); i++) /* relocate args */
ARG(t,i) = (term *)REL(ARG(t,i));
ARG(t,0) = (term *)REL(ARG(t,0)); /* and functor */
t = (term *)((long)t + sizeof(term) + (i-1)*sizeof(term *));
}
}
/* Save all prolog definitions */
short save_area();
short save_image(name)
char *name;
{
i_header x;
FILE *fp;
short i;
#ifdef ATARI /* need binary mode */
if ((fp = fopen(name, "wb")) == NULL)
#else
if ((fp = fopen(name, "w")) == NULL)
#endif
{
perror("cannot save prolog image");
return(FALSE);
}
relocate(); /* relocate the areas */
strcpy(x.magic, MAGIC);
x.version = IMAGEVERSION;
x.i_type = IMAGETYPE;
x.f_top = (char *)REL(functornext);
x.f_size = (long)functorfull - (long)functorsp;
x.f_tree = (functor *)REL(functors);
x.cl_top = (clause *)REL(clausenext);
x.cl_free = (clause *)REL(clausefree);
x.cl_size = clausefull;
x.p_top = (term *)REL(prototop);
x.p_size = (long)protofull - (long)protostack;
for (i=0; i<MAXARGS; i++)
x.p_free[i] = (term *)REL(protofree[i]);
x.c_size = (long)copyfull - (long)copystack;
x.s_size = (long)stackfull - (long)stack;
x.t_size = trailfull;
for (i=0; i<STDFUNCTORS; i++)
x.stdf[i] = (functor *)REL(stdfunctor[i]);
if (fwrite(&x, sizeof(i_header), 1, fp) != 1)
{
perror("cannot save image header");
exit(0);
}
save_area(fp,functorsp, functornext); /* save functors */
save_area(fp,clausesp, clausesp+clausefull); /* save clauses */
save_area(fp,protostack, protonext); /* save prototypes */
if (fclose(fp))
{
perror("cannot close image");
exit(0);
}
fprintf(stderr, "X Prolog image saved\n");
exit(0);
}
short save_area(fp, from, to)
FILE *fp;
char *from, *to;
{
while (from < to)
{
if (((long)to - (long)from) < 32000)
{
fwrite(from, (unsigned)((long)to-(long)from), 1, fp);
break;
}
else
fwrite(from, 32000, 1, fp);
from += 32000;
}
}
short bisave(args)
term *args[];
{
if (!ISATOM(args[0]))
BIERROR(EBAD);
return(save_image(NAME(args[0])));
}
/*****************************************************************************/
/* make the areas absolut */
void derelocate()
{
functor *f;
clause *c;
term *t;
long i;
/* derelocate all functors */
f = (functor *)functorsp;
while (f < functornext)
{
f->cp = (char *)DEREL(f->cp);
f->left = (functor *)DEREL(f->left);
f->right = (functor *)DEREL(f->right);
f = (functor *)((long)f + sizeof(functor) + strlen(f->name));
if ((long)f &0x1) /* odd */
f = (functor *)((long)f + 1);
}
/* derelocate all clauses */
c = clausesp;
while (c < clausenext)
{
c->next = (clause *)DEREL(c->next);
if (!ISBUILTIN(c))
{
c->head = (term *)DEREL(c->head);
c->body = (term *)DEREL(c->body);
}
c++;
}
/* derelocate all prototypes */
t = (term *)protostack;
while (t < protonext)
{
if (ISFREE(t)) /* derelocate free list */
{
ARG(t,0) = (term *)DEREL(ARG(t, 0));
t = (term *)((long)t+sizeof(term)+
(t->flags&0xff)*sizeof(term *));
continue;
}
if (ISINT(t) || ISVAR(t)) /* noth to do */
{
t++;
continue;
}
ARG(t,0) = (term *)DEREL(ARG(t, 0)); /* derelocate functor */
for (i=1; i<=ARITY(t); i++) /* derelocate args */
ARG(t,i) = (term *)DEREL(ARG(t,i));
t = (term *)((long)t + sizeof(term) + (i-1)*sizeof(term *));
}
}
short read_area();
short read_image(name)
char *name; /* the image name */
{
i_header x;
FILE *fp;
short i;
long l;
#ifdef ATARI /* need binary mode */
if ((fp = fopen(name, "rb")) == NULL)
#else
if ((fp = fopen(name, "r")) == NULL)
#endif
{
perror("cannot read prolog image");
return(FALSE);
}
if (fread(&x, sizeof(i_header), 1, fp) != 1)
{
perror("cannot read image header");
return(FALSE);
}
if (strcmp(x.magic, MAGIC))
{
fprintf(stderr, "garbled prolog image\n");
fclose(fp);
return(FALSE);
}
if (x.version != IMAGEVERSION)
{
fprintf(stderr, "old fashion prolog image\n");
fclose(fp);
return(FALSE);
}
if (x.i_type != IMAGETYPE)
{
fprintf(stderr, "cannot unpack image on this machine\n");
fclose(fp);
return(FALSE);
}
/* header is tested now, lets create the data space */
l = x.f_size+x.cl_size*sizeof(clause)+x.t_size*sizeof(term *)
+x.p_size+x.c_size+x.s_size+20;
if ((functorsp = lmalloc(l)) == NULL)
panic(NOMEMORY);
functornext = (char *)DEREL(x.f_top);
functorfull = functorsp+x.f_size;
functors = (functor *)DEREL(x.f_tree);
clausesp = (clause *)EVEN(functorfull);
clausefree = (clause *)DEREL(x.cl_free);
clausefull = x.cl_size;
clausenext = (clause *)DEREL(x.cl_top);
trailstack = (term **)EVEN(clausesp + clausefull);
trailtop = 0;
trailfull = x.t_size;
protostack = (char *)EVEN(trailstack+trailfull);
prototop = (char *)DEREL(x.p_top);
if (ISSTRUCT(prototop))
protonext = (term *)((long)prototop+sizeof(term)+
ARITY(prototop)*sizeof(term *));
else
protonext = prototop+1;
protofull = (char *)((long)protostack + x.p_size);
for (i=0; i<=MAXARGS; i++)
protofree[i] = (term *)DEREL(x.p_free[i]);
copystack = (char *)EVEN(protofull);
copytop = NULL;
copynext = (term *)copystack;
copyfull = (char *)((long)copystack + x.c_size);
stack = (char *)EVEN(copyfull);
stacktop = stack;
stackfull = stack+x.s_size;
for (i=0; i<STDFUNCTORS; i++)
stdfunctor[i] = (functor *)DEREL(x.stdf[i]);
Backpoint = (backlog *)0L;
Topenv = Preenv = (env *)0L;
read_area(fp,functorsp, functornext);
read_area(fp,clausesp, clausesp+clausefull);
read_area(fp,protostack, protonext);
derelocate();
if (fclose(fp))
{
perror("cannot close image");
return(FALSE);
}
return(TRUE);
}
short read_area(fp, from, to)
FILE *fp;
char *from, *to;
{
while (from < to)
{
if (((long)to - (long)from) < 32000)
{
fread(from, (unsigned)((long)to-(long)from), 1, fp);
break;
}
else
fread(from, 32000, 1, fp);
from += 32000;
}
}