Imported Upstream version 0.1beta
[debian/yforth] / yforth.h
1 /* yForth? - Written by Luca Padovani (C) 1996/97
2  * ------------------------------------------------------------------------
3  * This software is FreeWare as long as it comes with this header in each
4  * source file, anyway you can use it or any part of it whatever
5  * you want. It comes without any warranty, so use it at your own risk.
6  * ------------------------------------------------------------------------
7  * Module name:     yforth.h
8  * Abstract:        definition of constants, data types, prototypes, and so on.
9  */
10
11 #ifndef __YFORTH__
12 #define __YFORTH__
13
14 #include <setjmp.h>
15 #include <limits.h>
16 #include "errors.h"
17
18 #include "config.h"
19
20 /* Following definitions may be tuned for a particular system. Note however
21  * that their minimal value is defined by the standard.
22  */
23
24 #define TMP_BUFFER_SIZE         80
25 #define FILE_BUFFER_SIZE        128
26 #define FILE_NAME_SIZE          128
27
28 #define MAX_LOCALS                      8
29
30 #define VOC_HASH                        8
31 #define WORD_LISTS                      8
32
33 /* data structures definitions */
34
35 typedef void (*pfp)(void);
36
37 struct word_def {
38         Char *name;
39         struct word_def *link;
40         Cell class;
41         pfp func[1];
42 };
43
44 struct vocabulary {
45         struct word_def *voc[VOC_HASH];
46 };
47
48 struct voc_marker {                         /* MARKER structure */
49     struct vocabulary *list[WORD_LISTS];    /* vocabulary stack */
50     Cell top;                               /* top of stack */
51     struct vocabulary *voc;                 /* definition vocabulary */
52     struct vocabulary v_list[WORD_LISTS];   /* content of vocabularies in stack */
53     struct vocabulary v_voc;
54         Char *_dp;                                                              /* dictionary pointer */
55     struct word_def *last;                  /* ptr to last defined word */
56 };
57
58 struct raw_voc {
59         char *name;
60         void (*func) (void);
61         int class;
62 };
63
64 struct image_header {                       /* header for image file */
65         Char header[24];
66         Cell ver_hi, ver_lo;
67         UCell pattern;
68         Char *base;
69         Cell dspace_size;
70 };
71
72 #ifdef DCELL_MEM
73 union double_cell {
74         DCell d1;
75         struct {
76 #ifdef LITTLE_ENDIAN
77                 Cell low;
78                 Cell high;
79 #else
80                 Cell high;
81                 Cell low;
82 #endif
83         } d2;
84 };
85 DCell get_dcell(Cell *ptr);
86 void put_dcell(Cell *ptr, DCell d);
87 #endif
88
89 /* Some constant definitions. This should not be changed. */
90
91 #define INTERPRET       0
92 #define COMPILE         -1
93
94 #define BLOCK_SIZE              1024
95 #define NUM_BLOCKS              4
96
97 #define COMP_ONLY       0x0100
98 #define IMMEDIATE       0x0200
99 #define CLASS_MASK      (~(COMP_ONLY | IMMEDIATE))
100
101 #define A_PRIMITIVE     0
102 #define A_USER                  1
103 #define A_VARIABLE      2
104 #define A_COLON                 3
105 #define A_CONSTANT              4
106 #define A_FCONSTANT             5
107 #define A_FVARIABLE             6
108 #define A_CREATE                7
109 #define A_MARKER                8
110 #define A_2CONSTANT             9
111 #define A_2VARIABLE             10
112 #define A_LOCAL                 11
113 #define A_VALUE                 12
114 #define A_WORD                  15
115
116 /* Some macros */
117
118 #define ALIGN_PTR(n)            (((((Cell) (n)) - 1) | CellLog) + 1)
119 #define FALIGN_PTR(n)           (((((Cell) (n)) - 1) | RealLog) + 1)
120 #define WORD_PTR(ptr)           (ALIGN_PTR((ptr) + *(ptr) + sizeof(Char)))
121 #define compile_cell(x)     *((Cell *) _dp) = x, _dp += sizeof(Cell)
122 #define compile_real(x)         *((Real *) _dp) = x, _dp += sizeof(Real)
123 #define hash_func(name,len)     ((len) & (VOC_HASH - 1))
124 #ifdef DCELL_MEM
125 #       ifdef LITTLE_ENDIAN
126 #               define GET_DCELL(ptr)           get_dcell((Cell *) ptr)
127 #               define PUT_DCELL(ptr, d)        put_dcell((Cell *) ptr, (DCell) d)
128 #       else
129 #               define GET_DCELL(ptr)           *((DCell *) ptr)
130 #               define PUT_DCELL(ptr, d)        *((DCell *) ptr) = d
131 #       endif
132 #else
133 #       ifdef LITTLE_ENDIAN
134 #               define GET_DCELL(ptr)           ((DCell) (*(((Cell *) ptr) + 1)) + \
135                                                                         (((DCell) (*((Cell *) ptr))) << CellBits))
136 #               define PUT_DCELL(ptr, d)        *(((Cell *) ptr) + 1) = (Cell) d, \
137                                                                         *((Cell *) ptr) = (Cell) (d >> CellBits)
138 #       else
139 #               define GET_DCELL(ptr)           ((DCell) (*((Cell *) ptr)) + \
140                                                                         (((DCell) (*(((Cell *) ptr) + 1))) << CellBits))
141 #               define PUT_DCELL(ptr, d)        *((Cell *) ptr) = (Cell) d, \
142                                                                         *(((Cell *) ptr) + 1) = (Cell) (d >> CellBits)
143 #       endif
144 #endif
145
146 #define GET_UDCELL(ptr)         ((UDCell) GET_DCELL(ptr))
147 #define PUT_UDCELL(ptr, ud)     PUT_DCELL(ptr, ud)
148
149 /* Global variables */
150
151 extern jmp_buf warm_start_jump;
152 extern Char * dp0;
153 extern Cell dspace_size;
154 extern Cell dstack_size, rstack_size, fstack_size;
155 extern Cell tib_size;
156 extern Cell in_pnos, pnos_size;
157 extern Char * pnos, * p_pnos;
158 extern Cell pad_size;
159
160 extern struct vocabulary *list[WORD_LISTS];
161 extern Cell top;                /* indice primo vocabolario sulla pila */
162 extern struct vocabulary *voc;  /* ptr al vocabolario usato per le definzioni */
163 extern struct vocabulary *forth_wid;
164
165 /* Global functions prototypes */
166
167 void init_vocabulary(Char **dp);
168 void init_stacks(int dstack_size, int rstack_size, int fstack_size);
169 void init_data_space(int dspace_size);
170 void init_tib(int size);
171 void init_pad(int size);
172 void init_pnos(void);
173 void init_forth_environment(int reload);
174 void init_signals(void);
175 void print_version(void);
176
177 /* Virtual Machine registers definition */
178
179 extern pfp *ip;
180
181 extern Cell *sp, *sp_top, *sp_base;
182 extern Cell *rp, *rp_top, *rp_base;
183 extern Real *fp, *fp_top, *fp_base;
184 extern Cell *bp;
185
186 /* Some definitions that may be missing under certain systems or compilers */
187
188 #ifndef SEEK_SET
189 #   define SEEK_SET     0
190 #endif
191 #ifndef SEEK_CUR
192 #   define SEEK_CUR     1
193 #endif
194 #ifndef SEEK_END
195 #   define SEEK_END     2
196 #endif
197
198 #include "div.h"
199
200 #ifndef max
201 #   define max(a, b)    ((a) > (b) ? (a) : (b))
202 #endif
203
204 #endif