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