1883 lines · 55.3 KB · C
RawDownload
/* @(#) pf_inner.c 98/03/16 1.7 *//***************************************************************** Inner Interpreter for Forth based on 'C'**** Author: Phil Burk** Copyright 1994 3DO, Phil Burk, Larry Polansky, David Rosenboom**** The pForth software code is dedicated to the public domain,** and any third party may reproduce, distribute and modify** the pForth software code or any derivative works thereof** without any compensation or license.  The pForth software** code is provided on an "as is" basis without any warranty** of any kind, including, without limitation, the implied** warranties of merchantability and fitness for a particular** purpose and their equivalents under the laws of any jurisdiction.********************************************************************** 940502 PLB Creation.** 940505 PLB More macros.** 940509 PLB Moved all stack stuff into pfCatch.** 941014 PLB Converted to flat secondary strusture.** 941027 rdg added casts to ID_SP_FETCH, ID_RP_FETCH,**             and ID_HERE for armcc** 941130 PLB Made w@ unsigned*****************************************************************/ #include "pf_all.h" #if defined(WIN32) && !defined(__MINGW32__)#include <crtdbg.h>#endif #define SYSTEM_LOAD_FILE "system.fth" /***************************************************************** Macros for data stack access.** TOS is cached in a register in pfCatch.***************************************************************/ #define STKPTR   (DataStackPtr)#define M_POP    (*(STKPTR++))#define M_PUSH(n) {*(--(STKPTR)) = (cell_t) (n);}#define M_STACK(n) (STKPTR[n]) #define TOS      (TopOfStack)#define PUSH_TOS M_PUSH(TOS)#define M_DUP    PUSH_TOS;#define M_DROP   { TOS = M_POP; } #define ASCII_EOT   (0x04) /***************************************************************** Macros for Floating Point stack access.***************************************************************/#ifdef PF_SUPPORT_FP#define FP_STKPTR   (FloatStackPtr)#define M_FP_SPZERO (gCurrentTask->td_FloatStackBase)#define M_FP_POP    (*(FP_STKPTR++))#define M_FP_PUSH(n) {*(--(FP_STKPTR)) = (PF_FLOAT) (n);}#define M_FP_STACK(n) (FP_STKPTR[n]) #define FP_TOS      (fpTopOfStack)#define PUSH_FP_TOS M_FP_PUSH(FP_TOS)#define M_FP_DUP    PUSH_FP_TOS;#define M_FP_DROP   { FP_TOS = M_FP_POP; }#endif /***************************************************************** Macros for return stack access.***************************************************************/ #define TORPTR (ReturnStackPtr)#define M_R_DROP {TORPTR++;}#define M_R_POP (*(TORPTR++))#define M_R_PICK(n) (TORPTR[n])#define M_R_PUSH(n) {*(--(TORPTR)) = (cell_t) (n);} /***************************************************************** Misc Forth macros***************************************************************/ #define M_BRANCH   { InsPtr = (cell_t *) (((uint8_t *) InsPtr) + READ_CELL_DIC(InsPtr)); } /* Cache top of data stack like in JForth. */#ifdef PF_SUPPORT_FP#define LOAD_REGISTERS \    { \        STKPTR = gCurrentTask->td_StackPtr; \        TOS = M_POP; \        FP_STKPTR = gCurrentTask->td_FloatStackPtr; \        FP_TOS = M_FP_POP; \        TORPTR = gCurrentTask->td_ReturnPtr; \     } #define SAVE_REGISTERS \    { \        gCurrentTask->td_ReturnPtr = TORPTR; \        M_PUSH( TOS ); \        gCurrentTask->td_StackPtr = STKPTR; \        M_FP_PUSH( FP_TOS ); \        gCurrentTask->td_FloatStackPtr = FP_STKPTR; \     } #else/* Cache top of data stack like in JForth. */#define LOAD_REGISTERS \    { \        STKPTR = gCurrentTask->td_StackPtr; \        TOS = M_POP; \        TORPTR = gCurrentTask->td_ReturnPtr; \     } #define SAVE_REGISTERS \    { \        gCurrentTask->td_ReturnPtr = TORPTR; \        M_PUSH( TOS ); \        gCurrentTask->td_StackPtr = STKPTR; \     }#endif #define M_DOTS \    SAVE_REGISTERS; \    ffDotS( ); \    LOAD_REGISTERS; #define DO_VAR(varname) { PUSH_TOS; TOS = (cell_t) &varname; } #ifdef PF_SUPPORT_FP#define M_THROW(err) \    { \        ExceptionReturnCode = (ThrowCode)(err); \        TORPTR = InitialReturnStack; /* Will cause return to 'C' */ \        STKPTR = InitialDataStack; \        FP_STKPTR = InitialFloatStack; \    }#else#define M_THROW(err) \    { \        ExceptionReturnCode = (err); \        TORPTR = InitialReturnStack; /* Will cause return to 'C' */ \        STKPTR = InitialDataStack; \    }#endif /***************************************************************** Other macros***************************************************************/ #define BINARY_OP( op ) { TOS = M_POP op TOS; }#define endcase break #if defined(PF_NO_SHELL) || !defined(PF_SUPPORT_TRACE)    #define TRACENAMES /* no names */#else/* Display name of executing routine. */static void TraceNames( ExecToken Token, cell_t Level ){    char *DebugName;    cell_t i;     if( ffTokenToName( Token, &DebugName ) )    {        cell_t NumSpaces;        if( gCurrentTask->td_OUT > 0 ) EMIT_CR;        EMIT( '>' );        for( i=0; i<Level; i++ )        {            MSG( "  " );        }        TypeName( DebugName );/* Space out to column N then .S */        NumSpaces = 30 - gCurrentTask->td_OUT;        for( i=0; i < NumSpaces; i++ )        {            EMIT( ' ' );        }        ffDotS();/* No longer needed?        gCurrentTask->td_OUT = 0; */ /* !!! Hack for ffDotS() */     }    else    {        MSG_NUM_H("Couldn't find Name for ", Token);    }} #define TRACENAMES \    if( (gVarTraceLevel > Level) ) \    { SAVE_REGISTERS; TraceNames( Token, Level ); LOAD_REGISTERS; }#endif /* PF_NO_SHELL */ /* Use local copy of CODE_BASE for speed. */#define LOCAL_CODEREL_TO_ABS( a ) ((cell_t *) (((cell_t) a) + CodeBase)) /* Truncate the unsigned double cell integer LO/HI to an uint64_t. */static uint64_t UdToUint64( ucell_t Lo, ucell_t Hi ){    return (( 2 * sizeof(ucell_t) == sizeof(uint64_t) )        ? (((uint64_t)Lo) | (((uint64_t)Hi) >> (sizeof(ucell_t) * 8)))        : Lo);} /* Return TRUE if the unsigned double cell integer LO/HI is not greater * then the greatest uint64_t. */static int UdIsUint64( ucell_t Lo, ucell_t Hi ){    return (( 2 * sizeof(ucell_t) == sizeof(uint64_t) )        ? TRUE        : Hi == 0);} static const char *pfSelectFileModeCreate( cell_t fam );static const char *pfSelectFileModeOpen( cell_t fam ); /**************************************************************/static const char *pfSelectFileModeCreate( cell_t fam ){    const char *famText = NULL;    switch( fam )    {    case (PF_FAM_WRITE_ONLY + PF_FAM_BINARY_FLAG):        famText = PF_FAM_BIN_CREATE_WO;        break;    case (PF_FAM_READ_WRITE + PF_FAM_BINARY_FLAG):        famText = PF_FAM_BIN_CREATE_RW;        break;    case PF_FAM_WRITE_ONLY:        famText = PF_FAM_CREATE_WO;        break;    case PF_FAM_READ_WRITE:        famText = PF_FAM_CREATE_RW;        break;    default:        famText = "illegal";        break;    }    return famText;} /**************************************************************/static const char *pfSelectFileModeOpen( cell_t fam ){    const char *famText = NULL;    switch( fam )    {    case (PF_FAM_READ_ONLY + PF_FAM_BINARY_FLAG):        famText = PF_FAM_BIN_OPEN_RO;        break;    case (PF_FAM_WRITE_ONLY + PF_FAM_BINARY_FLAG):        famText = PF_FAM_BIN_CREATE_WO;        break;    case (PF_FAM_READ_WRITE + PF_FAM_BINARY_FLAG):        famText = PF_FAM_BIN_OPEN_RW;        break;    case PF_FAM_READ_ONLY:        famText = PF_FAM_OPEN_RO;        break;    case PF_FAM_WRITE_ONLY:        famText = PF_FAM_CREATE_WO;        break;    case PF_FAM_READ_WRITE:    default:        famText = PF_FAM_OPEN_RW;        break;    }    return famText;} /**************************************************************/ThrowCode pfCatch( ExecToken XT ){    register cell_t  TopOfStack;    /* Cache for faster execution. */    register cell_t *DataStackPtr;    register cell_t *ReturnStackPtr;    register cell_t *InsPtr = NULL;    register cell_t  Token;    cell_t           Scratch; #ifdef PF_SUPPORT_FP    PF_FLOAT       fpTopOfStack;    PF_FLOAT      *FloatStackPtr;    PF_FLOAT       fpScratch;    PF_FLOAT       fpTemp;    PF_FLOAT      *InitialFloatStack;#endif#ifdef PF_SUPPORT_TRACE    cell_t Level = 0;#endif    cell_t        *LocalsPtr = NULL;    cell_t         Temp;    cell_t        *InitialReturnStack;    cell_t        *InitialDataStack;    cell_t         FakeSecondary[2];    char          *CharPtr;    cell_t        *CellPtr;    FileStream    *FileID;    uint8_t       *CodeBase = (uint8_t *) CODE_BASE;    ThrowCode      ExceptionReturnCode = 0; /* FIXME    gExecutionDepth += 1;    PRT(("pfCatch( 0x%x ), depth = %d\n", XT, gExecutionDepth ));*/ /*** Initialize FakeSecondary this way to avoid having stuff in the data section,** which is not supported for some embedded system loaders.*/    FakeSecondary[0] = 0;    FakeSecondary[1] = ID_EXIT; /* For EXECUTE */ /* Move data from task structure to registers for speed. */    LOAD_REGISTERS; /* Save initial stack depths for THROW */    InitialReturnStack = TORPTR;    InitialDataStack   = STKPTR ;#ifdef PF_SUPPORT_FP    InitialFloatStack  = FP_STKPTR;#endif     Token = XT;     do    {DBUG(("pfCatch: Token = 0x%x\n", Token )); /* --------------------------------------------------------------- *//* If secondary, thread down code tree until we hit a primitive. */        while( !IsTokenPrimitive( Token ) )        {#ifdef PF_SUPPORT_TRACE            if((gVarTraceFlags & TRACE_INNER) )            {                MSG("pfCatch: Secondary Token = 0x");                ffDotHex(Token);                MSG_NUM_H(", InsPtr = 0x", InsPtr);            }            TRACENAMES;#endif /* Save IP on return stack like a JSR. */            M_R_PUSH( InsPtr ); /* Convert execution token to absolute address. */            InsPtr = (cell_t *) ( LOCAL_CODEREL_TO_ABS(Token) ); /* Fetch token at IP. */            Token = READ_CELL_DIC(InsPtr++); #ifdef PF_SUPPORT_TRACE/* Bump level for trace display */            Level++;#endif        }  #ifdef PF_SUPPORT_TRACE        TRACENAMES;#endif /* Execute primitive Token. */        switch( Token )        {     /* Pop up a level in Forth inner interpreter.    ** Used to implement semicolon.    ** Put first in switch because ID_EXIT==0 */        case ID_EXIT:            InsPtr = ( cell_t *) M_R_POP;#ifdef PF_SUPPORT_TRACE            Level--;#endif            endcase;         case ID_1MINUS:  TOS--; endcase;         case ID_1PLUS:   TOS++; endcase; #ifndef PF_NO_SHELL        case ID_2LITERAL:            ff2Literal( TOS, M_POP );            M_DROP;            endcase;#endif  /* !PF_NO_SHELL */         case ID_2LITERAL_P:/* hi part stored first, put on top of stack */            PUSH_TOS;            TOS = READ_CELL_DIC(InsPtr++);            M_PUSH(READ_CELL_DIC(InsPtr++));            endcase;         case ID_2MINUS:  TOS -= 2; endcase;         case ID_2PLUS:   TOS += 2; endcase;          case ID_2OVER:  /* ( a b c d -- a b c d a b ) */            PUSH_TOS;            Scratch = M_STACK(3);            M_PUSH(Scratch);            TOS = M_STACK(3);            endcase;         case ID_2SWAP:  /* ( a b c d -- c d a b ) */            Scratch = M_STACK(0);    /* c */            M_STACK(0) = M_STACK(2); /* a */            M_STACK(2) = Scratch;    /* c */            Scratch = TOS;           /* d */            TOS = M_STACK(1);        /* b */            M_STACK(1) = Scratch;    /* d */            endcase;         case ID_2DUP:   /* ( a b -- a b a b ) */            PUSH_TOS;            Scratch = M_STACK(1);            M_PUSH(Scratch);            endcase;         case ID_2_R_FETCH:            PUSH_TOS;            M_PUSH( (*(TORPTR+1)) );            TOS = (*(TORPTR));            endcase;         case ID_2_R_FROM:            PUSH_TOS;            TOS = M_R_POP;            M_PUSH( M_R_POP );            endcase;         case ID_2_TO_R:            M_R_PUSH( M_POP );            M_R_PUSH( TOS );            M_DROP;            endcase;         case ID_ACCEPT_P: /* ( c-addr +n1 -- +n2 ) */            CharPtr = (char *) M_POP;            TOS = ioAccept( CharPtr, TOS );            endcase; #ifndef PF_NO_SHELL        case ID_ALITERAL:            ffALiteral( ABS_TO_CODEREL(TOS) );            M_DROP;            endcase;#endif  /* !PF_NO_SHELL */         case ID_ALITERAL_P:            PUSH_TOS;            TOS = (cell_t) LOCAL_CODEREL_TO_ABS( READ_CELL_DIC(InsPtr++) );            endcase; /* Allocate some extra and put validation identifier at base */#define PF_MEMORY_VALIDATOR  (0xA81B4D69)        case ID_ALLOCATE:            /* Allocate at least one cell's worth because we clobber first cell. */            if ( TOS < sizeof(cell_t) )            {                Temp = sizeof(cell_t);            }            else            {                Temp = TOS;            }            /* Allocate extra cells worth because we store validation info. */            CellPtr = (cell_t *) pfAllocMem( Temp + sizeof(cell_t) );            if( CellPtr )            {/* This was broken into two steps because different compilers incremented** CellPtr before or after the XOR step. */                Temp = (cell_t)CellPtr ^ PF_MEMORY_VALIDATOR;                *CellPtr++ = Temp;                M_PUSH( (cell_t) CellPtr );                TOS = 0;            }            else            {                M_PUSH( 0 );                TOS = -1;  /* FIXME Fix error code. */            }            endcase;         case ID_AND:     BINARY_OP( & ); endcase;         case ID_ARSHIFT:     BINARY_OP( >> ); endcase;  /* Arithmetic right shift */         case ID_BODY_OFFSET:            PUSH_TOS;            TOS = CREATE_BODY_OFFSET;            endcase; /* Branch is followed by an offset relative to address of offset. */        case ID_BRANCH:DBUGX(("Before Branch: IP = 0x%x\n", InsPtr ));

Lines 1–500 of 1883.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t