1: /* command line interpretation, image loading etc. for Gforth
2:
3:
4: Copyright (C) 1995,1996,1997,1998,2000,2003 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 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
20: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
21: */
22:
23: #include "config.h"
24: #include "forth.h"
25: #include <errno.h>
26: #include <ctype.h>
27: #include <stdio.h>
28: #include <unistd.h>
29: #include <string.h>
30: #include <math.h>
31: #include <sys/types.h>
32: #ifndef STANDALONE
33: #include <sys/stat.h>
34: #endif
35: #include <fcntl.h>
36: #include <assert.h>
37: #include <stdlib.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: /* global variables for engine.c
51: We put them here because engine.c is compiled several times in
52: different ways for the same engine. */
53: Cell *SP;
54: Float *FP;
55: Address UP=NULL;
56:
57: #ifdef HAS_FFCALL
58: Cell *RP;
59: Address LP;
60:
61: #include <callback.h>
62:
63: va_alist clist;
64:
65: void engine_callback(Xt* fcall, void * alist)
66: {
67: engine(fcall, SP, RP, FP, LP);
68: clist = (va_alist)alist;
69: }
70: #endif
71:
72: #ifdef GFORTH_DEBUGGING
73: /* define some VM registers as global variables, so they survive exceptions;
74: global register variables are not up to the task (according to the
75: GNU C manual) */
76: Xt *saved_ip;
77: Cell *rp;
78: #endif
79:
80: #ifdef NO_IP
81: Label next_code;
82: #endif
83:
84: #ifdef HAS_FILE
85: char* fileattr[6]={"rb","rb","r+b","r+b","wb","wb"};
86: char* pfileattr[6]={"r","r","r+","r+","w","w"};
87:
88: #ifndef O_BINARY
89: #define O_BINARY 0
90: #endif
91: #ifndef O_TEXT
92: #define O_TEXT 0
93: #endif
94:
95: int ufileattr[6]= {
96: O_RDONLY|O_BINARY, O_RDONLY|O_BINARY,
97: O_RDWR |O_BINARY, O_RDWR |O_BINARY,
98: O_WRONLY|O_BINARY, O_WRONLY|O_BINARY };
99: #endif
100: /* end global vars for engine.c */
101:
102: #define PRIM_VERSION 1
103: /* increment this whenever the primitives change in an incompatible way */
104:
105: #ifndef DEFAULTPATH
106: # define DEFAULTPATH "."
107: #endif
108:
109: #ifdef MSDOS
110: jmp_buf throw_jmp_buf;
111: #endif
112:
113: #if defined(DOUBLY_INDIRECT)
114: # define CFA(n) ({Cell _n = (n); ((Cell)(((_n & 0x4000) ? symbols : xts)+(_n&~0x4000UL)));})
115: #else
116: # define CFA(n) ((Cell)(symbols+((n)&~0x4000UL)))
117: #endif
118:
119: #define maxaligned(n) (typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
120:
121: static UCell dictsize=0;
122: static UCell dsize=0;
123: static UCell rsize=0;
124: static UCell fsize=0;
125: static UCell lsize=0;
126: int offset_image=0;
127: int die_on_signal=0;
128: #ifndef INCLUDE_IMAGE
129: static int clear_dictionary=0;
130: UCell pagesize=1;
131: char *progname;
132: #else
133: char *progname = "gforth";
134: int optind = 1;
135: #endif
136:
137: #define CODE_BLOCK_SIZE (256*1024)
138: Address code_area=0;
139: Cell code_area_size = CODE_BLOCK_SIZE;
140: Address code_here=NULL+CODE_BLOCK_SIZE; /* does for code-area what HERE
141: does for the dictionary */
142: Address start_flush=NULL; /* start of unflushed code */
143: Cell last_jump=0; /* if the last prim was compiled without jump, this
144: is it's number, otherwise this contains 0 */
145:
146: static int no_super=0; /* true if compile_prim should not fuse prims */
147: static int no_dynamic=NO_DYNAMIC_DEFAULT; /* if true, no code is generated
148: dynamically */
149: static int print_metrics=0; /* if true, print metrics on exit */
150: static int static_super_number = 10000000; /* number of ss used if available */
151: static int ss_greedy = 0; /* if true: use greedy, not optimal ss selection */
152:
153: #ifdef HAS_DEBUG
154: int debug=0;
155: #else
156: # define perror(x...)
157: # define fprintf(x...)
158: #endif
159:
160: ImageHeader *gforth_header;
161: Label *vm_prims;
162: #ifdef DOUBLY_INDIRECT
163: Label *xts; /* same content as vm_prims, but should only be used for xts */
164: #endif
165:
166: #ifdef MEMCMP_AS_SUBROUTINE
167: int gforth_memcmp(const char * s1, const char * s2, size_t n)
168: {
169: return memcmp(s1, s2, n);
170: }
171: #endif
172:
173: /* image file format:
174: * "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.4.0 -i\n")
175: * padding to a multiple of 8
176: * magic: "Gforth3x" means format 0.6,
177: * where x is a byte with
178: * bit 7: reserved = 0
179: * bit 6:5: address unit size 2^n octets
180: * bit 4:3: character size 2^n octets
181: * bit 2:1: cell size 2^n octets
182: * bit 0: endian, big=0, little=1.
183: * The magic are always 8 octets, no matter what the native AU/character size is
184: * padding to max alignment (no padding necessary on current machines)
185: * ImageHeader structure (see forth.h)
186: * data (size in ImageHeader.image_size)
187: * tags ((if relocatable, 1 bit/data cell)
188: *
189: * tag==1 means that the corresponding word is an address;
190: * If the word is >=0, the address is within the image;
191: * addresses within the image are given relative to the start of the image.
192: * If the word =-1 (CF_NIL), the address is NIL,
193: * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
194: * If the word =CF(DODOES), it's a DOES> CFA
195: * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
196: * possibly containing a jump to dodoes)
197: * If the word is <CF(DOESJUMP) and bit 14 is set, it's the xt of a primitive
198: * If the word is <CF(DOESJUMP) and bit 14 is clear,
199: * it's the threaded code of a primitive
200: * bits 13..9 of a primitive token state which group the primitive belongs to,
201: * bits 8..0 of a primitive token index into the group
202: */
203:
204: Cell groups[32] = {
205: 0,
206: DOESJUMP+1
207: #undef GROUP
208: #undef GROUPADD
209: #define GROUPADD(n) +n
210: #define GROUP(x, n) , 0
211: #include "prim_grp.i"
212: #undef GROUP
213: #undef GROUPADD
214: #define GROUP(x, n)
215: #define GROUPADD(n)
216: };
217:
218: void relocate(Cell *image, const unsigned char *bitstring,
219: int size, Cell base, Label symbols[])
220: {
221: int i=0, j, k, steps=(size/sizeof(Cell))/RELINFOBITS;
222: Cell token;
223: char bits;
224: Cell max_symbols;
225: /*
226: * A virtual start address that's the real start address minus
227: * the one in the image
228: */
229: Cell *start = (Cell * ) (((void *) image) - ((void *) base));
230:
231: /* group index into table */
232: if(groups[31]==0) {
233: int groupsum=0;
234: for(i=0; i<32; i++) {
235: groupsum += groups[i];
236: groups[i] = groupsum;
237: /* printf("group[%d]=%d\n",i,groupsum); */
238: }
239: i=0;
240: }
241:
242: /* printf("relocating to %x[%x] start=%x base=%x\n", image, size, start, base); */
243:
244: for (max_symbols=DOESJUMP+1; symbols[max_symbols]!=0; max_symbols++)
245: ;
246: max_symbols--;
247: size/=sizeof(Cell);
248:
249: for(k=0; k<=steps; k++) {
250: for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
251: /* fprintf(stderr,"relocate: image[%d]\n", i);*/
252: if((i < size) && (bits & (1U << (RELINFOBITS-1)))) {
253: /* fprintf(stderr,"relocate: image[%d]=%d of %d\n", i, image[i], size/sizeof(Cell)); */
254: token=image[i];
255: if(token<0) {
256: int group = (-token & 0x3E00) >> 9;
257: if(group == 0) {
258: switch(token|0x4000) {
259: case CF_NIL : image[i]=0; break;
260: #if !defined(DOUBLY_INDIRECT)
261: case CF(DOCOL) :
262: case CF(DOVAR) :
263: case CF(DOCON) :
264: case CF(DOUSER) :
265: case CF(DODEFER) :
266: case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(token)]); break;
267: case CF(DOESJUMP): image[i]=0; break;
268: #endif /* !defined(DOUBLY_INDIRECT) */
269: case CF(DODOES) :
270: MAKE_DOES_CF(image+i,(Xt *)(image[i+1]+((Cell)start)));
271: break;
272: default : /* backward compatibility */
273: /* printf("Code field generation image[%x]:=CFA(%x)\n",
274: i, CF(image[i])); */
275: if (CF((token | 0x4000))<max_symbols) {
276: image[i]=(Cell)CFA(CF(token));
277: #ifdef DIRECT_THREADED
278: if ((token & 0x4000) == 0) /* threade code, no CFA */
279: compile_prim1(&image[i]);
280: #endif
281: } else
282: 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);
283: }
284: } else {
285: int tok = -token & 0x1FF;
286: if (tok < (groups[group+1]-groups[group])) {
287: #if defined(DOUBLY_INDIRECT)
288: image[i]=(Cell)CFA(((groups[group]+tok) | (CF(token) & 0x4000)));
289: #else
290: image[i]=(Cell)CFA((groups[group]+tok));
291: #endif
292: #ifdef DIRECT_THREADED
293: if ((token & 0x4000) == 0) /* threade code, no CFA */
294: compile_prim1(&image[i]);
295: #endif
296: } else
297: 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);
298: }
299: } else {
300: /* if base is > 0: 0 is a null reference so don't adjust*/
301: if (token>=base) {
302: image[i]+=(Cell)start;
303: }
304: }
305: }
306: }
307: }
308: finish_code();
309: ((ImageHeader*)(image))->base = (Address) image;
310: }
311:
312: UCell checksum(Label symbols[])
313: {
314: UCell r=PRIM_VERSION;
315: Cell i;
316:
317: for (i=DOCOL; i<=DOESJUMP; i++) {
318: r ^= (UCell)(symbols[i]);
319: r = (r << 5) | (r >> (8*sizeof(Cell)-5));
320: }
321: #ifdef DIRECT_THREADED
322: /* we have to consider all the primitives */
323: for (; symbols[i]!=(Label)0; i++) {
324: r ^= (UCell)(symbols[i]);
325: r = (r << 5) | (r >> (8*sizeof(Cell)-5));
326: }
327: #else
328: /* in indirect threaded code all primitives are accessed through the
329: symbols table, so we just have to put the base address of symbols
330: in the checksum */
331: r ^= (UCell)symbols;
332: #endif
333: return r;
334: }
335:
336: Address verbose_malloc(Cell size)
337: {
338: Address r;
339: /* leave a little room (64B) for stack underflows */
340: if ((r = malloc(size+64))==NULL) {
341: perror(progname);
342: exit(1);
343: }
344: r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
345: if (debug)
346: fprintf(stderr, "malloc succeeds, address=$%lx\n", (long)r);
347: return r;
348: }
349:
350: static Address next_address=0;
351: void after_alloc(Address r, Cell size)
352: {
353: if (r != (Address)-1) {
354: if (debug)
355: fprintf(stderr, "success, address=$%lx\n", (long) r);
356: if (pagesize != 1)
357: next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */
358: } else {
359: if (debug)
360: fprintf(stderr, "failed: %s\n", strerror(errno));
361: }
362: }
363:
364: #ifndef MAP_FAILED
365: #define MAP_FAILED ((Address) -1)
366: #endif
367: #ifndef MAP_FILE
368: # define MAP_FILE 0
369: #endif
370: #ifndef MAP_PRIVATE
371: # define MAP_PRIVATE 0
372: #endif
373: #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
374: # define MAP_ANON MAP_ANONYMOUS
375: #endif
376:
377: #if defined(HAVE_MMAP)
378: static Address alloc_mmap(Cell size)
379: {
380: Address r;
381:
382: #if defined(MAP_ANON)
383: if (debug)
384: fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_ANON, ...); ", (long)next_address, (long)size);
385: r = mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
386: #else /* !defined(MAP_ANON) */
387: /* Ultrix (at least) does not define MAP_FILE and MAP_PRIVATE (both are
388: apparently defaults) */
389: static int dev_zero=-1;
390:
391: if (dev_zero == -1)
392: dev_zero = open("/dev/zero", O_RDONLY);
393: if (dev_zero == -1) {
394: r = MAP_FAILED;
395: if (debug)
396: fprintf(stderr, "open(\"/dev/zero\"...) failed (%s), no mmap; ",
397: strerror(errno));
398: } else {
399: if (debug)
400: fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FILE, dev_zero, ...); ", (long)next_address, (long)size);
401: r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE, dev_zero, 0);
402: }
403: #endif /* !defined(MAP_ANON) */
404: after_alloc(r, size);
405: return r;
406: }
407: #endif
408:
409: Address my_alloc(Cell size)
410: {
411: #if HAVE_MMAP
412: Address r;
413:
414: r=alloc_mmap(size);
415: if (r!=(Address)MAP_FAILED)
416: return r;
417: #endif /* HAVE_MMAP */
418: /* use malloc as fallback */
419: return verbose_malloc(size);
420: }
421:
422: Address dict_alloc_read(FILE *file, Cell imagesize, Cell dictsize, Cell offset)
423: {
424: Address image = MAP_FAILED;
425:
426: #if defined(HAVE_MMAP)
427: if (offset==0) {
428: image=alloc_mmap(dictsize);
429: if (debug)
430: fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FIXED|MAP_FILE, imagefile, 0); ", (long)image, (long)imagesize);
431: image = mmap(image, imagesize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FIXED|MAP_FILE|MAP_PRIVATE, fileno(file), 0);
432: after_alloc(image,dictsize);
433: }
434: #endif /* defined(HAVE_MMAP) */
435: if (image == (Address)MAP_FAILED) {
436: image = my_alloc(dictsize+offset)+offset;
437: rewind(file); /* fseek(imagefile,0L,SEEK_SET); */
438: fread(image, 1, imagesize, file);
439: }
440: return image;
441: }
442:
443: void set_stack_sizes(ImageHeader * header)
444: {
445: if (dictsize==0)
446: dictsize = header->dict_size;
447: if (dsize==0)
448: dsize = header->data_stack_size;
449: if (rsize==0)
450: rsize = header->return_stack_size;
451: if (fsize==0)
452: fsize = header->fp_stack_size;
453: if (lsize==0)
454: lsize = header->locals_stack_size;
455: dictsize=maxaligned(dictsize);
456: dsize=maxaligned(dsize);
457: rsize=maxaligned(rsize);
458: lsize=maxaligned(lsize);
459: fsize=maxaligned(fsize);
460: }
461:
462: void alloc_stacks(ImageHeader * header)
463: {
464: header->dict_size=dictsize;
465: header->data_stack_size=dsize;
466: header->fp_stack_size=fsize;
467: header->return_stack_size=rsize;
468: header->locals_stack_size=lsize;
469:
470: header->data_stack_base=my_alloc(dsize);
471: header->fp_stack_base=my_alloc(fsize);
472: header->return_stack_base=my_alloc(rsize);
473: header->locals_stack_base=my_alloc(lsize);
474: }
475:
476: #warning You can ignore the warnings about clobbered variables in go_forth
477: int go_forth(Address image, int stack, Cell *entries)
478: {
479: volatile ImageHeader *image_header = (ImageHeader *)image;
480: Cell *sp0=(Cell*)(image_header->data_stack_base + dsize);
481: Cell *rp0=(Cell *)(image_header->return_stack_base + rsize);
482: Float *fp0=(Float *)(image_header->fp_stack_base + fsize);
483: #ifdef GFORTH_DEBUGGING
484: volatile Cell *orig_rp0=rp0;
485: #endif
486: Address lp0=image_header->locals_stack_base + lsize;
487: Xt *ip0=(Xt *)(image_header->boot_entry);
488: #ifdef SYSSIGNALS
489: int throw_code;
490: #endif
491:
492: /* ensure that the cached elements (if any) are accessible */
493: IF_spTOS(sp0--);
494: IF_fpTOS(fp0--);
495:
496: for(;stack>0;stack--)
497: *--sp0=entries[stack-1];
498:
499: #ifdef SYSSIGNALS
500: get_winsize();
501:
502: install_signal_handlers(); /* right place? */
503:
504: if ((throw_code=setjmp(throw_jmp_buf))) {
505: static Cell signal_data_stack[8];
506: static Cell signal_return_stack[8];
507: static Float signal_fp_stack[1];
508:
509: signal_data_stack[7]=throw_code;
510:
511: #ifdef GFORTH_DEBUGGING
512: if (debug)
513: fprintf(stderr,"\ncaught signal, throwing exception %d, ip=%p rp=%p\n",
514: throw_code, saved_ip, rp);
515: if (rp <= orig_rp0 && rp > (Cell *)(image_header->return_stack_base+5)) {
516: /* no rstack overflow or underflow */
517: rp0 = rp;
518: *--rp0 = (Cell)saved_ip;
519: }
520: else /* I love non-syntactic ifdefs :-) */
521: rp0 = signal_return_stack+8;
522: #else /* !defined(GFORTH_DEBUGGING) */
523: if (debug)
524: fprintf(stderr,"\ncaught signal, throwing exception %d\n", throw_code);
525: rp0 = signal_return_stack+8;
526: #endif /* !defined(GFORTH_DEBUGGING) */
527: /* fprintf(stderr, "rp=$%x\n",rp0);*/
528:
529: return((int)(Cell)engine(image_header->throw_entry, signal_data_stack+7,
530: rp0, signal_fp_stack, 0));
531: }
532: #endif
533:
534: return((int)(Cell)engine(ip0,sp0,rp0,fp0,lp0));
535: }
536:
537: #ifndef INCLUDE_IMAGE
538: void print_sizes(Cell sizebyte)
539: /* print size information */
540: {
541: static char* endianstring[]= { " big","little" };
542:
543: fprintf(stderr,"%s endian, cell=%d bytes, char=%d bytes, au=%d bytes\n",
544: endianstring[sizebyte & 1],
545: 1 << ((sizebyte >> 1) & 3),
546: 1 << ((sizebyte >> 3) & 3),
547: 1 << ((sizebyte >> 5) & 3));
548: }
549:
550: /* static superinstruction stuff */
551:
552: struct cost {
553: char loads; /* number of stack loads */
554: char stores; /* number of stack stores */
555: char updates; /* number of stack pointer updates */
556: short offset; /* offset into super2 table */
557: char length; /* number of components */
558: };
559:
560: short super2[] = {
561: #include "super2.i"
562: };
563:
564: struct cost super_costs[] = {
565: #include "costs.i"
566: };
567:
568: #define HASH_SIZE 256
569:
570: struct super_table_entry {
571: struct super_table_entry *next;
572: short *start;
573: short length;
574: short super;
575: } *super_table[HASH_SIZE];
576: int max_super=2;
577:
578: int hash_super(short *start, int length)
579: {
580: int i, r;
581:
582: for (i=0, r=0; i<length; i++) {
583: r <<= 1;
584: r += start[i];
585: }
586: return r & (HASH_SIZE-1);
587: }
588:
589: int lookup_super(short *start, int length)
590: {
591: int hash=hash_super(start,length);
592: struct super_table_entry *p = super_table[hash];
593:
594: assert(length >= 2);
595: for (; p!=NULL; p = p->next) {
596: if (length == p->length &&
597: memcmp((char *)p->start, (char *)start, length*sizeof(short))==0)
598: return p->super;
599: }
600: return -1;
601: }
602:
603: void prepare_super_table()
604: {
605: int i;
606: int nsupers = 0;
607:
608: for (i=0; i<sizeof(super_costs)/sizeof(super_costs[0]); i++) {
609: struct cost *c = &super_costs[i];
610: if (c->length > 1 && nsupers < static_super_number) {
611: int hash = hash_super(super2+c->offset, c->length);
612: struct super_table_entry **p = &super_table[hash];
613: struct super_table_entry *e = malloc(sizeof(struct super_table_entry));
614: e->next = *p;
615: e->start = super2 + c->offset;
616: e->length = c->length;
617: e->super = i;
618: *p = e;
619: if (c->length > max_super)
620: max_super = c->length;
621: nsupers++;
622: }
623: }
624: if (debug)
625: fprintf(stderr, "Using %d static superinsts\n", nsupers);
626: }
627:
628: /* dynamic replication/superinstruction stuff */
629:
630: #define MAX_IMMARGS 2
631:
632: #ifndef NO_DYNAMIC
633: typedef struct {
634: Label start;
635: Cell length; /* only includes the jump iff superend is true*/
636: Cell restlength; /* length of the rest (i.e., the jump or (on superend) 0) */
637: char superend; /* true if primitive ends superinstruction, i.e.,
638: unconditional branch, execute, etc. */
639: Cell nimmargs;
640: struct immarg {
641: Cell offset; /* offset of immarg within prim */
642: char rel; /* true if immarg is relative */
643: } immargs[MAX_IMMARGS];
644: } PrimInfo;
645:
646: PrimInfo *priminfos;
647: PrimInfo **decomp_prims;
648:
649: int compare_priminfo_length(const void *_a, const void *_b)
650: {
651: PrimInfo **a = (PrimInfo **)_a;
652: PrimInfo **b = (PrimInfo **)_b;
653: Cell diff = (*a)->length - (*b)->length;
654: if (diff)
655: return diff;
656: else /* break ties by start address; thus the decompiler produces
657: the earliest primitive with the same code (e.g. noop instead
658: of (char) and @ instead of >code-address */
659: return (*b)->start - (*a)->start;
660: }
661: #endif /* !defined(NO_DYNAMIC) */
662:
663: static char superend[]={
664: #include "prim_superend.i"
665: };
666:
667: Cell npriminfos=0;
668:
669: int compare_labels(const void *pa, const void *pb)
670: {
671: Label a = *(Label *)pa;
672: Label b = *(Label *)pb;
673: return a-b;
674: }
675:
676: Label bsearch_next(Label key, Label *a, UCell n)
677: /* a is sorted; return the label >=key that is the closest in a;
678: return NULL if there is no label in a >=key */
679: {
680: int mid = (n-1)/2;
681: if (n<1)
682: return NULL;
683: if (n == 1) {
684: if (a[0] < key)
685: return NULL;
686: else
687: return a[0];
688: }
689: if (a[mid] < key)
690: return bsearch_next(key, a+mid+1, n-mid-1);
691: else
692: return bsearch_next(key, a, mid+1);
693: }
694:
695: void check_prims(Label symbols1[])
696: {
697: int i;
698: #ifndef NO_DYNAMIC
699: Label *symbols2, *symbols3, *ends1, *ends1k, *ends1ksorted;
700: int nends1k;
701: #endif
702:
703: if (debug)
704: #ifdef __VERSION__
705: fprintf(stderr, "Compiled with gcc-" __VERSION__ "\n");
706: #else
707: #define xstr(s) str(s)
708: #define str(s) #s
709: fprintf(stderr, "Compiled with gcc-" xstr(__GNUC__) "." xstr(__GNUC_MINOR__) "\n");
710: #endif
711: for (i=DOESJUMP+1; symbols1[i+1]!=0; i++)
712: ;
713: npriminfos = i;
714:
715: #ifndef NO_DYNAMIC
716: if (no_dynamic)
717: return;
718: symbols2=engine2(0,0,0,0,0);
719: #if NO_IP
720: symbols3=engine3(0,0,0,0,0);
721: #else
722: symbols3=symbols1;
723: #endif
724: ends1 = symbols1+i+1-DOESJUMP;
725: ends1k = ends1+i+1-DOESJUMP;
726: nends1k = i+1-DOESJUMP;
727: ends1ksorted = (Label *)alloca(nends1k*sizeof(Label));
728: memcpy(ends1ksorted,ends1k,nends1k*sizeof(Label));
729: qsort(ends1ksorted, nends1k, sizeof(Label), compare_labels);
730:
731: priminfos = calloc(i,sizeof(PrimInfo));
732: for (i=DOESJUMP+1; symbols1[i+1]!=0; i++) {
733: int prim_len = ends1[i]-symbols1[i];
734: PrimInfo *pi=&priminfos[i];
735: int j=0;
736: char *s1 = (char *)symbols1[i];
737: char *s2 = (char *)symbols2[i];
738: char *s3 = (char *)symbols3[i];
739: Label endlabel = bsearch_next(symbols1[i]+1,ends1ksorted,nends1k);
740:
741: pi->start = s1;
742: pi->superend = superend[i-DOESJUMP-1]|no_super;
743: if (pi->superend)
744: pi->length = endlabel-symbols1[i];
745: else
746: pi->length = prim_len;
747: pi->restlength = endlabel - symbols1[i] - pi->length;
748: pi->nimmargs = 0;
749: if (debug)
750: fprintf(stderr, "Prim %3d @ %p %p %p, length=%3ld restlength=%2ld superend=%1d",
751: i, s1, s2, s3, (long)(pi->length), (long)(pi->restlength), pi->superend);
752: if (endlabel == NULL) {
753: pi->start = NULL; /* not relocatable */
754: if (debug)
755: fprintf(stderr,"\n non_reloc: no K label > start found\n");
756: continue;
757: }
758: if (ends1[i] > endlabel && !pi->superend) {
759: pi->start = NULL; /* not relocatable */
760: if (debug)
761: fprintf(stderr,"\n non_reloc: there is a K label before the J label (restlength<0)\n");
762: continue;
763: }
764: if (ends1[i] < pi->start && !pi->superend) {
765: pi->start = NULL; /* not relocatable */
766: if (debug)
767: fprintf(stderr,"\n non_reloc: J label before I label (length<0)\n");
768: continue;
769: }
770: assert(prim_len>=0);
771: assert(pi->restlength >=0);
772: while (j<(pi->length+pi->restlength)) {
773: if (s1[j]==s3[j]) {
774: if (s1[j] != s2[j]) {
775: pi->start = NULL; /* not relocatable */
776: if (debug)
777: fprintf(stderr,"\n non_reloc: engine1!=engine2 offset %3d",j);
778: /* assert(j<prim_len); */
779: break;
780: }
781: j++;
782: } else {
783: struct immarg *ia=&pi->immargs[pi->nimmargs];
784:
785: pi->nimmargs++;
786: ia->offset=j;
787: if ((~*(Cell *)&(s1[j]))==*(Cell *)&(s3[j])) {
788: ia->rel=0;
789: if (debug)
790: fprintf(stderr,"\n absolute immarg: offset %3d",j);
791: } else if ((&(s1[j]))+(*(Cell *)&(s1[j]))+4 ==
792: symbols1[DOESJUMP+1]) {
793: ia->rel=1;
794: if (debug)
795: fprintf(stderr,"\n relative immarg: offset %3d",j);
796: } else {
797: pi->start = NULL; /* not relocatable */
798: if (debug)
799: fprintf(stderr,"\n non_reloc: engine1!=engine3 offset %3d",j);
800: /* assert(j<prim_len);*/
801: break;
802: }
803: j+=4;
804: }
805: }
806: if (debug)
807: fprintf(stderr,"\n");
808: }
809: decomp_prims = calloc(i,sizeof(PrimInfo *));
810: for (i=DOESJUMP+1; i<npriminfos; i++)
811: decomp_prims[i] = &(priminfos[i]);
812: qsort(decomp_prims+DOESJUMP+1, npriminfos-DOESJUMP-1, sizeof(PrimInfo *),
813: compare_priminfo_length);
814: #endif
815: }
816:
817: void flush_to_here(void)
818: {
819: #ifndef NO_DYNAMIC
820: if (start_flush)
821: FLUSH_ICACHE(start_flush, code_here-start_flush);
822: start_flush=code_here;
823: #endif
824: }
825:
826: #ifndef NO_DYNAMIC
827: void append_jump(void)
828: {
829: if (last_jump) {
830: PrimInfo *pi = &priminfos[last_jump];
831:
832: memcpy(code_here, pi->start+pi->length, pi->restlength);
833: code_here += pi->restlength;
834: last_jump=0;
835: }
836: }
837:
838: /* Gforth remembers all code blocks in this list. On forgetting (by
839: executing a marker) the code blocks are not freed (because Gforth does
840: not remember how they were allocated; hmm, remembering that might be
841: easier and cleaner). Instead, code_here etc. are reset to the old
842: value, and the "forgotten" code blocks are reused when they are
843: needed. */
844:
845: struct code_block_list {
846: struct code_block_list *next;
847: Address block;
848: Cell size;
849: } *code_block_list=NULL, **next_code_blockp=&code_block_list;
850:
851: Address append_prim(Cell p)
852: {
853: PrimInfo *pi = &priminfos[p];
854: Address old_code_here = code_here;
855:
856: if (code_area+code_area_size < code_here+pi->length+pi->restlength) {
857: struct code_block_list *p;
858: append_jump();
859: flush_to_here();
860: if (*next_code_blockp == NULL) {
861: code_here = start_flush = code_area = my_alloc(code_area_size);
862: p = (struct code_block_list *)malloc(sizeof(struct code_block_list));
863: *next_code_blockp = p;
864: p->next = NULL;
865: p->block = code_here;
866: p->size = code_area_size;
867: } else {
868: p = *next_code_blockp;
869: code_here = start_flush = code_area = p->block;
870: }
871: old_code_here = code_here;
872: next_code_blockp = &(p->next);
873: }
874: memcpy(code_here, pi->start, pi->length);
875: code_here += pi->length;
876: return old_code_here;
877: }
878: #endif
879:
880: int forget_dyncode(Address code)
881: {
882: #ifdef NO_DYNAMIC
883: return -1;
884: #else
885: struct code_block_list *p, **pp;
886:
887: for (pp=&code_block_list, p=*pp; p!=NULL; pp=&(p->next), p=*pp) {
888: if (code >= p->block && code < p->block+p->size) {
889: next_code_blockp = &(p->next);
890: code_here = start_flush = code;
891: code_area = p->block;
892: last_jump = 0;
893: return -1;
894: }
895: }
896: return -no_dynamic;
897: #endif /* !defined(NO_DYNAMIC) */
898: }
899:
900: long dyncodesize(void)
901: {
902: #ifndef NO_DYNAMIC
903: struct code_block_list *p;
904: long size=0;
905: for (p=code_block_list; p!=NULL; p=p->next) {
906: if (code_here >= p->block && code_here < p->block+p->size)
907: return size + (code_here - p->block);
908: else
909: size += p->size;
910: }
911: #endif /* !defined(NO_DYNAMIC) */
912: return 0;
913: }
914:
915: Label decompile_code(Label _code)
916: {
917: #ifdef NO_DYNAMIC
918: return _code;
919: #else /* !defined(NO_DYNAMIC) */
920: Cell i;
921: struct code_block_list *p;
922: Address code=_code;
923:
924: /* first, check if we are in code at all */
925: for (p = code_block_list;; p = p->next) {
926: if (p == NULL)
927: return code;
928: if (code >= p->block && code < p->block+p->size)
929: break;
930: }
931: /* reverse order because NOOP might match other prims */
932: for (i=npriminfos-1; i>DOESJUMP; i--) {
933: PrimInfo *pi=decomp_prims[i];
934: if (pi->start==code || (pi->start && memcmp(code,pi->start,pi->length)==0))
935: return pi->start;
936: }
937: return code;
938: #endif /* !defined(NO_DYNAMIC) */
939: }
940:
941: #ifdef NO_IP
942: int nbranchinfos=0;
943:
944: struct branchinfo {
945: Label *targetptr; /* *(bi->targetptr) is the target */
946: Cell *addressptr; /* store the target here */
947: } branchinfos[100000];
948:
949: int ndoesexecinfos=0;
950: struct doesexecinfo {
951: int branchinfo; /* fix the targetptr of branchinfos[...->branchinfo] */
952: Cell *xt; /* cfa of word whose does-code needs calling */
953: } doesexecinfos[10000];
954:
955: /* definitions of N_execute etc. */
956: #include "prim_num.i"
957:
958: void set_rel_target(Cell *source, Label target)
959: {
960: *source = ((Cell)target)-(((Cell)source)+4);
961: }
962:
963: void register_branchinfo(Label source, Cell targetptr)
964: {
965: struct branchinfo *bi = &(branchinfos[nbranchinfos]);
966: bi->targetptr = (Label *)targetptr;
967: bi->addressptr = (Cell *)source;
968: nbranchinfos++;
969: }
970:
971: Cell *compile_prim1arg(Cell p)
972: {
973: int l = priminfos[p].length;
974: Address old_code_here=code_here;
975:
976: assert(vm_prims[p]==priminfos[p].start);
977: append_prim(p);
978: return (Cell*)(old_code_here+priminfos[p].immargs[0].offset);
979: }
980:
981: Cell *compile_call2(Cell targetptr)
982: {
983: Cell *next_code_target;
984: PrimInfo *pi = &priminfos[N_call2];
985: Address old_code_here = append_prim(N_call2);
986:
987: next_code_target = (Cell *)(old_code_here + pi->immargs[0].offset);
988: register_branchinfo(old_code_here + pi->immargs[1].offset, targetptr);
989: return next_code_target;
990: }
991: #endif
992:
993: void finish_code(void)
994: {
995: #ifdef NO_IP
996: Cell i;
997:
998: compile_prim1(NULL);
999: for (i=0; i<ndoesexecinfos; i++) {
1000: struct doesexecinfo *dei = &doesexecinfos[i];
1001: branchinfos[dei->branchinfo].targetptr = DOES_CODE1((dei->xt));
1002: }
1003: ndoesexecinfos = 0;
1004: for (i=0; i<nbranchinfos; i++) {
1005: struct branchinfo *bi=&branchinfos[i];
1006: set_rel_target(bi->addressptr, *(bi->targetptr));
1007: }
1008: nbranchinfos = 0;
1009: #endif
1010: flush_to_here();
1011: }
1012:
1013: #if 0
1014: /* compile *start into a dynamic superinstruction, updating *start */
1015: void compile_prim_dyn(Cell *start)
1016: {
1017: #if defined(NO_IP)
1018: static Cell *last_start=NULL;
1019: static Xt last_prim=NULL;
1020: /* delay work by one call in order to get relocated immargs */
1021:
1022: if (last_start) {
1023: unsigned i = last_prim-vm_prims;
1024: PrimInfo *pi=&priminfos[i];
1025: Cell *next_code_target=NULL;
1026:
1027: assert(i<npriminfos);
1028: if (i==N_execute||i==N_perform||i==N_lit_perform) {
1029: next_code_target = compile_prim1arg(N_set_next_code);
1030: }
1031: if (i==N_call) {
1032: next_code_target = compile_call2(last_start[1]);
1033: } else if (i==N_does_exec) {
1034: struct doesexecinfo *dei = &doesexecinfos[ndoesexecinfos++];
1035: *compile_prim1arg(N_lit) = (Cell)PFA(last_start[1]);
1036: /* we cannot determine the callee now (last_start[1] may be a
1037: forward reference), so just register an arbitrary target, and
1038: register in dei that we need to fix this before resolving
1039: branches */
1040: dei->branchinfo = nbranchinfos;
1041: dei->xt = (Cell *)(last_start[1]);
1042: next_code_target = compile_call2(NULL);
1043: } else if (pi->start == NULL) { /* non-reloc */
1044: next_code_target = compile_prim1arg(N_set_next_code);
1045: set_rel_target(compile_prim1arg(N_abranch),*(Xt)last_prim);
1046: } else {
1047: unsigned j;
1048: Address old_code_here = append_prim(i);
1049:
1050: for (j=0; j<pi->nimmargs; j++) {
1051: struct immarg *ia = &(pi->immargs[j]);
1052: Cell argval = last_start[pi->nimmargs - j]; /* !! specific to prims */
1053: if (ia->rel) { /* !! assumption: relative refs are branches */
1054: register_branchinfo(old_code_here + ia->offset, argval);
1055: } else /* plain argument */
1056: *(Cell *)(old_code_here + ia->offset) = argval;
1057: }
1058: }
1059: if (next_code_target!=NULL)
1060: *next_code_target = (Cell)code_here;
1061: }
1062: if (start) {
1063: last_prim = (Xt)*start;
1064: *start = (Cell)code_here;
1065: }
1066: last_start = start;
1067: return;
1068: #elif !defined(NO_DYNAMIC)
1069: Label prim=(Label)*start;
1070: unsigned i;
1071: Address old_code_here;
1072:
1073: i = ((Xt)prim)-vm_prims;
1074: prim = *(Xt)prim;
1075: if (no_dynamic) {
1076: *start = (Cell)prim;
1077: return;
1078: }
1079: if (i>=npriminfos || priminfos[i].start == 0) { /* not a relocatable prim */
1080: append_jump();
1081: *start = (Cell)prim;
1082: return;
1083: }
1084: assert(priminfos[i].start = prim);
1085: #ifdef ALIGN_CODE
1086: /* ALIGN_CODE;*/
1087: #endif
1088: assert(prim==priminfos[i].start);
1089: old_code_here = append_prim(i);
1090: last_jump = (priminfos[i].superend) ? 0 : i;
1091: *start = (Cell)old_code_here;
1092: return;
1093: #else /* !defined(DOUBLY_INDIRECT), no code replication */
1094: Label prim=(Label)*start;
1095: #if !defined(INDIRECT_THREADED)
1096: prim = *(Xt)prim;
1097: #endif
1098: *start = (Cell)prim;
1099: return;
1100: #endif /* !defined(DOUBLY_INDIRECT) */
1101: }
1102: #endif /* 0 */
1103:
1104: Cell compile_prim_dyn(unsigned p)
1105: {
1106: Cell static_prim = (Cell)vm_prims[p+DOESJUMP+1];
1107: #if defined(NO_DYNAMIC)
1108: return static_prim;
1109: #else /* !defined(NO_DYNAMIC) */
1110: Address old_code_here;
1111:
1112: if (no_dynamic)
1113: return static_prim;
1114: p += DOESJUMP+1;
1115: if (p>=npriminfos || priminfos[p].start == 0) { /* not a relocatable prim */
1116: append_jump();
1117: return static_prim;
1118: }
1119: old_code_here = append_prim(p);
1120: last_jump = (priminfos[p].superend) ? 0 : p;
1121: return (Cell)old_code_here;
1122: #endif /* !defined(NO_DYNAMIC) */
1123: }
1124:
1125: #ifndef NO_DYNAMIC
1126: int cost_codesize(int prim)
1127: {
1128: return priminfos[prim+DOESJUMP+1].length;
1129: }
1130: #endif
1131:
1132: int cost_ls(int prim)
1133: {
1134: struct cost *c = super_costs+prim;
1135:
1136: return c->loads + c->stores;
1137: }
1138:
1139: int cost_lsu(int prim)
1140: {
1141: struct cost *c = super_costs+prim;
1142:
1143: return c->loads + c->stores + c->updates;
1144: }
1145:
1146: int cost_nexts(int prim)
1147: {
1148: return 1;
1149: }
1150:
1151: typedef int Costfunc(int);
1152: Costfunc *ss_cost = /* cost function for optimize_bb */
1153: #ifdef NO_DYNAMIC
1154: cost_lsu;
1155: #else
1156: cost_codesize;
1157: #endif
1158:
1159: struct {
1160: Costfunc *costfunc;
1161: char *metricname;
1162: long sum;
1163: } cost_sums[] = {
1164: #ifndef NO_DYNAMIC
1165: { cost_codesize, "codesize", 0 },
1166: #endif
1167: { cost_ls, "ls", 0 },
1168: { cost_lsu, "lsu", 0 },
1169: { cost_nexts, "nexts", 0 }
1170: };
1171:
1172: #define MAX_BB 128 /* maximum number of instructions in BB */
1173:
1174: /* use dynamic programming to find the shortest paths within the basic
1175: block origs[0..ninsts-1]; optimals[i] contains the superinstruction
1176: on the shortest path to the end of the BB */
1177: void optimize_bb(short origs[], short optimals[], int ninsts)
1178: {
1179: int i,j, mincost;
1180: static int costs[MAX_BB+1];
1181:
1182: assert(ninsts<MAX_BB);
1183: costs[ninsts]=0;
1184: for (i=ninsts-1; i>=0; i--) {
1185: optimals[i] = origs[i];
1186: costs[i] = mincost = costs[i+1] + ss_cost(optimals[i]);
1187: for (j=2; j<=max_super && i+j<=ninsts ; j++) {
1188: int super, jcost;
1189:
1190: super = lookup_super(origs+i,j);
1191: if (super >= 0) {
1192: jcost = costs[i+j] + ss_cost(super);
1193: if (jcost <= mincost) {
1194: optimals[i] = super;
1195: mincost = jcost;
1196: if (!ss_greedy)
1197: costs[i] = jcost;
1198: }
1199: }
1200: }
1201: }
1202: }
1203:
1204: /* rewrite the instructions pointed to by instps to use the
1205: superinstructions in optimals */
1206: void rewrite_bb(Cell *instps[], short *optimals, int ninsts)
1207: {
1208: int i,j, nextdyn;
1209: Cell inst;
1210:
1211: for (i=0, nextdyn=0; i<ninsts; i++) {
1212: if (i==nextdyn) { /* compile dynamically */
1213: nextdyn += super_costs[optimals[i]].length;
1214: inst = compile_prim_dyn(optimals[i]);
1215: for (j=0; j<sizeof(cost_sums)/sizeof(cost_sums[0]); j++)
1216: cost_sums[j].sum += cost_sums[j].costfunc(optimals[i]);
1217: } else { /* compile statically */
1218: inst = (Cell)vm_prims[optimals[i]+DOESJUMP+1];
1219: }
1220: *(instps[i]) = inst;
1221: }
1222: }
1223:
1224: /* compile *start, possibly rewriting it into a static and/or dynamic
1225: superinstruction */
1226: void compile_prim1(Cell *start)
1227: {
1228: #if defined(DOUBLY_INDIRECT)
1229: Label prim=(Label)*start;
1230: if (prim<((Label)(xts+DOESJUMP)) || prim>((Label)(xts+npriminfos))) {
1231: fprintf(stderr,"compile_prim encountered xt %p\n", prim);
1232: *start=(Cell)prim;
1233: return;
1234: } else {
1235: *start = (Cell)(prim-((Label)xts)+((Label)vm_prims));
1236: return;
1237: }
1238: #elif defined(INDIRECT_THREADED)
1239: return;
1240: #else /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
1241: static Cell *instps[MAX_BB];
1242: static short origs[MAX_BB];
1243: static short optimals[MAX_BB];
1244: static int ninsts=0;
1245: unsigned prim_num;
1246:
1247: if (start==NULL)
1248: goto end_bb;
1249: prim_num = ((Xt)*start)-vm_prims;
1250: if (prim_num >= npriminfos)
1251: goto end_bb;
1252: assert(ninsts<MAX_BB);
1253: instps[ninsts] = start;
1254: origs[ninsts] = prim_num-DOESJUMP-1;
1255: ninsts++;
1256: if (ninsts >= MAX_BB || superend[prim_num-DOESJUMP-1]) {
1257: end_bb:
1258: optimize_bb(origs,optimals,ninsts);
1259: rewrite_bb(instps,optimals,ninsts);
1260: ninsts=0;
1261: }
1262: #endif /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
1263: }
1264:
1265: #if defined(PRINT_SUPER_LENGTHS) && !defined(NO_DYNAMIC)
1266: Cell prim_length(Cell prim)
1267: {
1268: return priminfos[prim+DOESJUMP+1].length;
1269: }
1270: #endif
1271:
1272: Address loader(FILE *imagefile, char* filename)
1273: /* returns the address of the image proper (after the preamble) */
1274: {
1275: ImageHeader header;
1276: Address image;
1277: Address imp; /* image+preamble */
1278: Char magic[8];
1279: char magic7; /* size byte of magic number */
1280: Cell preamblesize=0;
1281: Cell data_offset = offset_image ? 56*sizeof(Cell) : 0;
1282: UCell check_sum;
1283: Cell ausize = ((RELINFOBITS == 8) ? 0 :
1284: (RELINFOBITS == 16) ? 1 :
1285: (RELINFOBITS == 32) ? 2 : 3);
1286: Cell charsize = ((sizeof(Char) == 1) ? 0 :
1287: (sizeof(Char) == 2) ? 1 :
1288: (sizeof(Char) == 4) ? 2 : 3) + ausize;
1289: Cell cellsize = ((sizeof(Cell) == 1) ? 0 :
1290: (sizeof(Cell) == 2) ? 1 :
1291: (sizeof(Cell) == 4) ? 2 : 3) + ausize;
1292: Cell sizebyte = (ausize << 5) + (charsize << 3) + (cellsize << 1) +
1293: #ifdef WORDS_BIGENDIAN
1294: 0
1295: #else
1296: 1
1297: #endif
1298: ;
1299:
1300: vm_prims = engine(0,0,0,0,0);
1301: check_prims(vm_prims);
1302: prepare_super_table();
1303: #ifndef DOUBLY_INDIRECT
1304: #ifdef PRINT_SUPER_LENGTHS
1305: print_super_lengths();
1306: #endif
1307: check_sum = checksum(vm_prims);
1308: #else /* defined(DOUBLY_INDIRECT) */
1309: check_sum = (UCell)vm_prims;
1310: #endif /* defined(DOUBLY_INDIRECT) */
1311:
1312: do {
1313: if(fread(magic,sizeof(Char),8,imagefile) < 8) {
1314: fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.6) image.\n",
1315: progname, filename);
1316: exit(1);
1317: }
1318: preamblesize+=8;
1319: } while(memcmp(magic,"Gforth3",7));
1320: magic7 = magic[7];
1321: if (debug) {
1322: magic[7]='\0';
1323: fprintf(stderr,"Magic found: %s ", magic);
1324: print_sizes(magic7);
1325: }
1326:
1327: if (magic7 != sizebyte)
1328: {
1329: fprintf(stderr,"This image is: ");
1330: print_sizes(magic7);
1331: fprintf(stderr,"whereas the machine is ");
1332: print_sizes(sizebyte);
1333: exit(-2);
1334: };
1335:
1336: fread((void *)&header,sizeof(ImageHeader),1,imagefile);
1337:
1338: set_stack_sizes(&header);
1339:
1340: #if HAVE_GETPAGESIZE
1341: pagesize=getpagesize(); /* Linux/GNU libc offers this */
1342: #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
1343: pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
1344: #elif PAGESIZE
1345: pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
1346: #endif
1347: if (debug)
1348: fprintf(stderr,"pagesize=%ld\n",(unsigned long) pagesize);
1349:
1350: image = dict_alloc_read(imagefile, preamblesize+header.image_size,
1351: preamblesize+dictsize, data_offset);
1352: imp=image+preamblesize;
1353: alloc_stacks((ImageHeader *)imp);
1354: if (clear_dictionary)
1355: memset(imp+header.image_size, 0, dictsize-header.image_size);
1356: if(header.base==0 || header.base == (Address)0x100) {
1357: Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
1358: char reloc_bits[reloc_size];
1359: fseek(imagefile, preamblesize+header.image_size, SEEK_SET);
1360: fread(reloc_bits, 1, reloc_size, imagefile);
1361: relocate((Cell *)imp, reloc_bits, header.image_size, (Cell)header.base, vm_prims);
1362: #if 0
1363: { /* let's see what the relocator did */
1364: FILE *snapshot=fopen("snapshot.fi","wb");
1365: fwrite(image,1,imagesize,snapshot);
1366: fclose(snapshot);
1367: }
1368: #endif
1369: }
1370: else if(header.base!=imp) {
1371: fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
1372: progname, (unsigned long)header.base, (unsigned long)imp);
1373: exit(1);
1374: }
1375: if (header.checksum==0)
1376: ((ImageHeader *)imp)->checksum=check_sum;
1377: else if (header.checksum != check_sum) {
1378: fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
1379: progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
1380: exit(1);
1381: }
1382: #ifdef DOUBLY_INDIRECT
1383: ((ImageHeader *)imp)->xt_base = xts;
1384: #endif
1385: fclose(imagefile);
1386:
1387: /* unnecessary, except maybe for CODE words */
1388: /* FLUSH_ICACHE(imp, header.image_size);*/
1389:
1390: return imp;
1391: }
1392:
1393: /* pointer to last '/' or '\' in file, 0 if there is none. */
1394: char *onlypath(char *filename)
1395: {
1396: return strrchr(filename, DIRSEP);
1397: }
1398:
1399: FILE *openimage(char *fullfilename)
1400: {
1401: FILE *image_file;
1402: char * expfilename = tilde_cstr(fullfilename, strlen(fullfilename), 1);
1403:
1404: image_file=fopen(expfilename,"rb");
1405: if (image_file!=NULL && debug)
1406: fprintf(stderr, "Opened image file: %s\n", expfilename);
1407: return image_file;
1408: }
1409:
1410: /* try to open image file concat(path[0:len],imagename) */
1411: FILE *checkimage(char *path, int len, char *imagename)
1412: {
1413: int dirlen=len;
1414: char fullfilename[dirlen+strlen(imagename)+2];
1415:
1416: memcpy(fullfilename, path, dirlen);
1417: if (fullfilename[dirlen-1]!=DIRSEP)
1418: fullfilename[dirlen++]=DIRSEP;
1419: strcpy(fullfilename+dirlen,imagename);
1420: return openimage(fullfilename);
1421: }
1422:
1423: FILE * open_image_file(char * imagename, char * path)
1424: {
1425: FILE * image_file=NULL;
1426: char *origpath=path;
1427:
1428: if(strchr(imagename, DIRSEP)==NULL) {
1429: /* first check the directory where the exe file is in !! 01may97jaw */
1430: if (onlypath(progname))
1431: image_file=checkimage(progname, onlypath(progname)-progname, imagename);
1432: if (!image_file)
1433: do {
1434: char *pend=strchr(path, PATHSEP);
1435: if (pend==NULL)
1436: pend=path+strlen(path);
1437: if (strlen(path)==0) break;
1438: image_file=checkimage(path, pend-path, imagename);
1439: path=pend+(*pend==PATHSEP);
1440: } while (image_file==NULL);
1441: } else {
1442: image_file=openimage(imagename);
1443: }
1444:
1445: if (!image_file) {
1446: fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
1447: progname, imagename, origpath);
1448: exit(1);
1449: }
1450:
1451: return image_file;
1452: }
1453: #endif
1454:
1455: #ifdef HAS_OS
1456: UCell convsize(char *s, UCell elemsize)
1457: /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
1458: of bytes. the letter at the end indicates the unit, where e stands
1459: for the element size. default is e */
1460: {
1461: char *endp;
1462: UCell n,m;
1463:
1464: m = elemsize;
1465: n = strtoul(s,&endp,0);
1466: if (endp!=NULL) {
1467: if (strcmp(endp,"b")==0)
1468: m=1;
1469: else if (strcmp(endp,"k")==0)
1470: m=1024;
1471: else if (strcmp(endp,"M")==0)
1472: m=1024*1024;
1473: else if (strcmp(endp,"G")==0)
1474: m=1024*1024*1024;
1475: else if (strcmp(endp,"T")==0) {
1476: #if (SIZEOF_CHAR_P > 4)
1477: m=1024L*1024*1024*1024;
1478: #else
1479: fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp);
1480: exit(1);
1481: #endif
1482: } else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
1483: fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
1484: exit(1);
1485: }
1486: }
1487: return n*m;
1488: }
1489:
1490: enum {
1491: ss_number = 256,
1492: ss_min_codesize,
1493: ss_min_ls,
1494: ss_min_lsu,
1495: ss_min_nexts,
1496: };
1497:
1498: void gforth_args(int argc, char ** argv, char ** path, char ** imagename)
1499: {
1500: int c;
1501:
1502: opterr=0;
1503: while (1) {
1504: int option_index=0;
1505: static struct option opts[] = {
1506: {"appl-image", required_argument, NULL, 'a'},
1507: {"image-file", required_argument, NULL, 'i'},
1508: {"dictionary-size", required_argument, NULL, 'm'},
1509: {"data-stack-size", required_argument, NULL, 'd'},
1510: {"return-stack-size", required_argument, NULL, 'r'},
1511: {"fp-stack-size", required_argument, NULL, 'f'},
1512: {"locals-stack-size", required_argument, NULL, 'l'},
1513: {"path", required_argument, NULL, 'p'},
1514: {"version", no_argument, NULL, 'v'},
1515: {"help", no_argument, NULL, 'h'},
1516: /* put something != 0 into offset_image */
1517: {"offset-image", no_argument, &offset_image, 1},
1518: {"no-offset-im", no_argument, &offset_image, 0},
1519: {"clear-dictionary", no_argument, &clear_dictionary, 1},
1520: {"die-on-signal", no_argument, &die_on_signal, 1},
1521: {"debug", no_argument, &debug, 1},
1522: {"no-super", no_argument, &no_super, 1},
1523: {"no-dynamic", no_argument, &no_dynamic, 1},
1524: {"dynamic", no_argument, &no_dynamic, 0},
1525: {"print-metrics", no_argument, &print_metrics, 1},
1526: {"ss-number", required_argument, NULL, ss_number},
1527: #ifndef NO_DYNAMIC
1528: {"ss-min-codesize", no_argument, NULL, ss_min_codesize},
1529: #endif
1530: {"ss-min-ls", no_argument, NULL, ss_min_ls},
1531: {"ss-min-lsu", no_argument, NULL, ss_min_lsu},
1532: {"ss-min-nexts", no_argument, NULL, ss_min_nexts},
1533: {"ss-greedy", no_argument, &ss_greedy, 1},
1534: {0,0,0,0}
1535: /* no-init-file, no-rc? */
1536: };
1537:
1538: c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vhoncsx", opts, &option_index);
1539:
1540: switch (c) {
1541: case EOF: return;
1542: case '?': optind--; return;
1543: case 'a': *imagename = optarg; return;
1544: case 'i': *imagename = optarg; break;
1545: case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
1546: case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
1547: case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
1548: case 'f': fsize = convsize(optarg,sizeof(Float)); break;
1549: case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
1550: case 'p': *path = optarg; break;
1551: case 'o': offset_image = 1; break;
1552: case 'n': offset_image = 0; break;
1553: case 'c': clear_dictionary = 1; break;
1554: case 's': die_on_signal = 1; break;
1555: case 'x': debug = 1; break;
1556: case 'v': fputs(PACKAGE_STRING"\n", stderr); exit(0);
1557: case ss_number: static_super_number = atoi(optarg); break;
1558: #ifndef NO_DYNAMIC
1559: case ss_min_codesize: ss_cost = cost_codesize; break;
1560: #endif
1561: case ss_min_ls: ss_cost = cost_ls; break;
1562: case ss_min_lsu: ss_cost = cost_lsu; break;
1563: case ss_min_nexts: ss_cost = cost_nexts; break;
1564: case 'h':
1565: fprintf(stderr, "Usage: %s [engine options] ['--'] [image arguments]\n\
1566: Engine Options:\n\
1567: --appl-image FILE equivalent to '--image-file=FILE --'\n\
1568: --clear-dictionary Initialize the dictionary with 0 bytes\n\
1569: -d SIZE, --data-stack-size=SIZE Specify data stack size\n\
1570: --debug Print debugging information during startup\n\
1571: --die-on-signal exit instead of CATCHing some signals\n\
1572: --dynamic use dynamic native code\n\
1573: -f SIZE, --fp-stack-size=SIZE Specify floating point stack size\n\
1574: -h, --help Print this message and exit\n\
1575: -i FILE, --image-file=FILE Use image FILE instead of `gforth.fi'\n\
1576: -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
1577: -m SIZE, --dictionary-size=SIZE Specify Forth dictionary size\n\
1578: --no-dynamic Use only statically compiled primitives\n\
1579: --no-offset-im Load image at normal position\n\
1580: --no-super No dynamically formed superinstructions\n\
1581: --offset-image Load image at a different position\n\
1582: -p PATH, --path=PATH Search path for finding image and sources\n\
1583: --print-metrics Print some code generation metrics on exit\n\
1584: -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
1585: --ss-greedy greedy, not optimal superinst selection\n\
1586: --ss-min-codesize select superinsts for smallest native code\n\
1587: --ss-min-ls minimize loads and stores\n\
1588: --ss-min-lsu minimize loads, stores, and pointer updates\n\
1589: --ss-min-nexts minimize the number of static superinsts\n\
1590: --ss-number=N use N static superinsts (default max)\n\
1591: -v, --version Print engine version and exit\n\
1592: SIZE arguments consist of an integer followed by a unit. The unit can be\n\
1593: `b' (byte), `e' (element; default), `k' (KB), `M' (MB), `G' (GB) or `T' (TB).\n",
1594: argv[0]);
1595: optind--;
1596: return;
1597: }
1598: }
1599: }
1600: #endif
1601:
1602: #ifdef INCLUDE_IMAGE
1603: extern Cell image[];
1604: extern const char reloc_bits[];
1605: #endif
1606:
1607: int main(int argc, char **argv, char **env)
1608: {
1609: #ifdef HAS_OS
1610: char *path = getenv("GFORTHPATH") ? : DEFAULTPATH;
1611: #else
1612: char *path = DEFAULTPATH;
1613: #endif
1614: #ifndef INCLUDE_IMAGE
1615: char *imagename="gforth.fi";
1616: FILE *image_file;
1617: Address image;
1618: #endif
1619: int retvalue;
1620:
1621: #if defined(i386) && defined(ALIGNMENT_CHECK)
1622: /* turn on alignment checks on the 486.
1623: * on the 386 this should have no effect. */
1624: __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
1625: /* this is unusable with Linux' libc.4.6.27, because this library is
1626: not alignment-clean; we would have to replace some library
1627: functions (e.g., memcpy) to make it work. Also GCC doesn't try to keep
1628: the stack FP-aligned. */
1629: #endif
1630:
1631: /* buffering of the user output device */
1632: #ifdef _IONBF
1633: if (isatty(fileno(stdout))) {
1634: fflush(stdout);
1635: setvbuf(stdout,NULL,_IONBF,0);
1636: }
1637: #endif
1638:
1639: progname = argv[0];
1640:
1641: #ifdef HAS_OS
1642: gforth_args(argc, argv, &path, &imagename);
1643: #ifndef NO_DYNAMIC
1644: if (no_dynamic && ss_cost == cost_codesize) {
1645: ss_cost = cost_lsu;
1646: cost_sums[0] = cost_sums[1];
1647: if (debug)
1648: fprintf(stderr, "--no-dynamic conflicts with --ss-min-codesize, reverting to --ss-min-lsu\n");
1649: }
1650: #endif /* !defined(NO_DYNAMIC) */
1651: #endif /* defined(HAS_OS) */
1652:
1653: #ifdef INCLUDE_IMAGE
1654: set_stack_sizes((ImageHeader *)image);
1655: if(((ImageHeader *)image)->base != image)
1656: relocate(image, reloc_bits, ((ImageHeader *)image)->image_size,
1657: (Label*)engine(0, 0, 0, 0, 0));
1658: alloc_stacks((ImageHeader *)image);
1659: #else
1660: image_file = open_image_file(imagename, path);
1661: image = loader(image_file, imagename);
1662: #endif
1663: gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */
1664:
1665: {
1666: char path2[strlen(path)+1];
1667: char *p1, *p2;
1668: Cell environ[]= {
1669: (Cell)argc-(optind-1),
1670: (Cell)(argv+(optind-1)),
1671: (Cell)strlen(path),
1672: (Cell)path2};
1673: argv[optind-1] = progname;
1674: /*
1675: for (i=0; i<environ[0]; i++)
1676: printf("%s\n", ((char **)(environ[1]))[i]);
1677: */
1678: /* make path OS-independent by replacing path separators with NUL */
1679: for (p1=path, p2=path2; *p1!='\0'; p1++, p2++)
1680: if (*p1==PATHSEP)
1681: *p2 = '\0';
1682: else
1683: *p2 = *p1;
1684: *p2='\0';
1685: retvalue = go_forth(image, 4, environ);
1686: #ifdef SIGPIPE
1687: bsd_signal(SIGPIPE, SIG_IGN);
1688: #endif
1689: #ifdef VM_PROFILING
1690: vm_print_profile(stderr);
1691: #endif
1692: deprep_terminal();
1693: }
1694: if (print_metrics) {
1695: int i;
1696: fprintf(stderr, "code size = %8ld\n", dyncodesize());
1697: for (i=0; i<sizeof(cost_sums)/sizeof(cost_sums[0]); i++)
1698: fprintf(stderr, "metric %8s: %8ld\n",
1699: cost_sums[i].metricname, cost_sums[i].sum);
1700: }
1701: return retvalue;
1702: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>