[gforth] / gforth / engine / main.c  

gforth: gforth/engine/main.c


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help