/*******************************************************************1s y s d e p . h2** Forth Inspired Command Language3** Author: John Sadler ([email protected])4** Created: 16 Oct 19975** Ficl system dependent types and prototypes...6**7** Note: Ficl also depends on the use of "assert" when8** FICL_ROBUST is enabled. This may require some consideration9** in firmware systems since assert often10** assumes stderr/stdout.11** $Id: sysdep.h,v 1.6 2001-04-26 21:41:55-07 jsadler Exp jsadler $12*******************************************************************/13/*14** Copyright (c) 1997-2001 John Sadler ([email protected])15** All rights reserved.16**17** Get the latest Ficl release at http://ficl.sourceforge.net18**19** L I C E N S E and D I S C L A I M E R20**21** Redistribution and use in source and binary forms, with or without22** modification, are permitted provided that the following conditions23** are met:24** 1. Redistributions of source code must retain the above copyright25** notice, this list of conditions and the following disclaimer.26** 2. Redistributions in binary form must reproduce the above copyright27** notice, this list of conditions and the following disclaimer in the28** documentation and/or other materials provided with the distribution.29**30** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND31** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE32** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE33** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE34** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL35** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS36** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)37** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT38** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY39** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF40** SUCH DAMAGE.41**42** I am interested in hearing from anyone who uses ficl. If you have43** a problem, a success story, a defect, an enhancement request, or44** if you would like to contribute to the ficl release, please send45** contact me by email at the address above.46**47** $Id: sysdep.h,v 1.6 2001-04-26 21:41:55-07 jsadler Exp jsadler $48*/4950#if !defined (__SYSDEP_H__)51#define __SYSDEP_H__5253#include <sys/types.h>5455#include <stddef.h> /* size_t, NULL */56#include <setjmp.h>57#include <assert.h>5859#if !defined IGNORE /* Macro to silence unused param warnings */60#define IGNORE(x) (void)(x)61#endif6263/*64** TRUE and FALSE for C boolean operations, and65** portable 32 bit types for CELLs66**67*/68#if !defined TRUE69#define TRUE 170#endif71#if !defined FALSE72#define FALSE 073#endif747576/*77** System dependent data type declarations...78*/79#if !defined INT3280#define INT32 int81#endif8283#if !defined UNS3284#define UNS32 unsigned int85#endif8687#if !defined UNS1688#define UNS16 unsigned short89#endif9091#if !defined UNS892#define UNS8 unsigned char93#endif9495#if !defined NULL96#define NULL ((void *)0)97#endif9899/*100** FICL_UNS and FICL_INT must have the same size as a void* on101** the target system. A CELL is a union of void*, FICL_UNS, and102** FICL_INT.103** (11/2000: same for FICL_FLOAT)104*/105#if !defined FICL_INT106#define FICL_INT INT32107#endif108109#if !defined FICL_UNS110#define FICL_UNS UNS32111#endif112113#if !defined FICL_FLOAT114#define FICL_FLOAT float115#endif116117/*118** Ficl presently supports values of 32 and 64 for BITS_PER_CELL119*/120#if !defined BITS_PER_CELL121#define BITS_PER_CELL 32122#endif123124#if ((BITS_PER_CELL != 32) && (BITS_PER_CELL != 64))125Error!126#endif127128typedef struct129{130FICL_UNS hi;131FICL_UNS lo;132} DPUNS;133134typedef struct135{136FICL_UNS quot;137FICL_UNS rem;138} UNSQR;139140typedef struct141{142FICL_INT hi;143FICL_INT lo;144} DPINT;145146typedef struct147{148FICL_INT quot;149FICL_INT rem;150} INTQR;151152153/*154** B U I L D C O N T R O L S155*/156157#if !defined (FICL_MINIMAL)158#define FICL_MINIMAL 0159#endif160#if (FICL_MINIMAL)161#define FICL_WANT_SOFTWORDS 0162#define FICL_WANT_FILE 0163#define FICL_WANT_FLOAT 0164#define FICL_WANT_USER 0165#define FICL_WANT_LOCALS 0166#define FICL_WANT_DEBUGGER 0167#define FICL_WANT_OOP 0168#define FICL_PLATFORM_EXTEND 0169#define FICL_MULTITHREAD 0170#define FICL_ROBUST 1171#define FICL_EXTENDED_PREFIX 0172#endif173174/*175** FICL_PLATFORM_EXTEND176** Includes words defined in ficlCompilePlatform177*/178#if !defined (FICL_PLATFORM_EXTEND)179#define FICL_PLATFORM_EXTEND 1180#endif181182/*183** FICL_WANT_FILE184** Includes the FILE and FILE-EXT wordset and associated code. Turn this off if you do not185** have a filesystem!186** Contributed by Larry Hastings187*/188#if !defined (FICL_WANT_FILE)189#define FICL_WANT_FILE 0190#endif191192/*193** FICL_WANT_FLOAT194** Includes a floating point stack for the VM, and words to do float operations.195** Contributed by Guy Carver196*/197#if !defined (FICL_WANT_FLOAT)198#define FICL_WANT_FLOAT 0199#endif200201/*202** FICL_WANT_DEBUGGER203** Inludes a simple source level debugger204*/205#if !defined (FICL_WANT_DEBUGGER)206#define FICL_WANT_DEBUGGER 1207#endif208209/*210** User variables: per-instance variables bound to the VM.211** Kinda like thread-local storage. Could be implemented in a212** VM private dictionary, but I've chosen the lower overhead213** approach of an array of CELLs instead.214*/215#if !defined FICL_WANT_USER216#define FICL_WANT_USER 1217#endif218219#if !defined FICL_USER_CELLS220#define FICL_USER_CELLS 16221#endif222223/*224** FICL_WANT_LOCALS controls the creation of the LOCALS wordset and225** a private dictionary for local variable compilation.226*/227#if !defined FICL_WANT_LOCALS228#define FICL_WANT_LOCALS 1229#endif230231/* Max number of local variables per definition */232#if !defined FICL_MAX_LOCALS233#define FICL_MAX_LOCALS 16234#endif235236/*237** FICL_WANT_OOP238** Inludes object oriented programming support (in softwords)239** OOP support requires locals and user variables!240*/241#if !(FICL_WANT_LOCALS) || !(FICL_WANT_USER)242#if !defined (FICL_WANT_OOP)243#define FICL_WANT_OOP 0244#endif245#endif246247#if !defined (FICL_WANT_OOP)248#define FICL_WANT_OOP 1249#endif250251/*252** FICL_WANT_SOFTWORDS253** Controls inclusion of all softwords in softcore.c254*/255#if !defined (FICL_WANT_SOFTWORDS)256#define FICL_WANT_SOFTWORDS 1257#endif258259/*260** FICL_MULTITHREAD enables dictionary mutual exclusion261** wia the ficlLockDictionary system dependent function.262** Note: this implementation is experimental and poorly263** tested. Further, it's unnecessary unless you really264** intend to have multiple SESSIONS (poor choice of name265** on my part) - that is, threads that modify the dictionary266** at the same time.267*/268#if !defined FICL_MULTITHREAD269#define FICL_MULTITHREAD 0270#endif271272/*273** PORTABLE_LONGMULDIV causes ficlLongMul and ficlLongDiv to be274** defined in C in sysdep.c. Use this if you cannot easily275** generate an inline asm definition276*/277#if !defined (PORTABLE_LONGMULDIV)278#define PORTABLE_LONGMULDIV 0279#endif280281/*282** INLINE_INNER_LOOP causes the inner interpreter to be inline code283** instead of a function call. This is mainly because MS VC++ 5284** chokes with an internal compiler error on the function version.285** in release mode. Sheesh.286*/287#if !defined INLINE_INNER_LOOP288#if defined _DEBUG289#define INLINE_INNER_LOOP 0290#else291#define INLINE_INNER_LOOP 1292#endif293#endif294295/*296** FICL_ROBUST enables bounds checking of stacks and the dictionary.297** This will detect stack over and underflows and dictionary overflows.298** Any exceptional condition will result in an assertion failure.299** (As generated by the ANSI assert macro)300** FICL_ROBUST == 1 --> stack checking in the outer interpreter301** FICL_ROBUST == 2 also enables checking in many primitives302*/303304#if !defined FICL_ROBUST305#define FICL_ROBUST 2306#endif307308/*309** FICL_DEFAULT_STACK Specifies the default size (in CELLs) of310** a new virtual machine's stacks, unless overridden at311** create time.312*/313#if !defined FICL_DEFAULT_STACK314#define FICL_DEFAULT_STACK 128315#endif316317/*318** FICL_DEFAULT_DICT specifies the number of CELLs to allocate319** for the system dictionary by default. The value320** can be overridden at startup time as well.321** FICL_DEFAULT_ENV specifies the number of cells to allot322** for the environment-query dictionary.323*/324#if !defined FICL_DEFAULT_DICT325#define FICL_DEFAULT_DICT 12288326#endif327328#if !defined FICL_DEFAULT_ENV329#define FICL_DEFAULT_ENV 260330#endif331332/*333** FICL_DEFAULT_VOCS specifies the maximum number of wordlists in334** the dictionary search order. See Forth DPANS sec 16.3.3335** (file://dpans16.htm#16.3.3)336*/337#if !defined FICL_DEFAULT_VOCS338#define FICL_DEFAULT_VOCS 16339#endif340341/*342** FICL_MAX_PARSE_STEPS controls the size of an array in the FICL_SYSTEM structure343** that stores pointers to parser extension functions. I would never expect to have344** more than 8 of these, so that's the default limit. Too many of these functions345** will probably exact a nasty performance penalty.346*/347#if !defined FICL_MAX_PARSE_STEPS348#define FICL_MAX_PARSE_STEPS 8349#endif350351/*352** FICL_EXTENDED_PREFIX enables a bunch of extra prefixes in prefix.c and prefix.fr (if353** included as part of softcore.c)354*/355#if !defined FICL_EXTENDED_PREFIX356#define FICL_EXTENDED_PREFIX 0357#endif358359/*360** FICL_ALIGN is the power of two to which the dictionary361** pointer address must be aligned. This value is usually362** either 1 or 2, depending on the memory architecture363** of the target system; 2 is safe on any 16 or 32 bit364** machine. 3 would be appropriate for a 64 bit machine.365*/366#if !defined FICL_ALIGN367#define FICL_ALIGN 2368#define FICL_ALIGN_ADD ((1 << FICL_ALIGN) - 1)369#endif370371/*372** System dependent routines --373** edit the implementations in sysdep.c to be compatible374** with your runtime environment...375** ficlTextOut sends a NULL terminated string to the376** default output device - used for system error messages377** ficlMalloc and ficlFree have the same semantics as malloc and free378** in standard C379** ficlLongMul multiplies two UNS32s and returns a 64 bit unsigned380** product381** ficlLongDiv divides an UNS64 by an UNS32 and returns UNS32 quotient382** and remainder383*/384struct vm;385void ficlTextOut(struct vm *pVM, char *msg, int fNewline);386void *ficlMalloc (size_t size);387void ficlFree (void *p);388void *ficlRealloc(void *p, size_t size);389/*390** Stub function for dictionary access control - does nothing391** by default, user can redefine to guarantee exclusive dict392** access to a single thread for updates. All dict update code393** must be bracketed as follows:394** ficlLockDictionary(TRUE);395** <code that updates dictionary>396** ficlLockDictionary(FALSE);397**398** Returns zero if successful, nonzero if unable to acquire lock399** before timeout (optional - could also block forever)400**401** NOTE: this function must be implemented with lock counting402** semantics: nested calls must behave properly.403*/404#if FICL_MULTITHREAD405int ficlLockDictionary(short fLock);406#else407#define ficlLockDictionary(x) /* ignore */408#endif409410/*411** 64 bit integer math support routines: multiply two UNS32s412** to get a 64 bit product, & divide the product by an UNS32413** to get an UNS32 quotient and remainder. Much easier in asm414** on a 32 bit CPU than in C, which usually doesn't support415** the double length result (but it should).416*/417DPUNS ficlLongMul(FICL_UNS x, FICL_UNS y);418UNSQR ficlLongDiv(DPUNS q, FICL_UNS y);419420/*421** FICL_HAVE_FTRUNCATE indicates whether the current OS supports422** the ftruncate() function (available on most UNIXes). This423** function is necessary to provide the complete File-Access wordset.424*/425#if !defined (FICL_HAVE_FTRUNCATE)426#define FICL_HAVE_FTRUNCATE 0427#endif428429430#endif /*__SYSDEP_H__*/431432433