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; }
53 /***************************************************************
54 ** Macros for Floating Point stack access.
55 ***************************************************************/
57 #define FP_STKPTR (FloatStackPtr)
58 #define M_FP_SPZERO (gCurrentTask->td_FloatStackBase)
59 #define M_FP_POP (*(FP_STKPTR++))
60 #define M_FP_PUSH(n) {*(--(FP_STKPTR)) = (PF_FLOAT) (n);}
61 #define M_FP_STACK(n) (FP_STKPTR[n])
63 #define FP_TOS (fpTopOfStack)
64 #define PUSH_FP_TOS M_FP_PUSH(FP_TOS)
65 #define M_FP_DUP PUSH_FP_TOS;
66 #define M_FP_DROP { FP_TOS = M_FP_POP; }
69 /***************************************************************
70 ** Macros for return stack access.
71 ***************************************************************/
73 #define TORPTR (ReturnStackPtr)
74 #define M_R_DROP {TORPTR++;}
75 #define M_R_POP (*(TORPTR++))
76 #define M_R_PICK(n) (TORPTR[n])
77 #define M_R_PUSH(n) {*(--(TORPTR)) = (cell_t) (n);}
79 /***************************************************************
81 ***************************************************************/
83 #define M_BRANCH { InsPtr = (cell_t *) (((uint8_t *) InsPtr) + READ_CELL_DIC(InsPtr)); }
85 /* Cache top of data stack like in JForth. */
87 #define LOAD_REGISTERS \
89 STKPTR = gCurrentTask->td_StackPtr; \
91 FP_STKPTR = gCurrentTask->td_FloatStackPtr; \
93 TORPTR = gCurrentTask->td_ReturnPtr; \
96 #define SAVE_REGISTERS \
98 gCurrentTask->td_ReturnPtr = TORPTR; \
100 gCurrentTask->td_StackPtr = STKPTR; \
101 M_FP_PUSH( FP_TOS ); \
102 gCurrentTask->td_FloatStackPtr = FP_STKPTR; \
106 /* Cache top of data stack like in JForth. */
107 #define LOAD_REGISTERS \
109 STKPTR = gCurrentTask->td_StackPtr; \
111 TORPTR = gCurrentTask->td_ReturnPtr; \
114 #define SAVE_REGISTERS \
116 gCurrentTask->td_ReturnPtr = TORPTR; \
118 gCurrentTask->td_StackPtr = STKPTR; \
127 #define DO_VAR(varname) { PUSH_TOS; TOS = (cell_t) &varname; }
130 #define M_THROW(err) \
132 ExceptionReturnCode = (ThrowCode)(err); \
133 TORPTR = InitialReturnStack; /* Will cause return to 'C' */ \
134 STKPTR = InitialDataStack; \
135 FP_STKPTR = InitialFloatStack; \
138 #define M_THROW(err) \
140 ExceptionReturnCode = (err); \
141 TORPTR = InitialReturnStack; /* Will cause return to 'C' */ \
142 STKPTR = InitialDataStack; \
146 /***************************************************************
148 ***************************************************************/
150 #define BINARY_OP( op ) { TOS = M_POP op TOS; }
151 #define endcase break
153 #if defined(PF_NO_SHELL) || !defined(PF_SUPPORT_TRACE)
154 #define TRACENAMES /* no names */
156 /* Display name of executing routine. */
157 static void TraceNames( ExecToken Token, cell_t Level )
162 if( ffTokenToName( Token, &DebugName ) )
165 if( gCurrentTask->td_OUT > 0 ) EMIT_CR;
167 for( i=0; i<Level; i++ )
171 TypeName( DebugName );
172 /* Space out to column N then .S */
173 NumSpaces = 30 - gCurrentTask->td_OUT;
174 for( i=0; i < NumSpaces; i++ )
179 /* No longer needed? gCurrentTask->td_OUT = 0; */ /* !!! Hack for ffDotS() */
184 MSG_NUM_H("Couldn't find Name for ", Token);
189 if( (gVarTraceLevel > Level) ) \
190 { SAVE_REGISTERS; TraceNames( Token, Level ); LOAD_REGISTERS; }
191 #endif /* PF_NO_SHELL */
193 /* Use local copy of CODE_BASE for speed. */
194 #define LOCAL_CODEREL_TO_ABS( a ) ((cell_t *) (((cell_t) a) + CodeBase))
196 /* Truncate the unsigned double cell integer LO/HI to an uint64_t. */
197 static uint64_t UdToUint64( ucell_t Lo, ucell_t Hi )
199 return (( 2 * sizeof(ucell_t) == sizeof(uint64_t) )
200 ? (((uint64_t)Lo) | (((uint64_t)Hi) >> (sizeof(ucell_t) * 8)))
204 /* Return TRUE if the unsigned double cell integer LO/HI is not greater
205 * then the greatest uint64_t.
207 static int UdIsUint64( ucell_t Lo, ucell_t Hi )
209 return (( 2 * sizeof(ucell_t) == sizeof(uint64_t) )
214 static const char *pfSelectFileModeCreate( int fam );
215 static const char *pfSelectFileModeOpen( int fam );
217 /**************************************************************/
218 static const char *pfSelectFileModeCreate( int fam )
220 const char *famText = NULL;
223 case (PF_FAM_WRITE_ONLY + PF_FAM_BINARY_FLAG):
224 famText = PF_FAM_BIN_CREATE_WO;
226 case (PF_FAM_READ_WRITE + PF_FAM_BINARY_FLAG):
227 famText = PF_FAM_BIN_CREATE_RW;
229 case PF_FAM_WRITE_ONLY:
230 famText = PF_FAM_CREATE_WO;
232 case PF_FAM_READ_WRITE:
233 famText = PF_FAM_CREATE_RW;
242 /**************************************************************/
243 static const char *pfSelectFileModeOpen( int fam )
245 const char *famText = NULL;
248 case (PF_FAM_READ_ONLY + PF_FAM_BINARY_FLAG):
249 famText = PF_FAM_BIN_OPEN_RO;
251 case (PF_FAM_WRITE_ONLY + PF_FAM_BINARY_FLAG):
252 famText = PF_FAM_BIN_CREATE_WO;
254 case (PF_FAM_READ_WRITE + PF_FAM_BINARY_FLAG):
255 famText = PF_FAM_BIN_OPEN_RW;
257 case PF_FAM_READ_ONLY:
258 famText = PF_FAM_OPEN_RO;
260 case PF_FAM_WRITE_ONLY:
261 famText = PF_FAM_CREATE_WO;
263 case PF_FAM_READ_WRITE:
265 famText = PF_FAM_OPEN_RW;
271 /**************************************************************/
272 int pfCatch( ExecToken XT )
274 register cell_t TopOfStack; /* Cache for faster execution. */
275 register cell_t *DataStackPtr;
276 register cell_t *ReturnStackPtr;
277 register cell_t *InsPtr = NULL;
278 register cell_t Token;
282 PF_FLOAT fpTopOfStack;
283 PF_FLOAT *FloatStackPtr;
286 PF_FLOAT *InitialFloatStack;
288 #ifdef PF_SUPPORT_TRACE
291 cell_t *LocalsPtr = NULL;
293 cell_t *InitialReturnStack;
294 cell_t *InitialDataStack;
295 cell_t FakeSecondary[2];
299 uint8_t *CodeBase = (uint8_t *) CODE_BASE;
300 ThrowCode ExceptionReturnCode = 0;
303 gExecutionDepth += 1;
304 PRT(("pfCatch( 0x%x ), depth = %d\n", XT, gExecutionDepth ));
308 ** Initialize FakeSecondary this way to avoid having stuff in the data section,
309 ** which is not supported for some embedded system loaders.
311 FakeSecondary[0] = 0;
312 FakeSecondary[1] = ID_EXIT; /* For EXECUTE */
314 /* Move data from task structure to registers for speed. */
317 /* Save initial stack depths for THROW */
318 InitialReturnStack = TORPTR;
319 InitialDataStack = STKPTR ;
321 InitialFloatStack = FP_STKPTR;
328 DBUG(("pfCatch: Token = 0x%x\n", Token ));
330 /* --------------------------------------------------------------- */
331 /* If secondary, thread down code tree until we hit a primitive. */
332 while( !IsTokenPrimitive( Token ) )
334 #ifdef PF_SUPPORT_TRACE
335 if((gVarTraceFlags & TRACE_INNER) )
337 MSG("pfCatch: Secondary Token = 0x");
339 MSG_NUM_H(", InsPtr = 0x", InsPtr);
344 /* Save IP on return stack like a JSR. */
347 /* Convert execution token to absolute address. */
348 InsPtr = (cell_t *) ( LOCAL_CODEREL_TO_ABS(Token) );
350 /* Fetch token at IP. */
351 Token = READ_CELL_DIC(InsPtr++);
353 #ifdef PF_SUPPORT_TRACE
354 /* Bump level for trace display */
360 #ifdef PF_SUPPORT_TRACE
364 /* Execute primitive Token. */
368 /* Pop up a level in Forth inner interpreter.
369 ** Used to implement semicolon.
370 ** Put first in switch because ID_EXIT==0 */
372 InsPtr = ( cell_t *) M_R_POP;
373 #ifdef PF_SUPPORT_TRACE
378 case ID_1MINUS: TOS--; endcase;
380 case ID_1PLUS: TOS++; endcase;
384 ff2Literal( TOS, M_POP );
387 #endif /* !PF_NO_SHELL */
390 /* hi part stored first, put on top of stack */
392 TOS = READ_CELL_DIC(InsPtr++);
393 M_PUSH(READ_CELL_DIC(InsPtr++));
396 case ID_2MINUS: TOS -= 2; endcase;
398 case ID_2PLUS: TOS += 2; endcase;
401 case ID_2OVER: /* ( a b c d -- a b c d a b ) */
403 Scratch = M_STACK(3);
408 case ID_2SWAP: /* ( a b c d -- c d a b ) */
409 Scratch = M_STACK(0); /* c */
410 M_STACK(0) = M_STACK(2); /* a */
411 M_STACK(2) = Scratch; /* c */
412 Scratch = TOS; /* d */
413 TOS = M_STACK(1); /* b */
414 M_STACK(1) = Scratch; /* d */
417 case ID_2DUP: /* ( a b -- a b a b ) */
419 Scratch = M_STACK(1);
425 M_PUSH( (*(TORPTR+1)) );
441 case ID_ACCEPT_P: /* ( c-addr +n1 -- +n2 ) */
442 CharPtr = (char *) M_POP;
443 TOS = ioAccept( CharPtr, TOS );
448 ffALiteral( ABS_TO_CODEREL(TOS) );
451 #endif /* !PF_NO_SHELL */
455 TOS = (cell_t) LOCAL_CODEREL_TO_ABS( READ_CELL_DIC(InsPtr++) );
458 /* Allocate some extra and put validation identifier at base */
459 #define PF_MEMORY_VALIDATOR (0xA81B4D69)
461 /* Allocate at least one cell's worth because we clobber first cell. */
462 if ( TOS < sizeof(cell_t) )
464 Temp = sizeof(cell_t);
470 /* Allocate extra cells worth because we store validation info. */
471 CellPtr = (cell_t *) pfAllocMem( Temp + sizeof(cell_t) );
474 /* This was broken into two steps because different compilers incremented
475 ** CellPtr before or after the XOR step. */
476 Temp = (cell_t)CellPtr ^ PF_MEMORY_VALIDATOR;
478 M_PUSH( (cell_t) CellPtr );
484 TOS = -1; /* FIXME Fix error code. */
488 case ID_AND: BINARY_OP( & ); endcase;
490 case ID_ARSHIFT: BINARY_OP( >> ); endcase; /* Arithmetic right shift */
494 TOS = CREATE_BODY_OFFSET;
497 /* Branch is followed by an offset relative to address of offset. */
499 DBUGX(("Before Branch: IP = 0x%x\n", InsPtr ));
501 DBUGX(("After Branch: IP = 0x%x\n", InsPtr ));
505 M_THROW( THROW_BYE );
509 MSG("Emergency exit.\n");
517 Scratch = pfCatch( Scratch );
525 Scratch = READ_CELL_DIC(InsPtr++);
526 CallUserFunction( Scratch & 0xFFFF,
528 (Scratch >> 24) & 0x7F );
532 /* Support 32/64 bit operation. */
535 TOS = sizeof(cell_t);
539 TOS = TOS * sizeof(cell_t);
542 case ID_CFETCH: TOS = *((uint8_t *) TOS); endcase;
544 case ID_CMOVE: /* ( src dst n -- ) */
546 register char *DstPtr = (char *) M_POP; /* dst */
547 CharPtr = (char *) M_POP; /* src */
548 for( Scratch=0; (ucell_t) Scratch < (ucell_t) TOS ; Scratch++ )
550 *DstPtr++ = *CharPtr++;
556 case ID_CMOVE_UP: /* ( src dst n -- ) */
558 register char *DstPtr = ((char *) M_POP) + TOS; /* dst */
559 CharPtr = ((char *) M_POP) + TOS;; /* src */
560 for( Scratch=0; (ucell_t) Scratch < (ucell_t) TOS ; Scratch++ )
562 *(--DstPtr) = *(--CharPtr);
574 case ID_COLON_P: /* ( $name xt -- ) */
575 CreateDicEntry( TOS, (char *) M_POP, 0 );
578 #endif /* !PF_NO_SHELL */
584 s2 = (const char *) M_POP;
586 s1 = (const char *) M_POP;
587 TOS = ffCompare( s1, len1, s2, TOS );
591 /* ( a b -- flag , Comparisons ) */
593 TOS = ( TOS == M_POP ) ? FTRUE : FFALSE ;
595 case ID_COMP_NOT_EQUAL:
596 TOS = ( TOS != M_POP ) ? FTRUE : FFALSE ;
598 case ID_COMP_GREATERTHAN:
599 TOS = ( M_POP > TOS ) ? FTRUE : FFALSE ;
601 case ID_COMP_LESSTHAN:
602 TOS = ( M_POP < TOS ) ? FTRUE : FFALSE ;
604 case ID_COMP_U_GREATERTHAN:
605 TOS = ( ((ucell_t)M_POP) > ((ucell_t)TOS) ) ? FTRUE : FFALSE ;
607 case ID_COMP_U_LESSTHAN:
608 TOS = ( ((ucell_t)M_POP) < ((ucell_t)TOS) ) ? FTRUE : FFALSE ;
610 case ID_COMP_ZERO_EQUAL:
611 TOS = ( TOS == 0 ) ? FTRUE : FFALSE ;
613 case ID_COMP_ZERO_NOT_EQUAL:
614 TOS = ( TOS != 0 ) ? FTRUE : FALSE ;
616 case ID_COMP_ZERO_GREATERTHAN:
617 TOS = ( TOS > 0 ) ? FTRUE : FFALSE ;
619 case ID_COMP_ZERO_LESSTHAN:
620 TOS = ( TOS < 0 ) ? FTRUE : FFALSE ;
633 #endif /* !PF_NO_SHELL */
637 /* Put address of body on stack. Insptr points after code start. */
638 TOS = (cell_t) ((char *)InsPtr - sizeof(cell_t) + CREATE_BODY_OFFSET );
641 case ID_CSTORE: /* ( c caddr -- ) */
642 *((uint8_t *) TOS) = (uint8_t) M_POP;
646 /* Double precision add. */
647 case ID_D_PLUS: /* D+ ( al ah bl bh -- sl sh ) */
649 register ucell_t ah,al,bl,sh,sl;
656 if( sl < bl ) sh = 1; /* Carry */
664 /* Double precision subtract. */
665 case ID_D_MINUS: /* D- ( al ah bl bh -- sl sh ) */
667 register ucell_t ah,al,bl,sh,sl;
674 if( al < bl ) sh = 1; /* Borrow */
682 /* Assume 8-bit char and calculate cell width. */
683 #define NBITS ((sizeof(ucell_t)) * 8)
684 /* Define half the number of bits in a cell. */
685 #define HNBITS (NBITS / 2)
686 /* Assume two-complement arithmetic to calculate lower half. */
687 #define LOWER_HALF(n) ((n) & (((ucell_t)1 << HNBITS) - 1))
688 #define HIGH_BIT ((ucell_t)1 << (NBITS - 1))
690 /* Perform cell*cell bit multiply for a 2 cell result, by factoring into half cell quantities.
691 * Using an improved algorithm suggested by Steve Green.
692 * Converted to 64-bit by Aleksej Saushev.
694 case ID_D_UMTIMES: /* UM* ( a b -- lo hi ) */
696 ucell_t ahi, alo, bhi, blo; /* input parts */
697 ucell_t lo, hi, temp;
698 /* Get values from stack. */
701 /* Break into hi and lo 16 bit parts. */
702 alo = LOWER_HALF(ahi);
704 blo = LOWER_HALF(bhi);
709 /* higher part: ahi * bhi */
711 /* middle (overlapping) part: ahi * blo */
713 lo += LOWER_HALF(temp);
714 hi += temp >> HNBITS;
715 /* middle (overlapping) part: alo * bhi */
717 lo += LOWER_HALF(temp);
718 hi += temp >> HNBITS;
719 /* lower part: alo * blo */
721 /* its higher half overlaps with middle's lower half: */
722 lo += temp >> HNBITS;
726 /* combine lower part of result: */
727 lo = (lo << HNBITS) + LOWER_HALF(temp);
734 /* Perform cell*cell bit multiply for 2 cell result, using shift and add. */
735 case ID_D_MTIMES: /* M* ( a b -- pl ph ) */
737 ucell_t ahi, alo, bhi, blo; /* input parts */
738 ucell_t lo, hi, temp;
740 /* Get values from stack. */
744 /* Calculate product sign: */
745 sg = ((cell_t)(ahi ^ bhi) < 0);
746 /* Take absolute values and reduce to um* */
747 if ((cell_t)ahi < 0) ahi = (ucell_t)(-(cell_t)ahi);
748 if ((cell_t)bhi < 0) bhi = (ucell_t)(-(cell_t)bhi);
750 /* Break into hi and lo 16 bit parts. */
751 alo = LOWER_HALF(ahi);
753 blo = LOWER_HALF(bhi);
758 /* higher part: ahi * bhi */
760 /* middle (overlapping) part: ahi * blo */
762 lo += LOWER_HALF(temp);
763 hi += temp >> HNBITS;
764 /* middle (overlapping) part: alo * bhi */
766 lo += LOWER_HALF(temp);
767 hi += temp >> HNBITS;
768 /* lower part: alo * blo */
770 /* its higher half overlaps with middle's lower half: */
771 lo += temp >> HNBITS;
775 /* combine lower part of result: */
776 lo = (lo << HNBITS) + LOWER_HALF(temp);
778 /* Negate product if one operand negative. */
781 /* lo = (ucell_t)(- lo); */
783 hi = ~hi + ((lo == 0) ? 1 : 0);
791 #define DULT(du1l,du1h,du2l,du2h) ( (du2h<du1h) ? FALSE : ( (du2h==du1h) ? (du1l<du2l) : TRUE) )
792 /* Perform 2 cell by 1 cell divide for 1 cell result and remainder, using shift and subtract. */
793 case ID_D_UMSMOD: /* UM/MOD ( al ah bdiv -- rem q ) */
795 ucell_t ah,al, q,di, bl,bh, sl,sh;
801 for( di=0; di<NBITS; di++ )
803 if( !DULT(al,ah,bl,bh) )
807 if( al < bl ) sh = 1; /* Borrow */
814 bl = (bl >> 1) | (bh << (NBITS-1));
817 if( !DULT(al,ah,bl,bh) )
823 M_PUSH( al ); /* rem */
828 /* Perform 2 cell by 1 cell divide for 2 cell result and remainder, using shift and subtract. */
829 case ID_D_MUSMOD: /* MU/MOD ( al am bdiv -- rem ql qh ) */
831 register ucell_t ah,am,al,ql,qh,di;
832 #define bdiv ((ucell_t)TOS)
837 for( di=0; di<2*NBITS; di++ )
844 qh = (qh << 1) | (ql >> (NBITS-1));
846 ah = (ah << 1) | (am >> (NBITS-1));
847 am = (am << 1) | (al >> (NBITS-1));
849 DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql ));
856 M_PUSH( ah ); /* rem */
867 #endif /* !PF_NO_SHELL */
874 TOS = gCurrentTask->td_StackBase - STKPTR;
877 case ID_DIVIDE: BINARY_OP( / ); endcase;
888 case ID_DROP: M_DROP; endcase;
892 DumpMemory( (char *) Scratch, TOS );
896 case ID_DUP: M_DUP; endcase;
898 case ID_DO_P: /* ( limit start -- ) ( R: -- start limit ) */
904 case ID_EOL: /* ( -- end_of_line_char ) */
909 case ID_ERRORQ_P: /* ( flag num -- , quit if flag true ) */
928 /* Save IP on return stack like a JSR. */
930 #ifdef PF_SUPPORT_TRACE
931 /* Bump level for trace. */
934 if( IsTokenPrimitive( TOS ) )
936 WRITE_CELL_DIC( (cell_t *) &FakeSecondary[0], TOS); /* Build a fake secondary and execute it. */
937 InsPtr = &FakeSecondary[0];
941 InsPtr = (cell_t *) LOCAL_CODEREL_TO_ABS(TOS);
947 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
950 TOS = (cell_t) READ_CELL_DIC((cell_t *)TOS);
954 TOS = *((cell_t *)TOS);
957 TOS = *((cell_t *)TOS);
961 case ID_FILE_CREATE: /* ( c-addr u fam -- fid ior ) */
962 /* Build NUL terminated name string. */
963 Scratch = M_POP; /* u */
964 Temp = M_POP; /* caddr */
965 if( Scratch < TIB_SIZE-2 )
967 const char *famText = pfSelectFileModeCreate( TOS );
968 pfCopyMemory( gScratch, (char *) Temp, (ucell_t) Scratch );
969 gScratch[Scratch] = '\0';
970 DBUG(("Create file = %s with famTxt %s\n", gScratch, famText ));
971 FileID = sdOpenFile( gScratch, famText );
972 TOS = ( FileID == NULL ) ? -1 : 0 ;
973 M_PUSH( (cell_t) FileID );
977 ERR("Filename too large for name buffer.\n");
983 case ID_FILE_DELETE: /* ( c-addr u -- ior ) */
984 /* Build NUL terminated name string. */
985 Temp = M_POP; /* caddr */
986 if( TOS < TIB_SIZE-2 )
988 pfCopyMemory( gScratch, (char *) Temp, (ucell_t) TOS );
989 gScratch[TOS] = '\0';
990 DBUG(("Delete file = %s\n", gScratch ));
991 TOS = sdDeleteFile( gScratch );
995 ERR("Filename too large for name buffer.\n");
1000 case ID_FILE_OPEN: /* ( c-addr u fam -- fid ior ) */
1001 /* Build NUL terminated name string. */
1002 Scratch = M_POP; /* u */
1003 Temp = M_POP; /* caddr */
1004 if( Scratch < TIB_SIZE-2 )
1006 const char *famText = pfSelectFileModeOpen( TOS );
1007 pfCopyMemory( gScratch, (char *) Temp, (ucell_t) Scratch );
1008 gScratch[Scratch] = '\0';
1009 DBUG(("Open file = %s\n", gScratch ));
1010 FileID = sdOpenFile( gScratch, famText );
1012 TOS = ( FileID == NULL ) ? -1 : 0 ;
1013 M_PUSH( (cell_t) FileID );
1017 ERR("Filename too large for name buffer.\n");
1023 case ID_FILE_CLOSE: /* ( fid -- ior ) */
1024 TOS = sdCloseFile( (FileStream *) TOS );
1027 case ID_FILE_READ: /* ( addr len fid -- u2 ior ) */
1028 FileID = (FileStream *) TOS;
1030 CharPtr = (char *) M_POP;
1031 Temp = sdReadFile( CharPtr, 1, Scratch, FileID );
1032 /* TODO check feof() or ferror() */
1037 /* TODO Why does this crash when passed an illegal FID? */
1038 case ID_FILE_SIZE: /* ( fid -- ud ior ) */
1039 /* Determine file size by seeking to end and returning position. */
1040 FileID = (FileStream *) TOS;
1042 file_offset_t endposition = -1;
1043 file_offset_t original = sdTellFile( FileID );
1046 sdSeekFile( FileID, 0, PF_SEEK_END );
1047 endposition = sdTellFile( FileID );
1048 /* Restore original position. */
1049 sdSeekFile( FileID, original, PF_SEEK_SET );
1051 if (endposition < 0)
1053 M_PUSH(0); /* low */
1054 M_PUSH(0); /* high */
1055 TOS = -4; /* TODO proper error number */
1059 M_PUSH(endposition); /* low */
1060 /* We do not support double precision file offsets.*/
1061 M_PUSH(0); /* high */
1067 case ID_FILE_WRITE: /* ( addr len fid -- ior ) */
1068 FileID = (FileStream *) TOS;
1070 CharPtr = (char *) M_POP;
1071 Temp = sdWriteFile( CharPtr, 1, Scratch, FileID );
1072 TOS = (Temp != Scratch) ? -3 : 0;
1075 case ID_FILE_REPOSITION: /* ( ud fid -- ior ) */
1077 file_offset_t offset;
1080 FileID = (FileStream *) TOS;
1083 /* We do not support double precision file offsets in pForth.
1084 * So check to make sure the high bits are not used.
1086 if (offsetHigh != 0)
1088 TOS = -3; /* TODO err num? */
1092 TOS = sdSeekFile( FileID, offset, PF_SEEK_SET );
1096 case ID_FILE_POSITION: /* ( fid -- ud ior ) */
1098 file_offset_t position;
1099 FileID = (FileStream *) TOS;
1100 position = sdTellFile( FileID );
1103 M_PUSH(0); /* low */
1104 M_PUSH(0); /* high */
1105 TOS = -4; /* TODO proper error number */
1109 M_PUSH(position); /* low */
1110 /* We do not support double precision file offsets.*/
1111 M_PUSH(0); /* high */
1117 case ID_FILE_RO: /* ( -- fam ) */
1119 TOS = PF_FAM_READ_ONLY;
1122 case ID_FILE_RW: /* ( -- fam ) */
1124 TOS = PF_FAM_READ_WRITE;
1127 case ID_FILE_WO: /* ( -- fam ) */
1129 TOS = PF_FAM_WRITE_ONLY;
1132 case ID_FILE_BIN: /* ( -- fam ) */
1133 TOS = TOS | PF_FAM_BINARY_FLAG;
1136 case ID_FILE_FLUSH: /* ( fileid -- ior ) */
1138 FileStream *Stream = (FileStream *) TOS;
1139 TOS = (sdFlushFile( Stream ) == 0) ? 0 : THROW_FLUSH_FILE;
1143 case ID_FILE_RENAME: /* ( oldName newName -- ior ) */
1145 char *New = (char *) TOS;
1146 char *Old = (char *) M_POP;
1147 TOS = sdRenameFile( Old, New );
1151 case ID_FILE_RESIZE: /* ( ud fileid -- ior ) */
1153 FileStream *File = (FileStream *) TOS;
1154 ucell_t SizeHi = (ucell_t) M_POP;
1155 ucell_t SizeLo = (ucell_t) M_POP;
1156 TOS = ( UdIsUint64( SizeLo, SizeHi )
1157 ? sdResizeFile( File, UdToUint64( SizeLo, SizeHi ))
1158 : THROW_RESIZE_FILE );
1162 case ID_FILL: /* ( caddr num charval -- ) */
1164 register char *DstPtr;
1165 Temp = M_POP; /* num */
1166 DstPtr = (char *) M_POP; /* dst */
1167 for( Scratch=0; (ucell_t) Scratch < (ucell_t) Temp ; Scratch++ )
1169 *DstPtr++ = (char) TOS;
1176 case ID_FIND: /* ( $addr -- $addr 0 | xt +-1 ) */
1177 TOS = ffFind( (char *) TOS, (ExecToken *) &Temp );
1182 TOS = ffFindNFA( (const ForthString *) TOS, (const ForthString **) &Temp );
1183 M_PUSH( (cell_t) Temp );
1185 #endif /* !PF_NO_SHELL */
1191 /* Validate memory before freeing. Clobber validator and first word. */
1192 case ID_FREE: /* ( addr -- result ) */
1195 ERR("FREE passed NULL!\n");
1196 TOS = -2; /* FIXME error code */
1200 CellPtr = (cell_t *) TOS;
1202 if( ((ucell_t)*CellPtr) != ((ucell_t)CellPtr ^ PF_MEMORY_VALIDATOR))
1204 TOS = -2; /* FIXME error code */
1208 CellPtr[0] = 0xDeadBeef;
1209 pfFreeMem((char *)CellPtr);
1215 #include "pfinnrfp.h"
1219 TOS = (cell_t)CODE_HERE;
1222 case ID_NUMBERQ_P: /* ( addr -- 0 | n 1 ) */
1223 /* Convert using number converter in 'C'.
1224 ** Only supports single precision for bootstrap.
1226 TOS = (cell_t) ffNumberQ( (char *) TOS, &Temp );
1227 if( TOS == NUM_TYPE_SINGLE)
1229 M_PUSH( Temp ); /* Push single number */
1233 case ID_I: /* ( -- i , DO LOOP index ) */
1239 case ID_INCLUDE_FILE:
1240 FileID = (FileStream *) TOS;
1241 M_DROP; /* Drop now so that INCLUDE has a clean stack. */
1243 Scratch = ffIncludeFile( FileID );
1245 if( Scratch ) M_THROW(Scratch)
1247 #endif /* !PF_NO_SHELL */
1252 Scratch = ffInterpret();
1254 if( Scratch ) M_THROW(Scratch)
1256 #endif /* !PF_NO_SHELL */
1258 case ID_J: /* ( -- j , second DO LOOP index ) */
1273 #endif /* !PF_NO_SHELL */
1276 DBUG(("ID_LITERAL_P: InsPtr = 0x%x, *InsPtr = 0x%x\n", InsPtr, *InsPtr ));
1278 TOS = READ_CELL_DIC(InsPtr++);
1282 case ID_LOCAL_COMPILER: DO_VAR(gLocalCompiler_XT); endcase;
1283 #endif /* !PF_NO_SHELL */
1285 case ID_LOCAL_FETCH: /* ( i <local> -- n , fetch from local ) */
1286 TOS = *(LocalsPtr - TOS);
1289 #define LOCAL_FETCH_N(num) \
1290 case ID_LOCAL_FETCH_##num: /* ( <local> -- n , fetch from local ) */ \
1292 TOS = *(LocalsPtr -(num)); \
1304 case ID_LOCAL_STORE: /* ( n i <local> -- , store n in local ) */
1305 *(LocalsPtr - TOS) = M_POP;
1309 #define LOCAL_STORE_N(num) \
1310 case ID_LOCAL_STORE_##num: /* ( n <local> -- , store n in local ) */ \
1311 *(LocalsPtr - (num)) = TOS; \
1324 case ID_LOCAL_PLUSSTORE: /* ( n i <local> -- , add n to local ) */
1325 *(LocalsPtr - TOS) += M_POP;
1329 case ID_LOCAL_ENTRY: /* ( x0 x1 ... xn n -- ) */
1330 /* create local stack frame */
1334 DBUG(("LocalEntry: n = %d\n", TOS));
1335 /* End of locals. Create stack frame */
1336 DBUG(("LocalEntry: before RP@ = 0x%x, LP = 0x%x\n",
1337 TORPTR, LocalsPtr));
1338 M_R_PUSH(LocalsPtr);
1341 DBUG(("LocalEntry: after RP@ = 0x%x, LP = 0x%x\n",
1342 TORPTR, LocalsPtr));
1346 *lp++ = M_POP; /* Load local vars from stack */
1352 case ID_LOCAL_EXIT: /* cleanup up local stack frame */
1353 DBUG(("LocalExit: before RP@ = 0x%x, LP = 0x%x\n",
1354 TORPTR, LocalsPtr));
1356 LocalsPtr = (cell_t *) M_R_POP;
1357 DBUG(("LocalExit: after RP@ = 0x%x, LP = 0x%x\n",
1358 TORPTR, LocalsPtr));
1363 MSG("Load "); MSG(SYSTEM_LOAD_FILE); EMIT_CR;
1364 FileID = sdOpenFile(SYSTEM_LOAD_FILE, "r");
1368 Scratch = ffIncludeFile( FileID ); /* Also closes the file. */
1370 if( Scratch ) M_THROW(Scratch);
1374 ERR(SYSTEM_LOAD_FILE); ERR(" could not be opened!\n");
1377 #endif /* !PF_NO_SHELL */
1379 case ID_LEAVE_P: /* ( R: index limit -- ) */
1385 case ID_LOOP_P: /* ( R: index limit -- | index limit ) */
1386 Temp = M_R_POP; /* limit */
1387 Scratch = M_R_POP + 1; /* index */
1388 if( Scratch == Temp )
1390 InsPtr++; /* skip branch offset, exit loop */
1394 /* Push index and limit back to R */
1395 M_R_PUSH( Scratch );
1397 /* Branch back to just after (DO) */
1402 case ID_LSHIFT: BINARY_OP( << ); endcase;
1406 TOS = ( TOS > Scratch ) ? TOS : Scratch ;
1411 TOS = ( TOS < Scratch ) ? TOS : Scratch ;
1414 case ID_MINUS: BINARY_OP( - ); endcase;
1417 case ID_NAME_TO_TOKEN:
1418 TOS = (cell_t) NameToToken((ForthString *)TOS);
1421 case ID_NAME_TO_PREVIOUS:
1422 TOS = (cell_t) NameToPrevious((ForthString *)TOS);
1429 case ID_OR: BINARY_OP( | ); endcase;
1436 case ID_PICK: /* ( ... n -- sp(n) ) */
1440 case ID_PLUS: BINARY_OP( + ); endcase;
1442 case ID_PLUS_STORE: /* ( n addr -- , add n to *addr ) */
1443 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1444 if( IN_DICS( TOS ) )
1446 Scratch = READ_CELL_DIC((cell_t *)TOS);
1448 WRITE_CELL_DIC((cell_t *)TOS,Scratch);
1452 *((cell_t *)TOS) += M_POP;
1455 *((cell_t *)TOS) += M_POP;
1460 case ID_PLUSLOOP_P: /* ( delta -- ) ( R: index limit -- | index limit ) */
1462 cell_t Limit = M_R_POP;
1463 cell_t OldIndex = M_R_POP;
1464 cell_t Delta = TOS; /* add TOS to index, not 1 */
1465 cell_t NewIndex = OldIndex + Delta;
1466 cell_t OldDiff = OldIndex - Limit;
1468 /* This exploits this idea (lifted from Gforth):
1469 (x^y)<0 is equivalent to (x<0) != (y<0) */
1470 if( ((OldDiff ^ (OldDiff + Delta)) /* is the limit crossed? */
1471 & (OldDiff ^ Delta)) /* is it a wrap-around? */
1474 InsPtr++; /* skip branch offset, exit loop */
1478 /* Push index and limit back to R */
1479 M_R_PUSH( NewIndex );
1481 /* Branch back to just after (DO) */
1488 case ID_QDO_P: /* (?DO) ( limit start -- ) ( R: -- start limit ) */
1489 Scratch = M_POP; /* limit */
1490 if( Scratch == TOS )
1492 /* Branch to just after (LOOP) */
1498 M_R_PUSH( Scratch );
1499 InsPtr++; /* skip branch offset, enter loop */
1504 case ID_QDUP: if( TOS ) M_DUP; endcase;
1506 case ID_QTERMINAL: /* WARNING: Typically not fully implemented! */
1508 TOS = sdQueryTerminal();
1511 case ID_QUIT_P: /* Stop inner interpreter, go back to user. */
1512 #ifdef PF_SUPPORT_TRACE
1515 M_THROW(THROW_QUIT);
1534 TOS = (ffRefill() > 0) ? FTRUE : FFALSE;
1537 /* Resize memory allocated by ALLOCATE. */
1538 case ID_RESIZE: /* ( addr1 u -- addr2 result ) */
1540 cell_t *Addr1 = (cell_t *) M_POP;
1541 /* Point to validator below users address. */
1542 cell_t *FreePtr = Addr1 - 1;
1543 if( ((ucell_t)*FreePtr) != ((ucell_t)FreePtr ^ PF_MEMORY_VALIDATOR))
1545 /* 090218 - Fixed bug, was returning zero. */
1551 /* Try to allocate. */
1552 CellPtr = (cell_t *) pfAllocMem( TOS + sizeof(cell_t) );
1555 /* Copy memory including validation. */
1556 pfCopyMemory( (char *) CellPtr, (char *) FreePtr, TOS + sizeof(cell_t) );
1557 *CellPtr = (cell_t)(((ucell_t)CellPtr) ^ (ucell_t)PF_MEMORY_VALIDATOR);
1558 /* 090218 - Fixed bug that was incrementing the address twice. Thanks Reinhold Straub. */
1559 /* Increment past validator to user address. */
1560 M_PUSH( (cell_t) (CellPtr + 1) );
1561 TOS = 0; /* Result code. */
1562 /* Mark old cell as dead so we can't free it twice. */
1563 FreePtr[0] = 0xDeadBeef;
1564 pfFreeMem((char *) FreePtr);
1568 /* 090218 - Fixed bug, was returning zero. */
1570 TOS = -4; /* FIXME Fix error code. */
1577 ** RP@ and RP! are called secondaries so we must
1578 ** account for the return address pushed before calling.
1580 case ID_RP_FETCH: /* ( -- rp , address of top of return stack ) */
1582 TOS = (cell_t)TORPTR; /* value before calling RP@ */
1585 case ID_RP_STORE: /* ( rp -- , address of top of return stack ) */
1586 TORPTR = (cell_t *) TOS;
1590 case ID_ROLL: /* ( xu xu-1 xu-1 ... x0 u -- xu-1 xu-1 ... x0 xu ) */
1593 cell_t *srcPtr, *dstPtr;
1594 Scratch = M_STACK(TOS);
1595 srcPtr = &M_STACK(TOS-1);
1596 dstPtr = &M_STACK(TOS);
1597 for( ri=0; ri<TOS; ri++ )
1599 *dstPtr-- = *srcPtr--;
1606 case ID_ROT: /* ( a b c -- b c a ) */
1607 Scratch = M_POP; /* b */
1608 Temp = M_POP; /* a */
1609 M_PUSH( Scratch ); /* b */
1614 /* Logical right shift */
1615 case ID_RSHIFT: { TOS = ((ucell_t)M_POP) >> TOS; } endcase;
1618 case ID_SAVE_FORTH_P: /* ( $name Entry NameSize CodeSize -- err ) */
1620 cell_t NameSize, CodeSize, EntryPoint;
1624 ForthStringToC( gScratch, (char *) M_POP, sizeof(gScratch) );
1625 TOS = ffSaveForth( gScratch, EntryPoint, NameSize, CodeSize );
1630 case ID_SP_FETCH: /* ( -- sp , address of top of stack, sorta ) */
1632 TOS = (cell_t)STKPTR;
1635 case ID_SP_STORE: /* ( sp -- , address of top of stack, sorta ) */
1636 STKPTR = (cell_t *) TOS;
1640 case ID_STORE: /* ( n addr -- , write n to addr ) */
1641 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1642 if( IN_DICS( TOS ) )
1644 WRITE_CELL_DIC((cell_t *)TOS,M_POP);
1648 *((cell_t *)TOS) = M_POP;
1651 *((cell_t *)TOS) = M_POP;
1656 case ID_SCAN: /* ( addr cnt char -- addr' cnt' ) */
1657 Scratch = M_POP; /* cnt */
1658 Temp = M_POP; /* addr */
1659 TOS = ffScan( (char *) Temp, Scratch, (char) TOS, &CharPtr );
1660 M_PUSH((cell_t) CharPtr);
1666 Scratch = ffSemiColon();
1668 if( Scratch ) M_THROW( Scratch );
1670 #endif /* !PF_NO_SHELL */
1672 case ID_SKIP: /* ( addr cnt char -- addr' cnt' ) */
1673 Scratch = M_POP; /* cnt */
1674 Temp = M_POP; /* addr */
1675 TOS = ffSkip( (char *) Temp, Scratch, (char) TOS, &CharPtr );
1676 M_PUSH((cell_t) CharPtr);
1679 case ID_SOURCE: /* ( -- c-addr num ) */
1681 M_PUSH( (cell_t) gCurrentTask->td_SourcePtr );
1682 TOS = (cell_t) gCurrentTask->td_SourceNum;
1685 case ID_SOURCE_SET: /* ( c-addr num -- ) */
1686 gCurrentTask->td_SourcePtr = (char *) M_POP;
1687 gCurrentTask->td_SourceNum = TOS;
1693 TOS = ffConvertStreamToSourceID( gCurrentTask->td_InputStream ) ;
1696 case ID_SOURCE_ID_POP:
1698 TOS = ffConvertStreamToSourceID( ffPopInputStream() ) ;
1701 case ID_SOURCE_ID_PUSH: /* ( source-id -- ) */
1702 TOS = (cell_t)ffConvertSourceIDToStream( TOS );
1703 Scratch = ffPushInputStream((FileStream *) TOS );
1711 case ID_SOURCE_LINE_NUMBER_FETCH: /* ( -- linenr ) */
1713 TOS = gCurrentTask->td_LineNumber;
1716 case ID_SOURCE_LINE_NUMBER_STORE: /* ( linenr -- ) */
1717 gCurrentTask->td_LineNumber = TOS;
1737 case ID_THROW: /* ( k*x err -- k*x | i*x err , jump to where CATCH was called ) */
1748 CharPtr = (char *) ffWord( (char) ' ' );
1749 TOS = ffFind( CharPtr, (ExecToken *) &Temp );
1752 ERR("' could not find ");
1753 ioType( (char *) CharPtr+1, *CharPtr );
1761 #endif /* !PF_NO_SHELL */
1763 case ID_TIMES: BINARY_OP( * ); endcase;
1766 Scratch = M_POP; /* addr */
1767 ioType( (char *) Scratch, TOS );
1776 case ID_VAR_BASE: DO_VAR(gVarBase); endcase;
1777 case ID_VAR_CODE_BASE: DO_VAR(gCurrentDictionary->dic_CodeBase); endcase;
1778 case ID_VAR_CODE_LIMIT: DO_VAR(gCurrentDictionary->dic_CodeLimit); endcase;
1779 case ID_VAR_CONTEXT: DO_VAR(gVarContext); endcase;
1780 case ID_VAR_DP: DO_VAR(gCurrentDictionary->dic_CodePtr.Cell); endcase;
1781 case ID_VAR_ECHO: DO_VAR(gVarEcho); endcase;
1782 case ID_VAR_HEADERS_BASE: DO_VAR(gCurrentDictionary->dic_HeaderBase); endcase;
1783 case ID_VAR_HEADERS_LIMIT: DO_VAR(gCurrentDictionary->dic_HeaderLimit); endcase;
1784 case ID_VAR_HEADERS_PTR: DO_VAR(gCurrentDictionary->dic_HeaderPtr); endcase;
1785 case ID_VAR_NUM_TIB: DO_VAR(gCurrentTask->td_SourceNum); endcase;
1786 case ID_VAR_OUT: DO_VAR(gCurrentTask->td_OUT); endcase;
1787 case ID_VAR_STATE: DO_VAR(gVarState); endcase;
1788 case ID_VAR_TO_IN: DO_VAR(gCurrentTask->td_IN); endcase;
1789 case ID_VAR_TRACE_FLAGS: DO_VAR(gVarTraceFlags); endcase;
1790 case ID_VAR_TRACE_LEVEL: DO_VAR(gVarTraceLevel); endcase;
1791 case ID_VAR_TRACE_STACK: DO_VAR(gVarTraceStack); endcase;
1792 case ID_VAR_RETURN_CODE: DO_VAR(gVarReturnCode); endcase;
1795 TOS = (cell_t) ffWord( (char) TOS );
1798 case ID_WORD_FETCH: /* ( waddr -- w ) */
1799 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1800 if( IN_DICS( TOS ) )
1802 TOS = (uint16_t) READ_SHORT_DIC((uint16_t *)TOS);
1806 TOS = *((uint16_t *)TOS);
1809 TOS = *((uint16_t *)TOS);
1813 case ID_WORD_STORE: /* ( w waddr -- ) */
1815 #if (defined(PF_BIG_ENDIAN_DIC) || defined(PF_LITTLE_ENDIAN_DIC))
1816 if( IN_DICS( TOS ) )
1818 WRITE_SHORT_DIC((uint16_t *)TOS,(uint16_t)M_POP);
1822 *((uint16_t *)TOS) = (uint16_t) M_POP;
1825 *((uint16_t *)TOS) = (uint16_t) M_POP;
1830 case ID_XOR: BINARY_OP( ^ ); endcase;
1833 /* Branch is followed by an offset relative to address of offset. */
1834 case ID_ZERO_BRANCH:
1835 DBUGX(("Before 0Branch: IP = 0x%x\n", InsPtr ));
1842 InsPtr++; /* skip over offset */
1845 DBUGX(("After 0Branch: IP = 0x%x\n", InsPtr ));
1849 ERR("pfCatch: Unrecognised token = 0x");
1852 ffDotHex((cell_t) InsPtr);
1858 if(InsPtr) Token = READ_CELL_DIC(InsPtr++); /* Traverse to next token in secondary. */
1865 if( _CrtCheckMemory() == 0 )
1867 ERR("_CrtCheckMemory abort: InsPtr = 0x");
1868 ffDotHex((int)InsPtr);
1873 } while( (InitialReturnStack - TORPTR) > 0 );
1877 return ExceptionReturnCode;