Annotation of gforth/engine/main.c, revision 1.207
1.1 anton 1: /* command line interpretation, image loading etc. for Gforth
2:
3:
1.192 anton 4: Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007 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
1.193 anton 10: as published by the Free Software Foundation, either version 3
1.1 anton 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
1.193 anton 19: along with this program; if not, see http://www.gnu.org/licenses/.
1.1 anton 20: */
21:
22: #include "config.h"
1.82 anton 23: #include "forth.h"
1.1 anton 24: #include <errno.h>
25: #include <ctype.h>
26: #include <stdio.h>
1.2 pazsan 27: #include <unistd.h>
1.1 anton 28: #include <string.h>
29: #include <math.h>
30: #include <sys/types.h>
1.32 pazsan 31: #ifndef STANDALONE
1.1 anton 32: #include <sys/stat.h>
1.32 pazsan 33: #endif
1.1 anton 34: #include <fcntl.h>
35: #include <assert.h>
36: #include <stdlib.h>
1.194 anton 37: #include <stdbool.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
1.174 pazsan 47: /* #include <systypes.h> */
1.11 pazsan 48: #endif
1.1 anton 49:
1.190 anton 50: /* output rules etc. for burg with --debug and --print-sequences */
51: /* #define BURG_FORMAT*/
52:
1.121 anton 53: typedef enum prim_num {
1.119 anton 54: /* definitions of N_execute etc. */
1.126 anton 55: #include PRIM_NUM_I
1.119 anton 56: N_START_SUPER
1.121 anton 57: } PrimNum;
1.119 anton 58:
1.79 anton 59: /* global variables for engine.c
60: We put them here because engine.c is compiled several times in
61: different ways for the same engine. */
1.161 pazsan 62: Cell *gforth_SP;
63: Float *gforth_FP;
64: Address gforth_UP=NULL;
1.207 ! pazsan 65: Cell *gforth_RP;
! 66: Address gforth_LP;
1.79 anton 67:
1.115 pazsan 68: #ifdef HAS_FFCALL
69:
70: #include <callback.h>
71:
1.161 pazsan 72: va_alist gforth_clist;
1.115 pazsan 73:
1.161 pazsan 74: void gforth_callback(Xt* fcall, void * alist)
1.115 pazsan 75: {
1.140 pazsan 76: /* save global valiables */
1.161 pazsan 77: Cell *rp = gforth_RP;
78: Cell *sp = gforth_SP;
79: Float *fp = gforth_FP;
80: Address lp = gforth_LP;
1.168 pazsan 81: va_alist clist = gforth_clist;
1.140 pazsan 82:
1.161 pazsan 83: gforth_clist = (va_alist)alist;
1.140 pazsan 84:
1.197 anton 85: gforth_engine(fcall, sp, rp, fp, lp sr_call);
1.140 pazsan 86:
87: /* restore global variables */
1.161 pazsan 88: gforth_RP = rp;
89: gforth_SP = sp;
90: gforth_FP = fp;
91: gforth_LP = lp;
1.168 pazsan 92: gforth_clist = clist;
1.115 pazsan 93: }
94: #endif
95:
1.79 anton 96: #ifdef GFORTH_DEBUGGING
97: /* define some VM registers as global variables, so they survive exceptions;
98: global register variables are not up to the task (according to the
99: GNU C manual) */
1.197 anton 100: #if defined(GLOBALS_NONRELOC)
101: saved_regs saved_regs_v;
102: saved_regs *saved_regs_p = &saved_regs_v;
103: #else /* !defined(GLOBALS_NONRELOC) */
1.79 anton 104: Xt *saved_ip;
105: Cell *rp;
1.197 anton 106: #endif /* !defined(GLOBALS_NONRELOC) */
107: #endif /* !defined(GFORTH_DEBUGGING) */
1.79 anton 108:
109: #ifdef NO_IP
110: Label next_code;
111: #endif
112:
113: #ifdef HAS_FILE
114: char* fileattr[6]={"rb","rb","r+b","r+b","wb","wb"};
115: char* pfileattr[6]={"r","r","r+","r+","w","w"};
116:
117: #ifndef O_BINARY
118: #define O_BINARY 0
119: #endif
120: #ifndef O_TEXT
121: #define O_TEXT 0
122: #endif
123:
124: int ufileattr[6]= {
125: O_RDONLY|O_BINARY, O_RDONLY|O_BINARY,
126: O_RDWR |O_BINARY, O_RDWR |O_BINARY,
127: O_WRONLY|O_BINARY, O_WRONLY|O_BINARY };
128: #endif
129: /* end global vars for engine.c */
130:
1.1 anton 131: #define PRIM_VERSION 1
132: /* increment this whenever the primitives change in an incompatible way */
133:
1.14 pazsan 134: #ifndef DEFAULTPATH
1.39 anton 135: # define DEFAULTPATH "."
1.14 pazsan 136: #endif
137:
1.1 anton 138: #ifdef MSDOS
139: jmp_buf throw_jmp_buf;
140: #endif
141:
1.56 anton 142: #if defined(DOUBLY_INDIRECT)
143: # define CFA(n) ({Cell _n = (n); ((Cell)(((_n & 0x4000) ? symbols : xts)+(_n&~0x4000UL)));})
1.1 anton 144: #else
1.56 anton 145: # define CFA(n) ((Cell)(symbols+((n)&~0x4000UL)))
1.1 anton 146: #endif
147:
148: #define maxaligned(n) (typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
149:
150: static UCell dictsize=0;
151: static UCell dsize=0;
152: static UCell rsize=0;
153: static UCell fsize=0;
154: static UCell lsize=0;
155: int offset_image=0;
1.4 anton 156: int die_on_signal=0;
1.169 anton 157: int ignore_async_signals=0;
1.13 pazsan 158: #ifndef INCLUDE_IMAGE
1.1 anton 159: static int clear_dictionary=0;
1.24 anton 160: UCell pagesize=1;
1.22 pazsan 161: char *progname;
162: #else
163: char *progname = "gforth";
164: int optind = 1;
1.13 pazsan 165: #endif
1.181 anton 166: #ifndef MAP_NORESERVE
167: #define MAP_NORESERVE 0
168: #endif
1.183 pazsan 169: /* IF you have an old Cygwin, this may help:
1.182 pazsan 170: #ifdef __CYGWIN__
171: #define MAP_NORESERVE 0
172: #endif
1.183 pazsan 173: */
1.181 anton 174: static int map_noreserve=MAP_NORESERVE;
1.31 pazsan 175:
1.167 anton 176: #define CODE_BLOCK_SIZE (512*1024) /* !! overflow handling for -native */
1.48 anton 177: Address code_area=0;
1.73 anton 178: Cell code_area_size = CODE_BLOCK_SIZE;
1.75 anton 179: Address code_here=NULL+CODE_BLOCK_SIZE; /* does for code-area what HERE
180: does for the dictionary */
1.100 anton 181: Address start_flush=NULL; /* start of unflushed code */
1.74 anton 182: Cell last_jump=0; /* if the last prim was compiled without jump, this
183: is it's number, otherwise this contains 0 */
1.48 anton 184:
1.60 anton 185: static int no_super=0; /* true if compile_prim should not fuse prims */
1.81 anton 186: static int no_dynamic=NO_DYNAMIC_DEFAULT; /* if true, no code is generated
187: dynamically */
1.110 anton 188: static int print_metrics=0; /* if true, print metrics on exit */
1.194 anton 189: static int static_super_number = 10000; /* number of ss used if available */
1.152 anton 190: #define MAX_STATE 9 /* maximum number of states */
1.125 anton 191: static int maxstates = MAX_STATE; /* number of states for stack caching */
1.110 anton 192: static int ss_greedy = 0; /* if true: use greedy, not optimal ss selection */
1.144 pazsan 193: static int diag = 0; /* if true: print diagnostic informations */
1.158 anton 194: static int tpa_noequiv = 0; /* if true: no state equivalence checking */
195: static int tpa_noautomaton = 0; /* if true: no tree parsing automaton */
196: static int tpa_trace = 0; /* if true: data for line graph of new states etc. */
1.189 anton 197: static int print_sequences = 0; /* print primitive sequences for optimization */
1.144 pazsan 198: static int relocs = 0;
199: static int nonrelocs = 0;
1.60 anton 200:
1.30 pazsan 201: #ifdef HAS_DEBUG
1.68 anton 202: int debug=0;
1.190 anton 203: # define debugp(x...) do { if (debug) fprintf(x); } while (0)
1.31 pazsan 204: #else
205: # define perror(x...)
206: # define fprintf(x...)
1.144 pazsan 207: # define debugp(x...)
1.30 pazsan 208: #endif
1.31 pazsan 209:
1.24 anton 210: ImageHeader *gforth_header;
1.43 anton 211: Label *vm_prims;
1.53 anton 212: #ifdef DOUBLY_INDIRECT
213: Label *xts; /* same content as vm_prims, but should only be used for xts */
214: #endif
1.1 anton 215:
1.125 anton 216: #ifndef NO_DYNAMIC
1.186 anton 217: #ifndef CODE_ALIGNMENT
1.185 anton 218: #define CODE_ALIGNMENT 0
219: #endif
220:
1.125 anton 221: #define MAX_IMMARGS 2
222:
223: typedef struct {
224: Label start; /* NULL if not relocatable */
225: Cell length; /* only includes the jump iff superend is true*/
226: Cell restlength; /* length of the rest (i.e., the jump or (on superend) 0) */
227: char superend; /* true if primitive ends superinstruction, i.e.,
228: unconditional branch, execute, etc. */
229: Cell nimmargs;
230: struct immarg {
231: Cell offset; /* offset of immarg within prim */
232: char rel; /* true if immarg is relative */
233: } immargs[MAX_IMMARGS];
234: } PrimInfo;
235:
236: PrimInfo *priminfos;
237: PrimInfo **decomp_prims;
238:
1.139 anton 239: const char const* const prim_names[]={
240: #include PRIM_NAMES_I
241: };
242:
1.148 anton 243: void init_ss_cost(void);
244:
1.125 anton 245: static int is_relocatable(int p)
246: {
247: return !no_dynamic && priminfos[p].start != NULL;
248: }
249: #else /* defined(NO_DYNAMIC) */
250: static int is_relocatable(int p)
251: {
252: return 0;
253: }
254: #endif /* defined(NO_DYNAMIC) */
255:
1.30 pazsan 256: #ifdef MEMCMP_AS_SUBROUTINE
257: int gforth_memcmp(const char * s1, const char * s2, size_t n)
258: {
259: return memcmp(s1, s2, n);
260: }
261: #endif
262:
1.125 anton 263: static Cell max(Cell a, Cell b)
264: {
265: return a>b?a:b;
266: }
267:
268: static Cell min(Cell a, Cell b)
269: {
270: return a<b?a:b;
271: }
272:
1.175 pazsan 273: #ifndef STANDALONE
1.1 anton 274: /* image file format:
1.15 pazsan 275: * "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.4.0 -i\n")
1.1 anton 276: * padding to a multiple of 8
1.84 anton 277: * magic: "Gforth3x" means format 0.6,
1.15 pazsan 278: * where x is a byte with
279: * bit 7: reserved = 0
280: * bit 6:5: address unit size 2^n octets
281: * bit 4:3: character size 2^n octets
282: * bit 2:1: cell size 2^n octets
283: * bit 0: endian, big=0, little=1.
284: * The magic are always 8 octets, no matter what the native AU/character size is
1.1 anton 285: * padding to max alignment (no padding necessary on current machines)
1.24 anton 286: * ImageHeader structure (see forth.h)
1.1 anton 287: * data (size in ImageHeader.image_size)
288: * tags ((if relocatable, 1 bit/data cell)
289: *
290: * tag==1 means that the corresponding word is an address;
291: * If the word is >=0, the address is within the image;
292: * addresses within the image are given relative to the start of the image.
293: * If the word =-1 (CF_NIL), the address is NIL,
294: * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
295: * If the word =CF(DODOES), it's a DOES> CFA
296: * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
297: * possibly containing a jump to dodoes)
1.51 anton 298: * If the word is <CF(DOESJUMP) and bit 14 is set, it's the xt of a primitive
299: * If the word is <CF(DOESJUMP) and bit 14 is clear,
300: * it's the threaded code of a primitive
1.85 pazsan 301: * bits 13..9 of a primitive token state which group the primitive belongs to,
302: * bits 8..0 of a primitive token index into the group
1.1 anton 303: */
304:
1.115 pazsan 305: Cell groups[32] = {
1.85 pazsan 306: 0,
1.121 anton 307: 0
1.90 anton 308: #undef GROUP
1.115 pazsan 309: #undef GROUPADD
310: #define GROUPADD(n) +n
311: #define GROUP(x, n) , 0
1.126 anton 312: #include PRIM_GRP_I
1.90 anton 313: #undef GROUP
1.115 pazsan 314: #undef GROUPADD
1.85 pazsan 315: #define GROUP(x, n)
1.115 pazsan 316: #define GROUPADD(n)
1.85 pazsan 317: };
318:
1.161 pazsan 319: static unsigned char *branch_targets(Cell *image, const unsigned char *bitstring,
1.125 anton 320: int size, Cell base)
321: /* produce a bitmask marking all the branch targets */
322: {
1.130 anton 323: int i=0, j, k, steps=(((size-1)/sizeof(Cell))/RELINFOBITS)+1;
1.125 anton 324: Cell token;
325: unsigned char bits;
1.130 anton 326: unsigned char *result=malloc(steps);
327:
328: memset(result, 0, steps);
329: for(k=0; k<steps; k++) {
1.125 anton 330: for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
1.130 anton 331: if(bits & (1U << (RELINFOBITS-1))) {
332: assert(i*sizeof(Cell) < size);
1.125 anton 333: token=image[i];
334: if (token>=base) { /* relocatable address */
335: UCell bitnum=(token-base)/sizeof(Cell);
1.154 anton 336: if (bitnum/RELINFOBITS < (UCell)steps)
337: result[bitnum/RELINFOBITS] |= 1U << ((~bitnum)&(RELINFOBITS-1));
1.125 anton 338: }
339: }
340: }
341: }
342: return result;
343: }
344:
1.162 pazsan 345: void gforth_relocate(Cell *image, const Char *bitstring,
346: UCell size, Cell base, Label symbols[])
1.1 anton 347: {
1.130 anton 348: int i=0, j, k, steps=(((size-1)/sizeof(Cell))/RELINFOBITS)+1;
1.11 pazsan 349: Cell token;
1.1 anton 350: char bits;
1.37 anton 351: Cell max_symbols;
1.46 jwilke 352: /*
1.85 pazsan 353: * A virtual start address that's the real start address minus
1.46 jwilke 354: * the one in the image
355: */
1.45 jwilke 356: Cell *start = (Cell * ) (((void *) image) - ((void *) base));
1.125 anton 357: unsigned char *targets = branch_targets(image, bitstring, size, base);
1.1 anton 358:
1.85 pazsan 359: /* group index into table */
1.115 pazsan 360: if(groups[31]==0) {
361: int groupsum=0;
362: for(i=0; i<32; i++) {
363: groupsum += groups[i];
364: groups[i] = groupsum;
365: /* printf("group[%d]=%d\n",i,groupsum); */
366: }
367: i=0;
368: }
1.46 jwilke 369:
370: /* printf("relocating to %x[%x] start=%x base=%x\n", image, size, start, base); */
1.37 anton 371:
1.121 anton 372: for (max_symbols=0; symbols[max_symbols]!=0; max_symbols++)
1.37 anton 373: ;
1.47 anton 374: max_symbols--;
1.35 pazsan 375:
1.130 anton 376: for(k=0; k<steps; k++) {
1.13 pazsan 377: for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
1.1 anton 378: /* fprintf(stderr,"relocate: image[%d]\n", i);*/
1.130 anton 379: if(bits & (1U << (RELINFOBITS-1))) {
380: assert(i*sizeof(Cell) < size);
1.35 pazsan 381: /* fprintf(stderr,"relocate: image[%d]=%d of %d\n", i, image[i], size/sizeof(Cell)); */
1.45 jwilke 382: token=image[i];
1.85 pazsan 383: if(token<0) {
384: int group = (-token & 0x3E00) >> 9;
385: if(group == 0) {
386: switch(token|0x4000) {
1.1 anton 387: case CF_NIL : image[i]=0; break;
388: #if !defined(DOUBLY_INDIRECT)
389: case CF(DOCOL) :
390: case CF(DOVAR) :
391: case CF(DOCON) :
1.188 pazsan 392: case CF(DOVAL) :
1.1 anton 393: case CF(DOUSER) :
394: case CF(DODEFER) :
1.11 pazsan 395: case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(token)]); break;
1.92 anton 396: case CF(DOESJUMP): image[i]=0; break;
1.1 anton 397: #endif /* !defined(DOUBLY_INDIRECT) */
398: case CF(DODOES) :
1.45 jwilke 399: MAKE_DOES_CF(image+i,(Xt *)(image[i+1]+((Cell)start)));
1.1 anton 400: break;
1.85 pazsan 401: default : /* backward compatibility */
1.56 anton 402: /* printf("Code field generation image[%x]:=CFA(%x)\n",
1.1 anton 403: i, CF(image[i])); */
1.55 anton 404: if (CF((token | 0x4000))<max_symbols) {
1.56 anton 405: image[i]=(Cell)CFA(CF(token));
406: #ifdef DIRECT_THREADED
1.125 anton 407: if ((token & 0x4000) == 0) { /* threade code, no CFA */
408: if (targets[k] & (1U<<(RELINFOBITS-1-j)))
409: compile_prim1(0);
1.70 anton 410: compile_prim1(&image[i]);
1.125 anton 411: }
1.56 anton 412: #endif
1.55 anton 413: } else
1.115 pazsan 414: 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 415: }
1.85 pazsan 416: } else {
417: int tok = -token & 0x1FF;
418: if (tok < (groups[group+1]-groups[group])) {
419: #if defined(DOUBLY_INDIRECT)
420: image[i]=(Cell)CFA(((groups[group]+tok) | (CF(token) & 0x4000)));
421: #else
422: image[i]=(Cell)CFA((groups[group]+tok));
423: #endif
424: #ifdef DIRECT_THREADED
1.125 anton 425: if ((token & 0x4000) == 0) { /* threade code, no CFA */
426: if (targets[k] & (1U<<(RELINFOBITS-1-j)))
427: compile_prim1(0);
1.85 pazsan 428: compile_prim1(&image[i]);
1.125 anton 429: }
1.85 pazsan 430: #endif
431: } else
1.115 pazsan 432: 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 433: }
434: } else {
1.101 anton 435: /* if base is > 0: 0 is a null reference so don't adjust*/
1.45 jwilke 436: if (token>=base) {
437: image[i]+=(Cell)start;
438: }
1.46 jwilke 439: }
1.1 anton 440: }
441: }
1.31 pazsan 442: }
1.125 anton 443: free(targets);
1.70 anton 444: finish_code();
1.26 jwilke 445: ((ImageHeader*)(image))->base = (Address) image;
1.1 anton 446: }
447:
1.162 pazsan 448: #ifndef DOUBLY_INDIRECT
1.161 pazsan 449: static UCell checksum(Label symbols[])
1.1 anton 450: {
451: UCell r=PRIM_VERSION;
452: Cell i;
453:
454: for (i=DOCOL; i<=DOESJUMP; i++) {
455: r ^= (UCell)(symbols[i]);
456: r = (r << 5) | (r >> (8*sizeof(Cell)-5));
457: }
458: #ifdef DIRECT_THREADED
459: /* we have to consider all the primitives */
460: for (; symbols[i]!=(Label)0; i++) {
461: r ^= (UCell)(symbols[i]);
462: r = (r << 5) | (r >> (8*sizeof(Cell)-5));
463: }
464: #else
465: /* in indirect threaded code all primitives are accessed through the
466: symbols table, so we just have to put the base address of symbols
467: in the checksum */
468: r ^= (UCell)symbols;
469: #endif
470: return r;
471: }
1.162 pazsan 472: #endif
1.1 anton 473:
1.161 pazsan 474: static Address verbose_malloc(Cell size)
1.3 anton 475: {
476: Address r;
477: /* leave a little room (64B) for stack underflows */
478: if ((r = malloc(size+64))==NULL) {
479: perror(progname);
480: exit(1);
481: }
482: r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
1.144 pazsan 483: debugp(stderr, "malloc succeeds, address=$%lx\n", (long)r);
1.3 anton 484: return r;
485: }
486:
1.33 anton 487: static Address next_address=0;
1.161 pazsan 488: static void after_alloc(Address r, Cell size)
1.33 anton 489: {
490: if (r != (Address)-1) {
1.144 pazsan 491: debugp(stderr, "success, address=$%lx\n", (long) r);
1.173 anton 492: #if 0
493: /* not needed now that we protect the stacks with mprotect */
1.33 anton 494: if (pagesize != 1)
495: next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */
1.173 anton 496: #endif
1.33 anton 497: } else {
1.144 pazsan 498: debugp(stderr, "failed: %s\n", strerror(errno));
1.33 anton 499: }
500: }
501:
1.34 anton 502: #ifndef MAP_FAILED
503: #define MAP_FAILED ((Address) -1)
504: #endif
505: #ifndef MAP_FILE
506: # define MAP_FILE 0
507: #endif
508: #ifndef MAP_PRIVATE
509: # define MAP_PRIVATE 0
510: #endif
1.91 anton 511: #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
512: # define MAP_ANON MAP_ANONYMOUS
513: #endif
1.34 anton 514:
515: #if defined(HAVE_MMAP)
516: static Address alloc_mmap(Cell size)
1.1 anton 517: {
518: Address r;
519:
520: #if defined(MAP_ANON)
1.144 pazsan 521: debugp(stderr,"try mmap($%lx, $%lx, ..., MAP_ANON, ...); ", (long)next_address, (long)size);
1.181 anton 522: r = mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE|map_noreserve, -1, 0);
1.1 anton 523: #else /* !defined(MAP_ANON) */
1.17 anton 524: /* Ultrix (at least) does not define MAP_FILE and MAP_PRIVATE (both are
525: apparently defaults) */
1.1 anton 526: static int dev_zero=-1;
527:
528: if (dev_zero == -1)
529: dev_zero = open("/dev/zero", O_RDONLY);
530: if (dev_zero == -1) {
1.34 anton 531: r = MAP_FAILED;
1.144 pazsan 532: debugp(stderr, "open(\"/dev/zero\"...) failed (%s), no mmap; ",
1.1 anton 533: strerror(errno));
534: } else {
1.144 pazsan 535: debugp(stderr,"try mmap($%lx, $%lx, ..., MAP_FILE, dev_zero, ...); ", (long)next_address, (long)size);
1.181 anton 536: r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE|map_noreserve, dev_zero, 0);
1.1 anton 537: }
538: #endif /* !defined(MAP_ANON) */
1.34 anton 539: after_alloc(r, size);
540: return r;
541: }
1.172 anton 542:
543: static void page_noaccess(Address a)
544: {
545: /* try mprotect first; with munmap the page might be allocated later */
546: debugp(stderr, "try mprotect(%p,%ld,PROT_NONE); ", a, (long)pagesize);
547: if (mprotect(a, pagesize, PROT_NONE)==0) {
548: debugp(stderr, "ok\n");
549: return;
550: }
551: debugp(stderr, "failed: %s\n", strerror(errno));
552: debugp(stderr, "try munmap(%p,%ld); ", a, (long)pagesize);
553: if (munmap(a,pagesize)==0) {
554: debugp(stderr, "ok\n");
555: return;
556: }
557: debugp(stderr, "failed: %s\n", strerror(errno));
558: }
559:
1.173 anton 560: static size_t wholepage(size_t n)
1.172 anton 561: {
562: return (n+pagesize-1)&~(pagesize-1);
563: }
1.34 anton 564: #endif
565:
1.161 pazsan 566: Address gforth_alloc(Cell size)
1.34 anton 567: {
568: #if HAVE_MMAP
569: Address r;
570:
571: r=alloc_mmap(size);
1.117 anton 572: if (r!=(Address)MAP_FAILED)
1.1 anton 573: return r;
574: #endif /* HAVE_MMAP */
1.3 anton 575: /* use malloc as fallback */
576: return verbose_malloc(size);
1.1 anton 577: }
578:
1.161 pazsan 579: static Address dict_alloc_read(FILE *file, Cell imagesize, Cell dictsize, Cell offset)
1.33 anton 580: {
1.34 anton 581: Address image = MAP_FAILED;
1.33 anton 582:
1.56 anton 583: #if defined(HAVE_MMAP)
1.33 anton 584: if (offset==0) {
1.34 anton 585: image=alloc_mmap(dictsize);
1.150 anton 586: if (image != (Address)MAP_FAILED) {
587: Address image1;
588: debugp(stderr,"try mmap($%lx, $%lx, ..., MAP_FIXED|MAP_FILE, imagefile, 0); ", (long)image, (long)imagesize);
1.181 anton 589: image1 = mmap(image, imagesize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FIXED|MAP_FILE|MAP_PRIVATE|map_noreserve, fileno(file), 0);
1.150 anton 590: after_alloc(image1,dictsize);
591: if (image1 == (Address)MAP_FAILED)
592: goto read_image;
593: }
1.33 anton 594: }
1.56 anton 595: #endif /* defined(HAVE_MMAP) */
1.117 anton 596: if (image == (Address)MAP_FAILED) {
1.161 pazsan 597: image = gforth_alloc(dictsize+offset)+offset;
1.149 anton 598: read_image:
1.33 anton 599: rewind(file); /* fseek(imagefile,0L,SEEK_SET); */
1.34 anton 600: fread(image, 1, imagesize, file);
1.33 anton 601: }
602: return image;
603: }
1.175 pazsan 604: #endif
1.33 anton 605:
1.10 pazsan 606: void set_stack_sizes(ImageHeader * header)
607: {
608: if (dictsize==0)
609: dictsize = header->dict_size;
610: if (dsize==0)
611: dsize = header->data_stack_size;
612: if (rsize==0)
613: rsize = header->return_stack_size;
614: if (fsize==0)
615: fsize = header->fp_stack_size;
616: if (lsize==0)
617: lsize = header->locals_stack_size;
618: dictsize=maxaligned(dictsize);
619: dsize=maxaligned(dsize);
620: rsize=maxaligned(rsize);
621: lsize=maxaligned(lsize);
622: fsize=maxaligned(fsize);
623: }
624:
1.178 pazsan 625: #ifdef STANDALONE
626: void alloc_stacks(ImageHeader * h)
627: {
628: #define SSTACKSIZE 0x200
629: static Cell dstack[SSTACKSIZE+1];
630: static Cell rstack[SSTACKSIZE+1];
631:
632: h->dict_size=dictsize;
633: h->data_stack_size=dsize;
634: h->fp_stack_size=fsize;
635: h->return_stack_size=rsize;
636: h->locals_stack_size=lsize;
637:
638: h->data_stack_base=dstack+SSTACKSIZE;
639: // h->fp_stack_base=gforth_alloc(fsize);
640: h->return_stack_base=rstack+SSTACKSIZE;
641: // h->locals_stack_base=gforth_alloc(lsize);
642: }
643: #else
1.173 anton 644: void alloc_stacks(ImageHeader * h)
1.10 pazsan 645: {
1.173 anton 646: h->dict_size=dictsize;
647: h->data_stack_size=dsize;
648: h->fp_stack_size=fsize;
649: h->return_stack_size=rsize;
650: h->locals_stack_size=lsize;
1.10 pazsan 651:
1.176 pazsan 652: #if defined(HAVE_MMAP) && !defined(STANDALONE)
1.172 anton 653: if (pagesize > 1) {
1.173 anton 654: size_t p = pagesize;
655: size_t totalsize =
656: wholepage(dsize)+wholepage(fsize)+wholepage(rsize)+wholepage(lsize)+5*p;
1.172 anton 657: Address a = alloc_mmap(totalsize);
658: if (a != (Address)MAP_FAILED) {
1.173 anton 659: page_noaccess(a); a+=p; h-> data_stack_base=a; a+=wholepage(dsize);
660: page_noaccess(a); a+=p; h-> fp_stack_base=a; a+=wholepage(fsize);
661: page_noaccess(a); a+=p; h->return_stack_base=a; a+=wholepage(rsize);
662: page_noaccess(a); a+=p; h->locals_stack_base=a; a+=wholepage(lsize);
1.172 anton 663: page_noaccess(a);
664: debugp(stderr,"stack addresses: d=%p f=%p r=%p l=%p\n",
1.173 anton 665: h->data_stack_base,
666: h->fp_stack_base,
667: h->return_stack_base,
668: h->locals_stack_base);
1.172 anton 669: return;
670: }
671: }
672: #endif
1.173 anton 673: h->data_stack_base=gforth_alloc(dsize);
674: h->fp_stack_base=gforth_alloc(fsize);
675: h->return_stack_base=gforth_alloc(rsize);
676: h->locals_stack_base=gforth_alloc(lsize);
1.10 pazsan 677: }
1.178 pazsan 678: #endif
1.10 pazsan 679:
1.161 pazsan 680: #warning You can ignore the warnings about clobbered variables in gforth_go
681: int gforth_go(Address image, int stack, Cell *entries)
1.11 pazsan 682: {
1.38 anton 683: volatile ImageHeader *image_header = (ImageHeader *)image;
1.18 anton 684: Cell *sp0=(Cell*)(image_header->data_stack_base + dsize);
1.44 pazsan 685: Cell *rp0=(Cell *)(image_header->return_stack_base + rsize);
1.18 anton 686: Float *fp0=(Float *)(image_header->fp_stack_base + fsize);
1.44 pazsan 687: #ifdef GFORTH_DEBUGGING
1.38 anton 688: volatile Cell *orig_rp0=rp0;
1.44 pazsan 689: #endif
1.18 anton 690: Address lp0=image_header->locals_stack_base + lsize;
691: Xt *ip0=(Xt *)(image_header->boot_entry);
1.13 pazsan 692: #ifdef SYSSIGNALS
1.11 pazsan 693: int throw_code;
1.13 pazsan 694: #endif
1.11 pazsan 695:
696: /* ensure that the cached elements (if any) are accessible */
1.151 anton 697: #if !(defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING))
698: sp0 -= 8; /* make stuff below bottom accessible for stack caching */
1.187 anton 699: fp0--;
1.151 anton 700: #endif
1.11 pazsan 701:
702: for(;stack>0;stack--)
1.18 anton 703: *--sp0=entries[stack-1];
1.11 pazsan 704:
1.177 pazsan 705: #if defined(SYSSIGNALS) && !defined(STANDALONE)
1.11 pazsan 706: get_winsize();
707:
708: install_signal_handlers(); /* right place? */
709:
710: if ((throw_code=setjmp(throw_jmp_buf))) {
1.152 anton 711: static Cell signal_data_stack[24];
712: static Cell signal_return_stack[16];
1.11 pazsan 713: static Float signal_fp_stack[1];
1.13 pazsan 714:
1.152 anton 715: signal_data_stack[15]=throw_code;
1.18 anton 716:
717: #ifdef GFORTH_DEBUGGING
1.144 pazsan 718: debugp(stderr,"\ncaught signal, throwing exception %d, ip=%p rp=%p\n",
1.97 anton 719: throw_code, saved_ip, rp);
1.38 anton 720: if (rp <= orig_rp0 && rp > (Cell *)(image_header->return_stack_base+5)) {
1.18 anton 721: /* no rstack overflow or underflow */
722: rp0 = rp;
1.63 anton 723: *--rp0 = (Cell)saved_ip;
1.18 anton 724: }
725: else /* I love non-syntactic ifdefs :-) */
1.152 anton 726: rp0 = signal_return_stack+16;
1.97 anton 727: #else /* !defined(GFORTH_DEBUGGING) */
1.144 pazsan 728: debugp(stderr,"\ncaught signal, throwing exception %d\n", throw_code);
1.152 anton 729: rp0 = signal_return_stack+16;
1.97 anton 730: #endif /* !defined(GFORTH_DEBUGGING) */
1.25 anton 731: /* fprintf(stderr, "rp=$%x\n",rp0);*/
1.11 pazsan 732:
1.164 pazsan 733: return((int)(Cell)gforth_engine(image_header->throw_entry, signal_data_stack+15,
1.197 anton 734: rp0, signal_fp_stack, 0 sr_call));
1.11 pazsan 735: }
1.13 pazsan 736: #endif
1.11 pazsan 737:
1.197 anton 738: return((int)(Cell)gforth_engine(ip0,sp0,rp0,fp0,lp0 sr_call));
1.11 pazsan 739: }
740:
1.177 pazsan 741: #if !defined(INCLUDE_IMAGE) && !defined(STANDALONE)
1.161 pazsan 742: static void print_sizes(Cell sizebyte)
1.21 anton 743: /* print size information */
744: {
745: static char* endianstring[]= { " big","little" };
746:
747: fprintf(stderr,"%s endian, cell=%d bytes, char=%d bytes, au=%d bytes\n",
748: endianstring[sizebyte & 1],
749: 1 << ((sizebyte >> 1) & 3),
750: 1 << ((sizebyte >> 3) & 3),
751: 1 << ((sizebyte >> 5) & 3));
752: }
753:
1.106 anton 754: /* static superinstruction stuff */
755:
1.141 anton 756: struct cost { /* super_info might be a more accurate name */
1.106 anton 757: char loads; /* number of stack loads */
758: char stores; /* number of stack stores */
759: char updates; /* number of stack pointer updates */
1.123 anton 760: char branch; /* is it a branch (SET_IP) */
1.125 anton 761: unsigned char state_in; /* state on entry */
762: unsigned char state_out; /* state on exit */
1.142 anton 763: unsigned char imm_ops; /* number of immediate operands */
1.123 anton 764: short offset; /* offset into super2 table */
1.125 anton 765: unsigned char length; /* number of components */
1.106 anton 766: };
767:
1.121 anton 768: PrimNum super2[] = {
1.126 anton 769: #include SUPER2_I
1.106 anton 770: };
771:
772: struct cost super_costs[] = {
1.126 anton 773: #include COSTS_I
1.106 anton 774: };
775:
1.125 anton 776: struct super_state {
777: struct super_state *next;
778: PrimNum super;
779: };
780:
1.106 anton 781: #define HASH_SIZE 256
782:
783: struct super_table_entry {
784: struct super_table_entry *next;
1.121 anton 785: PrimNum *start;
1.106 anton 786: short length;
1.125 anton 787: struct super_state *ss_list; /* list of supers */
1.106 anton 788: } *super_table[HASH_SIZE];
789: int max_super=2;
790:
1.125 anton 791: struct super_state *state_transitions=NULL;
792:
1.161 pazsan 793: static int hash_super(PrimNum *start, int length)
1.106 anton 794: {
795: int i, r;
796:
797: for (i=0, r=0; i<length; i++) {
798: r <<= 1;
799: r += start[i];
800: }
801: return r & (HASH_SIZE-1);
802: }
803:
1.161 pazsan 804: static struct super_state **lookup_super(PrimNum *start, int length)
1.106 anton 805: {
806: int hash=hash_super(start,length);
807: struct super_table_entry *p = super_table[hash];
808:
1.125 anton 809: /* assert(length >= 2); */
1.106 anton 810: for (; p!=NULL; p = p->next) {
811: if (length == p->length &&
1.121 anton 812: memcmp((char *)p->start, (char *)start, length*sizeof(PrimNum))==0)
1.125 anton 813: return &(p->ss_list);
1.106 anton 814: }
1.125 anton 815: return NULL;
1.106 anton 816: }
817:
1.161 pazsan 818: static void prepare_super_table()
1.106 anton 819: {
820: int i;
1.109 anton 821: int nsupers = 0;
1.106 anton 822:
823: for (i=0; i<sizeof(super_costs)/sizeof(super_costs[0]); i++) {
824: struct cost *c = &super_costs[i];
1.125 anton 825: if ((c->length < 2 || nsupers < static_super_number) &&
826: c->state_in < maxstates && c->state_out < maxstates) {
827: struct super_state **ss_listp= lookup_super(super2+c->offset, c->length);
828: struct super_state *ss = malloc(sizeof(struct super_state));
829: ss->super= i;
830: if (c->offset==N_noop && i != N_noop) {
831: if (is_relocatable(i)) {
832: ss->next = state_transitions;
833: state_transitions = ss;
834: }
835: } else if (ss_listp != NULL) {
836: ss->next = *ss_listp;
837: *ss_listp = ss;
838: } else {
839: int hash = hash_super(super2+c->offset, c->length);
840: struct super_table_entry **p = &super_table[hash];
841: struct super_table_entry *e = malloc(sizeof(struct super_table_entry));
842: ss->next = NULL;
843: e->next = *p;
844: e->start = super2 + c->offset;
845: e->length = c->length;
846: e->ss_list = ss;
847: *p = e;
848: }
1.106 anton 849: if (c->length > max_super)
850: max_super = c->length;
1.125 anton 851: if (c->length >= 2)
852: nsupers++;
1.106 anton 853: }
854: }
1.144 pazsan 855: debugp(stderr, "Using %d static superinsts\n", nsupers);
1.195 anton 856: if (nsupers>0 && !tpa_noautomaton && !tpa_noequiv) {
857: /* Currently these two things don't work together; see Section 3.2
858: of <http://www.complang.tuwien.ac.at/papers/ertl+06pldi.ps.gz>,
859: in particular Footnote 6 for the reason; hmm, we should be able
860: to use an automaton without state equivalence, but that costs
861: significant space so we only do it if the user explicitly
862: disables state equivalence. */
863: debugp(stderr, "Disabling tpa-automaton, because nsupers>0 and state equivalence is enabled.\n");
1.194 anton 864: tpa_noautomaton = true;
865: }
1.106 anton 866: }
867:
868: /* dynamic replication/superinstruction stuff */
869:
1.69 anton 870: #ifndef NO_DYNAMIC
1.161 pazsan 871: static int compare_priminfo_length(const void *_a, const void *_b)
1.76 anton 872: {
1.90 anton 873: PrimInfo **a = (PrimInfo **)_a;
874: PrimInfo **b = (PrimInfo **)_b;
1.77 anton 875: Cell diff = (*a)->length - (*b)->length;
876: if (diff)
877: return diff;
878: else /* break ties by start address; thus the decompiler produces
879: the earliest primitive with the same code (e.g. noop instead
880: of (char) and @ instead of >code-address */
881: return (*b)->start - (*a)->start;
1.76 anton 882: }
1.112 anton 883: #endif /* !defined(NO_DYNAMIC) */
1.76 anton 884:
1.125 anton 885: static char MAYBE_UNUSED superend[]={
1.126 anton 886: #include PRIM_SUPEREND_I
1.106 anton 887: };
1.107 anton 888:
889: Cell npriminfos=0;
1.76 anton 890:
1.146 anton 891: Label goto_start;
892: Cell goto_len;
893:
1.162 pazsan 894: #ifndef NO_DYNAMIC
1.161 pazsan 895: static int compare_labels(const void *pa, const void *pb)
1.113 anton 896: {
1.114 anton 897: Label a = *(Label *)pa;
898: Label b = *(Label *)pb;
899: return a-b;
900: }
1.162 pazsan 901: #endif
1.113 anton 902:
1.161 pazsan 903: static Label bsearch_next(Label key, Label *a, UCell n)
1.114 anton 904: /* a is sorted; return the label >=key that is the closest in a;
905: return NULL if there is no label in a >=key */
906: {
907: int mid = (n-1)/2;
908: if (n<1)
909: return NULL;
910: if (n == 1) {
911: if (a[0] < key)
912: return NULL;
913: else
914: return a[0];
915: }
916: if (a[mid] < key)
917: return bsearch_next(key, a+mid+1, n-mid-1);
918: else
919: return bsearch_next(key, a, mid+1);
1.113 anton 920: }
921:
1.161 pazsan 922: static void check_prims(Label symbols1[])
1.47 anton 923: {
924: int i;
1.90 anton 925: #ifndef NO_DYNAMIC
1.146 anton 926: Label *symbols2, *symbols3, *ends1, *ends1j, *ends1jsorted, *goto_p;
1.119 anton 927: int nends1j;
1.90 anton 928: #endif
1.47 anton 929:
1.66 anton 930: if (debug)
931: #ifdef __VERSION__
932: fprintf(stderr, "Compiled with gcc-" __VERSION__ "\n");
933: #else
934: #define xstr(s) str(s)
935: #define str(s) #s
936: fprintf(stderr, "Compiled with gcc-" xstr(__GNUC__) "." xstr(__GNUC_MINOR__) "\n");
937: #endif
1.121 anton 938: for (i=0; symbols1[i]!=0; i++)
1.47 anton 939: ;
1.55 anton 940: npriminfos = i;
1.70 anton 941:
942: #ifndef NO_DYNAMIC
1.66 anton 943: if (no_dynamic)
944: return;
1.197 anton 945: symbols2=gforth_engine2(0,0,0,0,0 sr_call);
1.70 anton 946: #if NO_IP
1.197 anton 947: symbols3=gforth_engine3(0,0,0,0,0 sr_call);
1.70 anton 948: #else
949: symbols3=symbols1;
950: #endif
1.121 anton 951: ends1 = symbols1+i+1;
1.119 anton 952: ends1j = ends1+i;
1.146 anton 953: goto_p = ends1j+i+1; /* goto_p[0]==before; ...[1]==after;*/
1.121 anton 954: nends1j = i+1;
1.119 anton 955: ends1jsorted = (Label *)alloca(nends1j*sizeof(Label));
956: memcpy(ends1jsorted,ends1j,nends1j*sizeof(Label));
957: qsort(ends1jsorted, nends1j, sizeof(Label), compare_labels);
1.146 anton 958:
959: /* check whether the "goto *" is relocatable */
960: goto_len = goto_p[1]-goto_p[0];
961: debugp(stderr, "goto * %p %p len=%ld\n",
1.190 anton 962: goto_p[0],symbols2[goto_p-symbols1],(long)goto_len);
1.146 anton 963: if (memcmp(goto_p[0],symbols2[goto_p-symbols1],goto_len)!=0) { /* unequal */
964: no_dynamic=1;
965: debugp(stderr," not relocatable, disabling dynamic code generation\n");
1.148 anton 966: init_ss_cost();
1.146 anton 967: return;
968: }
969: goto_start = goto_p[0];
1.113 anton 970:
1.47 anton 971: priminfos = calloc(i,sizeof(PrimInfo));
1.121 anton 972: for (i=0; symbols1[i]!=0; i++) {
1.70 anton 973: int prim_len = ends1[i]-symbols1[i];
1.47 anton 974: PrimInfo *pi=&priminfos[i];
1.154 anton 975: struct cost *sc=&super_costs[i];
1.70 anton 976: int j=0;
977: char *s1 = (char *)symbols1[i];
978: char *s2 = (char *)symbols2[i];
979: char *s3 = (char *)symbols3[i];
1.119 anton 980: Label endlabel = bsearch_next(symbols1[i]+1,ends1jsorted,nends1j);
1.70 anton 981:
982: pi->start = s1;
1.121 anton 983: pi->superend = superend[i]|no_super;
1.147 anton 984: pi->length = prim_len;
1.113 anton 985: pi->restlength = endlabel - symbols1[i] - pi->length;
1.70 anton 986: pi->nimmargs = 0;
1.144 pazsan 987: relocs++;
1.190 anton 988: #if defined(BURG_FORMAT)
989: { /* output as burg-style rules */
990: int p=super_costs[i].offset;
991: if (p==N_noop)
992: debugp(stderr, "S%d: S%d = %d (%d);", sc->state_in, sc->state_out, i+1, pi->length);
993: else
994: debugp(stderr, "S%d: op%d(S%d) = %d (%d);", sc->state_in, p, sc->state_out, i+1, pi->length);
995: }
996: #else
1.154 anton 997: debugp(stderr, "%-15s %d-%d %4d %p %p %p len=%3ld rest=%2ld send=%1d",
998: prim_names[i], sc->state_in, sc->state_out,
999: i, s1, s2, s3, (long)(pi->length), (long)(pi->restlength),
1000: pi->superend);
1.190 anton 1001: #endif
1.114 anton 1002: if (endlabel == NULL) {
1003: pi->start = NULL; /* not relocatable */
1.122 anton 1004: if (pi->length<0) pi->length=100;
1.190 anton 1005: #ifndef BURG_FORMAT
1.144 pazsan 1006: debugp(stderr,"\n non_reloc: no J label > start found\n");
1.190 anton 1007: #endif
1.144 pazsan 1008: relocs--;
1009: nonrelocs++;
1.114 anton 1010: continue;
1011: }
1012: if (ends1[i] > endlabel && !pi->superend) {
1.113 anton 1013: pi->start = NULL; /* not relocatable */
1.122 anton 1014: pi->length = endlabel-symbols1[i];
1.190 anton 1015: #ifndef BURG_FORMAT
1.144 pazsan 1016: debugp(stderr,"\n non_reloc: there is a J label before the K label (restlength<0)\n");
1.190 anton 1017: #endif
1.144 pazsan 1018: relocs--;
1019: nonrelocs++;
1.113 anton 1020: continue;
1021: }
1.114 anton 1022: if (ends1[i] < pi->start && !pi->superend) {
1.113 anton 1023: pi->start = NULL; /* not relocatable */
1.122 anton 1024: pi->length = endlabel-symbols1[i];
1.190 anton 1025: #ifndef BURG_FORMAT
1.144 pazsan 1026: debugp(stderr,"\n non_reloc: K label before I label (length<0)\n");
1.190 anton 1027: #endif
1.144 pazsan 1028: relocs--;
1029: nonrelocs++;
1.113 anton 1030: continue;
1031: }
1.138 anton 1032: assert(pi->length>=0);
1.113 anton 1033: assert(pi->restlength >=0);
1.74 anton 1034: while (j<(pi->length+pi->restlength)) {
1.70 anton 1035: if (s1[j]==s3[j]) {
1036: if (s1[j] != s2[j]) {
1037: pi->start = NULL; /* not relocatable */
1.190 anton 1038: #ifndef BURG_FORMAT
1.144 pazsan 1039: debugp(stderr,"\n non_reloc: engine1!=engine2 offset %3d",j);
1.190 anton 1040: #endif
1.74 anton 1041: /* assert(j<prim_len); */
1.144 pazsan 1042: relocs--;
1043: nonrelocs++;
1.70 anton 1044: break;
1045: }
1046: j++;
1047: } else {
1048: struct immarg *ia=&pi->immargs[pi->nimmargs];
1049:
1050: pi->nimmargs++;
1051: ia->offset=j;
1052: if ((~*(Cell *)&(s1[j]))==*(Cell *)&(s3[j])) {
1053: ia->rel=0;
1.144 pazsan 1054: debugp(stderr,"\n absolute immarg: offset %3d",j);
1.70 anton 1055: } else if ((&(s1[j]))+(*(Cell *)&(s1[j]))+4 ==
1056: symbols1[DOESJUMP+1]) {
1057: ia->rel=1;
1.144 pazsan 1058: debugp(stderr,"\n relative immarg: offset %3d",j);
1.70 anton 1059: } else {
1060: pi->start = NULL; /* not relocatable */
1.190 anton 1061: #ifndef BURG_FORMAT
1.144 pazsan 1062: debugp(stderr,"\n non_reloc: engine1!=engine3 offset %3d",j);
1.190 anton 1063: #endif
1.74 anton 1064: /* assert(j<prim_len);*/
1.144 pazsan 1065: relocs--;
1066: nonrelocs++;
1.70 anton 1067: break;
1068: }
1069: j+=4;
1.47 anton 1070: }
1071: }
1.144 pazsan 1072: debugp(stderr,"\n");
1.70 anton 1073: }
1.76 anton 1074: decomp_prims = calloc(i,sizeof(PrimInfo *));
1075: for (i=DOESJUMP+1; i<npriminfos; i++)
1076: decomp_prims[i] = &(priminfos[i]);
1077: qsort(decomp_prims+DOESJUMP+1, npriminfos-DOESJUMP-1, sizeof(PrimInfo *),
1078: compare_priminfo_length);
1.70 anton 1079: #endif
1080: }
1081:
1.161 pazsan 1082: static void flush_to_here(void)
1.74 anton 1083: {
1.93 anton 1084: #ifndef NO_DYNAMIC
1.100 anton 1085: if (start_flush)
1086: FLUSH_ICACHE(start_flush, code_here-start_flush);
1.74 anton 1087: start_flush=code_here;
1.93 anton 1088: #endif
1.74 anton 1089: }
1090:
1.185 anton 1091: static void align_code(void)
1092: /* align code_here on some platforms */
1093: {
1094: #ifndef NO_DYNAMIC
1.186 anton 1095: #if defined(CODE_PADDING)
1.185 anton 1096: Cell alignment = CODE_ALIGNMENT;
1.186 anton 1097: static char nops[] = CODE_PADDING;
1098: UCell maxpadding=MAX_PADDING;
1.185 anton 1099: UCell offset = ((UCell)code_here)&(alignment-1);
1100: UCell length = alignment-offset;
1.186 anton 1101: if (length <= maxpadding) {
1102: memcpy(code_here,nops+offset,length);
1.185 anton 1103: code_here += length;
1104: }
1.186 anton 1105: #endif /* defined(CODE_PADDING) */
1.185 anton 1106: #endif /* defined(NO_DYNAMIC */
1107: }
1108:
1.93 anton 1109: #ifndef NO_DYNAMIC
1.161 pazsan 1110: static void append_jump(void)
1.74 anton 1111: {
1112: if (last_jump) {
1113: PrimInfo *pi = &priminfos[last_jump];
1114:
1115: memcpy(code_here, pi->start+pi->length, pi->restlength);
1116: code_here += pi->restlength;
1.147 anton 1117: memcpy(code_here, goto_start, goto_len);
1118: code_here += goto_len;
1.185 anton 1119: align_code();
1.74 anton 1120: last_jump=0;
1121: }
1122: }
1123:
1.75 anton 1124: /* Gforth remembers all code blocks in this list. On forgetting (by
1125: executing a marker) the code blocks are not freed (because Gforth does
1126: not remember how they were allocated; hmm, remembering that might be
1127: easier and cleaner). Instead, code_here etc. are reset to the old
1128: value, and the "forgotten" code blocks are reused when they are
1129: needed. */
1130:
1131: struct code_block_list {
1132: struct code_block_list *next;
1133: Address block;
1134: Cell size;
1135: } *code_block_list=NULL, **next_code_blockp=&code_block_list;
1136:
1.161 pazsan 1137: static Address append_prim(Cell p)
1.74 anton 1138: {
1139: PrimInfo *pi = &priminfos[p];
1140: Address old_code_here = code_here;
1141:
1.185 anton 1142: if (code_area+code_area_size < code_here+pi->length+pi->restlength+goto_len+CODE_ALIGNMENT) {
1.75 anton 1143: struct code_block_list *p;
1.74 anton 1144: append_jump();
1.93 anton 1145: flush_to_here();
1.75 anton 1146: if (*next_code_blockp == NULL) {
1.161 pazsan 1147: code_here = start_flush = code_area = gforth_alloc(code_area_size);
1.75 anton 1148: p = (struct code_block_list *)malloc(sizeof(struct code_block_list));
1149: *next_code_blockp = p;
1150: p->next = NULL;
1151: p->block = code_here;
1152: p->size = code_area_size;
1153: } else {
1154: p = *next_code_blockp;
1155: code_here = start_flush = code_area = p->block;
1156: }
1.74 anton 1157: old_code_here = code_here;
1.75 anton 1158: next_code_blockp = &(p->next);
1.74 anton 1159: }
1160: memcpy(code_here, pi->start, pi->length);
1161: code_here += pi->length;
1162: return old_code_here;
1163: }
1164: #endif
1.75 anton 1165:
1166: int forget_dyncode(Address code)
1167: {
1168: #ifdef NO_DYNAMIC
1169: return -1;
1170: #else
1171: struct code_block_list *p, **pp;
1172:
1173: for (pp=&code_block_list, p=*pp; p!=NULL; pp=&(p->next), p=*pp) {
1174: if (code >= p->block && code < p->block+p->size) {
1175: next_code_blockp = &(p->next);
1176: code_here = start_flush = code;
1177: code_area = p->block;
1178: last_jump = 0;
1179: return -1;
1180: }
1181: }
1.78 anton 1182: return -no_dynamic;
1.75 anton 1183: #endif /* !defined(NO_DYNAMIC) */
1184: }
1185:
1.161 pazsan 1186: static long dyncodesize(void)
1.104 anton 1187: {
1188: #ifndef NO_DYNAMIC
1.106 anton 1189: struct code_block_list *p;
1.104 anton 1190: long size=0;
1191: for (p=code_block_list; p!=NULL; p=p->next) {
1192: if (code_here >= p->block && code_here < p->block+p->size)
1193: return size + (code_here - p->block);
1194: else
1195: size += p->size;
1196: }
1197: #endif /* !defined(NO_DYNAMIC) */
1198: return 0;
1199: }
1200:
1.90 anton 1201: Label decompile_code(Label _code)
1.75 anton 1202: {
1.76 anton 1203: #ifdef NO_DYNAMIC
1.90 anton 1204: return _code;
1.76 anton 1205: #else /* !defined(NO_DYNAMIC) */
1206: Cell i;
1.77 anton 1207: struct code_block_list *p;
1.90 anton 1208: Address code=_code;
1.76 anton 1209:
1.77 anton 1210: /* first, check if we are in code at all */
1211: for (p = code_block_list;; p = p->next) {
1212: if (p == NULL)
1213: return code;
1214: if (code >= p->block && code < p->block+p->size)
1215: break;
1216: }
1.76 anton 1217: /* reverse order because NOOP might match other prims */
1218: for (i=npriminfos-1; i>DOESJUMP; i--) {
1219: PrimInfo *pi=decomp_prims[i];
1220: if (pi->start==code || (pi->start && memcmp(code,pi->start,pi->length)==0))
1.121 anton 1221: return vm_prims[super2[super_costs[pi-priminfos].offset]];
1.118 anton 1222: /* return pi->start;*/
1.76 anton 1223: }
1224: return code;
1225: #endif /* !defined(NO_DYNAMIC) */
1.75 anton 1226: }
1.74 anton 1227:
1.70 anton 1228: #ifdef NO_IP
1229: int nbranchinfos=0;
1230:
1231: struct branchinfo {
1.136 anton 1232: Label **targetpp; /* **(bi->targetpp) is the target */
1.70 anton 1233: Cell *addressptr; /* store the target here */
1234: } branchinfos[100000];
1235:
1236: int ndoesexecinfos=0;
1237: struct doesexecinfo {
1238: int branchinfo; /* fix the targetptr of branchinfos[...->branchinfo] */
1.136 anton 1239: Label *targetp; /*target for branch (because this is not in threaded code)*/
1.70 anton 1240: Cell *xt; /* cfa of word whose does-code needs calling */
1241: } doesexecinfos[10000];
1242:
1.161 pazsan 1243: static void set_rel_target(Cell *source, Label target)
1.70 anton 1244: {
1245: *source = ((Cell)target)-(((Cell)source)+4);
1246: }
1247:
1.161 pazsan 1248: static void register_branchinfo(Label source, Cell *targetpp)
1.70 anton 1249: {
1250: struct branchinfo *bi = &(branchinfos[nbranchinfos]);
1.136 anton 1251: bi->targetpp = (Label **)targetpp;
1.70 anton 1252: bi->addressptr = (Cell *)source;
1253: nbranchinfos++;
1254: }
1255:
1.161 pazsan 1256: static Address compile_prim1arg(PrimNum p, Cell **argp)
1.70 anton 1257: {
1.133 anton 1258: Address old_code_here=append_prim(p);
1.70 anton 1259:
1.74 anton 1260: assert(vm_prims[p]==priminfos[p].start);
1.133 anton 1261: *argp = (Cell*)(old_code_here+priminfos[p].immargs[0].offset);
1262: return old_code_here;
1.70 anton 1263: }
1264:
1.161 pazsan 1265: static Address compile_call2(Cell *targetpp, Cell **next_code_targetp)
1.70 anton 1266: {
1.73 anton 1267: PrimInfo *pi = &priminfos[N_call2];
1.74 anton 1268: Address old_code_here = append_prim(N_call2);
1.70 anton 1269:
1.134 anton 1270: *next_code_targetp = (Cell *)(old_code_here + pi->immargs[0].offset);
1.136 anton 1271: register_branchinfo(old_code_here + pi->immargs[1].offset, targetpp);
1.134 anton 1272: return old_code_here;
1.70 anton 1273: }
1274: #endif
1275:
1276: void finish_code(void)
1277: {
1278: #ifdef NO_IP
1279: Cell i;
1280:
1281: compile_prim1(NULL);
1282: for (i=0; i<ndoesexecinfos; i++) {
1283: struct doesexecinfo *dei = &doesexecinfos[i];
1.136 anton 1284: dei->targetp = (Label *)DOES_CODE1((dei->xt));
1285: branchinfos[dei->branchinfo].targetpp = &(dei->targetp);
1.70 anton 1286: }
1287: ndoesexecinfos = 0;
1288: for (i=0; i<nbranchinfos; i++) {
1289: struct branchinfo *bi=&branchinfos[i];
1.136 anton 1290: set_rel_target(bi->addressptr, **(bi->targetpp));
1.70 anton 1291: }
1292: nbranchinfos = 0;
1.128 anton 1293: #else
1294: compile_prim1(NULL);
1.48 anton 1295: #endif
1.93 anton 1296: flush_to_here();
1.48 anton 1297: }
1298:
1.162 pazsan 1299: #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
1.128 anton 1300: #ifdef NO_IP
1.161 pazsan 1301: static Cell compile_prim_dyn(PrimNum p, Cell *tcp)
1.128 anton 1302: /* compile prim #p dynamically (mod flags etc.) and return start
1303: address of generated code for putting it into the threaded
1304: code. This function is only called if all the associated
1305: inline arguments of p are already in place (at tcp[1] etc.) */
1306: {
1307: PrimInfo *pi=&priminfos[p];
1308: Cell *next_code_target=NULL;
1.135 anton 1309: Address codeaddr;
1310: Address primstart;
1.128 anton 1311:
1312: assert(p<npriminfos);
1313: if (p==N_execute || p==N_perform || p==N_lit_perform) {
1.134 anton 1314: codeaddr = compile_prim1arg(N_set_next_code, &next_code_target);
1.135 anton 1315: primstart = append_prim(p);
1316: goto other_prim;
1317: } else if (p==N_call) {
1.136 anton 1318: codeaddr = compile_call2(tcp+1, &next_code_target);
1.128 anton 1319: } else if (p==N_does_exec) {
1320: struct doesexecinfo *dei = &doesexecinfos[ndoesexecinfos++];
1.133 anton 1321: Cell *arg;
1322: codeaddr = compile_prim1arg(N_lit,&arg);
1323: *arg = (Cell)PFA(tcp[1]);
1.128 anton 1324: /* we cannot determine the callee now (last_start[1] may be a
1325: forward reference), so just register an arbitrary target, and
1326: register in dei that we need to fix this before resolving
1327: branches */
1328: dei->branchinfo = nbranchinfos;
1329: dei->xt = (Cell *)(tcp[1]);
1.134 anton 1330: compile_call2(0, &next_code_target);
1.128 anton 1331: } else if (!is_relocatable(p)) {
1.133 anton 1332: Cell *branch_target;
1333: codeaddr = compile_prim1arg(N_set_next_code, &next_code_target);
1334: compile_prim1arg(N_branch,&branch_target);
1335: set_rel_target(branch_target,vm_prims[p]);
1.128 anton 1336: } else {
1337: unsigned j;
1.135 anton 1338:
1339: codeaddr = primstart = append_prim(p);
1340: other_prim:
1.128 anton 1341: for (j=0; j<pi->nimmargs; j++) {
1342: struct immarg *ia = &(pi->immargs[j]);
1.136 anton 1343: Cell *argp = tcp + pi->nimmargs - j;
1344: Cell argval = *argp; /* !! specific to prims */
1.128 anton 1345: if (ia->rel) { /* !! assumption: relative refs are branches */
1.136 anton 1346: register_branchinfo(primstart + ia->offset, argp);
1.128 anton 1347: } else /* plain argument */
1.135 anton 1348: *(Cell *)(primstart + ia->offset) = argval;
1.128 anton 1349: }
1350: }
1351: if (next_code_target!=NULL)
1352: *next_code_target = (Cell)code_here;
1.135 anton 1353: return (Cell)codeaddr;
1.128 anton 1354: }
1355: #else /* !defined(NO_IP) */
1.161 pazsan 1356: static Cell compile_prim_dyn(PrimNum p, Cell *tcp)
1.128 anton 1357: /* compile prim #p dynamically (mod flags etc.) and return start
1358: address of generated code for putting it into the threaded code */
1.108 anton 1359: {
1.121 anton 1360: Cell static_prim = (Cell)vm_prims[p];
1.108 anton 1361: #if defined(NO_DYNAMIC)
1362: return static_prim;
1363: #else /* !defined(NO_DYNAMIC) */
1364: Address old_code_here;
1365:
1366: if (no_dynamic)
1367: return static_prim;
1.125 anton 1368: if (p>=npriminfos || !is_relocatable(p)) {
1.108 anton 1369: append_jump();
1370: return static_prim;
1371: }
1372: old_code_here = append_prim(p);
1.147 anton 1373: last_jump = p;
1374: if (priminfos[p].superend)
1375: append_jump();
1.108 anton 1376: return (Cell)old_code_here;
1377: #endif /* !defined(NO_DYNAMIC) */
1378: }
1.128 anton 1379: #endif /* !defined(NO_IP) */
1.162 pazsan 1380: #endif
1.70 anton 1381:
1.109 anton 1382: #ifndef NO_DYNAMIC
1.161 pazsan 1383: static int cost_codesize(int prim)
1.109 anton 1384: {
1.121 anton 1385: return priminfos[prim].length;
1.109 anton 1386: }
1387: #endif
1388:
1.161 pazsan 1389: static int cost_ls(int prim)
1.109 anton 1390: {
1391: struct cost *c = super_costs+prim;
1392:
1393: return c->loads + c->stores;
1394: }
1395:
1.161 pazsan 1396: static int cost_lsu(int prim)
1.109 anton 1397: {
1398: struct cost *c = super_costs+prim;
1399:
1400: return c->loads + c->stores + c->updates;
1401: }
1402:
1.161 pazsan 1403: static int cost_nexts(int prim)
1.109 anton 1404: {
1405: return 1;
1406: }
1407:
1408: typedef int Costfunc(int);
1409: Costfunc *ss_cost = /* cost function for optimize_bb */
1410: #ifdef NO_DYNAMIC
1411: cost_lsu;
1412: #else
1413: cost_codesize;
1414: #endif
1415:
1.110 anton 1416: struct {
1417: Costfunc *costfunc;
1418: char *metricname;
1419: long sum;
1420: } cost_sums[] = {
1421: #ifndef NO_DYNAMIC
1422: { cost_codesize, "codesize", 0 },
1423: #endif
1424: { cost_ls, "ls", 0 },
1425: { cost_lsu, "lsu", 0 },
1426: { cost_nexts, "nexts", 0 }
1427: };
1428:
1.148 anton 1429: #ifndef NO_DYNAMIC
1430: void init_ss_cost(void) {
1431: if (no_dynamic && ss_cost == cost_codesize) {
1432: ss_cost = cost_nexts;
1433: cost_sums[0] = cost_sums[1]; /* don't use cost_codesize for print-metrics */
1434: debugp(stderr, "--no-dynamic conflicts with --ss-min-codesize, reverting to --ss-min-nexts\n");
1435: }
1436: }
1437: #endif
1438:
1.106 anton 1439: #define MAX_BB 128 /* maximum number of instructions in BB */
1.125 anton 1440: #define INF_COST 1000000 /* infinite cost */
1441: #define CANONICAL_STATE 0
1442:
1443: struct waypoint {
1444: int cost; /* the cost from here to the end */
1445: PrimNum inst; /* the inst used from here to the next waypoint */
1446: char relocatable; /* the last non-transition was relocatable */
1447: char no_transition; /* don't use the next transition (relocatability)
1448: * or this transition (does not change state) */
1449: };
1450:
1.156 anton 1451: struct tpa_state { /* tree parsing automaton (like) state */
1.155 anton 1452: /* labeling is back-to-front */
1453: struct waypoint *inst; /* in front of instruction */
1454: struct waypoint *trans; /* in front of instruction and transition */
1455: };
1456:
1.156 anton 1457: struct tpa_state *termstate = NULL; /* initialized in loader() */
1.155 anton 1458:
1.158 anton 1459: /* statistics about tree parsing (lazyburg) stuff */
1460: long lb_basic_blocks = 0;
1461: long lb_labeler_steps = 0;
1462: long lb_labeler_automaton = 0;
1463: long lb_labeler_dynprog = 0;
1464: long lb_newstate_equiv = 0;
1465: long lb_newstate_new = 0;
1466: long lb_applicable_base_rules = 0;
1467: long lb_applicable_chain_rules = 0;
1468:
1.162 pazsan 1469: #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
1.161 pazsan 1470: static void init_waypoints(struct waypoint ws[])
1.125 anton 1471: {
1472: int k;
1473:
1474: for (k=0; k<maxstates; k++)
1475: ws[k].cost=INF_COST;
1476: }
1.106 anton 1477:
1.161 pazsan 1478: static struct tpa_state *empty_tpa_state()
1.155 anton 1479: {
1.156 anton 1480: struct tpa_state *s = malloc(sizeof(struct tpa_state));
1.155 anton 1481:
1.157 anton 1482: s->inst = calloc(maxstates,sizeof(struct waypoint));
1.155 anton 1483: init_waypoints(s->inst);
1.157 anton 1484: s->trans = calloc(maxstates,sizeof(struct waypoint));
1.155 anton 1485: /* init_waypoints(s->trans);*/
1486: return s;
1487: }
1488:
1.161 pazsan 1489: static void transitions(struct tpa_state *t)
1.107 anton 1490: {
1.125 anton 1491: int k;
1492: struct super_state *l;
1493:
1494: for (k=0; k<maxstates; k++) {
1.155 anton 1495: t->trans[k] = t->inst[k];
1496: t->trans[k].no_transition = 1;
1.125 anton 1497: }
1498: for (l = state_transitions; l != NULL; l = l->next) {
1499: PrimNum s = l->super;
1500: int jcost;
1501: struct cost *c=super_costs+s;
1.155 anton 1502: struct waypoint *wi=&(t->trans[c->state_in]);
1503: struct waypoint *wo=&(t->inst[c->state_out]);
1.158 anton 1504: lb_applicable_chain_rules++;
1.125 anton 1505: if (wo->cost == INF_COST)
1506: continue;
1507: jcost = wo->cost + ss_cost(s);
1508: if (jcost <= wi->cost) {
1509: wi->cost = jcost;
1510: wi->inst = s;
1511: wi->relocatable = wo->relocatable;
1512: wi->no_transition = 0;
1513: /* if (ss_greedy) wi->cost = wo->cost ? */
1514: }
1515: }
1516: }
1.107 anton 1517:
1.161 pazsan 1518: static struct tpa_state *make_termstate()
1.155 anton 1519: {
1.157 anton 1520: struct tpa_state *s = empty_tpa_state();
1.155 anton 1521:
1522: s->inst[CANONICAL_STATE].cost = 0;
1523: transitions(s);
1524: return s;
1525: }
1.162 pazsan 1526: #endif
1.155 anton 1527:
1.156 anton 1528: #define TPA_SIZE 16384
1529:
1530: struct tpa_entry {
1531: struct tpa_entry *next;
1532: PrimNum inst;
1533: struct tpa_state *state_behind; /* note: brack-to-front labeling */
1534: struct tpa_state *state_infront; /* note: brack-to-front labeling */
1535: } *tpa_table[TPA_SIZE];
1536:
1.162 pazsan 1537: #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
1.161 pazsan 1538: static Cell hash_tpa(PrimNum p, struct tpa_state *t)
1.156 anton 1539: {
1540: UCell it = (UCell )t;
1541: return (p+it+(it>>14))&(TPA_SIZE-1);
1542: }
1543:
1.161 pazsan 1544: static struct tpa_state **lookup_tpa(PrimNum p, struct tpa_state *t2)
1.156 anton 1545: {
1546: int hash=hash_tpa(p, t2);
1547: struct tpa_entry *te = tpa_table[hash];
1548:
1.158 anton 1549: if (tpa_noautomaton) {
1550: static struct tpa_state *t;
1551: t = NULL;
1552: return &t;
1553: }
1.156 anton 1554: for (; te!=NULL; te = te->next) {
1555: if (p == te->inst && t2 == te->state_behind)
1556: return &(te->state_infront);
1557: }
1558: te = (struct tpa_entry *)malloc(sizeof(struct tpa_entry));
1559: te->next = tpa_table[hash];
1560: te->inst = p;
1561: te->state_behind = t2;
1562: te->state_infront = NULL;
1563: tpa_table[hash] = te;
1564: return &(te->state_infront);
1565: }
1566:
1.161 pazsan 1567: static void tpa_state_normalize(struct tpa_state *t)
1.157 anton 1568: {
1569: /* normalize so cost of canonical state=0; this may result in
1570: negative states for some states */
1571: int d = t->inst[CANONICAL_STATE].cost;
1572: int i;
1573:
1574: for (i=0; i<maxstates; i++) {
1575: if (t->inst[i].cost != INF_COST)
1576: t->inst[i].cost -= d;
1577: if (t->trans[i].cost != INF_COST)
1578: t->trans[i].cost -= d;
1579: }
1580: }
1581:
1.161 pazsan 1582: static int tpa_state_equivalent(struct tpa_state *t1, struct tpa_state *t2)
1.157 anton 1583: {
1584: return (memcmp(t1->inst, t2->inst, maxstates*sizeof(struct waypoint)) == 0 &&
1585: memcmp(t1->trans,t2->trans,maxstates*sizeof(struct waypoint)) == 0);
1586: }
1.162 pazsan 1587: #endif
1.157 anton 1588:
1589: struct tpa_state_entry {
1590: struct tpa_state_entry *next;
1591: struct tpa_state *state;
1592: } *tpa_state_table[TPA_SIZE];
1593:
1.163 pazsan 1594: #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
1.161 pazsan 1595: static Cell hash_tpa_state(struct tpa_state *t)
1.157 anton 1596: {
1597: int *ti = (int *)(t->inst);
1598: int *tt = (int *)(t->trans);
1599: int r=0;
1600: int i;
1601:
1602: for (i=0; ti+i < (int *)(t->inst+maxstates); i++)
1603: r += ti[i]+tt[i];
1604: return (r+(r>>14)+(r>>22)) & (TPA_SIZE-1);
1605: }
1606:
1.161 pazsan 1607: static struct tpa_state *lookup_tpa_state(struct tpa_state *t)
1.157 anton 1608: {
1609: Cell hash = hash_tpa_state(t);
1610: struct tpa_state_entry *te = tpa_state_table[hash];
1611: struct tpa_state_entry *tn;
1612:
1.158 anton 1613: if (!tpa_noequiv) {
1614: for (; te!=NULL; te = te->next) {
1615: if (tpa_state_equivalent(t, te->state)) {
1616: lb_newstate_equiv++;
1617: free(t->inst);
1618: free(t->trans);
1619: free(t);
1620: return te->state;
1621: }
1.157 anton 1622: }
1.158 anton 1623: tn = (struct tpa_state_entry *)malloc(sizeof(struct tpa_state_entry));
1624: tn->next = te;
1625: tn->state = t;
1626: tpa_state_table[hash] = tn;
1627: }
1628: lb_newstate_new++;
1629: if (tpa_trace)
1630: fprintf(stderr, "%ld %ld lb_states\n", lb_labeler_steps, lb_newstate_new);
1.157 anton 1631: return t;
1632: }
1633:
1.125 anton 1634: /* use dynamic programming to find the shortest paths within the basic
1635: block origs[0..ninsts-1] and rewrite the instructions pointed to by
1636: instps to use it */
1.161 pazsan 1637: static void optimize_rewrite(Cell *instps[], PrimNum origs[], int ninsts)
1.125 anton 1638: {
1639: int i,j;
1.156 anton 1640: struct tpa_state *ts[ninsts+1];
1.125 anton 1641: int nextdyn, nextstate, no_transition;
1642:
1.158 anton 1643: lb_basic_blocks++;
1.155 anton 1644: ts[ninsts] = termstate;
1.189 anton 1645: #ifndef NO_DYNAMIC
1646: if (print_sequences) {
1647: for (i=0; i<ninsts; i++)
1.190 anton 1648: #if defined(BURG_FORMAT)
1649: fprintf(stderr, "op%d ", super_costs[origs[i]].offset);
1650: #else
1.189 anton 1651: fprintf(stderr, "%s ", prim_names[origs[i]]);
1.190 anton 1652: #endif
1.189 anton 1653: fprintf(stderr, "\n");
1654: }
1655: #endif
1.107 anton 1656: for (i=ninsts-1; i>=0; i--) {
1.156 anton 1657: struct tpa_state **tp = lookup_tpa(origs[i],ts[i+1]);
1658: struct tpa_state *t = *tp;
1.158 anton 1659: lb_labeler_steps++;
1660: if (t) {
1.156 anton 1661: ts[i] = t;
1.158 anton 1662: lb_labeler_automaton++;
1663: }
1.156 anton 1664: else {
1.158 anton 1665: lb_labeler_dynprog++;
1.156 anton 1666: ts[i] = empty_tpa_state();
1667: for (j=1; j<=max_super && i+j<=ninsts; j++) {
1668: struct super_state **superp = lookup_super(origs+i, j);
1669: if (superp!=NULL) {
1670: struct super_state *supers = *superp;
1671: for (; supers!=NULL; supers = supers->next) {
1672: PrimNum s = supers->super;
1673: int jcost;
1674: struct cost *c=super_costs+s;
1675: struct waypoint *wi=&(ts[i]->inst[c->state_in]);
1676: struct waypoint *wo=&(ts[i+j]->trans[c->state_out]);
1677: int no_transition = wo->no_transition;
1.158 anton 1678: lb_applicable_base_rules++;
1.156 anton 1679: if (!(is_relocatable(s)) && !wo->relocatable) {
1680: wo=&(ts[i+j]->inst[c->state_out]);
1681: no_transition=1;
1682: }
1683: if (wo->cost == INF_COST)
1684: continue;
1685: jcost = wo->cost + ss_cost(s);
1686: if (jcost <= wi->cost) {
1687: wi->cost = jcost;
1688: wi->inst = s;
1689: wi->relocatable = is_relocatable(s);
1690: wi->no_transition = no_transition;
1691: /* if (ss_greedy) wi->cost = wo->cost ? */
1692: }
1.125 anton 1693: }
1.107 anton 1694: }
1695: }
1.156 anton 1696: transitions(ts[i]);
1.157 anton 1697: tpa_state_normalize(ts[i]);
1698: *tp = ts[i] = lookup_tpa_state(ts[i]);
1.158 anton 1699: if (tpa_trace)
1700: fprintf(stderr, "%ld %ld lb_table_entries\n", lb_labeler_steps, lb_labeler_dynprog);
1.107 anton 1701: }
1.125 anton 1702: }
1703: /* now rewrite the instructions */
1704: nextdyn=0;
1705: nextstate=CANONICAL_STATE;
1.155 anton 1706: no_transition = ((!ts[0]->trans[nextstate].relocatable)
1707: ||ts[0]->trans[nextstate].no_transition);
1.125 anton 1708: for (i=0; i<ninsts; i++) {
1709: Cell tc=0, tc2;
1710: if (i==nextdyn) {
1711: if (!no_transition) {
1712: /* process trans */
1.155 anton 1713: PrimNum p = ts[i]->trans[nextstate].inst;
1.125 anton 1714: struct cost *c = super_costs+p;
1.155 anton 1715: assert(ts[i]->trans[nextstate].cost != INF_COST);
1.125 anton 1716: assert(c->state_in==nextstate);
1.128 anton 1717: tc = compile_prim_dyn(p,NULL);
1.125 anton 1718: nextstate = c->state_out;
1719: }
1720: {
1721: /* process inst */
1.155 anton 1722: PrimNum p = ts[i]->inst[nextstate].inst;
1.125 anton 1723: struct cost *c=super_costs+p;
1724: assert(c->state_in==nextstate);
1.155 anton 1725: assert(ts[i]->inst[nextstate].cost != INF_COST);
1.125 anton 1726: #if defined(GFORTH_DEBUGGING)
1727: assert(p == origs[i]);
1728: #endif
1.128 anton 1729: tc2 = compile_prim_dyn(p,instps[i]);
1.125 anton 1730: if (no_transition || !is_relocatable(p))
1731: /* !! actually what we care about is if and where
1732: * compile_prim_dyn() puts NEXTs */
1733: tc=tc2;
1.155 anton 1734: no_transition = ts[i]->inst[nextstate].no_transition;
1.125 anton 1735: nextstate = c->state_out;
1736: nextdyn += c->length;
1737: }
1738: } else {
1739: #if defined(GFORTH_DEBUGGING)
1740: assert(0);
1741: #endif
1742: tc=0;
1.155 anton 1743: /* tc= (Cell)vm_prims[ts[i]->inst[CANONICAL_STATE].inst]; */
1.125 anton 1744: }
1745: *(instps[i]) = tc;
1746: }
1747: if (!no_transition) {
1.155 anton 1748: PrimNum p = ts[i]->trans[nextstate].inst;
1.125 anton 1749: struct cost *c = super_costs+p;
1750: assert(c->state_in==nextstate);
1.155 anton 1751: assert(ts[i]->trans[nextstate].cost != INF_COST);
1.125 anton 1752: assert(i==nextdyn);
1.128 anton 1753: (void)compile_prim_dyn(p,NULL);
1.125 anton 1754: nextstate = c->state_out;
1.107 anton 1755: }
1.125 anton 1756: assert(nextstate==CANONICAL_STATE);
1.107 anton 1757: }
1.162 pazsan 1758: #endif
1.107 anton 1759:
1.105 anton 1760: /* compile *start, possibly rewriting it into a static and/or dynamic
1761: superinstruction */
1762: void compile_prim1(Cell *start)
1.70 anton 1763: {
1.108 anton 1764: #if defined(DOUBLY_INDIRECT)
1.125 anton 1765: Label prim;
1766:
1767: if (start==NULL)
1768: return;
1769: prim = (Label)*start;
1.108 anton 1770: if (prim<((Label)(xts+DOESJUMP)) || prim>((Label)(xts+npriminfos))) {
1771: fprintf(stderr,"compile_prim encountered xt %p\n", prim);
1772: *start=(Cell)prim;
1773: return;
1774: } else {
1775: *start = (Cell)(prim-((Label)xts)+((Label)vm_prims));
1776: return;
1777: }
1778: #elif defined(INDIRECT_THREADED)
1779: return;
1.112 anton 1780: #else /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
1.128 anton 1781: /* !! does not work, for unknown reasons; but something like this is
1782: probably needed to ensure that we don't call compile_prim_dyn
1783: before the inline arguments are there */
1784: static Cell *instps[MAX_BB];
1785: static PrimNum origs[MAX_BB];
1786: static int ninsts=0;
1787: PrimNum prim_num;
1788:
1789: if (start==NULL || ninsts >= MAX_BB ||
1790: (ninsts>0 && superend[origs[ninsts-1]])) {
1791: /* after bb, or at the start of the next bb */
1792: optimize_rewrite(instps,origs,ninsts);
1793: /* fprintf(stderr,"optimize_rewrite(...,%d)\n",ninsts); */
1794: ninsts=0;
1.185 anton 1795: if (start==NULL) {
1796: align_code();
1.128 anton 1797: return;
1.185 anton 1798: }
1.128 anton 1799: }
1800: prim_num = ((Xt)*start)-vm_prims;
1801: if(prim_num >= npriminfos) {
1802: optimize_rewrite(instps,origs,ninsts);
1.129 anton 1803: /* fprintf(stderr,"optimize_rewrite(...,%d)\n",ninsts);*/
1.128 anton 1804: ninsts=0;
1805: return;
1806: }
1807: assert(ninsts<MAX_BB);
1808: instps[ninsts] = start;
1809: origs[ninsts] = prim_num;
1810: ninsts++;
1.112 anton 1811: #endif /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
1.47 anton 1812: }
1813:
1.176 pazsan 1814: #ifndef STANDALONE
1.161 pazsan 1815: Address gforth_loader(FILE *imagefile, char* filename)
1.1 anton 1816: /* returns the address of the image proper (after the preamble) */
1817: {
1818: ImageHeader header;
1819: Address image;
1820: Address imp; /* image+preamble */
1.17 anton 1821: Char magic[8];
1822: char magic7; /* size byte of magic number */
1.1 anton 1823: Cell preamblesize=0;
1.6 pazsan 1824: Cell data_offset = offset_image ? 56*sizeof(Cell) : 0;
1.1 anton 1825: UCell check_sum;
1.15 pazsan 1826: Cell ausize = ((RELINFOBITS == 8) ? 0 :
1827: (RELINFOBITS == 16) ? 1 :
1828: (RELINFOBITS == 32) ? 2 : 3);
1829: Cell charsize = ((sizeof(Char) == 1) ? 0 :
1830: (sizeof(Char) == 2) ? 1 :
1831: (sizeof(Char) == 4) ? 2 : 3) + ausize;
1832: Cell cellsize = ((sizeof(Cell) == 1) ? 0 :
1833: (sizeof(Cell) == 2) ? 1 :
1834: (sizeof(Cell) == 4) ? 2 : 3) + ausize;
1.21 anton 1835: Cell sizebyte = (ausize << 5) + (charsize << 3) + (cellsize << 1) +
1836: #ifdef WORDS_BIGENDIAN
1837: 0
1838: #else
1839: 1
1840: #endif
1841: ;
1.1 anton 1842:
1.197 anton 1843: vm_prims = gforth_engine(0,0,0,0,0 sr_call);
1.47 anton 1844: check_prims(vm_prims);
1.106 anton 1845: prepare_super_table();
1.1 anton 1846: #ifndef DOUBLY_INDIRECT
1.59 anton 1847: #ifdef PRINT_SUPER_LENGTHS
1848: print_super_lengths();
1849: #endif
1.43 anton 1850: check_sum = checksum(vm_prims);
1.1 anton 1851: #else /* defined(DOUBLY_INDIRECT) */
1.43 anton 1852: check_sum = (UCell)vm_prims;
1.1 anton 1853: #endif /* defined(DOUBLY_INDIRECT) */
1.155 anton 1854: #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
1855: termstate = make_termstate();
1856: #endif /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
1.10 pazsan 1857:
1858: do {
1859: if(fread(magic,sizeof(Char),8,imagefile) < 8) {
1.84 anton 1860: fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.6) image.\n",
1.10 pazsan 1861: progname, filename);
1862: exit(1);
1.1 anton 1863: }
1.10 pazsan 1864: preamblesize+=8;
1.84 anton 1865: } while(memcmp(magic,"Gforth3",7));
1.17 anton 1866: magic7 = magic[7];
1.1 anton 1867: if (debug) {
1.17 anton 1868: magic[7]='\0';
1.21 anton 1869: fprintf(stderr,"Magic found: %s ", magic);
1870: print_sizes(magic7);
1.1 anton 1871: }
1872:
1.21 anton 1873: if (magic7 != sizebyte)
1874: {
1875: fprintf(stderr,"This image is: ");
1876: print_sizes(magic7);
1877: fprintf(stderr,"whereas the machine is ");
1878: print_sizes(sizebyte);
1.1 anton 1879: exit(-2);
1880: };
1881:
1882: fread((void *)&header,sizeof(ImageHeader),1,imagefile);
1.10 pazsan 1883:
1884: set_stack_sizes(&header);
1.1 anton 1885:
1886: #if HAVE_GETPAGESIZE
1887: pagesize=getpagesize(); /* Linux/GNU libc offers this */
1888: #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
1889: pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
1890: #elif PAGESIZE
1891: pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
1892: #endif
1.144 pazsan 1893: debugp(stderr,"pagesize=%ld\n",(unsigned long) pagesize);
1.1 anton 1894:
1.34 anton 1895: image = dict_alloc_read(imagefile, preamblesize+header.image_size,
1896: preamblesize+dictsize, data_offset);
1.33 anton 1897: imp=image+preamblesize;
1.178 pazsan 1898:
1.57 anton 1899: alloc_stacks((ImageHeader *)imp);
1.1 anton 1900: if (clear_dictionary)
1.33 anton 1901: memset(imp+header.image_size, 0, dictsize-header.image_size);
1.90 anton 1902: if(header.base==0 || header.base == (Address)0x100) {
1.1 anton 1903: Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
1.162 pazsan 1904: Char reloc_bits[reloc_size];
1.33 anton 1905: fseek(imagefile, preamblesize+header.image_size, SEEK_SET);
1.10 pazsan 1906: fread(reloc_bits, 1, reloc_size, imagefile);
1.161 pazsan 1907: gforth_relocate((Cell *)imp, reloc_bits, header.image_size, (Cell)header.base, vm_prims);
1.1 anton 1908: #if 0
1909: { /* let's see what the relocator did */
1910: FILE *snapshot=fopen("snapshot.fi","wb");
1911: fwrite(image,1,imagesize,snapshot);
1912: fclose(snapshot);
1913: }
1914: #endif
1.46 jwilke 1915: }
1916: else if(header.base!=imp) {
1917: fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
1918: progname, (unsigned long)header.base, (unsigned long)imp);
1919: exit(1);
1.1 anton 1920: }
1921: if (header.checksum==0)
1922: ((ImageHeader *)imp)->checksum=check_sum;
1923: else if (header.checksum != check_sum) {
1924: fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
1925: progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
1926: exit(1);
1927: }
1.53 anton 1928: #ifdef DOUBLY_INDIRECT
1929: ((ImageHeader *)imp)->xt_base = xts;
1930: #endif
1.1 anton 1931: fclose(imagefile);
1932:
1.56 anton 1933: /* unnecessary, except maybe for CODE words */
1934: /* FLUSH_ICACHE(imp, header.image_size);*/
1.1 anton 1935:
1936: return imp;
1937: }
1.176 pazsan 1938: #endif
1.1 anton 1939:
1.72 anton 1940: /* pointer to last '/' or '\' in file, 0 if there is none. */
1.161 pazsan 1941: static char *onlypath(char *filename)
1.10 pazsan 1942: {
1.72 anton 1943: return strrchr(filename, DIRSEP);
1.1 anton 1944: }
1945:
1.161 pazsan 1946: static FILE *openimage(char *fullfilename)
1.10 pazsan 1947: {
1948: FILE *image_file;
1.162 pazsan 1949: char * expfilename = tilde_cstr((Char *)fullfilename, strlen(fullfilename), 1);
1.10 pazsan 1950:
1.28 anton 1951: image_file=fopen(expfilename,"rb");
1.1 anton 1952: if (image_file!=NULL && debug)
1.28 anton 1953: fprintf(stderr, "Opened image file: %s\n", expfilename);
1.10 pazsan 1954: return image_file;
1.1 anton 1955: }
1956:
1.28 anton 1957: /* try to open image file concat(path[0:len],imagename) */
1.161 pazsan 1958: static FILE *checkimage(char *path, int len, char *imagename)
1.10 pazsan 1959: {
1960: int dirlen=len;
1.162 pazsan 1961: char fullfilename[dirlen+strlen((char *)imagename)+2];
1.10 pazsan 1962:
1.1 anton 1963: memcpy(fullfilename, path, dirlen);
1.71 pazsan 1964: if (fullfilename[dirlen-1]!=DIRSEP)
1965: fullfilename[dirlen++]=DIRSEP;
1.1 anton 1966: strcpy(fullfilename+dirlen,imagename);
1.10 pazsan 1967: return openimage(fullfilename);
1.1 anton 1968: }
1969:
1.161 pazsan 1970: static FILE * open_image_file(char * imagename, char * path)
1.1 anton 1971: {
1.10 pazsan 1972: FILE * image_file=NULL;
1.28 anton 1973: char *origpath=path;
1.10 pazsan 1974:
1.71 pazsan 1975: if(strchr(imagename, DIRSEP)==NULL) {
1.10 pazsan 1976: /* first check the directory where the exe file is in !! 01may97jaw */
1977: if (onlypath(progname))
1.72 anton 1978: image_file=checkimage(progname, onlypath(progname)-progname, imagename);
1.10 pazsan 1979: if (!image_file)
1980: do {
1981: char *pend=strchr(path, PATHSEP);
1982: if (pend==NULL)
1983: pend=path+strlen(path);
1984: if (strlen(path)==0) break;
1985: image_file=checkimage(path, pend-path, imagename);
1986: path=pend+(*pend==PATHSEP);
1987: } while (image_file==NULL);
1988: } else {
1989: image_file=openimage(imagename);
1990: }
1.1 anton 1991:
1.10 pazsan 1992: if (!image_file) {
1993: fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
1.28 anton 1994: progname, imagename, origpath);
1.10 pazsan 1995: exit(1);
1.7 anton 1996: }
1997:
1.10 pazsan 1998: return image_file;
1999: }
1.11 pazsan 2000: #endif
2001:
1.178 pazsan 2002: #ifdef STANDALONE_ALLOC
1.177 pazsan 2003: Address gforth_alloc(Cell size)
2004: {
2005: Address r;
2006: /* leave a little room (64B) for stack underflows */
2007: if ((r = malloc(size+64))==NULL) {
2008: perror(progname);
2009: exit(1);
2010: }
2011: r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
2012: debugp(stderr, "malloc succeeds, address=$%lx\n", (long)r);
2013: return r;
2014: }
2015: #endif
2016:
1.11 pazsan 2017: #ifdef HAS_OS
1.161 pazsan 2018: static UCell convsize(char *s, UCell elemsize)
1.11 pazsan 2019: /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
2020: of bytes. the letter at the end indicates the unit, where e stands
2021: for the element size. default is e */
2022: {
2023: char *endp;
2024: UCell n,m;
2025:
2026: m = elemsize;
2027: n = strtoul(s,&endp,0);
2028: if (endp!=NULL) {
2029: if (strcmp(endp,"b")==0)
2030: m=1;
2031: else if (strcmp(endp,"k")==0)
2032: m=1024;
2033: else if (strcmp(endp,"M")==0)
2034: m=1024*1024;
2035: else if (strcmp(endp,"G")==0)
2036: m=1024*1024*1024;
2037: else if (strcmp(endp,"T")==0) {
2038: #if (SIZEOF_CHAR_P > 4)
1.24 anton 2039: m=1024L*1024*1024*1024;
1.11 pazsan 2040: #else
2041: fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp);
2042: exit(1);
2043: #endif
2044: } else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
2045: fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
2046: exit(1);
2047: }
2048: }
2049: return n*m;
2050: }
1.10 pazsan 2051:
1.109 anton 2052: enum {
2053: ss_number = 256,
1.125 anton 2054: ss_states,
1.109 anton 2055: ss_min_codesize,
2056: ss_min_ls,
2057: ss_min_lsu,
2058: ss_min_nexts,
2059: };
2060:
1.179 pazsan 2061: #ifndef STANDALONE
1.10 pazsan 2062: void gforth_args(int argc, char ** argv, char ** path, char ** imagename)
2063: {
2064: int c;
2065:
1.1 anton 2066: opterr=0;
2067: while (1) {
2068: int option_index=0;
2069: static struct option opts[] = {
1.29 anton 2070: {"appl-image", required_argument, NULL, 'a'},
1.1 anton 2071: {"image-file", required_argument, NULL, 'i'},
2072: {"dictionary-size", required_argument, NULL, 'm'},
2073: {"data-stack-size", required_argument, NULL, 'd'},
2074: {"return-stack-size", required_argument, NULL, 'r'},
2075: {"fp-stack-size", required_argument, NULL, 'f'},
2076: {"locals-stack-size", required_argument, NULL, 'l'},
1.181 anton 2077: {"vm-commit", no_argument, &map_noreserve, 0},
1.1 anton 2078: {"path", required_argument, NULL, 'p'},
2079: {"version", no_argument, NULL, 'v'},
2080: {"help", no_argument, NULL, 'h'},
2081: /* put something != 0 into offset_image */
2082: {"offset-image", no_argument, &offset_image, 1},
2083: {"no-offset-im", no_argument, &offset_image, 0},
2084: {"clear-dictionary", no_argument, &clear_dictionary, 1},
1.201 anton 2085: {"debug", no_argument, &debug, 1},
2086: {"diag", no_argument, &diag, 1},
1.4 anton 2087: {"die-on-signal", no_argument, &die_on_signal, 1},
1.169 anton 2088: {"ignore-async-signals", no_argument, &ignore_async_signals, 1},
1.60 anton 2089: {"no-super", no_argument, &no_super, 1},
2090: {"no-dynamic", no_argument, &no_dynamic, 1},
1.66 anton 2091: {"dynamic", no_argument, &no_dynamic, 0},
1.110 anton 2092: {"print-metrics", no_argument, &print_metrics, 1},
1.189 anton 2093: {"print-sequences", no_argument, &print_sequences, 1},
1.109 anton 2094: {"ss-number", required_argument, NULL, ss_number},
1.125 anton 2095: {"ss-states", required_argument, NULL, ss_states},
1.109 anton 2096: #ifndef NO_DYNAMIC
2097: {"ss-min-codesize", no_argument, NULL, ss_min_codesize},
2098: #endif
2099: {"ss-min-ls", no_argument, NULL, ss_min_ls},
2100: {"ss-min-lsu", no_argument, NULL, ss_min_lsu},
2101: {"ss-min-nexts", no_argument, NULL, ss_min_nexts},
1.110 anton 2102: {"ss-greedy", no_argument, &ss_greedy, 1},
1.158 anton 2103: {"tpa-noequiv", no_argument, &tpa_noequiv, 1},
2104: {"tpa-noautomaton", no_argument, &tpa_noautomaton, 1},
2105: {"tpa-trace", no_argument, &tpa_trace, 1},
1.1 anton 2106: {0,0,0,0}
2107: /* no-init-file, no-rc? */
2108: };
2109:
1.36 pazsan 2110: c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vhoncsx", opts, &option_index);
1.1 anton 2111:
2112: switch (c) {
1.29 anton 2113: case EOF: return;
2114: case '?': optind--; return;
2115: case 'a': *imagename = optarg; return;
1.10 pazsan 2116: case 'i': *imagename = optarg; break;
1.1 anton 2117: case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
2118: case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
2119: case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
2120: case 'f': fsize = convsize(optarg,sizeof(Float)); break;
2121: case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
1.10 pazsan 2122: case 'p': *path = optarg; break;
1.36 pazsan 2123: case 'o': offset_image = 1; break;
2124: case 'n': offset_image = 0; break;
2125: case 'c': clear_dictionary = 1; break;
2126: case 's': die_on_signal = 1; break;
2127: case 'x': debug = 1; break;
1.83 anton 2128: case 'v': fputs(PACKAGE_STRING"\n", stderr); exit(0);
1.109 anton 2129: case ss_number: static_super_number = atoi(optarg); break;
1.125 anton 2130: case ss_states: maxstates = max(min(atoi(optarg),MAX_STATE),1); break;
1.109 anton 2131: #ifndef NO_DYNAMIC
2132: case ss_min_codesize: ss_cost = cost_codesize; break;
2133: #endif
2134: case ss_min_ls: ss_cost = cost_ls; break;
2135: case ss_min_lsu: ss_cost = cost_lsu; break;
2136: case ss_min_nexts: ss_cost = cost_nexts; break;
1.1 anton 2137: case 'h':
1.29 anton 2138: fprintf(stderr, "Usage: %s [engine options] ['--'] [image arguments]\n\
1.1 anton 2139: Engine Options:\n\
1.181 anton 2140: --appl-image FILE Equivalent to '--image-file=FILE --'\n\
1.10 pazsan 2141: --clear-dictionary Initialize the dictionary with 0 bytes\n\
2142: -d SIZE, --data-stack-size=SIZE Specify data stack size\n\
2143: --debug Print debugging information during startup\n\
1.144 pazsan 2144: --diag Print diagnostic information during startup\n\
1.181 anton 2145: --die-on-signal Exit instead of THROWing some signals\n\
2146: --dynamic Use dynamic native code\n\
1.10 pazsan 2147: -f SIZE, --fp-stack-size=SIZE Specify floating point stack size\n\
2148: -h, --help Print this message and exit\n\
1.181 anton 2149: --ignore-async-signals Ignore instead of THROWing async. signals\n\
1.10 pazsan 2150: -i FILE, --image-file=FILE Use image FILE instead of `gforth.fi'\n\
2151: -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
2152: -m SIZE, --dictionary-size=SIZE Specify Forth dictionary size\n\
1.60 anton 2153: --no-dynamic Use only statically compiled primitives\n\
1.10 pazsan 2154: --no-offset-im Load image at normal position\n\
1.181 anton 2155: --no-super No dynamically formed superinstructions\n\
1.10 pazsan 2156: --offset-image Load image at a different position\n\
2157: -p PATH, --path=PATH Search path for finding image and sources\n\
1.110 anton 2158: --print-metrics Print some code generation metrics on exit\n\
1.201 anton 2159: --print-sequences Print primitive sequences for optimization\n\
1.10 pazsan 2160: -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
1.181 anton 2161: --ss-greedy Greedy, not optimal superinst selection\n\
2162: --ss-min-codesize Select superinsts for smallest native code\n\
2163: --ss-min-ls Minimize loads and stores\n\
2164: --ss-min-lsu Minimize loads, stores, and pointer updates\n\
2165: --ss-min-nexts Minimize the number of static superinsts\n\
2166: --ss-number=N Use N static superinsts (default max)\n\
2167: --ss-states=N N states for stack caching (default max)\n\
2168: --tpa-noequiv Automaton without state equivalence\n\
2169: --tpa-noautomaton Dynamic programming only\n\
2170: --tpa-trace Report new states etc.\n\
1.66 anton 2171: -v, --version Print engine version and exit\n\
1.181 anton 2172: --vm-commit Use OS default for memory overcommit\n\
1.1 anton 2173: SIZE arguments consist of an integer followed by a unit. The unit can be\n\
1.10 pazsan 2174: `b' (byte), `e' (element; default), `k' (KB), `M' (MB), `G' (GB) or `T' (TB).\n",
2175: argv[0]);
2176: optind--;
2177: return;
1.1 anton 2178: }
2179: }
1.10 pazsan 2180: }
1.11 pazsan 2181: #endif
1.179 pazsan 2182: #endif
1.10 pazsan 2183:
1.161 pazsan 2184: static void print_diag()
1.144 pazsan 2185: {
2186:
1.207 ! pazsan 2187: #if !defined(HAVE_GETRUSAGE)
1.145 pazsan 2188: fprintf(stderr, "*** missing functionality ***\n"
1.144 pazsan 2189: #ifndef HAVE_GETRUSAGE
2190: " no getrusage -> CPUTIME broken\n"
2191: #endif
2192: );
2193: #endif
2194: if((relocs < nonrelocs) ||
2195: #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)
2196: 1
2197: #else
2198: 0
2199: #endif
2200: )
2201: debugp(stderr, "relocs: %d:%d\n", relocs, nonrelocs);
1.165 pazsan 2202: fprintf(stderr, "*** %sperformance problems ***\n%s",
1.204 anton 2203: #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) || !(defined(FORCE_REG) || defined(FORCE_REG_UNNECESSARY)) || defined(BUGGY_LONG_LONG)
1.165 pazsan 2204: "",
2205: #else
2206: "no ",
2207: #endif
1.144 pazsan 2208: #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)
2209: " double-cell integer type buggy ->\n "
2210: #ifdef BUGGY_LL_CMP
2211: "CMP, "
2212: #endif
2213: #ifdef BUGGY_LL_MUL
2214: "MUL, "
2215: #endif
2216: #ifdef BUGGY_LL_DIV
2217: "DIV, "
2218: #endif
2219: #ifdef BUGGY_LL_ADD
2220: "ADD, "
2221: #endif
2222: #ifdef BUGGY_LL_SHIFT
2223: "SHIFT, "
2224: #endif
2225: #ifdef BUGGY_LL_D2F
2226: "D2F, "
2227: #endif
2228: #ifdef BUGGY_LL_F2D
2229: "F2D, "
2230: #endif
2231: "\b\b slow\n"
1.145 pazsan 2232: #endif
1.200 anton 2233: #if !(defined(FORCE_REG) || defined(FORCE_REG_UNNECESSARY))
1.145 pazsan 2234: " automatic register allocation: performance degradation possible\n"
2235: #endif
1.198 anton 2236: "",
1.144 pazsan 2237: (relocs < nonrelocs) ? " gcc PR 15242 -> no dynamic code generation (use gcc-2.95 instead)\n" : "");
2238: }
2239:
1.179 pazsan 2240: #ifdef STANDALONE
2241: Cell data_abort_pc;
2242:
2243: void data_abort_C(void)
2244: {
2245: while(1) {
2246: }
2247: }
1.10 pazsan 2248: #endif
1.67 pazsan 2249:
1.10 pazsan 2250: int main(int argc, char **argv, char **env)
2251: {
1.30 pazsan 2252: #ifdef HAS_OS
1.10 pazsan 2253: char *path = getenv("GFORTHPATH") ? : DEFAULTPATH;
1.30 pazsan 2254: #else
2255: char *path = DEFAULTPATH;
2256: #endif
1.13 pazsan 2257: #ifndef INCLUDE_IMAGE
1.10 pazsan 2258: char *imagename="gforth.fi";
2259: FILE *image_file;
2260: Address image;
2261: #endif
2262: int retvalue;
2263:
1.179 pazsan 2264: #ifndef STANDALONE
1.10 pazsan 2265: /* buffering of the user output device */
1.11 pazsan 2266: #ifdef _IONBF
1.10 pazsan 2267: if (isatty(fileno(stdout))) {
2268: fflush(stdout);
2269: setvbuf(stdout,NULL,_IONBF,0);
1.1 anton 2270: }
1.11 pazsan 2271: #endif
1.180 pazsan 2272: #else
2273: prep_terminal();
1.179 pazsan 2274: #endif
1.1 anton 2275:
1.10 pazsan 2276: progname = argv[0];
2277:
1.199 pazsan 2278: #ifndef STANDALONE
1.191 anton 2279: if (lt_dlinit()!=0) {
2280: fprintf(stderr,"%s: lt_dlinit failed", progname);
2281: exit(1);
2282: }
1.203 anton 2283:
1.11 pazsan 2284: #ifdef HAS_OS
1.10 pazsan 2285: gforth_args(argc, argv, &path, &imagename);
1.109 anton 2286: #ifndef NO_DYNAMIC
1.148 anton 2287: init_ss_cost();
1.109 anton 2288: #endif /* !defined(NO_DYNAMIC) */
2289: #endif /* defined(HAS_OS) */
1.179 pazsan 2290: #endif
1.10 pazsan 2291:
1.175 pazsan 2292: #ifdef STANDALONE
1.197 anton 2293: image = gforth_engine(0, 0, 0, 0, 0 sr_call);
1.10 pazsan 2294: alloc_stacks((ImageHeader *)image);
2295: #else
2296: image_file = open_image_file(imagename, path);
1.161 pazsan 2297: image = gforth_loader(image_file, imagename);
1.10 pazsan 2298: #endif
1.24 anton 2299: gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */
1.1 anton 2300:
1.144 pazsan 2301: if (diag)
2302: print_diag();
1.1 anton 2303: {
1.10 pazsan 2304: char path2[strlen(path)+1];
1.1 anton 2305: char *p1, *p2;
2306: Cell environ[]= {
2307: (Cell)argc-(optind-1),
2308: (Cell)(argv+(optind-1)),
1.10 pazsan 2309: (Cell)strlen(path),
1.1 anton 2310: (Cell)path2};
2311: argv[optind-1] = progname;
2312: /*
2313: for (i=0; i<environ[0]; i++)
2314: printf("%s\n", ((char **)(environ[1]))[i]);
2315: */
2316: /* make path OS-independent by replacing path separators with NUL */
1.10 pazsan 2317: for (p1=path, p2=path2; *p1!='\0'; p1++, p2++)
1.1 anton 2318: if (*p1==PATHSEP)
2319: *p2 = '\0';
2320: else
2321: *p2 = *p1;
2322: *p2='\0';
1.161 pazsan 2323: retvalue = gforth_go(image, 4, environ);
1.178 pazsan 2324: #if defined(SIGPIPE) && !defined(STANDALONE)
1.102 anton 2325: bsd_signal(SIGPIPE, SIG_IGN);
2326: #endif
1.42 anton 2327: #ifdef VM_PROFILING
2328: vm_print_profile(stderr);
2329: #endif
1.1 anton 2330: deprep_terminal();
1.199 pazsan 2331: #ifndef STANDALONE
1.191 anton 2332: if (lt_dlexit()!=0)
2333: fprintf(stderr,"%s: lt_dlexit failed", progname);
1.199 pazsan 2334: #endif
1.104 anton 2335: }
1.110 anton 2336: if (print_metrics) {
2337: int i;
2338: fprintf(stderr, "code size = %8ld\n", dyncodesize());
1.177 pazsan 2339: #ifndef STANDALONE
1.110 anton 2340: for (i=0; i<sizeof(cost_sums)/sizeof(cost_sums[0]); i++)
2341: fprintf(stderr, "metric %8s: %8ld\n",
2342: cost_sums[i].metricname, cost_sums[i].sum);
1.177 pazsan 2343: #endif
1.158 anton 2344: fprintf(stderr,"lb_basic_blocks = %ld\n", lb_basic_blocks);
2345: fprintf(stderr,"lb_labeler_steps = %ld\n", lb_labeler_steps);
2346: fprintf(stderr,"lb_labeler_automaton = %ld\n", lb_labeler_automaton);
2347: fprintf(stderr,"lb_labeler_dynprog = %ld\n", lb_labeler_dynprog);
2348: fprintf(stderr,"lb_newstate_equiv = %ld\n", lb_newstate_equiv);
2349: fprintf(stderr,"lb_newstate_new = %ld\n", lb_newstate_new);
2350: fprintf(stderr,"lb_applicable_base_rules = %ld\n", lb_applicable_base_rules);
2351: fprintf(stderr,"lb_applicable_chain_rules = %ld\n", lb_applicable_chain_rules);
2352: }
2353: if (tpa_trace) {
2354: fprintf(stderr, "%ld %ld lb_states\n", lb_labeler_steps, lb_newstate_new);
2355: fprintf(stderr, "%ld %ld lb_table_entries\n", lb_labeler_steps, lb_labeler_dynprog);
1.1 anton 2356: }
1.13 pazsan 2357: return retvalue;
1.1 anton 2358: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>