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