1: /* common header file
2:
3: Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
4:
5: This file is part of Gforth.
6:
7: Gforth is free software; you can redistribute it and/or
8: modify it under the terms of the GNU General Public License
9: as published by the Free Software Foundation, either version 3
10: of the License, or (at your option) any later version.
11:
12: This program is distributed in the hope that it will be useful,
13: but WITHOUT ANY WARRANTY; without even the implied warranty of
14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: GNU General Public License for more details.
16:
17: You should have received a copy of the GNU General Public License
18: along with this program; if not, see http://www.gnu.org/licenses/.
19: */
20:
21: #include "config.h"
22: #include "128bit.h"
23: #include <stdio.h>
24: #include <sys/time.h>
25: #include <unistd.h>
26: #ifndef STANDALONE
27: #if defined(HAVE_LIBLTDL)
28: #include <ltdl.h>
29: #endif
30: #endif
31:
32: #if !defined(FORCE_LL) && !defined(BUGGY_LONG_LONG)
33: #define BUGGY_LONG_LONG
34: #endif
35:
36: #if defined(DOUBLY_INDIRECT)||defined(INDIRECT_THREADED)||defined(VM_PROFILING)
37: #define NO_DYNAMIC
38: #endif
39:
40: #if defined(DOUBLY_INDIRECT)
41: # undef DIRECT_THREADED
42: # undef INDIRECT_THREADED
43: # define INDIRECT_THREADED
44: #endif
45:
46: #if defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING)
47: # undef USE_TOS
48: # undef USE_FTOS
49: # undef USE_NO_TOS
50: # undef USE_NO_FTOS
51: # define USE_NO_TOS
52: # define USE_NO_FTOS
53:
54: #define PRIM_I "prim.i"
55: #define PRIM_LAB_I "prim_lab.i"
56: #define PRIM_NAMES_I "prim_names.i"
57: #define PRIM_SUPEREND_I "prim_superend.i"
58: #define PRIM_NUM_I "prim_num.i"
59: #define PRIM_GRP_I "prim_grp.i"
60: #define COSTS_I "costs.i"
61: #define SUPER2_I "super2.i"
62: /* #define PROFILE_I "profile.i" */
63:
64: #else
65: /* gforth-fast or gforth-native */
66: # undef USE_TOS
67: # undef USE_FTOS
68: # undef USE_NO_TOS
69: # undef USE_NO_FTOS
70: # define USE_TOS
71:
72: #define PRIM_I "prim-fast.i"
73: #define PRIM_LAB_I "prim_lab-fast.i"
74: #define PRIM_NAMES_I "prim_names-fast.i"
75: #define PRIM_SUPEREND_I "prim_superend-fast.i"
76: #define PRIM_NUM_I "prim_num-fast.i"
77: #define PRIM_GRP_I "prim_grp-fast.i"
78: #define COSTS_I "costs-fast.i"
79: #define SUPER2_I "super2-fast.i"
80: /* profile.c uses profile.i but does not define VM_PROFILING */
81: /* #define PROFILE_I "profile-fast.i" */
82:
83: #endif
84:
85:
86:
87: #include <limits.h>
88:
89: #if defined(NeXT)
90: # include <libc.h>
91: #endif /* NeXT */
92:
93: /* symbol indexed constants */
94:
95: #define DOCOL 0
96: #define DOCON 1
97: #define DOVAR 2
98: #define DOUSER 3
99: #define DODEFER 4
100: #define DOFIELD 5
101: #define DOVAL 6
102: #define DODOES 7
103: #define DOABICODE 8
104: #define DOSEMIABICODE 9
105: #define DOER_MAX 9
106:
107: #include "machine.h"
108:
109: /* C interface data types */
110:
111: typedef WYDE_TYPE Wyde;
112: typedef TETRABYTE_TYPE Tetrabyte;
113: typedef unsigned WYDE_TYPE UWyde;
114: typedef unsigned TETRABYTE_TYPE UTetrabyte;
115:
116: /* Forth data types */
117: /* Cell and UCell must be the same size as a pointer */
118: #define CELL_BITS (sizeof(Cell) * CHAR_BIT)
119: #define CELL_MIN (((Cell)1)<<(sizeof(Cell)*CHAR_BIT-1))
120:
121: #define HALFCELL_BITS (CELL_BITS/2)
122: #define HALFCELL_MASK ((~(UCell)0)>>HALFCELL_BITS)
123: #define UH(x) (((UCell)(x))>>HALFCELL_BITS)
124: #define LH(x) ((x)&HALFCELL_MASK)
125: #define L2U(x) (((UCell)(x))<<HALFCELL_BITS)
126: #define HIGHBIT(x) (((UCell)(x))>>(CELL_BITS-1))
127:
128: #define FLAG(b) (-(b))
129: #define FILEIO(error) (FLAG(error) & -37)
130: #define FILEEXIST(error) (FLAG(error) & -38)
131:
132: #define F_TRUE (FLAG(0==0))
133: #define F_FALSE (FLAG(0!=0))
134:
135: /* define this false if you want native division */
136: #ifdef FORCE_CDIV
137: #define FLOORED_DIV 0
138: #else
139: #define FLOORED_DIV ((1%-3)>0)
140: #endif
141:
142: #if defined(BUGGY_LONG_LONG)
143:
144: #define BUGGY_LL_CMP /* compares not possible */
145: #define BUGGY_LL_MUL /* multiplication not possible */
146: #define BUGGY_LL_DIV /* division not possible */
147: #define BUGGY_LL_ADD /* addition not possible */
148: #define BUGGY_LL_SHIFT /* shift not possible */
149: #define BUGGY_LL_D2F /* to float not possible */
150: #define BUGGY_LL_F2D /* from float not possible */
151: #define BUGGY_LL_SIZE /* long long "too short", so we use something else */
152:
153: typedef struct {
154: Cell hi;
155: UCell lo;
156: } DCell;
157:
158: typedef struct {
159: UCell hi;
160: UCell lo;
161: } UDCell;
162:
163: #define DHI(x) (x).hi
164: #define DLO(x) (x).lo
165: #define DHI_IS(x,y) (x).hi=(y)
166: #define DLO_IS(x,y) (x).lo=(y)
167:
168: #define UD2D(ud) ({UDCell _ud=(ud); (DCell){_ud.hi,_ud.lo};})
169: #define D2UD(d) ({DCell _d1=(d); (UDCell){_d1.hi,_d1.lo};})
170:
171: /* shifts by less than CELL_BITS */
172: #define DLSHIFT(d,u) ({DCell _d=(d); UCell _u=(u); \
173: ((_u==0) ? \
174: _d : \
175: (DCell){(_d.hi<<_u)|(_d.lo>>(CELL_BITS-_u)), \
176: _d.lo<<_u});})
177:
178: #define UDLSHIFT(ud,u) D2UD(DLSHIFT(UD2D(ud),u))
179:
180: #if SMALL_OFF_T
181: #define OFF2UD(o) ({UDCell _ud; _ud.hi=0; _ud.lo=(Cell)(o); _ud;})
182: #define UD2OFF(ud) ((ud).lo)
183: #else /* !SMALL_OFF_T */
184: #define OFF2UD(o) ({UDCell _ud; off_t _o=(o); _ud.hi=_o>>CELL_BITS; _ud.lo=(Cell)_o; _ud;})
185: #define UD2OFF(ud) ({UDCell _ud=(ud); (((off_t)_ud.hi)<<CELL_BITS)+_ud.lo;})
186: #endif /* !SMALL_OFF_T */
187: #define DZERO ((DCell){0,0})
188:
189: #else /* !defined(BUGGY_LONG_LONG) */
190:
191: /* DCell and UDCell must be twice as large as Cell */
192: typedef DOUBLE_CELL_TYPE DCell;
193: typedef DOUBLE_UCELL_TYPE UDCell;
194:
195: #define DHI(x) ({ Double_Store _d; _d.d=(x); _d.cells.high; })
196: #define DLO(x) ({ Double_Store _d; _d.d=(x); _d.cells.low; })
197:
198: /* beware with the assignment: x is referenced twice! */
199: #define DHI_IS(x,y) ({ Double_Store _d; _d.d=(x); _d.cells.high=(y); (x)=_d.d; })
200: #define DLO_IS(x,y) ({ Double_Store _d; _d.d=(x); _d.cells.low =(y); (x)=_d.d; })
201:
202: #define UD2D(ud) ((DCell)(ud))
203: #define D2UD(d) ((UDCell)(d))
204: #define OFF2UD(o) ((UDCell)(o))
205: #define UD2OFF(ud) ((off_t)(ud))
206: #define DZERO ((DCell)0)
207: /* shifts by less than CELL_BITS */
208: #define DLSHIFT(d,u) ((d)<<(u))
209: #define UDLSHIFT(d,u) ((d)<<(u))
210:
211: #endif /* !defined(BUGGY_LONG_LONG) */
212:
213: typedef union {
214: struct {
215: #if defined(WORDS_BIGENDIAN)||defined(BUGGY_LONG_LONG)
216: Cell high;
217: UCell low;
218: #else
219: UCell low;
220: Cell high;
221: #endif
222: } cells;
223: DCell d;
224: UDCell ud;
225: } Double_Store;
226:
227: #define FETCH_DCELL_T(d_,lo,hi,t_) ({ \
228: Double_Store _d; \
229: _d.cells.low = (lo); \
230: _d.cells.high = (hi); \
231: (d_) = _d.t_; \
232: })
233:
234: #define STORE_DCELL_T(d_,lo,hi,t_) ({ \
235: Double_Store _d; \
236: _d.t_ = (d_); \
237: (lo) = _d.cells.low; \
238: (hi) = _d.cells.high; \
239: })
240:
241: #define vm_twoCell2d(lo,hi,d_) FETCH_DCELL_T(d_,lo,hi,d);
242: #define vm_twoCell2ud(lo,hi,d_) FETCH_DCELL_T(d_,lo,hi,ud);
243:
244: #define vm_d2twoCell(d_,lo,hi) STORE_DCELL_T(d_,lo,hi,d);
245: #define vm_ud2twoCell(d_,lo,hi) STORE_DCELL_T(d_,lo,hi,ud);
246:
247: typedef Label *Xt;
248:
249: /* PFA gives the parameter field address corresponding to a cfa */
250: #define PFA(cfa) (((Cell *)cfa)+2)
251: /* PFA1 is a special version for use just after a NEXT1 */
252: #define PFA1(cfa) PFA(cfa)
253: /* CODE_ADDRESS is the address of the code jumped to through the code field */
254: #define CODE_ADDRESS(cfa) (*(Xt)(cfa))
255:
256: /* DOES_CODE is the Forth code does jumps to */
257: #if !defined(DOUBLY_INDIRECT)
258: # define DOES_CA (symbols[DODOES])
259: #else /* defined(DOUBLY_INDIRECT) */
260: # define DOES_CA ((Label)&xts[DODOES])
261: #endif /* defined(DOUBLY_INDIRECT) */
262:
263:
264:
265: #define DOES_CODE1(cfa) ((Xt *)(cfa[1]))
266: /* MAKE_CF creates an appropriate code field at the cfa;
267: ca is the code address */
268: #define MAKE_CF(cfa,ca) ((*(Label *)(cfa)) = ((Label)ca))
269: /* make a code field for a defining-word-defined word */
270:
271: #define CF(const) (-const-2)
272:
273: #define CF_NIL -1
274:
275: #ifndef FLUSH_ICACHE
276: #warning flush-icache probably will not work (see manual)
277: # define FLUSH_ICACHE(addr,size)
278: #warning no FLUSH_ICACHE, turning off dynamic native code by default
279: #undef NO_DYNAMIC_DEFAULT
280: #define NO_DYNAMIC_DEFAULT 1
281: #endif
282:
283: #if defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING)
284: #define STACK_CACHE_DEFAULT 0
285: #else
286: #define STACK_CACHE_DEFAULT STACK_CACHE_DEFAULT_FAST
287: #endif
288:
289: #ifdef USE_FTOS
290: #define IF_fpTOS(x) x
291: #else
292: #define IF_fpTOS(x)
293: #define fpTOS (fp[0])
294: #endif
295:
296: #define IF_rpTOS(x)
297: #define rpTOS (rp[0])
298:
299: typedef struct {
300: Address base; /* base address of image (0 if relocatable) */
301: UCell checksum; /* checksum of ca's to protect against some
302: incompatible binary/executable combinations
303: (0 if relocatable) */
304: UCell image_size; /* all sizes in bytes */
305: UCell dict_size;
306: UCell data_stack_size;
307: UCell fp_stack_size;
308: UCell return_stack_size;
309: UCell locals_stack_size;
310: Xt *boot_entry; /* initial ip for booting (in BOOT) */
311: Xt *throw_entry; /* ip after signal (in THROW) */
312: Cell unused1; /* possibly tib stack size */
313: Label *xt_base; /* base of DOUBLE_INDIRECT xts[], for comp-i.fs */
314: Address data_stack_base; /* this and the following fields are initialized by the loader */
315: Address fp_stack_base;
316: Address return_stack_base;
317: Address locals_stack_base;
318: } ImageHeader;
319: /* the image-header is created in main.fs */
320:
321: #ifdef HAS_F83HEADERSTRING
322: struct F83Name {
323: struct F83Name *next; /* the link field for old hands */
324: char countetc;
325: char name[0];
326: };
327:
328: #define F83NAME_COUNT(np) ((np)->countetc & 0x1f)
329: #endif
330: struct Longname {
331: struct Longname *next; /* the link field for old hands */
332: Cell countetc;
333: char name[0];
334: };
335:
336: #define LONGNAME_COUNT(np) ((np)->countetc & (((~((UCell)0))<<4)>>4))
337:
338: struct Cellpair {
339: Cell n1;
340: Cell n2;
341: };
342:
343: struct Cellquad {
344: Cell n1;
345: Cell n2;
346: Cell n3;
347: Cell n4;
348: };
349:
350: #define IOR(flag) ((flag)? -512-errno : 0)
351:
352: #ifdef GFORTH_DEBUGGING
353: #if defined(GLOBALS_NONRELOC)
354: /* if globals cause non-relocatable primitives, keep saved_ip and rp
355: in a structure and access it through locals */
356: typedef struct saved_regs {
357: Xt *sr_saved_ip;
358: Cell *sr_rp;
359: } saved_regs;
360: extern saved_regs saved_regs_v, *saved_regs_p;
361: #define saved_ip (saved_regs_p->sr_saved_ip)
362: #define rp (saved_regs_p->sr_rp)
363: /* for use in gforth_engine header */
364: #error sr_proto not passed in fflib.fs callbacks (solution: disable GLOBALS_NONRELOC)
365: #define sr_proto , struct saved_regs *saved_regs_p0
366: #define sr_call , saved_regs_p
367: #else /* !defined(GLOBALS_NONRELOC) */
368: extern Xt *saved_ip;
369: extern Cell *rp;
370: #define sr_proto
371: #define sr_call
372: #endif /* !defined(GLOBALS_NONRELOC) */
373: #else /* !defined(GFORTH_DEBUGGING) */
374: #define sr_proto
375: #define sr_call
376: #endif /* !defined(GFORTH_DEBUGGING) */
377:
378: Label *gforth_engine(Xt *ip, Cell *sp, Cell *rp0, Float *fp, Address lp sr_proto);
379: Label *gforth_engine2(Xt *ip, Cell *sp, Cell *rp0, Float *fp, Address lp sr_proto);
380: Label *gforth_engine3(Xt *ip, Cell *sp, Cell *rp0, Float *fp, Address lp sr_proto);
381:
382: /* for ABI-CODE and ;ABI-CODE */
383: typedef Cell *abifunc(Cell *sp, Float **fpp);
384: typedef Cell *semiabifunc(Cell *sp, Float **fpp, Address body);
385:
386: /* engine/prim support routines */
387: Address gforth_alloc(Cell size);
388: char *cstr(Char *from, UCell size, int clear);
389: char *tilde_cstr(Char *from, UCell size, int clear);
390: Cell opencreate_file(char *s, Cell wfam, int flags, Cell *wiorp);
391: DCell timeval2us(struct timeval *tvp);
392: void cmove(Char *c_from, Char *c_to, UCell u);
393: void cmove_up(Char *c_from, Char *c_to, UCell u);
394: Cell compare(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
395: struct Longname *listlfind(Char *c_addr, UCell u, struct Longname *longname1);
396: struct Longname *hashlfind(Char *c_addr, UCell u, Cell *a_addr);
397: struct Longname *tablelfind(Char *c_addr, UCell u, Cell *a_addr);
398: UCell hashkey1(Char *c_addr, UCell u, UCell ubits);
399: struct Cellpair parse_white(Char *c_addr1, UCell u1);
400: Cell rename_file(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
401: struct Cellquad read_line(Char *c_addr, UCell u1, FILE *wfileid);
402: struct Cellpair file_status(Char *c_addr, UCell u);
403: Cell to_float(Char *c_addr, UCell u, Float *r_p);
404: Float v_star(Float *f_addr1, Cell nstride1, Float *f_addr2, Cell nstride2, UCell ucount);
405: void faxpy(Float ra, Float *f_x, Cell nstridex, Float *f_y, Cell nstridey, UCell ucount);
406: UCell lshift(UCell u1, UCell n);
407: UCell rshift(UCell u1, UCell n);
408: int gforth_system(Char *c_addr, UCell u);
409: void gforth_ms(UCell u);
410: UCell gforth_dlopen(Char *c_addr, UCell u);
411: Cell capscompare(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
412: int gf_ungetc(int c, FILE *stream);
413: void gf_regetc(FILE *stream);
414: int gf_ungottenc(FILE *stream);
415:
416: /* signal handler stuff */
417: void install_signal_handlers(void);
418: void throw(int code);
419: /* throw codes */
420: #define BALL_DIVZERO -10
421: #define BALL_RESULTRANGE -11
422:
423: typedef void Sigfunc(int);
424: Sigfunc *bsd_signal(int signo, Sigfunc *func);
425:
426: /* dblsub routines */
427: DCell dnegate(DCell d1);
428: UDCell ummul (UCell a, UCell b);
429: DCell mmul (Cell a, Cell b);
430: UDCell umdiv (UDCell u, UCell v);
431: DCell smdiv (DCell num, Cell denom);
432: DCell fmdiv (DCell num, Cell denom);
433:
434: Cell memcasecmp(const Char *s1, const Char *s2, Cell n);
435:
436: void vm_print_profile(FILE *file);
437: void vm_count_block(Xt *ip);
438:
439: /* dynamic superinstruction stuff */
440: void compile_prim1(Cell *start);
441: void finish_code(void);
442: int forget_dyncode(Address code);
443: Label decompile_code(Label prim);
444:
445: extern int offset_image;
446: extern int die_on_signal;
447: extern int ignore_async_signals;
448: extern UCell pagesize;
449: extern ImageHeader *gforth_header;
450: extern Label *vm_prims;
451: extern Label *xts;
452: extern Cell npriminfos;
453:
454: #ifdef HAS_DEBUG
455: extern int debug;
456: #else
457: # define debug 0
458: #endif
459:
460: extern Cell *gforth_SP;
461: extern Cell *gforth_RP;
462: extern Address gforth_LP;
463: extern Float *gforth_FP;
464: extern Address gforth_UP;
465: #ifndef HAS_LINKBACK
466: extern void * gforth_pointers[];
467: #endif
468:
469: #ifdef HAS_FFCALL
470: extern Cell *gforth_RP;
471: extern Address gforth_LP;
472: extern void gforth_callback(Xt* fcall, void * alist);
473: #endif
474:
475: #ifdef NO_IP
476: extern Label next_code;
477: #endif
478:
479: #ifdef HAS_FILE
480: extern char* fileattr[6];
481: extern char* pfileattr[6];
482: extern int ufileattr[6];
483: #endif
484:
485: #ifdef PRINT_SUPER_LENGTHS
486: Cell prim_length(Cell prim);
487: void print_super_lengths();
488: #endif
489:
490: /* declare all the functions that are missing */
491: #ifndef HAVE_ATANH
492: extern double atanh(double r1);
493: extern double asinh(double r1);
494: extern double acosh(double r1);
495: #endif
496: #ifndef HAVE_ECVT
497: /* extern char* ecvt(double x, int len, int* exp, int* sign);*/
498: #endif
499: #ifndef HAVE_MEMMOVE
500: /* extern char *memmove(char *dest, const char *src, long n); */
501: #endif
502: #ifndef HAVE_POW10
503: extern double pow10(double x);
504: #endif
505: #ifndef HAVE_STRERROR
506: extern char *strerror(int err);
507: #endif
508: #ifndef HAVE_STRSIGNAL
509: extern char *strsignal(int sig);
510: #endif
511: #ifndef HAVE_STRTOUL
512: extern unsigned long int strtoul(const char *nptr, char **endptr, int base);
513: #endif
514:
515: #define GROUP(x, n)
516: #define GROUPADD(n)
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>