[gforth] / gforth / engine / main.c  

gforth: gforth/engine/main.c


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help