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