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