[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 :     #if defined(DIRECT_THREADED)
61 :     # define CA(n) (symbols[(n)])
62 :     #else
63 :     # define CA(n) ((Cell)(symbols+(n)))
64 :     #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 : pazsan 1.30 #ifdef HAS_DEBUG
85 : anton 1.1 static int debug=0;
86 : pazsan 1.31 #else
87 :     # define debug 0
88 :     # define perror(x...)
89 :     # define fprintf(x...)
90 : pazsan 1.30 #endif
91 : pazsan 1.31
92 : anton 1.24 ImageHeader *gforth_header;
93 : anton 1.43 Label *vm_prims;
94 : anton 1.1
95 : pazsan 1.30 #ifdef MEMCMP_AS_SUBROUTINE
96 :     int gforth_memcmp(const char * s1, const char * s2, size_t n)
97 :     {
98 :     return memcmp(s1, s2, n);
99 :     }
100 :     #endif
101 :    
102 : anton 1.1 /* image file format:
103 : pazsan 1.15 * "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.4.0 -i\n")
104 : anton 1.1 * padding to a multiple of 8
105 : pazsan 1.15 * magic: "Gforth2x" means format 0.4,
106 :     * where x is a byte with
107 :     * bit 7: reserved = 0
108 :     * bit 6:5: address unit size 2^n octets
109 :     * bit 4:3: character size 2^n octets
110 :     * bit 2:1: cell size 2^n octets
111 :     * bit 0: endian, big=0, little=1.
112 :     * The magic are always 8 octets, no matter what the native AU/character size is
113 : anton 1.1 * padding to max alignment (no padding necessary on current machines)
114 : anton 1.24 * ImageHeader structure (see forth.h)
115 : anton 1.1 * data (size in ImageHeader.image_size)
116 :     * tags ((if relocatable, 1 bit/data cell)
117 :     *
118 :     * tag==1 means that the corresponding word is an address;
119 :     * If the word is >=0, the address is within the image;
120 :     * addresses within the image are given relative to the start of the image.
121 :     * If the word =-1 (CF_NIL), the address is NIL,
122 :     * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
123 :     * If the word =CF(DODOES), it's a DOES> CFA
124 :     * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
125 :     * possibly containing a jump to dodoes)
126 :     * If the word is <CF(DOESJUMP), it's a primitive
127 :     */
128 :    
129 : pazsan 1.10 void relocate(Cell *image, const char *bitstring, int size, Label symbols[])
130 : anton 1.1 {
131 : pazsan 1.16 int i=0, j, k, steps=(size/sizeof(Cell))/RELINFOBITS;
132 : pazsan 1.11 Cell token;
133 : anton 1.1 char bits;
134 : anton 1.37 Cell max_symbols;
135 : anton 1.1
136 :     /* printf("relocating %x[%x]\n", image, size); */
137 : anton 1.37
138 :     for (max_symbols=DOESJUMP+1; symbols[max_symbols]!=0; max_symbols++)
139 :     ;
140 : pazsan 1.35 size/=sizeof(Cell);
141 :    
142 : pazsan 1.31 for(k=0; k<=steps; k++) {
143 : pazsan 1.13 for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
144 : anton 1.1 /* fprintf(stderr,"relocate: image[%d]\n", i);*/
145 : pazsan 1.35 if((i < size) && (bits & (1U << (RELINFOBITS-1)))) {
146 :     /* fprintf(stderr,"relocate: image[%d]=%d of %d\n", i, image[i], size/sizeof(Cell)); */
147 : pazsan 1.11 if((token=image[i])<0)
148 :     switch(token)
149 : anton 1.1 {
150 :     case CF_NIL : image[i]=0; break;
151 :     #if !defined(DOUBLY_INDIRECT)
152 :     case CF(DOCOL) :
153 :     case CF(DOVAR) :
154 :     case CF(DOCON) :
155 :     case CF(DOUSER) :
156 :     case CF(DODEFER) :
157 : pazsan 1.11 case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(token)]); break;
158 : anton 1.1 case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
159 :     #endif /* !defined(DOUBLY_INDIRECT) */
160 :     case CF(DODOES) :
161 : anton 1.33 MAKE_DOES_CF(image+i,(Xt *)(image[i+1]+((Cell)image)));
162 : anton 1.1 break;
163 :     default :
164 :     /* printf("Code field generation image[%x]:=CA(%x)\n",
165 :     i, CF(image[i])); */
166 : anton 1.37 if (CF(token)<max_symbols)
167 :     image[i]=(Cell)CA(CF(token));
168 :     else
169 :     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);
170 : anton 1.1 }
171 :     else
172 :     image[i]+=(Cell)image;
173 :     }
174 :     }
175 : pazsan 1.31 }
176 : jwilke 1.26 ((ImageHeader*)(image))->base = (Address) image;
177 : anton 1.1 }
178 :    
179 :     UCell checksum(Label symbols[])
180 :     {
181 :     UCell r=PRIM_VERSION;
182 :     Cell i;
183 :    
184 :     for (i=DOCOL; i<=DOESJUMP; i++) {
185 :     r ^= (UCell)(symbols[i]);
186 :     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
187 :     }
188 :     #ifdef DIRECT_THREADED
189 :     /* we have to consider all the primitives */
190 :     for (; symbols[i]!=(Label)0; i++) {
191 :     r ^= (UCell)(symbols[i]);
192 :     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
193 :     }
194 :     #else
195 :     /* in indirect threaded code all primitives are accessed through the
196 :     symbols table, so we just have to put the base address of symbols
197 :     in the checksum */
198 :     r ^= (UCell)symbols;
199 :     #endif
200 :     return r;
201 :     }
202 :    
203 : anton 1.3 Address verbose_malloc(Cell size)
204 :     {
205 :     Address r;
206 :     /* leave a little room (64B) for stack underflows */
207 :     if ((r = malloc(size+64))==NULL) {
208 :     perror(progname);
209 :     exit(1);
210 :     }
211 :     r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
212 :     if (debug)
213 :     fprintf(stderr, "malloc succeeds, address=$%lx\n", (long)r);
214 :     return r;
215 :     }
216 :    
217 : anton 1.33 static Address next_address=0;
218 :     void after_alloc(Address r, Cell size)
219 :     {
220 :     if (r != (Address)-1) {
221 :     if (debug)
222 :     fprintf(stderr, "success, address=$%lx\n", (long) r);
223 :     if (pagesize != 1)
224 :     next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */
225 :     } else {
226 :     if (debug)
227 :     fprintf(stderr, "failed: %s\n", strerror(errno));
228 :     }
229 :     }
230 :    
231 : anton 1.34 #ifndef MAP_FAILED
232 :     #define MAP_FAILED ((Address) -1)
233 :     #endif
234 :     #ifndef MAP_FILE
235 :     # define MAP_FILE 0
236 :     #endif
237 :     #ifndef MAP_PRIVATE
238 :     # define MAP_PRIVATE 0
239 :     #endif
240 :    
241 :     #if defined(HAVE_MMAP)
242 :     static Address alloc_mmap(Cell size)
243 : anton 1.1 {
244 :     Address r;
245 :    
246 :     #if defined(MAP_ANON)
247 :     if (debug)
248 :     fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_ANON, ...); ", (long)next_address, (long)size);
249 : anton 1.34 r = mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
250 : anton 1.1 #else /* !defined(MAP_ANON) */
251 : anton 1.17 /* Ultrix (at least) does not define MAP_FILE and MAP_PRIVATE (both are
252 :     apparently defaults) */
253 : anton 1.1 static int dev_zero=-1;
254 :    
255 :     if (dev_zero == -1)
256 :     dev_zero = open("/dev/zero", O_RDONLY);
257 :     if (dev_zero == -1) {
258 : anton 1.34 r = MAP_FAILED;
259 : anton 1.1 if (debug)
260 :     fprintf(stderr, "open(\"/dev/zero\"...) failed (%s), no mmap; ",
261 :     strerror(errno));
262 :     } else {
263 :     if (debug)
264 :     fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FILE, dev_zero, ...); ", (long)next_address, (long)size);
265 :     r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE, dev_zero, 0);
266 :     }
267 :     #endif /* !defined(MAP_ANON) */
268 : anton 1.34 after_alloc(r, size);
269 :     return r;
270 :     }
271 :     #endif
272 :    
273 :     Address my_alloc(Cell size)
274 :     {
275 :     #if HAVE_MMAP
276 :     Address r;
277 :    
278 :     r=alloc_mmap(size);
279 :     if (r!=MAP_FAILED)
280 : anton 1.1 return r;
281 :     #endif /* HAVE_MMAP */
282 : anton 1.3 /* use malloc as fallback */
283 :     return verbose_malloc(size);
284 : anton 1.1 }
285 :    
286 : anton 1.3 #if (defined(mips) && !defined(INDIRECT_THREADED))
287 :     /* the 256MB jump restriction on the MIPS architecture makes the
288 :     combination of direct threading and mmap unsafe. */
289 : anton 1.33 #define mips_dict_alloc 1
290 : anton 1.3 #define dict_alloc(size) verbose_malloc(size)
291 :     #else
292 :     #define dict_alloc(size) my_alloc(size)
293 :     #endif
294 :    
295 : anton 1.34 Address dict_alloc_read(FILE *file, Cell imagesize, Cell dictsize, Cell offset)
296 : anton 1.33 {
297 : anton 1.34 Address image = MAP_FAILED;
298 : anton 1.33
299 : anton 1.34 #if defined(HAVE_MMAP) && !defined(mips_dict_alloc)
300 : anton 1.33 if (offset==0) {
301 : anton 1.34 image=alloc_mmap(dictsize);
302 : anton 1.33 if (debug)
303 : anton 1.34 fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FIXED|MAP_FILE, imagefile, 0); ", (long)image, (long)imagesize);
304 :     image = mmap(image, imagesize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FIXED|MAP_FILE|MAP_PRIVATE, fileno(file), 0);
305 :     after_alloc(image,dictsize);
306 : anton 1.33 }
307 : anton 1.34 #endif /* defined(MAP_ANON) && !defined(mips_dict_alloc) */
308 :     if (image == MAP_FAILED) {
309 :     image = dict_alloc(dictsize+offset)+offset;
310 : anton 1.33 rewind(file); /* fseek(imagefile,0L,SEEK_SET); */
311 : anton 1.34 fread(image, 1, imagesize, file);
312 : anton 1.33 }
313 :     return image;
314 :     }
315 :    
316 : pazsan 1.10 void set_stack_sizes(ImageHeader * header)
317 :     {
318 :     if (dictsize==0)
319 :     dictsize = header->dict_size;
320 :     if (dsize==0)
321 :     dsize = header->data_stack_size;
322 :     if (rsize==0)
323 :     rsize = header->return_stack_size;
324 :     if (fsize==0)
325 :     fsize = header->fp_stack_size;
326 :     if (lsize==0)
327 :     lsize = header->locals_stack_size;
328 :     dictsize=maxaligned(dictsize);
329 :     dsize=maxaligned(dsize);
330 :     rsize=maxaligned(rsize);
331 :     lsize=maxaligned(lsize);
332 :     fsize=maxaligned(fsize);
333 :     }
334 :    
335 :     void alloc_stacks(ImageHeader * header)
336 :     {
337 :     header->dict_size=dictsize;
338 :     header->data_stack_size=dsize;
339 :     header->fp_stack_size=fsize;
340 :     header->return_stack_size=rsize;
341 :     header->locals_stack_size=lsize;
342 :    
343 :     header->data_stack_base=my_alloc(dsize);
344 :     header->fp_stack_base=my_alloc(fsize);
345 :     header->return_stack_base=my_alloc(rsize);
346 :     header->locals_stack_base=my_alloc(lsize);
347 :     }
348 :    
349 : pazsan 1.44 #warning You can ignore the warnings about clobbered variables in go_forth
350 : pazsan 1.11 int go_forth(Address image, int stack, Cell *entries)
351 :     {
352 : anton 1.38 volatile ImageHeader *image_header = (ImageHeader *)image;
353 : anton 1.18 Cell *sp0=(Cell*)(image_header->data_stack_base + dsize);
354 : pazsan 1.44 Cell *rp0=(Cell *)(image_header->return_stack_base + rsize);
355 : anton 1.18 Float *fp0=(Float *)(image_header->fp_stack_base + fsize);
356 : pazsan 1.44 #ifdef GFORTH_DEBUGGING
357 : anton 1.38 volatile Cell *orig_rp0=rp0;
358 : pazsan 1.44 #endif
359 : anton 1.18 Address lp0=image_header->locals_stack_base + lsize;
360 :     Xt *ip0=(Xt *)(image_header->boot_entry);
361 : pazsan 1.13 #ifdef SYSSIGNALS
362 : pazsan 1.11 int throw_code;
363 : pazsan 1.13 #endif
364 : pazsan 1.11
365 :     /* ensure that the cached elements (if any) are accessible */
366 : anton 1.41 IF_spTOS(sp0--);
367 :     IF_fpTOS(fp0--);
368 : pazsan 1.11
369 :     for(;stack>0;stack--)
370 : anton 1.18 *--sp0=entries[stack-1];
371 : pazsan 1.11
372 : pazsan 1.30 #ifdef SYSSIGNALS
373 : pazsan 1.11 get_winsize();
374 :    
375 :     install_signal_handlers(); /* right place? */
376 :    
377 :     if ((throw_code=setjmp(throw_jmp_buf))) {
378 :     static Cell signal_data_stack[8];
379 :     static Cell signal_return_stack[8];
380 :     static Float signal_fp_stack[1];
381 : pazsan 1.13
382 : pazsan 1.11 signal_data_stack[7]=throw_code;
383 : anton 1.18
384 :     #ifdef GFORTH_DEBUGGING
385 : anton 1.38 /* fprintf(stderr,"\nrp=%ld\n",(long)rp); */
386 :     if (rp <= orig_rp0 && rp > (Cell *)(image_header->return_stack_base+5)) {
387 : anton 1.18 /* no rstack overflow or underflow */
388 :     rp0 = rp;
389 : anton 1.27 *--rp0 = (Cell)ip;
390 : anton 1.18 }
391 :     else /* I love non-syntactic ifdefs :-) */
392 :     #endif
393 :     rp0 = signal_return_stack+8;
394 : anton 1.25 /* fprintf(stderr, "rp=$%x\n",rp0);*/
395 : pazsan 1.11
396 : anton 1.33 return((int)(Cell)engine(image_header->throw_entry, signal_data_stack+7,
397 : anton 1.18 rp0, signal_fp_stack, 0));
398 : pazsan 1.11 }
399 : pazsan 1.13 #endif
400 : pazsan 1.11
401 : anton 1.33 return((int)(Cell)engine(ip0,sp0,rp0,fp0,lp0));
402 : pazsan 1.11 }
403 :    
404 : anton 1.21
405 : pazsan 1.30 #ifndef INCLUDE_IMAGE
406 : anton 1.21 void print_sizes(Cell sizebyte)
407 :     /* print size information */
408 :     {
409 :     static char* endianstring[]= { " big","little" };
410 :    
411 :     fprintf(stderr,"%s endian, cell=%d bytes, char=%d bytes, au=%d bytes\n",
412 :     endianstring[sizebyte & 1],
413 :     1 << ((sizebyte >> 1) & 3),
414 :     1 << ((sizebyte >> 3) & 3),
415 :     1 << ((sizebyte >> 5) & 3));
416 :     }
417 :    
418 : anton 1.1 Address loader(FILE *imagefile, char* filename)
419 :     /* returns the address of the image proper (after the preamble) */
420 :     {
421 :     ImageHeader header;
422 :     Address image;
423 :     Address imp; /* image+preamble */
424 : anton 1.17 Char magic[8];
425 :     char magic7; /* size byte of magic number */
426 : anton 1.1 Cell preamblesize=0;
427 : pazsan 1.6 Cell data_offset = offset_image ? 56*sizeof(Cell) : 0;
428 : anton 1.1 UCell check_sum;
429 : pazsan 1.15 Cell ausize = ((RELINFOBITS == 8) ? 0 :
430 :     (RELINFOBITS == 16) ? 1 :
431 :     (RELINFOBITS == 32) ? 2 : 3);
432 :     Cell charsize = ((sizeof(Char) == 1) ? 0 :
433 :     (sizeof(Char) == 2) ? 1 :
434 :     (sizeof(Char) == 4) ? 2 : 3) + ausize;
435 :     Cell cellsize = ((sizeof(Cell) == 1) ? 0 :
436 :     (sizeof(Cell) == 2) ? 1 :
437 :     (sizeof(Cell) == 4) ? 2 : 3) + ausize;
438 : anton 1.21 Cell sizebyte = (ausize << 5) + (charsize << 3) + (cellsize << 1) +
439 :     #ifdef WORDS_BIGENDIAN
440 :     0
441 :     #else
442 :     1
443 :     #endif
444 :     ;
445 : anton 1.1
446 : anton 1.43 vm_prims = engine(0,0,0,0,0);
447 : anton 1.1 #ifndef DOUBLY_INDIRECT
448 : anton 1.43 check_sum = checksum(vm_prims);
449 : anton 1.1 #else /* defined(DOUBLY_INDIRECT) */
450 : anton 1.43 check_sum = (UCell)vm_prims;
451 : anton 1.1 #endif /* defined(DOUBLY_INDIRECT) */
452 : pazsan 1.10
453 :     do {
454 :     if(fread(magic,sizeof(Char),8,imagefile) < 8) {
455 : pazsan 1.15 fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.4) image.\n",
456 : pazsan 1.10 progname, filename);
457 :     exit(1);
458 : anton 1.1 }
459 : pazsan 1.10 preamblesize+=8;
460 : pazsan 1.15 } while(memcmp(magic,"Gforth2",7));
461 : anton 1.17 magic7 = magic[7];
462 : anton 1.1 if (debug) {
463 : anton 1.17 magic[7]='\0';
464 : anton 1.21 fprintf(stderr,"Magic found: %s ", magic);
465 :     print_sizes(magic7);
466 : anton 1.1 }
467 :    
468 : anton 1.21 if (magic7 != sizebyte)
469 :     {
470 :     fprintf(stderr,"This image is: ");
471 :     print_sizes(magic7);
472 :     fprintf(stderr,"whereas the machine is ");
473 :     print_sizes(sizebyte);
474 : anton 1.1 exit(-2);
475 :     };
476 :    
477 :     fread((void *)&header,sizeof(ImageHeader),1,imagefile);
478 : pazsan 1.10
479 :     set_stack_sizes(&header);
480 : anton 1.1
481 :     #if HAVE_GETPAGESIZE
482 :     pagesize=getpagesize(); /* Linux/GNU libc offers this */
483 :     #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
484 :     pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
485 :     #elif PAGESIZE
486 :     pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
487 :     #endif
488 :     if (debug)
489 : jwilke 1.5 fprintf(stderr,"pagesize=%ld\n",(unsigned long) pagesize);
490 : anton 1.1
491 : anton 1.34 image = dict_alloc_read(imagefile, preamblesize+header.image_size,
492 :     preamblesize+dictsize, data_offset);
493 : anton 1.33 imp=image+preamblesize;
494 : anton 1.1 if (clear_dictionary)
495 : anton 1.33 memset(imp+header.image_size, 0, dictsize-header.image_size);
496 : anton 1.1 if(header.base==0) {
497 :     Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
498 :     char reloc_bits[reloc_size];
499 : anton 1.33 fseek(imagefile, preamblesize+header.image_size, SEEK_SET);
500 : pazsan 1.10 fread(reloc_bits, 1, reloc_size, imagefile);
501 : anton 1.43 relocate((Cell *)imp, reloc_bits, header.image_size, vm_prims);
502 : anton 1.1 #if 0
503 :     { /* let's see what the relocator did */
504 :     FILE *snapshot=fopen("snapshot.fi","wb");
505 :     fwrite(image,1,imagesize,snapshot);
506 :     fclose(snapshot);
507 :     }
508 :     #endif
509 :     }
510 :     else if(header.base!=imp) {
511 :     fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
512 :     progname, (unsigned long)header.base, (unsigned long)imp);
513 :     exit(1);
514 :     }
515 :     if (header.checksum==0)
516 :     ((ImageHeader *)imp)->checksum=check_sum;
517 :     else if (header.checksum != check_sum) {
518 :     fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
519 :     progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
520 :     exit(1);
521 :     }
522 :     fclose(imagefile);
523 :    
524 : pazsan 1.10 alloc_stacks((ImageHeader *)imp);
525 : anton 1.1
526 :     CACHE_FLUSH(imp, header.image_size);
527 :    
528 :     return imp;
529 :     }
530 :    
531 : anton 1.28 /* index of last '/' or '\' in file, 0 if there is none. !! Hmm, could
532 :     be implemented with strrchr and the separator should be
533 :     OS-dependent */
534 : anton 1.1 int onlypath(char *file)
535 : pazsan 1.10 {
536 :     int i;
537 : anton 1.1 i=strlen(file);
538 : pazsan 1.10 while (i) {
539 :     if (file[i]=='\\' || file[i]=='/') break;
540 :     i--;
541 :     }
542 :     return i;
543 : anton 1.1 }
544 :    
545 :     FILE *openimage(char *fullfilename)
546 : pazsan 1.10 {
547 :     FILE *image_file;
548 : anton 1.28 char * expfilename = tilde_cstr(fullfilename, strlen(fullfilename), 1);
549 : pazsan 1.10
550 : anton 1.28 image_file=fopen(expfilename,"rb");
551 : anton 1.1 if (image_file!=NULL && debug)
552 : anton 1.28 fprintf(stderr, "Opened image file: %s\n", expfilename);
553 : pazsan 1.10 return image_file;
554 : anton 1.1 }
555 :    
556 : anton 1.28 /* try to open image file concat(path[0:len],imagename) */
557 : anton 1.1 FILE *checkimage(char *path, int len, char *imagename)
558 : pazsan 1.10 {
559 :     int dirlen=len;
560 : anton 1.1 char fullfilename[dirlen+strlen(imagename)+2];
561 : pazsan 1.10
562 : anton 1.1 memcpy(fullfilename, path, dirlen);
563 :     if (fullfilename[dirlen-1]!='/')
564 :     fullfilename[dirlen++]='/';
565 :     strcpy(fullfilename+dirlen,imagename);
566 : pazsan 1.10 return openimage(fullfilename);
567 : anton 1.1 }
568 :    
569 : pazsan 1.10 FILE * open_image_file(char * imagename, char * path)
570 : anton 1.1 {
571 : pazsan 1.10 FILE * image_file=NULL;
572 : anton 1.28 char *origpath=path;
573 : pazsan 1.10
574 :     if(strchr(imagename, '/')==NULL) {
575 :     /* first check the directory where the exe file is in !! 01may97jaw */
576 :     if (onlypath(progname))
577 :     image_file=checkimage(progname, onlypath(progname), imagename);
578 :     if (!image_file)
579 :     do {
580 :     char *pend=strchr(path, PATHSEP);
581 :     if (pend==NULL)
582 :     pend=path+strlen(path);
583 :     if (strlen(path)==0) break;
584 :     image_file=checkimage(path, pend-path, imagename);
585 :     path=pend+(*pend==PATHSEP);
586 :     } while (image_file==NULL);
587 :     } else {
588 :     image_file=openimage(imagename);
589 :     }
590 : anton 1.1
591 : pazsan 1.10 if (!image_file) {
592 :     fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
593 : anton 1.28 progname, imagename, origpath);
594 : pazsan 1.10 exit(1);
595 : anton 1.7 }
596 :    
597 : pazsan 1.10 return image_file;
598 :     }
599 : pazsan 1.11 #endif
600 :    
601 :     #ifdef HAS_OS
602 :     UCell convsize(char *s, UCell elemsize)
603 :     /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
604 :     of bytes. the letter at the end indicates the unit, where e stands
605 :     for the element size. default is e */
606 :     {
607 :     char *endp;
608 :     UCell n,m;
609 :    
610 :     m = elemsize;
611 :     n = strtoul(s,&endp,0);
612 :     if (endp!=NULL) {
613 :     if (strcmp(endp,"b")==0)
614 :     m=1;
615 :     else if (strcmp(endp,"k")==0)
616 :     m=1024;
617 :     else if (strcmp(endp,"M")==0)
618 :     m=1024*1024;
619 :     else if (strcmp(endp,"G")==0)
620 :     m=1024*1024*1024;
621 :     else if (strcmp(endp,"T")==0) {
622 :     #if (SIZEOF_CHAR_P > 4)
623 : anton 1.24 m=1024L*1024*1024*1024;
624 : pazsan 1.11 #else
625 :     fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp);
626 :     exit(1);
627 :     #endif
628 :     } else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
629 :     fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
630 :     exit(1);
631 :     }
632 :     }
633 :     return n*m;
634 :     }
635 : pazsan 1.10
636 :     void gforth_args(int argc, char ** argv, char ** path, char ** imagename)
637 :     {
638 :     int c;
639 :    
640 : anton 1.1 opterr=0;
641 :     while (1) {
642 :     int option_index=0;
643 :     static struct option opts[] = {
644 : anton 1.29 {"appl-image", required_argument, NULL, 'a'},
645 : anton 1.1 {"image-file", required_argument, NULL, 'i'},
646 :     {"dictionary-size", required_argument, NULL, 'm'},
647 :     {"data-stack-size", required_argument, NULL, 'd'},
648 :     {"return-stack-size", required_argument, NULL, 'r'},
649 :     {"fp-stack-size", required_argument, NULL, 'f'},
650 :     {"locals-stack-size", required_argument, NULL, 'l'},
651 :     {"path", required_argument, NULL, 'p'},
652 :     {"version", no_argument, NULL, 'v'},
653 :     {"help", no_argument, NULL, 'h'},
654 :     /* put something != 0 into offset_image */
655 :     {"offset-image", no_argument, &offset_image, 1},
656 :     {"no-offset-im", no_argument, &offset_image, 0},
657 :     {"clear-dictionary", no_argument, &clear_dictionary, 1},
658 : anton 1.4 {"die-on-signal", no_argument, &die_on_signal, 1},
659 : anton 1.1 {"debug", no_argument, &debug, 1},
660 :     {0,0,0,0}
661 :     /* no-init-file, no-rc? */
662 :     };
663 :    
664 : pazsan 1.36 c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vhoncsx", opts, &option_index);
665 : anton 1.1
666 :     switch (c) {
667 : anton 1.29 case EOF: return;
668 :     case '?': optind--; return;
669 :     case 'a': *imagename = optarg; return;
670 : pazsan 1.10 case 'i': *imagename = optarg; break;
671 : anton 1.1 case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
672 :     case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
673 :     case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
674 :     case 'f': fsize = convsize(optarg,sizeof(Float)); break;
675 :     case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
676 : pazsan 1.10 case 'p': *path = optarg; break;
677 : pazsan 1.36 case 'o': offset_image = 1; break;
678 :     case 'n': offset_image = 0; break;
679 :     case 'c': clear_dictionary = 1; break;
680 :     case 's': die_on_signal = 1; break;
681 :     case 'x': debug = 1; break;
682 : anton 1.8 case 'v': fprintf(stderr, "gforth %s\n", VERSION); exit(0);
683 : anton 1.1 case 'h':
684 : anton 1.29 fprintf(stderr, "Usage: %s [engine options] ['--'] [image arguments]\n\
685 : anton 1.1 Engine Options:\n\
686 : anton 1.29 --appl-image FILE equivalent to '--image-file=FILE --'\n\
687 : pazsan 1.10 --clear-dictionary Initialize the dictionary with 0 bytes\n\
688 :     -d SIZE, --data-stack-size=SIZE Specify data stack size\n\
689 :     --debug Print debugging information during startup\n\
690 :     --die-on-signal exit instead of CATCHing some signals\n\
691 :     -f SIZE, --fp-stack-size=SIZE Specify floating point stack size\n\
692 :     -h, --help Print this message and exit\n\
693 :     -i FILE, --image-file=FILE Use image FILE instead of `gforth.fi'\n\
694 :     -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
695 :     -m SIZE, --dictionary-size=SIZE Specify Forth dictionary size\n\
696 :     --no-offset-im Load image at normal position\n\
697 :     --offset-image Load image at a different position\n\
698 :     -p PATH, --path=PATH Search path for finding image and sources\n\
699 :     -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
700 :     -v, --version Print version and exit\n\
701 : anton 1.1 SIZE arguments consist of an integer followed by a unit. The unit can be\n\
702 : pazsan 1.10 `b' (byte), `e' (element; default), `k' (KB), `M' (MB), `G' (GB) or `T' (TB).\n",
703 :     argv[0]);
704 :     optind--;
705 :     return;
706 : anton 1.1 }
707 :     }
708 : pazsan 1.10 }
709 : pazsan 1.11 #endif
710 : pazsan 1.10
711 :     #ifdef INCLUDE_IMAGE
712 :     extern Cell image[];
713 :     extern const char reloc_bits[];
714 :     #endif
715 :    
716 :     int main(int argc, char **argv, char **env)
717 :     {
718 : pazsan 1.30 #ifdef HAS_OS
719 : pazsan 1.10 char *path = getenv("GFORTHPATH") ? : DEFAULTPATH;
720 : pazsan 1.30 #else
721 :     char *path = DEFAULTPATH;
722 :     #endif
723 : pazsan 1.13 #ifndef INCLUDE_IMAGE
724 : pazsan 1.10 char *imagename="gforth.fi";
725 :     FILE *image_file;
726 :     Address image;
727 :     #endif
728 :     int retvalue;
729 :    
730 :     #if defined(i386) && defined(ALIGNMENT_CHECK) && !defined(DIRECT_THREADED)
731 :     /* turn on alignment checks on the 486.
732 :     * on the 386 this should have no effect. */
733 :     __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
734 :     /* this is unusable with Linux' libc.4.6.27, because this library is
735 :     not alignment-clean; we would have to replace some library
736 :     functions (e.g., memcpy) to make it work. Also GCC doesn't try to keep
737 :     the stack FP-aligned. */
738 :     #endif
739 :    
740 :     /* buffering of the user output device */
741 : pazsan 1.11 #ifdef _IONBF
742 : pazsan 1.10 if (isatty(fileno(stdout))) {
743 :     fflush(stdout);
744 :     setvbuf(stdout,NULL,_IONBF,0);
745 : anton 1.1 }
746 : pazsan 1.11 #endif
747 : anton 1.1
748 : pazsan 1.10 progname = argv[0];
749 :    
750 : pazsan 1.11 #ifdef HAS_OS
751 : pazsan 1.10 gforth_args(argc, argv, &path, &imagename);
752 : pazsan 1.11 #endif
753 : pazsan 1.10
754 :     #ifdef INCLUDE_IMAGE
755 :     set_stack_sizes((ImageHeader *)image);
756 : pazsan 1.22 if(((ImageHeader *)image)->base != image)
757 :     relocate(image, reloc_bits, ((ImageHeader *)image)->image_size,
758 :     (Label*)engine(0, 0, 0, 0, 0));
759 : pazsan 1.10 alloc_stacks((ImageHeader *)image);
760 :     #else
761 :     image_file = open_image_file(imagename, path);
762 :     image = loader(image_file, imagename);
763 :     #endif
764 : anton 1.24 gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */
765 : anton 1.1
766 :     {
767 : pazsan 1.10 char path2[strlen(path)+1];
768 : anton 1.1 char *p1, *p2;
769 :     Cell environ[]= {
770 :     (Cell)argc-(optind-1),
771 :     (Cell)(argv+(optind-1)),
772 : pazsan 1.10 (Cell)strlen(path),
773 : anton 1.1 (Cell)path2};
774 :     argv[optind-1] = progname;
775 :     /*
776 :     for (i=0; i<environ[0]; i++)
777 :     printf("%s\n", ((char **)(environ[1]))[i]);
778 :     */
779 :     /* make path OS-independent by replacing path separators with NUL */
780 : pazsan 1.10 for (p1=path, p2=path2; *p1!='\0'; p1++, p2++)
781 : anton 1.1 if (*p1==PATHSEP)
782 :     *p2 = '\0';
783 :     else
784 :     *p2 = *p1;
785 :     *p2='\0';
786 : pazsan 1.10 retvalue = go_forth(image, 4, environ);
787 : anton 1.42 #ifdef VM_PROFILING
788 :     vm_print_profile(stderr);
789 :     #endif
790 : anton 1.1 deprep_terminal();
791 :     }
792 : pazsan 1.13 return retvalue;
793 : anton 1.1 }

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help