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