Annotation of gforth/engine/main.c, revision 1.76
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.75 anton 84: #define CODE_BLOCK_SIZE (64*1024)
1.48 anton 85: Address code_area=0;
1.73 anton 86: Cell code_area_size = CODE_BLOCK_SIZE;
1.75 anton 87: Address code_here=NULL+CODE_BLOCK_SIZE; /* does for code-area what HERE
88: does for the dictionary */
1.65 anton 89: Address start_flush=0; /* start of unflushed code */
1.74 anton 90: Cell last_jump=0; /* if the last prim was compiled without jump, this
91: is it's number, otherwise this contains 0 */
1.48 anton 92:
1.60 anton 93: static int no_super=0; /* true if compile_prim should not fuse prims */
1.66 anton 94: /* --no-dynamic by default on gcc versions >=3.1 (it works with 3.0.4,
95: but not with 3.2) */
96: #if (__GNUC__>2 && __GNUC_MINOR__>=1)
1.64 anton 97: static int no_dynamic=1; /* true if compile_prim should not generate code */
1.66 anton 98: #else
99: static int no_dynamic=0; /* true if compile_prim should not generate code */
100: #endif
1.60 anton 101:
1.30 pazsan 102: #ifdef HAS_DEBUG
1.68 anton 103: int debug=0;
1.31 pazsan 104: #else
105: # define perror(x...)
106: # define fprintf(x...)
1.30 pazsan 107: #endif
1.31 pazsan 108:
1.24 anton 109: ImageHeader *gforth_header;
1.43 anton 110: Label *vm_prims;
1.53 anton 111: #ifdef DOUBLY_INDIRECT
112: Label *xts; /* same content as vm_prims, but should only be used for xts */
113: #endif
1.1 anton 114:
1.30 pazsan 115: #ifdef MEMCMP_AS_SUBROUTINE
116: int gforth_memcmp(const char * s1, const char * s2, size_t n)
117: {
118: return memcmp(s1, s2, n);
119: }
120: #endif
121:
1.1 anton 122: /* image file format:
1.15 pazsan 123: * "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.4.0 -i\n")
1.1 anton 124: * padding to a multiple of 8
1.15 pazsan 125: * magic: "Gforth2x" means format 0.4,
126: * where x is a byte with
127: * bit 7: reserved = 0
128: * bit 6:5: address unit size 2^n octets
129: * bit 4:3: character size 2^n octets
130: * bit 2:1: cell size 2^n octets
131: * bit 0: endian, big=0, little=1.
132: * The magic are always 8 octets, no matter what the native AU/character size is
1.1 anton 133: * padding to max alignment (no padding necessary on current machines)
1.24 anton 134: * ImageHeader structure (see forth.h)
1.1 anton 135: * data (size in ImageHeader.image_size)
136: * tags ((if relocatable, 1 bit/data cell)
137: *
138: * tag==1 means that the corresponding word is an address;
139: * If the word is >=0, the address is within the image;
140: * addresses within the image are given relative to the start of the image.
141: * If the word =-1 (CF_NIL), the address is NIL,
142: * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
143: * If the word =CF(DODOES), it's a DOES> CFA
144: * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
145: * possibly containing a jump to dodoes)
1.51 anton 146: * If the word is <CF(DOESJUMP) and bit 14 is set, it's the xt of a primitive
147: * If the word is <CF(DOESJUMP) and bit 14 is clear,
148: * it's the threaded code of a primitive
1.1 anton 149: */
150:
1.46 jwilke 151: void relocate(Cell *image, const char *bitstring,
152: int size, int base, Label symbols[])
1.1 anton 153: {
1.16 pazsan 154: int i=0, j, k, steps=(size/sizeof(Cell))/RELINFOBITS;
1.11 pazsan 155: Cell token;
1.1 anton 156: char bits;
1.37 anton 157: Cell max_symbols;
1.46 jwilke 158: /*
159: * A virtial start address that's the real start address minus
160: * the one in the image
161: */
1.45 jwilke 162: Cell *start = (Cell * ) (((void *) image) - ((void *) base));
1.1 anton 163:
1.46 jwilke 164:
165: /* printf("relocating to %x[%x] start=%x base=%x\n", image, size, start, base); */
1.37 anton 166:
167: for (max_symbols=DOESJUMP+1; symbols[max_symbols]!=0; max_symbols++)
168: ;
1.47 anton 169: max_symbols--;
1.35 pazsan 170: size/=sizeof(Cell);
171:
1.31 pazsan 172: for(k=0; k<=steps; k++) {
1.13 pazsan 173: for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
1.1 anton 174: /* fprintf(stderr,"relocate: image[%d]\n", i);*/
1.35 pazsan 175: if((i < size) && (bits & (1U << (RELINFOBITS-1)))) {
176: /* fprintf(stderr,"relocate: image[%d]=%d of %d\n", i, image[i], size/sizeof(Cell)); */
1.45 jwilke 177: token=image[i];
178: if(token<0)
1.55 anton 179: switch(token|0x4000)
1.1 anton 180: {
181: case CF_NIL : image[i]=0; break;
182: #if !defined(DOUBLY_INDIRECT)
183: case CF(DOCOL) :
184: case CF(DOVAR) :
185: case CF(DOCON) :
186: case CF(DOUSER) :
187: case CF(DODEFER) :
1.11 pazsan 188: case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(token)]); break;
1.1 anton 189: case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
190: #endif /* !defined(DOUBLY_INDIRECT) */
191: case CF(DODOES) :
1.45 jwilke 192: MAKE_DOES_CF(image+i,(Xt *)(image[i+1]+((Cell)start)));
1.1 anton 193: break;
194: default :
1.56 anton 195: /* printf("Code field generation image[%x]:=CFA(%x)\n",
1.1 anton 196: i, CF(image[i])); */
1.55 anton 197: if (CF((token | 0x4000))<max_symbols) {
1.56 anton 198: image[i]=(Cell)CFA(CF(token));
199: #ifdef DIRECT_THREADED
200: if ((token & 0x4000) == 0) /* threade code, no CFA */
1.70 anton 201: compile_prim1(&image[i]);
1.56 anton 202: #endif
1.55 anton 203: } else
1.37 anton 204: 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 205: }
1.46 jwilke 206: else {
1.45 jwilke 207: // if base is > 0: 0 is a null reference so don't adjust
208: if (token>=base) {
209: image[i]+=(Cell)start;
210: }
1.46 jwilke 211: }
1.1 anton 212: }
213: }
1.31 pazsan 214: }
1.70 anton 215: finish_code();
1.26 jwilke 216: ((ImageHeader*)(image))->base = (Address) image;
1.1 anton 217: }
218:
219: UCell checksum(Label symbols[])
220: {
221: UCell r=PRIM_VERSION;
222: Cell i;
223:
224: for (i=DOCOL; i<=DOESJUMP; i++) {
225: r ^= (UCell)(symbols[i]);
226: r = (r << 5) | (r >> (8*sizeof(Cell)-5));
227: }
228: #ifdef DIRECT_THREADED
229: /* we have to consider all the primitives */
230: for (; symbols[i]!=(Label)0; i++) {
231: r ^= (UCell)(symbols[i]);
232: r = (r << 5) | (r >> (8*sizeof(Cell)-5));
233: }
234: #else
235: /* in indirect threaded code all primitives are accessed through the
236: symbols table, so we just have to put the base address of symbols
237: in the checksum */
238: r ^= (UCell)symbols;
239: #endif
240: return r;
241: }
242:
1.3 anton 243: Address verbose_malloc(Cell size)
244: {
245: Address r;
246: /* leave a little room (64B) for stack underflows */
247: if ((r = malloc(size+64))==NULL) {
248: perror(progname);
249: exit(1);
250: }
251: r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
252: if (debug)
253: fprintf(stderr, "malloc succeeds, address=$%lx\n", (long)r);
254: return r;
255: }
256:
1.33 anton 257: static Address next_address=0;
258: void after_alloc(Address r, Cell size)
259: {
260: if (r != (Address)-1) {
261: if (debug)
262: fprintf(stderr, "success, address=$%lx\n", (long) r);
263: if (pagesize != 1)
264: next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */
265: } else {
266: if (debug)
267: fprintf(stderr, "failed: %s\n", strerror(errno));
268: }
269: }
270:
1.34 anton 271: #ifndef MAP_FAILED
272: #define MAP_FAILED ((Address) -1)
273: #endif
274: #ifndef MAP_FILE
275: # define MAP_FILE 0
276: #endif
277: #ifndef MAP_PRIVATE
278: # define MAP_PRIVATE 0
279: #endif
280:
281: #if defined(HAVE_MMAP)
282: static Address alloc_mmap(Cell size)
1.1 anton 283: {
284: Address r;
285:
286: #if defined(MAP_ANON)
287: if (debug)
288: fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_ANON, ...); ", (long)next_address, (long)size);
1.34 anton 289: r = mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
1.1 anton 290: #else /* !defined(MAP_ANON) */
1.17 anton 291: /* Ultrix (at least) does not define MAP_FILE and MAP_PRIVATE (both are
292: apparently defaults) */
1.1 anton 293: static int dev_zero=-1;
294:
295: if (dev_zero == -1)
296: dev_zero = open("/dev/zero", O_RDONLY);
297: if (dev_zero == -1) {
1.34 anton 298: r = MAP_FAILED;
1.1 anton 299: if (debug)
300: fprintf(stderr, "open(\"/dev/zero\"...) failed (%s), no mmap; ",
301: strerror(errno));
302: } else {
303: if (debug)
304: fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FILE, dev_zero, ...); ", (long)next_address, (long)size);
305: r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE, dev_zero, 0);
306: }
307: #endif /* !defined(MAP_ANON) */
1.34 anton 308: after_alloc(r, size);
309: return r;
310: }
311: #endif
312:
313: Address my_alloc(Cell size)
314: {
315: #if HAVE_MMAP
316: Address r;
317:
318: r=alloc_mmap(size);
319: if (r!=MAP_FAILED)
1.1 anton 320: return r;
321: #endif /* HAVE_MMAP */
1.3 anton 322: /* use malloc as fallback */
323: return verbose_malloc(size);
1.1 anton 324: }
325:
1.34 anton 326: Address dict_alloc_read(FILE *file, Cell imagesize, Cell dictsize, Cell offset)
1.33 anton 327: {
1.34 anton 328: Address image = MAP_FAILED;
1.33 anton 329:
1.56 anton 330: #if defined(HAVE_MMAP)
1.33 anton 331: if (offset==0) {
1.34 anton 332: image=alloc_mmap(dictsize);
1.33 anton 333: if (debug)
1.34 anton 334: fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FIXED|MAP_FILE, imagefile, 0); ", (long)image, (long)imagesize);
335: image = mmap(image, imagesize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FIXED|MAP_FILE|MAP_PRIVATE, fileno(file), 0);
336: after_alloc(image,dictsize);
1.33 anton 337: }
1.56 anton 338: #endif /* defined(HAVE_MMAP) */
1.34 anton 339: if (image == MAP_FAILED) {
1.56 anton 340: image = my_alloc(dictsize+offset)+offset;
1.33 anton 341: rewind(file); /* fseek(imagefile,0L,SEEK_SET); */
1.34 anton 342: fread(image, 1, imagesize, file);
1.33 anton 343: }
344: return image;
345: }
346:
1.10 pazsan 347: void set_stack_sizes(ImageHeader * header)
348: {
349: if (dictsize==0)
350: dictsize = header->dict_size;
351: if (dsize==0)
352: dsize = header->data_stack_size;
353: if (rsize==0)
354: rsize = header->return_stack_size;
355: if (fsize==0)
356: fsize = header->fp_stack_size;
357: if (lsize==0)
358: lsize = header->locals_stack_size;
359: dictsize=maxaligned(dictsize);
360: dsize=maxaligned(dsize);
361: rsize=maxaligned(rsize);
362: lsize=maxaligned(lsize);
363: fsize=maxaligned(fsize);
364: }
365:
366: void alloc_stacks(ImageHeader * header)
367: {
368: header->dict_size=dictsize;
369: header->data_stack_size=dsize;
370: header->fp_stack_size=fsize;
371: header->return_stack_size=rsize;
372: header->locals_stack_size=lsize;
373:
374: header->data_stack_base=my_alloc(dsize);
375: header->fp_stack_base=my_alloc(fsize);
376: header->return_stack_base=my_alloc(rsize);
377: header->locals_stack_base=my_alloc(lsize);
378: }
379:
1.44 pazsan 380: #warning You can ignore the warnings about clobbered variables in go_forth
1.11 pazsan 381: int go_forth(Address image, int stack, Cell *entries)
382: {
1.38 anton 383: volatile ImageHeader *image_header = (ImageHeader *)image;
1.18 anton 384: Cell *sp0=(Cell*)(image_header->data_stack_base + dsize);
1.44 pazsan 385: Cell *rp0=(Cell *)(image_header->return_stack_base + rsize);
1.18 anton 386: Float *fp0=(Float *)(image_header->fp_stack_base + fsize);
1.44 pazsan 387: #ifdef GFORTH_DEBUGGING
1.38 anton 388: volatile Cell *orig_rp0=rp0;
1.44 pazsan 389: #endif
1.18 anton 390: Address lp0=image_header->locals_stack_base + lsize;
391: Xt *ip0=(Xt *)(image_header->boot_entry);
1.13 pazsan 392: #ifdef SYSSIGNALS
1.11 pazsan 393: int throw_code;
1.13 pazsan 394: #endif
1.11 pazsan 395:
396: /* ensure that the cached elements (if any) are accessible */
1.41 anton 397: IF_spTOS(sp0--);
398: IF_fpTOS(fp0--);
1.11 pazsan 399:
400: for(;stack>0;stack--)
1.18 anton 401: *--sp0=entries[stack-1];
1.11 pazsan 402:
1.30 pazsan 403: #ifdef SYSSIGNALS
1.11 pazsan 404: get_winsize();
405:
406: install_signal_handlers(); /* right place? */
407:
408: if ((throw_code=setjmp(throw_jmp_buf))) {
409: static Cell signal_data_stack[8];
410: static Cell signal_return_stack[8];
411: static Float signal_fp_stack[1];
1.13 pazsan 412:
1.11 pazsan 413: signal_data_stack[7]=throw_code;
1.18 anton 414:
415: #ifdef GFORTH_DEBUGGING
1.38 anton 416: /* fprintf(stderr,"\nrp=%ld\n",(long)rp); */
417: if (rp <= orig_rp0 && rp > (Cell *)(image_header->return_stack_base+5)) {
1.18 anton 418: /* no rstack overflow or underflow */
419: rp0 = rp;
1.63 anton 420: *--rp0 = (Cell)saved_ip;
1.18 anton 421: }
422: else /* I love non-syntactic ifdefs :-) */
423: #endif
424: rp0 = signal_return_stack+8;
1.25 anton 425: /* fprintf(stderr, "rp=$%x\n",rp0);*/
1.11 pazsan 426:
1.33 anton 427: return((int)(Cell)engine(image_header->throw_entry, signal_data_stack+7,
1.18 anton 428: rp0, signal_fp_stack, 0));
1.11 pazsan 429: }
1.13 pazsan 430: #endif
1.11 pazsan 431:
1.33 anton 432: return((int)(Cell)engine(ip0,sp0,rp0,fp0,lp0));
1.11 pazsan 433: }
434:
1.21 anton 435:
1.30 pazsan 436: #ifndef INCLUDE_IMAGE
1.21 anton 437: void print_sizes(Cell sizebyte)
438: /* print size information */
439: {
440: static char* endianstring[]= { " big","little" };
441:
442: fprintf(stderr,"%s endian, cell=%d bytes, char=%d bytes, au=%d bytes\n",
443: endianstring[sizebyte & 1],
444: 1 << ((sizebyte >> 1) & 3),
445: 1 << ((sizebyte >> 3) & 3),
446: 1 << ((sizebyte >> 5) & 3));
447: }
448:
1.70 anton 449: #define MAX_IMMARGS 2
450:
1.69 anton 451: #ifndef NO_DYNAMIC
1.47 anton 452: typedef struct {
453: Label start;
1.74 anton 454: Cell length; /* only includes the jump iff superend is true*/
455: Cell restlength; /* length of the rest (i.e., the jump or (on superend) 0) */
1.70 anton 456: char superend; /* true if primitive ends superinstruction, i.e.,
1.47 anton 457: unconditional branch, execute, etc. */
1.70 anton 458: Cell nimmargs;
459: struct immarg {
460: Cell offset; /* offset of immarg within prim */
461: char rel; /* true if immarg is relative */
462: } immargs[MAX_IMMARGS];
1.47 anton 463: } PrimInfo;
464:
465: PrimInfo *priminfos;
1.76 ! anton 466: PrimInfo **decomp_prims;
! 467:
! 468: int compare_priminfo_length(PrimInfo **a, PrimInfo **b)
! 469: {
! 470: return (*a)->length - (*b)->length;
! 471: }
! 472:
1.69 anton 473: #endif /* defined(NO_DYNAMIC) */
1.48 anton 474: Cell npriminfos=0;
1.47 anton 475:
1.76 ! anton 476:
1.47 anton 477: void check_prims(Label symbols1[])
478: {
479: int i;
1.70 anton 480: Label *symbols2, *symbols3, *ends1;
1.49 anton 481: static char superend[]={
1.48 anton 482: #include "prim_superend.i"
483: };
1.47 anton 484:
1.66 anton 485: if (debug)
486: #ifdef __VERSION__
487: fprintf(stderr, "Compiled with gcc-" __VERSION__ "\n");
488: #else
489: #define xstr(s) str(s)
490: #define str(s) #s
491: fprintf(stderr, "Compiled with gcc-" xstr(__GNUC__) "." xstr(__GNUC_MINOR__) "\n");
492: #endif
1.47 anton 493: for (i=DOESJUMP+1; symbols1[i+1]!=0; i++)
494: ;
1.55 anton 495: npriminfos = i;
1.70 anton 496:
497: #ifndef NO_DYNAMIC
1.66 anton 498: if (no_dynamic)
499: return;
1.55 anton 500: symbols2=engine2(0,0,0,0,0);
1.70 anton 501: #if NO_IP
502: symbols3=engine3(0,0,0,0,0);
503: #else
504: symbols3=symbols1;
505: #endif
506: ends1 = symbols1+i+1-DOESJUMP;
1.47 anton 507: priminfos = calloc(i,sizeof(PrimInfo));
508: for (i=DOESJUMP+1; symbols1[i+1]!=0; i++) {
1.70 anton 509: int prim_len = ends1[i]-symbols1[i];
1.47 anton 510: PrimInfo *pi=&priminfos[i];
1.70 anton 511: int j=0;
512: char *s1 = (char *)symbols1[i];
513: char *s2 = (char *)symbols2[i];
514: char *s3 = (char *)symbols3[i];
515:
516: pi->start = s1;
517: pi->superend = superend[i-DOESJUMP-1]|no_super;
518: if (pi->superend)
519: pi->length = symbols1[i+1]-symbols1[i];
520: else
521: pi->length = prim_len;
1.74 anton 522: pi->restlength = symbols1[i+1] - symbols1[i] - pi->length;
1.70 anton 523: pi->nimmargs = 0;
524: if (debug)
1.74 anton 525: fprintf(stderr, "Prim %3d @ %p %p %p, length=%3d restlength=%2d superend=%1d",
526: i, s1, s2, s3, pi->length, pi->restlength, pi->superend);
1.70 anton 527: assert(prim_len>=0);
1.74 anton 528: while (j<(pi->length+pi->restlength)) {
1.70 anton 529: if (s1[j]==s3[j]) {
530: if (s1[j] != s2[j]) {
531: pi->start = NULL; /* not relocatable */
532: if (debug)
533: fprintf(stderr,"\n non_reloc: engine1!=engine2 offset %3d",j);
1.74 anton 534: /* assert(j<prim_len); */
1.70 anton 535: break;
536: }
537: j++;
538: } else {
539: struct immarg *ia=&pi->immargs[pi->nimmargs];
540:
541: pi->nimmargs++;
542: ia->offset=j;
543: if ((~*(Cell *)&(s1[j]))==*(Cell *)&(s3[j])) {
544: ia->rel=0;
545: if (debug)
546: fprintf(stderr,"\n absolute immarg: offset %3d",j);
547: } else if ((&(s1[j]))+(*(Cell *)&(s1[j]))+4 ==
548: symbols1[DOESJUMP+1]) {
549: ia->rel=1;
550: if (debug)
551: fprintf(stderr,"\n relative immarg: offset %3d",j);
552: } else {
553: pi->start = NULL; /* not relocatable */
554: if (debug)
555: fprintf(stderr,"\n non_reloc: engine1!=engine3 offset %3d",j);
1.74 anton 556: /* assert(j<prim_len);*/
1.70 anton 557: break;
558: }
559: j+=4;
1.47 anton 560: }
561: }
1.70 anton 562: if (debug)
563: fprintf(stderr,"\n");
564: }
1.76 ! anton 565: decomp_prims = calloc(i,sizeof(PrimInfo *));
! 566: for (i=DOESJUMP+1; i<npriminfos; i++)
! 567: decomp_prims[i] = &(priminfos[i]);
! 568: qsort(decomp_prims+DOESJUMP+1, npriminfos-DOESJUMP-1, sizeof(PrimInfo *),
! 569: compare_priminfo_length);
1.70 anton 570: #endif
571: }
572:
1.74 anton 573: #ifndef NO_DYNAMIC
574: void flush_to_here(void)
575: {
576: FLUSH_ICACHE(start_flush, code_here-start_flush);
577: start_flush=code_here;
578: }
579:
580: void append_jump(void)
581: {
582: if (last_jump) {
583: PrimInfo *pi = &priminfos[last_jump];
584:
585: memcpy(code_here, pi->start+pi->length, pi->restlength);
586: code_here += pi->restlength;
587: last_jump=0;
588: flush_to_here();
589: }
590: }
591:
1.75 anton 592: /* Gforth remembers all code blocks in this list. On forgetting (by
593: executing a marker) the code blocks are not freed (because Gforth does
594: not remember how they were allocated; hmm, remembering that might be
595: easier and cleaner). Instead, code_here etc. are reset to the old
596: value, and the "forgotten" code blocks are reused when they are
597: needed. */
598:
599: struct code_block_list {
600: struct code_block_list *next;
601: Address block;
602: Cell size;
603: } *code_block_list=NULL, **next_code_blockp=&code_block_list;
604:
1.74 anton 605: Address append_prim(Cell p)
606: {
607: PrimInfo *pi = &priminfos[p];
608: Address old_code_here = code_here;
609:
610: if (code_area+code_area_size < code_here+pi->length+pi->restlength) {
1.75 anton 611: struct code_block_list *p;
1.74 anton 612: append_jump();
1.75 anton 613: if (*next_code_blockp == NULL) {
614: code_here = start_flush = code_area = my_alloc(code_area_size);
615: p = (struct code_block_list *)malloc(sizeof(struct code_block_list));
616: *next_code_blockp = p;
617: p->next = NULL;
618: p->block = code_here;
619: p->size = code_area_size;
620: } else {
621: p = *next_code_blockp;
622: code_here = start_flush = code_area = p->block;
623: }
1.74 anton 624: old_code_here = code_here;
1.75 anton 625: next_code_blockp = &(p->next);
1.74 anton 626: }
627: memcpy(code_here, pi->start, pi->length);
628: code_here += pi->length;
629: if (pi->superend)
630: flush_to_here();
631: return old_code_here;
632: }
633: #endif
1.75 anton 634:
635: int forget_dyncode(Address code)
636: {
637: #ifdef NO_DYNAMIC
638: return -1;
639: #else
640: struct code_block_list *p, **pp;
641:
642: for (pp=&code_block_list, p=*pp; p!=NULL; pp=&(p->next), p=*pp) {
643: if (code >= p->block && code < p->block+p->size) {
644: next_code_blockp = &(p->next);
645: code_here = start_flush = code;
646: code_area = p->block;
647: last_jump = 0;
648: return -1;
649: }
650: }
651: return 0;
652: #endif /* !defined(NO_DYNAMIC) */
653: }
654:
1.76 ! anton 655: Label decompile_code(Label code)
1.75 anton 656: {
1.76 ! anton 657: #ifdef NO_DYNAMIC
! 658: return code;
! 659: #else /* !defined(NO_DYNAMIC) */
! 660: Cell i;
! 661:
! 662: /* reverse order because NOOP might match other prims */
! 663: for (i=npriminfos-1; i>DOESJUMP; i--) {
! 664: PrimInfo *pi=decomp_prims[i];
! 665: if (pi->start==code || (pi->start && memcmp(code,pi->start,pi->length)==0))
! 666: return pi->start;
! 667: }
! 668: return code;
! 669: #endif /* !defined(NO_DYNAMIC) */
1.75 anton 670: }
1.74 anton 671:
1.70 anton 672: #ifdef NO_IP
673: int nbranchinfos=0;
674:
675: struct branchinfo {
676: Label *targetptr; /* *(bi->targetptr) is the target */
677: Cell *addressptr; /* store the target here */
678: } branchinfos[100000];
679:
680: int ndoesexecinfos=0;
681: struct doesexecinfo {
682: int branchinfo; /* fix the targetptr of branchinfos[...->branchinfo] */
683: Cell *xt; /* cfa of word whose does-code needs calling */
684: } doesexecinfos[10000];
685:
1.73 anton 686: /* definitions of N_execute etc. */
687: #include "prim_num.i"
1.70 anton 688:
689: void set_rel_target(Cell *source, Label target)
690: {
691: *source = ((Cell)target)-(((Cell)source)+4);
692: }
693:
694: void register_branchinfo(Label source, Cell targetptr)
695: {
696: struct branchinfo *bi = &(branchinfos[nbranchinfos]);
697: bi->targetptr = (Label *)targetptr;
698: bi->addressptr = (Cell *)source;
699: nbranchinfos++;
700: }
701:
702: Cell *compile_prim1arg(Cell p)
703: {
704: int l = priminfos[p].length;
705: Address old_code_here=code_here;
706:
1.74 anton 707: assert(vm_prims[p]==priminfos[p].start);
708: append_prim(p);
1.70 anton 709: return (Cell*)(old_code_here+priminfos[p].immargs[0].offset);
710: }
711:
712: Cell *compile_call2(Cell targetptr)
713: {
714: Cell *next_code_target;
1.73 anton 715: PrimInfo *pi = &priminfos[N_call2];
1.74 anton 716: Address old_code_here = append_prim(N_call2);
1.70 anton 717:
1.74 anton 718: next_code_target = (Cell *)(old_code_here + pi->immargs[0].offset);
719: register_branchinfo(old_code_here + pi->immargs[1].offset, targetptr);
1.70 anton 720: return next_code_target;
721: }
722: #endif
723:
724: void finish_code(void)
725: {
726: #ifdef NO_IP
727: Cell i;
728:
729: compile_prim1(NULL);
730: for (i=0; i<ndoesexecinfos; i++) {
731: struct doesexecinfo *dei = &doesexecinfos[i];
732: branchinfos[dei->branchinfo].targetptr = DOES_CODE1((dei->xt));
733: }
734: ndoesexecinfos = 0;
735: for (i=0; i<nbranchinfos; i++) {
736: struct branchinfo *bi=&branchinfos[i];
737: set_rel_target(bi->addressptr, *(bi->targetptr));
738: }
739: nbranchinfos = 0;
740: FLUSH_ICACHE(start_flush, code_here-start_flush);
741: start_flush=code_here;
1.48 anton 742: #endif
743: }
744:
1.70 anton 745: void compile_prim1(Cell *start)
1.48 anton 746: {
1.61 anton 747: #if defined(DOUBLY_INDIRECT)
1.70 anton 748: Label prim=(Label)*start;
1.54 anton 749: if (prim<((Label)(xts+DOESJUMP)) || prim>((Label)(xts+npriminfos))) {
750: fprintf(stderr,"compile_prim encountered xt %p\n", prim);
1.70 anton 751: *start=(Cell)prim;
752: return;
753: } else {
754: *start = prim-((Label)xts)+((Label)vm_prims);
755: return;
756: }
757: #elif defined(NO_IP)
758: static Cell *last_start=NULL;
759: static Xt last_prim=NULL;
760: /* delay work by one call in order to get relocated immargs */
761:
762: if (last_start) {
763: unsigned i = last_prim-vm_prims;
764: PrimInfo *pi=&priminfos[i];
765: Cell *next_code_target=NULL;
766:
767: assert(i<npriminfos);
1.73 anton 768: if (i==N_execute||i==N_perform||i==N_lit_perform) {
769: next_code_target = compile_prim1arg(N_set_next_code);
1.70 anton 770: }
1.73 anton 771: if (i==N_call) {
1.70 anton 772: next_code_target = compile_call2(last_start[1]);
1.73 anton 773: } else if (i==N_does_exec) {
1.70 anton 774: struct doesexecinfo *dei = &doesexecinfos[ndoesexecinfos++];
1.73 anton 775: *compile_prim1arg(N_lit) = (Cell)PFA(last_start[1]);
1.70 anton 776: /* we cannot determine the callee now (last_start[1] may be a
777: forward reference), so just register an arbitrary target, and
778: register in dei that we need to fix this before resolving
779: branches */
780: dei->branchinfo = nbranchinfos;
781: dei->xt = (Cell *)(last_start[1]);
782: next_code_target = compile_call2(NULL);
783: } else if (pi->start == NULL) { /* non-reloc */
1.73 anton 784: next_code_target = compile_prim1arg(N_set_next_code);
785: set_rel_target(compile_prim1arg(N_abranch),*(Xt)last_prim);
1.70 anton 786: } else {
787: unsigned j;
1.74 anton 788: Address old_code_here = append_prim(i);
1.70 anton 789:
790: for (j=0; j<pi->nimmargs; j++) {
791: struct immarg *ia = &(pi->immargs[j]);
792: Cell argval = last_start[pi->nimmargs - j]; /* !! specific to prims */
793: if (ia->rel) { /* !! assumption: relative refs are branches */
1.74 anton 794: register_branchinfo(old_code_here + ia->offset, argval);
1.70 anton 795: } else /* plain argument */
1.74 anton 796: *(Cell *)(old_code_here + ia->offset) = argval;
1.70 anton 797: }
798: }
799: if (next_code_target!=NULL)
800: *next_code_target = (Cell)code_here;
801: }
802: if (start) {
803: last_prim = (Xt)*start;
804: *start = (Cell)code_here;
805: }
806: last_start = start;
807: return;
808: #elif !defined(NO_DYNAMIC)
809: Label prim=(Label)*start;
1.58 anton 810: unsigned i;
1.74 anton 811: Address old_code_here;
1.48 anton 812:
1.58 anton 813: i = ((Xt)prim)-vm_prims;
1.56 anton 814: prim = *(Xt)prim;
1.70 anton 815: if (no_dynamic) {
816: *start = (Cell)prim;
817: return;
818: }
1.58 anton 819: if (i>=npriminfos || priminfos[i].start == 0) { /* not a relocatable prim */
1.74 anton 820: append_jump();
1.70 anton 821: *start = (Cell)prim;
822: return;
1.47 anton 823: }
1.58 anton 824: assert(priminfos[i].start = prim);
1.50 anton 825: #ifdef ALIGN_CODE
826: ALIGN_CODE;
827: #endif
1.74 anton 828: assert(prim==priminfos[i].start);
829: old_code_here = append_prim(i);
830: last_jump = (priminfos[i].superend) ? 0 : i;
1.70 anton 831: *start = (Cell)old_code_here;
832: return;
1.61 anton 833: #else /* !defined(DOUBLY_INDIRECT), no code replication */
1.70 anton 834: Label prim=(Label)*start;
1.61 anton 835: #if !defined(INDIRECT_THREADED)
1.56 anton 836: prim = *(Xt)prim;
1.61 anton 837: #endif
1.70 anton 838: *start = (Cell)prim;
839: return;
1.54 anton 840: #endif /* !defined(DOUBLY_INDIRECT) */
1.70 anton 841: }
842:
843: Label compile_prim(Label prim)
844: {
845: Cell x=(Cell)prim;
846: compile_prim1(&x);
847: return (Label)x;
1.47 anton 848: }
849:
1.69 anton 850: #if defined(PRINT_SUPER_LENGTHS) && !defined(NO_DYNAMIC)
1.59 anton 851: Cell prim_length(Cell prim)
852: {
853: return priminfos[prim+DOESJUMP+1].length;
854: }
855: #endif
856:
1.1 anton 857: Address loader(FILE *imagefile, char* filename)
858: /* returns the address of the image proper (after the preamble) */
859: {
860: ImageHeader header;
861: Address image;
862: Address imp; /* image+preamble */
1.17 anton 863: Char magic[8];
864: char magic7; /* size byte of magic number */
1.1 anton 865: Cell preamblesize=0;
1.6 pazsan 866: Cell data_offset = offset_image ? 56*sizeof(Cell) : 0;
1.1 anton 867: UCell check_sum;
1.15 pazsan 868: Cell ausize = ((RELINFOBITS == 8) ? 0 :
869: (RELINFOBITS == 16) ? 1 :
870: (RELINFOBITS == 32) ? 2 : 3);
871: Cell charsize = ((sizeof(Char) == 1) ? 0 :
872: (sizeof(Char) == 2) ? 1 :
873: (sizeof(Char) == 4) ? 2 : 3) + ausize;
874: Cell cellsize = ((sizeof(Cell) == 1) ? 0 :
875: (sizeof(Cell) == 2) ? 1 :
876: (sizeof(Cell) == 4) ? 2 : 3) + ausize;
1.21 anton 877: Cell sizebyte = (ausize << 5) + (charsize << 3) + (cellsize << 1) +
878: #ifdef WORDS_BIGENDIAN
879: 0
880: #else
881: 1
882: #endif
883: ;
1.1 anton 884:
1.43 anton 885: vm_prims = engine(0,0,0,0,0);
1.47 anton 886: check_prims(vm_prims);
1.1 anton 887: #ifndef DOUBLY_INDIRECT
1.59 anton 888: #ifdef PRINT_SUPER_LENGTHS
889: print_super_lengths();
890: #endif
1.43 anton 891: check_sum = checksum(vm_prims);
1.1 anton 892: #else /* defined(DOUBLY_INDIRECT) */
1.43 anton 893: check_sum = (UCell)vm_prims;
1.1 anton 894: #endif /* defined(DOUBLY_INDIRECT) */
1.10 pazsan 895:
896: do {
897: if(fread(magic,sizeof(Char),8,imagefile) < 8) {
1.15 pazsan 898: fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.4) image.\n",
1.10 pazsan 899: progname, filename);
900: exit(1);
1.1 anton 901: }
1.10 pazsan 902: preamblesize+=8;
1.15 pazsan 903: } while(memcmp(magic,"Gforth2",7));
1.17 anton 904: magic7 = magic[7];
1.1 anton 905: if (debug) {
1.17 anton 906: magic[7]='\0';
1.21 anton 907: fprintf(stderr,"Magic found: %s ", magic);
908: print_sizes(magic7);
1.1 anton 909: }
910:
1.21 anton 911: if (magic7 != sizebyte)
912: {
913: fprintf(stderr,"This image is: ");
914: print_sizes(magic7);
915: fprintf(stderr,"whereas the machine is ");
916: print_sizes(sizebyte);
1.1 anton 917: exit(-2);
918: };
919:
920: fread((void *)&header,sizeof(ImageHeader),1,imagefile);
1.10 pazsan 921:
922: set_stack_sizes(&header);
1.1 anton 923:
924: #if HAVE_GETPAGESIZE
925: pagesize=getpagesize(); /* Linux/GNU libc offers this */
926: #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
927: pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
928: #elif PAGESIZE
929: pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
930: #endif
931: if (debug)
1.5 jwilke 932: fprintf(stderr,"pagesize=%ld\n",(unsigned long) pagesize);
1.1 anton 933:
1.34 anton 934: image = dict_alloc_read(imagefile, preamblesize+header.image_size,
935: preamblesize+dictsize, data_offset);
1.33 anton 936: imp=image+preamblesize;
1.57 anton 937: alloc_stacks((ImageHeader *)imp);
1.1 anton 938: if (clear_dictionary)
1.33 anton 939: memset(imp+header.image_size, 0, dictsize-header.image_size);
1.46 jwilke 940: if(header.base==0 || header.base == 0x100) {
1.1 anton 941: Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
942: char reloc_bits[reloc_size];
1.33 anton 943: fseek(imagefile, preamblesize+header.image_size, SEEK_SET);
1.10 pazsan 944: fread(reloc_bits, 1, reloc_size, imagefile);
1.45 jwilke 945: relocate((Cell *)imp, reloc_bits, header.image_size, header.base, vm_prims);
1.1 anton 946: #if 0
947: { /* let's see what the relocator did */
948: FILE *snapshot=fopen("snapshot.fi","wb");
949: fwrite(image,1,imagesize,snapshot);
950: fclose(snapshot);
951: }
952: #endif
1.46 jwilke 953: }
954: else if(header.base!=imp) {
955: fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
956: progname, (unsigned long)header.base, (unsigned long)imp);
957: exit(1);
1.1 anton 958: }
959: if (header.checksum==0)
960: ((ImageHeader *)imp)->checksum=check_sum;
961: else if (header.checksum != check_sum) {
962: fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
963: progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
964: exit(1);
965: }
1.53 anton 966: #ifdef DOUBLY_INDIRECT
967: ((ImageHeader *)imp)->xt_base = xts;
968: #endif
1.1 anton 969: fclose(imagefile);
970:
1.56 anton 971: /* unnecessary, except maybe for CODE words */
972: /* FLUSH_ICACHE(imp, header.image_size);*/
1.1 anton 973:
974: return imp;
975: }
976:
1.72 anton 977: /* pointer to last '/' or '\' in file, 0 if there is none. */
978: char *onlypath(char *filename)
1.10 pazsan 979: {
1.72 anton 980: return strrchr(filename, DIRSEP);
1.1 anton 981: }
982:
983: FILE *openimage(char *fullfilename)
1.10 pazsan 984: {
985: FILE *image_file;
1.28 anton 986: char * expfilename = tilde_cstr(fullfilename, strlen(fullfilename), 1);
1.10 pazsan 987:
1.28 anton 988: image_file=fopen(expfilename,"rb");
1.1 anton 989: if (image_file!=NULL && debug)
1.28 anton 990: fprintf(stderr, "Opened image file: %s\n", expfilename);
1.10 pazsan 991: return image_file;
1.1 anton 992: }
993:
1.28 anton 994: /* try to open image file concat(path[0:len],imagename) */
1.1 anton 995: FILE *checkimage(char *path, int len, char *imagename)
1.10 pazsan 996: {
997: int dirlen=len;
1.1 anton 998: char fullfilename[dirlen+strlen(imagename)+2];
1.10 pazsan 999:
1.1 anton 1000: memcpy(fullfilename, path, dirlen);
1.71 pazsan 1001: if (fullfilename[dirlen-1]!=DIRSEP)
1002: fullfilename[dirlen++]=DIRSEP;
1.1 anton 1003: strcpy(fullfilename+dirlen,imagename);
1.10 pazsan 1004: return openimage(fullfilename);
1.1 anton 1005: }
1006:
1.10 pazsan 1007: FILE * open_image_file(char * imagename, char * path)
1.1 anton 1008: {
1.10 pazsan 1009: FILE * image_file=NULL;
1.28 anton 1010: char *origpath=path;
1.10 pazsan 1011:
1.71 pazsan 1012: if(strchr(imagename, DIRSEP)==NULL) {
1.10 pazsan 1013: /* first check the directory where the exe file is in !! 01may97jaw */
1014: if (onlypath(progname))
1.72 anton 1015: image_file=checkimage(progname, onlypath(progname)-progname, imagename);
1.10 pazsan 1016: if (!image_file)
1017: do {
1018: char *pend=strchr(path, PATHSEP);
1019: if (pend==NULL)
1020: pend=path+strlen(path);
1021: if (strlen(path)==0) break;
1022: image_file=checkimage(path, pend-path, imagename);
1023: path=pend+(*pend==PATHSEP);
1024: } while (image_file==NULL);
1025: } else {
1026: image_file=openimage(imagename);
1027: }
1.1 anton 1028:
1.10 pazsan 1029: if (!image_file) {
1030: fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
1.28 anton 1031: progname, imagename, origpath);
1.10 pazsan 1032: exit(1);
1.7 anton 1033: }
1034:
1.10 pazsan 1035: return image_file;
1036: }
1.11 pazsan 1037: #endif
1038:
1039: #ifdef HAS_OS
1040: UCell convsize(char *s, UCell elemsize)
1041: /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
1042: of bytes. the letter at the end indicates the unit, where e stands
1043: for the element size. default is e */
1044: {
1045: char *endp;
1046: UCell n,m;
1047:
1048: m = elemsize;
1049: n = strtoul(s,&endp,0);
1050: if (endp!=NULL) {
1051: if (strcmp(endp,"b")==0)
1052: m=1;
1053: else if (strcmp(endp,"k")==0)
1054: m=1024;
1055: else if (strcmp(endp,"M")==0)
1056: m=1024*1024;
1057: else if (strcmp(endp,"G")==0)
1058: m=1024*1024*1024;
1059: else if (strcmp(endp,"T")==0) {
1060: #if (SIZEOF_CHAR_P > 4)
1.24 anton 1061: m=1024L*1024*1024*1024;
1.11 pazsan 1062: #else
1063: fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp);
1064: exit(1);
1065: #endif
1066: } else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
1067: fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
1068: exit(1);
1069: }
1070: }
1071: return n*m;
1072: }
1.10 pazsan 1073:
1074: void gforth_args(int argc, char ** argv, char ** path, char ** imagename)
1075: {
1076: int c;
1077:
1.1 anton 1078: opterr=0;
1079: while (1) {
1080: int option_index=0;
1081: static struct option opts[] = {
1.29 anton 1082: {"appl-image", required_argument, NULL, 'a'},
1.1 anton 1083: {"image-file", required_argument, NULL, 'i'},
1084: {"dictionary-size", required_argument, NULL, 'm'},
1085: {"data-stack-size", required_argument, NULL, 'd'},
1086: {"return-stack-size", required_argument, NULL, 'r'},
1087: {"fp-stack-size", required_argument, NULL, 'f'},
1088: {"locals-stack-size", required_argument, NULL, 'l'},
1089: {"path", required_argument, NULL, 'p'},
1090: {"version", no_argument, NULL, 'v'},
1091: {"help", no_argument, NULL, 'h'},
1092: /* put something != 0 into offset_image */
1093: {"offset-image", no_argument, &offset_image, 1},
1094: {"no-offset-im", no_argument, &offset_image, 0},
1095: {"clear-dictionary", no_argument, &clear_dictionary, 1},
1.4 anton 1096: {"die-on-signal", no_argument, &die_on_signal, 1},
1.1 anton 1097: {"debug", no_argument, &debug, 1},
1.60 anton 1098: {"no-super", no_argument, &no_super, 1},
1099: {"no-dynamic", no_argument, &no_dynamic, 1},
1.66 anton 1100: {"dynamic", no_argument, &no_dynamic, 0},
1.1 anton 1101: {0,0,0,0}
1102: /* no-init-file, no-rc? */
1103: };
1104:
1.36 pazsan 1105: c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vhoncsx", opts, &option_index);
1.1 anton 1106:
1107: switch (c) {
1.29 anton 1108: case EOF: return;
1109: case '?': optind--; return;
1110: case 'a': *imagename = optarg; return;
1.10 pazsan 1111: case 'i': *imagename = optarg; break;
1.1 anton 1112: case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
1113: case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
1114: case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
1115: case 'f': fsize = convsize(optarg,sizeof(Float)); break;
1116: case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
1.10 pazsan 1117: case 'p': *path = optarg; break;
1.36 pazsan 1118: case 'o': offset_image = 1; break;
1119: case 'n': offset_image = 0; break;
1120: case 'c': clear_dictionary = 1; break;
1121: case 's': die_on_signal = 1; break;
1122: case 'x': debug = 1; break;
1.8 anton 1123: case 'v': fprintf(stderr, "gforth %s\n", VERSION); exit(0);
1.1 anton 1124: case 'h':
1.29 anton 1125: fprintf(stderr, "Usage: %s [engine options] ['--'] [image arguments]\n\
1.1 anton 1126: Engine Options:\n\
1.29 anton 1127: --appl-image FILE equivalent to '--image-file=FILE --'\n\
1.10 pazsan 1128: --clear-dictionary Initialize the dictionary with 0 bytes\n\
1129: -d SIZE, --data-stack-size=SIZE Specify data stack size\n\
1130: --debug Print debugging information during startup\n\
1131: --die-on-signal exit instead of CATCHing some signals\n\
1.66 anton 1132: --dynamic use dynamic native code\n\
1.10 pazsan 1133: -f SIZE, --fp-stack-size=SIZE Specify floating point stack size\n\
1134: -h, --help Print this message and exit\n\
1135: -i FILE, --image-file=FILE Use image FILE instead of `gforth.fi'\n\
1136: -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
1137: -m SIZE, --dictionary-size=SIZE Specify Forth dictionary size\n\
1.60 anton 1138: --no-dynamic Use only statically compiled primitives\n\
1.10 pazsan 1139: --no-offset-im Load image at normal position\n\
1.60 anton 1140: --no-super No dynamically formed superinstructions\n\
1.10 pazsan 1141: --offset-image Load image at a different position\n\
1142: -p PATH, --path=PATH Search path for finding image and sources\n\
1143: -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
1.66 anton 1144: -v, --version Print engine version and exit\n\
1.1 anton 1145: SIZE arguments consist of an integer followed by a unit. The unit can be\n\
1.10 pazsan 1146: `b' (byte), `e' (element; default), `k' (KB), `M' (MB), `G' (GB) or `T' (TB).\n",
1147: argv[0]);
1148: optind--;
1149: return;
1.1 anton 1150: }
1151: }
1.10 pazsan 1152: }
1.11 pazsan 1153: #endif
1.10 pazsan 1154:
1155: #ifdef INCLUDE_IMAGE
1156: extern Cell image[];
1157: extern const char reloc_bits[];
1158: #endif
1159:
1.67 pazsan 1160: DCell double2ll(Float r)
1161: {
1162: #ifndef BUGGY_LONG_LONG
1163: return (DCell)(r);
1164: #else
1165: DCell d;
1166: d.hi = ldexp(r,-(int)(CELL_BITS)) - (r<0);
1167: d.lo = r-ldexp((Float)d.hi,CELL_BITS);
1168: return d;
1169: #endif
1170: }
1171:
1.10 pazsan 1172: int main(int argc, char **argv, char **env)
1173: {
1.30 pazsan 1174: #ifdef HAS_OS
1.10 pazsan 1175: char *path = getenv("GFORTHPATH") ? : DEFAULTPATH;
1.30 pazsan 1176: #else
1177: char *path = DEFAULTPATH;
1178: #endif
1.13 pazsan 1179: #ifndef INCLUDE_IMAGE
1.10 pazsan 1180: char *imagename="gforth.fi";
1181: FILE *image_file;
1182: Address image;
1183: #endif
1184: int retvalue;
1185:
1.56 anton 1186: #if defined(i386) && defined(ALIGNMENT_CHECK)
1.10 pazsan 1187: /* turn on alignment checks on the 486.
1188: * on the 386 this should have no effect. */
1189: __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
1190: /* this is unusable with Linux' libc.4.6.27, because this library is
1191: not alignment-clean; we would have to replace some library
1192: functions (e.g., memcpy) to make it work. Also GCC doesn't try to keep
1193: the stack FP-aligned. */
1194: #endif
1195:
1196: /* buffering of the user output device */
1.11 pazsan 1197: #ifdef _IONBF
1.10 pazsan 1198: if (isatty(fileno(stdout))) {
1199: fflush(stdout);
1200: setvbuf(stdout,NULL,_IONBF,0);
1.1 anton 1201: }
1.11 pazsan 1202: #endif
1.1 anton 1203:
1.10 pazsan 1204: progname = argv[0];
1205:
1.11 pazsan 1206: #ifdef HAS_OS
1.10 pazsan 1207: gforth_args(argc, argv, &path, &imagename);
1.11 pazsan 1208: #endif
1.10 pazsan 1209:
1210: #ifdef INCLUDE_IMAGE
1211: set_stack_sizes((ImageHeader *)image);
1.22 pazsan 1212: if(((ImageHeader *)image)->base != image)
1213: relocate(image, reloc_bits, ((ImageHeader *)image)->image_size,
1214: (Label*)engine(0, 0, 0, 0, 0));
1.10 pazsan 1215: alloc_stacks((ImageHeader *)image);
1216: #else
1217: image_file = open_image_file(imagename, path);
1218: image = loader(image_file, imagename);
1219: #endif
1.24 anton 1220: gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */
1.1 anton 1221:
1222: {
1.10 pazsan 1223: char path2[strlen(path)+1];
1.1 anton 1224: char *p1, *p2;
1225: Cell environ[]= {
1226: (Cell)argc-(optind-1),
1227: (Cell)(argv+(optind-1)),
1.10 pazsan 1228: (Cell)strlen(path),
1.1 anton 1229: (Cell)path2};
1230: argv[optind-1] = progname;
1231: /*
1232: for (i=0; i<environ[0]; i++)
1233: printf("%s\n", ((char **)(environ[1]))[i]);
1234: */
1235: /* make path OS-independent by replacing path separators with NUL */
1.10 pazsan 1236: for (p1=path, p2=path2; *p1!='\0'; p1++, p2++)
1.1 anton 1237: if (*p1==PATHSEP)
1238: *p2 = '\0';
1239: else
1240: *p2 = *p1;
1241: *p2='\0';
1.10 pazsan 1242: retvalue = go_forth(image, 4, environ);
1.42 anton 1243: #ifdef VM_PROFILING
1244: vm_print_profile(stderr);
1245: #endif
1.1 anton 1246: deprep_terminal();
1247: }
1.13 pazsan 1248: return retvalue;
1.1 anton 1249: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>