Annotation of gforth/main.c, revision 1.42
1.30 anton 1: /* command line interpretation, image loading etc. for Gforth
2:
3:
4: Copyright (C) 1995 Free Software Foundation, Inc.
5:
6: This file is part of Gforth.
7:
8: Gforth is free software; you can redistribute it and/or
9: modify it under the terms of the GNU General Public License
10: as published by the Free Software Foundation; either version 2
11: of the License, or (at your option) any later version.
12:
13: This program is distributed in the hope that it will be useful,
14: but WITHOUT ANY WARRANTY; without even the implied warranty of
15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: GNU General Public License for more details.
17:
18: You should have received a copy of the GNU General Public License
19: along with this program; if not, write to the Free Software
20: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1.1 anton 21: */
22:
1.32 anton 23: #include "config.h"
1.35 anton 24: #include <errno.h>
1.1 anton 25: #include <ctype.h>
26: #include <stdio.h>
27: #include <string.h>
28: #include <math.h>
29: #include <sys/types.h>
30: #include <sys/stat.h>
31: #include <fcntl.h>
32: #include <assert.h>
33: #include <stdlib.h>
34: #include "forth.h"
1.5 pazsan 35: #include "io.h"
1.20 anton 36: #include "getopt.h"
1.2 pazsan 37:
1.22 pazsan 38: #ifdef MSDOS
39: jmp_buf throw_jmp_buf;
40: #endif
41:
1.42 ! anton 42: #ifndef FUZZ
! 43: # define FUZZ 0x4000
! 44: #endif
! 45:
1.10 anton 46: #ifndef DEFAULTPATH
1.16 pazsan 47: # define DEFAULTPATH "/usr/local/lib/gforth:."
1.10 anton 48: #endif
49:
1.1 anton 50: #ifdef DIRECT_THREADED
1.16 pazsan 51: # define CA(n) (symbols[(n)])
1.1 anton 52: #else
1.21 pazsan 53: # define CA(n) ((Cell)(symbols+(n)))
1.1 anton 54: #endif
55:
1.23 pazsan 56: #define maxaligned(n) (typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
1.10 anton 57:
1.21 pazsan 58: static Cell dictsize=0;
59: static Cell dsize=0;
60: static Cell rsize=0;
61: static Cell fsize=0;
62: static Cell lsize=0;
1.10 anton 63: char *progname;
64:
1.1 anton 65: /* image file format:
1.35 anton 66: * "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.2.0 -i\n")
67: * padding to a multiple of 8
1.36 anton 68: * magic: "Gforth1x" means format 0.2,
1.35 anton 69: * where x is even for big endian and odd for little endian
70: * and x & ~1 is the size of the cell in bytes.
71: * padding to max alignment (no padding necessary on current machines)
72: * ImageHeader structure (see below)
73: * data (size in ImageHeader.image_size)
74: * tags ((if relocatable, 1 bit/data cell)
1.1 anton 75: *
1.35 anton 76: * tag==1 means that the corresponding word is an address;
1.1 anton 77: * If the word is >=0, the address is within the image;
78: * addresses within the image are given relative to the start of the image.
1.35 anton 79: * If the word =-1 (CF_NIL), the address is NIL,
80: * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
81: * If the word =CF(DODOES), it's a DOES> CFA
82: * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
83: * possibly containing a jump to dodoes)
84: * If the word is <CF(DOESJUMP), it's a primitive
1.1 anton 85: */
86:
1.35 anton 87: typedef struct {
88: Address base; /* base address of image (0 if relocatable) */
89: UCell checksum; /* checksum of ca's to protect against some
90: incompatible binary/executable combinations
91: (0 if relocatable) */
92: UCell image_size; /* all sizes in bytes */
93: UCell dict_size;
94: UCell data_stack_size;
95: UCell fp_stack_size;
96: UCell return_stack_size;
97: UCell locals_stack_size;
98: Xt *boot_entry; /* initial ip for booting (in BOOT) */
99: Xt *throw_entry; /* ip after signal (in THROW) */
100: } ImageHeader;
101: /* the image-header is created in main.fs */
102:
1.10 anton 103: void relocate(Cell *image, char *bitstring, int size, Label symbols[])
1.1 anton 104: {
1.16 pazsan 105: int i=0, j, k, steps=(size/sizeof(Cell))/8;
106: char bits;
1.5 pazsan 107: /* static char bits[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};*/
1.37 pazsan 108:
109: /* printf("relocating %x[%x]\n", image, size); */
1.5 pazsan 110:
1.16 pazsan 111: for(k=0; k<=steps; k++)
112: for(j=0, bits=bitstring[k]; j<8; j++, i++, bits<<=1)
113: if(bits & 0x80)
114: if(image[i]<0)
115: switch(image[i])
116: {
117: case CF_NIL : image[i]=0; break;
118: case CF(DOCOL) :
119: case CF(DOVAR) :
120: case CF(DOCON) :
121: case CF(DOUSER) :
1.24 pazsan 122: case CF(DODEFER) :
1.28 anton 123: case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(image[i])]); break;
1.21 pazsan 124: case CF(DODOES) : MAKE_DOES_CF(image+i,image[i+1]+((Cell)image));
1.16 pazsan 125: break;
126: case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
1.37 pazsan 127: default :
128: /* printf("Code field generation image[%x]:=CA(%x)\n",
129: i, CF(image[i]));
130: */ image[i]=(Cell)CA(CF(image[i]));
1.16 pazsan 131: }
132: else
133: image[i]+=(Cell)image;
1.1 anton 134: }
135:
1.35 anton 136: UCell checksum(Label symbols[])
137: {
138: UCell r=0;
139: Cell i;
140:
141: for (i=DOCOL; i<=DOESJUMP; i++) {
142: r ^= (UCell)(symbols[i]);
143: r = (r << 5) | (r >> (8*sizeof(Cell)-5));
144: }
145: #ifdef DIRECT_THREADED
146: /* we have to consider all the primitives */
147: for (; symbols[i]!=(Label)0; i++) {
148: r ^= (UCell)(symbols[i]);
149: r = (r << 5) | (r >> (8*sizeof(Cell)-5));
150: }
151: #else
152: /* in indirect threaded code all primitives are accessed through the
153: symbols table, so we just have to put the base address of symbols
154: in the checksum */
155: r ^= (UCell)symbols;
156: #endif
157: return r;
158: }
159:
1.39 pazsan 160: Address loader(FILE *imagefile, char* filename)
1.35 anton 161: /* returns the address of the image proper (after the preamble) */
1.10 anton 162: {
1.35 anton 163: ImageHeader header;
164: Address image;
165: Address imp; /* image+preamble */
1.18 pazsan 166: Char magic[8];
1.35 anton 167: Cell wholesize;
168: Cell imagesize; /* everything needed by the image */
169: Cell preamblesize=0;
170: Label *symbols=engine(0,0,0,0,0);
171: UCell check_sum=checksum(symbols);
1.18 pazsan 172:
1.35 anton 173: static char* endianstring[]= { "big","little" };
1.25 pazsan 174:
1.18 pazsan 175: do
176: {
177: if(fread(magic,sizeof(Char),8,imagefile) < 8) {
1.39 pazsan 178: fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.2) image.\n",
179: progname, filename);
1.18 pazsan 180: exit(1);
181: }
1.35 anton 182: preamblesize+=8;
1.18 pazsan 183: #ifdef DEBUG
1.35 anton 184: fprintf(stderr,"Magic found: %-8s\n",magic);
1.18 pazsan 185: #endif
186: }
1.35 anton 187: while(memcmp(magic,"Gforth1",7));
1.25 pazsan 188:
1.35 anton 189: if(magic[7] != sizeof(Cell) +
1.25 pazsan 190: #ifdef WORDS_BIGENDIAN
191: '0'
192: #else
193: '1'
194: #endif
1.35 anton 195: )
196: { fprintf(stderr,"This image is %d bit %s-endian, whereas the machine is %d bit %s-endian.\n",
197: ((magic[7]-'0')&~1)*8, endianstring[magic[7]&1],
198: sizeof(Cell)*8, endianstring[
1.25 pazsan 199: #ifdef WORDS_BIGENDIAN
200: 0
201: #else
202: 1
203: #endif
204: ]);
205: exit(-2);
206: };
1.18 pazsan 207:
1.35 anton 208: fread((void *)&header,sizeof(ImageHeader),1,imagefile);
1.16 pazsan 209: if (dictsize==0)
1.35 anton 210: dictsize = header.dict_size;
1.16 pazsan 211: if (dsize==0)
1.35 anton 212: dsize=header.data_stack_size;
1.16 pazsan 213: if (rsize==0)
1.35 anton 214: rsize=header.return_stack_size;
1.16 pazsan 215: if (fsize==0)
1.35 anton 216: fsize=header.fp_stack_size;
1.16 pazsan 217: if (lsize==0)
1.35 anton 218: lsize=header.locals_stack_size;
1.16 pazsan 219: dictsize=maxaligned(dictsize);
220: dsize=maxaligned(dsize);
221: rsize=maxaligned(rsize);
222: lsize=maxaligned(lsize);
223: fsize=maxaligned(fsize);
224:
1.35 anton 225: wholesize = preamblesize+dictsize+dsize+rsize+fsize+lsize;
226: imagesize = preamblesize+header.image_size+((header.image_size-1)/sizeof(Cell))/8+1;
1.42 ! anton 227: image=malloc((wholesize>imagesize?wholesize:imagesize)
! 228: #ifndef __unix__
! 229: +FUZZ
! 230: #endif
! 231: );
1.35 anton 232: /*image = maxaligned(image);*/
1.42 ! anton 233: /* memset(image,0,wholesize); */
! 234:
! 235: #ifndef __unix__
! 236: if(header.base==0) image += FUZZ/2;
! 237: else if((UCell)(header.base - (Cell)image + preamblesize) < FUZZ)
! 238: image = header.base - preamblesize;
! 239: #endif
1.35 anton 240: rewind(imagefile); /* fseek(imagefile,0L,SEEK_SET); */
241: fread(image,1,imagesize,imagefile);
1.16 pazsan 242: fclose(imagefile);
1.35 anton 243: imp=image+preamblesize;
1.16 pazsan 244:
1.35 anton 245: if(header.base==0) {
246: relocate((Cell *)imp,imp+header.image_size,header.image_size,symbols);
247: ((ImageHeader *)imp)->checksum=check_sum;
1.16 pazsan 248: }
1.35 anton 249: else if(header.base!=imp) {
1.36 anton 250: fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\nThe Gforth installer should look into the INSTALL file\n",
1.35 anton 251: progname, (unsigned long)header.base, (unsigned long)imp);
252: exit(1);
253: } else if (header.checksum != check_sum) {
1.36 anton 254: fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\nThe Gforth installer should look into the INSTALL file\n",
1.35 anton 255: progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
1.16 pazsan 256: exit(1);
257: }
1.1 anton 258:
1.35 anton 259: ((ImageHeader *)imp)->dict_size=dictsize;
260: ((ImageHeader *)imp)->data_stack_size=dsize;
261: ((ImageHeader *)imp)->return_stack_size=rsize;
262: ((ImageHeader *)imp)->fp_stack_size=fsize;
263: ((ImageHeader *)imp)->locals_stack_size=lsize;
264:
1.38 anton 265: CACHE_FLUSH(imp, header.image_size);
1.35 anton 266:
267: return imp;
1.1 anton 268: }
269:
1.35 anton 270: int go_forth(Address image, int stack, Cell *entries)
1.1 anton 271: {
1.35 anton 272: Cell *sp=(Cell*)(image+dictsize+dsize);
1.15 anton 273: Address lp=(Address)((void *)sp+lsize);
274: Float *fp=(Float *)((void *)lp+fsize);
275: Cell *rp=(Cell*)((void *)fp+rsize);
1.35 anton 276: Xt *ip=(Xt *)(((ImageHeader *)image)->boot_entry);
1.15 anton 277: int throw_code;
278:
279: for(;stack>0;stack--)
280: *--sp=entries[stack-1];
1.39 pazsan 281:
282: #ifndef MSDOS
1.33 anton 283: get_winsize();
1.39 pazsan 284: #endif
285:
1.15 anton 286: install_signal_handlers(); /* right place? */
287:
288: if ((throw_code=setjmp(throw_jmp_buf))) {
289: static Cell signal_data_stack[8];
290: static Cell signal_return_stack[8];
291: static Float signal_fp_stack[1];
292:
293: signal_data_stack[7]=throw_code;
294:
1.35 anton 295: return((int)engine(((ImageHeader *)image)->throw_entry,signal_data_stack+7,
1.15 anton 296: signal_return_stack+8,signal_fp_stack,0));
297: }
1.35 anton 298:
1.15 anton 299: return((int)engine(ip,sp,rp,fp,lp));
1.1 anton 300: }
301:
1.10 anton 302: int convsize(char *s, int elemsize)
303: /* converts s of the format #+u (e.g. 25k) into the number of bytes.
304: the unit u can be one of bekM, where e stands for the element
305: size. default is e */
306: {
307: char *endp;
308: int n,m;
309:
310: m = elemsize;
311: n = strtoul(s,&endp,0);
312: if (endp!=NULL) {
313: if (strcmp(endp,"b")==0)
314: m=1;
315: else if (strcmp(endp,"k")==0)
316: m=1024;
317: else if (strcmp(endp,"M")==0)
318: m=1024*1024;
1.31 anton 319: else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
320: fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
1.10 anton 321: exit(1);
322: }
323: }
324: return n*m;
325: }
326:
1.1 anton 327: int main(int argc, char **argv, char **env)
328: {
1.42 ! anton 329: char *path, *path1, *p;
1.16 pazsan 330: char *imagename="gforth.fi";
331: FILE *image_file;
332: int c, retvalue;
1.42 ! anton 333: #ifdef __unix__
! 334: char pathsep=':';
! 335: #else
! 336: char pathsep=';';
! 337: #endif
1.10 anton 338:
1.14 anton 339: #if defined(i386) && defined(ALIGNMENT_CHECK) && !defined(DIRECT_THREADED)
1.30 anton 340: /* turn on alignment checks on the 486.
341: * on the 386 this should have no effect. */
342: __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
343: /* this is unusable with Linux' libc.4.6.27, because this library is
344: not alignment-clean; we would have to replace some library
345: functions (e.g., memcpy) to make it work */
1.6 anton 346: #endif
1.2 pazsan 347:
1.16 pazsan 348: progname = argv[0];
349: if ((path=getenv("GFORTHPATH"))==NULL)
1.41 anton 350: path = DEFAULTPATH;
1.16 pazsan 351: opterr=0;
352: while (1) {
353: int option_index=0;
354: static struct option opts[] = {
355: {"image-file", required_argument, NULL, 'i'},
356: {"dictionary-size", required_argument, NULL, 'm'},
357: {"data-stack-size", required_argument, NULL, 'd'},
358: {"return-stack-size", required_argument, NULL, 'r'},
359: {"fp-stack-size", required_argument, NULL, 'f'},
360: {"locals-stack-size", required_argument, NULL, 'l'},
361: {"path", required_argument, NULL, 'p'},
362: {0,0,0,0}
363: /* no-init-file, no-rc? */
364: };
1.10 anton 365:
1.17 pazsan 366: c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:", opts, &option_index);
1.12 pazsan 367:
1.16 pazsan 368: if (c==EOF)
369: break;
370: if (c=='?') {
371: optind--;
372: break;
373: }
374: switch (c) {
375: case 'i': imagename = optarg; break;
376: case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
377: case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
378: case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
379: case 'f': fsize = convsize(optarg,sizeof(Float)); break;
380: case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
381: case 'p': path = optarg; break;
382: }
383: }
384: path1=path;
1.29 pazsan 385:
386: if(strchr(imagename, '/')==NULL)
387: {
388: do {
1.42 ! anton 389: char *pend=strchr(path, pathsep);
1.29 pazsan 390: if (pend==NULL)
391: pend=path+strlen(path);
392: if (strlen(path)==0) {
393: fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
394: progname, imagename, path1);
395: exit(1);
396: }
397: {
398: int dirlen=pend-path;
399: char fullfilename[dirlen+strlen(imagename)+2];
400: memcpy(fullfilename, path, dirlen);
401: if (fullfilename[dirlen-1]!='/')
402: fullfilename[dirlen++]='/';
403: strcpy(fullfilename+dirlen,imagename);
404: image_file=fopen(fullfilename,"rb");
405: }
1.42 ! anton 406: path=pend+(*pend==pathsep);
1.29 pazsan 407: } while (image_file==NULL);
1.16 pazsan 408: }
1.29 pazsan 409: else
1.16 pazsan 410: {
1.29 pazsan 411: image_file=fopen(imagename,"rb");
1.35 anton 412: if(image_file==NULL) {
413: fprintf(stderr,"%s: %s: %s\n", progname, imagename, strerror(errno));
414: exit(1);
415: }
1.16 pazsan 416: }
1.29 pazsan 417:
1.16 pazsan 418: {
419: Cell environ[]= {
420: (Cell)argc-(optind-1),
421: (Cell)(argv+(optind-1)),
1.42 ! anton 422: (Cell)strlen(path1),
1.16 pazsan 423: (Cell)path1};
424: argv[optind-1] = progname;
425: /*
426: for (i=0; i<environ[0]; i++)
427: printf("%s\n", ((char **)(environ[1]))[i]);
428: */
1.42 ! anton 429: /* make path OS-independent by replacing path separators with NUL */
! 430: for (p=path1; *p!='\0'; p++)
! 431: if (*p==pathsep)
! 432: *p='\0';
! 433: retvalue=go_forth(loader(image_file, imagename),4,environ);
1.16 pazsan 434: deprep_terminal();
435: exit(retvalue);
436: }
1.1 anton 437: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>