[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 : anton 1.82 #include "forth.h"
25 : anton 1.1 #include <errno.h>
26 :     #include <ctype.h>
27 :     #include <stdio.h>
28 : pazsan 1.2 #include <unistd.h>
29 : anton 1.1 #include <string.h>
30 :     #include <math.h>
31 :     #include <sys/types.h>
32 : pazsan 1.32 #ifndef STANDALONE
33 : anton 1.1 #include <sys/stat.h>
34 : pazsan 1.32 #endif
35 : anton 1.1 #include <fcntl.h>
36 :     #include <assert.h>
37 :     #include <stdlib.h>
38 : pazsan 1.11 #ifndef STANDALONE
39 : anton 1.1 #if HAVE_SYS_MMAN_H
40 :     #include <sys/mman.h>
41 :     #endif
42 : pazsan 1.11 #endif
43 : anton 1.1 #include "io.h"
44 :     #include "getopt.h"
45 : pazsan 1.11 #ifdef STANDALONE
46 :     #include <systypes.h>
47 :     #endif
48 : anton 1.1
49 : anton 1.79 /* global variables for engine.c
50 :     We put them here because engine.c is compiled several times in
51 :     different ways for the same engine. */
52 :     Cell *SP;
53 :     Float *FP;
54 :     Address UP=NULL;
55 :    
56 :     #ifdef GFORTH_DEBUGGING
57 :     /* define some VM registers as global variables, so they survive exceptions;
58 :     global register variables are not up to the task (according to the
59 :     GNU C manual) */
60 :     Xt *saved_ip;
61 :     Cell *rp;
62 :     #endif
63 :    
64 :     #ifdef NO_IP
65 :     Label next_code;
66 :     #endif
67 :    
68 :     #ifdef HAS_FILE
69 :     char* fileattr[6]={"rb","rb","r+b","r+b","wb","wb"};
70 :     char* pfileattr[6]={"r","r","r+","r+","w","w"};
71 :    
72 :     #ifndef O_BINARY
73 :     #define O_BINARY 0
74 :     #endif
75 :     #ifndef O_TEXT
76 :     #define O_TEXT 0
77 :     #endif
78 :    
79 :     int ufileattr[6]= {
80 :     O_RDONLY|O_BINARY, O_RDONLY|O_BINARY,
81 :     O_RDWR |O_BINARY, O_RDWR |O_BINARY,
82 :     O_WRONLY|O_BINARY, O_WRONLY|O_BINARY };
83 :     #endif
84 :     /* end global vars for engine.c */
85 :    
86 : anton 1.1 #define PRIM_VERSION 1
87 :     /* increment this whenever the primitives change in an incompatible way */
88 :    
89 : pazsan 1.14 #ifndef DEFAULTPATH
90 : anton 1.39 # define DEFAULTPATH "."
91 : pazsan 1.14 #endif
92 :    
93 : anton 1.1 #ifdef MSDOS
94 :     jmp_buf throw_jmp_buf;
95 :     #endif
96 :    
97 : anton 1.56 #if defined(DOUBLY_INDIRECT)
98 :     # define CFA(n) ({Cell _n = (n); ((Cell)(((_n & 0x4000) ? symbols : xts)+(_n&~0x4000UL)));})
99 : anton 1.1 #else
100 : anton 1.56 # define CFA(n) ((Cell)(symbols+((n)&~0x4000UL)))
101 : anton 1.1 #endif
102 :    
103 :     #define maxaligned(n) (typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
104 :    
105 :     static UCell dictsize=0;
106 :     static UCell dsize=0;
107 :     static UCell rsize=0;
108 :     static UCell fsize=0;
109 :     static UCell lsize=0;
110 :     int offset_image=0;
111 : anton 1.4 int die_on_signal=0;
112 : pazsan 1.13 #ifndef INCLUDE_IMAGE
113 : anton 1.1 static int clear_dictionary=0;
114 : anton 1.24 UCell pagesize=1;
115 : pazsan 1.22 char *progname;
116 :     #else
117 :     char *progname = "gforth";
118 :     int optind = 1;
119 : pazsan 1.13 #endif
120 : pazsan 1.31
121 : anton 1.75 #define CODE_BLOCK_SIZE (64*1024)
122 : anton 1.48 Address code_area=0;
123 : anton 1.73 Cell code_area_size = CODE_BLOCK_SIZE;
124 : anton 1.75 Address code_here=NULL+CODE_BLOCK_SIZE; /* does for code-area what HERE
125 :     does for the dictionary */
126 : anton 1.65 Address start_flush=0; /* start of unflushed code */
127 : anton 1.74 Cell last_jump=0; /* if the last prim was compiled without jump, this
128 :     is it's number, otherwise this contains 0 */
129 : anton 1.48
130 : anton 1.60 static int no_super=0; /* true if compile_prim should not fuse prims */
131 : anton 1.81 static int no_dynamic=NO_DYNAMIC_DEFAULT; /* if true, no code is generated
132 :     dynamically */
133 : anton 1.60
134 : pazsan 1.30 #ifdef HAS_DEBUG
135 : anton 1.68 int debug=0;
136 : pazsan 1.31 #else
137 :     # define perror(x...)
138 :     # define fprintf(x...)
139 : pazsan 1.30 #endif
140 : pazsan 1.31
141 : anton 1.24 ImageHeader *gforth_header;
142 : anton 1.43 Label *vm_prims;
143 : anton 1.53 #ifdef DOUBLY_INDIRECT
144 :     Label *xts; /* same content as vm_prims, but should only be used for xts */
145 :     #endif
146 : anton 1.1
147 : pazsan 1.30 #ifdef MEMCMP_AS_SUBROUTINE
148 :     int gforth_memcmp(const char * s1, const char * s2, size_t n)
149 :     {
150 :     return memcmp(s1, s2, n);
151 :     }
152 :     #endif
153 :    
154 : anton 1.1 /* image file format:
155 : pazsan 1.15 * "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.4.0 -i\n")
156 : anton 1.1 * padding to a multiple of 8
157 : anton 1.84 * magic: "Gforth3x" means format 0.6,
158 : pazsan 1.15 * where x is a byte with
159 :     * bit 7: reserved = 0
160 :     * bit 6:5: address unit size 2^n octets
161 :     * bit 4:3: character size 2^n octets
162 :     * bit 2:1: cell size 2^n octets
163 :     * bit 0: endian, big=0, little=1.
164 :     * The magic are always 8 octets, no matter what the native AU/character size is
165 : anton 1.1 * padding to max alignment (no padding necessary on current machines)
166 : anton 1.24 * ImageHeader structure (see forth.h)
167 : anton 1.1 * data (size in ImageHeader.image_size)
168 :     * tags ((if relocatable, 1 bit/data cell)
169 :     *
170 :     * tag==1 means that the corresponding word is an address;
171 :     * If the word is >=0, the address is within the image;
172 :     * addresses within the image are given relative to the start of the image.
173 :     * If the word =-1 (CF_NIL), the address is NIL,
174 :     * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
175 :     * If the word =CF(DODOES), it's a DOES> CFA
176 :     * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
177 :     * possibly containing a jump to dodoes)
178 : anton 1.51 * If the word is <CF(DOESJUMP) and bit 14 is set, it's the xt of a primitive
179 :     * If the word is <CF(DOESJUMP) and bit 14 is clear,
180 :     * it's the threaded code of a primitive
181 : pazsan 1.85 * bits 13..9 of a primitive token state which group the primitive belongs to,
182 :     * bits 8..0 of a primitive token index into the group
183 : anton 1.1 */
184 :    
185 : pazsan 1.85 static Cell groups[32] = {
186 :     0,
187 : anton 1.90 #undef GROUP
188 : pazsan 1.85 #define GROUP(x, n) DOESJUMP+1+n,
189 : anton 1.86 #include "prim_grp.i"
190 : anton 1.90 #undef GROUP
191 : pazsan 1.85 #define GROUP(x, n)
192 :     };
193 :    
194 : jwilke 1.46 void relocate(Cell *image, const char *bitstring,
195 : anton 1.90 int size, Cell base, Label symbols[])
196 : anton 1.1 {
197 : pazsan 1.16 int i=0, j, k, steps=(size/sizeof(Cell))/RELINFOBITS;
198 : pazsan 1.11 Cell token;
199 : anton 1.1 char bits;
200 : anton 1.37 Cell max_symbols;
201 : jwilke 1.46 /*
202 : pazsan 1.85 * A virtual start address that's the real start address minus
203 : jwilke 1.46 * the one in the image
204 :     */
205 : jwilke 1.45 Cell *start = (Cell * ) (((void *) image) - ((void *) base));
206 : anton 1.1
207 : pazsan 1.85 /* group index into table */
208 : jwilke 1.46
209 :     /* printf("relocating to %x[%x] start=%x base=%x\n", image, size, start, base); */
210 : anton 1.37
211 :     for (max_symbols=DOESJUMP+1; symbols[max_symbols]!=0; max_symbols++)
212 :     ;
213 : anton 1.47 max_symbols--;
214 : pazsan 1.35 size/=sizeof(Cell);
215 :    
216 : pazsan 1.31 for(k=0; k<=steps; k++) {
217 : pazsan 1.13 for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
218 : anton 1.1 /* fprintf(stderr,"relocate: image[%d]\n", i);*/
219 : pazsan 1.35 if((i < size) && (bits & (1U << (RELINFOBITS-1)))) {
220 :     /* fprintf(stderr,"relocate: image[%d]=%d of %d\n", i, image[i], size/sizeof(Cell)); */
221 : jwilke 1.45 token=image[i];
222 : pazsan 1.85 if(token<0) {
223 :     int group = (-token & 0x3E00) >> 9;
224 :     if(group == 0) {
225 :     switch(token|0x4000) {
226 : anton 1.1 case CF_NIL : image[i]=0; break;
227 :     #if !defined(DOUBLY_INDIRECT)
228 :     case CF(DOCOL) :
229 :     case CF(DOVAR) :
230 :     case CF(DOCON) :
231 :     case CF(DOUSER) :
232 :     case CF(DODEFER) :
233 : pazsan 1.11 case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(token)]); break;
234 : anton 1.1 case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
235 :     #endif /* !defined(DOUBLY_INDIRECT) */
236 :     case CF(DODOES) :
237 : jwilke 1.45 MAKE_DOES_CF(image+i,(Xt *)(image[i+1]+((Cell)start)));
238 : anton 1.1 break;
239 : pazsan 1.85 default : /* backward compatibility */
240 : anton 1.56 /* printf("Code field generation image[%x]:=CFA(%x)\n",
241 : anton 1.1 i, CF(image[i])); */
242 : anton 1.55 if (CF((token | 0x4000))<max_symbols) {
243 : anton 1.56 image[i]=(Cell)CFA(CF(token));
244 :     #ifdef DIRECT_THREADED
245 :     if ((token & 0x4000) == 0) /* threade code, no CFA */
246 : anton 1.70 compile_prim1(&image[i]);
247 : anton 1.56 #endif
248 : anton 1.55 } else
249 : anton 1.83 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],PACKAGE_VERSION);
250 : anton 1.1 }
251 : pazsan 1.85 } else {
252 :     int tok = -token & 0x1FF;
253 :     if (tok < (groups[group+1]-groups[group])) {
254 :     #if defined(DOUBLY_INDIRECT)
255 :     image[i]=(Cell)CFA(((groups[group]+tok) | (CF(token) & 0x4000)));
256 :     #else
257 :     image[i]=(Cell)CFA((groups[group]+tok));
258 :     #endif
259 :     #ifdef DIRECT_THREADED
260 :     if ((token & 0x4000) == 0) /* threade code, no CFA */
261 :     compile_prim1(&image[i]);
262 :     #endif
263 :     } else
264 :     fprintf(stderr,"Primitive %x, %d of group %d used in this image at $%lx is not implemented by this\n engine (%s); executing this code will crash.\n", -token, tok, group, (long)&image[i],PACKAGE_VERSION);
265 :     }
266 :     } else {
267 : jwilke 1.45 // if base is > 0: 0 is a null reference so don't adjust
268 :     if (token>=base) {
269 :     image[i]+=(Cell)start;
270 :     }
271 : jwilke 1.46 }
272 : anton 1.1 }
273 :     }
274 : pazsan 1.31 }
275 : anton 1.70 finish_code();
276 : jwilke 1.26 ((ImageHeader*)(image))->base = (Address) image;
277 : anton 1.1 }
278 :    
279 :     UCell checksum(Label symbols[])
280 :     {
281 :     UCell r=PRIM_VERSION;
282 :     Cell i;
283 :    
284 :     for (i=DOCOL; i<=DOESJUMP; i++) {
285 :     r ^= (UCell)(symbols[i]);
286 :     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
287 :     }
288 :     #ifdef DIRECT_THREADED
289 :     /* we have to consider all the primitives */
290 :     for (; symbols[i]!=(Label)0; i++) {
291 :     r ^= (UCell)(symbols[i]);
292 :     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
293 :     }
294 :     #else
295 :     /* in indirect threaded code all primitives are accessed through the
296 :     symbols table, so we just have to put the base address of symbols
297 :     in the checksum */
298 :     r ^= (UCell)symbols;
299 :     #endif
300 :     return r;
301 :     }
302 :    
303 : anton 1.3 Address verbose_malloc(Cell size)
304 :     {
305 :     Address r;
306 :     /* leave a little room (64B) for stack underflows */
307 :     if ((r = malloc(size+64))==NULL) {
308 :     perror(progname);
309 :     exit(1);
310 :     }
311 :     r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
312 :     if (debug)
313 :     fprintf(stderr, "malloc succeeds, address=$%lx\n", (long)r);
314 :     return r;
315 :     }
316 :    
317 : anton 1.33 static Address next_address=0;
318 :     void after_alloc(Address r, Cell size)
319 :     {
320 :     if (r != (Address)-1) {
321 :     if (debug)
322 :     fprintf(stderr, "success, address=$%lx\n", (long) r);
323 :     if (pagesize != 1)
324 :     next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */
325 :     } else {
326 :     if (debug)
327 :     fprintf(stderr, "failed: %s\n", strerror(errno));
328 :     }
329 :     }
330 :    
331 : anton 1.34 #ifndef MAP_FAILED
332 :     #define MAP_FAILED ((Address) -1)
333 :     #endif
334 :     #ifndef MAP_FILE
335 :     # define MAP_FILE 0
336 :     #endif
337 :     #ifndef MAP_PRIVATE
338 :     # define MAP_PRIVATE 0
339 :     #endif
340 : anton 1.91 #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
341 :     # define MAP_ANON MAP_ANONYMOUS
342 :     #endif
343 : anton 1.34
344 :     #if defined(HAVE_MMAP)
345 :     static Address alloc_mmap(Cell size)
346 : anton 1.1 {
347 :     Address r;
348 :    
349 :     #if defined(MAP_ANON)
350 :     if (debug)
351 :     fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_ANON, ...); ", (long)next_address, (long)size);
352 : anton 1.34 r = mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
353 : anton 1.1 #else /* !defined(MAP_ANON) */
354 : anton 1.17 /* Ultrix (at least) does not define MAP_FILE and MAP_PRIVATE (both are
355 :     apparently defaults) */
356 : anton 1.1 static int dev_zero=-1;
357 :    
358 :     if (dev_zero == -1)
359 :     dev_zero = open("/dev/zero", O_RDONLY);
360 :     if (dev_zero == -1) {
361 : anton 1.34 r = MAP_FAILED;
362 : anton 1.1 if (debug)
363 :     fprintf(stderr, "open(\"/dev/zero\"...) failed (%s), no mmap; ",
364 :     strerror(errno));
365 :     } else {
366 :     if (debug)
367 :     fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FILE, dev_zero, ...); ", (long)next_address, (long)size);
368 :     r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE, dev_zero, 0);
369 :     }
370 :     #endif /* !defined(MAP_ANON) */
371 : anton 1.34 after_alloc(r, size);
372 :     return r;
373 :     }
374 :     #endif
375 :    
376 :     Address my_alloc(Cell size)
377 :     {
378 :     #if HAVE_MMAP
379 :     Address r;
380 :    
381 :     r=alloc_mmap(size);
382 :     if (r!=MAP_FAILED)
383 : anton 1.1 return r;
384 :     #endif /* HAVE_MMAP */
385 : anton 1.3 /* use malloc as fallback */
386 :     return verbose_malloc(size);
387 : anton 1.1 }
388 :    
389 : anton 1.34 Address dict_alloc_read(FILE *file, Cell imagesize, Cell dictsize, Cell offset)
390 : anton 1.33 {
391 : anton 1.34 Address image = MAP_FAILED;
392 : anton 1.33
393 : anton 1.56 #if defined(HAVE_MMAP)
394 : anton 1.33 if (offset==0) {
395 : anton 1.34 image=alloc_mmap(dictsize);
396 : anton 1.33 if (debug)
397 : anton 1.34 fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FIXED|MAP_FILE, imagefile, 0); ", (long)image, (long)imagesize);
398 :     image = mmap(image, imagesize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FIXED|MAP_FILE|MAP_PRIVATE, fileno(file), 0);
399 :     after_alloc(image,dictsize);
400 : anton 1.33 }
401 : anton 1.56 #endif /* defined(HAVE_MMAP) */
402 : anton 1.34 if (image == MAP_FAILED) {
403 : anton 1.56 image = my_alloc(dictsize+offset)+offset;
404 : anton 1.33 rewind(file); /* fseek(imagefile,0L,SEEK_SET); */
405 : anton 1.34 fread(image, 1, imagesize, file);
406 : anton 1.33 }
407 :     return image;
408 :     }
409 :    
410 : pazsan 1.10 void set_stack_sizes(ImageHeader * header)
411 :     {
412 :     if (dictsize==0)
413 :     dictsize = header->dict_size;
414 :     if (dsize==0)
415 :     dsize = header->data_stack_size;
416 :     if (rsize==0)
417 :     rsize = header->return_stack_size;
418 :     if (fsize==0)
419 :     fsize = header->fp_stack_size;
420 :     if (lsize==0)
421 :     lsize = header->locals_stack_size;
422 :     dictsize=maxaligned(dictsize);
423 :     dsize=maxaligned(dsize);
424 :     rsize=maxaligned(rsize);
425 :     lsize=maxaligned(lsize);
426 :     fsize=maxaligned(fsize);
427 :     }
428 :    
429 :     void alloc_stacks(ImageHeader * header)
430 :     {
431 :     header->dict_size=dictsize;
432 :     header->data_stack_size=dsize;
433 :     header->fp_stack_size=fsize;
434 :     header->return_stack_size=rsize;
435 :     header->locals_stack_size=lsize;
436 :    
437 :     header->data_stack_base=my_alloc(dsize);
438 :     header->fp_stack_base=my_alloc(fsize);
439 :     header->return_stack_base=my_alloc(rsize);
440 :     header->locals_stack_base=my_alloc(lsize);
441 :     }
442 :    
443 : pazsan 1.44 #warning You can ignore the warnings about clobbered variables in go_forth
444 : pazsan 1.11 int go_forth(Address image, int stack, Cell *entries)
445 :     {
446 : anton 1.38 volatile ImageHeader *image_header = (ImageHeader *)image;
447 : anton 1.18 Cell *sp0=(Cell*)(image_header->data_stack_base + dsize);
448 : pazsan 1.44 Cell *rp0=(Cell *)(image_header->return_stack_base + rsize);
449 : anton 1.18 Float *fp0=(Float *)(image_header->fp_stack_base + fsize);
450 : pazsan 1.44 #ifdef GFORTH_DEBUGGING
451 : anton 1.38 volatile Cell *orig_rp0=rp0;
452 : pazsan 1.44 #endif
453 : anton 1.18 Address lp0=image_header->locals_stack_base + lsize;
454 :     Xt *ip0=(Xt *)(image_header->boot_entry);
455 : pazsan 1.13 #ifdef SYSSIGNALS
456 : pazsan 1.11 int throw_code;
457 : pazsan 1.13 #endif
458 : pazsan 1.11
459 :     /* ensure that the cached elements (if any) are accessible */
460 : anton 1.41 IF_spTOS(sp0--);
461 :     IF_fpTOS(fp0--);
462 : pazsan 1.11
463 :     for(;stack>0;stack--)
464 : anton 1.18 *--sp0=entries[stack-1];
465 : pazsan 1.11
466 : pazsan 1.30 #ifdef SYSSIGNALS
467 : pazsan 1.11 get_winsize();
468 :    
469 :     install_signal_handlers(); /* right place? */
470 :    
471 :     if ((throw_code=setjmp(throw_jmp_buf))) {
472 :     static Cell signal_data_stack[8];
473 :     static Cell signal_return_stack[8];
474 :     static Float signal_fp_stack[1];
475 : pazsan 1.13
476 : pazsan 1.11 signal_data_stack[7]=throw_code;
477 : anton 1.18
478 :     #ifdef GFORTH_DEBUGGING
479 : anton 1.38 /* fprintf(stderr,"\nrp=%ld\n",(long)rp); */
480 :     if (rp <= orig_rp0 && rp > (Cell *)(image_header->return_stack_base+5)) {
481 : anton 1.18 /* no rstack overflow or underflow */
482 :     rp0 = rp;
483 : anton 1.63 *--rp0 = (Cell)saved_ip;
484 : anton 1.18 }
485 :     else /* I love non-syntactic ifdefs :-) */
486 :     #endif
487 :     rp0 = signal_return_stack+8;
488 : anton 1.25 /* fprintf(stderr, "rp=$%x\n",rp0);*/
489 : pazsan 1.11
490 : anton 1.33 return((int)(Cell)engine(image_header->throw_entry, signal_data_stack+7,
491 : anton 1.18 rp0, signal_fp_stack, 0));
492 : pazsan 1.11 }
493 : pazsan 1.13 #endif
494 : pazsan 1.11
495 : anton 1.33 return((int)(Cell)engine(ip0,sp0,rp0,fp0,lp0));
496 : pazsan 1.11 }
497 :    
498 : pazsan 1.30 #ifndef INCLUDE_IMAGE
499 : anton 1.21 void print_sizes(Cell sizebyte)
500 :     /* print size information */
501 :     {
502 :     static char* endianstring[]= { " big","little" };
503 :    
504 :     fprintf(stderr,"%s endian, cell=%d bytes, char=%d bytes, au=%d bytes\n",
505 :     endianstring[sizebyte & 1],
506 :     1 << ((sizebyte >> 1) & 3),
507 :     1 << ((sizebyte >> 3) & 3),
508 :     1 << ((sizebyte >> 5) & 3));
509 :     }
510 :    
511 : anton 1.70 #define MAX_IMMARGS 2
512 :    
513 : anton 1.69 #ifndef NO_DYNAMIC
514 : anton 1.47 typedef struct {
515 :     Label start;
516 : anton 1.74 Cell length; /* only includes the jump iff superend is true*/
517 :     Cell restlength; /* length of the rest (i.e., the jump or (on superend) 0) */
518 : anton 1.70 char superend; /* true if primitive ends superinstruction, i.e.,
519 : anton 1.47 unconditional branch, execute, etc. */
520 : anton 1.70 Cell nimmargs;
521 :     struct immarg {
522 :     Cell offset; /* offset of immarg within prim */
523 :     char rel; /* true if immarg is relative */
524 :     } immargs[MAX_IMMARGS];
525 : anton 1.47 } PrimInfo;
526 :    
527 :     PrimInfo *priminfos;
528 : anton 1.76 PrimInfo **decomp_prims;
529 :    
530 : anton 1.90 int compare_priminfo_length(const void *_a, const void *_b)
531 : anton 1.76 {
532 : anton 1.90 PrimInfo **a = (PrimInfo **)_a;
533 :     PrimInfo **b = (PrimInfo **)_b;
534 : anton 1.77 Cell diff = (*a)->length - (*b)->length;
535 :     if (diff)
536 :     return diff;
537 :     else /* break ties by start address; thus the decompiler produces
538 :     the earliest primitive with the same code (e.g. noop instead
539 :     of (char) and @ instead of >code-address */
540 :     return (*b)->start - (*a)->start;
541 : anton 1.76 }
542 :    
543 : anton 1.69 #endif /* defined(NO_DYNAMIC) */
544 : anton 1.48 Cell npriminfos=0;
545 : anton 1.47
546 : anton 1.76
547 : anton 1.47 void check_prims(Label symbols1[])
548 :     {
549 :     int i;
550 : anton 1.90 #ifndef NO_DYNAMIC
551 : anton 1.70 Label *symbols2, *symbols3, *ends1;
552 : anton 1.49 static char superend[]={
553 : anton 1.48 #include "prim_superend.i"
554 :     };
555 : anton 1.90 #endif
556 : anton 1.47
557 : anton 1.66 if (debug)
558 :     #ifdef __VERSION__
559 :     fprintf(stderr, "Compiled with gcc-" __VERSION__ "\n");
560 :     #else
561 :     #define xstr(s) str(s)
562 :     #define str(s) #s
563 :     fprintf(stderr, "Compiled with gcc-" xstr(__GNUC__) "." xstr(__GNUC_MINOR__) "\n");
564 :     #endif
565 : anton 1.47 for (i=DOESJUMP+1; symbols1[i+1]!=0; i++)
566 :     ;
567 : anton 1.55 npriminfos = i;
568 : anton 1.70
569 :     #ifndef NO_DYNAMIC
570 : anton 1.66 if (no_dynamic)
571 :     return;
572 : anton 1.55 symbols2=engine2(0,0,0,0,0);
573 : anton 1.70 #if NO_IP
574 :     symbols3=engine3(0,0,0,0,0);
575 :     #else
576 :     symbols3=symbols1;
577 :     #endif
578 :     ends1 = symbols1+i+1-DOESJUMP;
579 : anton 1.47 priminfos = calloc(i,sizeof(PrimInfo));
580 :     for (i=DOESJUMP+1; symbols1[i+1]!=0; i++) {
581 : anton 1.70 int prim_len = ends1[i]-symbols1[i];
582 : anton 1.47 PrimInfo *pi=&priminfos[i];
583 : anton 1.70 int j=0;
584 :     char *s1 = (char *)symbols1[i];
585 :     char *s2 = (char *)symbols2[i];
586 :     char *s3 = (char *)symbols3[i];
587 :    
588 :     pi->start = s1;
589 :     pi->superend = superend[i-DOESJUMP-1]|no_super;
590 :     if (pi->superend)
591 :     pi->length = symbols1[i+1]-symbols1[i];
592 :     else
593 :     pi->length = prim_len;
594 : anton 1.74 pi->restlength = symbols1[i+1] - symbols1[i] - pi->length;
595 : anton 1.70 pi->nimmargs = 0;
596 :     if (debug)
597 : anton 1.74 fprintf(stderr, "Prim %3d @ %p %p %p, length=%3d restlength=%2d superend=%1d",
598 :     i, s1, s2, s3, pi->length, pi->restlength, pi->superend);
599 : anton 1.70 assert(prim_len>=0);
600 : anton 1.74 while (j<(pi->length+pi->restlength)) {
601 : anton 1.70 if (s1[j]==s3[j]) {
602 :     if (s1[j] != s2[j]) {
603 :     pi->start = NULL; /* not relocatable */
604 :     if (debug)
605 :     fprintf(stderr,"\n non_reloc: engine1!=engine2 offset %3d",j);
606 : anton 1.74 /* assert(j<prim_len); */
607 : anton 1.70 break;
608 :     }
609 :     j++;
610 :     } else {
611 :     struct immarg *ia=&pi->immargs[pi->nimmargs];
612 :    
613 :     pi->nimmargs++;
614 :     ia->offset=j;
615 :     if ((~*(Cell *)&(s1[j]))==*(Cell *)&(s3[j])) {
616 :     ia->rel=0;
617 :     if (debug)
618 :     fprintf(stderr,"\n absolute immarg: offset %3d",j);
619 :     } else if ((&(s1[j]))+(*(Cell *)&(s1[j]))+4 ==
620 :     symbols1[DOESJUMP+1]) {
621 :     ia->rel=1;
622 :     if (debug)
623 :     fprintf(stderr,"\n relative immarg: offset %3d",j);
624 :     } else {
625 :     pi->start = NULL; /* not relocatable */
626 :     if (debug)
627 :     fprintf(stderr,"\n non_reloc: engine1!=engine3 offset %3d",j);
628 : anton 1.74 /* assert(j<prim_len);*/
629 : anton 1.70 break;
630 :     }
631 :     j+=4;
632 : anton 1.47 }
633 :     }
634 : anton 1.70 if (debug)
635 :     fprintf(stderr,"\n");
636 :     }
637 : anton 1.76 decomp_prims = calloc(i,sizeof(PrimInfo *));
638 :     for (i=DOESJUMP+1; i<npriminfos; i++)
639 :     decomp_prims[i] = &(priminfos[i]);
640 :     qsort(decomp_prims+DOESJUMP+1, npriminfos-DOESJUMP-1, sizeof(PrimInfo *),
641 :     compare_priminfo_length);
642 : anton 1.70 #endif
643 :     }
644 :    
645 : anton 1.74 #ifndef NO_DYNAMIC
646 :     void flush_to_here(void)
647 :     {
648 :     FLUSH_ICACHE(start_flush, code_here-start_flush);
649 :     start_flush=code_here;
650 :     }
651 :    
652 :     void append_jump(void)
653 :     {
654 :     if (last_jump) {
655 :     PrimInfo *pi = &priminfos[last_jump];
656 :    
657 :     memcpy(code_here, pi->start+pi->length, pi->restlength);
658 :     code_here += pi->restlength;
659 :     last_jump=0;
660 :     flush_to_here();
661 :     }
662 :     }
663 :    
664 : anton 1.75 /* Gforth remembers all code blocks in this list. On forgetting (by
665 :     executing a marker) the code blocks are not freed (because Gforth does
666 :     not remember how they were allocated; hmm, remembering that might be
667 :     easier and cleaner). Instead, code_here etc. are reset to the old
668 :     value, and the "forgotten" code blocks are reused when they are
669 :     needed. */
670 :    
671 :     struct code_block_list {
672 :     struct code_block_list *next;
673 :     Address block;
674 :     Cell size;
675 :     } *code_block_list=NULL, **next_code_blockp=&code_block_list;
676 :    
677 : anton 1.74 Address append_prim(Cell p)
678 :     {
679 :     PrimInfo *pi = &priminfos[p];
680 :     Address old_code_here = code_here;
681 :    
682 :     if (code_area+code_area_size < code_here+pi->length+pi->restlength) {
683 : anton 1.75 struct code_block_list *p;
684 : anton 1.74 append_jump();
685 : anton 1.75 if (*next_code_blockp == NULL) {
686 :     code_here = start_flush = code_area = my_alloc(code_area_size);
687 :     p = (struct code_block_list *)malloc(sizeof(struct code_block_list));
688 :     *next_code_blockp = p;
689 :     p->next = NULL;
690 :     p->block = code_here;
691 :     p->size = code_area_size;
692 :     } else {
693 :     p = *next_code_blockp;
694 :     code_here = start_flush = code_area = p->block;
695 :     }
696 : anton 1.74 old_code_here = code_here;
697 : anton 1.75 next_code_blockp = &(p->next);
698 : anton 1.74 }
699 :     memcpy(code_here, pi->start, pi->length);
700 :     code_here += pi->length;
701 :     if (pi->superend)
702 :     flush_to_here();
703 :     return old_code_here;
704 :     }
705 :     #endif
706 : anton 1.75
707 :     int forget_dyncode(Address code)
708 :     {
709 :     #ifdef NO_DYNAMIC
710 :     return -1;
711 :     #else
712 :     struct code_block_list *p, **pp;
713 :    
714 :     for (pp=&code_block_list, p=*pp; p!=NULL; pp=&(p->next), p=*pp) {
715 :     if (code >= p->block && code < p->block+p->size) {
716 :     next_code_blockp = &(p->next);
717 :     code_here = start_flush = code;
718 :     code_area = p->block;
719 :     last_jump = 0;
720 :     return -1;
721 :     }
722 :     }
723 : anton 1.78 return -no_dynamic;
724 : anton 1.75 #endif /* !defined(NO_DYNAMIC) */
725 :     }
726 :    
727 : anton 1.90 Label decompile_code(Label _code)
728 : anton 1.75 {
729 : anton 1.76 #ifdef NO_DYNAMIC
730 : anton 1.90 return _code;
731 : anton 1.76 #else /* !defined(NO_DYNAMIC) */
732 :     Cell i;
733 : anton 1.77 struct code_block_list *p;
734 : anton 1.90 Address code=_code;
735 : anton 1.76
736 : anton 1.77 /* first, check if we are in code at all */
737 :     for (p = code_block_list;; p = p->next) {
738 :     if (p == NULL)
739 :     return code;
740 :     if (code >= p->block && code < p->block+p->size)
741 :     break;
742 :     }
743 : anton 1.76 /* reverse order because NOOP might match other prims */
744 :     for (i=npriminfos-1; i>DOESJUMP; i--) {
745 :     PrimInfo *pi=decomp_prims[i];
746 :     if (pi->start==code || (pi->start && memcmp(code,pi->start,pi->length)==0))
747 :     return pi->start;
748 :     }
749 :     return code;
750 :     #endif /* !defined(NO_DYNAMIC) */
751 : anton 1.75 }
752 : anton 1.74
753 : anton 1.70 #ifdef NO_IP
754 :     int nbranchinfos=0;
755 :    
756 :     struct branchinfo {
757 :     Label *targetptr; /* *(bi->targetptr) is the target */
758 :     Cell *addressptr; /* store the target here */
759 :     } branchinfos[100000];
760 :    
761 :     int ndoesexecinfos=0;
762 :     struct doesexecinfo {
763 :     int branchinfo; /* fix the targetptr of branchinfos[...->branchinfo] */
764 :     Cell *xt; /* cfa of word whose does-code needs calling */
765 :     } doesexecinfos[10000];
766 :    
767 : anton 1.73 /* definitions of N_execute etc. */
768 :     #include "prim_num.i"
769 : anton 1.70
770 :     void set_rel_target(Cell *source, Label target)
771 :     {
772 :     *source = ((Cell)target)-(((Cell)source)+4);
773 :     }
774 :    
775 :     void register_branchinfo(Label source, Cell targetptr)
776 :     {
777 :     struct branchinfo *bi = &(branchinfos[nbranchinfos]);
778 :     bi->targetptr = (Label *)targetptr;
779 :     bi->addressptr = (Cell *)source;
780 :     nbranchinfos++;
781 :     }
782 :    
783 :     Cell *compile_prim1arg(Cell p)
784 :     {
785 :     int l = priminfos[p].length;
786 :     Address old_code_here=code_here;
787 :    
788 : anton 1.74 assert(vm_prims[p]==priminfos[p].start);
789 :     append_prim(p);
790 : anton 1.70 return (Cell*)(old_code_here+priminfos[p].immargs[0].offset);
791 :     }
792 :    
793 :     Cell *compile_call2(Cell targetptr)
794 :     {
795 :     Cell *next_code_target;
796 : anton 1.73 PrimInfo *pi = &priminfos[N_call2];
797 : anton 1.74 Address old_code_here = append_prim(N_call2);
798 : anton 1.70
799 : anton 1.74 next_code_target = (Cell *)(old_code_here + pi->immargs[0].offset);
800 :     register_branchinfo(old_code_here + pi->immargs[1].offset, targetptr);
801 : anton 1.70 return next_code_target;
802 :     }
803 :     #endif
804 :    
805 :     void finish_code(void)
806 :     {
807 :     #ifdef NO_IP
808 :     Cell i;
809 :    
810 :     compile_prim1(NULL);
811 :     for (i=0; i<ndoesexecinfos; i++) {
812 :     struct doesexecinfo *dei = &doesexecinfos[i];
813 :     branchinfos[dei->branchinfo].targetptr = DOES_CODE1((dei->xt));
814 :     }
815 :     ndoesexecinfos = 0;
816 :     for (i=0; i<nbranchinfos; i++) {
817 :     struct branchinfo *bi=&branchinfos[i];
818 :     set_rel_target(bi->addressptr, *(bi->targetptr));
819 :     }
820 :     nbranchinfos = 0;
821 :     FLUSH_ICACHE(start_flush, code_here-start_flush);
822 :     start_flush=code_here;
823 : anton 1.48 #endif
824 :     }
825 :    
826 : anton 1.70 void compile_prim1(Cell *start)
827 : anton 1.48 {
828 : anton 1.61 #if defined(DOUBLY_INDIRECT)
829 : anton 1.70 Label prim=(Label)*start;
830 : anton 1.54 if (prim<((Label)(xts+DOESJUMP)) || prim>((Label)(xts+npriminfos))) {
831 :     fprintf(stderr,"compile_prim encountered xt %p\n", prim);
832 : anton 1.70 *start=(Cell)prim;
833 :     return;
834 :     } else {
835 : anton 1.90 *start = (Cell)(prim-((Label)xts)+((Label)vm_prims));
836 : anton 1.70 return;
837 :     }
838 :     #elif defined(NO_IP)
839 :     static Cell *last_start=NULL;
840 :     static Xt last_prim=NULL;
841 :     /* delay work by one call in order to get relocated immargs */
842 :    
843 :     if (last_start) {
844 :     unsigned i = last_prim-vm_prims;
845 :     PrimInfo *pi=&priminfos[i];
846 :     Cell *next_code_target=NULL;
847 :    
848 :     assert(i<npriminfos);
849 : anton 1.73 if (i==N_execute||i==N_perform||i==N_lit_perform) {
850 :     next_code_target = compile_prim1arg(N_set_next_code);
851 : anton 1.70 }
852 : anton 1.73 if (i==N_call) {
853 : anton 1.70 next_code_target = compile_call2(last_start[1]);
854 : anton 1.73 } else if (i==N_does_exec) {
855 : anton 1.70 struct doesexecinfo *dei = &doesexecinfos[ndoesexecinfos++];
856 : anton 1.73 *compile_prim1arg(N_lit) = (Cell)PFA(last_start[1]);
857 : anton 1.70 /* we cannot determine the callee now (last_start[1] may be a
858 :     forward reference), so just register an arbitrary target, and
859 :     register in dei that we need to fix this before resolving
860 :     branches */
861 :     dei->branchinfo = nbranchinfos;
862 :     dei->xt = (Cell *)(last_start[1]);
863 :     next_code_target = compile_call2(NULL);
864 :     } else if (pi->start == NULL) { /* non-reloc */
865 : anton 1.73 next_code_target = compile_prim1arg(N_set_next_code);
866 :     set_rel_target(compile_prim1arg(N_abranch),*(Xt)last_prim);
867 : anton 1.70 } else {
868 :     unsigned j;
869 : anton 1.74 Address old_code_here = append_prim(i);
870 : anton 1.70
871 :     for (j=0; j<pi->nimmargs; j++) {
872 :     struct immarg *ia = &(pi->immargs[j]);
873 :     Cell argval = last_start[pi->nimmargs - j]; /* !! specific to prims */
874 :     if (ia->rel) { /* !! assumption: relative refs are branches */
875 : anton 1.74 register_branchinfo(old_code_here + ia->offset, argval);
876 : anton 1.70 } else /* plain argument */
877 : anton 1.74 *(Cell *)(old_code_here + ia->offset) = argval;
878 : anton 1.70 }
879 :     }
880 :     if (next_code_target!=NULL)
881 :     *next_code_target = (Cell)code_here;
882 :     }
883 :     if (start) {
884 :     last_prim = (Xt)*start;
885 :     *start = (Cell)code_here;
886 :     }
887 :     last_start = start;
888 :     return;
889 :     #elif !defined(NO_DYNAMIC)
890 :     Label prim=(Label)*start;
891 : anton 1.58 unsigned i;
892 : anton 1.74 Address old_code_here;
893 : anton 1.48
894 : anton 1.58 i = ((Xt)prim)-vm_prims;
895 : anton 1.56 prim = *(Xt)prim;
896 : anton 1.70 if (no_dynamic) {
897 :     *start = (Cell)prim;
898 :     return;
899 :     }
900 : anton 1.58 if (i>=npriminfos || priminfos[i].start == 0) { /* not a relocatable prim */
901 : anton 1.74 append_jump();
902 : anton 1.70 *start = (Cell)prim;
903 :     return;
904 : anton 1.47 }
905 : anton 1.58 assert(priminfos[i].start = prim);
906 : anton 1.50 #ifdef ALIGN_CODE
907 : anton 1.87 /* ALIGN_CODE;*/
908 : anton 1.50 #endif
909 : anton 1.74 assert(prim==priminfos[i].start);
910 :     old_code_here = append_prim(i);
911 :     last_jump = (priminfos[i].superend) ? 0 : i;
912 : anton 1.70 *start = (Cell)old_code_here;
913 :     return;
914 : anton 1.61 #else /* !defined(DOUBLY_INDIRECT), no code replication */
915 : anton 1.70 Label prim=(Label)*start;
916 : anton 1.61 #if !defined(INDIRECT_THREADED)
917 : anton 1.56 prim = *(Xt)prim;
918 : anton 1.61 #endif
919 : anton 1.70 *start = (Cell)prim;
920 :     return;
921 : anton 1.54 #endif /* !defined(DOUBLY_INDIRECT) */
922 : anton 1.70 }
923 :    
924 :     Label compile_prim(Label prim)
925 :     {
926 :     Cell x=(Cell)prim;
927 : anton 1.80 assert(0);
928 : anton 1.70 compile_prim1(&x);
929 :     return (Label)x;
930 : anton 1.47 }
931 :    
932 : anton 1.69 #if defined(PRINT_SUPER_LENGTHS) && !defined(NO_DYNAMIC)
933 : anton 1.59 Cell prim_length(Cell prim)
934 :     {
935 :     return priminfos[prim+DOESJUMP+1].length;
936 :     }
937 :     #endif
938 :    
939 : anton 1.1 Address loader(FILE *imagefile, char* filename)
940 :     /* returns the address of the image proper (after the preamble) */
941 :     {
942 :     ImageHeader header;
943 :     Address image;
944 :     Address imp; /* image+preamble */
945 : anton 1.17 Char magic[8];
946 :     char magic7; /* size byte of magic number */
947 : anton 1.1 Cell preamblesize=0;
948 : pazsan 1.6 Cell data_offset = offset_image ? 56*sizeof(Cell) : 0;
949 : anton 1.1 UCell check_sum;
950 : pazsan 1.15 Cell ausize = ((RELINFOBITS == 8) ? 0 :
951 :     (RELINFOBITS == 16) ? 1 :
952 :     (RELINFOBITS == 32) ? 2 : 3);
953 :     Cell charsize = ((sizeof(Char) == 1) ? 0 :
954 :     (sizeof(Char) == 2) ? 1 :
955 :     (sizeof(Char) == 4) ? 2 : 3) + ausize;
956 :     Cell cellsize = ((sizeof(Cell) == 1) ? 0 :
957 :     (sizeof(Cell) == 2) ? 1 :
958 :     (sizeof(Cell) == 4) ? 2 : 3) + ausize;
959 : anton 1.21 Cell sizebyte = (ausize << 5) + (charsize << 3) + (cellsize << 1) +
960 :     #ifdef WORDS_BIGENDIAN
961 :     0
962 :     #else
963 :     1
964 :     #endif
965 :     ;
966 : anton 1.1
967 : anton 1.43 vm_prims = engine(0,0,0,0,0);
968 : anton 1.47 check_prims(vm_prims);
969 : anton 1.1 #ifndef DOUBLY_INDIRECT
970 : anton 1.59 #ifdef PRINT_SUPER_LENGTHS
971 :     print_super_lengths();
972 :     #endif
973 : anton 1.43 check_sum = checksum(vm_prims);
974 : anton 1.1 #else /* defined(DOUBLY_INDIRECT) */
975 : anton 1.43 check_sum = (UCell)vm_prims;
976 : anton 1.1 #endif /* defined(DOUBLY_INDIRECT) */
977 : pazsan 1.10
978 :     do {
979 :     if(fread(magic,sizeof(Char),8,imagefile) < 8) {
980 : anton 1.84 fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.6) image.\n",
981 : pazsan 1.10 progname, filename);
982 :     exit(1);
983 : anton 1.1 }
984 : pazsan 1.10 preamblesize+=8;
985 : anton 1.84 } while(memcmp(magic,"Gforth3",7));
986 : anton 1.17 magic7 = magic[7];
987 : anton 1.1 if (debug) {
988 : anton 1.17 magic[7]='\0';
989 : anton 1.21 fprintf(stderr,"Magic found: %s ", magic);
990 :     print_sizes(magic7);
991 : anton 1.1 }
992 :    
993 : anton 1.21 if (magic7 != sizebyte)
994 :     {
995 :     fprintf(stderr,"This image is: ");
996 :     print_sizes(magic7);
997 :     fprintf(stderr,"whereas the machine is ");
998 :     print_sizes(sizebyte);
999 : anton 1.1 exit(-2);
1000 :     };
1001 :    
1002 :     fread((void *)&header,sizeof(ImageHeader),1,imagefile);
1003 : pazsan 1.10
1004 :     set_stack_sizes(&header);
1005 : anton 1.1
1006 :     #if HAVE_GETPAGESIZE
1007 :     pagesize=getpagesize(); /* Linux/GNU libc offers this */
1008 :     #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
1009 :     pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
1010 :     #elif PAGESIZE
1011 :     pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
1012 :     #endif
1013 :     if (debug)
1014 : jwilke 1.5 fprintf(stderr,"pagesize=%ld\n",(unsigned long) pagesize);
1015 : anton 1.1
1016 : anton 1.34 image = dict_alloc_read(imagefile, preamblesize+header.image_size,
1017 :     preamblesize+dictsize, data_offset);
1018 : anton 1.33 imp=image+preamblesize;
1019 : anton 1.57 alloc_stacks((ImageHeader *)imp);
1020 : anton 1.1 if (clear_dictionary)
1021 : anton 1.33 memset(imp+header.image_size, 0, dictsize-header.image_size);
1022 : anton 1.90 if(header.base==0 || header.base == (Address)0x100) {
1023 : anton 1.1 Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
1024 :     char reloc_bits[reloc_size];
1025 : anton 1.33 fseek(imagefile, preamblesize+header.image_size, SEEK_SET);
1026 : pazsan 1.10 fread(reloc_bits, 1, reloc_size, imagefile);
1027 : anton 1.90 relocate((Cell *)imp, reloc_bits, header.image_size, (Cell)header.base, vm_prims);
1028 : anton 1.1 #if 0
1029 :     { /* let's see what the relocator did */
1030 :     FILE *snapshot=fopen("snapshot.fi","wb");
1031 :     fwrite(image,1,imagesize,snapshot);
1032 :     fclose(snapshot);
1033 :     }
1034 :     #endif
1035 : jwilke 1.46 }
1036 :     else if(header.base!=imp) {
1037 :     fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
1038 :     progname, (unsigned long)header.base, (unsigned long)imp);
1039 :     exit(1);
1040 : anton 1.1 }
1041 :     if (header.checksum==0)
1042 :     ((ImageHeader *)imp)->checksum=check_sum;
1043 :     else if (header.checksum != check_sum) {
1044 :     fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
1045 :     progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
1046 :     exit(1);
1047 :     }
1048 : anton 1.53 #ifdef DOUBLY_INDIRECT
1049 :     ((ImageHeader *)imp)->xt_base = xts;
1050 :     #endif
1051 : anton 1.1 fclose(imagefile);
1052 :    
1053 : anton 1.56 /* unnecessary, except maybe for CODE words */
1054 :     /* FLUSH_ICACHE(imp, header.image_size);*/
1055 : anton 1.1
1056 :     return imp;
1057 :     }
1058 :    
1059 : anton 1.72 /* pointer to last '/' or '\' in file, 0 if there is none. */
1060 :     char *onlypath(char *filename)
1061 : pazsan 1.10 {
1062 : anton 1.72 return strrchr(filename, DIRSEP);
1063 : anton 1.1 }
1064 :    
1065 :     FILE *openimage(char *fullfilename)
1066 : pazsan 1.10 {
1067 :     FILE *image_file;
1068 : anton 1.28 char * expfilename = tilde_cstr(fullfilename, strlen(fullfilename), 1);
1069 : pazsan 1.10
1070 : anton 1.28 image_file=fopen(expfilename,"rb");
1071 : anton 1.1 if (image_file!=NULL && debug)
1072 : anton 1.28 fprintf(stderr, "Opened image file: %s\n", expfilename);
1073 : pazsan 1.10 return image_file;
1074 : anton 1.1 }
1075 :    
1076 : anton 1.28 /* try to open image file concat(path[0:len],imagename) */
1077 : anton 1.1 FILE *checkimage(char *path, int len, char *imagename)
1078 : pazsan 1.10 {
1079 :     int dirlen=len;
1080 : anton 1.1 char fullfilename[dirlen+strlen(imagename)+2];
1081 : pazsan 1.10
1082 : anton 1.1 memcpy(fullfilename, path, dirlen);
1083 : pazsan 1.71 if (fullfilename[dirlen-1]!=DIRSEP)
1084 :     fullfilename[dirlen++]=DIRSEP;
1085 : anton 1.1 strcpy(fullfilename+dirlen,imagename);
1086 : pazsan 1.10 return openimage(fullfilename);
1087 : anton 1.1 }
1088 :    
1089 : pazsan 1.10 FILE * open_image_file(char * imagename, char * path)
1090 : anton 1.1 {
1091 : pazsan 1.10 FILE * image_file=NULL;
1092 : anton 1.28 char *origpath=path;
1093 : pazsan 1.10
1094 : pazsan 1.71 if(strchr(imagename, DIRSEP)==NULL) {
1095 : pazsan 1.10 /* first check the directory where the exe file is in !! 01may97jaw */
1096 :     if (onlypath(progname))
1097 : anton 1.72 image_file=checkimage(progname, onlypath(progname)-progname, imagename);
1098 : pazsan 1.10 if (!image_file)
1099 :     do {
1100 :     char *pend=strchr(path, PATHSEP);
1101 :     if (pend==NULL)
1102 :     pend=path+strlen(path);
1103 :     if (strlen(path)==0) break;
1104 :     image_file=checkimage(path, pend-path, imagename);
1105 :     path=pend+(*pend==PATHSEP);
1106 :     } while (image_file==NULL);
1107 :     } else {
1108 :     image_file=openimage(imagename);
1109 :     }
1110 : anton 1.1
1111 : pazsan 1.10 if (!image_file) {
1112 :     fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
1113 : anton 1.28 progname, imagename, origpath);
1114 : pazsan 1.10 exit(1);
1115 : anton 1.7 }
1116 :    
1117 : pazsan 1.10 return image_file;
1118 :     }
1119 : pazsan 1.11 #endif
1120 :    
1121 :     #ifdef HAS_OS
1122 :     UCell convsize(char *s, UCell elemsize)
1123 :     /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
1124 :     of bytes. the letter at the end indicates the unit, where e stands
1125 :     for the element size. default is e */
1126 :     {
1127 :     char *endp;
1128 :     UCell n,m;
1129 :    
1130 :     m = elemsize;
1131 :     n = strtoul(s,&endp,0);
1132 :     if (endp!=NULL) {
1133 :     if (strcmp(endp,"b")==0)
1134 :     m=1;
1135 :     else if (strcmp(endp,"k")==0)
1136 :     m=1024;
1137 :     else if (strcmp(endp,"M")==0)
1138 :     m=1024*1024;
1139 :     else if (strcmp(endp,"G")==0)
1140 :     m=1024*1024*1024;
1141 :     else if (strcmp(endp,"T")==0) {
1142 :     #if (SIZEOF_CHAR_P > 4)
1143 : anton 1.24 m=1024L*1024*1024*1024;
1144 : pazsan 1.11 #else
1145 :     fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp);
1146 :     exit(1);
1147 :     #endif
1148 :     } else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
1149 :     fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
1150 :     exit(1);
1151 :     }
1152 :     }
1153 :     return n*m;
1154 :     }
1155 : pazsan 1.10
1156 :     void gforth_args(int argc, char ** argv, char ** path, char ** imagename)
1157 :     {
1158 :     int c;
1159 :    
1160 : anton 1.1 opterr=0;
1161 :     while (1) {
1162 :     int option_index=0;
1163 :     static struct option opts[] = {
1164 : anton 1.29 {"appl-image", required_argument, NULL, 'a'},
1165 : anton 1.1 {"image-file", required_argument, NULL, 'i'},
1166 :     {"dictionary-size", required_argument, NULL, 'm'},
1167 :     {"data-stack-size", required_argument, NULL, 'd'},
1168 :     {"return-stack-size", required_argument, NULL, 'r'},
1169 :     {"fp-stack-size", required_argument, NULL, 'f'},
1170 :     {"locals-stack-size", required_argument, NULL, 'l'},
1171 :     {"path", required_argument, NULL, 'p'},
1172 :     {"version", no_argument, NULL, 'v'},
1173 :     {"help", no_argument, NULL, 'h'},
1174 :     /* put something != 0 into offset_image */
1175 :     {"offset-image", no_argument, &offset_image, 1},
1176 :     {"no-offset-im", no_argument, &offset_image, 0},
1177 :     {"clear-dictionary", no_argument, &clear_dictionary, 1},
1178 : anton 1.4 {"die-on-signal", no_argument, &die_on_signal, 1},
1179 : anton 1.1 {"debug", no_argument, &debug, 1},
1180 : anton 1.60 {"no-super", no_argument, &no_super, 1},
1181 :     {"no-dynamic", no_argument, &no_dynamic, 1},
1182 : anton 1.66 {"dynamic", no_argument, &no_dynamic, 0},
1183 : anton 1.1 {0,0,0,0}
1184 :     /* no-init-file, no-rc? */
1185 :     };
1186 :    
1187 : pazsan 1.36 c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vhoncsx", opts, &option_index);
1188 : anton 1.1
1189 :     switch (c) {
1190 : anton 1.29 case EOF: return;
1191 :     case '?': optind--; return;
1192 :     case 'a': *imagename = optarg; return;
1193 : pazsan 1.10 case 'i': *imagename = optarg; break;
1194 : anton 1.1 case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
1195 :     case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
1196 :     case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
1197 :     case 'f': fsize = convsize(optarg,sizeof(Float)); break;
1198 :     case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
1199 : pazsan 1.10 case 'p': *path = optarg; break;
1200 : pazsan 1.36 case 'o': offset_image = 1; break;
1201 :     case 'n': offset_image = 0; break;
1202 :     case 'c': clear_dictionary = 1; break;
1203 :     case 's': die_on_signal = 1; break;
1204 :     case 'x': debug = 1; break;
1205 : anton 1.83 case 'v': fputs(PACKAGE_STRING"\n", stderr); exit(0);
1206 : anton 1.1 case 'h':
1207 : anton 1.29 fprintf(stderr, "Usage: %s [engine options] ['--'] [image arguments]\n\
1208 : anton 1.1 Engine Options:\n\
1209 : anton 1.29 --appl-image FILE equivalent to '--image-file=FILE --'\n\
1210 : pazsan 1.10 --clear-dictionary Initialize the dictionary with 0 bytes\n\
1211 :     -d SIZE, --data-stack-size=SIZE Specify data stack size\n\
1212 :     --debug Print debugging information during startup\n\
1213 :     --die-on-signal exit instead of CATCHing some signals\n\
1214 : anton 1.66 --dynamic use dynamic native code\n\
1215 : pazsan 1.10 -f SIZE, --fp-stack-size=SIZE Specify floating point stack size\n\
1216 :     -h, --help Print this message and exit\n\
1217 :     -i FILE, --image-file=FILE Use image FILE instead of `gforth.fi'\n\
1218 :     -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
1219 :     -m SIZE, --dictionary-size=SIZE Specify Forth dictionary size\n\
1220 : anton 1.60 --no-dynamic Use only statically compiled primitives\n\
1221 : pazsan 1.10 --no-offset-im Load image at normal position\n\
1222 : anton 1.60 --no-super No dynamically formed superinstructions\n\
1223 : pazsan 1.10 --offset-image Load image at a different position\n\
1224 :     -p PATH, --path=PATH Search path for finding image and sources\n\
1225 :     -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
1226 : anton 1.66 -v, --version Print engine version and exit\n\
1227 : anton 1.1 SIZE arguments consist of an integer followed by a unit. The unit can be\n\
1228 : pazsan 1.10 `b' (byte), `e' (element; default), `k' (KB), `M' (MB), `G' (GB) or `T' (TB).\n",
1229 :     argv[0]);
1230 :     optind--;
1231 :     return;
1232 : anton 1.1 }
1233 :     }
1234 : pazsan 1.10 }
1235 : pazsan 1.11 #endif
1236 : pazsan 1.10
1237 :     #ifdef INCLUDE_IMAGE
1238 :     extern Cell image[];
1239 :     extern const char reloc_bits[];
1240 :     #endif
1241 : pazsan 1.67
1242 : pazsan 1.10 int main(int argc, char **argv, char **env)
1243 :     {
1244 : pazsan 1.30 #ifdef HAS_OS
1245 : pazsan 1.10 char *path = getenv("GFORTHPATH") ? : DEFAULTPATH;
1246 : pazsan 1.30 #else
1247 :     char *path = DEFAULTPATH;
1248 :     #endif
1249 : pazsan 1.13 #ifndef INCLUDE_IMAGE
1250 : pazsan 1.10 char *imagename="gforth.fi";
1251 :     FILE *image_file;
1252 :     Address image;
1253 :     #endif
1254 :     int retvalue;
1255 :    
1256 : anton 1.56 #if defined(i386) && defined(ALIGNMENT_CHECK)
1257 : pazsan 1.10 /* turn on alignment checks on the 486.
1258 :     * on the 386 this should have no effect. */
1259 :     __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
1260 :     /* this is unusable with Linux' libc.4.6.27, because this library is
1261 :     not alignment-clean; we would have to replace some library
1262 :     functions (e.g., memcpy) to make it work. Also GCC doesn't try to keep
1263 :     the stack FP-aligned. */
1264 :     #endif
1265 :    
1266 :     /* buffering of the user output device */
1267 : pazsan 1.11 #ifdef _IONBF
1268 : pazsan 1.10 if (isatty(fileno(stdout))) {
1269 :     fflush(stdout);
1270 :     setvbuf(stdout,NULL,_IONBF,0);
1271 : anton 1.1 }
1272 : pazsan 1.11 #endif
1273 : anton 1.1
1274 : pazsan 1.10 progname = argv[0];
1275 :    
1276 : pazsan 1.11 #ifdef HAS_OS
1277 : pazsan 1.10 gforth_args(argc, argv, &path, &imagename);
1278 : pazsan 1.11 #endif
1279 : pazsan 1.10
1280 :     #ifdef INCLUDE_IMAGE
1281 :     set_stack_sizes((ImageHeader *)image);
1282 : pazsan 1.22 if(((ImageHeader *)image)->base != image)
1283 :     relocate(image, reloc_bits, ((ImageHeader *)image)->image_size,
1284 :     (Label*)engine(0, 0, 0, 0, 0));
1285 : pazsan 1.10 alloc_stacks((ImageHeader *)image);
1286 :     #else
1287 :     image_file = open_image_file(imagename, path);
1288 :     image = loader(image_file, imagename);
1289 :     #endif
1290 : anton 1.24 gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */
1291 : anton 1.1
1292 :     {
1293 : pazsan 1.10 char path2[strlen(path)+1];
1294 : anton 1.1 char *p1, *p2;
1295 :     Cell environ[]= {
1296 :     (Cell)argc-(optind-1),
1297 :     (Cell)(argv+(optind-1)),
1298 : pazsan 1.10 (Cell)strlen(path),
1299 : anton 1.1 (Cell)path2};
1300 :     argv[optind-1] = progname;
1301 :     /*
1302 :     for (i=0; i<environ[0]; i++)
1303 :     printf("%s\n", ((char **)(environ[1]))[i]);
1304 :     */
1305 :     /* make path OS-independent by replacing path separators with NUL */
1306 : pazsan 1.10 for (p1=path, p2=path2; *p1!='\0'; p1++, p2++)
1307 : anton 1.1 if (*p1==PATHSEP)
1308 :     *p2 = '\0';
1309 :     else
1310 :     *p2 = *p1;
1311 :     *p2='\0';
1312 : pazsan 1.10 retvalue = go_forth(image, 4, environ);
1313 : anton 1.42 #ifdef VM_PROFILING
1314 :     vm_print_profile(stderr);
1315 :     #endif
1316 : anton 1.1 deprep_terminal();
1317 :     }
1318 : pazsan 1.13 return retvalue;
1319 : anton 1.1 }

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help