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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help