Annotation of gforth/engine/main.c, revision 1.52
1.1 anton 1: /* command line interpretation, image loading etc. for Gforth
2:
3:
1.39 anton 4: Copyright (C) 1995,1996,1997,1998,2000 Free Software Foundation, Inc.
1.1 anton 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
1.40 anton 20: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.1 anton 21: */
22:
23: #include "config.h"
24: #include <errno.h>
25: #include <ctype.h>
26: #include <stdio.h>
1.2 pazsan 27: #include <unistd.h>
1.1 anton 28: #include <string.h>
29: #include <math.h>
30: #include <sys/types.h>
1.32 pazsan 31: #ifndef STANDALONE
1.1 anton 32: #include <sys/stat.h>
1.32 pazsan 33: #endif
1.1 anton 34: #include <fcntl.h>
35: #include <assert.h>
36: #include <stdlib.h>
1.11 pazsan 37: #ifndef STANDALONE
1.1 anton 38: #if HAVE_SYS_MMAN_H
39: #include <sys/mman.h>
40: #endif
1.11 pazsan 41: #endif
1.1 anton 42: #include "forth.h"
43: #include "io.h"
44: #include "getopt.h"
1.11 pazsan 45: #ifdef STANDALONE
46: #include <systypes.h>
47: #endif
1.1 anton 48:
49: #define PRIM_VERSION 1
50: /* increment this whenever the primitives change in an incompatible way */
51:
1.14 pazsan 52: #ifndef DEFAULTPATH
1.39 anton 53: # define DEFAULTPATH "."
1.14 pazsan 54: #endif
55:
1.1 anton 56: #ifdef MSDOS
57: jmp_buf throw_jmp_buf;
58: #endif
59:
60: #if defined(DIRECT_THREADED)
61: # define CA(n) (symbols[(n)])
62: #else
63: # define CA(n) ((Cell)(symbols+(n)))
64: #endif
65:
66: #define maxaligned(n) (typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
67:
68: static UCell dictsize=0;
69: static UCell dsize=0;
70: static UCell rsize=0;
71: static UCell fsize=0;
72: static UCell lsize=0;
73: int offset_image=0;
1.4 anton 74: int die_on_signal=0;
1.13 pazsan 75: #ifndef INCLUDE_IMAGE
1.1 anton 76: static int clear_dictionary=0;
1.24 anton 77: UCell pagesize=1;
1.22 pazsan 78: char *progname;
79: #else
80: char *progname = "gforth";
81: int optind = 1;
1.13 pazsan 82: #endif
1.31 pazsan 83:
1.48 anton 84: Address code_area=0;
85: Address code_here=0; /* does for code-area what HERE does for the dictionary */
86:
1.30 pazsan 87: #ifdef HAS_DEBUG
1.1 anton 88: static int debug=0;
1.31 pazsan 89: #else
90: # define debug 0
91: # define perror(x...)
92: # define fprintf(x...)
1.30 pazsan 93: #endif
1.31 pazsan 94:
1.24 anton 95: ImageHeader *gforth_header;
1.43 anton 96: Label *vm_prims;
1.1 anton 97:
1.30 pazsan 98: #ifdef MEMCMP_AS_SUBROUTINE
99: int gforth_memcmp(const char * s1, const char * s2, size_t n)
100: {
101: return memcmp(s1, s2, n);
102: }
103: #endif
104:
1.1 anton 105: /* image file format:
1.15 pazsan 106: * "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.4.0 -i\n")
1.1 anton 107: * padding to a multiple of 8
1.15 pazsan 108: * magic: "Gforth2x" means format 0.4,
109: * where x is a byte with
110: * bit 7: reserved = 0
111: * bit 6:5: address unit size 2^n octets
112: * bit 4:3: character size 2^n octets
113: * bit 2:1: cell size 2^n octets
114: * bit 0: endian, big=0, little=1.
115: * The magic are always 8 octets, no matter what the native AU/character size is
1.1 anton 116: * padding to max alignment (no padding necessary on current machines)
1.24 anton 117: * ImageHeader structure (see forth.h)
1.1 anton 118: * data (size in ImageHeader.image_size)
119: * tags ((if relocatable, 1 bit/data cell)
120: *
121: * tag==1 means that the corresponding word is an address;
122: * If the word is >=0, the address is within the image;
123: * addresses within the image are given relative to the start of the image.
124: * If the word =-1 (CF_NIL), the address is NIL,
125: * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
126: * If the word =CF(DODOES), it's a DOES> CFA
127: * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
128: * possibly containing a jump to dodoes)
1.51 anton 129: * If the word is <CF(DOESJUMP) and bit 14 is set, it's the xt of a primitive
130: * If the word is <CF(DOESJUMP) and bit 14 is clear,
131: * it's the threaded code of a primitive
1.1 anton 132: */
133:
1.46 jwilke 134: void relocate(Cell *image, const char *bitstring,
135: int size, int base, Label symbols[])
1.1 anton 136: {
1.16 pazsan 137: int i=0, j, k, steps=(size/sizeof(Cell))/RELINFOBITS;
1.11 pazsan 138: Cell token;
1.1 anton 139: char bits;
1.37 anton 140: Cell max_symbols;
1.46 jwilke 141: /*
142: * A virtial start address that's the real start address minus
143: * the one in the image
144: */
1.45 jwilke 145: Cell *start = (Cell * ) (((void *) image) - ((void *) base));
1.1 anton 146:
1.46 jwilke 147:
148: /* printf("relocating to %x[%x] start=%x base=%x\n", image, size, start, base); */
1.37 anton 149:
150: for (max_symbols=DOESJUMP+1; symbols[max_symbols]!=0; max_symbols++)
151: ;
1.47 anton 152: max_symbols--;
1.35 pazsan 153: size/=sizeof(Cell);
154:
1.31 pazsan 155: for(k=0; k<=steps; k++) {
1.13 pazsan 156: for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
1.1 anton 157: /* fprintf(stderr,"relocate: image[%d]\n", i);*/
1.35 pazsan 158: if((i < size) && (bits & (1U << (RELINFOBITS-1)))) {
159: /* fprintf(stderr,"relocate: image[%d]=%d of %d\n", i, image[i], size/sizeof(Cell)); */
1.45 jwilke 160: token=image[i];
161: if(token<0)
1.11 pazsan 162: switch(token)
1.1 anton 163: {
164: case CF_NIL : image[i]=0; break;
165: #if !defined(DOUBLY_INDIRECT)
166: case CF(DOCOL) :
167: case CF(DOVAR) :
168: case CF(DOCON) :
169: case CF(DOUSER) :
170: case CF(DODEFER) :
1.11 pazsan 171: case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(token)]); break;
1.1 anton 172: case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
173: #endif /* !defined(DOUBLY_INDIRECT) */
174: case CF(DODOES) :
1.45 jwilke 175: MAKE_DOES_CF(image+i,(Xt *)(image[i+1]+((Cell)start)));
1.1 anton 176: break;
177: default :
178: /* printf("Code field generation image[%x]:=CA(%x)\n",
179: i, CF(image[i])); */
1.52 ! pazsan 180: #if !defined(DOUBLY_INDIRECT)
! 181: if (((token | 0x4000) >= CF(DODOES)) && (token < -0x4000))
! 182: fprintf(stderr,"Doer %d used in this image at $%lx is marked as Xt; executing this code will crash.\n",CF((token | 0x4000)),(long)&image[i],VERSION);
! 183: #endif
1.51 anton 184: token |= 0x4000; /* only meaningful for hybrid engines */
1.37 anton 185: if (CF(token)<max_symbols)
186: image[i]=(Cell)CA(CF(token));
187: else
188: fprintf(stderr,"Primitive %d used in this image at $%lx is not implemented by this\n engine (%s); executing this code will crash.\n",CF(token),(long)&image[i],VERSION);
1.1 anton 189: }
1.46 jwilke 190: else {
1.45 jwilke 191: // if base is > 0: 0 is a null reference so don't adjust
192: if (token>=base) {
193: image[i]+=(Cell)start;
194: }
1.46 jwilke 195: }
1.1 anton 196: }
197: }
1.31 pazsan 198: }
1.26 jwilke 199: ((ImageHeader*)(image))->base = (Address) image;
1.1 anton 200: }
201:
202: UCell checksum(Label symbols[])
203: {
204: UCell r=PRIM_VERSION;
205: Cell i;
206:
207: for (i=DOCOL; i<=DOESJUMP; i++) {
208: r ^= (UCell)(symbols[i]);
209: r = (r << 5) | (r >> (8*sizeof(Cell)-5));
210: }
211: #ifdef DIRECT_THREADED
212: /* we have to consider all the primitives */
213: for (; symbols[i]!=(Label)0; i++) {
214: r ^= (UCell)(symbols[i]);
215: r = (r << 5) | (r >> (8*sizeof(Cell)-5));
216: }
217: #else
218: /* in indirect threaded code all primitives are accessed through the
219: symbols table, so we just have to put the base address of symbols
220: in the checksum */
221: r ^= (UCell)symbols;
222: #endif
223: return r;
224: }
225:
1.3 anton 226: Address verbose_malloc(Cell size)
227: {
228: Address r;
229: /* leave a little room (64B) for stack underflows */
230: if ((r = malloc(size+64))==NULL) {
231: perror(progname);
232: exit(1);
233: }
234: r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
235: if (debug)
236: fprintf(stderr, "malloc succeeds, address=$%lx\n", (long)r);
237: return r;
238: }
239:
1.33 anton 240: static Address next_address=0;
241: void after_alloc(Address r, Cell size)
242: {
243: if (r != (Address)-1) {
244: if (debug)
245: fprintf(stderr, "success, address=$%lx\n", (long) r);
246: if (pagesize != 1)
247: next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */
248: } else {
249: if (debug)
250: fprintf(stderr, "failed: %s\n", strerror(errno));
251: }
252: }
253:
1.34 anton 254: #ifndef MAP_FAILED
255: #define MAP_FAILED ((Address) -1)
256: #endif
257: #ifndef MAP_FILE
258: # define MAP_FILE 0
259: #endif
260: #ifndef MAP_PRIVATE
261: # define MAP_PRIVATE 0
262: #endif
263:
264: #if defined(HAVE_MMAP)
265: static Address alloc_mmap(Cell size)
1.1 anton 266: {
267: Address r;
268:
269: #if defined(MAP_ANON)
270: if (debug)
271: fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_ANON, ...); ", (long)next_address, (long)size);
1.34 anton 272: r = mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
1.1 anton 273: #else /* !defined(MAP_ANON) */
1.17 anton 274: /* Ultrix (at least) does not define MAP_FILE and MAP_PRIVATE (both are
275: apparently defaults) */
1.1 anton 276: static int dev_zero=-1;
277:
278: if (dev_zero == -1)
279: dev_zero = open("/dev/zero", O_RDONLY);
280: if (dev_zero == -1) {
1.34 anton 281: r = MAP_FAILED;
1.1 anton 282: if (debug)
283: fprintf(stderr, "open(\"/dev/zero\"...) failed (%s), no mmap; ",
284: strerror(errno));
285: } else {
286: if (debug)
287: fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FILE, dev_zero, ...); ", (long)next_address, (long)size);
288: r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE, dev_zero, 0);
289: }
290: #endif /* !defined(MAP_ANON) */
1.34 anton 291: after_alloc(r, size);
292: return r;
293: }
294: #endif
295:
296: Address my_alloc(Cell size)
297: {
298: #if HAVE_MMAP
299: Address r;
300:
301: r=alloc_mmap(size);
302: if (r!=MAP_FAILED)
1.1 anton 303: return r;
304: #endif /* HAVE_MMAP */
1.3 anton 305: /* use malloc as fallback */
306: return verbose_malloc(size);
1.1 anton 307: }
308:
1.3 anton 309: #if (defined(mips) && !defined(INDIRECT_THREADED))
310: /* the 256MB jump restriction on the MIPS architecture makes the
311: combination of direct threading and mmap unsafe. */
1.33 anton 312: #define mips_dict_alloc 1
1.3 anton 313: #define dict_alloc(size) verbose_malloc(size)
314: #else
315: #define dict_alloc(size) my_alloc(size)
316: #endif
317:
1.34 anton 318: Address dict_alloc_read(FILE *file, Cell imagesize, Cell dictsize, Cell offset)
1.33 anton 319: {
1.34 anton 320: Address image = MAP_FAILED;
1.33 anton 321:
1.34 anton 322: #if defined(HAVE_MMAP) && !defined(mips_dict_alloc)
1.33 anton 323: if (offset==0) {
1.34 anton 324: image=alloc_mmap(dictsize);
1.33 anton 325: if (debug)
1.34 anton 326: fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FIXED|MAP_FILE, imagefile, 0); ", (long)image, (long)imagesize);
327: image = mmap(image, imagesize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FIXED|MAP_FILE|MAP_PRIVATE, fileno(file), 0);
328: after_alloc(image,dictsize);
1.33 anton 329: }
1.34 anton 330: #endif /* defined(MAP_ANON) && !defined(mips_dict_alloc) */
331: if (image == MAP_FAILED) {
332: image = dict_alloc(dictsize+offset)+offset;
1.33 anton 333: rewind(file); /* fseek(imagefile,0L,SEEK_SET); */
1.34 anton 334: fread(image, 1, imagesize, file);
1.33 anton 335: }
336: return image;
337: }
338:
1.10 pazsan 339: void set_stack_sizes(ImageHeader * header)
340: {
341: if (dictsize==0)
342: dictsize = header->dict_size;
343: if (dsize==0)
344: dsize = header->data_stack_size;
345: if (rsize==0)
346: rsize = header->return_stack_size;
347: if (fsize==0)
348: fsize = header->fp_stack_size;
349: if (lsize==0)
350: lsize = header->locals_stack_size;
351: dictsize=maxaligned(dictsize);
352: dsize=maxaligned(dsize);
353: rsize=maxaligned(rsize);
354: lsize=maxaligned(lsize);
355: fsize=maxaligned(fsize);
356: }
357:
358: void alloc_stacks(ImageHeader * header)
359: {
360: header->dict_size=dictsize;
361: header->data_stack_size=dsize;
362: header->fp_stack_size=fsize;
363: header->return_stack_size=rsize;
364: header->locals_stack_size=lsize;
365:
366: header->data_stack_base=my_alloc(dsize);
367: header->fp_stack_base=my_alloc(fsize);
368: header->return_stack_base=my_alloc(rsize);
369: header->locals_stack_base=my_alloc(lsize);
1.48 anton 370: code_here = code_area = my_alloc(dictsize);
1.10 pazsan 371: }
372:
1.44 pazsan 373: #warning You can ignore the warnings about clobbered variables in go_forth
1.11 pazsan 374: int go_forth(Address image, int stack, Cell *entries)
375: {
1.38 anton 376: volatile ImageHeader *image_header = (ImageHeader *)image;
1.18 anton 377: Cell *sp0=(Cell*)(image_header->data_stack_base + dsize);
1.44 pazsan 378: Cell *rp0=(Cell *)(image_header->return_stack_base + rsize);
1.18 anton 379: Float *fp0=(Float *)(image_header->fp_stack_base + fsize);
1.44 pazsan 380: #ifdef GFORTH_DEBUGGING
1.38 anton 381: volatile Cell *orig_rp0=rp0;
1.44 pazsan 382: #endif
1.18 anton 383: Address lp0=image_header->locals_stack_base + lsize;
384: Xt *ip0=(Xt *)(image_header->boot_entry);
1.13 pazsan 385: #ifdef SYSSIGNALS
1.11 pazsan 386: int throw_code;
1.13 pazsan 387: #endif
1.11 pazsan 388:
389: /* ensure that the cached elements (if any) are accessible */
1.41 anton 390: IF_spTOS(sp0--);
391: IF_fpTOS(fp0--);
1.11 pazsan 392:
393: for(;stack>0;stack--)
1.18 anton 394: *--sp0=entries[stack-1];
1.11 pazsan 395:
1.30 pazsan 396: #ifdef SYSSIGNALS
1.11 pazsan 397: get_winsize();
398:
399: install_signal_handlers(); /* right place? */
400:
401: if ((throw_code=setjmp(throw_jmp_buf))) {
402: static Cell signal_data_stack[8];
403: static Cell signal_return_stack[8];
404: static Float signal_fp_stack[1];
1.13 pazsan 405:
1.11 pazsan 406: signal_data_stack[7]=throw_code;
1.18 anton 407:
408: #ifdef GFORTH_DEBUGGING
1.38 anton 409: /* fprintf(stderr,"\nrp=%ld\n",(long)rp); */
410: if (rp <= orig_rp0 && rp > (Cell *)(image_header->return_stack_base+5)) {
1.18 anton 411: /* no rstack overflow or underflow */
412: rp0 = rp;
1.27 anton 413: *--rp0 = (Cell)ip;
1.18 anton 414: }
415: else /* I love non-syntactic ifdefs :-) */
416: #endif
417: rp0 = signal_return_stack+8;
1.25 anton 418: /* fprintf(stderr, "rp=$%x\n",rp0);*/
1.11 pazsan 419:
1.33 anton 420: return((int)(Cell)engine(image_header->throw_entry, signal_data_stack+7,
1.18 anton 421: rp0, signal_fp_stack, 0));
1.11 pazsan 422: }
1.13 pazsan 423: #endif
1.11 pazsan 424:
1.33 anton 425: return((int)(Cell)engine(ip0,sp0,rp0,fp0,lp0));
1.11 pazsan 426: }
427:
1.21 anton 428:
1.30 pazsan 429: #ifndef INCLUDE_IMAGE
1.21 anton 430: void print_sizes(Cell sizebyte)
431: /* print size information */
432: {
433: static char* endianstring[]= { " big","little" };
434:
435: fprintf(stderr,"%s endian, cell=%d bytes, char=%d bytes, au=%d bytes\n",
436: endianstring[sizebyte & 1],
437: 1 << ((sizebyte >> 1) & 3),
438: 1 << ((sizebyte >> 3) & 3),
439: 1 << ((sizebyte >> 5) & 3));
440: }
441:
1.47 anton 442: typedef struct {
443: Label start;
444: Cell length; /* excluding the jump */
445: char super_end; /* true if primitive ends superinstruction, i.e.,
446: unconditional branch, execute, etc. */
447: } PrimInfo;
448:
449: PrimInfo *priminfos;
1.48 anton 450: Cell npriminfos=0;
1.47 anton 451:
452: void check_prims(Label symbols1[])
453: {
1.50 anton 454: #if defined(IS_NEXT_JUMP) && !defined(DOUBLY_INDIRECT)
1.47 anton 455: int i;
456: Label *symbols2=engine2(0,0,0,0,0);
1.49 anton 457: static char superend[]={
1.48 anton 458: #include "prim_superend.i"
459: };
1.47 anton 460:
461: for (i=DOESJUMP+1; symbols1[i+1]!=0; i++)
462: ;
463: priminfos = calloc(i,sizeof(PrimInfo));
1.48 anton 464: npriminfos = i;
1.47 anton 465: for (i=DOESJUMP+1; symbols1[i+1]!=0; i++) {
466: int prim_len=symbols1[i+1]-symbols1[i];
467: PrimInfo *pi=&priminfos[i];
468: int j;
1.48 anton 469: pi->super_end = superend[i-DOESJUMP-1];
1.50 anton 470: for (j=prim_len-IND_JUMP_LENGTH; ; j--) {
471: if (IS_NEXT_JUMP(symbols1[i]+j)) {
1.47 anton 472: prim_len = j;
1.48 anton 473: if (pi->super_end)
1.50 anton 474: prim_len += IND_JUMP_LENGTH; /* include the jump */
1.47 anton 475: break;
476: }
477: if (j==0) { /* NEXT jump not found, e.g., execute */
1.48 anton 478: if (!pi->super_end && debug)
479: fprintf(stderr, "NEXT jump not found for primitive %d, making it super_end\n", i);
480: pi->super_end = 1;
1.47 anton 481: break;
482: }
483: }
484: /* fprintf(stderr,"checking primitive %d: memcmp(%p, %p, %d)\n",
485: i, symbols1[i], symbols2[i], prim_len);*/
486: if (memcmp(symbols1[i],symbols2[i],prim_len)!=0) {
487: if (debug)
488: fprintf(stderr,"Primitive %d not relocatable: memcmp(%p, %p, %d)\n",
489: i, symbols1[i], symbols2[i], prim_len);
490: } else {
491: pi->start = symbols1[i];
492: pi->length = prim_len;
493: if (debug)
494: fprintf(stderr,"Primitive %d relocatable: start %p, length %ld, super_end %d\n",
495: i, pi->start, pi->length, pi->super_end);
496: }
1.48 anton 497: }
498: #endif
499: }
500:
501: Label compile_prim(Label prim)
502: {
1.50 anton 503: #ifdef IND_JUMP_LENGTH
1.48 anton 504: int i;
505: Address old_code_here=code_here;
506: static Address last_jump=0;
507:
508: for (i=0; ; i++) {
509: if (i>=npriminfos) { /* not a relocatable prim */
510: if (last_jump) { /* make sure the last sequence is complete */
1.50 anton 511: memcpy(code_here, last_jump, IND_JUMP_LENGTH);
512: code_here += IND_JUMP_LENGTH;
1.48 anton 513: last_jump = 0;
514: }
515: return prim;
516: }
517: if (priminfos[i].start==prim)
518: break;
1.47 anton 519: }
1.50 anton 520: #ifdef ALIGN_CODE
521: ALIGN_CODE;
522: #endif
1.48 anton 523: memcpy(code_here, (Address)prim, priminfos[i].length);
524: code_here += priminfos[i].length;
525: last_jump = (priminfos[i].super_end) ? 0 : (prim+priminfos[i].length);
526: return (Label)old_code_here;
1.50 anton 527: #else
528: return prim;
529: #endif
1.47 anton 530: }
531:
1.1 anton 532: Address loader(FILE *imagefile, char* filename)
533: /* returns the address of the image proper (after the preamble) */
534: {
535: ImageHeader header;
536: Address image;
537: Address imp; /* image+preamble */
1.17 anton 538: Char magic[8];
539: char magic7; /* size byte of magic number */
1.1 anton 540: Cell preamblesize=0;
1.6 pazsan 541: Cell data_offset = offset_image ? 56*sizeof(Cell) : 0;
1.1 anton 542: UCell check_sum;
1.15 pazsan 543: Cell ausize = ((RELINFOBITS == 8) ? 0 :
544: (RELINFOBITS == 16) ? 1 :
545: (RELINFOBITS == 32) ? 2 : 3);
546: Cell charsize = ((sizeof(Char) == 1) ? 0 :
547: (sizeof(Char) == 2) ? 1 :
548: (sizeof(Char) == 4) ? 2 : 3) + ausize;
549: Cell cellsize = ((sizeof(Cell) == 1) ? 0 :
550: (sizeof(Cell) == 2) ? 1 :
551: (sizeof(Cell) == 4) ? 2 : 3) + ausize;
1.21 anton 552: Cell sizebyte = (ausize << 5) + (charsize << 3) + (cellsize << 1) +
553: #ifdef WORDS_BIGENDIAN
554: 0
555: #else
556: 1
557: #endif
558: ;
1.1 anton 559:
1.43 anton 560: vm_prims = engine(0,0,0,0,0);
1.47 anton 561: check_prims(vm_prims);
1.1 anton 562: #ifndef DOUBLY_INDIRECT
1.43 anton 563: check_sum = checksum(vm_prims);
1.1 anton 564: #else /* defined(DOUBLY_INDIRECT) */
1.43 anton 565: check_sum = (UCell)vm_prims;
1.1 anton 566: #endif /* defined(DOUBLY_INDIRECT) */
1.10 pazsan 567:
568: do {
569: if(fread(magic,sizeof(Char),8,imagefile) < 8) {
1.15 pazsan 570: fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.4) image.\n",
1.10 pazsan 571: progname, filename);
572: exit(1);
1.1 anton 573: }
1.10 pazsan 574: preamblesize+=8;
1.15 pazsan 575: } while(memcmp(magic,"Gforth2",7));
1.17 anton 576: magic7 = magic[7];
1.1 anton 577: if (debug) {
1.17 anton 578: magic[7]='\0';
1.21 anton 579: fprintf(stderr,"Magic found: %s ", magic);
580: print_sizes(magic7);
1.1 anton 581: }
582:
1.21 anton 583: if (magic7 != sizebyte)
584: {
585: fprintf(stderr,"This image is: ");
586: print_sizes(magic7);
587: fprintf(stderr,"whereas the machine is ");
588: print_sizes(sizebyte);
1.1 anton 589: exit(-2);
590: };
591:
592: fread((void *)&header,sizeof(ImageHeader),1,imagefile);
1.10 pazsan 593:
594: set_stack_sizes(&header);
1.1 anton 595:
596: #if HAVE_GETPAGESIZE
597: pagesize=getpagesize(); /* Linux/GNU libc offers this */
598: #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
599: pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
600: #elif PAGESIZE
601: pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
602: #endif
603: if (debug)
1.5 jwilke 604: fprintf(stderr,"pagesize=%ld\n",(unsigned long) pagesize);
1.1 anton 605:
1.34 anton 606: image = dict_alloc_read(imagefile, preamblesize+header.image_size,
607: preamblesize+dictsize, data_offset);
1.33 anton 608: imp=image+preamblesize;
1.1 anton 609: if (clear_dictionary)
1.33 anton 610: memset(imp+header.image_size, 0, dictsize-header.image_size);
1.46 jwilke 611: if(header.base==0 || header.base == 0x100) {
1.1 anton 612: Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
613: char reloc_bits[reloc_size];
1.33 anton 614: fseek(imagefile, preamblesize+header.image_size, SEEK_SET);
1.10 pazsan 615: fread(reloc_bits, 1, reloc_size, imagefile);
1.45 jwilke 616: relocate((Cell *)imp, reloc_bits, header.image_size, header.base, vm_prims);
1.1 anton 617: #if 0
618: { /* let's see what the relocator did */
619: FILE *snapshot=fopen("snapshot.fi","wb");
620: fwrite(image,1,imagesize,snapshot);
621: fclose(snapshot);
622: }
623: #endif
1.46 jwilke 624: }
625: else if(header.base!=imp) {
626: fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
627: progname, (unsigned long)header.base, (unsigned long)imp);
628: exit(1);
1.1 anton 629: }
630: if (header.checksum==0)
631: ((ImageHeader *)imp)->checksum=check_sum;
632: else if (header.checksum != check_sum) {
633: fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
634: progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
635: exit(1);
636: }
637: fclose(imagefile);
638:
1.10 pazsan 639: alloc_stacks((ImageHeader *)imp);
1.1 anton 640:
641: CACHE_FLUSH(imp, header.image_size);
642:
643: return imp;
644: }
645:
1.28 anton 646: /* index of last '/' or '\' in file, 0 if there is none. !! Hmm, could
647: be implemented with strrchr and the separator should be
648: OS-dependent */
1.1 anton 649: int onlypath(char *file)
1.10 pazsan 650: {
651: int i;
1.1 anton 652: i=strlen(file);
1.10 pazsan 653: while (i) {
654: if (file[i]=='\\' || file[i]=='/') break;
655: i--;
656: }
657: return i;
1.1 anton 658: }
659:
660: FILE *openimage(char *fullfilename)
1.10 pazsan 661: {
662: FILE *image_file;
1.28 anton 663: char * expfilename = tilde_cstr(fullfilename, strlen(fullfilename), 1);
1.10 pazsan 664:
1.28 anton 665: image_file=fopen(expfilename,"rb");
1.1 anton 666: if (image_file!=NULL && debug)
1.28 anton 667: fprintf(stderr, "Opened image file: %s\n", expfilename);
1.10 pazsan 668: return image_file;
1.1 anton 669: }
670:
1.28 anton 671: /* try to open image file concat(path[0:len],imagename) */
1.1 anton 672: FILE *checkimage(char *path, int len, char *imagename)
1.10 pazsan 673: {
674: int dirlen=len;
1.1 anton 675: char fullfilename[dirlen+strlen(imagename)+2];
1.10 pazsan 676:
1.1 anton 677: memcpy(fullfilename, path, dirlen);
678: if (fullfilename[dirlen-1]!='/')
679: fullfilename[dirlen++]='/';
680: strcpy(fullfilename+dirlen,imagename);
1.10 pazsan 681: return openimage(fullfilename);
1.1 anton 682: }
683:
1.10 pazsan 684: FILE * open_image_file(char * imagename, char * path)
1.1 anton 685: {
1.10 pazsan 686: FILE * image_file=NULL;
1.28 anton 687: char *origpath=path;
1.10 pazsan 688:
689: if(strchr(imagename, '/')==NULL) {
690: /* first check the directory where the exe file is in !! 01may97jaw */
691: if (onlypath(progname))
692: image_file=checkimage(progname, onlypath(progname), imagename);
693: if (!image_file)
694: do {
695: char *pend=strchr(path, PATHSEP);
696: if (pend==NULL)
697: pend=path+strlen(path);
698: if (strlen(path)==0) break;
699: image_file=checkimage(path, pend-path, imagename);
700: path=pend+(*pend==PATHSEP);
701: } while (image_file==NULL);
702: } else {
703: image_file=openimage(imagename);
704: }
1.1 anton 705:
1.10 pazsan 706: if (!image_file) {
707: fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
1.28 anton 708: progname, imagename, origpath);
1.10 pazsan 709: exit(1);
1.7 anton 710: }
711:
1.10 pazsan 712: return image_file;
713: }
1.11 pazsan 714: #endif
715:
716: #ifdef HAS_OS
717: UCell convsize(char *s, UCell elemsize)
718: /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
719: of bytes. the letter at the end indicates the unit, where e stands
720: for the element size. default is e */
721: {
722: char *endp;
723: UCell n,m;
724:
725: m = elemsize;
726: n = strtoul(s,&endp,0);
727: if (endp!=NULL) {
728: if (strcmp(endp,"b")==0)
729: m=1;
730: else if (strcmp(endp,"k")==0)
731: m=1024;
732: else if (strcmp(endp,"M")==0)
733: m=1024*1024;
734: else if (strcmp(endp,"G")==0)
735: m=1024*1024*1024;
736: else if (strcmp(endp,"T")==0) {
737: #if (SIZEOF_CHAR_P > 4)
1.24 anton 738: m=1024L*1024*1024*1024;
1.11 pazsan 739: #else
740: fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp);
741: exit(1);
742: #endif
743: } else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
744: fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
745: exit(1);
746: }
747: }
748: return n*m;
749: }
1.10 pazsan 750:
751: void gforth_args(int argc, char ** argv, char ** path, char ** imagename)
752: {
753: int c;
754:
1.1 anton 755: opterr=0;
756: while (1) {
757: int option_index=0;
758: static struct option opts[] = {
1.29 anton 759: {"appl-image", required_argument, NULL, 'a'},
1.1 anton 760: {"image-file", required_argument, NULL, 'i'},
761: {"dictionary-size", required_argument, NULL, 'm'},
762: {"data-stack-size", required_argument, NULL, 'd'},
763: {"return-stack-size", required_argument, NULL, 'r'},
764: {"fp-stack-size", required_argument, NULL, 'f'},
765: {"locals-stack-size", required_argument, NULL, 'l'},
766: {"path", required_argument, NULL, 'p'},
767: {"version", no_argument, NULL, 'v'},
768: {"help", no_argument, NULL, 'h'},
769: /* put something != 0 into offset_image */
770: {"offset-image", no_argument, &offset_image, 1},
771: {"no-offset-im", no_argument, &offset_image, 0},
772: {"clear-dictionary", no_argument, &clear_dictionary, 1},
1.4 anton 773: {"die-on-signal", no_argument, &die_on_signal, 1},
1.1 anton 774: {"debug", no_argument, &debug, 1},
775: {0,0,0,0}
776: /* no-init-file, no-rc? */
777: };
778:
1.36 pazsan 779: c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vhoncsx", opts, &option_index);
1.1 anton 780:
781: switch (c) {
1.29 anton 782: case EOF: return;
783: case '?': optind--; return;
784: case 'a': *imagename = optarg; return;
1.10 pazsan 785: case 'i': *imagename = optarg; break;
1.1 anton 786: case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
787: case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
788: case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
789: case 'f': fsize = convsize(optarg,sizeof(Float)); break;
790: case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
1.10 pazsan 791: case 'p': *path = optarg; break;
1.36 pazsan 792: case 'o': offset_image = 1; break;
793: case 'n': offset_image = 0; break;
794: case 'c': clear_dictionary = 1; break;
795: case 's': die_on_signal = 1; break;
796: case 'x': debug = 1; break;
1.8 anton 797: case 'v': fprintf(stderr, "gforth %s\n", VERSION); exit(0);
1.1 anton 798: case 'h':
1.29 anton 799: fprintf(stderr, "Usage: %s [engine options] ['--'] [image arguments]\n\
1.1 anton 800: Engine Options:\n\
1.29 anton 801: --appl-image FILE equivalent to '--image-file=FILE --'\n\
1.10 pazsan 802: --clear-dictionary Initialize the dictionary with 0 bytes\n\
803: -d SIZE, --data-stack-size=SIZE Specify data stack size\n\
804: --debug Print debugging information during startup\n\
805: --die-on-signal exit instead of CATCHing some signals\n\
806: -f SIZE, --fp-stack-size=SIZE Specify floating point stack size\n\
807: -h, --help Print this message and exit\n\
808: -i FILE, --image-file=FILE Use image FILE instead of `gforth.fi'\n\
809: -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
810: -m SIZE, --dictionary-size=SIZE Specify Forth dictionary size\n\
811: --no-offset-im Load image at normal position\n\
812: --offset-image Load image at a different position\n\
813: -p PATH, --path=PATH Search path for finding image and sources\n\
814: -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
815: -v, --version Print version and exit\n\
1.1 anton 816: SIZE arguments consist of an integer followed by a unit. The unit can be\n\
1.10 pazsan 817: `b' (byte), `e' (element; default), `k' (KB), `M' (MB), `G' (GB) or `T' (TB).\n",
818: argv[0]);
819: optind--;
820: return;
1.1 anton 821: }
822: }
1.10 pazsan 823: }
1.11 pazsan 824: #endif
1.10 pazsan 825:
826: #ifdef INCLUDE_IMAGE
827: extern Cell image[];
828: extern const char reloc_bits[];
829: #endif
830:
831: int main(int argc, char **argv, char **env)
832: {
1.30 pazsan 833: #ifdef HAS_OS
1.10 pazsan 834: char *path = getenv("GFORTHPATH") ? : DEFAULTPATH;
1.30 pazsan 835: #else
836: char *path = DEFAULTPATH;
837: #endif
1.13 pazsan 838: #ifndef INCLUDE_IMAGE
1.10 pazsan 839: char *imagename="gforth.fi";
840: FILE *image_file;
841: Address image;
842: #endif
843: int retvalue;
844:
845: #if defined(i386) && defined(ALIGNMENT_CHECK) && !defined(DIRECT_THREADED)
846: /* turn on alignment checks on the 486.
847: * on the 386 this should have no effect. */
848: __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
849: /* this is unusable with Linux' libc.4.6.27, because this library is
850: not alignment-clean; we would have to replace some library
851: functions (e.g., memcpy) to make it work. Also GCC doesn't try to keep
852: the stack FP-aligned. */
853: #endif
854:
855: /* buffering of the user output device */
1.11 pazsan 856: #ifdef _IONBF
1.10 pazsan 857: if (isatty(fileno(stdout))) {
858: fflush(stdout);
859: setvbuf(stdout,NULL,_IONBF,0);
1.1 anton 860: }
1.11 pazsan 861: #endif
1.1 anton 862:
1.10 pazsan 863: progname = argv[0];
864:
1.11 pazsan 865: #ifdef HAS_OS
1.10 pazsan 866: gforth_args(argc, argv, &path, &imagename);
1.11 pazsan 867: #endif
1.10 pazsan 868:
869: #ifdef INCLUDE_IMAGE
870: set_stack_sizes((ImageHeader *)image);
1.22 pazsan 871: if(((ImageHeader *)image)->base != image)
872: relocate(image, reloc_bits, ((ImageHeader *)image)->image_size,
873: (Label*)engine(0, 0, 0, 0, 0));
1.10 pazsan 874: alloc_stacks((ImageHeader *)image);
875: #else
876: image_file = open_image_file(imagename, path);
877: image = loader(image_file, imagename);
878: #endif
1.24 anton 879: gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */
1.1 anton 880:
881: {
1.10 pazsan 882: char path2[strlen(path)+1];
1.1 anton 883: char *p1, *p2;
884: Cell environ[]= {
885: (Cell)argc-(optind-1),
886: (Cell)(argv+(optind-1)),
1.10 pazsan 887: (Cell)strlen(path),
1.1 anton 888: (Cell)path2};
889: argv[optind-1] = progname;
890: /*
891: for (i=0; i<environ[0]; i++)
892: printf("%s\n", ((char **)(environ[1]))[i]);
893: */
894: /* make path OS-independent by replacing path separators with NUL */
1.10 pazsan 895: for (p1=path, p2=path2; *p1!='\0'; p1++, p2++)
1.1 anton 896: if (*p1==PATHSEP)
897: *p2 = '\0';
898: else
899: *p2 = *p1;
900: *p2='\0';
1.10 pazsan 901: retvalue = go_forth(image, 4, environ);
1.42 anton 902: #ifdef VM_PROFILING
903: vm_print_profile(stderr);
904: #endif
1.1 anton 905: deprep_terminal();
906: }
1.13 pazsan 907: return retvalue;
1.1 anton 908: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>