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