Annotation of gforth/engine/main.c, revision 1.75
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.69 anton 466: #endif /* defined(NO_DYNAMIC) */
1.48 anton 467: Cell npriminfos=0;
1.47 anton 468:
469: void check_prims(Label symbols1[])
470: {
471: int i;
1.70 anton 472: Label *symbols2, *symbols3, *ends1;
1.49 anton 473: static char superend[]={
1.48 anton 474: #include "prim_superend.i"
475: };
1.47 anton 476:
1.66 anton 477: if (debug)
478: #ifdef __VERSION__
479: fprintf(stderr, "Compiled with gcc-" __VERSION__ "\n");
480: #else
481: #define xstr(s) str(s)
482: #define str(s) #s
483: fprintf(stderr, "Compiled with gcc-" xstr(__GNUC__) "." xstr(__GNUC_MINOR__) "\n");
484: #endif
1.47 anton 485: for (i=DOESJUMP+1; symbols1[i+1]!=0; i++)
486: ;
1.55 anton 487: npriminfos = i;
1.70 anton 488:
489: #ifndef NO_DYNAMIC
1.66 anton 490: if (no_dynamic)
491: return;
1.55 anton 492: symbols2=engine2(0,0,0,0,0);
1.70 anton 493: #if NO_IP
494: symbols3=engine3(0,0,0,0,0);
495: #else
496: symbols3=symbols1;
497: #endif
498: ends1 = symbols1+i+1-DOESJUMP;
1.47 anton 499: priminfos = calloc(i,sizeof(PrimInfo));
500: for (i=DOESJUMP+1; symbols1[i+1]!=0; i++) {
1.70 anton 501: int prim_len = ends1[i]-symbols1[i];
1.47 anton 502: PrimInfo *pi=&priminfos[i];
1.70 anton 503: int j=0;
504: char *s1 = (char *)symbols1[i];
505: char *s2 = (char *)symbols2[i];
506: char *s3 = (char *)symbols3[i];
507:
508: pi->start = s1;
509: pi->superend = superend[i-DOESJUMP-1]|no_super;
510: if (pi->superend)
511: pi->length = symbols1[i+1]-symbols1[i];
512: else
513: pi->length = prim_len;
1.74 anton 514: pi->restlength = symbols1[i+1] - symbols1[i] - pi->length;
1.70 anton 515: pi->nimmargs = 0;
516: if (debug)
1.74 anton 517: fprintf(stderr, "Prim %3d @ %p %p %p, length=%3d restlength=%2d superend=%1d",
518: i, s1, s2, s3, pi->length, pi->restlength, pi->superend);
1.70 anton 519: assert(prim_len>=0);
1.74 anton 520: while (j<(pi->length+pi->restlength)) {
1.70 anton 521: if (s1[j]==s3[j]) {
522: if (s1[j] != s2[j]) {
523: pi->start = NULL; /* not relocatable */
524: if (debug)
525: fprintf(stderr,"\n non_reloc: engine1!=engine2 offset %3d",j);
1.74 anton 526: /* assert(j<prim_len); */
1.70 anton 527: break;
528: }
529: j++;
530: } else {
531: struct immarg *ia=&pi->immargs[pi->nimmargs];
532:
533: pi->nimmargs++;
534: ia->offset=j;
535: if ((~*(Cell *)&(s1[j]))==*(Cell *)&(s3[j])) {
536: ia->rel=0;
537: if (debug)
538: fprintf(stderr,"\n absolute immarg: offset %3d",j);
539: } else if ((&(s1[j]))+(*(Cell *)&(s1[j]))+4 ==
540: symbols1[DOESJUMP+1]) {
541: ia->rel=1;
542: if (debug)
543: fprintf(stderr,"\n relative immarg: offset %3d",j);
544: } else {
545: pi->start = NULL; /* not relocatable */
546: if (debug)
547: fprintf(stderr,"\n non_reloc: engine1!=engine3 offset %3d",j);
1.74 anton 548: /* assert(j<prim_len);*/
1.70 anton 549: break;
550: }
551: j+=4;
1.47 anton 552: }
553: }
1.70 anton 554: if (debug)
555: fprintf(stderr,"\n");
556: }
557: #endif
558: }
559:
1.74 anton 560: #ifndef NO_DYNAMIC
561: void flush_to_here(void)
562: {
563: FLUSH_ICACHE(start_flush, code_here-start_flush);
564: start_flush=code_here;
565: }
566:
567: void append_jump(void)
568: {
569: if (last_jump) {
570: PrimInfo *pi = &priminfos[last_jump];
571:
572: memcpy(code_here, pi->start+pi->length, pi->restlength);
573: code_here += pi->restlength;
574: last_jump=0;
575: flush_to_here();
576: }
577: }
578:
1.75 ! anton 579: /* Gforth remembers all code blocks in this list. On forgetting (by
! 580: executing a marker) the code blocks are not freed (because Gforth does
! 581: not remember how they were allocated; hmm, remembering that might be
! 582: easier and cleaner). Instead, code_here etc. are reset to the old
! 583: value, and the "forgotten" code blocks are reused when they are
! 584: needed. */
! 585:
! 586: struct code_block_list {
! 587: struct code_block_list *next;
! 588: Address block;
! 589: Cell size;
! 590: } *code_block_list=NULL, **next_code_blockp=&code_block_list;
! 591:
1.74 anton 592: Address append_prim(Cell p)
593: {
594: PrimInfo *pi = &priminfos[p];
595: Address old_code_here = code_here;
596:
597: if (code_area+code_area_size < code_here+pi->length+pi->restlength) {
1.75 ! anton 598: struct code_block_list *p;
1.74 anton 599: append_jump();
1.75 ! anton 600: if (*next_code_blockp == NULL) {
! 601: code_here = start_flush = code_area = my_alloc(code_area_size);
! 602: p = (struct code_block_list *)malloc(sizeof(struct code_block_list));
! 603: *next_code_blockp = p;
! 604: p->next = NULL;
! 605: p->block = code_here;
! 606: p->size = code_area_size;
! 607: } else {
! 608: p = *next_code_blockp;
! 609: code_here = start_flush = code_area = p->block;
! 610: }
1.74 anton 611: old_code_here = code_here;
1.75 ! anton 612: next_code_blockp = &(p->next);
1.74 anton 613: }
614: memcpy(code_here, pi->start, pi->length);
615: code_here += pi->length;
616: if (pi->superend)
617: flush_to_here();
618: return old_code_here;
619: }
620: #endif
1.75 ! anton 621:
! 622: int forget_dyncode(Address code)
! 623: {
! 624: #ifdef NO_DYNAMIC
! 625: return -1;
! 626: #else
! 627: struct code_block_list *p, **pp;
! 628:
! 629: for (pp=&code_block_list, p=*pp; p!=NULL; pp=&(p->next), p=*pp) {
! 630: if (code >= p->block && code < p->block+p->size) {
! 631: next_code_blockp = &(p->next);
! 632: code_here = start_flush = code;
! 633: code_area = p->block;
! 634: last_jump = 0;
! 635: return -1;
! 636: }
! 637: }
! 638: return 0;
! 639: #endif /* !defined(NO_DYNAMIC) */
! 640: }
! 641:
! 642: Label decompile_code(Label prim)
! 643: {
! 644: return prim;
! 645: }
1.74 anton 646:
1.70 anton 647: #ifdef NO_IP
648: int nbranchinfos=0;
649:
650: struct branchinfo {
651: Label *targetptr; /* *(bi->targetptr) is the target */
652: Cell *addressptr; /* store the target here */
653: } branchinfos[100000];
654:
655: int ndoesexecinfos=0;
656: struct doesexecinfo {
657: int branchinfo; /* fix the targetptr of branchinfos[...->branchinfo] */
658: Cell *xt; /* cfa of word whose does-code needs calling */
659: } doesexecinfos[10000];
660:
1.73 anton 661: /* definitions of N_execute etc. */
662: #include "prim_num.i"
1.70 anton 663:
664: void set_rel_target(Cell *source, Label target)
665: {
666: *source = ((Cell)target)-(((Cell)source)+4);
667: }
668:
669: void register_branchinfo(Label source, Cell targetptr)
670: {
671: struct branchinfo *bi = &(branchinfos[nbranchinfos]);
672: bi->targetptr = (Label *)targetptr;
673: bi->addressptr = (Cell *)source;
674: nbranchinfos++;
675: }
676:
677: Cell *compile_prim1arg(Cell p)
678: {
679: int l = priminfos[p].length;
680: Address old_code_here=code_here;
681:
1.74 anton 682: assert(vm_prims[p]==priminfos[p].start);
683: append_prim(p);
1.70 anton 684: return (Cell*)(old_code_here+priminfos[p].immargs[0].offset);
685: }
686:
687: Cell *compile_call2(Cell targetptr)
688: {
689: Cell *next_code_target;
1.73 anton 690: PrimInfo *pi = &priminfos[N_call2];
1.74 anton 691: Address old_code_here = append_prim(N_call2);
1.70 anton 692:
1.74 anton 693: next_code_target = (Cell *)(old_code_here + pi->immargs[0].offset);
694: register_branchinfo(old_code_here + pi->immargs[1].offset, targetptr);
1.70 anton 695: return next_code_target;
696: }
697: #endif
698:
699: void finish_code(void)
700: {
701: #ifdef NO_IP
702: Cell i;
703:
704: compile_prim1(NULL);
705: for (i=0; i<ndoesexecinfos; i++) {
706: struct doesexecinfo *dei = &doesexecinfos[i];
707: branchinfos[dei->branchinfo].targetptr = DOES_CODE1((dei->xt));
708: }
709: ndoesexecinfos = 0;
710: for (i=0; i<nbranchinfos; i++) {
711: struct branchinfo *bi=&branchinfos[i];
712: set_rel_target(bi->addressptr, *(bi->targetptr));
713: }
714: nbranchinfos = 0;
715: FLUSH_ICACHE(start_flush, code_here-start_flush);
716: start_flush=code_here;
1.48 anton 717: #endif
718: }
719:
1.70 anton 720: void compile_prim1(Cell *start)
1.48 anton 721: {
1.61 anton 722: #if defined(DOUBLY_INDIRECT)
1.70 anton 723: Label prim=(Label)*start;
1.54 anton 724: if (prim<((Label)(xts+DOESJUMP)) || prim>((Label)(xts+npriminfos))) {
725: fprintf(stderr,"compile_prim encountered xt %p\n", prim);
1.70 anton 726: *start=(Cell)prim;
727: return;
728: } else {
729: *start = prim-((Label)xts)+((Label)vm_prims);
730: return;
731: }
732: #elif defined(NO_IP)
733: static Cell *last_start=NULL;
734: static Xt last_prim=NULL;
735: /* delay work by one call in order to get relocated immargs */
736:
737: if (last_start) {
738: unsigned i = last_prim-vm_prims;
739: PrimInfo *pi=&priminfos[i];
740: Cell *next_code_target=NULL;
741:
742: assert(i<npriminfos);
1.73 anton 743: if (i==N_execute||i==N_perform||i==N_lit_perform) {
744: next_code_target = compile_prim1arg(N_set_next_code);
1.70 anton 745: }
1.73 anton 746: if (i==N_call) {
1.70 anton 747: next_code_target = compile_call2(last_start[1]);
1.73 anton 748: } else if (i==N_does_exec) {
1.70 anton 749: struct doesexecinfo *dei = &doesexecinfos[ndoesexecinfos++];
1.73 anton 750: *compile_prim1arg(N_lit) = (Cell)PFA(last_start[1]);
1.70 anton 751: /* we cannot determine the callee now (last_start[1] may be a
752: forward reference), so just register an arbitrary target, and
753: register in dei that we need to fix this before resolving
754: branches */
755: dei->branchinfo = nbranchinfos;
756: dei->xt = (Cell *)(last_start[1]);
757: next_code_target = compile_call2(NULL);
758: } else if (pi->start == NULL) { /* non-reloc */
1.73 anton 759: next_code_target = compile_prim1arg(N_set_next_code);
760: set_rel_target(compile_prim1arg(N_abranch),*(Xt)last_prim);
1.70 anton 761: } else {
762: unsigned j;
1.74 anton 763: Address old_code_here = append_prim(i);
1.70 anton 764:
765: for (j=0; j<pi->nimmargs; j++) {
766: struct immarg *ia = &(pi->immargs[j]);
767: Cell argval = last_start[pi->nimmargs - j]; /* !! specific to prims */
768: if (ia->rel) { /* !! assumption: relative refs are branches */
1.74 anton 769: register_branchinfo(old_code_here + ia->offset, argval);
1.70 anton 770: } else /* plain argument */
1.74 anton 771: *(Cell *)(old_code_here + ia->offset) = argval;
1.70 anton 772: }
773: }
774: if (next_code_target!=NULL)
775: *next_code_target = (Cell)code_here;
776: }
777: if (start) {
778: last_prim = (Xt)*start;
779: *start = (Cell)code_here;
780: }
781: last_start = start;
782: return;
783: #elif !defined(NO_DYNAMIC)
784: Label prim=(Label)*start;
1.58 anton 785: unsigned i;
1.74 anton 786: Address old_code_here;
1.48 anton 787:
1.58 anton 788: i = ((Xt)prim)-vm_prims;
1.56 anton 789: prim = *(Xt)prim;
1.70 anton 790: if (no_dynamic) {
791: *start = (Cell)prim;
792: return;
793: }
1.58 anton 794: if (i>=npriminfos || priminfos[i].start == 0) { /* not a relocatable prim */
1.74 anton 795: append_jump();
1.70 anton 796: *start = (Cell)prim;
797: return;
1.47 anton 798: }
1.58 anton 799: assert(priminfos[i].start = prim);
1.50 anton 800: #ifdef ALIGN_CODE
801: ALIGN_CODE;
802: #endif
1.74 anton 803: assert(prim==priminfos[i].start);
804: old_code_here = append_prim(i);
805: last_jump = (priminfos[i].superend) ? 0 : i;
1.70 anton 806: *start = (Cell)old_code_here;
807: return;
1.61 anton 808: #else /* !defined(DOUBLY_INDIRECT), no code replication */
1.70 anton 809: Label prim=(Label)*start;
1.61 anton 810: #if !defined(INDIRECT_THREADED)
1.56 anton 811: prim = *(Xt)prim;
1.61 anton 812: #endif
1.70 anton 813: *start = (Cell)prim;
814: return;
1.54 anton 815: #endif /* !defined(DOUBLY_INDIRECT) */
1.70 anton 816: }
817:
818: Label compile_prim(Label prim)
819: {
820: Cell x=(Cell)prim;
821: compile_prim1(&x);
822: return (Label)x;
1.47 anton 823: }
824:
1.69 anton 825: #if defined(PRINT_SUPER_LENGTHS) && !defined(NO_DYNAMIC)
1.59 anton 826: Cell prim_length(Cell prim)
827: {
828: return priminfos[prim+DOESJUMP+1].length;
829: }
830: #endif
831:
1.1 anton 832: Address loader(FILE *imagefile, char* filename)
833: /* returns the address of the image proper (after the preamble) */
834: {
835: ImageHeader header;
836: Address image;
837: Address imp; /* image+preamble */
1.17 anton 838: Char magic[8];
839: char magic7; /* size byte of magic number */
1.1 anton 840: Cell preamblesize=0;
1.6 pazsan 841: Cell data_offset = offset_image ? 56*sizeof(Cell) : 0;
1.1 anton 842: UCell check_sum;
1.15 pazsan 843: Cell ausize = ((RELINFOBITS == 8) ? 0 :
844: (RELINFOBITS == 16) ? 1 :
845: (RELINFOBITS == 32) ? 2 : 3);
846: Cell charsize = ((sizeof(Char) == 1) ? 0 :
847: (sizeof(Char) == 2) ? 1 :
848: (sizeof(Char) == 4) ? 2 : 3) + ausize;
849: Cell cellsize = ((sizeof(Cell) == 1) ? 0 :
850: (sizeof(Cell) == 2) ? 1 :
851: (sizeof(Cell) == 4) ? 2 : 3) + ausize;
1.21 anton 852: Cell sizebyte = (ausize << 5) + (charsize << 3) + (cellsize << 1) +
853: #ifdef WORDS_BIGENDIAN
854: 0
855: #else
856: 1
857: #endif
858: ;
1.1 anton 859:
1.43 anton 860: vm_prims = engine(0,0,0,0,0);
1.47 anton 861: check_prims(vm_prims);
1.1 anton 862: #ifndef DOUBLY_INDIRECT
1.59 anton 863: #ifdef PRINT_SUPER_LENGTHS
864: print_super_lengths();
865: #endif
1.43 anton 866: check_sum = checksum(vm_prims);
1.1 anton 867: #else /* defined(DOUBLY_INDIRECT) */
1.43 anton 868: check_sum = (UCell)vm_prims;
1.1 anton 869: #endif /* defined(DOUBLY_INDIRECT) */
1.10 pazsan 870:
871: do {
872: if(fread(magic,sizeof(Char),8,imagefile) < 8) {
1.15 pazsan 873: fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.4) image.\n",
1.10 pazsan 874: progname, filename);
875: exit(1);
1.1 anton 876: }
1.10 pazsan 877: preamblesize+=8;
1.15 pazsan 878: } while(memcmp(magic,"Gforth2",7));
1.17 anton 879: magic7 = magic[7];
1.1 anton 880: if (debug) {
1.17 anton 881: magic[7]='\0';
1.21 anton 882: fprintf(stderr,"Magic found: %s ", magic);
883: print_sizes(magic7);
1.1 anton 884: }
885:
1.21 anton 886: if (magic7 != sizebyte)
887: {
888: fprintf(stderr,"This image is: ");
889: print_sizes(magic7);
890: fprintf(stderr,"whereas the machine is ");
891: print_sizes(sizebyte);
1.1 anton 892: exit(-2);
893: };
894:
895: fread((void *)&header,sizeof(ImageHeader),1,imagefile);
1.10 pazsan 896:
897: set_stack_sizes(&header);
1.1 anton 898:
899: #if HAVE_GETPAGESIZE
900: pagesize=getpagesize(); /* Linux/GNU libc offers this */
901: #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
902: pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
903: #elif PAGESIZE
904: pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
905: #endif
906: if (debug)
1.5 jwilke 907: fprintf(stderr,"pagesize=%ld\n",(unsigned long) pagesize);
1.1 anton 908:
1.34 anton 909: image = dict_alloc_read(imagefile, preamblesize+header.image_size,
910: preamblesize+dictsize, data_offset);
1.33 anton 911: imp=image+preamblesize;
1.57 anton 912: alloc_stacks((ImageHeader *)imp);
1.1 anton 913: if (clear_dictionary)
1.33 anton 914: memset(imp+header.image_size, 0, dictsize-header.image_size);
1.46 jwilke 915: if(header.base==0 || header.base == 0x100) {
1.1 anton 916: Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
917: char reloc_bits[reloc_size];
1.33 anton 918: fseek(imagefile, preamblesize+header.image_size, SEEK_SET);
1.10 pazsan 919: fread(reloc_bits, 1, reloc_size, imagefile);
1.45 jwilke 920: relocate((Cell *)imp, reloc_bits, header.image_size, header.base, vm_prims);
1.1 anton 921: #if 0
922: { /* let's see what the relocator did */
923: FILE *snapshot=fopen("snapshot.fi","wb");
924: fwrite(image,1,imagesize,snapshot);
925: fclose(snapshot);
926: }
927: #endif
1.46 jwilke 928: }
929: else if(header.base!=imp) {
930: fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
931: progname, (unsigned long)header.base, (unsigned long)imp);
932: exit(1);
1.1 anton 933: }
934: if (header.checksum==0)
935: ((ImageHeader *)imp)->checksum=check_sum;
936: else if (header.checksum != check_sum) {
937: fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
938: progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
939: exit(1);
940: }
1.53 anton 941: #ifdef DOUBLY_INDIRECT
942: ((ImageHeader *)imp)->xt_base = xts;
943: #endif
1.1 anton 944: fclose(imagefile);
945:
1.56 anton 946: /* unnecessary, except maybe for CODE words */
947: /* FLUSH_ICACHE(imp, header.image_size);*/
1.1 anton 948:
949: return imp;
950: }
951:
1.72 anton 952: /* pointer to last '/' or '\' in file, 0 if there is none. */
953: char *onlypath(char *filename)
1.10 pazsan 954: {
1.72 anton 955: return strrchr(filename, DIRSEP);
1.1 anton 956: }
957:
958: FILE *openimage(char *fullfilename)
1.10 pazsan 959: {
960: FILE *image_file;
1.28 anton 961: char * expfilename = tilde_cstr(fullfilename, strlen(fullfilename), 1);
1.10 pazsan 962:
1.28 anton 963: image_file=fopen(expfilename,"rb");
1.1 anton 964: if (image_file!=NULL && debug)
1.28 anton 965: fprintf(stderr, "Opened image file: %s\n", expfilename);
1.10 pazsan 966: return image_file;
1.1 anton 967: }
968:
1.28 anton 969: /* try to open image file concat(path[0:len],imagename) */
1.1 anton 970: FILE *checkimage(char *path, int len, char *imagename)
1.10 pazsan 971: {
972: int dirlen=len;
1.1 anton 973: char fullfilename[dirlen+strlen(imagename)+2];
1.10 pazsan 974:
1.1 anton 975: memcpy(fullfilename, path, dirlen);
1.71 pazsan 976: if (fullfilename[dirlen-1]!=DIRSEP)
977: fullfilename[dirlen++]=DIRSEP;
1.1 anton 978: strcpy(fullfilename+dirlen,imagename);
1.10 pazsan 979: return openimage(fullfilename);
1.1 anton 980: }
981:
1.10 pazsan 982: FILE * open_image_file(char * imagename, char * path)
1.1 anton 983: {
1.10 pazsan 984: FILE * image_file=NULL;
1.28 anton 985: char *origpath=path;
1.10 pazsan 986:
1.71 pazsan 987: if(strchr(imagename, DIRSEP)==NULL) {
1.10 pazsan 988: /* first check the directory where the exe file is in !! 01may97jaw */
989: if (onlypath(progname))
1.72 anton 990: image_file=checkimage(progname, onlypath(progname)-progname, imagename);
1.10 pazsan 991: if (!image_file)
992: do {
993: char *pend=strchr(path, PATHSEP);
994: if (pend==NULL)
995: pend=path+strlen(path);
996: if (strlen(path)==0) break;
997: image_file=checkimage(path, pend-path, imagename);
998: path=pend+(*pend==PATHSEP);
999: } while (image_file==NULL);
1000: } else {
1001: image_file=openimage(imagename);
1002: }
1.1 anton 1003:
1.10 pazsan 1004: if (!image_file) {
1005: fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
1.28 anton 1006: progname, imagename, origpath);
1.10 pazsan 1007: exit(1);
1.7 anton 1008: }
1009:
1.10 pazsan 1010: return image_file;
1011: }
1.11 pazsan 1012: #endif
1013:
1014: #ifdef HAS_OS
1015: UCell convsize(char *s, UCell elemsize)
1016: /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
1017: of bytes. the letter at the end indicates the unit, where e stands
1018: for the element size. default is e */
1019: {
1020: char *endp;
1021: UCell n,m;
1022:
1023: m = elemsize;
1024: n = strtoul(s,&endp,0);
1025: if (endp!=NULL) {
1026: if (strcmp(endp,"b")==0)
1027: m=1;
1028: else if (strcmp(endp,"k")==0)
1029: m=1024;
1030: else if (strcmp(endp,"M")==0)
1031: m=1024*1024;
1032: else if (strcmp(endp,"G")==0)
1033: m=1024*1024*1024;
1034: else if (strcmp(endp,"T")==0) {
1035: #if (SIZEOF_CHAR_P > 4)
1.24 anton 1036: m=1024L*1024*1024*1024;
1.11 pazsan 1037: #else
1038: fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp);
1039: exit(1);
1040: #endif
1041: } else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
1042: fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
1043: exit(1);
1044: }
1045: }
1046: return n*m;
1047: }
1.10 pazsan 1048:
1049: void gforth_args(int argc, char ** argv, char ** path, char ** imagename)
1050: {
1051: int c;
1052:
1.1 anton 1053: opterr=0;
1054: while (1) {
1055: int option_index=0;
1056: static struct option opts[] = {
1.29 anton 1057: {"appl-image", required_argument, NULL, 'a'},
1.1 anton 1058: {"image-file", required_argument, NULL, 'i'},
1059: {"dictionary-size", required_argument, NULL, 'm'},
1060: {"data-stack-size", required_argument, NULL, 'd'},
1061: {"return-stack-size", required_argument, NULL, 'r'},
1062: {"fp-stack-size", required_argument, NULL, 'f'},
1063: {"locals-stack-size", required_argument, NULL, 'l'},
1064: {"path", required_argument, NULL, 'p'},
1065: {"version", no_argument, NULL, 'v'},
1066: {"help", no_argument, NULL, 'h'},
1067: /* put something != 0 into offset_image */
1068: {"offset-image", no_argument, &offset_image, 1},
1069: {"no-offset-im", no_argument, &offset_image, 0},
1070: {"clear-dictionary", no_argument, &clear_dictionary, 1},
1.4 anton 1071: {"die-on-signal", no_argument, &die_on_signal, 1},
1.1 anton 1072: {"debug", no_argument, &debug, 1},
1.60 anton 1073: {"no-super", no_argument, &no_super, 1},
1074: {"no-dynamic", no_argument, &no_dynamic, 1},
1.66 anton 1075: {"dynamic", no_argument, &no_dynamic, 0},
1.1 anton 1076: {0,0,0,0}
1077: /* no-init-file, no-rc? */
1078: };
1079:
1.36 pazsan 1080: c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vhoncsx", opts, &option_index);
1.1 anton 1081:
1082: switch (c) {
1.29 anton 1083: case EOF: return;
1084: case '?': optind--; return;
1085: case 'a': *imagename = optarg; return;
1.10 pazsan 1086: case 'i': *imagename = optarg; break;
1.1 anton 1087: case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
1088: case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
1089: case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
1090: case 'f': fsize = convsize(optarg,sizeof(Float)); break;
1091: case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
1.10 pazsan 1092: case 'p': *path = optarg; break;
1.36 pazsan 1093: case 'o': offset_image = 1; break;
1094: case 'n': offset_image = 0; break;
1095: case 'c': clear_dictionary = 1; break;
1096: case 's': die_on_signal = 1; break;
1097: case 'x': debug = 1; break;
1.8 anton 1098: case 'v': fprintf(stderr, "gforth %s\n", VERSION); exit(0);
1.1 anton 1099: case 'h':
1.29 anton 1100: fprintf(stderr, "Usage: %s [engine options] ['--'] [image arguments]\n\
1.1 anton 1101: Engine Options:\n\
1.29 anton 1102: --appl-image FILE equivalent to '--image-file=FILE --'\n\
1.10 pazsan 1103: --clear-dictionary Initialize the dictionary with 0 bytes\n\
1104: -d SIZE, --data-stack-size=SIZE Specify data stack size\n\
1105: --debug Print debugging information during startup\n\
1106: --die-on-signal exit instead of CATCHing some signals\n\
1.66 anton 1107: --dynamic use dynamic native code\n\
1.10 pazsan 1108: -f SIZE, --fp-stack-size=SIZE Specify floating point stack size\n\
1109: -h, --help Print this message and exit\n\
1110: -i FILE, --image-file=FILE Use image FILE instead of `gforth.fi'\n\
1111: -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
1112: -m SIZE, --dictionary-size=SIZE Specify Forth dictionary size\n\
1.60 anton 1113: --no-dynamic Use only statically compiled primitives\n\
1.10 pazsan 1114: --no-offset-im Load image at normal position\n\
1.60 anton 1115: --no-super No dynamically formed superinstructions\n\
1.10 pazsan 1116: --offset-image Load image at a different position\n\
1117: -p PATH, --path=PATH Search path for finding image and sources\n\
1118: -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
1.66 anton 1119: -v, --version Print engine version and exit\n\
1.1 anton 1120: SIZE arguments consist of an integer followed by a unit. The unit can be\n\
1.10 pazsan 1121: `b' (byte), `e' (element; default), `k' (KB), `M' (MB), `G' (GB) or `T' (TB).\n",
1122: argv[0]);
1123: optind--;
1124: return;
1.1 anton 1125: }
1126: }
1.10 pazsan 1127: }
1.11 pazsan 1128: #endif
1.10 pazsan 1129:
1130: #ifdef INCLUDE_IMAGE
1131: extern Cell image[];
1132: extern const char reloc_bits[];
1133: #endif
1134:
1.67 pazsan 1135: DCell double2ll(Float r)
1136: {
1137: #ifndef BUGGY_LONG_LONG
1138: return (DCell)(r);
1139: #else
1140: DCell d;
1141: d.hi = ldexp(r,-(int)(CELL_BITS)) - (r<0);
1142: d.lo = r-ldexp((Float)d.hi,CELL_BITS);
1143: return d;
1144: #endif
1145: }
1146:
1.10 pazsan 1147: int main(int argc, char **argv, char **env)
1148: {
1.30 pazsan 1149: #ifdef HAS_OS
1.10 pazsan 1150: char *path = getenv("GFORTHPATH") ? : DEFAULTPATH;
1.30 pazsan 1151: #else
1152: char *path = DEFAULTPATH;
1153: #endif
1.13 pazsan 1154: #ifndef INCLUDE_IMAGE
1.10 pazsan 1155: char *imagename="gforth.fi";
1156: FILE *image_file;
1157: Address image;
1158: #endif
1159: int retvalue;
1160:
1.56 anton 1161: #if defined(i386) && defined(ALIGNMENT_CHECK)
1.10 pazsan 1162: /* turn on alignment checks on the 486.
1163: * on the 386 this should have no effect. */
1164: __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
1165: /* this is unusable with Linux' libc.4.6.27, because this library is
1166: not alignment-clean; we would have to replace some library
1167: functions (e.g., memcpy) to make it work. Also GCC doesn't try to keep
1168: the stack FP-aligned. */
1169: #endif
1170:
1171: /* buffering of the user output device */
1.11 pazsan 1172: #ifdef _IONBF
1.10 pazsan 1173: if (isatty(fileno(stdout))) {
1174: fflush(stdout);
1175: setvbuf(stdout,NULL,_IONBF,0);
1.1 anton 1176: }
1.11 pazsan 1177: #endif
1.1 anton 1178:
1.10 pazsan 1179: progname = argv[0];
1180:
1.11 pazsan 1181: #ifdef HAS_OS
1.10 pazsan 1182: gforth_args(argc, argv, &path, &imagename);
1.11 pazsan 1183: #endif
1.10 pazsan 1184:
1185: #ifdef INCLUDE_IMAGE
1186: set_stack_sizes((ImageHeader *)image);
1.22 pazsan 1187: if(((ImageHeader *)image)->base != image)
1188: relocate(image, reloc_bits, ((ImageHeader *)image)->image_size,
1189: (Label*)engine(0, 0, 0, 0, 0));
1.10 pazsan 1190: alloc_stacks((ImageHeader *)image);
1191: #else
1192: image_file = open_image_file(imagename, path);
1193: image = loader(image_file, imagename);
1194: #endif
1.24 anton 1195: gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */
1.1 anton 1196:
1197: {
1.10 pazsan 1198: char path2[strlen(path)+1];
1.1 anton 1199: char *p1, *p2;
1200: Cell environ[]= {
1201: (Cell)argc-(optind-1),
1202: (Cell)(argv+(optind-1)),
1.10 pazsan 1203: (Cell)strlen(path),
1.1 anton 1204: (Cell)path2};
1205: argv[optind-1] = progname;
1206: /*
1207: for (i=0; i<environ[0]; i++)
1208: printf("%s\n", ((char **)(environ[1]))[i]);
1209: */
1210: /* make path OS-independent by replacing path separators with NUL */
1.10 pazsan 1211: for (p1=path, p2=path2; *p1!='\0'; p1++, p2++)
1.1 anton 1212: if (*p1==PATHSEP)
1213: *p2 = '\0';
1214: else
1215: *p2 = *p1;
1216: *p2='\0';
1.10 pazsan 1217: retvalue = go_forth(image, 4, environ);
1.42 anton 1218: #ifdef VM_PROFILING
1219: vm_print_profile(stderr);
1220: #endif
1.1 anton 1221: deprep_terminal();
1222: }
1.13 pazsan 1223: return retvalue;
1.1 anton 1224: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>