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