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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help