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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help