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