xref: /freebsd/stand/ficl/riscv/sysdep.h (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*******************************************************************
2                     s y s d e p . h
3 ** Forth Inspired Command Language
4 ** Author: John Sadler (john_sadler@alum.mit.edu)
5 ** Created: 16 Oct 1997
6 ** Ficl system dependent types and prototypes...
7 **
8 ** Note: Ficl also depends on the use of "assert" when
9 ** FICL_ROBUST is enabled. This may require some consideration
10 ** in firmware systems since assert often
11 ** assumes stderr/stdout.
12 ** $Id: sysdep.h,v 1.6 2001-04-26 21:41:55-07 jsadler Exp jsadler $
13 *******************************************************************/
14 /*
15 ** Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu)
16 ** All rights reserved.
17 **
18 ** Get the latest Ficl release at http://ficl.sourceforge.net
19 **
20 ** L I C E N S E  and  D I S C L A I M E R
21 **
22 ** Redistribution and use in source and binary forms, with or without
23 ** modification, are permitted provided that the following conditions
24 ** are met:
25 ** 1. Redistributions of source code must retain the above copyright
26 **    notice, this list of conditions and the following disclaimer.
27 ** 2. Redistributions in binary form must reproduce the above copyright
28 **    notice, this list of conditions and the following disclaimer in the
29 **    documentation and/or other materials provided with the distribution.
30 **
31 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
32 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
35 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 ** SUCH DAMAGE.
42 **
43 ** I am interested in hearing from anyone who uses ficl. If you have
44 ** a problem, a success story, a defect, an enhancement request, or
45 ** if you would like to contribute to the ficl release, please send
46 ** contact me by email at the address above.
47 **
48 ** $Id: sysdep.h,v 1.6 2001-04-26 21:41:55-07 jsadler Exp jsadler $
49 */
50 
51 #if !defined (__SYSDEP_H__)
52 #define __SYSDEP_H__
53 
54 #include <sys/types.h>
55 
56 #include <stddef.h> /* size_t, NULL */
57 #include <setjmp.h>
58 #include <assert.h>
59 
60 #if !defined IGNORE		/* Macro to silence unused param warnings */
61 #define IGNORE(x) (void)(x)
62 #endif
63 
64 /*
65 ** TRUE and FALSE for C boolean operations, and
66 ** portable 32 bit types for CELLs
67 **
68 */
69 #if !defined TRUE
70 #define TRUE 1
71 #endif
72 #if !defined FALSE
73 #define FALSE 0
74 #endif
75 
76 
77 /*
78 ** System dependent data type declarations...
79 */
80 #if !defined INT32
81 #define INT32 int
82 #endif
83 
84 #if !defined UNS32
85 #define UNS32 unsigned int
86 #endif
87 
88 #if !defined UNS16
89 #define UNS16 unsigned short
90 #endif
91 
92 #if !defined UNS8
93 #define UNS8 unsigned char
94 #endif
95 
96 #if !defined NULL
97 #define NULL ((void *)0)
98 #endif
99 
100 /*
101 ** FICL_UNS and FICL_INT must have the same size as a void* on
102 ** the target system. A CELL is a union of void*, FICL_UNS, and
103 ** FICL_INT.
104 ** (11/2000: same for FICL_FLOAT)
105 */
106 #if !defined FICL_INT
107 #define FICL_INT long
108 #endif
109 
110 #if !defined FICL_UNS
111 #define FICL_UNS unsigned long
112 #endif
113 
114 #if !defined FICL_FLOAT
115 #define FICL_FLOAT float
116 #endif
117 
118 /*
119 ** Ficl presently supports values of 32 and 64 for BITS_PER_CELL
120 */
121 #if !defined BITS_PER_CELL
122 #define BITS_PER_CELL 64
123 #endif
124 
125 #if ((BITS_PER_CELL != 32) && (BITS_PER_CELL != 64))
126     Error!
127 #endif
128 
129 typedef struct
130 {
131     FICL_UNS hi;
132     FICL_UNS lo;
133 } DPUNS;
134 
135 typedef struct
136 {
137     FICL_UNS quot;
138     FICL_UNS rem;
139 } UNSQR;
140 
141 typedef struct
142 {
143     FICL_INT hi;
144     FICL_INT lo;
145 } DPINT;
146 
147 typedef struct
148 {
149     FICL_INT quot;
150     FICL_INT rem;
151 } INTQR;
152 
153 
154 /*
155 ** B U I L D   C O N T R O L S
156 */
157 
158 #if !defined (FICL_MINIMAL)
159 #define FICL_MINIMAL 0
160 #endif
161 #if (FICL_MINIMAL)
162 #define FICL_WANT_SOFTWORDS  0
163 #define FICL_WANT_FLOAT      0
164 #define FICL_WANT_USER       0
165 #define FICL_WANT_LOCALS     0
166 #define FICL_WANT_DEBUGGER   0
167 #define FICL_WANT_OOP        0
168 #define FICL_PLATFORM_EXTEND 0
169 #define FICL_MULTITHREAD     0
170 #define FICL_ROBUST          0
171 #define FICL_EXTENDED_PREFIX 0
172 #endif
173 
174 /*
175 ** FICL_PLATFORM_EXTEND
176 ** Includes words defined in ficlCompilePlatform
177 */
178 #if !defined (FICL_PLATFORM_EXTEND)
179 #define FICL_PLATFORM_EXTEND 1
180 #endif
181 
182 /*
183 ** FICL_WANT_FLOAT
184 ** Includes a floating point stack for the VM, and words to do float operations.
185 ** Contributed by Guy Carver
186 */
187 #if !defined (FICL_WANT_FLOAT)
188 #define FICL_WANT_FLOAT 0
189 #endif
190 
191 /*
192 ** FICL_WANT_DEBUGGER
193 ** Inludes a simple source level debugger
194 */
195 #if !defined (FICL_WANT_DEBUGGER)
196 #define FICL_WANT_DEBUGGER 1
197 #endif
198 
199 /*
200 ** User variables: per-instance variables bound to the VM.
201 ** Kinda like thread-local storage. Could be implemented in a
202 ** VM private dictionary, but I've chosen the lower overhead
203 ** approach of an array of CELLs instead.
204 */
205 #if !defined FICL_WANT_USER
206 #define FICL_WANT_USER 1
207 #endif
208 
209 #if !defined FICL_USER_CELLS
210 #define FICL_USER_CELLS 16
211 #endif
212 
213 /*
214 ** FICL_WANT_LOCALS controls the creation of the LOCALS wordset and
215 ** a private dictionary for local variable compilation.
216 */
217 #if !defined FICL_WANT_LOCALS
218 #define FICL_WANT_LOCALS 1
219 #endif
220 
221 /* Max number of local variables per definition */
222 #if !defined FICL_MAX_LOCALS
223 #define FICL_MAX_LOCALS 16
224 #endif
225 
226 /*
227 ** FICL_WANT_OOP
228 ** Inludes object oriented programming support (in softwords)
229 ** OOP support requires locals and user variables!
230 */
231 #if !(FICL_WANT_LOCALS) || !(FICL_WANT_USER)
232 #if !defined (FICL_WANT_OOP)
233 #define FICL_WANT_OOP 0
234 #endif
235 #endif
236 
237 #if !defined (FICL_WANT_OOP)
238 #define FICL_WANT_OOP 1
239 #endif
240 
241 /*
242 ** FICL_WANT_SOFTWORDS
243 ** Controls inclusion of all softwords in softcore.c
244 */
245 #if !defined (FICL_WANT_SOFTWORDS)
246 #define FICL_WANT_SOFTWORDS 1
247 #endif
248 
249 /*
250 ** FICL_MULTITHREAD enables dictionary mutual exclusion
251 ** wia the ficlLockDictionary system dependent function.
252 ** Note: this implementation is experimental and poorly
253 ** tested. Further, it's unnecessary unless you really
254 ** intend to have multiple SESSIONS (poor choice of name
255 ** on my part) - that is, threads that modify the dictionary
256 ** at the same time.
257 */
258 #if !defined FICL_MULTITHREAD
259 #define FICL_MULTITHREAD 0
260 #endif
261 
262 /*
263 ** PORTABLE_LONGMULDIV causes ficlLongMul and ficlLongDiv to be
264 ** defined in C in sysdep.c. Use this if you cannot easily
265 ** generate an inline asm definition
266 */
267 #if !defined (PORTABLE_LONGMULDIV)
268 #define PORTABLE_LONGMULDIV 0
269 #endif
270 
271 /*
272 ** INLINE_INNER_LOOP causes the inner interpreter to be inline code
273 ** instead of a function call. This is mainly because MS VC++ 5
274 ** chokes with an internal compiler error on the function version.
275 ** in release mode. Sheesh.
276 */
277 #if !defined INLINE_INNER_LOOP
278 #if defined _DEBUG
279 #define INLINE_INNER_LOOP 0
280 #else
281 #define INLINE_INNER_LOOP 1
282 #endif
283 #endif
284 
285 /*
286 ** FICL_ROBUST enables bounds checking of stacks and the dictionary.
287 ** This will detect stack over and underflows and dictionary overflows.
288 ** Any exceptional condition will result in an assertion failure.
289 ** (As generated by the ANSI assert macro)
290 ** FICL_ROBUST == 1 --> stack checking in the outer interpreter
291 ** FICL_ROBUST == 2 also enables checking in many primitives
292 */
293 
294 #if !defined FICL_ROBUST
295 #define FICL_ROBUST 2
296 #endif
297 
298 /*
299 ** FICL_DEFAULT_STACK Specifies the default size (in CELLs) of
300 ** a new virtual machine's stacks, unless overridden at
301 ** create time.
302 */
303 #if !defined FICL_DEFAULT_STACK
304 #define FICL_DEFAULT_STACK 128
305 #endif
306 
307 /*
308 ** FICL_DEFAULT_DICT specifies the number of CELLs to allocate
309 ** for the system dictionary by default. The value
310 ** can be overridden at startup time as well.
311 ** FICL_DEFAULT_ENV specifies the number of cells to allot
312 ** for the environment-query dictionary.
313 */
314 #if !defined FICL_DEFAULT_DICT
315 #define FICL_DEFAULT_DICT 12288
316 #endif
317 
318 #if !defined FICL_DEFAULT_ENV
319 #define FICL_DEFAULT_ENV 260
320 #endif
321 
322 /*
323 ** FICL_DEFAULT_VOCS specifies the maximum number of wordlists in
324 ** the dictionary search order. See Forth DPANS sec 16.3.3
325 ** (file://dpans16.htm#16.3.3)
326 */
327 #if !defined FICL_DEFAULT_VOCS
328 #define FICL_DEFAULT_VOCS 16
329 #endif
330 
331 /*
332 ** FICL_MAX_PARSE_STEPS controls the size of an array in the FICL_SYSTEM structure
333 ** that stores pointers to parser extension functions. I would never expect to have
334 ** more than 8 of these, so that's the default limit. Too many of these functions
335 ** will probably exact a nasty performance penalty.
336 */
337 #if !defined FICL_MAX_PARSE_STEPS
338 #define FICL_MAX_PARSE_STEPS 8
339 #endif
340 
341 /*
342 ** FICL_EXTENDED_PREFIX enables a bunch of extra prefixes in prefix.c and prefix.fr (if
343 ** included as part of softcore.c)
344 */
345 #if !defined FICL_EXTENDED_PREFIX
346 #define FICL_EXTENDED_PREFIX 0
347 #endif
348 
349 /*
350 ** FICL_ALIGN is the power of two to which the dictionary
351 ** pointer address must be aligned. This value is usually
352 ** either 1 or 2, depending on the memory architecture
353 ** of the target system; 2 is safe on any 16 or 32 bit
354 ** machine. 3 would be appropriate for a 64 bit machine.
355 */
356 #if !defined FICL_ALIGN
357 #define FICL_ALIGN 3
358 #define FICL_ALIGN_ADD ((1 << FICL_ALIGN) - 1)
359 #endif
360 
361 /*
362 ** System dependent routines --
363 ** edit the implementations in sysdep.c to be compatible
364 ** with your runtime environment...
365 ** ficlTextOut sends a NULL terminated string to the
366 **   default output device - used for system error messages
367 ** ficlMalloc and ficlFree have the same semantics as malloc and free
368 **   in standard C
369 ** ficlLongMul multiplies two UNS32s and returns a 64 bit unsigned
370 **   product
371 ** ficlLongDiv divides an UNS64 by an UNS32 and returns UNS32 quotient
372 **   and remainder
373 */
374 struct vm;
375 void  ficlTextOut(struct vm *pVM, char *msg, int fNewline);
376 void *ficlMalloc (size_t size);
377 void  ficlFree   (void *p);
378 void *ficlRealloc(void *p, size_t size);
379 /*
380 ** Stub function for dictionary access control - does nothing
381 ** by default, user can redefine to guarantee exclusive dict
382 ** access to a single thread for updates. All dict update code
383 ** must be bracketed as follows:
384 ** ficlLockDictionary(TRUE);
385 ** <code that updates dictionary>
386 ** ficlLockDictionary(FALSE);
387 **
388 ** Returns zero if successful, nonzero if unable to acquire lock
389 ** before timeout (optional - could also block forever)
390 **
391 ** NOTE: this function must be implemented with lock counting
392 ** semantics: nested calls must behave properly.
393 */
394 #if FICL_MULTITHREAD
395 int ficlLockDictionary(short fLock);
396 #else
397 #define ficlLockDictionary(x) 0 /* ignore */
398 #endif
399 
400 /*
401 ** 64 bit integer math support routines: multiply two UNS32s
402 ** to get a 64 bit product, & divide the product by an UNS32
403 ** to get an UNS32 quotient and remainder. Much easier in asm
404 ** on a 32 bit CPU than in C, which usually doesn't support
405 ** the double length result (but it should).
406 */
407 DPUNS ficlLongMul(FICL_UNS x, FICL_UNS y);
408 UNSQR ficlLongDiv(DPUNS    q, FICL_UNS y);
409 
410 #endif /*__SYSDEP_H__*/
411