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 ** Permission to use, copy, modify, and/or distribute this
9 ** software for any purpose with or without fee is hereby granted.
11 ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
12 ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
13 ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
14 ** THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
15 ** CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
16 ** FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
17 ** CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 ** OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 ****************************************************************
22 ** 940502 PLB Creation.
23 ** 940505 PLB More macros.
24 ** 940509 PLB Moved all stack stuff into pfCatch.
25 ** 941014 PLB Converted to flat secondary strusture.
26 ** 941027 rdg added casts to ID_SP_FETCH, ID_RP_FETCH,
27 ** and ID_HERE for armcc
28 ** 941130 PLB Made w@ unsigned
30 ***************************************************************/
34 #if defined(WIN32) && !defined(__MINGW32__)
38 #define SYSTEM_LOAD_FILE "system.fth"
40 /***************************************************************
41 ** Macros for data stack access.
42 ** TOS is cached in a register in pfCatch.
43 ***************************************************************/
45 #define STKPTR (DataStackPtr)
46 #define M_POP (*(STKPTR++))
47 #define M_PUSH(n) {*(--(STKPTR)) = (cell_t) (n);}
48 #define M_STACK(n) (STKPTR[n])
50 #define TOS (TopOfStack)
51 #define PUSH_TOS M_PUSH(TOS)
52 #define M_DUP PUSH_TOS;
53 #define M_DROP { TOS = M_POP; }
55 #define ASCII_EOT (0x04)
57 /***************************************************************
58 ** Macros for Floating Point stack access.
59 ***************************************************************/
61 #define FP_STKPTR (FloatStackPtr)
62 #define M_FP_SPZERO (gCurrentTask->td_FloatStackBase)
63 #define M_FP_POP (*(FP_STKPTR++))
64 #define M_FP_PUSH(n) {*(--(FP_STKPTR)) = (PF_FLOAT) (n);}
65 #define M_FP_STACK(n) (FP_STKPTR[n])
67 #define FP_TOS (fpTopOfStack)
68 #define PUSH_FP_TOS M_FP_PUSH(FP_TOS)
69 #define M_FP_DUP PUSH_FP_TOS;
70 #define M_FP_DROP { FP_TOS = M_FP_POP; }
73 /***************************************************************
74 ** Macros for return stack access.
75 ***************************************************************/
77 #define TORPTR (ReturnStackPtr)
78 #define M_R_DROP {TORPTR++;}
79 #define M_R_POP (*(TORPTR++))
80 #define M_R_PICK(n) (TORPTR[n])
81 #define M_R_PUSH(n) {*(--(TORPTR)) = (cell_t) (n);}
83 /***************************************************************
85 ***************************************************************/
87 #define M_BRANCH { InsPtr = (cell_t *) (((uint8_t *) InsPtr) + READ_CELL_DIC(InsPtr)); }
89 /* Cache top of data stack like in JForth. */
91 #define LOAD_REGISTERS \
93 STKPTR = gCurrentTask->td_StackPtr; \
95 FP_STKPTR = gCurrentTask->td_FloatStackPtr; \
97 TORPTR = gCurrentTask->td_ReturnPtr; \
100 #define SAVE_REGISTERS \
102 gCurrentTask->td_ReturnPtr = TORPTR; \
104 gCurrentTask->td_StackPtr = STKPTR; \
105 M_FP_PUSH( FP_TOS ); \
106 gCurrentTask->td_FloatStackPtr = FP_STKPTR; \
110 /* Cache top of data stack like in JForth. */
111 #define LOAD_REGISTERS \
113 STKPTR = gCurrentTask->td_StackPtr; \
115 TORPTR = gCurrentTask->td_ReturnPtr; \
118 #define SAVE_REGISTERS \
120 gCurrentTask->td_ReturnPtr = TORPTR; \
122 gCurrentTask->td_StackPtr = STKPTR; \
131 #define DO_VAR(varname) { PUSH_TOS; TOS = (cell_t) &varname; }
134 #define M_THROW(err) \
136 ExceptionReturnCode = (ThrowCode)(err); \
137 TORPTR = InitialReturnStack; /* Will cause return to 'C' */ \
138 STKPTR = InitialDataStack; \
139 FP_STKPTR = InitialFloatStack; \
142 #define M_THROW(err) \
144 ExceptionReturnCode = (err); \
145 TORPTR = InitialReturnStack; /* Will cause return to 'C' */ \
146 STKPTR = InitialDataStack; \
150 /***************************************************************
152 ***************************************************************/
154 #define BINARY_OP( op ) { TOS = M_POP op TOS; }
155 #define endcase break
157 #if defined(PF_NO_SHELL) || !defined(PF_SUPPORT_TRACE)
158 #define TRACENAMES /* no names */
160 /* Display name of executing routine. */
161 static void TraceNames( ExecToken Token, cell_t Level )
166 if( ffTokenToName( Token, &DebugName ) )
169 if( gCurrentTask->td_OUT > 0 ) EMIT_CR;
171 for( i=0; i<Level; i++ )
175 TypeName( DebugName );
176 /* Space out to column N then .S */
177 NumSpaces = 30 - gCurrentTask->td_OUT;
178 for( i=0; i < NumSpaces; i++ )
183 /* No longer needed? gCurrentTask->td_OUT = 0; */ /* !!! Hack for ffDotS() */
188 MSG_NUM_H("Couldn't find Name for ", Token);
193 if( (gVarTraceLevel > Level) ) \
194 { SAVE_REGISTERS; TraceNames( Token, Level ); LOAD_REGISTERS; }
195 #endif /* PF_NO_SHELL */
197 /* Use local copy of CODE_BASE for speed. */
198 #define LOCAL_CODEREL_TO_ABS( a ) ((cell_t *) (((cell_t) a) + CodeBase))
200 /* Truncate the unsigned double cell integer LO/HI to an uint64_t. */
201 static uint64_t UdToUint64( ucell_t Lo, ucell_t Hi )
203 return (( 2 * sizeof(ucell_t) == sizeof(uint64_t) )
204 ? (((uint64_t)Lo) | (((uint64_t)Hi) >> (sizeof(ucell_t) * 8)))
208 /* Return TRUE if the unsigned double cell integer LO/HI is not greater
209 * then the greatest uint64_t.
211 static int UdIsUint64( ucell_t Lo, ucell_t Hi )
213 return (( 2 * sizeof(ucell_t) == sizeof(uint64_t) )
218 static const char *pfSelectFileModeCreate( cell_t fam );
219 static const char *pfSelectFileModeOpen( cell_t fam );
221 /**************************************************************/
222 static const char *pfSelectFileModeCreate( cell_t fam )
224 const char *famText = NULL;
227 case (PF_FAM_WRITE_ONLY + PF_FAM_BINARY_FLAG):
228 famText = PF_FAM_BIN_CREATE_WO;
230 case (PF_FAM_READ_WRITE + PF_FAM_BINARY_FLAG):
231 famText = PF_FAM_BIN_CREATE_RW;
233 case PF_FAM_WRITE_ONLY:
234 famText = PF_FAM_CREATE_WO;
236 case PF_FAM_READ_WRITE:
237 famText = PF_FAM_CREATE_RW;
246 /**************************************************************/
247 static const char *pfSelectFileModeOpen( cell_t fam )
249 const char *famText = NULL;
252 case (PF_FAM_READ_ONLY + PF_FAM_BINARY_FLAG):
253 famText = PF_FAM_BIN_OPEN_RO;
255 case (PF_FAM_WRITE_ONLY + PF_FAM_BINARY_FLAG):
256 famText = PF_FAM_BIN_CREATE_WO;
258 case (PF_FAM_READ_WRITE + PF_FAM_BINARY_FLAG):
259 famText = PF_FAM_BIN_OPEN_RW;
261 case PF_FAM_READ_ONLY:
262 famText = PF_FAM_OPEN_RO;
264 case PF_FAM_WRITE_ONLY:
265 famText = PF_FAM_CREATE_WO;
267 case PF_FAM_READ_WRITE:
269 famText = PF_FAM_OPEN_RW;
275 /**************************************************************/
276 ThrowCode pfCatch( ExecToken XT )
278 register cell_t TopOfStack; /* Cache for faster execution. */
279 register cell_t *DataStackPtr;
280 register cell_t *ReturnStackPtr;
281 register cell_t *InsPtr = NULL;
282 register cell_t Token;
286 PF_FLOAT fpTopOfStack;
287 PF_FLOAT *FloatStackPtr;
290 PF_FLOAT *InitialFloatStack;
292 #ifdef PF_SUPPORT_TRACE
295 cell_t *LocalsPtr = NULL;
297 cell_t *InitialReturnStack;
298 cell_t *InitialDataStack;
299 cell_t FakeSecondary[2];
303 uint8_t *CodeBase = (uint8_t *) CODE_BASE;
304 ThrowCode ExceptionReturnCode = 0;
307 gExecutionDepth += 1;
308 PRT(("pfCatch( 0x%x ), depth = %d\n", XT, gExecutionDepth ));
312 ** Initialize FakeSecondary this way to avoid having stuff in the data section,
313 ** which is not supported for some embedded system loaders.
315 FakeSecondary[0] = 0;
316 FakeSecondary[1] = ID_EXIT; /* For EXECUTE */
318 /* Move data from task structure to registers for speed. */
321 /* Save initial stack depths for THROW */
322 InitialReturnStack = TORPTR;
323 InitialDataStack = STKPTR ;
325 InitialFloatStack = FP_STKPTR;
332 DBUG(("pfCatch: Token = 0x%x\n", Token ));
334 /* --------------------------------------------------------------- */
335 /* If secondary, thread down code tree until we hit a primitive. */
336 while( !IsTokenPrimitive( Token ) )
338 #ifdef PF_SUPPORT_TRACE
339 if((gVarTraceFlags & TRACE_INNER) )
341 MSG("pfCatch: Secondary Token = 0x");
343 MSG_NUM_H(", InsPtr = 0x", InsPtr);
348 /* Save IP on return stack like a JSR. */
351 /* Convert execution token to absolute address. */
352 InsPtr = (cell_t *) ( LOCAL_CODEREL_TO_ABS(Token) );
354 /* Fetch token at IP. */
355 Token = READ_CELL_DIC(InsPtr++);
357 #ifdef PF_SUPPORT_TRACE
358 /* Bump level for trace display */
364 #ifdef PF_SUPPORT_TRACE
368 /* Execute primitive Token. */
372 /* Pop up a level in Forth inner interpreter.
373 ** Used to implement semicolon.
374 ** Put first in switch because ID_EXIT==0 */
376 InsPtr = ( cell_t *) M_R_POP;
377 #ifdef PF_SUPPORT_TRACE
382 case ID_1MINUS: TOS--; endcase;
384 case ID_1PLUS: TOS++; endcase;
388 ff2Literal( TOS, M_POP );
391 #endif /* !PF_NO_SHELL */
394 /* hi part stored first, put on top of stack */
396 TOS = READ_CELL_DIC(InsPtr++);
397 M_PUSH(READ_CELL_DIC(InsPtr++));
400 case ID_2MINUS: TOS -= 2; endcase;
402 case ID_2PLUS: TOS += 2; endcase;
405 case ID_2OVER: /* ( a b c d -- a b c d a b ) */
407 Scratch = M_STACK(3);
412 case ID_2SWAP: /* ( a b c d -- c d a b ) */
413 Scratch = M_STACK(0); /* c */
414 M_STACK(0) = M_STACK(2); /* a */
415 M_STACK(2) = Scratch; /* c */
416 Scratch = TOS; /* d */
417 TOS = M_STACK(1); /* b */
418 M_STACK(1) = Scratch; /* d */
421 case ID_2DUP: /* ( a b -- a b a b ) */
423 Scratch = M_STACK(1);
429 M_PUSH( (*(TORPTR+1)) );
445 case ID_ACCEPT_P: /* ( c-addr +n1 -- +n2 ) */
446 CharPtr = (char *) M_POP;
447 TOS = ioAccept( CharPtr, TOS );
452 ffALiteral( ABS_TO_CODEREL(TOS) );
455 #endif /* !PF_NO_SHELL */
459 TOS = (cell_t) LOCAL_CODEREL_TO_ABS( READ_CELL_DIC(InsPtr++) );
462 /* Allocate some extra and put validation identifier at base */
463 #define PF_MEMORY_VALIDATOR (0xA81B4D69)
465 /* Allocate at least one cell's worth because we clobber first cell. */
466 if ( TOS < sizeof(cell_t) )
468 Temp = sizeof(cell_t);
474 /* Allocate extra cells worth because we store validation info. */
475 CellPtr = (cell_t *) pfAllocMem( Temp + sizeof(cell_t) );
478 /* This was broken into two steps because different compilers incremented
479 ** CellPtr before or after the XOR step. */
480 Temp = (cell_t)CellPtr ^ PF_MEMORY_VALIDATOR;
482 M_PUSH( (cell_t) CellPtr );
488 TOS = -1; /* FIXME Fix error code. */
492 case ID_AND: BINARY_OP( & ); endcase;
494 case ID_ARSHIFT: BINARY_OP( >> ); endcase; /* Arithmetic right shift */
498 TOS = CREATE_BODY_OFFSET;
501 /* Branch is followed by an offset relative to address of offset. */
503 DBUGX(("Before Branch: IP = 0x%x\n", InsPtr ));
505 DBUGX(("After Branch: IP = 0x%x\n", InsPtr ));
510 M_THROW( THROW_BYE );
514 MSG("Emergency exit.\n");
522 Scratch = pfCatch( Scratch );
530 Scratch = READ_CELL_DIC(InsPtr++);
531 CallUserFunction( Scratch & 0xFFFF,
533 (Scratch >> 24) & 0x7F );
537 /* Support 32/64 bit operation. */
540 TOS = sizeof(cell_t);
544 TOS = TOS * sizeof(cell_t);
547 case ID_CFETCH: TOS = *((uint8_t *) TOS); endcase;
549 case ID_CMOVE: /* ( src dst n -- ) */
551 register char *DstPtr = (char *) M_POP; /* dst */
552 CharPtr = (char *) M_POP; /* src */
553 for( Scratch=0; (ucell_t) Scratch < (ucell_t) TOS ; Scratch++ )
555 *DstPtr++ = *CharPtr++;
561 case ID_CMOVE_UP: /* ( src dst n -- ) */
563 register char *DstPtr = ((char *) M_POP) + TOS; /* dst */
564 CharPtr = ((char *) M_POP) + TOS;; /* src */
565 for( Scratch=0; (ucell_t) Scratch < (ucell_t) TOS ; Scratch++ )
567 *(--DstPtr) = *(--CharPtr);
579 case ID_COLON_P: /* ( $name xt -- ) */
580 CreateDicEntry( TOS, (char *) M_POP, 0 );
583 #endif /* !PF_NO_SHELL */
589 s2 = (const char *) M_POP;
591 s1 = (const char *) M_POP;
592 TOS = ffCompare( s1, len1, s2, TOS );
596 /* ( a b -- flag , Comparisons ) */
598 TOS = ( TOS == M_POP ) ? FTRUE : FFALSE ;
600 case ID_COMP_NOT_EQUAL:
601 TOS = ( TOS != M_POP ) ? FTRUE : FFALSE ;
603 case ID_COMP_GREATERTHAN:
604 TOS = ( M_POP > TOS ) ? FTRUE : FFALSE ;
606 case ID_COMP_LESSTHAN:
607 TOS = ( M_POP < TOS ) ? FTRUE : FFALSE ;
609 case ID_COMP_U_GREATERTHAN:
610 TOS = ( ((ucell_t)M_POP) > ((ucell_t)TOS) ) ? FTRUE : FFALSE ;
612 case ID_COMP_U_LESSTHAN:
613 TOS = ( ((ucell_t)M_POP) < ((ucell_t)TOS) ) ? FTRUE : FFALSE ;
615 case ID_COMP_ZERO_EQUAL:
616 TOS = ( TOS == 0 ) ? FTRUE : FFALSE ;
618 case ID_COMP_ZERO_NOT_EQUAL:
619 TOS = ( TOS != 0 ) ? FTRUE : FALSE ;
621 case ID_COMP_ZERO_GREATERTHAN:
622 TOS = ( TOS > 0 ) ? FTRUE : FFALSE ;
624 case ID_COMP_ZERO_LESSTHAN:
625 TOS = ( TOS < 0 ) ? FTRUE : FFALSE ;
638 #endif /* !PF_NO_SHELL */
642 /* Put address of body on stack. Insptr points after code start. */
643 TOS = (cell_t) ((char *)InsPtr - sizeof(cell_t) + CREATE_BODY_OFFSET );
646 case ID_CSTORE: /* ( c caddr -- ) */
647 *((uint8_t *) TOS) = (uint8_t) M_POP;
651 /* Double precision add. */
652 case ID_D_PLUS: /* D+ ( al ah bl bh -- sl sh ) */
654 register ucell_t ah,al,bl,sh,sl;
661 if( sl < bl ) sh = 1; /* Carry */
669 /* Double precision subtract. */
670 case ID_D_MINUS: /* D- ( al ah bl bh -- sl sh ) */
672 register ucell_t ah,al,bl,sh,sl;
679 if( al < bl ) sh = 1; /* Borrow */
687 /* Assume 8-bit char and calculate cell width. */
688 #define NBITS ((sizeof(ucell_t)) * 8)
689 /* Define half the number of bits in a cell. */
690 #define HNBITS (NBITS / 2)
691 /* Assume two-complement arithmetic to calculate lower half. */
692 #define LOWER_HALF(n) ((n) & (((ucell_t)1 << HNBITS) - 1))
693 #define HIGH_BIT ((ucell_t)1 << (NBITS - 1))
695 /* Perform cell*cell bit multiply for a 2 cell result, by factoring into half cell quantities.
696 * Using an improved algorithm suggested by Steve Green.
697 * Converted to 64-bit by Aleksej Saushev.
699 case ID_D_UMTIMES: /* UM* ( a b -- lo hi ) */
701 ucell_t ahi, alo, bhi, blo; /* input parts */
702 ucell_t lo, hi, temp;
703 /* Get values from stack. */
706 /* Break into hi and lo 16 bit parts. */
707 alo = LOWER_HALF(ahi);
709 blo = LOWER_HALF(bhi);
714 /* higher part: ahi * bhi */
716 /* middle (overlapping) part: ahi * blo */
718 lo += LOWER_HALF(temp);
719 hi += temp >> HNBITS;
720 /* middle (overlapping) part: alo * bhi */
722 lo += LOWER_HALF(temp);
723 hi += temp >> HNBITS;
724 /* lower part: alo * blo */
726 /* its higher half overlaps with middle's lower half: */
727 lo += temp >> HNBITS;
731 /* combine lower part of result: */
732 lo = (lo << HNBITS) + LOWER_HALF(temp);
739 /* Perform cell*cell bit multiply for 2 cell result, using shift and add. */
740 case ID_D_MTIMES: /* M* ( a b -- pl ph ) */
742 ucell_t ahi, alo, bhi, blo; /* input parts */
743 ucell_t lo, hi, temp;
745 /* Get values from stack. */
749 /* Calculate product sign: */
750 sg = ((cell_t)(ahi ^ bhi) < 0);
751 /* Take absolute values and reduce to um* */
752 if ((cell_t)ahi < 0) ahi = (ucell_t)(-(cell_t)ahi);
753 if ((cell_t)bhi < 0) bhi = (ucell_t)(-(cell_t)bhi);
755 /* Break into hi and lo 16 bit parts. */
756 alo = LOWER_HALF(ahi);
758 blo = LOWER_HALF(bhi);
763 /* higher part: ahi * bhi */
765 /* middle (overlapping) part: ahi * blo */
767 lo += LOWER_HALF(temp);
768 hi += temp >> HNBITS;
769 /* middle (overlapping) part: alo * bhi */
771 lo += LOWER_HALF(temp);
772 hi += temp >> HNBITS;
773 /* lower part: alo * blo */
775 /* its higher half overlaps with middle's lower half: */
776 lo += temp >> HNBITS;
780 /* combine lower part of result: */
781 lo = (lo << HNBITS) + LOWER_HALF(temp);
783 /* Negate product if one operand negative. */
786 /* lo = (ucell_t)(- lo); */
788 hi = ~hi + ((lo == 0) ? 1 : 0);
796 #define DULT(du1l,du1h,du2l,du2h) ( (du2h<du1h) ? FALSE : ( (du2h==du1h) ? (du1l<du2l) : TRUE) )
797 /* Perform 2 cell by 1 cell divide for 1 cell result and remainder, using shift and subtract. */
798 case ID_D_UMSMOD: /* UM/MOD ( al ah bdiv -- rem q ) */
800 ucell_t ah,al, q,di, bl,bh, sl,sh;
806 for( di=0; di<NBITS; di++ )
808 if( !DULT(al,ah,bl,bh) )
812 if( al < bl ) sh = 1; /* Borrow */
819 bl = (bl >> 1) | (bh << (NBITS-1));
822 if( !DULT(al,ah,bl,bh) )
828 M_PUSH( al ); /* rem */
833 /* Perform 2 cell by 1 cell divide for 2 cell result and remainder, using shift and subtract. */
834 case ID_D_MUSMOD: /* MU/MOD ( al am bdiv -- rem ql qh ) */
836 register ucell_t ah,am,al,ql,qh,di;
837 #define bdiv ((ucell_t)TOS)
842 for( di=0; di<2*NBITS; di++ )
849 qh = (qh << 1) | (ql >> (NBITS-1));
851 ah = (ah << 1) | (am >> (NBITS-1));
852 am = (am << 1) | (al >> (NBITS-1));
854 DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql ));
861 M_PUSH( ah ); /* rem */
872 #endif /* !PF_NO_SHELL */
879 TOS = gCurrentTask->td_StackBase - STKPTR;
882 case ID_DIVIDE: BINARY_OP( / ); endcase;
893 case ID_DROP: M_DROP; endcase;
897 DumpMemory( (char *) Scratch, TOS );
901 case ID_DUP: M_DUP; endcase;
903 case ID_DO_P: /* ( limit start -- ) ( R: -- start limit ) */
909 case ID_EOL: /* ( -- end_of_line_char ) */
914 case ID_ERRORQ_P: /* ( flag num -- , quit if flag true ) */
933 /* Save IP on return stack like a JSR. */
935 #ifdef PF_SUPPORT_TRACE
936 /* Bump level for trace. */
939 if( IsTokenPrimitive( TOS ) )
941 WRITE_CELL_DIC( (cell_t *) &FakeSecondary[0], TOS); /* Build a fake secondary and execute it. */
942 InsPtr = &FakeSecondary[0];
946 InsPtr = (cell_t *) LOCAL_CODEREL_TO_ABS(TOS);
952 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
955 TOS = (cell_t) READ_CELL_DIC((cell_t *)TOS);
959 TOS = *((cell_t *)TOS);
962 TOS = *((cell_t *)TOS);
966 case ID_FILE_CREATE: /* ( c-addr u fam -- fid ior ) */
967 /* Build NUL terminated name string. */
968 Scratch = M_POP; /* u */
969 Temp = M_POP; /* caddr */
970 if( Scratch < TIB_SIZE-2 )
972 const char *famText = pfSelectFileModeCreate( TOS );
973 pfCopyMemory( gScratch, (char *) Temp, (ucell_t) Scratch );
974 gScratch[Scratch] = '\0';
975 DBUG(("Create file = %s with famTxt %s\n", gScratch, famText ));
976 FileID = sdOpenFile( gScratch, famText );
977 TOS = ( FileID == NULL ) ? -1 : 0 ;
978 M_PUSH( (cell_t) FileID );
982 ERR("Filename too large for name buffer.\n");
988 case ID_FILE_DELETE: /* ( c-addr u -- ior ) */
989 /* Build NUL terminated name string. */
990 Temp = M_POP; /* caddr */
991 if( TOS < TIB_SIZE-2 )
993 pfCopyMemory( gScratch, (char *) Temp, (ucell_t) TOS );
994 gScratch[TOS] = '\0';
995 DBUG(("Delete file = %s\n", gScratch ));
996 TOS = sdDeleteFile( gScratch );
1000 ERR("Filename too large for name buffer.\n");
1005 case ID_FILE_OPEN: /* ( c-addr u fam -- fid ior ) */
1006 /* Build NUL terminated name string. */
1007 Scratch = M_POP; /* u */
1008 Temp = M_POP; /* caddr */
1009 if( Scratch < TIB_SIZE-2 )
1011 const char *famText = pfSelectFileModeOpen( TOS );
1012 pfCopyMemory( gScratch, (char *) Temp, (ucell_t) Scratch );
1013 gScratch[Scratch] = '\0';
1014 DBUG(("Open file = %s\n", gScratch ));
1015 FileID = sdOpenFile( gScratch, famText );
1017 TOS = ( FileID == NULL ) ? -1 : 0 ;
1018 M_PUSH( (cell_t) FileID );
1022 ERR("Filename too large for name buffer.\n");
1028 case ID_FILE_CLOSE: /* ( fid -- ior ) */
1029 TOS = sdCloseFile( (FileStream *) TOS );
1032 case ID_FILE_READ: /* ( addr len fid -- u2 ior ) */
1033 FileID = (FileStream *) TOS;
1035 CharPtr = (char *) M_POP;
1036 Temp = sdReadFile( CharPtr, 1, Scratch, FileID );
1037 /* TODO check feof() or ferror() */
1042 /* TODO Why does this crash when passed an illegal FID? */
1043 case ID_FILE_SIZE: /* ( fid -- ud ior ) */
1044 /* Determine file size by seeking to end and returning position. */
1045 FileID = (FileStream *) TOS;
1047 file_offset_t endposition = -1;
1048 file_offset_t original = sdTellFile( FileID );
1051 sdSeekFile( FileID, 0, PF_SEEK_END );
1052 endposition = sdTellFile( FileID );
1053 /* Restore original position. */
1054 sdSeekFile( FileID, original, PF_SEEK_SET );
1056 if (endposition < 0)
1058 M_PUSH(0); /* low */
1059 M_PUSH(0); /* high */
1060 TOS = -4; /* TODO proper error number */
1064 M_PUSH(endposition); /* low */
1065 /* We do not support double precision file offsets.*/
1066 M_PUSH(0); /* high */
1072 case ID_FILE_WRITE: /* ( addr len fid -- ior ) */
1073 FileID = (FileStream *) TOS;
1075 CharPtr = (char *) M_POP;
1076 Temp = sdWriteFile( CharPtr, 1, Scratch, FileID );
1077 TOS = (Temp != Scratch) ? -3 : 0;
1080 case ID_FILE_REPOSITION: /* ( ud fid -- ior ) */
1082 file_offset_t offset;
1085 FileID = (FileStream *) TOS;
1088 /* We do not support double precision file offsets in pForth.
1089 * So check to make sure the high bits are not used.
1091 if (offsetHigh != 0)
1093 TOS = -3; /* TODO err num? */
1096 offset = (file_offset_t)offsetLow;
1097 TOS = sdSeekFile( FileID, offset, PF_SEEK_SET );
1101 case ID_FILE_POSITION: /* ( fid -- ud ior ) */
1103 file_offset_t position;
1104 FileID = (FileStream *) TOS;
1105 position = sdTellFile( FileID );
1108 M_PUSH(0); /* low */
1109 M_PUSH(0); /* high */
1110 TOS = -4; /* TODO proper error number */
1114 M_PUSH(position); /* low */
1115 /* We do not support double precision file offsets.*/
1116 M_PUSH(0); /* high */
1122 case ID_FILE_RO: /* ( -- fam ) */
1124 TOS = PF_FAM_READ_ONLY;
1127 case ID_FILE_RW: /* ( -- fam ) */
1129 TOS = PF_FAM_READ_WRITE;
1132 case ID_FILE_WO: /* ( -- fam ) */
1134 TOS = PF_FAM_WRITE_ONLY;
1137 case ID_FILE_BIN: /* ( fam1 -- fam2 ) */
1138 TOS = TOS | PF_FAM_BINARY_FLAG;
1141 case ID_FILE_FLUSH: /* ( fileid -- ior ) */
1143 FileStream *Stream = (FileStream *) TOS;
1144 TOS = (sdFlushFile( Stream ) == 0) ? 0 : THROW_FLUSH_FILE;
1148 case ID_FILE_RENAME: /* ( oldName newName -- ior ) */
1150 char *New = (char *) TOS;
1151 char *Old = (char *) M_POP;
1152 TOS = sdRenameFile( Old, New );
1156 case ID_FILE_RESIZE: /* ( ud fileid -- ior ) */
1158 FileStream *File = (FileStream *) TOS;
1159 ucell_t SizeHi = (ucell_t) M_POP;
1160 ucell_t SizeLo = (ucell_t) M_POP;
1161 TOS = ( UdIsUint64( SizeLo, SizeHi )
1162 ? sdResizeFile( File, UdToUint64( SizeLo, SizeHi ))
1163 : THROW_RESIZE_FILE );
1167 case ID_FILL: /* ( caddr num charval -- ) */
1169 register char *DstPtr;
1170 Temp = M_POP; /* num */
1171 DstPtr = (char *) M_POP; /* dst */
1172 for( Scratch=0; (ucell_t) Scratch < (ucell_t) Temp ; Scratch++ )
1174 *DstPtr++ = (char) TOS;
1181 case ID_FIND: /* ( $addr -- $addr 0 | xt +-1 ) */
1182 TOS = ffFind( (char *) TOS, (ExecToken *) &Temp );
1187 TOS = ffFindNFA( (const ForthString *) TOS, (const ForthString **) &Temp );
1188 M_PUSH( (cell_t) Temp );
1190 #endif /* !PF_NO_SHELL */
1196 /* Validate memory before freeing. Clobber validator and first word. */
1197 case ID_FREE: /* ( addr -- result ) */
1200 ERR("FREE passed NULL!\n");
1201 TOS = -2; /* FIXME error code */
1205 CellPtr = (cell_t *) TOS;
1207 if( ((ucell_t)*CellPtr) != ((ucell_t)CellPtr ^ PF_MEMORY_VALIDATOR))
1209 TOS = -2; /* FIXME error code */
1213 CellPtr[0] = 0xDeadBeef;
1214 pfFreeMem((char *)CellPtr);
1220 #include "pfinnrfp.h"
1224 TOS = (cell_t)CODE_HERE;
1227 case ID_NUMBERQ_P: /* ( addr -- 0 | n 1 ) */
1228 /* Convert using number converter in 'C'.
1229 ** Only supports single precision for bootstrap.
1231 TOS = (cell_t) ffNumberQ( (char *) TOS, &Temp );
1232 if( TOS == NUM_TYPE_SINGLE)
1234 M_PUSH( Temp ); /* Push single number */
1238 case ID_I: /* ( -- i , DO LOOP index ) */
1244 case ID_INCLUDE_FILE:
1245 FileID = (FileStream *) TOS;
1246 M_DROP; /* Drop now so that INCLUDE has a clean stack. */
1248 Scratch = ffIncludeFile( FileID );
1250 if( Scratch ) M_THROW(Scratch)
1252 #endif /* !PF_NO_SHELL */
1257 Scratch = ffInterpret();
1259 if( Scratch ) M_THROW(Scratch)
1261 #endif /* !PF_NO_SHELL */
1263 case ID_J: /* ( -- j , second DO LOOP index ) */
1271 if (TOS == ASCII_EOT) {
1281 #endif /* !PF_NO_SHELL */
1284 DBUG(("ID_LITERAL_P: InsPtr = 0x%x, *InsPtr = 0x%x\n", InsPtr, *InsPtr ));
1286 TOS = READ_CELL_DIC(InsPtr++);
1290 case ID_LOCAL_COMPILER: DO_VAR(gLocalCompiler_XT); endcase;
1291 #endif /* !PF_NO_SHELL */
1293 case ID_LOCAL_FETCH: /* ( i <local> -- n , fetch from local ) */
1294 TOS = *(LocalsPtr - TOS);
1297 #define LOCAL_FETCH_N(num) \
1298 case ID_LOCAL_FETCH_##num: /* ( <local> -- n , fetch from local ) */ \
1300 TOS = *(LocalsPtr -(num)); \
1312 case ID_LOCAL_STORE: /* ( n i <local> -- , store n in local ) */
1313 *(LocalsPtr - TOS) = M_POP;
1317 #define LOCAL_STORE_N(num) \
1318 case ID_LOCAL_STORE_##num: /* ( n <local> -- , store n in local ) */ \
1319 *(LocalsPtr - (num)) = TOS; \
1332 case ID_LOCAL_PLUSSTORE: /* ( n i <local> -- , add n to local ) */
1333 *(LocalsPtr - TOS) += M_POP;
1337 case ID_LOCAL_ENTRY: /* ( x0 x1 ... xn n -- ) */
1338 /* create local stack frame */
1342 DBUG(("LocalEntry: n = %d\n", TOS));
1343 /* End of locals. Create stack frame */
1344 DBUG(("LocalEntry: before RP@ = 0x%x, LP = 0x%x\n",
1345 TORPTR, LocalsPtr));
1346 M_R_PUSH(LocalsPtr);
1349 DBUG(("LocalEntry: after RP@ = 0x%x, LP = 0x%x\n",
1350 TORPTR, LocalsPtr));
1354 *lp++ = M_POP; /* Load local vars from stack */
1360 case ID_LOCAL_EXIT: /* cleanup up local stack frame */
1361 DBUG(("LocalExit: before RP@ = 0x%x, LP = 0x%x\n",
1362 TORPTR, LocalsPtr));
1364 LocalsPtr = (cell_t *) M_R_POP;
1365 DBUG(("LocalExit: after RP@ = 0x%x, LP = 0x%x\n",
1366 TORPTR, LocalsPtr));
1371 MSG("Load "); MSG(SYSTEM_LOAD_FILE); EMIT_CR;
1372 FileID = sdOpenFile(SYSTEM_LOAD_FILE, "r");
1376 Scratch = ffIncludeFile( FileID ); /* Also closes the file. */
1378 if( Scratch ) M_THROW(Scratch);
1382 ERR(SYSTEM_LOAD_FILE); ERR(" could not be opened!\n");
1385 #endif /* !PF_NO_SHELL */
1387 case ID_LEAVE_P: /* ( R: index limit -- ) */
1393 case ID_LOOP_P: /* ( R: index limit -- | index limit ) */
1394 Temp = M_R_POP; /* limit */
1395 Scratch = M_R_POP + 1; /* index */
1396 if( Scratch == Temp )
1398 InsPtr++; /* skip branch offset, exit loop */
1402 /* Push index and limit back to R */
1403 M_R_PUSH( Scratch );
1405 /* Branch back to just after (DO) */
1410 case ID_LSHIFT: BINARY_OP( << ); endcase;
1414 TOS = ( TOS > Scratch ) ? TOS : Scratch ;
1419 TOS = ( TOS < Scratch ) ? TOS : Scratch ;
1422 case ID_MINUS: BINARY_OP( - ); endcase;
1425 case ID_NAME_TO_TOKEN:
1426 TOS = (cell_t) NameToToken((ForthString *)TOS);
1429 case ID_NAME_TO_PREVIOUS:
1430 TOS = (cell_t) NameToPrevious((ForthString *)TOS);
1437 case ID_OR: BINARY_OP( | ); endcase;
1444 case ID_PICK: /* ( ... n -- sp(n) ) */
1448 case ID_PLUS: BINARY_OP( + ); endcase;
1450 case ID_PLUS_STORE: /* ( n addr -- , add n to *addr ) */
1451 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1452 if( IN_DICS( TOS ) )
1454 Scratch = READ_CELL_DIC((cell_t *)TOS);
1456 WRITE_CELL_DIC((cell_t *)TOS,Scratch);
1460 *((cell_t *)TOS) += M_POP;
1463 *((cell_t *)TOS) += M_POP;
1468 case ID_PLUSLOOP_P: /* ( delta -- ) ( R: index limit -- | index limit ) */
1470 cell_t Limit = M_R_POP;
1471 cell_t OldIndex = M_R_POP;
1472 cell_t Delta = TOS; /* add TOS to index, not 1 */
1473 cell_t NewIndex = OldIndex + Delta;
1474 cell_t OldDiff = OldIndex - Limit;
1476 /* This exploits this idea (lifted from Gforth):
1477 (x^y)<0 is equivalent to (x<0) != (y<0) */
1478 if( ((OldDiff ^ (OldDiff + Delta)) /* is the limit crossed? */
1479 & (OldDiff ^ Delta)) /* is it a wrap-around? */
1482 InsPtr++; /* skip branch offset, exit loop */
1486 /* Push index and limit back to R */
1487 M_R_PUSH( NewIndex );
1489 /* Branch back to just after (DO) */
1496 case ID_QDO_P: /* (?DO) ( limit start -- ) ( R: -- start limit ) */
1497 Scratch = M_POP; /* limit */
1498 if( Scratch == TOS )
1500 /* Branch to just after (LOOP) */
1506 M_R_PUSH( Scratch );
1507 InsPtr++; /* skip branch offset, enter loop */
1512 case ID_QDUP: if( TOS ) M_DUP; endcase;
1514 case ID_QTERMINAL: /* WARNING: Typically not fully implemented! */
1516 TOS = sdQueryTerminal();
1519 case ID_QUIT_P: /* Stop inner interpreter, go back to user. */
1520 #ifdef PF_SUPPORT_TRACE
1523 M_THROW(THROW_QUIT);
1542 TOS = (ffRefill() > 0) ? FTRUE : FFALSE;
1545 /* Resize memory allocated by ALLOCATE. */
1546 case ID_RESIZE: /* ( addr1 u -- addr2 result ) */
1548 cell_t *Addr1 = (cell_t *) M_POP;
1549 /* Point to validator below users address. */
1550 cell_t *FreePtr = Addr1 - 1;
1551 if( ((ucell_t)*FreePtr) != ((ucell_t)FreePtr ^ PF_MEMORY_VALIDATOR))
1553 /* 090218 - Fixed bug, was returning zero. */
1559 /* Try to allocate. */
1560 CellPtr = (cell_t *) pfAllocMem( TOS + sizeof(cell_t) );
1563 /* Copy memory including validation. */
1564 pfCopyMemory( (char *) CellPtr, (char *) FreePtr, TOS + sizeof(cell_t) );
1565 *CellPtr = (cell_t)(((ucell_t)CellPtr) ^ (ucell_t)PF_MEMORY_VALIDATOR);
1566 /* 090218 - Fixed bug that was incrementing the address twice. Thanks Reinhold Straub. */
1567 /* Increment past validator to user address. */
1568 M_PUSH( (cell_t) (CellPtr + 1) );
1569 TOS = 0; /* Result code. */
1570 /* Mark old cell as dead so we can't free it twice. */
1571 FreePtr[0] = 0xDeadBeef;
1572 pfFreeMem((char *) FreePtr);
1576 /* 090218 - Fixed bug, was returning zero. */
1578 TOS = -4; /* FIXME Fix error code. */
1585 ** RP@ and RP! are called secondaries so we must
1586 ** account for the return address pushed before calling.
1588 case ID_RP_FETCH: /* ( -- rp , address of top of return stack ) */
1590 TOS = (cell_t)TORPTR; /* value before calling RP@ */
1593 case ID_RP_STORE: /* ( rp -- , address of top of return stack ) */
1594 TORPTR = (cell_t *) TOS;
1598 case ID_ROLL: /* ( xu xu-1 xu-1 ... x0 u -- xu-1 xu-1 ... x0 xu ) */
1601 cell_t *srcPtr, *dstPtr;
1602 Scratch = M_STACK(TOS);
1603 srcPtr = &M_STACK(TOS-1);
1604 dstPtr = &M_STACK(TOS);
1605 for( ri=0; ri<TOS; ri++ )
1607 *dstPtr-- = *srcPtr--;
1614 case ID_ROT: /* ( a b c -- b c a ) */
1615 Scratch = M_POP; /* b */
1616 Temp = M_POP; /* a */
1617 M_PUSH( Scratch ); /* b */
1622 /* Logical right shift */
1623 case ID_RSHIFT: { TOS = ((ucell_t)M_POP) >> TOS; } endcase;
1626 case ID_SAVE_FORTH_P: /* ( $name Entry NameSize CodeSize -- err ) */
1628 cell_t NameSize, CodeSize, EntryPoint;
1632 ForthStringToC( gScratch, (char *) M_POP, sizeof(gScratch) );
1633 TOS = ffSaveForth( gScratch, EntryPoint, NameSize, CodeSize );
1639 TOS = sdSleepMillis(TOS);
1642 case ID_SP_FETCH: /* ( -- sp , address of top of stack, sorta ) */
1644 TOS = (cell_t)STKPTR;
1647 case ID_SP_STORE: /* ( sp -- , address of top of stack, sorta ) */
1648 STKPTR = (cell_t *) TOS;
1652 case ID_STORE: /* ( n addr -- , write n to addr ) */
1653 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1654 if( IN_DICS( TOS ) )
1656 WRITE_CELL_DIC((cell_t *)TOS,M_POP);
1660 *((cell_t *)TOS) = M_POP;
1663 *((cell_t *)TOS) = M_POP;
1668 case ID_SCAN: /* ( addr cnt char -- addr' cnt' ) */
1669 Scratch = M_POP; /* cnt */
1670 Temp = M_POP; /* addr */
1671 TOS = ffScan( (char *) Temp, Scratch, (char) TOS, &CharPtr );
1672 M_PUSH((cell_t) CharPtr);
1678 Scratch = ffSemiColon();
1680 if( Scratch ) M_THROW( Scratch );
1682 #endif /* !PF_NO_SHELL */
1684 case ID_SKIP: /* ( addr cnt char -- addr' cnt' ) */
1685 Scratch = M_POP; /* cnt */
1686 Temp = M_POP; /* addr */
1687 TOS = ffSkip( (char *) Temp, Scratch, (char) TOS, &CharPtr );
1688 M_PUSH((cell_t) CharPtr);
1691 case ID_SOURCE: /* ( -- c-addr num ) */
1693 M_PUSH( (cell_t) gCurrentTask->td_SourcePtr );
1694 TOS = (cell_t) gCurrentTask->td_SourceNum;
1697 case ID_SOURCE_SET: /* ( c-addr num -- ) */
1698 gCurrentTask->td_SourcePtr = (char *) M_POP;
1699 gCurrentTask->td_SourceNum = TOS;
1705 TOS = ffConvertStreamToSourceID( gCurrentTask->td_InputStream ) ;
1708 case ID_SOURCE_ID_POP:
1710 TOS = ffConvertStreamToSourceID( ffPopInputStream() ) ;
1713 case ID_SOURCE_ID_PUSH: /* ( source-id -- ) */
1714 TOS = (cell_t)ffConvertSourceIDToStream( TOS );
1715 Scratch = ffPushInputStream((FileStream *) TOS );
1723 case ID_SOURCE_LINE_NUMBER_FETCH: /* ( -- linenr ) */
1725 TOS = gCurrentTask->td_LineNumber;
1728 case ID_SOURCE_LINE_NUMBER_STORE: /* ( linenr -- ) */
1729 gCurrentTask->td_LineNumber = TOS;
1749 case ID_THROW: /* ( k*x err -- k*x | i*x err , jump to where CATCH was called ) */
1760 CharPtr = (char *) ffWord( (char) ' ' );
1761 TOS = ffFind( CharPtr, (ExecToken *) &Temp );
1764 ERR("' could not find ");
1765 ioType( (char *) CharPtr+1, *CharPtr );
1773 #endif /* !PF_NO_SHELL */
1775 case ID_TIMES: BINARY_OP( * ); endcase;
1778 Scratch = M_POP; /* addr */
1779 ioType( (char *) Scratch, TOS );
1788 case ID_VAR_BASE: DO_VAR(gVarBase); endcase;
1789 case ID_VAR_CODE_BASE: DO_VAR(gCurrentDictionary->dic_CodeBase); endcase;
1790 case ID_VAR_CODE_LIMIT: DO_VAR(gCurrentDictionary->dic_CodeLimit); endcase;
1791 case ID_VAR_CONTEXT: DO_VAR(gVarContext); endcase;
1792 case ID_VAR_DP: DO_VAR(gCurrentDictionary->dic_CodePtr.Cell); endcase;
1793 case ID_VAR_ECHO: DO_VAR(gVarEcho); endcase;
1794 case ID_VAR_HEADERS_BASE: DO_VAR(gCurrentDictionary->dic_HeaderBase); endcase;
1795 case ID_VAR_HEADERS_LIMIT: DO_VAR(gCurrentDictionary->dic_HeaderLimit); endcase;
1796 case ID_VAR_HEADERS_PTR: DO_VAR(gCurrentDictionary->dic_HeaderPtr); endcase;
1797 case ID_VAR_NUM_TIB: DO_VAR(gCurrentTask->td_SourceNum); endcase;
1798 case ID_VAR_OUT: DO_VAR(gCurrentTask->td_OUT); endcase;
1799 case ID_VAR_STATE: DO_VAR(gVarState); endcase;
1800 case ID_VAR_TO_IN: DO_VAR(gCurrentTask->td_IN); endcase;
1801 case ID_VAR_TRACE_FLAGS: DO_VAR(gVarTraceFlags); endcase;
1802 case ID_VAR_TRACE_LEVEL: DO_VAR(gVarTraceLevel); endcase;
1803 case ID_VAR_TRACE_STACK: DO_VAR(gVarTraceStack); endcase;
1804 case ID_VAR_RETURN_CODE: DO_VAR(gVarReturnCode); endcase;
1807 TOS = (cell_t) ffWord( (char) TOS );
1810 case ID_WORD_FETCH: /* ( waddr -- w ) */
1811 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1812 if( IN_DICS( TOS ) )
1814 TOS = (uint16_t) READ_SHORT_DIC((uint16_t *)TOS);
1818 TOS = *((uint16_t *)TOS);
1821 TOS = *((uint16_t *)TOS);
1825 case ID_WORD_STORE: /* ( w waddr -- ) */
1827 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1828 if( IN_DICS( TOS ) )
1830 WRITE_SHORT_DIC((uint16_t *)TOS,(uint16_t)M_POP);
1834 *((uint16_t *)TOS) = (uint16_t) M_POP;
1837 *((uint16_t *)TOS) = (uint16_t) M_POP;
1842 case ID_XOR: BINARY_OP( ^ ); endcase;
1845 /* Branch is followed by an offset relative to address of offset. */
1846 case ID_ZERO_BRANCH:
1847 DBUGX(("Before 0Branch: IP = 0x%x\n", InsPtr ));
1854 InsPtr++; /* skip over offset */
1857 DBUGX(("After 0Branch: IP = 0x%x\n", InsPtr ));
1861 ERR("pfCatch: Unrecognised token = 0x");
1864 ffDotHex((cell_t) InsPtr);
1870 if(InsPtr) Token = READ_CELL_DIC(InsPtr++); /* Traverse to next token in secondary. */
1877 if( _CrtCheckMemory() == 0 )
1879 ERR("_CrtCheckMemory abort: InsPtr = 0x");
1880 ffDotHex((int)InsPtr);
1885 } while( (InitialReturnStack - TORPTR) > 0 );
1889 return ExceptionReturnCode;