示例#1
0
void asCRestore::ReadFunctionSignature(asCScriptFunction *func)
{
	int i, count;
	asCDataType dt;
	int num;

	ReadString(&func->name);
	ReadDataType(&func->returnType);
	READ_NUM(count);
	func->parameterTypes.Allocate(count, 0);
	for( i = 0; i < count; ++i ) 
	{
		ReadDataType(&dt);
		func->parameterTypes.PushLast(dt);
	}

	READ_NUM(count);
	func->inOutFlags.Allocate(count, 0);
	for( i = 0; i < count; ++i )
	{
		READ_NUM(num);
		func->inOutFlags.PushLast(static_cast<asETypeModifiers>(num));
	}

	READ_NUM(func->funcType);

	func->objectType = ReadObjectType();

	READ_NUM(func->isReadOnly);
}
示例#2
0
void asCRestore::ReadFunction(asCScriptFunction* func) 
{
	int i, count;
	asCDataType dt;
	int num;

	ReadString(&func->name);

	ReadDataType(&func->returnType);

	READ_NUM(count);
	func->parameterTypes.Allocate(count, 0);
	for( i = 0; i < count; ++i ) 
	{
		ReadDataType(&dt);
		func->parameterTypes.PushLast(dt);
	}

	READ_NUM(func->id);
	
	READ_NUM(count);
	func->byteCode.Allocate(count, 0);
	ReadByteCode(func->byteCode.AddressOf(), count);
	func->byteCode.SetLength(count);

	READ_NUM(count);
	func->objVariablePos.Allocate(count, 0);
	func->objVariableTypes.Allocate(count, 0);
	for( i = 0; i < count; ++i )
	{
		func->objVariableTypes.PushLast(ReadObjectType());
		READ_NUM(num);
		func->objVariablePos.PushLast(num);
	}

	READ_NUM(func->stackNeeded);

	func->objectType = ReadObjectType();

	int length;
	READ_NUM(length);
	func->lineNumbers.SetLength(length);
	for( i = 0; i < length; ++i )
		READ_NUM(func->lineNumbers[i]);
}
void asCRestore::ReadGlobalProperty() 
{
	asCString name;
	asCDataType type;

	ReadString(&name);
	ReadDataType(&type);

	module->AllocateGlobalProperty(name.AddressOf(), type);
}
示例#4
0
void asCRestore::ReadDataType(asCDataType *dt) 
{
	bool b;
	READ_NUM(b);
	if( b ) 
	{
		bool isObjectHandle;
		READ_NUM(isObjectHandle);
		bool isReadOnly;
		READ_NUM(isReadOnly);
		bool isHandleToConst;
		READ_NUM(isHandleToConst);
		bool isReference;
		READ_NUM(isReference);

		asCDataType sub;
		ReadDataType(&sub);

		*dt = sub;
		dt->MakeArray(engine);
		if( isObjectHandle )
		{
			dt->MakeReadOnly(isHandleToConst);
			dt->MakeHandle(true);
		}
		dt->MakeReadOnly(isReadOnly);
		dt->MakeReference(isReference);
	}
	else
	{
		eTokenType tokenType;
		READ_NUM(tokenType);
		asCObjectType *objType = ReadObjectType();
		bool isObjectHandle;
		READ_NUM(isObjectHandle);
		bool isReadOnly;
		READ_NUM(isReadOnly);
		bool isHandleToConst;
		READ_NUM(isHandleToConst);
		bool isReference;
		READ_NUM(isReference);

		if( tokenType == ttIdentifier )
			*dt = asCDataType::CreateObject(objType, false);
		else
			*dt = asCDataType::CreatePrimitive(tokenType, false);
		if( isObjectHandle )
		{
			dt->MakeReadOnly(isHandleToConst);
			dt->MakeHandle(true);
		}
		dt->MakeReadOnly(isReadOnly);
		dt->MakeReference(isReference);
	}
}
void asCRestore::ReadUsedTypeIds()
{
	asUINT n;
	asUINT count;
	READ_NUM(count);
	usedTypeIds.SetLength(count);
	for( n = 0; n < count; n++ )
	{
		asCDataType dt;
		ReadDataType(&dt);
		usedTypeIds[n] = engine->GetTypeIdFromDataType(dt);
	}
}
void asCRestore::ReadUsedGlobalProps()
{
	int c;
	READ_NUM(c);

	usedGlobalProperties.SetLength(c);

	for( int n = 0; n < c; n++ )
	{
		asCString name;
		asCDataType type;
		char moduleProp;

		ReadString(&name);
		ReadDataType(&type);
		READ_NUM(moduleProp);

		// Find the real property
		void *prop = 0;
		if( moduleProp )
		{
			for( asUINT p = 0; p < module->scriptGlobals.GetLength(); p++ )
			{
				if( module->scriptGlobals[p]->name == name &&
					module->scriptGlobals[p]->type == type )
				{
					prop = module->scriptGlobals[p]->GetAddressOfValue();
					break;
				}
			}
		}
		else
		{
			for( asUINT p = 0; p < engine->registeredGlobalProps.GetLength(); p++ )
			{
				if( engine->registeredGlobalProps[p] &&
					engine->registeredGlobalProps[p]->name == name &&
					engine->registeredGlobalProps[p]->type == type )
				{
					prop = engine->registeredGlobalProps[p]->GetAddressOfValue();
					break;
				}
			}
		}

		// TODO: If the property isn't found, we must give an error
		asASSERT(prop);

		usedGlobalProperties[n] = prop;
	}
}
示例#7
0
void asCRestore::ReadUsedTypeIds()
{
	asUINT n;
	asUINT count;
	READ_NUM(count);
	usedTypeIds.SetLength(count);
	for( n = 0; n < count; n++ )
	{
		asCDataType dt;
		ReadDataType(&dt);
		usedTypeIds[n] = engine->GetTypeIdFromDataType(dt);
	}

	// Translate all the TYPEID bytecodes
	TranslateFunction(&module->initFunction);
	for( n = 0; n < module->scriptFunctions.GetLength(); n++ )
		TranslateFunction(module->scriptFunctions[n]);
}
示例#8
0
文件: tvm.c 项目: ltangt/tlbasic
/*=========================================================================
* FUNCTION:      tVM_Execute
* TYPE:          public interface
* OVERVIEW:      execute a basic function
* INTERFACE:
*   parameters:  
*   returns:     
*                the result of the basic function
*=======================================================================*/
int tVM_Execute()
{
	int       running = 1;
	u8        bytecode;
	u8        type;
	u8        ac_flag;
	s32       integer;
	s32       stackindex,index;
	tVMValue  value1,value2,value3;
	tVMValue  retValue;// = (tVMValue*)mem_alloc(sizeof(tVMValue));

	/* initialize the running Stack FP */
	setFP(FirstFP);

	/* seek to the entry function */
	setFI(0);
	
	/* initialize the code reader */
	tVM_InitializeCodeReader();

	/* execute the byte codes in loop  */
	while(running)
	{
		bytecode = ReadCode();
		switch(bytecode)
		{
		case C_NOP:
			break;
		case C_CONST:
			{
				ReadVMValue(&value1);
				PushVMValue(&value1);
				break;
			}
		case C_LOAD:
			{
				ac_flag  =ReadAccessFlag();
				if(ac_flag == ACCESS_FLAG_GLOBAL)
					stackindex = ReadIndex();
				else
					stackindex = ReadIndex() + getFP();
				LoadVMValue(stackindex,&value1);
				PushVMValue(&value1);
				break;
			}
		case C_STORE:
			{
				ac_flag  =ReadAccessFlag();
				if(ac_flag == ACCESS_FLAG_GLOBAL)
					stackindex = ReadIndex();
				else
					stackindex = ReadIndex() + getFP();
				PopVMValue(&value1); /* pop the source value */
				StoreVMValue(stackindex,&value1);
				break;
			}
		case C_HEAP_LOAD:
			{
				type = ReadDataType();

				PopVMValue(&value2); /* Pop Addr */
				PopVMValue(&value1); /* Pop Base */	
				tVMValue_HeapLoad(value1.value.ptr_val+value2.value.int_val,type,&value3); /* load the heap memory */
				PushVMValue(&value3); /* push the loaded value */
				break;
			}
		case C_HEAP_STORE:
			{
				ptr32 addr;
				type = ReadDataType();

				PopVMValue(&value3); /* Pop Addr */
				PopVMValue(&value2); /* Pop Base */
				PopVMValue(&value1); /* Pop Value */
				addr = (ptr32)(value2.value.ptr_val + value3.value.int_val);
				if(value1.type != type)
				{
					tVMValue_ConvertType(&value1,type);
				}
				tVMValue_HeapStore(addr,&value1);
				break;
			}
		case C_FORCE_LOAD:
			{
				ac_flag  =ReadAccessFlag();
				if(ac_flag == ACCESS_FLAG_GLOBAL)
					stackindex = ReadIndex();
				else
					stackindex = ReadIndex() + getFP();
				type = ReadDataType();

				ForceLoadVMValue(stackindex,type,&value1);
				PushVMValue(&value1);
				break;
			}
		case C_ALLOC:
			{
				PopVMValue(&value1);
				value2.type = PtrType;
				value2.value.ptr_val = (ptr32)mem_alloc(value1.value.int_val);
				memset(value2.value.ptr_val,0,value1.value.int_val);
				PushVMValue(&value2);
				break;
			}
		case C_ALLOC_ARRAY:
			{
				s32   i;
				s32  dimension; 
				s32* index_ranges;
				
				dimension = ReadInteger();
				if(dimension < 1)
					break;
				index_ranges = (s32*)mem_alloc(sizeof(s32)*dimension);
				for(i=0;i<dimension;i++)
				{
					PopVMValue(&value1);
					index_ranges[dimension-i-1] = value1.value.int_val;
				}
				value1.type = PtrType;
				value1.value.ptr_val = tVMValue_HeapAllocMultiArray(dimension,index_ranges,0);
				PushVMValue(&value1);
				
				mem_free(index_ranges);
				break;
			}
		case C_FREE:
			{
				PopVMValue(&value1);
				if(value1.value.ptr_val != NULL)
					mem_free(value1.value.ptr_val);
				break;
			}
		case C_FREE_ARRAY:
			{
				break;
			}
		case C_PUSH:
			{
				value1.type  = ReadDataType();
				value1.value.int_val = 0;
				PushVMValue(&value1);
				break;
			}
		case C_POP:
			{
				s32 i;
				integer = ReadInteger();
				for(i=0;i<integer;i++)
				{
					PopVMValue(&value1);
					tVMValue_FreeSelf(&value1);
				}
				break;
			}
		case C_POP_RESTOP:
			{
				s32 i;
				integer = ReadInteger();
				PopVMValue(&value2); /* reserve top value */
				for(i=0;i<integer;i++)
				{
					PopVMValue(&value1);
					tVMValue_FreeSelf(&value1);
				}
				PushVMValue(&value2); /* push back top value */
				break;
			}
		case C_CONVERT:
			{
				u8 type = (u8)ReadDataType();
				PopVMValue(&value1);
				tVMValue_ConvertType(&value1,type);
				PushVMValue(&value1);
				break;
			}
		case C_ADD:
			{
				PopVMValue(&value2);
				PopVMValue(&value1);
				tVMValue_Add(&value1,&value2,&value3);
				PushVMValue(&value3);

				tVMValue_FreeSelf(&value1);
				tVMValue_FreeSelf(&value2);
				break;
			}
		case C_SUB:
			{
				PopVMValue(&value2);
				PopVMValue(&value1);
				tVMValue_Sub(&value1,&value2,&value3);
				PushVMValue(&value3);

				tVMValue_FreeSelf(&value1);
				tVMValue_FreeSelf(&value2);
				break;
			}
		case C_MUL:
			{
				PopVMValue(&value2);
				PopVMValue(&value1);
				tVMValue_Mul(&value1,&value2,&value3);
				PushVMValue(&value3);

				tVMValue_FreeSelf(&value1);
				tVMValue_FreeSelf(&value2);
				break;
			}
		case C_DIV:
			{
				PopVMValue(&value2);
				PopVMValue(&value1);
				tVMValue_Div(&value1,&value2,&value3);
				PushVMValue(&value3);

				tVMValue_FreeSelf(&value1);
				tVMValue_FreeSelf(&value2);
				break;
			}
		case C_MOD:
			{
				PopVMValue(&value2);
				PopVMValue(&value1);
				tVMValue_Mod(&value1,&value2,&value3);
				PushVMValue(&value3);

				tVMValue_FreeSelf(&value1);
				tVMValue_FreeSelf(&value2);
				break;
			}
		case C_OPP:
			{
				PopVMValue(&value1);
				tVMValue_Opp(&value1,&value2);
				PushVMValue(&value2);

				tVMValue_FreeSelf(&value1);
				break;
			}
		case C_AND:
			{
				PopVMValue(&value2);
				PopVMValue(&value1);
				tVMValue_AND(&value1,&value2,&value3);
				PushVMValue(&value3);

				tVMValue_FreeSelf(&value1);
				tVMValue_FreeSelf(&value2);
				break;
			}
		case C_OR:
			{
				PopVMValue(&value2);
				PopVMValue(&value1);
				tVMValue_OR(&value1,&value2,&value3);
				PushVMValue(&value3);

				tVMValue_FreeSelf(&value1);
				tVMValue_FreeSelf(&value2);
				break;
			}
		case C_EQ:
			{
				PopVMValue(&value2);
				PopVMValue(&value1);
				tVMValue_EQ(&value1,&value2,&value3);
				PushVMValue(&value3);

				tVMValue_FreeSelf(&value1);
				tVMValue_FreeSelf(&value2);
				break;
			}
		case C_NOT_EQ:
			{
				PopVMValue(&value2);
				PopVMValue(&value1);
				tVMValue_NOTEQ(&value1,&value2,&value3);
				PushVMValue(&value3);

				tVMValue_FreeSelf(&value1);
				tVMValue_FreeSelf(&value2);
				break;
			}
		case C_LT:
			{
				PopVMValue(&value2);
				PopVMValue(&value1);
				tVMValue_LT(&value1,&value2,&value3);
				PushVMValue(&value3);

				tVMValue_FreeSelf(&value1);
				tVMValue_FreeSelf(&value2);
				break;
			}
		case C_LG:
			{
				PopVMValue(&value2);
				PopVMValue(&value1);
				tVMValue_LG(&value1,&value2,&value3);
				PushVMValue(&value3);

				tVMValue_FreeSelf(&value1);
				tVMValue_FreeSelf(&value2);
				break;
			}
		case C_LT_EQ:
			{
				PopVMValue(&value2);
				PopVMValue(&value1);
				tVMValue_LTEQ(&value1,&value2,&value3);
				PushVMValue(&value3);

				tVMValue_FreeSelf(&value1);
				tVMValue_FreeSelf(&value2);
				break;
			}
		case C_LG_EQ:
			{
				PopVMValue(&value2);
				PopVMValue(&value1);
				tVMValue_LGEQ(&value1,&value2,&value3);
				PushVMValue(&value3);

				tVMValue_FreeSelf(&value1);
				tVMValue_FreeSelf(&value2);
				break;
			}
		case C_FJP:
			{
				s32 size = ReadIndex();
				PopVMValue(&value1);
				if(value1.value.int_val == 0) /* if it is false */
					addIP(size);
				break;
			}
		case C_TJP:
			{
				s32 size = ReadIndex();
				PopVMValue(&value1);
				if(value1.value.int_val != 0) /* if it is true */
					addIP(size);
				break;
			}
		case C_JMP:
			{
				s32 size = ReadIndex();
				addIP(size);
				break;
			}
		case C_CALL:
			{
				/* read function name */
				integer = ReadIndex();
				/* push the stack frame */
				PushInteger(getIP());
				PushInteger(getFI());
				PushInteger(getFP());
				/* goto the call function code */
				tVM_ReleaseCodeReader();
				setFI(integer);
				tVM_InitializeCodeReader();
				/* set new FP,RP */
				setFP(getSP());
				break;
			}
		case C_INVOKE:
			{
				/* read function name */
				index = ReadIndex();
				/* execute the native function */
				tNativeFunction_Invoke(index);
				break;
			}
		case C_RET:
			{
				u32 param_bytes = ReadIndex();

				/* get the result of the function */
				retValue.type = NullType;
				PopVMValue(&retValue); 
				
				/* if this is the start function,then exit the loop */
				if(getFP() == FirstFP)
				{
					running = 0; /* set flag to stop while */
					break;
				}

				/* restore last stack frame and return to last function code */
				tVM_ReleaseCodeReader();
				PopInteger(integer);
				setFP(integer); 
				PopInteger(integer);
				setFI(integer);
				tVM_InitializeCodeReader();
				PopInteger(integer);
				setIP(integer);
				
				/* pop the old parameters */
				PopBytes(param_bytes);

				/* push back result of last function */
				PushVMValue(&retValue);
				break;
			}
		}
	}

	/* close the code reader */
	tVM_ReleaseCodeReader();
	return 1;
}
示例#9
0
void asCRestore::ReadProperty(asCProperty* prop) 
{
	ReadString(&prop->name);
	ReadDataType(&prop->type);
	READ_NUM(prop->index);
}
示例#10
0
void asCRestore::ReadObjectProperty(asCObjectProperty* prop) 
{
	ReadString(&prop->name);
	ReadDataType(&prop->type);
	READ_NUM(prop->byteOffset);
}