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 ***************************************************************/
30 #include <sys/types.h>
37 #if defined(WIN32) && !defined(__MINGW32__)
41 #define SYSTEM_LOAD_FILE "system.fth"
43 /***************************************************************
44 ** Macros for data stack access.
45 ** TOS is cached in a register in pfCatch.
46 ***************************************************************/
48 #define STKPTR (DataStackPtr)
49 #define M_POP (*(STKPTR++))
50 #define M_PUSH(n) {*(--(STKPTR)) = (cell_t) (n);}
51 #define M_STACK(n) (STKPTR[n])
53 #define TOS (TopOfStack)
54 #define PUSH_TOS M_PUSH(TOS)
55 #define M_DUP PUSH_TOS;
56 #define M_DROP { TOS = M_POP; }
59 /***************************************************************
60 ** Macros for Floating Point stack access.
61 ***************************************************************/
63 #define FP_STKPTR (FloatStackPtr)
64 #define M_FP_SPZERO (gCurrentTask->td_FloatStackBase)
65 #define M_FP_POP (*(FP_STKPTR++))
66 #define M_FP_PUSH(n) {*(--(FP_STKPTR)) = (PF_FLOAT) (n);}
67 #define M_FP_STACK(n) (FP_STKPTR[n])
69 #define FP_TOS (fpTopOfStack)
70 #define PUSH_FP_TOS M_FP_PUSH(FP_TOS)
71 #define M_FP_DUP PUSH_FP_TOS;
72 #define M_FP_DROP { FP_TOS = M_FP_POP; }
75 /***************************************************************
76 ** Macros for return stack access.
77 ***************************************************************/
79 #define TORPTR (ReturnStackPtr)
80 #define M_R_DROP {TORPTR++;}
81 #define M_R_POP (*(TORPTR++))
82 #define M_R_PICK(n) (TORPTR[n])
83 #define M_R_PUSH(n) {*(--(TORPTR)) = (cell_t) (n);}
85 /***************************************************************
87 ***************************************************************/
89 #define M_BRANCH { InsPtr = (cell_t *) (((uint8_t *) InsPtr) + READ_CELL_DIC(InsPtr)); }
91 /* Cache top of data stack like in JForth. */
93 #define LOAD_REGISTERS \
95 STKPTR = gCurrentTask->td_StackPtr; \
97 FP_STKPTR = gCurrentTask->td_FloatStackPtr; \
99 TORPTR = gCurrentTask->td_ReturnPtr; \
102 #define SAVE_REGISTERS \
104 gCurrentTask->td_ReturnPtr = TORPTR; \
106 gCurrentTask->td_StackPtr = STKPTR; \
107 M_FP_PUSH( FP_TOS ); \
108 gCurrentTask->td_FloatStackPtr = FP_STKPTR; \
112 /* Cache top of data stack like in JForth. */
113 #define LOAD_REGISTERS \
115 STKPTR = gCurrentTask->td_StackPtr; \
117 TORPTR = gCurrentTask->td_ReturnPtr; \
120 #define SAVE_REGISTERS \
122 gCurrentTask->td_ReturnPtr = TORPTR; \
124 gCurrentTask->td_StackPtr = STKPTR; \
133 #define DO_VAR(varname) { PUSH_TOS; TOS = (cell_t) &varname; }
136 #define M_THROW(err) \
138 ExceptionReturnCode = (ThrowCode)(err); \
139 TORPTR = InitialReturnStack; /* Will cause return to 'C' */ \
140 STKPTR = InitialDataStack; \
141 FP_STKPTR = InitialFloatStack; \
144 #define M_THROW(err) \
146 ExceptionReturnCode = (err); \
147 TORPTR = InitialReturnStack; /* Will cause return to 'C' */ \
148 STKPTR = InitialDataStack; \
152 /***************************************************************
154 ***************************************************************/
156 #define BINARY_OP( op ) { TOS = M_POP op TOS; }
157 #define endcase break
159 #if defined(PF_NO_SHELL) || !defined(PF_SUPPORT_TRACE)
160 #define TRACENAMES /* no names */
162 /* Display name of executing routine. */
163 static void TraceNames( ExecToken Token, cell_t Level )
168 if( ffTokenToName( Token, &DebugName ) )
171 if( gCurrentTask->td_OUT > 0 ) EMIT_CR;
173 for( i=0; i<Level; i++ )
177 TypeName( DebugName );
178 /* Space out to column N then .S */
179 NumSpaces = 30 - gCurrentTask->td_OUT;
180 for( i=0; i < NumSpaces; i++ )
185 /* No longer needed? gCurrentTask->td_OUT = 0; */ /* !!! Hack for ffDotS() */
190 MSG_NUM_H("Couldn't find Name for ", Token);
195 if( (gVarTraceLevel > Level) ) \
196 { SAVE_REGISTERS; TraceNames( Token, Level ); LOAD_REGISTERS; }
197 #endif /* PF_NO_SHELL */
199 /* Use local copy of CODE_BASE for speed. */
200 #define LOCAL_CODEREL_TO_ABS( a ) ((cell_t *) (((cell_t) a) + CodeBase))
202 /* Truncate the unsigned double cell integer LO/HI to an uint64_t. */
203 static uint64_t UdToUint64( ucell_t Lo, ucell_t Hi )
205 return (( 2 * sizeof(ucell_t) == sizeof(uint64_t) )
206 ? (((uint64_t)Lo) | (((uint64_t)Hi) >> (sizeof(ucell_t) * 8)))
210 /* Return TRUE if the unsigned double cell integer LO/HI is not greater
211 * then the greatest uint64_t.
213 static int UdIsUint64( ucell_t Lo, ucell_t Hi )
215 return (( 2 * sizeof(ucell_t) == sizeof(uint64_t) )
220 static const char *pfSelectFileModeCreate( int fam );
221 static const char *pfSelectFileModeOpen( int fam );
223 /**************************************************************/
224 static const char *pfSelectFileModeCreate( int fam )
226 const char *famText = NULL;
229 case (PF_FAM_WRITE_ONLY + PF_FAM_BINARY_FLAG):
230 famText = PF_FAM_BIN_CREATE_WO;
232 case (PF_FAM_READ_WRITE + PF_FAM_BINARY_FLAG):
233 famText = PF_FAM_BIN_CREATE_RW;
235 case PF_FAM_WRITE_ONLY:
236 famText = PF_FAM_CREATE_WO;
238 case PF_FAM_READ_WRITE:
239 famText = PF_FAM_CREATE_RW;
248 /**************************************************************/
249 static const char *pfSelectFileModeOpen( int fam )
251 const char *famText = NULL;
254 case (PF_FAM_READ_ONLY + PF_FAM_BINARY_FLAG):
255 famText = PF_FAM_BIN_OPEN_RO;
257 case (PF_FAM_WRITE_ONLY + PF_FAM_BINARY_FLAG):
258 famText = PF_FAM_BIN_CREATE_WO;
260 case (PF_FAM_READ_WRITE + PF_FAM_BINARY_FLAG):
261 famText = PF_FAM_BIN_OPEN_RW;
263 case PF_FAM_READ_ONLY:
264 famText = PF_FAM_OPEN_RO;
266 case PF_FAM_WRITE_ONLY:
267 famText = PF_FAM_CREATE_WO;
269 case PF_FAM_READ_WRITE:
271 famText = PF_FAM_OPEN_RW;
277 /**************************************************************/
278 int pfCatch( ExecToken XT )
280 register cell_t TopOfStack; /* Cache for faster execution. */
281 register cell_t *DataStackPtr;
282 register cell_t *ReturnStackPtr;
283 register cell_t *InsPtr = NULL;
284 register cell_t Token;
288 PF_FLOAT fpTopOfStack;
289 PF_FLOAT *FloatStackPtr;
292 PF_FLOAT *InitialFloatStack;
294 #ifdef PF_SUPPORT_TRACE
297 cell_t *LocalsPtr = NULL;
299 cell_t *InitialReturnStack;
300 cell_t *InitialDataStack;
301 cell_t FakeSecondary[2];
305 uint8_t *CodeBase = (uint8_t *) CODE_BASE;
306 ThrowCode ExceptionReturnCode = 0;
309 gExecutionDepth += 1;
310 PRT(("pfCatch( 0x%x ), depth = %d\n", XT, gExecutionDepth ));
314 ** Initialize FakeSecondary this way to avoid having stuff in the data section,
315 ** which is not supported for some embedded system loaders.
317 FakeSecondary[0] = 0;
318 FakeSecondary[1] = ID_EXIT; /* For EXECUTE */
320 /* Move data from task structure to registers for speed. */
323 /* Save initial stack depths for THROW */
324 InitialReturnStack = TORPTR;
325 InitialDataStack = STKPTR ;
327 InitialFloatStack = FP_STKPTR;
334 DBUG(("pfCatch: Token = 0x%x\n", Token ));
336 /* --------------------------------------------------------------- */
337 /* If secondary, thread down code tree until we hit a primitive. */
338 while( !IsTokenPrimitive( Token ) )
340 #ifdef PF_SUPPORT_TRACE
341 if((gVarTraceFlags & TRACE_INNER) )
343 MSG("pfCatch: Secondary Token = 0x");
345 MSG_NUM_H(", InsPtr = 0x", InsPtr);
350 /* Save IP on return stack like a JSR. */
353 /* Convert execution token to absolute address. */
354 InsPtr = (cell_t *) ( LOCAL_CODEREL_TO_ABS(Token) );
356 /* Fetch token at IP. */
357 Token = READ_CELL_DIC(InsPtr++);
359 #ifdef PF_SUPPORT_TRACE
360 /* Bump level for trace display */
366 #ifdef PF_SUPPORT_TRACE
370 /* Execute primitive Token. */
374 /* Pop up a level in Forth inner interpreter.
375 ** Used to implement semicolon.
376 ** Put first in switch because ID_EXIT==0 */
378 InsPtr = ( cell_t *) M_R_POP;
379 #ifdef PF_SUPPORT_TRACE
384 case ID_1MINUS: TOS--; endcase;
386 case ID_1PLUS: TOS++; endcase;
390 ff2Literal( TOS, M_POP );
393 #endif /* !PF_NO_SHELL */
396 /* hi part stored first, put on top of stack */
398 TOS = READ_CELL_DIC(InsPtr++);
399 M_PUSH(READ_CELL_DIC(InsPtr++));
402 case ID_2MINUS: TOS -= 2; endcase;
404 case ID_2PLUS: TOS += 2; endcase;
407 case ID_2OVER: /* ( a b c d -- a b c d a b ) */
409 Scratch = M_STACK(3);
414 case ID_2SWAP: /* ( a b c d -- c d a b ) */
415 Scratch = M_STACK(0); /* c */
416 M_STACK(0) = M_STACK(2); /* a */
417 M_STACK(2) = Scratch; /* c */
418 Scratch = TOS; /* d */
419 TOS = M_STACK(1); /* b */
420 M_STACK(1) = Scratch; /* d */
423 case ID_2DUP: /* ( a b -- a b a b ) */
425 Scratch = M_STACK(1);
431 M_PUSH( (*(TORPTR+1)) );
447 case ID_ACCEPT_P: /* ( c-addr +n1 -- +n2 ) */
448 CharPtr = (char *) M_POP;
449 TOS = ioAccept( CharPtr, TOS );
454 ffALiteral( ABS_TO_CODEREL(TOS) );
457 #endif /* !PF_NO_SHELL */
461 TOS = (cell_t) LOCAL_CODEREL_TO_ABS( READ_CELL_DIC(InsPtr++) );
464 /* Allocate some extra and put validation identifier at base */
465 #define PF_MEMORY_VALIDATOR (0xA81B4D69)
467 /* Allocate at least one cell's worth because we clobber first cell. */
468 if ( TOS < sizeof(cell_t) )
470 Temp = sizeof(cell_t);
476 /* Allocate extra cells worth because we store validation info. */
477 CellPtr = (cell_t *) pfAllocMem( Temp + sizeof(cell_t) );
480 /* This was broken into two steps because different compilers incremented
481 ** CellPtr before or after the XOR step. */
482 Temp = (cell_t)CellPtr ^ PF_MEMORY_VALIDATOR;
484 M_PUSH( (cell_t) CellPtr );
490 TOS = -1; /* FIXME Fix error code. */
494 case ID_AND: BINARY_OP( & ); endcase;
496 case ID_ARSHIFT: BINARY_OP( >> ); endcase; /* Arithmetic right shift */
500 TOS = CREATE_BODY_OFFSET;
503 /* Branch is followed by an offset relative to address of offset. */
505 DBUGX(("Before Branch: IP = 0x%x\n", InsPtr ));
507 DBUGX(("After Branch: IP = 0x%x\n", InsPtr ));
511 M_THROW( THROW_BYE );
515 MSG("Emergency exit.\n");
523 Scratch = pfCatch( Scratch );
531 Scratch = READ_CELL_DIC(InsPtr++);
532 CallUserFunction( Scratch & 0xFFFF,
534 (Scratch >> 24) & 0x7F );
538 /* Support 32/64 bit operation. */
541 TOS = sizeof(cell_t);
545 TOS = TOS * sizeof(cell_t);
548 case ID_CFETCH: TOS = *((uint8_t *) TOS); endcase;
550 case ID_CMOVE: /* ( src dst n -- ) */
552 register char *DstPtr = (char *) M_POP; /* dst */
553 CharPtr = (char *) M_POP; /* src */
554 for( Scratch=0; (ucell_t) Scratch < (ucell_t) TOS ; Scratch++ )
556 *DstPtr++ = *CharPtr++;
562 case ID_CMOVE_UP: /* ( src dst n -- ) */
564 register char *DstPtr = ((char *) M_POP) + TOS; /* dst */
565 CharPtr = ((char *) M_POP) + TOS;; /* src */
566 for( Scratch=0; (ucell_t) Scratch < (ucell_t) TOS ; Scratch++ )
568 *(--DstPtr) = *(--CharPtr);
580 case ID_COLON_P: /* ( $name xt -- ) */
581 CreateDicEntry( TOS, (char *) M_POP, 0 );
584 #endif /* !PF_NO_SHELL */
590 s2 = (const char *) M_POP;
592 s1 = (const char *) M_POP;
593 TOS = ffCompare( s1, len1, s2, TOS );
597 /* ( a b -- flag , Comparisons ) */
599 TOS = ( TOS == M_POP ) ? FTRUE : FFALSE ;
601 case ID_COMP_NOT_EQUAL:
602 TOS = ( TOS != M_POP ) ? FTRUE : FFALSE ;
604 case ID_COMP_GREATERTHAN:
605 TOS = ( M_POP > TOS ) ? FTRUE : FFALSE ;
607 case ID_COMP_LESSTHAN:
608 TOS = ( M_POP < TOS ) ? FTRUE : FFALSE ;
610 case ID_COMP_U_GREATERTHAN:
611 TOS = ( ((ucell_t)M_POP) > ((ucell_t)TOS) ) ? FTRUE : FFALSE ;
613 case ID_COMP_U_LESSTHAN:
614 TOS = ( ((ucell_t)M_POP) < ((ucell_t)TOS) ) ? FTRUE : FFALSE ;
616 case ID_COMP_ZERO_EQUAL:
617 TOS = ( TOS == 0 ) ? FTRUE : FFALSE ;
619 case ID_COMP_ZERO_NOT_EQUAL:
620 TOS = ( TOS != 0 ) ? FTRUE : FALSE ;
622 case ID_COMP_ZERO_GREATERTHAN:
623 TOS = ( TOS > 0 ) ? FTRUE : FFALSE ;
625 case ID_COMP_ZERO_LESSTHAN:
626 TOS = ( TOS < 0 ) ? FTRUE : FFALSE ;
639 #endif /* !PF_NO_SHELL */
643 /* Put address of body on stack. Insptr points after code start. */
644 TOS = (cell_t) ((char *)InsPtr - sizeof(cell_t) + CREATE_BODY_OFFSET );
647 case ID_CSTORE: /* ( c caddr -- ) */
648 *((uint8_t *) TOS) = (uint8_t) M_POP;
652 /* Double precision add. */
653 case ID_D_PLUS: /* D+ ( al ah bl bh -- sl sh ) */
655 register ucell_t ah,al,bl,sh,sl;
662 if( sl < bl ) sh = 1; /* Carry */
670 /* Double precision subtract. */
671 case ID_D_MINUS: /* D- ( al ah bl bh -- sl sh ) */
673 register ucell_t ah,al,bl,sh,sl;
680 if( al < bl ) sh = 1; /* Borrow */
688 /* Assume 8-bit char and calculate cell width. */
689 #define NBITS ((sizeof(ucell_t)) * 8)
690 /* Define half the number of bits in a cell. */
691 #define HNBITS (NBITS / 2)
692 /* Assume two-complement arithmetic to calculate lower half. */
693 #define LOWER_HALF(n) ((n) & (((ucell_t)1 << HNBITS) - 1))
694 #define HIGH_BIT ((ucell_t)1 << (NBITS - 1))
696 /* Perform cell*cell bit multiply for a 2 cell result, by factoring into half cell quantities.
697 * Using an improved algorithm suggested by Steve Green.
698 * Converted to 64-bit by Aleksej Saushev.
700 case ID_D_UMTIMES: /* UM* ( a b -- lo hi ) */
702 ucell_t ahi, alo, bhi, blo; /* input parts */
703 ucell_t lo, hi, temp;
704 /* Get values from stack. */
707 /* Break into hi and lo 16 bit parts. */
708 alo = LOWER_HALF(ahi);
710 blo = LOWER_HALF(bhi);
715 /* higher part: ahi * bhi */
717 /* middle (overlapping) part: ahi * blo */
719 lo += LOWER_HALF(temp);
720 hi += temp >> HNBITS;
721 /* middle (overlapping) part: alo * bhi */
723 lo += LOWER_HALF(temp);
724 hi += temp >> HNBITS;
725 /* lower part: alo * blo */
727 /* its higher half overlaps with middle's lower half: */
728 lo += temp >> HNBITS;
732 /* combine lower part of result: */
733 lo = (lo << HNBITS) + LOWER_HALF(temp);
740 /* Perform cell*cell bit multiply for 2 cell result, using shift and add. */
741 case ID_D_MTIMES: /* M* ( a b -- pl ph ) */
743 ucell_t ahi, alo, bhi, blo; /* input parts */
744 ucell_t lo, hi, temp;
746 /* Get values from stack. */
750 /* Calculate product sign: */
751 sg = ((cell_t)(ahi ^ bhi) < 0);
752 /* Take absolute values and reduce to um* */
753 if ((cell_t)ahi < 0) ahi = (ucell_t)(-ahi);
754 if ((cell_t)bhi < 0) bhi = (ucell_t)(-bhi);
756 /* Break into hi and lo 16 bit parts. */
757 alo = LOWER_HALF(ahi);
759 blo = LOWER_HALF(bhi);
764 /* higher part: ahi * bhi */
766 /* middle (overlapping) part: ahi * blo */
768 lo += LOWER_HALF(temp);
769 hi += temp >> HNBITS;
770 /* middle (overlapping) part: alo * bhi */
772 lo += LOWER_HALF(temp);
773 hi += temp >> HNBITS;
774 /* lower part: alo * blo */
776 /* its higher half overlaps with middle's lower half: */
777 lo += temp >> HNBITS;
781 /* combine lower part of result: */
782 lo = (lo << HNBITS) + LOWER_HALF(temp);
784 /* Negate product if one operand negative. */
787 /* lo = (ucell_t)(- lo); */
789 hi = ~hi + ((lo == 0) ? 1 : 0);
797 #define DULT(du1l,du1h,du2l,du2h) ( (du2h<du1h) ? FALSE : ( (du2h==du1h) ? (du1l<du2l) : TRUE) )
798 /* Perform 2 cell by 1 cell divide for 1 cell result and remainder, using shift and subtract. */
799 case ID_D_UMSMOD: /* UM/MOD ( al ah bdiv -- rem q ) */
801 ucell_t ah,al, q,di, bl,bh, sl,sh;
807 for( di=0; di<NBITS; di++ )
809 if( !DULT(al,ah,bl,bh) )
813 if( al < bl ) sh = 1; /* Borrow */
820 bl = (bl >> 1) | (bh << (NBITS-1));
823 if( !DULT(al,ah,bl,bh) )
829 M_PUSH( al ); /* rem */
834 /* Perform 2 cell by 1 cell divide for 2 cell result and remainder, using shift and subtract. */
835 case ID_D_MUSMOD: /* MU/MOD ( al am bdiv -- rem ql qh ) */
837 register ucell_t ah,am,al,ql,qh,di;
838 #define bdiv ((ucell_t)TOS)
843 for( di=0; di<2*NBITS; di++ )
850 qh = (qh << 1) | (ql >> (NBITS-1));
852 ah = (ah << 1) | (am >> (NBITS-1));
853 am = (am << 1) | (al >> (NBITS-1));
855 DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql ));
862 M_PUSH( ah ); /* rem */
873 #endif /* !PF_NO_SHELL */
880 TOS = gCurrentTask->td_StackBase - STKPTR;
883 case ID_DIVIDE: BINARY_OP( / ); endcase;
894 case ID_DROP: M_DROP; endcase;
898 DumpMemory( (char *) Scratch, TOS );
902 case ID_DUP: M_DUP; endcase;
904 case ID_DO_P: /* ( limit start -- ) ( R: -- start limit ) */
910 case ID_EOL: /* ( -- end_of_line_char ) */
915 case ID_ERRORQ_P: /* ( flag num -- , quit if flag true ) */
934 /* Save IP on return stack like a JSR. */
936 #ifdef PF_SUPPORT_TRACE
937 /* Bump level for trace. */
940 if( IsTokenPrimitive( TOS ) )
942 WRITE_CELL_DIC( (cell_t *) &FakeSecondary[0], TOS); /* Build a fake secondary and execute it. */
943 InsPtr = &FakeSecondary[0];
947 InsPtr = (cell_t *) LOCAL_CODEREL_TO_ABS(TOS);
953 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
956 TOS = (cell_t) READ_CELL_DIC((cell_t *)TOS);
960 TOS = *((cell_t *)TOS);
963 TOS = *((cell_t *)TOS);
967 case ID_FILE_CREATE: /* ( c-addr u fam -- fid ior ) */
968 /* Build NUL terminated name string. */
969 Scratch = M_POP; /* u */
970 Temp = M_POP; /* caddr */
971 if( Scratch < TIB_SIZE-2 )
973 const char *famText = pfSelectFileModeCreate( TOS );
974 pfCopyMemory( gScratch, (char *) Temp, (ucell_t) Scratch );
975 gScratch[Scratch] = '\0';
976 DBUG(("Create file = %s with famTxt %s\n", gScratch, famText ));
977 FileID = sdOpenFile( gScratch, famText );
978 TOS = ( FileID == NULL ) ? -1 : 0 ;
979 M_PUSH( (cell_t) FileID );
983 ERR("Filename too large for name buffer.\n");
989 case ID_FILE_DELETE: /* ( c-addr u -- ior ) */
990 /* Build NUL terminated name string. */
991 Temp = M_POP; /* caddr */
992 if( TOS < TIB_SIZE-2 )
994 pfCopyMemory( gScratch, (char *) Temp, (ucell_t) TOS );
995 gScratch[TOS] = '\0';
996 DBUG(("Delete file = %s\n", gScratch ));
997 TOS = sdDeleteFile( gScratch );
1001 ERR("Filename too large for name buffer.\n");
1006 case ID_FILE_OPEN: /* ( c-addr u fam -- fid ior ) */
1007 /* Build NUL terminated name string. */
1008 Scratch = M_POP; /* u */
1009 Temp = M_POP; /* caddr */
1010 if( Scratch < TIB_SIZE-2 )
1012 const char *famText = pfSelectFileModeOpen( TOS );
1013 pfCopyMemory( gScratch, (char *) Temp, (ucell_t) Scratch );
1014 gScratch[Scratch] = '\0';
1015 DBUG(("Open file = %s\n", gScratch ));
1016 FileID = sdOpenFile( gScratch, famText );
1018 TOS = ( FileID == NULL ) ? -1 : 0 ;
1019 M_PUSH( (cell_t) FileID );
1023 ERR("Filename too large for name buffer.\n");
1029 case ID_FILE_CLOSE: /* ( fid -- ior ) */
1030 TOS = sdCloseFile( (FileStream *) TOS );
1033 case ID_FILE_READ: /* ( addr len fid -- u2 ior ) */
1034 FileID = (FileStream *) TOS;
1036 CharPtr = (char *) M_POP;
1037 Temp = sdReadFile( CharPtr, 1, Scratch, FileID );
1042 case ID_FILE_SIZE: /* ( fid -- ud ior ) */
1043 /* Determine file size by seeking to end and returning position. */
1044 FileID = (FileStream *) TOS;
1046 off_t endposition, offsetHi;
1047 off_t original = sdTellFile( FileID );
1048 sdSeekFile( FileID, 0, PF_SEEK_END );
1049 endposition = sdTellFile( FileID );
1050 M_PUSH(endposition);
1051 /* Just use a 0 if they are the same size. */
1052 offsetHi = (sizeof(off_t) > sizeof(cell_t)) ? (endposition >> (8*sizeof(cell_t))) : 0 ;
1054 sdSeekFile( FileID, original, PF_SEEK_SET );
1055 TOS = (original < 0) ? -4 : 0 ; /* !!! err num */
1059 case ID_FILE_WRITE: /* ( addr len fid -- ior ) */
1060 FileID = (FileStream *) TOS;
1062 CharPtr = (char *) M_POP;
1063 Temp = sdWriteFile( CharPtr, 1, Scratch, FileID );
1064 TOS = (Temp != Scratch) ? -3 : 0;
1067 case ID_FILE_REPOSITION: /* ( ud fid -- ior ) */
1070 FileID = (FileStream *) TOS;
1072 /* Avoid compiler warnings on Mac. */
1073 offset = (sizeof(off_t) > sizeof(cell_t)) ? (offset << 8*sizeof(cell_t)) : 0 ;
1075 TOS = sdSeekFile( FileID, offset, PF_SEEK_SET );
1079 case ID_FILE_POSITION: /* ( fid -- ud ior ) */
1083 FileID = (FileStream *) TOS;
1084 position = sdTellFile( FileID );
1086 /* Just use a 0 if they are the same size. */
1087 offsetHi = (sizeof(off_t) > sizeof(cell_t)) ? (position >> (8*sizeof(cell_t))) : 0 ;
1089 TOS = (position < 0) ? -4 : 0 ; /* !!! err num */
1093 case ID_FILE_RO: /* ( -- fam ) */
1095 TOS = PF_FAM_READ_ONLY;
1098 case ID_FILE_RW: /* ( -- fam ) */
1100 TOS = PF_FAM_READ_WRITE;
1103 case ID_FILE_WO: /* ( -- fam ) */
1105 TOS = PF_FAM_WRITE_ONLY;
1108 case ID_FILE_BIN: /* ( -- fam ) */
1109 TOS = TOS | PF_FAM_BINARY_FLAG;
1112 case ID_FILE_FLUSH: /* ( fileid -- ior ) */
1114 FileStream *Stream = (FileStream *) TOS;
1115 TOS = (sdFlushFile( Stream ) == 0) ? 0 : THROW_FLUSH_FILE;
1119 case ID_FILE_RENAME: /* ( oldName newName -- ior ) */
1121 char *New = (char *) TOS;
1122 char *Old = (char *) M_POP;
1123 TOS = sdRenameFile( Old, New );
1127 case ID_FILE_RESIZE: /* ( ud fileid -- ior ) */
1129 FileStream *File = (FileStream *) TOS;
1130 ucell_t SizeHi = (ucell_t) M_POP;
1131 ucell_t SizeLo = (ucell_t) M_POP;
1132 TOS = ( UdIsUint64( SizeLo, SizeHi )
1133 ? sdResizeFile( File, UdToUint64( SizeLo, SizeHi ))
1134 : THROW_RESIZE_FILE );
1138 case ID_FILL: /* ( caddr num charval -- ) */
1140 register char *DstPtr;
1141 Temp = M_POP; /* num */
1142 DstPtr = (char *) M_POP; /* dst */
1143 for( Scratch=0; (ucell_t) Scratch < (ucell_t) Temp ; Scratch++ )
1145 *DstPtr++ = (char) TOS;
1152 case ID_FIND: /* ( $addr -- $addr 0 | xt +-1 ) */
1153 TOS = ffFind( (char *) TOS, (ExecToken *) &Temp );
1158 TOS = ffFindNFA( (const ForthString *) TOS, (const ForthString **) &Temp );
1159 M_PUSH( (cell_t) Temp );
1161 #endif /* !PF_NO_SHELL */
1167 /* Validate memory before freeing. Clobber validator and first word. */
1168 case ID_FREE: /* ( addr -- result ) */
1171 ERR("FREE passed NULL!\n");
1172 TOS = -2; /* FIXME error code */
1176 CellPtr = (cell_t *) TOS;
1178 if( ((ucell_t)*CellPtr) != ((ucell_t)CellPtr ^ PF_MEMORY_VALIDATOR))
1180 TOS = -2; /* FIXME error code */
1184 CellPtr[0] = 0xDeadBeef;
1185 pfFreeMem((char *)CellPtr);
1191 #include "pfinnrfp.h"
1195 TOS = (cell_t)CODE_HERE;
1198 case ID_NUMBERQ_P: /* ( addr -- 0 | n 1 ) */
1199 /* Convert using number converter in 'C'.
1200 ** Only supports single precision for bootstrap.
1202 TOS = (cell_t) ffNumberQ( (char *) TOS, &Temp );
1203 if( TOS == NUM_TYPE_SINGLE)
1205 M_PUSH( Temp ); /* Push single number */
1209 case ID_I: /* ( -- i , DO LOOP index ) */
1215 case ID_INCLUDE_FILE:
1216 FileID = (FileStream *) TOS;
1217 M_DROP; /* Drop now so that INCLUDE has a clean stack. */
1219 Scratch = ffIncludeFile( FileID );
1221 if( Scratch ) M_THROW(Scratch)
1223 #endif /* !PF_NO_SHELL */
1228 Scratch = ffInterpret();
1230 if( Scratch ) M_THROW(Scratch)
1232 #endif /* !PF_NO_SHELL */
1234 case ID_J: /* ( -- j , second DO LOOP index ) */
1249 #endif /* !PF_NO_SHELL */
1252 DBUG(("ID_LITERAL_P: InsPtr = 0x%x, *InsPtr = 0x%x\n", InsPtr, *InsPtr ));
1254 TOS = READ_CELL_DIC(InsPtr++);
1258 case ID_LOCAL_COMPILER: DO_VAR(gLocalCompiler_XT); endcase;
1259 #endif /* !PF_NO_SHELL */
1261 case ID_LOCAL_FETCH: /* ( i <local> -- n , fetch from local ) */
1262 TOS = *(LocalsPtr - TOS);
1265 #define LOCAL_FETCH_N(num) \
1266 case ID_LOCAL_FETCH_##num: /* ( <local> -- n , fetch from local ) */ \
1268 TOS = *(LocalsPtr -(num)); \
1280 case ID_LOCAL_STORE: /* ( n i <local> -- , store n in local ) */
1281 *(LocalsPtr - TOS) = M_POP;
1285 #define LOCAL_STORE_N(num) \
1286 case ID_LOCAL_STORE_##num: /* ( n <local> -- , store n in local ) */ \
1287 *(LocalsPtr - (num)) = TOS; \
1300 case ID_LOCAL_PLUSSTORE: /* ( n i <local> -- , add n to local ) */
1301 *(LocalsPtr - TOS) += M_POP;
1305 case ID_LOCAL_ENTRY: /* ( x0 x1 ... xn n -- ) */
1306 /* create local stack frame */
1310 DBUG(("LocalEntry: n = %d\n", TOS));
1311 /* End of locals. Create stack frame */
1312 DBUG(("LocalEntry: before RP@ = 0x%x, LP = 0x%x\n",
1313 TORPTR, LocalsPtr));
1314 M_R_PUSH(LocalsPtr);
1317 DBUG(("LocalEntry: after RP@ = 0x%x, LP = 0x%x\n",
1318 TORPTR, LocalsPtr));
1322 *lp++ = M_POP; /* Load local vars from stack */
1328 case ID_LOCAL_EXIT: /* cleanup up local stack frame */
1329 DBUG(("LocalExit: before RP@ = 0x%x, LP = 0x%x\n",
1330 TORPTR, LocalsPtr));
1332 LocalsPtr = (cell_t *) M_R_POP;
1333 DBUG(("LocalExit: after RP@ = 0x%x, LP = 0x%x\n",
1334 TORPTR, LocalsPtr));
1339 MSG("Load "); MSG(SYSTEM_LOAD_FILE); EMIT_CR;
1340 FileID = sdOpenFile(SYSTEM_LOAD_FILE, "r");
1344 Scratch = ffIncludeFile( FileID ); /* Also closes the file. */
1346 if( Scratch ) M_THROW(Scratch);
1350 ERR(SYSTEM_LOAD_FILE); ERR(" could not be opened!\n");
1353 #endif /* !PF_NO_SHELL */
1355 case ID_LEAVE_P: /* ( R: index limit -- ) */
1361 case ID_LOOP_P: /* ( R: index limit -- | index limit ) */
1362 Temp = M_R_POP; /* limit */
1363 Scratch = M_R_POP + 1; /* index */
1364 if( Scratch == Temp )
1366 InsPtr++; /* skip branch offset, exit loop */
1370 /* Push index and limit back to R */
1371 M_R_PUSH( Scratch );
1373 /* Branch back to just after (DO) */
1378 case ID_LSHIFT: BINARY_OP( << ); endcase;
1382 TOS = ( TOS > Scratch ) ? TOS : Scratch ;
1387 TOS = ( TOS < Scratch ) ? TOS : Scratch ;
1390 case ID_MINUS: BINARY_OP( - ); endcase;
1393 case ID_NAME_TO_TOKEN:
1394 TOS = (cell_t) NameToToken((ForthString *)TOS);
1397 case ID_NAME_TO_PREVIOUS:
1398 TOS = (cell_t) NameToPrevious((ForthString *)TOS);
1405 case ID_OR: BINARY_OP( | ); endcase;
1412 case ID_PICK: /* ( ... n -- sp(n) ) */
1416 case ID_PLUS: BINARY_OP( + ); endcase;
1418 case ID_PLUS_STORE: /* ( n addr -- , add n to *addr ) */
1419 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1420 if( IN_DICS( TOS ) )
1422 Scratch = READ_CELL_DIC((cell_t *)TOS);
1424 WRITE_CELL_DIC((cell_t *)TOS,Scratch);
1428 *((cell_t *)TOS) += M_POP;
1431 *((cell_t *)TOS) += M_POP;
1436 case ID_PLUSLOOP_P: /* ( delta -- ) ( R: index limit -- | index limit ) */
1438 cell_t Limit = M_R_POP;
1439 cell_t OldIndex = M_R_POP;
1440 cell_t Delta = TOS; /* add TOS to index, not 1 */
1441 cell_t NewIndex = OldIndex + Delta;
1442 cell_t OldDiff = OldIndex - Limit;
1444 /* This exploits this idea (lifted from Gforth):
1445 (x^y)<0 is equivalent to (x<0) != (y<0) */
1446 if( ((OldDiff ^ (OldDiff + Delta)) /* is the limit crossed? */
1447 & (OldDiff ^ Delta)) /* is it a wrap-around? */
1450 InsPtr++; /* skip branch offset, exit loop */
1454 /* Push index and limit back to R */
1455 M_R_PUSH( NewIndex );
1457 /* Branch back to just after (DO) */
1464 case ID_QDO_P: /* (?DO) ( limit start -- ) ( R: -- start limit ) */
1465 Scratch = M_POP; /* limit */
1466 if( Scratch == TOS )
1468 /* Branch to just after (LOOP) */
1474 M_R_PUSH( Scratch );
1475 InsPtr++; /* skip branch offset, enter loop */
1480 case ID_QDUP: if( TOS ) M_DUP; endcase;
1482 case ID_QTERMINAL: /* WARNING: Typically not fully implemented! */
1484 TOS = sdQueryTerminal();
1487 case ID_QUIT_P: /* Stop inner interpreter, go back to user. */
1488 #ifdef PF_SUPPORT_TRACE
1491 M_THROW(THROW_QUIT);
1510 TOS = (ffRefill() > 0) ? FTRUE : FFALSE;
1513 /* Resize memory allocated by ALLOCATE. */
1514 case ID_RESIZE: /* ( addr1 u -- addr2 result ) */
1516 cell_t *Addr1 = (cell_t *) M_POP;
1517 /* Point to validator below users address. */
1518 cell_t *FreePtr = Addr1 - 1;
1519 if( ((ucell_t)*FreePtr) != ((ucell_t)FreePtr ^ PF_MEMORY_VALIDATOR))
1521 /* 090218 - Fixed bug, was returning zero. */
1527 /* Try to allocate. */
1528 CellPtr = (cell_t *) pfAllocMem( TOS + sizeof(cell_t) );
1531 /* Copy memory including validation. */
1532 pfCopyMemory( (char *) CellPtr, (char *) FreePtr, TOS + sizeof(cell_t) );
1533 *CellPtr = (cell_t)(((ucell_t)CellPtr) ^ (ucell_t)PF_MEMORY_VALIDATOR);
1534 /* 090218 - Fixed bug that was incrementing the address twice. Thanks Reinhold Straub. */
1535 /* Increment past validator to user address. */
1536 M_PUSH( (cell_t) (CellPtr + 1) );
1537 TOS = 0; /* Result code. */
1538 /* Mark old cell as dead so we can't free it twice. */
1539 FreePtr[0] = 0xDeadBeef;
1540 pfFreeMem((char *) FreePtr);
1544 /* 090218 - Fixed bug, was returning zero. */
1546 TOS = -4; /* FIXME Fix error code. */
1553 ** RP@ and RP! are called secondaries so we must
1554 ** account for the return address pushed before calling.
1556 case ID_RP_FETCH: /* ( -- rp , address of top of return stack ) */
1558 TOS = (cell_t)TORPTR; /* value before calling RP@ */
1561 case ID_RP_STORE: /* ( rp -- , address of top of return stack ) */
1562 TORPTR = (cell_t *) TOS;
1566 case ID_ROLL: /* ( xu xu-1 xu-1 ... x0 u -- xu-1 xu-1 ... x0 xu ) */
1569 cell_t *srcPtr, *dstPtr;
1570 Scratch = M_STACK(TOS);
1571 srcPtr = &M_STACK(TOS-1);
1572 dstPtr = &M_STACK(TOS);
1573 for( ri=0; ri<TOS; ri++ )
1575 *dstPtr-- = *srcPtr--;
1582 case ID_ROT: /* ( a b c -- b c a ) */
1583 Scratch = M_POP; /* b */
1584 Temp = M_POP; /* a */
1585 M_PUSH( Scratch ); /* b */
1590 /* Logical right shift */
1591 case ID_RSHIFT: { TOS = ((ucell_t)M_POP) >> TOS; } endcase;
1594 case ID_SAVE_FORTH_P: /* ( $name Entry NameSize CodeSize -- err ) */
1596 cell_t NameSize, CodeSize, EntryPoint;
1600 ForthStringToC( gScratch, (char *) M_POP, sizeof(gScratch) );
1601 TOS = ffSaveForth( gScratch, EntryPoint, NameSize, CodeSize );
1606 case ID_SP_FETCH: /* ( -- sp , address of top of stack, sorta ) */
1608 TOS = (cell_t)STKPTR;
1611 case ID_SP_STORE: /* ( sp -- , address of top of stack, sorta ) */
1612 STKPTR = (cell_t *) TOS;
1616 case ID_STORE: /* ( n addr -- , write n to addr ) */
1617 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1618 if( IN_DICS( TOS ) )
1620 WRITE_CELL_DIC((cell_t *)TOS,M_POP);
1624 *((cell_t *)TOS) = M_POP;
1627 *((cell_t *)TOS) = M_POP;
1632 case ID_SCAN: /* ( addr cnt char -- addr' cnt' ) */
1633 Scratch = M_POP; /* cnt */
1634 Temp = M_POP; /* addr */
1635 TOS = ffScan( (char *) Temp, Scratch, (char) TOS, &CharPtr );
1636 M_PUSH((cell_t) CharPtr);
1642 Scratch = ffSemiColon();
1644 if( Scratch ) M_THROW( Scratch );
1646 #endif /* !PF_NO_SHELL */
1648 case ID_SKIP: /* ( addr cnt char -- addr' cnt' ) */
1649 Scratch = M_POP; /* cnt */
1650 Temp = M_POP; /* addr */
1651 TOS = ffSkip( (char *) Temp, Scratch, (char) TOS, &CharPtr );
1652 M_PUSH((cell_t) CharPtr);
1655 case ID_SOURCE: /* ( -- c-addr num ) */
1657 M_PUSH( (cell_t) gCurrentTask->td_SourcePtr );
1658 TOS = (cell_t) gCurrentTask->td_SourceNum;
1661 case ID_SOURCE_SET: /* ( c-addr num -- ) */
1662 gCurrentTask->td_SourcePtr = (char *) M_POP;
1663 gCurrentTask->td_SourceNum = TOS;
1669 TOS = ffConvertStreamToSourceID( gCurrentTask->td_InputStream ) ;
1672 case ID_SOURCE_ID_POP:
1674 TOS = ffConvertStreamToSourceID( ffPopInputStream() ) ;
1677 case ID_SOURCE_ID_PUSH: /* ( source-id -- ) */
1678 TOS = (cell_t)ffConvertSourceIDToStream( TOS );
1679 Scratch = ffPushInputStream((FileStream *) TOS );
1687 case ID_SOURCE_LINE_NUMBER_FETCH: /* ( -- linenr ) */
1689 TOS = gCurrentTask->td_LineNumber;
1692 case ID_SOURCE_LINE_NUMBER_STORE: /* ( linenr -- ) */
1693 gCurrentTask->td_LineNumber = TOS;
1713 case ID_THROW: /* ( k*x err -- k*x | i*x err , jump to where CATCH was called ) */
1724 CharPtr = (char *) ffWord( (char) ' ' );
1725 TOS = ffFind( CharPtr, (ExecToken *) &Temp );
1728 ERR("' could not find ");
1729 ioType( (char *) CharPtr+1, *CharPtr );
1737 #endif /* !PF_NO_SHELL */
1739 case ID_TIMES: BINARY_OP( * ); endcase;
1742 Scratch = M_POP; /* addr */
1743 ioType( (char *) Scratch, TOS );
1752 case ID_VAR_BASE: DO_VAR(gVarBase); endcase;
1753 case ID_VAR_CODE_BASE: DO_VAR(gCurrentDictionary->dic_CodeBase); endcase;
1754 case ID_VAR_CODE_LIMIT: DO_VAR(gCurrentDictionary->dic_CodeLimit); endcase;
1755 case ID_VAR_CONTEXT: DO_VAR(gVarContext); endcase;
1756 case ID_VAR_DP: DO_VAR(gCurrentDictionary->dic_CodePtr.Cell); endcase;
1757 case ID_VAR_ECHO: DO_VAR(gVarEcho); endcase;
1758 case ID_VAR_HEADERS_BASE: DO_VAR(gCurrentDictionary->dic_HeaderBase); endcase;
1759 case ID_VAR_HEADERS_LIMIT: DO_VAR(gCurrentDictionary->dic_HeaderLimit); endcase;
1760 case ID_VAR_HEADERS_PTR: DO_VAR(gCurrentDictionary->dic_HeaderPtr); endcase;
1761 case ID_VAR_NUM_TIB: DO_VAR(gCurrentTask->td_SourceNum); endcase;
1762 case ID_VAR_OUT: DO_VAR(gCurrentTask->td_OUT); endcase;
1763 case ID_VAR_STATE: DO_VAR(gVarState); endcase;
1764 case ID_VAR_TO_IN: DO_VAR(gCurrentTask->td_IN); endcase;
1765 case ID_VAR_TRACE_FLAGS: DO_VAR(gVarTraceFlags); endcase;
1766 case ID_VAR_TRACE_LEVEL: DO_VAR(gVarTraceLevel); endcase;
1767 case ID_VAR_TRACE_STACK: DO_VAR(gVarTraceStack); endcase;
1768 case ID_VAR_RETURN_CODE: DO_VAR(gVarReturnCode); endcase;
1771 TOS = (cell_t) ffWord( (char) TOS );
1774 case ID_WORD_FETCH: /* ( waddr -- w ) */
1775 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1776 if( IN_DICS( TOS ) )
1778 TOS = (uint16_t) READ_SHORT_DIC((uint16_t *)TOS);
1782 TOS = *((uint16_t *)TOS);
1785 TOS = *((uint16_t *)TOS);
1789 case ID_WORD_STORE: /* ( w waddr -- ) */
1791 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1792 if( IN_DICS( TOS ) )
1794 WRITE_SHORT_DIC((uint16_t *)TOS,(uint16_t)M_POP);
1798 *((uint16_t *)TOS) = (uint16_t) M_POP;
1801 *((uint16_t *)TOS) = (uint16_t) M_POP;
1806 case ID_XOR: BINARY_OP( ^ ); endcase;
1809 /* Branch is followed by an offset relative to address of offset. */
1810 case ID_ZERO_BRANCH:
1811 DBUGX(("Before 0Branch: IP = 0x%x\n", InsPtr ));
1818 InsPtr++; /* skip over offset */
1821 DBUGX(("After 0Branch: IP = 0x%x\n", InsPtr ));
1825 ERR("pfCatch: Unrecognised token = 0x");
1828 ffDotHex((cell_t) InsPtr);
1834 if(InsPtr) Token = READ_CELL_DIC(InsPtr++); /* Traverse to next token in secondary. */
1841 if( _CrtCheckMemory() == 0 )
1843 ERR("_CrtCheckMemory abort: InsPtr = 0x");
1844 ffDotHex((int)InsPtr);
1849 } while( (InitialReturnStack - TORPTR) > 0 );
1853 return ExceptionReturnCode;