| /* command line interpretation, image loading etc. for Gforth |
/* command line interpretation, image loading etc. for Gforth |
| |
|
| |
|
| Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006 Free Software Foundation, Inc. |
Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007,2008 Free Software Foundation, Inc. |
| |
|
| This file is part of Gforth. |
This file is part of Gforth. |
| |
|
| Gforth is free software; you can redistribute it and/or |
Gforth is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
as published by the Free Software Foundation, either version 3 |
| of the License, or (at your option) any later version. |
of the License, or (at your option) any later version. |
| |
|
| This program is distributed in the hope that it will be useful, |
This program is distributed in the hope that it will be useful, |
| GNU General Public License for more details. |
GNU General Public License for more details. |
| |
|
| You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
| along with this program; if not, write to the Free Software |
along with this program; if not, see http://www.gnu.org/licenses/. |
| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
|
| */ |
*/ |
| |
|
| #include "config.h" |
#include "config.h" |
| #include <string.h> |
#include <string.h> |
| #include <math.h> |
#include <math.h> |
| #include <sys/types.h> |
#include <sys/types.h> |
| |
#ifdef HAVE_ALLOCA_H |
| |
#include <alloca.h> |
| |
#endif |
| #ifndef STANDALONE |
#ifndef STANDALONE |
| #include <sys/stat.h> |
#include <sys/stat.h> |
| #endif |
#endif |
| #include <assert.h> |
#include <assert.h> |
| #include <stdlib.h> |
#include <stdlib.h> |
| #include <signal.h> |
#include <signal.h> |
| |
|
| #ifndef STANDALONE |
#ifndef STANDALONE |
| #if HAVE_SYS_MMAN_H |
#if HAVE_SYS_MMAN_H |
| #include <sys/mman.h> |
#include <sys/mman.h> |
| /* #include <systypes.h> */ |
/* #include <systypes.h> */ |
| #endif |
#endif |
| |
|
| |
/* output rules etc. for burg with --debug and --print-sequences */ |
| |
/* #define BURG_FORMAT*/ |
| |
|
| typedef enum prim_num { |
typedef enum prim_num { |
| /* definitions of N_execute etc. */ |
/* definitions of N_execute etc. */ |
| #include PRIM_NUM_I |
#include PRIM_NUM_I |
| Cell *gforth_SP; |
Cell *gforth_SP; |
| Float *gforth_FP; |
Float *gforth_FP; |
| Address gforth_UP=NULL; |
Address gforth_UP=NULL; |
| |
|
| #ifdef HAS_FFCALL |
|
| Cell *gforth_RP; |
Cell *gforth_RP; |
| Address gforth_LP; |
Address gforth_LP; |
| |
|
| |
#ifndef HAS_LINKBACK |
| |
void * gforth_pointers[] = { |
| |
(void*)&gforth_SP, |
| |
(void*)&gforth_FP, |
| |
(void*)&gforth_LP, |
| |
(void*)&gforth_RP, |
| |
(void*)&gforth_UP, |
| |
(void*)gforth_engine, |
| |
(void*)cstr, |
| |
(void*)tilde_cstr }; |
| |
#endif |
| |
|
| |
#ifdef HAS_FFCALL |
| |
|
| #include <callback.h> |
#include <callback.h> |
| |
|
| va_alist gforth_clist; |
va_alist gforth_clist; |
| |
|
| gforth_clist = (va_alist)alist; |
gforth_clist = (va_alist)alist; |
| |
|
| gforth_engine(fcall, sp, rp, fp, lp); |
gforth_engine(fcall, sp, rp, fp, lp sr_call); |
| |
|
| /* restore global variables */ |
|
| gforth_RP = rp; |
|
| gforth_SP = sp; |
|
| gforth_FP = fp; |
|
| gforth_LP = lp; |
|
| gforth_clist = clist; |
|
| } |
|
| #endif |
|
| |
|
| #ifdef HAS_LIBFFI |
|
| Cell *gforth_RP; |
|
| Address gforth_LP; |
|
| |
|
| #include <ffi.h> |
|
| |
|
| void ** gforth_clist; |
|
| void * gforth_ritem; |
|
| |
|
| void gforth_callback(ffi_cif * cif, void * resp, void ** args, void * ip) |
|
| { |
|
| Cell *rp = gforth_RP; |
|
| Cell *sp = gforth_SP; |
|
| Float *fp = gforth_FP; |
|
| Address lp = gforth_LP; |
|
| void ** clist = gforth_clist; |
|
| void * ritem = gforth_ritem; |
|
| |
|
| gforth_clist = args; |
|
| gforth_ritem = resp; |
|
| |
|
| gforth_engine((Xt *)ip, sp, rp, fp, lp); |
|
| |
|
| /* restore global variables */ |
/* restore global variables */ |
| gforth_RP = rp; |
gforth_RP = rp; |
| gforth_FP = fp; |
gforth_FP = fp; |
| gforth_LP = lp; |
gforth_LP = lp; |
| gforth_clist = clist; |
gforth_clist = clist; |
| gforth_ritem = ritem; |
|
| } |
} |
| #endif |
#endif |
| |
|
| /* define some VM registers as global variables, so they survive exceptions; |
/* define some VM registers as global variables, so they survive exceptions; |
| global register variables are not up to the task (according to the |
global register variables are not up to the task (according to the |
| GNU C manual) */ |
GNU C manual) */ |
| |
#if defined(GLOBALS_NONRELOC) |
| |
saved_regs saved_regs_v; |
| |
saved_regs *saved_regs_p = &saved_regs_v; |
| |
#else /* !defined(GLOBALS_NONRELOC) */ |
| Xt *saved_ip; |
Xt *saved_ip; |
| Cell *rp; |
Cell *rp; |
| #endif |
#endif /* !defined(GLOBALS_NONRELOC) */ |
| |
#endif /* !defined(GFORTH_DEBUGGING) */ |
| |
|
| #ifdef NO_IP |
#ifdef NO_IP |
| Label next_code; |
Label next_code; |
| #define CODE_BLOCK_SIZE (512*1024) /* !! overflow handling for -native */ |
#define CODE_BLOCK_SIZE (512*1024) /* !! overflow handling for -native */ |
| Address code_area=0; |
Address code_area=0; |
| Cell code_area_size = CODE_BLOCK_SIZE; |
Cell code_area_size = CODE_BLOCK_SIZE; |
| Address code_here=NULL+CODE_BLOCK_SIZE; /* does for code-area what HERE |
Address code_here; /* does for code-area what HERE does for the dictionary */ |
| does for the dictionary */ |
|
| Address start_flush=NULL; /* start of unflushed code */ |
Address start_flush=NULL; /* start of unflushed code */ |
| Cell last_jump=0; /* if the last prim was compiled without jump, this |
Cell last_jump=0; /* if the last prim was compiled without jump, this |
| is it's number, otherwise this contains 0 */ |
is it's number, otherwise this contains 0 */ |
| static int no_dynamic=NO_DYNAMIC_DEFAULT; /* if true, no code is generated |
static int no_dynamic=NO_DYNAMIC_DEFAULT; /* if true, no code is generated |
| dynamically */ |
dynamically */ |
| static int print_metrics=0; /* if true, print metrics on exit */ |
static int print_metrics=0; /* if true, print metrics on exit */ |
| static int static_super_number = 0; /* number of ss used if available */ |
static int static_super_number = 10000; /* number of ss used if available */ |
| /* disabled because of tpa */ |
|
| #define MAX_STATE 9 /* maximum number of states */ |
#define MAX_STATE 9 /* maximum number of states */ |
| static int maxstates = MAX_STATE; /* number of states for stack caching */ |
static int maxstates = MAX_STATE; /* number of states for stack caching */ |
| static int ss_greedy = 0; /* if true: use greedy, not optimal ss selection */ |
static int ss_greedy = 0; /* if true: use greedy, not optimal ss selection */ |
| |
|
| #ifdef HAS_DEBUG |
#ifdef HAS_DEBUG |
| int debug=0; |
int debug=0; |
| # define debugp(x...) if (debug) fprintf(x); |
# define debugp(x...) do { if (debug) fprintf(x); } while (0) |
| #else |
#else |
| # define perror(x...) |
# define perror(x...) |
| # define fprintf(x...) |
# define fprintf(x...) |
| return r; |
return r; |
| } |
} |
| |
|
| static Address next_address=0; |
static void *next_address=0; |
| static void after_alloc(Address r, Cell size) |
static void after_alloc(Address r, Cell size) |
| { |
{ |
| if (r != (Address)-1) { |
if (r != (Address)-1) { |
| #ifndef MAP_PRIVATE |
#ifndef MAP_PRIVATE |
| # define MAP_PRIVATE 0 |
# define MAP_PRIVATE 0 |
| #endif |
#endif |
| |
#ifndef PROT_NONE |
| |
# define PROT_NONE 0 |
| |
#endif |
| #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS) |
#if !defined(MAP_ANON) && defined(MAP_ANONYMOUS) |
| # define MAP_ANON MAP_ANONYMOUS |
# define MAP_ANON MAP_ANONYMOUS |
| #endif |
#endif |
| #if defined(HAVE_MMAP) |
#if defined(HAVE_MMAP) |
| static Address alloc_mmap(Cell size) |
static Address alloc_mmap(Cell size) |
| { |
{ |
| Address r; |
void *r; |
| |
|
| #if defined(MAP_ANON) |
#if defined(MAP_ANON) |
| debugp(stderr,"try mmap($%lx, $%lx, ..., MAP_ANON, ...); ", (long)next_address, (long)size); |
debugp(stderr,"try mmap($%lx, $%lx, ..., MAP_ANON, ...); ", (long)next_address, (long)size); |
| return r; |
return r; |
| } |
} |
| |
|
| static void page_noaccess(Address a) |
static void page_noaccess(void *a) |
| { |
{ |
| /* try mprotect first; with munmap the page might be allocated later */ |
/* try mprotect first; with munmap the page might be allocated later */ |
| debugp(stderr, "try mprotect(%p,%ld,PROT_NONE); ", a, (long)pagesize); |
debugp(stderr, "try mprotect(%p,$%lx,PROT_NONE); ", a, (long)pagesize); |
| if (mprotect(a, pagesize, PROT_NONE)==0) { |
if (mprotect(a, pagesize, PROT_NONE)==0) { |
| debugp(stderr, "ok\n"); |
debugp(stderr, "ok\n"); |
| return; |
return; |
| } |
} |
| debugp(stderr, "failed: %s\n", strerror(errno)); |
debugp(stderr, "failed: %s\n", strerror(errno)); |
| debugp(stderr, "try munmap(%p,%ld); ", a, (long)pagesize); |
debugp(stderr, "try munmap(%p,$%lx); ", a, (long)pagesize); |
| if (munmap(a,pagesize)==0) { |
if (munmap(a,pagesize)==0) { |
| debugp(stderr, "ok\n"); |
debugp(stderr, "ok\n"); |
| return; |
return; |
| return verbose_malloc(size); |
return verbose_malloc(size); |
| } |
} |
| |
|
| static Address dict_alloc_read(FILE *file, Cell imagesize, Cell dictsize, Cell offset) |
static void *dict_alloc_read(FILE *file, Cell imagesize, Cell dictsize, Cell offset) |
| { |
{ |
| Address image = MAP_FAILED; |
void *image = MAP_FAILED; |
| |
|
| #if defined(HAVE_MMAP) |
#if defined(HAVE_MMAP) |
| if (offset==0) { |
if (offset==0) { |
| image=alloc_mmap(dictsize); |
image=alloc_mmap(dictsize); |
| if (image != (Address)MAP_FAILED) { |
if (image != (void *)MAP_FAILED) { |
| Address image1; |
void *image1; |
| debugp(stderr,"try mmap($%lx, $%lx, ..., MAP_FIXED|MAP_FILE, imagefile, 0); ", (long)image, (long)imagesize); |
debugp(stderr,"try mmap($%lx, $%lx, ..., MAP_FIXED|MAP_FILE, imagefile, 0); ", (long)image, (long)imagesize); |
| image1 = mmap(image, imagesize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FIXED|MAP_FILE|MAP_PRIVATE|map_noreserve, fileno(file), 0); |
image1 = mmap(image, imagesize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FIXED|MAP_FILE|MAP_PRIVATE|map_noreserve, fileno(file), 0); |
| after_alloc(image1,dictsize); |
after_alloc(image1,dictsize); |
| if (image1 == (Address)MAP_FAILED) |
if (image1 == (void *)MAP_FAILED) |
| goto read_image; |
goto read_image; |
| } |
} |
| } |
} |
| #endif /* defined(HAVE_MMAP) */ |
#endif /* defined(HAVE_MMAP) */ |
| if (image == (Address)MAP_FAILED) { |
if (image == (void *)MAP_FAILED) { |
| image = gforth_alloc(dictsize+offset)+offset; |
image = gforth_alloc(dictsize+offset)+offset; |
| read_image: |
read_image: |
| rewind(file); /* fseek(imagefile,0L,SEEK_SET); */ |
rewind(file); /* fseek(imagefile,0L,SEEK_SET); */ |
| size_t p = pagesize; |
size_t p = pagesize; |
| size_t totalsize = |
size_t totalsize = |
| wholepage(dsize)+wholepage(fsize)+wholepage(rsize)+wholepage(lsize)+5*p; |
wholepage(dsize)+wholepage(fsize)+wholepage(rsize)+wholepage(lsize)+5*p; |
| Address a = alloc_mmap(totalsize); |
void *a = alloc_mmap(totalsize); |
| if (a != (Address)MAP_FAILED) { |
if (a != (void *)MAP_FAILED) { |
| page_noaccess(a); a+=p; h-> data_stack_base=a; a+=wholepage(dsize); |
page_noaccess(a); a+=p; h-> data_stack_base=a; a+=wholepage(dsize); |
| page_noaccess(a); a+=p; h-> fp_stack_base=a; a+=wholepage(fsize); |
page_noaccess(a); a+=p; h-> fp_stack_base=a; a+=wholepage(fsize); |
| page_noaccess(a); a+=p; h->return_stack_base=a; a+=wholepage(rsize); |
page_noaccess(a); a+=p; h->return_stack_base=a; a+=wholepage(rsize); |
| #endif |
#endif |
| |
|
| #warning You can ignore the warnings about clobbered variables in gforth_go |
#warning You can ignore the warnings about clobbered variables in gforth_go |
| int gforth_go(Address image, int stack, Cell *entries) |
int gforth_go(void *image, int stack, Cell *entries) |
| { |
{ |
| volatile ImageHeader *image_header = (ImageHeader *)image; |
volatile ImageHeader *image_header = (ImageHeader *)image; |
| Cell *sp0=(Cell*)(image_header->data_stack_base + dsize); |
Cell *sp0=(Cell*)(image_header->data_stack_base + dsize); |
| /* fprintf(stderr, "rp=$%x\n",rp0);*/ |
/* fprintf(stderr, "rp=$%x\n",rp0);*/ |
| |
|
| return((int)(Cell)gforth_engine(image_header->throw_entry, signal_data_stack+15, |
return((int)(Cell)gforth_engine(image_header->throw_entry, signal_data_stack+15, |
| rp0, signal_fp_stack, 0)); |
rp0, signal_fp_stack, 0 sr_call)); |
| } |
} |
| #endif |
#endif |
| |
|
| return((int)(Cell)gforth_engine(ip0,sp0,rp0,fp0,lp0)); |
return((int)(Cell)gforth_engine(ip0,sp0,rp0,fp0,lp0 sr_call)); |
| } |
} |
| |
|
| #if !defined(INCLUDE_IMAGE) && !defined(STANDALONE) |
#if !defined(INCLUDE_IMAGE) && !defined(STANDALONE) |
| } |
} |
| } |
} |
| debugp(stderr, "Using %d static superinsts\n", nsupers); |
debugp(stderr, "Using %d static superinsts\n", nsupers); |
| |
if (nsupers>0 && !tpa_noautomaton && !tpa_noequiv) { |
| |
/* Currently these two things don't work together; see Section 3.2 |
| |
of <http://www.complang.tuwien.ac.at/papers/ertl+06pldi.ps.gz>, |
| |
in particular Footnote 6 for the reason; hmm, we should be able |
| |
to use an automaton without state equivalence, but that costs |
| |
significant space so we only do it if the user explicitly |
| |
disables state equivalence. */ |
| |
debugp(stderr, "Disabling tpa-automaton, because nsupers>0 and state equivalence is enabled.\n"); |
| |
tpa_noautomaton = 1; |
| |
} |
| } |
} |
| |
|
| /* dynamic replication/superinstruction stuff */ |
/* dynamic replication/superinstruction stuff */ |
| #ifndef NO_DYNAMIC |
#ifndef NO_DYNAMIC |
| if (no_dynamic) |
if (no_dynamic) |
| return; |
return; |
| symbols2=gforth_engine2(0,0,0,0,0); |
symbols2=gforth_engine2(0,0,0,0,0 sr_call); |
| #if NO_IP |
#if NO_IP |
| symbols3=gforth_engine3(0,0,0,0,0); |
symbols3=gforth_engine3(0,0,0,0,0 sr_call); |
| #else |
#else |
| symbols3=symbols1; |
symbols3=symbols1; |
| #endif |
#endif |
| /* check whether the "goto *" is relocatable */ |
/* check whether the "goto *" is relocatable */ |
| goto_len = goto_p[1]-goto_p[0]; |
goto_len = goto_p[1]-goto_p[0]; |
| debugp(stderr, "goto * %p %p len=%ld\n", |
debugp(stderr, "goto * %p %p len=%ld\n", |
| goto_p[0],symbols2[goto_p-symbols1],goto_len); |
goto_p[0],symbols2[goto_p-symbols1],(long)goto_len); |
| if (memcmp(goto_p[0],symbols2[goto_p-symbols1],goto_len)!=0) { /* unequal */ |
if (memcmp(goto_p[0],symbols2[goto_p-symbols1],goto_len)!=0) { /* unequal */ |
| no_dynamic=1; |
no_dynamic=1; |
| debugp(stderr," not relocatable, disabling dynamic code generation\n"); |
debugp(stderr," not relocatable, disabling dynamic code generation\n"); |
| pi->restlength = endlabel - symbols1[i] - pi->length; |
pi->restlength = endlabel - symbols1[i] - pi->length; |
| pi->nimmargs = 0; |
pi->nimmargs = 0; |
| relocs++; |
relocs++; |
| |
#if defined(BURG_FORMAT) |
| |
{ /* output as burg-style rules */ |
| |
int p=super_costs[i].offset; |
| |
if (p==N_noop) |
| |
debugp(stderr, "S%d: S%d = %d (%d);", sc->state_in, sc->state_out, i+1, pi->length); |
| |
else |
| |
debugp(stderr, "S%d: op%d(S%d) = %d (%d);", sc->state_in, p, sc->state_out, i+1, pi->length); |
| |
} |
| |
#else |
| debugp(stderr, "%-15s %d-%d %4d %p %p %p len=%3ld rest=%2ld send=%1d", |
debugp(stderr, "%-15s %d-%d %4d %p %p %p len=%3ld rest=%2ld send=%1d", |
| prim_names[i], sc->state_in, sc->state_out, |
prim_names[i], sc->state_in, sc->state_out, |
| i, s1, s2, s3, (long)(pi->length), (long)(pi->restlength), |
i, s1, s2, s3, (long)(pi->length), (long)(pi->restlength), |
| pi->superend); |
pi->superend); |
| |
#endif |
| if (endlabel == NULL) { |
if (endlabel == NULL) { |
| pi->start = NULL; /* not relocatable */ |
pi->start = NULL; /* not relocatable */ |
| if (pi->length<0) pi->length=100; |
if (pi->length<0) pi->length=100; |
| |
#ifndef BURG_FORMAT |
| debugp(stderr,"\n non_reloc: no J label > start found\n"); |
debugp(stderr,"\n non_reloc: no J label > start found\n"); |
| |
#endif |
| relocs--; |
relocs--; |
| nonrelocs++; |
nonrelocs++; |
| continue; |
continue; |
| if (ends1[i] > endlabel && !pi->superend) { |
if (ends1[i] > endlabel && !pi->superend) { |
| pi->start = NULL; /* not relocatable */ |
pi->start = NULL; /* not relocatable */ |
| pi->length = endlabel-symbols1[i]; |
pi->length = endlabel-symbols1[i]; |
| |
#ifndef BURG_FORMAT |
| debugp(stderr,"\n non_reloc: there is a J label before the K label (restlength<0)\n"); |
debugp(stderr,"\n non_reloc: there is a J label before the K label (restlength<0)\n"); |
| |
#endif |
| relocs--; |
relocs--; |
| nonrelocs++; |
nonrelocs++; |
| continue; |
continue; |
| if (ends1[i] < pi->start && !pi->superend) { |
if (ends1[i] < pi->start && !pi->superend) { |
| pi->start = NULL; /* not relocatable */ |
pi->start = NULL; /* not relocatable */ |
| pi->length = endlabel-symbols1[i]; |
pi->length = endlabel-symbols1[i]; |
| |
#ifndef BURG_FORMAT |
| debugp(stderr,"\n non_reloc: K label before I label (length<0)\n"); |
debugp(stderr,"\n non_reloc: K label before I label (length<0)\n"); |
| |
#endif |
| relocs--; |
relocs--; |
| nonrelocs++; |
nonrelocs++; |
| continue; |
continue; |
| if (s1[j]==s3[j]) { |
if (s1[j]==s3[j]) { |
| if (s1[j] != s2[j]) { |
if (s1[j] != s2[j]) { |
| pi->start = NULL; /* not relocatable */ |
pi->start = NULL; /* not relocatable */ |
| |
#ifndef BURG_FORMAT |
| debugp(stderr,"\n non_reloc: engine1!=engine2 offset %3d",j); |
debugp(stderr,"\n non_reloc: engine1!=engine2 offset %3d",j); |
| |
#endif |
| /* assert(j<prim_len); */ |
/* assert(j<prim_len); */ |
| relocs--; |
relocs--; |
| nonrelocs++; |
nonrelocs++; |
| debugp(stderr,"\n relative immarg: offset %3d",j); |
debugp(stderr,"\n relative immarg: offset %3d",j); |
| } else { |
} else { |
| pi->start = NULL; /* not relocatable */ |
pi->start = NULL; /* not relocatable */ |
| |
#ifndef BURG_FORMAT |
| debugp(stderr,"\n non_reloc: engine1!=engine3 offset %3d",j); |
debugp(stderr,"\n non_reloc: engine1!=engine3 offset %3d",j); |
| |
#endif |
| /* assert(j<prim_len);*/ |
/* assert(j<prim_len);*/ |
| relocs--; |
relocs--; |
| nonrelocs++; |
nonrelocs++; |
| { |
{ |
| #ifndef NO_DYNAMIC |
#ifndef NO_DYNAMIC |
| if (start_flush) |
if (start_flush) |
| FLUSH_ICACHE(start_flush, code_here-start_flush); |
FLUSH_ICACHE((caddr_t)start_flush, code_here-start_flush); |
| start_flush=code_here; |
start_flush=code_here; |
| #endif |
#endif |
| } |
} |
| |
|
| static void align_code(void) |
static void MAYBE_UNUSED align_code(void) |
| /* align code_here on some platforms */ |
/* align code_here on some platforms */ |
| { |
{ |
| #ifndef NO_DYNAMIC |
#ifndef NO_DYNAMIC |
| Cell size; |
Cell size; |
| } *code_block_list=NULL, **next_code_blockp=&code_block_list; |
} *code_block_list=NULL, **next_code_blockp=&code_block_list; |
| |
|
| static Address append_prim(Cell p) |
static void reserve_code_space(UCell size) |
| { |
{ |
| PrimInfo *pi = &priminfos[p]; |
if (code_area+code_area_size < code_here+size) { |
| Address old_code_here = code_here; |
|
| |
|
| if (code_area+code_area_size < code_here+pi->length+pi->restlength+goto_len+CODE_ALIGNMENT) { |
|
| struct code_block_list *p; |
struct code_block_list *p; |
| append_jump(); |
append_jump(); |
| flush_to_here(); |
flush_to_here(); |
| p = *next_code_blockp; |
p = *next_code_blockp; |
| code_here = start_flush = code_area = p->block; |
code_here = start_flush = code_area = p->block; |
| } |
} |
| old_code_here = code_here; |
|
| next_code_blockp = &(p->next); |
next_code_blockp = &(p->next); |
| } |
} |
| |
} |
| |
|
| |
static Address append_prim(Cell p) |
| |
{ |
| |
PrimInfo *pi = &priminfos[p]; |
| |
Address old_code_here; |
| |
reserve_code_space(pi->length+pi->restlength+goto_len+CODE_ALIGNMENT-1); |
| memcpy(code_here, pi->start, pi->length); |
memcpy(code_here, pi->start, pi->length); |
| |
old_code_here = code_here; |
| code_here += pi->length; |
code_here += pi->length; |
| return old_code_here; |
return old_code_here; |
| } |
} |
| |
|
| |
static void reserve_code_super(PrimNum origs[], int ninsts) |
| |
{ |
| |
int i; |
| |
UCell size = CODE_ALIGNMENT-1; /* alignment may happen first */ |
| |
if (no_dynamic) |
| |
return; |
| |
/* use size of the original primitives as an upper bound for the |
| |
size of the superinstruction. !! This is only safe if we |
| |
optimize for code size (the default) */ |
| |
for (i=0; i<ninsts; i++) { |
| |
PrimNum p = origs[i]; |
| |
PrimInfo *pi = &priminfos[p]; |
| |
if (is_relocatable(p)) |
| |
size += pi->length; |
| |
else |
| |
if (i>0) |
| |
size += priminfos[origs[i-1]].restlength+goto_len+CODE_ALIGNMENT-1; |
| |
} |
| |
size += priminfos[origs[i-1]].restlength+goto_len; |
| |
reserve_code_space(size); |
| |
} |
| #endif |
#endif |
| |
|
| int forget_dyncode(Address code) |
int forget_dyncode(Address code) |
| static void tpa_state_normalize(struct tpa_state *t) |
static void tpa_state_normalize(struct tpa_state *t) |
| { |
{ |
| /* normalize so cost of canonical state=0; this may result in |
/* normalize so cost of canonical state=0; this may result in |
| negative states for some states */ |
negative costs for some states */ |
| int d = t->inst[CANONICAL_STATE].cost; |
int d = t->inst[CANONICAL_STATE].cost; |
| int i; |
int i; |
| |
|
| int i,j; |
int i,j; |
| struct tpa_state *ts[ninsts+1]; |
struct tpa_state *ts[ninsts+1]; |
| int nextdyn, nextstate, no_transition; |
int nextdyn, nextstate, no_transition; |
| |
Address old_code_area; |
| |
|
| lb_basic_blocks++; |
lb_basic_blocks++; |
| ts[ninsts] = termstate; |
ts[ninsts] = termstate; |
| #ifndef NO_DYNAMIC |
#ifndef NO_DYNAMIC |
| if (print_sequences) { |
if (print_sequences) { |
| for (i=0; i<ninsts; i++) |
for (i=0; i<ninsts; i++) |
| |
#if defined(BURG_FORMAT) |
| |
fprintf(stderr, "op%d ", super_costs[origs[i]].offset); |
| |
#else |
| fprintf(stderr, "%s ", prim_names[origs[i]]); |
fprintf(stderr, "%s ", prim_names[origs[i]]); |
| |
#endif |
| fprintf(stderr, "\n"); |
fprintf(stderr, "\n"); |
| } |
} |
| #endif |
#endif |
| } |
} |
| } |
} |
| /* now rewrite the instructions */ |
/* now rewrite the instructions */ |
| |
reserve_code_super(origs,ninsts); |
| |
old_code_area = code_area; |
| nextdyn=0; |
nextdyn=0; |
| nextstate=CANONICAL_STATE; |
nextstate=CANONICAL_STATE; |
| no_transition = ((!ts[0]->trans[nextstate].relocatable) |
no_transition = ((!ts[0]->trans[nextstate].relocatable) |
| nextstate = c->state_out; |
nextstate = c->state_out; |
| } |
} |
| assert(nextstate==CANONICAL_STATE); |
assert(nextstate==CANONICAL_STATE); |
| |
assert(code_area==old_code_area); /* does reserve_code_super() work? */ |
| } |
} |
| #endif |
#endif |
| |
|
| #endif |
#endif |
| ; |
; |
| |
|
| vm_prims = gforth_engine(0,0,0,0,0); |
vm_prims = gforth_engine(0,0,0,0,0 sr_call); |
| check_prims(vm_prims); |
check_prims(vm_prims); |
| prepare_super_table(); |
prepare_super_table(); |
| #ifndef DOUBLY_INDIRECT |
#ifndef DOUBLY_INDIRECT |
| debugp(stderr,"pagesize=%ld\n",(unsigned long) pagesize); |
debugp(stderr,"pagesize=%ld\n",(unsigned long) pagesize); |
| |
|
| image = dict_alloc_read(imagefile, preamblesize+header.image_size, |
image = dict_alloc_read(imagefile, preamblesize+header.image_size, |
| preamblesize+dictsize, data_offset); |
dictsize, data_offset); |
| imp=image+preamblesize; |
imp=image+preamblesize; |
| |
|
| alloc_stacks((ImageHeader *)imp); |
alloc_stacks((ImageHeader *)imp); |
| {"offset-image", no_argument, &offset_image, 1}, |
{"offset-image", no_argument, &offset_image, 1}, |
| {"no-offset-im", no_argument, &offset_image, 0}, |
{"no-offset-im", no_argument, &offset_image, 0}, |
| {"clear-dictionary", no_argument, &clear_dictionary, 1}, |
{"clear-dictionary", no_argument, &clear_dictionary, 1}, |
| {"die-on-signal", no_argument, &die_on_signal, 1}, |
|
| {"ignore-async-signals", no_argument, &ignore_async_signals, 1}, |
|
| {"debug", no_argument, &debug, 1}, |
{"debug", no_argument, &debug, 1}, |
| {"diag", no_argument, &diag, 1}, |
{"diag", no_argument, &diag, 1}, |
| |
{"die-on-signal", no_argument, &die_on_signal, 1}, |
| |
{"ignore-async-signals", no_argument, &ignore_async_signals, 1}, |
| {"no-super", no_argument, &no_super, 1}, |
{"no-super", no_argument, &no_super, 1}, |
| {"no-dynamic", no_argument, &no_dynamic, 1}, |
{"no-dynamic", no_argument, &no_dynamic, 1}, |
| {"dynamic", no_argument, &no_dynamic, 0}, |
{"dynamic", no_argument, &no_dynamic, 0}, |
| static void print_diag() |
static void print_diag() |
| { |
{ |
| |
|
| #if !defined(HAVE_GETRUSAGE) || (!defined(HAS_FFCALL) && !defined(HAS_LIBFFI)) |
#if !defined(HAVE_GETRUSAGE) |
| fprintf(stderr, "*** missing functionality ***\n" |
fprintf(stderr, "*** missing functionality ***\n" |
| #ifndef HAVE_GETRUSAGE |
#ifndef HAVE_GETRUSAGE |
| " no getrusage -> CPUTIME broken\n" |
" no getrusage -> CPUTIME broken\n" |
| #endif |
#endif |
| #if !defined(HAS_FFCALL) && !defined(HAS_LIBFFI) |
|
| " no ffcall -> only old-style foreign function calls (no fflib.fs)\n" |
|
| #endif |
|
| ); |
); |
| #endif |
#endif |
| if((relocs < nonrelocs) || |
if((relocs < nonrelocs) || |
| #endif |
#endif |
| ) |
) |
| debugp(stderr, "relocs: %d:%d\n", relocs, nonrelocs); |
debugp(stderr, "relocs: %d:%d\n", relocs, nonrelocs); |
| fprintf(stderr, "*** %sperformance problems ***\n%s", |
fprintf(stderr, "*** %sperformance problems ***\n%s%s", |
| #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(BUGGY_LONG_LONG) |
#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) |
| "", |
"", |
| #else |
#else |
| "no ", |
"no ", |
| #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) |
#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) |
| " double-cell integer type buggy ->\n " |
" double-cell integer type buggy ->\n " |
| #ifdef BUGGY_LL_CMP |
#ifdef BUGGY_LL_CMP |
| "CMP, " |
"double comparisons, " |
| #endif |
#endif |
| #ifdef BUGGY_LL_MUL |
#ifdef BUGGY_LL_MUL |
| "MUL, " |
"*/MOD */ M* UM* " |
| #endif |
#endif |
| #ifdef BUGGY_LL_DIV |
#ifdef BUGGY_LL_DIV |
| "DIV, " |
/* currently nothing is affected */ |
| #endif |
#endif |
| #ifdef BUGGY_LL_ADD |
#ifdef BUGGY_LL_ADD |
| "ADD, " |
"M+ D+ D- DNEGATE " |
| #endif |
#endif |
| #ifdef BUGGY_LL_SHIFT |
#ifdef BUGGY_LL_SHIFT |
| "SHIFT, " |
"D2/ " |
| #endif |
#endif |
| #ifdef BUGGY_LL_D2F |
#ifdef BUGGY_LL_D2F |
| "D2F, " |
"D>F " |
| #endif |
#endif |
| #ifdef BUGGY_LL_F2D |
#ifdef BUGGY_LL_F2D |
| "F2D, " |
"F>D " |
| #endif |
#endif |
| "\b\b slow\n" |
"\b\b slow\n" |
| #endif |
#endif |
| #ifndef FORCE_REG |
#if !(defined(FORCE_REG) || defined(FORCE_REG_UNNECESSARY)) |
| " automatic register allocation: performance degradation possible\n" |
" automatic register allocation: performance degradation possible\n" |
| #endif |
#endif |
| #if !defined(FORCE_REG) || defined(BUGGY_LONG_LONG) |
"", |
| "*** Suggested remedy: try ./configure" |
(relocs < nonrelocs) ? "no dynamic code generation (--debug for details) -> factor 2 slowdown\n" : ""); |
| #ifndef FORCE_REG |
|
| " --enable-force-reg" |
|
| #endif |
|
| #ifdef BUGGY_LONG_LONG |
|
| " --enable-force-ll" |
|
| #endif |
|
| "\n" |
|
| #else |
|
| "" |
|
| #endif |
|
| , |
|
| (relocs < nonrelocs) ? " gcc PR 15242 -> no dynamic code generation (use gcc-2.95 instead)\n" : ""); |
|
| } |
} |
| |
|
| #ifdef STANDALONE |
#ifdef STANDALONE |
| Address image; |
Address image; |
| #endif |
#endif |
| int retvalue; |
int retvalue; |
| |
#if 0 && defined(__i386) |
| #if defined(i386) && defined(ALIGNMENT_CHECK) |
/* disabled because the drawbacks may be worse than the benefits */ |
| /* turn on alignment checks on the 486. |
/* set 387 precision control to use 53-bit mantissae to avoid most |
| * on the 386 this should have no effect. */ |
cases of double rounding */ |
| __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;"); |
short fpu_control = 0x027f ; |
| /* this is unusable with Linux' libc.4.6.27, because this library is |
asm("fldcw %0" : : "m"(fpu_control)); |
| not alignment-clean; we would have to replace some library |
#endif /* defined(__i386) */ |
| functions (e.g., memcpy) to make it work. Also GCC doesn't try to keep |
|
| the stack FP-aligned. */ |
code_here = ((void *)0)+CODE_BLOCK_SIZE; /* llvm-gcc does not like this as |
| |
initializer, so we do it here */ |
| |
#ifdef MACOSX_DEPLOYMENT_TARGET |
| |
setenv("MACOSX_DEPLOYMENT_TARGET", MACOSX_DEPLOYMENT_TARGET, 0); |
| |
#endif |
| |
#ifdef LTDL_LIBRARY_PATH |
| |
setenv("LTDL_LIBRARY_PATH", LTDL_LIBRARY_PATH, 0); |
| #endif |
#endif |
| |
|
| #ifndef STANDALONE |
#ifndef STANDALONE |
| /* buffering of the user output device */ |
/* buffering of the user output device */ |
| #ifdef _IONBF |
#ifdef _IONBF |
| progname = argv[0]; |
progname = argv[0]; |
| |
|
| #ifndef STANDALONE |
#ifndef STANDALONE |
| |
#ifdef HAVE_LIBLTDL |
| |
if (lt_dlinit()!=0) { |
| |
fprintf(stderr,"%s: lt_dlinit failed", progname); |
| |
exit(1); |
| |
} |
| |
#endif |
| |
|
| #ifdef HAS_OS |
#ifdef HAS_OS |
| gforth_args(argc, argv, &path, &imagename); |
gforth_args(argc, argv, &path, &imagename); |
| #ifndef NO_DYNAMIC |
#ifndef NO_DYNAMIC |
| #endif |
#endif |
| |
|
| #ifdef STANDALONE |
#ifdef STANDALONE |
| image = gforth_engine(0, 0, 0, 0, 0); |
image = gforth_engine(0, 0, 0, 0, 0 sr_call); |
| alloc_stacks((ImageHeader *)image); |
alloc_stacks((ImageHeader *)image); |
| #else |
#else |
| image_file = open_image_file(imagename, path); |
image_file = open_image_file(imagename, path); |
| vm_print_profile(stderr); |
vm_print_profile(stderr); |
| #endif |
#endif |
| deprep_terminal(); |
deprep_terminal(); |
| |
#ifndef STANDALONE |
| |
#ifdef HAVE_LIBLTDL |
| |
if (lt_dlexit()!=0) |
| |
fprintf(stderr,"%s: lt_dlexit failed", progname); |
| |
#endif |
| |
#endif |
| } |
} |
| if (print_metrics) { |
if (print_metrics) { |
| int i; |
int i; |