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