[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.170 Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006 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.121 typedef enum prim_num {
51 : anton 1.119 /* definitions of N_execute etc. */
52 : anton 1.126 #include PRIM_NUM_I
53 : anton 1.119 N_START_SUPER
54 : anton 1.121 } PrimNum;
55 : anton 1.119
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 : pazsan 1.161 Cell *gforth_SP;
60 :     Float *gforth_FP;
61 :     Address gforth_UP=NULL;
62 : anton 1.79
63 : pazsan 1.115 #ifdef HAS_FFCALL
64 : pazsan 1.161 Cell *gforth_RP;
65 :     Address gforth_LP;
66 : pazsan 1.115
67 :     #include <callback.h>
68 :    
69 : pazsan 1.161 va_alist gforth_clist;
70 : pazsan 1.115
71 : pazsan 1.161 void gforth_callback(Xt* fcall, void * alist)
72 : pazsan 1.115 {
73 : pazsan 1.140 /* save global valiables */
74 : pazsan 1.161 Cell *rp = gforth_RP;
75 :     Cell *sp = gforth_SP;
76 :     Float *fp = gforth_FP;
77 :     Address lp = gforth_LP;
78 : pazsan 1.168 va_alist clist = gforth_clist;
79 : pazsan 1.140
80 : pazsan 1.161 gforth_clist = (va_alist)alist;
81 : pazsan 1.140
82 : pazsan 1.161 gforth_engine(fcall, sp, rp, fp, lp);
83 : pazsan 1.140
84 :     /* restore global variables */
85 : pazsan 1.161 gforth_RP = rp;
86 :     gforth_SP = sp;
87 :     gforth_FP = fp;
88 :     gforth_LP = lp;
89 : pazsan 1.168 gforth_clist = clist;
90 : pazsan 1.115 }
91 :     #endif
92 :    
93 : pazsan 1.153 #ifdef HAS_LIBFFI
94 : pazsan 1.161 Cell *gforth_RP;
95 :     Address gforth_LP;
96 : pazsan 1.153
97 :     #include <ffi.h>
98 :    
99 : pazsan 1.164 void ** gforth_clist;
100 :     void * gforth_ritem;
101 : pazsan 1.153
102 : pazsan 1.162 void gforth_callback(ffi_cif * cif, void * resp, void ** args, void * ip)
103 : pazsan 1.153 {
104 : pazsan 1.161 Cell *rp = gforth_RP;
105 :     Cell *sp = gforth_SP;
106 :     Float *fp = gforth_FP;
107 :     Address lp = gforth_LP;
108 : pazsan 1.168 void ** clist = gforth_clist;
109 :     void * ritem = gforth_ritem;
110 : pazsan 1.153
111 : pazsan 1.164 gforth_clist = args;
112 :     gforth_ritem = resp;
113 : pazsan 1.153
114 : pazsan 1.164 gforth_engine((Xt *)ip, sp, rp, fp, lp);
115 : pazsan 1.153
116 :     /* restore global variables */
117 : pazsan 1.161 gforth_RP = rp;
118 :     gforth_SP = sp;
119 :     gforth_FP = fp;
120 :     gforth_LP = lp;
121 : pazsan 1.168 gforth_clist = clist;
122 :     gforth_ritem = ritem;
123 : pazsan 1.153 }
124 :     #endif
125 :    
126 : anton 1.79 #ifdef GFORTH_DEBUGGING
127 :     /* define some VM registers as global variables, so they survive exceptions;
128 :     global register variables are not up to the task (according to the
129 :     GNU C manual) */
130 :     Xt *saved_ip;
131 :     Cell *rp;
132 :     #endif
133 :    
134 :     #ifdef NO_IP
135 :     Label next_code;
136 :     #endif
137 :    
138 :     #ifdef HAS_FILE
139 :     char* fileattr[6]={"rb","rb","r+b","r+b","wb","wb"};
140 :     char* pfileattr[6]={"r","r","r+","r+","w","w"};
141 :    
142 :     #ifndef O_BINARY
143 :     #define O_BINARY 0
144 :     #endif
145 :     #ifndef O_TEXT
146 :     #define O_TEXT 0
147 :     #endif
148 :    
149 :     int ufileattr[6]= {
150 :     O_RDONLY|O_BINARY, O_RDONLY|O_BINARY,
151 :     O_RDWR |O_BINARY, O_RDWR |O_BINARY,
152 :     O_WRONLY|O_BINARY, O_WRONLY|O_BINARY };
153 :     #endif
154 :     /* end global vars for engine.c */
155 :    
156 : anton 1.1 #define PRIM_VERSION 1
157 :     /* increment this whenever the primitives change in an incompatible way */
158 :    
159 : pazsan 1.14 #ifndef DEFAULTPATH
160 : anton 1.39 # define DEFAULTPATH "."
161 : pazsan 1.14 #endif
162 :    
163 : anton 1.1 #ifdef MSDOS
164 :     jmp_buf throw_jmp_buf;
165 :     #endif
166 :    
167 : anton 1.56 #if defined(DOUBLY_INDIRECT)
168 :     # define CFA(n) ({Cell _n = (n); ((Cell)(((_n & 0x4000) ? symbols : xts)+(_n&~0x4000UL)));})
169 : anton 1.1 #else
170 : anton 1.56 # define CFA(n) ((Cell)(symbols+((n)&~0x4000UL)))
171 : anton 1.1 #endif
172 :    
173 :     #define maxaligned(n) (typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
174 :    
175 :     static UCell dictsize=0;
176 :     static UCell dsize=0;
177 :     static UCell rsize=0;
178 :     static UCell fsize=0;
179 :     static UCell lsize=0;
180 :     int offset_image=0;
181 : anton 1.4 int die_on_signal=0;
182 : anton 1.169 int ignore_async_signals=0;
183 : pazsan 1.13 #ifndef INCLUDE_IMAGE
184 : anton 1.1 static int clear_dictionary=0;
185 : anton 1.24 UCell pagesize=1;
186 : pazsan 1.22 char *progname;
187 :     #else
188 :     char *progname = "gforth";
189 :     int optind = 1;
190 : pazsan 1.13 #endif
191 : pazsan 1.31
192 : anton 1.167 #define CODE_BLOCK_SIZE (512*1024) /* !! overflow handling for -native */
193 : anton 1.48 Address code_area=0;
194 : anton 1.73 Cell code_area_size = CODE_BLOCK_SIZE;
195 : anton 1.75 Address code_here=NULL+CODE_BLOCK_SIZE; /* does for code-area what HERE
196 :     does for the dictionary */
197 : anton 1.100 Address start_flush=NULL; /* start of unflushed code */
198 : anton 1.74 Cell last_jump=0; /* if the last prim was compiled without jump, this
199 :     is it's number, otherwise this contains 0 */
200 : anton 1.48
201 : anton 1.60 static int no_super=0; /* true if compile_prim should not fuse prims */
202 : anton 1.81 static int no_dynamic=NO_DYNAMIC_DEFAULT; /* if true, no code is generated
203 :     dynamically */
204 : anton 1.110 static int print_metrics=0; /* if true, print metrics on exit */
205 : anton 1.171 static int static_super_number = 0; /* number of ss used if available */
206 :     /* disabled because of tpa */
207 : anton 1.152 #define MAX_STATE 9 /* maximum number of states */
208 : anton 1.125 static int maxstates = MAX_STATE; /* number of states for stack caching */
209 : anton 1.110 static int ss_greedy = 0; /* if true: use greedy, not optimal ss selection */
210 : pazsan 1.144 static int diag = 0; /* if true: print diagnostic informations */
211 : anton 1.158 static int tpa_noequiv = 0; /* if true: no state equivalence checking */
212 :     static int tpa_noautomaton = 0; /* if true: no tree parsing automaton */
213 :     static int tpa_trace = 0; /* if true: data for line graph of new states etc. */
214 : pazsan 1.144 static int relocs = 0;
215 :     static int nonrelocs = 0;
216 : anton 1.60
217 : pazsan 1.30 #ifdef HAS_DEBUG
218 : anton 1.68 int debug=0;
219 : pazsan 1.144 # define debugp(x...) if (debug) fprintf(x);
220 : pazsan 1.31 #else
221 :     # define perror(x...)
222 :     # define fprintf(x...)
223 : pazsan 1.144 # define debugp(x...)
224 : pazsan 1.30 #endif
225 : pazsan 1.31
226 : anton 1.24 ImageHeader *gforth_header;
227 : anton 1.43 Label *vm_prims;
228 : anton 1.53 #ifdef DOUBLY_INDIRECT
229 :     Label *xts; /* same content as vm_prims, but should only be used for xts */
230 :     #endif
231 : anton 1.1
232 : anton 1.125 #ifndef NO_DYNAMIC
233 :     #define MAX_IMMARGS 2
234 :    
235 :     typedef struct {
236 :     Label start; /* NULL if not relocatable */
237 :     Cell length; /* only includes the jump iff superend is true*/
238 :     Cell restlength; /* length of the rest (i.e., the jump or (on superend) 0) */
239 :     char superend; /* true if primitive ends superinstruction, i.e.,
240 :     unconditional branch, execute, etc. */
241 :     Cell nimmargs;
242 :     struct immarg {
243 :     Cell offset; /* offset of immarg within prim */
244 :     char rel; /* true if immarg is relative */
245 :     } immargs[MAX_IMMARGS];
246 :     } PrimInfo;
247 :    
248 :     PrimInfo *priminfos;
249 :     PrimInfo **decomp_prims;
250 :    
251 : anton 1.139 const char const* const prim_names[]={
252 :     #include PRIM_NAMES_I
253 :     };
254 :    
255 : anton 1.148 void init_ss_cost(void);
256 :    
257 : anton 1.125 static int is_relocatable(int p)
258 :     {
259 :     return !no_dynamic && priminfos[p].start != NULL;
260 :     }
261 :     #else /* defined(NO_DYNAMIC) */
262 :     static int is_relocatable(int p)
263 :     {
264 :     return 0;
265 :     }
266 :     #endif /* defined(NO_DYNAMIC) */
267 :    
268 : pazsan 1.30 #ifdef MEMCMP_AS_SUBROUTINE
269 :     int gforth_memcmp(const char * s1, const char * s2, size_t n)
270 :     {
271 :     return memcmp(s1, s2, n);
272 :     }
273 :     #endif
274 :    
275 : anton 1.125 static Cell max(Cell a, Cell b)
276 :     {
277 :     return a>b?a:b;
278 :     }
279 :    
280 :     static Cell min(Cell a, Cell b)
281 :     {
282 :     return a<b?a:b;
283 :     }
284 :    
285 : anton 1.1 /* image file format:
286 : pazsan 1.15 * "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.4.0 -i\n")
287 : anton 1.1 * padding to a multiple of 8
288 : anton 1.84 * magic: "Gforth3x" means format 0.6,
289 : pazsan 1.15 * where x is a byte with
290 :     * bit 7: reserved = 0
291 :     * bit 6:5: address unit size 2^n octets
292 :     * bit 4:3: character size 2^n octets
293 :     * bit 2:1: cell size 2^n octets
294 :     * bit 0: endian, big=0, little=1.
295 :     * The magic are always 8 octets, no matter what the native AU/character size is
296 : anton 1.1 * padding to max alignment (no padding necessary on current machines)
297 : anton 1.24 * ImageHeader structure (see forth.h)
298 : anton 1.1 * data (size in ImageHeader.image_size)
299 :     * tags ((if relocatable, 1 bit/data cell)
300 :     *
301 :     * tag==1 means that the corresponding word is an address;
302 :     * If the word is >=0, the address is within the image;
303 :     * addresses within the image are given relative to the start of the image.
304 :     * If the word =-1 (CF_NIL), the address is NIL,
305 :     * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
306 :     * If the word =CF(DODOES), it's a DOES> CFA
307 :     * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
308 :     * possibly containing a jump to dodoes)
309 : anton 1.51 * If the word is <CF(DOESJUMP) and bit 14 is set, it's the xt of a primitive
310 :     * If the word is <CF(DOESJUMP) and bit 14 is clear,
311 :     * it's the threaded code of a primitive
312 : pazsan 1.85 * bits 13..9 of a primitive token state which group the primitive belongs to,
313 :     * bits 8..0 of a primitive token index into the group
314 : anton 1.1 */
315 :    
316 : pazsan 1.115 Cell groups[32] = {
317 : pazsan 1.85 0,
318 : anton 1.121 0
319 : anton 1.90 #undef GROUP
320 : pazsan 1.115 #undef GROUPADD
321 :     #define GROUPADD(n) +n
322 :     #define GROUP(x, n) , 0
323 : anton 1.126 #include PRIM_GRP_I
324 : anton 1.90 #undef GROUP
325 : pazsan 1.115 #undef GROUPADD
326 : pazsan 1.85 #define GROUP(x, n)
327 : pazsan 1.115 #define GROUPADD(n)
328 : pazsan 1.85 };
329 :    
330 : pazsan 1.161 static unsigned char *branch_targets(Cell *image, const unsigned char *bitstring,
331 : anton 1.125 int size, Cell base)
332 :     /* produce a bitmask marking all the branch targets */
333 :     {
334 : anton 1.130 int i=0, j, k, steps=(((size-1)/sizeof(Cell))/RELINFOBITS)+1;
335 : anton 1.125 Cell token;
336 :     unsigned char bits;
337 : anton 1.130 unsigned char *result=malloc(steps);
338 :    
339 :     memset(result, 0, steps);
340 :     for(k=0; k<steps; k++) {
341 : anton 1.125 for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
342 : anton 1.130 if(bits & (1U << (RELINFOBITS-1))) {
343 :     assert(i*sizeof(Cell) < size);
344 : anton 1.125 token=image[i];
345 :     if (token>=base) { /* relocatable address */
346 :     UCell bitnum=(token-base)/sizeof(Cell);
347 : anton 1.154 if (bitnum/RELINFOBITS < (UCell)steps)
348 :     result[bitnum/RELINFOBITS] |= 1U << ((~bitnum)&(RELINFOBITS-1));
349 : anton 1.125 }
350 :     }
351 :     }
352 :     }
353 :     return result;
354 :     }
355 :    
356 : pazsan 1.162 void gforth_relocate(Cell *image, const Char *bitstring,
357 :     UCell size, Cell base, Label symbols[])
358 : anton 1.1 {
359 : anton 1.130 int i=0, j, k, steps=(((size-1)/sizeof(Cell))/RELINFOBITS)+1;
360 : pazsan 1.11 Cell token;
361 : anton 1.1 char bits;
362 : anton 1.37 Cell max_symbols;
363 : jwilke 1.46 /*
364 : pazsan 1.85 * A virtual start address that's the real start address minus
365 : jwilke 1.46 * the one in the image
366 :     */
367 : jwilke 1.45 Cell *start = (Cell * ) (((void *) image) - ((void *) base));
368 : anton 1.125 unsigned char *targets = branch_targets(image, bitstring, size, base);
369 : anton 1.1
370 : pazsan 1.85 /* group index into table */
371 : pazsan 1.115 if(groups[31]==0) {
372 :     int groupsum=0;
373 :     for(i=0; i<32; i++) {
374 :     groupsum += groups[i];
375 :     groups[i] = groupsum;
376 :     /* printf("group[%d]=%d\n",i,groupsum); */
377 :     }
378 :     i=0;
379 :     }
380 : jwilke 1.46
381 :     /* printf("relocating to %x[%x] start=%x base=%x\n", image, size, start, base); */
382 : anton 1.37
383 : anton 1.121 for (max_symbols=0; symbols[max_symbols]!=0; max_symbols++)
384 : anton 1.37 ;
385 : anton 1.47 max_symbols--;
386 : pazsan 1.35
387 : anton 1.130 for(k=0; k<steps; k++) {
388 : pazsan 1.13 for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
389 : anton 1.1 /* fprintf(stderr,"relocate: image[%d]\n", i);*/
390 : anton 1.130 if(bits & (1U << (RELINFOBITS-1))) {
391 :     assert(i*sizeof(Cell) < size);
392 : pazsan 1.35 /* fprintf(stderr,"relocate: image[%d]=%d of %d\n", i, image[i], size/sizeof(Cell)); */
393 : jwilke 1.45 token=image[i];
394 : pazsan 1.85 if(token<0) {
395 :     int group = (-token & 0x3E00) >> 9;
396 :     if(group == 0) {
397 :     switch(token|0x4000) {
398 : anton 1.1 case CF_NIL : image[i]=0; break;
399 :     #if !defined(DOUBLY_INDIRECT)
400 :     case CF(DOCOL) :
401 :     case CF(DOVAR) :
402 :     case CF(DOCON) :
403 :     case CF(DOUSER) :
404 :     case CF(DODEFER) :
405 : pazsan 1.11 case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(token)]); break;
406 : anton 1.92 case CF(DOESJUMP): image[i]=0; break;
407 : anton 1.1 #endif /* !defined(DOUBLY_INDIRECT) */
408 :     case CF(DODOES) :
409 : jwilke 1.45 MAKE_DOES_CF(image+i,(Xt *)(image[i+1]+((Cell)start)));
410 : anton 1.1 break;
411 : pazsan 1.85 default : /* backward compatibility */
412 : anton 1.56 /* printf("Code field generation image[%x]:=CFA(%x)\n",
413 : anton 1.1 i, CF(image[i])); */
414 : anton 1.55 if (CF((token | 0x4000))<max_symbols) {
415 : anton 1.56 image[i]=(Cell)CFA(CF(token));
416 :     #ifdef DIRECT_THREADED
417 : anton 1.125 if ((token & 0x4000) == 0) { /* threade code, no CFA */
418 :     if (targets[k] & (1U<<(RELINFOBITS-1-j)))
419 :     compile_prim1(0);
420 : anton 1.70 compile_prim1(&image[i]);
421 : anton 1.125 }
422 : anton 1.56 #endif
423 : anton 1.55 } else
424 : 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);
425 : anton 1.1 }
426 : pazsan 1.85 } else {
427 :     int tok = -token & 0x1FF;
428 :     if (tok < (groups[group+1]-groups[group])) {
429 :     #if defined(DOUBLY_INDIRECT)
430 :     image[i]=(Cell)CFA(((groups[group]+tok) | (CF(token) & 0x4000)));
431 :     #else
432 :     image[i]=(Cell)CFA((groups[group]+tok));
433 :     #endif
434 :     #ifdef DIRECT_THREADED
435 : anton 1.125 if ((token & 0x4000) == 0) { /* threade code, no CFA */
436 :     if (targets[k] & (1U<<(RELINFOBITS-1-j)))
437 :     compile_prim1(0);
438 : pazsan 1.85 compile_prim1(&image[i]);
439 : anton 1.125 }
440 : pazsan 1.85 #endif
441 :     } else
442 : 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);
443 : pazsan 1.85 }
444 :     } else {
445 : anton 1.101 /* if base is > 0: 0 is a null reference so don't adjust*/
446 : jwilke 1.45 if (token>=base) {
447 :     image[i]+=(Cell)start;
448 :     }
449 : jwilke 1.46 }
450 : anton 1.1 }
451 :     }
452 : pazsan 1.31 }
453 : anton 1.125 free(targets);
454 : anton 1.70 finish_code();
455 : jwilke 1.26 ((ImageHeader*)(image))->base = (Address) image;
456 : anton 1.1 }
457 :    
458 : pazsan 1.162 #ifndef DOUBLY_INDIRECT
459 : pazsan 1.161 static UCell checksum(Label symbols[])
460 : anton 1.1 {
461 :     UCell r=PRIM_VERSION;
462 :     Cell i;
463 :    
464 :     for (i=DOCOL; i<=DOESJUMP; i++) {
465 :     r ^= (UCell)(symbols[i]);
466 :     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
467 :     }
468 :     #ifdef DIRECT_THREADED
469 :     /* we have to consider all the primitives */
470 :     for (; symbols[i]!=(Label)0; i++) {
471 :     r ^= (UCell)(symbols[i]);
472 :     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
473 :     }
474 :     #else
475 :     /* in indirect threaded code all primitives are accessed through the
476 :     symbols table, so we just have to put the base address of symbols
477 :     in the checksum */
478 :     r ^= (UCell)symbols;
479 :     #endif
480 :     return r;
481 :     }
482 : pazsan 1.162 #endif
483 : anton 1.1
484 : pazsan 1.161 static Address verbose_malloc(Cell size)
485 : anton 1.3 {
486 :     Address r;
487 :     /* leave a little room (64B) for stack underflows */
488 :     if ((r = malloc(size+64))==NULL) {
489 :     perror(progname);
490 :     exit(1);
491 :     }
492 :     r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
493 : pazsan 1.144 debugp(stderr, "malloc succeeds, address=$%lx\n", (long)r);
494 : anton 1.3 return r;
495 :     }
496 :    
497 : anton 1.33 static Address next_address=0;
498 : pazsan 1.161 static void after_alloc(Address r, Cell size)
499 : anton 1.33 {
500 :     if (r != (Address)-1) {
501 : pazsan 1.144 debugp(stderr, "success, address=$%lx\n", (long) r);
502 : anton 1.173 #if 0
503 :     /* not needed now that we protect the stacks with mprotect */
504 : anton 1.33 if (pagesize != 1)
505 :     next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */
506 : anton 1.173 #endif
507 : anton 1.33 } else {
508 : pazsan 1.144 debugp(stderr, "failed: %s\n", strerror(errno));
509 : anton 1.33 }
510 :     }
511 :    
512 : anton 1.34 #ifndef MAP_FAILED
513 :     #define MAP_FAILED ((Address) -1)
514 :     #endif
515 :     #ifndef MAP_FILE
516 :     # define MAP_FILE 0
517 :     #endif
518 :     #ifndef MAP_PRIVATE
519 :     # define MAP_PRIVATE 0
520 :     #endif
521 : anton 1.91 #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
522 :     # define MAP_ANON MAP_ANONYMOUS
523 :     #endif
524 : anton 1.34
525 :     #if defined(HAVE_MMAP)
526 :     static Address alloc_mmap(Cell size)
527 : anton 1.1 {
528 :     Address r;
529 :    
530 :     #if defined(MAP_ANON)
531 : pazsan 1.144 debugp(stderr,"try mmap($%lx, $%lx, ..., MAP_ANON, ...); ", (long)next_address, (long)size);
532 : anton 1.34 r = mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
533 : anton 1.1 #else /* !defined(MAP_ANON) */
534 : anton 1.17 /* Ultrix (at least) does not define MAP_FILE and MAP_PRIVATE (both are
535 :     apparently defaults) */
536 : anton 1.1 static int dev_zero=-1;
537 :    
538 :     if (dev_zero == -1)
539 :     dev_zero = open("/dev/zero", O_RDONLY);
540 :     if (dev_zero == -1) {
541 : anton 1.34 r = MAP_FAILED;
542 : pazsan 1.144 debugp(stderr, "open(\"/dev/zero\"...) failed (%s), no mmap; ",
543 : anton 1.1 strerror(errno));
544 :     } else {
545 : pazsan 1.144 debugp(stderr,"try mmap($%lx, $%lx, ..., MAP_FILE, dev_zero, ...); ", (long)next_address, (long)size);
546 : anton 1.1 r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE, dev_zero, 0);
547 :     }
548 :     #endif /* !defined(MAP_ANON) */
549 : anton 1.34 after_alloc(r, size);
550 :     return r;
551 :     }
552 : anton 1.172
553 :     static void page_noaccess(Address a)
554 :     {
555 :     /* try mprotect first; with munmap the page might be allocated later */
556 :     debugp(stderr, "try mprotect(%p,%ld,PROT_NONE); ", a, (long)pagesize);
557 :     if (mprotect(a, pagesize, PROT_NONE)==0) {
558 :     debugp(stderr, "ok\n");
559 :     return;
560 :     }
561 :     debugp(stderr, "failed: %s\n", strerror(errno));
562 :     debugp(stderr, "try munmap(%p,%ld); ", a, (long)pagesize);
563 :     if (munmap(a,pagesize)==0) {
564 :     debugp(stderr, "ok\n");
565 :     return;
566 :     }
567 :     debugp(stderr, "failed: %s\n", strerror(errno));
568 :     }
569 :    
570 : anton 1.173 static size_t wholepage(size_t n)
571 : anton 1.172 {
572 :     return (n+pagesize-1)&~(pagesize-1);
573 :     }
574 : anton 1.34 #endif
575 :    
576 : pazsan 1.161 Address gforth_alloc(Cell size)
577 : anton 1.34 {
578 :     #if HAVE_MMAP
579 :     Address r;
580 :    
581 :     r=alloc_mmap(size);
582 : anton 1.117 if (r!=(Address)MAP_FAILED)
583 : anton 1.1 return r;
584 :     #endif /* HAVE_MMAP */
585 : anton 1.3 /* use malloc as fallback */
586 :     return verbose_malloc(size);
587 : anton 1.1 }
588 :    
589 : pazsan 1.161 static Address dict_alloc_read(FILE *file, Cell imagesize, Cell dictsize, Cell offset)
590 : anton 1.33 {
591 : anton 1.34 Address image = MAP_FAILED;
592 : anton 1.33
593 : anton 1.56 #if defined(HAVE_MMAP)
594 : anton 1.33 if (offset==0) {
595 : anton 1.34 image=alloc_mmap(dictsize);
596 : anton 1.150 if (image != (Address)MAP_FAILED) {
597 :     Address image1;
598 :     debugp(stderr,"try mmap($%lx, $%lx, ..., MAP_FIXED|MAP_FILE, imagefile, 0); ", (long)image, (long)imagesize);
599 :     image1 = mmap(image, imagesize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FIXED|MAP_FILE|MAP_PRIVATE, fileno(file), 0);
600 :     after_alloc(image1,dictsize);
601 :     if (image1 == (Address)MAP_FAILED)
602 :     goto read_image;
603 :     }
604 : anton 1.33 }
605 : anton 1.56 #endif /* defined(HAVE_MMAP) */
606 : anton 1.117 if (image == (Address)MAP_FAILED) {
607 : pazsan 1.161 image = gforth_alloc(dictsize+offset)+offset;
608 : anton 1.149 read_image:
609 : anton 1.33 rewind(file); /* fseek(imagefile,0L,SEEK_SET); */
610 : anton 1.34 fread(image, 1, imagesize, file);
611 : anton 1.33 }
612 :     return image;
613 :     }
614 :    
615 : pazsan 1.10 void set_stack_sizes(ImageHeader * header)
616 :     {
617 :     if (dictsize==0)
618 :     dictsize = header->dict_size;
619 :     if (dsize==0)
620 :     dsize = header->data_stack_size;
621 :     if (rsize==0)
622 :     rsize = header->return_stack_size;
623 :     if (fsize==0)
624 :     fsize = header->fp_stack_size;
625 :     if (lsize==0)
626 :     lsize = header->locals_stack_size;
627 :     dictsize=maxaligned(dictsize);
628 :     dsize=maxaligned(dsize);
629 :     rsize=maxaligned(rsize);
630 :     lsize=maxaligned(lsize);
631 :     fsize=maxaligned(fsize);
632 :     }
633 :    
634 : anton 1.173 void alloc_stacks(ImageHeader * h)
635 : pazsan 1.10 {
636 : anton 1.173 h->dict_size=dictsize;
637 :     h->data_stack_size=dsize;
638 :     h->fp_stack_size=fsize;
639 :     h->return_stack_size=rsize;
640 :     h->locals_stack_size=lsize;
641 : pazsan 1.10
642 : anton 1.172 #if defined(HAVE_MMAP)
643 :     if (pagesize > 1) {
644 : anton 1.173 size_t p = pagesize;
645 :     size_t totalsize =
646 :     wholepage(dsize)+wholepage(fsize)+wholepage(rsize)+wholepage(lsize)+5*p;
647 : anton 1.172 Address a = alloc_mmap(totalsize);
648 :     if (a != (Address)MAP_FAILED) {
649 : anton 1.173 page_noaccess(a); a+=p; h-> data_stack_base=a; a+=wholepage(dsize);
650 :     page_noaccess(a); a+=p; h-> fp_stack_base=a; a+=wholepage(fsize);
651 :     page_noaccess(a); a+=p; h->return_stack_base=a; a+=wholepage(rsize);
652 :     page_noaccess(a); a+=p; h->locals_stack_base=a; a+=wholepage(lsize);
653 : anton 1.172 page_noaccess(a);
654 :     debugp(stderr,"stack addresses: d=%p f=%p r=%p l=%p\n",
655 : anton 1.173 h->data_stack_base,
656 :     h->fp_stack_base,
657 :     h->return_stack_base,
658 :     h->locals_stack_base);
659 : anton 1.172 return;
660 :     }
661 :     }
662 :     #endif
663 : anton 1.173 h->data_stack_base=gforth_alloc(dsize);
664 :     h->fp_stack_base=gforth_alloc(fsize);
665 :     h->return_stack_base=gforth_alloc(rsize);
666 :     h->locals_stack_base=gforth_alloc(lsize);
667 : pazsan 1.10 }
668 :    
669 : pazsan 1.161 #warning You can ignore the warnings about clobbered variables in gforth_go
670 :     int gforth_go(Address image, int stack, Cell *entries)
671 : pazsan 1.11 {
672 : anton 1.38 volatile ImageHeader *image_header = (ImageHeader *)image;
673 : anton 1.18 Cell *sp0=(Cell*)(image_header->data_stack_base + dsize);
674 : pazsan 1.44 Cell *rp0=(Cell *)(image_header->return_stack_base + rsize);
675 : anton 1.18 Float *fp0=(Float *)(image_header->fp_stack_base + fsize);
676 : pazsan 1.44 #ifdef GFORTH_DEBUGGING
677 : anton 1.38 volatile Cell *orig_rp0=rp0;
678 : pazsan 1.44 #endif
679 : anton 1.18 Address lp0=image_header->locals_stack_base + lsize;
680 :     Xt *ip0=(Xt *)(image_header->boot_entry);
681 : pazsan 1.13 #ifdef SYSSIGNALS
682 : pazsan 1.11 int throw_code;
683 : pazsan 1.13 #endif
684 : pazsan 1.11
685 :     /* ensure that the cached elements (if any) are accessible */
686 : anton 1.151 #if !(defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING))
687 :     sp0 -= 8; /* make stuff below bottom accessible for stack caching */
688 :     #endif
689 : anton 1.41 IF_fpTOS(fp0--);
690 : pazsan 1.11
691 :     for(;stack>0;stack--)
692 : anton 1.18 *--sp0=entries[stack-1];
693 : pazsan 1.11
694 : pazsan 1.30 #ifdef SYSSIGNALS
695 : pazsan 1.11 get_winsize();
696 :    
697 :     install_signal_handlers(); /* right place? */
698 :    
699 :     if ((throw_code=setjmp(throw_jmp_buf))) {
700 : anton 1.152 static Cell signal_data_stack[24];
701 :     static Cell signal_return_stack[16];
702 : pazsan 1.11 static Float signal_fp_stack[1];
703 : pazsan 1.13
704 : anton 1.152 signal_data_stack[15]=throw_code;
705 : anton 1.18
706 :     #ifdef GFORTH_DEBUGGING
707 : pazsan 1.144 debugp(stderr,"\ncaught signal, throwing exception %d, ip=%p rp=%p\n",
708 : anton 1.97 throw_code, saved_ip, rp);
709 : anton 1.38 if (rp <= orig_rp0 && rp > (Cell *)(image_header->return_stack_base+5)) {
710 : anton 1.18 /* no rstack overflow or underflow */
711 :     rp0 = rp;
712 : anton 1.63 *--rp0 = (Cell)saved_ip;
713 : anton 1.18 }
714 :     else /* I love non-syntactic ifdefs :-) */
715 : anton 1.152 rp0 = signal_return_stack+16;
716 : anton 1.97 #else /* !defined(GFORTH_DEBUGGING) */
717 : pazsan 1.144 debugp(stderr,"\ncaught signal, throwing exception %d\n", throw_code);
718 : anton 1.152 rp0 = signal_return_stack+16;
719 : anton 1.97 #endif /* !defined(GFORTH_DEBUGGING) */
720 : anton 1.25 /* fprintf(stderr, "rp=$%x\n",rp0);*/
721 : pazsan 1.11
722 : pazsan 1.164 return((int)(Cell)gforth_engine(image_header->throw_entry, signal_data_stack+15,
723 : anton 1.18 rp0, signal_fp_stack, 0));
724 : pazsan 1.11 }
725 : pazsan 1.13 #endif
726 : pazsan 1.11
727 : pazsan 1.164 return((int)(Cell)gforth_engine(ip0,sp0,rp0,fp0,lp0));
728 : pazsan 1.11 }
729 :    
730 : pazsan 1.30 #ifndef INCLUDE_IMAGE
731 : pazsan 1.161 static void print_sizes(Cell sizebyte)
732 : anton 1.21 /* print size information */
733 :     {
734 :     static char* endianstring[]= { " big","little" };
735 :    
736 :     fprintf(stderr,"%s endian, cell=%d bytes, char=%d bytes, au=%d bytes\n",
737 :     endianstring[sizebyte & 1],
738 :     1 << ((sizebyte >> 1) & 3),
739 :     1 << ((sizebyte >> 3) & 3),
740 :     1 << ((sizebyte >> 5) & 3));
741 :     }
742 :    
743 : anton 1.106 /* static superinstruction stuff */
744 :    
745 : anton 1.141 struct cost { /* super_info might be a more accurate name */
746 : anton 1.106 char loads; /* number of stack loads */
747 :     char stores; /* number of stack stores */
748 :     char updates; /* number of stack pointer updates */
749 : anton 1.123 char branch; /* is it a branch (SET_IP) */
750 : anton 1.125 unsigned char state_in; /* state on entry */
751 :     unsigned char state_out; /* state on exit */
752 : anton 1.142 unsigned char imm_ops; /* number of immediate operands */
753 : anton 1.123 short offset; /* offset into super2 table */
754 : anton 1.125 unsigned char length; /* number of components */
755 : anton 1.106 };
756 :    
757 : anton 1.121 PrimNum super2[] = {
758 : anton 1.126 #include SUPER2_I
759 : anton 1.106 };
760 :    
761 :     struct cost super_costs[] = {
762 : anton 1.126 #include COSTS_I
763 : anton 1.106 };
764 :    
765 : anton 1.125 struct super_state {
766 :     struct super_state *next;
767 :     PrimNum super;
768 :     };
769 :    
770 : anton 1.106 #define HASH_SIZE 256
771 :    
772 :     struct super_table_entry {
773 :     struct super_table_entry *next;
774 : anton 1.121 PrimNum *start;
775 : anton 1.106 short length;
776 : anton 1.125 struct super_state *ss_list; /* list of supers */
777 : anton 1.106 } *super_table[HASH_SIZE];
778 :     int max_super=2;
779 :    
780 : anton 1.125 struct super_state *state_transitions=NULL;
781 :    
782 : pazsan 1.161 static int hash_super(PrimNum *start, int length)
783 : anton 1.106 {
784 :     int i, r;
785 :    
786 :     for (i=0, r=0; i<length; i++) {
787 :     r <<= 1;
788 :     r += start[i];
789 :     }
790 :     return r & (HASH_SIZE-1);
791 :     }
792 :    
793 : pazsan 1.161 static struct super_state **lookup_super(PrimNum *start, int length)
794 : anton 1.106 {
795 :     int hash=hash_super(start,length);
796 :     struct super_table_entry *p = super_table[hash];
797 :    
798 : anton 1.125 /* assert(length >= 2); */
799 : anton 1.106 for (; p!=NULL; p = p->next) {
800 :     if (length == p->length &&
801 : anton 1.121 memcmp((char *)p->start, (char *)start, length*sizeof(PrimNum))==0)
802 : anton 1.125 return &(p->ss_list);
803 : anton 1.106 }
804 : anton 1.125 return NULL;
805 : anton 1.106 }
806 :    
807 : pazsan 1.161 static void prepare_super_table()
808 : anton 1.106 {
809 :     int i;
810 : anton 1.109 int nsupers = 0;
811 : anton 1.106
812 :     for (i=0; i<sizeof(super_costs)/sizeof(super_costs[0]); i++) {
813 :     struct cost *c = &super_costs[i];
814 : anton 1.125 if ((c->length < 2 || nsupers < static_super_number) &&
815 :     c->state_in < maxstates && c->state_out < maxstates) {
816 :     struct super_state **ss_listp= lookup_super(super2+c->offset, c->length);
817 :     struct super_state *ss = malloc(sizeof(struct super_state));
818 :     ss->super= i;
819 :     if (c->offset==N_noop && i != N_noop) {
820 :     if (is_relocatable(i)) {
821 :     ss->next = state_transitions;
822 :     state_transitions = ss;
823 :     }
824 :     } else if (ss_listp != NULL) {
825 :     ss->next = *ss_listp;
826 :     *ss_listp = ss;
827 :     } else {
828 :     int hash = hash_super(super2+c->offset, c->length);
829 :     struct super_table_entry **p = &super_table[hash];
830 :     struct super_table_entry *e = malloc(sizeof(struct super_table_entry));
831 :     ss->next = NULL;
832 :     e->next = *p;
833 :     e->start = super2 + c->offset;
834 :     e->length = c->length;
835 :     e->ss_list = ss;
836 :     *p = e;
837 :     }
838 : anton 1.106 if (c->length > max_super)
839 :     max_super = c->length;
840 : anton 1.125 if (c->length >= 2)
841 :     nsupers++;
842 : anton 1.106 }
843 :     }
844 : pazsan 1.144 debugp(stderr, "Using %d static superinsts\n", nsupers);
845 : anton 1.106 }
846 :    
847 :     /* dynamic replication/superinstruction stuff */
848 :    
849 : anton 1.69 #ifndef NO_DYNAMIC
850 : pazsan 1.161 static int compare_priminfo_length(const void *_a, const void *_b)
851 : anton 1.76 {
852 : anton 1.90 PrimInfo **a = (PrimInfo **)_a;
853 :     PrimInfo **b = (PrimInfo **)_b;
854 : anton 1.77 Cell diff = (*a)->length - (*b)->length;
855 :     if (diff)
856 :     return diff;
857 :     else /* break ties by start address; thus the decompiler produces
858 :     the earliest primitive with the same code (e.g. noop instead
859 :     of (char) and @ instead of >code-address */
860 :     return (*b)->start - (*a)->start;
861 : anton 1.76 }
862 : anton 1.112 #endif /* !defined(NO_DYNAMIC) */
863 : anton 1.76
864 : anton 1.125 static char MAYBE_UNUSED superend[]={
865 : anton 1.126 #include PRIM_SUPEREND_I
866 : anton 1.106 };
867 : anton 1.107
868 :     Cell npriminfos=0;
869 : anton 1.76
870 : anton 1.146 Label goto_start;
871 :     Cell goto_len;
872 :    
873 : pazsan 1.162 #ifndef NO_DYNAMIC
874 : pazsan 1.161 static int compare_labels(const void *pa, const void *pb)
875 : anton 1.113 {
876 : anton 1.114 Label a = *(Label *)pa;
877 :     Label b = *(Label *)pb;
878 :     return a-b;
879 :     }
880 : pazsan 1.162 #endif
881 : anton 1.113
882 : pazsan 1.161 static Label bsearch_next(Label key, Label *a, UCell n)
883 : anton 1.114 /* a is sorted; return the label >=key that is the closest in a;
884 :     return NULL if there is no label in a >=key */
885 :     {
886 :     int mid = (n-1)/2;
887 :     if (n<1)
888 :     return NULL;
889 :     if (n == 1) {
890 :     if (a[0] < key)
891 :     return NULL;
892 :     else
893 :     return a[0];
894 :     }
895 :     if (a[mid] < key)
896 :     return bsearch_next(key, a+mid+1, n-mid-1);
897 :     else
898 :     return bsearch_next(key, a, mid+1);
899 : anton 1.113 }
900 :    
901 : pazsan 1.161 static void check_prims(Label symbols1[])
902 : anton 1.47 {
903 :     int i;
904 : anton 1.90 #ifndef NO_DYNAMIC
905 : anton 1.146 Label *symbols2, *symbols3, *ends1, *ends1j, *ends1jsorted, *goto_p;
906 : anton 1.119 int nends1j;
907 : anton 1.90 #endif
908 : anton 1.47
909 : anton 1.66 if (debug)
910 :     #ifdef __VERSION__
911 :     fprintf(stderr, "Compiled with gcc-" __VERSION__ "\n");
912 :     #else
913 :     #define xstr(s) str(s)
914 :     #define str(s) #s
915 :     fprintf(stderr, "Compiled with gcc-" xstr(__GNUC__) "." xstr(__GNUC_MINOR__) "\n");
916 :     #endif
917 : anton 1.121 for (i=0; symbols1[i]!=0; i++)
918 : anton 1.47 ;
919 : anton 1.55 npriminfos = i;
920 : anton 1.70
921 :     #ifndef NO_DYNAMIC
922 : anton 1.66 if (no_dynamic)
923 :     return;
924 : pazsan 1.164 symbols2=gforth_engine2(0,0,0,0,0);
925 : anton 1.70 #if NO_IP
926 : pazsan 1.164 symbols3=gforth_engine3(0,0,0,0,0);
927 : anton 1.70 #else
928 :     symbols3=symbols1;
929 :     #endif
930 : anton 1.121 ends1 = symbols1+i+1;
931 : anton 1.119 ends1j = ends1+i;
932 : anton 1.146 goto_p = ends1j+i+1; /* goto_p[0]==before; ...[1]==after;*/
933 : anton 1.121 nends1j = i+1;
934 : anton 1.119 ends1jsorted = (Label *)alloca(nends1j*sizeof(Label));
935 :     memcpy(ends1jsorted,ends1j,nends1j*sizeof(Label));
936 :     qsort(ends1jsorted, nends1j, sizeof(Label), compare_labels);
937 : anton 1.146
938 :     /* check whether the "goto *" is relocatable */
939 :     goto_len = goto_p[1]-goto_p[0];
940 :     debugp(stderr, "goto * %p %p len=%ld\n",
941 :     goto_p[0],symbols2[goto_p-symbols1],goto_len);
942 :     if (memcmp(goto_p[0],symbols2[goto_p-symbols1],goto_len)!=0) { /* unequal */
943 :     no_dynamic=1;
944 :     debugp(stderr," not relocatable, disabling dynamic code generation\n");
945 : anton 1.148 init_ss_cost();
946 : anton 1.146 return;
947 :     }
948 :     goto_start = goto_p[0];
949 : anton 1.113
950 : anton 1.47 priminfos = calloc(i,sizeof(PrimInfo));
951 : anton 1.121 for (i=0; symbols1[i]!=0; i++) {
952 : anton 1.70 int prim_len = ends1[i]-symbols1[i];
953 : anton 1.47 PrimInfo *pi=&priminfos[i];
954 : anton 1.154 struct cost *sc=&super_costs[i];
955 : anton 1.70 int j=0;
956 :     char *s1 = (char *)symbols1[i];
957 :     char *s2 = (char *)symbols2[i];
958 :     char *s3 = (char *)symbols3[i];
959 : anton 1.119 Label endlabel = bsearch_next(symbols1[i]+1,ends1jsorted,nends1j);
960 : anton 1.70
961 :     pi->start = s1;
962 : anton 1.121 pi->superend = superend[i]|no_super;
963 : anton 1.147 pi->length = prim_len;
964 : anton 1.113 pi->restlength = endlabel - symbols1[i] - pi->length;
965 : anton 1.70 pi->nimmargs = 0;
966 : pazsan 1.144 relocs++;
967 : anton 1.154 debugp(stderr, "%-15s %d-%d %4d %p %p %p len=%3ld rest=%2ld send=%1d",
968 :     prim_names[i], sc->state_in, sc->state_out,
969 :     i, s1, s2, s3, (long)(pi->length), (long)(pi->restlength),
970 :     pi->superend);
971 : anton 1.114 if (endlabel == NULL) {
972 :     pi->start = NULL; /* not relocatable */
973 : anton 1.122 if (pi->length<0) pi->length=100;
974 : pazsan 1.144 debugp(stderr,"\n non_reloc: no J label > start found\n");
975 :     relocs--;
976 :     nonrelocs++;
977 : anton 1.114 continue;
978 :     }
979 :     if (ends1[i] > endlabel && !pi->superend) {
980 : anton 1.113 pi->start = NULL; /* not relocatable */
981 : anton 1.122 pi->length = endlabel-symbols1[i];
982 : pazsan 1.144 debugp(stderr,"\n non_reloc: there is a J label before the K label (restlength<0)\n");
983 :     relocs--;
984 :     nonrelocs++;
985 : anton 1.113 continue;
986 :     }
987 : anton 1.114 if (ends1[i] < pi->start && !pi->superend) {
988 : anton 1.113 pi->start = NULL; /* not relocatable */
989 : anton 1.122 pi->length = endlabel-symbols1[i];
990 : pazsan 1.144 debugp(stderr,"\n non_reloc: K label before I label (length<0)\n");
991 :     relocs--;
992 :     nonrelocs++;
993 : anton 1.113 continue;
994 :     }
995 : anton 1.138 assert(pi->length>=0);
996 : anton 1.113 assert(pi->restlength >=0);
997 : anton 1.74 while (j<(pi->length+pi->restlength)) {
998 : anton 1.70 if (s1[j]==s3[j]) {
999 :     if (s1[j] != s2[j]) {
1000 :     pi->start = NULL; /* not relocatable */
1001 : pazsan 1.144 debugp(stderr,"\n non_reloc: engine1!=engine2 offset %3d",j);
1002 : anton 1.74 /* assert(j<prim_len); */
1003 : pazsan 1.144 relocs--;
1004 :     nonrelocs++;
1005 : anton 1.70 break;
1006 :     }
1007 :     j++;
1008 :     } else {
1009 :     struct immarg *ia=&pi->immargs[pi->nimmargs];
1010 :    
1011 :     pi->nimmargs++;
1012 :     ia->offset=j;
1013 :     if ((~*(Cell *)&(s1[j]))==*(Cell *)&(s3[j])) {
1014 :     ia->rel=0;
1015 : pazsan 1.144 debugp(stderr,"\n absolute immarg: offset %3d",j);
1016 : anton 1.70 } else if ((&(s1[j]))+(*(Cell *)&(s1[j]))+4 ==
1017 :     symbols1[DOESJUMP+1]) {
1018 :     ia->rel=1;
1019 : pazsan 1.144 debugp(stderr,"\n relative immarg: offset %3d",j);
1020 : anton 1.70 } else {
1021 :     pi->start = NULL; /* not relocatable */
1022 : pazsan 1.144 debugp(stderr,"\n non_reloc: engine1!=engine3 offset %3d",j);
1023 : anton 1.74 /* assert(j<prim_len);*/
1024 : pazsan 1.144 relocs--;
1025 :     nonrelocs++;
1026 : anton 1.70 break;
1027 :     }
1028 :     j+=4;
1029 : anton 1.47 }
1030 :     }
1031 : pazsan 1.144 debugp(stderr,"\n");
1032 : anton 1.70 }
1033 : anton 1.76 decomp_prims = calloc(i,sizeof(PrimInfo *));
1034 :     for (i=DOESJUMP+1; i<npriminfos; i++)
1035 :     decomp_prims[i] = &(priminfos[i]);
1036 :     qsort(decomp_prims+DOESJUMP+1, npriminfos-DOESJUMP-1, sizeof(PrimInfo *),
1037 :     compare_priminfo_length);
1038 : anton 1.70 #endif
1039 :     }
1040 :    
1041 : pazsan 1.161 static void flush_to_here(void)
1042 : anton 1.74 {
1043 : anton 1.93 #ifndef NO_DYNAMIC
1044 : anton 1.100 if (start_flush)
1045 :     FLUSH_ICACHE(start_flush, code_here-start_flush);
1046 : anton 1.74 start_flush=code_here;
1047 : anton 1.93 #endif
1048 : anton 1.74 }
1049 :    
1050 : anton 1.93 #ifndef NO_DYNAMIC
1051 : pazsan 1.161 static void append_jump(void)
1052 : anton 1.74 {
1053 :     if (last_jump) {
1054 :     PrimInfo *pi = &priminfos[last_jump];
1055 :    
1056 :     memcpy(code_here, pi->start+pi->length, pi->restlength);
1057 :     code_here += pi->restlength;
1058 : anton 1.147 memcpy(code_here, goto_start, goto_len);
1059 :     code_here += goto_len;
1060 : anton 1.74 last_jump=0;
1061 :     }
1062 :     }
1063 :    
1064 : anton 1.75 /* Gforth remembers all code blocks in this list. On forgetting (by
1065 :     executing a marker) the code blocks are not freed (because Gforth does
1066 :     not remember how they were allocated; hmm, remembering that might be
1067 :     easier and cleaner). Instead, code_here etc. are reset to the old
1068 :     value, and the "forgotten" code blocks are reused when they are
1069 :     needed. */
1070 :    
1071 :     struct code_block_list {
1072 :     struct code_block_list *next;
1073 :     Address block;
1074 :     Cell size;
1075 :     } *code_block_list=NULL, **next_code_blockp=&code_block_list;
1076 :    
1077 : pazsan 1.161 static Address append_prim(Cell p)
1078 : anton 1.74 {
1079 :     PrimInfo *pi = &priminfos[p];
1080 :     Address old_code_here = code_here;
1081 :    
1082 : anton 1.159 if (code_area+code_area_size < code_here+pi->length+pi->restlength+goto_len) {
1083 : anton 1.75 struct code_block_list *p;
1084 : anton 1.74 append_jump();
1085 : anton 1.93 flush_to_here();
1086 : anton 1.75 if (*next_code_blockp == NULL) {
1087 : pazsan 1.161 code_here = start_flush = code_area = gforth_alloc(code_area_size);
1088 : anton 1.75 p = (struct code_block_list *)malloc(sizeof(struct code_block_list));
1089 :     *next_code_blockp = p;
1090 :     p->next = NULL;
1091 :     p->block = code_here;
1092 :     p->size = code_area_size;
1093 :     } else {
1094 :     p = *next_code_blockp;
1095 :     code_here = start_flush = code_area = p->block;
1096 :     }
1097 : anton 1.74 old_code_here = code_here;
1098 : anton 1.75 next_code_blockp = &(p->next);
1099 : anton 1.74 }
1100 :     memcpy(code_here, pi->start, pi->length);
1101 :     code_here += pi->length;
1102 :     return old_code_here;
1103 :     }
1104 :     #endif
1105 : anton 1.75
1106 :     int forget_dyncode(Address code)
1107 :     {
1108 :     #ifdef NO_DYNAMIC
1109 :     return -1;
1110 :     #else
1111 :     struct code_block_list *p, **pp;
1112 :    
1113 :     for (pp=&code_block_list, p=*pp; p!=NULL; pp=&(p->next), p=*pp) {
1114 :     if (code >= p->block && code < p->block+p->size) {
1115 :     next_code_blockp = &(p->next);
1116 :     code_here = start_flush = code;
1117 :     code_area = p->block;
1118 :     last_jump = 0;
1119 :     return -1;
1120 :     }
1121 :     }
1122 : anton 1.78 return -no_dynamic;
1123 : anton 1.75 #endif /* !defined(NO_DYNAMIC) */
1124 :     }
1125 :    
1126 : pazsan 1.161 static long dyncodesize(void)
1127 : anton 1.104 {
1128 :     #ifndef NO_DYNAMIC
1129 : anton 1.106 struct code_block_list *p;
1130 : anton 1.104 long size=0;
1131 :     for (p=code_block_list; p!=NULL; p=p->next) {
1132 :     if (code_here >= p->block && code_here < p->block+p->size)
1133 :     return size + (code_here - p->block);
1134 :     else
1135 :     size += p->size;
1136 :     }
1137 :     #endif /* !defined(NO_DYNAMIC) */
1138 :     return 0;
1139 :     }
1140 :    
1141 : anton 1.90 Label decompile_code(Label _code)
1142 : anton 1.75 {
1143 : anton 1.76 #ifdef NO_DYNAMIC
1144 : anton 1.90 return _code;
1145 : anton 1.76 #else /* !defined(NO_DYNAMIC) */
1146 :     Cell i;
1147 : anton 1.77 struct code_block_list *p;
1148 : anton 1.90 Address code=_code;
1149 : anton 1.76
1150 : anton 1.77 /* first, check if we are in code at all */
1151 :     for (p = code_block_list;; p = p->next) {
1152 :     if (p == NULL)
1153 :     return code;
1154 :     if (code >= p->block && code < p->block+p->size)
1155 :     break;
1156 :     }
1157 : anton 1.76 /* reverse order because NOOP might match other prims */
1158 :     for (i=npriminfos-1; i>DOESJUMP; i--) {
1159 :     PrimInfo *pi=decomp_prims[i];
1160 :     if (pi->start==code || (pi->start && memcmp(code,pi->start,pi->length)==0))
1161 : anton 1.121 return vm_prims[super2[super_costs[pi-priminfos].offset]];
1162 : anton 1.118 /* return pi->start;*/
1163 : anton 1.76 }
1164 :     return code;
1165 :     #endif /* !defined(NO_DYNAMIC) */
1166 : anton 1.75 }
1167 : anton 1.74
1168 : anton 1.70 #ifdef NO_IP
1169 :     int nbranchinfos=0;
1170 :    
1171 :     struct branchinfo {
1172 : anton 1.136 Label **targetpp; /* **(bi->targetpp) is the target */
1173 : anton 1.70 Cell *addressptr; /* store the target here */
1174 :     } branchinfos[100000];
1175 :    
1176 :     int ndoesexecinfos=0;
1177 :     struct doesexecinfo {
1178 :     int branchinfo; /* fix the targetptr of branchinfos[...->branchinfo] */
1179 : anton 1.136 Label *targetp; /*target for branch (because this is not in threaded code)*/
1180 : anton 1.70 Cell *xt; /* cfa of word whose does-code needs calling */
1181 :     } doesexecinfos[10000];
1182 :    
1183 : pazsan 1.161 static void set_rel_target(Cell *source, Label target)
1184 : anton 1.70 {
1185 :     *source = ((Cell)target)-(((Cell)source)+4);
1186 :     }
1187 :    
1188 : pazsan 1.161 static void register_branchinfo(Label source, Cell *targetpp)
1189 : anton 1.70 {
1190 :     struct branchinfo *bi = &(branchinfos[nbranchinfos]);
1191 : anton 1.136 bi->targetpp = (Label **)targetpp;
1192 : anton 1.70 bi->addressptr = (Cell *)source;
1193 :     nbranchinfos++;
1194 :     }
1195 :    
1196 : pazsan 1.161 static Address compile_prim1arg(PrimNum p, Cell **argp)
1197 : anton 1.70 {
1198 : anton 1.133 Address old_code_here=append_prim(p);
1199 : anton 1.70
1200 : anton 1.74 assert(vm_prims[p]==priminfos[p].start);
1201 : anton 1.133 *argp = (Cell*)(old_code_here+priminfos[p].immargs[0].offset);
1202 :     return old_code_here;
1203 : anton 1.70 }
1204 :    
1205 : pazsan 1.161 static Address compile_call2(Cell *targetpp, Cell **next_code_targetp)
1206 : anton 1.70 {
1207 : anton 1.73 PrimInfo *pi = &priminfos[N_call2];
1208 : anton 1.74 Address old_code_here = append_prim(N_call2);
1209 : anton 1.70
1210 : anton 1.134 *next_code_targetp = (Cell *)(old_code_here + pi->immargs[0].offset);
1211 : anton 1.136 register_branchinfo(old_code_here + pi->immargs[1].offset, targetpp);
1212 : anton 1.134 return old_code_here;
1213 : anton 1.70 }
1214 :     #endif
1215 :    
1216 :     void finish_code(void)
1217 :     {
1218 :     #ifdef NO_IP
1219 :     Cell i;
1220 :    
1221 :     compile_prim1(NULL);
1222 :     for (i=0; i<ndoesexecinfos; i++) {
1223 :     struct doesexecinfo *dei = &doesexecinfos[i];
1224 : anton 1.136 dei->targetp = (Label *)DOES_CODE1((dei->xt));
1225 :     branchinfos[dei->branchinfo].targetpp = &(dei->targetp);
1226 : anton 1.70 }
1227 :     ndoesexecinfos = 0;
1228 :     for (i=0; i<nbranchinfos; i++) {
1229 :     struct branchinfo *bi=&branchinfos[i];
1230 : anton 1.136 set_rel_target(bi->addressptr, **(bi->targetpp));
1231 : anton 1.70 }
1232 :     nbranchinfos = 0;
1233 : anton 1.128 #else
1234 :     compile_prim1(NULL);
1235 : anton 1.48 #endif
1236 : anton 1.93 flush_to_here();
1237 : anton 1.48 }
1238 :    
1239 : pazsan 1.162 #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
1240 : anton 1.128 #ifdef NO_IP
1241 : pazsan 1.161 static Cell compile_prim_dyn(PrimNum p, Cell *tcp)
1242 : anton 1.128 /* compile prim #p dynamically (mod flags etc.) and return start
1243 :     address of generated code for putting it into the threaded
1244 :     code. This function is only called if all the associated
1245 :     inline arguments of p are already in place (at tcp[1] etc.) */
1246 :     {
1247 :     PrimInfo *pi=&priminfos[p];
1248 :     Cell *next_code_target=NULL;
1249 : anton 1.135 Address codeaddr;
1250 :     Address primstart;
1251 : anton 1.128
1252 :     assert(p<npriminfos);
1253 :     if (p==N_execute || p==N_perform || p==N_lit_perform) {
1254 : anton 1.134 codeaddr = compile_prim1arg(N_set_next_code, &next_code_target);
1255 : anton 1.135 primstart = append_prim(p);
1256 :     goto other_prim;
1257 :     } else if (p==N_call) {
1258 : anton 1.136 codeaddr = compile_call2(tcp+1, &next_code_target);
1259 : anton 1.128 } else if (p==N_does_exec) {
1260 :     struct doesexecinfo *dei = &doesexecinfos[ndoesexecinfos++];
1261 : anton 1.133 Cell *arg;
1262 :     codeaddr = compile_prim1arg(N_lit,&arg);
1263 :     *arg = (Cell)PFA(tcp[1]);
1264 : anton 1.128 /* we cannot determine the callee now (last_start[1] may be a
1265 :     forward reference), so just register an arbitrary target, and
1266 :     register in dei that we need to fix this before resolving
1267 :     branches */
1268 :     dei->branchinfo = nbranchinfos;
1269 :     dei->xt = (Cell *)(tcp[1]);
1270 : anton 1.134 compile_call2(0, &next_code_target);
1271 : anton 1.128 } else if (!is_relocatable(p)) {
1272 : anton 1.133 Cell *branch_target;
1273 :     codeaddr = compile_prim1arg(N_set_next_code, &next_code_target);
1274 :     compile_prim1arg(N_branch,&branch_target);
1275 :     set_rel_target(branch_target,vm_prims[p]);
1276 : anton 1.128 } else {
1277 :     unsigned j;
1278 : anton 1.135
1279 :     codeaddr = primstart = append_prim(p);
1280 :     other_prim:
1281 : anton 1.128 for (j=0; j<pi->nimmargs; j++) {
1282 :     struct immarg *ia = &(pi->immargs[j]);
1283 : anton 1.136 Cell *argp = tcp + pi->nimmargs - j;
1284 :     Cell argval = *argp; /* !! specific to prims */
1285 : anton 1.128 if (ia->rel) { /* !! assumption: relative refs are branches */
1286 : anton 1.136 register_branchinfo(primstart + ia->offset, argp);
1287 : anton 1.128 } else /* plain argument */
1288 : anton 1.135 *(Cell *)(primstart + ia->offset) = argval;
1289 : anton 1.128 }
1290 :     }
1291 :     if (next_code_target!=NULL)
1292 :     *next_code_target = (Cell)code_here;
1293 : anton 1.135 return (Cell)codeaddr;
1294 : anton 1.128 }
1295 :     #else /* !defined(NO_IP) */
1296 : pazsan 1.161 static Cell compile_prim_dyn(PrimNum p, Cell *tcp)
1297 : anton 1.128 /* compile prim #p dynamically (mod flags etc.) and return start
1298 :     address of generated code for putting it into the threaded code */
1299 : anton 1.108 {
1300 : anton 1.121 Cell static_prim = (Cell)vm_prims[p];
1301 : anton 1.108 #if defined(NO_DYNAMIC)
1302 :     return static_prim;
1303 :     #else /* !defined(NO_DYNAMIC) */
1304 :     Address old_code_here;
1305 :    
1306 :     if (no_dynamic)
1307 :     return static_prim;
1308 : anton 1.125 if (p>=npriminfos || !is_relocatable(p)) {
1309 : anton 1.108 append_jump();
1310 :     return static_prim;
1311 :     }
1312 :     old_code_here = append_prim(p);
1313 : anton 1.147 last_jump = p;
1314 :     if (priminfos[p].superend)
1315 :     append_jump();
1316 : anton 1.108 return (Cell)old_code_here;
1317 :     #endif /* !defined(NO_DYNAMIC) */
1318 :     }
1319 : anton 1.128 #endif /* !defined(NO_IP) */
1320 : pazsan 1.162 #endif
1321 : anton 1.70
1322 : anton 1.109 #ifndef NO_DYNAMIC
1323 : pazsan 1.161 static int cost_codesize(int prim)
1324 : anton 1.109 {
1325 : anton 1.121 return priminfos[prim].length;
1326 : anton 1.109 }
1327 :     #endif
1328 :    
1329 : pazsan 1.161 static int cost_ls(int prim)
1330 : anton 1.109 {
1331 :     struct cost *c = super_costs+prim;
1332 :    
1333 :     return c->loads + c->stores;
1334 :     }
1335 :    
1336 : pazsan 1.161 static int cost_lsu(int prim)
1337 : anton 1.109 {
1338 :     struct cost *c = super_costs+prim;
1339 :    
1340 :     return c->loads + c->stores + c->updates;
1341 :     }
1342 :    
1343 : pazsan 1.161 static int cost_nexts(int prim)
1344 : anton 1.109 {
1345 :     return 1;
1346 :     }
1347 :    
1348 :     typedef int Costfunc(int);
1349 :     Costfunc *ss_cost = /* cost function for optimize_bb */
1350 :     #ifdef NO_DYNAMIC
1351 :     cost_lsu;
1352 :     #else
1353 :     cost_codesize;
1354 :     #endif
1355 :    
1356 : anton 1.110 struct {
1357 :     Costfunc *costfunc;
1358 :     char *metricname;
1359 :     long sum;
1360 :     } cost_sums[] = {
1361 :     #ifndef NO_DYNAMIC
1362 :     { cost_codesize, "codesize", 0 },
1363 :     #endif
1364 :     { cost_ls, "ls", 0 },
1365 :     { cost_lsu, "lsu", 0 },
1366 :     { cost_nexts, "nexts", 0 }
1367 :     };
1368 :    
1369 : anton 1.148 #ifndef NO_DYNAMIC
1370 :     void init_ss_cost(void) {
1371 :     if (no_dynamic && ss_cost == cost_codesize) {
1372 :     ss_cost = cost_nexts;
1373 :     cost_sums[0] = cost_sums[1]; /* don't use cost_codesize for print-metrics */
1374 :     debugp(stderr, "--no-dynamic conflicts with --ss-min-codesize, reverting to --ss-min-nexts\n");
1375 :     }
1376 :     }
1377 :     #endif
1378 :    
1379 : anton 1.106 #define MAX_BB 128 /* maximum number of instructions in BB */
1380 : anton 1.125 #define INF_COST 1000000 /* infinite cost */
1381 :     #define CANONICAL_STATE 0
1382 :    
1383 :     struct waypoint {
1384 :     int cost; /* the cost from here to the end */
1385 :     PrimNum inst; /* the inst used from here to the next waypoint */
1386 :     char relocatable; /* the last non-transition was relocatable */
1387 :     char no_transition; /* don't use the next transition (relocatability)
1388 :     * or this transition (does not change state) */
1389 :     };
1390 :    
1391 : anton 1.156 struct tpa_state { /* tree parsing automaton (like) state */
1392 : anton 1.155 /* labeling is back-to-front */
1393 :     struct waypoint *inst; /* in front of instruction */
1394 :     struct waypoint *trans; /* in front of instruction and transition */
1395 :     };
1396 :    
1397 : anton 1.156 struct tpa_state *termstate = NULL; /* initialized in loader() */
1398 : anton 1.155
1399 : anton 1.158 /* statistics about tree parsing (lazyburg) stuff */
1400 :     long lb_basic_blocks = 0;
1401 :     long lb_labeler_steps = 0;
1402 :     long lb_labeler_automaton = 0;
1403 :     long lb_labeler_dynprog = 0;
1404 :     long lb_newstate_equiv = 0;
1405 :     long lb_newstate_new = 0;
1406 :     long lb_applicable_base_rules = 0;
1407 :     long lb_applicable_chain_rules = 0;
1408 :    
1409 : pazsan 1.162 #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
1410 : pazsan 1.161 static void init_waypoints(struct waypoint ws[])
1411 : anton 1.125 {
1412 :     int k;
1413 :    
1414 :     for (k=0; k<maxstates; k++)
1415 :     ws[k].cost=INF_COST;
1416 :     }
1417 : anton 1.106
1418 : pazsan 1.161 static struct tpa_state *empty_tpa_state()
1419 : anton 1.155 {
1420 : anton 1.156 struct tpa_state *s = malloc(sizeof(struct tpa_state));
1421 : anton 1.155
1422 : anton 1.157 s->inst = calloc(maxstates,sizeof(struct waypoint));
1423 : anton 1.155 init_waypoints(s->inst);
1424 : anton 1.157 s->trans = calloc(maxstates,sizeof(struct waypoint));
1425 : anton 1.155 /* init_waypoints(s->trans);*/
1426 :     return s;
1427 :     }
1428 :    
1429 : pazsan 1.161 static void transitions(struct tpa_state *t)
1430 : anton 1.107 {
1431 : anton 1.125 int k;
1432 :     struct super_state *l;
1433 :    
1434 :     for (k=0; k<maxstates; k++) {
1435 : anton 1.155 t->trans[k] = t->inst[k];
1436 :     t->trans[k].no_transition = 1;
1437 : anton 1.125 }
1438 :     for (l = state_transitions; l != NULL; l = l->next) {
1439 :     PrimNum s = l->super;
1440 :     int jcost;
1441 :     struct cost *c=super_costs+s;
1442 : anton 1.155 struct waypoint *wi=&(t->trans[c->state_in]);
1443 :     struct waypoint *wo=&(t->inst[c->state_out]);
1444 : anton 1.158 lb_applicable_chain_rules++;
1445 : anton 1.125 if (wo->cost == INF_COST)
1446 :     continue;
1447 :     jcost = wo->cost + ss_cost(s);
1448 :     if (jcost <= wi->cost) {
1449 :     wi->cost = jcost;
1450 :     wi->inst = s;
1451 :     wi->relocatable = wo->relocatable;
1452 :     wi->no_transition = 0;
1453 :     /* if (ss_greedy) wi->cost = wo->cost ? */
1454 :     }
1455 :     }
1456 :     }
1457 : anton 1.107
1458 : pazsan 1.161 static struct tpa_state *make_termstate()
1459 : anton 1.155 {
1460 : anton 1.157 struct tpa_state *s = empty_tpa_state();
1461 : anton 1.155
1462 :     s->inst[CANONICAL_STATE].cost = 0;
1463 :     transitions(s);
1464 :     return s;
1465 :     }
1466 : pazsan 1.162 #endif
1467 : anton 1.155
1468 : anton 1.156 #define TPA_SIZE 16384
1469 :    
1470 :     struct tpa_entry {
1471 :     struct tpa_entry *next;
1472 :     PrimNum inst;
1473 :     struct tpa_state *state_behind; /* note: brack-to-front labeling */
1474 :     struct tpa_state *state_infront; /* note: brack-to-front labeling */
1475 :     } *tpa_table[TPA_SIZE];
1476 :    
1477 : pazsan 1.162 #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
1478 : pazsan 1.161 static Cell hash_tpa(PrimNum p, struct tpa_state *t)
1479 : anton 1.156 {
1480 :     UCell it = (UCell )t;
1481 :     return (p+it+(it>>14))&(TPA_SIZE-1);
1482 :     }
1483 :    
1484 : pazsan 1.161 static struct tpa_state **lookup_tpa(PrimNum p, struct tpa_state *t2)
1485 : anton 1.156 {
1486 :     int hash=hash_tpa(p, t2);
1487 :     struct tpa_entry *te = tpa_table[hash];
1488 :    
1489 : anton 1.158 if (tpa_noautomaton) {
1490 :     static struct tpa_state *t;
1491 :     t = NULL;
1492 :     return &t;
1493 :     }
1494 : anton 1.156 for (; te!=NULL; te = te->next) {
1495 :     if (p == te->inst && t2 == te->state_behind)
1496 :     return &(te->state_infront);
1497 :     }
1498 :     te = (struct tpa_entry *)malloc(sizeof(struct tpa_entry));
1499 :     te->next = tpa_table[hash];
1500 :     te->inst = p;
1501 :     te->state_behind = t2;
1502 :     te->state_infront = NULL;
1503 :     tpa_table[hash] = te;
1504 :     return &(te->state_infront);
1505 :     }
1506 :    
1507 : pazsan 1.161 static void tpa_state_normalize(struct tpa_state *t)
1508 : anton 1.157 {
1509 :     /* normalize so cost of canonical state=0; this may result in
1510 :     negative states for some states */
1511 :     int d = t->inst[CANONICAL_STATE].cost;
1512 :     int i;
1513 :    
1514 :     for (i=0; i<maxstates; i++) {
1515 :     if (t->inst[i].cost != INF_COST)
1516 :     t->inst[i].cost -= d;
1517 :     if (t->trans[i].cost != INF_COST)
1518 :     t->trans[i].cost -= d;
1519 :     }
1520 :     }
1521 :    
1522 : pazsan 1.161 static int tpa_state_equivalent(struct tpa_state *t1, struct tpa_state *t2)
1523 : anton 1.157 {
1524 :     return (memcmp(t1->inst, t2->inst, maxstates*sizeof(struct waypoint)) == 0 &&
1525 :     memcmp(t1->trans,t2->trans,maxstates*sizeof(struct waypoint)) == 0);
1526 :     }
1527 : pazsan 1.162 #endif
1528 : anton 1.157
1529 :     struct tpa_state_entry {
1530 :     struct tpa_state_entry *next;
1531 :     struct tpa_state *state;
1532 :     } *tpa_state_table[TPA_SIZE];
1533 :    
1534 : pazsan 1.163 #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
1535 : pazsan 1.161 static Cell hash_tpa_state(struct tpa_state *t)
1536 : anton 1.157 {
1537 :     int *ti = (int *)(t->inst);
1538 :     int *tt = (int *)(t->trans);
1539 :     int r=0;
1540 :     int i;
1541 :    
1542 :     for (i=0; ti+i < (int *)(t->inst+maxstates); i++)
1543 :     r += ti[i]+tt[i];
1544 :     return (r+(r>>14)+(r>>22)) & (TPA_SIZE-1);
1545 :     }
1546 :    
1547 : pazsan 1.161 static struct tpa_state *lookup_tpa_state(struct tpa_state *t)
1548 : anton 1.157 {
1549 :     Cell hash = hash_tpa_state(t);
1550 :     struct tpa_state_entry *te = tpa_state_table[hash];
1551 :     struct tpa_state_entry *tn;
1552 :    
1553 : anton 1.158 if (!tpa_noequiv) {
1554 :     for (; te!=NULL; te = te->next) {
1555 :     if (tpa_state_equivalent(t, te->state)) {
1556 :     lb_newstate_equiv++;
1557 :     free(t->inst);
1558 :     free(t->trans);
1559 :     free(t);
1560 :     return te->state;
1561 :     }
1562 : anton 1.157 }
1563 : anton 1.158 tn = (struct tpa_state_entry *)malloc(sizeof(struct tpa_state_entry));
1564 :     tn->next = te;
1565 :     tn->state = t;
1566 :     tpa_state_table[hash] = tn;
1567 :     }
1568 :     lb_newstate_new++;
1569 :     if (tpa_trace)
1570 :     fprintf(stderr, "%ld %ld lb_states\n", lb_labeler_steps, lb_newstate_new);
1571 : anton 1.157 return t;
1572 :     }
1573 :    
1574 : anton 1.125 /* use dynamic programming to find the shortest paths within the basic
1575 :     block origs[0..ninsts-1] and rewrite the instructions pointed to by
1576 :     instps to use it */
1577 : pazsan 1.161 static void optimize_rewrite(Cell *instps[], PrimNum origs[], int ninsts)
1578 : anton 1.125 {
1579 :     int i,j;
1580 : anton 1.156 struct tpa_state *ts[ninsts+1];
1581 : anton 1.125 int nextdyn, nextstate, no_transition;
1582 :    
1583 : anton 1.158 lb_basic_blocks++;
1584 : anton 1.155 ts[ninsts] = termstate;
1585 : anton 1.107 for (i=ninsts-1; i>=0; i--) {
1586 : anton 1.156 struct tpa_state **tp = lookup_tpa(origs[i],ts[i+1]);
1587 :     struct tpa_state *t = *tp;
1588 : anton 1.158 lb_labeler_steps++;
1589 :     if (t) {
1590 : anton 1.156 ts[i] = t;
1591 : anton 1.158 lb_labeler_automaton++;
1592 :     }
1593 : anton 1.156 else {
1594 : anton 1.158 lb_labeler_dynprog++;
1595 : anton 1.156 ts[i] = empty_tpa_state();
1596 :     for (j=1; j<=max_super && i+j<=ninsts; j++) {
1597 :     struct super_state **superp = lookup_super(origs+i, j);
1598 :     if (superp!=NULL) {
1599 :     struct super_state *supers = *superp;
1600 :     for (; supers!=NULL; supers = supers->next) {
1601 :     PrimNum s = supers->super;
1602 :     int jcost;
1603 :     struct cost *c=super_costs+s;
1604 :     struct waypoint *wi=&(ts[i]->inst[c->state_in]);
1605 :     struct waypoint *wo=&(ts[i+j]->trans[c->state_out]);
1606 :     int no_transition = wo->no_transition;
1607 : anton 1.158 lb_applicable_base_rules++;
1608 : anton 1.156 if (!(is_relocatable(s)) && !wo->relocatable) {
1609 :     wo=&(ts[i+j]->inst[c->state_out]);
1610 :     no_transition=1;
1611 :     }
1612 :     if (wo->cost == INF_COST)
1613 :     continue;
1614 :     jcost = wo->cost + ss_cost(s);
1615 :     if (jcost <= wi->cost) {
1616 :     wi->cost = jcost;
1617 :     wi->inst = s;
1618 :     wi->relocatable = is_relocatable(s);
1619 :     wi->no_transition = no_transition;
1620 :     /* if (ss_greedy) wi->cost = wo->cost ? */
1621 :     }
1622 : anton 1.125 }
1623 : anton 1.107 }
1624 :     }
1625 : anton 1.156 transitions(ts[i]);
1626 : anton 1.157 tpa_state_normalize(ts[i]);
1627 :     *tp = ts[i] = lookup_tpa_state(ts[i]);
1628 : anton 1.158 if (tpa_trace)
1629 :     fprintf(stderr, "%ld %ld lb_table_entries\n", lb_labeler_steps, lb_labeler_dynprog);
1630 : anton 1.107 }
1631 : anton 1.125 }
1632 :     /* now rewrite the instructions */
1633 :     nextdyn=0;
1634 :     nextstate=CANONICAL_STATE;
1635 : anton 1.155 no_transition = ((!ts[0]->trans[nextstate].relocatable)
1636 :     ||ts[0]->trans[nextstate].no_transition);
1637 : anton 1.125 for (i=0; i<ninsts; i++) {
1638 :     Cell tc=0, tc2;
1639 :     if (i==nextdyn) {
1640 :     if (!no_transition) {
1641 :     /* process trans */
1642 : anton 1.155 PrimNum p = ts[i]->trans[nextstate].inst;
1643 : anton 1.125 struct cost *c = super_costs+p;
1644 : anton 1.155 assert(ts[i]->trans[nextstate].cost != INF_COST);
1645 : anton 1.125 assert(c->state_in==nextstate);
1646 : anton 1.128 tc = compile_prim_dyn(p,NULL);
1647 : anton 1.125 nextstate = c->state_out;
1648 :     }
1649 :     {
1650 :     /* process inst */
1651 : anton 1.155 PrimNum p = ts[i]->inst[nextstate].inst;
1652 : anton 1.125 struct cost *c=super_costs+p;
1653 :     assert(c->state_in==nextstate);
1654 : anton 1.155 assert(ts[i]->inst[nextstate].cost != INF_COST);
1655 : anton 1.125 #if defined(GFORTH_DEBUGGING)
1656 :     assert(p == origs[i]);
1657 :     #endif
1658 : anton 1.128 tc2 = compile_prim_dyn(p,instps[i]);
1659 : anton 1.125 if (no_transition || !is_relocatable(p))
1660 :     /* !! actually what we care about is if and where
1661 :     * compile_prim_dyn() puts NEXTs */
1662 :     tc=tc2;
1663 : anton 1.155 no_transition = ts[i]->inst[nextstate].no_transition;
1664 : anton 1.125 nextstate = c->state_out;
1665 :     nextdyn += c->length;
1666 :     }
1667 :     } else {
1668 :     #if defined(GFORTH_DEBUGGING)
1669 :     assert(0);
1670 :     #endif
1671 :     tc=0;
1672 : anton 1.155 /* tc= (Cell)vm_prims[ts[i]->inst[CANONICAL_STATE].inst]; */
1673 : anton 1.125 }
1674 :     *(instps[i]) = tc;
1675 :     }
1676 :     if (!no_transition) {
1677 : anton 1.155 PrimNum p = ts[i]->trans[nextstate].inst;
1678 : anton 1.125 struct cost *c = super_costs+p;
1679 :     assert(c->state_in==nextstate);
1680 : anton 1.155 assert(ts[i]->trans[nextstate].cost != INF_COST);
1681 : anton 1.125 assert(i==nextdyn);
1682 : anton 1.128 (void)compile_prim_dyn(p,NULL);
1683 : anton 1.125 nextstate = c->state_out;
1684 : anton 1.107 }
1685 : anton 1.125 assert(nextstate==CANONICAL_STATE);
1686 : anton 1.107 }
1687 : pazsan 1.162 #endif
1688 : anton 1.107
1689 : anton 1.105 /* compile *start, possibly rewriting it into a static and/or dynamic
1690 :     superinstruction */
1691 :     void compile_prim1(Cell *start)
1692 : anton 1.70 {
1693 : anton 1.108 #if defined(DOUBLY_INDIRECT)
1694 : anton 1.125 Label prim;
1695 :    
1696 :     if (start==NULL)
1697 :     return;
1698 :     prim = (Label)*start;
1699 : anton 1.108 if (prim<((Label)(xts+DOESJUMP)) || prim>((Label)(xts+npriminfos))) {
1700 :     fprintf(stderr,"compile_prim encountered xt %p\n", prim);
1701 :     *start=(Cell)prim;
1702 :     return;
1703 :     } else {
1704 :     *start = (Cell)(prim-((Label)xts)+((Label)vm_prims));
1705 :     return;
1706 :     }
1707 :     #elif defined(INDIRECT_THREADED)
1708 :     return;
1709 : anton 1.112 #else /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
1710 : anton 1.128 /* !! does not work, for unknown reasons; but something like this is
1711 :     probably needed to ensure that we don't call compile_prim_dyn
1712 :     before the inline arguments are there */
1713 :     static Cell *instps[MAX_BB];
1714 :     static PrimNum origs[MAX_BB];
1715 :     static int ninsts=0;
1716 :     PrimNum prim_num;
1717 :    
1718 :     if (start==NULL || ninsts >= MAX_BB ||
1719 :     (ninsts>0 && superend[origs[ninsts-1]])) {
1720 :     /* after bb, or at the start of the next bb */
1721 :     optimize_rewrite(instps,origs,ninsts);
1722 :     /* fprintf(stderr,"optimize_rewrite(...,%d)\n",ninsts); */
1723 :     ninsts=0;
1724 :     if (start==NULL)
1725 :     return;
1726 :     }
1727 :     prim_num = ((Xt)*start)-vm_prims;
1728 :     if(prim_num >= npriminfos) {
1729 :     optimize_rewrite(instps,origs,ninsts);
1730 : anton 1.129 /* fprintf(stderr,"optimize_rewrite(...,%d)\n",ninsts);*/
1731 : anton 1.128 ninsts=0;
1732 :     return;
1733 :     }
1734 :     assert(ninsts<MAX_BB);
1735 :     instps[ninsts] = start;
1736 :     origs[ninsts] = prim_num;
1737 :     ninsts++;
1738 : anton 1.112 #endif /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
1739 : anton 1.47 }
1740 :    
1741 : pazsan 1.161 Address gforth_loader(FILE *imagefile, char* filename)
1742 : anton 1.1 /* returns the address of the image proper (after the preamble) */
1743 :     {
1744 :     ImageHeader header;
1745 :     Address image;
1746 :     Address imp; /* image+preamble */
1747 : anton 1.17 Char magic[8];
1748 :     char magic7; /* size byte of magic number */
1749 : anton 1.1 Cell preamblesize=0;
1750 : pazsan 1.6 Cell data_offset = offset_image ? 56*sizeof(Cell) : 0;
1751 : anton 1.1 UCell check_sum;
1752 : pazsan 1.15 Cell ausize = ((RELINFOBITS == 8) ? 0 :
1753 :     (RELINFOBITS == 16) ? 1 :
1754 :     (RELINFOBITS == 32) ? 2 : 3);
1755 :     Cell charsize = ((sizeof(Char) == 1) ? 0 :
1756 :     (sizeof(Char) == 2) ? 1 :
1757 :     (sizeof(Char) == 4) ? 2 : 3) + ausize;
1758 :     Cell cellsize = ((sizeof(Cell) == 1) ? 0 :
1759 :     (sizeof(Cell) == 2) ? 1 :
1760 :     (sizeof(Cell) == 4) ? 2 : 3) + ausize;
1761 : anton 1.21 Cell sizebyte = (ausize << 5) + (charsize << 3) + (cellsize << 1) +
1762 :     #ifdef WORDS_BIGENDIAN
1763 :     0
1764 :     #else
1765 :     1
1766 :     #endif
1767 :     ;
1768 : anton 1.1
1769 : pazsan 1.164 vm_prims = gforth_engine(0,0,0,0,0);
1770 : anton 1.47 check_prims(vm_prims);
1771 : anton 1.106 prepare_super_table();
1772 : anton 1.1 #ifndef DOUBLY_INDIRECT
1773 : anton 1.59 #ifdef PRINT_SUPER_LENGTHS
1774 :     print_super_lengths();
1775 :     #endif
1776 : anton 1.43 check_sum = checksum(vm_prims);
1777 : anton 1.1 #else /* defined(DOUBLY_INDIRECT) */
1778 : anton 1.43 check_sum = (UCell)vm_prims;
1779 : anton 1.1 #endif /* defined(DOUBLY_INDIRECT) */
1780 : anton 1.155 #if !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED))
1781 :     termstate = make_termstate();
1782 :     #endif /* !(defined(DOUBLY_INDIRECT) || defined(INDIRECT_THREADED)) */
1783 : pazsan 1.10
1784 :     do {
1785 :     if(fread(magic,sizeof(Char),8,imagefile) < 8) {
1786 : anton 1.84 fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.6) image.\n",
1787 : pazsan 1.10 progname, filename);
1788 :     exit(1);
1789 : anton 1.1 }
1790 : pazsan 1.10 preamblesize+=8;
1791 : anton 1.84 } while(memcmp(magic,"Gforth3",7));
1792 : anton 1.17 magic7 = magic[7];
1793 : anton 1.1 if (debug) {
1794 : anton 1.17 magic[7]='\0';
1795 : anton 1.21 fprintf(stderr,"Magic found: %s ", magic);
1796 :     print_sizes(magic7);
1797 : anton 1.1 }
1798 :    
1799 : anton 1.21 if (magic7 != sizebyte)
1800 :     {
1801 :     fprintf(stderr,"This image is: ");
1802 :     print_sizes(magic7);
1803 :     fprintf(stderr,"whereas the machine is ");
1804 :     print_sizes(sizebyte);
1805 : anton 1.1 exit(-2);
1806 :     };
1807 :    
1808 :     fread((void *)&header,sizeof(ImageHeader),1,imagefile);
1809 : pazsan 1.10
1810 :     set_stack_sizes(&header);
1811 : anton 1.1
1812 :     #if HAVE_GETPAGESIZE
1813 :     pagesize=getpagesize(); /* Linux/GNU libc offers this */
1814 :     #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
1815 :     pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
1816 :     #elif PAGESIZE
1817 :     pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
1818 :     #endif
1819 : pazsan 1.144 debugp(stderr,"pagesize=%ld\n",(unsigned long) pagesize);
1820 : anton 1.1
1821 : anton 1.34 image = dict_alloc_read(imagefile, preamblesize+header.image_size,
1822 :     preamblesize+dictsize, data_offset);
1823 : anton 1.33 imp=image+preamblesize;
1824 : anton 1.57 alloc_stacks((ImageHeader *)imp);
1825 : anton 1.1 if (clear_dictionary)
1826 : anton 1.33 memset(imp+header.image_size, 0, dictsize-header.image_size);
1827 : anton 1.90 if(header.base==0 || header.base == (Address)0x100) {
1828 : anton 1.1 Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
1829 : pazsan 1.162 Char reloc_bits[reloc_size];
1830 : anton 1.33 fseek(imagefile, preamblesize+header.image_size, SEEK_SET);
1831 : pazsan 1.10 fread(reloc_bits, 1, reloc_size, imagefile);
1832 : pazsan 1.161 gforth_relocate((Cell *)imp, reloc_bits, header.image_size, (Cell)header.base, vm_prims);
1833 : anton 1.1 #if 0
1834 :     { /* let's see what the relocator did */
1835 :     FILE *snapshot=fopen("snapshot.fi","wb");
1836 :     fwrite(image,1,imagesize,snapshot);
1837 :     fclose(snapshot);
1838 :     }
1839 :     #endif
1840 : jwilke 1.46 }
1841 :     else if(header.base!=imp) {
1842 :     fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
1843 :     progname, (unsigned long)header.base, (unsigned long)imp);
1844 :     exit(1);
1845 : anton 1.1 }
1846 :     if (header.checksum==0)
1847 :     ((ImageHeader *)imp)->checksum=check_sum;
1848 :     else if (header.checksum != check_sum) {
1849 :     fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
1850 :     progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
1851 :     exit(1);
1852 :     }
1853 : anton 1.53 #ifdef DOUBLY_INDIRECT
1854 :     ((ImageHeader *)imp)->xt_base = xts;
1855 :     #endif
1856 : anton 1.1 fclose(imagefile);
1857 :    
1858 : anton 1.56 /* unnecessary, except maybe for CODE words */
1859 :     /* FLUSH_ICACHE(imp, header.image_size);*/
1860 : anton 1.1
1861 :     return imp;
1862 :     }
1863 :    
1864 : anton 1.72 /* pointer to last '/' or '\' in file, 0 if there is none. */
1865 : pazsan 1.161 static char *onlypath(char *filename)
1866 : pazsan 1.10 {
1867 : anton 1.72 return strrchr(filename, DIRSEP);
1868 : anton 1.1 }
1869 :    
1870 : pazsan 1.161 static FILE *openimage(char *fullfilename)
1871 : pazsan 1.10 {
1872 :     FILE *image_file;
1873 : pazsan 1.162 char * expfilename = tilde_cstr((Char *)fullfilename, strlen(fullfilename), 1);
1874 : pazsan 1.10
1875 : anton 1.28 image_file=fopen(expfilename,"rb");
1876 : anton 1.1 if (image_file!=NULL && debug)
1877 : anton 1.28 fprintf(stderr, "Opened image file: %s\n", expfilename);
1878 : pazsan 1.10 return image_file;
1879 : anton 1.1 }
1880 :    
1881 : anton 1.28 /* try to open image file concat(path[0:len],imagename) */
1882 : pazsan 1.161 static FILE *checkimage(char *path, int len, char *imagename)
1883 : pazsan 1.10 {
1884 :     int dirlen=len;
1885 : pazsan 1.162 char fullfilename[dirlen+strlen((char *)imagename)+2];
1886 : pazsan 1.10
1887 : anton 1.1 memcpy(fullfilename, path, dirlen);
1888 : pazsan 1.71 if (fullfilename[dirlen-1]!=DIRSEP)
1889 :     fullfilename[dirlen++]=DIRSEP;
1890 : anton 1.1 strcpy(fullfilename+dirlen,imagename);
1891 : pazsan 1.10 return openimage(fullfilename);
1892 : anton 1.1 }
1893 :    
1894 : pazsan 1.161 static FILE * open_image_file(char * imagename, char * path)
1895 : anton 1.1 {
1896 : pazsan 1.10 FILE * image_file=NULL;
1897 : anton 1.28 char *origpath=path;
1898 : pazsan 1.10
1899 : pazsan 1.71 if(strchr(imagename, DIRSEP)==NULL) {
1900 : pazsan 1.10 /* first check the directory where the exe file is in !! 01may97jaw */
1901 :     if (onlypath(progname))
1902 : anton 1.72 image_file=checkimage(progname, onlypath(progname)-progname, imagename);
1903 : pazsan 1.10 if (!image_file)
1904 :     do {
1905 :     char *pend=strchr(path, PATHSEP);
1906 :     if (pend==NULL)
1907 :     pend=path+strlen(path);
1908 :     if (strlen(path)==0) break;
1909 :     image_file=checkimage(path, pend-path, imagename);
1910 :     path=pend+(*pend==PATHSEP);
1911 :     } while (image_file==NULL);
1912 :     } else {
1913 :     image_file=openimage(imagename);
1914 :     }
1915 : anton 1.1
1916 : pazsan 1.10 if (!image_file) {
1917 :     fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
1918 : anton 1.28 progname, imagename, origpath);
1919 : pazsan 1.10 exit(1);
1920 : anton 1.7 }
1921 :    
1922 : pazsan 1.10 return image_file;
1923 :     }
1924 : pazsan 1.11 #endif
1925 :    
1926 :     #ifdef HAS_OS
1927 : pazsan 1.161 static UCell convsize(char *s, UCell elemsize)
1928 : pazsan 1.11 /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
1929 :     of bytes. the letter at the end indicates the unit, where e stands
1930 :     for the element size. default is e */
1931 :     {
1932 :     char *endp;
1933 :     UCell n,m;
1934 :    
1935 :     m = elemsize;
1936 :     n = strtoul(s,&endp,0);
1937 :     if (endp!=NULL) {
1938 :     if (strcmp(endp,"b")==0)
1939 :     m=1;
1940 :     else if (strcmp(endp,"k")==0)
1941 :     m=1024;
1942 :     else if (strcmp(endp,"M")==0)
1943 :     m=1024*1024;
1944 :     else if (strcmp(endp,"G")==0)
1945 :     m=1024*1024*1024;
1946 :     else if (strcmp(endp,"T")==0) {
1947 :     #if (SIZEOF_CHAR_P > 4)
1948 : anton 1.24 m=1024L*1024*1024*1024;
1949 : pazsan 1.11 #else
1950 :     fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp);
1951 :     exit(1);
1952 :     #endif
1953 :     } else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
1954 :     fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
1955 :     exit(1);
1956 :     }
1957 :     }
1958 :     return n*m;
1959 :     }
1960 : pazsan 1.10
1961 : anton 1.109 enum {
1962 :     ss_number = 256,
1963 : anton 1.125 ss_states,
1964 : anton 1.109 ss_min_codesize,
1965 :     ss_min_ls,
1966 :     ss_min_lsu,
1967 :     ss_min_nexts,
1968 :     };
1969 :    
1970 : pazsan 1.10 void gforth_args(int argc, char ** argv, char ** path, char ** imagename)
1971 :     {
1972 :     int c;
1973 :    
1974 : anton 1.1 opterr=0;
1975 :     while (1) {
1976 :     int option_index=0;
1977 :     static struct option opts[] = {
1978 : anton 1.29 {"appl-image", required_argument, NULL, 'a'},
1979 : anton 1.1 {"image-file", required_argument, NULL, 'i'},
1980 :     {"dictionary-size", required_argument, NULL, 'm'},
1981 :     {"data-stack-size", required_argument, NULL, 'd'},
1982 :     {"return-stack-size", required_argument, NULL, 'r'},
1983 :     {"fp-stack-size", required_argument, NULL, 'f'},
1984 :     {"locals-stack-size", required_argument, NULL, 'l'},
1985 :     {"path", required_argument, NULL, 'p'},
1986 :     {"version", no_argument, NULL, 'v'},
1987 :     {"help", no_argument, NULL, 'h'},
1988 :     /* put something != 0 into offset_image */
1989 :     {"offset-image", no_argument, &offset_image, 1},
1990 :     {"no-offset-im", no_argument, &offset_image, 0},
1991 :     {"clear-dictionary", no_argument, &clear_dictionary, 1},
1992 : anton 1.4 {"die-on-signal", no_argument, &die_on_signal, 1},
1993 : anton 1.169 {"ignore-async-signals", no_argument, &ignore_async_signals, 1},
1994 : anton 1.1 {"debug", no_argument, &debug, 1},
1995 : pazsan 1.144 {"diag", no_argument, &diag, 1},
1996 : anton 1.60 {"no-super", no_argument, &no_super, 1},
1997 :     {"no-dynamic", no_argument, &no_dynamic, 1},
1998 : anton 1.66 {"dynamic", no_argument, &no_dynamic, 0},
1999 : anton 1.110 {"print-metrics", no_argument, &print_metrics, 1},
2000 : anton 1.109 {"ss-number", required_argument, NULL, ss_number},
2001 : anton 1.125 {"ss-states", required_argument, NULL, ss_states},
2002 : anton 1.109 #ifndef NO_DYNAMIC
2003 :     {"ss-min-codesize", no_argument, NULL, ss_min_codesize},
2004 :     #endif
2005 :     {"ss-min-ls", no_argument, NULL, ss_min_ls},
2006 :     {"ss-min-lsu", no_argument, NULL, ss_min_lsu},
2007 :     {"ss-min-nexts", no_argument, NULL, ss_min_nexts},
2008 : anton 1.110 {"ss-greedy", no_argument, &ss_greedy, 1},
2009 : anton 1.158 {"tpa-noequiv", no_argument, &tpa_noequiv, 1},
2010 :     {"tpa-noautomaton", no_argument, &tpa_noautomaton, 1},
2011 :     {"tpa-trace", no_argument, &tpa_trace, 1},
2012 : anton 1.1 {0,0,0,0}
2013 :     /* no-init-file, no-rc? */
2014 :     };
2015 :    
2016 : pazsan 1.36 c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vhoncsx", opts, &option_index);
2017 : anton 1.1
2018 :     switch (c) {
2019 : anton 1.29 case EOF: return;
2020 :     case '?': optind--; return;
2021 :     case 'a': *imagename = optarg; return;
2022 : pazsan 1.10 case 'i': *imagename = optarg; break;
2023 : anton 1.1 case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
2024 :     case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
2025 :     case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
2026 :     case 'f': fsize = convsize(optarg,sizeof(Float)); break;
2027 :     case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
2028 : pazsan 1.10 case 'p': *path = optarg; break;
2029 : pazsan 1.36 case 'o': offset_image = 1; break;
2030 :     case 'n': offset_image = 0; break;
2031 :     case 'c': clear_dictionary = 1; break;
2032 :     case 's': die_on_signal = 1; break;
2033 :     case 'x': debug = 1; break;
2034 : anton 1.83 case 'v': fputs(PACKAGE_STRING"\n", stderr); exit(0);
2035 : anton 1.109 case ss_number: static_super_number = atoi(optarg); break;
2036 : anton 1.125 case ss_states: maxstates = max(min(atoi(optarg),MAX_STATE),1); break;
2037 : anton 1.109 #ifndef NO_DYNAMIC
2038 :     case ss_min_codesize: ss_cost = cost_codesize; break;
2039 :     #endif
2040 :     case ss_min_ls: ss_cost = cost_ls; break;
2041 :     case ss_min_lsu: ss_cost = cost_lsu; break;
2042 :     case ss_min_nexts: ss_cost = cost_nexts; break;
2043 : anton 1.1 case 'h':
2044 : anton 1.29 fprintf(stderr, "Usage: %s [engine options] ['--'] [image arguments]\n\
2045 : anton 1.1 Engine Options:\n\
2046 : anton 1.29 --appl-image FILE equivalent to '--image-file=FILE --'\n\
2047 : pazsan 1.10 --clear-dictionary Initialize the dictionary with 0 bytes\n\
2048 :     -d SIZE, --data-stack-size=SIZE Specify data stack size\n\
2049 :     --debug Print debugging information during startup\n\
2050 : pazsan 1.144 --diag Print diagnostic information during startup\n\
2051 : anton 1.169 --die-on-signal exit instead of THROWing some signals\n\
2052 : anton 1.66 --dynamic use dynamic native code\n\
2053 : pazsan 1.10 -f SIZE, --fp-stack-size=SIZE Specify floating point stack size\n\
2054 :     -h, --help Print this message and exit\n\
2055 : anton 1.169 --ignore-async-signals ignore instead of THROWing async. signals\n\
2056 : pazsan 1.10 -i FILE, --image-file=FILE Use image FILE instead of `gforth.fi'\n\
2057 :     -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
2058 :     -m SIZE, --dictionary-size=SIZE Specify Forth dictionary size\n\
2059 : anton 1.60 --no-dynamic Use only statically compiled primitives\n\
2060 : pazsan 1.10 --no-offset-im Load image at normal position\n\
2061 : anton 1.60 --no-super No dynamically formed superinstructions\n\
2062 : pazsan 1.10 --offset-image Load image at a different position\n\
2063 :     -p PATH, --path=PATH Search path for finding image and sources\n\
2064 : anton 1.110 --print-metrics Print some code generation metrics on exit\n\
2065 : pazsan 1.10 -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
2066 : anton 1.111 --ss-greedy greedy, not optimal superinst selection\n\
2067 :     --ss-min-codesize select superinsts for smallest native code\n\
2068 :     --ss-min-ls minimize loads and stores\n\
2069 :     --ss-min-lsu minimize loads, stores, and pointer updates\n\
2070 :     --ss-min-nexts minimize the number of static superinsts\n\
2071 :     --ss-number=N use N static superinsts (default max)\n\
2072 : anton 1.125 --ss-states=N N states for stack caching (default max)\n\
2073 : anton 1.158 --tpa-noequiv automaton without state equivalence\n\
2074 :     --tpa-noautomaton dynamic programming only\n\
2075 :     --tpa-trace report new states etc.\n\
2076 : anton 1.66 -v, --version Print engine version and exit\n\
2077 : anton 1.1 SIZE arguments consist of an integer followed by a unit. The unit can be\n\
2078 : pazsan 1.10 `b' (byte), `e' (element; default), `k' (KB), `M' (MB), `G' (GB) or `T' (TB).\n",
2079 :     argv[0]);
2080 :     optind--;
2081 :     return;
2082 : anton 1.1 }
2083 :     }
2084 : pazsan 1.10 }
2085 : pazsan 1.11 #endif
2086 : pazsan 1.10
2087 : pazsan 1.161 static void print_diag()
2088 : pazsan 1.144 {
2089 :    
2090 : pazsan 1.153 #if !defined(HAVE_GETRUSAGE) || (!defined(HAS_FFCALL) && !defined(HAS_LIBFFI))
2091 : pazsan 1.145 fprintf(stderr, "*** missing functionality ***\n"
2092 : pazsan 1.144 #ifndef HAVE_GETRUSAGE
2093 :     " no getrusage -> CPUTIME broken\n"
2094 :     #endif
2095 : pazsan 1.153 #if !defined(HAS_FFCALL) && !defined(HAS_LIBFFI)
2096 : pazsan 1.144 " no ffcall -> only old-style foreign function calls (no fflib.fs)\n"
2097 :     #endif
2098 :     );
2099 :     #endif
2100 :     if((relocs < nonrelocs) ||
2101 :     #if defined(BUGGY_LL_CMP) || defined(BUGGY_LL_MUL) || defined(BUGGY_LL_DIV) || defined(BUGGY_LL_ADD) || defined(BUGGY_LL_SHIFT) || defined(BUGGY_LL_D2F) || defined(BUGGY_LL_F2D)
2102 :     1
2103 :     #else
2104 :     0
2105 :     #endif
2106 :     )
2107 :     debugp(stderr, "relocs: %d:%d\n", relocs, nonrelocs);
2108 : pazsan 1.165 fprintf(stderr, "*** %sperformance problems ***\n%s",
2109 :     #if defined(BUGGY_LL_CMP) || defined(BUGGY_LL_MUL) || defined(BUGGY_LL_DIV) || defined(BUGGY_LL_ADD) || defined(BUGGY_LL_SHIFT) || defined(BUGGY_LL_D2F) || defined(BUGGY_LL_F2D) || !defined(FORCE_REG) || defined(BUGGY_LONG_LONG)
2110 :     "",
2111 :     #else
2112 :     "no ",
2113 :     #endif
2114 : pazsan 1.144 #if defined(BUGGY_LL_CMP) || defined(BUGGY_LL_MUL) || defined(BUGGY_LL_DIV) || defined(BUGGY_LL_ADD) || defined(BUGGY_LL_SHIFT) || defined(BUGGY_LL_D2F) || defined(BUGGY_LL_F2D)
2115 :     " double-cell integer type buggy ->\n "
2116 :     #ifdef BUGGY_LL_CMP
2117 :     "CMP, "
2118 :     #endif
2119 :     #ifdef BUGGY_LL_MUL
2120 :     "MUL, "
2121 :     #endif
2122 :     #ifdef BUGGY_LL_DIV
2123 :     "DIV, "
2124 :     #endif
2125 :     #ifdef BUGGY_LL_ADD
2126 :     "ADD, "
2127 :     #endif
2128 :     #ifdef BUGGY_LL_SHIFT
2129 :     "SHIFT, "
2130 :     #endif
2131 :     #ifdef BUGGY_LL_D2F
2132 :     "D2F, "
2133 :     #endif
2134 :     #ifdef BUGGY_LL_F2D
2135 :     "F2D, "
2136 :     #endif
2137 :     "\b\b slow\n"
2138 : pazsan 1.145 #endif
2139 :     #ifndef FORCE_REG
2140 :     " automatic register allocation: performance degradation possible\n"
2141 :     #endif
2142 :     #if !defined(FORCE_REG) || defined(BUGGY_LONG_LONG)
2143 :     "*** Suggested remedy: try ./configure"
2144 :     #ifndef FORCE_REG
2145 :     " --enable-force-reg"
2146 :     #endif
2147 :     #ifdef BUGGY_LONG_LONG
2148 :     " --enable-force-ll"
2149 :     #endif
2150 :     "\n"
2151 : pazsan 1.166 #else
2152 :     ""
2153 : pazsan 1.144 #endif
2154 :     ,
2155 :     (relocs < nonrelocs) ? " gcc PR 15242 -> no dynamic code generation (use gcc-2.95 instead)\n" : "");
2156 :     }
2157 :    
2158 : pazsan 1.10 #ifdef INCLUDE_IMAGE
2159 :     extern Cell image[];
2160 :     extern const char reloc_bits[];
2161 :     #endif
2162 : pazsan 1.67
2163 : pazsan 1.10 int main(int argc, char **argv, char **env)
2164 :     {
2165 : pazsan 1.30 #ifdef HAS_OS
2166 : pazsan 1.10 char *path = getenv("GFORTHPATH") ? : DEFAULTPATH;
2167 : pazsan 1.30 #else
2168 :     char *path = DEFAULTPATH;
2169 :     #endif
2170 : pazsan 1.13 #ifndef INCLUDE_IMAGE
2171 : pazsan 1.10 char *imagename="gforth.fi";
2172 :     FILE *image_file;
2173 :     Address image;
2174 :     #endif
2175 :     int retvalue;
2176 :    
2177 : anton 1.56 #if defined(i386) && defined(ALIGNMENT_CHECK)
2178 : pazsan 1.10 /* turn on alignment checks on the 486.
2179 :     * on the 386 this should have no effect. */
2180 :     __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
2181 :     /* this is unusable with Linux' libc.4.6.27, because this library is
2182 :     not alignment-clean; we would have to replace some library
2183 :     functions (e.g., memcpy) to make it work. Also GCC doesn't try to keep
2184 :     the stack FP-aligned. */
2185 :     #endif
2186 :    
2187 :     /* buffering of the user output device */
2188 : pazsan 1.11 #ifdef _IONBF
2189 : pazsan 1.10 if (isatty(fileno(stdout))) {
2190 :     fflush(stdout);
2191 :     setvbuf(stdout,NULL,_IONBF,0);
2192 : anton 1.1 }
2193 : pazsan 1.11 #endif
2194 : anton 1.1
2195 : pazsan 1.10 progname = argv[0];
2196 :    
2197 : pazsan 1.11 #ifdef HAS_OS
2198 : pazsan 1.10 gforth_args(argc, argv, &path, &imagename);
2199 : anton 1.109 #ifndef NO_DYNAMIC
2200 : anton 1.148 init_ss_cost();
2201 : anton 1.109 #endif /* !defined(NO_DYNAMIC) */
2202 :     #endif /* defined(HAS_OS) */
2203 : pazsan 1.10
2204 :     #ifdef INCLUDE_IMAGE
2205 :     set_stack_sizes((ImageHeader *)image);
2206 : pazsan 1.22 if(((ImageHeader *)image)->base != image)
2207 : pazsan 1.161 gforth_relocate(image, reloc_bits, ((ImageHeader *)image)->image_size,
2208 : pazsan 1.164 (Label*)gforth_engine(0, 0, 0, 0, 0));
2209 : pazsan 1.10 alloc_stacks((ImageHeader *)image);
2210 :     #else
2211 :     image_file = open_image_file(imagename, path);
2212 : pazsan 1.161 image = gforth_loader(image_file, imagename);
2213 : pazsan 1.10 #endif
2214 : anton 1.24 gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */
2215 : anton 1.1
2216 : pazsan 1.144 if (diag)
2217 :     print_diag();
2218 : anton 1.1 {
2219 : pazsan 1.10 char path2[strlen(path)+1];
2220 : anton 1.1 char *p1, *p2;
2221 :     Cell environ[]= {
2222 :     (Cell)argc-(optind-1),
2223 :     (Cell)(argv+(optind-1)),
2224 : pazsan 1.10 (Cell)strlen(path),
2225 : anton 1.1 (Cell)path2};
2226 :     argv[optind-1] = progname;
2227 :     /*
2228 :     for (i=0; i<environ[0]; i++)
2229 :     printf("%s\n", ((char **)(environ[1]))[i]);
2230 :     */
2231 :     /* make path OS-independent by replacing path separators with NUL */
2232 : pazsan 1.10 for (p1=path, p2=path2; *p1!='\0'; p1++, p2++)
2233 : anton 1.1 if (*p1==PATHSEP)
2234 :     *p2 = '\0';
2235 :     else
2236 :     *p2 = *p1;
2237 :     *p2='\0';
2238 : pazsan 1.161 retvalue = gforth_go(image, 4, environ);
2239 : anton 1.102 #ifdef SIGPIPE
2240 :     bsd_signal(SIGPIPE, SIG_IGN);
2241 :     #endif
2242 : anton 1.42 #ifdef VM_PROFILING
2243 :     vm_print_profile(stderr);
2244 :     #endif
2245 : anton 1.1 deprep_terminal();
2246 : anton 1.104 }
2247 : anton 1.110 if (print_metrics) {
2248 :     int i;
2249 :     fprintf(stderr, "code size = %8ld\n", dyncodesize());
2250 :     for (i=0; i<sizeof(cost_sums)/sizeof(cost_sums[0]); i++)
2251 :     fprintf(stderr, "metric %8s: %8ld\n",
2252 :     cost_sums[i].metricname, cost_sums[i].sum);
2253 : anton 1.158 fprintf(stderr,"lb_basic_blocks = %ld\n", lb_basic_blocks);
2254 :     fprintf(stderr,"lb_labeler_steps = %ld\n", lb_labeler_steps);
2255 :     fprintf(stderr,"lb_labeler_automaton = %ld\n", lb_labeler_automaton);
2256 :     fprintf(stderr,"lb_labeler_dynprog = %ld\n", lb_labeler_dynprog);
2257 :     fprintf(stderr,"lb_newstate_equiv = %ld\n", lb_newstate_equiv);
2258 :     fprintf(stderr,"lb_newstate_new = %ld\n", lb_newstate_new);
2259 :     fprintf(stderr,"lb_applicable_base_rules = %ld\n", lb_applicable_base_rules);
2260 :     fprintf(stderr,"lb_applicable_chain_rules = %ld\n", lb_applicable_chain_rules);
2261 :     }
2262 :     if (tpa_trace) {
2263 :     fprintf(stderr, "%ld %ld lb_states\n", lb_labeler_steps, lb_newstate_new);
2264 :     fprintf(stderr, "%ld %ld lb_table_entries\n", lb_labeler_steps, lb_labeler_dynprog);
2265 : anton 1.1 }
2266 : pazsan 1.13 return retvalue;
2267 : anton 1.1 }

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help