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