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