2007-06-28 10:47:08 -07:00
|
|
|
/*
|
|
|
|
* Interp.c
|
|
|
|
*
|
|
|
|
* Execute the compiled version of a script
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Control the execution trace printf's */
|
|
|
|
#define DEBUG_GROUP0
|
2006-06-02 12:34:58 -07:00
|
|
|
#include "lib/framework/frame.h"
|
2007-06-28 10:47:08 -07:00
|
|
|
#include "interp.h"
|
|
|
|
#include "stack.h"
|
|
|
|
#include "codeprint.h"
|
|
|
|
#include "script.h"
|
2006-08-22 07:28:49 -07:00
|
|
|
#include "event.h" //needed for eventGetEventID()
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
|
|
|
|
// the maximum number of instructions to execute before assuming
|
|
|
|
// an infinite loop
|
|
|
|
#define INTERP_MAXINSTRUCTIONS 100000
|
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
UDWORD CallerIndex;
|
|
|
|
UDWORD *ReturnAddress;
|
|
|
|
} ReturnAddressStack_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the return address stack
|
|
|
|
*/
|
|
|
|
static inline void retStackReset(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the return address stack is empty
|
|
|
|
*
|
|
|
|
* \return True when empty, false otherwise
|
|
|
|
*/
|
|
|
|
static inline BOOL retStackIsEmpty(void);
|
2006-08-19 06:26:11 -07:00
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
/**
|
|
|
|
* Check whether the return address stack is full
|
|
|
|
*
|
|
|
|
* \return True when full, false otherwise
|
|
|
|
*/
|
|
|
|
static inline BOOL retStackIsFull(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push a new address/event pair on the return address stack
|
|
|
|
*
|
|
|
|
* \param CallerIndex Index of the calling function
|
|
|
|
* \param ReturnAddress Address to return to
|
|
|
|
* \return False on failure (stack full)
|
|
|
|
*/
|
|
|
|
static BOOL retStackPush(UDWORD CallerIndex, UDWORD *ReturnAddress);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pop an address/event pair from the return address stack
|
|
|
|
*
|
|
|
|
* \param CallerIndex Index of the calling function
|
|
|
|
* \param ReturnAddress Address to return to
|
|
|
|
* \return False on failure (stack empty)
|
|
|
|
*/
|
|
|
|
static BOOL retStackPop(UDWORD *CallerIndex, UDWORD **ReturnAddress);
|
2006-08-19 06:26:11 -07:00
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
/* The size of each opcode */
|
|
|
|
SDWORD aOpSize[] =
|
|
|
|
{
|
|
|
|
2, // OP_PUSH | type, value
|
|
|
|
2, // OP_PUSHREF | type, value
|
|
|
|
1, // OP_POP
|
|
|
|
|
|
|
|
1, // OP_PUSHGLOBAL | var_num
|
|
|
|
1, // OP_POPGLOBAL | var_num
|
|
|
|
|
|
|
|
1, // OP_PUSHARRAYGLOBAL | array_dimensions | array_base
|
|
|
|
1, // OP_POPARRAYGLOBAL | array_dimensions | array_base
|
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
2, // OP_CALL | func_pointer
|
|
|
|
2, // OP_VARCALL | func_pointer
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
1, // OP_JUMP | offset
|
|
|
|
1, // OP_JUMPTRUE | offset
|
|
|
|
1, // OP_JUMPFALSE | offset
|
|
|
|
|
|
|
|
1, // OP_BINARYOP | secondary op
|
|
|
|
1, // OP_UNARYOP | secondary op
|
|
|
|
|
|
|
|
1, // OP_EXIT
|
|
|
|
1, // OP_PAUSE
|
2006-09-19 09:30:50 -07:00
|
|
|
|
|
|
|
-1, // OP_ADD
|
|
|
|
-1, // OP_SUB
|
|
|
|
-1, // OP_MUL
|
|
|
|
-1, // OP_DIV
|
|
|
|
-1, // OP_NEG
|
|
|
|
|
|
|
|
-1, // OP_AND
|
|
|
|
-1, // OP_OR
|
|
|
|
-1, // OP_NOT
|
|
|
|
|
|
|
|
-1, // OP_CANC
|
|
|
|
|
|
|
|
-1, // OP_EQUAL
|
|
|
|
-1, // OP_NOTEQUAL
|
|
|
|
-1, // OP_GREATEREQUAL
|
|
|
|
-1, // OP_LESSEQUAL
|
|
|
|
-1, // OP_GREATER
|
|
|
|
-1, // OP_LESS
|
|
|
|
|
|
|
|
2, // OP_FUNC | func_pointer
|
|
|
|
1, // OP_POPLOCAL
|
|
|
|
1, // OP_PUSHLOCAL
|
|
|
|
2, // OP_PUSHLOCALREF
|
2007-06-28 10:47:08 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/* The type equivalence table */
|
|
|
|
static TYPE_EQUIV *asInterpTypeEquiv;
|
|
|
|
|
|
|
|
// whether the interpreter is running
|
|
|
|
static BOOL bInterpRunning = FALSE;
|
|
|
|
|
|
|
|
/* Whether to output trace information */
|
|
|
|
BOOL interpTrace;
|
|
|
|
|
|
|
|
/* Print out trace info if tracing is turned on */
|
|
|
|
#define TRCPRINTF(x) \
|
|
|
|
if (interpTrace) \
|
2006-08-22 07:28:49 -07:00
|
|
|
debug( LOG_NEVER, x)
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
#define TRCPRINTVAL(x) \
|
|
|
|
if (interpTrace) \
|
|
|
|
cpPrintVal(x)
|
|
|
|
|
|
|
|
#define TRCPRINTMATHSOP(x) \
|
|
|
|
if (interpTrace) \
|
|
|
|
cpPrintMathsOp(x)
|
|
|
|
|
|
|
|
#define TRCPRINTSTACKTOP() \
|
|
|
|
if (interpTrace) \
|
|
|
|
stackPrintTop()
|
|
|
|
|
|
|
|
|
|
|
|
#define TRCPRINTFUNC(x) \
|
|
|
|
if (interpTrace) \
|
|
|
|
cpPrintFunc(x)
|
|
|
|
|
|
|
|
#define TRCPRINTVARFUNC(x, data) \
|
|
|
|
if (interpTrace) \
|
|
|
|
cpPrintVarFunc(x, data)
|
|
|
|
|
|
|
|
|
|
|
|
// TRUE if the interpreter is currently running
|
|
|
|
BOOL interpProcessorActive(void)
|
|
|
|
{
|
|
|
|
return bInterpRunning;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the value store for a global variable */
|
|
|
|
__inline INTERP_VAL *interpGetVarData(VAL_CHUNK *psGlobals, UDWORD index)
|
|
|
|
{
|
|
|
|
VAL_CHUNK *psChunk;
|
|
|
|
|
|
|
|
psChunk = psGlobals;
|
|
|
|
while (index >= CONTEXT_VALS)
|
|
|
|
{
|
|
|
|
index -= CONTEXT_VALS;
|
|
|
|
psChunk = psChunk->psNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
return psChunk->asVals + index;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get the array data for an array operation
|
|
|
|
BOOL interpGetArrayVarData(UDWORD **pip, VAL_CHUNK *psGlobals, SCRIPT_CODE *psProg, INTERP_VAL **ppsVal)
|
|
|
|
{
|
|
|
|
SDWORD i, dimensions, vals[VAR_MAX_DIMENSIONS];
|
|
|
|
UBYTE *elements; //[VAR_MAX_DIMENSIONS]
|
|
|
|
SDWORD size, val;//, elementDWords;
|
|
|
|
// UBYTE *pElem;
|
2006-09-19 09:30:50 -07:00
|
|
|
UDWORD *InstrPointer = *pip;
|
2007-06-28 10:47:08 -07:00
|
|
|
UDWORD base, index;
|
|
|
|
|
|
|
|
// get the base index of the array
|
2006-09-19 09:30:50 -07:00
|
|
|
base = (*InstrPointer) & ARRAY_BASE_MASK;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
// get the number of dimensions
|
2006-09-19 09:30:50 -07:00
|
|
|
dimensions = ((*InstrPointer) & ARRAY_DIMENSION_MASK) >> ARRAY_DIMENSION_SHIFT;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
if (base >= psProg->numArrays)
|
|
|
|
{
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE,
|
|
|
|
"interpGetArrayVarData: array base index out of range" );
|
2007-06-28 10:47:08 -07:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (dimensions != psProg->psArrayInfo[base].dimensions)
|
|
|
|
{
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE,
|
|
|
|
"interpGetArrayVarData: dimensions do not match" );
|
2007-06-28 10:47:08 -07:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the number of elements for each dimension
|
|
|
|
elements = psProg->psArrayInfo[base].elements;
|
|
|
|
/* pElem = (UBYTE *) (ip + 1);
|
|
|
|
for(i=0; i<dimensions; i+=1)
|
|
|
|
{
|
|
|
|
elements[i] = *pElem;
|
|
|
|
|
|
|
|
pElem += 1;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
// calculate the index of the array element
|
|
|
|
size = 1;
|
|
|
|
index = 0;
|
|
|
|
for(i=dimensions-1; i>=0; i-=1)
|
|
|
|
{
|
|
|
|
if (!stackPopParams(1, VAL_INT, &val))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (val < 0) || (val >= elements[i]) )
|
|
|
|
{
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpGetArrayVarData: Array index for dimension %d out of range", i );
|
2007-06-28 10:47:08 -07:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
index += val * size;
|
|
|
|
size *= psProg->psArrayInfo[base].elements[i];
|
|
|
|
vals[i] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// print out the debug trace
|
|
|
|
if (interpTrace)
|
|
|
|
{
|
2006-08-22 07:28:49 -07:00
|
|
|
debug( LOG_NEVER, "%d->", base );
|
2007-06-28 10:47:08 -07:00
|
|
|
for(i=0; i<dimensions; i+= 1)
|
|
|
|
{
|
2006-08-22 07:28:49 -07:00
|
|
|
debug( LOG_NEVER, "[%d/%d]", vals[i], elements[i] );
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2006-08-22 07:28:49 -07:00
|
|
|
debug( LOG_NEVER, "(%d) ", index );
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// check the index is valid
|
|
|
|
if (index > psProg->arraySize)
|
|
|
|
{
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpGetArrayVarData: Array indexes out of variable space" );
|
2007-06-28 10:47:08 -07:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the variable data
|
|
|
|
*ppsVal = interpGetVarData(psGlobals, psProg->psArrayInfo[base].base + index);
|
|
|
|
|
|
|
|
// calculate the number of DWORDs needed to store the number of elements for each dimension of the array
|
|
|
|
// elementDWords = (dimensions - 1)/4 + 1;
|
|
|
|
|
|
|
|
// update the insrtuction pointer
|
|
|
|
*pip += 1;// + elementDWords;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Initialise the interpreter
|
|
|
|
BOOL interpInitialise(void)
|
|
|
|
{
|
|
|
|
asInterpTypeEquiv = NULL;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run a compiled script */
|
|
|
|
BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD index, UDWORD offset)
|
|
|
|
{
|
2006-09-19 09:30:50 -07:00
|
|
|
UDWORD *InstrPointer, opcode, data;
|
2007-06-28 10:47:08 -07:00
|
|
|
INTERP_VAL sVal, *psVar;
|
|
|
|
VAL_CHUNK *psGlobals;
|
|
|
|
UDWORD numGlobals;
|
|
|
|
UDWORD *pCodeStart, *pCodeEnd, *pCodeBase;
|
|
|
|
SCRIPT_FUNC scriptFunc;
|
|
|
|
SCRIPT_VARFUNC scriptVarFunc;
|
|
|
|
SCRIPT_CODE *psProg;
|
|
|
|
// SDWORD arrayIndex, dimensions, arrayElements[VAR_MAX_DIMENSIONS];
|
|
|
|
SDWORD instructionCount = 0;
|
|
|
|
|
2006-08-12 03:02:59 -07:00
|
|
|
UDWORD CurEvent;
|
2006-08-15 11:38:51 -07:00
|
|
|
BOOL bStop,bEvent;
|
2006-08-19 06:26:11 -07:00
|
|
|
SDWORD callDepth;
|
2006-09-13 14:16:17 -07:00
|
|
|
const STRING *pTrigLab, *pEventLab;
|
2006-08-19 06:26:11 -07:00
|
|
|
|
|
|
|
//debug(LOG_SCRIPT, "interpRunScript 1");
|
2006-08-12 03:02:59 -07:00
|
|
|
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( PTRVALID(psContext, sizeof(SCRIPT_CONTEXT)),
|
|
|
|
"interpRunScript: invalid context pointer" );
|
2007-06-28 10:47:08 -07:00
|
|
|
psProg=psContext->psCode;
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( PTRVALID(psProg, sizeof(SCRIPT_CODE)),
|
|
|
|
"interpRunScript: invalid script code pointer" );
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2006-08-21 14:20:17 -07:00
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
if (bInterpRunning)
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug(LOG_ERROR,"interpRunScript: interpreter already running"
|
|
|
|
" - callback being called from within a script function?");
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE,
|
2007-06-28 10:47:08 -07:00
|
|
|
"interpRunScript: interpreter already running"
|
2006-08-23 05:58:48 -07:00
|
|
|
" - callback being called from within a script function?" );
|
2007-06-28 10:47:08 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// note that the interpreter is running to stop recursive script calls
|
|
|
|
bInterpRunning = TRUE;
|
|
|
|
|
|
|
|
// Reset the stack in case another script messed up
|
|
|
|
stackReset();
|
|
|
|
|
2006-08-12 03:02:59 -07:00
|
|
|
//reset return stack
|
|
|
|
retStackReset();
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
// Turn off tracing initially
|
|
|
|
interpTrace = FALSE;
|
|
|
|
|
|
|
|
/* Get the global variables */
|
|
|
|
numGlobals = psProg->numGlobals;
|
|
|
|
psGlobals = psContext->psGlobals;
|
|
|
|
|
2006-08-15 11:38:51 -07:00
|
|
|
bEvent = FALSE;
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
// Find the code range
|
|
|
|
switch (runType)
|
|
|
|
{
|
|
|
|
case IRT_TRIGGER:
|
|
|
|
if (index > psProg->numTriggers)
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug(LOG_ERROR,"interpRunScript: trigger index out of range");
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: trigger index out of range" );
|
2007-06-28 10:47:08 -07:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
pCodeBase = psProg->pCode + psProg->pTriggerTab[index];
|
|
|
|
pCodeStart = pCodeBase;
|
|
|
|
pCodeEnd = psProg->pCode + psProg->pTriggerTab[index+1];
|
|
|
|
break;
|
|
|
|
case IRT_EVENT:
|
|
|
|
if (index > psProg->numEvents)
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug(LOG_ERROR,"interpRunScript: trigger index out of range");
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: trigger index out of range" );
|
2007-06-28 10:47:08 -07:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
pCodeBase = psProg->pCode + psProg->pEventTab[index];
|
2006-08-21 14:20:17 -07:00
|
|
|
pCodeStart = pCodeBase + offset; //offset only used for pause() script function
|
2007-06-28 10:47:08 -07:00
|
|
|
pCodeEnd = psProg->pCode + psProg->pEventTab[index+1];
|
2006-08-15 11:38:51 -07:00
|
|
|
|
|
|
|
bEvent = TRUE; //remember it's an event
|
2007-06-28 10:47:08 -07:00
|
|
|
break;
|
|
|
|
default:
|
2006-08-15 11:38:51 -07:00
|
|
|
debug(LOG_ERROR,"interpRunScript: unknown run type");
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: unknown run type" );
|
2007-06-28 10:47:08 -07:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-08-22 07:28:49 -07:00
|
|
|
|
2006-08-21 14:20:17 -07:00
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
// Get the first opcode
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer = pCodeStart;
|
|
|
|
opcode = (*InstrPointer) >> OPCODE_SHIFT;
|
2007-06-28 10:47:08 -07:00
|
|
|
instructionCount = 0;
|
|
|
|
|
2006-08-12 03:02:59 -07:00
|
|
|
CurEvent = index;
|
|
|
|
bStop = FALSE;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2006-08-19 06:26:11 -07:00
|
|
|
//debug(LOG_SCRIPT, "interpRunScript 2");
|
|
|
|
|
2006-08-12 03:02:59 -07:00
|
|
|
while(!bStop)
|
|
|
|
{
|
|
|
|
// Run the code
|
2006-09-19 09:30:50 -07:00
|
|
|
if (InstrPointer < pCodeEnd)// && opcode != OP_EXIT)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2006-08-12 03:02:59 -07:00
|
|
|
if (instructionCount > INTERP_MAXINSTRUCTIONS)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: max instruction count exceeded - infinite loop ?" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE,
|
|
|
|
"interpRunScript: max instruction count exceeded - infinite loop ?" );
|
2007-06-28 10:47:08 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
2006-08-12 03:02:59 -07:00
|
|
|
instructionCount += 1;
|
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
TRCPRINTF(("%-6d ", InstrPointer - psProg->pCode));
|
|
|
|
opcode = (*InstrPointer) >> OPCODE_SHIFT;
|
|
|
|
data = (*InstrPointer) & OPCODE_DATAMASK;
|
2006-08-12 03:02:59 -07:00
|
|
|
switch (opcode)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2006-08-12 03:02:59 -07:00
|
|
|
/* Custom function call */
|
|
|
|
case OP_FUNC:
|
2006-08-15 11:38:51 -07:00
|
|
|
//debug( LOG_SCRIPT, "-OP_FUNC" );
|
|
|
|
//debug( LOG_SCRIPT, "OP_FUNC: remember event %d, ip=%d", CurEvent, (ip + 2) );
|
2006-09-19 09:30:50 -07:00
|
|
|
if(!retStackPush(CurEvent, (InstrPointer + aOpSize[opcode]))) //Remember where to jump back later
|
2006-08-12 03:02:59 -07:00
|
|
|
{
|
2006-09-19 09:30:50 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript() - retStackPush() failed.");
|
2006-08-12 03:02:59 -07:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
CurEvent = *(InstrPointer+1); //Current event = event to jump to
|
2006-08-15 11:38:51 -07:00
|
|
|
|
2006-08-12 03:02:59 -07:00
|
|
|
if (CurEvent > psProg->numEvents)
|
|
|
|
{
|
|
|
|
debug( LOG_ERROR, "interpRunScript: trigger index out of range");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Set new code execution boundries
|
|
|
|
//----------------------------------
|
|
|
|
pCodeBase = psProg->pCode + psProg->pEventTab[CurEvent];
|
|
|
|
pCodeStart = pCodeBase;
|
|
|
|
pCodeEnd = psProg->pCode + psProg->pEventTab[CurEvent+1];
|
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer = pCodeStart; //Start at the beginning of the new event
|
2006-08-12 03:02:59 -07:00
|
|
|
|
2006-08-15 11:38:51 -07:00
|
|
|
//debug( LOG_SCRIPT, "-OP_FUNC: jumped to event %d; ip=%d, numLocalVars: %d", CurEvent, ip, psContext->psCode->numLocalVars[CurEvent] );
|
|
|
|
|
|
|
|
//debug( LOG_SCRIPT, "-END OP_FUNC" );
|
|
|
|
|
2006-08-12 03:02:59 -07:00
|
|
|
break;
|
|
|
|
|
2006-08-15 11:38:51 -07:00
|
|
|
//handle local variables
|
|
|
|
case OP_PUSHLOCAL:
|
|
|
|
|
|
|
|
//debug( LOG_SCRIPT, "OP_PUSHLOCAL");
|
|
|
|
//debug( LOG_SCRIPT, "OP_PUSHLOCAL, (CurEvent=%d, data =%d) num loc vars: %d; pushing: %d", CurEvent, data, psContext->psCode->numLocalVars[CurEvent], psContext->psCode->ppsLocalVarVal[CurEvent][data].v.ival);
|
2006-08-22 07:28:49 -07:00
|
|
|
|
2006-08-15 11:38:51 -07:00
|
|
|
if (data >= psContext->psCode->numLocalVars[CurEvent])
|
|
|
|
{
|
|
|
|
debug(LOG_ERROR, "interpRunScript: OP_PUSHLOCAL: variable index out of range");
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: OP_PUSHLOCAL: variable index out of range" );
|
2006-08-15 11:38:51 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
//debug( LOG_SCRIPT, "OP_PUSHLOCAL 2");
|
|
|
|
//debug(LOG_SCRIPT, "OP_PUSHLOCAL type: %d", psContext->psCode->ppsLocalVarVal[CurEvent][data].type);
|
2006-08-22 07:28:49 -07:00
|
|
|
|
2006-08-15 11:38:51 -07:00
|
|
|
if (!stackPush( &(psContext->psCode->ppsLocalVarVal[CurEvent][data]) ))
|
|
|
|
{
|
|
|
|
debug(LOG_ERROR, "interpRunScript: OP_PUSHLOCAL: push failed");
|
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
//debug( LOG_SCRIPT, "OP_PUSHLOCAL 3");
|
2006-08-22 07:28:49 -07:00
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += aOpSize[opcode];
|
2006-08-15 11:38:51 -07:00
|
|
|
break;
|
|
|
|
case OP_POPLOCAL:
|
2006-08-22 07:28:49 -07:00
|
|
|
|
2006-08-15 11:38:51 -07:00
|
|
|
//debug( LOG_SCRIPT, "OP_POPLOCAL, event index: '%d', data: '%d'", CurEvent, data);
|
|
|
|
//debug( LOG_SCRIPT, "OP_POPLOCAL, numLocalVars: '%d'", psContext->psCode->numLocalVars[CurEvent]);
|
|
|
|
|
|
|
|
if (data >= psContext->psCode->numLocalVars[CurEvent])
|
|
|
|
{
|
|
|
|
debug(LOG_ERROR, "interpRunScript: OP_POPLOCAL: variable index out of range");
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: variable index out of range" );
|
2006-08-15 11:38:51 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
//debug( LOG_SCRIPT, "OP_POPLOCAL 2");
|
|
|
|
//DbgMsg("OP_POPLOCAL type: %d, CurEvent=%d, data=%d", psContext->psCode->ppsLocalVarVal[CurEvent][data].type, CurEvent, data);
|
|
|
|
|
|
|
|
if (!stackPopType( &(psContext->psCode->ppsLocalVarVal[CurEvent][data]) ))
|
|
|
|
{
|
|
|
|
debug(LOG_ERROR, "interpRunScript: OP_POPLOCAL: pop failed");
|
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
//debug(LOG_SCRIPT, "OP_POPLOCAL: type=%d, val=%d", psContext->psCode->ppsLocalVarVal[CurEvent][data].type, psContext->psCode->ppsLocalVarVal[CurEvent][data].v.ival);
|
|
|
|
|
|
|
|
//debug( LOG_SCRIPT, "OP_POPLOCAL 3");
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += aOpSize[opcode];
|
2006-08-15 11:38:51 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OP_PUSHLOCALREF:
|
|
|
|
|
|
|
|
// The type of the variable is stored in with the opcode
|
2006-09-19 09:30:50 -07:00
|
|
|
sVal.type = (*InstrPointer) & OPCODE_DATAMASK;
|
2006-08-15 11:38:51 -07:00
|
|
|
|
|
|
|
/* get local var index */
|
2006-09-19 09:30:50 -07:00
|
|
|
data = *(InstrPointer + 1);
|
2006-08-15 11:38:51 -07:00
|
|
|
|
|
|
|
if (data >= psContext->psCode->numLocalVars[CurEvent])
|
|
|
|
{
|
|
|
|
debug(LOG_ERROR, "interpRunScript: OP_PUSHLOCALREF: variable index out of range");
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: OP_PUSHLOCALREF: variable index out of range" );
|
2006-08-15 11:38:51 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
/* get local variable */
|
|
|
|
psVar = &(psContext->psCode->ppsLocalVarVal[CurEvent][data]);
|
|
|
|
|
|
|
|
sVal.v.oval = &(psVar->v.ival);
|
|
|
|
|
|
|
|
TRCPRINTF(("PUSHREF "));
|
|
|
|
TRCPRINTVAL(&sVal);
|
|
|
|
TRCPRINTF(("\n"));
|
|
|
|
|
|
|
|
if (!stackPush(&sVal))
|
|
|
|
{
|
|
|
|
debug(LOG_ERROR, "interpRunScript: OP_PUSHLOCALREF: push failed");
|
|
|
|
goto exit_with_error;
|
|
|
|
}
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += aOpSize[opcode];
|
2006-08-15 11:38:51 -07:00
|
|
|
break;
|
|
|
|
|
2006-08-12 03:02:59 -07:00
|
|
|
case OP_PUSH:
|
|
|
|
// The type of the value is stored in with the opcode
|
2006-09-19 09:30:50 -07:00
|
|
|
sVal.type = (*InstrPointer) & OPCODE_DATAMASK;
|
2006-08-12 03:02:59 -07:00
|
|
|
// Copy the data as a DWORD
|
2006-09-19 09:30:50 -07:00
|
|
|
sVal.v.ival = (SDWORD)(*(InstrPointer+1));
|
2006-08-12 03:02:59 -07:00
|
|
|
TRCPRINTF(("PUSH "));
|
|
|
|
TRCPRINTVAL(&sVal);
|
|
|
|
TRCPRINTF(("\n"));
|
|
|
|
if (!stackPush(&sVal))
|
|
|
|
{
|
|
|
|
// Eeerk, out of memory
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: out of memory!" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: out of memory!" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += aOpSize[opcode];
|
2006-08-12 03:02:59 -07:00
|
|
|
break;
|
|
|
|
case OP_PUSHREF:
|
|
|
|
// The type of the variable is stored in with the opcode
|
2006-09-19 09:30:50 -07:00
|
|
|
sVal.type = (*InstrPointer) & OPCODE_DATAMASK;
|
2006-08-12 03:02:59 -07:00
|
|
|
// store the pointer
|
2006-09-19 09:30:50 -07:00
|
|
|
psVar = interpGetVarData(psGlobals, *(InstrPointer + 1));
|
2006-08-12 03:02:59 -07:00
|
|
|
sVal.v.oval = &(psVar->v.ival);
|
|
|
|
TRCPRINTF(("PUSHREF "));
|
|
|
|
TRCPRINTVAL(&sVal);
|
|
|
|
TRCPRINTF(("\n"));
|
|
|
|
if (!stackPush(&sVal))
|
|
|
|
{
|
|
|
|
// Eeerk, out of memory
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: out of memory!" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: out of memory!" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += aOpSize[opcode];
|
2006-08-12 03:02:59 -07:00
|
|
|
break;
|
|
|
|
case OP_POP:
|
|
|
|
TRCPRINTF(("POP\n"));
|
|
|
|
if (!stackPop(&sVal))
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: could not do stack pop" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: could not do stack pop" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += aOpSize[opcode];
|
2006-08-12 03:02:59 -07:00
|
|
|
break;
|
|
|
|
case OP_BINARYOP:
|
|
|
|
TRCPRINTMATHSOP(data);
|
|
|
|
if (!stackBinaryOp((OPCODE)data))
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: could not do binary op" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: could not do binary op" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
TRCPRINTSTACKTOP();
|
|
|
|
TRCPRINTF(("\n"));
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += aOpSize[opcode];
|
2006-08-12 03:02:59 -07:00
|
|
|
break;
|
|
|
|
case OP_UNARYOP:
|
|
|
|
TRCPRINTMATHSOP(data);
|
|
|
|
if (!stackUnaryOp((OPCODE)data))
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: could not do unary op" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: could not do unary op" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
TRCPRINTSTACKTOP();
|
|
|
|
TRCPRINTF(("\n"));
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += aOpSize[opcode];
|
2006-08-12 03:02:59 -07:00
|
|
|
break;
|
|
|
|
case OP_PUSHGLOBAL:
|
|
|
|
TRCPRINTF(("PUSHGLOBAL %d\n", data));
|
|
|
|
if (data >= numGlobals)
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: variable index out of range" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: variable index out of range" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
if (!stackPush(interpGetVarData(psGlobals, data)))
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: could not do stack push" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: could not do stack push" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += aOpSize[opcode];
|
2006-08-12 03:02:59 -07:00
|
|
|
break;
|
|
|
|
case OP_POPGLOBAL:
|
|
|
|
TRCPRINTF(("POPGLOBAL %d ", data));
|
|
|
|
TRCPRINTSTACKTOP();
|
|
|
|
TRCPRINTF(("\n"));
|
|
|
|
if (data >= numGlobals)
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: variable index out of range" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: variable index out of range" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
if (!stackPopType(interpGetVarData(psGlobals, data)))
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: could not do stack pop" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: could not do stack pop" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += aOpSize[opcode];
|
2006-08-12 03:02:59 -07:00
|
|
|
break;
|
|
|
|
case OP_PUSHARRAYGLOBAL:
|
|
|
|
// get the number of array elements
|
|
|
|
// arrayElements = (data & ARRAY_ELEMENT_MASK) >> ARRAY_ELEMENT_SHIFT;
|
|
|
|
// data = data & ARRAY_INDEX_MASK;
|
|
|
|
// get the array index
|
|
|
|
// if (!stackPopParams(1, VAL_INT, &arrayIndex))
|
|
|
|
// {
|
|
|
|
// goto exit_with_error;
|
|
|
|
// }
|
|
|
|
// TRCPRINTF(("PUSHARRAYGLOBAL [%d] %d(+%d)\n", arrayIndex, data, arrayElements));
|
|
|
|
// if (data + arrayElements > numGlobals)
|
|
|
|
// {
|
2006-08-23 05:58:48 -07:00
|
|
|
// ASSERT( FALSE, "interpRunScript: variable index out of range" );
|
2006-08-12 03:02:59 -07:00
|
|
|
// goto exit_with_error;
|
|
|
|
// }
|
|
|
|
// if (arrayIndex < 0 || arrayIndex >= arrayElements)
|
|
|
|
// {
|
2006-08-23 05:58:48 -07:00
|
|
|
// ASSERT( FALSE, "interpRunScript: array index out of range" );
|
2006-08-12 03:02:59 -07:00
|
|
|
// goto exit_with_error;
|
|
|
|
// }
|
|
|
|
TRCPRINTF(("PUSHARRAYGLOBAL "));
|
2006-09-19 09:30:50 -07:00
|
|
|
if (!interpGetArrayVarData(&InstrPointer, psGlobals, psProg, &psVar))
|
2006-08-12 03:02:59 -07:00
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: could not get array var data, CurEvent=%d", CurEvent );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: could not get array var data" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
TRCPRINTF(("\n"));
|
|
|
|
if (!stackPush(psVar))
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: could not do stack push" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: could not do stack push" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OP_POPARRAYGLOBAL:
|
|
|
|
// get the number of array elements
|
|
|
|
// arrayElements = (data & ARRAY_ELEMENT_MASK) >> ARRAY_ELEMENT_SHIFT;
|
|
|
|
// data = data & ARRAY_INDEX_MASK;
|
|
|
|
// get the array index
|
|
|
|
/* if (!stackPopParams(1, VAL_INT, &arrayIndex))
|
|
|
|
{
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: could not do pop of params" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
TRCPRINTF(("POPARRAYGLOBAL [%d] %d(+%d) ", arrayIndex, data, arrayElements));
|
|
|
|
TRCPRINTSTACKTOP();
|
|
|
|
TRCPRINTF(("\n"));
|
|
|
|
if (data + arrayElements > numGlobals)
|
|
|
|
{
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: variable index out of range" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
if (arrayIndex < 0 || arrayIndex >= arrayElements)
|
|
|
|
{
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: array index out of range" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
if (!stackPopType(interpGetVarData(psGlobals, data + arrayIndex)))
|
|
|
|
{
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: could not do pop stack of type" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
ip += aOpSize[opcode];*/
|
|
|
|
TRCPRINTF(("POPARRAYGLOBAL "));
|
2006-09-19 09:30:50 -07:00
|
|
|
if (!interpGetArrayVarData(&InstrPointer, psGlobals, psProg, &psVar))
|
2006-08-12 03:02:59 -07:00
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: could not get array var data" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: could not get array var data" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
TRCPRINTSTACKTOP();
|
|
|
|
TRCPRINTF(("\n"));
|
|
|
|
if (!stackPopType(psVar))
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: could not do pop stack of type" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: could not do pop stack of type" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OP_JUMPFALSE:
|
|
|
|
TRCPRINTF(("JUMPFALSE %d (%d)",
|
2006-09-19 09:30:50 -07:00
|
|
|
(SWORD)data, InstrPointer - psProg->pCode + (SWORD)data));
|
2006-08-12 03:02:59 -07:00
|
|
|
if (!stackPop(&sVal))
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: could not do pop of stack" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: could not do pop of stack" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
if (!sVal.v.bval)
|
|
|
|
{
|
|
|
|
// Do the jump
|
|
|
|
TRCPRINTF((" - done -\n"));
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += (SWORD)data;
|
|
|
|
if (InstrPointer < pCodeStart || InstrPointer > pCodeEnd)
|
2006-08-12 03:02:59 -07:00
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: jump out of range" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: jump out of range" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TRCPRINTF(("\n"));
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += aOpSize[opcode];
|
2006-08-12 03:02:59 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OP_JUMP:
|
|
|
|
TRCPRINTF(("JUMP %d (%d)\n",
|
2006-09-19 09:30:50 -07:00
|
|
|
(SWORD)data, InstrPointer - psProg->pCode + (SWORD)data));
|
2007-06-28 10:47:08 -07:00
|
|
|
// Do the jump
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += (SWORD)data;
|
|
|
|
if (InstrPointer < pCodeStart || InstrPointer > pCodeEnd)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: jump out of range" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: jump out of range" );
|
2007-06-28 10:47:08 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
2006-08-12 03:02:59 -07:00
|
|
|
break;
|
|
|
|
case OP_CALL:
|
2006-08-19 06:26:11 -07:00
|
|
|
//debug(LOG_SCRIPT, "OP_CALL");
|
2006-09-19 09:30:50 -07:00
|
|
|
TRCPRINTFUNC( (SCRIPT_FUNC)(*(InstrPointer+1)) );
|
2007-06-28 10:47:08 -07:00
|
|
|
TRCPRINTF(("\n"));
|
2006-09-19 09:30:50 -07:00
|
|
|
scriptFunc = (SCRIPT_FUNC)*(InstrPointer+1);
|
2006-08-19 06:26:11 -07:00
|
|
|
//debug(LOG_SCRIPT, "OP_CALL 1");
|
2006-08-12 03:02:59 -07:00
|
|
|
if (!scriptFunc())
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: could not do func" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: could not do func" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
2006-08-19 06:26:11 -07:00
|
|
|
//debug(LOG_SCRIPT, "OP_CALL 2");
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += aOpSize[opcode];
|
2006-08-19 06:26:11 -07:00
|
|
|
//debug(LOG_SCRIPT, "OP_CALL 3");
|
2006-08-12 03:02:59 -07:00
|
|
|
break;
|
|
|
|
case OP_VARCALL:
|
|
|
|
TRCPRINTF(("VARCALL "));
|
2006-09-19 09:30:50 -07:00
|
|
|
TRCPRINTVARFUNC( (SCRIPT_VARFUNC)(*(InstrPointer+1)), data );
|
2006-08-12 03:02:59 -07:00
|
|
|
TRCPRINTF(("(%d)\n", data));
|
2006-09-19 09:30:50 -07:00
|
|
|
scriptVarFunc = (SCRIPT_VARFUNC)*(InstrPointer+1);
|
2006-08-12 03:02:59 -07:00
|
|
|
if (!scriptVarFunc(data))
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: could not do var func" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: could not do var func" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += aOpSize[opcode];
|
2006-08-12 03:02:59 -07:00
|
|
|
break;
|
|
|
|
case OP_EXIT:
|
|
|
|
// jump out of the code
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer = pCodeEnd;
|
2006-08-12 03:02:59 -07:00
|
|
|
break;
|
|
|
|
case OP_PAUSE:
|
|
|
|
TRCPRINTF(("PAUSE %d\n", data));
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( stackEmpty(),
|
|
|
|
"interpRunScript: OP_PAUSE without empty stack" );
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer += aOpSize[opcode];
|
2006-08-12 03:02:59 -07:00
|
|
|
// tell the event system to reschedule this event
|
2006-09-19 09:30:50 -07:00
|
|
|
if (!eventAddPauseTrigger(psContext, index, InstrPointer - pCodeBase, data))
|
2006-08-12 03:02:59 -07:00
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript: could not add pause trigger" );
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( FALSE, "interpRunScript: could not add pause trigger" );
|
2006-08-12 03:02:59 -07:00
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
// now jump out of the event
|
2006-09-19 09:30:50 -07:00
|
|
|
InstrPointer = pCodeEnd;
|
2006-08-12 03:02:59 -07:00
|
|
|
break;
|
|
|
|
default:
|
2006-09-19 09:07:06 -07:00
|
|
|
debug(LOG_ERROR, "interpRunScript: unknown opcode: %d", opcode);
|
|
|
|
ASSERT( FALSE, "interpRunScript: unknown opcode: %d", opcode );
|
2007-06-28 10:47:08 -07:00
|
|
|
goto exit_with_error;
|
2006-08-12 03:02:59 -07:00
|
|
|
break;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2006-08-12 03:02:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
else //End of the event reached, see if we have to jump back to the caller function or just exit
|
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
//debug(LOG_SCRIPT, "End of event reached");
|
2006-08-12 03:02:59 -07:00
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
if(!retStackIsEmpty()) //There was a caller function before this one
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
//debug(LOG_SCRIPT, "GetCallDepth = %d", GetCallDepth());
|
|
|
|
|
2006-08-21 14:20:17 -07:00
|
|
|
//reset local vars (since trigger can't be called, only local vars of an event can be reset here)
|
|
|
|
if(!resetLocalVars(psProg, CurEvent))
|
|
|
|
{
|
|
|
|
debug( LOG_ERROR, "interpRunScript: could not reset local vars for event %d", CurEvent );
|
|
|
|
goto exit_with_error;
|
|
|
|
}
|
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
if (!retStackPop(&CurEvent, &InstrPointer))
|
2006-08-12 03:02:59 -07:00
|
|
|
{
|
2006-09-19 09:30:50 -07:00
|
|
|
debug( LOG_ERROR, "interpRunScript() - PopRetStack() failed.");
|
2006-08-12 03:02:59 -07:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-08-15 11:38:51 -07:00
|
|
|
//debug( LOG_SCRIPT, "RETURNED TO CALLER EVENT %d", CurEvent );
|
2006-08-12 03:02:59 -07:00
|
|
|
|
|
|
|
//Set new boundries
|
|
|
|
//--------------------------
|
2006-09-19 09:30:50 -07:00
|
|
|
if(retStackIsEmpty()) //if we jumped back to the original caller
|
2006-08-21 14:20:17 -07:00
|
|
|
{
|
|
|
|
if(!bEvent) //original caller was a trigger (is it possible at all?)
|
|
|
|
{
|
|
|
|
pCodeBase = psProg->pCode + psProg->pTriggerTab[CurEvent];
|
|
|
|
pCodeStart = pCodeBase;
|
|
|
|
pCodeEnd = psProg->pCode + psProg->pTriggerTab[CurEvent+1];
|
|
|
|
}
|
|
|
|
else //original caller was an event
|
|
|
|
{
|
|
|
|
pCodeBase = psProg->pCode + psProg->pEventTab[CurEvent];
|
|
|
|
pCodeStart = pCodeBase + offset; //also use the offset passed, since it's an original caller event (offset is used for pause() )
|
|
|
|
pCodeEnd = psProg->pCode + psProg->pEventTab[CurEvent+1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else //we are still jumping thru functions (this can't be a callback, since it can't/should not be called)
|
|
|
|
{
|
|
|
|
pCodeBase = psProg->pCode + psProg->pEventTab[CurEvent];
|
|
|
|
pCodeStart = pCodeBase;
|
|
|
|
pCodeEnd = psProg->pCode + psProg->pEventTab[CurEvent+1];
|
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2006-08-12 03:02:59 -07:00
|
|
|
else
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2006-08-15 11:38:51 -07:00
|
|
|
//debug( LOG_SCRIPT, " *** CALL STACK EMPTY ***" );
|
|
|
|
|
2006-08-21 14:20:17 -07:00
|
|
|
//reset local vars only if original caller was an event, not a trigger
|
|
|
|
if(bEvent)
|
2006-08-15 11:38:51 -07:00
|
|
|
{
|
2006-08-21 14:20:17 -07:00
|
|
|
if(!resetLocalVars(psProg, index))
|
|
|
|
{
|
|
|
|
debug( LOG_ERROR, "interpRunScript: could not reset local vars" );
|
|
|
|
goto exit_with_error;
|
|
|
|
}
|
2006-08-15 11:38:51 -07:00
|
|
|
}
|
2006-08-12 03:02:59 -07:00
|
|
|
|
|
|
|
bStop = TRUE; //Stop execution of this event here, no more calling functions stored
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
2006-08-12 03:02:59 -07:00
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2006-08-12 03:02:59 -07:00
|
|
|
|
|
|
|
|
2006-08-19 06:26:11 -07:00
|
|
|
//debug(LOG_SCRIPT, "interpRunScript 3");
|
2006-08-12 03:02:59 -07:00
|
|
|
|
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
TRCPRINTF(("%-6d EXIT\n", InstrPointer - psProg->pCode));
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
bInterpRunning = FALSE;
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
exit_with_error:
|
|
|
|
// Deal with the script crashing or running out of memory
|
2006-08-15 11:38:51 -07:00
|
|
|
debug(LOG_ERROR,"interpRunScript: *** ERROR EXIT *** (CurEvent=%d)", CurEvent);
|
2006-08-19 06:26:11 -07:00
|
|
|
|
2006-08-21 14:20:17 -07:00
|
|
|
if(bEvent)
|
|
|
|
debug(LOG_ERROR,"Original event ID: %d (of %d)", index, psProg->numEvents);
|
|
|
|
else
|
|
|
|
debug(LOG_ERROR,"Original trigger ID: %d (of %d)", index, psProg->numTriggers);
|
2006-08-19 06:26:11 -07:00
|
|
|
|
|
|
|
debug(LOG_ERROR,"Current event ID: %d (of %d)", CurEvent, psProg->numEvents);
|
2006-09-19 09:30:50 -07:00
|
|
|
callDepth = retStackCallDepth();
|
2006-08-19 06:26:11 -07:00
|
|
|
debug(LOG_ERROR,"Call depth : %d", callDepth);
|
|
|
|
|
|
|
|
if(psProg->psDebug != NULL)
|
|
|
|
{
|
|
|
|
debug(LOG_ERROR,"Displaying debug info:");
|
|
|
|
|
|
|
|
if(bEvent)
|
|
|
|
{
|
|
|
|
// find the debug info for the original (caller) event
|
|
|
|
pEventLab = eventGetEventID(psProg, index);
|
|
|
|
debug(LOG_ERROR,"Original event name: %s", pEventLab);
|
|
|
|
|
|
|
|
pEventLab = eventGetEventID(psProg, CurEvent);
|
|
|
|
debug(LOG_ERROR,"Current event name: %s", pEventLab);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// find the debug info for the trigger
|
|
|
|
pTrigLab = eventGetTriggerID(psProg, index);
|
|
|
|
debug(LOG_ERROR,"Trigger: %s", pTrigLab);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
TRCPRINTF(("*** ERROR EXIT ***\n"));
|
|
|
|
bInterpRunning = FALSE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Set the type equivalence table */
|
|
|
|
void scriptSetTypeEquiv(TYPE_EQUIV *psTypeTab)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
SDWORD i;
|
|
|
|
|
|
|
|
for(i=0; psTypeTab[i].base != 0; i++)
|
|
|
|
{
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( psTypeTab[i].base >= VAL_USERTYPESTART,
|
|
|
|
"scriptSetTypeEquiv: can only set type equivalence for user types" );
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
asInterpTypeEquiv = psTypeTab;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Check if two types are equivalent */
|
|
|
|
BOOL interpCheckEquiv(INTERP_TYPE to, INTERP_TYPE from)
|
|
|
|
{
|
|
|
|
SDWORD i,j;
|
|
|
|
BOOL toRef = FALSE, fromRef = FALSE;
|
|
|
|
|
|
|
|
// check for the VAL_REF flag
|
|
|
|
if (to & VAL_REF)
|
|
|
|
{
|
|
|
|
toRef = TRUE;
|
|
|
|
to = to & ~VAL_REF;
|
|
|
|
}
|
|
|
|
if (from & VAL_REF)
|
|
|
|
{
|
|
|
|
fromRef = TRUE;
|
|
|
|
from = from & ~VAL_REF;
|
|
|
|
}
|
|
|
|
if (toRef != fromRef)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (to == from)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (asInterpTypeEquiv)
|
|
|
|
{
|
|
|
|
for(i=0; asInterpTypeEquiv[i].base != 0; i++)
|
|
|
|
{
|
|
|
|
if (asInterpTypeEquiv[i].base == to)
|
|
|
|
{
|
|
|
|
for(j=0; j<asInterpTypeEquiv[i].numEquiv; j++)
|
|
|
|
{
|
|
|
|
if (asInterpTypeEquiv[i].aEquivTypes[j] == from)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Instinct function to turn on tracing */
|
|
|
|
BOOL interpTraceOn(void)
|
|
|
|
{
|
|
|
|
interpTrace = TRUE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Instinct function to turn off tracing */
|
|
|
|
BOOL interpTraceOff(void)
|
|
|
|
{
|
|
|
|
interpTrace = FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-08-12 03:02:59 -07:00
|
|
|
|
|
|
|
/* Call stack stuff */
|
2006-09-19 09:30:50 -07:00
|
|
|
#define RETSTACK_SIZE 100
|
2006-08-12 03:02:59 -07:00
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
static ReturnAddressStack_t retStack[RETSTACK_SIZE]; // Primitive stack of return addresses
|
|
|
|
static Sint8 retStackPos = -1; // Current Position, always points to the last valid element
|
2006-08-12 03:02:59 -07:00
|
|
|
|
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
inline Sint8 retStackCallDepth(void)
|
2006-08-12 03:02:59 -07:00
|
|
|
{
|
2006-09-19 09:30:50 -07:00
|
|
|
return (retStackPos + 1);
|
|
|
|
}
|
2006-08-12 03:02:59 -07:00
|
|
|
|
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
static inline void retStackReset(void)
|
|
|
|
{
|
|
|
|
retStackPos = -1; // Beginning of the stack
|
|
|
|
}
|
2006-08-12 03:02:59 -07:00
|
|
|
|
2006-08-15 11:38:51 -07:00
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
static inline BOOL retStackIsEmpty(void)
|
|
|
|
{
|
|
|
|
if(retStackPos < 0) return TRUE;
|
|
|
|
return FALSE;
|
2006-08-12 03:02:59 -07:00
|
|
|
}
|
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
|
|
|
|
static inline BOOL retStackIsFull(void)
|
2006-08-12 03:02:59 -07:00
|
|
|
{
|
2006-09-19 09:30:50 -07:00
|
|
|
if(retStackPos >= RETSTACK_SIZE) return TRUE;
|
2006-08-12 03:02:59 -07:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
|
|
|
|
static BOOL retStackPush(UDWORD CallerIndex, UDWORD *ReturnAddress)
|
2006-08-12 03:02:59 -07:00
|
|
|
{
|
2006-09-19 09:30:50 -07:00
|
|
|
if (retStackIsFull())
|
2006-08-12 03:02:59 -07:00
|
|
|
{
|
2006-09-19 09:30:50 -07:00
|
|
|
debug( LOG_ERROR, "retStackPush(): return address stack is full");
|
|
|
|
return FALSE; // Stack full
|
2006-08-12 03:02:59 -07:00
|
|
|
}
|
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
retStackPos++;
|
|
|
|
retStack[retStackPos].CallerIndex = CallerIndex;
|
|
|
|
retStack[retStackPos].ReturnAddress = ReturnAddress;
|
2006-08-12 03:02:59 -07:00
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
//debug( LOG_SCRIPT, "retStackPush: Event=%i Address=%p, ", EventTrigIndex, ReturnAddress);
|
2006-08-12 03:02:59 -07:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-09-19 09:30:50 -07:00
|
|
|
|
|
|
|
static BOOL retStackPop(UDWORD *CallerIndex, UDWORD **ReturnAddress)
|
2006-08-12 03:02:59 -07:00
|
|
|
{
|
2006-09-19 09:30:50 -07:00
|
|
|
if (retStackIsEmpty())
|
|
|
|
{
|
|
|
|
debug( LOG_ERROR, "retStackPop(): return address stack is empty");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*CallerIndex = retStack[retStackPos].CallerIndex;
|
|
|
|
*ReturnAddress = retStack[retStackPos].ReturnAddress;
|
|
|
|
retStackPos--;
|
|
|
|
|
|
|
|
//debug( LOG_SCRIPT, "retStackPop: Event=%i Address=%p", *EventTrigIndex, *ReturnAddress);
|
|
|
|
|
|
|
|
return TRUE;
|
2006-08-12 09:52:37 -07:00
|
|
|
}
|