as_context.cpp 111 KB

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