Annotation of gforth/engine/main.c, revision 1.212

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>