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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help