1 /* @(#) pf_inner.c 98/03/16 1.7 */
2 /***************************************************************
3 ** Inner Interpreter for Forth based on 'C'
6 ** Copyright 1994 3DO, Phil Burk, Larry Polansky, David Rosenboom
8 ** The pForth software code is dedicated to the public domain,
9 ** and any third party may reproduce, distribute and modify
10 ** the pForth software code or any derivative works thereof
11 ** without any compensation or license. The pForth software
12 ** code is provided on an "as is" basis without any warranty
13 ** of any kind, including, without limitation, the implied
14 ** warranties of merchantability and fitness for a particular
15 ** purpose and their equivalents under the laws of any jurisdiction.
17 ****************************************************************
19 ** 940502 PLB Creation.
20 ** 940505 PLB More macros.
21 ** 940509 PLB Moved all stack stuff into pfCatch.
22 ** 941014 PLB Converted to flat secondary strusture.
23 ** 941027 rdg added casts to ID_SP_FETCH, ID_RP_FETCH,
24 ** and ID_HERE for armcc
25 ** 941130 PLB Made w@ unsigned
27 ***************************************************************/
31 #if defined(WIN32) && !defined(__MINGW32__)
35 #define SYSTEM_LOAD_FILE "system.fth"
37 /***************************************************************
38 ** Macros for data stack access.
39 ** TOS is cached in a register in pfCatch.
40 ***************************************************************/
42 #define STKPTR (DataStackPtr)
43 #define M_POP (*(STKPTR++))
44 #define M_PUSH(n) {*(--(STKPTR)) = (cell_t) (n);}
45 #define M_STACK(n) (STKPTR[n])
47 #define TOS (TopOfStack)
48 #define PUSH_TOS M_PUSH(TOS)
49 #define M_DUP PUSH_TOS;
50 #define M_DROP { TOS = M_POP; }
52 #define ASCII_EOT (0x04)
54 /***************************************************************
55 ** Macros for Floating Point stack access.
56 ***************************************************************/
58 #define FP_STKPTR (FloatStackPtr)
59 #define M_FP_SPZERO (gCurrentTask->td_FloatStackBase)
60 #define M_FP_POP (*(FP_STKPTR++))
61 #define M_FP_PUSH(n) {*(--(FP_STKPTR)) = (PF_FLOAT) (n);}
62 #define M_FP_STACK(n) (FP_STKPTR[n])
64 #define FP_TOS (fpTopOfStack)
65 #define PUSH_FP_TOS M_FP_PUSH(FP_TOS)
66 #define M_FP_DUP PUSH_FP_TOS;
67 #define M_FP_DROP { FP_TOS = M_FP_POP; }
70 /***************************************************************
71 ** Macros for return stack access.
72 ***************************************************************/
74 #define TORPTR (ReturnStackPtr)
75 #define M_R_DROP {TORPTR++;}
76 #define M_R_POP (*(TORPTR++))
77 #define M_R_PICK(n) (TORPTR[n])
78 #define M_R_PUSH(n) {*(--(TORPTR)) = (cell_t) (n);}
80 /***************************************************************
82 ***************************************************************/
84 #define M_BRANCH { InsPtr = (cell_t *) (((uint8_t *) InsPtr) + READ_CELL_DIC(InsPtr)); }
86 /* Cache top of data stack like in JForth. */
88 #define LOAD_REGISTERS \
90 STKPTR = gCurrentTask->td_StackPtr; \
92 FP_STKPTR = gCurrentTask->td_FloatStackPtr; \
94 TORPTR = gCurrentTask->td_ReturnPtr; \
97 #define SAVE_REGISTERS \
99 gCurrentTask->td_ReturnPtr = TORPTR; \
101 gCurrentTask->td_StackPtr = STKPTR; \
102 M_FP_PUSH( FP_TOS ); \
103 gCurrentTask->td_FloatStackPtr = FP_STKPTR; \
107 /* Cache top of data stack like in JForth. */
108 #define LOAD_REGISTERS \
110 STKPTR = gCurrentTask->td_StackPtr; \
112 TORPTR = gCurrentTask->td_ReturnPtr; \
115 #define SAVE_REGISTERS \
117 gCurrentTask->td_ReturnPtr = TORPTR; \
119 gCurrentTask->td_StackPtr = STKPTR; \
128 #define DO_VAR(varname) { PUSH_TOS; TOS = (cell_t) &varname; }
131 #define M_THROW(err) \
133 ExceptionReturnCode = (ThrowCode)(err); \
134 TORPTR = InitialReturnStack; /* Will cause return to 'C' */ \
135 STKPTR = InitialDataStack; \
136 FP_STKPTR = InitialFloatStack; \
139 #define M_THROW(err) \
141 ExceptionReturnCode = (err); \
142 TORPTR = InitialReturnStack; /* Will cause return to 'C' */ \
143 STKPTR = InitialDataStack; \
147 /***************************************************************
149 ***************************************************************/
151 #define BINARY_OP( op ) { TOS = M_POP op TOS; }
152 #define endcase break
154 #if defined(PF_NO_SHELL) || !defined(PF_SUPPORT_TRACE)
155 #define TRACENAMES /* no names */
157 /* Display name of executing routine. */
158 static void TraceNames( ExecToken Token, cell_t Level )
163 if( ffTokenToName( Token, &DebugName ) )
166 if( gCurrentTask->td_OUT > 0 ) EMIT_CR;
168 for( i=0; i<Level; i++ )
172 TypeName( DebugName );
173 /* Space out to column N then .S */
174 NumSpaces = 30 - gCurrentTask->td_OUT;
175 for( i=0; i < NumSpaces; i++ )
180 /* No longer needed? gCurrentTask->td_OUT = 0; */ /* !!! Hack for ffDotS() */
185 MSG_NUM_H("Couldn't find Name for ", Token);
190 if( (gVarTraceLevel > Level) ) \
191 { SAVE_REGISTERS; TraceNames( Token, Level ); LOAD_REGISTERS; }
192 #endif /* PF_NO_SHELL */
194 /* Use local copy of CODE_BASE for speed. */
195 #define LOCAL_CODEREL_TO_ABS( a ) ((cell_t *) (((cell_t) a) + CodeBase))
197 /* Truncate the unsigned double cell integer LO/HI to an uint64_t. */
198 static uint64_t UdToUint64( ucell_t Lo, ucell_t Hi )
200 return (( 2 * sizeof(ucell_t) == sizeof(uint64_t) )
201 ? (((uint64_t)Lo) | (((uint64_t)Hi) >> (sizeof(ucell_t) * 8)))
205 /* Return TRUE if the unsigned double cell integer LO/HI is not greater
206 * then the greatest uint64_t.
208 static int UdIsUint64( ucell_t Lo, ucell_t Hi )
210 return (( 2 * sizeof(ucell_t) == sizeof(uint64_t) )
215 static const char *pfSelectFileModeCreate( cell_t fam );
216 static const char *pfSelectFileModeOpen( cell_t fam );
218 /**************************************************************/
219 static const char *pfSelectFileModeCreate( cell_t fam )
221 const char *famText = NULL;
224 case (PF_FAM_WRITE_ONLY + PF_FAM_BINARY_FLAG):
225 famText = PF_FAM_BIN_CREATE_WO;
227 case (PF_FAM_READ_WRITE + PF_FAM_BINARY_FLAG):
228 famText = PF_FAM_BIN_CREATE_RW;
230 case PF_FAM_WRITE_ONLY:
231 famText = PF_FAM_CREATE_WO;
233 case PF_FAM_READ_WRITE:
234 famText = PF_FAM_CREATE_RW;
243 /**************************************************************/
244 static const char *pfSelectFileModeOpen( cell_t fam )
246 const char *famText = NULL;
249 case (PF_FAM_READ_ONLY + PF_FAM_BINARY_FLAG):
250 famText = PF_FAM_BIN_OPEN_RO;
252 case (PF_FAM_WRITE_ONLY + PF_FAM_BINARY_FLAG):
253 famText = PF_FAM_BIN_CREATE_WO;
255 case (PF_FAM_READ_WRITE + PF_FAM_BINARY_FLAG):
256 famText = PF_FAM_BIN_OPEN_RW;
258 case PF_FAM_READ_ONLY:
259 famText = PF_FAM_OPEN_RO;
261 case PF_FAM_WRITE_ONLY:
262 famText = PF_FAM_CREATE_WO;
264 case PF_FAM_READ_WRITE:
266 famText = PF_FAM_OPEN_RW;
272 /**************************************************************/
273 ThrowCode pfCatch( ExecToken XT )
275 register cell_t TopOfStack; /* Cache for faster execution. */
276 register cell_t *DataStackPtr;
277 register cell_t *ReturnStackPtr;
278 register cell_t *InsPtr = NULL;
279 register cell_t Token;
283 PF_FLOAT fpTopOfStack;
284 PF_FLOAT *FloatStackPtr;
287 PF_FLOAT *InitialFloatStack;
289 #ifdef PF_SUPPORT_TRACE
292 cell_t *LocalsPtr = NULL;
294 cell_t *InitialReturnStack;
295 cell_t *InitialDataStack;
296 cell_t FakeSecondary[2];
300 uint8_t *CodeBase = (uint8_t *) CODE_BASE;
301 ThrowCode ExceptionReturnCode = 0;
304 gExecutionDepth += 1;
305 PRT(("pfCatch( 0x%x ), depth = %d\n", XT, gExecutionDepth ));
309 ** Initialize FakeSecondary this way to avoid having stuff in the data section,
310 ** which is not supported for some embedded system loaders.
312 FakeSecondary[0] = 0;
313 FakeSecondary[1] = ID_EXIT; /* For EXECUTE */
315 /* Move data from task structure to registers for speed. */
318 /* Save initial stack depths for THROW */
319 InitialReturnStack = TORPTR;
320 InitialDataStack = STKPTR ;
322 InitialFloatStack = FP_STKPTR;
329 DBUG(("pfCatch: Token = 0x%x\n", Token ));
331 /* --------------------------------------------------------------- */
332 /* If secondary, thread down code tree until we hit a primitive. */
333 while( !IsTokenPrimitive( Token ) )
335 #ifdef PF_SUPPORT_TRACE
336 if((gVarTraceFlags & TRACE_INNER) )
338 MSG("pfCatch: Secondary Token = 0x");
340 MSG_NUM_H(", InsPtr = 0x", InsPtr);
345 /* Save IP on return stack like a JSR. */
348 /* Convert execution token to absolute address. */
349 InsPtr = (cell_t *) ( LOCAL_CODEREL_TO_ABS(Token) );
351 /* Fetch token at IP. */
352 Token = READ_CELL_DIC(InsPtr++);
354 #ifdef PF_SUPPORT_TRACE
355 /* Bump level for trace display */
361 #ifdef PF_SUPPORT_TRACE
365 /* Execute primitive Token. */
369 /* Pop up a level in Forth inner interpreter.
370 ** Used to implement semicolon.
371 ** Put first in switch because ID_EXIT==0 */
373 InsPtr = ( cell_t *) M_R_POP;
374 #ifdef PF_SUPPORT_TRACE
379 case ID_1MINUS: TOS--; endcase;
381 case ID_1PLUS: TOS++; endcase;
385 ff2Literal( TOS, M_POP );
388 #endif /* !PF_NO_SHELL */
391 /* hi part stored first, put on top of stack */
393 TOS = READ_CELL_DIC(InsPtr++);
394 M_PUSH(READ_CELL_DIC(InsPtr++));
397 case ID_2MINUS: TOS -= 2; endcase;
399 case ID_2PLUS: TOS += 2; endcase;
402 case ID_2OVER: /* ( a b c d -- a b c d a b ) */
404 Scratch = M_STACK(3);
409 case ID_2SWAP: /* ( a b c d -- c d a b ) */
410 Scratch = M_STACK(0); /* c */
411 M_STACK(0) = M_STACK(2); /* a */
412 M_STACK(2) = Scratch; /* c */
413 Scratch = TOS; /* d */
414 TOS = M_STACK(1); /* b */
415 M_STACK(1) = Scratch; /* d */
418 case ID_2DUP: /* ( a b -- a b a b ) */
420 Scratch = M_STACK(1);
426 M_PUSH( (*(TORPTR+1)) );
442 case ID_ACCEPT_P: /* ( c-addr +n1 -- +n2 ) */
443 CharPtr = (char *) M_POP;
444 TOS = ioAccept( CharPtr, TOS );
449 ffALiteral( ABS_TO_CODEREL(TOS) );
452 #endif /* !PF_NO_SHELL */
456 TOS = (cell_t) LOCAL_CODEREL_TO_ABS( READ_CELL_DIC(InsPtr++) );
459 /* Allocate some extra and put validation identifier at base */
460 #define PF_MEMORY_VALIDATOR (0xA81B4D69)
462 /* Allocate at least one cell's worth because we clobber first cell. */
463 if ( TOS < sizeof(cell_t) )
465 Temp = sizeof(cell_t);
471 /* Allocate extra cells worth because we store validation info. */
472 CellPtr = (cell_t *) pfAllocMem( Temp + sizeof(cell_t) );
475 /* This was broken into two steps because different compilers incremented
476 ** CellPtr before or after the XOR step. */
477 Temp = (cell_t)CellPtr ^ PF_MEMORY_VALIDATOR;
479 M_PUSH( (cell_t) CellPtr );
485 TOS = -1; /* FIXME Fix error code. */
489 case ID_AND: BINARY_OP( & ); endcase;
491 case ID_ARSHIFT: BINARY_OP( >> ); endcase; /* Arithmetic right shift */
495 TOS = CREATE_BODY_OFFSET;
498 /* Branch is followed by an offset relative to address of offset. */
500 DBUGX(("Before Branch: IP = 0x%x\n", InsPtr ));
502 DBUGX(("After Branch: IP = 0x%x\n", InsPtr ));
507 M_THROW( THROW_BYE );
511 MSG("Emergency exit.\n");
519 Scratch = pfCatch( Scratch );
527 Scratch = READ_CELL_DIC(InsPtr++);
528 CallUserFunction( Scratch & 0xFFFF,
530 (Scratch >> 24) & 0x7F );
534 /* Support 32/64 bit operation. */
537 TOS = sizeof(cell_t);
541 TOS = TOS * sizeof(cell_t);
544 case ID_CFETCH: TOS = *((uint8_t *) TOS); endcase;
546 case ID_CMOVE: /* ( src dst n -- ) */
548 register char *DstPtr = (char *) M_POP; /* dst */
549 CharPtr = (char *) M_POP; /* src */
550 for( Scratch=0; (ucell_t) Scratch < (ucell_t) TOS ; Scratch++ )
552 *DstPtr++ = *CharPtr++;
558 case ID_CMOVE_UP: /* ( src dst n -- ) */
560 register char *DstPtr = ((char *) M_POP) + TOS; /* dst */
561 CharPtr = ((char *) M_POP) + TOS;; /* src */
562 for( Scratch=0; (ucell_t) Scratch < (ucell_t) TOS ; Scratch++ )
564 *(--DstPtr) = *(--CharPtr);
576 case ID_COLON_P: /* ( $name xt -- ) */
577 CreateDicEntry( TOS, (char *) M_POP, 0 );
580 #endif /* !PF_NO_SHELL */
586 s2 = (const char *) M_POP;
588 s1 = (const char *) M_POP;
589 TOS = ffCompare( s1, len1, s2, TOS );
593 /* ( a b -- flag , Comparisons ) */
595 TOS = ( TOS == M_POP ) ? FTRUE : FFALSE ;
597 case ID_COMP_NOT_EQUAL:
598 TOS = ( TOS != M_POP ) ? FTRUE : FFALSE ;
600 case ID_COMP_GREATERTHAN:
601 TOS = ( M_POP > TOS ) ? FTRUE : FFALSE ;
603 case ID_COMP_LESSTHAN:
604 TOS = ( M_POP < TOS ) ? FTRUE : FFALSE ;
606 case ID_COMP_U_GREATERTHAN:
607 TOS = ( ((ucell_t)M_POP) > ((ucell_t)TOS) ) ? FTRUE : FFALSE ;
609 case ID_COMP_U_LESSTHAN:
610 TOS = ( ((ucell_t)M_POP) < ((ucell_t)TOS) ) ? FTRUE : FFALSE ;
612 case ID_COMP_ZERO_EQUAL:
613 TOS = ( TOS == 0 ) ? FTRUE : FFALSE ;
615 case ID_COMP_ZERO_NOT_EQUAL:
616 TOS = ( TOS != 0 ) ? FTRUE : FALSE ;
618 case ID_COMP_ZERO_GREATERTHAN:
619 TOS = ( TOS > 0 ) ? FTRUE : FFALSE ;
621 case ID_COMP_ZERO_LESSTHAN:
622 TOS = ( TOS < 0 ) ? FTRUE : FFALSE ;
635 #endif /* !PF_NO_SHELL */
639 /* Put address of body on stack. Insptr points after code start. */
640 TOS = (cell_t) ((char *)InsPtr - sizeof(cell_t) + CREATE_BODY_OFFSET );
643 case ID_CSTORE: /* ( c caddr -- ) */
644 *((uint8_t *) TOS) = (uint8_t) M_POP;
648 /* Double precision add. */
649 case ID_D_PLUS: /* D+ ( al ah bl bh -- sl sh ) */
651 register ucell_t ah,al,bl,sh,sl;
658 if( sl < bl ) sh = 1; /* Carry */
666 /* Double precision subtract. */
667 case ID_D_MINUS: /* D- ( al ah bl bh -- sl sh ) */
669 register ucell_t ah,al,bl,sh,sl;
676 if( al < bl ) sh = 1; /* Borrow */
684 /* Assume 8-bit char and calculate cell width. */
685 #define NBITS ((sizeof(ucell_t)) * 8)
686 /* Define half the number of bits in a cell. */
687 #define HNBITS (NBITS / 2)
688 /* Assume two-complement arithmetic to calculate lower half. */
689 #define LOWER_HALF(n) ((n) & (((ucell_t)1 << HNBITS) - 1))
690 #define HIGH_BIT ((ucell_t)1 << (NBITS - 1))
692 /* Perform cell*cell bit multiply for a 2 cell result, by factoring into half cell quantities.
693 * Using an improved algorithm suggested by Steve Green.
694 * Converted to 64-bit by Aleksej Saushev.
696 case ID_D_UMTIMES: /* UM* ( a b -- lo hi ) */
698 ucell_t ahi, alo, bhi, blo; /* input parts */
699 ucell_t lo, hi, temp;
700 /* Get values from stack. */
703 /* Break into hi and lo 16 bit parts. */
704 alo = LOWER_HALF(ahi);
706 blo = LOWER_HALF(bhi);
711 /* higher part: ahi * bhi */
713 /* middle (overlapping) part: ahi * blo */
715 lo += LOWER_HALF(temp);
716 hi += temp >> HNBITS;
717 /* middle (overlapping) part: alo * bhi */
719 lo += LOWER_HALF(temp);
720 hi += temp >> HNBITS;
721 /* lower part: alo * blo */
723 /* its higher half overlaps with middle's lower half: */
724 lo += temp >> HNBITS;
728 /* combine lower part of result: */
729 lo = (lo << HNBITS) + LOWER_HALF(temp);
736 /* Perform cell*cell bit multiply for 2 cell result, using shift and add. */
737 case ID_D_MTIMES: /* M* ( a b -- pl ph ) */
739 ucell_t ahi, alo, bhi, blo; /* input parts */
740 ucell_t lo, hi, temp;
742 /* Get values from stack. */
746 /* Calculate product sign: */
747 sg = ((cell_t)(ahi ^ bhi) < 0);
748 /* Take absolute values and reduce to um* */
749 if ((cell_t)ahi < 0) ahi = (ucell_t)(-(cell_t)ahi);
750 if ((cell_t)bhi < 0) bhi = (ucell_t)(-(cell_t)bhi);
752 /* Break into hi and lo 16 bit parts. */
753 alo = LOWER_HALF(ahi);
755 blo = LOWER_HALF(bhi);
760 /* higher part: ahi * bhi */
762 /* middle (overlapping) part: ahi * blo */
764 lo += LOWER_HALF(temp);
765 hi += temp >> HNBITS;
766 /* middle (overlapping) part: alo * bhi */
768 lo += LOWER_HALF(temp);
769 hi += temp >> HNBITS;
770 /* lower part: alo * blo */
772 /* its higher half overlaps with middle's lower half: */
773 lo += temp >> HNBITS;
777 /* combine lower part of result: */
778 lo = (lo << HNBITS) + LOWER_HALF(temp);
780 /* Negate product if one operand negative. */
783 /* lo = (ucell_t)(- lo); */
785 hi = ~hi + ((lo == 0) ? 1 : 0);
793 #define DULT(du1l,du1h,du2l,du2h) ( (du2h<du1h) ? FALSE : ( (du2h==du1h) ? (du1l<du2l) : TRUE) )
794 /* Perform 2 cell by 1 cell divide for 1 cell result and remainder, using shift and subtract. */
795 case ID_D_UMSMOD: /* UM/MOD ( al ah bdiv -- rem q ) */
797 ucell_t ah,al, q,di, bl,bh, sl,sh;
803 for( di=0; di<NBITS; di++ )
805 if( !DULT(al,ah,bl,bh) )
809 if( al < bl ) sh = 1; /* Borrow */
816 bl = (bl >> 1) | (bh << (NBITS-1));
819 if( !DULT(al,ah,bl,bh) )
825 M_PUSH( al ); /* rem */
830 /* Perform 2 cell by 1 cell divide for 2 cell result and remainder, using shift and subtract. */
831 case ID_D_MUSMOD: /* MU/MOD ( al am bdiv -- rem ql qh ) */
833 register ucell_t ah,am,al,ql,qh,di;
834 #define bdiv ((ucell_t)TOS)
839 for( di=0; di<2*NBITS; di++ )
846 qh = (qh << 1) | (ql >> (NBITS-1));
848 ah = (ah << 1) | (am >> (NBITS-1));
849 am = (am << 1) | (al >> (NBITS-1));
851 DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql ));
858 M_PUSH( ah ); /* rem */
869 #endif /* !PF_NO_SHELL */
876 TOS = gCurrentTask->td_StackBase - STKPTR;
879 case ID_DIVIDE: BINARY_OP( / ); endcase;
890 case ID_DROP: M_DROP; endcase;
894 DumpMemory( (char *) Scratch, TOS );
898 case ID_DUP: M_DUP; endcase;
900 case ID_DO_P: /* ( limit start -- ) ( R: -- start limit ) */
906 case ID_EOL: /* ( -- end_of_line_char ) */
911 case ID_ERRORQ_P: /* ( flag num -- , quit if flag true ) */
930 /* Save IP on return stack like a JSR. */
932 #ifdef PF_SUPPORT_TRACE
933 /* Bump level for trace. */
936 if( IsTokenPrimitive( TOS ) )
938 WRITE_CELL_DIC( (cell_t *) &FakeSecondary[0], TOS); /* Build a fake secondary and execute it. */
939 InsPtr = &FakeSecondary[0];
943 InsPtr = (cell_t *) LOCAL_CODEREL_TO_ABS(TOS);
949 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
952 TOS = (cell_t) READ_CELL_DIC((cell_t *)TOS);
956 TOS = *((cell_t *)TOS);
959 TOS = *((cell_t *)TOS);
963 case ID_FILE_CREATE: /* ( c-addr u fam -- fid ior ) */
964 /* Build NUL terminated name string. */
965 Scratch = M_POP; /* u */
966 Temp = M_POP; /* caddr */
967 if( Scratch < TIB_SIZE-2 )
969 const char *famText = pfSelectFileModeCreate( TOS );
970 pfCopyMemory( gScratch, (char *) Temp, (ucell_t) Scratch );
971 gScratch[Scratch] = '\0';
972 DBUG(("Create file = %s with famTxt %s\n", gScratch, famText ));
973 FileID = sdOpenFile( gScratch, famText );
974 TOS = ( FileID == NULL ) ? -1 : 0 ;
975 M_PUSH( (cell_t) FileID );
979 ERR("Filename too large for name buffer.\n");
985 case ID_FILE_DELETE: /* ( c-addr u -- ior ) */
986 /* Build NUL terminated name string. */
987 Temp = M_POP; /* caddr */
988 if( TOS < TIB_SIZE-2 )
990 pfCopyMemory( gScratch, (char *) Temp, (ucell_t) TOS );
991 gScratch[TOS] = '\0';
992 DBUG(("Delete file = %s\n", gScratch ));
993 TOS = sdDeleteFile( gScratch );
997 ERR("Filename too large for name buffer.\n");
1002 case ID_FILE_OPEN: /* ( c-addr u fam -- fid ior ) */
1003 /* Build NUL terminated name string. */
1004 Scratch = M_POP; /* u */
1005 Temp = M_POP; /* caddr */
1006 if( Scratch < TIB_SIZE-2 )
1008 const char *famText = pfSelectFileModeOpen( TOS );
1009 pfCopyMemory( gScratch, (char *) Temp, (ucell_t) Scratch );
1010 gScratch[Scratch] = '\0';
1011 DBUG(("Open file = %s\n", gScratch ));
1012 FileID = sdOpenFile( gScratch, famText );
1014 TOS = ( FileID == NULL ) ? -1 : 0 ;
1015 M_PUSH( (cell_t) FileID );
1019 ERR("Filename too large for name buffer.\n");
1025 case ID_FILE_CLOSE: /* ( fid -- ior ) */
1026 TOS = sdCloseFile( (FileStream *) TOS );
1029 case ID_FILE_READ: /* ( addr len fid -- u2 ior ) */
1030 FileID = (FileStream *) TOS;
1032 CharPtr = (char *) M_POP;
1033 Temp = sdReadFile( CharPtr, 1, Scratch, FileID );
1034 /* TODO check feof() or ferror() */
1039 /* TODO Why does this crash when passed an illegal FID? */
1040 case ID_FILE_SIZE: /* ( fid -- ud ior ) */
1041 /* Determine file size by seeking to end and returning position. */
1042 FileID = (FileStream *) TOS;
1044 file_offset_t endposition = -1;
1045 file_offset_t original = sdTellFile( FileID );
1048 sdSeekFile( FileID, 0, PF_SEEK_END );
1049 endposition = sdTellFile( FileID );
1050 /* Restore original position. */
1051 sdSeekFile( FileID, original, PF_SEEK_SET );
1053 if (endposition < 0)
1055 M_PUSH(0); /* low */
1056 M_PUSH(0); /* high */
1057 TOS = -4; /* TODO proper error number */
1061 M_PUSH(endposition); /* low */
1062 /* We do not support double precision file offsets.*/
1063 M_PUSH(0); /* high */
1069 case ID_FILE_WRITE: /* ( addr len fid -- ior ) */
1070 FileID = (FileStream *) TOS;
1072 CharPtr = (char *) M_POP;
1073 Temp = sdWriteFile( CharPtr, 1, Scratch, FileID );
1074 TOS = (Temp != Scratch) ? -3 : 0;
1077 case ID_FILE_REPOSITION: /* ( ud fid -- ior ) */
1079 file_offset_t offset;
1082 FileID = (FileStream *) TOS;
1085 /* We do not support double precision file offsets in pForth.
1086 * So check to make sure the high bits are not used.
1088 if (offsetHigh != 0)
1090 TOS = -3; /* TODO err num? */
1093 offset = (file_offset_t)offsetLow;
1094 TOS = sdSeekFile( FileID, offset, PF_SEEK_SET );
1098 case ID_FILE_POSITION: /* ( fid -- ud ior ) */
1100 file_offset_t position;
1101 FileID = (FileStream *) TOS;
1102 position = sdTellFile( FileID );
1105 M_PUSH(0); /* low */
1106 M_PUSH(0); /* high */
1107 TOS = -4; /* TODO proper error number */
1111 M_PUSH(position); /* low */
1112 /* We do not support double precision file offsets.*/
1113 M_PUSH(0); /* high */
1119 case ID_FILE_RO: /* ( -- fam ) */
1121 TOS = PF_FAM_READ_ONLY;
1124 case ID_FILE_RW: /* ( -- fam ) */
1126 TOS = PF_FAM_READ_WRITE;
1129 case ID_FILE_WO: /* ( -- fam ) */
1131 TOS = PF_FAM_WRITE_ONLY;
1134 case ID_FILE_BIN: /* ( -- fam ) */
1135 TOS = TOS | PF_FAM_BINARY_FLAG;
1138 case ID_FILE_FLUSH: /* ( fileid -- ior ) */
1140 FileStream *Stream = (FileStream *) TOS;
1141 TOS = (sdFlushFile( Stream ) == 0) ? 0 : THROW_FLUSH_FILE;
1145 case ID_FILE_RENAME: /* ( oldName newName -- ior ) */
1147 char *New = (char *) TOS;
1148 char *Old = (char *) M_POP;
1149 TOS = sdRenameFile( Old, New );
1153 case ID_FILE_RESIZE: /* ( ud fileid -- ior ) */
1155 FileStream *File = (FileStream *) TOS;
1156 ucell_t SizeHi = (ucell_t) M_POP;
1157 ucell_t SizeLo = (ucell_t) M_POP;
1158 TOS = ( UdIsUint64( SizeLo, SizeHi )
1159 ? sdResizeFile( File, UdToUint64( SizeLo, SizeHi ))
1160 : THROW_RESIZE_FILE );
1164 case ID_FILL: /* ( caddr num charval -- ) */
1166 register char *DstPtr;
1167 Temp = M_POP; /* num */
1168 DstPtr = (char *) M_POP; /* dst */
1169 for( Scratch=0; (ucell_t) Scratch < (ucell_t) Temp ; Scratch++ )
1171 *DstPtr++ = (char) TOS;
1178 case ID_FIND: /* ( $addr -- $addr 0 | xt +-1 ) */
1179 TOS = ffFind( (char *) TOS, (ExecToken *) &Temp );
1184 TOS = ffFindNFA( (const ForthString *) TOS, (const ForthString **) &Temp );
1185 M_PUSH( (cell_t) Temp );
1187 #endif /* !PF_NO_SHELL */
1193 /* Validate memory before freeing. Clobber validator and first word. */
1194 case ID_FREE: /* ( addr -- result ) */
1197 ERR("FREE passed NULL!\n");
1198 TOS = -2; /* FIXME error code */
1202 CellPtr = (cell_t *) TOS;
1204 if( ((ucell_t)*CellPtr) != ((ucell_t)CellPtr ^ PF_MEMORY_VALIDATOR))
1206 TOS = -2; /* FIXME error code */
1210 CellPtr[0] = 0xDeadBeef;
1211 pfFreeMem((char *)CellPtr);
1217 #include "pfinnrfp.h"
1221 TOS = (cell_t)CODE_HERE;
1224 case ID_NUMBERQ_P: /* ( addr -- 0 | n 1 ) */
1225 /* Convert using number converter in 'C'.
1226 ** Only supports single precision for bootstrap.
1228 TOS = (cell_t) ffNumberQ( (char *) TOS, &Temp );
1229 if( TOS == NUM_TYPE_SINGLE)
1231 M_PUSH( Temp ); /* Push single number */
1235 case ID_I: /* ( -- i , DO LOOP index ) */
1241 case ID_INCLUDE_FILE:
1242 FileID = (FileStream *) TOS;
1243 M_DROP; /* Drop now so that INCLUDE has a clean stack. */
1245 Scratch = ffIncludeFile( FileID );
1247 if( Scratch ) M_THROW(Scratch)
1249 #endif /* !PF_NO_SHELL */
1254 Scratch = ffInterpret();
1256 if( Scratch ) M_THROW(Scratch)
1258 #endif /* !PF_NO_SHELL */
1260 case ID_J: /* ( -- j , second DO LOOP index ) */
1268 if (TOS == ASCII_EOT) {
1278 #endif /* !PF_NO_SHELL */
1281 DBUG(("ID_LITERAL_P: InsPtr = 0x%x, *InsPtr = 0x%x\n", InsPtr, *InsPtr ));
1283 TOS = READ_CELL_DIC(InsPtr++);
1287 case ID_LOCAL_COMPILER: DO_VAR(gLocalCompiler_XT); endcase;
1288 #endif /* !PF_NO_SHELL */
1290 case ID_LOCAL_FETCH: /* ( i <local> -- n , fetch from local ) */
1291 TOS = *(LocalsPtr - TOS);
1294 #define LOCAL_FETCH_N(num) \
1295 case ID_LOCAL_FETCH_##num: /* ( <local> -- n , fetch from local ) */ \
1297 TOS = *(LocalsPtr -(num)); \
1309 case ID_LOCAL_STORE: /* ( n i <local> -- , store n in local ) */
1310 *(LocalsPtr - TOS) = M_POP;
1314 #define LOCAL_STORE_N(num) \
1315 case ID_LOCAL_STORE_##num: /* ( n <local> -- , store n in local ) */ \
1316 *(LocalsPtr - (num)) = TOS; \
1329 case ID_LOCAL_PLUSSTORE: /* ( n i <local> -- , add n to local ) */
1330 *(LocalsPtr - TOS) += M_POP;
1334 case ID_LOCAL_ENTRY: /* ( x0 x1 ... xn n -- ) */
1335 /* create local stack frame */
1339 DBUG(("LocalEntry: n = %d\n", TOS));
1340 /* End of locals. Create stack frame */
1341 DBUG(("LocalEntry: before RP@ = 0x%x, LP = 0x%x\n",
1342 TORPTR, LocalsPtr));
1343 M_R_PUSH(LocalsPtr);
1346 DBUG(("LocalEntry: after RP@ = 0x%x, LP = 0x%x\n",
1347 TORPTR, LocalsPtr));
1351 *lp++ = M_POP; /* Load local vars from stack */
1357 case ID_LOCAL_EXIT: /* cleanup up local stack frame */
1358 DBUG(("LocalExit: before RP@ = 0x%x, LP = 0x%x\n",
1359 TORPTR, LocalsPtr));
1361 LocalsPtr = (cell_t *) M_R_POP;
1362 DBUG(("LocalExit: after RP@ = 0x%x, LP = 0x%x\n",
1363 TORPTR, LocalsPtr));
1368 MSG("Load "); MSG(SYSTEM_LOAD_FILE); EMIT_CR;
1369 FileID = sdOpenFile(SYSTEM_LOAD_FILE, "r");
1373 Scratch = ffIncludeFile( FileID ); /* Also closes the file. */
1375 if( Scratch ) M_THROW(Scratch);
1379 ERR(SYSTEM_LOAD_FILE); ERR(" could not be opened!\n");
1382 #endif /* !PF_NO_SHELL */
1384 case ID_LEAVE_P: /* ( R: index limit -- ) */
1390 case ID_LOOP_P: /* ( R: index limit -- | index limit ) */
1391 Temp = M_R_POP; /* limit */
1392 Scratch = M_R_POP + 1; /* index */
1393 if( Scratch == Temp )
1395 InsPtr++; /* skip branch offset, exit loop */
1399 /* Push index and limit back to R */
1400 M_R_PUSH( Scratch );
1402 /* Branch back to just after (DO) */
1407 case ID_LSHIFT: BINARY_OP( << ); endcase;
1411 TOS = ( TOS > Scratch ) ? TOS : Scratch ;
1416 TOS = ( TOS < Scratch ) ? TOS : Scratch ;
1419 case ID_MINUS: BINARY_OP( - ); endcase;
1422 case ID_NAME_TO_TOKEN:
1423 TOS = (cell_t) NameToToken((ForthString *)TOS);
1426 case ID_NAME_TO_PREVIOUS:
1427 TOS = (cell_t) NameToPrevious((ForthString *)TOS);
1434 case ID_OR: BINARY_OP( | ); endcase;
1441 case ID_PICK: /* ( ... n -- sp(n) ) */
1445 case ID_PLUS: BINARY_OP( + ); endcase;
1447 case ID_PLUS_STORE: /* ( n addr -- , add n to *addr ) */
1448 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1449 if( IN_DICS( TOS ) )
1451 Scratch = READ_CELL_DIC((cell_t *)TOS);
1453 WRITE_CELL_DIC((cell_t *)TOS,Scratch);
1457 *((cell_t *)TOS) += M_POP;
1460 *((cell_t *)TOS) += M_POP;
1465 case ID_PLUSLOOP_P: /* ( delta -- ) ( R: index limit -- | index limit ) */
1467 cell_t Limit = M_R_POP;
1468 cell_t OldIndex = M_R_POP;
1469 cell_t Delta = TOS; /* add TOS to index, not 1 */
1470 cell_t NewIndex = OldIndex + Delta;
1471 cell_t OldDiff = OldIndex - Limit;
1473 /* This exploits this idea (lifted from Gforth):
1474 (x^y)<0 is equivalent to (x<0) != (y<0) */
1475 if( ((OldDiff ^ (OldDiff + Delta)) /* is the limit crossed? */
1476 & (OldDiff ^ Delta)) /* is it a wrap-around? */
1479 InsPtr++; /* skip branch offset, exit loop */
1483 /* Push index and limit back to R */
1484 M_R_PUSH( NewIndex );
1486 /* Branch back to just after (DO) */
1493 case ID_QDO_P: /* (?DO) ( limit start -- ) ( R: -- start limit ) */
1494 Scratch = M_POP; /* limit */
1495 if( Scratch == TOS )
1497 /* Branch to just after (LOOP) */
1503 M_R_PUSH( Scratch );
1504 InsPtr++; /* skip branch offset, enter loop */
1509 case ID_QDUP: if( TOS ) M_DUP; endcase;
1511 case ID_QTERMINAL: /* WARNING: Typically not fully implemented! */
1513 TOS = sdQueryTerminal();
1516 case ID_QUIT_P: /* Stop inner interpreter, go back to user. */
1517 #ifdef PF_SUPPORT_TRACE
1520 M_THROW(THROW_QUIT);
1539 TOS = (ffRefill() > 0) ? FTRUE : FFALSE;
1542 /* Resize memory allocated by ALLOCATE. */
1543 case ID_RESIZE: /* ( addr1 u -- addr2 result ) */
1545 cell_t *Addr1 = (cell_t *) M_POP;
1546 /* Point to validator below users address. */
1547 cell_t *FreePtr = Addr1 - 1;
1548 if( ((ucell_t)*FreePtr) != ((ucell_t)FreePtr ^ PF_MEMORY_VALIDATOR))
1550 /* 090218 - Fixed bug, was returning zero. */
1556 /* Try to allocate. */
1557 CellPtr = (cell_t *) pfAllocMem( TOS + sizeof(cell_t) );
1560 /* Copy memory including validation. */
1561 pfCopyMemory( (char *) CellPtr, (char *) FreePtr, TOS + sizeof(cell_t) );
1562 *CellPtr = (cell_t)(((ucell_t)CellPtr) ^ (ucell_t)PF_MEMORY_VALIDATOR);
1563 /* 090218 - Fixed bug that was incrementing the address twice. Thanks Reinhold Straub. */
1564 /* Increment past validator to user address. */
1565 M_PUSH( (cell_t) (CellPtr + 1) );
1566 TOS = 0; /* Result code. */
1567 /* Mark old cell as dead so we can't free it twice. */
1568 FreePtr[0] = 0xDeadBeef;
1569 pfFreeMem((char *) FreePtr);
1573 /* 090218 - Fixed bug, was returning zero. */
1575 TOS = -4; /* FIXME Fix error code. */
1582 ** RP@ and RP! are called secondaries so we must
1583 ** account for the return address pushed before calling.
1585 case ID_RP_FETCH: /* ( -- rp , address of top of return stack ) */
1587 TOS = (cell_t)TORPTR; /* value before calling RP@ */
1590 case ID_RP_STORE: /* ( rp -- , address of top of return stack ) */
1591 TORPTR = (cell_t *) TOS;
1595 case ID_ROLL: /* ( xu xu-1 xu-1 ... x0 u -- xu-1 xu-1 ... x0 xu ) */
1598 cell_t *srcPtr, *dstPtr;
1599 Scratch = M_STACK(TOS);
1600 srcPtr = &M_STACK(TOS-1);
1601 dstPtr = &M_STACK(TOS);
1602 for( ri=0; ri<TOS; ri++ )
1604 *dstPtr-- = *srcPtr--;
1611 case ID_ROT: /* ( a b c -- b c a ) */
1612 Scratch = M_POP; /* b */
1613 Temp = M_POP; /* a */
1614 M_PUSH( Scratch ); /* b */
1619 /* Logical right shift */
1620 case ID_RSHIFT: { TOS = ((ucell_t)M_POP) >> TOS; } endcase;
1623 case ID_SAVE_FORTH_P: /* ( $name Entry NameSize CodeSize -- err ) */
1625 cell_t NameSize, CodeSize, EntryPoint;
1629 ForthStringToC( gScratch, (char *) M_POP, sizeof(gScratch) );
1630 TOS = ffSaveForth( gScratch, EntryPoint, NameSize, CodeSize );
1635 case ID_SP_FETCH: /* ( -- sp , address of top of stack, sorta ) */
1637 TOS = (cell_t)STKPTR;
1640 case ID_SP_STORE: /* ( sp -- , address of top of stack, sorta ) */
1641 STKPTR = (cell_t *) TOS;
1645 case ID_STORE: /* ( n addr -- , write n to addr ) */
1646 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1647 if( IN_DICS( TOS ) )
1649 WRITE_CELL_DIC((cell_t *)TOS,M_POP);
1653 *((cell_t *)TOS) = M_POP;
1656 *((cell_t *)TOS) = M_POP;
1661 case ID_SCAN: /* ( addr cnt char -- addr' cnt' ) */
1662 Scratch = M_POP; /* cnt */
1663 Temp = M_POP; /* addr */
1664 TOS = ffScan( (char *) Temp, Scratch, (char) TOS, &CharPtr );
1665 M_PUSH((cell_t) CharPtr);
1671 Scratch = ffSemiColon();
1673 if( Scratch ) M_THROW( Scratch );
1675 #endif /* !PF_NO_SHELL */
1677 case ID_SKIP: /* ( addr cnt char -- addr' cnt' ) */
1678 Scratch = M_POP; /* cnt */
1679 Temp = M_POP; /* addr */
1680 TOS = ffSkip( (char *) Temp, Scratch, (char) TOS, &CharPtr );
1681 M_PUSH((cell_t) CharPtr);
1684 case ID_SOURCE: /* ( -- c-addr num ) */
1686 M_PUSH( (cell_t) gCurrentTask->td_SourcePtr );
1687 TOS = (cell_t) gCurrentTask->td_SourceNum;
1690 case ID_SOURCE_SET: /* ( c-addr num -- ) */
1691 gCurrentTask->td_SourcePtr = (char *) M_POP;
1692 gCurrentTask->td_SourceNum = TOS;
1698 TOS = ffConvertStreamToSourceID( gCurrentTask->td_InputStream ) ;
1701 case ID_SOURCE_ID_POP:
1703 TOS = ffConvertStreamToSourceID( ffPopInputStream() ) ;
1706 case ID_SOURCE_ID_PUSH: /* ( source-id -- ) */
1707 TOS = (cell_t)ffConvertSourceIDToStream( TOS );
1708 Scratch = ffPushInputStream((FileStream *) TOS );
1716 case ID_SOURCE_LINE_NUMBER_FETCH: /* ( -- linenr ) */
1718 TOS = gCurrentTask->td_LineNumber;
1721 case ID_SOURCE_LINE_NUMBER_STORE: /* ( linenr -- ) */
1722 gCurrentTask->td_LineNumber = TOS;
1742 case ID_THROW: /* ( k*x err -- k*x | i*x err , jump to where CATCH was called ) */
1753 CharPtr = (char *) ffWord( (char) ' ' );
1754 TOS = ffFind( CharPtr, (ExecToken *) &Temp );
1757 ERR("' could not find ");
1758 ioType( (char *) CharPtr+1, *CharPtr );
1766 #endif /* !PF_NO_SHELL */
1768 case ID_TIMES: BINARY_OP( * ); endcase;
1771 Scratch = M_POP; /* addr */
1772 ioType( (char *) Scratch, TOS );
1781 case ID_VAR_BASE: DO_VAR(gVarBase); endcase;
1782 case ID_VAR_CODE_BASE: DO_VAR(gCurrentDictionary->dic_CodeBase); endcase;
1783 case ID_VAR_CODE_LIMIT: DO_VAR(gCurrentDictionary->dic_CodeLimit); endcase;
1784 case ID_VAR_CONTEXT: DO_VAR(gVarContext); endcase;
1785 case ID_VAR_DP: DO_VAR(gCurrentDictionary->dic_CodePtr.Cell); endcase;
1786 case ID_VAR_ECHO: DO_VAR(gVarEcho); endcase;
1787 case ID_VAR_HEADERS_BASE: DO_VAR(gCurrentDictionary->dic_HeaderBase); endcase;
1788 case ID_VAR_HEADERS_LIMIT: DO_VAR(gCurrentDictionary->dic_HeaderLimit); endcase;
1789 case ID_VAR_HEADERS_PTR: DO_VAR(gCurrentDictionary->dic_HeaderPtr); endcase;
1790 case ID_VAR_NUM_TIB: DO_VAR(gCurrentTask->td_SourceNum); endcase;
1791 case ID_VAR_OUT: DO_VAR(gCurrentTask->td_OUT); endcase;
1792 case ID_VAR_STATE: DO_VAR(gVarState); endcase;
1793 case ID_VAR_TO_IN: DO_VAR(gCurrentTask->td_IN); endcase;
1794 case ID_VAR_TRACE_FLAGS: DO_VAR(gVarTraceFlags); endcase;
1795 case ID_VAR_TRACE_LEVEL: DO_VAR(gVarTraceLevel); endcase;
1796 case ID_VAR_TRACE_STACK: DO_VAR(gVarTraceStack); endcase;
1797 case ID_VAR_RETURN_CODE: DO_VAR(gVarReturnCode); endcase;
1800 TOS = (cell_t) ffWord( (char) TOS );
1803 case ID_WORD_FETCH: /* ( waddr -- w ) */
1804 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1805 if( IN_DICS( TOS ) )
1807 TOS = (uint16_t) READ_SHORT_DIC((uint16_t *)TOS);
1811 TOS = *((uint16_t *)TOS);
1814 TOS = *((uint16_t *)TOS);
1818 case ID_WORD_STORE: /* ( w waddr -- ) */
1820 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1821 if( IN_DICS( TOS ) )
1823 WRITE_SHORT_DIC((uint16_t *)TOS,(uint16_t)M_POP);
1827 *((uint16_t *)TOS) = (uint16_t) M_POP;
1830 *((uint16_t *)TOS) = (uint16_t) M_POP;
1835 case ID_XOR: BINARY_OP( ^ ); endcase;
1838 /* Branch is followed by an offset relative to address of offset. */
1839 case ID_ZERO_BRANCH:
1840 DBUGX(("Before 0Branch: IP = 0x%x\n", InsPtr ));
1847 InsPtr++; /* skip over offset */
1850 DBUGX(("After 0Branch: IP = 0x%x\n", InsPtr ));
1854 ERR("pfCatch: Unrecognised token = 0x");
1857 ffDotHex((cell_t) InsPtr);
1863 if(InsPtr) Token = READ_CELL_DIC(InsPtr++); /* Traverse to next token in secondary. */
1870 if( _CrtCheckMemory() == 0 )
1872 ERR("_CrtCheckMemory abort: InsPtr = 0x");
1873 ffDotHex((int)InsPtr);
1878 } while( (InitialReturnStack - TORPTR) > 0 );
1882 return ExceptionReturnCode;