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