Annotation of gforth/main.c, revision 1.9
1.1 anton 1: /*
1.9 ! anton 2: $Id: main.c,v 1.8 1994/07/13 19:21:04 pazsan Exp $
1.1 anton 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"
1.5 pazsan 16: #include "io.h"
1.1 anton 17:
1.7 anton 18: Xt *throw_ip;
19:
20:
1.2 pazsan 21: #ifndef DEFAULTBIN
22: # define DEFAULTBIN ""
23: #endif
24:
1.1 anton 25: #ifdef DIRECT_THREADED
1.2 pazsan 26: # define CA(n) (symbols[(n)])
1.1 anton 27: #else
1.2 pazsan 28: # define CA(n) ((int)(symbols+(n)))
1.1 anton 29: #endif
30:
31: /* image file format:
32: * size of image with stacks without tags (in bytes)
33: * size of image without stacks and tags (in bytes)
1.5 pazsan 34: * size of data and FP stack (in bytes)
1.1 anton 35: * pointer to start of code
1.7 anton 36: * pointer into throw (for signal handling)
1.1 anton 37: * data (size in image[1])
38: * tags (1 bit/data cell)
39: *
40: * tag==1 mean that the corresponding word is an address;
41: * If the word is >=0, the address is within the image;
42: * addresses within the image are given relative to the start of the image.
43: * If the word is =-1, the address is NIL,
1.2 pazsan 44: * If the word is between -2 and -5, it's a CFA (:, Create, Constant, User)
45: * If the word is -6, it's a DOES> CFA
46: * If the word is -7, it's a DOES JUMP
47: * If the word is <-7, it's a primitive
1.1 anton 48: */
49:
50: void relocate(int *image, char *bitstring, int size, Label symbols[])
51: {
1.5 pazsan 52: int i=0, j, k, steps=(size/sizeof(Cell))/8;
53: char bits;
54: /* static char bits[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};*/
55:
56: for(k=0; k<=steps; k++)
57: for(j=0, bits=bitstring[k]; j<8; j++, i++, bits<<=1)
58: if(bits & 0x80)
59: if(image[i]<0)
60: switch(image[i])
61: {
62: case CF_NIL : image[i]=0; break;
63: case CF(DOCOL) :
64: case CF(DOVAR) :
65: case CF(DOCON) :
1.9 ! anton 66: case CF(DOUSER) :
! 67: case CF(DODEFER) :
! 68: MAKE_CF(image+i,symbols[CF(image[i])]); break;
1.5 pazsan 69: case CF(DODOES) : MAKE_DOES_CF(image+i,image[i+1]+((int)image));
70: break;
71: case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
72: default : image[i]=(Cell)CA(CF(image[i]));
73: }
74: else
75: image[i]+=(Cell)image;
1.2 pazsan 76:
1.5 pazsan 77: CACHE_FLUSH(image,size);
1.1 anton 78: }
79:
80: int* loader(const char* filename)
81: { int header[2];
82: FILE *imagefile;
83: int *image;
84:
85: if(!(int)(imagefile=fopen(filename,"rb")))
86: {
87: fprintf(stderr,"Can't open image file '%s'",filename);
88: exit(1);
89: }
90:
91: fread(header,1,2*sizeof(int),imagefile);
92:
93: image=malloc(header[0]+((header[0]-1)/sizeof(Cell))/8+1);
94:
95: memset(image,0,header[0]+((header[0]-1)/sizeof(Cell))/8+1);
96:
97: image[0]=header[0];
98: image[1]=header[1];
99:
100: fread(image+2,1,header[1]-2*sizeof(Cell),imagefile);
101: fread(((void *)image)+header[0],1,((header[1]-1)/sizeof(Cell))/8+1,
102: imagefile);
103: fclose(imagefile);
104:
1.4 anton 105: relocate(image,(char *)image+header[0],header[1],engine(0,0,0,0,0));
1.1 anton 106:
107: return(image);
108: }
109:
110: int go_forth(int *image, int stack, Cell *entries)
111: {
112: Cell* rp=(Cell*)((void *)image+image[0]);
113: double* fp=(double*)((void *)rp-image[2]);
1.4 anton 114: Address lp=(Address)((void *)fp-image[2]);
115: Cell* sp=(Cell*)((void *)lp-image[2]);
1.1 anton 116: Cell* ip=(Cell*)(image[3]);
1.8 pazsan 117: int throw_code;
1.7 anton 118:
119: throw_ip = (Xt *)(image[4]);
1.1 anton 120: for(;stack>0;stack--)
121: *--sp=entries[stack-1];
122:
123: install_signal_handlers(); /* right place? */
1.8 pazsan 124:
125: if ((throw_code=setjmp(throw_jmp_buf))) {
126: static Cell signal_data_stack[8];
127: static Cell signal_return_stack[8];
128:
129: signal_data_stack[7]=throw_code;
130:
131: return((int)engine(image[4],signal_data_stack+7,
132: signal_return_stack+8,0,0));
133: }
1.1 anton 134:
1.4 anton 135: return((int)engine(ip,sp,rp,fp,lp));
1.1 anton 136: }
137:
138: int main(int argc, char **argv, char **env)
139: {
140: char imagefile[256];
141: Cell environ[3] = {(Cell)argc, (Cell)argv, (Cell)env};
142: char* imagepath;
143:
1.6 anton 144: #if defined(i386) && defined(ALIGNMENT_CHECK)
145: /* turn on alignment checks on the 486.
146: * on the 386 this should have no effect. */
147: __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
148: #endif
1.1 anton 149: if((int)(imagepath=getenv("FORTHBIN")))
150: {
151: strcpy(imagefile,imagepath);
152:
153: if(imagefile[strlen(imagefile)-1]!='/')
154: imagefile[strlen(imagefile)]='/';
155: }
156: else
1.2 pazsan 157: {
158: strcpy(imagefile,DEFAULTBIN);
159:
160: if(imagefile[0]!=0 && imagefile[strlen(imagefile)-1]!='/')
161: imagefile[strlen(imagefile)]='/';
162: }
1.1 anton 163:
164: if(argc>1 && argv[1][0]=='@')
165: {
166: if(argv[1][1]=='/')
167: strcpy(imagefile,argv[1]+1);
168: else
169: strcpy(imagefile+strlen(imagefile),argv[1]+1);
170:
171: environ[0]-=1;
172: environ[1]+=sizeof(argv);
173: argv[1]=argv[0];
174: }
175: else
176: strcpy(imagefile+strlen(imagefile),"kernal.fi");
177:
178: exit(go_forth(loader(imagefile),3,environ));
179: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>