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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help