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