as_context.cpp 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 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. [email protected]
  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. currentFunction = 0;
  130. regs.objectRegister = 0;
  131. initialFunction = 0;
  132. lineCallback = false;
  133. exceptionCallback = false;
  134. regs.doProcessSuspend = false;
  135. doSuspend = false;
  136. userData = 0;
  137. }
  138. asCContext::~asCContext()
  139. {
  140. DetachEngine();
  141. }
  142. int asCContext::AddRef() const
  143. {
  144. return refCount.atomicInc();
  145. }
  146. int asCContext::Release() const
  147. {
  148. int r = refCount.atomicDec();
  149. if( r == 0 )
  150. {
  151. asDELETE(const_cast<asCContext*>(this),asCContext);
  152. return 0;
  153. }
  154. return r;
  155. }
  156. void asCContext::DetachEngine()
  157. {
  158. if( engine == 0 ) return;
  159. // Abort any execution
  160. Abort();
  161. // Free all resources
  162. Unprepare();
  163. // Free the stack blocks
  164. for( asUINT n = 0; n < stackBlocks.GetLength(); n++ )
  165. {
  166. if( stackBlocks[n] )
  167. {
  168. asDELETEARRAY(stackBlocks[n]);
  169. }
  170. }
  171. stackBlocks.SetLength(0);
  172. stackBlockSize = 0;
  173. // Clean the user data
  174. if( userData && engine->cleanContextFunc )
  175. engine->cleanContextFunc(this);
  176. // Clear engine pointer
  177. if( holdEngineRef )
  178. engine->Release();
  179. engine = 0;
  180. }
  181. asIScriptEngine *asCContext::GetEngine() const
  182. {
  183. return engine;
  184. }
  185. void *asCContext::SetUserData(void *data)
  186. {
  187. void *oldData = userData;
  188. userData = data;
  189. return oldData;
  190. }
  191. void *asCContext::GetUserData() const
  192. {
  193. return userData;
  194. }
  195. int asCContext::Prepare(int funcID)
  196. {
  197. if( status == asEXECUTION_ACTIVE || status == asEXECUTION_SUSPENDED )
  198. return asCONTEXT_ACTIVE;
  199. // Clean the stack if not done before
  200. if( status != asEXECUTION_FINISHED && status != asEXECUTION_UNINITIALIZED )
  201. CleanStack();
  202. // Release the returned object (if any)
  203. CleanReturnObject();
  204. if( funcID == -1 )
  205. {
  206. // Use the previously prepared function
  207. if( initialFunction == 0 )
  208. return asNO_FUNCTION;
  209. currentFunction = initialFunction;
  210. }
  211. else if( initialFunction && initialFunction->id == funcID )
  212. {
  213. currentFunction = initialFunction;
  214. }
  215. else
  216. {
  217. // Check engine pointer
  218. asASSERT( engine );
  219. if( initialFunction )
  220. initialFunction->Release();
  221. initialFunction = engine->GetScriptFunction(funcID);
  222. if( initialFunction == 0 )
  223. return asNO_FUNCTION;
  224. initialFunction->AddRef();
  225. currentFunction = initialFunction;
  226. // Determine the minimum stack size needed
  227. // TODO: optimize: GetSpaceNeededForArguments() should be precomputed
  228. int stackSize = currentFunction->GetSpaceNeededForArguments() + currentFunction->stackNeeded + RESERVE_STACK;
  229. stackSize = stackSize > engine->initialContextStackSize ? stackSize : engine->initialContextStackSize;
  230. if( stackSize > stackBlockSize )
  231. {
  232. // Free old stack blocks so new ones can be allocted
  233. for( asUINT n = 0; n < stackBlocks.GetLength(); n++ )
  234. if( stackBlocks[n] )
  235. {
  236. asDELETEARRAY(stackBlocks[n]);
  237. }
  238. stackBlocks.SetLength(0);
  239. stackBlockSize = stackSize;
  240. asDWORD *stack = asNEWARRAY(asDWORD,stackBlockSize);
  241. stackBlocks.PushLast(stack);
  242. }
  243. // Reserve space for the arguments and return value
  244. returnValueSize = currentFunction->GetSpaceNeededForReturnValue();
  245. // TODO: optimize: GetSpaceNeededForArguments() should be precomputed
  246. argumentsSize = currentFunction->GetSpaceNeededForArguments() + (currentFunction->objectType ? AS_PTR_SIZE : 0);
  247. }
  248. // Reset state
  249. // Most of the time the previous state will be asEXECUTION_FINISHED, in which case the values are already initialized
  250. if( status != asEXECUTION_FINISHED )
  251. {
  252. exceptionLine = -1;
  253. exceptionFunction = 0;
  254. isCallingSystemFunction = false;
  255. doAbort = false;
  256. doSuspend = false;
  257. regs.doProcessSuspend = lineCallback;
  258. externalSuspendRequest = false;
  259. stackIndex = 0;
  260. }
  261. status = asEXECUTION_PREPARED;
  262. // Reserve space for the arguments and return value
  263. regs.stackFramePointer = stackBlocks[0] + stackBlockSize - argumentsSize;
  264. regs.stackPointer = regs.stackFramePointer;
  265. // Set arguments to 0
  266. memset(regs.stackPointer, 0, 4*argumentsSize);
  267. if( currentFunction->funcType == asFUNC_SCRIPT )
  268. {
  269. regs.programPointer = currentFunction->byteCode.AddressOf();
  270. // Set all object variables to 0
  271. for( asUINT n = 0; n < currentFunction->objVariablePos.GetLength(); n++ )
  272. {
  273. if( !currentFunction->objVariableIsOnHeap[n] ) continue;
  274. int pos = currentFunction->objVariablePos[n];
  275. *(size_t*)&regs.stackFramePointer[-pos] = 0;
  276. }
  277. }
  278. else
  279. regs.programPointer = 0;
  280. return asSUCCESS;
  281. }
  282. // Free all resources
  283. int asCContext::Unprepare()
  284. {
  285. if( status == asEXECUTION_ACTIVE || status == asEXECUTION_SUSPENDED )
  286. return asCONTEXT_ACTIVE;
  287. // Only clean the stack if the context was prepared but not executed until the end
  288. if( status != asEXECUTION_UNINITIALIZED &&
  289. status != asEXECUTION_FINISHED )
  290. CleanStack();
  291. // Release the returned object (if any)
  292. CleanReturnObject();
  293. // Release the initial function
  294. if( initialFunction )
  295. initialFunction->Release();
  296. // Clear function pointers
  297. initialFunction = 0;
  298. currentFunction = 0;
  299. exceptionFunction = 0;
  300. regs.programPointer = 0;
  301. // Reset status
  302. status = asEXECUTION_UNINITIALIZED;
  303. regs.stackFramePointer = 0;
  304. regs.stackPointer = 0;
  305. stackIndex = 0;
  306. return 0;
  307. }
  308. asBYTE asCContext::GetReturnByte()
  309. {
  310. if( status != asEXECUTION_FINISHED ) return 0;
  311. asCDataType *dt = &initialFunction->returnType;
  312. if( dt->IsObject() || dt->IsReference() ) return 0;
  313. return *(asBYTE*)&regs.valueRegister;
  314. }
  315. asWORD asCContext::GetReturnWord()
  316. {
  317. if( status != asEXECUTION_FINISHED ) return 0;
  318. asCDataType *dt = &initialFunction->returnType;
  319. if( dt->IsObject() || dt->IsReference() ) return 0;
  320. return *(asWORD*)&regs.valueRegister;
  321. }
  322. asDWORD asCContext::GetReturnDWord()
  323. {
  324. if( status != asEXECUTION_FINISHED ) return 0;
  325. asCDataType *dt = &initialFunction->returnType;
  326. if( dt->IsObject() || dt->IsReference() ) return 0;
  327. return *(asDWORD*)&regs.valueRegister;
  328. }
  329. asQWORD asCContext::GetReturnQWord()
  330. {
  331. if( status != asEXECUTION_FINISHED ) return 0;
  332. asCDataType *dt = &initialFunction->returnType;
  333. if( dt->IsObject() || dt->IsReference() ) return 0;
  334. return regs.valueRegister;
  335. }
  336. float asCContext::GetReturnFloat()
  337. {
  338. if( status != asEXECUTION_FINISHED ) return 0;
  339. asCDataType *dt = &initialFunction->returnType;
  340. if( dt->IsObject() || dt->IsReference() ) return 0;
  341. return *(float*)&regs.valueRegister;
  342. }
  343. double asCContext::GetReturnDouble()
  344. {
  345. if( status != asEXECUTION_FINISHED ) return 0;
  346. asCDataType *dt = &initialFunction->returnType;
  347. if( dt->IsObject() || dt->IsReference() ) return 0;
  348. return *(double*)&regs.valueRegister;
  349. }
  350. void *asCContext::GetReturnAddress()
  351. {
  352. if( status != asEXECUTION_FINISHED ) return 0;
  353. asCDataType *dt = &initialFunction->returnType;
  354. if( dt->IsReference() )
  355. return *(void**)&regs.valueRegister;
  356. else if( dt->IsObject() )
  357. return regs.objectRegister;
  358. return 0;
  359. }
  360. void *asCContext::GetReturnObject()
  361. {
  362. if( status != asEXECUTION_FINISHED ) return 0;
  363. asCDataType *dt = &initialFunction->returnType;
  364. if( !dt->IsObject() ) return 0;
  365. if( dt->IsReference() )
  366. return *(void**)(size_t)regs.valueRegister;
  367. else
  368. return regs.objectRegister;
  369. }
  370. void *asCContext::GetAddressOfReturnValue()
  371. {
  372. if( status != asEXECUTION_FINISHED ) return 0;
  373. asCDataType *dt = &initialFunction->returnType;
  374. // An object is stored in the objectRegister
  375. if( !dt->IsReference() && dt->IsObject() )
  376. {
  377. // Need to dereference objects
  378. if( !dt->IsObjectHandle() )
  379. return *(void**)&regs.objectRegister;
  380. return &regs.objectRegister;
  381. }
  382. // Primitives and references are stored in valueRegister
  383. return &regs.valueRegister;
  384. }
  385. int asCContext::SetObject(void *obj)
  386. {
  387. if( status != asEXECUTION_PREPARED )
  388. return asCONTEXT_NOT_PREPARED;
  389. if( !initialFunction->objectType )
  390. {
  391. status = asEXECUTION_ERROR;
  392. return asERROR;
  393. }
  394. *(size_t*)&regs.stackFramePointer[0] = (size_t)obj;
  395. return 0;
  396. }
  397. int asCContext::SetArgByte(asUINT arg, asBYTE value)
  398. {
  399. if( status != asEXECUTION_PREPARED )
  400. return asCONTEXT_NOT_PREPARED;
  401. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  402. {
  403. status = asEXECUTION_ERROR;
  404. return asINVALID_ARG;
  405. }
  406. // Verify the type of the argument
  407. asCDataType *dt = &initialFunction->parameterTypes[arg];
  408. if( dt->IsObject() || dt->IsReference() )
  409. {
  410. status = asEXECUTION_ERROR;
  411. return asINVALID_TYPE;
  412. }
  413. if( dt->GetSizeInMemoryBytes() != 1 )
  414. {
  415. status = asEXECUTION_ERROR;
  416. return asINVALID_TYPE;
  417. }
  418. // Determine the position of the argument
  419. int offset = 0;
  420. if( initialFunction->objectType )
  421. offset += AS_PTR_SIZE;
  422. for( asUINT n = 0; n < arg; n++ )
  423. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  424. // Set the value
  425. *(asBYTE*)&regs.stackFramePointer[offset] = value;
  426. return 0;
  427. }
  428. int asCContext::SetArgWord(asUINT arg, asWORD value)
  429. {
  430. if( status != asEXECUTION_PREPARED )
  431. return asCONTEXT_NOT_PREPARED;
  432. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  433. {
  434. status = asEXECUTION_ERROR;
  435. return asINVALID_ARG;
  436. }
  437. // Verify the type of the argument
  438. asCDataType *dt = &initialFunction->parameterTypes[arg];
  439. if( dt->IsObject() || dt->IsReference() )
  440. {
  441. status = asEXECUTION_ERROR;
  442. return asINVALID_TYPE;
  443. }
  444. if( dt->GetSizeInMemoryBytes() != 2 )
  445. {
  446. status = asEXECUTION_ERROR;
  447. return asINVALID_TYPE;
  448. }
  449. // Determine the position of the argument
  450. int offset = 0;
  451. if( initialFunction->objectType )
  452. offset += AS_PTR_SIZE;
  453. for( asUINT n = 0; n < arg; n++ )
  454. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  455. // Set the value
  456. *(asWORD*)&regs.stackFramePointer[offset] = value;
  457. return 0;
  458. }
  459. int asCContext::SetArgDWord(asUINT arg, asDWORD value)
  460. {
  461. if( status != asEXECUTION_PREPARED )
  462. return asCONTEXT_NOT_PREPARED;
  463. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  464. {
  465. status = asEXECUTION_ERROR;
  466. return asINVALID_ARG;
  467. }
  468. // Verify the type of the argument
  469. asCDataType *dt = &initialFunction->parameterTypes[arg];
  470. if( dt->IsObject() || dt->IsReference() )
  471. {
  472. status = asEXECUTION_ERROR;
  473. return asINVALID_TYPE;
  474. }
  475. if( dt->GetSizeInMemoryBytes() != 4 )
  476. {
  477. status = asEXECUTION_ERROR;
  478. return asINVALID_TYPE;
  479. }
  480. // Determine the position of the argument
  481. int offset = 0;
  482. if( initialFunction->objectType )
  483. offset += AS_PTR_SIZE;
  484. for( asUINT n = 0; n < arg; n++ )
  485. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  486. // Set the value
  487. *(asDWORD*)&regs.stackFramePointer[offset] = value;
  488. return 0;
  489. }
  490. int asCContext::SetArgQWord(asUINT arg, asQWORD value)
  491. {
  492. if( status != asEXECUTION_PREPARED )
  493. return asCONTEXT_NOT_PREPARED;
  494. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  495. {
  496. status = asEXECUTION_ERROR;
  497. return asINVALID_ARG;
  498. }
  499. // Verify the type of the argument
  500. asCDataType *dt = &initialFunction->parameterTypes[arg];
  501. if( dt->IsObject() || dt->IsReference() )
  502. {
  503. status = asEXECUTION_ERROR;
  504. return asINVALID_TYPE;
  505. }
  506. if( dt->GetSizeOnStackDWords() != 2 )
  507. {
  508. status = asEXECUTION_ERROR;
  509. return asINVALID_TYPE;
  510. }
  511. // Determine the position of the argument
  512. int offset = 0;
  513. if( initialFunction->objectType )
  514. offset += AS_PTR_SIZE;
  515. for( asUINT n = 0; n < arg; n++ )
  516. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  517. // Set the value
  518. *(asQWORD*)(&regs.stackFramePointer[offset]) = value;
  519. return 0;
  520. }
  521. int asCContext::SetArgFloat(asUINT arg, float value)
  522. {
  523. if( status != asEXECUTION_PREPARED )
  524. return asCONTEXT_NOT_PREPARED;
  525. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  526. {
  527. status = asEXECUTION_ERROR;
  528. return asINVALID_ARG;
  529. }
  530. // Verify the type of the argument
  531. asCDataType *dt = &initialFunction->parameterTypes[arg];
  532. if( dt->IsObject() || dt->IsReference() )
  533. {
  534. status = asEXECUTION_ERROR;
  535. return asINVALID_TYPE;
  536. }
  537. if( dt->GetSizeOnStackDWords() != 1 )
  538. {
  539. status = asEXECUTION_ERROR;
  540. return asINVALID_TYPE;
  541. }
  542. // Determine the position of the argument
  543. int offset = 0;
  544. if( initialFunction->objectType )
  545. offset += AS_PTR_SIZE;
  546. for( asUINT n = 0; n < arg; n++ )
  547. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  548. // Set the value
  549. *(float*)(&regs.stackFramePointer[offset]) = value;
  550. return 0;
  551. }
  552. int asCContext::SetArgDouble(asUINT arg, double value)
  553. {
  554. if( status != asEXECUTION_PREPARED )
  555. return asCONTEXT_NOT_PREPARED;
  556. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  557. {
  558. status = asEXECUTION_ERROR;
  559. return asINVALID_ARG;
  560. }
  561. // Verify the type of the argument
  562. asCDataType *dt = &initialFunction->parameterTypes[arg];
  563. if( dt->IsObject() || dt->IsReference() )
  564. {
  565. status = asEXECUTION_ERROR;
  566. return asINVALID_TYPE;
  567. }
  568. if( dt->GetSizeOnStackDWords() != 2 )
  569. {
  570. status = asEXECUTION_ERROR;
  571. return asINVALID_TYPE;
  572. }
  573. // Determine the position of the argument
  574. int offset = 0;
  575. if( initialFunction->objectType )
  576. offset += AS_PTR_SIZE;
  577. for( asUINT n = 0; n < arg; n++ )
  578. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  579. // Set the value
  580. *(double*)(&regs.stackFramePointer[offset]) = value;
  581. return 0;
  582. }
  583. int asCContext::SetArgAddress(asUINT arg, void *value)
  584. {
  585. if( status != asEXECUTION_PREPARED )
  586. return asCONTEXT_NOT_PREPARED;
  587. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  588. {
  589. status = asEXECUTION_ERROR;
  590. return asINVALID_ARG;
  591. }
  592. // Verify the type of the argument
  593. asCDataType *dt = &initialFunction->parameterTypes[arg];
  594. if( !dt->IsReference() && !dt->IsObjectHandle() )
  595. {
  596. status = asEXECUTION_ERROR;
  597. return asINVALID_TYPE;
  598. }
  599. // Determine the position of the argument
  600. int offset = 0;
  601. if( initialFunction->objectType )
  602. offset += AS_PTR_SIZE;
  603. for( asUINT n = 0; n < arg; n++ )
  604. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  605. // Set the value
  606. *(size_t*)(&regs.stackFramePointer[offset]) = (size_t)value;
  607. return 0;
  608. }
  609. int asCContext::SetArgObject(asUINT arg, void *obj)
  610. {
  611. if( status != asEXECUTION_PREPARED )
  612. return asCONTEXT_NOT_PREPARED;
  613. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  614. {
  615. status = asEXECUTION_ERROR;
  616. return asINVALID_ARG;
  617. }
  618. // Verify the type of the argument
  619. asCDataType *dt = &initialFunction->parameterTypes[arg];
  620. if( !dt->IsObject() )
  621. {
  622. status = asEXECUTION_ERROR;
  623. return asINVALID_TYPE;
  624. }
  625. // If the object should be sent by value we must make a copy of it
  626. if( !dt->IsReference() )
  627. {
  628. if( dt->IsObjectHandle() )
  629. {
  630. // Increase the reference counter
  631. asSTypeBehaviour *beh = &dt->GetObjectType()->beh;
  632. if( obj && beh->addref )
  633. engine->CallObjectMethod(obj, beh->addref);
  634. }
  635. else
  636. {
  637. obj = engine->CreateScriptObjectCopy(obj, engine->GetTypeIdFromDataType(*dt));
  638. }
  639. }
  640. // Determine the position of the argument
  641. int offset = 0;
  642. if( initialFunction->objectType )
  643. offset += AS_PTR_SIZE;
  644. for( asUINT n = 0; n < arg; n++ )
  645. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  646. // Set the value
  647. *(size_t*)(&regs.stackFramePointer[offset]) = (size_t)obj;
  648. return 0;
  649. }
  650. // TODO: Instead of GetAddressOfArg, maybe we need a SetArgValue(int arg, void *value, bool takeOwnership) instead.
  651. // interface
  652. void *asCContext::GetAddressOfArg(asUINT arg)
  653. {
  654. if( status != asEXECUTION_PREPARED )
  655. return 0;
  656. if( arg >= (unsigned)initialFunction->parameterTypes.GetLength() )
  657. return 0;
  658. // Determine the position of the argument
  659. int offset = 0;
  660. if( initialFunction->objectType )
  661. offset += AS_PTR_SIZE;
  662. for( asUINT n = 0; n < arg; n++ )
  663. offset += initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  664. // We should return the address of the location where the argument value will be placed
  665. // All registered types are always sent by reference, even if
  666. // the function is declared to receive the argument by value.
  667. return &regs.stackFramePointer[offset];
  668. }
  669. int asCContext::Abort()
  670. {
  671. // TODO: multithread: Make thread safe
  672. if( engine == 0 ) return asERROR;
  673. if( status == asEXECUTION_SUSPENDED )
  674. status = asEXECUTION_ABORTED;
  675. doSuspend = true;
  676. regs.doProcessSuspend = true;
  677. externalSuspendRequest = true;
  678. doAbort = true;
  679. return 0;
  680. }
  681. // interface
  682. int asCContext::Suspend()
  683. {
  684. // This function just sets some internal flags and is safe
  685. // to call from a secondary thread, even if the library has
  686. // been built without multi-thread support.
  687. if( engine == 0 ) return asERROR;
  688. doSuspend = true;
  689. externalSuspendRequest = true;
  690. regs.doProcessSuspend = true;
  691. return 0;
  692. }
  693. // interface
  694. int asCContext::Execute()
  695. {
  696. asASSERT( engine != 0 );
  697. if( status != asEXECUTION_SUSPENDED && status != asEXECUTION_PREPARED )
  698. return asERROR;
  699. status = asEXECUTION_ACTIVE;
  700. asPushActiveContext((asIScriptContext *)this);
  701. if( regs.programPointer == 0 )
  702. {
  703. if( currentFunction->funcType == asFUNC_VIRTUAL ||
  704. currentFunction->funcType == asFUNC_INTERFACE )
  705. {
  706. // The currentFunction is a virtual method
  707. // Determine the true function from the object
  708. asCScriptObject *obj = *(asCScriptObject**)(size_t*)regs.stackFramePointer;
  709. if( obj == 0 )
  710. {
  711. SetInternalException(TXT_NULL_POINTER_ACCESS);
  712. }
  713. else
  714. {
  715. asCObjectType *objType = obj->objType;
  716. asCScriptFunction *realFunc = 0;
  717. if( currentFunction->funcType == asFUNC_VIRTUAL )
  718. {
  719. if( objType->virtualFunctionTable.GetLength() > (asUINT)currentFunction->vfTableIdx )
  720. {
  721. realFunc = objType->virtualFunctionTable[currentFunction->vfTableIdx];
  722. }
  723. }
  724. else
  725. {
  726. // Search the object type for a function that matches the interface function
  727. for( asUINT n = 0; n < objType->methods.GetLength(); n++ )
  728. {
  729. asCScriptFunction *f2 = engine->scriptFunctions[objType->methods[n]];
  730. if( f2->signatureId == currentFunction->signatureId )
  731. {
  732. if( f2->funcType == asFUNC_VIRTUAL )
  733. realFunc = objType->virtualFunctionTable[f2->vfTableIdx];
  734. else
  735. realFunc = f2;
  736. break;
  737. }
  738. }
  739. }
  740. if( realFunc )
  741. {
  742. if( realFunc->signatureId != currentFunction->signatureId )
  743. {
  744. SetInternalException(TXT_NULL_POINTER_ACCESS);
  745. }
  746. else
  747. {
  748. currentFunction = realFunc;
  749. regs.programPointer = currentFunction->byteCode.AddressOf();
  750. // Set the local objects to 0
  751. for( asUINT n = 0; n < currentFunction->objVariablePos.GetLength(); n++ )
  752. {
  753. int pos = currentFunction->objVariablePos[n];
  754. *(size_t*)&regs.stackFramePointer[-pos] = 0;
  755. }
  756. }
  757. }
  758. }
  759. }
  760. else if( currentFunction->funcType == asFUNC_SYSTEM )
  761. {
  762. // The current function is an application registered function
  763. // Call the function directly
  764. CallSystemFunction(currentFunction->id, this, 0);
  765. // Was the call successful?
  766. if( status == asEXECUTION_ACTIVE )
  767. {
  768. status = asEXECUTION_FINISHED;
  769. }
  770. }
  771. else
  772. {
  773. // This shouldn't happen
  774. asASSERT(false);
  775. }
  776. }
  777. while( status == asEXECUTION_ACTIVE )
  778. ExecuteNext();
  779. doSuspend = false;
  780. regs.doProcessSuspend = lineCallback;
  781. asPopActiveContext((asIScriptContext *)this);
  782. #ifdef AS_DEBUG
  783. /*
  784. // Output instruction statistics
  785. // This is useful for determining what needs to be optimized.
  786. _mkdir("AS_DEBUG");
  787. FILE *f = fopen("AS_DEBUG/stats.txt", "at");
  788. fprintf(f, "\n");
  789. asQWORD total = 0;
  790. int n;
  791. for( n = 0; n < 256; n++ )
  792. {
  793. if( bcName[n].name && instrCount[n] )
  794. fprintf(f, "%-10.10s : %d\n", bcName[n].name, instrCount[n]);
  795. total += instrCount[n];
  796. }
  797. fprintf(f, "\ntotal : %I64d\n", total);
  798. fprintf(f, "\n");
  799. for( n = 0; n < 256; n++ )
  800. {
  801. if( bcName[n].name )
  802. {
  803. for( int m = 0; m < 256; m++ )
  804. {
  805. if( instrCount2[n][m] )
  806. fprintf(f, "%-10.10s, %-10.10s : %d\n", bcName[n].name, bcName[m].name, instrCount2[n][m]);
  807. }
  808. }
  809. }
  810. fclose(f);
  811. */
  812. #endif
  813. if( status == asEXECUTION_FINISHED )
  814. {
  815. regs.objectType = initialFunction->returnType.GetObjectType();
  816. return asEXECUTION_FINISHED;
  817. }
  818. if( doAbort )
  819. {
  820. doAbort = false;
  821. status = asEXECUTION_ABORTED;
  822. return asEXECUTION_ABORTED;
  823. }
  824. if( status == asEXECUTION_SUSPENDED )
  825. return asEXECUTION_SUSPENDED;
  826. if( status == asEXECUTION_EXCEPTION )
  827. return asEXECUTION_EXCEPTION;
  828. return asERROR;
  829. }
  830. void asCContext::PushCallState()
  831. {
  832. callStack.SetLength(callStack.GetLength() + CALLSTACK_FRAME_SIZE);
  833. // Separating the loads and stores limits data cache trash, and with a smart compiler
  834. // could turn into SIMD style loading/storing if available.
  835. // The compiler can't do this itself due to potential pointer aliasing between the pointers,
  836. // ie writing to tmp could overwrite the data contained in registers.stackFramePointer for example
  837. // for all the compiler knows. So introducing the local variable s, which is never referred to by
  838. // its address we avoid this issue.
  839. size_t s[5];
  840. s[0] = (size_t)regs.stackFramePointer;
  841. s[1] = (size_t)currentFunction;
  842. s[2] = (size_t)regs.programPointer;
  843. s[3] = (size_t)regs.stackPointer;
  844. s[4] = stackIndex;
  845. size_t *tmp = callStack.AddressOf() + callStack.GetLength() - CALLSTACK_FRAME_SIZE;
  846. tmp[0] = s[0];
  847. tmp[1] = s[1];
  848. tmp[2] = s[2];
  849. tmp[3] = s[3];
  850. tmp[4] = s[4];
  851. }
  852. void asCContext::PopCallState()
  853. {
  854. // See comments in PushCallState about pointer aliasing and data cache trashing
  855. size_t *tmp = callStack.AddressOf() + callStack.GetLength() - CALLSTACK_FRAME_SIZE;
  856. size_t s[5];
  857. s[0] = tmp[0];
  858. s[1] = tmp[1];
  859. s[2] = tmp[2];
  860. s[3] = tmp[3];
  861. s[4] = tmp[4];
  862. regs.stackFramePointer = (asDWORD*)s[0];
  863. currentFunction = (asCScriptFunction*)s[1];
  864. regs.programPointer = (asDWORD*)s[2];
  865. regs.stackPointer = (asDWORD*)s[3];
  866. stackIndex = (int)s[4];
  867. callStack.SetLength(callStack.GetLength() - CALLSTACK_FRAME_SIZE);
  868. }
  869. // interface
  870. asUINT asCContext::GetCallstackSize()
  871. {
  872. if( currentFunction == 0 ) return 0;
  873. // The current function is accessed at stackLevel 0
  874. return asUINT(1 + callStack.GetLength() / CALLSTACK_FRAME_SIZE);
  875. }
  876. // interface
  877. asIScriptFunction *asCContext::GetFunction(asUINT stackLevel)
  878. {
  879. if( stackLevel >= GetCallstackSize() ) return 0;
  880. if( stackLevel == 0 ) return currentFunction;
  881. size_t *s = callStack.AddressOf() + (GetCallstackSize() - stackLevel - 1)*CALLSTACK_FRAME_SIZE;
  882. asCScriptFunction *func = (asCScriptFunction*)s[1];
  883. return func;
  884. }
  885. // interface
  886. int asCContext::GetLineNumber(asUINT stackLevel, int *column, const char **sectionName)
  887. {
  888. if( stackLevel >= GetCallstackSize() ) return asINVALID_ARG;
  889. asCScriptFunction *func;
  890. asDWORD *bytePos;
  891. if( stackLevel == 0 )
  892. {
  893. func = currentFunction;
  894. bytePos = regs.programPointer;
  895. }
  896. else
  897. {
  898. size_t *s = callStack.AddressOf() + (GetCallstackSize()-stackLevel-1)*CALLSTACK_FRAME_SIZE;
  899. func = (asCScriptFunction*)s[1];
  900. bytePos = (asDWORD*)s[2];
  901. }
  902. asDWORD line = func->GetLineNumber(int(bytePos - func->byteCode.AddressOf()));
  903. if( column ) *column = (line >> 20);
  904. if( sectionName ) *sectionName = func->GetScriptSectionName();
  905. return (line & 0xFFFFF);
  906. }
  907. void asCContext::CallScriptFunction(asCScriptFunction *func)
  908. {
  909. // Push the framepointer, function id and programCounter on the stack
  910. PushCallState();
  911. currentFunction = func;
  912. regs.programPointer = currentFunction->byteCode.AddressOf();
  913. // Verify if there is enough room in the stack block. Allocate new block if not
  914. if( regs.stackPointer - (func->stackNeeded + RESERVE_STACK) < stackBlocks[stackIndex] )
  915. {
  916. asDWORD *oldStackPointer = regs.stackPointer;
  917. // The size of each stack block is determined by the following formula:
  918. // size = stackBlockSize << index
  919. while( regs.stackPointer - (func->stackNeeded + RESERVE_STACK) < stackBlocks[stackIndex] )
  920. {
  921. // Make sure we don't allocate more space than allowed
  922. if( engine->ep.maximumContextStackSize )
  923. {
  924. // This test will only stop growth once it has already crossed the limit
  925. if( stackBlockSize * ((1 << (stackIndex+1)) - 1) > engine->ep.maximumContextStackSize )
  926. {
  927. isStackMemoryNotAllocated = true;
  928. // Set the stackFramePointer, even though the stackPointer wasn't updated
  929. regs.stackFramePointer = regs.stackPointer;
  930. // TODO: Make sure the exception handler doesn't try to free objects that have not been initialized
  931. SetInternalException(TXT_STACK_OVERFLOW);
  932. return;
  933. }
  934. }
  935. stackIndex++;
  936. if( (int)stackBlocks.GetLength() == stackIndex )
  937. {
  938. asDWORD *stack = asNEWARRAY(asDWORD,(stackBlockSize << stackIndex));
  939. stackBlocks.PushLast(stack);
  940. }
  941. regs.stackPointer = stackBlocks[stackIndex] + (stackBlockSize<<stackIndex) - func->GetSpaceNeededForArguments() - (func->objectType ? AS_PTR_SIZE : 0);
  942. }
  943. // Copy the function arguments to the new stack space
  944. int numDwords = func->GetSpaceNeededForArguments() + (func->objectType ? AS_PTR_SIZE : 0);
  945. memcpy(regs.stackPointer, oldStackPointer, sizeof(asDWORD)*numDwords);
  946. }
  947. // Update framepointer and programCounter
  948. regs.stackFramePointer = regs.stackPointer;
  949. // TODO: optimize: This can be avoided handling this as is done for value types in the exception handler
  950. // Set all object variables to 0
  951. for( asUINT n = 0; n < currentFunction->objVariablePos.GetLength(); n++ )
  952. {
  953. if( !currentFunction->objVariableIsOnHeap[n] ) continue;
  954. int pos = currentFunction->objVariablePos[n];
  955. *(size_t*)&regs.stackFramePointer[-pos] = 0;
  956. }
  957. }
  958. void asCContext::CallInterfaceMethod(asCScriptFunction *func)
  959. {
  960. // Resolve the interface method using the current script type
  961. asCScriptObject *obj = *(asCScriptObject**)(size_t*)regs.stackPointer;
  962. if( obj == 0 )
  963. {
  964. SetInternalException(TXT_NULL_POINTER_ACCESS);
  965. return;
  966. }
  967. asCObjectType *objType = obj->objType;
  968. // TODO: optimize: The object type should have a list of only those methods that
  969. // implement interface methods. This list should be ordered by
  970. // the signatureId so that a binary search can be made, instead
  971. // of a linear search.
  972. //
  973. // When this is done, we must also make sure the signatureId of a
  974. // function never changes, e.g. when if the signature functions are
  975. // released.
  976. // Search the object type for a function that matches the interface function
  977. asCScriptFunction *realFunc = 0;
  978. if( func->funcType == asFUNC_INTERFACE )
  979. {
  980. for( asUINT n = 0; n < objType->methods.GetLength(); n++ )
  981. {
  982. asCScriptFunction *f2 = engine->scriptFunctions[objType->methods[n]];
  983. if( f2->signatureId == func->signatureId )
  984. {
  985. if( f2->funcType == asFUNC_VIRTUAL )
  986. realFunc = objType->virtualFunctionTable[f2->vfTableIdx];
  987. else
  988. realFunc = f2;
  989. break;
  990. }
  991. }
  992. if( realFunc == 0 )
  993. {
  994. SetInternalException(TXT_NULL_POINTER_ACCESS);
  995. return;
  996. }
  997. }
  998. else // if( func->funcType == asFUNC_VIRTUAL )
  999. {
  1000. realFunc = objType->virtualFunctionTable[func->vfTableIdx];
  1001. }
  1002. // Then call the true script function
  1003. CallScriptFunction(realFunc);
  1004. }
  1005. void asCContext::ExecuteNext()
  1006. {
  1007. asDWORD *l_bc = regs.programPointer;
  1008. asDWORD *l_sp = regs.stackPointer;
  1009. asDWORD *l_fp = regs.stackFramePointer;
  1010. for(;;)
  1011. {
  1012. #ifdef AS_DEBUG
  1013. ++stats.instrCount[*(asBYTE*)l_bc];
  1014. ++instrCount[*(asBYTE*)l_bc];
  1015. ++instrCount2[lastBC][*(asBYTE*)l_bc];
  1016. lastBC = *(asBYTE*)l_bc;
  1017. // Used to verify that the size of the instructions are correct
  1018. asDWORD *old = l_bc;
  1019. #endif
  1020. // Remember to keep the cases in order and without
  1021. // gaps, because that will make the switch faster.
  1022. // It will be faster since only one lookup will be
  1023. // made to find the correct jump destination. If not
  1024. // in order, the switch will make two lookups.
  1025. switch( *(asBYTE*)l_bc )
  1026. {
  1027. //--------------
  1028. // memory access functions
  1029. // Decrease the stack pointer with n dwords (stack grows downward)
  1030. case asBC_POP:
  1031. l_sp += asBC_WORDARG0(l_bc);
  1032. l_bc++;
  1033. break;
  1034. // Increase the stack pointer with n dwords
  1035. case asBC_PUSH:
  1036. l_sp -= asBC_WORDARG0(l_bc);
  1037. l_bc++;
  1038. break;
  1039. // Push a dword value on the stack
  1040. case asBC_PshC4:
  1041. --l_sp;
  1042. *l_sp = asBC_DWORDARG(l_bc);
  1043. l_bc += 2;
  1044. break;
  1045. // Push the dword value of a variable on the stack
  1046. case asBC_PshV4:
  1047. --l_sp;
  1048. *l_sp = *(l_fp - asBC_SWORDARG0(l_bc));
  1049. l_bc++;
  1050. break;
  1051. // Push the address of a variable on the stack
  1052. case asBC_PSF:
  1053. l_sp -= AS_PTR_SIZE;
  1054. *(asPTRWORD*)l_sp = (asPTRWORD)size_t(l_fp - asBC_SWORDARG0(l_bc));
  1055. l_bc++;
  1056. break;
  1057. // Swap the top 2 dwords on the stack
  1058. case asBC_SWAP4:
  1059. {
  1060. asDWORD d = (asDWORD)*l_sp;
  1061. *l_sp = *(l_sp+1);
  1062. *(asDWORD*)(l_sp+1) = d;
  1063. l_bc++;
  1064. }
  1065. break;
  1066. // Do a boolean not operation, modifying the value of the variable
  1067. case asBC_NOT:
  1068. #if AS_SIZEOF_BOOL == 1
  1069. {
  1070. // Set the value to true if it is equal to 0
  1071. // We need to use volatile here to tell the compiler it cannot
  1072. // change the order of read and write operations on the pointer.
  1073. volatile asBYTE *ptr = (asBYTE*)(l_fp - asBC_SWORDARG0(l_bc));
  1074. asBYTE val = (ptr[0] == 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1075. ptr[0] = val; // The result is stored in the lower byte
  1076. ptr[1] = 0; // Make sure the rest of the DWORD is 0
  1077. ptr[2] = 0;
  1078. ptr[3] = 0;
  1079. }
  1080. #else
  1081. *(l_fp - asBC_SWORDARG0(l_bc)) = (*(l_fp - asBC_SWORDARG0(l_bc)) == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  1082. #endif
  1083. l_bc++;
  1084. break;
  1085. // Push the dword value of a global variable on the stack
  1086. case asBC_PshG4:
  1087. --l_sp;
  1088. *l_sp = *(asDWORD*)(size_t)asBC_PTRARG(l_bc);
  1089. l_bc += 1 + AS_PTR_SIZE;
  1090. break;
  1091. // Load the address of a global variable in the register, then
  1092. // copy the value of the global variable into a local variable
  1093. case asBC_LdGRdR4:
  1094. *(void**)&regs.valueRegister = (void*)(size_t)asBC_PTRARG(l_bc);
  1095. *(l_fp - asBC_SWORDARG0(l_bc)) = **(asDWORD**)&regs.valueRegister;
  1096. l_bc += 1+AS_PTR_SIZE;
  1097. break;
  1098. //----------------
  1099. // path control instructions
  1100. // Begin execution of a script function
  1101. case asBC_CALL:
  1102. {
  1103. int i = asBC_INTARG(l_bc);
  1104. l_bc += 2;
  1105. asASSERT( i >= 0 );
  1106. asASSERT( (i & FUNC_IMPORTED) == 0 );
  1107. // Need to move the values back to the context
  1108. regs.programPointer = l_bc;
  1109. regs.stackPointer = l_sp;
  1110. regs.stackFramePointer = l_fp;
  1111. CallScriptFunction(engine->scriptFunctions[i]);
  1112. // Extract the values from the context again
  1113. l_bc = regs.programPointer;
  1114. l_sp = regs.stackPointer;
  1115. l_fp = regs.stackFramePointer;
  1116. // If status isn't active anymore then we must stop
  1117. if( status != asEXECUTION_ACTIVE )
  1118. return;
  1119. }
  1120. break;
  1121. // Return to the caller, and remove the arguments from the stack
  1122. case asBC_RET:
  1123. {
  1124. if( callStack.GetLength() == 0 )
  1125. {
  1126. status = asEXECUTION_FINISHED;
  1127. return;
  1128. }
  1129. asWORD w = asBC_WORDARG0(l_bc);
  1130. // Read the old framepointer, functionid, and programCounter from the call stack
  1131. PopCallState();
  1132. // Extract the values from the context again
  1133. l_bc = regs.programPointer;
  1134. l_sp = regs.stackPointer;
  1135. l_fp = regs.stackFramePointer;
  1136. // Pop arguments from stack
  1137. l_sp += w;
  1138. }
  1139. break;
  1140. // Jump to a relative position
  1141. case asBC_JMP:
  1142. l_bc += 2 + asBC_INTARG(l_bc);
  1143. break;
  1144. //----------------
  1145. // Conditional jumps
  1146. // Jump to a relative position if the value in the register is 0
  1147. case asBC_JZ:
  1148. if( *(int*)&regs.valueRegister == 0 )
  1149. l_bc += asBC_INTARG(l_bc) + 2;
  1150. else
  1151. l_bc += 2;
  1152. break;
  1153. // Jump to a relative position if the value in the register is not 0
  1154. case asBC_JNZ:
  1155. if( *(int*)&regs.valueRegister != 0 )
  1156. l_bc += asBC_INTARG(l_bc) + 2;
  1157. else
  1158. l_bc += 2;
  1159. break;
  1160. // Jump to a relative position if the value in the register is negative
  1161. case asBC_JS:
  1162. if( *(int*)&regs.valueRegister < 0 )
  1163. l_bc += asBC_INTARG(l_bc) + 2;
  1164. else
  1165. l_bc += 2;
  1166. break;
  1167. // Jump to a relative position if the value in the register it not negative
  1168. case asBC_JNS:
  1169. if( *(int*)&regs.valueRegister >= 0 )
  1170. l_bc += asBC_INTARG(l_bc) + 2;
  1171. else
  1172. l_bc += 2;
  1173. break;
  1174. // Jump to a relative position if the value in the register is greater than 0
  1175. case asBC_JP:
  1176. if( *(int*)&regs.valueRegister > 0 )
  1177. l_bc += asBC_INTARG(l_bc) + 2;
  1178. else
  1179. l_bc += 2;
  1180. break;
  1181. // Jump to a relative position if the value in the register is not greater than 0
  1182. case asBC_JNP:
  1183. if( *(int*)&regs.valueRegister <= 0 )
  1184. l_bc += asBC_INTARG(l_bc) + 2;
  1185. else
  1186. l_bc += 2;
  1187. break;
  1188. //--------------------
  1189. // test instructions
  1190. // If the value in the register is 0, then set the register to 1, else to 0
  1191. case asBC_TZ:
  1192. #if AS_SIZEOF_BOOL == 1
  1193. {
  1194. // Set the value to true if it is equal to 0
  1195. // We need to use volatile here to tell the compiler it cannot
  1196. // change the order of read and write operations on valueRegister.
  1197. volatile int *regPtr = (int*)&regs.valueRegister;
  1198. volatile asBYTE *regBptr = (asBYTE*)&regs.valueRegister;
  1199. asBYTE val = (regPtr[0] == 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1200. regBptr[0] = val; // The result is stored in the lower byte
  1201. regBptr[1] = 0; // Make sure the rest of the register is 0
  1202. regBptr[2] = 0;
  1203. regBptr[3] = 0;
  1204. regBptr[4] = 0;
  1205. regBptr[5] = 0;
  1206. regBptr[6] = 0;
  1207. regBptr[7] = 0;
  1208. }
  1209. #else
  1210. *(int*)&regs.valueRegister = (*(int*)&regs.valueRegister == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  1211. #endif
  1212. l_bc++;
  1213. break;
  1214. // If the value in the register is not 0, then set the register to 1, else to 0
  1215. case asBC_TNZ:
  1216. #if AS_SIZEOF_BOOL == 1
  1217. {
  1218. // Set the value to true if it is not equal to 0
  1219. // We need to use volatile here to tell the compiler it cannot
  1220. // change the order of read and write operations on valueRegister.
  1221. volatile int *regPtr = (int*)&regs.valueRegister;
  1222. volatile asBYTE *regBptr = (asBYTE*)&regs.valueRegister;
  1223. asBYTE val = (regPtr[0] == 0) ? 0 : VALUE_OF_BOOLEAN_TRUE;
  1224. regBptr[0] = val; // The result is stored in the lower byte
  1225. regBptr[1] = 0; // Make sure the rest of the register is 0
  1226. regBptr[2] = 0;
  1227. regBptr[3] = 0;
  1228. regBptr[4] = 0;
  1229. regBptr[5] = 0;
  1230. regBptr[6] = 0;
  1231. regBptr[7] = 0;
  1232. }
  1233. #else
  1234. *(int*)&regs.valueRegister = (*(int*)&regs.valueRegister == 0 ? 0 : VALUE_OF_BOOLEAN_TRUE);
  1235. #endif
  1236. l_bc++;
  1237. break;
  1238. // If the value in the register is negative, then set the register to 1, else to 0
  1239. case asBC_TS:
  1240. #if AS_SIZEOF_BOOL == 1
  1241. {
  1242. // Set the value to true if it is less than 0
  1243. // We need to use volatile here to tell the compiler it cannot
  1244. // change the order of read and write operations on valueRegister.
  1245. volatile int *regPtr = (int*)&regs.valueRegister;
  1246. volatile asBYTE *regBptr = (asBYTE*)&regs.valueRegister;
  1247. asBYTE val = (regPtr[0] < 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1248. regBptr[0] = val; // The result is stored in the lower byte
  1249. regBptr[1] = 0; // Make sure the rest of the register is 0
  1250. regBptr[2] = 0;
  1251. regBptr[3] = 0;
  1252. regBptr[4] = 0;
  1253. regBptr[5] = 0;
  1254. regBptr[6] = 0;
  1255. regBptr[7] = 0;
  1256. }
  1257. #else
  1258. *(int*)&regs.valueRegister = (*(int*)&regs.valueRegister < 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  1259. #endif
  1260. l_bc++;
  1261. break;
  1262. // If the value in the register is not negative, then set the register to 1, else to 0
  1263. case asBC_TNS:
  1264. #if AS_SIZEOF_BOOL == 1
  1265. {
  1266. // Set the value to true if it is not less than 0
  1267. // We need to use volatile here to tell the compiler it cannot
  1268. // change the order of read and write operations on valueRegister.
  1269. volatile int *regPtr = (int*)&regs.valueRegister;
  1270. volatile asBYTE *regBptr = (asBYTE*)&regs.valueRegister;
  1271. asBYTE val = (regPtr[0] >= 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1272. regBptr[0] = val; // The result is stored in the lower byte
  1273. regBptr[1] = 0; // Make sure the rest of the register is 0
  1274. regBptr[2] = 0;
  1275. regBptr[3] = 0;
  1276. regBptr[4] = 0;
  1277. regBptr[5] = 0;
  1278. regBptr[6] = 0;
  1279. regBptr[7] = 0;
  1280. }
  1281. #else
  1282. *(int*)&regs.valueRegister = (*(int*)&regs.valueRegister < 0 ? 0 : VALUE_OF_BOOLEAN_TRUE);
  1283. #endif
  1284. l_bc++;
  1285. break;
  1286. // If the value in the register is greater than 0, then set the register to 1, else to 0
  1287. case asBC_TP:
  1288. #if AS_SIZEOF_BOOL == 1
  1289. {
  1290. // Set the value to true if it is greater than 0
  1291. // We need to use volatile here to tell the compiler it cannot
  1292. // change the order of read and write operations on valueRegister.
  1293. volatile int *regPtr = (int*)&regs.valueRegister;
  1294. volatile asBYTE *regBptr = (asBYTE*)&regs.valueRegister;
  1295. asBYTE val = (regPtr[0] > 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1296. regBptr[0] = val; // The result is stored in the lower byte
  1297. regBptr[1] = 0; // Make sure the rest of the register is 0
  1298. regBptr[2] = 0;
  1299. regBptr[3] = 0;
  1300. regBptr[4] = 0;
  1301. regBptr[5] = 0;
  1302. regBptr[6] = 0;
  1303. regBptr[7] = 0;
  1304. }
  1305. #else
  1306. *(int*)&regs.valueRegister = (*(int*)&regs.valueRegister > 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  1307. #endif
  1308. l_bc++;
  1309. break;
  1310. // If the value in the register is not greater than 0, then set the register to 1, else to 0
  1311. case asBC_TNP:
  1312. #if AS_SIZEOF_BOOL == 1
  1313. {
  1314. // Set the value to true if it is not greater than 0
  1315. // We need to use volatile here to tell the compiler it cannot
  1316. // change the order of read and write operations on valueRegister.
  1317. volatile int *regPtr = (int*)&regs.valueRegister;
  1318. volatile asBYTE *regBptr = (asBYTE*)&regs.valueRegister;
  1319. asBYTE val = (regPtr[0] <= 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1320. regBptr[0] = val; // The result is stored in the lower byte
  1321. regBptr[1] = 0; // Make sure the rest of the register is 0
  1322. regBptr[2] = 0;
  1323. regBptr[3] = 0;
  1324. regBptr[4] = 0;
  1325. regBptr[5] = 0;
  1326. regBptr[6] = 0;
  1327. regBptr[7] = 0;
  1328. }
  1329. #else
  1330. *(int*)&regs.valueRegister = (*(int*)&regs.valueRegister > 0 ? 0 : VALUE_OF_BOOLEAN_TRUE);
  1331. #endif
  1332. l_bc++;
  1333. break;
  1334. //--------------------
  1335. // negate value
  1336. // Negate the integer value in the variable
  1337. case asBC_NEGi:
  1338. *(l_fp - asBC_SWORDARG0(l_bc)) = asDWORD(-int(*(l_fp - asBC_SWORDARG0(l_bc))));
  1339. l_bc++;
  1340. break;
  1341. // Negate the float value in the variable
  1342. case asBC_NEGf:
  1343. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = -*(float*)(l_fp - asBC_SWORDARG0(l_bc));
  1344. l_bc++;
  1345. break;
  1346. // Negate the double value in the variable
  1347. case asBC_NEGd:
  1348. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = -*(double*)(l_fp - asBC_SWORDARG0(l_bc));
  1349. l_bc++;
  1350. break;
  1351. //-------------------------
  1352. // Increment value pointed to by address in register
  1353. // Increment the short value pointed to by the register
  1354. case asBC_INCi16:
  1355. (**(short**)&regs.valueRegister)++;
  1356. l_bc++;
  1357. break;
  1358. // Increment the byte value pointed to by the register
  1359. case asBC_INCi8:
  1360. (**(char**)&regs.valueRegister)++;
  1361. l_bc++;
  1362. break;
  1363. // Decrement the short value pointed to by the register
  1364. case asBC_DECi16:
  1365. (**(short**)&regs.valueRegister)--;
  1366. l_bc++;
  1367. break;
  1368. // Decrement the byte value pointed to by the register
  1369. case asBC_DECi8:
  1370. (**(char**)&regs.valueRegister)--;
  1371. l_bc++;
  1372. break;
  1373. // Increment the integer value pointed to by the register
  1374. case asBC_INCi:
  1375. ++(**(int**)&regs.valueRegister);
  1376. l_bc++;
  1377. break;
  1378. // Decrement the integer value pointed to by the register
  1379. case asBC_DECi:
  1380. --(**(int**)&regs.valueRegister);
  1381. l_bc++;
  1382. break;
  1383. // Increment the float value pointed to by the register
  1384. case asBC_INCf:
  1385. ++(**(float**)&regs.valueRegister);
  1386. l_bc++;
  1387. break;
  1388. // Decrement the float value pointed to by the register
  1389. case asBC_DECf:
  1390. --(**(float**)&regs.valueRegister);
  1391. l_bc++;
  1392. break;
  1393. // Increment the double value pointed to by the register
  1394. case asBC_INCd:
  1395. ++(**(double**)&regs.valueRegister);
  1396. l_bc++;
  1397. break;
  1398. // Decrement the double value pointed to by the register
  1399. case asBC_DECd:
  1400. --(**(double**)&regs.valueRegister);
  1401. l_bc++;
  1402. break;
  1403. // Increment the local integer variable
  1404. case asBC_IncVi:
  1405. (*(int*)(l_fp - asBC_SWORDARG0(l_bc)))++;
  1406. l_bc++;
  1407. break;
  1408. // Decrement the local integer variable
  1409. case asBC_DecVi:
  1410. (*(int*)(l_fp - asBC_SWORDARG0(l_bc)))--;
  1411. l_bc++;
  1412. break;
  1413. //--------------------
  1414. // bits instructions
  1415. // Do a bitwise not on the value in the variable
  1416. case asBC_BNOT:
  1417. *(l_fp - asBC_SWORDARG0(l_bc)) = ~*(l_fp - asBC_SWORDARG0(l_bc));
  1418. l_bc++;
  1419. break;
  1420. // Do a bitwise and of two variables and store the result in a third variable
  1421. case asBC_BAND:
  1422. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) & *(l_fp - asBC_SWORDARG2(l_bc));
  1423. l_bc += 2;
  1424. break;
  1425. // Do a bitwise or of two variables and store the result in a third variable
  1426. case asBC_BOR:
  1427. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) | *(l_fp - asBC_SWORDARG2(l_bc));
  1428. l_bc += 2;
  1429. break;
  1430. // Do a bitwise xor of two variables and store the result in a third variable
  1431. case asBC_BXOR:
  1432. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) ^ *(l_fp - asBC_SWORDARG2(l_bc));
  1433. l_bc += 2;
  1434. break;
  1435. // Do a logical shift left of two variables and store the result in a third variable
  1436. case asBC_BSLL:
  1437. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) << *(l_fp - asBC_SWORDARG2(l_bc));
  1438. l_bc += 2;
  1439. break;
  1440. // Do a logical shift right of two variables and store the result in a third variable
  1441. case asBC_BSRL:
  1442. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) >> *(l_fp - asBC_SWORDARG2(l_bc));
  1443. l_bc += 2;
  1444. break;
  1445. // Do an arithmetic shift right of two variables and store the result in a third variable
  1446. case asBC_BSRA:
  1447. *(l_fp - asBC_SWORDARG0(l_bc)) = int(*(l_fp - asBC_SWORDARG1(l_bc))) >> *(l_fp - asBC_SWORDARG2(l_bc));
  1448. l_bc += 2;
  1449. break;
  1450. case asBC_COPY:
  1451. {
  1452. void *d = (void*)*(size_t*)l_sp; l_sp += AS_PTR_SIZE;
  1453. void *s = (void*)*(size_t*)l_sp;
  1454. if( s == 0 || d == 0 )
  1455. {
  1456. // Need to move the values back to the context
  1457. regs.programPointer = l_bc;
  1458. regs.stackPointer = l_sp;
  1459. regs.stackFramePointer = l_fp;
  1460. // Raise exception
  1461. SetInternalException(TXT_NULL_POINTER_ACCESS);
  1462. return;
  1463. }
  1464. memcpy(d, s, asBC_WORDARG0(l_bc)*4);
  1465. // replace the pointer on the stack with the lvalue
  1466. *(size_t**)l_sp = (size_t*)d;
  1467. }
  1468. l_bc += 2;
  1469. break;
  1470. case asBC_PshC8:
  1471. l_sp -= 2;
  1472. *(asQWORD*)l_sp = asBC_QWORDARG(l_bc);
  1473. l_bc += 3;
  1474. break;
  1475. case asBC_RDS8:
  1476. #ifndef AS_64BIT_PTR
  1477. *(asQWORD*)(l_sp-1) = *(asQWORD*)*(size_t*)l_sp;
  1478. --l_sp;
  1479. #else
  1480. *(asQWORD*)l_sp = *(asQWORD*)*(size_t*)l_sp;
  1481. #endif
  1482. l_bc++;
  1483. break;
  1484. case asBC_SWAP8:
  1485. {
  1486. asQWORD q = *(asQWORD*)l_sp;
  1487. *(asQWORD*)l_sp = *(asQWORD*)(l_sp+2);
  1488. *(asQWORD*)(l_sp+2) = q;
  1489. l_bc++;
  1490. }
  1491. break;
  1492. //----------------------------
  1493. // Comparisons
  1494. case asBC_CMPd:
  1495. {
  1496. double dbl = *(double*)(l_fp - asBC_SWORDARG0(l_bc)) - *(double*)(l_fp - asBC_SWORDARG1(l_bc));
  1497. if( dbl == 0 ) *(int*)&regs.valueRegister = 0;
  1498. else if( dbl < 0 ) *(int*)&regs.valueRegister = -1;
  1499. else *(int*)&regs.valueRegister = 1;
  1500. l_bc += 2;
  1501. }
  1502. break;
  1503. case asBC_CMPu:
  1504. {
  1505. asDWORD d = *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  1506. asDWORD d2 = *(asDWORD*)(l_fp - asBC_SWORDARG1(l_bc));
  1507. if( d == d2 ) *(int*)&regs.valueRegister = 0;
  1508. else if( d < d2 ) *(int*)&regs.valueRegister = -1;
  1509. else *(int*)&regs.valueRegister = 1;
  1510. l_bc += 2;
  1511. }
  1512. break;
  1513. case asBC_CMPf:
  1514. {
  1515. float f = *(float*)(l_fp - asBC_SWORDARG0(l_bc)) - *(float*)(l_fp - asBC_SWORDARG1(l_bc));
  1516. if( f == 0 ) *(int*)&regs.valueRegister = 0;
  1517. else if( f < 0 ) *(int*)&regs.valueRegister = -1;
  1518. else *(int*)&regs.valueRegister = 1;
  1519. l_bc += 2;
  1520. }
  1521. break;
  1522. case asBC_CMPi:
  1523. {
  1524. int i = *(int*)(l_fp - asBC_SWORDARG0(l_bc)) - *(int*)(l_fp - asBC_SWORDARG1(l_bc));
  1525. if( i == 0 ) *(int*)&regs.valueRegister = 0;
  1526. else if( i < 0 ) *(int*)&regs.valueRegister = -1;
  1527. else *(int*)&regs.valueRegister = 1;
  1528. l_bc += 2;
  1529. }
  1530. break;
  1531. //----------------------------
  1532. // Comparisons with constant value
  1533. case asBC_CMPIi:
  1534. {
  1535. int i = *(int*)(l_fp - asBC_SWORDARG0(l_bc)) - asBC_INTARG(l_bc);
  1536. if( i == 0 ) *(int*)&regs.valueRegister = 0;
  1537. else if( i < 0 ) *(int*)&regs.valueRegister = -1;
  1538. else *(int*)&regs.valueRegister = 1;
  1539. l_bc += 2;
  1540. }
  1541. break;
  1542. case asBC_CMPIf:
  1543. {
  1544. float f = *(float*)(l_fp - asBC_SWORDARG0(l_bc)) - asBC_FLOATARG(l_bc);
  1545. if( f == 0 ) *(int*)&regs.valueRegister = 0;
  1546. else if( f < 0 ) *(int*)&regs.valueRegister = -1;
  1547. else *(int*)&regs.valueRegister = 1;
  1548. l_bc += 2;
  1549. }
  1550. break;
  1551. case asBC_CMPIu:
  1552. {
  1553. asDWORD d1 = *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  1554. asDWORD d2 = asBC_DWORDARG(l_bc);
  1555. if( d1 == d2 ) *(int*)&regs.valueRegister = 0;
  1556. else if( d1 < d2 ) *(int*)&regs.valueRegister = -1;
  1557. else *(int*)&regs.valueRegister = 1;
  1558. l_bc += 2;
  1559. }
  1560. break;
  1561. case asBC_JMPP:
  1562. l_bc += 1 + (*(int*)(l_fp - asBC_SWORDARG0(l_bc)))*2;
  1563. break;
  1564. case asBC_PopRPtr:
  1565. *(asPTRWORD*)&regs.valueRegister = *(asPTRWORD*)l_sp;
  1566. l_sp += AS_PTR_SIZE;
  1567. l_bc++;
  1568. break;
  1569. case asBC_PshRPtr:
  1570. l_sp -= AS_PTR_SIZE;
  1571. *(asPTRWORD*)l_sp = *(asPTRWORD*)&regs.valueRegister;
  1572. l_bc++;
  1573. break;
  1574. case asBC_STR:
  1575. {
  1576. // Get the string id from the argument
  1577. asWORD w = asBC_WORDARG0(l_bc);
  1578. // Push the string pointer on the stack
  1579. const asCString &b = engine->GetConstantString(w);
  1580. l_sp -= AS_PTR_SIZE;
  1581. *(asPTRWORD*)l_sp = (asPTRWORD)(size_t)b.AddressOf();
  1582. // Push the string length on the stack
  1583. --l_sp;
  1584. *l_sp = (asDWORD)b.GetLength();
  1585. l_bc++;
  1586. }
  1587. break;
  1588. case asBC_CALLSYS:
  1589. {
  1590. // Get function ID from the argument
  1591. int i = asBC_INTARG(l_bc);
  1592. // Need to move the values back to the context as the called functions
  1593. // may use the debug interface to inspect the registers
  1594. regs.programPointer = l_bc;
  1595. regs.stackPointer = l_sp;
  1596. regs.stackFramePointer = l_fp;
  1597. l_sp += CallSystemFunction(i, this, 0);
  1598. // Update the program position after the call so that line number is correct
  1599. l_bc += 2;
  1600. if( regs.doProcessSuspend )
  1601. {
  1602. // Should the execution be suspended?
  1603. if( doSuspend )
  1604. {
  1605. regs.programPointer = l_bc;
  1606. regs.stackPointer = l_sp;
  1607. regs.stackFramePointer = l_fp;
  1608. status = asEXECUTION_SUSPENDED;
  1609. return;
  1610. }
  1611. // An exception might have been raised
  1612. if( status != asEXECUTION_ACTIVE )
  1613. {
  1614. regs.programPointer = l_bc;
  1615. regs.stackPointer = l_sp;
  1616. regs.stackFramePointer = l_fp;
  1617. return;
  1618. }
  1619. }
  1620. }
  1621. break;
  1622. case asBC_CALLBND:
  1623. {
  1624. // Get the function ID from the stack
  1625. int i = asBC_INTARG(l_bc);
  1626. l_bc += 2;
  1627. asASSERT( i >= 0 );
  1628. asASSERT( i & FUNC_IMPORTED );
  1629. // Need to move the values back to the context
  1630. regs.programPointer = l_bc;
  1631. regs.stackPointer = l_sp;
  1632. regs.stackFramePointer = l_fp;
  1633. int funcID = engine->importedFunctions[i&0xFFFF]->boundFunctionId;
  1634. if( funcID == -1 )
  1635. {
  1636. SetInternalException(TXT_UNBOUND_FUNCTION);
  1637. return;
  1638. }
  1639. else
  1640. {
  1641. asCScriptFunction *func = engine->GetScriptFunction(funcID);
  1642. CallScriptFunction(func);
  1643. }
  1644. // Extract the values from the context again
  1645. l_bc = regs.programPointer;
  1646. l_sp = regs.stackPointer;
  1647. l_fp = regs.stackFramePointer;
  1648. // If status isn't active anymore then we must stop
  1649. if( status != asEXECUTION_ACTIVE )
  1650. return;
  1651. }
  1652. break;
  1653. case asBC_SUSPEND:
  1654. if( regs.doProcessSuspend )
  1655. {
  1656. if( lineCallback )
  1657. {
  1658. regs.programPointer = l_bc;
  1659. regs.stackPointer = l_sp;
  1660. regs.stackFramePointer = l_fp;
  1661. CallLineCallback();
  1662. }
  1663. if( doSuspend )
  1664. {
  1665. l_bc++;
  1666. // Need to move the values back to the context
  1667. regs.programPointer = l_bc;
  1668. regs.stackPointer = l_sp;
  1669. regs.stackFramePointer = l_fp;
  1670. status = asEXECUTION_SUSPENDED;
  1671. return;
  1672. }
  1673. }
  1674. l_bc++;
  1675. break;
  1676. case asBC_ALLOC:
  1677. {
  1678. asCObjectType *objType = (asCObjectType*)(size_t)asBC_PTRARG(l_bc);
  1679. int func = asBC_INTARG(l_bc+AS_PTR_SIZE);
  1680. if( objType->flags & asOBJ_SCRIPT_OBJECT )
  1681. {
  1682. // Pre-allocate the memory
  1683. asDWORD *mem = (asDWORD*)engine->CallAlloc(objType);
  1684. // Pre-initialize the memory by calling the constructor for asCScriptObject
  1685. ScriptObject_Construct(objType, (asCScriptObject*)mem);
  1686. // Call the constructor to initalize the memory
  1687. asCScriptFunction *f = engine->scriptFunctions[func];
  1688. asDWORD **a = (asDWORD**)*(size_t*)(l_sp + f->GetSpaceNeededForArguments());
  1689. if( a ) *a = mem;
  1690. // Push the object pointer on the stack
  1691. l_sp -= AS_PTR_SIZE;
  1692. *(size_t*)l_sp = (size_t)mem;
  1693. l_bc += 2+AS_PTR_SIZE;
  1694. // Need to move the values back to the context
  1695. regs.programPointer = l_bc;
  1696. regs.stackPointer = l_sp;
  1697. regs.stackFramePointer = l_fp;
  1698. CallScriptFunction(f);
  1699. // Extract the values from the context again
  1700. l_bc = regs.programPointer;
  1701. l_sp = regs.stackPointer;
  1702. l_fp = regs.stackFramePointer;
  1703. // If status isn't active anymore then we must stop
  1704. if( status != asEXECUTION_ACTIVE )
  1705. return;
  1706. }
  1707. else
  1708. {
  1709. // Pre-allocate the memory
  1710. asDWORD *mem = (asDWORD*)engine->CallAlloc(objType);
  1711. if( func )
  1712. {
  1713. // Need to move the values back to the context as the called functions
  1714. // may use the debug interface to inspect the registers
  1715. regs.programPointer = l_bc;
  1716. regs.stackPointer = l_sp;
  1717. regs.stackFramePointer = l_fp;
  1718. l_sp += CallSystemFunction(func, this, mem);
  1719. }
  1720. // Pop the variable address from the stack
  1721. asDWORD **a = (asDWORD**)*(size_t*)l_sp;
  1722. l_sp += AS_PTR_SIZE;
  1723. if( a ) *a = mem;
  1724. l_bc += 2+AS_PTR_SIZE;
  1725. if( regs.doProcessSuspend )
  1726. {
  1727. // Should the execution be suspended?
  1728. if( doSuspend )
  1729. {
  1730. regs.programPointer = l_bc;
  1731. regs.stackPointer = l_sp;
  1732. regs.stackFramePointer = l_fp;
  1733. status = asEXECUTION_SUSPENDED;
  1734. return;
  1735. }
  1736. // An exception might have been raised
  1737. if( status != asEXECUTION_ACTIVE )
  1738. {
  1739. regs.programPointer = l_bc;
  1740. regs.stackPointer = l_sp;
  1741. regs.stackFramePointer = l_fp;
  1742. engine->CallFree(mem);
  1743. *a = 0;
  1744. return;
  1745. }
  1746. }
  1747. }
  1748. }
  1749. break;
  1750. case asBC_FREE:
  1751. {
  1752. // Get the variable that holds the object handle/reference
  1753. asPTRWORD *a = (asPTRWORD*)size_t(l_fp - asBC_SWORDARG0(l_bc));
  1754. if( *a )
  1755. {
  1756. asCObjectType *objType = (asCObjectType*)(size_t)asBC_PTRARG(l_bc);
  1757. asSTypeBehaviour *beh = &objType->beh;
  1758. // Need to move the values back to the context as the called functions
  1759. // may use the debug interface to inspect the registers
  1760. regs.programPointer = l_bc;
  1761. regs.stackPointer = l_sp;
  1762. regs.stackFramePointer = l_fp;
  1763. if( beh->release )
  1764. {
  1765. engine->CallObjectMethod((void*)(size_t)*a, beh->release);
  1766. }
  1767. else
  1768. {
  1769. if( beh->destruct )
  1770. {
  1771. engine->CallObjectMethod((void*)(size_t)*a, beh->destruct);
  1772. }
  1773. engine->CallFree((void*)(size_t)*a);
  1774. }
  1775. // Clear the variable
  1776. *a = 0;
  1777. }
  1778. }
  1779. l_bc += 1+AS_PTR_SIZE;
  1780. break;
  1781. case asBC_LOADOBJ:
  1782. {
  1783. // Move the object pointer from the object variable into the object register
  1784. void **a = (void**)(l_fp - asBC_SWORDARG0(l_bc));
  1785. regs.objectType = 0;
  1786. regs.objectRegister = *a;
  1787. *a = 0;
  1788. }
  1789. l_bc++;
  1790. break;
  1791. case asBC_STOREOBJ:
  1792. // Move the object pointer from the object register to the object variable
  1793. *(size_t*)(l_fp - asBC_SWORDARG0(l_bc)) = size_t(regs.objectRegister);
  1794. regs.objectRegister = 0;
  1795. l_bc++;
  1796. break;
  1797. case asBC_GETOBJ:
  1798. {
  1799. // Read variable index from location on stack
  1800. size_t *a = (size_t*)(l_sp + asBC_WORDARG0(l_bc));
  1801. asDWORD offset = *(asDWORD*)a;
  1802. // Move pointer from variable to the same location on the stack
  1803. size_t *v = (size_t*)(l_fp - offset);
  1804. *a = *v;
  1805. // Clear variable
  1806. *v = 0;
  1807. }
  1808. l_bc++;
  1809. break;
  1810. case asBC_REFCPY:
  1811. {
  1812. asCObjectType *objType = (asCObjectType*)(size_t)asBC_PTRARG(l_bc);
  1813. asSTypeBehaviour *beh = &objType->beh;
  1814. // Pop address of destination pointer from the stack
  1815. void **d = (void**)*(size_t*)l_sp;
  1816. l_sp += AS_PTR_SIZE;
  1817. // Read wanted pointer from the stack
  1818. void *s = (void*)*(size_t*)l_sp;
  1819. // Need to move the values back to the context as the called functions
  1820. // may use the debug interface to inspect the registers
  1821. regs.programPointer = l_bc;
  1822. regs.stackPointer = l_sp;
  1823. regs.stackFramePointer = l_fp;
  1824. // Release previous object held by destination pointer
  1825. if( *d != 0 )
  1826. engine->CallObjectMethod(*d, beh->release);
  1827. // Increase ref counter of wanted object
  1828. if( s != 0 )
  1829. engine->CallObjectMethod(s, beh->addref);
  1830. // Set the new object in the destination
  1831. *d = s;
  1832. }
  1833. l_bc += 1+AS_PTR_SIZE;
  1834. break;
  1835. case asBC_CHKREF:
  1836. {
  1837. // Verify if the pointer on the stack is null
  1838. // This is used when validating a pointer that an operator will work on
  1839. size_t a = *(size_t*)l_sp;
  1840. if( a == 0 )
  1841. {
  1842. regs.programPointer = l_bc;
  1843. regs.stackPointer = l_sp;
  1844. regs.stackFramePointer = l_fp;
  1845. SetInternalException(TXT_NULL_POINTER_ACCESS);
  1846. return;
  1847. }
  1848. }
  1849. l_bc++;
  1850. break;
  1851. case asBC_GETOBJREF:
  1852. {
  1853. // Get the location on the stack where the reference will be placed
  1854. size_t *a = (size_t*)(l_sp + asBC_WORDARG0(l_bc));
  1855. // Replace the variable index with the object handle held in the variable
  1856. *(size_t**)a = *(size_t**)(l_fp - *a);
  1857. }
  1858. l_bc++;
  1859. break;
  1860. case asBC_GETREF:
  1861. {
  1862. // Get the location on the stack where the reference will be placed
  1863. size_t *a = (size_t*)(l_sp + asBC_WORDARG0(l_bc));
  1864. // Replace the variable index with the address of the variable
  1865. *(size_t**)a = (size_t*)(l_fp - (int)*a);
  1866. }
  1867. l_bc++;
  1868. break;
  1869. case asBC_SWAP48:
  1870. {
  1871. asDWORD d = *(asDWORD*)l_sp;
  1872. asQWORD q = *(asQWORD*)(l_sp+1);
  1873. *(asQWORD*)l_sp = q;
  1874. *(asDWORD*)(l_sp+2) = d;
  1875. l_bc++;
  1876. }
  1877. break;
  1878. case asBC_SWAP84:
  1879. {
  1880. asQWORD q = *(asQWORD*)l_sp;
  1881. asDWORD d = *(asDWORD*)(l_sp+2);
  1882. *(asDWORD*)l_sp = d;
  1883. *(asQWORD*)(l_sp+1) = q;
  1884. l_bc++;
  1885. }
  1886. break;
  1887. case asBC_OBJTYPE:
  1888. // Push the object type on the stack
  1889. l_sp -= AS_PTR_SIZE;
  1890. *(asPTRWORD*)l_sp = asBC_PTRARG(l_bc);
  1891. l_bc += 1+AS_PTR_SIZE;
  1892. break;
  1893. case asBC_TYPEID:
  1894. // Equivalent to PshC4, but kept as separate instruction for bytecode serialization
  1895. --l_sp;
  1896. *l_sp = asBC_DWORDARG(l_bc);
  1897. l_bc += 2;
  1898. break;
  1899. case asBC_SetV4:
  1900. *(l_fp - asBC_SWORDARG0(l_bc)) = asBC_DWORDARG(l_bc);
  1901. l_bc += 2;
  1902. break;
  1903. case asBC_SetV8:
  1904. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = asBC_QWORDARG(l_bc);
  1905. l_bc += 3;
  1906. break;
  1907. case asBC_ADDSi:
  1908. *(size_t*)l_sp = size_t(asPTRWORD(*(size_t*)l_sp) + asBC_SWORDARG0(l_bc));
  1909. l_bc += 2;
  1910. break;
  1911. case asBC_CpyVtoV4:
  1912. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc));
  1913. l_bc += 2;
  1914. break;
  1915. case asBC_CpyVtoV8:
  1916. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc));
  1917. l_bc += 2;
  1918. break;
  1919. case asBC_CpyVtoR4:
  1920. *(asDWORD*)&regs.valueRegister = *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  1921. l_bc++;
  1922. break;
  1923. case asBC_CpyVtoR8:
  1924. *(asQWORD*)&regs.valueRegister = *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  1925. l_bc++;
  1926. break;
  1927. case asBC_CpyVtoG4:
  1928. *(asDWORD*)(size_t)asBC_PTRARG(l_bc) = *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  1929. l_bc += 1 + AS_PTR_SIZE;
  1930. break;
  1931. case asBC_CpyRtoV4:
  1932. *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asDWORD*)&regs.valueRegister;
  1933. l_bc++;
  1934. break;
  1935. case asBC_CpyRtoV8:
  1936. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = regs.valueRegister;
  1937. l_bc++;
  1938. break;
  1939. case asBC_CpyGtoV4:
  1940. *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asDWORD*)(size_t)asBC_PTRARG(l_bc);
  1941. l_bc += 1 + AS_PTR_SIZE;
  1942. break;
  1943. case asBC_WRTV1:
  1944. // The pointer in the register points to a byte, and *(l_fp - offset) too
  1945. **(asBYTE**)&regs.valueRegister = *(asBYTE*)(l_fp - asBC_SWORDARG0(l_bc));
  1946. l_bc++;
  1947. break;
  1948. case asBC_WRTV2:
  1949. // The pointer in the register points to a word, and *(l_fp - offset) too
  1950. **(asWORD**)&regs.valueRegister = *(asWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  1951. l_bc++;
  1952. break;
  1953. case asBC_WRTV4:
  1954. **(asDWORD**)&regs.valueRegister = *(l_fp - asBC_SWORDARG0(l_bc));
  1955. l_bc++;
  1956. break;
  1957. case asBC_WRTV8:
  1958. **(asQWORD**)&regs.valueRegister = *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  1959. l_bc++;
  1960. break;
  1961. case asBC_RDR1:
  1962. {
  1963. // The pointer in the register points to a byte, and *(l_fp - offset) will also point to a byte
  1964. asBYTE *bPtr = (asBYTE*)(l_fp - asBC_SWORDARG0(l_bc));
  1965. bPtr[0] = **(asBYTE**)&regs.valueRegister; // read the byte
  1966. bPtr[1] = 0; // 0 the rest of the DWORD
  1967. bPtr[2] = 0;
  1968. bPtr[3] = 0;
  1969. }
  1970. l_bc++;
  1971. break;
  1972. case asBC_RDR2:
  1973. {
  1974. // The pointer in the register points to a word, and *(l_fp - offset) will also point to a word
  1975. asWORD *wPtr = (asWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  1976. wPtr[0] = **(asWORD**)&regs.valueRegister; // read the word
  1977. wPtr[1] = 0; // 0 the rest of the DWORD
  1978. }
  1979. l_bc++;
  1980. break;
  1981. case asBC_RDR4:
  1982. *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = **(asDWORD**)&regs.valueRegister;
  1983. l_bc++;
  1984. break;
  1985. case asBC_RDR8:
  1986. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = **(asQWORD**)&regs.valueRegister;
  1987. l_bc++;
  1988. break;
  1989. case asBC_LDG:
  1990. *(asPTRWORD*)&regs.valueRegister = asBC_PTRARG(l_bc);
  1991. l_bc += 1+AS_PTR_SIZE;
  1992. break;
  1993. case asBC_LDV:
  1994. *(asDWORD**)&regs.valueRegister = (l_fp - asBC_SWORDARG0(l_bc));
  1995. l_bc++;
  1996. break;
  1997. case asBC_PGA:
  1998. l_sp -= AS_PTR_SIZE;
  1999. *(asPTRWORD*)l_sp = asBC_PTRARG(l_bc);
  2000. l_bc += 1+AS_PTR_SIZE;
  2001. break;
  2002. case asBC_RDS4:
  2003. #ifndef AS_64BIT_PTR
  2004. *l_sp = *(asDWORD*)*(size_t*)l_sp;
  2005. #else
  2006. {
  2007. asDWORD d = *(asDWORD*)*(size_t*)l_sp;
  2008. l_sp++;
  2009. *l_sp = d;
  2010. }
  2011. #endif
  2012. l_bc++;
  2013. break;
  2014. case asBC_VAR:
  2015. l_sp -= AS_PTR_SIZE;
  2016. *(size_t*)l_sp = (size_t)asBC_SWORDARG0(l_bc);
  2017. l_bc++;
  2018. break;
  2019. //----------------------------
  2020. // Type conversions
  2021. case asBC_iTOf:
  2022. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = float(*(int*)(l_fp - asBC_SWORDARG0(l_bc)));
  2023. l_bc++;
  2024. break;
  2025. case asBC_fTOi:
  2026. *(l_fp - asBC_SWORDARG0(l_bc)) = int(*(float*)(l_fp - asBC_SWORDARG0(l_bc)));
  2027. l_bc++;
  2028. break;
  2029. case asBC_uTOf:
  2030. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = float(*(l_fp - asBC_SWORDARG0(l_bc)));
  2031. l_bc++;
  2032. break;
  2033. case asBC_fTOu:
  2034. // We must cast to int first, because on some compilers the cast of a negative float value to uint result in 0
  2035. *(l_fp - asBC_SWORDARG0(l_bc)) = asUINT(int(*(float*)(l_fp - asBC_SWORDARG0(l_bc))));
  2036. l_bc++;
  2037. break;
  2038. case asBC_sbTOi:
  2039. // *(l_fp - offset) points to a char, and will point to an int afterwards
  2040. *(l_fp - asBC_SWORDARG0(l_bc)) = *(signed char*)(l_fp - asBC_SWORDARG0(l_bc));
  2041. l_bc++;
  2042. break;
  2043. case asBC_swTOi:
  2044. // *(l_fp - offset) points to a short, and will point to an int afterwards
  2045. *(l_fp - asBC_SWORDARG0(l_bc)) = *(short*)(l_fp - asBC_SWORDARG0(l_bc));
  2046. l_bc++;
  2047. break;
  2048. case asBC_ubTOi:
  2049. // (l_fp - offset) points to a byte, and will point to an int afterwards
  2050. *(l_fp - asBC_SWORDARG0(l_bc)) = *(asBYTE*)(l_fp - asBC_SWORDARG0(l_bc));
  2051. l_bc++;
  2052. break;
  2053. case asBC_uwTOi:
  2054. // *(l_fp - offset) points to a word, and will point to an int afterwards
  2055. *(l_fp - asBC_SWORDARG0(l_bc)) = *(asWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2056. l_bc++;
  2057. break;
  2058. case asBC_dTOi:
  2059. *(l_fp - asBC_SWORDARG0(l_bc)) = int(*(double*)(l_fp - asBC_SWORDARG1(l_bc)));
  2060. l_bc += 2;
  2061. break;
  2062. case asBC_dTOu:
  2063. // We must cast to int first, because on some compilers the cast of a negative float value to uint result in 0
  2064. *(l_fp - asBC_SWORDARG0(l_bc)) = asUINT(int(*(double*)(l_fp - asBC_SWORDARG1(l_bc))));
  2065. l_bc += 2;
  2066. break;
  2067. case asBC_dTOf:
  2068. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = float(*(double*)(l_fp - asBC_SWORDARG1(l_bc)));
  2069. l_bc += 2;
  2070. break;
  2071. case asBC_iTOd:
  2072. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = double(*(int*)(l_fp - asBC_SWORDARG1(l_bc)));
  2073. l_bc += 2;
  2074. break;
  2075. case asBC_uTOd:
  2076. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = double(*(asUINT*)(l_fp - asBC_SWORDARG1(l_bc)));
  2077. l_bc += 2;
  2078. break;
  2079. case asBC_fTOd:
  2080. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = double(*(float*)(l_fp - asBC_SWORDARG1(l_bc)));
  2081. l_bc += 2;
  2082. break;
  2083. //------------------------------
  2084. // Math operations
  2085. case asBC_ADDi:
  2086. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) + *(int*)(l_fp - asBC_SWORDARG2(l_bc));
  2087. l_bc += 2;
  2088. break;
  2089. case asBC_SUBi:
  2090. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) - *(int*)(l_fp - asBC_SWORDARG2(l_bc));
  2091. l_bc += 2;
  2092. break;
  2093. case asBC_MULi:
  2094. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) * *(int*)(l_fp - asBC_SWORDARG2(l_bc));
  2095. l_bc += 2;
  2096. break;
  2097. case asBC_DIVi:
  2098. {
  2099. int divider = *(int*)(l_fp - asBC_SWORDARG2(l_bc));
  2100. if( divider == 0 )
  2101. {
  2102. // Need to move the values back to the context
  2103. regs.programPointer = l_bc;
  2104. regs.stackPointer = l_sp;
  2105. regs.stackFramePointer = l_fp;
  2106. // Raise exception
  2107. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2108. return;
  2109. }
  2110. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) / divider;
  2111. }
  2112. l_bc += 2;
  2113. break;
  2114. case asBC_MODi:
  2115. {
  2116. int divider = *(int*)(l_fp - asBC_SWORDARG2(l_bc));
  2117. if( divider == 0 )
  2118. {
  2119. // Need to move the values back to the context
  2120. regs.programPointer = l_bc;
  2121. regs.stackPointer = l_sp;
  2122. regs.stackFramePointer = l_fp;
  2123. // Raise exception
  2124. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2125. return;
  2126. }
  2127. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) % divider;
  2128. }
  2129. l_bc += 2;
  2130. break;
  2131. case asBC_ADDf:
  2132. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = *(float*)(l_fp - asBC_SWORDARG1(l_bc)) + *(float*)(l_fp - asBC_SWORDARG2(l_bc));
  2133. l_bc += 2;
  2134. break;
  2135. case asBC_SUBf:
  2136. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = *(float*)(l_fp - asBC_SWORDARG1(l_bc)) - *(float*)(l_fp - asBC_SWORDARG2(l_bc));
  2137. l_bc += 2;
  2138. break;
  2139. case asBC_MULf:
  2140. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = *(float*)(l_fp - asBC_SWORDARG1(l_bc)) * *(float*)(l_fp - asBC_SWORDARG2(l_bc));
  2141. l_bc += 2;
  2142. break;
  2143. case asBC_DIVf:
  2144. {
  2145. float divider = *(float*)(l_fp - asBC_SWORDARG2(l_bc));
  2146. if( divider == 0 )
  2147. {
  2148. // Need to move the values back to the context
  2149. regs.programPointer = l_bc;
  2150. regs.stackPointer = l_sp;
  2151. regs.stackFramePointer = l_fp;
  2152. // Raise exception
  2153. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2154. return;
  2155. }
  2156. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = *(float*)(l_fp - asBC_SWORDARG1(l_bc)) / divider;
  2157. }
  2158. l_bc += 2;
  2159. break;
  2160. case asBC_MODf:
  2161. {
  2162. float divider = *(float*)(l_fp - asBC_SWORDARG2(l_bc));
  2163. if( divider == 0 )
  2164. {
  2165. // Need to move the values back to the context
  2166. regs.programPointer = l_bc;
  2167. regs.stackPointer = l_sp;
  2168. regs.stackFramePointer = l_fp;
  2169. // Raise exception
  2170. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2171. return;
  2172. }
  2173. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = fmodf(*(float*)(l_fp - asBC_SWORDARG1(l_bc)), divider);
  2174. }
  2175. l_bc += 2;
  2176. break;
  2177. case asBC_ADDd:
  2178. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = *(double*)(l_fp - asBC_SWORDARG1(l_bc)) + *(double*)(l_fp - asBC_SWORDARG2(l_bc));
  2179. l_bc += 2;
  2180. break;
  2181. case asBC_SUBd:
  2182. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = *(double*)(l_fp - asBC_SWORDARG1(l_bc)) - *(double*)(l_fp - asBC_SWORDARG2(l_bc));
  2183. l_bc += 2;
  2184. break;
  2185. case asBC_MULd:
  2186. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = *(double*)(l_fp - asBC_SWORDARG1(l_bc)) * *(double*)(l_fp - asBC_SWORDARG2(l_bc));
  2187. l_bc += 2;
  2188. break;
  2189. case asBC_DIVd:
  2190. {
  2191. double divider = *(double*)(l_fp - asBC_SWORDARG2(l_bc));
  2192. if( divider == 0 )
  2193. {
  2194. // Need to move the values back to the context
  2195. regs.programPointer = l_bc;
  2196. regs.stackPointer = l_sp;
  2197. regs.stackFramePointer = l_fp;
  2198. // Raise exception
  2199. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2200. return;
  2201. }
  2202. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = *(double*)(l_fp - asBC_SWORDARG1(l_bc)) / divider;
  2203. l_bc += 2;
  2204. }
  2205. break;
  2206. case asBC_MODd:
  2207. {
  2208. double divider = *(double*)(l_fp - asBC_SWORDARG2(l_bc));
  2209. if( divider == 0 )
  2210. {
  2211. // Need to move the values back to the context
  2212. regs.programPointer = l_bc;
  2213. regs.stackPointer = l_sp;
  2214. regs.stackFramePointer = l_fp;
  2215. // Raise exception
  2216. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2217. return;
  2218. }
  2219. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = fmod(*(double*)(l_fp - asBC_SWORDARG1(l_bc)), divider);
  2220. l_bc += 2;
  2221. }
  2222. break;
  2223. //------------------------------
  2224. // Math operations with constant value
  2225. case asBC_ADDIi:
  2226. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) + asBC_INTARG(l_bc+1);
  2227. l_bc += 3;
  2228. break;
  2229. case asBC_SUBIi:
  2230. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) - asBC_INTARG(l_bc+1);
  2231. l_bc += 3;
  2232. break;
  2233. case asBC_MULIi:
  2234. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) * asBC_INTARG(l_bc+1);
  2235. l_bc += 3;
  2236. break;
  2237. case asBC_ADDIf:
  2238. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = *(float*)(l_fp - asBC_SWORDARG1(l_bc)) + asBC_FLOATARG(l_bc+1);
  2239. l_bc += 3;
  2240. break;
  2241. case asBC_SUBIf:
  2242. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = *(float*)(l_fp - asBC_SWORDARG1(l_bc)) - asBC_FLOATARG(l_bc+1);
  2243. l_bc += 3;
  2244. break;
  2245. case asBC_MULIf:
  2246. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = *(float*)(l_fp - asBC_SWORDARG1(l_bc)) * asBC_FLOATARG(l_bc+1);
  2247. l_bc += 3;
  2248. break;
  2249. //-----------------------------------
  2250. case asBC_SetG4:
  2251. *(asDWORD*)(size_t)asBC_PTRARG(l_bc) = asBC_DWORDARG(l_bc+AS_PTR_SIZE);
  2252. l_bc += 2 + AS_PTR_SIZE;
  2253. break;
  2254. case asBC_ChkRefS:
  2255. {
  2256. // Verify if the pointer on the stack refers to a non-null value
  2257. // This is used to validate a reference to a handle
  2258. asDWORD *a = (asDWORD*)*(size_t*)l_sp;
  2259. if( *a == 0 )
  2260. {
  2261. regs.programPointer = l_bc;
  2262. regs.stackPointer = l_sp;
  2263. regs.stackFramePointer = l_fp;
  2264. SetInternalException(TXT_NULL_POINTER_ACCESS);
  2265. return;
  2266. }
  2267. }
  2268. l_bc++;
  2269. break;
  2270. case asBC_ChkNullV:
  2271. {
  2272. // Verify if variable (on the stack) is not null
  2273. asDWORD *a = *(asDWORD**)(l_fp - asBC_SWORDARG0(l_bc));
  2274. if( a == 0 )
  2275. {
  2276. regs.programPointer = l_bc;
  2277. regs.stackPointer = l_sp;
  2278. regs.stackFramePointer = l_fp;
  2279. SetInternalException(TXT_NULL_POINTER_ACCESS);
  2280. return;
  2281. }
  2282. }
  2283. l_bc++;
  2284. break;
  2285. case asBC_CALLINTF:
  2286. {
  2287. int i = asBC_INTARG(l_bc);
  2288. l_bc += 2;
  2289. asASSERT( i >= 0 );
  2290. asASSERT( (i & FUNC_IMPORTED) == 0 );
  2291. // Need to move the values back to the context
  2292. regs.programPointer = l_bc;
  2293. regs.stackPointer = l_sp;
  2294. regs.stackFramePointer = l_fp;
  2295. CallInterfaceMethod(engine->GetScriptFunction(i));
  2296. // Extract the values from the context again
  2297. l_bc = regs.programPointer;
  2298. l_sp = regs.stackPointer;
  2299. l_fp = regs.stackFramePointer;
  2300. // If status isn't active anymore then we must stop
  2301. if( status != asEXECUTION_ACTIVE )
  2302. return;
  2303. }
  2304. break;
  2305. case asBC_iTOb:
  2306. {
  2307. // *(l_fp - offset) points to an int, and will point to a byte afterwards
  2308. // We need to use volatile here to tell the compiler not to rearrange
  2309. // read and write operations during optimizations.
  2310. volatile asDWORD val = *(l_fp - asBC_SWORDARG0(l_bc));
  2311. volatile asBYTE *bPtr = (asBYTE*)(l_fp - asBC_SWORDARG0(l_bc));
  2312. bPtr[0] = (asBYTE)val; // write the byte
  2313. bPtr[1] = 0; // 0 the rest of the DWORD
  2314. bPtr[2] = 0;
  2315. bPtr[3] = 0;
  2316. }
  2317. l_bc++;
  2318. break;
  2319. case asBC_iTOw:
  2320. {
  2321. // *(l_fp - offset) points to an int, and will point to word afterwards
  2322. // We need to use volatile here to tell the compiler not to rearrange
  2323. // read and write operations during optimizations.
  2324. volatile asDWORD val = *(l_fp - asBC_SWORDARG0(l_bc));
  2325. volatile asWORD *wPtr = (asWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2326. wPtr[0] = (asWORD)val; // write the word
  2327. wPtr[1] = 0; // 0 the rest of the DWORD
  2328. }
  2329. l_bc++;
  2330. break;
  2331. case asBC_SetV1:
  2332. // TODO: This is exactly the same as SetV4. This is a left over from the time
  2333. // when the bytecode instructions were more tightly packed. It can now
  2334. // be removed. When removing it, make sure the value is correctly converted
  2335. // on big-endian CPUs.
  2336. // The byte is already stored correctly in the argument
  2337. *(l_fp - asBC_SWORDARG0(l_bc)) = asBC_DWORDARG(l_bc);
  2338. l_bc += 2;
  2339. break;
  2340. case asBC_SetV2:
  2341. // TODO: This is exactly the same as SetV4. This is a left over from the time
  2342. // when the bytecode instructions were more tightly packed. It can now
  2343. // be removed. When removing it, make sure the value is correctly converted
  2344. // on big-endian CPUs.
  2345. // The word is already stored correctly in the argument
  2346. *(l_fp - asBC_SWORDARG0(l_bc)) = asBC_DWORDARG(l_bc);
  2347. l_bc += 2;
  2348. break;
  2349. case asBC_Cast:
  2350. // Cast the handle at the top of the stack to the type in the argument
  2351. {
  2352. asDWORD **a = (asDWORD**)*(size_t*)l_sp;
  2353. if( a && *a )
  2354. {
  2355. asDWORD typeId = asBC_DWORDARG(l_bc);
  2356. asCScriptObject *obj = (asCScriptObject *)* a;
  2357. asCObjectType *objType = obj->objType;
  2358. asCObjectType *to = engine->GetObjectTypeFromTypeId(typeId);
  2359. // This instruction can only be used with script classes and interfaces
  2360. asASSERT( objType->flags & asOBJ_SCRIPT_OBJECT );
  2361. asASSERT( to->flags & asOBJ_SCRIPT_OBJECT );
  2362. if( objType->Implements(to) || objType->DerivesFrom(to) )
  2363. {
  2364. regs.objectType = 0;
  2365. regs.objectRegister = obj;
  2366. obj->AddRef();
  2367. }
  2368. else
  2369. {
  2370. // The object register should already be null, so there
  2371. // is no need to clear it if the cast is unsuccessful
  2372. asASSERT( regs.objectRegister == 0 );
  2373. }
  2374. }
  2375. l_sp += AS_PTR_SIZE;
  2376. }
  2377. l_bc += 2;
  2378. break;
  2379. case asBC_i64TOi:
  2380. *(l_fp - asBC_SWORDARG0(l_bc)) = int(*(asINT64*)(l_fp - asBC_SWORDARG1(l_bc)));
  2381. l_bc += 2;
  2382. break;
  2383. case asBC_uTOi64:
  2384. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = asINT64(*(asUINT*)(l_fp - asBC_SWORDARG1(l_bc)));
  2385. l_bc += 2;
  2386. break;
  2387. case asBC_iTOi64:
  2388. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = asINT64(*(int*)(l_fp - asBC_SWORDARG1(l_bc)));
  2389. l_bc += 2;
  2390. break;
  2391. case asBC_fTOi64:
  2392. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = asINT64(*(float*)(l_fp - asBC_SWORDARG1(l_bc)));
  2393. l_bc += 2;
  2394. break;
  2395. case asBC_dTOi64:
  2396. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = asINT64(*(double*)(l_fp - asBC_SWORDARG0(l_bc)));
  2397. l_bc++;
  2398. break;
  2399. case asBC_fTOu64:
  2400. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = asQWORD(asINT64(*(float*)(l_fp - asBC_SWORDARG1(l_bc))));
  2401. l_bc += 2;
  2402. break;
  2403. case asBC_dTOu64:
  2404. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = asQWORD(asINT64(*(double*)(l_fp - asBC_SWORDARG0(l_bc))));
  2405. l_bc++;
  2406. break;
  2407. case asBC_i64TOf:
  2408. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = float(*(asINT64*)(l_fp - asBC_SWORDARG1(l_bc)));
  2409. l_bc += 2;
  2410. break;
  2411. case asBC_u64TOf:
  2412. #if _MSC_VER <= 1200 // MSVC6
  2413. {
  2414. // MSVC6 doesn't permit UINT64 to double
  2415. asINT64 v = *(asINT64*)(l_fp - asBC_SWORDARG1(l_bc));
  2416. if( v < 0 )
  2417. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = 18446744073709551615.0f+float(v);
  2418. else
  2419. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = float(v);
  2420. }
  2421. #else
  2422. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = float(*(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)));
  2423. #endif
  2424. l_bc += 2;
  2425. break;
  2426. case asBC_i64TOd:
  2427. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = double(*(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)));
  2428. l_bc++;
  2429. break;
  2430. case asBC_u64TOd:
  2431. #if _MSC_VER <= 1200 // MSVC6
  2432. {
  2433. // MSVC6 doesn't permit UINT64 to double
  2434. asINT64 v = *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc));
  2435. if( v < 0 )
  2436. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = 18446744073709551615.0+double(v);
  2437. else
  2438. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = double(v);
  2439. }
  2440. #else
  2441. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = double(*(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)));
  2442. #endif
  2443. l_bc++;
  2444. break;
  2445. case asBC_NEGi64:
  2446. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = -*(asINT64*)(l_fp - asBC_SWORDARG0(l_bc));
  2447. l_bc++;
  2448. break;
  2449. case asBC_INCi64:
  2450. ++(**(asQWORD**)&regs.valueRegister);
  2451. l_bc++;
  2452. break;
  2453. case asBC_DECi64:
  2454. --(**(asQWORD**)&regs.valueRegister);
  2455. l_bc++;
  2456. break;
  2457. case asBC_BNOT64:
  2458. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = ~*(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2459. l_bc++;
  2460. break;
  2461. case asBC_ADDi64:
  2462. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) + *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  2463. l_bc += 2;
  2464. break;
  2465. case asBC_SUBi64:
  2466. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) - *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  2467. l_bc += 2;
  2468. break;
  2469. case asBC_MULi64:
  2470. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) * *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  2471. l_bc += 2;
  2472. break;
  2473. case asBC_DIVi64:
  2474. {
  2475. asINT64 divider = *(asINT64*)(l_fp - asBC_SWORDARG2(l_bc));
  2476. if( divider == 0 )
  2477. {
  2478. // Need to move the values back to the context
  2479. regs.programPointer = l_bc;
  2480. regs.stackPointer = l_sp;
  2481. regs.stackFramePointer = l_fp;
  2482. // Raise exception
  2483. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2484. return;
  2485. }
  2486. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asINT64*)(l_fp - asBC_SWORDARG1(l_bc)) / divider;
  2487. }
  2488. l_bc += 2;
  2489. break;
  2490. case asBC_MODi64:
  2491. {
  2492. asINT64 divider = *(asINT64*)(l_fp - asBC_SWORDARG2(l_bc));
  2493. if( divider == 0 )
  2494. {
  2495. // Need to move the values back to the context
  2496. regs.programPointer = l_bc;
  2497. regs.stackPointer = l_sp;
  2498. regs.stackFramePointer = l_fp;
  2499. // Raise exception
  2500. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2501. return;
  2502. }
  2503. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asINT64*)(l_fp - asBC_SWORDARG1(l_bc)) % divider;
  2504. }
  2505. l_bc += 2;
  2506. break;
  2507. case asBC_BAND64:
  2508. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) & *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  2509. l_bc += 2;
  2510. break;
  2511. case asBC_BOR64:
  2512. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) | *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  2513. l_bc += 2;
  2514. break;
  2515. case asBC_BXOR64:
  2516. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) ^ *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  2517. l_bc += 2;
  2518. break;
  2519. case asBC_BSLL64:
  2520. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) << *(l_fp - asBC_SWORDARG2(l_bc));
  2521. l_bc += 2;
  2522. break;
  2523. case asBC_BSRL64:
  2524. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) >> *(l_fp - asBC_SWORDARG2(l_bc));
  2525. l_bc += 2;
  2526. break;
  2527. case asBC_BSRA64:
  2528. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asINT64*)(l_fp - asBC_SWORDARG1(l_bc)) >> *(l_fp - asBC_SWORDARG2(l_bc));
  2529. l_bc += 2;
  2530. break;
  2531. case asBC_CMPi64:
  2532. {
  2533. asINT64 i = *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) - *(asINT64*)(l_fp - asBC_SWORDARG1(l_bc));
  2534. if( i == 0 ) *(int*)&regs.valueRegister = 0;
  2535. else if( i < 0 ) *(int*)&regs.valueRegister = -1;
  2536. else *(int*)&regs.valueRegister = 1;
  2537. l_bc += 2;
  2538. }
  2539. break;
  2540. case asBC_CMPu64:
  2541. {
  2542. asQWORD d = *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2543. asQWORD d2 = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc));
  2544. if( d == d2 ) *(int*)&regs.valueRegister = 0;
  2545. else if( d < d2 ) *(int*)&regs.valueRegister = -1;
  2546. else *(int*)&regs.valueRegister = 1;
  2547. l_bc += 2;
  2548. }
  2549. break;
  2550. case asBC_ChkNullS:
  2551. {
  2552. // Verify if the pointer on the stack is null
  2553. // This is used for example when validating handles passed as function arguments
  2554. size_t a = *(size_t*)(l_sp + asBC_WORDARG0(l_bc));
  2555. if( a == 0 )
  2556. {
  2557. regs.programPointer = l_bc;
  2558. regs.stackPointer = l_sp;
  2559. regs.stackFramePointer = l_fp;
  2560. SetInternalException(TXT_NULL_POINTER_ACCESS);
  2561. return;
  2562. }
  2563. }
  2564. l_bc++;
  2565. break;
  2566. case asBC_ClrHi:
  2567. #if AS_SIZEOF_BOOL == 1
  2568. {
  2569. // Clear the upper bytes, so that trash data don't interfere with boolean operations
  2570. // We need to use volatile here to tell the compiler it cannot
  2571. // change the order of read and write operations on the pointer.
  2572. volatile asBYTE *ptr = (asBYTE*)&regs.valueRegister;
  2573. ptr[1] = 0; // The boolean value is stored in the lower byte, so we clear the rest
  2574. ptr[2] = 0;
  2575. ptr[3] = 0;
  2576. }
  2577. #else
  2578. // We don't have anything to do here
  2579. #endif
  2580. l_bc++;
  2581. break;
  2582. case asBC_JitEntry:
  2583. {
  2584. if( currentFunction->jitFunction )
  2585. {
  2586. unsigned int jitOffset = asBC_WORDARG0(l_bc);
  2587. if( jitOffset )
  2588. {
  2589. // Resume JIT operation
  2590. regs.programPointer = l_bc;
  2591. regs.stackPointer = l_sp;
  2592. regs.stackFramePointer = l_fp;
  2593. // TODO: JIT: We should return from this function if the jitFunction tells us to
  2594. (currentFunction->jitFunction)(&regs, jitOffset-1);
  2595. l_bc = regs.programPointer;
  2596. l_sp = regs.stackPointer;
  2597. l_fp = regs.stackFramePointer;
  2598. break;
  2599. }
  2600. }
  2601. // Not a JIT resume point, treat as nop
  2602. l_bc++;
  2603. }
  2604. break;
  2605. case asBC_CallPtr:
  2606. {
  2607. // Get the function pointer from the local variable
  2608. asCScriptFunction *func = *(asCScriptFunction**)(l_fp - asBC_SWORDARG0(l_bc));
  2609. l_bc++;
  2610. // Need to move the values back to the context
  2611. regs.programPointer = l_bc;
  2612. regs.stackPointer = l_sp;
  2613. regs.stackFramePointer = l_fp;
  2614. if( func == 0 )
  2615. {
  2616. // TODO: funcdef: Should we have a different exception string?
  2617. SetInternalException(TXT_UNBOUND_FUNCTION);
  2618. return;
  2619. }
  2620. else
  2621. CallScriptFunction(func);
  2622. // Extract the values from the context again
  2623. l_bc = regs.programPointer;
  2624. l_sp = regs.stackPointer;
  2625. l_fp = regs.stackFramePointer;
  2626. // If status isn't active anymore then we must stop
  2627. if( status != asEXECUTION_ACTIVE )
  2628. return;
  2629. }
  2630. break;
  2631. case asBC_FuncPtr:
  2632. // Push the function pointer on the stack. The pointer is in the argument
  2633. l_sp -= AS_PTR_SIZE;
  2634. *(asPTRWORD*)l_sp = asBC_PTRARG(l_bc);
  2635. l_bc += 1+AS_PTR_SIZE;
  2636. break;
  2637. case asBC_LoadThisR:
  2638. {
  2639. // PshVPtr 0
  2640. asPTRWORD tmp = *(asPTRWORD*)l_fp;
  2641. // ADDSi
  2642. tmp = tmp + asBC_SWORDARG0(l_bc);
  2643. // PopRPtr
  2644. *(asPTRWORD*)&regs.valueRegister = tmp;
  2645. l_bc += 2;
  2646. }
  2647. break;
  2648. // Push the qword value of a variable on the stack
  2649. case asBC_PshV8:
  2650. l_sp -= 2;
  2651. *(asQWORD*)l_sp = *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2652. l_bc++;
  2653. break;
  2654. case asBC_DIVu:
  2655. {
  2656. asUINT divider = *(asUINT*)(l_fp - asBC_SWORDARG2(l_bc));
  2657. if( divider == 0 )
  2658. {
  2659. // Need to move the values back to the context
  2660. regs.programPointer = l_bc;
  2661. regs.stackPointer = l_sp;
  2662. regs.stackFramePointer = l_fp;
  2663. // Raise exception
  2664. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2665. return;
  2666. }
  2667. *(asUINT*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asUINT*)(l_fp - asBC_SWORDARG1(l_bc)) / divider;
  2668. }
  2669. l_bc += 2;
  2670. break;
  2671. case asBC_MODu:
  2672. {
  2673. asUINT divider = *(asUINT*)(l_fp - asBC_SWORDARG2(l_bc));
  2674. if( divider == 0 )
  2675. {
  2676. // Need to move the values back to the context
  2677. regs.programPointer = l_bc;
  2678. regs.stackPointer = l_sp;
  2679. regs.stackFramePointer = l_fp;
  2680. // Raise exception
  2681. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2682. return;
  2683. }
  2684. *(asUINT*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asUINT*)(l_fp - asBC_SWORDARG1(l_bc)) % divider;
  2685. }
  2686. l_bc += 2;
  2687. break;
  2688. case asBC_DIVu64:
  2689. {
  2690. asQWORD divider = *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  2691. if( divider == 0 )
  2692. {
  2693. // Need to move the values back to the context
  2694. regs.programPointer = l_bc;
  2695. regs.stackPointer = l_sp;
  2696. regs.stackFramePointer = l_fp;
  2697. // Raise exception
  2698. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2699. return;
  2700. }
  2701. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) / divider;
  2702. }
  2703. l_bc += 2;
  2704. break;
  2705. case asBC_MODu64:
  2706. {
  2707. asQWORD divider = *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  2708. if( divider == 0 )
  2709. {
  2710. // Need to move the values back to the context
  2711. regs.programPointer = l_bc;
  2712. regs.stackPointer = l_sp;
  2713. regs.stackFramePointer = l_fp;
  2714. // Raise exception
  2715. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2716. return;
  2717. }
  2718. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) % divider;
  2719. }
  2720. l_bc += 2;
  2721. break;
  2722. case asBC_LoadRObjR:
  2723. {
  2724. // PshVPtr x
  2725. asPTRWORD tmp = *(asPTRWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2726. // ADDSi y
  2727. tmp = tmp + asBC_SWORDARG1(l_bc);
  2728. // PopRPtr
  2729. *(asPTRWORD*)&regs.valueRegister = tmp;
  2730. l_bc += 3;
  2731. }
  2732. break;
  2733. case asBC_LoadVObjR:
  2734. {
  2735. // PSF x
  2736. asPTRWORD tmp = (asPTRWORD)(size_t)(l_fp - asBC_SWORDARG0(l_bc));
  2737. // ADDSi y
  2738. tmp = tmp + asBC_SWORDARG1(l_bc);
  2739. // PopRPtr
  2740. *(asPTRWORD*)&regs.valueRegister = tmp;
  2741. l_bc += 3;
  2742. }
  2743. break;
  2744. // Don't let the optimizer optimize for size,
  2745. // since it requires extra conditions and jumps
  2746. case 186: l_bc = (asDWORD*)186; break;
  2747. case 187: l_bc = (asDWORD*)187; break;
  2748. case 188: l_bc = (asDWORD*)188; break;
  2749. case 189: l_bc = (asDWORD*)189; break;
  2750. case 190: l_bc = (asDWORD*)190; break;
  2751. case 191: l_bc = (asDWORD*)191; break;
  2752. case 192: l_bc = (asDWORD*)192; break;
  2753. case 193: l_bc = (asDWORD*)193; break;
  2754. case 194: l_bc = (asDWORD*)194; break;
  2755. case 195: l_bc = (asDWORD*)195; break;
  2756. case 196: l_bc = (asDWORD*)196; break;
  2757. case 197: l_bc = (asDWORD*)197; break;
  2758. case 198: l_bc = (asDWORD*)198; break;
  2759. case 199: l_bc = (asDWORD*)199; break;
  2760. case 200: l_bc = (asDWORD*)200; break;
  2761. case 201: l_bc = (asDWORD*)201; break;
  2762. case 202: l_bc = (asDWORD*)202; break;
  2763. case 203: l_bc = (asDWORD*)203; break;
  2764. case 204: l_bc = (asDWORD*)204; break;
  2765. case 205: l_bc = (asDWORD*)205; break;
  2766. case 206: l_bc = (asDWORD*)206; break;
  2767. case 207: l_bc = (asDWORD*)207; break;
  2768. case 208: l_bc = (asDWORD*)208; break;
  2769. case 209: l_bc = (asDWORD*)209; break;
  2770. case 210: l_bc = (asDWORD*)210; break;
  2771. case 211: l_bc = (asDWORD*)211; break;
  2772. case 212: l_bc = (asDWORD*)212; break;
  2773. case 213: l_bc = (asDWORD*)213; break;
  2774. case 214: l_bc = (asDWORD*)214; break;
  2775. case 215: l_bc = (asDWORD*)215; break;
  2776. case 216: l_bc = (asDWORD*)216; break;
  2777. case 217: l_bc = (asDWORD*)217; break;
  2778. case 218: l_bc = (asDWORD*)218; break;
  2779. case 219: l_bc = (asDWORD*)219; break;
  2780. case 220: l_bc = (asDWORD*)220; break;
  2781. case 221: l_bc = (asDWORD*)221; break;
  2782. case 222: l_bc = (asDWORD*)222; break;
  2783. case 223: l_bc = (asDWORD*)223; break;
  2784. case 224: l_bc = (asDWORD*)224; break;
  2785. case 225: l_bc = (asDWORD*)225; break;
  2786. case 226: l_bc = (asDWORD*)226; break;
  2787. case 227: l_bc = (asDWORD*)227; break;
  2788. case 228: l_bc = (asDWORD*)228; break;
  2789. case 229: l_bc = (asDWORD*)229; break;
  2790. case 230: l_bc = (asDWORD*)230; break;
  2791. case 231: l_bc = (asDWORD*)231; break;
  2792. case 232: l_bc = (asDWORD*)232; break;
  2793. case 233: l_bc = (asDWORD*)233; break;
  2794. case 234: l_bc = (asDWORD*)234; break;
  2795. case 235: l_bc = (asDWORD*)235; break;
  2796. case 236: l_bc = (asDWORD*)236; break;
  2797. case 237: l_bc = (asDWORD*)237; break;
  2798. case 238: l_bc = (asDWORD*)238; break;
  2799. case 239: l_bc = (asDWORD*)239; break;
  2800. case 240: l_bc = (asDWORD*)240; break;
  2801. case 241: l_bc = (asDWORD*)241; break;
  2802. case 242: l_bc = (asDWORD*)242; break;
  2803. case 243: l_bc = (asDWORD*)243; break;
  2804. case 244: l_bc = (asDWORD*)244; break;
  2805. case 245: l_bc = (asDWORD*)245; break;
  2806. case 246: l_bc = (asDWORD*)246; break;
  2807. case 247: l_bc = (asDWORD*)247; break;
  2808. case 248: l_bc = (asDWORD*)248; break;
  2809. case 249: l_bc = (asDWORD*)249; break;
  2810. case 250: l_bc = (asDWORD*)250; break;
  2811. case 251: l_bc = (asDWORD*)251; break;
  2812. case 252: l_bc = (asDWORD*)252; break;
  2813. case 253: l_bc = (asDWORD*)253; break;
  2814. case 254: l_bc = (asDWORD*)254; break;
  2815. case 255: l_bc = (asDWORD*)255; break;
  2816. #ifdef AS_DEBUG
  2817. default:
  2818. asASSERT(false);
  2819. #endif
  2820. /*
  2821. default:
  2822. // This Microsoft specific code allows the
  2823. // compiler to optimize the switch case as
  2824. // it will know that the code will never
  2825. // reach this point
  2826. __assume(0);
  2827. */ }
  2828. #ifdef AS_DEBUG
  2829. asDWORD instr = *(asBYTE*)old;
  2830. if( instr != asBC_JMP && instr != asBC_JMPP && (instr < asBC_JZ || instr > asBC_JNP) &&
  2831. instr != asBC_CALL && instr != asBC_CALLBND && instr != asBC_CALLINTF && instr != asBC_RET && instr != asBC_ALLOC && instr != asBC_CallPtr )
  2832. {
  2833. asASSERT( (l_bc - old) == asBCTypeSize[asBCInfo[instr].type] );
  2834. }
  2835. #endif
  2836. }
  2837. SetInternalException(TXT_UNRECOGNIZED_BYTE_CODE);
  2838. }
  2839. int asCContext::SetException(const char *descr)
  2840. {
  2841. // Only allow this if we're executing a CALL byte code
  2842. if( !isCallingSystemFunction ) return asERROR;
  2843. SetInternalException(descr);
  2844. return 0;
  2845. }
  2846. void asCContext::SetInternalException(const char *descr)
  2847. {
  2848. if( inExceptionHandler )
  2849. {
  2850. asASSERT(false); // Shouldn't happen
  2851. return; // but if it does, at least this will not crash the application
  2852. }
  2853. status = asEXECUTION_EXCEPTION;
  2854. regs.doProcessSuspend = true;
  2855. exceptionString = descr;
  2856. exceptionFunction = currentFunction->id;
  2857. exceptionLine = currentFunction->GetLineNumber(int(regs.programPointer - currentFunction->byteCode.AddressOf()));
  2858. exceptionColumn = exceptionLine >> 20;
  2859. exceptionLine &= 0xFFFFF;
  2860. if( exceptionCallback )
  2861. CallExceptionCallback();
  2862. }
  2863. void asCContext::CleanReturnObject()
  2864. {
  2865. if( regs.objectRegister == 0 ) return;
  2866. asASSERT( regs.objectType != 0 );
  2867. if( regs.objectType )
  2868. {
  2869. // Call the destructor on the object
  2870. asSTypeBehaviour *beh = &((asCObjectType*)regs.objectType)->beh;
  2871. if( beh->release )
  2872. {
  2873. engine->CallObjectMethod(regs.objectRegister, beh->release);
  2874. regs.objectRegister = 0;
  2875. // The release method is responsible for freeing the memory
  2876. }
  2877. else
  2878. {
  2879. if( beh->destruct )
  2880. engine->CallObjectMethod(regs.objectRegister, beh->destruct);
  2881. // Free the memory
  2882. engine->CallFree(regs.objectRegister);
  2883. regs.objectRegister = 0;
  2884. }
  2885. }
  2886. }
  2887. void asCContext::CleanStack()
  2888. {
  2889. inExceptionHandler = true;
  2890. // Run the clean up code for each of the functions called
  2891. CleanStackFrame();
  2892. while( callStack.GetLength() > 0 )
  2893. {
  2894. PopCallState();
  2895. CleanStackFrame();
  2896. }
  2897. inExceptionHandler = false;
  2898. }
  2899. // Interface
  2900. bool asCContext::IsVarInScope(asUINT varIndex, asUINT stackLevel)
  2901. {
  2902. asASSERT( stackLevel < GetCallstackSize() );
  2903. asCScriptFunction *func;
  2904. asUINT pos;
  2905. if( stackLevel == 0 )
  2906. {
  2907. func = currentFunction;
  2908. pos = asUINT(regs.programPointer - func->byteCode.AddressOf());
  2909. }
  2910. else
  2911. {
  2912. size_t *s = callStack.AddressOf() + (GetCallstackSize()-stackLevel-1)*CALLSTACK_FRAME_SIZE;
  2913. func = (asCScriptFunction*)s[1];
  2914. pos = asUINT((asDWORD*)s[2] - func->byteCode.AddressOf());
  2915. }
  2916. // First determine if the program position is after the variable declaration
  2917. if( func->variables.GetLength() <= varIndex ) return false;
  2918. if( func->variables[varIndex]->declaredAtProgramPos > pos ) return false;
  2919. asUINT declaredAt = func->variables[varIndex]->declaredAtProgramPos;
  2920. // If the program position is after the variable declaration it is necessary
  2921. // determine if the program position is still inside the statement block where
  2922. // the variable was delcared.
  2923. for( int n = 0; n < (int)func->objVariableInfo.GetLength(); n++ )
  2924. {
  2925. if( func->objVariableInfo[n].programPos >= declaredAt )
  2926. {
  2927. // If the current block ends between the declaredAt and current
  2928. // program position, then we know the variable is no longer visible
  2929. int level = 0;
  2930. for( ; n < (int)func->objVariableInfo.GetLength(); n++ )
  2931. {
  2932. if( func->objVariableInfo[n].programPos > pos )
  2933. break;
  2934. if( func->objVariableInfo[n].option == asBLOCK_BEGIN ) level++;
  2935. if( func->objVariableInfo[n].option == asBLOCK_END && --level < 0 )
  2936. return false;
  2937. }
  2938. break;
  2939. }
  2940. }
  2941. // Variable is visible
  2942. return true;
  2943. }
  2944. // Internal
  2945. void asCContext::DetermineLiveObjects(asCArray<int> &liveObjects, asUINT stackLevel)
  2946. {
  2947. asASSERT( stackLevel < GetCallstackSize() );
  2948. asCScriptFunction *func;
  2949. asUINT pos;
  2950. if( stackLevel == 0 )
  2951. {
  2952. func = currentFunction;
  2953. pos = asUINT(regs.programPointer - func->byteCode.AddressOf());
  2954. }
  2955. else
  2956. {
  2957. size_t *s = callStack.AddressOf() + (GetCallstackSize()-stackLevel-1)*CALLSTACK_FRAME_SIZE;
  2958. func = (asCScriptFunction*)s[1];
  2959. pos = asUINT((asDWORD*)s[2] - func->byteCode.AddressOf());
  2960. }
  2961. // Determine which object variables that are really live ones
  2962. liveObjects.SetLength(func->objVariablePos.GetLength());
  2963. memset(liveObjects.AddressOf(), 0, sizeof(int)*liveObjects.GetLength());
  2964. for( int n = 0; n < (int)func->objVariableInfo.GetLength(); n++ )
  2965. {
  2966. if( func->objVariableInfo[n].programPos >= pos )
  2967. {
  2968. // We've determined how far the execution ran, now determine which variables are alive
  2969. for( --n; n >= 0; n-- )
  2970. {
  2971. switch( func->objVariableInfo[n].option )
  2972. {
  2973. case asOBJ_UNINIT: // Object was destroyed
  2974. {
  2975. // TODO: optimize: This should have been done by the compiler already
  2976. // Which variable is this?
  2977. asUINT var = 0;
  2978. for( asUINT v = 0; v < func->objVariablePos.GetLength(); v++ )
  2979. if( func->objVariablePos[v] == func->objVariableInfo[n].variableOffset )
  2980. {
  2981. var = v;
  2982. break;
  2983. }
  2984. liveObjects[var] -= 1;
  2985. }
  2986. break;
  2987. case asOBJ_INIT: // Object was created
  2988. {
  2989. // Which variable is this?
  2990. asUINT var = 0;
  2991. for( asUINT v = 0; v < func->objVariablePos.GetLength(); v++ )
  2992. if( func->objVariablePos[v] == func->objVariableInfo[n].variableOffset )
  2993. {
  2994. var = v;
  2995. break;
  2996. }
  2997. liveObjects[var] += 1;
  2998. }
  2999. break;
  3000. case asBLOCK_BEGIN: // Start block
  3001. // We should ignore start blocks, since it just means the
  3002. // program was within the block when the exception ocurred
  3003. break;
  3004. case asBLOCK_END: // End block
  3005. // We need to skip the entire block, as the objects created
  3006. // and destroyed inside this block are already out of scope
  3007. {
  3008. int nested = 1;
  3009. while( nested > 0 )
  3010. {
  3011. int option = func->objVariableInfo[--n].option;
  3012. if( option == 3 )
  3013. nested++;
  3014. if( option == 2 )
  3015. nested--;
  3016. }
  3017. }
  3018. break;
  3019. }
  3020. }
  3021. // We're done with the investigation
  3022. break;
  3023. }
  3024. }
  3025. }
  3026. void asCContext::CleanStackFrame()
  3027. {
  3028. // Clean object variables
  3029. if( !isStackMemoryNotAllocated )
  3030. {
  3031. // Determine which object variables that are really live ones
  3032. asCArray<int> liveObjects;
  3033. DetermineLiveObjects(liveObjects, 0);
  3034. for( asUINT n = 0; n < currentFunction->objVariablePos.GetLength(); n++ )
  3035. {
  3036. int pos = currentFunction->objVariablePos[n];
  3037. if( currentFunction->objVariableIsOnHeap[n] )
  3038. {
  3039. // Check if the pointer is initialized
  3040. if( *(size_t*)&regs.stackFramePointer[-pos] )
  3041. {
  3042. // Call the object's destructor
  3043. asSTypeBehaviour *beh = &currentFunction->objVariableTypes[n]->beh;
  3044. if( beh->release )
  3045. {
  3046. engine->CallObjectMethod((void*)*(size_t*)&regs.stackFramePointer[-pos], beh->release);
  3047. *(size_t*)&regs.stackFramePointer[-pos] = 0;
  3048. }
  3049. else
  3050. {
  3051. if( beh->destruct )
  3052. engine->CallObjectMethod((void*)*(size_t*)&regs.stackFramePointer[-pos], beh->destruct);
  3053. // Free the memory
  3054. engine->CallFree((void*)*(size_t*)&regs.stackFramePointer[-pos]);
  3055. *(size_t*)&regs.stackFramePointer[-pos] = 0;
  3056. }
  3057. }
  3058. }
  3059. else
  3060. {
  3061. asASSERT( currentFunction->objVariableTypes[n]->GetFlags() & asOBJ_VALUE );
  3062. // Only destroy the object if it is truly alive
  3063. if( liveObjects[n] > 0 )
  3064. {
  3065. asSTypeBehaviour *beh = &currentFunction->objVariableTypes[n]->beh;
  3066. if( beh->destruct )
  3067. engine->CallObjectMethod((void*)(size_t*)&regs.stackFramePointer[-pos], beh->destruct);
  3068. }
  3069. }
  3070. }
  3071. // If the object is a script declared object, then we must release it
  3072. // as the compiler adds a reference at the entry of the function. Make sure
  3073. // the function has actually been entered
  3074. if( currentFunction->objectType && regs.programPointer != currentFunction->byteCode.AddressOf() )
  3075. {
  3076. asSTypeBehaviour *beh = &currentFunction->objectType->beh;
  3077. if( beh->release && *(size_t*)&regs.stackFramePointer[0] != 0 )
  3078. {
  3079. engine->CallObjectMethod((void*)*(size_t*)&regs.stackFramePointer[0], beh->release);
  3080. *(size_t*)&regs.stackFramePointer[0] = 0;
  3081. }
  3082. }
  3083. }
  3084. else
  3085. isStackMemoryNotAllocated = false;
  3086. // Functions that do not own the object and parameters shouldn't do any clean up
  3087. if( currentFunction->dontCleanUpOnException )
  3088. return;
  3089. // Clean object and parameters
  3090. int offset = 0;
  3091. if( currentFunction->objectType )
  3092. {
  3093. offset += AS_PTR_SIZE;
  3094. }
  3095. for( asUINT n = 0; n < currentFunction->parameterTypes.GetLength(); n++ )
  3096. {
  3097. if( currentFunction->parameterTypes[n].IsObject() && !currentFunction->parameterTypes[n].IsReference() )
  3098. {
  3099. if( *(size_t*)&regs.stackFramePointer[offset] )
  3100. {
  3101. // Call the object's destructor
  3102. asSTypeBehaviour *beh = currentFunction->parameterTypes[n].GetBehaviour();
  3103. if( beh->release )
  3104. {
  3105. engine->CallObjectMethod((void*)*(size_t*)&regs.stackFramePointer[offset], beh->release);
  3106. *(size_t*)&regs.stackFramePointer[offset] = 0;
  3107. }
  3108. else
  3109. {
  3110. if( beh->destruct )
  3111. engine->CallObjectMethod((void*)*(size_t*)&regs.stackFramePointer[offset], beh->destruct);
  3112. // Free the memory
  3113. engine->CallFree((void*)*(size_t*)&regs.stackFramePointer[offset]);
  3114. *(size_t*)&regs.stackFramePointer[offset] = 0;
  3115. }
  3116. }
  3117. }
  3118. offset += currentFunction->parameterTypes[n].GetSizeOnStackDWords();
  3119. }
  3120. }
  3121. // interface
  3122. int asCContext::GetExceptionLineNumber(int *column, const char **sectionName)
  3123. {
  3124. if( GetState() != asEXECUTION_EXCEPTION ) return asERROR;
  3125. if( column ) *column = exceptionColumn;
  3126. if( sectionName ) *sectionName = engine->scriptFunctions[exceptionFunction]->GetScriptSectionName();
  3127. return exceptionLine;
  3128. }
  3129. // interface
  3130. int asCContext::GetExceptionFunction()
  3131. {
  3132. if( GetState() != asEXECUTION_EXCEPTION ) return asERROR;
  3133. return exceptionFunction;
  3134. }
  3135. // interface
  3136. const char *asCContext::GetExceptionString()
  3137. {
  3138. if( GetState() != asEXECUTION_EXCEPTION ) return 0;
  3139. return exceptionString.AddressOf();
  3140. }
  3141. // interface
  3142. asEContextState asCContext::GetState() const
  3143. {
  3144. return status;
  3145. }
  3146. // interface
  3147. int asCContext::SetLineCallback(asSFuncPtr callback, void *obj, int callConv)
  3148. {
  3149. lineCallback = true;
  3150. regs.doProcessSuspend = true;
  3151. lineCallbackObj = obj;
  3152. bool isObj = false;
  3153. if( (unsigned)callConv == asCALL_GENERIC )
  3154. {
  3155. lineCallback = false;
  3156. regs.doProcessSuspend = doSuspend;
  3157. return asNOT_SUPPORTED;
  3158. }
  3159. if( (unsigned)callConv >= asCALL_THISCALL )
  3160. {
  3161. isObj = true;
  3162. if( obj == 0 )
  3163. {
  3164. lineCallback = false;
  3165. regs.doProcessSuspend = doSuspend;
  3166. return asINVALID_ARG;
  3167. }
  3168. }
  3169. int r = DetectCallingConvention(isObj, callback, callConv, &lineCallbackFunc);
  3170. if( r < 0 ) lineCallback = false;
  3171. regs.doProcessSuspend = doSuspend || lineCallback;
  3172. return r;
  3173. }
  3174. void asCContext::CallLineCallback()
  3175. {
  3176. if( lineCallbackFunc.callConv < ICC_THISCALL )
  3177. engine->CallGlobalFunction(this, lineCallbackObj, &lineCallbackFunc, 0);
  3178. else
  3179. engine->CallObjectMethod(lineCallbackObj, this, &lineCallbackFunc, 0);
  3180. }
  3181. // interface
  3182. int asCContext::SetExceptionCallback(asSFuncPtr callback, void *obj, int callConv)
  3183. {
  3184. exceptionCallback = true;
  3185. exceptionCallbackObj = obj;
  3186. bool isObj = false;
  3187. if( (unsigned)callConv == asCALL_GENERIC )
  3188. return asNOT_SUPPORTED;
  3189. if( (unsigned)callConv >= asCALL_THISCALL )
  3190. {
  3191. isObj = true;
  3192. if( obj == 0 )
  3193. {
  3194. exceptionCallback = false;
  3195. return asINVALID_ARG;
  3196. }
  3197. }
  3198. int r = DetectCallingConvention(isObj, callback, callConv, &exceptionCallbackFunc);
  3199. if( r < 0 ) exceptionCallback = false;
  3200. return r;
  3201. }
  3202. void asCContext::CallExceptionCallback()
  3203. {
  3204. if( exceptionCallbackFunc.callConv < ICC_THISCALL )
  3205. engine->CallGlobalFunction(this, exceptionCallbackObj, &exceptionCallbackFunc, 0);
  3206. else
  3207. engine->CallObjectMethod(exceptionCallbackObj, this, &exceptionCallbackFunc, 0);
  3208. }
  3209. // interface
  3210. void asCContext::ClearLineCallback()
  3211. {
  3212. lineCallback = false;
  3213. regs.doProcessSuspend = doSuspend;
  3214. }
  3215. // interface
  3216. void asCContext::ClearExceptionCallback()
  3217. {
  3218. exceptionCallback = false;
  3219. }
  3220. int asCContext::CallGeneric(int id, void *objectPointer)
  3221. {
  3222. asCScriptFunction *sysFunction = engine->scriptFunctions[id];
  3223. asSSystemFunctionInterface *sysFunc = sysFunction->sysFuncIntf;
  3224. void (*func)(asIScriptGeneric*) = (void (*)(asIScriptGeneric*))sysFunc->func;
  3225. int popSize = sysFunc->paramSize;
  3226. asDWORD *args = regs.stackPointer;
  3227. // Verify the object pointer if it is a class method
  3228. void *currentObject = 0;
  3229. if( sysFunc->callConv == ICC_GENERIC_METHOD )
  3230. {
  3231. if( objectPointer )
  3232. {
  3233. currentObject = objectPointer;
  3234. // Don't increase the reference of this pointer
  3235. // since it will not have been constructed yet
  3236. }
  3237. else
  3238. {
  3239. // The object pointer should be popped from the context stack
  3240. popSize += AS_PTR_SIZE;
  3241. // Check for null pointer
  3242. currentObject = (void*)*(size_t*)(args);
  3243. if( currentObject == 0 )
  3244. {
  3245. SetInternalException(TXT_NULL_POINTER_ACCESS);
  3246. return 0;
  3247. }
  3248. // Add the base offset for multiple inheritance
  3249. currentObject = (void*)(size_t(currentObject) + sysFunc->baseOffset);
  3250. // Skip object pointer
  3251. args += AS_PTR_SIZE;
  3252. }
  3253. }
  3254. asCGeneric gen(engine, sysFunction, currentObject, args);
  3255. isCallingSystemFunction = true;
  3256. func(&gen);
  3257. isCallingSystemFunction = false;
  3258. regs.valueRegister = gen.returnVal;
  3259. regs.objectRegister = gen.objectRegister;
  3260. regs.objectType = sysFunction->returnType.GetObjectType();
  3261. // Clean up function parameters
  3262. int offset = 0;
  3263. for( asUINT n = 0; n < sysFunction->parameterTypes.GetLength(); n++ )
  3264. {
  3265. if( sysFunction->parameterTypes[n].IsObject() && !sysFunction->parameterTypes[n].IsReference() )
  3266. {
  3267. void *obj = *(void**)&args[offset];
  3268. if( obj )
  3269. {
  3270. // Release the object
  3271. asSTypeBehaviour *beh = &sysFunction->parameterTypes[n].GetObjectType()->beh;
  3272. if( beh->release )
  3273. engine->CallObjectMethod(obj, beh->release);
  3274. else
  3275. {
  3276. // Call the destructor then free the memory
  3277. if( beh->destruct )
  3278. engine->CallObjectMethod(obj, beh->destruct);
  3279. engine->CallFree(obj);
  3280. }
  3281. }
  3282. }
  3283. offset += sysFunction->parameterTypes[n].GetSizeOnStackDWords();
  3284. }
  3285. // Return how much should be popped from the stack
  3286. return popSize;
  3287. }
  3288. // interface
  3289. int asCContext::GetVarCount(asUINT stackLevel)
  3290. {
  3291. asIScriptFunction *func = GetFunction(stackLevel);
  3292. if( func == 0 ) return asINVALID_ARG;
  3293. return func->GetVarCount();
  3294. }
  3295. // interface
  3296. const char *asCContext::GetVarName(asUINT varIndex, asUINT stackLevel)
  3297. {
  3298. asIScriptFunction *func = GetFunction(stackLevel);
  3299. if( func == 0 ) return 0;
  3300. const char *name = 0;
  3301. int r = func->GetVar(varIndex, &name);
  3302. return r >= 0 ? name : 0;
  3303. }
  3304. // interface
  3305. const char *asCContext::GetVarDeclaration(asUINT varIndex, asUINT stackLevel)
  3306. {
  3307. asIScriptFunction *func = GetFunction(stackLevel);
  3308. if( func == 0 ) return 0;
  3309. return func->GetVarDecl(varIndex);
  3310. }
  3311. // interface
  3312. int asCContext::GetVarTypeId(asUINT varIndex, asUINT stackLevel)
  3313. {
  3314. asIScriptFunction *func = GetFunction(stackLevel);
  3315. if( func == 0 ) return asINVALID_ARG;
  3316. int typeId;
  3317. int r = func->GetVar(varIndex, 0, &typeId);
  3318. return r < 0 ? r : typeId;
  3319. }
  3320. // interface
  3321. void *asCContext::GetAddressOfVar(asUINT varIndex, asUINT stackLevel)
  3322. {
  3323. if( stackLevel >= GetCallstackSize() ) return 0;
  3324. asCScriptFunction *func;
  3325. asDWORD *sf;
  3326. if( stackLevel == 0 )
  3327. {
  3328. func = currentFunction;
  3329. sf = regs.stackFramePointer;
  3330. }
  3331. else
  3332. {
  3333. size_t *s = callStack.AddressOf() + (GetCallstackSize()-stackLevel-1)*CALLSTACK_FRAME_SIZE;
  3334. func = (asCScriptFunction*)s[1];
  3335. sf = (asDWORD*)s[0];
  3336. }
  3337. if( func == 0 )
  3338. return 0;
  3339. if( varIndex >= func->variables.GetLength() )
  3340. return 0;
  3341. // For object variables it's necessary to dereference the pointer to get the address of the value
  3342. if( func->variables[varIndex]->type.IsObject() && !func->variables[varIndex]->type.IsObjectHandle() )
  3343. {
  3344. // Determine if the object is really on the heap
  3345. bool onHeap = true;
  3346. if( func->variables[varIndex]->type.GetObjectType()->GetFlags() & asOBJ_VALUE )
  3347. {
  3348. int pos = func->variables[varIndex]->stackOffset;
  3349. for( asUINT n = 0; n < func->objVariablePos.GetLength(); n++ )
  3350. {
  3351. if( func->objVariablePos[n] == pos )
  3352. {
  3353. onHeap = func->objVariableIsOnHeap[n];
  3354. if( !onHeap )
  3355. {
  3356. // If the object on the stack is not initialized return a null pointer instead
  3357. asCArray<int> liveObjects;
  3358. DetermineLiveObjects(liveObjects, stackLevel);
  3359. if( liveObjects[n] <= 0 )
  3360. return 0;
  3361. }
  3362. break;
  3363. }
  3364. }
  3365. }
  3366. if( onHeap )
  3367. return *(void**)(sf - func->variables[varIndex]->stackOffset);
  3368. }
  3369. return sf - func->variables[varIndex]->stackOffset;
  3370. }
  3371. // interface
  3372. // returns the typeId of the 'this' object at the given call stack level (-1 for current)
  3373. // returns 0 if the function call at the given stack level is not a method
  3374. int asCContext::GetThisTypeId(asUINT stackLevel)
  3375. {
  3376. asIScriptFunction *func = GetFunction(stackLevel);
  3377. if( func == 0 ) return asINVALID_ARG;
  3378. if( func->GetObjectType() == 0 )
  3379. return 0; // not in a method
  3380. // create a datatype
  3381. asCDataType dt = asCDataType::CreateObject((asCObjectType*)func->GetObjectType(), false);
  3382. // return a typeId from the data type
  3383. return engine->GetTypeIdFromDataType(dt);
  3384. }
  3385. // interface
  3386. // returns the 'this' object pointer at the given call stack level (-1 for current)
  3387. // returns 0 if the function call at the given stack level is not a method
  3388. void *asCContext::GetThisPointer(asUINT stackLevel)
  3389. {
  3390. if( stackLevel >= GetCallstackSize() )
  3391. return 0;
  3392. asCScriptFunction *func;
  3393. asDWORD *sf;
  3394. if( stackLevel == 0 )
  3395. {
  3396. func = currentFunction;
  3397. sf = regs.stackFramePointer;
  3398. }
  3399. else
  3400. {
  3401. size_t *s = callStack.AddressOf() + (GetCallstackSize()-stackLevel-1)*CALLSTACK_FRAME_SIZE;
  3402. func = (asCScriptFunction*)s[1];
  3403. sf = (asDWORD*)s[0];
  3404. }
  3405. if( func == 0 )
  3406. return 0;
  3407. if( func->objectType == 0 )
  3408. return 0; // not in a method
  3409. void *thisPointer = (void*)*(size_t*)(sf);
  3410. if( thisPointer == 0 )
  3411. {
  3412. return 0;
  3413. }
  3414. // NOTE: this returns the pointer to the 'this' while the GetVarPointer functions return
  3415. // a pointer to a pointer. I can't imagine someone would want to change the 'this'
  3416. return thisPointer;
  3417. }
  3418. END_AS_NAMESPACE