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