Annotation of gforth/engine/main.c, revision 1.130
1.1 anton 1: /* command line interpretation, image loading etc. for Gforth
2:
3:
1.103 anton 4: Copyright (C) 1995,1996,1997,1998,2000,2003 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"
1.82 anton 24: #include "forth.h"
1.1 anton 25: #include <errno.h>
26: #include <ctype.h>
27: #include <stdio.h>
1.2 pazsan 28: #include <unistd.h>
1.1 anton 29: #include <string.h>
30: #include <math.h>
31: #include <sys/types.h>
1.32 pazsan 32: #ifndef STANDALONE
1.1 anton 33: #include <sys/stat.h>
1.32 pazsan 34: #endif
1.1 anton 35: #include <fcntl.h>
36: #include <assert.h>
37: #include <stdlib.h>
1.102 anton 38: #include <signal.h>
1.11 pazsan 39: #ifndef STANDALONE
1.1 anton 40: #if HAVE_SYS_MMAN_H
41: #include <sys/mman.h>
42: #endif
1.11 pazsan 43: #endif
1.1 anton 44: #include "io.h"
45: #include "getopt.h"
1.11 pazsan 46: #ifdef STANDALONE
47: #include <systypes.h>
48: #endif
1.1 anton 49:
1.121 anton 50: typedef enum prim_num {
1.119 anton 51: /* definitions of N_execute etc. */
1.126 anton 52: #include PRIM_NUM_I
1.119 anton 53: N_START_SUPER
1.121 anton 54: } PrimNum;
1.119 anton 55:
1.79 anton 56: /* global variables for engine.c
57: We put them here because engine.c is compiled several times in
58: different ways for the same engine. */
59: Cell *SP;
60: Float *FP;
61: Address UP=NULL;
62:
1.115 pazsan 63: #ifdef HAS_FFCALL
64: Cell *RP;
65: Address LP;
66:
67: #include <callback.h>
68:
69: va_alist clist;
70:
1.116 pazsan 71: void engine_callback(Xt* fcall, void * alist)
1.115 pazsan 72: {
1.120 pazsan 73: clist = (va_alist)alist;
1.115 pazsan 74: engine(fcall, SP, RP, FP, LP);
75: }
76: #endif
77:
1.79 anton 78: #ifdef GFORTH_DEBUGGING
79: /* define some VM registers as global variables, so they survive exceptions;
80: global register variables are not up to the task (according to the
81: GNU C manual) */
82: Xt *saved_ip;
83: Cell *rp;
84: #endif
85:
86: #ifdef NO_IP
87: Label next_code;
88: #endif
89:
90: #ifdef HAS_FILE
91: char* fileattr[6]={"rb","rb","r+b","r+b","wb","wb"};
92: char* pfileattr[6]={"r","r","r+","r+","w","w"};
93:
94: #ifndef O_BINARY
95: #define O_BINARY 0
96: #endif
97: #ifndef O_TEXT
98: #define O_TEXT 0
99: #endif
100:
101: int ufileattr[6]= {
102: O_RDONLY|O_BINARY, O_RDONLY|O_BINARY,
103: O_RDWR |O_BINARY, O_RDWR |O_BINARY,
104: O_WRONLY|O_BINARY, O_WRONLY|O_BINARY };
105: #endif
106: /* end global vars for engine.c */
107:
1.1 anton 108: #define PRIM_VERSION 1
109: /* increment this whenever the primitives change in an incompatible way */
110:
1.14 pazsan 111: #ifndef DEFAULTPATH
1.39 anton 112: # define DEFAULTPATH "."
1.14 pazsan 113: #endif
114:
1.1 anton 115: #ifdef MSDOS
116: jmp_buf throw_jmp_buf;
117: #endif
118:
1.56 anton 119: #if defined(DOUBLY_INDIRECT)
120: # define CFA(n) ({Cell _n = (n); ((Cell)(((_n & 0x4000) ? symbols : xts)+(_n&~0x4000UL)));})
1.1 anton 121: #else
1.56 anton 122: # define CFA(n) ((Cell)(symbols+((n)&~0x4000UL)))
1.1 anton 123: #endif
124:
125: #define maxaligned(n) (typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
126:
127: static UCell dictsize=0;
128: static UCell dsize=0;
129: static UCell rsize=0;
130: static UCell fsize=0;
131: static UCell lsize=0;
132: int offset_image=0;
1.4 anton 133: int die_on_signal=0;
1.13 pazsan 134: #ifndef INCLUDE_IMAGE
1.1 anton 135: static int clear_dictionary=0;
1.24 anton 136: UCell pagesize=1;
1.22 pazsan 137: char *progname;
138: #else
139: char *progname = "gforth";
140: int optind = 1;
1.13 pazsan 141: #endif
1.31 pazsan 142:
1.97 anton 143: #define CODE_BLOCK_SIZE (256*1024)
1.48 anton 144: Address code_area=0;
1.73 anton 145: Cell code_area_size = CODE_BLOCK_SIZE;
1.75 anton 146: Address code_here=NULL+CODE_BLOCK_SIZE; /* does for code-area what HERE
147: does for the dictionary */
1.100 anton 148: Address start_flush=NULL; /* start of unflushed code */
1.74 anton 149: Cell last_jump=0; /* if the last prim was compiled without jump, this
150: is it's number, otherwise this contains 0 */
1.48 anton 151:
1.60 anton 152: static int no_super=0; /* true if compile_prim should not fuse prims */
1.81 anton 153: static int no_dynamic=NO_DYNAMIC_DEFAULT; /* if true, no code is generated
154: dynamically */
1.110 anton 155: static int print_metrics=0; /* if true, print metrics on exit */
1.109 anton 156: static int static_super_number = 10000000; /* number of ss used if available */
1.125 anton 157: #define MAX_STATE 4 /* maximum number of states */
158: static int maxstates = MAX_STATE; /* number of states for stack caching */
1.110 anton 159: static int ss_greedy = 0; /* if true: use greedy, not optimal ss selection */
1.60 anton 160:
1.30 pazsan 161: #ifdef HAS_DEBUG
1.68 anton 162: int debug=0;
1.31 pazsan 163: #else
164: # define perror(x...)
165: # define fprintf(x...)
1.30 pazsan 166: #endif
1.31 pazsan 167:
1.24 anton 168: ImageHeader *gforth_header;
1.43 anton 169: Label *vm_prims;
1.53 anton 170: #ifdef DOUBLY_INDIRECT
171: Label *xts; /* same content as vm_prims, but should only be used for xts */
172: #endif
1.1 anton 173:
1.125 anton 174: #ifndef NO_DYNAMIC
175: #define MAX_IMMARGS 2
176:
177: typedef struct {
178: Label start; /* NULL if not relocatable */
179: Cell length; /* only includes the jump iff superend is true*/
180: Cell restlength; /* length of the rest (i.e., the jump or (on superend) 0) */
181: char superend; /* true if primitive ends superinstruction, i.e.,
182: unconditional branch, execute, etc. */
183: Cell nimmargs;
184: struct immarg {
185: Cell offset; /* offset of immarg within prim */
186: char rel; /* true if immarg is relative */
187: } immargs[MAX_IMMARGS];
188: } PrimInfo;
189:
190: PrimInfo *priminfos;
191: PrimInfo **decomp_prims;
192:
193: static int is_relocatable(int p)
194: {
195: return !no_dynamic && priminfos[p].start != NULL;
196: }
197: #else /* defined(NO_DYNAMIC) */
198: static int is_relocatable(int p)
199: {
200: return 0;
201: }
202: #endif /* defined(NO_DYNAMIC) */
203:
1.30 pazsan 204: #ifdef MEMCMP_AS_SUBROUTINE
205: int gforth_memcmp(const char * s1, const char * s2, size_t n)
206: {
207: return memcmp(s1, s2, n);
208: }
209: #endif
210:
1.125 anton 211: static Cell max(Cell a, Cell b)
212: {
213: return a>b?a:b;
214: }
215:
216: static Cell min(Cell a, Cell b)
217: {
218: return a<b?a:b;
219: }
220:
1.1 anton 221: /* image file format:
1.15 pazsan 222: * "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.4.0 -i\n")
1.1 anton 223: * padding to a multiple of 8
1.84 anton 224: * magic: "Gforth3x" means format 0.6,
1.15 pazsan 225: * where x is a byte with
226: * bit 7: reserved = 0
227: * bit 6:5: address unit size 2^n octets
228: * bit 4:3: character size 2^n octets
229: * bit 2:1: cell size 2^n octets
230: * bit 0: endian, big=0, little=1.
231: * The magic are always 8 octets, no matter what the native AU/character size is
1.1 anton 232: * padding to max alignment (no padding necessary on current machines)
1.24 anton 233: * ImageHeader structure (see forth.h)
1.1 anton 234: * data (size in ImageHeader.image_size)
235: * tags ((if relocatable, 1 bit/data cell)
236: *
237: * tag==1 means that the corresponding word is an address;
238: * If the word is >=0, the address is within the image;
239: * addresses within the image are given relative to the start of the image.
240: * If the word =-1 (CF_NIL), the address is NIL,
241: * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
242: * If the word =CF(DODOES), it's a DOES> CFA
243: * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
244: * possibly containing a jump to dodoes)
1.51 anton 245: * If the word is <CF(DOESJUMP) and bit 14 is set, it's the xt of a primitive
246: * If the word is <CF(DOESJUMP) and bit 14 is clear,
247: * it's the threaded code of a primitive
1.85 pazsan 248: * bits 13..9 of a primitive token state which group the primitive belongs to,
249: * bits 8..0 of a primitive token index into the group
1.1 anton 250: */
251:
1.115 pazsan 252: Cell groups[32] = {
1.85 pazsan 253: 0,
1.121 anton 254: 0
1.90 anton 255: #undef GROUP
1.115 pazsan 256: #undef GROUPADD
257: #define GROUPADD(n) +n
258: #define GROUP(x, n) , 0
1.126 anton 259: #include PRIM_GRP_I
1.90 anton 260: #undef GROUP
1.115 pazsan 261: #undef GROUPADD
1.85 pazsan 262: #define GROUP(x, n)
1.115 pazsan 263: #define GROUPADD(n)
1.85 pazsan 264: };
265:
1.125 anton 266: unsigned char *branch_targets(Cell *image, const unsigned char *bitstring,
267: int size, Cell base)
268: /* produce a bitmask marking all the branch targets */
269: {
1.130 ! anton 270: int i=0, j, k, steps=(((size-1)/sizeof(Cell))/RELINFOBITS)+1;
1.125 anton 271: Cell token;
272: unsigned char bits;
1.130 ! anton 273: unsigned char *result=malloc(steps);
! 274:
! 275: memset(result, 0, steps);
! 276: for(k=0; k<steps; k++) {
1.125 anton 277: for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
1.130 ! anton 278: if(bits & (1U << (RELINFOBITS-1))) {
! 279: assert(i*sizeof(Cell) < size);
1.125 anton 280: token=image[i];
281: if (token>=base) { /* relocatable address */
282: UCell bitnum=(token-base)/sizeof(Cell);
283: result[bitnum/RELINFOBITS] |= 1U << ((~bitnum)&(RELINFOBITS-1));
284: }
285: }
286: }
287: }
288: return result;
289: }
290:
1.115 pazsan 291: void relocate(Cell *image, const unsigned char *bitstring,
1.90 anton 292: int size, Cell base, Label symbols[])
1.1 anton 293: {
1.130 ! anton 294: int i=0, j, k, steps=(((size-1)/sizeof(Cell))/RELINFOBITS)+1;
1.11 pazsan 295: Cell token;
1.1 anton 296: char bits;
1.37 anton 297: Cell max_symbols;
1.46 jwilke 298: /*
1.85 pazsan 299: * A virtual start address that's the real start address minus
1.46 jwilke 300: * the one in the image
301: */
1.45 jwilke 302: Cell *start = (Cell * ) (((void *) image) - ((void *) base));
1.125 anton 303: unsigned char *targets = branch_targets(image, bitstring, size, base);
1.1 anton 304:
1.85 pazsan 305: /* group index into table */
1.115 pazsan 306: if(groups[31]==0) {
307: int groupsum=0;
308: for(i=0; i<32; i++) {
309: groupsum += groups[i];
310: groups[i] = groupsum;
311: /* printf("group[%d]=%d\n",i,groupsum); */
312: }
313: i=0;
314: }
1.46 jwilke 315:
316: /* printf("relocating to %x[%x] start=%x base=%x\n", image, size, start, base); */
1.37 anton 317:
1.121 anton 318: for (max_symbols=0; symbols[max_symbols]!=0; max_symbols++)
1.37 anton 319: ;
1.47 anton 320: max_symbols--;
1.35 pazsan 321:
1.130 ! anton 322: for(k=0; k<steps; k++) {
1.13 pazsan 323: for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
1.1 anton 324: /* fprintf(stderr,"relocate: image[%d]\n", i);*/
1.130 ! anton 325: if(bits & (1U << (RELINFOBITS-1))) {
! 326: assert(i*sizeof(Cell) < size);
1.35 pazsan 327: /* fprintf(stderr,"relocate: image[%d]=%d of %d\n", i, image[i], size/sizeof(Cell)); */
1.45 jwilke 328: token=image[i];
1.85 pazsan 329: if(token<0) {
330: int group = (-token & 0x3E00) >> 9;
331: if(group == 0) {
332: switch(token|0x4000) {
1.1 anton 333: case CF_NIL : image[i]=0; break;
334: #if !defined(DOUBLY_INDIRECT)
335: case CF(DOCOL) :
336: case CF(DOVAR) :
337: case CF(DOCON) :
338: case CF(DOUSER) :
339: case CF(DODEFER) :
1.11 pazsan 340: case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(token)]); break;
1.92 anton 341: case CF(DOESJUMP): image[i]=0; break;
1.1 anton 342: #endif /* !defined(DOUBLY_INDIRECT) */
343: case CF(DODOES) :
1.45 jwilke 344: MAKE_DOES_CF(image+i,(Xt *)(image[i+1]+((Cell)start)));
1.1 anton 345: break;
1.85 pazsan 346: default : /* backward compatibility */
1.56 anton 347: /* printf("Code field generation image[%x]:=CFA(%x)\n",
1.1 anton 348: i, CF(image[i])); */
1.55 anton 349: if (CF((token | 0x4000))<max_symbols) {
1.56 anton 350: image[i]=(Cell)CFA(CF(token));
351: #ifdef DIRECT_THREADED
1.125 anton 352: if ((token & 0x4000) == 0) { /* threade code, no CFA */
353: if (targets[k] & (1U<<(RELINFOBITS-1-j)))
354: compile_prim1(0);
1.70 anton 355: compile_prim1(&image[i]);
1.125 anton 356: }
1.56 anton 357: #endif
1.55 anton 358: } else
1.115 pazsan 359: fprintf(stderr,"Primitive %ld used in this image at $%lx (offset $%x) is not implemented by this\n engine (%s); executing this code will crash.\n",(long)CF(token),(long)&image[i], i, PACKAGE_VERSION);
1.1 anton 360: }
1.85 pazsan 361: } else {
362: int tok = -token & 0x1FF;
363: if (tok < (groups[group+1]-groups[group])) {
364: #if defined(DOUBLY_INDIRECT)
365: image[i]=(Cell)CFA(((groups[group]+tok) | (CF(token) & 0x4000)));
366: #else
367: image[i]=(Cell)CFA((groups[group]+tok));
368: #endif
369: #ifdef DIRECT_THREADED
1.125 anton 370: if ((token & 0x4000) == 0) { /* threade code, no CFA */
371: if (targets[k] & (1U<<(RELINFOBITS-1-j)))
372: compile_prim1(0);
1.85 pazsan 373: compile_prim1(&image[i]);
1.125 anton 374: }
1.85 pazsan 375: #endif
376: } else
1.115 pazsan 377: fprintf(stderr,"Primitive %lx, %d of group %d used in this image at $%lx (offset $%x) is not implemented by this\n engine (%s); executing this code will crash.\n", (long)-token, tok, group, (long)&image[i],i,PACKAGE_VERSION);
1.85 pazsan 378: }
379: } else {
1.101 anton 380: /* if base is > 0: 0 is a null reference so don't adjust*/
1.45 jwilke 381: if (token>=base) {
382: image[i]+=(Cell)start;
383: }
1.46 jwilke 384: }
1.1 anton 385: }
386: }
1.31 pazsan 387: }
1.125 anton 388: free(targets);
1.70 anton 389: finish_code();
1.26 jwilke 390: ((ImageHeader*)(image))->base = (Address) image;
1.1 anton 391: }
392:
393: UCell checksum(Label symbols[])
394: {
395: UCell r=PRIM_VERSION;
396: Cell i;
397:
398: for (i=DOCOL; i<=DOESJUMP; i++) {
399: r ^= (UCell)(symbols[i]);
400: r = (r << 5) | (r >> (8*sizeof(Cell)-5));
401: }
402: #ifdef DIRECT_THREADED
403: /* we have to consider all the primitives */
404: for (; symbols[i]!=(Label)0; i++) {
405: r ^= (UCell)(symbols[i]);
406: r = (r << 5) | (r >> (8*sizeof(Cell)-5));
407: }
408: #else
409: /* in indirect threaded code all primitives are accessed through the
410: symbols table, so we just have to put the base address of symbols
411: in the checksum */
412: r ^= (UCell)symbols;
413: #endif
414: return r;
415: }
416:
1.3 anton 417: Address verbose_malloc(Cell size)
418: {
419: Address r;
420: /* leave a little room (64B) for stack underflows */
421: if ((r = malloc(size+64))==NULL) {
422: perror(progname);
423: exit(1);
424: }
425: r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
426: if (debug)
427: fprintf(stderr, "malloc succeeds, address=$%lx\n", (long)r);
428: return r;
429: }
430:
1.33 anton 431: static Address next_address=0;
432: void after_alloc(Address r, Cell size)
433: {
434: if (r != (Address)-1) {
435: if (debug)
436: fprintf(stderr, "success, address=$%lx\n", (long) r);
437: if (pagesize != 1)
438: next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */
439: } else {
440: if (debug)
441: fprintf(stderr, "failed: %s\n", strerror(errno));
442: }
443: }
444:
1.34 anton 445: #ifndef MAP_FAILED
446: #define MAP_FAILED ((Address) -1)
447: #endif
448: #ifndef MAP_FILE
449: # define MAP_FILE 0
450: #endif
451: #ifndef MAP_PRIVATE
452: # define MAP_PRIVATE 0
453: #endif
1.91 anton 454: #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
455: # define MAP_ANON MAP_ANONYMOUS
456: #endif
1.34 anton 457:
458: #if defined(HAVE_MMAP)
459: static Address alloc_mmap(Cell size)
1.1 anton 460: {
461: Address r;
462:
463: #if defined(MAP_ANON)
464: if (debug)
465: fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_ANON, ...); ", (long)next_address, (long)size);
1.34 anton 466: r = mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
1.1 anton 467: #else /* !defined(MAP_ANON) */
1.17 anton 468: /* Ultrix (at least) does not define MAP_FILE and MAP_PRIVATE (both are
469: apparently defaults) */
1.1 anton 470: static int dev_zero=-1;
471:
472: if (dev_zero == -1)
473: dev_zero = open("/dev/zero", O_RDONLY);
474: if (dev_zero == -1) {
1.34 anton 475: r = MAP_FAILED;
1.1 anton 476: if (debug)
477: fprintf(stderr, "open(\"/dev/zero\"...) failed (%s), no mmap; ",
478: strerror(errno));
479: } else {
480: if (debug)
481: fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FILE, dev_zero, ...); ", (long)next_address, (long)size);
482: r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE, dev_zero, 0);
483: }
484: #endif /* !defined(MAP_ANON) */
1.34 anton 485: after_alloc(r, size);
486: return r;
487: }
488: #endif
489:
490: Address my_alloc(Cell size)
491: {
492: #if HAVE_MMAP
493: Address r;
494:
495: r=alloc_mmap(size);
1.117 anton 496: if (r!=(Address)MAP_FAILED)
1.1 anton 497: return r;
498: #endif /* HAVE_MMAP */
1.3 anton 499: /* use malloc as fallback */
500: return verbose_malloc(size);
1.1 anton 501: }
502:
1.34 anton 503: Address dict_alloc_read(FILE *file, Cell imagesize, Cell dictsize, Cell offset)
1.33 anton 504: {
1.34 anton 505: Address image = MAP_FAILED;
1.33 anton 506:
1.56 anton 507: #if defined(HAVE_MMAP)
1.33 anton 508: if (offset==0) {
1.34 anton 509: image=alloc_mmap(dictsize);
1.33 anton 510: if (debug)
1.34 anton 511: fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FIXED|MAP_FILE, imagefile, 0); ", (long)image, (long)imagesize);
512: image = mmap(image, imagesize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FIXED|MAP_FILE|MAP_PRIVATE, fileno(file), 0);
513: after_alloc(image,dictsize);
1.33 anton 514: }
1.56 anton 515: #endif /* defined(HAVE_MMAP) */
1.117 anton 516: if (image == (Address)MAP_FAILED) {
1.56 anton 517: image = my_alloc(dictsize+offset)+offset;
1.33 anton 518: rewind(file); /* fseek(imagefile,0L,SEEK_SET); */
1.34 anton 519: fread(image, 1, imagesize, file);
1.33 anton 520: }
521: return image;
522: }
523:
1.10 pazsan 524: void set_stack_sizes(ImageHeader * header)
525: {
526: if (dictsize==0)
527: dictsize = header->dict_size;
528: if (dsize==0)
529: dsize = header->data_stack_size;
530: if (rsize==0)
531: rsize = header->return_stack_size;
532: if (fsize==0)
533: fsize = header->fp_stack_size;
534: if (lsize==0)
535: lsize = header->locals_stack_size;
536: dictsize=maxaligned(dictsize);
537: dsize=maxaligned(dsize);
538: rsize=maxaligned(rsize);
539: lsize=maxaligned(lsize);
540: fsize=maxaligned(fsize);
541: }
542:
543: void alloc_stacks(ImageHeader * header)
544: {
545: header->dict_size=dictsize;
546: header->data_stack_size=dsize;
547: header->fp_stack_size=fsize;
548: header->return_stack_size=rsize;
549: header->locals_stack_size=lsize;
550:
551: header->data_stack_base=my_alloc(dsize);
552: header->fp_stack_base=my_alloc(fsize);
553: header->return_stack_base=my_alloc(rsize);
554: header->locals_stack_base=my_alloc(lsize);
555: }
556:
1.44 pazsan 557: #warning You can ignore the warnings about clobbered variables in go_forth
1.11 pazsan 558: int go_forth(Address image, int stack, Cell *entries)
559: {
1.38 anton 560: volatile ImageHeader *image_header = (ImageHeader *)image;
1.18 anton 561: Cell *sp0=(Cell*)(image_header->data_stack_base + dsize);
1.44 pazsan 562: Cell *rp0=(Cell *)(image_header->return_stack_base + rsize);
1.18 anton 563: Float *fp0=(Float *)(image_header->fp_stack_base + fsize);
1.44 pazsan 564: #ifdef GFORTH_DEBUGGING
1.38 anton 565: volatile Cell *orig_rp0=rp0;
1.44 pazsan 566: #endif
1.18 anton 567: Address lp0=image_header->locals_stack_base + lsize;
568: Xt *ip0=(Xt *)(image_header->boot_entry);
1.13 pazsan 569: #ifdef SYSSIGNALS
1.11 pazsan 570: int throw_code;
1.13 pazsan 571: #endif
1.11 pazsan 572:
573: /* ensure that the cached elements (if any) are accessible */
1.41 anton 574: IF_spTOS(sp0--);
575: IF_fpTOS(fp0--);
1.11 pazsan 576:
577: for(;stack>0;stack--)
1.18 anton 578: *--sp0=entries[stack-1];
1.11 pazsan 579:
1.30 pazsan 580: #ifdef SYSSIGNALS
1.11 pazsan 581: get_winsize();
582:
583: install_signal_handlers(); /* right place? */
584:
585: if ((throw_code=setjmp(throw_jmp_buf))) {
586: static Cell signal_data_stack[8];
587: static Cell signal_return_stack[8];
588: static Float signal_fp_stack[1];
1.13 pazsan 589:
1.11 pazsan 590: signal_data_stack[7]=throw_code;
1.18 anton 591:
592: #ifdef GFORTH_DEBUGGING
1.97 anton 593: if (debug)
594: fprintf(stderr,"\ncaught signal, throwing exception %d, ip=%p rp=%p\n",
595: throw_code, saved_ip, rp);
1.38 anton 596: if (rp <= orig_rp0 && rp > (Cell *)(image_header->return_stack_base+5)) {
1.18 anton 597: /* no rstack overflow or underflow */
598: rp0 = rp;
1.63 anton 599: *--rp0 = (Cell)saved_ip;
1.18 anton 600: }
601: else /* I love non-syntactic ifdefs :-) */
1.97 anton 602: rp0 = signal_return_stack+8;
603: #else /* !defined(GFORTH_DEBUGGING) */
604: if (debug)
605: fprintf(stderr,"\ncaught signal, throwing exception %d\n", throw_code);
606: rp0 = signal_return_stack+8;
607: #endif /* !defined(GFORTH_DEBUGGING) */
1.25 anton 608: /* fprintf(stderr, "rp=$%x\n",rp0);*/
1.11 pazsan 609:
1.33 anton 610: return((int)(Cell)engine(image_header->throw_entry, signal_data_stack+7,
1.18 anton 611: rp0, signal_fp_stack, 0));
1.11 pazsan 612: }
1.13 pazsan 613: #endif
1.11 pazsan 614:
1.33 anton 615: return((int)(Cell)engine(ip0,sp0,rp0,fp0,lp0));
1.11 pazsan 616: }
617:
1.30 pazsan 618: #ifndef INCLUDE_IMAGE
1.21 anton 619: void print_sizes(Cell sizebyte)
620: /* print size information */
621: {
622: static char* endianstring[]= { " big","little" };
623:
624: fprintf(stderr,"%s endian, cell=%d bytes, char=%d bytes, au=%d bytes\n",
625: endianstring[sizebyte & 1],
626: 1 << ((sizebyte >> 1) & 3),
627: 1 << ((sizebyte >> 3) & 3),
628: 1 << ((sizebyte >> 5) & 3));
629: }
630:
1.106 anton 631: /* static superinstruction stuff */
632:
633: struct cost {
634: char loads; /* number of stack loads */
635: char stores; /* number of stack stores */
636: char updates; /* number of stack pointer updates */
1.123 anton 637: char branch; /* is it a branch (SET_IP) */
1.125 anton 638: unsigned char state_in; /* state on entry */
639: unsigned char state_out; /* state on exit */
1.123 anton 640: short offset; /* offset into super2 table */
1.125 anton 641: unsigned char length; /* number of components */
1.106 anton 642: };
643:
1.121 anton 644: PrimNum super2[] = {
1.126 anton 645: #include SUPER2_I
1.106 anton 646: };
647:
648: struct cost super_costs[] = {
1.126 anton 649: #include COSTS_I
1.106 anton 650: };
651:
1.125 anton 652: struct super_state {
653: struct super_state *next;
654: PrimNum super;
655: };
656:
1.106 anton 657: #define HASH_SIZE 256
658:
659: struct super_table_entry {
660: struct super_table_entry *next;
1.121 anton 661: PrimNum *start;
1.106 anton 662: short length;
1.125 anton 663: struct super_state *ss_list; /* list of supers */
1.106 anton 664: } *super_table[HASH_SIZE];
665: int max_super=2;
666:
1.125 anton 667: struct super_state *state_transitions=NULL;
668:
1.121 anton 669: int hash_super(PrimNum *start, int length)
1.106 anton 670: {
671: int i, r;
672:
673: for (i=0, r=0; i<length; i++) {
674: r <<= 1;
675: r += start[i];
676: }
677: return r & (HASH_SIZE-1);
678: }
679:
1.125 anton 680: struct super_state **lookup_super(PrimNum *start, int length)
1.106 anton 681: {
682: int hash=hash_super(start,length);
683: struct super_table_entry *p = super_table[hash];
684:
1.125 anton 685: /* assert(length >= 2); */
1.106 anton 686: for (; p!=NULL; p = p->next) {
687: if (length == p->length &&
1.121 anton 688: memcmp((char *)p->start, (char *)start, length*sizeof(PrimNum))==0)
1.125 anton 689: return &(p->ss_list);
1.106 anton 690: }
1.125 anton 691: return NULL;
1.106 anton 692: }
693:
694: void prepare_super_table()
695: {
696: int i;
1.109 anton 697: int nsupers = 0;
1.106 anton 698:
699: for (i=0; i<sizeof(super_costs)/sizeof(super_costs[0]); i++) {
700: struct cost *c = &super_costs[i];
1.125 anton 701: if ((c->length < 2 || nsupers < static_super_number) &&
702: c->state_in < maxstates && c->state_out < maxstates) {
703: struct super_state **ss_listp= lookup_super(super2+c->offset, c->length);
704: struct super_state *ss = malloc(sizeof(struct super_state));
705: ss->super= i;
706: if (c->offset==N_noop && i != N_noop) {
707: if (is_relocatable(i)) {
708: ss->next = state_transitions;
709: state_transitions = ss;
710: }
711: } else if (ss_listp != NULL) {
712: ss->next = *ss_listp;
713: *ss_listp = ss;
714: } else {
715: int hash = hash_super(super2+c->offset, c->length);
716: struct super_table_entry **p = &super_table[hash];
717: struct super_table_entry *e = malloc(sizeof(struct super_table_entry));
718: ss->next = NULL;
719: e->next = *p;
720: e->start = super2 + c->offset;
721: e->length = c->length;
722: e->ss_list = ss;
723: *p = e;
724: }
1.106 anton 725: if (c->length > max_super)
726: max_super = c->length;
1.125 anton 727: if (c->length >= 2)
728: nsupers++;
1.106 anton 729: }
730: }
1.109 anton 731: if (debug)
732: fprintf(stderr, "Using %d static superinsts\n", nsupers);
1.106 anton 733: }
734:
735: /* dynamic replication/superinstruction stuff */
736:
1.69 anton 737: #ifndef NO_DYNAMIC
1.90 anton 738: int compare_priminfo_length(const void *_a, const void *_b)
1.76 anton 739: {
1.90 anton 740: PrimInfo **a = (PrimInfo **)_a;
741: PrimInfo **b = (PrimInfo **)_b;
1.77 anton 742: Cell diff = (*a)->length - (*b)->length;
743: if (diff)
744: return diff;
745: else /* break ties by start address; thus the decompiler produces
746: the earliest primitive with the same code (e.g. noop instead
747: of (char) and @ instead of >code-address */
748: return (*b)->start - (*a)->start;
1.76 anton 749: }
1.112 anton 750: #endif /* !defined(NO_DYNAMIC) */
1.76 anton 751:
1.125 anton 752: static char MAYBE_UNUSED superend[]={
1.126 anton 753: #include PRIM_SUPEREND_I
1.106 anton 754: };
1.107 anton 755:
756: Cell npriminfos=0;
1.76 anton 757:
1.114 anton 758: int compare_labels(const void *pa, const void *pb)
1.113 anton 759: {
1.114 anton 760: Label a = *(Label *)pa;
761: Label b = *(Label *)pb;
762: return a-b;
763: }
1.113 anton 764:
1.114 anton 765: Label bsearch_next(Label key, Label *a, UCell n)
766: /* a is sorted; return the label >=key that is the closest in a;
767: return NULL if there is no label in a >=key */
768: {
769: int mid = (n-1)/2;
770: if (n<1)
771: return NULL;
772: if (n == 1) {
773: if (a[0] < key)
774: return NULL;
775: else
776: return a[0];
777: }
778: if (a[mid] < key)
779: return bsearch_next(key, a+mid+1, n-mid-1);
780: else
781: return bsearch_next(key, a, mid+1);
1.113 anton 782: }
783:
1.47 anton 784: void check_prims(Label symbols1[])
785: {
786: int i;
1.90 anton 787: #ifndef NO_DYNAMIC
1.119 anton 788: Label *symbols2, *symbols3, *ends1, *ends1j, *ends1jsorted;
789: int nends1j;
1.90 anton 790: #endif
1.47 anton 791:
1.66 anton 792: if (debug)
793: #ifdef __VERSION__
794: fprintf(stderr, "Compiled with gcc-" __VERSION__ "\n");
795: #else
796: #define xstr(s) str(s)
797: #define str(s) #s
798: fprintf(stderr, "Compiled with gcc-" xstr(__GNUC__) "." xstr(__GNUC_MINOR__) "\n");
799: #endif
1.121 anton 800: for (i=0; symbols1[i]!=0; i++)
1.47 anton 801: ;
1.55 anton 802: npriminfos = i;
1.70 anton 803:
804: #ifndef NO_DYNAMIC
1.66 anton 805: if (no_dynamic)
806: return;
1.55 anton 807: symbols2=engine2(0,0,0,0,0);
1.70 anton 808: #if NO_IP
809: symbols3=engine3(0,0,0,0,0);
810: #else
811: symbols3=symbols1;
812: #endif
1.121 anton 813: ends1 = symbols1+i+1;
1.119 anton 814: ends1j = ends1+i;
1.121 anton 815: nends1j = i+1;
1.119 anton 816: ends1jsorted = (Label *)alloca(nends1j*sizeof(Label));
817: memcpy(ends1jsorted,ends1j,nends1j*sizeof(Label));
818: qsort(ends1jsorted, nends1j, sizeof(Label), compare_labels);
1.113 anton 819:
1.47 anton 820: priminfos = calloc(i,sizeof(PrimInfo));
1.121 anton 821: for (i=0; symbols1[i]!=0; i++) {
1.70 anton 822: int prim_len = ends1[i]-symbols1[i];
1.47 anton 823: PrimInfo *pi=&priminfos[i];
1.70 anton 824: int j=0;
825: char *s1 = (char *)symbols1[i];
826: char *s2 = (char *)symbols2[i];
827: char *s3 = (char *)symbols3[i];
1.119 anton 828: Label endlabel = bsearch_next(symbols1[i]+1,ends1jsorted,nends1j);
1.70 anton 829:
830: pi->start = s1;
1.121 anton 831: pi->superend = superend[i]|no_super;
1.70 anton 832: if (pi->superend)
1.113 anton 833: pi->length = endlabel-symbols1[i];
1.70 anton 834: else
835: pi->length = prim_len;
1.113 anton 836: pi->restlength = endlabel - symbols1[i] - pi->length;
1.70 anton 837: pi->nimmargs = 0;
838: if (debug)
1.98 anton 839: fprintf(stderr, "Prim %3d @ %p %p %p, length=%3ld restlength=%2ld superend=%1d",
840: i, s1, s2, s3, (long)(pi->length), (long)(pi->restlength), pi->superend);
1.114 anton 841: if (endlabel == NULL) {
842: pi->start = NULL; /* not relocatable */
1.122 anton 843: if (pi->length<0) pi->length=100;
1.114 anton 844: if (debug)
1.119 anton 845: fprintf(stderr,"\n non_reloc: no J label > start found\n");
1.114 anton 846: continue;
847: }
848: if (ends1[i] > endlabel && !pi->superend) {
1.113 anton 849: pi->start = NULL; /* not relocatable */
1.122 anton 850: pi->length = endlabel-symbols1[i];
1.113 anton 851: if (debug)
1.121 anton 852: fprintf(stderr,"\n non_reloc: there is a J label before the K label (restlength<0)\n");
1.113 anton 853: continue;
854: }
1.114 anton 855: if (ends1[i] < pi->start && !pi->superend) {
1.113 anton 856: pi->start = NULL; /* not relocatable */
1.122 anton 857: pi->length = endlabel-symbols1[i];
1.113 anton 858: if (debug)
1.119 anton 859: fprintf(stderr,"\n non_reloc: K label before I label (length<0)\n");
1.113 anton 860: continue;
861: }
1.70 anton 862: assert(prim_len>=0);
1.113 anton 863: assert(pi->restlength >=0);
1.74 anton 864: while (j<(pi->length+pi->restlength)) {
1.70 anton 865: if (s1[j]==s3[j]) {
866: if (s1[j] != s2[j]) {
867: pi->start = NULL; /* not relocatable */
868: if (debug)
869: fprintf(stderr,"\n non_reloc: engine1!=engine2 offset %3d",j);
1.74 anton 870: /* assert(j<prim_len); */
1.70 anton 871: break;
872: }
873: j++;
874: } else {
875: struct immarg *ia=&pi->immargs[pi->nimmargs];
876:
877: pi->nimmargs++;
878: ia->offset=j;
879: if ((~*(Cell *)&(s1[j]))==*(Cell *)&(s3[j])) {
880: ia->rel=0;
881: if (debug)
882: fprintf(stderr,"\n absolute immarg: offset %3d",j);
883: } else if ((&(s1[j]))+(*(Cell *)&(s1[j]))+4 ==
884: symbols1[DOESJUMP+1]) {
885: ia->rel=1;
886: if (debug)
887: fprintf(stderr,"\n relative immarg: offset %3d",j);
888: } else {
889: pi->start = NULL; /* not relocatable */
890: if (debug)
891: fprintf(stderr,"\n non_reloc: engine1!=engine3 offset %3d",j);
1.74 anton 892: /* assert(j<prim_len);*/
1.70 anton 893: break;
894: }
895: j+=4;
1.47 anton 896: }
897: }
1.70 anton 898: if (debug)
899: fprintf(stderr,"\n");
900: }
1.76 anton 901: decomp_prims = calloc(i,sizeof(PrimInfo *));
902: for (i=DOESJUMP+1; i<npriminfos; i++)
903: decomp_prims[i] = &(priminfos[i]);
904: qsort(decomp_prims+DOESJUMP+1, npriminfos-DOESJUMP-1, sizeof(PrimInfo *),
905: compare_priminfo_length);
1.70 anton 906: #endif
907: }
908:
1.74 anton 909: void flush_to_here(void)
910: {
1.93 anton 911: #ifndef NO_DYNAMIC
1.100 anton 912: if (start_flush)
913: FLUSH_ICACHE(start_flush, code_here-start_flush);
1.74 anton 914: start_flush=code_here;
1.93 anton 915: #endif
1.74 anton 916: }
917:
1.93 anton 918: #ifndef NO_DYNAMIC
1.74 anton 919: void append_jump(void)
920: {
921: if (last_jump) {
922: PrimInfo *pi = &priminfos[last_jump];
923:
924: memcpy(code_here, pi->start+pi->length, pi->restlength);
925: code_here += pi->restlength;
926: last_jump=0;
927: }
928: }
929:
1.75 anton 930: /* Gforth remembers all code blocks in this list. On forgetting (by
931: executing a marker) the code blocks are not freed (because Gforth does
932: not remember how they were allocated; hmm, remembering that might be
933: easier and cleaner). Instead, code_here etc. are reset to the old
934: value, and the "forgotten" code blocks are reused when they are
935: needed. */
936:
937: struct code_block_list {
938: struct code_block_list *next;
939: Address block;
940: Cell size;
941: } *code_block_list=NULL, **next_code_blockp=&code_block_list;
942:
1.74 anton 943: Address append_prim(Cell p)
944: {
945: PrimInfo *pi = &priminfos[p];
946: Address old_code_here = code_here;
947:
948: if (code_area+code_area_size < code_here+pi->length+pi->restlength) {
1.75 anton 949: struct code_block_list *p;
1.74 anton 950: append_jump();
1.93 anton 951: flush_to_here();
1.75 anton 952: if (*next_code_blockp == NULL) {
953: code_here = start_flush = code_area = my_alloc(code_area_size);
954: p = (struct code_block_list *)malloc(sizeof(struct code_block_list));
955: *next_code_blockp = p;
956: p->next = NULL;
957: p->block = code_here;
958: p->size = code_area_size;
959: } else {
960: p = *next_code_blockp;
961: code_here = start_flush = code_area = p->block;
962: }
1.74 anton 963: old_code_here = code_here;
1.75 anton 964: next_code_blockp = &(p->next);
1.74 anton 965: }
966: memcpy(code_here, pi->start, pi->length);
967: code_here += pi->length;
968: return old_code_here;
969: }
970: #endif
1.75 anton 971:
972: int forget_dyncode(Address code)
973: {
974: #ifdef NO_DYNAMIC
975: return -1;
976: #else
977: struct code_block_list *p, **pp;
978:
979: for (pp=&code_block_list, p=*pp; p!=NULL; pp=&(p->next), p=*pp) {
980: if (code >= p->block && code < p->block+p->size) {
981: next_code_blockp = &(p->next);
982: code_here = start_flush = code;
983: code_area = p->block;
984: last_jump = 0;
985: return -1;
986: }
987: }
1.78 anton 988: return -no_dynamic;
1.75 anton 989: #endif /* !defined(NO_DYNAMIC) */
990: }
991:
1.104 anton 992: long dyncodesize(void)
993: {
994: #ifndef NO_DYNAMIC
1.106 anton 995: struct code_block_list *p;
1.104 anton 996: long size=0;
997: for (p=code_block_list; p!=NULL; p=p->next) {
998: if (code_here >= p->block && code_here < p->block+p->size)
999: return size + (code_here - p->block);
1000: else
1001: size += p->size;
1002: }
1003: #endif /* !defined(NO_DYNAMIC) */
1004: return 0;
1005: }
1006:
1.90 anton 1007: Label decompile_code(Label _code)
1.75 anton 1008: {
1.76 anton 1009: #ifdef NO_DYNAMIC
1.90 anton 1010: return _code;
1.76 anton 1011: #else /* !defined(NO_DYNAMIC) */
1012: Cell i;
1.77 anton 1013: struct code_block_list *p;
1.90 anton 1014: Address code=_code;
1.76 anton 1015:
1.77 anton 1016: /* first, check if we are in code at all */
1017: for (p = code_block_list;; p = p->next) {
1018: if (p == NULL)
1019: return code;
1020: if (code >= p->block && code < p->block+p->size)
1021: break;
1022: }
1.76 anton 1023: /* reverse order because NOOP might match other prims */
1024: for (i=npriminfos-1; i>DOESJUMP; i--) {
1025: PrimInfo *pi=decomp_prims[i];
1026: if (pi->start==code || (pi->start && memcmp(code,pi->start,pi->length)==0))
1.121 anton 1027: return vm_prims[super2[super_costs[pi-priminfos].offset]];
1.118 anton 1028: /* return pi->start;*/
1.76 anton 1029: }
1030: return code;
1031: #endif /* !defined(NO_DYNAMIC) */
1.75 anton 1032: }
1.74 anton 1033:
1.70 anton 1034: #ifdef NO_IP
1035: int nbranchinfos=0;
1036:
1037: struct branchinfo {
1038: Label *targetptr; /* *(bi->targetptr) is the target */
1039: Cell *addressptr; /* store the target here */
1040: } branchinfos[100000];
1041:
1042: int ndoesexecinfos=0;
1043: struct doesexecinfo {
1044: int branchinfo; /* fix the targetptr of branchinfos[...->branchinfo] */
1045: Cell *xt; /* cfa of word whose does-code needs calling */
1046: } doesexecinfos[10000];
1047:
1048: void set_rel_target(Cell *source, Label target)
1049: {
1050: *source = ((Cell)target)-(((Cell)source)+4);
1051: }
1052:
1053: void register_branchinfo(Label source, Cell targetptr)
1054: {
1055: struct branchinfo *bi = &(branchinfos[nbranchinfos]);
1056: bi->targetptr = (Label *)targetptr;
1057: bi->addressptr = (Cell *)source;
1058: nbranchinfos++;
1059: }
1060:
1.128 anton 1061: Cell *compile_prim1arg(PrimNum p)
1.70 anton 1062: {
1063: int l = priminfos[p].length;
1064: Address old_code_here=code_here;
1065:
1.74 anton 1066: assert(vm_prims[p]==priminfos[p].start);
1067: append_prim(p);
1.70 anton 1068: return (Cell*)(old_code_here+priminfos[p].immargs[0].offset);
1069: }
1070:
1071: Cell *compile_call2(Cell targetptr)
1072: {
1073: Cell *next_code_target;
1.73 anton 1074: PrimInfo *pi = &priminfos[N_call2];
1.74 anton 1075: Address old_code_here = append_prim(N_call2);
1.70 anton 1076:
1.74 anton 1077: next_code_target = (Cell *)(old_code_here + pi->immargs[0].offset);
1078: register_branchinfo(old_code_here + pi->immargs[1].offset, targetptr);
1.70 anton 1079: return next_code_target;
1080: }
1081: #endif
1082:
1083: void finish_code(void)
1084: {
1085: #ifdef NO_IP
1086: Cell i;
1087:
1088: compile_prim1(NULL);
1089: for (i=0; i<ndoesexecinfos; i++) {
1090: struct doesexecinfo *dei = &doesexecinfos[i];
1091: branchinfos[dei->branchinfo].targetptr = DOES_CODE1((dei->xt));
1092: }
1093: ndoesexecinfos = 0;
1094: for (i=0; i<nbranchinfos; i++) {
1095: struct branchinfo *bi=&branchinfos[i];
1096: set_rel_target(bi->addressptr, *(bi->targetptr));
1097: }
1098: nbranchinfos = 0;
1.128 anton 1099: #else
1100: compile_prim1(NULL);
1.48 anton 1101: #endif
1.93 anton 1102: flush_to_here();
1.48 anton 1103: }
1104:
1.128 anton 1105: #ifdef NO_IP
1106: Cell compile_prim_dyn(PrimNum p, Cell *tcp)
1107: /* compile prim #p dynamically (mod flags etc.) and return start
1108: address of generated code for putting it into the threaded
1109: code. This function is only called if all the associated
1110: inline arguments of p are already in place (at tcp[1] etc.) */
1111: {
1112: PrimInfo *pi=&priminfos[p];
1113: Cell *next_code_target=NULL;
1114: Cell codeaddr = (Cell)code_here;
1115:
1116: assert(p<npriminfos);
1117: if (p==N_execute || p==N_perform || p==N_lit_perform) {
1118: next_code_target = compile_prim1arg(N_set_next_code);
1119: }
1120: if (p==N_call) {
1121: next_code_target = compile_call2(tcp[1]);
1122: } else if (p==N_does_exec) {
1123: struct doesexecinfo *dei = &doesexecinfos[ndoesexecinfos++];
1124: *compile_prim1arg(N_lit) = (Cell)PFA(tcp[1]);
1125: /* we cannot determine the callee now (last_start[1] may be a
1126: forward reference), so just register an arbitrary target, and
1127: register in dei that we need to fix this before resolving
1128: branches */
1129: dei->branchinfo = nbranchinfos;
1130: dei->xt = (Cell *)(tcp[1]);
1131: next_code_target = compile_call2(0);
1132: } else if (!is_relocatable(p)) {
1133: next_code_target = compile_prim1arg(N_set_next_code);
1134: set_rel_target(compile_prim1arg(N_branch),vm_prims[p]);
1135: } else {
1136: unsigned j;
1137: Address old_code_here = append_prim(p);
1138:
1139: for (j=0; j<pi->nimmargs; j++) {
1140: struct immarg *ia = &(pi->immargs[j]);
1141: Cell argval = tcp[pi->nimmargs - j]; /* !! specific to prims */
1142: if (ia->rel) { /* !! assumption: relative refs are branches */
1143: register_branchinfo(old_code_here + ia->offset, argval);
1144: } else /* plain argument */
1145: *(Cell *)(old_code_here + ia->offset) = argval;
1146: }
1147: }
1148: if (next_code_target!=NULL)
1149: *next_code_target = (Cell)code_here;
1150: return codeaddr;
1151: }
1152: #else /* !defined(NO_IP) */
1153: Cell compile_prim_dyn(PrimNum p, Cell *tcp)
1154: /* compile prim #p dynamically (mod flags etc.) and return start
1155: address of generated code for putting it into the threaded code */
1.108 anton 1156: {
1.121 anton 1157: Cell static_prim = (Cell)vm_prims[p];
1.108 anton 1158: #if defined(NO_DYNAMIC)
1159: return static_prim;
1160: #else /* !defined(NO_DYNAMIC) */
1161: Address old_code_here;
1162:
1163: if (no_dynamic)
1164: return static_prim;
1.125 anton 1165: if (p>=npriminfos || !is_relocatable(p)) {
1.108 anton 1166: append_jump();
1167: return static_prim;
1168: }
1169: old_code_here = append_prim(p);
1170: last_jump = (priminfos[p].superend) ? 0 : p;
1171: return (Cell)old_code_here;
1172: #endif /* !defined(NO_DYNAMIC) */
1173: }
1.128 anton 1174: #endif /* !defined(NO_IP) */
1.70 anton 1175:
1.109 anton 1176: #ifndef NO_DYNAMIC
1177: int cost_codesize(int prim)
1178: {
1.121 anton 1179: return priminfos[prim].length;
1.109 anton 1180: }
1181: #endif
1182:
1183: int cost_ls(int prim)
1184: {
1185: struct cost *c = super_costs+prim;
1186:
1187: return c->loads + c->stores;
1188: }
1189:
1190: int cost_lsu(int prim)
1191: {
1192: struct cost *c = super_costs+prim;
1193:
1194: return c->loads + c->stores + c->updates;
1195: }
1196:
1197: int cost_nexts(int prim)
1198: {
1199: return 1;
1200: }
1201:
1202: typedef int Costfunc(int);
1203: Costfunc *ss_cost = /* cost function for optimize_bb */
1204: #ifdef NO_DYNAMIC
1205: cost_lsu;
1206: #else
1207: cost_codesize;
1208: #endif
1209:
1.110 anton 1210: struct {
1211: Costfunc *costfunc;
1212: char *metricname;
1213: long sum;
1214: } cost_sums[] = {
1215: #ifndef NO_DYNAMIC
1216: { cost_codesize, "codesize", 0 },
1217: #endif
1218: { cost_ls, "ls", 0 },
1219: { cost_lsu, "lsu", 0 },
1220: { cost_nexts, "nexts", 0 }
1221: };
1222:
1.106 anton 1223: #define MAX_BB 128 /* maximum number of instructions in BB */
1.125 anton 1224: #define INF_COST 1000000 /* infinite cost */
1225: #define CANONICAL_STATE 0
1226:
1227: struct waypoint {
1228: int cost; /* the cost from here to the end */
1229: PrimNum inst; /* the inst used from here to the next waypoint */
1230: char relocatable; /* the last non-transition was relocatable */
1231: char no_transition; /* don't use the next transition (relocatability)
1232: * or this transition (does not change state) */
1233: };
1234:
1235: void init_waypoints(struct waypoint ws[])
1236: {
1237: int k;
1238:
1239: for (k=0; k<maxstates; k++)
1240: ws[k].cost=INF_COST;
1241: }
1.106 anton 1242:
1.125 anton 1243: void transitions(struct waypoint inst[], struct waypoint trans[])
1.107 anton 1244: {
1.125 anton 1245: int k;
1246: struct super_state *l;
1247:
1248: for (k=0; k<maxstates; k++) {
1249: trans[k] = inst[k];
1250: trans[k].no_transition = 1;
1251: }
1252: for (l = state_transitions; l != NULL; l = l->next) {
1253: PrimNum s = l->super;
1254: int jcost;
1255: struct cost *c=super_costs+s;
1256: struct waypoint *wi=&(trans[c->state_in]);
1257: struct waypoint *wo=&(inst[c->state_out]);
1258: if (wo->cost == INF_COST)
1259: continue;
1260: jcost = wo->cost + ss_cost(s);
1261: if (jcost <= wi->cost) {
1262: wi->cost = jcost;
1263: wi->inst = s;
1264: wi->relocatable = wo->relocatable;
1265: wi->no_transition = 0;
1266: /* if (ss_greedy) wi->cost = wo->cost ? */
1267: }
1268: }
1269: }
1.107 anton 1270:
1.125 anton 1271: /* use dynamic programming to find the shortest paths within the basic
1272: block origs[0..ninsts-1] and rewrite the instructions pointed to by
1273: instps to use it */
1274: void optimize_rewrite(Cell *instps[], PrimNum origs[], int ninsts)
1275: {
1276: int i,j;
1277: static struct waypoint inst[MAX_BB+1][MAX_STATE]; /* before instruction*/
1278: static struct waypoint trans[MAX_BB+1][MAX_STATE]; /* before transition */
1279: int nextdyn, nextstate, no_transition;
1280:
1281: init_waypoints(inst[ninsts]);
1282: inst[ninsts][CANONICAL_STATE].cost=0;
1283: transitions(inst[ninsts],trans[ninsts]);
1.107 anton 1284: for (i=ninsts-1; i>=0; i--) {
1.125 anton 1285: init_waypoints(inst[i]);
1286: for (j=1; j<=max_super && i+j<=ninsts; j++) {
1287: struct super_state **superp = lookup_super(origs+i, j);
1288: if (superp!=NULL) {
1289: struct super_state *supers = *superp;
1290: for (; supers!=NULL; supers = supers->next) {
1291: PrimNum s = supers->super;
1292: int jcost;
1293: struct cost *c=super_costs+s;
1294: struct waypoint *wi=&(inst[i][c->state_in]);
1295: struct waypoint *wo=&(trans[i+j][c->state_out]);
1296: int no_transition = wo->no_transition;
1297: if (!(is_relocatable(s)) && !wo->relocatable) {
1298: wo=&(inst[i+j][c->state_out]);
1299: no_transition=1;
1300: }
1301: if (wo->cost == INF_COST)
1302: continue;
1303: jcost = wo->cost + ss_cost(s);
1304: if (jcost <= wi->cost) {
1305: wi->cost = jcost;
1306: wi->inst = s;
1307: wi->relocatable = is_relocatable(s);
1308: wi->no_transition = no_transition;
1309: /* if (ss_greedy) wi->cost = wo->cost ? */
1310: }
1.107 anton 1311: }
1312: }
1313: }
1.125 anton 1314: transitions(inst[i],trans[i]);
1315: }
1316: /* now rewrite the instructions */
1317: nextdyn=0;
1318: nextstate=CANONICAL_STATE;
1319: no_transition = ((!trans[0][nextstate].relocatable)
1320: ||trans[0][nextstate].no_transition);
1321: for (i=0; i<ninsts; i++) {
1322: Cell tc=0, tc2;
1323: if (i==nextdyn) {
1324: if (!no_transition) {
1325: /* process trans */
1326: PrimNum p = trans[i][nextstate].inst;
1327: struct cost *c = super_costs+p;
1328: assert(trans[i][nextstate].cost != INF_COST);
1329: assert(c->state_in==nextstate);
1.128 anton 1330: tc = compile_prim_dyn(p,NULL);
1.125 anton 1331: nextstate = c->state_out;
1332: }
1333: {
1334: /* process inst */
1335: PrimNum p = inst[i][nextstate].inst;
1336: struct cost *c=super_costs+p;
1337: assert(c->state_in==nextstate);
1338: assert(inst[i][nextstate].cost != INF_COST);
1339: #if defined(GFORTH_DEBUGGING)
1340: assert(p == origs[i]);
1341: #endif
1.128 anton 1342: tc2 = compile_prim_dyn(p,instps[i]);
1.125 anton 1343: if (no_transition || !is_relocatable(p))
1344: /* !! actually what we care about is if and where
1345: * compile_prim_dyn() puts NEXTs */
1346: tc=tc2;
1347: no_transition = inst[i][nextstate].no_transition;
1348: nextstate = c->state_out;
1349: nextdyn += c->length;
1350: }
1351: } else {
1352: #if defined(GFORTH_DEBUGGING)
1353: assert(0);
1354: #endif
1355: tc=0;
1356: /* tc= (Cell)vm_prims[inst[i][CANONICAL_STATE].inst]; */
1357: }
1358: *(instps[i]) = tc;
1359: }
1360: if (!no_transition) {
1361: PrimNum p = trans[i][nextstate].inst;
1362: struct cost *c = super_costs+p;
1363: assert(c->state_in==nextstate);
1364: assert(trans[i][nextstate].cost != INF_COST);
1365: assert(i==nextdyn);
1.128 anton 1366: (void)compile_prim_dyn(p,NULL);
1.125 anton 1367: nextstate = c->state_out;
1.107 anton 1368: }
1.125 anton 1369: assert(nextstate==CANONICAL_STATE);
1.107 anton 1370: }
1371:
1.105 anton 1372: /* compile *start, possibly rewriting it into a static and/or dynamic
1373: superinstruction */
1374: void compile_prim1(Cell *start)
1.70 anton 1375: {
1.108 anton 1376: #if defined(DOUBLY_INDIRECT)
1.125 anton 1377: Label prim;
1378:
1379: if (start==NULL)
1380: return;
1381: prim = (Label)*start;
1.108 anton 1382: if (prim<((Label)(xts+DOESJUMP)) || prim>((Label)(xts+npriminfos))) {
1383: fprintf(stderr,"compile_prim encountered xt %p\n", prim);
1384: *start=(Cell)prim;
1385: return;
1386: } else {
1387: *start = (Cell)(prim-((Label)xts)+((Label)vm_prims));
1388: return;
1389: }
1390: #elif defined(INDIRECT_THREADED)
1391: return;
1.112 anton 1392: #else /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
1.128 anton 1393: /* !! does not work, for unknown reasons; but something like this is
1394: probably needed to ensure that we don't call compile_prim_dyn
1395: before the inline arguments are there */
1396: static Cell *instps[MAX_BB];
1397: static PrimNum origs[MAX_BB];
1398: static int ninsts=0;
1399: PrimNum prim_num;
1400:
1401: if (start==NULL || ninsts >= MAX_BB ||
1402: (ninsts>0 && superend[origs[ninsts-1]])) {
1403: /* after bb, or at the start of the next bb */
1404: optimize_rewrite(instps,origs,ninsts);
1405: /* fprintf(stderr,"optimize_rewrite(...,%d)\n",ninsts); */
1406: ninsts=0;
1407: if (start==NULL)
1408: return;
1409: }
1410: prim_num = ((Xt)*start)-vm_prims;
1411: if(prim_num >= npriminfos) {
1412: optimize_rewrite(instps,origs,ninsts);
1.129 anton 1413: /* fprintf(stderr,"optimize_rewrite(...,%d)\n",ninsts);*/
1.128 anton 1414: ninsts=0;
1415: return;
1416: }
1417: assert(ninsts<MAX_BB);
1418: instps[ninsts] = start;
1419: origs[ninsts] = prim_num;
1420: ninsts++;
1.112 anton 1421: #endif /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
1.47 anton 1422: }
1423:
1.1 anton 1424: Address loader(FILE *imagefile, char* filename)
1425: /* returns the address of the image proper (after the preamble) */
1426: {
1427: ImageHeader header;
1428: Address image;
1429: Address imp; /* image+preamble */
1.17 anton 1430: Char magic[8];
1431: char magic7; /* size byte of magic number */
1.1 anton 1432: Cell preamblesize=0;
1.6 pazsan 1433: Cell data_offset = offset_image ? 56*sizeof(Cell) : 0;
1.1 anton 1434: UCell check_sum;
1.15 pazsan 1435: Cell ausize = ((RELINFOBITS == 8) ? 0 :
1436: (RELINFOBITS == 16) ? 1 :
1437: (RELINFOBITS == 32) ? 2 : 3);
1438: Cell charsize = ((sizeof(Char) == 1) ? 0 :
1439: (sizeof(Char) == 2) ? 1 :
1440: (sizeof(Char) == 4) ? 2 : 3) + ausize;
1441: Cell cellsize = ((sizeof(Cell) == 1) ? 0 :
1442: (sizeof(Cell) == 2) ? 1 :
1443: (sizeof(Cell) == 4) ? 2 : 3) + ausize;
1.21 anton 1444: Cell sizebyte = (ausize << 5) + (charsize << 3) + (cellsize << 1) +
1445: #ifdef WORDS_BIGENDIAN
1446: 0
1447: #else
1448: 1
1449: #endif
1450: ;
1.1 anton 1451:
1.43 anton 1452: vm_prims = engine(0,0,0,0,0);
1.47 anton 1453: check_prims(vm_prims);
1.106 anton 1454: prepare_super_table();
1.1 anton 1455: #ifndef DOUBLY_INDIRECT
1.59 anton 1456: #ifdef PRINT_SUPER_LENGTHS
1457: print_super_lengths();
1458: #endif
1.43 anton 1459: check_sum = checksum(vm_prims);
1.1 anton 1460: #else /* defined(DOUBLY_INDIRECT) */
1.43 anton 1461: check_sum = (UCell)vm_prims;
1.1 anton 1462: #endif /* defined(DOUBLY_INDIRECT) */
1.10 pazsan 1463:
1464: do {
1465: if(fread(magic,sizeof(Char),8,imagefile) < 8) {
1.84 anton 1466: fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.6) image.\n",
1.10 pazsan 1467: progname, filename);
1468: exit(1);
1.1 anton 1469: }
1.10 pazsan 1470: preamblesize+=8;
1.84 anton 1471: } while(memcmp(magic,"Gforth3",7));
1.17 anton 1472: magic7 = magic[7];
1.1 anton 1473: if (debug) {
1.17 anton 1474: magic[7]='\0';
1.21 anton 1475: fprintf(stderr,"Magic found: %s ", magic);
1476: print_sizes(magic7);
1.1 anton 1477: }
1478:
1.21 anton 1479: if (magic7 != sizebyte)
1480: {
1481: fprintf(stderr,"This image is: ");
1482: print_sizes(magic7);
1483: fprintf(stderr,"whereas the machine is ");
1484: print_sizes(sizebyte);
1.1 anton 1485: exit(-2);
1486: };
1487:
1488: fread((void *)&header,sizeof(ImageHeader),1,imagefile);
1.10 pazsan 1489:
1490: set_stack_sizes(&header);
1.1 anton 1491:
1492: #if HAVE_GETPAGESIZE
1493: pagesize=getpagesize(); /* Linux/GNU libc offers this */
1494: #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
1495: pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
1496: #elif PAGESIZE
1497: pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
1498: #endif
1499: if (debug)
1.5 jwilke 1500: fprintf(stderr,"pagesize=%ld\n",(unsigned long) pagesize);
1.1 anton 1501:
1.34 anton 1502: image = dict_alloc_read(imagefile, preamblesize+header.image_size,
1503: preamblesize+dictsize, data_offset);
1.33 anton 1504: imp=image+preamblesize;
1.57 anton 1505: alloc_stacks((ImageHeader *)imp);
1.1 anton 1506: if (clear_dictionary)
1.33 anton 1507: memset(imp+header.image_size, 0, dictsize-header.image_size);
1.90 anton 1508: if(header.base==0 || header.base == (Address)0x100) {
1.1 anton 1509: Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
1510: char reloc_bits[reloc_size];
1.33 anton 1511: fseek(imagefile, preamblesize+header.image_size, SEEK_SET);
1.10 pazsan 1512: fread(reloc_bits, 1, reloc_size, imagefile);
1.90 anton 1513: relocate((Cell *)imp, reloc_bits, header.image_size, (Cell)header.base, vm_prims);
1.1 anton 1514: #if 0
1515: { /* let's see what the relocator did */
1516: FILE *snapshot=fopen("snapshot.fi","wb");
1517: fwrite(image,1,imagesize,snapshot);
1518: fclose(snapshot);
1519: }
1520: #endif
1.46 jwilke 1521: }
1522: else if(header.base!=imp) {
1523: fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
1524: progname, (unsigned long)header.base, (unsigned long)imp);
1525: exit(1);
1.1 anton 1526: }
1527: if (header.checksum==0)
1528: ((ImageHeader *)imp)->checksum=check_sum;
1529: else if (header.checksum != check_sum) {
1530: fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
1531: progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
1532: exit(1);
1533: }
1.53 anton 1534: #ifdef DOUBLY_INDIRECT
1535: ((ImageHeader *)imp)->xt_base = xts;
1536: #endif
1.1 anton 1537: fclose(imagefile);
1538:
1.56 anton 1539: /* unnecessary, except maybe for CODE words */
1540: /* FLUSH_ICACHE(imp, header.image_size);*/
1.1 anton 1541:
1542: return imp;
1543: }
1544:
1.72 anton 1545: /* pointer to last '/' or '\' in file, 0 if there is none. */
1546: char *onlypath(char *filename)
1.10 pazsan 1547: {
1.72 anton 1548: return strrchr(filename, DIRSEP);
1.1 anton 1549: }
1550:
1551: FILE *openimage(char *fullfilename)
1.10 pazsan 1552: {
1553: FILE *image_file;
1.28 anton 1554: char * expfilename = tilde_cstr(fullfilename, strlen(fullfilename), 1);
1.10 pazsan 1555:
1.28 anton 1556: image_file=fopen(expfilename,"rb");
1.1 anton 1557: if (image_file!=NULL && debug)
1.28 anton 1558: fprintf(stderr, "Opened image file: %s\n", expfilename);
1.10 pazsan 1559: return image_file;
1.1 anton 1560: }
1561:
1.28 anton 1562: /* try to open image file concat(path[0:len],imagename) */
1.1 anton 1563: FILE *checkimage(char *path, int len, char *imagename)
1.10 pazsan 1564: {
1565: int dirlen=len;
1.1 anton 1566: char fullfilename[dirlen+strlen(imagename)+2];
1.10 pazsan 1567:
1.1 anton 1568: memcpy(fullfilename, path, dirlen);
1.71 pazsan 1569: if (fullfilename[dirlen-1]!=DIRSEP)
1570: fullfilename[dirlen++]=DIRSEP;
1.1 anton 1571: strcpy(fullfilename+dirlen,imagename);
1.10 pazsan 1572: return openimage(fullfilename);
1.1 anton 1573: }
1574:
1.10 pazsan 1575: FILE * open_image_file(char * imagename, char * path)
1.1 anton 1576: {
1.10 pazsan 1577: FILE * image_file=NULL;
1.28 anton 1578: char *origpath=path;
1.10 pazsan 1579:
1.71 pazsan 1580: if(strchr(imagename, DIRSEP)==NULL) {
1.10 pazsan 1581: /* first check the directory where the exe file is in !! 01may97jaw */
1582: if (onlypath(progname))
1.72 anton 1583: image_file=checkimage(progname, onlypath(progname)-progname, imagename);
1.10 pazsan 1584: if (!image_file)
1585: do {
1586: char *pend=strchr(path, PATHSEP);
1587: if (pend==NULL)
1588: pend=path+strlen(path);
1589: if (strlen(path)==0) break;
1590: image_file=checkimage(path, pend-path, imagename);
1591: path=pend+(*pend==PATHSEP);
1592: } while (image_file==NULL);
1593: } else {
1594: image_file=openimage(imagename);
1595: }
1.1 anton 1596:
1.10 pazsan 1597: if (!image_file) {
1598: fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
1.28 anton 1599: progname, imagename, origpath);
1.10 pazsan 1600: exit(1);
1.7 anton 1601: }
1602:
1.10 pazsan 1603: return image_file;
1604: }
1.11 pazsan 1605: #endif
1606:
1607: #ifdef HAS_OS
1608: UCell convsize(char *s, UCell elemsize)
1609: /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
1610: of bytes. the letter at the end indicates the unit, where e stands
1611: for the element size. default is e */
1612: {
1613: char *endp;
1614: UCell n,m;
1615:
1616: m = elemsize;
1617: n = strtoul(s,&endp,0);
1618: if (endp!=NULL) {
1619: if (strcmp(endp,"b")==0)
1620: m=1;
1621: else if (strcmp(endp,"k")==0)
1622: m=1024;
1623: else if (strcmp(endp,"M")==0)
1624: m=1024*1024;
1625: else if (strcmp(endp,"G")==0)
1626: m=1024*1024*1024;
1627: else if (strcmp(endp,"T")==0) {
1628: #if (SIZEOF_CHAR_P > 4)
1.24 anton 1629: m=1024L*1024*1024*1024;
1.11 pazsan 1630: #else
1631: fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp);
1632: exit(1);
1633: #endif
1634: } else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
1635: fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
1636: exit(1);
1637: }
1638: }
1639: return n*m;
1640: }
1.10 pazsan 1641:
1.109 anton 1642: enum {
1643: ss_number = 256,
1.125 anton 1644: ss_states,
1.109 anton 1645: ss_min_codesize,
1646: ss_min_ls,
1647: ss_min_lsu,
1648: ss_min_nexts,
1649: };
1650:
1.10 pazsan 1651: void gforth_args(int argc, char ** argv, char ** path, char ** imagename)
1652: {
1653: int c;
1654:
1.1 anton 1655: opterr=0;
1656: while (1) {
1657: int option_index=0;
1658: static struct option opts[] = {
1.29 anton 1659: {"appl-image", required_argument, NULL, 'a'},
1.1 anton 1660: {"image-file", required_argument, NULL, 'i'},
1661: {"dictionary-size", required_argument, NULL, 'm'},
1662: {"data-stack-size", required_argument, NULL, 'd'},
1663: {"return-stack-size", required_argument, NULL, 'r'},
1664: {"fp-stack-size", required_argument, NULL, 'f'},
1665: {"locals-stack-size", required_argument, NULL, 'l'},
1666: {"path", required_argument, NULL, 'p'},
1667: {"version", no_argument, NULL, 'v'},
1668: {"help", no_argument, NULL, 'h'},
1669: /* put something != 0 into offset_image */
1670: {"offset-image", no_argument, &offset_image, 1},
1671: {"no-offset-im", no_argument, &offset_image, 0},
1672: {"clear-dictionary", no_argument, &clear_dictionary, 1},
1.4 anton 1673: {"die-on-signal", no_argument, &die_on_signal, 1},
1.1 anton 1674: {"debug", no_argument, &debug, 1},
1.60 anton 1675: {"no-super", no_argument, &no_super, 1},
1676: {"no-dynamic", no_argument, &no_dynamic, 1},
1.66 anton 1677: {"dynamic", no_argument, &no_dynamic, 0},
1.110 anton 1678: {"print-metrics", no_argument, &print_metrics, 1},
1.109 anton 1679: {"ss-number", required_argument, NULL, ss_number},
1.125 anton 1680: {"ss-states", required_argument, NULL, ss_states},
1.109 anton 1681: #ifndef NO_DYNAMIC
1682: {"ss-min-codesize", no_argument, NULL, ss_min_codesize},
1683: #endif
1684: {"ss-min-ls", no_argument, NULL, ss_min_ls},
1685: {"ss-min-lsu", no_argument, NULL, ss_min_lsu},
1686: {"ss-min-nexts", no_argument, NULL, ss_min_nexts},
1.110 anton 1687: {"ss-greedy", no_argument, &ss_greedy, 1},
1.1 anton 1688: {0,0,0,0}
1689: /* no-init-file, no-rc? */
1690: };
1691:
1.36 pazsan 1692: c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vhoncsx", opts, &option_index);
1.1 anton 1693:
1694: switch (c) {
1.29 anton 1695: case EOF: return;
1696: case '?': optind--; return;
1697: case 'a': *imagename = optarg; return;
1.10 pazsan 1698: case 'i': *imagename = optarg; break;
1.1 anton 1699: case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
1700: case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
1701: case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
1702: case 'f': fsize = convsize(optarg,sizeof(Float)); break;
1703: case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
1.10 pazsan 1704: case 'p': *path = optarg; break;
1.36 pazsan 1705: case 'o': offset_image = 1; break;
1706: case 'n': offset_image = 0; break;
1707: case 'c': clear_dictionary = 1; break;
1708: case 's': die_on_signal = 1; break;
1709: case 'x': debug = 1; break;
1.83 anton 1710: case 'v': fputs(PACKAGE_STRING"\n", stderr); exit(0);
1.109 anton 1711: case ss_number: static_super_number = atoi(optarg); break;
1.125 anton 1712: case ss_states: maxstates = max(min(atoi(optarg),MAX_STATE),1); break;
1.109 anton 1713: #ifndef NO_DYNAMIC
1714: case ss_min_codesize: ss_cost = cost_codesize; break;
1715: #endif
1716: case ss_min_ls: ss_cost = cost_ls; break;
1717: case ss_min_lsu: ss_cost = cost_lsu; break;
1718: case ss_min_nexts: ss_cost = cost_nexts; break;
1.1 anton 1719: case 'h':
1.29 anton 1720: fprintf(stderr, "Usage: %s [engine options] ['--'] [image arguments]\n\
1.1 anton 1721: Engine Options:\n\
1.29 anton 1722: --appl-image FILE equivalent to '--image-file=FILE --'\n\
1.10 pazsan 1723: --clear-dictionary Initialize the dictionary with 0 bytes\n\
1724: -d SIZE, --data-stack-size=SIZE Specify data stack size\n\
1725: --debug Print debugging information during startup\n\
1726: --die-on-signal exit instead of CATCHing some signals\n\
1.66 anton 1727: --dynamic use dynamic native code\n\
1.10 pazsan 1728: -f SIZE, --fp-stack-size=SIZE Specify floating point stack size\n\
1729: -h, --help Print this message and exit\n\
1730: -i FILE, --image-file=FILE Use image FILE instead of `gforth.fi'\n\
1731: -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
1732: -m SIZE, --dictionary-size=SIZE Specify Forth dictionary size\n\
1.60 anton 1733: --no-dynamic Use only statically compiled primitives\n\
1.10 pazsan 1734: --no-offset-im Load image at normal position\n\
1.60 anton 1735: --no-super No dynamically formed superinstructions\n\
1.10 pazsan 1736: --offset-image Load image at a different position\n\
1737: -p PATH, --path=PATH Search path for finding image and sources\n\
1.110 anton 1738: --print-metrics Print some code generation metrics on exit\n\
1.10 pazsan 1739: -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
1.111 anton 1740: --ss-greedy greedy, not optimal superinst selection\n\
1741: --ss-min-codesize select superinsts for smallest native code\n\
1742: --ss-min-ls minimize loads and stores\n\
1743: --ss-min-lsu minimize loads, stores, and pointer updates\n\
1744: --ss-min-nexts minimize the number of static superinsts\n\
1745: --ss-number=N use N static superinsts (default max)\n\
1.125 anton 1746: --ss-states=N N states for stack caching (default max)\n\
1.66 anton 1747: -v, --version Print engine version and exit\n\
1.1 anton 1748: SIZE arguments consist of an integer followed by a unit. The unit can be\n\
1.10 pazsan 1749: `b' (byte), `e' (element; default), `k' (KB), `M' (MB), `G' (GB) or `T' (TB).\n",
1750: argv[0]);
1751: optind--;
1752: return;
1.1 anton 1753: }
1754: }
1.10 pazsan 1755: }
1.11 pazsan 1756: #endif
1.10 pazsan 1757:
1758: #ifdef INCLUDE_IMAGE
1759: extern Cell image[];
1760: extern const char reloc_bits[];
1761: #endif
1.67 pazsan 1762:
1.10 pazsan 1763: int main(int argc, char **argv, char **env)
1764: {
1.30 pazsan 1765: #ifdef HAS_OS
1.10 pazsan 1766: char *path = getenv("GFORTHPATH") ? : DEFAULTPATH;
1.30 pazsan 1767: #else
1768: char *path = DEFAULTPATH;
1769: #endif
1.13 pazsan 1770: #ifndef INCLUDE_IMAGE
1.10 pazsan 1771: char *imagename="gforth.fi";
1772: FILE *image_file;
1773: Address image;
1774: #endif
1775: int retvalue;
1776:
1.56 anton 1777: #if defined(i386) && defined(ALIGNMENT_CHECK)
1.10 pazsan 1778: /* turn on alignment checks on the 486.
1779: * on the 386 this should have no effect. */
1780: __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
1781: /* this is unusable with Linux' libc.4.6.27, because this library is
1782: not alignment-clean; we would have to replace some library
1783: functions (e.g., memcpy) to make it work. Also GCC doesn't try to keep
1784: the stack FP-aligned. */
1785: #endif
1786:
1787: /* buffering of the user output device */
1.11 pazsan 1788: #ifdef _IONBF
1.10 pazsan 1789: if (isatty(fileno(stdout))) {
1790: fflush(stdout);
1791: setvbuf(stdout,NULL,_IONBF,0);
1.1 anton 1792: }
1.11 pazsan 1793: #endif
1.1 anton 1794:
1.10 pazsan 1795: progname = argv[0];
1796:
1.11 pazsan 1797: #ifdef HAS_OS
1.10 pazsan 1798: gforth_args(argc, argv, &path, &imagename);
1.109 anton 1799: #ifndef NO_DYNAMIC
1800: if (no_dynamic && ss_cost == cost_codesize) {
1.122 anton 1801: ss_cost = cost_nexts;
1802: cost_sums[0] = cost_sums[1]; /* don't use cost_codesize for print-metrics */
1.109 anton 1803: if (debug)
1.122 anton 1804: fprintf(stderr, "--no-dynamic conflicts with --ss-min-codesize, reverting to --ss-min-nexts\n");
1.109 anton 1805: }
1806: #endif /* !defined(NO_DYNAMIC) */
1807: #endif /* defined(HAS_OS) */
1.10 pazsan 1808:
1809: #ifdef INCLUDE_IMAGE
1810: set_stack_sizes((ImageHeader *)image);
1.22 pazsan 1811: if(((ImageHeader *)image)->base != image)
1812: relocate(image, reloc_bits, ((ImageHeader *)image)->image_size,
1813: (Label*)engine(0, 0, 0, 0, 0));
1.10 pazsan 1814: alloc_stacks((ImageHeader *)image);
1815: #else
1816: image_file = open_image_file(imagename, path);
1817: image = loader(image_file, imagename);
1818: #endif
1.24 anton 1819: gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */
1.1 anton 1820:
1821: {
1.10 pazsan 1822: char path2[strlen(path)+1];
1.1 anton 1823: char *p1, *p2;
1824: Cell environ[]= {
1825: (Cell)argc-(optind-1),
1826: (Cell)(argv+(optind-1)),
1.10 pazsan 1827: (Cell)strlen(path),
1.1 anton 1828: (Cell)path2};
1829: argv[optind-1] = progname;
1830: /*
1831: for (i=0; i<environ[0]; i++)
1832: printf("%s\n", ((char **)(environ[1]))[i]);
1833: */
1834: /* make path OS-independent by replacing path separators with NUL */
1.10 pazsan 1835: for (p1=path, p2=path2; *p1!='\0'; p1++, p2++)
1.1 anton 1836: if (*p1==PATHSEP)
1837: *p2 = '\0';
1838: else
1839: *p2 = *p1;
1840: *p2='\0';
1.10 pazsan 1841: retvalue = go_forth(image, 4, environ);
1.102 anton 1842: #ifdef SIGPIPE
1843: bsd_signal(SIGPIPE, SIG_IGN);
1844: #endif
1.42 anton 1845: #ifdef VM_PROFILING
1846: vm_print_profile(stderr);
1847: #endif
1.1 anton 1848: deprep_terminal();
1.104 anton 1849: }
1.110 anton 1850: if (print_metrics) {
1851: int i;
1852: fprintf(stderr, "code size = %8ld\n", dyncodesize());
1853: for (i=0; i<sizeof(cost_sums)/sizeof(cost_sums[0]); i++)
1854: fprintf(stderr, "metric %8s: %8ld\n",
1855: cost_sums[i].metricname, cost_sums[i].sum);
1.1 anton 1856: }
1.13 pazsan 1857: return retvalue;
1.1 anton 1858: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>