1 /*******************************************************************
3 ** Forth Inspired Command Language - virtual machine methods
4 ** Author: John Sadler (john_sadler@alum.mit.edu)
5 ** Created: 19 July 1997
6 ** $Id: vm.c,v 1.13 2001/12/05 07:21:34 jsadler Exp $
7 *******************************************************************/
9 ** This file implements the virtual machine of FICL. Each virtual
10 ** machine retains the state of an interpreter. A virtual machine
11 ** owns a pair of stacks for parameters and return addresses, as
12 ** well as a pile of state variables and the two dedicated registers
16 ** Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu)
17 ** All rights reserved.
19 ** Get the latest Ficl release at http://ficl.sourceforge.net
21 ** I am interested in hearing from anyone who uses ficl. If you have
22 ** a problem, a success story, a defect, an enhancement request, or
23 ** if you would like to contribute to the ficl release, please
24 ** contact me by email at the address above.
26 ** L I C E N S E and D I S C L A I M E R
28 ** Redistribution and use in source and binary forms, with or without
29 ** modification, are permitted provided that the following conditions
31 ** 1. Redistributions of source code must retain the above copyright
32 ** notice, this list of conditions and the following disclaimer.
33 ** 2. Redistributions in binary form must reproduce the above copyright
34 ** notice, this list of conditions and the following disclaimer in the
35 ** documentation and/or other materials provided with the distribution.
37 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
38 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
41 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 static char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
66 /**************************************************************************
67 v m B r a n c h R e l a t i v e
69 **************************************************************************/
70 void vmBranchRelative(FICL_VM *pVM, int offset)
77 /**************************************************************************
79 ** Creates a virtual machine either from scratch (if pVM is NULL on entry)
80 ** or by resizing and reinitializing an existing VM to the specified stack
82 **************************************************************************/
83 FICL_VM *vmCreate(FICL_VM *pVM, unsigned nPStack, unsigned nRStack)
87 pVM = (FICL_VM *)ficlMalloc(sizeof (FICL_VM));
89 memset(pVM, 0, sizeof (FICL_VM));
93 stackDelete(pVM->pStack);
94 pVM->pStack = stackCreate(nPStack);
97 stackDelete(pVM->rStack);
98 pVM->rStack = stackCreate(nRStack);
102 stackDelete(pVM->fStack);
103 pVM->fStack = stackCreate(nPStack);
106 pVM->textOut = ficlTextOut;
113 /**************************************************************************
115 ** Free all memory allocated to the specified VM and its subordinate
117 **************************************************************************/
118 void vmDelete (FICL_VM *pVM)
122 ficlFree(pVM->pStack);
123 ficlFree(pVM->rStack);
125 ficlFree(pVM->fStack);
134 /**************************************************************************
136 ** Sets up the specified word to be run by the inner interpreter.
137 ** Executes the word's code part immediately, but in the case of
138 ** colon definition, the definition itself needs the inner interp
139 ** to complete. This does not happen until control reaches ficlExec
140 **************************************************************************/
141 void vmExecute(FICL_VM *pVM, FICL_WORD *pWord)
143 pVM->runningWord = pWord;
149 /**************************************************************************
150 v m I n n e r L o o p
151 ** the mysterious inner interpreter...
152 ** This loop is the address interpreter that makes colon definitions
153 ** work. Upon entry, it assumes that the IP points to an entry in
154 ** a definition (the body of a colon word). It runs one word at a time
155 ** until something does vmThrow. The catcher for this is expected to exist
156 ** in the calling code.
157 ** vmThrow gets you out of this loop with a longjmp()
158 ** Visual C++ 5 chokes on this loop in Release mode. Aargh.
159 **************************************************************************/
160 #if INLINE_INNER_LOOP == 0
161 void vmInnerLoop(FICL_VM *pVM)
168 ** Recast inner loop that inlines tokens for control structures, arithmetic and stack operations,
169 ** as well as create does> : ; and various literals
194 typedef CELL *IPTYPE;
196 void vmInnerLoop(FICL_VM *pVM)
199 FICL_STACK *pStack = pVM->pStack;
203 OPCODE o = (*ip++).i;
208 stackPushINT(pStack, 0);
211 stackPushINT(pStack, 1);
214 stackPushINT(pStack, 2);
217 stackPushINT(pStack, -1);
220 stackPushINT(pStack, -2);
223 stackDrop(pStack, 1);
226 stackRoll(pStack, 1);
229 stackPick(pStack, 0);
233 stackPick(pStack, c.i);
237 stackRoll(pStack, c.i);
250 /**************************************************************************
252 ** Returns the address dictionary for this VM's system
253 **************************************************************************/
254 FICL_DICT *vmGetDict(FICL_VM *pVM)
257 return pVM->pSys->dp;
261 /**************************************************************************
262 v m G e t S t r i n g
263 ** Parses a string out of the VM input buffer and copies up to the first
264 ** FICL_STRING_MAX characters to the supplied destination buffer, a
265 ** FICL_STRING. The destination string is NULL terminated.
267 ** Returns the address of the first unused character in the dest buffer.
268 **************************************************************************/
269 char *vmGetString(FICL_VM *pVM, FICL_STRING *spDest, char delimiter)
271 STRINGINFO si = vmParseStringEx(pVM, delimiter, 0);
273 if (SI_COUNT(si) > FICL_STRING_MAX)
275 SI_SETLEN(si, FICL_STRING_MAX);
278 strncpy(spDest->text, SI_PTR(si), SI_COUNT(si));
279 spDest->text[SI_COUNT(si)] = '\0';
280 spDest->count = (FICL_COUNT)SI_COUNT(si);
282 return spDest->text + SI_COUNT(si) + 1;
286 /**************************************************************************
288 ** vmGetWord calls vmGetWord0 repeatedly until it gets a string with
290 **************************************************************************/
291 STRINGINFO vmGetWord(FICL_VM *pVM)
293 STRINGINFO si = vmGetWord0(pVM);
295 if (SI_COUNT(si) == 0)
297 vmThrow(pVM, VM_RESTART);
304 /**************************************************************************
306 ** Skip leading whitespace and parse a space delimited word from the tib.
307 ** Returns the start address and length of the word. Updates the tib
308 ** to reflect characters consumed, including the trailing delimiter.
309 ** If there's nothing of interest in the tib, returns zero. This function
310 ** does not use vmParseString because it uses isspace() rather than a
311 ** single delimiter character.
312 **************************************************************************/
313 STRINGINFO vmGetWord0(FICL_VM *pVM)
315 char *pSrc = vmGetInBuf(pVM);
316 char *pEnd = vmGetInBufEnd(pVM);
321 pSrc = skipSpace(pSrc, pEnd);
324 for (ch = *pSrc; (pEnd != pSrc) && !isspace(ch); ch = *++pSrc)
329 SI_SETLEN(si, count);
331 if ((pEnd != pSrc) && isspace(ch)) /* skip one trailing delimiter */
334 vmUpdateTib(pVM, pSrc);
340 /**************************************************************************
341 v m G e t W o r d T o P a d
342 ** Does vmGetWord and copies the result to the pad as a NULL terminated
343 ** string. Returns the length of the string. If the string is too long
344 ** to fit in the pad, it is truncated.
345 **************************************************************************/
346 int vmGetWordToPad(FICL_VM *pVM)
349 char *cp = (char *)pVM->pad;
352 if (SI_COUNT(si) > nPAD)
355 strncpy(cp, SI_PTR(si), SI_COUNT(si));
356 cp[SI_COUNT(si)] = '\0';
357 return (int)(SI_COUNT(si));
361 /**************************************************************************
362 v m P a r s e S t r i n g
363 ** Parses a string out of the input buffer using the delimiter
364 ** specified. Skips leading delimiters, marks the start of the string,
365 ** and counts characters to the next delimiter it encounters. It then
366 ** updates the vm input buffer to consume all these chars, including the
367 ** trailing delimiter.
368 ** Returns the address and length of the parsed string, not including the
369 ** trailing delimiter.
370 **************************************************************************/
371 STRINGINFO vmParseString(FICL_VM *pVM, char delim)
373 return vmParseStringEx(pVM, delim, 1);
376 STRINGINFO vmParseStringEx(FICL_VM *pVM, char delim, char fSkipLeading)
379 char *pSrc = vmGetInBuf(pVM);
380 char *pEnd = vmGetInBufEnd(pVM);
384 { /* skip lead delimiters */
385 while ((pSrc != pEnd) && (*pSrc == delim))
389 SI_SETPTR(si, pSrc); /* mark start of text */
391 for (ch = *pSrc; (pSrc != pEnd)
394 && (ch != '\n'); ch = *++pSrc)
396 ; /* find next delimiter or end of line */
399 /* set length of result */
400 SI_SETLEN(si, pSrc - SI_PTR(si));
402 if ((pSrc != pEnd) && (*pSrc == delim)) /* gobble trailing delimiter */
405 vmUpdateTib(pVM, pSrc);
410 /**************************************************************************
413 **************************************************************************/
414 CELL vmPop(FICL_VM *pVM)
416 return stackPop(pVM->pStack);
420 /**************************************************************************
423 **************************************************************************/
424 void vmPush(FICL_VM *pVM, CELL c)
426 stackPush(pVM->pStack, c);
431 /**************************************************************************
434 **************************************************************************/
435 void vmPopIP(FICL_VM *pVM)
437 pVM->ip = (IPTYPE)(stackPopPtr(pVM->rStack));
442 /**************************************************************************
445 **************************************************************************/
446 void vmPushIP(FICL_VM *pVM, IPTYPE newIP)
448 stackPushPtr(pVM->rStack, (void *)pVM->ip);
454 /**************************************************************************
456 ** Binds the specified input string to the VM and clears >IN (the index)
457 **************************************************************************/
458 void vmPushTib(FICL_VM *pVM, char *text, FICL_INT nChars, TIB *pSaveTib)
462 *pSaveTib = pVM->tib;
466 pVM->tib.end = text + nChars;
471 void vmPopTib(FICL_VM *pVM, TIB *pTib)
481 /**************************************************************************
484 **************************************************************************/
485 void vmQuit(FICL_VM *pVM)
487 stackReset(pVM->rStack);
490 pVM->runningWord = NULL;
491 pVM->state = INTERPRET;
501 /**************************************************************************
504 **************************************************************************/
505 void vmReset(FICL_VM *pVM)
508 stackReset(pVM->pStack);
510 stackReset(pVM->fStack);
517 /**************************************************************************
518 v m S e t T e x t O u t
519 ** Binds the specified output callback to the vm. If you pass NULL,
520 ** binds the default output function (ficlTextOut)
521 **************************************************************************/
522 void vmSetTextOut(FICL_VM *pVM, OUTFUNC textOut)
525 pVM->textOut = textOut;
527 pVM->textOut = ficlTextOut;
533 /**************************************************************************
535 ** Feeds text to the vm's output callback
536 **************************************************************************/
537 void vmTextOut(FICL_VM *pVM, char *text, int fNewline)
540 assert(pVM->textOut);
541 (pVM->textOut)(pVM, text, fNewline);
547 /**************************************************************************
550 **************************************************************************/
551 void vmThrow(FICL_VM *pVM, int except)
554 longjmp(*(pVM->pState), except);
558 void vmThrowErr(FICL_VM *pVM, char *fmt, ...)
562 vsprintf(pVM->pad, fmt, va);
563 vmTextOut(pVM, pVM->pad, 1);
565 longjmp(*(pVM->pState), VM_ERREXIT);
569 /**************************************************************************
570 w o r d I s I m m e d i a t e
572 **************************************************************************/
573 int wordIsImmediate(FICL_WORD *pFW)
575 return ((pFW != NULL) && (pFW->flags & FW_IMMEDIATE));
579 /**************************************************************************
580 w o r d I s C o m p i l e O n l y
582 **************************************************************************/
583 int wordIsCompileOnly(FICL_WORD *pFW)
585 return ((pFW != NULL) && (pFW->flags & FW_COMPILE));
589 /**************************************************************************
592 **************************************************************************/
593 char *strrev( char *string )
594 { /* reverse a string in-place */
595 int i = strlen(string);
596 char *p1 = string; /* first char of string */
597 char *p2 = string + i - 1; /* last non-NULL char of string */
615 /**************************************************************************
616 d i g i t _ t o _ c h a r
618 **************************************************************************/
619 char digit_to_char(int value)
621 return digits[value];
625 /**************************************************************************
626 i s P o w e r O f T w o
627 ** Tests whether supplied argument is an integer power of 2 (2**n)
628 ** where 32 > n > 1, and returns n if so. Otherwise returns zero.
629 **************************************************************************/
630 int isPowerOfTwo(FICL_UNS u)
635 for (; ((t <= u) && (t != 0)); i++, t <<= 1)
645 /**************************************************************************
648 **************************************************************************/
649 char *ltoa( FICL_INT value, char *string, int radix )
650 { /* convert long to string, any base */
652 int sign = ((radix == 10) && (value < 0));
659 pwr = isPowerOfTwo((FICL_UNS)radix);
668 FICL_UNS v = (FICL_UNS) value;
669 FICL_UNS mask = (FICL_UNS) ~(-1 << pwr);
672 *cp++ = digits[v & mask];
681 v.lo = (FICL_UNS)value;
684 result = ficlLongDiv(v, (FICL_UNS)radix);
685 *cp++ = digits[result.rem];
695 return strrev(string);
699 /**************************************************************************
702 **************************************************************************/
703 char *ultoa(FICL_UNS value, char *string, int radix )
704 { /* convert long to string, any base */
723 result = ficlLongDiv(ud, (FICL_UNS)radix);
725 *cp++ = digits[result.rem];
731 return strrev(string);
735 /**************************************************************************
737 ** Case folds a NULL terminated string in place. All characters
738 ** get converted to lower case.
739 **************************************************************************/
740 char *caseFold(char *cp)
747 *cp = (char)tolower(*cp);
755 /**************************************************************************
757 ** (jws) simplified the code a bit in hopes of appeasing Purify
758 **************************************************************************/
759 int strincmp(char *cp1, char *cp2, FICL_UNS count)
763 for (; 0 < count; ++cp1, ++cp2, --count)
765 i = tolower(*cp1) - tolower(*cp2);
768 else if (*cp1 == '\0')
774 /**************************************************************************
776 ** Given a string pointer, returns a pointer to the first non-space
777 ** char of the string, or to the NULL terminator if no such char found.
778 ** If the pointer reaches "end" first, stop there. Pass NULL to
779 ** suppress this behavior.
780 **************************************************************************/
781 char *skipSpace(char *cp, char *end)
785 while ((cp != end) && isspace(*cp))