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