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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help