File:
[gforth] /
gforth /
Attic /
main.c
Revision
1.7:
download - view:
text,
annotated -
select for diffs
Fri Jul 8 15:00:55 1994 UTC (29 years, 9 months ago) by
anton
Branches:
MAIN
CVS tags:
HEAD
signals are now translated into THROWs
A number of bug fixes (make a diff of BUGS for details)
added assert.fs and debugging.fs
made .s nicer
keep names of included files (in loadfilename) and print them upon error
1: /*
2: $Id: main.c,v 1.7 1994/07/08 15:00:55 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: #include "io.h"
17:
18: Xt *throw_ip;
19:
20:
21: #ifndef DEFAULTBIN
22: # define DEFAULTBIN ""
23: #endif
24:
25: #ifdef DIRECT_THREADED
26: # define CA(n) (symbols[(n)])
27: #else
28: # define CA(n) ((int)(symbols+(n)))
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)
34: * size of data and FP stack (in bytes)
35: * pointer to start of code
36: * pointer into throw (for signal handling)
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,
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
48: */
49:
50: void relocate(int *image, char *bitstring, int size, Label symbols[])
51: {
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) :
66: case CF(DOUSER) : MAKE_CF(image+i,symbols[CF(image[i])]); break;
67: case CF(DODOES) : MAKE_DOES_CF(image+i,image[i+1]+((int)image));
68: break;
69: case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
70: default : image[i]=(Cell)CA(CF(image[i]));
71: }
72: else
73: image[i]+=(Cell)image;
74:
75: CACHE_FLUSH(image,size);
76: }
77:
78: int* loader(const char* filename)
79: { int header[2];
80: FILE *imagefile;
81: int *image;
82:
83: if(!(int)(imagefile=fopen(filename,"rb")))
84: {
85: fprintf(stderr,"Can't open image file '%s'",filename);
86: exit(1);
87: }
88:
89: fread(header,1,2*sizeof(int),imagefile);
90:
91: image=malloc(header[0]+((header[0]-1)/sizeof(Cell))/8+1);
92:
93: memset(image,0,header[0]+((header[0]-1)/sizeof(Cell))/8+1);
94:
95: image[0]=header[0];
96: image[1]=header[1];
97:
98: fread(image+2,1,header[1]-2*sizeof(Cell),imagefile);
99: fread(((void *)image)+header[0],1,((header[1]-1)/sizeof(Cell))/8+1,
100: imagefile);
101: fclose(imagefile);
102:
103: relocate(image,(char *)image+header[0],header[1],engine(0,0,0,0,0));
104:
105: return(image);
106: }
107:
108: int go_forth(int *image, int stack, Cell *entries)
109: {
110: Cell* rp=(Cell*)((void *)image+image[0]);
111: double* fp=(double*)((void *)rp-image[2]);
112: Address lp=(Address)((void *)fp-image[2]);
113: Cell* sp=(Cell*)((void *)lp-image[2]);
114: Cell* ip=(Cell*)(image[3]);
115:
116: throw_ip = (Xt *)(image[4]);
117: for(;stack>0;stack--)
118: *--sp=entries[stack-1];
119:
120: install_signal_handlers(); /* right place? */
121:
122: return((int)engine(ip,sp,rp,fp,lp));
123: }
124:
125: int main(int argc, char **argv, char **env)
126: {
127: char imagefile[256];
128: Cell environ[3] = {(Cell)argc, (Cell)argv, (Cell)env};
129: char* imagepath;
130:
131: #if defined(i386) && defined(ALIGNMENT_CHECK)
132: /* turn on alignment checks on the 486.
133: * on the 386 this should have no effect. */
134: __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
135: #endif
136: if((int)(imagepath=getenv("FORTHBIN")))
137: {
138: strcpy(imagefile,imagepath);
139:
140: if(imagefile[strlen(imagefile)-1]!='/')
141: imagefile[strlen(imagefile)]='/';
142: }
143: else
144: {
145: strcpy(imagefile,DEFAULTBIN);
146:
147: if(imagefile[0]!=0 && imagefile[strlen(imagefile)-1]!='/')
148: imagefile[strlen(imagefile)]='/';
149: }
150:
151: if(argc>1 && argv[1][0]=='@')
152: {
153: if(argv[1][1]=='/')
154: strcpy(imagefile,argv[1]+1);
155: else
156: strcpy(imagefile+strlen(imagefile),argv[1]+1);
157:
158: environ[0]-=1;
159: environ[1]+=sizeof(argv);
160: argv[1]=argv[0];
161: }
162: else
163: strcpy(imagefile+strlen(imagefile),"kernal.fi");
164:
165: exit(go_forth(loader(imagefile),3,environ));
166: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>