Annotation of gforth/engine/main.c, revision 1.147
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.146 anton 771: Label goto_start;
772: Cell goto_len;
773:
1.114 anton 774: int compare_labels(const void *pa, const void *pb)
1.113 anton 775: {
1.114 anton 776: Label a = *(Label *)pa;
777: Label b = *(Label *)pb;
778: return a-b;
779: }
1.113 anton 780:
1.114 anton 781: Label bsearch_next(Label key, Label *a, UCell n)
782: /* a is sorted; return the label >=key that is the closest in a;
783: return NULL if there is no label in a >=key */
784: {
785: int mid = (n-1)/2;
786: if (n<1)
787: return NULL;
788: if (n == 1) {
789: if (a[0] < key)
790: return NULL;
791: else
792: return a[0];
793: }
794: if (a[mid] < key)
795: return bsearch_next(key, a+mid+1, n-mid-1);
796: else
797: return bsearch_next(key, a, mid+1);
1.113 anton 798: }
799:
1.47 anton 800: void check_prims(Label symbols1[])
801: {
802: int i;
1.90 anton 803: #ifndef NO_DYNAMIC
1.146 anton 804: Label *symbols2, *symbols3, *ends1, *ends1j, *ends1jsorted, *goto_p;
1.119 anton 805: int nends1j;
1.90 anton 806: #endif
1.47 anton 807:
1.66 anton 808: if (debug)
809: #ifdef __VERSION__
810: fprintf(stderr, "Compiled with gcc-" __VERSION__ "\n");
811: #else
812: #define xstr(s) str(s)
813: #define str(s) #s
814: fprintf(stderr, "Compiled with gcc-" xstr(__GNUC__) "." xstr(__GNUC_MINOR__) "\n");
815: #endif
1.121 anton 816: for (i=0; symbols1[i]!=0; i++)
1.47 anton 817: ;
1.55 anton 818: npriminfos = i;
1.70 anton 819:
820: #ifndef NO_DYNAMIC
1.66 anton 821: if (no_dynamic)
822: return;
1.55 anton 823: symbols2=engine2(0,0,0,0,0);
1.70 anton 824: #if NO_IP
825: symbols3=engine3(0,0,0,0,0);
826: #else
827: symbols3=symbols1;
828: #endif
1.121 anton 829: ends1 = symbols1+i+1;
1.119 anton 830: ends1j = ends1+i;
1.146 anton 831: goto_p = ends1j+i+1; /* goto_p[0]==before; ...[1]==after;*/
1.121 anton 832: nends1j = i+1;
1.119 anton 833: ends1jsorted = (Label *)alloca(nends1j*sizeof(Label));
834: memcpy(ends1jsorted,ends1j,nends1j*sizeof(Label));
835: qsort(ends1jsorted, nends1j, sizeof(Label), compare_labels);
1.146 anton 836:
837: /* check whether the "goto *" is relocatable */
838: goto_len = goto_p[1]-goto_p[0];
839: debugp(stderr, "goto * %p %p len=%ld\n",
840: goto_p[0],symbols2[goto_p-symbols1],goto_len);
841: if (memcmp(goto_p[0],symbols2[goto_p-symbols1],goto_len)!=0) { /* unequal */
842: no_dynamic=1;
843: debugp(stderr," not relocatable, disabling dynamic code generation\n");
844: return;
845: }
846: goto_start = goto_p[0];
1.113 anton 847:
1.47 anton 848: priminfos = calloc(i,sizeof(PrimInfo));
1.121 anton 849: for (i=0; symbols1[i]!=0; i++) {
1.70 anton 850: int prim_len = ends1[i]-symbols1[i];
1.47 anton 851: PrimInfo *pi=&priminfos[i];
1.70 anton 852: int j=0;
853: char *s1 = (char *)symbols1[i];
854: char *s2 = (char *)symbols2[i];
855: char *s3 = (char *)symbols3[i];
1.119 anton 856: Label endlabel = bsearch_next(symbols1[i]+1,ends1jsorted,nends1j);
1.70 anton 857:
858: pi->start = s1;
1.121 anton 859: pi->superend = superend[i]|no_super;
1.147 ! anton 860: pi->length = prim_len;
1.113 anton 861: pi->restlength = endlabel - symbols1[i] - pi->length;
1.70 anton 862: pi->nimmargs = 0;
1.144 pazsan 863: relocs++;
864: debugp(stderr, "%-15s %3d %p %p %p len=%3ld restlen=%2ld s-end=%1d",
1.139 anton 865: prim_names[i], i, s1, s2, s3, (long)(pi->length), (long)(pi->restlength), pi->superend);
1.114 anton 866: if (endlabel == NULL) {
867: pi->start = NULL; /* not relocatable */
1.122 anton 868: if (pi->length<0) pi->length=100;
1.144 pazsan 869: debugp(stderr,"\n non_reloc: no J label > start found\n");
870: relocs--;
871: nonrelocs++;
1.114 anton 872: continue;
873: }
874: if (ends1[i] > endlabel && !pi->superend) {
1.113 anton 875: pi->start = NULL; /* not relocatable */
1.122 anton 876: pi->length = endlabel-symbols1[i];
1.144 pazsan 877: debugp(stderr,"\n non_reloc: there is a J label before the K label (restlength<0)\n");
878: relocs--;
879: nonrelocs++;
1.113 anton 880: continue;
881: }
1.114 anton 882: if (ends1[i] < pi->start && !pi->superend) {
1.113 anton 883: pi->start = NULL; /* not relocatable */
1.122 anton 884: pi->length = endlabel-symbols1[i];
1.144 pazsan 885: debugp(stderr,"\n non_reloc: K label before I label (length<0)\n");
886: relocs--;
887: nonrelocs++;
1.113 anton 888: continue;
889: }
1.138 anton 890: assert(pi->length>=0);
1.113 anton 891: assert(pi->restlength >=0);
1.74 anton 892: while (j<(pi->length+pi->restlength)) {
1.70 anton 893: if (s1[j]==s3[j]) {
894: if (s1[j] != s2[j]) {
895: pi->start = NULL; /* not relocatable */
1.144 pazsan 896: debugp(stderr,"\n non_reloc: engine1!=engine2 offset %3d",j);
1.74 anton 897: /* assert(j<prim_len); */
1.144 pazsan 898: relocs--;
899: nonrelocs++;
1.70 anton 900: break;
901: }
902: j++;
903: } else {
904: struct immarg *ia=&pi->immargs[pi->nimmargs];
905:
906: pi->nimmargs++;
907: ia->offset=j;
908: if ((~*(Cell *)&(s1[j]))==*(Cell *)&(s3[j])) {
909: ia->rel=0;
1.144 pazsan 910: debugp(stderr,"\n absolute immarg: offset %3d",j);
1.70 anton 911: } else if ((&(s1[j]))+(*(Cell *)&(s1[j]))+4 ==
912: symbols1[DOESJUMP+1]) {
913: ia->rel=1;
1.144 pazsan 914: debugp(stderr,"\n relative immarg: offset %3d",j);
1.70 anton 915: } else {
916: pi->start = NULL; /* not relocatable */
1.144 pazsan 917: debugp(stderr,"\n non_reloc: engine1!=engine3 offset %3d",j);
1.74 anton 918: /* assert(j<prim_len);*/
1.144 pazsan 919: relocs--;
920: nonrelocs++;
1.70 anton 921: break;
922: }
923: j+=4;
1.47 anton 924: }
925: }
1.144 pazsan 926: debugp(stderr,"\n");
1.70 anton 927: }
1.76 anton 928: decomp_prims = calloc(i,sizeof(PrimInfo *));
929: for (i=DOESJUMP+1; i<npriminfos; i++)
930: decomp_prims[i] = &(priminfos[i]);
931: qsort(decomp_prims+DOESJUMP+1, npriminfos-DOESJUMP-1, sizeof(PrimInfo *),
932: compare_priminfo_length);
1.70 anton 933: #endif
934: }
935:
1.74 anton 936: void flush_to_here(void)
937: {
1.93 anton 938: #ifndef NO_DYNAMIC
1.100 anton 939: if (start_flush)
940: FLUSH_ICACHE(start_flush, code_here-start_flush);
1.74 anton 941: start_flush=code_here;
1.93 anton 942: #endif
1.74 anton 943: }
944:
1.93 anton 945: #ifndef NO_DYNAMIC
1.74 anton 946: void append_jump(void)
947: {
948: if (last_jump) {
949: PrimInfo *pi = &priminfos[last_jump];
950:
951: memcpy(code_here, pi->start+pi->length, pi->restlength);
952: code_here += pi->restlength;
1.147 ! anton 953: memcpy(code_here, goto_start, goto_len);
! 954: code_here += goto_len;
1.74 anton 955: last_jump=0;
956: }
957: }
958:
1.75 anton 959: /* Gforth remembers all code blocks in this list. On forgetting (by
960: executing a marker) the code blocks are not freed (because Gforth does
961: not remember how they were allocated; hmm, remembering that might be
962: easier and cleaner). Instead, code_here etc. are reset to the old
963: value, and the "forgotten" code blocks are reused when they are
964: needed. */
965:
966: struct code_block_list {
967: struct code_block_list *next;
968: Address block;
969: Cell size;
970: } *code_block_list=NULL, **next_code_blockp=&code_block_list;
971:
1.74 anton 972: Address append_prim(Cell p)
973: {
974: PrimInfo *pi = &priminfos[p];
975: Address old_code_here = code_here;
976:
977: if (code_area+code_area_size < code_here+pi->length+pi->restlength) {
1.75 anton 978: struct code_block_list *p;
1.74 anton 979: append_jump();
1.93 anton 980: flush_to_here();
1.75 anton 981: if (*next_code_blockp == NULL) {
982: code_here = start_flush = code_area = my_alloc(code_area_size);
983: p = (struct code_block_list *)malloc(sizeof(struct code_block_list));
984: *next_code_blockp = p;
985: p->next = NULL;
986: p->block = code_here;
987: p->size = code_area_size;
988: } else {
989: p = *next_code_blockp;
990: code_here = start_flush = code_area = p->block;
991: }
1.74 anton 992: old_code_here = code_here;
1.75 anton 993: next_code_blockp = &(p->next);
1.74 anton 994: }
995: memcpy(code_here, pi->start, pi->length);
996: code_here += pi->length;
997: return old_code_here;
998: }
999: #endif
1.75 anton 1000:
1001: int forget_dyncode(Address code)
1002: {
1003: #ifdef NO_DYNAMIC
1004: return -1;
1005: #else
1006: struct code_block_list *p, **pp;
1007:
1008: for (pp=&code_block_list, p=*pp; p!=NULL; pp=&(p->next), p=*pp) {
1009: if (code >= p->block && code < p->block+p->size) {
1010: next_code_blockp = &(p->next);
1011: code_here = start_flush = code;
1012: code_area = p->block;
1013: last_jump = 0;
1014: return -1;
1015: }
1016: }
1.78 anton 1017: return -no_dynamic;
1.75 anton 1018: #endif /* !defined(NO_DYNAMIC) */
1019: }
1020:
1.104 anton 1021: long dyncodesize(void)
1022: {
1023: #ifndef NO_DYNAMIC
1.106 anton 1024: struct code_block_list *p;
1.104 anton 1025: long size=0;
1026: for (p=code_block_list; p!=NULL; p=p->next) {
1027: if (code_here >= p->block && code_here < p->block+p->size)
1028: return size + (code_here - p->block);
1029: else
1030: size += p->size;
1031: }
1032: #endif /* !defined(NO_DYNAMIC) */
1033: return 0;
1034: }
1035:
1.90 anton 1036: Label decompile_code(Label _code)
1.75 anton 1037: {
1.76 anton 1038: #ifdef NO_DYNAMIC
1.90 anton 1039: return _code;
1.76 anton 1040: #else /* !defined(NO_DYNAMIC) */
1041: Cell i;
1.77 anton 1042: struct code_block_list *p;
1.90 anton 1043: Address code=_code;
1.76 anton 1044:
1.77 anton 1045: /* first, check if we are in code at all */
1046: for (p = code_block_list;; p = p->next) {
1047: if (p == NULL)
1048: return code;
1049: if (code >= p->block && code < p->block+p->size)
1050: break;
1051: }
1.76 anton 1052: /* reverse order because NOOP might match other prims */
1053: for (i=npriminfos-1; i>DOESJUMP; i--) {
1054: PrimInfo *pi=decomp_prims[i];
1055: if (pi->start==code || (pi->start && memcmp(code,pi->start,pi->length)==0))
1.121 anton 1056: return vm_prims[super2[super_costs[pi-priminfos].offset]];
1.118 anton 1057: /* return pi->start;*/
1.76 anton 1058: }
1059: return code;
1060: #endif /* !defined(NO_DYNAMIC) */
1.75 anton 1061: }
1.74 anton 1062:
1.70 anton 1063: #ifdef NO_IP
1064: int nbranchinfos=0;
1065:
1066: struct branchinfo {
1.136 anton 1067: Label **targetpp; /* **(bi->targetpp) is the target */
1.70 anton 1068: Cell *addressptr; /* store the target here */
1069: } branchinfos[100000];
1070:
1071: int ndoesexecinfos=0;
1072: struct doesexecinfo {
1073: int branchinfo; /* fix the targetptr of branchinfos[...->branchinfo] */
1.136 anton 1074: Label *targetp; /*target for branch (because this is not in threaded code)*/
1.70 anton 1075: Cell *xt; /* cfa of word whose does-code needs calling */
1076: } doesexecinfos[10000];
1077:
1078: void set_rel_target(Cell *source, Label target)
1079: {
1080: *source = ((Cell)target)-(((Cell)source)+4);
1081: }
1082:
1.136 anton 1083: void register_branchinfo(Label source, Cell *targetpp)
1.70 anton 1084: {
1085: struct branchinfo *bi = &(branchinfos[nbranchinfos]);
1.136 anton 1086: bi->targetpp = (Label **)targetpp;
1.70 anton 1087: bi->addressptr = (Cell *)source;
1088: nbranchinfos++;
1089: }
1090:
1.133 anton 1091: Address compile_prim1arg(PrimNum p, Cell **argp)
1.70 anton 1092: {
1.133 anton 1093: Address old_code_here=append_prim(p);
1.70 anton 1094:
1.74 anton 1095: assert(vm_prims[p]==priminfos[p].start);
1.133 anton 1096: *argp = (Cell*)(old_code_here+priminfos[p].immargs[0].offset);
1097: return old_code_here;
1.70 anton 1098: }
1099:
1.136 anton 1100: Address compile_call2(Cell *targetpp, Cell **next_code_targetp)
1.70 anton 1101: {
1.73 anton 1102: PrimInfo *pi = &priminfos[N_call2];
1.74 anton 1103: Address old_code_here = append_prim(N_call2);
1.70 anton 1104:
1.134 anton 1105: *next_code_targetp = (Cell *)(old_code_here + pi->immargs[0].offset);
1.136 anton 1106: register_branchinfo(old_code_here + pi->immargs[1].offset, targetpp);
1.134 anton 1107: return old_code_here;
1.70 anton 1108: }
1109: #endif
1110:
1111: void finish_code(void)
1112: {
1113: #ifdef NO_IP
1114: Cell i;
1115:
1116: compile_prim1(NULL);
1117: for (i=0; i<ndoesexecinfos; i++) {
1118: struct doesexecinfo *dei = &doesexecinfos[i];
1.136 anton 1119: dei->targetp = (Label *)DOES_CODE1((dei->xt));
1120: branchinfos[dei->branchinfo].targetpp = &(dei->targetp);
1.70 anton 1121: }
1122: ndoesexecinfos = 0;
1123: for (i=0; i<nbranchinfos; i++) {
1124: struct branchinfo *bi=&branchinfos[i];
1.136 anton 1125: set_rel_target(bi->addressptr, **(bi->targetpp));
1.70 anton 1126: }
1127: nbranchinfos = 0;
1.128 anton 1128: #else
1129: compile_prim1(NULL);
1.48 anton 1130: #endif
1.93 anton 1131: flush_to_here();
1.48 anton 1132: }
1133:
1.128 anton 1134: #ifdef NO_IP
1135: Cell compile_prim_dyn(PrimNum p, Cell *tcp)
1136: /* compile prim #p dynamically (mod flags etc.) and return start
1137: address of generated code for putting it into the threaded
1138: code. This function is only called if all the associated
1139: inline arguments of p are already in place (at tcp[1] etc.) */
1140: {
1141: PrimInfo *pi=&priminfos[p];
1142: Cell *next_code_target=NULL;
1.135 anton 1143: Address codeaddr;
1144: Address primstart;
1.128 anton 1145:
1146: assert(p<npriminfos);
1147: if (p==N_execute || p==N_perform || p==N_lit_perform) {
1.134 anton 1148: codeaddr = compile_prim1arg(N_set_next_code, &next_code_target);
1.135 anton 1149: primstart = append_prim(p);
1150: goto other_prim;
1151: } else if (p==N_call) {
1.136 anton 1152: codeaddr = compile_call2(tcp+1, &next_code_target);
1.128 anton 1153: } else if (p==N_does_exec) {
1154: struct doesexecinfo *dei = &doesexecinfos[ndoesexecinfos++];
1.133 anton 1155: Cell *arg;
1156: codeaddr = compile_prim1arg(N_lit,&arg);
1157: *arg = (Cell)PFA(tcp[1]);
1.128 anton 1158: /* we cannot determine the callee now (last_start[1] may be a
1159: forward reference), so just register an arbitrary target, and
1160: register in dei that we need to fix this before resolving
1161: branches */
1162: dei->branchinfo = nbranchinfos;
1163: dei->xt = (Cell *)(tcp[1]);
1.134 anton 1164: compile_call2(0, &next_code_target);
1.128 anton 1165: } else if (!is_relocatable(p)) {
1.133 anton 1166: Cell *branch_target;
1167: codeaddr = compile_prim1arg(N_set_next_code, &next_code_target);
1168: compile_prim1arg(N_branch,&branch_target);
1169: set_rel_target(branch_target,vm_prims[p]);
1.128 anton 1170: } else {
1171: unsigned j;
1.135 anton 1172:
1173: codeaddr = primstart = append_prim(p);
1174: other_prim:
1.128 anton 1175: for (j=0; j<pi->nimmargs; j++) {
1176: struct immarg *ia = &(pi->immargs[j]);
1.136 anton 1177: Cell *argp = tcp + pi->nimmargs - j;
1178: Cell argval = *argp; /* !! specific to prims */
1.128 anton 1179: if (ia->rel) { /* !! assumption: relative refs are branches */
1.136 anton 1180: register_branchinfo(primstart + ia->offset, argp);
1.128 anton 1181: } else /* plain argument */
1.135 anton 1182: *(Cell *)(primstart + ia->offset) = argval;
1.128 anton 1183: }
1184: }
1185: if (next_code_target!=NULL)
1186: *next_code_target = (Cell)code_here;
1.135 anton 1187: return (Cell)codeaddr;
1.128 anton 1188: }
1189: #else /* !defined(NO_IP) */
1190: Cell compile_prim_dyn(PrimNum p, Cell *tcp)
1191: /* compile prim #p dynamically (mod flags etc.) and return start
1192: address of generated code for putting it into the threaded code */
1.108 anton 1193: {
1.121 anton 1194: Cell static_prim = (Cell)vm_prims[p];
1.108 anton 1195: #if defined(NO_DYNAMIC)
1196: return static_prim;
1197: #else /* !defined(NO_DYNAMIC) */
1198: Address old_code_here;
1199:
1200: if (no_dynamic)
1201: return static_prim;
1.125 anton 1202: if (p>=npriminfos || !is_relocatable(p)) {
1.108 anton 1203: append_jump();
1204: return static_prim;
1205: }
1206: old_code_here = append_prim(p);
1.147 ! anton 1207: last_jump = p;
! 1208: if (priminfos[p].superend)
! 1209: append_jump();
1.108 anton 1210: return (Cell)old_code_here;
1211: #endif /* !defined(NO_DYNAMIC) */
1212: }
1.128 anton 1213: #endif /* !defined(NO_IP) */
1.70 anton 1214:
1.109 anton 1215: #ifndef NO_DYNAMIC
1216: int cost_codesize(int prim)
1217: {
1.121 anton 1218: return priminfos[prim].length;
1.109 anton 1219: }
1220: #endif
1221:
1222: int cost_ls(int prim)
1223: {
1224: struct cost *c = super_costs+prim;
1225:
1226: return c->loads + c->stores;
1227: }
1228:
1229: int cost_lsu(int prim)
1230: {
1231: struct cost *c = super_costs+prim;
1232:
1233: return c->loads + c->stores + c->updates;
1234: }
1235:
1236: int cost_nexts(int prim)
1237: {
1238: return 1;
1239: }
1240:
1241: typedef int Costfunc(int);
1242: Costfunc *ss_cost = /* cost function for optimize_bb */
1243: #ifdef NO_DYNAMIC
1244: cost_lsu;
1245: #else
1246: cost_codesize;
1247: #endif
1248:
1.110 anton 1249: struct {
1250: Costfunc *costfunc;
1251: char *metricname;
1252: long sum;
1253: } cost_sums[] = {
1254: #ifndef NO_DYNAMIC
1255: { cost_codesize, "codesize", 0 },
1256: #endif
1257: { cost_ls, "ls", 0 },
1258: { cost_lsu, "lsu", 0 },
1259: { cost_nexts, "nexts", 0 }
1260: };
1261:
1.106 anton 1262: #define MAX_BB 128 /* maximum number of instructions in BB */
1.125 anton 1263: #define INF_COST 1000000 /* infinite cost */
1264: #define CANONICAL_STATE 0
1265:
1266: struct waypoint {
1267: int cost; /* the cost from here to the end */
1268: PrimNum inst; /* the inst used from here to the next waypoint */
1269: char relocatable; /* the last non-transition was relocatable */
1270: char no_transition; /* don't use the next transition (relocatability)
1271: * or this transition (does not change state) */
1272: };
1273:
1274: void init_waypoints(struct waypoint ws[])
1275: {
1276: int k;
1277:
1278: for (k=0; k<maxstates; k++)
1279: ws[k].cost=INF_COST;
1280: }
1.106 anton 1281:
1.125 anton 1282: void transitions(struct waypoint inst[], struct waypoint trans[])
1.107 anton 1283: {
1.125 anton 1284: int k;
1285: struct super_state *l;
1286:
1287: for (k=0; k<maxstates; k++) {
1288: trans[k] = inst[k];
1289: trans[k].no_transition = 1;
1290: }
1291: for (l = state_transitions; l != NULL; l = l->next) {
1292: PrimNum s = l->super;
1293: int jcost;
1294: struct cost *c=super_costs+s;
1295: struct waypoint *wi=&(trans[c->state_in]);
1296: struct waypoint *wo=&(inst[c->state_out]);
1297: if (wo->cost == INF_COST)
1298: continue;
1299: jcost = wo->cost + ss_cost(s);
1300: if (jcost <= wi->cost) {
1301: wi->cost = jcost;
1302: wi->inst = s;
1303: wi->relocatable = wo->relocatable;
1304: wi->no_transition = 0;
1305: /* if (ss_greedy) wi->cost = wo->cost ? */
1306: }
1307: }
1308: }
1.107 anton 1309:
1.125 anton 1310: /* use dynamic programming to find the shortest paths within the basic
1311: block origs[0..ninsts-1] and rewrite the instructions pointed to by
1312: instps to use it */
1313: void optimize_rewrite(Cell *instps[], PrimNum origs[], int ninsts)
1314: {
1315: int i,j;
1316: static struct waypoint inst[MAX_BB+1][MAX_STATE]; /* before instruction*/
1317: static struct waypoint trans[MAX_BB+1][MAX_STATE]; /* before transition */
1318: int nextdyn, nextstate, no_transition;
1319:
1320: init_waypoints(inst[ninsts]);
1321: inst[ninsts][CANONICAL_STATE].cost=0;
1322: transitions(inst[ninsts],trans[ninsts]);
1.107 anton 1323: for (i=ninsts-1; i>=0; i--) {
1.125 anton 1324: init_waypoints(inst[i]);
1325: for (j=1; j<=max_super && i+j<=ninsts; j++) {
1326: struct super_state **superp = lookup_super(origs+i, j);
1327: if (superp!=NULL) {
1328: struct super_state *supers = *superp;
1329: for (; supers!=NULL; supers = supers->next) {
1330: PrimNum s = supers->super;
1331: int jcost;
1332: struct cost *c=super_costs+s;
1333: struct waypoint *wi=&(inst[i][c->state_in]);
1334: struct waypoint *wo=&(trans[i+j][c->state_out]);
1335: int no_transition = wo->no_transition;
1336: if (!(is_relocatable(s)) && !wo->relocatable) {
1337: wo=&(inst[i+j][c->state_out]);
1338: no_transition=1;
1339: }
1340: if (wo->cost == INF_COST)
1341: continue;
1342: jcost = wo->cost + ss_cost(s);
1343: if (jcost <= wi->cost) {
1344: wi->cost = jcost;
1345: wi->inst = s;
1346: wi->relocatable = is_relocatable(s);
1347: wi->no_transition = no_transition;
1348: /* if (ss_greedy) wi->cost = wo->cost ? */
1349: }
1.107 anton 1350: }
1351: }
1352: }
1.125 anton 1353: transitions(inst[i],trans[i]);
1354: }
1355: /* now rewrite the instructions */
1356: nextdyn=0;
1357: nextstate=CANONICAL_STATE;
1358: no_transition = ((!trans[0][nextstate].relocatable)
1359: ||trans[0][nextstate].no_transition);
1360: for (i=0; i<ninsts; i++) {
1361: Cell tc=0, tc2;
1362: if (i==nextdyn) {
1363: if (!no_transition) {
1364: /* process trans */
1365: PrimNum p = trans[i][nextstate].inst;
1366: struct cost *c = super_costs+p;
1367: assert(trans[i][nextstate].cost != INF_COST);
1368: assert(c->state_in==nextstate);
1.128 anton 1369: tc = compile_prim_dyn(p,NULL);
1.125 anton 1370: nextstate = c->state_out;
1371: }
1372: {
1373: /* process inst */
1374: PrimNum p = inst[i][nextstate].inst;
1375: struct cost *c=super_costs+p;
1376: assert(c->state_in==nextstate);
1377: assert(inst[i][nextstate].cost != INF_COST);
1378: #if defined(GFORTH_DEBUGGING)
1379: assert(p == origs[i]);
1380: #endif
1.128 anton 1381: tc2 = compile_prim_dyn(p,instps[i]);
1.125 anton 1382: if (no_transition || !is_relocatable(p))
1383: /* !! actually what we care about is if and where
1384: * compile_prim_dyn() puts NEXTs */
1385: tc=tc2;
1386: no_transition = inst[i][nextstate].no_transition;
1387: nextstate = c->state_out;
1388: nextdyn += c->length;
1389: }
1390: } else {
1391: #if defined(GFORTH_DEBUGGING)
1392: assert(0);
1393: #endif
1394: tc=0;
1395: /* tc= (Cell)vm_prims[inst[i][CANONICAL_STATE].inst]; */
1396: }
1397: *(instps[i]) = tc;
1398: }
1399: if (!no_transition) {
1400: PrimNum p = trans[i][nextstate].inst;
1401: struct cost *c = super_costs+p;
1402: assert(c->state_in==nextstate);
1403: assert(trans[i][nextstate].cost != INF_COST);
1404: assert(i==nextdyn);
1.128 anton 1405: (void)compile_prim_dyn(p,NULL);
1.125 anton 1406: nextstate = c->state_out;
1.107 anton 1407: }
1.125 anton 1408: assert(nextstate==CANONICAL_STATE);
1.107 anton 1409: }
1410:
1.105 anton 1411: /* compile *start, possibly rewriting it into a static and/or dynamic
1412: superinstruction */
1413: void compile_prim1(Cell *start)
1.70 anton 1414: {
1.108 anton 1415: #if defined(DOUBLY_INDIRECT)
1.125 anton 1416: Label prim;
1417:
1418: if (start==NULL)
1419: return;
1420: prim = (Label)*start;
1.108 anton 1421: if (prim<((Label)(xts+DOESJUMP)) || prim>((Label)(xts+npriminfos))) {
1422: fprintf(stderr,"compile_prim encountered xt %p\n", prim);
1423: *start=(Cell)prim;
1424: return;
1425: } else {
1426: *start = (Cell)(prim-((Label)xts)+((Label)vm_prims));
1427: return;
1428: }
1429: #elif defined(INDIRECT_THREADED)
1430: return;
1.112 anton 1431: #else /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
1.128 anton 1432: /* !! does not work, for unknown reasons; but something like this is
1433: probably needed to ensure that we don't call compile_prim_dyn
1434: before the inline arguments are there */
1435: static Cell *instps[MAX_BB];
1436: static PrimNum origs[MAX_BB];
1437: static int ninsts=0;
1438: PrimNum prim_num;
1439:
1440: if (start==NULL || ninsts >= MAX_BB ||
1441: (ninsts>0 && superend[origs[ninsts-1]])) {
1442: /* after bb, or at the start of the next bb */
1443: optimize_rewrite(instps,origs,ninsts);
1444: /* fprintf(stderr,"optimize_rewrite(...,%d)\n",ninsts); */
1445: ninsts=0;
1446: if (start==NULL)
1447: return;
1448: }
1449: prim_num = ((Xt)*start)-vm_prims;
1450: if(prim_num >= npriminfos) {
1451: optimize_rewrite(instps,origs,ninsts);
1.129 anton 1452: /* fprintf(stderr,"optimize_rewrite(...,%d)\n",ninsts);*/
1.128 anton 1453: ninsts=0;
1454: return;
1455: }
1456: assert(ninsts<MAX_BB);
1457: instps[ninsts] = start;
1458: origs[ninsts] = prim_num;
1459: ninsts++;
1.112 anton 1460: #endif /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
1.47 anton 1461: }
1462:
1.1 anton 1463: Address loader(FILE *imagefile, char* filename)
1464: /* returns the address of the image proper (after the preamble) */
1465: {
1466: ImageHeader header;
1467: Address image;
1468: Address imp; /* image+preamble */
1.17 anton 1469: Char magic[8];
1470: char magic7; /* size byte of magic number */
1.1 anton 1471: Cell preamblesize=0;
1.6 pazsan 1472: Cell data_offset = offset_image ? 56*sizeof(Cell) : 0;
1.1 anton 1473: UCell check_sum;
1.15 pazsan 1474: Cell ausize = ((RELINFOBITS == 8) ? 0 :
1475: (RELINFOBITS == 16) ? 1 :
1476: (RELINFOBITS == 32) ? 2 : 3);
1477: Cell charsize = ((sizeof(Char) == 1) ? 0 :
1478: (sizeof(Char) == 2) ? 1 :
1479: (sizeof(Char) == 4) ? 2 : 3) + ausize;
1480: Cell cellsize = ((sizeof(Cell) == 1) ? 0 :
1481: (sizeof(Cell) == 2) ? 1 :
1482: (sizeof(Cell) == 4) ? 2 : 3) + ausize;
1.21 anton 1483: Cell sizebyte = (ausize << 5) + (charsize << 3) + (cellsize << 1) +
1484: #ifdef WORDS_BIGENDIAN
1485: 0
1486: #else
1487: 1
1488: #endif
1489: ;
1.1 anton 1490:
1.43 anton 1491: vm_prims = engine(0,0,0,0,0);
1.47 anton 1492: check_prims(vm_prims);
1.106 anton 1493: prepare_super_table();
1.1 anton 1494: #ifndef DOUBLY_INDIRECT
1.59 anton 1495: #ifdef PRINT_SUPER_LENGTHS
1496: print_super_lengths();
1497: #endif
1.43 anton 1498: check_sum = checksum(vm_prims);
1.1 anton 1499: #else /* defined(DOUBLY_INDIRECT) */
1.43 anton 1500: check_sum = (UCell)vm_prims;
1.1 anton 1501: #endif /* defined(DOUBLY_INDIRECT) */
1.10 pazsan 1502:
1503: do {
1504: if(fread(magic,sizeof(Char),8,imagefile) < 8) {
1.84 anton 1505: fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.6) image.\n",
1.10 pazsan 1506: progname, filename);
1507: exit(1);
1.1 anton 1508: }
1.10 pazsan 1509: preamblesize+=8;
1.84 anton 1510: } while(memcmp(magic,"Gforth3",7));
1.17 anton 1511: magic7 = magic[7];
1.1 anton 1512: if (debug) {
1.17 anton 1513: magic[7]='\0';
1.21 anton 1514: fprintf(stderr,"Magic found: %s ", magic);
1515: print_sizes(magic7);
1.1 anton 1516: }
1517:
1.21 anton 1518: if (magic7 != sizebyte)
1519: {
1520: fprintf(stderr,"This image is: ");
1521: print_sizes(magic7);
1522: fprintf(stderr,"whereas the machine is ");
1523: print_sizes(sizebyte);
1.1 anton 1524: exit(-2);
1525: };
1526:
1527: fread((void *)&header,sizeof(ImageHeader),1,imagefile);
1.10 pazsan 1528:
1529: set_stack_sizes(&header);
1.1 anton 1530:
1531: #if HAVE_GETPAGESIZE
1532: pagesize=getpagesize(); /* Linux/GNU libc offers this */
1533: #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
1534: pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
1535: #elif PAGESIZE
1536: pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
1537: #endif
1.144 pazsan 1538: debugp(stderr,"pagesize=%ld\n",(unsigned long) pagesize);
1.1 anton 1539:
1.34 anton 1540: image = dict_alloc_read(imagefile, preamblesize+header.image_size,
1541: preamblesize+dictsize, data_offset);
1.33 anton 1542: imp=image+preamblesize;
1.57 anton 1543: alloc_stacks((ImageHeader *)imp);
1.1 anton 1544: if (clear_dictionary)
1.33 anton 1545: memset(imp+header.image_size, 0, dictsize-header.image_size);
1.90 anton 1546: if(header.base==0 || header.base == (Address)0x100) {
1.1 anton 1547: Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
1548: char reloc_bits[reloc_size];
1.33 anton 1549: fseek(imagefile, preamblesize+header.image_size, SEEK_SET);
1.10 pazsan 1550: fread(reloc_bits, 1, reloc_size, imagefile);
1.90 anton 1551: relocate((Cell *)imp, reloc_bits, header.image_size, (Cell)header.base, vm_prims);
1.1 anton 1552: #if 0
1553: { /* let's see what the relocator did */
1554: FILE *snapshot=fopen("snapshot.fi","wb");
1555: fwrite(image,1,imagesize,snapshot);
1556: fclose(snapshot);
1557: }
1558: #endif
1.46 jwilke 1559: }
1560: else if(header.base!=imp) {
1561: fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
1562: progname, (unsigned long)header.base, (unsigned long)imp);
1563: exit(1);
1.1 anton 1564: }
1565: if (header.checksum==0)
1566: ((ImageHeader *)imp)->checksum=check_sum;
1567: else if (header.checksum != check_sum) {
1568: fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
1569: progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
1570: exit(1);
1571: }
1.53 anton 1572: #ifdef DOUBLY_INDIRECT
1573: ((ImageHeader *)imp)->xt_base = xts;
1574: #endif
1.1 anton 1575: fclose(imagefile);
1576:
1.56 anton 1577: /* unnecessary, except maybe for CODE words */
1578: /* FLUSH_ICACHE(imp, header.image_size);*/
1.1 anton 1579:
1580: return imp;
1581: }
1582:
1.72 anton 1583: /* pointer to last '/' or '\' in file, 0 if there is none. */
1584: char *onlypath(char *filename)
1.10 pazsan 1585: {
1.72 anton 1586: return strrchr(filename, DIRSEP);
1.1 anton 1587: }
1588:
1589: FILE *openimage(char *fullfilename)
1.10 pazsan 1590: {
1591: FILE *image_file;
1.28 anton 1592: char * expfilename = tilde_cstr(fullfilename, strlen(fullfilename), 1);
1.10 pazsan 1593:
1.28 anton 1594: image_file=fopen(expfilename,"rb");
1.1 anton 1595: if (image_file!=NULL && debug)
1.28 anton 1596: fprintf(stderr, "Opened image file: %s\n", expfilename);
1.10 pazsan 1597: return image_file;
1.1 anton 1598: }
1599:
1.28 anton 1600: /* try to open image file concat(path[0:len],imagename) */
1.1 anton 1601: FILE *checkimage(char *path, int len, char *imagename)
1.10 pazsan 1602: {
1603: int dirlen=len;
1.1 anton 1604: char fullfilename[dirlen+strlen(imagename)+2];
1.10 pazsan 1605:
1.1 anton 1606: memcpy(fullfilename, path, dirlen);
1.71 pazsan 1607: if (fullfilename[dirlen-1]!=DIRSEP)
1608: fullfilename[dirlen++]=DIRSEP;
1.1 anton 1609: strcpy(fullfilename+dirlen,imagename);
1.10 pazsan 1610: return openimage(fullfilename);
1.1 anton 1611: }
1612:
1.10 pazsan 1613: FILE * open_image_file(char * imagename, char * path)
1.1 anton 1614: {
1.10 pazsan 1615: FILE * image_file=NULL;
1.28 anton 1616: char *origpath=path;
1.10 pazsan 1617:
1.71 pazsan 1618: if(strchr(imagename, DIRSEP)==NULL) {
1.10 pazsan 1619: /* first check the directory where the exe file is in !! 01may97jaw */
1620: if (onlypath(progname))
1.72 anton 1621: image_file=checkimage(progname, onlypath(progname)-progname, imagename);
1.10 pazsan 1622: if (!image_file)
1623: do {
1624: char *pend=strchr(path, PATHSEP);
1625: if (pend==NULL)
1626: pend=path+strlen(path);
1627: if (strlen(path)==0) break;
1628: image_file=checkimage(path, pend-path, imagename);
1629: path=pend+(*pend==PATHSEP);
1630: } while (image_file==NULL);
1631: } else {
1632: image_file=openimage(imagename);
1633: }
1.1 anton 1634:
1.10 pazsan 1635: if (!image_file) {
1636: fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
1.28 anton 1637: progname, imagename, origpath);
1.10 pazsan 1638: exit(1);
1.7 anton 1639: }
1640:
1.10 pazsan 1641: return image_file;
1642: }
1.11 pazsan 1643: #endif
1644:
1645: #ifdef HAS_OS
1646: UCell convsize(char *s, UCell elemsize)
1647: /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
1648: of bytes. the letter at the end indicates the unit, where e stands
1649: for the element size. default is e */
1650: {
1651: char *endp;
1652: UCell n,m;
1653:
1654: m = elemsize;
1655: n = strtoul(s,&endp,0);
1656: if (endp!=NULL) {
1657: if (strcmp(endp,"b")==0)
1658: m=1;
1659: else if (strcmp(endp,"k")==0)
1660: m=1024;
1661: else if (strcmp(endp,"M")==0)
1662: m=1024*1024;
1663: else if (strcmp(endp,"G")==0)
1664: m=1024*1024*1024;
1665: else if (strcmp(endp,"T")==0) {
1666: #if (SIZEOF_CHAR_P > 4)
1.24 anton 1667: m=1024L*1024*1024*1024;
1.11 pazsan 1668: #else
1669: fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp);
1670: exit(1);
1671: #endif
1672: } else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
1673: fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
1674: exit(1);
1675: }
1676: }
1677: return n*m;
1678: }
1.10 pazsan 1679:
1.109 anton 1680: enum {
1681: ss_number = 256,
1.125 anton 1682: ss_states,
1.109 anton 1683: ss_min_codesize,
1684: ss_min_ls,
1685: ss_min_lsu,
1686: ss_min_nexts,
1687: };
1688:
1.10 pazsan 1689: void gforth_args(int argc, char ** argv, char ** path, char ** imagename)
1690: {
1691: int c;
1692:
1.1 anton 1693: opterr=0;
1694: while (1) {
1695: int option_index=0;
1696: static struct option opts[] = {
1.29 anton 1697: {"appl-image", required_argument, NULL, 'a'},
1.1 anton 1698: {"image-file", required_argument, NULL, 'i'},
1699: {"dictionary-size", required_argument, NULL, 'm'},
1700: {"data-stack-size", required_argument, NULL, 'd'},
1701: {"return-stack-size", required_argument, NULL, 'r'},
1702: {"fp-stack-size", required_argument, NULL, 'f'},
1703: {"locals-stack-size", required_argument, NULL, 'l'},
1704: {"path", required_argument, NULL, 'p'},
1705: {"version", no_argument, NULL, 'v'},
1706: {"help", no_argument, NULL, 'h'},
1707: /* put something != 0 into offset_image */
1708: {"offset-image", no_argument, &offset_image, 1},
1709: {"no-offset-im", no_argument, &offset_image, 0},
1710: {"clear-dictionary", no_argument, &clear_dictionary, 1},
1.4 anton 1711: {"die-on-signal", no_argument, &die_on_signal, 1},
1.1 anton 1712: {"debug", no_argument, &debug, 1},
1.144 pazsan 1713: {"diag", no_argument, &diag, 1},
1.60 anton 1714: {"no-super", no_argument, &no_super, 1},
1715: {"no-dynamic", no_argument, &no_dynamic, 1},
1.66 anton 1716: {"dynamic", no_argument, &no_dynamic, 0},
1.110 anton 1717: {"print-metrics", no_argument, &print_metrics, 1},
1.109 anton 1718: {"ss-number", required_argument, NULL, ss_number},
1.125 anton 1719: {"ss-states", required_argument, NULL, ss_states},
1.109 anton 1720: #ifndef NO_DYNAMIC
1721: {"ss-min-codesize", no_argument, NULL, ss_min_codesize},
1722: #endif
1723: {"ss-min-ls", no_argument, NULL, ss_min_ls},
1724: {"ss-min-lsu", no_argument, NULL, ss_min_lsu},
1725: {"ss-min-nexts", no_argument, NULL, ss_min_nexts},
1.110 anton 1726: {"ss-greedy", no_argument, &ss_greedy, 1},
1.1 anton 1727: {0,0,0,0}
1728: /* no-init-file, no-rc? */
1729: };
1730:
1.36 pazsan 1731: c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vhoncsx", opts, &option_index);
1.1 anton 1732:
1733: switch (c) {
1.29 anton 1734: case EOF: return;
1735: case '?': optind--; return;
1736: case 'a': *imagename = optarg; return;
1.10 pazsan 1737: case 'i': *imagename = optarg; break;
1.1 anton 1738: case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
1739: case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
1740: case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
1741: case 'f': fsize = convsize(optarg,sizeof(Float)); break;
1742: case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
1.10 pazsan 1743: case 'p': *path = optarg; break;
1.36 pazsan 1744: case 'o': offset_image = 1; break;
1745: case 'n': offset_image = 0; break;
1746: case 'c': clear_dictionary = 1; break;
1747: case 's': die_on_signal = 1; break;
1748: case 'x': debug = 1; break;
1.83 anton 1749: case 'v': fputs(PACKAGE_STRING"\n", stderr); exit(0);
1.109 anton 1750: case ss_number: static_super_number = atoi(optarg); break;
1.125 anton 1751: case ss_states: maxstates = max(min(atoi(optarg),MAX_STATE),1); break;
1.109 anton 1752: #ifndef NO_DYNAMIC
1753: case ss_min_codesize: ss_cost = cost_codesize; break;
1754: #endif
1755: case ss_min_ls: ss_cost = cost_ls; break;
1756: case ss_min_lsu: ss_cost = cost_lsu; break;
1757: case ss_min_nexts: ss_cost = cost_nexts; break;
1.1 anton 1758: case 'h':
1.29 anton 1759: fprintf(stderr, "Usage: %s [engine options] ['--'] [image arguments]\n\
1.1 anton 1760: Engine Options:\n\
1.29 anton 1761: --appl-image FILE equivalent to '--image-file=FILE --'\n\
1.10 pazsan 1762: --clear-dictionary Initialize the dictionary with 0 bytes\n\
1763: -d SIZE, --data-stack-size=SIZE Specify data stack size\n\
1764: --debug Print debugging information during startup\n\
1.144 pazsan 1765: --diag Print diagnostic information during startup\n\
1.10 pazsan 1766: --die-on-signal exit instead of CATCHing some signals\n\
1.66 anton 1767: --dynamic use dynamic native code\n\
1.10 pazsan 1768: -f SIZE, --fp-stack-size=SIZE Specify floating point stack size\n\
1769: -h, --help Print this message and exit\n\
1770: -i FILE, --image-file=FILE Use image FILE instead of `gforth.fi'\n\
1771: -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
1772: -m SIZE, --dictionary-size=SIZE Specify Forth dictionary size\n\
1.60 anton 1773: --no-dynamic Use only statically compiled primitives\n\
1.10 pazsan 1774: --no-offset-im Load image at normal position\n\
1.60 anton 1775: --no-super No dynamically formed superinstructions\n\
1.10 pazsan 1776: --offset-image Load image at a different position\n\
1777: -p PATH, --path=PATH Search path for finding image and sources\n\
1.110 anton 1778: --print-metrics Print some code generation metrics on exit\n\
1.10 pazsan 1779: -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
1.111 anton 1780: --ss-greedy greedy, not optimal superinst selection\n\
1781: --ss-min-codesize select superinsts for smallest native code\n\
1782: --ss-min-ls minimize loads and stores\n\
1783: --ss-min-lsu minimize loads, stores, and pointer updates\n\
1784: --ss-min-nexts minimize the number of static superinsts\n\
1785: --ss-number=N use N static superinsts (default max)\n\
1.125 anton 1786: --ss-states=N N states for stack caching (default max)\n\
1.66 anton 1787: -v, --version Print engine version and exit\n\
1.1 anton 1788: SIZE arguments consist of an integer followed by a unit. The unit can be\n\
1.10 pazsan 1789: `b' (byte), `e' (element; default), `k' (KB), `M' (MB), `G' (GB) or `T' (TB).\n",
1790: argv[0]);
1791: optind--;
1792: return;
1.1 anton 1793: }
1794: }
1.10 pazsan 1795: }
1.11 pazsan 1796: #endif
1.10 pazsan 1797:
1.144 pazsan 1798: void print_diag()
1799: {
1800:
1801: #if !defined(HAVE_GETRUSAGE) || !defined(HAS_FFCALL)
1.145 pazsan 1802: fprintf(stderr, "*** missing functionality ***\n"
1.144 pazsan 1803: #ifndef HAVE_GETRUSAGE
1804: " no getrusage -> CPUTIME broken\n"
1805: #endif
1806: #ifndef HAS_FFCALL
1807: " no ffcall -> only old-style foreign function calls (no fflib.fs)\n"
1808: #endif
1809: );
1810: #endif
1811: if((relocs < nonrelocs) ||
1812: #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)
1813: 1
1814: #else
1815: 0
1816: #endif
1817: )
1818: debugp(stderr, "relocs: %d:%d\n", relocs, nonrelocs);
1.145 pazsan 1819: fprintf(stderr, "*** performance problems ***\n%s"
1.144 pazsan 1820: #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)
1821: " double-cell integer type buggy ->\n "
1822: #ifdef BUGGY_LL_CMP
1823: "CMP, "
1824: #endif
1825: #ifdef BUGGY_LL_MUL
1826: "MUL, "
1827: #endif
1828: #ifdef BUGGY_LL_DIV
1829: "DIV, "
1830: #endif
1831: #ifdef BUGGY_LL_ADD
1832: "ADD, "
1833: #endif
1834: #ifdef BUGGY_LL_SHIFT
1835: "SHIFT, "
1836: #endif
1837: #ifdef BUGGY_LL_D2F
1838: "D2F, "
1839: #endif
1840: #ifdef BUGGY_LL_F2D
1841: "F2D, "
1842: #endif
1843: "\b\b slow\n"
1.145 pazsan 1844: #endif
1845: #ifndef FORCE_REG
1846: " automatic register allocation: performance degradation possible\n"
1847: #endif
1848: #if !defined(FORCE_REG) || defined(BUGGY_LONG_LONG)
1849: "*** Suggested remedy: try ./configure"
1850: #ifndef FORCE_REG
1851: " --enable-force-reg"
1852: #endif
1853: #ifdef BUGGY_LONG_LONG
1854: " --enable-force-ll"
1855: #endif
1856: "\n"
1.144 pazsan 1857: #endif
1858: ,
1859: (relocs < nonrelocs) ? " gcc PR 15242 -> no dynamic code generation (use gcc-2.95 instead)\n" : "");
1860: }
1861:
1.10 pazsan 1862: #ifdef INCLUDE_IMAGE
1863: extern Cell image[];
1864: extern const char reloc_bits[];
1865: #endif
1.67 pazsan 1866:
1.10 pazsan 1867: int main(int argc, char **argv, char **env)
1868: {
1.30 pazsan 1869: #ifdef HAS_OS
1.10 pazsan 1870: char *path = getenv("GFORTHPATH") ? : DEFAULTPATH;
1.30 pazsan 1871: #else
1872: char *path = DEFAULTPATH;
1873: #endif
1.13 pazsan 1874: #ifndef INCLUDE_IMAGE
1.10 pazsan 1875: char *imagename="gforth.fi";
1876: FILE *image_file;
1877: Address image;
1878: #endif
1879: int retvalue;
1880:
1.56 anton 1881: #if defined(i386) && defined(ALIGNMENT_CHECK)
1.10 pazsan 1882: /* turn on alignment checks on the 486.
1883: * on the 386 this should have no effect. */
1884: __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
1885: /* this is unusable with Linux' libc.4.6.27, because this library is
1886: not alignment-clean; we would have to replace some library
1887: functions (e.g., memcpy) to make it work. Also GCC doesn't try to keep
1888: the stack FP-aligned. */
1889: #endif
1890:
1891: /* buffering of the user output device */
1.11 pazsan 1892: #ifdef _IONBF
1.10 pazsan 1893: if (isatty(fileno(stdout))) {
1894: fflush(stdout);
1895: setvbuf(stdout,NULL,_IONBF,0);
1.1 anton 1896: }
1.11 pazsan 1897: #endif
1.1 anton 1898:
1.10 pazsan 1899: progname = argv[0];
1900:
1.11 pazsan 1901: #ifdef HAS_OS
1.10 pazsan 1902: gforth_args(argc, argv, &path, &imagename);
1.109 anton 1903: #ifndef NO_DYNAMIC
1904: if (no_dynamic && ss_cost == cost_codesize) {
1.122 anton 1905: ss_cost = cost_nexts;
1906: cost_sums[0] = cost_sums[1]; /* don't use cost_codesize for print-metrics */
1.144 pazsan 1907: debugp(stderr, "--no-dynamic conflicts with --ss-min-codesize, reverting to --ss-min-nexts\n");
1.109 anton 1908: }
1909: #endif /* !defined(NO_DYNAMIC) */
1910: #endif /* defined(HAS_OS) */
1.10 pazsan 1911:
1912: #ifdef INCLUDE_IMAGE
1913: set_stack_sizes((ImageHeader *)image);
1.22 pazsan 1914: if(((ImageHeader *)image)->base != image)
1915: relocate(image, reloc_bits, ((ImageHeader *)image)->image_size,
1916: (Label*)engine(0, 0, 0, 0, 0));
1.10 pazsan 1917: alloc_stacks((ImageHeader *)image);
1918: #else
1919: image_file = open_image_file(imagename, path);
1920: image = loader(image_file, imagename);
1921: #endif
1.24 anton 1922: gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */
1.1 anton 1923:
1.144 pazsan 1924: if (diag)
1925: print_diag();
1.1 anton 1926: {
1.10 pazsan 1927: char path2[strlen(path)+1];
1.1 anton 1928: char *p1, *p2;
1929: Cell environ[]= {
1930: (Cell)argc-(optind-1),
1931: (Cell)(argv+(optind-1)),
1.10 pazsan 1932: (Cell)strlen(path),
1.1 anton 1933: (Cell)path2};
1934: argv[optind-1] = progname;
1935: /*
1936: for (i=0; i<environ[0]; i++)
1937: printf("%s\n", ((char **)(environ[1]))[i]);
1938: */
1939: /* make path OS-independent by replacing path separators with NUL */
1.10 pazsan 1940: for (p1=path, p2=path2; *p1!='\0'; p1++, p2++)
1.1 anton 1941: if (*p1==PATHSEP)
1942: *p2 = '\0';
1943: else
1944: *p2 = *p1;
1945: *p2='\0';
1.10 pazsan 1946: retvalue = go_forth(image, 4, environ);
1.102 anton 1947: #ifdef SIGPIPE
1948: bsd_signal(SIGPIPE, SIG_IGN);
1949: #endif
1.42 anton 1950: #ifdef VM_PROFILING
1951: vm_print_profile(stderr);
1952: #endif
1.1 anton 1953: deprep_terminal();
1.104 anton 1954: }
1.110 anton 1955: if (print_metrics) {
1956: int i;
1957: fprintf(stderr, "code size = %8ld\n", dyncodesize());
1958: for (i=0; i<sizeof(cost_sums)/sizeof(cost_sums[0]); i++)
1959: fprintf(stderr, "metric %8s: %8ld\n",
1960: cost_sums[i].metricname, cost_sums[i].sum);
1.1 anton 1961: }
1.13 pazsan 1962: return retvalue;
1.1 anton 1963: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>