xref: /freebsd/stand/ficl/amd64/sysdep.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 ** $Id: sysdep.h,v 1.6 2001-04-26 21:41:55-07 jsadler Exp jsadler $
49 */
50 
51 /* $FreeBSD$ */
52 
53 #if !defined (__SYSDEP_H__)
54 #define __SYSDEP_H__
55 
56 #include <sys/types.h>
57 
58 #include <stddef.h> /* size_t, NULL */
59 #include <setjmp.h>
60 #include <assert.h>
61 
62 #if !defined IGNORE		/* Macro to silence unused param warnings */
63 #define IGNORE(x) &x
64 #endif
65 
66 /*
67 ** TRUE and FALSE for C boolean operations, and
68 ** portable 32 bit types for CELLs
69 **
70 */
71 #if !defined TRUE
72 #define TRUE 1
73 #endif
74 #if !defined FALSE
75 #define FALSE 0
76 #endif
77 
78 /*
79 ** System dependent data type declarations...
80 */
81 #if !defined INT32
82 #define INT32 int
83 #endif
84 
85 #if !defined UNS32
86 #define UNS32 unsigned int
87 #endif
88 
89 #if !defined UNS16
90 #define UNS16 unsigned short
91 #endif
92 
93 #if !defined UNS8
94 #define UNS8 unsigned char
95 #endif
96 
97 #if !defined NULL
98 #define NULL ((void *)0)
99 #endif
100 
101 /*
102 ** FICL_UNS and FICL_INT must have the same size as a void* on
103 ** the target system. A CELL is a union of void*, FICL_UNS, and
104 ** FICL_INT.
105 ** (11/2000: same for FICL_FLOAT)
106 */
107 #if !defined FICL_INT
108 #define FICL_INT long
109 #endif
110 
111 #if !defined FICL_UNS
112 #define FICL_UNS unsigned long
113 #endif
114 
115 #if !defined FICL_FLOAT
116 #define FICL_FLOAT float
117 #endif
118 
119 /*
120 ** Ficl presently supports values of 32 and 64 for BITS_PER_CELL
121 */
122 #if !defined BITS_PER_CELL
123 #define BITS_PER_CELL 64
124 #endif
125 
126 #if ((BITS_PER_CELL != 32) && (BITS_PER_CELL != 64))
127     Error!
128 #endif
129 
130 typedef struct
131 {
132     FICL_UNS hi;
133     FICL_UNS lo;
134 } DPUNS;
135 
136 typedef struct
137 {
138     FICL_UNS quot;
139     FICL_UNS rem;
140 } UNSQR;
141 
142 typedef struct
143 {
144     FICL_INT hi;
145     FICL_INT lo;
146 } DPINT;
147 
148 typedef struct
149 {
150     FICL_INT quot;
151     FICL_INT rem;
152 } INTQR;
153 
154 
155 /*
156 ** B U I L D   C O N T R O L S
157 */
158 
159 #if !defined (FICL_MINIMAL)
160 #define FICL_MINIMAL 0
161 #endif
162 #if (FICL_MINIMAL)
163 #define FICL_WANT_SOFTWORDS  0
164 #define FICL_WANT_FILE       0
165 #define FICL_WANT_FLOAT      0
166 #define FICL_WANT_USER       0
167 #define FICL_WANT_LOCALS     0
168 #define FICL_WANT_DEBUGGER   0
169 #define FICL_WANT_OOP        0
170 #define FICL_PLATFORM_EXTEND 0
171 #define FICL_MULTITHREAD     0
172 #define FICL_ROBUST          0
173 #define FICL_EXTENDED_PREFIX 0
174 #endif
175 
176 /*
177 ** FICL_PLATFORM_EXTEND
178 ** Includes words defined in ficlCompilePlatform
179 */
180 #if !defined (FICL_PLATFORM_EXTEND)
181 #define FICL_PLATFORM_EXTEND 1
182 #endif
183 
184 
185 /*
186 ** FICL_WANT_FILE
187 ** Includes the FILE and FILE-EXT wordset and associated code. Turn this off if you do not
188 ** have a filesystem!
189 ** Contributed by Larry Hastings
190 */
191 #if !defined (FICL_WANT_FILE)
192 #define FICL_WANT_FILE 0
193 #endif
194 
195 /*
196 ** FICL_WANT_FLOAT
197 ** Includes a floating point stack for the VM, and words to do float operations.
198 ** Contributed by Guy Carver
199 */
200 #if !defined (FICL_WANT_FLOAT)
201 #define FICL_WANT_FLOAT 0
202 #endif
203 
204 /*
205 ** FICL_WANT_DEBUGGER
206 ** Inludes a simple source level debugger
207 */
208 #if !defined (FICL_WANT_DEBUGGER)
209 #define FICL_WANT_DEBUGGER 1
210 #endif
211 
212 /*
213 ** FICL_EXTENDED_PREFIX enables a bunch of extra prefixes in prefix.c and prefix.fr (if
214 ** included as part of softcore.c)
215 */
216 #if !defined FICL_EXTENDED_PREFIX
217 #define FICL_EXTENDED_PREFIX 0
218 #endif
219 
220 /*
221 ** User variables: per-instance variables bound to the VM.
222 ** Kinda like thread-local storage. Could be implemented in a
223 ** VM private dictionary, but I've chosen the lower overhead
224 ** approach of an array of CELLs instead.
225 */
226 #if !defined FICL_WANT_USER
227 #define FICL_WANT_USER 1
228 #endif
229 
230 #if !defined FICL_USER_CELLS
231 #define FICL_USER_CELLS 16
232 #endif
233 
234 /*
235 ** FICL_WANT_LOCALS controls the creation of the LOCALS wordset and
236 ** a private dictionary for local variable compilation.
237 */
238 #if !defined FICL_WANT_LOCALS
239 #define FICL_WANT_LOCALS 1
240 #endif
241 
242 /* Max number of local variables per definition */
243 #if !defined FICL_MAX_LOCALS
244 #define FICL_MAX_LOCALS 16
245 #endif
246 
247 /*
248 ** FICL_WANT_OOP
249 ** Inludes object oriented programming support (in softwords)
250 ** OOP support requires locals and user variables!
251 */
252 #if !(FICL_WANT_LOCALS) || !(FICL_WANT_USER)
253 #if !defined (FICL_WANT_OOP)
254 #define FICL_WANT_OOP 0
255 #endif
256 #endif
257 
258 #if !defined (FICL_WANT_OOP)
259 #define FICL_WANT_OOP 1
260 #endif
261 
262 /*
263 ** FICL_WANT_SOFTWORDS
264 ** Controls inclusion of all softwords in softcore.c
265 */
266 #if !defined (FICL_WANT_SOFTWORDS)
267 #define FICL_WANT_SOFTWORDS 1
268 #endif
269 
270 /*
271 ** FICL_MULTITHREAD enables dictionary mutual exclusion
272 ** wia the ficlLockDictionary system dependent function.
273 ** Note: this implementation is experimental and poorly
274 ** tested. Further, it's unnecessary unless you really
275 ** intend to have multiple SESSIONS (poor choice of name
276 ** on my part) - that is, threads that modify the dictionary
277 ** at the same time.
278 */
279 #if !defined FICL_MULTITHREAD
280 #define FICL_MULTITHREAD 0
281 #endif
282 
283 /*
284 ** PORTABLE_LONGMULDIV causes ficlLongMul and ficlLongDiv to be
285 ** defined in C in sysdep.c. Use this if you cannot easily
286 ** generate an inline asm definition
287 */
288 #if !defined (PORTABLE_LONGMULDIV)
289 #define PORTABLE_LONGMULDIV 0
290 #endif
291 
292 /*
293 ** INLINE_INNER_LOOP causes the inner interpreter to be inline code
294 ** instead of a function call. This is mainly because MS VC++ 5
295 ** chokes with an internal compiler error on the function version.
296 ** in release mode. Sheesh.
297 */
298 #if !defined INLINE_INNER_LOOP
299 #if defined _DEBUG
300 #define INLINE_INNER_LOOP 0
301 #else
302 #define INLINE_INNER_LOOP 1
303 #endif
304 #endif
305 
306 /*
307 ** FICL_ROBUST enables bounds checking of stacks and the dictionary.
308 ** This will detect stack over and underflows and dictionary overflows.
309 ** Any exceptional condition will result in an assertion failure.
310 ** (As generated by the ANSI assert macro)
311 ** FICL_ROBUST == 1 --> stack checking in the outer interpreter
312 ** FICL_ROBUST == 2 also enables checking in many primitives
313 */
314 
315 #if !defined FICL_ROBUST
316 #define FICL_ROBUST 2
317 #endif
318 
319 /*
320 ** FICL_DEFAULT_STACK Specifies the default size (in CELLs) of
321 ** a new virtual machine's stacks, unless overridden at
322 ** create time.
323 */
324 #if !defined FICL_DEFAULT_STACK
325 #define FICL_DEFAULT_STACK 128
326 #endif
327 
328 /*
329 ** FICL_DEFAULT_DICT specifies the number of CELLs to allocate
330 ** for the system dictionary by default. The value
331 ** can be overridden at startup time as well.
332 ** FICL_DEFAULT_ENV specifies the number of cells to allot
333 ** for the environment-query dictionary.
334 */
335 #if !defined FICL_DEFAULT_DICT
336 #define FICL_DEFAULT_DICT 12288
337 #endif
338 
339 #if !defined FICL_DEFAULT_ENV
340 #define FICL_DEFAULT_ENV 260
341 #endif
342 
343 /*
344 ** FICL_DEFAULT_VOCS specifies the maximum number of wordlists in
345 ** the dictionary search order. See Forth DPANS sec 16.3.3
346 ** (file://dpans16.htm#16.3.3)
347 */
348 #if !defined FICL_DEFAULT_VOCS
349 #define FICL_DEFAULT_VOCS 16
350 #endif
351 
352 /*
353 ** FICL_MAX_PARSE_STEPS controls the size of an array in the FICL_SYSTEM structure
354 ** that stores pointers to parser extension functions. I would never expect to have
355 ** more than 8 of these, so that's the default limit. Too many of these functions
356 ** will probably exact a nasty performance penalty.
357 */
358 #if !defined FICL_MAX_PARSE_STEPS
359 #define FICL_MAX_PARSE_STEPS 8
360 #endif
361 
362 /*
363 ** FICL_ALIGN is the power of two to which the dictionary
364 ** pointer address must be aligned. This value is usually
365 ** either 1 or 2, depending on the memory architecture
366 ** of the target system; 2 is safe on any 16 or 32 bit
367 ** machine. 3 would be appropriate for a 64 bit machine.
368 */
369 #if !defined FICL_ALIGN
370 #define FICL_ALIGN 3
371 #define FICL_ALIGN_ADD ((1 << FICL_ALIGN) - 1)
372 #endif
373 
374 /*
375 ** System dependent routines --
376 ** edit the implementations in sysdep.c to be compatible
377 ** with your runtime environment...
378 ** ficlTextOut sends a NULL terminated string to the
379 **   default output device - used for system error messages
380 ** ficlMalloc and ficlFree have the same semantics as malloc and free
381 **   in standard C
382 ** ficlLongMul multiplies two UNS32s and returns a 64 bit unsigned
383 **   product
384 ** ficlLongDiv divides an UNS64 by an UNS32 and returns UNS32 quotient
385 **   and remainder
386 */
387 struct vm;
388 void  ficlTextOut(struct vm *pVM, char *msg, int fNewline);
389 void *ficlMalloc (size_t size);
390 void  ficlFree   (void *p);
391 void *ficlRealloc(void *p, size_t size);
392 /*
393 ** Stub function for dictionary access control - does nothing
394 ** by default, user can redefine to guarantee exclusive dict
395 ** access to a single thread for updates. All dict update code
396 ** must be bracketed as follows:
397 ** ficlLockDictionary(TRUE);
398 ** <code that updates dictionary>
399 ** ficlLockDictionary(FALSE);
400 **
401 ** Returns zero if successful, nonzero if unable to acquire lock
402 ** before timeout (optional - could also block forever)
403 **
404 ** NOTE: this function must be implemented with lock counting
405 ** semantics: nested calls must behave properly.
406 */
407 #if FICL_MULTITHREAD
408 int ficlLockDictionary(short fLock);
409 #else
410 #define ficlLockDictionary(x) 0 /* ignore */
411 #endif
412 
413 /*
414 ** 64 bit integer math support routines: multiply two UNS32s
415 ** to get a 64 bit product, & divide the product by an UNS32
416 ** to get an UNS32 quotient and remainder. Much easier in asm
417 ** on a 32 bit CPU than in C, which usually doesn't support
418 ** the double length result (but it should).
419 */
420 DPUNS ficlLongMul(FICL_UNS x, FICL_UNS y);
421 UNSQR ficlLongDiv(DPUNS    q, FICL_UNS y);
422 
423 
424 /*
425 ** FICL_HAVE_FTRUNCATE indicates whether the current OS supports
426 ** the ftruncate() function (available on most UNIXes).  This
427 ** function is necessary to provide the complete File-Access wordset.
428 */
429 #if !defined (FICL_HAVE_FTRUNCATE)
430 #define FICL_HAVE_FTRUNCATE 0
431 #endif
432 
433 
434 #endif /*__SYSDEP_H__*/
435