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