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