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