| 1 : |
anton
|
1.1
|
/* |
| 2 : |
|
|
$Id: main.c,v 1.8 1993/11/02 13:34:38 anton Exp $ |
| 3 : |
|
|
Copyright 1993 by the ANSI figForth Development Group |
| 4 : |
|
|
*/ |
| 5 : |
|
|
|
| 6 : |
|
|
#include <ctype.h> |
| 7 : |
|
|
#include <stdio.h> |
| 8 : |
|
|
#include <string.h> |
| 9 : |
|
|
#include <math.h> |
| 10 : |
|
|
#include <sys/types.h> |
| 11 : |
|
|
#include <sys/stat.h> |
| 12 : |
|
|
#include <fcntl.h> |
| 13 : |
|
|
#include <assert.h> |
| 14 : |
|
|
#include <stdlib.h> |
| 15 : |
|
|
#include "forth.h" |
| 16 : |
|
|
|
| 17 : |
pazsan
|
1.2
|
#ifndef DEFAULTBIN |
| 18 : |
|
|
# define DEFAULTBIN "" |
| 19 : |
|
|
#endif |
| 20 : |
|
|
|
| 21 : |
anton
|
1.1
|
#ifdef DIRECT_THREADED |
| 22 : |
pazsan
|
1.2
|
# define CA(n) (symbols[(n)]) |
| 23 : |
anton
|
1.1
|
#else |
| 24 : |
pazsan
|
1.2
|
# define CA(n) ((int)(symbols+(n))) |
| 25 : |
anton
|
1.1
|
#endif |
| 26 : |
|
|
|
| 27 : |
|
|
/* image file format: |
| 28 : |
|
|
* size of image with stacks without tags (in bytes) |
| 29 : |
|
|
* size of image without stacks and tags (in bytes) |
| 30 : |
|
|
* size of data and FP stack (in bytes) |
| 31 : |
|
|
* pointer to start of code |
| 32 : |
|
|
* data (size in image[1]) |
| 33 : |
|
|
* tags (1 bit/data cell) |
| 34 : |
|
|
* |
| 35 : |
|
|
* tag==1 mean that the corresponding word is an address; |
| 36 : |
|
|
* If the word is >=0, the address is within the image; |
| 37 : |
|
|
* addresses within the image are given relative to the start of the image. |
| 38 : |
|
|
* If the word is =-1, the address is NIL, |
| 39 : |
pazsan
|
1.2
|
* If the word is between -2 and -5, it's a CFA (:, Create, Constant, User) |
| 40 : |
|
|
* If the word is -6, it's a DOES> CFA |
| 41 : |
|
|
* If the word is -7, it's a DOES JUMP |
| 42 : |
|
|
* If the word is <-7, it's a primitive |
| 43 : |
anton
|
1.1
|
*/ |
| 44 : |
|
|
|
| 45 : |
|
|
void relocate(int *image, char *bitstring, int size, Label symbols[]) |
| 46 : |
|
|
{ |
| 47 : |
|
|
int i; |
| 48 : |
|
|
static char bits[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; |
| 49 : |
pazsan
|
1.2
|
|
| 50 : |
|
|
#ifdef DEBUG |
| 51 : |
|
|
printf("Dodoes-Adresse: %08x\n",(int)symbols[DODOES]); |
| 52 : |
|
|
#endif |
| 53 : |
anton
|
1.1
|
|
| 54 : |
|
|
for(i=0;i<size/sizeof(Cell);i++) |
| 55 : |
|
|
if(bitstring[i >> 3] & bits[i & 7]) |
| 56 : |
|
|
if(image[i]<0) |
| 57 : |
pazsan
|
1.2
|
switch(image[i]) |
| 58 : |
anton
|
1.1
|
{ |
| 59 : |
pazsan
|
1.2
|
case CF_NIL : |
| 60 : |
|
|
image[i]=0; break; |
| 61 : |
|
|
case CF(DOCOL) : |
| 62 : |
|
|
case CF(DOVAR) : |
| 63 : |
|
|
case CF(DOCON) : |
| 64 : |
|
|
case CF(DOUSER) : |
| 65 : |
|
|
MAKE_CF(image+i,symbols[CF(image[i])]); break; |
| 66 : |
|
|
case CF(DODOES) : |
| 67 : |
|
|
MAKE_DOES_CF(image+i,image[i+1]+((int)image)); |
| 68 : |
|
|
i++; break; /* is this necessary? */ |
| 69 : |
|
|
case CF(DOESJUMP): |
| 70 : |
|
|
MAKE_DOES_HANDLER(image+i); |
| 71 : |
|
|
break; |
| 72 : |
|
|
default: |
| 73 : |
|
|
image[i]=(Cell)CA(CF(image[i])); |
| 74 : |
anton
|
1.1
|
} |
| 75 : |
|
|
else |
| 76 : |
|
|
image[i]+=(Cell)image; |
| 77 : |
|
|
} |
| 78 : |
|
|
|
| 79 : |
|
|
int* loader(const char* filename) |
| 80 : |
|
|
{ int header[2]; |
| 81 : |
|
|
FILE *imagefile; |
| 82 : |
|
|
int *image; |
| 83 : |
|
|
|
| 84 : |
|
|
if(!(int)(imagefile=fopen(filename,"rb"))) |
| 85 : |
|
|
{ |
| 86 : |
|
|
fprintf(stderr,"Can't open image file '%s'",filename); |
| 87 : |
|
|
exit(1); |
| 88 : |
|
|
} |
| 89 : |
|
|
|
| 90 : |
|
|
fread(header,1,2*sizeof(int),imagefile); |
| 91 : |
|
|
|
| 92 : |
|
|
image=malloc(header[0]+((header[0]-1)/sizeof(Cell))/8+1); |
| 93 : |
|
|
|
| 94 : |
|
|
memset(image,0,header[0]+((header[0]-1)/sizeof(Cell))/8+1); |
| 95 : |
|
|
|
| 96 : |
|
|
image[0]=header[0]; |
| 97 : |
|
|
image[1]=header[1]; |
| 98 : |
|
|
|
| 99 : |
|
|
fread(image+2,1,header[1]-2*sizeof(Cell),imagefile); |
| 100 : |
|
|
fread(((void *)image)+header[0],1,((header[1]-1)/sizeof(Cell))/8+1, |
| 101 : |
|
|
imagefile); |
| 102 : |
|
|
fclose(imagefile); |
| 103 : |
|
|
|
| 104 : |
|
|
relocate(image,(char *)image+header[0],header[1],engine(0,0,0,0)); |
| 105 : |
|
|
|
| 106 : |
|
|
return(image); |
| 107 : |
|
|
} |
| 108 : |
|
|
|
| 109 : |
|
|
int go_forth(int *image, int stack, Cell *entries) |
| 110 : |
|
|
{ |
| 111 : |
|
|
Cell* rp=(Cell*)((void *)image+image[0]); |
| 112 : |
|
|
double* fp=(double*)((void *)rp-image[2]); |
| 113 : |
|
|
Cell* sp=(Cell*)((void *)fp-image[2]); |
| 114 : |
|
|
Cell* ip=(Cell*)(image[3]); |
| 115 : |
|
|
|
| 116 : |
|
|
for(;stack>0;stack--) |
| 117 : |
|
|
*--sp=entries[stack-1]; |
| 118 : |
|
|
|
| 119 : |
|
|
install_signal_handlers(); /* right place? */ |
| 120 : |
|
|
|
| 121 : |
|
|
return((int)engine(ip,sp,rp,fp)); |
| 122 : |
|
|
} |
| 123 : |
|
|
|
| 124 : |
|
|
int main(int argc, char **argv, char **env) |
| 125 : |
|
|
{ |
| 126 : |
|
|
char imagefile[256]; |
| 127 : |
|
|
Cell environ[3] = {(Cell)argc, (Cell)argv, (Cell)env}; |
| 128 : |
|
|
char* imagepath; |
| 129 : |
|
|
|
| 130 : |
|
|
if((int)(imagepath=getenv("FORTHBIN"))) |
| 131 : |
|
|
{ |
| 132 : |
|
|
strcpy(imagefile,imagepath); |
| 133 : |
|
|
|
| 134 : |
|
|
if(imagefile[strlen(imagefile)-1]!='/') |
| 135 : |
|
|
imagefile[strlen(imagefile)]='/'; |
| 136 : |
|
|
} |
| 137 : |
|
|
else |
| 138 : |
pazsan
|
1.2
|
{ |
| 139 : |
|
|
strcpy(imagefile,DEFAULTBIN); |
| 140 : |
|
|
|
| 141 : |
|
|
if(imagefile[0]!=0 && imagefile[strlen(imagefile)-1]!='/') |
| 142 : |
|
|
imagefile[strlen(imagefile)]='/'; |
| 143 : |
|
|
} |
| 144 : |
anton
|
1.1
|
|
| 145 : |
|
|
if(argc>1 && argv[1][0]=='@') |
| 146 : |
|
|
{ |
| 147 : |
|
|
if(argv[1][1]=='/') |
| 148 : |
|
|
strcpy(imagefile,argv[1]+1); |
| 149 : |
|
|
else |
| 150 : |
|
|
strcpy(imagefile+strlen(imagefile),argv[1]+1); |
| 151 : |
|
|
|
| 152 : |
|
|
environ[0]-=1; |
| 153 : |
|
|
environ[1]+=sizeof(argv); |
| 154 : |
|
|
argv[1]=argv[0]; |
| 155 : |
|
|
} |
| 156 : |
|
|
else |
| 157 : |
|
|
strcpy(imagefile+strlen(imagefile),"kernal.fi"); |
| 158 : |
|
|
|
| 159 : |
|
|
exit(go_forth(loader(imagefile),3,environ)); |
| 160 : |
|
|
} |