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