/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

  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2009 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. andreas@angelcode.com
  22. */
  23. //
  24. // as_context.cpp
  25. //
  26. // This class handles the execution of the byte code
  27. //
  28. #include <math.h> // fmodf()
  29. #include "as_config.h"
  30. #include "as_context.h"
  31. #include "as_scriptengine.h"
  32. #include "as_tokendef.h"
  33. #include "as_texts.h"
  34. #include "as_callfunc.h"
  35. #include "as_generic.h"
  36. #include "as_debug.h" // mkdir()
  37. #include "as_bytecode.h"
  38. #include "as_scriptobject.h"
  39. #ifdef _MSC_VER
  40. #pragma warning(disable:4702) // unreachable code
  41. #endif
  42. BEGIN_AS_NAMESPACE
  43. // We need at least 2 DWORDs reserved for exception handling
  44. // We need at least 1 DWORD reserved for calling system functions
  45. const int RESERVE_STACK = 2*AS_PTR_SIZE;
  46. // For each script function call we push 5 DWORDs on the call stack
  47. const int CALLSTACK_FRAME_SIZE = 5;
  48. #ifdef AS_DEBUG
  49. // Instruction statistics
  50. int instrCount[256];
  51. int instrCount2[256][256];
  52. int lastBC;
  53. class asCDebugStats
  54. {
  55. public:
  56. asCDebugStats()
  57. {
  58. memset(instrCount, 0, sizeof(instrCount));
  59. }
  60. ~asCDebugStats()
  61. {
  62. /*
  63. // This code writes out some statistics for the VM.
  64. // It's useful for determining what needs to be optimized.
  65. _mkdir("AS_DEBUG");
  66. FILE *f = fopen("AS_DEBUG/total.txt", "at");
  67. if( f )
  68. {
  69. // Output instruction statistics
  70. fprintf(f, "\nTotal count\n");
  71. int n;
  72. for( n = 0; n < BC_MAXBYTECODE; n++ )
  73. {
  74. if( bcName[n].name && instrCount[n] > 0 )
  75. fprintf(f, "%-10.10s : %.0f\n", bcName[n].name, instrCount[n]);
  76. }
  77. fprintf(f, "\nNever executed\n");
  78. for( n = 0; n < BC_MAXBYTECODE; n++ )
  79. {
  80. if( bcName[n].name && instrCount[n] == 0 )
  81. fprintf(f, "%-10.10s\n", bcName[n].name);
  82. }
  83. fclose(f);
  84. }
  85. */
  86. }
  87. double instrCount[256];
  88. } stats;
  89. #endif
  90. AS_API asIScriptContext *asGetActiveContext()
  91. {
  92. asASSERT(threadManager);
  93. asCThreadLocalData *tld = threadManager->GetLocalData();
  94. if( tld->activeContexts.GetLength() == 0 )
  95. return 0;
  96. return tld->activeContexts[tld->activeContexts.GetLength()-1];
  97. }
  98. void asPushActiveContext(asIScriptContext *ctx)
  99. {
  100. asASSERT(threadManager);
  101. asCThreadLocalData *tld = threadManager->GetLocalData();
  102. tld->activeContexts.PushLast(ctx);
  103. }
  104. void asPopActiveContext(asIScriptContext *ctx)
  105. {
  106. asASSERT(threadManager);
  107. asCThreadLocalData *tld = threadManager->GetLocalData();
  108. asASSERT(tld->activeContexts.GetLength() > 0);
  109. asASSERT(tld->activeContexts[tld->activeContexts.GetLength()-1] == ctx);
  110. UNUSED_VAR(ctx);
  111. tld->activeContexts.PopLast();
  112. }
  113. asCContext::asCContext(asCScriptEngine *engine, bool holdRef)
  114. {
  115. #ifdef AS_DEBUG
  116. memset(instrCount, 0, sizeof(instrCount));
  117. memset(instrCount2, 0, sizeof(instrCount2));
  118. lastBC = 255;
  119. #endif
  120. holdEngineRef = holdRef;
  121. if( holdRef )
  122. engine->AddRef();
  123. this->engine = engine;
  124. status = asEXECUTION_UNINITIALIZED;
  125. stackBlockSize = 0;
  126. refCount.set(1);
  127. inExceptionHandler = false;
  128. isStackMemoryNotAllocated = false;
  129. #ifdef AS_DEPRECATED
  130. // Deprecated since 2009-12-08, 2.18.0
  131. stringFunction = 0;
  132. #endif
  133. currentFunction = 0;
  134. regs.objectRegister = 0;
  135. initialFunction = 0;
  136. lineCallback = false;
  137. exceptionCallback = false;
  138. regs.doProcessSuspend = false;
  139. doSuspend = false;
  140. userData = 0;
  141. }
  142. asCContext::~asCContext()
  143. {
  144. DetachEngine();
  145. }
  146. int asCContext::AddRef()
  147. {
  148. return refCount.atomicInc();
  149. }
  150. int asCContext::Release()
  151. {
  152. int r = refCount.atomicDec();
  153. if( r == 0 )
  154. {
  155. asDELETE(this,asCContext);
  156. return 0;
  157. }
  158. return r;
  159. }
  160. void asCContext::DetachEngine()
  161. {
  162. if( engine == 0 ) return;
  163. // Abort any execution
  164. Abort();
  165. // Free all resources
  166. Unprepare();
  167. // Clear engine pointer
  168. if( holdEngineRef )
  169. engine->Release();
  170. engine = 0;
  171. }
  172. asIScriptEngine *asCContext::GetEngine()
  173. {
  174. return engine;
  175. }
  176. void *asCContext::SetUserData(void *data)
  177. {
  178. void *oldData = userData;
  179. userData = data;
  180. return oldData;
  181. }
  182. void *asCContext::GetUserData()
  183. {
  184. return userData;
  185. }
  186. int asCContext::Prepare(int funcID)
  187. {
  188. if( status == asEXECUTION_ACTIVE || status == asEXECUTION_SUSPENDED )
  189. return asCONTEXT_ACTIVE;
  190. // Clean the stack if not done before
  191. if( status != asEXECUTION_FINISHED && status != asEXECUTION_UNINITIALIZED )
  192. CleanStack();
  193. // Release the returned object (if any)
  194. CleanReturnObject();
  195. if( funcID == -1 )
  196. {
  197. // Use the previously prepared function
  198. if( initialFunction == 0 )
  199. return asNO_FUNCTION;
  200. currentFunction = initialFunction;
  201. }
  202. else if( initialFunction && initialFunction->id == funcID )
  203. {
  204. currentFunction = initialFunction;
  205. }
  206. else
  207. {
  208. // Check engine pointer
  209. asASSERT( engine );
  210. if( initialFunction )
  211. initialFunction->Release();
  212. initialFunction = engine->GetScriptFunction(funcID);
  213. if( initialFunction == 0 )
  214. return asNO_FUNCTION;
  215. initialFunction->AddRef();
  216. currentFunction = initialFunction;
  217. regs.globalVarPointers = currentFunction->globalVarPointers.AddressOf();
  218. // Determine the minimum stack size needed
  219. // TODO: optimize: GetSpaceNeededForArguments() should be precomputed
  220. int stackSize = currentFunction->GetSpaceNeededForArguments() + currentFunction->stackNeeded + RESERVE_STACK;
  221. stackSize = stackSize > engine->initialContextStackSize ? stackSize : engine->initialContextStackSize;
  222. if( stackSize > stackBlockSize )
  223. {
  224. for( asUINT n = 0; n < stackBlocks.GetLength(); n++ )
  225. if( stackBlocks[n] )
  226. {
  227. asDELETEARRAY(stackBlocks[n]);
  228. }
  229. stackBlocks.SetLength(0);
  230. stackBlockSize = stackSize;
  231. asDWORD *stack = asNEWARRAY(asDWORD,stackBlockSize);
  232. stackBlocks.PushLast(stack);
  233. }
  234. // Reserve space for the arguments and return value
  235. returnValueSize = currentFunction->GetSpaceNeededForReturnValue();
  236. // TODO: optimize: GetSpaceNeededForArguments() should be precomputed
  237. argumentsSize = currentFunction->GetSpaceNeededForArguments() + (currentFunction->objectType ? AS_PTR_SIZE : 0);
  238. }
  239. // Reset state
  240. // Most of the time the previous state will be asEXECUTION_FINISHED, in which case the values are already initialized
  241. if( status != asEXECUTION_FINISHED )
  242. {
  243. exceptionLine = -1;
  244. exceptionFunction = 0;
  245. isCallingSystemFunction = false;
  246. doAbort = false;
  247. doSuspend = false;
  248. regs.doProcessSuspend = lineCallback;
  249. externalSuspendRequest = false;
  250. stackIndex = 0;
  251. }
  252. status = asEXECUTION_PREPARED;
  253. // Reserve space for the arguments and return value
  254. regs.stackFramePointer = stackBlocks[0] + stackBlockSize - argumentsSize;
  255. regs.stackPointer = regs.stackFramePointer;
  256. // Set arguments to 0
  257. memset(regs.stackPointer, 0, 4*argumentsSize);
  258. if( currentFunction->funcType == asFUNC_SCRIPT )
  259. {
  260. regs.programPointer = currentFunction->byteCode.AddressOf();
  261. // Set all object variables to 0
  262. for( asUINT n = 0; n < currentFunction->objVariablePos.GetLength(); n++ )
  263. {
  264. int pos = currentFunction->objVariablePos[n];
  265. *(size_t*)&regs.stackFramePointer[-pos] = 0;
  266. }
  267. }
  268. else
  269. regs.programPointer = 0;
  270. return asSUCCESS;
  271. }
  272. // Free all resources
  273. int asCContext::Unprepare()
  274. {
  275. if( status == asEXECUTION_ACTIVE || status == asEXECUTION_SUSPENDED )
  276. return asCONTEXT_ACTIVE;
  277. // Only clean the stack if the context was prepared but not executed
  278. if( status != asEXECUTION_UNINITIALIZED )
  279. CleanStack();
  280. // Release the returned object (if any)
  281. CleanReturnObject();
  282. // Release the initial function
  283. if( initialFunction )
  284. initialFunction->Release();
  285. // Clear function pointers
  286. initialFunction = 0;
  287. currentFunction = 0;
  288. exceptionFunction = 0;
  289. regs.programPointer = 0;
  290. // Reset status
  291. status = asEXECUTION_UNINITIALIZED;
  292. // Deallocate the stack blocks
  293. for( asUINT n = 0; n < stackBlocks.GetLength(); n++ )
  294. {
  295. if( stackBlocks[n] )
  296. {
  297. asDELETEARRAY(stackBlocks[n]);
  298. }
  299. }
  300. stackBlocks.SetLength(0);
  301. stackBlockSize = 0;
  302. regs.stackFramePointer = 0;
  303. regs.stackPointer = 0;
  304. stackIndex = 0;
  305. #ifdef AS_DEPRECATED
  306. // Deprecated since 2009-12-08, 2.18.0
  307. // Deallocate string function
  308. if( stringFunction )
  309. {
  310. stringFunction->Release();
  311. stringFunction = 0;
  312. }
  313. #endif
  314. return 0;
  315. }
  316. #ifdef AS_DEPRECATED
  317. // Deprecated since 2009-12-08, 2.18.0
  318. int asCContext::SetExecuteStringFunction(asCScriptFunction *func)
  319. {
  320. if( stringFunction )
  321. stringFunction->Release();
  322. // The new function already has the refCount set to 1
  323. stringFunction = func;
  324. return 0;
  325. }
  326. #endif
  327. asBYTE asCContext::GetReturnByte()
  328. {
  329. if( status != asEXECUTION_FINISHED ) return 0;
  330. asCDataType *dt = &initialFunction->returnType;
  331. if( dt->IsObject() || dt->IsReference() ) return 0;
  332. return *(asBYTE*)&regs.valueRegister;
  333. }
  334. asWORD asCContext::GetReturnWord()
  335. {
  336. if( status != asEXECUTION_FINISHED ) return 0;
  337. asCDataType *dt = &initialFunction->returnType;
  338. if( dt->IsObject() || dt->IsReference() ) return 0;
  339. return *(asWORD*)&regs.valueRegister;
  340. }
  341. asDWORD asCContext::GetReturnDWord()
  342. {
  343. if( status != asEXECUTION_FINISHED ) return 0;
  344. asCDataType *dt = &initialFunction->returnType;
  345. if( dt->IsObject() || dt->IsReference() ) return 0;
  346. return *(asDWORD*)&regs.valueRegister;
  347. }
  348. asQWORD asCContext::GetReturnQWord()
  349. {
  350. if( status != asEXECUTION_FINISHED ) return 0;
  351. asCDataType *dt = &initialFunction->returnType;
  352. if( dt->IsObject() || dt->IsReference() ) return 0;
  353. return regs.valueRegister;
  354. }
  355. float asCContext::GetReturnFloat()
  356. {
  357. if( status != asEXECUTION_FINISHED ) return 0;
  358. asCDataType *dt = &initialFunction->returnType;
  359. if( dt->IsObject() || dt->IsReference() ) return 0;
  360. return *(float*)&regs.valueRegister;
  361. }
  362. double asCContext::GetReturnDouble()
  363. {
  364. if( status != asEXECUTION_FINISHED ) return 0;
  365. asCDataType *dt = &initialFunction->returnType;
  366. if( dt->IsObject() || dt->IsReference() ) return 0;
  367. return *(double*)&regs.valueRegister;
  368. }
  369. void *asCContext::GetReturnAddress()
  370. {
  371. if( status != asEXECUTION_FINISHED ) return 0;
  372. asCDataType *dt = &initialFunction->returnType;
  373. if( dt->IsReference() )
  374. return *(void**)&regs.valueRegister;
  375. else if( dt->IsObject() )
  376. return regs.objectRegister;
  377. return 0;
  378. }
  379. void *asCContext::GetReturnObject()
  380. {
  381. if( status != asEXECUTION_FINISHED ) return 0;
  382. asCDataType *dt = &initialFunction->returnType;
  383. if( !dt->IsObject() ) return 0;
  384. if( dt->IsReference() )
  385. return *(void**)(size_t)regs.valueRegister;
  386. else
  387. return regs.objectRegister;
  388. }
  389. void *asCContext::GetAddressOfReturnValue()
  390. {
  391. if( status != asEXECUTION_FINISHED ) return 0;
  392. asCDataType *dt = &initialFunction->returnType;
  393. // An object is stored in the objectRegister
  394. if( !dt->IsReference() && dt->IsObject() )
  395. {
  396. // Need to dereference objects
  397. if( !dt->IsObjectHandle() )
  398. return *(void**)&regs.objectRegister;
  399. return &regs.objectRegister;
  400. }
  401. // Primitives and references are stored in valueRegister
  402. return &regs.valueRegister;
  403. }
  404. int asCContext::SetObject(void *obj)
  405. {
  406. if( status != asEXECUTION_PREPARED )
  407. return asCONTEXT_NOT_PREPARED;
  408. if( !initialFunction->objectType )
  409. {
  410. status = asEXECUTION_ERROR;
  411. return asERROR;
  412. }
  413. *(size_t*)&regs.stackFramePointer[0] = (size_t)obj;
  414. return 0;
  415. }
  416. int asCContext::SetArgByte(asUINT arg, asBYTE value)
  417. {
  418. if( status != asEXECUTION_PREPARED )
  419. return asCONTEXT_NOT_PREPARED;
  420. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  421. {
  422. status = asEXECUTION_ERROR;
  423. return asINVALID_ARG;
  424. }
  425. // Verify the type of the argument
  426. asCDataType *dt = &initialFunction->parameterTypes[arg];
  427. if( dt->IsObject() || dt->IsReference() )
  428. {
  429. status = asEXECUTION_ERROR;
  430. return asINVALID_TYPE;
  431. }
  432. if( dt->GetSizeInMemoryBytes() != 1 )
  433. {
  434. status = asEXECUTION_ERROR;
  435. return asINVALID_TYPE;
  436. }
  437. // Determine the position of the argument
  438. int offset = 0;
  439. if( initialFunction->objectType )
  440. offset += AS_PTR_SIZE;
  441. for( asUINT n = 0; n < arg; n++ )
  442. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  443. // Set the value
  444. *(asBYTE*)&regs.stackFramePointer[offset] = value;
  445. return 0;
  446. }
  447. int asCContext::SetArgWord(asUINT arg, asWORD value)
  448. {
  449. if( status != asEXECUTION_PREPARED )
  450. return asCONTEXT_NOT_PREPARED;
  451. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  452. {
  453. status = asEXECUTION_ERROR;
  454. return asINVALID_ARG;
  455. }
  456. // Verify the type of the argument
  457. asCDataType *dt = &initialFunction->parameterTypes[arg];
  458. if( dt->IsObject() || dt->IsReference() )
  459. {
  460. status = asEXECUTION_ERROR;
  461. return asINVALID_TYPE;
  462. }
  463. if( dt->GetSizeInMemoryBytes() != 2 )
  464. {
  465. status = asEXECUTION_ERROR;
  466. return asINVALID_TYPE;
  467. }
  468. // Determine the position of the argument
  469. int offset = 0;
  470. if( initialFunction->objectType )
  471. offset += AS_PTR_SIZE;
  472. for( asUINT n = 0; n < arg; n++ )
  473. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  474. // Set the value
  475. *(asWORD*)&regs.stackFramePointer[offset] = value;
  476. return 0;
  477. }
  478. int asCContext::SetArgDWord(asUINT arg, asDWORD value)
  479. {
  480. if( status != asEXECUTION_PREPARED )
  481. return asCONTEXT_NOT_PREPARED;
  482. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  483. {
  484. status = asEXECUTION_ERROR;
  485. return asINVALID_ARG;
  486. }
  487. // Verify the type of the argument
  488. asCDataType *dt = &initialFunction->parameterTypes[arg];
  489. if( dt->IsObject() || dt->IsReference() )
  490. {
  491. status = asEXECUTION_ERROR;
  492. return asINVALID_TYPE;
  493. }
  494. if( dt->GetSizeInMemoryBytes() != 4 )
  495. {
  496. status = asEXECUTION_ERROR;
  497. return asINVALID_TYPE;
  498. }
  499. // Determine the position of the argument
  500. int offset = 0;
  501. if( initialFunction->objectType )
  502. offset += AS_PTR_SIZE;
  503. for( asUINT n = 0; n < arg; n++ )
  504. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  505. // Set the value
  506. *(asDWORD*)&regs.stackFramePointer[offset] = value;
  507. return 0;
  508. }
  509. int asCContext::SetArgQWord(asUINT arg, asQWORD value)
  510. {
  511. if( status != asEXECUTION_PREPARED )
  512. return asCONTEXT_NOT_PREPARED;
  513. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  514. {
  515. status = asEXECUTION_ERROR;
  516. return asINVALID_ARG;
  517. }
  518. // Verify the type of the argument
  519. asCDataType *dt = &initialFunction->parameterTypes[arg];
  520. if( dt->IsObject() || dt->IsReference() )
  521. {
  522. status = asEXECUTION_ERROR;
  523. return asINVALID_TYPE;
  524. }
  525. if( dt->GetSizeOnStackDWords() != 2 )
  526. {
  527. status = asEXECUTION_ERROR;
  528. return asINVALID_TYPE;
  529. }
  530. // Determine the position of the argument
  531. int offset = 0;
  532. if( initialFunction->objectType )
  533. offset += AS_PTR_SIZE;
  534. for( asUINT n = 0; n < arg; n++ )
  535. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  536. // Set the value
  537. *(asQWORD*)(&regs.stackFramePointer[offset]) = value;
  538. return 0;
  539. }
  540. int asCContext::SetArgFloat(asUINT arg, float value)
  541. {
  542. if( status != asEXECUTION_PREPARED )
  543. return asCONTEXT_NOT_PREPARED;
  544. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  545. {
  546. status = asEXECUTION_ERROR;
  547. return asINVALID_ARG;
  548. }
  549. // Verify the type of the argument
  550. asCDataType *dt = &initialFunction->parameterTypes[arg];
  551. if( dt->IsObject() || dt->IsReference() )
  552. {
  553. status = asEXECUTION_ERROR;
  554. return asINVALID_TYPE;
  555. }
  556. if( dt->GetSizeOnStackDWords() != 1 )
  557. {
  558. status = asEXECUTION_ERROR;
  559. return asINVALID_TYPE;
  560. }
  561. // Determine the position of the argument
  562. int offset = 0;
  563. if( initialFunction->objectType )
  564. offset += AS_PTR_SIZE;
  565. for( asUINT n = 0; n < arg; n++ )
  566. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  567. // Set the value
  568. *(float*)(&regs.stackFramePointer[offset]) = value;
  569. return 0;
  570. }
  571. int asCContext::SetArgDouble(asUINT arg, double value)
  572. {
  573. if( status != asEXECUTION_PREPARED )
  574. return asCONTEXT_NOT_PREPARED;
  575. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  576. {
  577. status = asEXECUTION_ERROR;
  578. return asINVALID_ARG;
  579. }
  580. // Verify the type of the argument
  581. asCDataType *dt = &initialFunction->parameterTypes[arg];
  582. if( dt->IsObject() || dt->IsReference() )
  583. {
  584. status = asEXECUTION_ERROR;
  585. return asINVALID_TYPE;
  586. }
  587. if( dt->GetSizeOnStackDWords() != 2 )
  588. {
  589. status = asEXECUTION_ERROR;
  590. return asINVALID_TYPE;
  591. }
  592. // Determine the position of the argument
  593. int offset = 0;
  594. if( initialFunction->objectType )
  595. offset += AS_PTR_SIZE;
  596. for( asUINT n = 0; n < arg; n++ )
  597. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  598. // Set the value
  599. *(double*)(&regs.stackFramePointer[offset]) = value;
  600. return 0;
  601. }
  602. int asCContext::SetArgAddress(asUINT arg, void *value)
  603. {
  604. if( status != asEXECUTION_PREPARED )
  605. return asCONTEXT_NOT_PREPARED;
  606. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  607. {
  608. status = asEXECUTION_ERROR;
  609. return asINVALID_ARG;
  610. }
  611. // Verify the type of the argument
  612. asCDataType *dt = &initialFunction->parameterTypes[arg];
  613. if( !dt->IsReference() && !dt->IsObjectHandle() )
  614. {
  615. status = asEXECUTION_ERROR;
  616. return asINVALID_TYPE;
  617. }
  618. // Determine the position of the argument
  619. int offset = 0;
  620. if( initialFunction->objectType )
  621. offset += AS_PTR_SIZE;
  622. for( asUINT n = 0; n < arg; n++ )
  623. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  624. // Set the value
  625. *(size_t*)(&regs.stackFramePointer[offset]) = (size_t)value;
  626. return 0;
  627. }
  628. int asCContext::SetArgObject(asUINT arg, void *obj)
  629. {
  630. if( status != asEXECUTION_PREPARED )
  631. return asCONTEXT_NOT_PREPARED;
  632. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  633. {
  634. status = asEXECUTION_ERROR;
  635. return asINVALID_ARG;
  636. }
  637. // Verify the type of the argument
  638. asCDataType *dt = &initialFunction->parameterTypes[arg];
  639. if( !dt->IsObject() )
  640. {
  641. status = asEXECUTION_ERROR;
  642. return asINVALID_TYPE;
  643. }
  644. // If the object should be sent by value we must make a copy of it
  645. if( !dt->IsReference() )
  646. {
  647. if( dt->IsObjectHandle() )
  648. {
  649. // Increase the reference counter
  650. asSTypeBehaviour *beh = &dt->GetObjectType()->beh;
  651. if( obj && beh->addref )
  652. engine->CallObjectMethod(obj, beh->addref);
  653. }
  654. else
  655. {
  656. obj = engine->CreateScriptObjectCopy(obj, engine->GetTypeIdFromDataType(*dt));
  657. }
  658. }
  659. // Determine the position of the argument
  660. int offset = 0;
  661. if( initialFunction->objectType )
  662. offset += AS_PTR_SIZE;
  663. for( asUINT n = 0; n < arg; n++ )
  664. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  665. // Set the value
  666. *(size_t*)(&regs.stackFramePointer[offset]) = (size_t)obj;
  667. return 0;
  668. }
  669. // TODO: Instead of GetAddressOfArg, maybe we need a SetArgValue(int arg, void *value, bool takeOwnership) instead.
  670. // interface
  671. void *asCContext::GetAddressOfArg(asUINT arg)
  672. {
  673. if( status != asEXECUTION_PREPARED )
  674. return 0;
  675. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  676. return 0;
  677. // Determine the position of the argument
  678. int offset = 0;
  679. if( initialFunction->objectType )
  680. offset += AS_PTR_SIZE;
  681. for( asUINT n = 0; n < arg; n++ )
  682. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  683. // We should return the address of the location where the argument value will be placed
  684. // All registered types are always sent by reference, even if
  685. // the function is declared to receive the argument by value.
  686. return &regs.stackFramePointer[offset];
  687. }
  688. int asCContext::Abort()
  689. {
  690. // TODO: multithread: Make thread safe
  691. if( engine == 0 ) return asERROR;
  692. if( status == asEXECUTION_SUSPENDED )
  693. status = asEXECUTION_ABORTED;
  694. doSuspend = true;
  695. regs.doProcessSuspend = true;
  696. externalSuspendRequest = true;
  697. doAbort = true;
  698. return 0;
  699. }
  700. // interface
  701. int asCContext::Suspend()
  702. {
  703. // This function just sets some internal flags and is safe
  704. // to call from a secondary thread, even if the library has
  705. // been built without multi-thread support.
  706. if( engine == 0 ) return asERROR;
  707. doSuspend = true;
  708. externalSuspendRequest = true;
  709. regs.doProcessSuspend = true;
  710. return 0;
  711. }
  712. // interface
  713. int asCContext::Execute()
  714. {
  715. asASSERT( engine != 0 );
  716. if( status != asEXECUTION_SUSPENDED && status != asEXECUTION_PREPARED )
  717. return asERROR;
  718. status = asEXECUTION_ACTIVE;
  719. asPushActiveContext((asIScriptContext *)this);
  720. if( regs.programPointer == 0 )
  721. {
  722. if( currentFunction->funcType == asFUNC_VIRTUAL ||
  723. currentFunction->funcType == asFUNC_INTERFACE )
  724. {
  725. // The currentFunction is a virtual method
  726. // Determine the true function from the object
  727. asCScriptObject *obj = *(asCScriptObject**)(size_t*)regs.stackFramePointer;
  728. if( obj == 0 )
  729. {
  730. SetInternalException(TXT_NULL_POINTER_ACCESS);
  731. }
  732. else
  733. {
  734. asCObjectType *objType = obj->objType;
  735. asCScriptFunction *realFunc = 0;
  736. if( currentFunction->funcType == asFUNC_VIRTUAL )
  737. {
  738. if( objType->virtualFunctionTable.GetLength() > (asUINT)currentFunction->vfTableIdx )
  739. {
  740. realFunc = objType->virtualFunctionTable[currentFunction->vfTableIdx];
  741. }
  742. }
  743. else
  744. {
  745. // Search the object type for a function that matches the interface function
  746. for( asUINT n = 0; n < objType->methods.GetLength(); n++ )
  747. {
  748. asCScriptFunction *f2 = engine->scriptFunctions[objType->methods[n]];
  749. if( f2->signatureId == currentFunction->signatureId )
  750. {
  751. if( f2->funcType == asFUNC_VIRTUAL )
  752. realFunc = objType->virtualFunctionTable[f2->vfTableIdx];
  753. else
  754. realFunc = f2;
  755. break;
  756. }
  757. }
  758. }
  759. if( realFunc )
  760. {
  761. if( realFunc->signatureId != currentFunction->signatureId )
  762. {
  763. SetInternalException(TXT_NULL_POINTER_ACCESS);
  764. }
  765. else
  766. {
  767. currentFunction = realFunc;
  768. regs.programPointer = currentFunction->byteCode.AddressOf();
  769. regs.globalVarPointers = currentFunction->globalVarPointers.AddressOf();
  770. // Set the local objects to 0
  771. for( asUINT n = 0; n < currentFunction->objVariablePos.GetLength(); n++ )
  772. {
  773. int pos = currentFunction->objVariablePos[n];
  774. *(size_t*)&regs.stackFramePointer[-pos] = 0;
  775. }
  776. }
  777. }
  778. }
  779. }
  780. else if( currentFunction->funcType == asFUNC_SYSTEM )
  781. {
  782. // The current function is an application registered function
  783. // Call the function directly
  784. CallSystemFunction(currentFunction->id, this, 0);
  785. // Was the call successful?
  786. if( status == asEXECUTION_ACTIVE )
  787. {
  788. status = asEXECUTION_FINISHED;
  789. }
  790. }
  791. else
  792. {
  793. // This shouldn't happen
  794. asASSERT(false);
  795. }
  796. }
  797. while( status == asEXECUTION_ACTIVE )
  798. ExecuteNext();
  799. doSuspend = false;
  800. regs.doProcessSuspend = lineCallback;
  801. asPopActiveContext((asIScriptContext *)this);
  802. #ifdef AS_DEBUG
  803. /*
  804. // Output instruction statistics
  805. // This is useful for determining what needs to be optimized.
  806. _mkdir("AS_DEBUG");
  807. FILE *f = fopen("AS_DEBUG/stats.txt", "at");
  808. fprintf(f, "\n");
  809. asQWORD total = 0;
  810. int n;
  811. for( n = 0; n < 256; n++ )
  812. {
  813. if( bcName[n].name && instrCount[n] )
  814. fprintf(f, "%-10.10s : %d\n", bcName[n].name, instrCount[n]);
  815. total += instrCount[n];
  816. }
  817. fprintf(f, "\ntotal : %I64d\n", total);
  818. fprintf(f, "\n");
  819. for( n = 0; n < 256; n++ )
  820. {
  821. if( bcName[n].name )
  822. {
  823. for( int m = 0; m < 256; m++ )
  824. {
  825. if( instrCount2[n][m] )
  826. fprintf(f, "%-10.10s, %-10.10s : %d\n", bcName[n].name, bcName[m].name, instrCount2[n][m]);
  827. }
  828. }
  829. }
  830. fclose(f);
  831. */
  832. #endif
  833. if( status == asEXECUTION_FINISHED )
  834. {
  835. regs.objectType = initialFunction->returnType.GetObjectType();
  836. return asEXECUTION_FINISHED;
  837. }
  838. if( status == asEXECUTION_SUSPENDED )
  839. return asEXECUTION_SUSPENDED;
  840. if( doAbort )
  841. {
  842. doAbort = false;
  843. status = asEXECUTION_ABORTED;
  844. return asEXECUTION_ABORTED;
  845. }
  846. if( status == asEXECUTION_EXCEPTION )
  847. return asEXECUTION_EXCEPTION;
  848. return asERROR;
  849. }
  850. void asCContext::PushCallState()
  851. {
  852. callStack.SetLength(callStack.GetLength() + CALLSTACK_FRAME_SIZE);
  853. // Separating the loads and stores limits data cache trash, and with a smart compiler
  854. // could turn into SIMD style loading/storing if available.
  855. // The compiler can't do this itself due to potential pointer aliasing between the pointers,
  856. // ie writing to tmp could overwrite the data contained in registers.stackFramePointer for example
  857. // for all the compiler knows. So introducing the local variable s, which is never referred to by
  858. // its address we avoid this issue.
  859. size_t s[5];
  860. s[0] = (size_t)regs.stackFramePointer;
  861. s[1] = (size_t)currentFunction;
  862. s[2] = (size_t)regs.programPointer;
  863. s[3] = (size_t)regs.stackPointer;
  864. s[4] = stackIndex;
  865. size_t *tmp = callStack.AddressOf() + callStack.GetLength() - CALLSTACK_FRAME_SIZE;
  866. tmp[0] = s[0];
  867. tmp[1] = s[1];
  868. tmp[2] = s[2];
  869. tmp[3] = s[3];
  870. tmp[4] = s[4];
  871. }
  872. void asCContext::PopCallState()
  873. {
  874. // See comments in PushCallState about pointer aliasing and data cache trashing
  875. size_t *tmp = callStack.AddressOf() + callStack.GetLength() - CALLSTACK_FRAME_SIZE;
  876. size_t s[5];
  877. s[0] = tmp[0];
  878. s[1] = tmp[1];
  879. s[2] = tmp[2];
  880. s[3] = tmp[3];
  881. s[4] = tmp[4];
  882. regs.stackFramePointer = (asDWORD*)s[0];
  883. currentFunction = (asCScriptFunction*)s[1];
  884. regs.programPointer = (asDWORD*)s[2];
  885. regs.stackPointer = (asDWORD*)s[3];
  886. stackIndex = (int)s[4];
  887. regs.globalVarPointers = currentFunction->globalVarPointers.AddressOf();
  888. callStack.SetLength(callStack.GetLength() - CALLSTACK_FRAME_SIZE);
  889. }
  890. int asCContext::GetCallstackSize()
  891. {
  892. return (int)callStack.GetLength() / CALLSTACK_FRAME_SIZE;
  893. }
  894. int asCContext::GetCallstackFunction(int index)
  895. {
  896. if( index < 0 || index >= GetCallstackSize() ) return asINVALID_ARG;
  897. size_t *s = callStack.AddressOf() + index*CALLSTACK_FRAME_SIZE;
  898. asCScriptFunction *func = (asCScriptFunction*)s[1];
  899. return func->id;
  900. }
  901. int asCContext::GetCallstackLineNumber(int index, int *column)
  902. {
  903. if( index < 0 || index >= GetCallstackSize() ) return asINVALID_ARG;
  904. size_t *s = callStack.AddressOf() + index*CALLSTACK_FRAME_SIZE;
  905. asCScriptFunction *func = (asCScriptFunction*)s[1];
  906. asDWORD *bytePos = (asDWORD*)s[2];
  907. asDWORD line = func->GetLineNumber(int(bytePos - func->byteCode.AddressOf()));
  908. if( column ) *column = (line >> 20);
  909. return (line & 0xFFFFF);
  910. }
  911. void asCContext::CallScriptFunction(asCScriptFunction *func)
  912. {
  913. // Push the framepointer, function id and programCounter on the stack
  914. PushCallState();
  915. currentFunction = func;
  916. regs.globalVarPointers = currentFunction->globalVarPointers.AddressOf();
  917. regs.programPointer = currentFunction->byteCode.AddressOf();
  918. // Verify if there is enough room in the stack block. Allocate new block if not
  919. if( regs.stackPointer - (func->stackNeeded + RESERVE_STACK) < stackBlocks[stackIndex] )
  920. {
  921. asDWORD *oldStackPointer = regs.stackPointer;
  922. // The size of each stack block is determined by the following formula:
  923. // size = stackBlockSize << index
  924. while( regs.stackPointer - (func->stackNeeded + RESERVE_STACK) < stackBlocks[stackIndex] )
  925. {
  926. // Make sure we don't allocate more space than allowed
  927. if( engine->ep.maximumContextStackSize )
  928. {
  929. // This test will only stop growth once it has already crossed the limit
  930. if( stackBlockSize * ((1 << (stackIndex+1)) - 1) > engine->ep.maximumContextStackSize )
  931. {
  932. isStackMemoryNotAllocated = true;
  933. // Set the stackFramePointer, even though the stackPointer wasn't updated
  934. regs.stackFramePointer = regs.stackPointer;
  935. // TODO: Make sure the exception handler doesn't try to free objects that have not been initialized
  936. SetInternalException(TXT_STACK_OVERFLOW);
  937. return;
  938. }
  939. }
  940. stackIndex++;
  941. if( (int)stackBlocks.GetLength() == stackIndex )
  942. {
  943. asDWORD *stack = asNEWARRAY(asDWORD,(stackBlockSize << stackIndex));
  944. stackBlocks.PushLast(stack);
  945. }
  946. regs.stackPointer = stackBlocks[stackIndex] + (stackBlockSize<<stackIndex) - func->GetSpaceNeededForArguments();
  947. }
  948. // Copy the function arguments to the new stack space
  949. memcpy(regs.stackPointer, oldStackPointer, sizeof(asDWORD)*func->GetSpaceNeededForArguments());
  950. }
  951. // Update framepointer and programCounter
  952. regs.stackFramePointer = regs.stackPointer;
  953. // Set all object variables to 0
  954. for( asUINT n = 0; n < currentFunction->objVariablePos.GetLength(); n++ )
  955. {
  956. int pos = currentFunction->objVariablePos[n];
  957. *(size_t*)&regs.stackFramePointer[-pos] = 0;
  958. }
  959. }
  960. void asCContext::CallInterfaceMethod(asCScriptFunction *func)
  961. {
  962. // Resolve the interface method using the current script type
  963. asCScriptObject *obj = *(asCScriptObject**)(size_t*)regs.stackPointer;
  964. if( obj == 0 )
  965. {
  966. SetInternalException(TXT_NULL_POINTER_ACCESS);
  967. return;
  968. }
  969. asCObjectType *objType = obj->objType;
  970. // TODO: optimize: The object type should have a list of only those methods that
  971. // implement interface methods. This list should be ordered by
  972. // the signatureId so that a binary search can be made, instead
  973. // of a linear search.
  974. //
  975. // When this is done, we must also make sure the signatureId of a
  976. // function never changes, e.g. when if the signature functions are
  977. // released.
  978. // Search the object type for a function that matches the interface function
  979. asCScriptFunction *realFunc = 0;
  980. if( func->funcType == asFUNC_INTERFACE )
  981. {
  982. for( asUINT n = 0; n < objType->methods.GetLength(); n++ )
  983. {
  984. asCScriptFunction *f2 = engine->scriptFunctions[objType->methods[n]];
  985. if( f2->signatureId == func->signatureId )
  986. {
  987. if( f2->funcType == asFUNC_VIRTUAL )
  988. realFunc = objType->virtualFunctionTable[f2->vfTableIdx];
  989. else
  990. realFunc = f2;
  991. break;
  992. }
  993. }
  994. if( realFunc == 0 )
  995. {
  996. SetInternalException(TXT_NULL_POINTER_ACCESS);
  997. return;
  998. }
  999. }
  1000. else /* if( func->funcType == asFUNC_VIRTUAL ) */
  1001. {
  1002. realFunc = objType->virtualFunctionTable[func->vfTableIdx];
  1003. }
  1004. // Then call the true script function
  1005. CallScriptFunction(realFunc);
  1006. }
  1007. void asCContext::ExecuteNext()
  1008. {
  1009. asDWORD *l_bc = regs.programPointer;
  1010. asDWORD *l_sp = regs.stackPointer;
  1011. asDWORD *l_fp = regs.stackFramePointer;
  1012. for(;;)
  1013. {
  1014. #ifdef AS_DEBUG
  1015. ++stats.instrCount[*(asBYTE*)l_bc];
  1016. ++instrCount[*(asBYTE*)l_bc];
  1017. ++instrCount2[lastBC][*(asBYTE*)l_bc];
  1018. lastBC = *(asBYTE*)l_bc;
  1019. // Used to verify that the size of the instructions are correct
  1020. asDWORD *old = l_bc;
  1021. #endif
  1022. // Remember to keep the cases in order and without
  1023. // gaps, because that will make the switch faster.
  1024. // It will be faster since only one lookup will be
  1025. // made to find the correct jump destination. If not
  1026. // in order, the switch will make two lookups.
  1027. switch( *(asBYTE*)l_bc )
  1028. {
  1029. //--------------
  1030. // memory access functions
  1031. // Decrease the stack pointer with n dwords (stack grows downward)
  1032. case asBC_POP:
  1033. l_sp += asBC_WORDARG0(l_bc);
  1034. l_bc++;
  1035. break;
  1036. // Increase the stack pointer with n dwords
  1037. case asBC_PUSH:
  1038. l_sp -= asBC_WORDARG0(l_bc);
  1039. l_bc++;
  1040. break;
  1041. // Push a dword value on the stack
  1042. case asBC_PshC4:
  1043. --l_sp;
  1044. *l_sp = asBC_DWORDARG(l_bc);
  1045. l_bc += 2;
  1046. break;
  1047. // Push the dword value of a variable on the stack
  1048. case asBC_PshV4:
  1049. --l_sp;
  1050. *l_sp = *(l_fp - asBC_SWORDARG0(l_bc));
  1051. l_bc++;
  1052. break;
  1053. // Push the address of a variable on the stack
  1054. case asBC_PSF:
  1055. l_sp -= AS_PTR_SIZE;
  1056. *(asPTRWORD*)l_sp = (asPTRWORD)size_t(l_fp - asBC_SWORDARG0(l_bc));
  1057. l_bc++;
  1058. break;
  1059. // Swap the top 2 dwords on the stack
  1060. case asBC_SWAP4:
  1061. {
  1062. asDWORD d = (asDWORD)*l_sp;
  1063. *l_sp = *(l_sp+1);
  1064. *(asDWORD*)(l_sp+1) = d;
  1065. l_bc++;
  1066. }
  1067. break;
  1068. // Do a boolean not operation, modifying the value of the variable
  1069. case asBC_NOT:
  1070. #if AS_SIZEOF_BOOL == 1
  1071. {
  1072. // Set the value to true if it is equal to 0
  1073. // We need to use volatile here to tell the compiler it cannot
  1074. // change the order of read and write operations on the pointer.
  1075. volatile asBYTE *ptr = (asBYTE*)(l_fp - asBC_SWORDARG0(l_bc));
  1076. asBYTE val = (ptr[0] == 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1077. ptr[0] = val; // The result is stored in the lower byte
  1078. ptr[1] = 0; // Make sure the rest of the DWORD is 0
  1079. ptr[2] = 0;
  1080. ptr[3] = 0;
  1081. }
  1082. #else
  1083. *(l_fp - asBC_SWORDARG0(l_bc)) = (*(l_fp - asBC_SWORDARG0(l_bc)) == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  1084. #endif
  1085. l_bc++;
  1086. break;
  1087. // Push the dword value of a global variable on the stack
  1088. case asBC_PshG4:
  1089. --l_sp;
  1090. // TODO: global: The global var address should be stored in the instruction directly
  1091. *l_sp = *(asDWORD*)regs.globalVarPointers[asBC_WORDARG0(l_bc)];
  1092. l_bc++;
  1093. break;
  1094. // Load the address of a global variable in the register, then
  1095. // copy the value of the global variable into a local variable
  1096. case asBC_LdGRdR4:
  1097. // TODO: global: The global var address should be stored in the instruction directly
  1098. *(void**)&regs.valueRegister = regs.globalVarPointers[asBC_WORDARG1(l_bc)];
  1099. *(l_fp - asBC_SWORDARG0(l_bc)) = **(asDWORD**)&regs.valueRegister;
  1100. l_bc += 2;
  1101. break;
  1102. //----------------
  1103. // path control instructions
  1104. // Begin execution of a script function
  1105. case asBC_CALL:
  1106. {
  1107. int i = asBC_INTARG(l_bc);
  1108. l_bc += 2;
  1109. asASSERT( i >= 0 );
  1110. asASSERT( (i & FUNC_IMPORTED) == 0 );
  1111. // Need to move the values back to the context
  1112. regs.programPointer = l_bc;
  1113. regs.stackPointer = l_sp;
  1114. regs.stackFramePointer = l_fp;
  1115. CallScriptFunction(engine->scriptFunctions[i]);
  1116. // Extract the values from the context again
  1117. l_bc = regs.programPointer;
  1118. l_sp = regs.stackPointer;
  1119. l_fp = regs.stackFramePointer;
  1120. // If status isn't active anymore then we must stop
  1121. if( status != asEXECUTION_ACTIVE )
  1122. return;
  1123. }
  1124. break;
  1125. // Return to the caller, and remove the arguments from the stack
  1126. case asBC_RET:
  1127. {
  1128. if( callStack.GetLength() == 0 )
  1129. {
  1130. status = asEXECUTION_FINISHED;
  1131. return;
  1132. }
  1133. asWORD w = asBC_WORDARG0(l_bc);
  1134. // Read the old framepointer, functionid, and programCounter from the call stack
  1135. PopCallState();
  1136. // Extract the values from the context again
  1137. l_bc = regs.programPointer;
  1138. l_sp = regs.stackPointer;
  1139. l_fp = regs.stackFramePointer;
  1140. // Pop arguments from stack
  1141. l_sp += w;
  1142. }
  1143. break;
  1144. // Jump to a relative position
  1145. case asBC_JMP:
  1146. l_bc += 2 + asBC_INTARG(l_bc);
  1147. break;
  1148. //----------------
  1149. // Conditional jumps
  1150. // Jump to a relative position if the value in the register is 0
  1151. case asBC_JZ:
  1152. if( *(int*)&regs.valueRegister == 0 )
  1153. l_bc += asBC_INTARG(l_bc) + 2;
  1154. else
  1155. l_bc += 2;
  1156. break;
  1157. // Jump to a relative position if the value in the register is not 0
  1158. case asBC_JNZ:
  1159. if( *(int*)&regs.valueRegister != 0 )
  1160. l_bc += asBC_INTARG(l_bc) + 2;
  1161. else
  1162. l_bc += 2;
  1163. break;
  1164. // Jump to a relative position if the value in the register is negative
  1165. case asBC_JS:
  1166. if( *(int*)&regs.valueRegister < 0 )
  1167. l_bc += asBC_INTARG(l_bc) + 2;
  1168. else
  1169. l_bc += 2;
  1170. break;
  1171. // Jump to a relative position if the value in the register it not negative
  1172. case asBC_JNS:
  1173. if( *(int*)&regs.valueRegister >= 0 )
  1174. l_bc += asBC_INTARG(l_bc) + 2;
  1175. else
  1176. l_bc += 2;
  1177. break;
  1178. // Jump to a relative position if the value in the register is greater than 0
  1179. case asBC_JP:
  1180. if( *(int*)&regs.valueRegister > 0 )
  1181. l_bc += asBC_INTARG(l_bc) + 2;
  1182. else
  1183. l_bc += 2;
  1184. break;
  1185. // Jump to a relative position if the value in the register is not greater than 0
  1186. case asBC_JNP:
  1187. if( *(int*)&regs.valueRegister <= 0 )
  1188. l_bc += asBC_INTARG(l_bc) + 2;
  1189. else
  1190. l_bc += 2;
  1191. break;
  1192. //--------------------
  1193. // test instructions
  1194. // If the value in the register is 0, then set the register to 1, else to 0
  1195. case asBC_TZ:
  1196. #if AS_SIZEOF_BOOL == 1
  1197. {
  1198. // Set the value to true if it is equal to 0
  1199. // We need to use volatile here to tell the compiler it cannot
  1200. // change the order of read and write operations on valueRegister.
  1201. volatile int *regPtr = (int*)&regs.valueRegister;
  1202. volatile asBYTE *regBptr = (asBYTE*)&regs.valueRegister;
  1203. asBYTE val = (regPtr[0] == 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1204. regBptr[0] = val; // The result is stored in the lower byte
  1205. regBptr[1] = 0; // Make sure the rest of the register is 0
  1206. regBptr[2] = 0;
  1207. regBptr[3] = 0;
  1208. regBptr[4] = 0;
  1209. regBptr[5] = 0;
  1210. regBptr[6] = 0;
  1211. regBptr[7] = 0;
  1212. }
  1213. #else
  1214. *(int*)&regs.valueRegister = (*(int*)&regs.valueRegister == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  1215. #endif
  1216. l_bc++;
  1217. break;
  1218. // If the value in the register is not 0, then set the register to 1, else to 0
  1219. case asBC_TNZ:
  1220. #if AS_SIZEOF_BOOL == 1
  1221. {
  1222. // Set the value to true if it is not equal to 0
  1223. // We need to use volatile here to tell the compiler it cannot
  1224. // change the order of read and write operations on valueRegister.
  1225. volatile int *regPtr = (int*)&regs.valueRegister;
  1226. volatile asBYTE *regBptr = (asBYTE*)&regs.valueRegister;
  1227. asBYTE val = (regPtr[0] == 0) ? 0 : VALUE_OF_BOOLEAN_TRUE;
  1228. regBptr[0] = val; // The result is stored in the lower byte
  1229. regBptr[1] = 0; // Make sure the rest of the register is 0
  1230. regBptr[2] = 0;
  1231. regBptr[3] = 0;
  1232. regBptr[4] = 0;
  1233. regBptr[5] = 0;
  1234. regBptr[6] = 0;
  1235. regBptr[7] = 0;
  1236. }
  1237. #else
  1238. *(int*)&regs.valueRegister = (*(int*)&regs.valueRegister == 0 ? 0 : VALUE_OF_BOOLEAN_TRUE);
  1239. #endif
  1240. l_bc++;
  1241. break;
  1242. // If the value in the register is negative, then set the register to 1, else to 0
  1243. case asBC_TS:
  1244. #if AS_SIZEOF_BOOL == 1
  1245. {
  1246. // Set the value to true if it is less than 0
  1247. // We need to use volatile here to tell the compiler it cannot
  1248. // change the order of read and write operations on valueRegister.
  1249. volatile int *regPtr = (int*)&regs.valueRegister;
  1250. volatile asBYTE *regBptr = (asBYTE*)&regs.valueRegister;
  1251. asBYTE val = (regPtr[0] < 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1252. regBptr[0] = val; // The result is stored in the lower byte
  1253. regBptr[1] = 0; // Make sure the rest of the register is 0
  1254. regBptr[2] = 0;
  1255. regBptr[3] = 0;
  1256. regBptr[4] = 0;
  1257. regBptr[5] = 0;
  1258. regBptr[6] = 0;
  1259. regBptr[7] = 0;
  1260. }
  1261. #else
  1262. *(int*)&regs.valueRegister = (*(int*)&regs.valueRegister < 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  1263. #endif
  1264. l_bc++;
  1265. break;
  1266. // If the value in the register is not negative, then set the register to 1, else to 0
  1267. case asBC_TNS:
  1268. #if AS_SIZEOF_BOOL == 1
  1269. {
  1270. // Set the value to true if it is not less than 0
  1271. // We need to use volatile here to tell the compiler it cannot
  1272. // change the order of read and write operations on valueRegister.
  1273. volatile int *regPtr = (int*)&regs.valueRegister;
  1274. volatile asBYTE *regBptr = (asBYTE*)&regs.valueRegister;
  1275. asBYTE val = (regPtr[0] >= 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1276. regBptr[0] = val; // The result is stored in the lower byte
  1277. regBptr[1] = 0; // Make sure the rest of the register is 0
  1278. regBptr[2] = 0;
  1279. regBptr[3] = 0;
  1280. regBptr[4] = 0;
  1281. regBptr[5] = 0;
  1282. regBptr[6] = 0;
  1283. regBptr[7] = 0;
  1284. }
  1285. #else
  1286. *(int*)&regs.valueRegister = (*(int*)&regs.valueRegister < 0 ? 0 : VALUE_OF_BOOLEAN_TRUE);
  1287. #endif
  1288. l_bc++;
  1289. break;
  1290. // If the value in the register is greater than 0, then set the register to 1, else to 0
  1291. case asBC_TP:
  1292. #if AS_SIZEOF_BOOL == 1
  1293. {
  1294. // Set the value to true if it is greater than 0
  1295. // We need to use volatile here to tell the compiler it cannot
  1296. // change the order of read and write operations on valueRegister.
  1297. volatile int *regPtr = (int*)&regs.valueRegister;
  1298. volatile asBYTE *regBptr = (asBYTE*)&regs.valueRegister;
  1299. asBYTE val = (regPtr[0] > 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1300. regBptr[0] = val; // The result is stored in the lower byte
  1301. regBptr[1] = 0; // Make sure the rest of the register is 0
  1302. regBptr[2] = 0;
  1303. regBptr[3] = 0;
  1304. regBptr[4] = 0;
  1305. regBptr[5] = 0;
  1306. regBptr[6] = 0;
  1307. regBptr[7] = 0;
  1308. }
  1309. #else
  1310. *(int*)&regs.valueRegister = (*(int*)&regs.valueRegister > 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  1311. #endif
  1312. l_bc++;
  1313. break;
  1314. // If the value in the register is not greater than 0, then set the register to 1, else to 0
  1315. case asBC_TNP:
  1316. #if AS_SIZEOF_BOOL == 1
  1317. {
  1318. // Set the value to true if it is not greater than 0
  1319. // We need to use volatile here to tell the compiler it cannot
  1320. // change the order of read and write operations on valueRegister.
  1321. volatile int *regPtr = (int*)&regs.valueRegister;
  1322. volatile asBYTE *regBptr = (asBYTE*)&regs.valueRegister;
  1323. asBYTE val = (regPtr[0] <= 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1324. regBptr[0] = val; // The result is stored in the lower byte
  1325. regBptr[1] = 0; // Make sure the rest of the register is 0
  1326. regBptr[2] = 0;
  1327. regBptr[3] = 0;
  1328. regBptr[4] = 0;
  1329. regBptr[5] = 0;
  1330. regBptr[6] = 0;
  1331. regBptr[7] = 0;
  1332. }
  1333. #else
  1334. *(int*)&regs.valueRegister = (*(int*)&regs.valueRegister > 0 ? 0 : VALUE_OF_BOOLEAN_TRUE);
  1335. #endif
  1336. l_bc++;
  1337. break;
  1338. //--------------------
  1339. // negate value
  1340. // Negate the integer value in the variable
  1341. case asBC_NEGi:
  1342. *(l_fp - asBC_SWORDARG0(l_bc)) = asDWORD(-int(*(l_fp - asBC_SWORDARG0(l_bc))));
  1343. l_bc++;
  1344. break;
  1345. // Negate the float value in the variable
  1346. case asBC_NEGf:
  1347. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = -*(float*)(l_fp - asBC_SWORDARG0(l_bc));
  1348. l_bc++;
  1349. break;
  1350. // Negate the double value in the variable
  1351. case asBC_NEGd:
  1352. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = -*(double*)(l_fp - asBC_SWORDARG0(l_bc));
  1353. l_bc++;
  1354. break;
  1355. //-------------------------
  1356. // Increment value pointed to by address in register
  1357. // Increment the short value pointed to by the register
  1358. case asBC_INCi16:
  1359. (**(short**)&regs.valueRegister)++;
  1360. l_bc++;
  1361. break;
  1362. // Increment the byte value pointed to by the register
  1363. case asBC_INCi8:
  1364. (**(char**)&regs.valueRegister)++;
  1365. l_bc++;
  1366. break;
  1367. // Decrement the short value pointed to by the register
  1368. case asBC_DECi16:
  1369. (**(short**)&regs.valueRegister)--;
  1370. l_bc++;
  1371. break;
  1372. // Decrement the byte value pointed to by the register
  1373. case asBC_DECi8:
  1374. (**(char**)&regs.valueRegister)--;
  1375. l_bc++;
  1376. break;
  1377. // Increment the integer value pointed to by the register
  1378. case asBC_INCi:
  1379. ++(**(int**)&regs.valueRegister);
  1380. l_bc++;
  1381. break;
  1382. // Decrement the integer value pointed to by the register
  1383. case asBC_DECi:
  1384. --(**(int**)&regs.valueRegister);
  1385. l_bc++;
  1386. break;
  1387. // Increment the float value pointed to by the register
  1388. case asBC_INCf:
  1389. ++(**(float**)&regs.valueRegister);
  1390. l_bc++;
  1391. break;
  1392. // Decrement the float value pointed to by the register
  1393. case asBC_DECf:
  1394. --(**(float**)&regs.valueRegister);
  1395. l_bc++;
  1396. break;
  1397. // Increment the double value pointed to by the register
  1398. case asBC_INCd:
  1399. ++(**(double**)&regs.valueRegister);
  1400. l_bc++;
  1401. break;
  1402. // Decrement the double value pointed to by the register
  1403. case asBC_DECd:
  1404. --(**(double**)&regs.valueRegister);
  1405. l_bc++;
  1406. break;
  1407. // Increment the local integer variable
  1408. case asBC_IncVi:
  1409. (*(int*)(l_fp - asBC_SWORDARG0(l_bc)))++;
  1410. l_bc++;
  1411. break;
  1412. // Decrement the local integer variable
  1413. case asBC_DecVi:
  1414. (*(int*)(l_fp - asBC_SWORDARG0(l_bc)))--;
  1415. l_bc++;
  1416. break;
  1417. //--------------------
  1418. // bits instructions
  1419. // Do a bitwise not on the value in the variable
  1420. case asBC_BNOT:
  1421. *(l_fp - asBC_SWORDARG0(l_bc)) = ~*(l_fp - asBC_SWORDARG0(l_bc));
  1422. l_bc++;
  1423. break;
  1424. // Do a bitwise and of two variables and store the result in a third variable
  1425. case asBC_BAND:
  1426. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) & *(l_fp - asBC_SWORDARG2(l_bc));
  1427. l_bc += 2;
  1428. break;
  1429. // Do a bitwise or of two variables and store the result in a third variable
  1430. case asBC_BOR:
  1431. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) | *(l_fp - asBC_SWORDARG2(l_bc));
  1432. l_bc += 2;
  1433. break;
  1434. // Do a bitwise xor of two variables and store the result in a third variable
  1435. case asBC_BXOR:
  1436. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) ^ *(l_fp - asBC_SWORDARG2(l_bc));
  1437. l_bc += 2;
  1438. break;
  1439. // Do a logical shift left of two variables and store the result in a third variable
  1440. case asBC_BSLL:
  1441. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) << *(l_fp - asBC_SWORDARG2(l_bc));
  1442. l_bc += 2;
  1443. break;
  1444. // Do a logical shift right of two variables and store the result in a third variable
  1445. case asBC_BSRL:
  1446. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) >> *(l_fp - asBC_SWORDARG2(l_bc));
  1447. l_bc += 2;
  1448. break;
  1449. // Do an arithmetic shift right of two variables and store the result in a third variable
  1450. case asBC_BSRA:
  1451. *(l_fp - asBC_SWORDARG0(l_bc)) = int(*(l_fp - asBC_SWORDARG1(l_bc))) >> *(l_fp - asBC_SWORDARG2(l_bc));
  1452. l_bc += 2;
  1453. break;
  1454. case asBC_COPY:
  1455. {
  1456. void *d = (void*)*(size_t*)l_sp; l_sp += AS_PTR_SIZE;
  1457. void *s = (void*)*(size_t*)l_sp;
  1458. if( s == 0 || d == 0 )
  1459. {
  1460. // Need to move the values back to the context
  1461. regs.programPointer = l_bc;
  1462. regs.stackPointer = l_sp;
  1463. regs.stackFramePointer = l_fp;
  1464. // Raise exception
  1465. SetInternalException(TXT_NULL_POINTER_ACCESS);
  1466. return;
  1467. }
  1468. memcpy(d, s, asBC_WORDARG0(l_bc)*4);
  1469. // replace the pointer on the stack with the lvalue
  1470. *(size_t**)l_sp = (size_t*)d;
  1471. }
  1472. l_bc++;
  1473. break;
  1474. case asBC_PshC8:
  1475. l_sp -= 2;
  1476. *(asQWORD*)l_sp = asBC_QWORDARG(l_bc);
  1477. l_bc += 3;
  1478. break;
  1479. case asBC_RDS8:
  1480. #ifndef AS_64BIT_PTR
  1481. *(asQWORD*)(l_sp-1) = *(asQWORD*)*(size_t*)l_sp;
  1482. --l_sp;
  1483. #else
  1484. *(asQWORD*)l_sp = *(asQWORD*)*(size_t*)l_sp;
  1485. #endif
  1486. l_bc++;
  1487. break;
  1488. case asBC_SWAP8:
  1489. {
  1490. asQWORD q = *(asQWORD*)l_sp;
  1491. *(asQWORD*)l_sp = *(asQWORD*)(l_sp+2);
  1492. *(asQWORD*)(l_sp+2) = q;
  1493. l_bc++;
  1494. }
  1495. break;
  1496. //----------------------------
  1497. // Comparisons
  1498. case asBC_CMPd:
  1499. {
  1500. double dbl = *(double*)(l_fp - asBC_SWORDARG0(l_bc)) - *(double*)(l_fp - asBC_SWORDARG1(l_bc));
  1501. if( dbl == 0 ) *(int*)&regs.valueRegister = 0;
  1502. else if( dbl < 0 ) *(int*)&regs.valueRegister = -1;
  1503. else *(int*)&regs.valueRegister = 1;
  1504. l_bc += 2;
  1505. }
  1506. break;
  1507. case asBC_CMPu:
  1508. {
  1509. asDWORD d = *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  1510. asDWORD d2 = *(asDWORD*)(l_fp - asBC_SWORDARG1(l_bc));
  1511. if( d == d2 ) *(int*)&regs.valueRegister = 0;
  1512. else if( d < d2 ) *(int*)&regs.valueRegister = -1;
  1513. else *(int*)&regs.valueRegister = 1;
  1514. l_bc += 2;
  1515. }
  1516. break;
  1517. case asBC_CMPf:
  1518. {
  1519. float f = *(float*)(l_fp - asBC_SWORDARG0(l_bc)) - *(float*)(l_fp - asBC_SWORDARG1(l_bc));
  1520. if( f == 0 ) *(int*)&regs.valueRegister = 0;
  1521. else if( f < 0 ) *(int*)&regs.valueRegister = -1;
  1522. else *(int*)&regs.valueRegister = 1;
  1523. l_bc += 2;
  1524. }
  1525. break;
  1526. case asBC_CMPi:
  1527. {
  1528. int i = *(int*)(l_fp - asBC_SWORDARG0(l_bc)) - *(int*)(l_fp - asBC_SWORDARG1(l_bc));
  1529. if( i == 0 ) *(int*)&regs.valueRegister = 0;
  1530. else if( i < 0 ) *(int*)&regs.valueRegister = -1;
  1531. else *(int*)&regs.valueRegister = 1;
  1532. l_bc += 2;
  1533. }
  1534. break;
  1535. //----------------------------
  1536. // Comparisons with constant value
  1537. case asBC_CMPIi:
  1538. {
  1539. int i = *(int*)(l_fp - asBC_SWORDARG0(l_bc)) - asBC_INTARG(l_bc);
  1540. if( i == 0 ) *(int*)&regs.valueRegister = 0;
  1541. else if( i < 0 ) *(int*)&regs.valueRegister = -1;
  1542. else *(int*)&regs.valueRegister = 1;
  1543. l_bc += 2;
  1544. }
  1545. break;
  1546. case asBC_CMPIf:
  1547. {
  1548. float f = *(float*)(l_fp - asBC_SWORDARG0(l_bc)) - asBC_FLOATARG(l_bc);
  1549. if( f == 0 ) *(int*)&regs.valueRegister = 0;
  1550. else if( f < 0 ) *(int*)&regs.valueRegister = -1;
  1551. else *(int*)&regs.valueRegister = 1;
  1552. l_bc += 2;
  1553. }
  1554. break;
  1555. case asBC_CMPIu:
  1556. {
  1557. asDWORD d1 = *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  1558. asDWORD d2 = asBC_DWORDARG(l_bc);
  1559. if( d1 == d2 ) *(int*)&regs.valueRegister = 0;
  1560. else if( d1 < d2 ) *(int*)&regs.valueRegister = -1;
  1561. else *(int*)&regs.valueRegister = 1;
  1562. l_bc += 2;
  1563. }
  1564. break;
  1565. case asBC_JMPP:
  1566. l_bc += 1 + (*(int*)(l_fp - asBC_SWORDARG0(l_bc)))*2;
  1567. break;
  1568. case asBC_PopRPtr:
  1569. *(asPTRWORD*)&regs.valueRegister = *(asPTRWORD*)l_sp;
  1570. l_sp += AS_PTR_SIZE;
  1571. l_bc++;
  1572. break;
  1573. case asBC_PshRPtr:
  1574. l_sp -= AS_PTR_SIZE;
  1575. *(asPTRWORD*)l_sp = *(asPTRWORD*)&regs.valueRegister;
  1576. l_bc++;
  1577. break;
  1578. case asBC_STR:
  1579. {
  1580. // Get the string id from the argument
  1581. asWORD w = asBC_WORDARG0(l_bc);
  1582. // Push the string pointer on the stack
  1583. const asCString &b = engine->GetConstantString(w);
  1584. l_sp -= AS_PTR_SIZE;
  1585. *(asPTRWORD*)l_sp = (asPTRWORD)(size_t)b.AddressOf();
  1586. // Push the string length on the stack
  1587. --l_sp;
  1588. *l_sp = (asDWORD)b.GetLength();
  1589. l_bc++;
  1590. }
  1591. break;
  1592. case asBC_CALLSYS:
  1593. {
  1594. // Get function ID from the argument
  1595. int i = asBC_INTARG(l_bc);
  1596. // Need to move the values back to the context as the called functions
  1597. // may use the debug interface to inspect the registers
  1598. regs.programPointer = l_bc;
  1599. regs.stackPointer = l_sp;
  1600. regs.stackFramePointer = l_fp;
  1601. l_sp += CallSystemFunction(i, this, 0);
  1602. // Update the program position after the call so that line number is correct
  1603. l_bc += 2;
  1604. if( regs.doProcessSuspend )
  1605. {
  1606. // Should the execution be suspended?
  1607. if( doSuspend )
  1608. {
  1609. regs.programPointer = l_bc;
  1610. regs.stackPointer = l_sp;
  1611. regs.stackFramePointer = l_fp;
  1612. status = asEXECUTION_SUSPENDED;
  1613. return;
  1614. }
  1615. // An exception might have been raised
  1616. if( status != asEXECUTION_ACTIVE )
  1617. {
  1618. regs.programPointer = l_bc;
  1619. regs.stackPointer = l_sp;
  1620. regs.stackFramePointer = l_fp;
  1621. return;
  1622. }
  1623. }
  1624. }
  1625. break;
  1626. case asBC_CALLBND:
  1627. {
  1628. // Get the function ID from the stack
  1629. int i = asBC_INTARG(l_bc);
  1630. l_bc += 2;
  1631. asASSERT( i >= 0 );
  1632. asASSERT( i & FUNC_IMPORTED