/xbmc/visualizations/Vortex/angelscript/angelscript/source/as_context.cpp
http://github.com/xbmc/xbmc · C++ · 3828 lines · 2717 code · 637 blank · 474 comment · 518 complexity · a92b774b4f5f67260a2119c3919efb9d MD5 · raw file
Large files are truncated click here to view the full file
- /*
- AngelCode Scripting Library
- Copyright (c) 2003-2009 Andreas Jonsson
- This software is provided 'as-is', without any express or implied
- warranty. In no event will the authors be held liable for any
- damages arising from the use of this software.
- Permission is granted to anyone to use this software for any
- purpose, including commercial applications, and to alter it and
- redistribute it freely, subject to the following restrictions:
- 1. The origin of this software must not be misrepresented; you
- must not claim that you wrote the original software. If you use
- this software in a product, an acknowledgment in the product
- documentation would be appreciated but is not required.
- 2. Altered source versions must be plainly marked as such, and
- must not be misrepresented as being the original software.
- 3. This notice may not be removed or altered from any source
- distribution.
- The original version of this library can be located at:
- http://www.angelcode.com/angelscript/
- Andreas Jonsson
- andreas@angelcode.com
- */
- //
- // as_context.cpp
- //
- // This class handles the execution of the byte code
- //
- #include <math.h> // fmodf()
- #include "as_config.h"
- #include "as_context.h"
- #include "as_scriptengine.h"
- #include "as_tokendef.h"
- #include "as_texts.h"
- #include "as_callfunc.h"
- #include "as_generic.h"
- #include "as_debug.h" // mkdir()
- #include "as_bytecode.h"
- #include "as_scriptobject.h"
- #ifdef _MSC_VER
- #pragma warning(disable:4702) // unreachable code
- #endif
- BEGIN_AS_NAMESPACE
- // We need at least 2 DWORDs reserved for exception handling
- // We need at least 1 DWORD reserved for calling system functions
- const int RESERVE_STACK = 2*AS_PTR_SIZE;
- // For each script function call we push 5 DWORDs on the call stack
- const int CALLSTACK_FRAME_SIZE = 5;
- #ifdef AS_DEBUG
- // Instruction statistics
- int instrCount[256];
- int instrCount2[256][256];
- int lastBC;
- class asCDebugStats
- {
- public:
- asCDebugStats()
- {
- memset(instrCount, 0, sizeof(instrCount));
- }
- ~asCDebugStats()
- {
- /*
- // This code writes out some statistics for the VM.
- // It's useful for determining what needs to be optimized.
- _mkdir("AS_DEBUG");
- FILE *f = fopen("AS_DEBUG/total.txt", "at");
- if( f )
- {
- // Output instruction statistics
- fprintf(f, "\nTotal count\n");
- int n;
- for( n = 0; n < BC_MAXBYTECODE; n++ )
- {
- if( bcName[n].name && instrCount[n] > 0 )
- fprintf(f, "%-10.10s : %.0f\n", bcName[n].name, instrCount[n]);
- }
- fprintf(f, "\nNever executed\n");
- for( n = 0; n < BC_MAXBYTECODE; n++ )
- {
- if( bcName[n].name && instrCount[n] == 0 )
- fprintf(f, "%-10.10s\n", bcName[n].name);
- }
- fclose(f);
- }
- */
- }
- double instrCount[256];
- } stats;
- #endif
- AS_API asIScriptContext *asGetActiveContext()
- {
- asASSERT(threadManager);
- asCThreadLocalData *tld = threadManager->GetLocalData();
- if( tld->activeContexts.GetLength() == 0 )
- return 0;
- return tld->activeContexts[tld->activeContexts.GetLength()-1];
- }
- void asPushActiveContext(asIScriptContext *ctx)
- {
- asASSERT(threadManager);
- asCThreadLocalData *tld = threadManager->GetLocalData();
- tld->activeContexts.PushLast(ctx);
- }
- void asPopActiveContext(asIScriptContext *ctx)
- {
- asASSERT(threadManager);
- asCThreadLocalData *tld = threadManager->GetLocalData();
- asASSERT(tld->activeContexts.GetLength() > 0);
- asASSERT(tld->activeContexts[tld->activeContexts.GetLength()-1] == ctx);
- UNUSED_VAR(ctx);
- tld->activeContexts.PopLast();
- }
- asCContext::asCContext(asCScriptEngine *engine, bool holdRef)
- {
- #ifdef AS_DEBUG
- memset(instrCount, 0, sizeof(instrCount));
- memset(instrCount2, 0, sizeof(instrCount2));
- lastBC = 255;
- #endif
- holdEngineRef = holdRef;
- if( holdRef )
- engine->AddRef();
- this->engine = engine;
- status = asEXECUTION_UNINITIALIZED;
- stackBlockSize = 0;
- refCount.set(1);
- inExceptionHandler = false;
- isStackMemoryNotAllocated = false;
- #ifdef AS_DEPRECATED
- // Deprecated since 2009-12-08, 2.18.0
- stringFunction = 0;
- #endif
- currentFunction = 0;
- regs.objectRegister = 0;
- initialFunction = 0;
- lineCallback = false;
- exceptionCallback = false;
- regs.doProcessSuspend = false;
- doSuspend = false;
- userData = 0;
- }
- asCContext::~asCContext()
- {
- DetachEngine();
- }
- int asCContext::AddRef()
- {
- return refCount.atomicInc();
- }
- int asCContext::Release()
- {
- int r = refCount.atomicDec();
- if( r == 0 )
- {
- asDELETE(this,asCContext);
- return 0;
- }
- return r;
- }
- void asCContext::DetachEngine()
- {
- if( engine == 0 ) return;
- // Abort any execution
- Abort();
- // Free all resources
- Unprepare();
- // Clear engine pointer
- if( holdEngineRef )
- engine->Release();
- engine = 0;
- }
- asIScriptEngine *asCContext::GetEngine()
- {
- return engine;
- }
- void *asCContext::SetUserData(void *data)
- {
- void *oldData = userData;
- userData = data;
- return oldData;
- }
- void *asCContext::GetUserData()
- {
- return userData;
- }
- int asCContext::Prepare(int funcID)
- {
- if( status == asEXECUTION_ACTIVE || status == asEXECUTION_SUSPENDED )
- return asCONTEXT_ACTIVE;
- // Clean the stack if not done before
- if( status != asEXECUTION_FINISHED && status != asEXECUTION_UNINITIALIZED )
- CleanStack();
- // Release the returned object (if any)
- CleanReturnObject();
- if( funcID == -1 )
- {
- // Use the previously prepared function
- if( initialFunction == 0 )
- return asNO_FUNCTION;
- currentFunction = initialFunction;
- }
- else if( initialFunction && initialFunction->id == funcID )
- {
- currentFunction = initialFunction;
- }
- else
- {
- // Check engine pointer
- asASSERT( engine );
- if( initialFunction )
- initialFunction->Release();
- initialFunction = engine->GetScriptFunction(funcID);
- if( initialFunction == 0 )
- return asNO_FUNCTION;
- initialFunction->AddRef();
- currentFunction = initialFunction;
- regs.globalVarPointers = currentFunction->globalVarPointers.AddressOf();
- // Determine the minimum stack size needed
- // TODO: optimize: GetSpaceNeededForArguments() should be precomputed
- int stackSize = currentFunction->GetSpaceNeededForArguments() + currentFunction->stackNeeded + RESERVE_STACK;
- stackSize = stackSize > engine->initialContextStackSize ? stackSize : engine->initialContextStackSize;
- if( stackSize > stackBlockSize )
- {
- for( asUINT n = 0; n < stackBlocks.GetLength(); n++ )
- if( stackBlocks[n] )
- {
- asDELETEARRAY(stackBlocks[n]);
- }
- stackBlocks.SetLength(0);
- stackBlockSize = stackSize;
- asDWORD *stack = asNEWARRAY(asDWORD,stackBlockSize);
- stackBlocks.PushLast(stack);
- }
- // Reserve space for the arguments and return value
- returnValueSize = currentFunction->GetSpaceNeededForReturnValue();
- // TODO: optimize: GetSpaceNeededForArguments() should be precomputed
- argumentsSize = currentFunction->GetSpaceNeededForArguments() + (currentFunction->objectType ? AS_PTR_SIZE : 0);
- }
- // Reset state
- // Most of the time the previous state will be asEXECUTION_FINISHED, in which case the values are already initialized
- if( status != asEXECUTION_FINISHED )
- {
- exceptionLine = -1;
- exceptionFunction = 0;
- isCallingSystemFunction = false;
- doAbort = false;
- doSuspend = false;
- regs.doProcessSuspend = lineCallback;
- externalSuspendRequest = false;
- stackIndex = 0;
- }
- status = asEXECUTION_PREPARED;
- // Reserve space for the arguments and return value
- regs.stackFramePointer = stackBlocks[0] + stackBlockSize - argumentsSize;
- regs.stackPointer = regs.stackFramePointer;
- // Set arguments to 0
- memset(regs.stackPointer, 0, 4*argumentsSize);
- if( currentFunction->funcType == asFUNC_SCRIPT )
- {
- regs.programPointer = currentFunction->byteCode.AddressOf();
- // Set all object variables to 0
- for( asUINT n = 0; n < currentFunction->objVariablePos.GetLength(); n++ )
- {
- int pos = currentFunction->objVariablePos[n];
- *(size_t*)®s.stackFramePointer[-pos] = 0;
- }
- }
- else
- regs.programPointer = 0;
- return asSUCCESS;
- }
- // Free all resources
- int asCContext::Unprepare()
- {
- if( status == asEXECUTION_ACTIVE || status == asEXECUTION_SUSPENDED )
- return asCONTEXT_ACTIVE;
- // Only clean the stack if the context was prepared but not executed
- if( status != asEXECUTION_UNINITIALIZED )
- CleanStack();
- // Release the returned object (if any)
- CleanReturnObject();
- // Release the initial function
- if( initialFunction )
- initialFunction->Release();
- // Clear function pointers
- initialFunction = 0;
- currentFunction = 0;
- exceptionFunction = 0;
- regs.programPointer = 0;
- // Reset status
- status = asEXECUTION_UNINITIALIZED;
- // Deallocate the stack blocks
- for( asUINT n = 0; n < stackBlocks.GetLength(); n++ )
- {
- if( stackBlocks[n] )
- {
- asDELETEARRAY(stackBlocks[n]);
- }
- }
- stackBlocks.SetLength(0);
- stackBlockSize = 0;
- regs.stackFramePointer = 0;
- regs.stackPointer = 0;
- stackIndex = 0;
- #ifdef AS_DEPRECATED
- // Deprecated since 2009-12-08, 2.18.0
- // Deallocate string function
- if( stringFunction )
- {
- stringFunction->Release();
- stringFunction = 0;
- }
- #endif
-
- return 0;
- }
- #ifdef AS_DEPRECATED
- // Deprecated since 2009-12-08, 2.18.0
- int asCContext::SetExecuteStringFunction(asCScriptFunction *func)
- {
- if( stringFunction )
- stringFunction->Release();
- // The new function already has the refCount set to 1
- stringFunction = func;
- return 0;
- }
- #endif
- asBYTE asCContext::GetReturnByte()
- {
- if( status != asEXECUTION_FINISHED ) return 0;
- asCDataType *dt = &initialFunction->returnType;
- if( dt->IsObject() || dt->IsReference() ) return 0;
- return *(asBYTE*)®s.valueRegister;
- }
- asWORD asCContext::GetReturnWord()
- {
- if( status != asEXECUTION_FINISHED ) return 0;
- asCDataType *dt = &initialFunction->returnType;
- if( dt->IsObject() || dt->IsReference() ) return 0;
- return *(asWORD*)®s.valueRegister;
- }
- asDWORD asCContext::GetReturnDWord()
- {
- if( status != asEXECUTION_FINISHED ) return 0;
- asCDataType *dt = &initialFunction->returnType;
- if( dt->IsObject() || dt->IsReference() ) return 0;
- return *(asDWORD*)®s.valueRegister;
- }
- asQWORD asCContext::GetReturnQWord()
- {
- if( status != asEXECUTION_FINISHED ) return 0;
- asCDataType *dt = &initialFunction->returnType;
- if( dt->IsObject() || dt->IsReference() ) return 0;
- return regs.valueRegister;
- }
- float asCContext::GetReturnFloat()
- {
- if( status != asEXECUTION_FINISHED ) return 0;
- asCDataType *dt = &initialFunction->returnType;
- if( dt->IsObject() || dt->IsReference() ) return 0;
- return *(float*)®s.valueRegister;
- }
- double asCContext::GetReturnDouble()
- {
- if( status != asEXECUTION_FINISHED ) return 0;
- asCDataType *dt = &initialFunction->returnType;
- if( dt->IsObject() || dt->IsReference() ) return 0;
- return *(double*)®s.valueRegister;
- }
- void *asCContext::GetReturnAddress()
- {
- if( status != asEXECUTION_FINISHED ) return 0;
- asCDataType *dt = &initialFunction->returnType;
- if( dt->IsReference() )
- return *(void**)®s.valueRegister;
- else if( dt->IsObject() )
- return regs.objectRegister;
- return 0;
- }
- void *asCContext::GetReturnObject()
- {
- if( status != asEXECUTION_FINISHED ) return 0;
- asCDataType *dt = &initialFunction->returnType;
- if( !dt->IsObject() ) return 0;
- if( dt->IsReference() )
- return *(void**)(size_t)regs.valueRegister;
- else
- return regs.objectRegister;
- }
- void *asCContext::GetAddressOfReturnValue()
- {
- if( status != asEXECUTION_FINISHED ) return 0;
- asCDataType *dt = &initialFunction->returnType;
- // An object is stored in the objectRegister
- if( !dt->IsReference() && dt->IsObject() )
- {
- // Need to dereference objects
- if( !dt->IsObjectHandle() )
- return *(void**)®s.objectRegister;
- return ®s.objectRegister;
- }
- // Primitives and references are stored in valueRegister
- return ®s.valueRegister;
- }
- int asCContext::SetObject(void *obj)
- {
- if( status != asEXECUTION_PREPARED )
- return asCONTEXT_NOT_PREPARED;
- if( !initialFunction->objectType )
- {
- status = asEXECUTION_ERROR;
- return asERROR;
- }
- *(size_t*)®s.stackFramePointer[0] = (size_t)obj;
- return 0;
- }
- int asCContext::SetArgByte(asUINT arg, asBYTE value)
- {
- if( status != asEXECUTION_PREPARED )
- return asCONTEXT_NOT_PREPARED;
- if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_ARG;
- }
- // Verify the type of the argument
- asCDataType *dt = &initialFunction->parameterTypes[arg];
- if( dt->IsObject() || dt->IsReference() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_TYPE;
- }
- if( dt->GetSizeInMemoryBytes() != 1 )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_TYPE;
- }
- // Determine the position of the argument
- int offset = 0;
- if( initialFunction->objectType )
- offset += AS_PTR_SIZE;
- for( asUINT n = 0; n < arg; n++ )
- offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
- // Set the value
- *(asBYTE*)®s.stackFramePointer[offset] = value;
- return 0;
- }
- int asCContext::SetArgWord(asUINT arg, asWORD value)
- {
- if( status != asEXECUTION_PREPARED )
- return asCONTEXT_NOT_PREPARED;
- if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_ARG;
- }
- // Verify the type of the argument
- asCDataType *dt = &initialFunction->parameterTypes[arg];
- if( dt->IsObject() || dt->IsReference() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_TYPE;
- }
- if( dt->GetSizeInMemoryBytes() != 2 )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_TYPE;
- }
- // Determine the position of the argument
- int offset = 0;
- if( initialFunction->objectType )
- offset += AS_PTR_SIZE;
- for( asUINT n = 0; n < arg; n++ )
- offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
- // Set the value
- *(asWORD*)®s.stackFramePointer[offset] = value;
- return 0;
- }
- int asCContext::SetArgDWord(asUINT arg, asDWORD value)
- {
- if( status != asEXECUTION_PREPARED )
- return asCONTEXT_NOT_PREPARED;
- if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_ARG;
- }
- // Verify the type of the argument
- asCDataType *dt = &initialFunction->parameterTypes[arg];
- if( dt->IsObject() || dt->IsReference() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_TYPE;
- }
- if( dt->GetSizeInMemoryBytes() != 4 )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_TYPE;
- }
- // Determine the position of the argument
- int offset = 0;
- if( initialFunction->objectType )
- offset += AS_PTR_SIZE;
- for( asUINT n = 0; n < arg; n++ )
- offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
- // Set the value
- *(asDWORD*)®s.stackFramePointer[offset] = value;
- return 0;
- }
- int asCContext::SetArgQWord(asUINT arg, asQWORD value)
- {
- if( status != asEXECUTION_PREPARED )
- return asCONTEXT_NOT_PREPARED;
- if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_ARG;
- }
- // Verify the type of the argument
- asCDataType *dt = &initialFunction->parameterTypes[arg];
- if( dt->IsObject() || dt->IsReference() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_TYPE;
- }
- if( dt->GetSizeOnStackDWords() != 2 )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_TYPE;
- }
- // Determine the position of the argument
- int offset = 0;
- if( initialFunction->objectType )
- offset += AS_PTR_SIZE;
- for( asUINT n = 0; n < arg; n++ )
- offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
- // Set the value
- *(asQWORD*)(®s.stackFramePointer[offset]) = value;
- return 0;
- }
- int asCContext::SetArgFloat(asUINT arg, float value)
- {
- if( status != asEXECUTION_PREPARED )
- return asCONTEXT_NOT_PREPARED;
- if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_ARG;
- }
- // Verify the type of the argument
- asCDataType *dt = &initialFunction->parameterTypes[arg];
- if( dt->IsObject() || dt->IsReference() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_TYPE;
- }
- if( dt->GetSizeOnStackDWords() != 1 )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_TYPE;
- }
- // Determine the position of the argument
- int offset = 0;
- if( initialFunction->objectType )
- offset += AS_PTR_SIZE;
- for( asUINT n = 0; n < arg; n++ )
- offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
- // Set the value
- *(float*)(®s.stackFramePointer[offset]) = value;
- return 0;
- }
- int asCContext::SetArgDouble(asUINT arg, double value)
- {
- if( status != asEXECUTION_PREPARED )
- return asCONTEXT_NOT_PREPARED;
- if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_ARG;
- }
- // Verify the type of the argument
- asCDataType *dt = &initialFunction->parameterTypes[arg];
- if( dt->IsObject() || dt->IsReference() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_TYPE;
- }
- if( dt->GetSizeOnStackDWords() != 2 )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_TYPE;
- }
- // Determine the position of the argument
- int offset = 0;
- if( initialFunction->objectType )
- offset += AS_PTR_SIZE;
- for( asUINT n = 0; n < arg; n++ )
- offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
- // Set the value
- *(double*)(®s.stackFramePointer[offset]) = value;
- return 0;
- }
- int asCContext::SetArgAddress(asUINT arg, void *value)
- {
- if( status != asEXECUTION_PREPARED )
- return asCONTEXT_NOT_PREPARED;
- if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_ARG;
- }
- // Verify the type of the argument
- asCDataType *dt = &initialFunction->parameterTypes[arg];
- if( !dt->IsReference() && !dt->IsObjectHandle() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_TYPE;
- }
- // Determine the position of the argument
- int offset = 0;
- if( initialFunction->objectType )
- offset += AS_PTR_SIZE;
- for( asUINT n = 0; n < arg; n++ )
- offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
- // Set the value
- *(size_t*)(®s.stackFramePointer[offset]) = (size_t)value;
- return 0;
- }
- int asCContext::SetArgObject(asUINT arg, void *obj)
- {
- if( status != asEXECUTION_PREPARED )
- return asCONTEXT_NOT_PREPARED;
- if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_ARG;
- }
- // Verify the type of the argument
- asCDataType *dt = &initialFunction->parameterTypes[arg];
- if( !dt->IsObject() )
- {
- status = asEXECUTION_ERROR;
- return asINVALID_TYPE;
- }
- // If the object should be sent by value we must make a copy of it
- if( !dt->IsReference() )
- {
- if( dt->IsObjectHandle() )
- {
- // Increase the reference counter
- asSTypeBehaviour *beh = &dt->GetObjectType()->beh;
- if( obj && beh->addref )
- engine->CallObjectMethod(obj, beh->addref);
- }
- else
- {
- obj = engine->CreateScriptObjectCopy(obj, engine->GetTypeIdFromDataType(*dt));
- }
- }
- // Determine the position of the argument
- int offset = 0;
- if( initialFunction->objectType )
- offset += AS_PTR_SIZE;
- for( asUINT n = 0; n < arg; n++ )
- offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
- // Set the value
- *(size_t*)(®s.stackFramePointer[offset]) = (size_t)obj;
- return 0;
- }
- // TODO: Instead of GetAddressOfArg, maybe we need a SetArgValue(int arg, void *value, bool takeOwnership) instead.
- // interface
- void *asCContext::GetAddressOfArg(asUINT arg)
- {
- if( status != asEXECUTION_PREPARED )
- return 0;
- if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
- return 0;
- // Determine the position of the argument
- int offset = 0;
- if( initialFunction->objectType )
- offset += AS_PTR_SIZE;
- for( asUINT n = 0; n < arg; n++ )
- offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
- // We should return the address of the location where the argument value will be placed
- // All registered types are always sent by reference, even if
- // the function is declared to receive the argument by value.
- return ®s.stackFramePointer[offset];
- }
- int asCContext::Abort()
- {
- // TODO: multithread: Make thread safe
- if( engine == 0 ) return asERROR;
- if( status == asEXECUTION_SUSPENDED )
- status = asEXECUTION_ABORTED;
- doSuspend = true;
- regs.doProcessSuspend = true;
- externalSuspendRequest = true;
- doAbort = true;
- return 0;
- }
- // interface
- int asCContext::Suspend()
- {
- // This function just sets some internal flags and is safe
- // to call from a secondary thread, even if the library has
- // been built without multi-thread support.
- if( engine == 0 ) return asERROR;
- doSuspend = true;
- externalSuspendRequest = true;
- regs.doProcessSuspend = true;
- return 0;
- }
- // interface
- int asCContext::Execute()
- {
- asASSERT( engine != 0 );
- if( status != asEXECUTION_SUSPENDED && status != asEXECUTION_PREPARED )
- return asERROR;
- status = asEXECUTION_ACTIVE;
- asPushActiveContext((asIScriptContext *)this);
- if( regs.programPointer == 0 )
- {
- if( currentFunction->funcType == asFUNC_VIRTUAL ||
- currentFunction->funcType == asFUNC_INTERFACE )
- {
- // The currentFunction is a virtual method
- // Determine the true function from the object
- asCScriptObject *obj = *(asCScriptObject**)(size_t*)regs.stackFramePointer;
- if( obj == 0 )
- {
- SetInternalException(TXT_NULL_POINTER_ACCESS);
- }
- else
- {
- asCObjectType *objType = obj->objType;
- asCScriptFunction *realFunc = 0;
- if( currentFunction->funcType == asFUNC_VIRTUAL )
- {
- if( objType->virtualFunctionTable.GetLength() > (asUINT)currentFunction->vfTableIdx )
- {
- realFunc = objType->virtualFunctionTable[currentFunction->vfTableIdx];
- }
- }
- else
- {
- // Search the object type for a function that matches the interface function
- for( asUINT n = 0; n < objType->methods.GetLength(); n++ )
- {
- asCScriptFunction *f2 = engine->scriptFunctions[objType->methods[n]];
- if( f2->signatureId == currentFunction->signatureId )
- {
- if( f2->funcType == asFUNC_VIRTUAL )
- realFunc = objType->virtualFunctionTable[f2->vfTableIdx];
- else
- realFunc = f2;
- break;
- }
- }
- }
- if( realFunc )
- {
- if( realFunc->signatureId != currentFunction->signatureId )
- {
- SetInternalException(TXT_NULL_POINTER_ACCESS);
- }
- else
- {
- currentFunction = realFunc;
- regs.programPointer = currentFunction->byteCode.AddressOf();
- regs.globalVarPointers = currentFunction->globalVarPointers.AddressOf();
- // Set the local objects to 0
- for( asUINT n = 0; n < currentFunction->objVariablePos.GetLength(); n++ )
- {
- int pos = currentFunction->objVariablePos[n];
- *(size_t*)®s.stackFramePointer[-pos] = 0;
- }
- }
- }
- }
- }
- else if( currentFunction->funcType == asFUNC_SYSTEM )
- {
- // The current function is an application registered function
- // Call the function directly
- CallSystemFunction(currentFunction->id, this, 0);
-
- // Was the call successful?
- if( status == asEXECUTION_ACTIVE )
- {
- status = asEXECUTION_FINISHED;
- }
- }
- else
- {
- // This shouldn't happen
- asASSERT(false);
- }
- }
- while( status == asEXECUTION_ACTIVE )
- ExecuteNext();
- doSuspend = false;
- regs.doProcessSuspend = lineCallback;
- asPopActiveContext((asIScriptContext *)this);
- #ifdef AS_DEBUG
- /*
- // Output instruction statistics
- // This is useful for determining what needs to be optimized.
- _mkdir("AS_DEBUG");
- FILE *f = fopen("AS_DEBUG/stats.txt", "at");
- fprintf(f, "\n");
- asQWORD total = 0;
- int n;
- for( n = 0; n < 256; n++ )
- {
- if( bcName[n].name && instrCount[n] )
- fprintf(f, "%-10.10s : %d\n", bcName[n].name, instrCount[n]);
- total += instrCount[n];
- }
- fprintf(f, "\ntotal : %I64d\n", total);
- fprintf(f, "\n");
- for( n = 0; n < 256; n++ )
- {
- if( bcName[n].name )
- {
- for( int m = 0; m < 256; m++ )
- {
- if( instrCount2[n][m] )
- fprintf(f, "%-10.10s, %-10.10s : %d\n", bcName[n].name, bcName[m].name, instrCount2[n][m]);
- }
- }
- }
- fclose(f);
- */
- #endif
- if( status == asEXECUTION_FINISHED )
- {
- regs.objectType = initialFunction->returnType.GetObjectType();
- return asEXECUTION_FINISHED;
- }
- if( status == asEXECUTION_SUSPENDED )
- return asEXECUTION_SUSPENDED;
- if( doAbort )
- {
- doAbort = false;
- status = asEXECUTION_ABORTED;
- return asEXECUTION_ABORTED;
- }
- if( status == asEXECUTION_EXCEPTION )
- return asEXECUTION_EXCEPTION;
- return asERROR;
- }
- void asCContext::PushCallState()
- {
- callStack.SetLength(callStack.GetLength() + CALLSTACK_FRAME_SIZE);
- // Separating the loads and stores limits data cache trash, and with a smart compiler
- // could turn into SIMD style loading/storing if available.
- // The compiler can't do this itself due to potential pointer aliasing between the pointers,
- // ie writing to tmp could overwrite the data contained in registers.stackFramePointer for example
- // for all the compiler knows. So introducing the local variable s, which is never referred to by
- // its address we avoid this issue.
- size_t s[5];
- s[0] = (size_t)regs.stackFramePointer;
- s[1] = (size_t)currentFunction;
- s[2] = (size_t)regs.programPointer;
- s[3] = (size_t)regs.stackPointer;
- s[4] = stackIndex;
- size_t *tmp = callStack.AddressOf() + callStack.GetLength() - CALLSTACK_FRAME_SIZE;
- tmp[0] = s[0];
- tmp[1] = s[1];
- tmp[2] = s[2];
- tmp[3] = s[3];
- tmp[4] = s[4];
- }
- void asCContext::PopCallState()
- {
- // See comments in PushCallState about pointer aliasing and data cache trashing
- size_t *tmp = callStack.AddressOf() + callStack.GetLength() - CALLSTACK_FRAME_SIZE;
- size_t s[5];
- s[0] = tmp[0];
- s[1] = tmp[1];
- s[2] = tmp[2];
- s[3] = tmp[3];
- s[4] = tmp[4];
- regs.stackFramePointer = (asDWORD*)s[0];
- currentFunction = (asCScriptFunction*)s[1];
- regs.programPointer = (asDWORD*)s[2];
- regs.stackPointer = (asDWORD*)s[3];
- stackIndex = (int)s[4];
- regs.globalVarPointers = currentFunction->globalVarPointers.AddressOf();
- callStack.SetLength(callStack.GetLength() - CALLSTACK_FRAME_SIZE);
- }
- int asCContext::GetCallstackSize()
- {
- return (int)callStack.GetLength() / CALLSTACK_FRAME_SIZE;
- }
- int asCContext::GetCallstackFunction(int index)
- {
- if( index < 0 || index >= GetCallstackSize() ) return asINVALID_ARG;
- size_t *s = callStack.AddressOf() + index*CALLSTACK_FRAME_SIZE;
- asCScriptFunction *func = (asCScriptFunction*)s[1];
- return func->id;
- }
- int asCContext::GetCallstackLineNumber(int index, int *column)
- {
- if( index < 0 || index >= GetCallstackSize() ) return asINVALID_ARG;
- size_t *s = callStack.AddressOf() + index*CALLSTACK_FRAME_SIZE;
- asCScriptFunction *func = (asCScriptFunction*)s[1];
- asDWORD *bytePos = (asDWORD*)s[2];
- asDWORD line = func->GetLineNumber(int(bytePos - func->byteCode.AddressOf()));
- if( column ) *column = (line >> 20);
- return (line & 0xFFFFF);
- }
- void asCContext::CallScriptFunction(asCScriptFunction *func)
- {
- // Push the framepointer, function id and programCounter on the stack
- PushCallState();
- currentFunction = func;
- regs.globalVarPointers = currentFunction->globalVarPointers.AddressOf();
- regs.programPointer = currentFunction->byteCode.AddressOf();
- // Verify if there is enough room in the stack block. Allocate new block if not
- if( regs.stackPointer - (func->stackNeeded + RESERVE_STACK) < stackBlocks[stackIndex] )
- {
- asDWORD *oldStackPointer = regs.stackPointer;
- // The size of each stack block is determined by the following formula:
- // size = stackBlockSize << index
- while( regs.stackPointer - (func->stackNeeded + RESERVE_STACK) < stackBlocks[stackIndex] )
- {
- // Make sure we don't allocate more space than allowed
- if( engine->ep.maximumContextStackSize )
- {
- // This test will only stop growth once it has already crossed the limit
- if( stackBlockSize * ((1 << (stackIndex+1)) - 1) > engine->ep.maximumContextStackSize )
- {
- isStackMemoryNotAllocated = true;
- // Set the stackFramePointer, even though the stackPointer wasn't updated
- regs.stackFramePointer = regs.stackPointer;
- // TODO: Make sure the exception handler doesn't try to free objects that have not been initialized
- SetInternalException(TXT_STACK_OVERFLOW);
- return;
- }
- }
- stackIndex++;
- if( (int)stackBlocks.GetLength() == stackIndex )
- {
- asDWORD *stack = asNEWARRAY(asDWORD,(stackBlockSize << stackIndex));
- stackBlocks.PushLast(stack);
- }
- regs.stackPointer = stackBlocks[stackIndex] + (stackBlockSize<<stackIndex) - func->GetSpaceNeededForArguments();
- }
- // Copy the function arguments to the new stack space
- memcpy(regs.stackPointer, oldStackPointer, sizeof(asDWORD)*func->GetSpaceNeededForArguments());
- }
- // Update framepointer and programCounter
- regs.stackFramePointer = regs.stackPointer;
- // Set all object variables to 0
- for( asUINT n = 0; n < currentFunction->objVariablePos.GetLength(); n++ )
- {
- int pos = currentFunction->objVariablePos[n];
- *(size_t*)®s.stackFramePointer[-pos] = 0;
- }
- }
- void asCContext::CallInterfaceMethod(asCScriptFunction *func)
- {
- // Resolve the interface method using the current script type
- asCScriptObject *obj = *(asCScriptObject**)(size_t*)regs.stackPointer;
- if( obj == 0 )
- {
- SetInternalException(TXT_NULL_POINTER_ACCESS);
- return;
- }
- asCObjectType *objType = obj->objType;
- // TODO: optimize: The object type should have a list of only those methods that
- // implement interface methods. This list should be ordered by
- // the signatureId so that a binary search can be made, instead
- // of a linear search.
- //
- // When this is done, we must also make sure the signatureId of a
- // function never changes, e.g. when if the signature functions are
- // released.
- // Search the object type for a function that matches the interface function
- asCScriptFunction *realFunc = 0;
- if( func->funcType == asFUNC_INTERFACE )
- {
- for( asUINT n = 0; n < objType->methods.GetLength(); n++ )
- {
- asCScriptFunction *f2 = engine->scriptFunctions[objType->methods[n]];
- if( f2->signatureId == func->signatureId )
- {
- if( f2->funcType == asFUNC_VIRTUAL )
- realFunc = objType->virtualFunctionTable[f2->vfTableIdx];
- else
- realFunc = f2;
- break;
- }
- }
- if( realFunc == 0 )
- {
- SetInternalException(TXT_NULL_POINTER_ACCESS);
- return;
- }
- }
- else /* if( func->funcType == asFUNC_VIRTUAL ) */
- {
- realFunc = objType->virtualFunctionTable[func->vfTableIdx];
- }
- // Then call the true script function
- CallScriptFunction(realFunc);
- }
- void asCContext::ExecuteNext()
- {
- asDWORD *l_bc = regs.programPointer;
- asDWORD *l_sp = regs.stackPointer;
- asDWORD *l_fp = regs.stackFramePointer;
- for(;;)
- {
- #ifdef AS_DEBUG
- ++stats.instrCount[*(asBYTE*)l_bc];
- ++instrCount[*(asBYTE*)l_bc];
- ++instrCount2[lastBC][*(asBYTE*)l_bc];
- lastBC = *(asBYTE*)l_bc;
- // Used to verify that the size of the instructions are correct
- asDWORD *old = l_bc;
- #endif
- // Remember to keep the cases in order and without
- // gaps, because that will make the switch faster.
- // It will be faster since only one lookup will be
- // made to find the correct jump destination. If not
- // in order, the switch will make two lookups.
- switch( *(asBYTE*)l_bc )
- {
- //--------------
- // memory access functions
- // Decrease the stack pointer with n dwords (stack grows downward)
- case asBC_POP:
- l_sp += asBC_WORDARG0(l_bc);
- l_bc++;
- break;
- // Increase the stack pointer with n dwords
- case asBC_PUSH:
- l_sp -= asBC_WORDARG0(l_bc);
- l_bc++;
- break;
- // Push a dword value on the stack
- case asBC_PshC4:
- --l_sp;
- *l_sp = asBC_DWORDARG(l_bc);
- l_bc += 2;
- break;
- // Push the dword value of a variable on the stack
- case asBC_PshV4:
- --l_sp;
- *l_sp = *(l_fp - asBC_SWORDARG0(l_bc));
- l_bc++;
- break;
- // Push the address of a variable on the stack
- case asBC_PSF:
- l_sp -= AS_PTR_SIZE;
- *(asPTRWORD*)l_sp = (asPTRWORD)size_t(l_fp - asBC_SWORDARG0(l_bc));
- l_bc++;
- break;
- // Swap the top 2 dwords on the stack
- case asBC_SWAP4:
- {
- asDWORD d = (asDWORD)*l_sp;
- *l_sp = *(l_sp+1);
- *(asDWORD*)(l_sp+1) = d;
- l_bc++;
- }
- break;
- // Do a boolean not operation, modifying the value of the variable
- case asBC_NOT:
- #if AS_SIZEOF_BOOL == 1
- {
- // Set the value to true if it is equal to 0
- // We need to use volatile here to tell the compiler it cannot
- // change the order of read and write operations on the pointer.
- volatile asBYTE *ptr = (asBYTE*)(l_fp - asBC_SWORDARG0(l_bc));
- asBYTE val = (ptr[0] == 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
- ptr[0] = val; // The result is stored in the lower byte
- ptr[1] = 0; // Make sure the rest of the DWORD is 0
- ptr[2] = 0;
- ptr[3] = 0;
- }
- #else
- *(l_fp - asBC_SWORDARG0(l_bc)) = (*(l_fp - asBC_SWORDARG0(l_bc)) == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
- #endif
- l_bc++;
- break;
- // Push the dword value of a global variable on the stack
- case asBC_PshG4:
- --l_sp;
- // TODO: global: The global var address should be stored in the instruction directly
- *l_sp = *(asDWORD*)regs.globalVarPointers[asBC_WORDARG0(l_bc)];
- l_bc++;
- break;
- // Load the address of a global variable in the register, then
- // copy the value of the global variable into a local variable
- case asBC_LdGRdR4:
- // TODO: global: The global var address should be stored in the instruction directly
- *(void**)®s.valueRegister = regs.globalVarPointers[asBC_WORDARG1(l_bc)];
- *(l_fp - asBC_SWORDARG0(l_bc)) = **(asDWORD**)®s.valueRegister;
- l_bc += 2;
- break;
- //----------------
- // path control instructions
- // Begin execution of a script function
- case asBC_CALL:
- {
- int i = asBC_INTARG(l_bc);
- l_bc += 2;
- asASSERT( i >= 0 );
- asASSERT( (i & FUNC_IMPORTED) == 0 );
- // Need to move the values back to the context
- regs.programPointer = l_bc;
- regs.stackPointer = l_sp;
- regs.stackFramePointer = l_fp;
- CallScriptFunction(engine->scriptFunctions[i]);
- // Extract the values from the context again
- l_bc = regs.programPointer;
- l_sp = regs.stackPointer;
- l_fp = regs.stackFramePointer;
- // If status isn't active anymore then we must stop
- if( status != asEXECUTION_ACTIVE )
- return;
- }
- break;
- // Return to the caller, and remove the arguments from the stack
- case asBC_RET:
- {
- if( callStack.GetLength() == 0 )
- {
- status = asEXECUTION_FINISHED;
- return;
- }
- asWORD w = asBC_WORDARG0(l_bc);
- // Read the old framepointer, functionid, and programCounter from the call stack
- PopCallState();
- // Extract the values from the context again
- l_bc = regs.programPointer;
- l_sp = regs.stackPointer;
- l_fp = regs.stackFramePointer;
- // Pop arguments from stack
- l_sp += w;
- }
- break;
- // Jump to a relative position
- case asBC_JMP:
- l_bc += 2 + asBC_INTARG(l_bc);
- break;
- //----------------
- // Conditional jumps
- // Jump to a relative position if the value in the register is 0
- case asBC_JZ:
- if( *(int*)®s.valueRegister == 0 )
- l_bc += asBC_INTARG(l_bc) + 2;
- else
- l_bc += 2;
- break;
- // Jump to a relative position if the value in the register is not 0
- case asBC_JNZ:
- if( *(int*)®s.valueRegister != 0 )
- l_bc += asBC_INTARG(l_bc) + 2;
- else
- l_bc += 2;
- break;
- // Jump to a relative position if the value in the register is negative
- case asBC_JS:
- if( *(int*)®s.valueRegister < 0 )
- l_bc += asBC_INTARG(l_bc) + 2;
- else
- l_bc += 2;
- break;
- // Jump to a relative position if the value in the register it not negative
- case asBC_JNS:
- if( *(int*)®s.valueRegister >= 0 )
- l_bc += asBC_INTARG(l_bc) + 2;
- else
- l_bc += 2;
- break;
- // Jump to a relative position if the value in the register is greater than 0
- case asBC_JP:
- if( *(int*)®s.valueRegister > 0 )
- l_bc += asBC_INTARG(l_bc) + 2;
- else
- l_bc += 2;
- break;
- // Jump to a relative position if the value in the register is not greater than 0
- case asBC_JNP:
- if( *(int*)®s.valueRegister <= 0 )
- l_bc += asBC_INTARG(l_bc) + 2;
- else
- l_bc += 2;
- break;
- //--------------------
- // test instructions
- // If the value in the register is 0, then set the register to 1, else to 0
- case asBC_TZ:
- #if AS_SIZEOF_BOOL == 1
- {
- // Set the value to true if it is equal to 0
- // We need to use volatile here to tell the compiler it cannot
- // change the order of read and write operations on valueRegister.
- volatile int *regPtr = (int*)®s.valueRegister;
- volatile asBYTE *regBptr = (asBYTE*)®s.valueRegister;
- asBYTE val = (regPtr[0] == 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
- regBptr[0] = val; // The result is stored in the lower byte
- regBptr[1] = 0; // Make sure the rest of the register is 0
- regBptr[2] = 0;
- regBptr[3] = 0;
- regBptr[4] = 0;
- regBptr[5] = 0;
- regBptr[6] = 0;
- regBptr[7] = 0;
- }
- #else
- *(int*)®s.valueRegister = (*(int*)®s.valueRegister == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
- #endif
- l_bc++;
- break;
- // If the value in the register is not 0, then set the register to 1, else to 0
- case asBC_TNZ:
- #if AS_SIZEOF_BOOL == 1
- {
- // Set the value to true if it is not equal to 0
- // We need to use volatile here to tell the compiler it cannot
- // change the order of read and write operations on valueRegister.
- volatile int *regPtr = (int*)®s.valueRegister;
- volatile asBYTE *regBptr = (asBYTE*)®s.valueRegister;
- asBYTE val = (regPtr[0] == 0) ? 0 : VALUE_OF_BOOLEAN_TRUE;
- regBptr[0] = val; // The result is stored in the lower byte
- regBptr[1] = 0; // Make sure the rest of the register is 0
- regBptr[2] = 0;
- regBptr[3] = 0;
- regBptr[4] = 0;
- regBptr[5] = 0;
- regBptr[6] = 0;
- regBptr[7] = 0;
- }
- #else
- *(int*)®s.valueRegister = (*(int*)®s.valueRegister == 0 ? 0 : VALUE_OF_BOOLEAN_TRUE);
- #endif
- l_bc++;
- break;
- // If the value in the register is negative, then set the register to 1, else to 0
- case asBC_TS:
- #if AS_SIZEOF_BOOL == 1
- {
- // Set the value to true if it is less than 0
- // We need to use volatile here to tell the compiler it cannot
- // change the order of read and write operations on valueRegister.
- volatile int *regPtr = (int*)®s.valueRegister;
- volatile asBYTE *regBptr = (asBYTE*)®s.valueRegister;
- asBYTE val = (regPtr[0] < 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
- regBptr[0] = val; // The result is stored in the lower byte
- regBptr[1] = 0; // Make sure the rest of the register is 0
- regBptr[2] = 0;
- regBptr[3] = 0;
- regBptr[4] = 0;
- regBptr[5] = 0;
- regBptr[6] = 0;
- regBptr[7] = 0;
- }
- #else
- *(int*)®s.valueRegister = (*(int*)®s.valueRegister < 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
- #endif
- l_bc++;
- break;
- // If the value in the register is not negative, then set the register to 1, else to 0
- case asBC_TNS:
- #if AS_SIZEOF_BOOL == 1
- {
- // Set the value to true if it is not less than 0
- // We need to use volatile here to tell the compiler it cannot
- // change the order of read and write operations on valueRegister.
- volatile int *regPtr = (int*)®s.valueRegister;
- volatile asBYTE *regBptr = (asBYTE*)®s.valueRegister;
- asBYTE val = (regPtr[0] >= 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
- regBptr[0] = val; // The result is stored in the lower byte
- regBptr[1] = 0; // Make sure the rest of the register is 0
- regBptr[2] = 0;
- regBptr[3] = 0;
- regBptr[4] = 0;
- regBptr[5] = 0;
- regBptr[6] = 0;
- regBptr[7] = 0;
- }
- #else
- *(int*)®s.valueRegister = (*(int*)®s.valueRegister < 0 ? 0 : VALUE_OF_BOOLEAN_TRUE);
- #endif
- l_bc++;
- break;
- // If the value in the register is greater than 0, then set the register to 1, else to 0
- case asBC_TP:
- #if AS_SIZEOF_BOOL == 1
- {
- // Set the value to true if it is greater than 0
- // We need to use volatile here to tell the compiler it cannot
- // change the order of read and write operations on valueRegister.
- volatile int *regPtr = (int*)®s.valueRegister;
- volatile asBYTE *regBptr = (asBYTE*)®s.valueRegister;
- asBYTE val = (regPtr[0] > 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
- regBptr[0] = val; // The result is stored in the lower byte
- regBptr[1] = 0; // Make sure the rest of the register is 0
- regBptr[2] = 0;
- regBptr[3] = 0;
- regBptr[4] = 0;
- regBptr[5] = 0;
- regBptr[6] = 0;
- regBptr[7] = 0;
- }
- #else
- *(int*)®s.valueRegister = (*(int*)®s.valueRegister > 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
- #endif
- l_bc++;
- break;
- // If the value in the register is not greater than 0, then set the register to 1, else to 0
- case asBC_TNP:
- #if AS_SIZEOF_BOOL == 1
- {
- // Set the value to true if it is not greater than 0
- // We need to use volatile here to tell the compiler it cannot
- // change the order of read and write operations on valueRegister.
- volatile int *regPtr = (int*)®s.valueRegister;
- volatile asBYTE *regBptr = (asBYTE*)®s.valueRegister;
- asBYTE val = (regPtr[0] <= 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
- regBptr[0] = val; // The result is stored in the lower byte
- regBptr[1] = 0; // Make sure the rest of the register is 0
- regBptr[2] = 0;
- regBptr[3] = 0;
- regBptr[4] = 0;
- regBptr[5] = 0;
- regBptr[6] = 0;
- regBptr[7] = 0;
- }
- #else
- *(int*)®s.valueRegister = (*(int*)®s.valueRegister > 0 ? 0 : VALUE_OF_BOOLEAN_TRUE);
- #endif
- l_bc++;
- break;
- //--------------------
- // negate value
- // Negate the integer value in the variable
- case asBC_NEGi:
- *(l_fp - asBC_SWORDARG0(l_bc)) = asDWORD(-int(*(l_fp - asBC_SWORDARG0(l_bc))));
- l_bc++;
- break;
- // Negate the float value in the variable
- case asBC_NEGf:
- *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = -*(float*)(l_fp - asBC_SWORDARG0(l_bc));
- l_bc++;
- break;
- // Negate the double value in the variable
- case asBC_NEGd:
- *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = -*(double*)(l_fp - asBC_SWORDARG0(l_bc));
- l_bc++;
- break;
- //-------------------------
- // Increment value pointed to by address in register
- // Increment the short value pointed to by the register
- case asBC_INCi16:
- (**(short**)®s.valueRegister)++;
- l_bc++;
- break;
- // Increment the byte value pointed to by the register
- case asBC_INCi8:
- (**(char**)®s.valueRegister)++;
- l_bc++;
- break;
- // Decrement the short value pointed to by the register
- case asBC_DECi16:
- (**(short**)®s.valueRegister)--;
- l_bc++;
- break;
- // Decrement the byte value pointed to by the register
- case asBC_DECi8:
- (**(char**)®s.valueRegister)--;
- l_bc++;
- break;
- // Increment the integer value pointed to by the register
- case asBC_INCi:
- ++(**(int**)®s.valueRegister);
- l_bc++;
- break;
- // Decrement the integer value pointed to by the register
- case asBC_DECi:
- --(**(int**)®s.valueRegister);
- l_bc++;
- break;
- // Increment the float value pointed to by the register
- case asBC_INCf:
- ++(**(float**)®s.valueRegister);
- l_bc++;
- break;
- // Decrement the float value pointed to by the register
- case asBC_DECf:
- --(**(float**)®s.valueRegister);
- l_bc++;
- break;
- // Increment the double value pointed to by the register
- case asBC_INCd:
- ++(**(double**)®s.valueRegister);
- l_bc++;
- break;
- // Decrement the double value pointed to by the register
- case asBC_DECd:
- --(**(double**)®s.valueRegister);
- l_bc++;
- break;
- // Increment the local integer variable
- case asBC_IncVi:
- (*(int*)(l_fp - asBC_SWORDARG0(l_bc)))++;
- l_bc++;
- break;
- // Decrement the local integer variable
- case asBC_DecVi:
- (*(int*)(l_fp - asBC_SWORDARG0(l_bc)))--;
- l_bc++;
- break;
- //--------------------
- // bits instructions
- // Do a bitwise not on the value in the variable
- case asBC_BNOT:
- *(l_fp - asBC_SWORDARG0(l_bc)) = ~*(l_fp - asBC_SWORDARG0(l_bc));
- l_bc++;
- break;
- // Do a bitwise and of two variables and store the result in a third variable
- case asBC_BAND:
- *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) & *(l_fp - asBC_SWORDARG2(l_bc));
- l_bc += 2;
- break;
- // Do a bitwise or of two variables and store the result in a third variable
- case asBC_BOR:
- *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) | *(l_fp - asBC_SWORDARG2(l_bc));
- l_bc += 2;
- break;
- // Do a bitwise xor of two variables and store the result in a third variable
- case asBC_BXOR:
- *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) ^ *(l_fp - asBC_SWORDARG2(l_bc));
- l_bc += 2;
- break;
- // Do a logical shift left of two variables and store the result in a third variable
- case asBC_BSLL:
- *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) << *(l_fp - asBC_SWORDARG2(l_bc));
- l_bc += 2;
- break;
- // Do a logical shift right of two variables and store the result in a third variable
- case asBC_BSRL:
- *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) >> *(l_fp - asBC_SWORDARG2(l_bc));
- l_bc += 2;
- break;
- // Do an arithmetic shift right of two variables and store the result in a third variable
- case asBC_BSRA:
- *(l_fp - asBC_SWORDARG0(l_bc)) = int(*(l_fp - asBC_SWORDARG1(l_bc))) >> *(l_fp - asBC_SWORDARG2(l_bc));
- l_bc += 2;
- break;
- case asBC_COPY:
- {
- void *d = (void*)*(size_t*)l_sp; l_sp += AS_PTR_SIZE;
- void *s = (void*)*(size_t*)l_sp;
- if( s == 0 || d == 0 )
- {
- // Need to move the values back to the context
- regs.programPointer = l_bc;
- regs.stackPointer = l_sp;
- regs.stackFramePointer = l_fp;
- // Raise exception
- SetInternalException(TXT_NULL_POINTER_ACCESS);
- return;
- }
- memcpy(d, s, asBC_WORDARG0(l_bc)*4);
- // replace the pointer on the stack with the lvalue
- *(size_t**)l_sp = (size_t*)d;
- }
- l_bc++;
- break;
- case asBC_PshC8:
- l_sp -= 2;
- *(asQWORD*)l_sp = asBC_QWORDARG(l_bc);
- l_bc += 3;
- break;
- case asBC_RDS8:
- #ifndef AS_64BIT_PTR
- *(asQWORD*)(l_sp-1) = *(asQWORD*)*(size_t*)l_sp;
- --l_sp;
- #else
- *(asQWORD*)l_sp = *(asQWORD*)*(size_t*)l_sp;
- #endif
- l_bc++;
- break;
- case asBC_SWAP8:
- {
- asQWORD q = *(asQWORD*)l_sp;
- *(asQWORD*)l_sp = *(asQWORD*)(l_sp+2);
- *(asQWORD*)(l_sp+2) = q;
- l_bc++;
- }
- break;
- //----------------------------
- // Comparisons
- case asBC_CMPd:
- {
- double dbl = *(double*)(l_fp - asBC_SWORDARG0(l_bc)) - *(double*)(l_fp - asBC_SWORDARG1(l_bc));
- if( dbl == 0 ) *(int*)®s.valueRegister = 0;
- else if( dbl < 0 ) *(int*)®s.valueRegister = -1;
- else *(int*)®s.valueRegister = 1;
- l_bc += 2;
- }
- break;
- case asBC_CMPu:
- {
- asDWORD d = *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc));
- asDWORD d2 = *(asDWORD*)(l_fp - asBC_SWORDARG1(l_bc));
- if( d == d2 ) *(int*)®s.valueRegister = 0;
- else if( d < d2 ) *(int*)®s.valueRegister = -1;
- else *(int*)®s.valueRegister = 1;
- l_bc += 2;
- }
- break;
- case asBC_CMPf:
- {
- float f = *(float*)(l_fp - asBC_SWORDARG0(l_bc)) - *(float*)(l_fp - asBC_SWORDARG1(l_bc));
- if( f == 0 ) *(int*)®s.valueRegister = 0;
- else if( f < 0 ) *(int*)®s.valueRegister = -1;
- else *(int*)®s.valueRegister = 1;
- l_bc += 2;
- }
- break;
- case asBC_CMPi:
- {
- int i = *(int*)(l_fp - asBC_SWORDARG0(l_bc)) - *(int*)(l_fp - asBC_SWORDARG1(l_bc));
- if( i == 0 ) *(int*)®s.valueRegister = 0;
- else if( i < 0 ) *(int*)®s.valueRegister = -1;
- else *(int*)®s.valueRegister = 1;
- l_bc += 2;
- }
- break;
- //----------------------------
- // Comparisons with constant value
- case asBC_CMPIi:
- {
- int i = *(int*)(l_fp - asBC_SWORDARG0(l_bc)) - asBC_INTARG(l_bc);
- if( i == 0 ) *(int*)®s.valueRegister = 0;
- else if( i < 0 ) *(int*)®s.valueRegister = -1;
- else *(int*)®s.valueRegister = 1;
- l_bc += 2;
- }
- break;
- case asBC_CMPIf:
- {
- float f = *(float*)(l_fp - asBC_SWORDARG0(l_bc)) - asBC_FLOATARG(l_bc);
- if( f == 0 ) *(int*)®s.valueRegister = 0;
- else if( f < 0 ) *(int*)®s.valueRegister = -1;
- else *(int*)®s.valueRegister = 1;
- l_bc += 2;
- }
- break;
- case asBC_CMPIu:
- {
- asDWORD d1 = *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc));
- asDWORD d2 = asBC_DWORDARG(l_bc);
- if( d1 == d2 ) *(int*)®s.valueRegister = 0;
- else if( d1 < d2 ) *(int*)®s.valueRegister = -1;
- else *(int*)®s.valueRegister = 1;
- l_bc += 2;
- }
- break;
- case asBC_JMPP:
- l_bc += 1 + (*(int*)(l_fp - asBC_SWORDARG0(l_bc)))*2;
- break;
- case asBC_PopRPtr:
- *(asPTRWORD*)®s.valueRegister = *(asPTRWORD*)l_sp;
- l_sp += AS_PTR_SIZE;
- l_bc++;
- break;
- case asBC_PshRPtr:
- l_sp -= AS_PTR_SIZE;
- *(asPTRWORD*)l_sp = *(asPTRWORD*)®s.valueRegister;
- l_bc++;
- break;
- case asBC_STR:
- {
- // Get the string id from the argument
- asWORD w = asBC_WORDARG0(l_bc);
- // Push the string pointer on the stack
- const asCString &b = engine->GetConstantString(w);
- l_sp -= AS_PTR_SIZE;
- *(asPTRWORD*)l_sp = (asPTRWORD)(size_t)b.AddressOf();
- // Push the string length on the stack
- --l_sp;
- *l_sp = (asDWORD)b.GetLength();
- l_bc++;
- }
- break;
- case asBC_CALLSYS:
- {
- // Get function ID from the argument
- int i = asBC_INTARG(l_bc);
- // Need to move the values back to the context as the called functions
- // may use the debug interface to inspect the registers
- regs.programPointer = l_bc;
- regs.stackPointer = l_sp;
- regs.stackFramePointer = l_fp;
- l_sp += CallSystemFunction(i, this, 0);
- // Update the program position after the call so that line number is correct
- l_bc += 2;
- if( regs.doProcessSuspend )
- {
- // Should the execution be suspended?
- if( doSuspend )
- {
- regs.programPointer = l_bc;
- regs.stackPointer = l_sp;
- regs.stackFramePointer = l_fp;
- status = asEXECUTION_SUSPENDED;
- return;
- }
- // An exception might have been raised
- if( status != asEXECUTION_ACTIVE )
- {
- regs.programPointer = l_bc;
- regs.stackPointer = l_sp;
- regs.stackFramePointer = l_fp;
- return;
- }
- }
- }
- break;
- case asBC_CALLBND:
- {
- // Get the function ID from the stack
- int i = asBC_INTARG(l_bc);
- l_bc += 2;
- asASSERT( i >= 0 );
- asASSERT( i & FUNC_IMPORTED…