1161454Simp/******************************************************************* 2161454Simp s y s d e p . h 3161454Simp** Forth Inspired Command Language 4161454Simp** Author: John Sadler (john_sadler@alum.mit.edu) 5161454Simp** Created: 16 Oct 1997 6161454Simp** Ficl system dependent types and prototypes... 7161454Simp** 8161454Simp** Note: Ficl also depends on the use of "assert" when 9161454Simp** FICL_ROBUST is enabled. This may require some consideration 10161454Simp** in firmware systems since assert often 11161454Simp** assumes stderr/stdout. 12161454Simp** $Id: sysdep.h,v 1.6 2001-04-26 21:41:55-07 jsadler Exp jsadler $ 13161454Simp*******************************************************************/ 14161454Simp/* 15161454Simp** Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu) 16161454Simp** All rights reserved. 17161454Simp** 18161454Simp** Get the latest Ficl release at http://ficl.sourceforge.net 19161454Simp** 20161454Simp** L I C E N S E and D I S C L A I M E R 21161454Simp** 22161454Simp** Redistribution and use in source and binary forms, with or without 23161454Simp** modification, are permitted provided that the following conditions 24161454Simp** are met: 25161454Simp** 1. Redistributions of source code must retain the above copyright 26161454Simp** notice, this list of conditions and the following disclaimer. 27161454Simp** 2. Redistributions in binary form must reproduce the above copyright 28161454Simp** notice, this list of conditions and the following disclaimer in the 29161454Simp** documentation and/or other materials provided with the distribution. 30161454Simp** 31161454Simp** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 32161454Simp** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 33161454Simp** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 34161454Simp** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 35161454Simp** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36161454Simp** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37161454Simp** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38161454Simp** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 39161454Simp** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 40161454Simp** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 41161454Simp** SUCH DAMAGE. 42161454Simp** 43161454Simp** I am interested in hearing from anyone who uses ficl. If you have 44161454Simp** a problem, a success story, a defect, an enhancement request, or 45161454Simp** if you would like to contribute to the ficl release, please send 46161454Simp** contact me by email at the address above. 47161454Simp** 48161454Simp** $Id: sysdep.h,v 1.6 2001-04-26 21:41:55-07 jsadler Exp jsadler $ 49161454Simp** $FreeBSD$ 50161454Simp*/ 51161454Simp 52161454Simp#if !defined (__SYSDEP_H__) 53161454Simp#define __SYSDEP_H__ 54161454Simp 55161454Simp#include <sys/types.h> 56161454Simp 57161454Simp#include <stddef.h> /* size_t, NULL */ 58161454Simp#include <setjmp.h> 59161454Simp#include <assert.h> 60161454Simp 61161454Simp#if !defined IGNORE /* Macro to silence unused param warnings */ 62249222Skientzle#define IGNORE(x) (void)(x) 63161454Simp#endif 64161454Simp 65161454Simp/* 66161454Simp** TRUE and FALSE for C boolean operations, and 67161454Simp** portable 32 bit types for CELLs 68161454Simp** 69161454Simp*/ 70161454Simp#if !defined TRUE 71161454Simp#define TRUE 1 72161454Simp#endif 73161454Simp#if !defined FALSE 74161454Simp#define FALSE 0 75161454Simp#endif 76161454Simp 77161454Simp 78161454Simp/* 79161454Simp** System dependent data type declarations... 80161454Simp*/ 81161454Simp#if !defined INT32 82161454Simp#define INT32 int 83161454Simp#endif 84161454Simp 85161454Simp#if !defined UNS32 86161454Simp#define UNS32 unsigned int 87161454Simp#endif 88161454Simp 89161454Simp#if !defined UNS16 90161454Simp#define UNS16 unsigned short 91161454Simp#endif 92161454Simp 93161454Simp#if !defined UNS8 94161454Simp#define UNS8 unsigned char 95161454Simp#endif 96161454Simp 97161454Simp#if !defined NULL 98161454Simp#define NULL ((void *)0) 99161454Simp#endif 100161454Simp 101161454Simp/* 102161454Simp** FICL_UNS and FICL_INT must have the same size as a void* on 103161454Simp** the target system. A CELL is a union of void*, FICL_UNS, and 104161454Simp** FICL_INT. 105161454Simp** (11/2000: same for FICL_FLOAT) 106161454Simp*/ 107161454Simp#if !defined FICL_INT 108161454Simp#define FICL_INT INT32 109161454Simp#endif 110161454Simp 111161454Simp#if !defined FICL_UNS 112161454Simp#define FICL_UNS UNS32 113161454Simp#endif 114161454Simp 115161454Simp#if !defined FICL_FLOAT 116161454Simp#define FICL_FLOAT float 117161454Simp#endif 118161454Simp 119161454Simp/* 120161454Simp** Ficl presently supports values of 32 and 64 for BITS_PER_CELL 121161454Simp*/ 122161454Simp#if !defined BITS_PER_CELL 123161454Simp#define BITS_PER_CELL 32 124161454Simp#endif 125161454Simp 126161454Simp#if ((BITS_PER_CELL != 32) && (BITS_PER_CELL != 64)) 127161454Simp Error! 128161454Simp#endif 129161454Simp 130161454Simptypedef struct 131161454Simp{ 132161454Simp FICL_UNS hi; 133161454Simp FICL_UNS lo; 134161454Simp} DPUNS; 135161454Simp 136161454Simptypedef struct 137161454Simp{ 138161454Simp FICL_UNS quot; 139161454Simp FICL_UNS rem; 140161454Simp} UNSQR; 141161454Simp 142161454Simptypedef struct 143161454Simp{ 144161454Simp FICL_INT hi; 145161454Simp FICL_INT lo; 146161454Simp} DPINT; 147161454Simp 148161454Simptypedef struct 149161454Simp{ 150161454Simp FICL_INT quot; 151161454Simp FICL_INT rem; 152161454Simp} INTQR; 153161454Simp 154161454Simp 155161454Simp/* 156161454Simp** B U I L D C O N T R O L S 157161454Simp*/ 158161454Simp 159161454Simp#if !defined (FICL_MINIMAL) 160161454Simp#define FICL_MINIMAL 0 161161454Simp#endif 162161454Simp#if (FICL_MINIMAL) 163161454Simp#define FICL_WANT_SOFTWORDS 0 164161454Simp#define FICL_WANT_FILE 0 165161454Simp#define FICL_WANT_FLOAT 0 166161454Simp#define FICL_WANT_USER 0 167161454Simp#define FICL_WANT_LOCALS 0 168161454Simp#define FICL_WANT_DEBUGGER 0 169161454Simp#define FICL_WANT_OOP 0 170161454Simp#define FICL_PLATFORM_EXTEND 0 171161454Simp#define FICL_MULTITHREAD 0 172161454Simp#define FICL_ROBUST 1 173161454Simp#define FICL_EXTENDED_PREFIX 0 174161454Simp#endif 175161454Simp 176161454Simp/* 177161454Simp** FICL_PLATFORM_EXTEND 178161454Simp** Includes words defined in ficlCompilePlatform 179161454Simp*/ 180161454Simp#if !defined (FICL_PLATFORM_EXTEND) 181161454Simp#define FICL_PLATFORM_EXTEND 1 182161454Simp#endif 183161454Simp 184161454Simp/* 185161454Simp** FICL_WANT_FILE 186161454Simp** Includes the FILE and FILE-EXT wordset and associated code. Turn this off if you do not 187161454Simp** have a filesystem! 188161454Simp** Contributed by Larry Hastings 189161454Simp*/ 190161454Simp#if !defined (FICL_WANT_FILE) 191161454Simp#define FICL_WANT_FILE 0 192161454Simp#endif 193161454Simp 194161454Simp/* 195161454Simp** FICL_WANT_FLOAT 196161454Simp** Includes a floating point stack for the VM, and words to do float operations. 197161454Simp** Contributed by Guy Carver 198161454Simp*/ 199161454Simp#if !defined (FICL_WANT_FLOAT) 200161454Simp#define FICL_WANT_FLOAT 0 201161454Simp#endif 202161454Simp 203161454Simp/* 204161454Simp** FICL_WANT_DEBUGGER 205161454Simp** Inludes a simple source level debugger 206161454Simp*/ 207161454Simp#if !defined (FICL_WANT_DEBUGGER) 208161454Simp#define FICL_WANT_DEBUGGER 1 209161454Simp#endif 210161454Simp 211161454Simp/* 212161454Simp** User variables: per-instance variables bound to the VM. 213161454Simp** Kinda like thread-local storage. Could be implemented in a 214161454Simp** VM private dictionary, but I've chosen the lower overhead 215161454Simp** approach of an array of CELLs instead. 216161454Simp*/ 217161454Simp#if !defined FICL_WANT_USER 218161454Simp#define FICL_WANT_USER 1 219161454Simp#endif 220161454Simp 221161454Simp#if !defined FICL_USER_CELLS 222161454Simp#define FICL_USER_CELLS 16 223161454Simp#endif 224161454Simp 225161454Simp/* 226161454Simp** FICL_WANT_LOCALS controls the creation of the LOCALS wordset and 227161454Simp** a private dictionary for local variable compilation. 228161454Simp*/ 229161454Simp#if !defined FICL_WANT_LOCALS 230161454Simp#define FICL_WANT_LOCALS 1 231161454Simp#endif 232161454Simp 233161454Simp/* Max number of local variables per definition */ 234161454Simp#if !defined FICL_MAX_LOCALS 235161454Simp#define FICL_MAX_LOCALS 16 236161454Simp#endif 237161454Simp 238161454Simp/* 239161454Simp** FICL_WANT_OOP 240161454Simp** Inludes object oriented programming support (in softwords) 241161454Simp** OOP support requires locals and user variables! 242161454Simp*/ 243161454Simp#if !(FICL_WANT_LOCALS) || !(FICL_WANT_USER) 244161454Simp#if !defined (FICL_WANT_OOP) 245161454Simp#define FICL_WANT_OOP 0 246161454Simp#endif 247161454Simp#endif 248161454Simp 249161454Simp#if !defined (FICL_WANT_OOP) 250161454Simp#define FICL_WANT_OOP 1 251161454Simp#endif 252161454Simp 253161454Simp/* 254161454Simp** FICL_WANT_SOFTWORDS 255161454Simp** Controls inclusion of all softwords in softcore.c 256161454Simp*/ 257161454Simp#if !defined (FICL_WANT_SOFTWORDS) 258161454Simp#define FICL_WANT_SOFTWORDS 1 259161454Simp#endif 260161454Simp 261161454Simp/* 262161454Simp** FICL_MULTITHREAD enables dictionary mutual exclusion 263161454Simp** wia the ficlLockDictionary system dependent function. 264161454Simp** Note: this implementation is experimental and poorly 265161454Simp** tested. Further, it's unnecessary unless you really 266161454Simp** intend to have multiple SESSIONS (poor choice of name 267161454Simp** on my part) - that is, threads that modify the dictionary 268161454Simp** at the same time. 269161454Simp*/ 270161454Simp#if !defined FICL_MULTITHREAD 271161454Simp#define FICL_MULTITHREAD 0 272161454Simp#endif 273161454Simp 274161454Simp/* 275161454Simp** PORTABLE_LONGMULDIV causes ficlLongMul and ficlLongDiv to be 276161454Simp** defined in C in sysdep.c. Use this if you cannot easily 277161454Simp** generate an inline asm definition 278161454Simp*/ 279161454Simp#if !defined (PORTABLE_LONGMULDIV) 280161454Simp#define PORTABLE_LONGMULDIV 0 281161454Simp#endif 282161454Simp 283161454Simp/* 284161454Simp** INLINE_INNER_LOOP causes the inner interpreter to be inline code 285161454Simp** instead of a function call. This is mainly because MS VC++ 5 286161454Simp** chokes with an internal compiler error on the function version. 287161454Simp** in release mode. Sheesh. 288161454Simp*/ 289161454Simp#if !defined INLINE_INNER_LOOP 290161454Simp#if defined _DEBUG 291161454Simp#define INLINE_INNER_LOOP 0 292161454Simp#else 293161454Simp#define INLINE_INNER_LOOP 1 294161454Simp#endif 295161454Simp#endif 296161454Simp 297161454Simp/* 298161454Simp** FICL_ROBUST enables bounds checking of stacks and the dictionary. 299161454Simp** This will detect stack over and underflows and dictionary overflows. 300161454Simp** Any exceptional condition will result in an assertion failure. 301161454Simp** (As generated by the ANSI assert macro) 302161454Simp** FICL_ROBUST == 1 --> stack checking in the outer interpreter 303161454Simp** FICL_ROBUST == 2 also enables checking in many primitives 304161454Simp*/ 305161454Simp 306161454Simp#if !defined FICL_ROBUST 307161454Simp#define FICL_ROBUST 2 308161454Simp#endif 309161454Simp 310161454Simp/* 311161454Simp** FICL_DEFAULT_STACK Specifies the default size (in CELLs) of 312161454Simp** a new virtual machine's stacks, unless overridden at 313161454Simp** create time. 314161454Simp*/ 315161454Simp#if !defined FICL_DEFAULT_STACK 316161454Simp#define FICL_DEFAULT_STACK 128 317161454Simp#endif 318161454Simp 319161454Simp/* 320161454Simp** FICL_DEFAULT_DICT specifies the number of CELLs to allocate 321161454Simp** for the system dictionary by default. The value 322161454Simp** can be overridden at startup time as well. 323161454Simp** FICL_DEFAULT_ENV specifies the number of cells to allot 324161454Simp** for the environment-query dictionary. 325161454Simp*/ 326161454Simp#if !defined FICL_DEFAULT_DICT 327161454Simp#define FICL_DEFAULT_DICT 12288 328161454Simp#endif 329161454Simp 330161454Simp#if !defined FICL_DEFAULT_ENV 331161454Simp#define FICL_DEFAULT_ENV 260 332161454Simp#endif 333161454Simp 334161454Simp/* 335161454Simp** FICL_DEFAULT_VOCS specifies the maximum number of wordlists in 336161454Simp** the dictionary search order. See Forth DPANS sec 16.3.3 337161454Simp** (file://dpans16.htm#16.3.3) 338161454Simp*/ 339161454Simp#if !defined FICL_DEFAULT_VOCS 340161454Simp#define FICL_DEFAULT_VOCS 16 341161454Simp#endif 342161454Simp 343161454Simp/* 344161454Simp** FICL_MAX_PARSE_STEPS controls the size of an array in the FICL_SYSTEM structure 345161454Simp** that stores pointers to parser extension functions. I would never expect to have 346161454Simp** more than 8 of these, so that's the default limit. Too many of these functions 347161454Simp** will probably exact a nasty performance penalty. 348161454Simp*/ 349161454Simp#if !defined FICL_MAX_PARSE_STEPS 350161454Simp#define FICL_MAX_PARSE_STEPS 8 351161454Simp#endif 352161454Simp 353161454Simp/* 354161454Simp** FICL_EXTENDED_PREFIX enables a bunch of extra prefixes in prefix.c and prefix.fr (if 355161454Simp** included as part of softcore.c) 356161454Simp*/ 357161454Simp#if !defined FICL_EXTENDED_PREFIX 358161454Simp#define FICL_EXTENDED_PREFIX 0 359161454Simp#endif 360161454Simp 361161454Simp/* 362161454Simp** FICL_ALIGN is the power of two to which the dictionary 363161454Simp** pointer address must be aligned. This value is usually 364161454Simp** either 1 or 2, depending on the memory architecture 365161454Simp** of the target system; 2 is safe on any 16 or 32 bit 366161454Simp** machine. 3 would be appropriate for a 64 bit machine. 367161454Simp*/ 368161454Simp#if !defined FICL_ALIGN 369161454Simp#define FICL_ALIGN 2 370161454Simp#define FICL_ALIGN_ADD ((1 << FICL_ALIGN) - 1) 371161454Simp#endif 372161454Simp 373161454Simp/* 374161454Simp** System dependent routines -- 375161454Simp** edit the implementations in sysdep.c to be compatible 376161454Simp** with your runtime environment... 377161454Simp** ficlTextOut sends a NULL terminated string to the 378161454Simp** default output device - used for system error messages 379161454Simp** ficlMalloc and ficlFree have the same semantics as malloc and free 380161454Simp** in standard C 381161454Simp** ficlLongMul multiplies two UNS32s and returns a 64 bit unsigned 382161454Simp** product 383161454Simp** ficlLongDiv divides an UNS64 by an UNS32 and returns UNS32 quotient 384161454Simp** and remainder 385161454Simp*/ 386161454Simpstruct vm; 387161454Simpvoid ficlTextOut(struct vm *pVM, char *msg, int fNewline); 388161454Simpvoid *ficlMalloc (size_t size); 389161454Simpvoid ficlFree (void *p); 390161454Simpvoid *ficlRealloc(void *p, size_t size); 391161454Simp/* 392161454Simp** Stub function for dictionary access control - does nothing 393161454Simp** by default, user can redefine to guarantee exclusive dict 394161454Simp** access to a single thread for updates. All dict update code 395161454Simp** must be bracketed as follows: 396161454Simp** ficlLockDictionary(TRUE); 397161454Simp** <code that updates dictionary> 398161454Simp** ficlLockDictionary(FALSE); 399161454Simp** 400161454Simp** Returns zero if successful, nonzero if unable to acquire lock 401161454Simp** before timeout (optional - could also block forever) 402161454Simp** 403161454Simp** NOTE: this function must be implemented with lock counting 404161454Simp** semantics: nested calls must behave properly. 405161454Simp*/ 406161454Simp#if FICL_MULTITHREAD 407161454Simpint ficlLockDictionary(short fLock); 408161454Simp#else 409249222Skientzle#define ficlLockDictionary(x) /* ignore */ 410161454Simp#endif 411161454Simp 412161454Simp/* 413161454Simp** 64 bit integer math support routines: multiply two UNS32s 414161454Simp** to get a 64 bit product, & divide the product by an UNS32 415161454Simp** to get an UNS32 quotient and remainder. Much easier in asm 416161454Simp** on a 32 bit CPU than in C, which usually doesn't support 417161454Simp** the double length result (but it should). 418161454Simp*/ 419161454SimpDPUNS ficlLongMul(FICL_UNS x, FICL_UNS y); 420161454SimpUNSQR ficlLongDiv(DPUNS q, FICL_UNS y); 421161454Simp 422161454Simp/* 423161454Simp** FICL_HAVE_FTRUNCATE indicates whether the current OS supports 424161454Simp** the ftruncate() function (available on most UNIXes). This 425161454Simp** function is necessary to provide the complete File-Access wordset. 426161454Simp*/ 427161454Simp#if !defined (FICL_HAVE_FTRUNCATE) 428161454Simp#define FICL_HAVE_FTRUNCATE 0 429161454Simp#endif 430161454Simp 431161454Simp 432161454Simp#endif /*__SYSDEP_H__*/ 433