[gforth] / gforth / engine / main.c  

gforth: gforth/engine/main.c


1 : anton 1.1 /* command line interpretation, image loading etc. for Gforth
2 :    
3 :    
4 : anton 1.39 Copyright (C) 1995,1996,1997,1998,2000 Free Software Foundation, Inc.
5 : anton 1.1
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 : anton 1.40 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
21 : anton 1.1 */
22 :    
23 :     #include "config.h"
24 :     #include <errno.h>
25 :     #include <ctype.h>
26 :     #include <stdio.h>
27 : pazsan 1.2 #include <unistd.h>
28 : anton 1.1 #include <string.h>
29 :     #include <math.h>
30 :     #include <sys/types.h>
31 : pazsan 1.32 #ifndef STANDALONE
32 : anton 1.1 #include <sys/stat.h>
33 : pazsan 1.32 #endif
34 : anton 1.1 #include <fcntl.h>
35 :     #include <assert.h>
36 :     #include <stdlib.h>
37 : pazsan 1.11 #ifndef STANDALONE
38 : anton 1.1 #if HAVE_SYS_MMAN_H
39 :     #include <sys/mman.h>
40 :     #endif
41 : pazsan 1.11 #endif
42 : anton 1.1 #include "forth.h"
43 :     #include "io.h"
44 :     #include "getopt.h"
45 : pazsan 1.11 #ifdef STANDALONE
46 :     #include <systypes.h>
47 :     #endif
48 : anton 1.1
49 :     #define PRIM_VERSION 1
50 :     /* increment this whenever the primitives change in an incompatible way */
51 :    
52 : pazsan 1.14 #ifndef DEFAULTPATH
53 : anton 1.39 # define DEFAULTPATH "."
54 : pazsan 1.14 #endif
55 :    
56 : anton 1.1 #ifdef MSDOS
57 :     jmp_buf throw_jmp_buf;
58 :     #endif
59 :    
60 : anton 1.56 #if defined(DOUBLY_INDIRECT)
61 :     # define CFA(n) ({Cell _n = (n); ((Cell)(((_n & 0x4000) ? symbols : xts)+(_n&~0x4000UL)));})
62 : anton 1.1 #else
63 : anton 1.56 # define CFA(n) ((Cell)(symbols+((n)&~0x4000UL)))
64 : anton 1.1 #endif
65 :    
66 :     #define maxaligned(n) (typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
67 :    
68 :     static UCell dictsize=0;
69 :     static UCell dsize=0;
70 :     static UCell rsize=0;
71 :     static UCell fsize=0;
72 :     static UCell lsize=0;
73 :     int offset_image=0;
74 : anton 1.4 int die_on_signal=0;
75 : pazsan 1.13 #ifndef INCLUDE_IMAGE
76 : anton 1.1 static int clear_dictionary=0;
77 : anton 1.24 UCell pagesize=1;
78 : pazsan 1.22 char *progname;
79 :     #else
80 :     char *progname = "gforth";
81 :     int optind = 1;
82 : pazsan 1.13 #endif
83 : pazsan 1.31
84 : anton 1.48 Address code_area=0;
85 :     Address code_here=0; /* does for code-area what HERE does for the dictionary */
86 : anton 1.65 Address start_flush=0; /* start of unflushed code */
87 : anton 1.48
88 : anton 1.60 static int no_super=0; /* true if compile_prim should not fuse prims */
89 : anton 1.66 /* --no-dynamic by default on gcc versions >=3.1 (it works with 3.0.4,
90 :     but not with 3.2) */
91 :     #if (__GNUC__>2 && __GNUC_MINOR__>=1)
92 : anton 1.64 static int no_dynamic=1; /* true if compile_prim should not generate code */
93 : anton 1.66 #else
94 :     static int no_dynamic=0; /* true if compile_prim should not generate code */
95 :     #endif
96 : anton 1.60
97 : pazsan 1.30 #ifdef HAS_DEBUG
98 : anton 1.68 int debug=0;
99 : pazsan 1.31 #else
100 :     # define perror(x...)
101 :     # define fprintf(x...)
102 : pazsan 1.30 #endif
103 : pazsan 1.31
104 : anton 1.24 ImageHeader *gforth_header;
105 : anton 1.43 Label *vm_prims;
106 : anton 1.53 #ifdef DOUBLY_INDIRECT
107 :     Label *xts; /* same content as vm_prims, but should only be used for xts */
108 :     #endif
109 : anton 1.1
110 : pazsan 1.30 #ifdef MEMCMP_AS_SUBROUTINE
111 :     int gforth_memcmp(const char * s1, const char * s2, size_t n)
112 :     {
113 :     return memcmp(s1, s2, n);
114 :     }
115 :     #endif
116 :    
117 : anton 1.1 /* image file format:
118 : pazsan 1.15 * "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.4.0 -i\n")
119 : anton 1.1 * padding to a multiple of 8
120 : pazsan 1.15 * magic: "Gforth2x" means format 0.4,
121 :     * where x is a byte with
122 :     * bit 7: reserved = 0
123 :     * bit 6:5: address unit size 2^n octets
124 :     * bit 4:3: character size 2^n octets
125 :     * bit 2:1: cell size 2^n octets
126 :     * bit 0: endian, big=0, little=1.
127 :     * The magic are always 8 octets, no matter what the native AU/character size is
128 : anton 1.1 * padding to max alignment (no padding necessary on current machines)
129 : anton 1.24 * ImageHeader structure (see forth.h)
130 : anton 1.1 * data (size in ImageHeader.image_size)
131 :     * tags ((if relocatable, 1 bit/data cell)
132 :     *
133 :     * tag==1 means that the corresponding word is an address;
134 :     * If the word is >=0, the address is within the image;
135 :     * addresses within the image are given relative to the start of the image.
136 :     * If the word =-1 (CF_NIL), the address is NIL,
137 :     * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
138 :     * If the word =CF(DODOES), it's a DOES> CFA
139 :     * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
140 :     * possibly containing a jump to dodoes)
141 : anton 1.51 * If the word is <CF(DOESJUMP) and bit 14 is set, it's the xt of a primitive
142 :     * If the word is <CF(DOESJUMP) and bit 14 is clear,
143 :     * it's the threaded code of a primitive
144 : anton 1.1 */
145 :    
146 : jwilke 1.46 void relocate(Cell *image, const char *bitstring,
147 :     int size, int base, Label symbols[])
148 : anton 1.1 {
149 : pazsan 1.16 int i=0, j, k, steps=(size/sizeof(Cell))/RELINFOBITS;
150 : pazsan 1.11 Cell token;
151 : anton 1.1 char bits;
152 : anton 1.37 Cell max_symbols;
153 : jwilke 1.46 /*
154 :     * A virtial start address that's the real start address minus
155 :     * the one in the image
156 :     */
157 : jwilke 1.45 Cell *start = (Cell * ) (((void *) image) - ((void *) base));
158 : anton 1.1
159 : jwilke 1.46
160 :     /* printf("relocating to %x[%x] start=%x base=%x\n", image, size, start, base); */
161 : anton 1.37
162 :     for (max_symbols=DOESJUMP+1; symbols[max_symbols]!=0; max_symbols++)
163 :     ;
164 : anton 1.47 max_symbols--;
165 : pazsan 1.35 size/=sizeof(Cell);
166 :    
167 : pazsan 1.31 for(k=0; k<=steps; k++) {
168 : pazsan 1.13 for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
169 : anton 1.1 /* fprintf(stderr,"relocate: image[%d]\n", i);*/
170 : pazsan 1.35 if((i < size) && (bits & (1U << (RELINFOBITS-1)))) {
171 :     /* fprintf(stderr,"relocate: image[%d]=%d of %d\n", i, image[i], size/sizeof(Cell)); */
172 : jwilke 1.45 token=image[i];
173 :     if(token<0)
174 : anton 1.55 switch(token|0x4000)
175 : anton 1.1 {
176 :     case CF_NIL : image[i]=0; break;
177 :     #if !defined(DOUBLY_INDIRECT)
178 :     case CF(DOCOL) :
179 :     case CF(DOVAR) :
180 :     case CF(DOCON) :
181 :     case CF(DOUSER) :
182 :     case CF(DODEFER) :
183 : pazsan 1.11 case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(token)]); break;
184 : anton 1.1 case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
185 :     #endif /* !defined(DOUBLY_INDIRECT) */
186 :     case CF(DODOES) :
187 : jwilke 1.45 MAKE_DOES_CF(image+i,(Xt *)(image[i+1]+((Cell)start)));
188 : anton 1.1 break;
189 :     default :
190 : anton 1.56 /* printf("Code field generation image[%x]:=CFA(%x)\n",
191 : anton 1.1 i, CF(image[i])); */
192 : anton 1.55 if (CF((token | 0x4000))<max_symbols) {
193 : anton 1.56 image[i]=(Cell)CFA(CF(token));
194 :     #ifdef DIRECT_THREADED
195 :     if ((token & 0x4000) == 0) /* threade code, no CFA */
196 : anton 1.57 image[i] = (Cell)compile_prim((Label)image[i]);
197 : anton 1.56 #endif
198 : anton 1.55 } else
199 : anton 1.37 fprintf(stderr,"Primitive %d used in this image at $%lx is not implemented by this\n engine (%s); executing this code will crash.\n",CF(token),(long)&image[i],VERSION);
200 : anton 1.1 }
201 : jwilke 1.46 else {
202 : jwilke 1.45 // if base is > 0: 0 is a null reference so don't adjust
203 :     if (token>=base) {
204 :     image[i]+=(Cell)start;
205 :     }
206 : jwilke 1.46 }
207 : anton 1.1 }
208 :     }
209 : pazsan 1.31 }
210 : jwilke 1.26 ((ImageHeader*)(image))->base = (Address) image;
211 : anton 1.1 }
212 :    
213 :     UCell checksum(Label symbols[])
214 :     {
215 :     UCell r=PRIM_VERSION;
216 :     Cell i;
217 :    
218 :     for (i=DOCOL; i<=DOESJUMP; i++) {
219 :     r ^= (UCell)(symbols[i]);
220 :     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
221 :     }
222 :     #ifdef DIRECT_THREADED
223 :     /* we have to consider all the primitives */
224 :     for (; symbols[i]!=(Label)0; i++) {
225 :     r ^= (UCell)(symbols[i]);
226 :     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
227 :     }
228 :     #else
229 :     /* in indirect threaded code all primitives are accessed through the
230 :     symbols table, so we just have to put the base address of symbols
231 :     in the checksum */
232 :     r ^= (UCell)symbols;
233 :     #endif
234 :     return r;
235 :     }
236 :    
237 : anton 1.3 Address verbose_malloc(Cell size)
238 :     {
239 :     Address r;
240 :     /* leave a little room (64B) for stack underflows */
241 :     if ((r = malloc(size+64))==NULL) {
242 :     perror(progname);
243 :     exit(1);
244 :     }
245 :     r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
246 :     if (debug)
247 :     fprintf(stderr, "malloc succeeds, address=$%lx\n", (long)r);
248 :     return r;
249 :     }
250 :    
251 : anton 1.33 static Address next_address=0;
252 :     void after_alloc(Address r, Cell size)
253 :     {
254 :     if (r != (Address)-1) {
255 :     if (debug)
256 :     fprintf(stderr, "success, address=$%lx\n", (long) r);
257 :     if (pagesize != 1)
258 :     next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */
259 :     } else {
260 :     if (debug)
261 :     fprintf(stderr, "failed: %s\n", strerror(errno));
262 :     }
263 :     }
264 :    
265 : anton 1.34 #ifndef MAP_FAILED
266 :     #define MAP_FAILED ((Address) -1)
267 :     #endif
268 :     #ifndef MAP_FILE
269 :     # define MAP_FILE 0
270 :     #endif
271 :     #ifndef MAP_PRIVATE
272 :     # define MAP_PRIVATE 0
273 :     #endif
274 :    
275 :     #if defined(HAVE_MMAP)
276 :     static Address alloc_mmap(Cell size)
277 : anton 1.1 {
278 :     Address r;
279 :    
280 :     #if defined(MAP_ANON)
281 :     if (debug)
282 :     fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_ANON, ...); ", (long)next_address, (long)size);
283 : anton 1.34 r = mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
284 : anton 1.1 #else /* !defined(MAP_ANON) */
285 : anton 1.17 /* Ultrix (at least) does not define MAP_FILE and MAP_PRIVATE (both are
286 :     apparently defaults) */
287 : anton 1.1 static int dev_zero=-1;
288 :    
289 :     if (dev_zero == -1)
290 :     dev_zero = open("/dev/zero", O_RDONLY);
291 :     if (dev_zero == -1) {
292 : anton 1.34 r = MAP_FAILED;
293 : anton 1.1 if (debug)
294 :     fprintf(stderr, "open(\"/dev/zero\"...) failed (%s), no mmap; ",
295 :     strerror(errno));
296 :     } else {
297 :     if (debug)
298 :     fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FILE, dev_zero, ...); ", (long)next_address, (long)size);
299 :     r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE, dev_zero, 0);
300 :     }
301 :     #endif /* !defined(MAP_ANON) */
302 : anton 1.34 after_alloc(r, size);
303 :     return r;
304 :     }
305 :     #endif
306 :    
307 :     Address my_alloc(Cell size)
308 :     {
309 :     #if HAVE_MMAP
310 :     Address r;
311 :    
312 :     r=alloc_mmap(size);
313 :     if (r!=MAP_FAILED)
314 : anton 1.1 return r;
315 :     #endif /* HAVE_MMAP */
316 : anton 1.3 /* use malloc as fallback */
317 :     return verbose_malloc(size);
318 : anton 1.1 }
319 :    
320 : anton 1.34 Address dict_alloc_read(FILE *file, Cell imagesize, Cell dictsize, Cell offset)
321 : anton 1.33 {
322 : anton 1.34 Address image = MAP_FAILED;
323 : anton 1.33
324 : anton 1.56 #if defined(HAVE_MMAP)
325 : anton 1.33 if (offset==0) {
326 : anton 1.34 image=alloc_mmap(dictsize);
327 : anton 1.33 if (debug)
328 : anton 1.34 fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FIXED|MAP_FILE, imagefile, 0); ", (long)image, (long)imagesize);
329 :     image = mmap(image, imagesize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FIXED|MAP_FILE|MAP_PRIVATE, fileno(file), 0);
330 :     after_alloc(image,dictsize);
331 : anton 1.33 }
332 : anton 1.56 #endif /* defined(HAVE_MMAP) */
333 : anton 1.34 if (image == MAP_FAILED) {
334 : anton 1.56 image = my_alloc(dictsize+offset)+offset;
335 : anton 1.33 rewind(file); /* fseek(imagefile,0L,SEEK_SET); */
336 : anton 1.34 fread(image, 1, imagesize, file);
337 : anton 1.33 }
338 :     return image;
339 :     }
340 :    
341 : pazsan 1.10 void set_stack_sizes(ImageHeader * header)
342 :     {
343 :     if (dictsize==0)
344 :     dictsize = header->dict_size;
345 :     if (dsize==0)
346 :     dsize = header->data_stack_size;
347 :     if (rsize==0)
348 :     rsize = header->return_stack_size;
349 :     if (fsize==0)
350 :     fsize = header->fp_stack_size;
351 :     if (lsize==0)
352 :     lsize = header->locals_stack_size;
353 :     dictsize=maxaligned(dictsize);
354 :     dsize=maxaligned(dsize);
355 :     rsize=maxaligned(rsize);
356 :     lsize=maxaligned(lsize);
357 :     fsize=maxaligned(fsize);
358 :     }
359 :    
360 :     void alloc_stacks(ImageHeader * header)
361 :     {
362 :     header->dict_size=dictsize;
363 :     header->data_stack_size=dsize;
364 :     header->fp_stack_size=fsize;
365 :     header->return_stack_size=rsize;
366 :     header->locals_stack_size=lsize;
367 :    
368 :     header->data_stack_base=my_alloc(dsize);
369 :     header->fp_stack_base=my_alloc(fsize);
370 :     header->return_stack_base=my_alloc(rsize);
371 :     header->locals_stack_base=my_alloc(lsize);
372 : anton 1.65 code_here = start_flush = code_area = my_alloc(dictsize);
373 : pazsan 1.10 }
374 :    
375 : pazsan 1.44 #warning You can ignore the warnings about clobbered variables in go_forth
376 : pazsan 1.11 int go_forth(Address image, int stack, Cell *entries)
377 :     {
378 : anton 1.38 volatile ImageHeader *image_header = (ImageHeader *)image;
379 : anton 1.18 Cell *sp0=(Cell*)(image_header->data_stack_base + dsize);
380 : pazsan 1.44 Cell *rp0=(Cell *)(image_header->return_stack_base + rsize);
381 : anton 1.18 Float *fp0=(Float *)(image_header->fp_stack_base + fsize);
382 : pazsan 1.44 #ifdef GFORTH_DEBUGGING
383 : anton 1.38 volatile Cell *orig_rp0=rp0;
384 : pazsan 1.44 #endif
385 : anton 1.18 Address lp0=image_header->locals_stack_base + lsize;
386 :     Xt *ip0=(Xt *)(image_header->boot_entry);
387 : pazsan 1.13 #ifdef SYSSIGNALS
388 : pazsan 1.11 int throw_code;
389 : pazsan 1.13 #endif
390 : pazsan 1.11
391 :     /* ensure that the cached elements (if any) are accessible */
392 : anton 1.41 IF_spTOS(sp0--);
393 :     IF_fpTOS(fp0--);
394 : pazsan 1.11
395 :     for(;stack>0;stack--)
396 : anton 1.18 *--sp0=entries[stack-1];
397 : pazsan 1.11
398 : pazsan 1.30 #ifdef SYSSIGNALS
399 : pazsan 1.11 get_winsize();
400 :    
401 :     install_signal_handlers(); /* right place? */
402 :    
403 :     if ((throw_code=setjmp(throw_jmp_buf))) {
404 :     static Cell signal_data_stack[8];
405 :     static Cell signal_return_stack[8];
406 :     static Float signal_fp_stack[1];
407 : pazsan 1.13
408 : pazsan 1.11 signal_data_stack[7]=throw_code;
409 : anton 1.18
410 :     #ifdef GFORTH_DEBUGGING
411 : anton 1.38 /* fprintf(stderr,"\nrp=%ld\n",(long)rp); */
412 :     if (rp <= orig_rp0 && rp > (Cell *)(image_header->return_stack_base+5)) {
413 : anton 1.18 /* no rstack overflow or underflow */
414 :     rp0 = rp;
415 : anton 1.63 *--rp0 = (Cell)saved_ip;
416 : anton 1.18 }
417 :     else /* I love non-syntactic ifdefs :-) */
418 :     #endif
419 :     rp0 = signal_return_stack+8;
420 : anton 1.25 /* fprintf(stderr, "rp=$%x\n",rp0);*/
421 : pazsan 1.11
422 : anton 1.33 return((int)(Cell)engine(image_header->throw_entry, signal_data_stack+7,
423 : anton 1.18 rp0, signal_fp_stack, 0));
424 : pazsan 1.11 }
425 : pazsan 1.13 #endif
426 : pazsan 1.11
427 : anton 1.33 return((int)(Cell)engine(ip0,sp0,rp0,fp0,lp0));
428 : pazsan 1.11 }
429 :    
430 : anton 1.21
431 : pazsan 1.30 #ifndef INCLUDE_IMAGE
432 : anton 1.21 void print_sizes(Cell sizebyte)
433 :     /* print size information */
434 :     {
435 :     static char* endianstring[]= { " big","little" };
436 :    
437 :     fprintf(stderr,"%s endian, cell=%d bytes, char=%d bytes, au=%d bytes\n",
438 :     endianstring[sizebyte & 1],
439 :     1 << ((sizebyte >> 1) & 3),
440 :     1 << ((sizebyte >> 3) & 3),
441 :     1 << ((sizebyte >> 5) & 3));
442 :     }
443 :    
444 : anton 1.47 typedef struct {
445 :     Label start;
446 :     Cell length; /* excluding the jump */
447 :     char super_end; /* true if primitive ends superinstruction, i.e.,
448 :     unconditional branch, execute, etc. */
449 :     } PrimInfo;
450 :    
451 :     PrimInfo *priminfos;
452 : anton 1.48 Cell npriminfos=0;
453 : anton 1.47
454 :     void check_prims(Label symbols1[])
455 :     {
456 :     int i;
457 : anton 1.55 Label *symbols2;
458 : anton 1.49 static char superend[]={
459 : anton 1.48 #include "prim_superend.i"
460 :     };
461 : anton 1.47
462 : anton 1.66 if (debug)
463 :     #ifdef __VERSION__
464 :     fprintf(stderr, "Compiled with gcc-" __VERSION__ "\n");
465 :     #else
466 :     #define xstr(s) str(s)
467 :     #define str(s) #s
468 :     fprintf(stderr, "Compiled with gcc-" xstr(__GNUC__) "." xstr(__GNUC_MINOR__) "\n");
469 :     #endif
470 : anton 1.47 for (i=DOESJUMP+1; symbols1[i+1]!=0; i++)
471 :     ;
472 : anton 1.55 npriminfos = i;
473 :    
474 : anton 1.66 #if defined(IS_NEXT_JUMP) && !defined(DOUBLY_INDIRECT)
475 :     if (no_dynamic)
476 :     return;
477 : anton 1.55 symbols2=engine2(0,0,0,0,0);
478 : anton 1.47 priminfos = calloc(i,sizeof(PrimInfo));
479 :     for (i=DOESJUMP+1; symbols1[i+1]!=0; i++) {
480 :     int prim_len=symbols1[i+1]-symbols1[i];
481 :     PrimInfo *pi=&priminfos[i];
482 :     int j;
483 : anton 1.60 pi->super_end = superend[i-DOESJUMP-1]|no_super;
484 : anton 1.50 for (j=prim_len-IND_JUMP_LENGTH; ; j--) {
485 :     if (IS_NEXT_JUMP(symbols1[i]+j)) {
486 : anton 1.47 prim_len = j;
487 : anton 1.48 if (pi->super_end)
488 : anton 1.50 prim_len += IND_JUMP_LENGTH; /* include the jump */
489 : anton 1.47 break;
490 :     }
491 :     if (j==0) { /* NEXT jump not found, e.g., execute */
492 : anton 1.48 if (!pi->super_end && debug)
493 :     fprintf(stderr, "NEXT jump not found for primitive %d, making it super_end\n", i);
494 :     pi->super_end = 1;
495 : anton 1.47 break;
496 :     }
497 :     }
498 : anton 1.59 pi->length = prim_len;
499 : anton 1.47 /* fprintf(stderr,"checking primitive %d: memcmp(%p, %p, %d)\n",
500 :     i, symbols1[i], symbols2[i], prim_len);*/
501 : anton 1.66 if (memcmp(symbols1[i],symbols2[i],prim_len)!=0) {
502 : anton 1.47 if (debug)
503 :     fprintf(stderr,"Primitive %d not relocatable: memcmp(%p, %p, %d)\n",
504 :     i, symbols1[i], symbols2[i], prim_len);
505 :     } else {
506 :     pi->start = symbols1[i];
507 :     if (debug)
508 :     fprintf(stderr,"Primitive %d relocatable: start %p, length %ld, super_end %d\n",
509 :     i, pi->start, pi->length, pi->super_end);
510 :     }
511 : anton 1.48 }
512 :     #endif
513 :     }
514 :    
515 :     Label compile_prim(Label prim)
516 :     {
517 : anton 1.61 #if defined(DOUBLY_INDIRECT)
518 : anton 1.54 if (prim<((Label)(xts+DOESJUMP)) || prim>((Label)(xts+npriminfos))) {
519 :     fprintf(stderr,"compile_prim encountered xt %p\n", prim);
520 :     return prim;
521 :     } else
522 :     return prim-((Label)xts)+((Label)vm_prims);
523 : anton 1.66 #elif defined(IND_JUMP_LENGTH) && !defined(VM_PROFILING) && !defined(INDIRECT_THREADED)
524 : anton 1.58 unsigned i;
525 : anton 1.48 Address old_code_here=code_here;
526 :     static Address last_jump=0;
527 :    
528 : anton 1.58 i = ((Xt)prim)-vm_prims;
529 : anton 1.56 prim = *(Xt)prim;
530 : anton 1.66 if (no_dynamic)
531 :     return prim;
532 : anton 1.58 if (i>=npriminfos || priminfos[i].start == 0) { /* not a relocatable prim */
533 :     if (last_jump) { /* make sure the last sequence is complete */
534 :     memcpy(code_here, last_jump, IND_JUMP_LENGTH);
535 :     code_here += IND_JUMP_LENGTH;
536 :     last_jump = 0;
537 : anton 1.65 FLUSH_ICACHE(start_flush, code_here-start_flush);
538 :     start_flush=code_here;
539 : anton 1.48 }
540 : anton 1.58 return prim;
541 : anton 1.47 }
542 : anton 1.58 assert(priminfos[i].start = prim);
543 : anton 1.50 #ifdef ALIGN_CODE
544 :     ALIGN_CODE;
545 :     #endif
546 : anton 1.48 memcpy(code_here, (Address)prim, priminfos[i].length);
547 :     code_here += priminfos[i].length;
548 :     last_jump = (priminfos[i].super_end) ? 0 : (prim+priminfos[i].length);
549 : anton 1.65 if (last_jump == 0) {
550 :     FLUSH_ICACHE(start_flush, code_here-start_flush);
551 :     start_flush=code_here;
552 :     }
553 : anton 1.48 return (Label)old_code_here;
554 : anton 1.61 #else /* !defined(DOUBLY_INDIRECT), no code replication */
555 :     #if !defined(INDIRECT_THREADED)
556 : anton 1.56 prim = *(Xt)prim;
557 : anton 1.61 #endif
558 : anton 1.50 return prim;
559 : anton 1.54 #endif /* !defined(DOUBLY_INDIRECT) */
560 : anton 1.47 }
561 :    
562 : anton 1.59 #ifdef PRINT_SUPER_LENGTHS
563 :     Cell prim_length(Cell prim)
564 :     {
565 :     return priminfos[prim+DOESJUMP+1].length;
566 :     }
567 :     #endif
568 :    
569 : anton 1.1 Address loader(FILE *imagefile, char* filename)
570 :     /* returns the address of the image proper (after the preamble) */
571 :     {
572 :     ImageHeader header;
573 :     Address image;
574 :     Address imp; /* image+preamble */
575 : anton 1.17 Char magic[8];
576 :     char magic7; /* size byte of magic number */
577 : anton 1.1 Cell preamblesize=0;
578 : pazsan 1.6 Cell data_offset = offset_image ? 56*sizeof(Cell) : 0;
579 : anton 1.1 UCell check_sum;
580 : pazsan 1.15 Cell ausize = ((RELINFOBITS == 8) ? 0 :
581 :     (RELINFOBITS == 16) ? 1 :
582 :     (RELINFOBITS == 32) ? 2 : 3);
583 :     Cell charsize = ((sizeof(Char) == 1) ? 0 :
584 :     (sizeof(Char) == 2) ? 1 :
585 :     (sizeof(Char) == 4) ? 2 : 3) + ausize;
586 :     Cell cellsize = ((sizeof(Cell) == 1) ? 0 :
587 :     (sizeof(Cell) == 2) ? 1 :
588 :     (sizeof(Cell) == 4) ? 2 : 3) + ausize;
589 : anton 1.21 Cell sizebyte = (ausize << 5) + (charsize << 3) + (cellsize << 1) +
590 :     #ifdef WORDS_BIGENDIAN
591 :     0
592 :     #else
593 :     1
594 :     #endif
595 :     ;
596 : anton 1.1
597 : anton 1.43 vm_prims = engine(0,0,0,0,0);
598 : anton 1.47 check_prims(vm_prims);
599 : anton 1.1 #ifndef DOUBLY_INDIRECT
600 : anton 1.59 #ifdef PRINT_SUPER_LENGTHS
601 :     print_super_lengths();
602 :     #endif
603 : anton 1.43 check_sum = checksum(vm_prims);
604 : anton 1.1 #else /* defined(DOUBLY_INDIRECT) */
605 : anton 1.43 check_sum = (UCell)vm_prims;
606 : anton 1.1 #endif /* defined(DOUBLY_INDIRECT) */
607 : pazsan 1.10
608 :     do {
609 :     if(fread(magic,sizeof(Char),8,imagefile) < 8) {
610 : pazsan 1.15 fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.4) image.\n",
611 : pazsan 1.10 progname, filename);
612 :     exit(1);
613 : anton 1.1 }
614 : pazsan 1.10 preamblesize+=8;
615 : pazsan 1.15 } while(memcmp(magic,"Gforth2",7));
616 : anton 1.17 magic7 = magic[7];
617 : anton 1.1 if (debug) {
618 : anton 1.17 magic[7]='\0';
619 : anton 1.21 fprintf(stderr,"Magic found: %s ", magic);
620 :     print_sizes(magic7);
621 : anton 1.1 }
622 :    
623 : anton 1.21 if (magic7 != sizebyte)
624 :     {
625 :     fprintf(stderr,"This image is: ");
626 :     print_sizes(magic7);
627 :     fprintf(stderr,"whereas the machine is ");
628 :     print_sizes(sizebyte);
629 : anton 1.1 exit(-2);
630 :     };
631 :    
632 :     fread((void *)&header,sizeof(ImageHeader),1,imagefile);
633 : pazsan 1.10
634 :     set_stack_sizes(&header);
635 : anton 1.1
636 :     #if HAVE_GETPAGESIZE
637 :     pagesize=getpagesize(); /* Linux/GNU libc offers this */
638 :     #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
639 :     pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
640 :     #elif PAGESIZE
641 :     pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
642 :     #endif
643 :     if (debug)
644 : jwilke 1.5 fprintf(stderr,"pagesize=%ld\n",(unsigned long) pagesize);
645 : anton 1.1
646 : anton 1.34 image = dict_alloc_read(imagefile, preamblesize+header.image_size,
647 :     preamblesize+dictsize, data_offset);
648 : anton 1.33 imp=image+preamblesize;
649 : anton 1.57 alloc_stacks((ImageHeader *)imp);
650 : anton 1.1 if (clear_dictionary)
651 : anton 1.33 memset(imp+header.image_size, 0, dictsize-header.image_size);
652 : jwilke 1.46 if(header.base==0 || header.base == 0x100) {
653 : anton 1.1 Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
654 :     char reloc_bits[reloc_size];
655 : anton 1.33 fseek(imagefile, preamblesize+header.image_size, SEEK_SET);
656 : pazsan 1.10 fread(reloc_bits, 1, reloc_size, imagefile);
657 : jwilke 1.45 relocate((Cell *)imp, reloc_bits, header.image_size, header.base, vm_prims);
658 : anton 1.1 #if 0
659 :     { /* let's see what the relocator did */
660 :     FILE *snapshot=fopen("snapshot.fi","wb");
661 :     fwrite(image,1,imagesize,snapshot);
662 :     fclose(snapshot);
663 :     }
664 :     #endif
665 : jwilke 1.46 }
666 :     else if(header.base!=imp) {
667 :     fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
668 :     progname, (unsigned long)header.base, (unsigned long)imp);
669 :     exit(1);
670 : anton 1.1 }
671 :     if (header.checksum==0)
672 :     ((ImageHeader *)imp)->checksum=check_sum;
673 :     else if (header.checksum != check_sum) {
674 :     fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
675 :     progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
676 :     exit(1);
677 :     }
678 : anton 1.53 #ifdef DOUBLY_INDIRECT
679 :     ((ImageHeader *)imp)->xt_base = xts;
680 :     #endif
681 : anton 1.1 fclose(imagefile);
682 :    
683 : anton 1.56 /* unnecessary, except maybe for CODE words */
684 :     /* FLUSH_ICACHE(imp, header.image_size);*/
685 : anton 1.1
686 :     return imp;
687 :     }
688 :    
689 : anton 1.28 /* index of last '/' or '\' in file, 0 if there is none. !! Hmm, could
690 :     be implemented with strrchr and the separator should be
691 :     OS-dependent */
692 : anton 1.1 int onlypath(char *file)
693 : pazsan 1.10 {
694 :     int i;
695 : anton 1.1 i=strlen(file);
696 : pazsan 1.10 while (i) {
697 :     if (file[i]=='\\' || file[i]=='/') break;
698 :     i--;
699 :     }
700 :     return i;
701 : anton 1.1 }
702 :    
703 :     FILE *openimage(char *fullfilename)
704 : pazsan 1.10 {
705 :     FILE *image_file;
706 : anton 1.28 char * expfilename = tilde_cstr(fullfilename, strlen(fullfilename), 1);
707 : pazsan 1.10
708 : anton 1.28 image_file=fopen(expfilename,"rb");
709 : anton 1.1 if (image_file!=NULL && debug)
710 : anton 1.28 fprintf(stderr, "Opened image file: %s\n", expfilename);
711 : pazsan 1.10 return image_file;
712 : anton 1.1 }
713 :    
714 : anton 1.28 /* try to open image file concat(path[0:len],imagename) */
715 : anton 1.1 FILE *checkimage(char *path, int len, char *imagename)
716 : pazsan 1.10 {
717 :     int dirlen=len;
718 : anton 1.1 char fullfilename[dirlen+strlen(imagename)+2];
719 : pazsan 1.10
720 : anton 1.1 memcpy(fullfilename, path, dirlen);
721 :     if (fullfilename[dirlen-1]!='/')
722 :     fullfilename[dirlen++]='/';
723 :     strcpy(fullfilename+dirlen,imagename);
724 : pazsan 1.10 return openimage(fullfilename);
725 : anton 1.1 }
726 :    
727 : pazsan 1.10 FILE * open_image_file(char * imagename, char * path)
728 : anton 1.1 {
729 : pazsan 1.10 FILE * image_file=NULL;
730 : anton 1.28 char *origpath=path;
731 : pazsan 1.10
732 :     if(strchr(imagename, '/')==NULL) {
733 :     /* first check the directory where the exe file is in !! 01may97jaw */
734 :     if (onlypath(progname))
735 :     image_file=checkimage(progname, onlypath(progname), imagename);
736 :     if (!image_file)
737 :     do {
738 :     char *pend=strchr(path, PATHSEP);
739 :     if (pend==NULL)
740 :     pend=path+strlen(path);
741 :     if (strlen(path)==0) break;
742 :     image_file=checkimage(path, pend-path, imagename);
743 :     path=pend+(*pend==PATHSEP);
744 :     } while (image_file==NULL);
745 :     } else {
746 :     image_file=openimage(imagename);
747 :     }
748 : anton 1.1
749 : pazsan 1.10 if (!image_file) {
750 :     fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
751 : anton 1.28 progname, imagename, origpath);
752 : pazsan 1.10 exit(1);
753 : anton 1.7 }
754 :    
755 : pazsan 1.10 return image_file;
756 :     }
757 : pazsan 1.11 #endif
758 :    
759 :     #ifdef HAS_OS
760 :     UCell convsize(char *s, UCell elemsize)
761 :     /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
762 :     of bytes. the letter at the end indicates the unit, where e stands
763 :     for the element size. default is e */
764 :     {
765 :     char *endp;
766 :     UCell n,m;
767 :    
768 :     m = elemsize;
769 :     n = strtoul(s,&endp,0);
770 :     if (endp!=NULL) {
771 :     if (strcmp(endp,"b")==0)
772 :     m=1;
773 :     else if (strcmp(endp,"k")==0)
774 :     m=1024;
775 :     else if (strcmp(endp,"M")==0)
776 :     m=1024*1024;
777 :     else if (strcmp(endp,"G")==0)
778 :     m=1024*1024*1024;
779 :     else if (strcmp(endp,"T")==0) {
780 :     #if (SIZEOF_CHAR_P > 4)
781 : anton 1.24 m=1024L*1024*1024*1024;
782 : pazsan 1.11 #else
783 :     fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp);
784 :     exit(1);
785 :     #endif
786 :     } else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
787 :     fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
788 :     exit(1);
789 :     }
790 :     }
791 :     return n*m;
792 :     }
793 : pazsan 1.10
794 :     void gforth_args(int argc, char ** argv, char ** path, char ** imagename)
795 :     {
796 :     int c;
797 :    
798 : anton 1.1 opterr=0;
799 :     while (1) {
800 :     int option_index=0;
801 :     static struct option opts[] = {
802 : anton 1.29 {"appl-image", required_argument, NULL, 'a'},
803 : anton 1.1 {"image-file", required_argument, NULL, 'i'},
804 :     {"dictionary-size", required_argument, NULL, 'm'},
805 :     {"data-stack-size", required_argument, NULL, 'd'},
806 :     {"return-stack-size", required_argument, NULL, 'r'},
807 :     {"fp-stack-size", required_argument, NULL, 'f'},
808 :     {"locals-stack-size", required_argument, NULL, 'l'},
809 :     {"path", required_argument, NULL, 'p'},
810 :     {"version", no_argument, NULL, 'v'},
811 :     {"help", no_argument, NULL, 'h'},
812 :     /* put something != 0 into offset_image */
813 :     {"offset-image", no_argument, &offset_image, 1},
814 :     {"no-offset-im", no_argument, &offset_image, 0},
815 :     {"clear-dictionary", no_argument, &clear_dictionary, 1},
816 : anton 1.4 {"die-on-signal", no_argument, &die_on_signal, 1},
817 : anton 1.1 {"debug", no_argument, &debug, 1},
818 : anton 1.60 {"no-super", no_argument, &no_super, 1},
819 :     {"no-dynamic", no_argument, &no_dynamic, 1},
820 : anton 1.66 {"dynamic", no_argument, &no_dynamic, 0},
821 : anton 1.1 {0,0,0,0}
822 :     /* no-init-file, no-rc? */
823 :     };
824 :    
825 : pazsan 1.36 c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vhoncsx", opts, &option_index);
826 : anton 1.1
827 :     switch (c) {
828 : anton 1.29 case EOF: return;
829 :     case '?': optind--; return;
830 :     case 'a': *imagename = optarg; return;
831 : pazsan 1.10 case 'i': *imagename = optarg; break;
832 : anton 1.1 case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
833 :     case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
834 :     case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
835 :     case 'f': fsize = convsize(optarg,sizeof(Float)); break;
836 :     case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
837 : pazsan 1.10 case 'p': *path = optarg; break;
838 : pazsan 1.36 case 'o': offset_image = 1; break;
839 :     case 'n': offset_image = 0; break;
840 :     case 'c': clear_dictionary = 1; break;
841 :     case 's': die_on_signal = 1; break;
842 :     case 'x': debug = 1; break;
843 : anton 1.8 case 'v': fprintf(stderr, "gforth %s\n", VERSION); exit(0);
844 : anton 1.1 case 'h':
845 : anton 1.29 fprintf(stderr, "Usage: %s [engine options] ['--'] [image arguments]\n\
846 : anton 1.1 Engine Options:\n\
847 : anton 1.29 --appl-image FILE equivalent to '--image-file=FILE --'\n\
848 : pazsan 1.10 --clear-dictionary Initialize the dictionary with 0 bytes\n\
849 :     -d SIZE, --data-stack-size=SIZE Specify data stack size\n\
850 :     --debug Print debugging information during startup\n\
851 :     --die-on-signal exit instead of CATCHing some signals\n\
852 : anton 1.66 --dynamic use dynamic native code\n\
853 : pazsan 1.10 -f SIZE, --fp-stack-size=SIZE Specify floating point stack size\n\
854 :     -h, --help Print this message and exit\n\
855 :     -i FILE, --image-file=FILE Use image FILE instead of `gforth.fi'\n\
856 :     -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
857 :     -m SIZE, --dictionary-size=SIZE Specify Forth dictionary size\n\
858 : anton 1.60 --no-dynamic Use only statically compiled primitives\n\
859 : pazsan 1.10 --no-offset-im Load image at normal position\n\
860 : anton 1.60 --no-super No dynamically formed superinstructions\n\
861 : pazsan 1.10 --offset-image Load image at a different position\n\
862 :     -p PATH, --path=PATH Search path for finding image and sources\n\
863 :     -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
864 : anton 1.66 -v, --version Print engine version and exit\n\
865 : anton 1.1 SIZE arguments consist of an integer followed by a unit. The unit can be\n\
866 : pazsan 1.10 `b' (byte), `e' (element; default), `k' (KB), `M' (MB), `G' (GB) or `T' (TB).\n",
867 :     argv[0]);
868 :     optind--;
869 :     return;
870 : anton 1.1 }
871 :     }
872 : pazsan 1.10 }
873 : pazsan 1.11 #endif
874 : pazsan 1.10
875 :     #ifdef INCLUDE_IMAGE
876 :     extern Cell image[];
877 :     extern const char reloc_bits[];
878 :     #endif
879 :    
880 : pazsan 1.67 DCell double2ll(Float r)
881 :     {
882 :     #ifndef BUGGY_LONG_LONG
883 :     return (DCell)(r);
884 :     #else
885 :     DCell d;
886 :     d.hi = ldexp(r,-(int)(CELL_BITS)) - (r<0);
887 :     d.lo = r-ldexp((Float)d.hi,CELL_BITS);
888 :     return d;
889 :     #endif
890 :     }
891 :    
892 : pazsan 1.10 int main(int argc, char **argv, char **env)
893 :     {
894 : pazsan 1.30 #ifdef HAS_OS
895 : pazsan 1.10 char *path = getenv("GFORTHPATH") ? : DEFAULTPATH;
896 : pazsan 1.30 #else
897 :     char *path = DEFAULTPATH;
898 :     #endif
899 : pazsan 1.13 #ifndef INCLUDE_IMAGE
900 : pazsan 1.10 char *imagename="gforth.fi";
901 :     FILE *image_file;
902 :     Address image;
903 :     #endif
904 :     int retvalue;
905 :    
906 : anton 1.56 #if defined(i386) && defined(ALIGNMENT_CHECK)
907 : pazsan 1.10 /* turn on alignment checks on the 486.
908 :     * on the 386 this should have no effect. */
909 :     __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
910 :     /* this is unusable with Linux' libc.4.6.27, because this library is
911 :     not alignment-clean; we would have to replace some library
912 :     functions (e.g., memcpy) to make it work. Also GCC doesn't try to keep
913 :     the stack FP-aligned. */
914 :     #endif
915 :    
916 :     /* buffering of the user output device */
917 : pazsan 1.11 #ifdef _IONBF
918 : pazsan 1.10 if (isatty(fileno(stdout))) {
919 :     fflush(stdout);
920 :     setvbuf(stdout,NULL,_IONBF,0);
921 : anton 1.1 }
922 : pazsan 1.11 #endif
923 : anton 1.1
924 : pazsan 1.10 progname = argv[0];
925 :    
926 : pazsan 1.11 #ifdef HAS_OS
927 : pazsan 1.10 gforth_args(argc, argv, &path, &imagename);
928 : pazsan 1.11 #endif
929 : pazsan 1.10
930 :     #ifdef INCLUDE_IMAGE
931 :     set_stack_sizes((ImageHeader *)image);
932 : pazsan 1.22 if(((ImageHeader *)image)->base != image)
933 :     relocate(image, reloc_bits, ((ImageHeader *)image)->image_size,
934 :     (Label*)engine(0, 0, 0, 0, 0));
935 : pazsan 1.10 alloc_stacks((ImageHeader *)image);
936 :     #else
937 :     image_file = open_image_file(imagename, path);
938 :     image = loader(image_file, imagename);
939 :     #endif
940 : anton 1.24 gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */
941 : anton 1.1
942 :     {
943 : pazsan 1.10 char path2[strlen(path)+1];
944 : anton 1.1 char *p1, *p2;
945 :     Cell environ[]= {
946 :     (Cell)argc-(optind-1),
947 :     (Cell)(argv+(optind-1)),
948 : pazsan 1.10 (Cell)strlen(path),
949 : anton 1.1 (Cell)path2};
950 :     argv[optind-1] = progname;
951 :     /*
952 :     for (i=0; i<environ[0]; i++)
953 :     printf("%s\n", ((char **)(environ[1]))[i]);
954 :     */
955 :     /* make path OS-independent by replacing path separators with NUL */
956 : pazsan 1.10 for (p1=path, p2=path2; *p1!='\0'; p1++, p2++)
957 : anton 1.1 if (*p1==PATHSEP)
958 :     *p2 = '\0';
959 :     else
960 :     *p2 = *p1;
961 :     *p2='\0';
962 : pazsan 1.10 retvalue = go_forth(image, 4, environ);
963 : anton 1.42 #ifdef VM_PROFILING
964 :     vm_print_profile(stderr);
965 :     #endif
966 : anton 1.1 deprep_terminal();
967 :     }
968 : pazsan 1.13 return retvalue;
969 : anton 1.1 }

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help