Annotation of gforth/main.c, revision 1.53
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>
1.49 anton 34: #if HAVE_SYS_MMAN_H
35: #include <sys/mman.h>
36: #endif
1.1 anton 37: #include "forth.h"
1.5 pazsan 38: #include "io.h"
1.20 anton 39: #include "getopt.h"
1.45 anton 40: #include "version.h"
1.2 pazsan 41:
1.52 anton 42: #define PRIM_VERSION 1
43: /* increment this whenever the primitives change in an incompatible way */
44:
1.22 pazsan 45: #ifdef MSDOS
46: jmp_buf throw_jmp_buf;
47: #endif
48:
1.10 anton 49: #ifndef DEFAULTPATH
1.16 pazsan 50: # define DEFAULTPATH "/usr/local/lib/gforth:."
1.10 anton 51: #endif
52:
1.1 anton 53: #ifdef DIRECT_THREADED
1.16 pazsan 54: # define CA(n) (symbols[(n)])
1.1 anton 55: #else
1.21 pazsan 56: # define CA(n) ((Cell)(symbols+(n)))
1.1 anton 57: #endif
58:
1.23 pazsan 59: #define maxaligned(n) (typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
1.10 anton 60:
1.50 anton 61: static UCell dictsize=0;
62: static UCell dsize=0;
63: static UCell rsize=0;
64: static UCell fsize=0;
65: static UCell lsize=0;
1.48 anton 66: static int image_offset=0;
1.49 anton 67: static int clear_dictionary=0;
68: static int debug=0;
69: static size_t pagesize=0;
1.10 anton 70: char *progname;
71:
1.1 anton 72: /* image file format:
1.35 anton 73: * "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.2.0 -i\n")
74: * padding to a multiple of 8
1.36 anton 75: * magic: "Gforth1x" means format 0.2,
1.35 anton 76: * where x is even for big endian and odd for little endian
77: * and x & ~1 is the size of the cell in bytes.
78: * padding to max alignment (no padding necessary on current machines)
79: * ImageHeader structure (see below)
80: * data (size in ImageHeader.image_size)
81: * tags ((if relocatable, 1 bit/data cell)
1.1 anton 82: *
1.35 anton 83: * tag==1 means that the corresponding word is an address;
1.1 anton 84: * If the word is >=0, the address is within the image;
85: * addresses within the image are given relative to the start of the image.
1.35 anton 86: * If the word =-1 (CF_NIL), the address is NIL,
87: * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
88: * If the word =CF(DODOES), it's a DOES> CFA
89: * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
90: * possibly containing a jump to dodoes)
91: * If the word is <CF(DOESJUMP), it's a primitive
1.1 anton 92: */
93:
1.35 anton 94: typedef struct {
95: Address base; /* base address of image (0 if relocatable) */
96: UCell checksum; /* checksum of ca's to protect against some
97: incompatible binary/executable combinations
98: (0 if relocatable) */
99: UCell image_size; /* all sizes in bytes */
100: UCell dict_size;
101: UCell data_stack_size;
102: UCell fp_stack_size;
103: UCell return_stack_size;
104: UCell locals_stack_size;
105: Xt *boot_entry; /* initial ip for booting (in BOOT) */
106: Xt *throw_entry; /* ip after signal (in THROW) */
1.49 anton 107: Cell unused1; /* possibly tib stack size */
108: Cell unused2;
109: Address data_stack_base; /* this and the following fields are initialized by the loader */
110: Address fp_stack_base;
111: Address return_stack_base;
112: Address locals_stack_base;
1.35 anton 113: } ImageHeader;
114: /* the image-header is created in main.fs */
115:
1.10 anton 116: void relocate(Cell *image, char *bitstring, int size, Label symbols[])
1.1 anton 117: {
1.16 pazsan 118: int i=0, j, k, steps=(size/sizeof(Cell))/8;
119: char bits;
1.5 pazsan 120: /* static char bits[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};*/
1.37 pazsan 121:
122: /* printf("relocating %x[%x]\n", image, size); */
1.5 pazsan 123:
1.16 pazsan 124: for(k=0; k<=steps; k++)
125: for(j=0, bits=bitstring[k]; j<8; j++, i++, bits<<=1)
126: if(bits & 0x80)
127: if(image[i]<0)
128: switch(image[i])
129: {
130: case CF_NIL : image[i]=0; break;
131: case CF(DOCOL) :
132: case CF(DOVAR) :
133: case CF(DOCON) :
134: case CF(DOUSER) :
1.24 pazsan 135: case CF(DODEFER) :
1.28 anton 136: case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(image[i])]); break;
1.21 pazsan 137: case CF(DODOES) : MAKE_DOES_CF(image+i,image[i+1]+((Cell)image));
1.16 pazsan 138: break;
139: case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
1.37 pazsan 140: default :
141: /* printf("Code field generation image[%x]:=CA(%x)\n",
142: i, CF(image[i]));
143: */ image[i]=(Cell)CA(CF(image[i]));
1.16 pazsan 144: }
145: else
146: image[i]+=(Cell)image;
1.1 anton 147: }
148:
1.35 anton 149: UCell checksum(Label symbols[])
150: {
1.52 anton 151: UCell r=PRIM_VERSION;
1.35 anton 152: Cell i;
153:
154: for (i=DOCOL; i<=DOESJUMP; i++) {
155: r ^= (UCell)(symbols[i]);
156: r = (r << 5) | (r >> (8*sizeof(Cell)-5));
157: }
158: #ifdef DIRECT_THREADED
159: /* we have to consider all the primitives */
160: for (; symbols[i]!=(Label)0; i++) {
161: r ^= (UCell)(symbols[i]);
162: r = (r << 5) | (r >> (8*sizeof(Cell)-5));
163: }
164: #else
165: /* in indirect threaded code all primitives are accessed through the
166: symbols table, so we just have to put the base address of symbols
167: in the checksum */
168: r ^= (UCell)symbols;
169: #endif
170: return r;
171: }
172:
1.49 anton 173: Address my_alloc(Cell size)
174: {
175: static Address next_address=0;
176: Address r;
177:
178: #if HAVE_MMAP && defined(MAP_ANON)
179: if (debug)
180: fprintf(stderr,"try mmap($%lx, $%lx, ...); ", (long)next_address, (long)size);
181: r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
182: if (r != (Address)-1) {
183: if (debug)
184: fprintf(stderr, "success, address=$%lx\n", (long) r);
185: if (pagesize != 0)
186: next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */
187: return r;
188: }
189: if (debug)
190: fprintf(stderr, "failed: %s\n", strerror(errno));
191: #endif
192: /* use malloc as fallback, leave a little room (64B) for stack underflows */
193: if ((r = malloc(size+64))==NULL) {
194: perror(progname);
195: exit(1);
196: }
1.53 ! pazsan 197: r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
1.49 anton 198: if (debug)
199: fprintf(stderr, "malloc succeeds, address=$%lx\n", (long)r);
200: return r;
201: }
202:
1.39 pazsan 203: Address loader(FILE *imagefile, char* filename)
1.35 anton 204: /* returns the address of the image proper (after the preamble) */
1.10 anton 205: {
1.35 anton 206: ImageHeader header;
207: Address image;
208: Address imp; /* image+preamble */
1.53 ! pazsan 209: Char magic[9];
1.35 anton 210: Cell preamblesize=0;
211: Label *symbols=engine(0,0,0,0,0);
212: UCell check_sum=checksum(symbols);
1.18 pazsan 213:
1.35 anton 214: static char* endianstring[]= { "big","little" };
1.25 pazsan 215:
1.18 pazsan 216: do
217: {
218: if(fread(magic,sizeof(Char),8,imagefile) < 8) {
1.39 pazsan 219: fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.2) image.\n",
220: progname, filename);
1.18 pazsan 221: exit(1);
222: }
1.35 anton 223: preamblesize+=8;
1.18 pazsan 224: }
1.35 anton 225: while(memcmp(magic,"Gforth1",7));
1.53 ! pazsan 226: if (debug) {
! 227: magic[8]='\0';
! 228: fprintf(stderr,"Magic found: %s\n", magic);
! 229: }
! 230:
1.35 anton 231: if(magic[7] != sizeof(Cell) +
1.25 pazsan 232: #ifdef WORDS_BIGENDIAN
233: '0'
234: #else
235: '1'
236: #endif
1.35 anton 237: )
238: { fprintf(stderr,"This image is %d bit %s-endian, whereas the machine is %d bit %s-endian.\n",
239: ((magic[7]-'0')&~1)*8, endianstring[magic[7]&1],
240: sizeof(Cell)*8, endianstring[
1.25 pazsan 241: #ifdef WORDS_BIGENDIAN
242: 0
243: #else
244: 1
245: #endif
246: ]);
247: exit(-2);
248: };
1.18 pazsan 249:
1.35 anton 250: fread((void *)&header,sizeof(ImageHeader),1,imagefile);
1.16 pazsan 251: if (dictsize==0)
1.35 anton 252: dictsize = header.dict_size;
1.16 pazsan 253: if (dsize==0)
1.35 anton 254: dsize=header.data_stack_size;
1.16 pazsan 255: if (rsize==0)
1.35 anton 256: rsize=header.return_stack_size;
1.16 pazsan 257: if (fsize==0)
1.35 anton 258: fsize=header.fp_stack_size;
1.16 pazsan 259: if (lsize==0)
1.35 anton 260: lsize=header.locals_stack_size;
1.16 pazsan 261: dictsize=maxaligned(dictsize);
262: dsize=maxaligned(dsize);
263: rsize=maxaligned(rsize);
264: lsize=maxaligned(lsize);
265: fsize=maxaligned(fsize);
266:
1.49 anton 267: #if HAVE_GETPAGESIZE
268: pagesize=getpagesize(); /* Linux/GNU libc offers this */
269: #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
270: pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
271: #elif PAGESIZE
272: pagesize=PAGESIZE; /* in limits.h accoring to Gallmeister's POSIX.4 book */
273: #endif
274: if (debug)
275: fprintf(stderr,"pagesize=%d\n",pagesize);
276:
1.51 anton 277: image = my_alloc(preamblesize+dictsize+image_offset)+image_offset;
1.35 anton 278: rewind(imagefile); /* fseek(imagefile,0L,SEEK_SET); */
1.49 anton 279: if (clear_dictionary)
280: memset(image,0,dictsize);
281: fread(image,1,preamblesize+header.image_size,imagefile);
1.35 anton 282: imp=image+preamblesize;
283: if(header.base==0) {
1.49 anton 284: Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
285: char reloc_bits[reloc_size];
286: fread(reloc_bits,1,reloc_size,imagefile);
287: relocate((Cell *)imp,reloc_bits,header.image_size,symbols);
1.48 anton 288: #if 0
289: { /* let's see what the relocator did */
290: FILE *snapshot=fopen("snapshot.fi","wb");
291: fwrite(image,1,imagesize,snapshot);
292: fclose(snapshot);
293: }
294: #endif
1.16 pazsan 295: }
1.35 anton 296: else if(header.base!=imp) {
1.51 anton 297: fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
1.35 anton 298: progname, (unsigned long)header.base, (unsigned long)imp);
299: exit(1);
1.48 anton 300: }
301: if (header.checksum==0)
302: ((ImageHeader *)imp)->checksum=check_sum;
303: else if (header.checksum != check_sum) {
1.36 anton 304: 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 305: progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
1.16 pazsan 306: exit(1);
307: }
1.49 anton 308: fclose(imagefile);
1.1 anton 309:
1.35 anton 310: ((ImageHeader *)imp)->dict_size=dictsize;
311: ((ImageHeader *)imp)->data_stack_size=dsize;
1.49 anton 312: ((ImageHeader *)imp)->fp_stack_size=fsize;
1.35 anton 313: ((ImageHeader *)imp)->return_stack_size=rsize;
314: ((ImageHeader *)imp)->locals_stack_size=lsize;
315:
1.49 anton 316: ((ImageHeader *)imp)->data_stack_base=my_alloc(dsize);
317: ((ImageHeader *)imp)->fp_stack_base=my_alloc(fsize);
318: ((ImageHeader *)imp)->return_stack_base=my_alloc(rsize);
319: ((ImageHeader *)imp)->locals_stack_base=my_alloc(lsize);
320:
1.38 anton 321: CACHE_FLUSH(imp, header.image_size);
1.35 anton 322:
323: return imp;
1.1 anton 324: }
325:
1.35 anton 326: int go_forth(Address image, int stack, Cell *entries)
1.1 anton 327: {
1.49 anton 328: Cell *sp=(Cell*)(((ImageHeader *)image)->data_stack_base + dsize);
329: Float *fp=(Float *)(((ImageHeader *)image)->fp_stack_base + fsize);
330: Cell *rp=(Cell *)(((ImageHeader *)image)->return_stack_base + rsize);
331: Address lp=((ImageHeader *)image)->locals_stack_base + lsize;
1.35 anton 332: Xt *ip=(Xt *)(((ImageHeader *)image)->boot_entry);
1.15 anton 333: int throw_code;
1.49 anton 334:
335: /* ensure that the cached elements (if any) are accessible */
336: IF_TOS(sp--);
337: IF_FTOS(fp--);
1.15 anton 338:
339: for(;stack>0;stack--)
340: *--sp=entries[stack-1];
1.39 pazsan 341:
1.44 pazsan 342: #if !defined(MSDOS) && !defined(_WIN32) && !defined(__EMX__)
1.33 anton 343: get_winsize();
1.39 pazsan 344: #endif
345:
1.15 anton 346: install_signal_handlers(); /* right place? */
347:
348: if ((throw_code=setjmp(throw_jmp_buf))) {
349: static Cell signal_data_stack[8];
350: static Cell signal_return_stack[8];
351: static Float signal_fp_stack[1];
352:
353: signal_data_stack[7]=throw_code;
354:
1.35 anton 355: return((int)engine(((ImageHeader *)image)->throw_entry,signal_data_stack+7,
1.15 anton 356: signal_return_stack+8,signal_fp_stack,0));
357: }
1.35 anton 358:
1.15 anton 359: return((int)engine(ip,sp,rp,fp,lp));
1.1 anton 360: }
361:
1.50 anton 362: UCell convsize(char *s, UCell elemsize)
1.10 anton 363: /* converts s of the format #+u (e.g. 25k) into the number of bytes.
364: the unit u can be one of bekM, where e stands for the element
365: size. default is e */
366: {
367: char *endp;
1.50 anton 368: UCell n,m;
1.10 anton 369:
370: m = elemsize;
371: n = strtoul(s,&endp,0);
372: if (endp!=NULL) {
373: if (strcmp(endp,"b")==0)
374: m=1;
375: else if (strcmp(endp,"k")==0)
376: m=1024;
377: else if (strcmp(endp,"M")==0)
378: m=1024*1024;
1.31 anton 379: else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
380: fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
1.10 anton 381: exit(1);
382: }
383: }
384: return n*m;
385: }
386:
1.1 anton 387: int main(int argc, char **argv, char **env)
388: {
1.43 anton 389: char *path, *path1;
1.16 pazsan 390: char *imagename="gforth.fi";
391: FILE *image_file;
392: int c, retvalue;
1.10 anton 393:
1.14 anton 394: #if defined(i386) && defined(ALIGNMENT_CHECK) && !defined(DIRECT_THREADED)
1.30 anton 395: /* turn on alignment checks on the 486.
396: * on the 386 this should have no effect. */
397: __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
398: /* this is unusable with Linux' libc.4.6.27, because this library is
399: not alignment-clean; we would have to replace some library
400: functions (e.g., memcpy) to make it work */
1.6 anton 401: #endif
1.2 pazsan 402:
1.16 pazsan 403: progname = argv[0];
1.44 pazsan 404: if ((path1=getenv("GFORTHPATH"))==NULL)
405: path1 = DEFAULTPATH;
406:
1.16 pazsan 407: opterr=0;
408: while (1) {
409: int option_index=0;
410: static struct option opts[] = {
411: {"image-file", required_argument, NULL, 'i'},
412: {"dictionary-size", required_argument, NULL, 'm'},
413: {"data-stack-size", required_argument, NULL, 'd'},
414: {"return-stack-size", required_argument, NULL, 'r'},
415: {"fp-stack-size", required_argument, NULL, 'f'},
416: {"locals-stack-size", required_argument, NULL, 'l'},
417: {"path", required_argument, NULL, 'p'},
1.45 anton 418: {"version", no_argument, NULL, 'v'},
419: {"help", no_argument, NULL, 'h'},
1.53 ! pazsan 420: {"clear-dictionary", no_argument, NULL, 'c'},
1.48 anton 421: /* put something != 0 into image_offset; it should be a
422: not-too-large max-aligned number */
1.53 ! pazsan 423: {"offset-image", no_argument, NULL, 'o'},
1.49 anton 424: {"debug", no_argument, &debug, 1},
1.16 pazsan 425: {0,0,0,0}
426: /* no-init-file, no-rc? */
427: };
1.44 pazsan 428:
1.53 ! pazsan 429: c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vhco", opts, &option_index);
1.44 pazsan 430:
1.16 pazsan 431: if (c==EOF)
432: break;
433: if (c=='?') {
434: optind--;
435: break;
436: }
437: switch (c) {
1.53 ! pazsan 438: case 'c': clear_dictionary=1; break;
! 439: case 'o': image_offset=28*sizeof(Cell); break;
1.16 pazsan 440: case 'i': imagename = optarg; break;
441: case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
442: case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
443: case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
444: case 'f': fsize = convsize(optarg,sizeof(Float)); break;
445: case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
1.44 pazsan 446: case 'p': path1 = optarg; break;
1.45 anton 447: case 'v': fprintf(stderr, "gforth %s\n", gforth_version); exit(0);
448: case 'h':
449: fprintf(stderr, "Usage: %s [engine options] [image arguments]\n\
450: Engine Options:\n\
1.53 ! pazsan 451: -c, --clear-dictionary Initialize the dictionary with 0 bytes\n\
1.45 anton 452: -d SIZE, --data-stack-size=SIZE Specify data stack size\n\
1.49 anton 453: --debug Print debugging information during startup\n\
1.45 anton 454: -f SIZE, --fp-stack-size=SIZE Specify floating point stack size\n\
455: -h, --help Print this message and exit\n\
456: -i FILE, --image-file=FILE Use image FILE instead of `gforth.fi'\n\
457: -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
458: -m SIZE, --dictionary-size=SIZE Specify Forth dictionary size\n\
1.49 anton 459: --offset-image Load image at a different position\n\
1.45 anton 460: -p PATH, --path=PATH Search path for finding image and sources\n\
461: -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
462: -v, --version Print version and exit\n\
463: SIZE arguments consists of an integer followed by a unit. The unit can be\n\
464: `b' (bytes), `e' (elements), `k' (kilobytes), or `M' (Megabytes).\n\
465: \n\
466: Arguments of default image `gforth.fi':\n\
467: FILE load FILE (with `require')\n\
468: -e STRING, --evaluate STRING interpret STRING (with `EVALUATE')\n",
469: argv[0]); exit(0);
1.16 pazsan 470: }
471: }
1.44 pazsan 472: path=path1;
473:
1.29 pazsan 474: if(strchr(imagename, '/')==NULL)
475: {
476: do {
1.43 anton 477: char *pend=strchr(path, PATHSEP);
1.29 pazsan 478: if (pend==NULL)
479: pend=path+strlen(path);
480: if (strlen(path)==0) {
481: fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
1.47 anton 482: progname, imagename, path1);
1.29 pazsan 483: exit(1);
484: }
485: {
486: int dirlen=pend-path;
487: char fullfilename[dirlen+strlen(imagename)+2];
488: memcpy(fullfilename, path, dirlen);
489: if (fullfilename[dirlen-1]!='/')
490: fullfilename[dirlen++]='/';
491: strcpy(fullfilename+dirlen,imagename);
492: image_file=fopen(fullfilename,"rb");
493: }
1.43 anton 494: path=pend+(*pend==PATHSEP);
1.29 pazsan 495: } while (image_file==NULL);
1.16 pazsan 496: }
1.29 pazsan 497: else
1.16 pazsan 498: {
1.29 pazsan 499: image_file=fopen(imagename,"rb");
1.16 pazsan 500: }
1.29 pazsan 501:
1.16 pazsan 502: {
1.45 anton 503: char path2[strlen(path1)+1];
1.43 anton 504: char *p1, *p2;
1.16 pazsan 505: Cell environ[]= {
506: (Cell)argc-(optind-1),
507: (Cell)(argv+(optind-1)),
1.42 anton 508: (Cell)strlen(path1),
1.43 anton 509: (Cell)path2};
1.16 pazsan 510: argv[optind-1] = progname;
511: /*
512: for (i=0; i<environ[0]; i++)
513: printf("%s\n", ((char **)(environ[1]))[i]);
514: */
1.42 anton 515: /* make path OS-independent by replacing path separators with NUL */
1.43 anton 516: for (p1=path1, p2=path2; *p1!='\0'; p1++, p2++)
517: if (*p1==PATHSEP)
518: *p2 = '\0';
519: else
520: *p2 = *p1;
521: *p2='\0';
1.42 anton 522: retvalue=go_forth(loader(image_file, imagename),4,environ);
1.16 pazsan 523: deprep_terminal();
524: exit(retvalue);
525: }
1.1 anton 526: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>