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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help