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