as_context.cpp 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2013 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 PTRs reserved for exception handling
  44. // We need at least 1 PTR reserved for calling system functions
  45. const int RESERVE_STACK = 2*AS_PTR_SIZE;
  46. // For each script function call we push 9 PTRs on the call stack
  47. const int CALLSTACK_FRAME_SIZE = 9;
  48. #if defined(AS_DEBUG)
  49. class asCDebugStats
  50. {
  51. public:
  52. asCDebugStats()
  53. {
  54. memset(instrCount, 0, sizeof(instrCount));
  55. memset(instrCount2, 0, sizeof(instrCount2));
  56. lastBC = 255;
  57. }
  58. ~asCDebugStats()
  59. {
  60. // This code writes out some statistics for the VM.
  61. // It's useful for determining what needs to be optimized.
  62. _mkdir("AS_DEBUG");
  63. #if _MSC_VER >= 1500 && !defined(AS_MARMALADE)
  64. FILE *f;
  65. fopen_s(&f, "AS_DEBUG/stats.txt", "wt");
  66. #else
  67. FILE *f = fopen("AS_DEBUG/stats.txt", "wt");
  68. #endif
  69. if( f )
  70. {
  71. // Output instruction statistics
  72. fprintf(f, "\nTotal count\n");
  73. int n;
  74. for( n = 0; n < asBC_MAXBYTECODE; n++ )
  75. {
  76. if( asBCInfo[n].name && instrCount[n] > 0 )
  77. fprintf(f, "%-10.10s : %.0f\n", asBCInfo[n].name, instrCount[n]);
  78. }
  79. fprintf(f, "\nNever executed\n");
  80. for( n = 0; n < asBC_MAXBYTECODE; n++ )
  81. {
  82. if( asBCInfo[n].name && instrCount[n] == 0 )
  83. fprintf(f, "%-10.10s\n", asBCInfo[n].name);
  84. }
  85. fprintf(f, "\nSequences\n");
  86. for( n = 0; n < 256; n++ )
  87. {
  88. if( asBCInfo[n].name )
  89. {
  90. for( int m = 0; m < 256; m++ )
  91. {
  92. if( instrCount2[n][m] )
  93. fprintf(f, "%-10.10s, %-10.10s : %.0f\n", asBCInfo[n].name, asBCInfo[m].name, instrCount2[n][m]);
  94. }
  95. }
  96. }
  97. fclose(f);
  98. }
  99. }
  100. void Instr(asBYTE bc)
  101. {
  102. ++instrCount[bc];
  103. ++instrCount2[lastBC][bc];
  104. lastBC = bc;
  105. }
  106. // Instruction statistics
  107. double instrCount[256];
  108. double instrCount2[256][256];
  109. int lastBC;
  110. } stats;
  111. #endif
  112. AS_API asIScriptContext *asGetActiveContext()
  113. {
  114. asCThreadLocalData *tld = asCThreadManager::GetLocalData();
  115. if( tld->activeContexts.GetLength() == 0 )
  116. return 0;
  117. return tld->activeContexts[tld->activeContexts.GetLength()-1];
  118. }
  119. asCThreadLocalData *asPushActiveContext(asIScriptContext *ctx)
  120. {
  121. asCThreadLocalData *tld = asCThreadManager::GetLocalData();
  122. tld->activeContexts.PushLast(ctx);
  123. return tld;
  124. }
  125. asCContext::asCContext(asCScriptEngine *engine, bool holdRef)
  126. {
  127. m_refCount.set(1);
  128. m_holdEngineRef = holdRef;
  129. if( holdRef )
  130. engine->AddRef();
  131. m_engine = engine;
  132. m_status = asEXECUTION_UNINITIALIZED;
  133. m_stackBlockSize = 0;
  134. m_originalStackPointer = 0;
  135. m_inExceptionHandler = false;
  136. m_isStackMemoryNotAllocated = false;
  137. m_needToCleanupArgs = false;
  138. m_currentFunction = 0;
  139. m_callingSystemFunction = 0;
  140. m_regs.objectRegister = 0;
  141. m_initialFunction = 0;
  142. m_lineCallback = false;
  143. m_exceptionCallback = false;
  144. m_regs.doProcessSuspend = false;
  145. m_doSuspend = false;
  146. m_userData = 0;
  147. m_regs.ctx = this;
  148. }
  149. asCContext::~asCContext()
  150. {
  151. DetachEngine();
  152. }
  153. // interface
  154. bool asCContext::IsNested(asUINT *nestCount) const
  155. {
  156. if( nestCount )
  157. *nestCount = 0;
  158. asUINT c = GetCallstackSize();
  159. if( c == 0 )
  160. return false;
  161. // Search for a marker on the call stack
  162. // This loop starts at 2 because the 0th entry is not stored in m_callStack,
  163. // and then we need to subtract one more to get the base of each frame
  164. for( asUINT n = 2; n <= c; n++ )
  165. {
  166. const asPWORD *s = m_callStack.AddressOf() + (c - n)*CALLSTACK_FRAME_SIZE;
  167. if( s && s[0] == 0 )
  168. {
  169. if( nestCount )
  170. (*nestCount)++;
  171. else
  172. return true;
  173. }
  174. }
  175. if( nestCount && *nestCount > 0 )
  176. return true;
  177. return false;
  178. }
  179. // interface
  180. int asCContext::AddRef() const
  181. {
  182. return m_refCount.atomicInc();
  183. }
  184. // interface
  185. int asCContext::Release() const
  186. {
  187. int r = m_refCount.atomicDec();
  188. if( r == 0 )
  189. {
  190. asDELETE(const_cast<asCContext*>(this),asCContext);
  191. return 0;
  192. }
  193. return r;
  194. }
  195. // internal
  196. void asCContext::DetachEngine()
  197. {
  198. if( m_engine == 0 ) return;
  199. // Clean up all calls, included nested ones
  200. do
  201. {
  202. // Abort any execution
  203. Abort();
  204. // Free all resources
  205. Unprepare();
  206. }
  207. while( IsNested() );
  208. // Free the stack blocks
  209. for( asUINT n = 0; n < m_stackBlocks.GetLength(); n++ )
  210. {
  211. if( m_stackBlocks[n] )
  212. {
  213. asDELETEARRAY(m_stackBlocks[n]);
  214. }
  215. }
  216. m_stackBlocks.SetLength(0);
  217. m_stackBlockSize = 0;
  218. // Clean the user data
  219. if( m_userData && m_engine->cleanContextFunc )
  220. m_engine->cleanContextFunc(this);
  221. // Clear engine pointer
  222. if( m_holdEngineRef )
  223. m_engine->Release();
  224. m_engine = 0;
  225. }
  226. // interface
  227. asIScriptEngine *asCContext::GetEngine() const
  228. {
  229. return m_engine;
  230. }
  231. // interface
  232. void *asCContext::SetUserData(void *data)
  233. {
  234. void *oldData = m_userData;
  235. m_userData = data;
  236. return oldData;
  237. }
  238. // interface
  239. void *asCContext::GetUserData() const
  240. {
  241. return m_userData;
  242. }
  243. // interface
  244. asIScriptFunction *asCContext::GetSystemFunction()
  245. {
  246. return m_callingSystemFunction;
  247. }
  248. // interface
  249. int asCContext::Prepare(asIScriptFunction *func)
  250. {
  251. if( func == 0 )
  252. {
  253. asCString str;
  254. str.Format(TXT_FAILED_IN_FUNC_s_WITH_s_d, "Prepare", "null", asNO_FUNCTION);
  255. m_engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  256. return asNO_FUNCTION;
  257. }
  258. if( m_status == asEXECUTION_ACTIVE || m_status == asEXECUTION_SUSPENDED )
  259. {
  260. asCString str;
  261. str.Format(TXT_FAILED_IN_FUNC_s_d, "Prepare", asCONTEXT_ACTIVE);
  262. m_engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  263. return asCONTEXT_ACTIVE;
  264. }
  265. // Clean the stack if not done before
  266. if( m_status != asEXECUTION_FINISHED && m_status != asEXECUTION_UNINITIALIZED )
  267. CleanStack();
  268. // Release the returned object (if any)
  269. CleanReturnObject();
  270. if( m_initialFunction && m_initialFunction == func )
  271. {
  272. // If the same function is executed again, we can skip a lot of the setup
  273. m_currentFunction = m_initialFunction;
  274. // Reset stack pointer
  275. m_regs.stackPointer = m_originalStackPointer;
  276. // Make sure the stack pointer is pointing to the original position,
  277. // otherwise something is wrong with the way it is being updated
  278. asASSERT( IsNested() || m_stackIndex > 0 || (m_regs.stackPointer == m_stackBlocks[0] + m_stackBlockSize) );
  279. }
  280. else
  281. {
  282. asASSERT( m_engine );
  283. if( m_initialFunction )
  284. {
  285. m_initialFunction->Release();
  286. // Reset stack pointer
  287. m_regs.stackPointer = m_originalStackPointer;
  288. // Make sure the stack pointer is pointing to the original position,
  289. // otherwise something is wrong with the way it is being updated
  290. asASSERT( IsNested() || m_stackIndex > 0 || (m_regs.stackPointer == m_stackBlocks[0] + m_stackBlockSize) );
  291. }
  292. // We trust the application not to pass anything else but a asCScriptFunction
  293. m_initialFunction = reinterpret_cast<asCScriptFunction *>(func);
  294. m_initialFunction->AddRef();
  295. m_currentFunction = m_initialFunction;
  296. // TODO: runtime optimize: GetSpaceNeededForArguments() should be precomputed
  297. m_argumentsSize = m_currentFunction->GetSpaceNeededForArguments() + (m_currentFunction->objectType ? AS_PTR_SIZE : 0);
  298. // Reserve space for the arguments and return value
  299. if( m_currentFunction->DoesReturnOnStack() )
  300. {
  301. m_returnValueSize = m_currentFunction->returnType.GetSizeInMemoryDWords();
  302. m_argumentsSize += AS_PTR_SIZE;
  303. }
  304. else
  305. m_returnValueSize = 0;
  306. // Determine the minimum stack size needed
  307. int stackSize = m_argumentsSize + m_returnValueSize;
  308. if( m_currentFunction->scriptData )
  309. stackSize += m_currentFunction->scriptData->stackNeeded;
  310. // Make sure there is enough space on the stack for the arguments and return value
  311. if( !ReserveStackSpace(stackSize) )
  312. return asOUT_OF_MEMORY;
  313. }
  314. // Reset state
  315. // Most of the time the previous state will be asEXECUTION_FINISHED, in which case the values are already initialized
  316. if( m_status != asEXECUTION_FINISHED )
  317. {
  318. m_exceptionLine = -1;
  319. m_exceptionFunction = 0;
  320. m_doAbort = false;
  321. m_doSuspend = false;
  322. m_regs.doProcessSuspend = m_lineCallback;
  323. m_externalSuspendRequest = false;
  324. }
  325. m_status = asEXECUTION_PREPARED;
  326. m_regs.programPointer = 0;
  327. // Reserve space for the arguments and return value
  328. m_regs.stackFramePointer = m_regs.stackPointer - m_argumentsSize - m_returnValueSize;
  329. m_originalStackPointer = m_regs.stackPointer;
  330. m_regs.stackPointer = m_regs.stackFramePointer;
  331. // Set arguments to 0
  332. memset(m_regs.stackPointer, 0, 4*m_argumentsSize);
  333. if( m_returnValueSize )
  334. {
  335. // Set the address of the location where the return value should be put
  336. asDWORD *ptr = m_regs.stackFramePointer;
  337. if( m_currentFunction->objectType )
  338. ptr += AS_PTR_SIZE;
  339. *(void**)ptr = (void*)(m_regs.stackFramePointer + m_argumentsSize);
  340. }
  341. return asSUCCESS;
  342. }
  343. // Free all resources
  344. int asCContext::Unprepare()
  345. {
  346. if( m_status == asEXECUTION_ACTIVE || m_status == asEXECUTION_SUSPENDED )
  347. return asCONTEXT_ACTIVE;
  348. // Only clean the stack if the context was prepared but not executed until the end
  349. if( m_status != asEXECUTION_UNINITIALIZED &&
  350. m_status != asEXECUTION_FINISHED )
  351. CleanStack();
  352. asASSERT( m_needToCleanupArgs == false );
  353. // Release the returned object (if any)
  354. CleanReturnObject();
  355. // Release the initial function
  356. if( m_initialFunction )
  357. {
  358. m_initialFunction->Release();
  359. // Reset stack pointer
  360. m_regs.stackPointer = m_originalStackPointer;
  361. // Make sure the stack pointer is pointing to the original position,
  362. // otherwise something is wrong with the way it is being updated
  363. asASSERT( IsNested() || m_stackIndex > 0 || (m_regs.stackPointer == m_stackBlocks[0] + m_stackBlockSize) );
  364. }
  365. // Clear function pointers
  366. m_initialFunction = 0;
  367. m_currentFunction = 0;
  368. m_exceptionFunction = 0;
  369. m_regs.programPointer = 0;
  370. // Reset status
  371. m_status = asEXECUTION_UNINITIALIZED;
  372. m_regs.stackFramePointer = 0;
  373. return 0;
  374. }
  375. asBYTE asCContext::GetReturnByte()
  376. {
  377. if( m_status != asEXECUTION_FINISHED ) return 0;
  378. asCDataType *dt = &m_initialFunction->returnType;
  379. if( dt->IsObject() || dt->IsReference() ) return 0;
  380. return *(asBYTE*)&m_regs.valueRegister;
  381. }
  382. asWORD asCContext::GetReturnWord()
  383. {
  384. if( m_status != asEXECUTION_FINISHED ) return 0;
  385. asCDataType *dt = &m_initialFunction->returnType;
  386. if( dt->IsObject() || dt->IsReference() ) return 0;
  387. return *(asWORD*)&m_regs.valueRegister;
  388. }
  389. asDWORD asCContext::GetReturnDWord()
  390. {
  391. if( m_status != asEXECUTION_FINISHED ) return 0;
  392. asCDataType *dt = &m_initialFunction->returnType;
  393. if( dt->IsObject() || dt->IsReference() ) return 0;
  394. return *(asDWORD*)&m_regs.valueRegister;
  395. }
  396. asQWORD asCContext::GetReturnQWord()
  397. {
  398. if( m_status != asEXECUTION_FINISHED ) return 0;
  399. asCDataType *dt = &m_initialFunction->returnType;
  400. if( dt->IsObject() || dt->IsReference() ) return 0;
  401. return m_regs.valueRegister;
  402. }
  403. float asCContext::GetReturnFloat()
  404. {
  405. if( m_status != asEXECUTION_FINISHED ) return 0;
  406. asCDataType *dt = &m_initialFunction->returnType;
  407. if( dt->IsObject() || dt->IsReference() ) return 0;
  408. return *(float*)&m_regs.valueRegister;
  409. }
  410. double asCContext::GetReturnDouble()
  411. {
  412. if( m_status != asEXECUTION_FINISHED ) return 0;
  413. asCDataType *dt = &m_initialFunction->returnType;
  414. if( dt->IsObject() || dt->IsReference() ) return 0;
  415. return *(double*)&m_regs.valueRegister;
  416. }
  417. void *asCContext::GetReturnAddress()
  418. {
  419. if( m_status != asEXECUTION_FINISHED ) return 0;
  420. asCDataType *dt = &m_initialFunction->returnType;
  421. if( dt->IsReference() )
  422. return *(void**)&m_regs.valueRegister;
  423. else if( dt->IsObject() )
  424. {
  425. if( m_initialFunction->DoesReturnOnStack() )
  426. {
  427. // The address of the return value was passed as the first argument, after the object pointer
  428. int offset = 0;
  429. if( m_initialFunction->objectType )
  430. offset += AS_PTR_SIZE;
  431. return *(void**)(&m_regs.stackFramePointer[offset]);
  432. }
  433. return m_regs.objectRegister;
  434. }
  435. return 0;
  436. }
  437. void *asCContext::GetReturnObject()
  438. {
  439. if( m_status != asEXECUTION_FINISHED ) return 0;
  440. asCDataType *dt = &m_initialFunction->returnType;
  441. if( !dt->IsObject() ) return 0;
  442. if( dt->IsReference() )
  443. return *(void**)(asPWORD)m_regs.valueRegister;
  444. else
  445. {
  446. if( m_initialFunction->DoesReturnOnStack() )
  447. {
  448. // The address of the return value was passed as the first argument, after the object pointer
  449. int offset = 0;
  450. if( m_initialFunction->objectType )
  451. offset += AS_PTR_SIZE;
  452. return *(void**)(&m_regs.stackFramePointer[offset]);
  453. }
  454. return m_regs.objectRegister;
  455. }
  456. }
  457. void *asCContext::GetAddressOfReturnValue()
  458. {
  459. if( m_status != asEXECUTION_FINISHED ) return 0;
  460. asCDataType *dt = &m_initialFunction->returnType;
  461. // An object is stored in the objectRegister
  462. if( !dt->IsReference() && dt->IsObject() )
  463. {
  464. // Need to dereference objects
  465. if( !dt->IsObjectHandle() )
  466. {
  467. if( m_initialFunction->DoesReturnOnStack() )
  468. {
  469. // The address of the return value was passed as the first argument, after the object pointer
  470. int offset = 0;
  471. if( m_initialFunction->objectType )
  472. offset += AS_PTR_SIZE;
  473. return *(void**)(&m_regs.stackFramePointer[offset]);
  474. }
  475. return *(void**)&m_regs.objectRegister;
  476. }
  477. return &m_regs.objectRegister;
  478. }
  479. // Primitives and references are stored in valueRegister
  480. return &m_regs.valueRegister;
  481. }
  482. int asCContext::SetObject(void *obj)
  483. {
  484. if( m_status != asEXECUTION_PREPARED )
  485. return asCONTEXT_NOT_PREPARED;
  486. if( !m_initialFunction->objectType )
  487. {
  488. m_status = asEXECUTION_ERROR;
  489. return asERROR;
  490. }
  491. *(asPWORD*)&m_regs.stackFramePointer[0] = (asPWORD)obj;
  492. return 0;
  493. }
  494. int asCContext::SetArgByte(asUINT arg, asBYTE value)
  495. {
  496. if( m_status != asEXECUTION_PREPARED )
  497. return asCONTEXT_NOT_PREPARED;
  498. if( arg >= (unsigned)m_initialFunction->parameterTypes.GetLength() )
  499. {
  500. m_status = asEXECUTION_ERROR;
  501. return asINVALID_ARG;
  502. }
  503. // Verify the type of the argument
  504. asCDataType *dt = &m_initialFunction->parameterTypes[arg];
  505. if( dt->IsObject() || dt->IsReference() )
  506. {
  507. m_status = asEXECUTION_ERROR;
  508. return asINVALID_TYPE;
  509. }
  510. if( dt->GetSizeInMemoryBytes() != 1 )
  511. {
  512. m_status = asEXECUTION_ERROR;
  513. return asINVALID_TYPE;
  514. }
  515. // Determine the position of the argument
  516. int offset = 0;
  517. if( m_initialFunction->objectType )
  518. offset += AS_PTR_SIZE;
  519. // If function returns object by value an extra pointer is pushed on the stack
  520. if( m_returnValueSize )
  521. offset += AS_PTR_SIZE;
  522. for( asUINT n = 0; n < arg; n++ )
  523. offset += m_initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  524. // Set the value
  525. *(asBYTE*)&m_regs.stackFramePointer[offset] = value;
  526. return 0;
  527. }
  528. int asCContext::SetArgWord(asUINT arg, asWORD value)
  529. {
  530. if( m_status != asEXECUTION_PREPARED )
  531. return asCONTEXT_NOT_PREPARED;
  532. if( arg >= m_initialFunction->parameterTypes.GetLength() )
  533. {
  534. m_status = asEXECUTION_ERROR;
  535. return asINVALID_ARG;
  536. }
  537. // Verify the type of the argument
  538. asCDataType *dt = &m_initialFunction->parameterTypes[arg];
  539. if( dt->IsObject() || dt->IsReference() )
  540. {
  541. m_status = asEXECUTION_ERROR;
  542. return asINVALID_TYPE;
  543. }
  544. if( dt->GetSizeInMemoryBytes() != 2 )
  545. {
  546. m_status = asEXECUTION_ERROR;
  547. return asINVALID_TYPE;
  548. }
  549. // Determine the position of the argument
  550. int offset = 0;
  551. if( m_initialFunction->objectType )
  552. offset += AS_PTR_SIZE;
  553. // If function returns object by value an extra pointer is pushed on the stack
  554. if( m_returnValueSize )
  555. offset += AS_PTR_SIZE;
  556. for( asUINT n = 0; n < arg; n++ )
  557. offset += m_initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  558. // Set the value
  559. *(asWORD*)&m_regs.stackFramePointer[offset] = value;
  560. return 0;
  561. }
  562. int asCContext::SetArgDWord(asUINT arg, asDWORD value)
  563. {
  564. if( m_status != asEXECUTION_PREPARED )
  565. return asCONTEXT_NOT_PREPARED;
  566. if( arg >= (unsigned)m_initialFunction->parameterTypes.GetLength() )
  567. {
  568. m_status = asEXECUTION_ERROR;
  569. return asINVALID_ARG;
  570. }
  571. // Verify the type of the argument
  572. asCDataType *dt = &m_initialFunction->parameterTypes[arg];
  573. if( dt->IsObject() || dt->IsReference() )
  574. {
  575. m_status = asEXECUTION_ERROR;
  576. return asINVALID_TYPE;
  577. }
  578. if( dt->GetSizeInMemoryBytes() != 4 )
  579. {
  580. m_status = asEXECUTION_ERROR;
  581. return asINVALID_TYPE;
  582. }
  583. // Determine the position of the argument
  584. int offset = 0;
  585. if( m_initialFunction->objectType )
  586. offset += AS_PTR_SIZE;
  587. // If function returns object by value an extra pointer is pushed on the stack
  588. if( m_returnValueSize )
  589. offset += AS_PTR_SIZE;
  590. for( asUINT n = 0; n < arg; n++ )
  591. offset += m_initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  592. // Set the value
  593. *(asDWORD*)&m_regs.stackFramePointer[offset] = value;
  594. return 0;
  595. }
  596. int asCContext::SetArgQWord(asUINT arg, asQWORD value)
  597. {
  598. if( m_status != asEXECUTION_PREPARED )
  599. return asCONTEXT_NOT_PREPARED;
  600. if( arg >= (unsigned)m_initialFunction->parameterTypes.GetLength() )
  601. {
  602. m_status = asEXECUTION_ERROR;
  603. return asINVALID_ARG;
  604. }
  605. // Verify the type of the argument
  606. asCDataType *dt = &m_initialFunction->parameterTypes[arg];
  607. if( dt->IsObject() || dt->IsReference() )
  608. {
  609. m_status = asEXECUTION_ERROR;
  610. return asINVALID_TYPE;
  611. }
  612. if( dt->GetSizeOnStackDWords() != 2 )
  613. {
  614. m_status = asEXECUTION_ERROR;
  615. return asINVALID_TYPE;
  616. }
  617. // Determine the position of the argument
  618. int offset = 0;
  619. if( m_initialFunction->objectType )
  620. offset += AS_PTR_SIZE;
  621. // If function returns object by value an extra pointer is pushed on the stack
  622. if( m_returnValueSize )
  623. offset += AS_PTR_SIZE;
  624. for( asUINT n = 0; n < arg; n++ )
  625. offset += m_initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  626. // Set the value
  627. *(asQWORD*)(&m_regs.stackFramePointer[offset]) = value;
  628. return 0;
  629. }
  630. int asCContext::SetArgFloat(asUINT arg, float value)
  631. {
  632. if( m_status != asEXECUTION_PREPARED )
  633. return asCONTEXT_NOT_PREPARED;
  634. if( arg >= (unsigned)m_initialFunction->parameterTypes.GetLength() )
  635. {
  636. m_status = asEXECUTION_ERROR;
  637. return asINVALID_ARG;
  638. }
  639. // Verify the type of the argument
  640. asCDataType *dt = &m_initialFunction->parameterTypes[arg];
  641. if( dt->IsObject() || dt->IsReference() )
  642. {
  643. m_status = asEXECUTION_ERROR;
  644. return asINVALID_TYPE;
  645. }
  646. if( dt->GetSizeOnStackDWords() != 1 )
  647. {
  648. m_status = asEXECUTION_ERROR;
  649. return asINVALID_TYPE;
  650. }
  651. // Determine the position of the argument
  652. int offset = 0;
  653. if( m_initialFunction->objectType )
  654. offset += AS_PTR_SIZE;
  655. // If function returns object by value an extra pointer is pushed on the stack
  656. if( m_returnValueSize )
  657. offset += AS_PTR_SIZE;
  658. for( asUINT n = 0; n < arg; n++ )
  659. offset += m_initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  660. // Set the value
  661. *(float*)(&m_regs.stackFramePointer[offset]) = value;
  662. return 0;
  663. }
  664. int asCContext::SetArgDouble(asUINT arg, double value)
  665. {
  666. if( m_status != asEXECUTION_PREPARED )
  667. return asCONTEXT_NOT_PREPARED;
  668. if( arg >= (unsigned)m_initialFunction->parameterTypes.GetLength() )
  669. {
  670. m_status = asEXECUTION_ERROR;
  671. return asINVALID_ARG;
  672. }
  673. // Verify the type of the argument
  674. asCDataType *dt = &m_initialFunction->parameterTypes[arg];
  675. if( dt->IsObject() || dt->IsReference() )
  676. {
  677. m_status = asEXECUTION_ERROR;
  678. return asINVALID_TYPE;
  679. }
  680. if( dt->GetSizeOnStackDWords() != 2 )
  681. {
  682. m_status = asEXECUTION_ERROR;
  683. return asINVALID_TYPE;
  684. }
  685. // Determine the position of the argument
  686. int offset = 0;
  687. if( m_initialFunction->objectType )
  688. offset += AS_PTR_SIZE;
  689. // If function returns object by value an extra pointer is pushed on the stack
  690. if( m_returnValueSize )
  691. offset += AS_PTR_SIZE;
  692. for( asUINT n = 0; n < arg; n++ )
  693. offset += m_initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  694. // Set the value
  695. *(double*)(&m_regs.stackFramePointer[offset]) = value;
  696. return 0;
  697. }
  698. int asCContext::SetArgAddress(asUINT arg, void *value)
  699. {
  700. if( m_status != asEXECUTION_PREPARED )
  701. return asCONTEXT_NOT_PREPARED;
  702. if( arg >= (unsigned)m_initialFunction->parameterTypes.GetLength() )
  703. {
  704. m_status = asEXECUTION_ERROR;
  705. return asINVALID_ARG;
  706. }
  707. // Verify the type of the argument
  708. asCDataType *dt = &m_initialFunction->parameterTypes[arg];
  709. if( !dt->IsReference() && !dt->IsObjectHandle() )
  710. {
  711. m_status = asEXECUTION_ERROR;
  712. return asINVALID_TYPE;
  713. }
  714. // Determine the position of the argument
  715. int offset = 0;
  716. if( m_initialFunction->objectType )
  717. offset += AS_PTR_SIZE;
  718. // If function returns object by value an extra pointer is pushed on the stack
  719. if( m_returnValueSize )
  720. offset += AS_PTR_SIZE;
  721. for( asUINT n = 0; n < arg; n++ )
  722. offset += m_initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  723. // Set the value
  724. *(asPWORD*)(&m_regs.stackFramePointer[offset]) = (asPWORD)value;
  725. return 0;
  726. }
  727. int asCContext::SetArgObject(asUINT arg, void *obj)
  728. {
  729. if( m_status != asEXECUTION_PREPARED )
  730. return asCONTEXT_NOT_PREPARED;
  731. if( arg >= (unsigned)m_initialFunction->parameterTypes.GetLength() )
  732. {
  733. m_status = asEXECUTION_ERROR;
  734. return asINVALID_ARG;
  735. }
  736. // Verify the type of the argument
  737. asCDataType *dt = &m_initialFunction->parameterTypes[arg];
  738. if( !dt->IsObject() )
  739. {
  740. m_status = asEXECUTION_ERROR;
  741. return asINVALID_TYPE;
  742. }
  743. // If the object should be sent by value we must make a copy of it
  744. if( !dt->IsReference() )
  745. {
  746. if( dt->IsObjectHandle() )
  747. {
  748. // Increase the reference counter
  749. asSTypeBehaviour *beh = &dt->GetObjectType()->beh;
  750. if( obj && beh->addref )
  751. m_engine->CallObjectMethod(obj, beh->addref);
  752. }
  753. else
  754. {
  755. obj = m_engine->CreateScriptObjectCopy(obj, dt->GetObjectType());
  756. }
  757. }
  758. // Determine the position of the argument
  759. int offset = 0;
  760. if( m_initialFunction->objectType )
  761. offset += AS_PTR_SIZE;
  762. // If function returns object by value an extra pointer is pushed on the stack
  763. if( m_returnValueSize )
  764. offset += AS_PTR_SIZE;
  765. for( asUINT n = 0; n < arg; n++ )
  766. offset += m_initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  767. // Set the value
  768. *(asPWORD*)(&m_regs.stackFramePointer[offset]) = (asPWORD)obj;
  769. return 0;
  770. }
  771. // TODO: Instead of GetAddressOfArg, maybe we need a SetArgValue(int arg, void *value, bool takeOwnership) instead.
  772. // interface
  773. void *asCContext::GetAddressOfArg(asUINT arg)
  774. {
  775. if( m_status != asEXECUTION_PREPARED )
  776. return 0;
  777. if( arg >= (unsigned)m_initialFunction->parameterTypes.GetLength() )
  778. return 0;
  779. // Determine the position of the argument
  780. int offset = 0;
  781. if( m_initialFunction->objectType )
  782. offset += AS_PTR_SIZE;
  783. // If function returns object by value an extra pointer is pushed on the stack
  784. if( m_returnValueSize )
  785. offset += AS_PTR_SIZE;
  786. for( asUINT n = 0; n < arg; n++ )
  787. offset += m_initialFunction->parameterTypes[n].GetSizeOnStackDWords();
  788. // We should return the address of the location where the argument value will be placed
  789. // All registered types are always sent by reference, even if
  790. // the function is declared to receive the argument by value.
  791. return &m_regs.stackFramePointer[offset];
  792. }
  793. int asCContext::Abort()
  794. {
  795. if( m_engine == 0 ) return asERROR;
  796. // TODO: multithread: Make thread safe. There is a chance that the status
  797. // changes to something else after being set to ABORTED here.
  798. if( m_status == asEXECUTION_SUSPENDED )
  799. m_status = asEXECUTION_ABORTED;
  800. m_doSuspend = true;
  801. m_regs.doProcessSuspend = true;
  802. m_externalSuspendRequest = true;
  803. m_doAbort = true;
  804. return 0;
  805. }
  806. // interface
  807. int asCContext::Suspend()
  808. {
  809. // This function just sets some internal flags and is safe
  810. // to call from a secondary thread, even if the library has
  811. // been built without multi-thread support.
  812. if( m_engine == 0 ) return asERROR;
  813. m_doSuspend = true;
  814. m_externalSuspendRequest = true;
  815. m_regs.doProcessSuspend = true;
  816. return 0;
  817. }
  818. // interface
  819. int asCContext::Execute()
  820. {
  821. asASSERT( m_engine != 0 );
  822. if( m_status != asEXECUTION_SUSPENDED && m_status != asEXECUTION_PREPARED )
  823. {
  824. asCString str;
  825. str.Format(TXT_FAILED_IN_FUNC_s_d, "Execute", asCONTEXT_NOT_PREPARED);
  826. m_engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  827. return asCONTEXT_NOT_PREPARED;
  828. }
  829. m_status = asEXECUTION_ACTIVE;
  830. asCThreadLocalData *tld = asPushActiveContext((asIScriptContext *)this);
  831. if( m_regs.programPointer == 0 )
  832. {
  833. if( m_currentFunction->funcType == asFUNC_DELEGATE )
  834. {
  835. // Push the object pointer onto the stack
  836. asASSERT( m_regs.stackPointer - AS_PTR_SIZE >= m_stackBlocks[m_stackIndex] );
  837. m_regs.stackPointer -= AS_PTR_SIZE;
  838. m_regs.stackFramePointer -= AS_PTR_SIZE;
  839. *(asPWORD*)m_regs.stackPointer = asPWORD(m_currentFunction->objForDelegate);
  840. // Make the call to the delegated object method
  841. m_currentFunction = m_currentFunction->funcForDelegate;
  842. }
  843. if( m_currentFunction->funcType == asFUNC_VIRTUAL ||
  844. m_currentFunction->funcType == asFUNC_INTERFACE )
  845. {
  846. // The currentFunction is a virtual method
  847. // Determine the true function from the object
  848. asCScriptObject *obj = *(asCScriptObject**)(asPWORD*)m_regs.stackFramePointer;
  849. if( obj == 0 )
  850. {
  851. SetInternalException(TXT_NULL_POINTER_ACCESS);
  852. }
  853. else
  854. {
  855. asCObjectType *objType = obj->objType;
  856. asCScriptFunction *realFunc = 0;
  857. if( m_currentFunction->funcType == asFUNC_VIRTUAL )
  858. {
  859. if( objType->virtualFunctionTable.GetLength() > (asUINT)m_currentFunction->vfTableIdx )
  860. {
  861. realFunc = objType->virtualFunctionTable[m_currentFunction->vfTableIdx];
  862. }
  863. }
  864. else
  865. {
  866. // Search the object type for a function that matches the interface function
  867. for( asUINT n = 0; n < objType->methods.GetLength(); n++ )
  868. {
  869. asCScriptFunction *f2 = m_engine->scriptFunctions[objType->methods[n]];
  870. if( f2->signatureId == m_currentFunction->signatureId )
  871. {
  872. if( f2->funcType == asFUNC_VIRTUAL )
  873. realFunc = objType->virtualFunctionTable[f2->vfTableIdx];
  874. else
  875. realFunc = f2;
  876. break;
  877. }
  878. }
  879. }
  880. if( realFunc )
  881. {
  882. if( realFunc->signatureId != m_currentFunction->signatureId )
  883. SetInternalException(TXT_NULL_POINTER_ACCESS);
  884. else
  885. m_currentFunction = realFunc;
  886. }
  887. }
  888. }
  889. if( m_currentFunction->funcType == asFUNC_SCRIPT )
  890. {
  891. m_regs.programPointer = m_currentFunction->scriptData->byteCode.AddressOf();
  892. // Set up the internal registers for executing the script function
  893. PrepareScriptFunction();
  894. }
  895. else if( m_currentFunction->funcType == asFUNC_SYSTEM )
  896. {
  897. // The current function is an application registered function
  898. // Call the function directly
  899. CallSystemFunction(m_currentFunction->id, this, 0);
  900. // Was the call successful?
  901. if( m_status == asEXECUTION_ACTIVE )
  902. {
  903. m_status = asEXECUTION_FINISHED;
  904. }
  905. }
  906. else
  907. {
  908. // This shouldn't happen
  909. asASSERT(false);
  910. }
  911. }
  912. asUINT gcPreObjects = 0;
  913. if( m_engine->ep.autoGarbageCollect )
  914. m_engine->gc.GetStatistics(&gcPreObjects, 0, 0, 0, 0);
  915. while( m_status == asEXECUTION_ACTIVE )
  916. ExecuteNext();
  917. if( m_lineCallback )
  918. {
  919. // Call the line callback one last time before leaving
  920. // so anyone listening can catch the state change
  921. CallLineCallback();
  922. m_regs.doProcessSuspend = true;
  923. }
  924. else
  925. m_regs.doProcessSuspend = false;
  926. m_doSuspend = false;
  927. if( m_engine->ep.autoGarbageCollect )
  928. {
  929. asUINT gcPosObjects = 0;
  930. m_engine->gc.GetStatistics(&gcPosObjects, 0, 0, 0, 0);
  931. if( gcPosObjects > gcPreObjects )
  932. {
  933. // Execute as many steps as there were new objects created
  934. while( gcPosObjects-- > gcPreObjects )
  935. m_engine->GarbageCollect(asGC_ONE_STEP | asGC_DESTROY_GARBAGE | asGC_DETECT_GARBAGE);
  936. }
  937. else if( gcPosObjects > 0 )
  938. {
  939. // Execute at least one step, even if no new objects were created
  940. m_engine->GarbageCollect(asGC_ONE_STEP | asGC_DESTROY_GARBAGE | asGC_DETECT_GARBAGE);
  941. }
  942. }
  943. // Pop the active context
  944. asASSERT(tld->activeContexts[tld->activeContexts.GetLength()-1] == this);
  945. tld->activeContexts.PopLast();
  946. if( m_status == asEXECUTION_FINISHED )
  947. {
  948. m_regs.objectType = m_initialFunction->returnType.GetObjectType();
  949. return asEXECUTION_FINISHED;
  950. }
  951. if( m_doAbort )
  952. {
  953. m_doAbort = false;
  954. m_status = asEXECUTION_ABORTED;
  955. return asEXECUTION_ABORTED;
  956. }
  957. if( m_status == asEXECUTION_SUSPENDED )
  958. return asEXECUTION_SUSPENDED;
  959. if( m_status == asEXECUTION_EXCEPTION )
  960. return asEXECUTION_EXCEPTION;
  961. return asERROR;
  962. }
  963. int asCContext::PushState()
  964. {
  965. // Only allow the state to be pushed when active
  966. // TODO: Can we support a suspended state too? So the reuse of
  967. // the context can be done outside the Execute() call?
  968. if( m_status != asEXECUTION_ACTIVE )
  969. {
  970. // TODO: Write message. Wrong usage
  971. return asERROR;
  972. }
  973. // Push the current script function that is calling the system function
  974. PushCallState();
  975. // Push the system function too, which will serve both as a marker and
  976. // informing which system function that created the nested call
  977. if( m_callStack.GetLength() == m_callStack.GetCapacity() )
  978. {
  979. // Allocate space for 10 call states at a time to save time
  980. m_callStack.AllocateNoConstruct(m_callStack.GetLength() + 10*CALLSTACK_FRAME_SIZE, true);
  981. }
  982. m_callStack.SetLengthNoConstruct(m_callStack.GetLength() + CALLSTACK_FRAME_SIZE);
  983. // Need to push m_initialFunction as it must be restored later
  984. asPWORD *tmp = m_callStack.AddressOf() + m_callStack.GetLength() - CALLSTACK_FRAME_SIZE;
  985. tmp[0] = 0;
  986. tmp[1] = (asPWORD)m_callingSystemFunction;
  987. tmp[2] = (asPWORD)m_initialFunction;
  988. tmp[3] = (asPWORD)m_originalStackPointer;
  989. tmp[4] = (asPWORD)m_argumentsSize;
  990. // Need to push the value of registers so they can be restored
  991. tmp[5] = (asPWORD)asDWORD(m_regs.valueRegister);
  992. tmp[6] = (asPWORD)asDWORD(m_regs.valueRegister>>32);
  993. tmp[7] = (asPWORD)m_regs.objectRegister;
  994. tmp[8] = (asPWORD)m_regs.objectType;
  995. // Decrease stackpointer to prevent the top value from being overwritten
  996. m_regs.stackPointer -= 2;
  997. // Clear the initial function so that Prepare() knows it must do all validations
  998. m_initialFunction = 0;
  999. // After this the state should appear as if uninitialized
  1000. m_callingSystemFunction = 0;
  1001. m_regs.objectRegister = 0;
  1002. m_regs.objectType = 0;
  1003. // Set the status to uninitialized as application
  1004. // should call Prepare() after this to reuse the context
  1005. m_status = asEXECUTION_UNINITIALIZED;
  1006. return asSUCCESS;
  1007. }
  1008. int asCContext::PopState()
  1009. {
  1010. if( !IsNested() )
  1011. return asERROR;
  1012. // Clean up the current execution
  1013. Unprepare();
  1014. // The topmost state must be a marker for nested call
  1015. asASSERT( m_callStack[m_callStack.GetLength() - CALLSTACK_FRAME_SIZE] == 0 );
  1016. // Restore the previous state
  1017. asPWORD *tmp = &m_callStack[m_callStack.GetLength() - CALLSTACK_FRAME_SIZE];
  1018. m_callingSystemFunction = reinterpret_cast<asCScriptFunction*>(tmp[1]);
  1019. m_callStack.SetLength(m_callStack.GetLength() - CALLSTACK_FRAME_SIZE);
  1020. // Restore the previous initial function and the associated values
  1021. m_initialFunction = reinterpret_cast<asCScriptFunction*>(tmp[2]);
  1022. m_originalStackPointer = (asDWORD*)tmp[3];
  1023. m_argumentsSize = (int)tmp[4];
  1024. m_regs.valueRegister = asQWORD(asDWORD(tmp[5]));
  1025. m_regs.valueRegister |= asQWORD(tmp[6])<<32;
  1026. m_regs.objectRegister = (void*)tmp[7];
  1027. m_regs.objectType = (asIObjectType*)tmp[8];
  1028. // Calculate the returnValueSize
  1029. if( m_initialFunction->DoesReturnOnStack() )
  1030. m_returnValueSize = m_initialFunction->returnType.GetSizeInMemoryDWords();
  1031. else
  1032. m_returnValueSize = 0;
  1033. // Pop the current script function. This will also restore the previous stack pointer
  1034. PopCallState();
  1035. m_status = asEXECUTION_ACTIVE;
  1036. return asSUCCESS;
  1037. }
  1038. void asCContext::PushCallState()
  1039. {
  1040. if( m_callStack.GetLength() == m_callStack.GetCapacity() )
  1041. {
  1042. // Allocate space for 10 call states at a time to save time
  1043. m_callStack.AllocateNoConstruct(m_callStack.GetLength() + 10*CALLSTACK_FRAME_SIZE, true);
  1044. }
  1045. m_callStack.SetLengthNoConstruct(m_callStack.GetLength() + CALLSTACK_FRAME_SIZE);
  1046. // Separating the loads and stores limits data cache trash, and with a smart compiler
  1047. // could turn into SIMD style loading/storing if available.
  1048. // The compiler can't do this itself due to potential pointer aliasing between the pointers,
  1049. // ie writing to tmp could overwrite the data contained in registers.stackFramePointer for example
  1050. // for all the compiler knows. So introducing the local variable s, which is never referred to by
  1051. // its address we avoid this issue.
  1052. asPWORD s[5];
  1053. s[0] = (asPWORD)m_regs.stackFramePointer;
  1054. s[1] = (asPWORD)m_currentFunction;
  1055. s[2] = (asPWORD)m_regs.programPointer;
  1056. s[3] = (asPWORD)m_regs.stackPointer;
  1057. s[4] = m_stackIndex;
  1058. asPWORD *tmp = m_callStack.AddressOf() + m_callStack.GetLength() - CALLSTACK_FRAME_SIZE;
  1059. tmp[0] = s[0];
  1060. tmp[1] = s[1];
  1061. tmp[2] = s[2];
  1062. tmp[3] = s[3];
  1063. tmp[4] = s[4];
  1064. }
  1065. void asCContext::PopCallState()
  1066. {
  1067. // See comments in PushCallState about pointer aliasing and data cache trashing
  1068. asPWORD *tmp = m_callStack.AddressOf() + m_callStack.GetLength() - CALLSTACK_FRAME_SIZE;
  1069. asPWORD s[5];
  1070. s[0] = tmp[0];
  1071. s[1] = tmp[1];
  1072. s[2] = tmp[2];
  1073. s[3] = tmp[3];
  1074. s[4] = tmp[4];
  1075. m_regs.stackFramePointer = (asDWORD*)s[0];
  1076. m_currentFunction = (asCScriptFunction*)s[1];
  1077. m_regs.programPointer = (asDWORD*)s[2];
  1078. m_regs.stackPointer = (asDWORD*)s[3];
  1079. m_stackIndex = (int)s[4];
  1080. m_callStack.SetLength(m_callStack.GetLength() - CALLSTACK_FRAME_SIZE);
  1081. }
  1082. // interface
  1083. asUINT asCContext::GetCallstackSize() const
  1084. {
  1085. if( m_currentFunction == 0 ) return 0;
  1086. // The current function is accessed at stackLevel 0
  1087. return asUINT(1 + m_callStack.GetLength() / CALLSTACK_FRAME_SIZE);
  1088. }
  1089. // interface
  1090. asIScriptFunction *asCContext::GetFunction(asUINT stackLevel)
  1091. {
  1092. if( stackLevel >= GetCallstackSize() ) return 0;
  1093. if( stackLevel == 0 ) return m_currentFunction;
  1094. asPWORD *s = m_callStack.AddressOf() + (GetCallstackSize() - stackLevel - 1)*CALLSTACK_FRAME_SIZE;
  1095. asCScriptFunction *func = (asCScriptFunction*)s[1];
  1096. return func;
  1097. }
  1098. // interface
  1099. int asCContext::GetLineNumber(asUINT stackLevel, int *column, const char **sectionName)
  1100. {
  1101. if( stackLevel >= GetCallstackSize() ) return asINVALID_ARG;
  1102. asCScriptFunction *func;
  1103. asDWORD *bytePos;
  1104. if( stackLevel == 0 )
  1105. {
  1106. func = m_currentFunction;
  1107. if( func->scriptData == 0 ) return 0;
  1108. bytePos = m_regs.programPointer;
  1109. }
  1110. else
  1111. {
  1112. asPWORD *s = m_callStack.AddressOf() + (GetCallstackSize()-stackLevel-1)*CALLSTACK_FRAME_SIZE;
  1113. func = (asCScriptFunction*)s[1];
  1114. if( func->scriptData == 0 ) return 0;
  1115. bytePos = (asDWORD*)s[2];
  1116. // Subract 1 from the bytePos, because we want the line where
  1117. // the call was made, and not the instruction after the call
  1118. bytePos -= 1;
  1119. }
  1120. // For nested calls it is possible that func is null
  1121. if( func == 0 )
  1122. {
  1123. if( column ) *column = 0;
  1124. if( sectionName ) *sectionName = 0;
  1125. return 0;
  1126. }
  1127. int sectionIdx;
  1128. asDWORD line = func->GetLineNumber(int(bytePos - func->scriptData->byteCode.AddressOf()), &sectionIdx);
  1129. if( column ) *column = (line >> 20);
  1130. if( sectionName )
  1131. {
  1132. asASSERT( sectionIdx < int(m_engine->scriptSectionNames.GetLength()) );
  1133. if( sectionIdx >= 0 && asUINT(sectionIdx) < m_engine->scriptSectionNames.GetLength() )
  1134. *sectionName = m_engine->scriptSectionNames[sectionIdx]->AddressOf();
  1135. else
  1136. *sectionName = 0;
  1137. }
  1138. return (line & 0xFFFFF);
  1139. }
  1140. // internal
  1141. bool asCContext::ReserveStackSpace(asUINT size)
  1142. {
  1143. // Make sure the first stack block is allocated
  1144. if( m_stackBlocks.GetLength() == 0 )
  1145. {
  1146. m_stackBlockSize = m_engine->initialContextStackSize;
  1147. asASSERT( m_stackBlockSize > 0 );
  1148. asDWORD *stack = asNEWARRAY(asDWORD,m_stackBlockSize);
  1149. if( stack == 0 )
  1150. {
  1151. // Out of memory
  1152. return false;
  1153. }
  1154. m_stackBlocks.PushLast(stack);
  1155. m_stackIndex = 0;
  1156. m_regs.stackPointer = m_stackBlocks[0] + m_stackBlockSize;
  1157. }
  1158. // Check if there is enough space on the current stack block, otherwise move
  1159. // to the next one. New and larger blocks will be allocated as necessary
  1160. while( m_regs.stackPointer - (size + RESERVE_STACK) < m_stackBlocks[m_stackIndex] )
  1161. {
  1162. // Make sure we don't allocate more space than allowed
  1163. if( m_engine->ep.maximumContextStackSize )
  1164. {
  1165. // This test will only stop growth once it has already crossed the limit
  1166. if( m_stackBlockSize * ((1 << (m_stackIndex+1)) - 1) > m_engine->ep.maximumContextStackSize )
  1167. {
  1168. m_isStackMemoryNotAllocated = true;
  1169. // Set the stackFramePointer, even though the stackPointer wasn't updated
  1170. m_regs.stackFramePointer = m_regs.stackPointer;
  1171. SetInternalException(TXT_STACK_OVERFLOW);
  1172. return false;
  1173. }
  1174. }
  1175. m_stackIndex++;
  1176. if( m_stackBlocks.GetLength() == m_stackIndex )
  1177. {
  1178. // Allocate the new stack block, with twice the size of the previous
  1179. asDWORD *stack = asNEWARRAY(asDWORD,(m_stackBlockSize << m_stackIndex));
  1180. if( stack == 0 )
  1181. {
  1182. // Out of memory
  1183. m_isStackMemoryNotAllocated = true;
  1184. // Set the stackFramePointer, even though the stackPointer wasn't updated
  1185. m_regs.stackFramePointer = m_regs.stackPointer;
  1186. SetInternalException(TXT_STACK_OVERFLOW);
  1187. return false;
  1188. }
  1189. m_stackBlocks.PushLast(stack);
  1190. }
  1191. // Update the stack pointer to point to the new block.
  1192. // Leave enough room above the stackpointer to copy the arguments from the previous stackblock
  1193. m_regs.stackPointer = m_stackBlocks[m_stackIndex] +
  1194. (m_stackBlockSize<<m_stackIndex) -
  1195. m_currentFunction->GetSpaceNeededForArguments() -
  1196. (m_currentFunction->objectType ? AS_PTR_SIZE : 0) -
  1197. (m_currentFunction->DoesReturnOnStack() ? AS_PTR_SIZE : 0);
  1198. }
  1199. return true;
  1200. }
  1201. // internal
  1202. void asCContext::CallScriptFunction(asCScriptFunction *func)
  1203. {
  1204. asASSERT( func->scriptData );
  1205. // Push the framepointer, function id and programCounter on the stack
  1206. PushCallState();
  1207. // Update the current function and program position before increasing the stack
  1208. // so the exception handler will know what to do if there is a stack overflow
  1209. m_currentFunction = func;
  1210. m_regs.programPointer = m_currentFunction->scriptData->byteCode.AddressOf();
  1211. // Make sure there is space on the stack to execute the function
  1212. asDWORD *oldStackPointer = m_regs.stackPointer;
  1213. if( !ReserveStackSpace(func->scriptData->stackNeeded) )
  1214. return;
  1215. // If a new stack block was allocated then we'll need to move
  1216. // over the function arguments to the new block
  1217. if( m_regs.stackPointer != oldStackPointer )
  1218. {
  1219. int numDwords = func->GetSpaceNeededForArguments() + (func->objectType ? AS_PTR_SIZE : 0) + (func->DoesReturnOnStack() ? AS_PTR_SIZE : 0);
  1220. memcpy(m_regs.stackPointer, oldStackPointer, sizeof(asDWORD)*numDwords);
  1221. }
  1222. PrepareScriptFunction();
  1223. }
  1224. void asCContext::PrepareScriptFunction()
  1225. {
  1226. asASSERT( m_currentFunction->scriptData );
  1227. // Update framepointer
  1228. m_regs.stackFramePointer = m_regs.stackPointer;
  1229. // Set all object variables to 0 to guarantee that they are null before they are used
  1230. // Only variables on the heap should be cleared. The rest will be cleared by calling the constructor
  1231. asUINT n = m_currentFunction->scriptData->objVariablesOnHeap;
  1232. while( n-- > 0 )
  1233. {
  1234. int pos = m_currentFunction->scriptData->objVariablePos[n];
  1235. *(asPWORD*)&m_regs.stackFramePointer[-pos] = 0;
  1236. }
  1237. // Initialize the stack pointer with the space needed for local variables
  1238. m_regs.stackPointer -= m_currentFunction->scriptData->variableSpace;
  1239. // Call the line callback for each script function, to guarantee that infinitely recursive scripts can
  1240. // be interrupted, even if the scripts have been compiled with asEP_BUILD_WITHOUT_LINE_CUES
  1241. if( m_regs.doProcessSuspend )
  1242. {
  1243. if( m_lineCallback )
  1244. CallLineCallback();
  1245. if( m_doSuspend )
  1246. m_status = asEXECUTION_SUSPENDED;
  1247. }
  1248. }
  1249. void asCContext::CallInterfaceMethod(asCScriptFunction *func)
  1250. {
  1251. // Resolve the interface method using the current script type
  1252. asCScriptObject *obj = *(asCScriptObject**)(asPWORD*)m_regs.stackPointer;
  1253. if( obj == 0 )
  1254. {
  1255. // Tell the exception handler to clean up the arguments to this method
  1256. m_needToCleanupArgs = true;
  1257. SetInternalException(TXT_NULL_POINTER_ACCESS);
  1258. return;
  1259. }
  1260. asCObjectType *objType = obj->objType;
  1261. // TODO: runtime optimize: The object type should have a list of only those methods that
  1262. // implement interface methods. This list should be ordered by
  1263. // the signatureId so that a binary search can be made, instead
  1264. // of a linear search.
  1265. //
  1266. // When this is done, we must also make sure the signatureId of a
  1267. // function never changes, e.g. when if the signature functions are
  1268. // released.
  1269. // Search the object type for a function that matches the interface function
  1270. asCScriptFunction *realFunc = 0;
  1271. if( func->funcType == asFUNC_INTERFACE )
  1272. {
  1273. for( asUINT n = 0; n < objType->methods.GetLength(); n++ )
  1274. {
  1275. asCScriptFunction *f2 = m_engine->scriptFunctions[objType->methods[n]];
  1276. if( f2->signatureId == func->signatureId )
  1277. {
  1278. if( f2->funcType == asFUNC_VIRTUAL )
  1279. realFunc = objType->virtualFunctionTable[f2->vfTableIdx];
  1280. else
  1281. realFunc = f2;
  1282. break;
  1283. }
  1284. }
  1285. if( realFunc == 0 )
  1286. {
  1287. // Tell the exception handler to clean up the arguments to this method
  1288. m_needToCleanupArgs = true;
  1289. SetInternalException(TXT_NULL_POINTER_ACCESS);
  1290. return;
  1291. }
  1292. }
  1293. else // if( func->funcType == asFUNC_VIRTUAL )
  1294. {
  1295. realFunc = objType->virtualFunctionTable[func->vfTableIdx];
  1296. }
  1297. // Then call the true script function
  1298. CallScriptFunction(realFunc);
  1299. }
  1300. void asCContext::ExecuteNext()
  1301. {
  1302. asDWORD *l_bc = m_regs.programPointer;
  1303. asDWORD *l_sp = m_regs.stackPointer;
  1304. asDWORD *l_fp = m_regs.stackFramePointer;
  1305. for(;;)
  1306. {
  1307. #ifdef AS_DEBUG
  1308. // Gather statistics on executed bytecode
  1309. stats.Instr(*(asBYTE*)l_bc);
  1310. // Used to verify that the size of the instructions are correct
  1311. asDWORD *old = l_bc;
  1312. #endif
  1313. // Remember to keep the cases in order and without
  1314. // gaps, because that will make the switch faster.
  1315. // It will be faster since only one lookup will be
  1316. // made to find the correct jump destination. If not
  1317. // in order, the switch will make two lookups.
  1318. switch( *(asBYTE*)l_bc )
  1319. {
  1320. //--------------
  1321. // memory access functions
  1322. case asBC_PopPtr:
  1323. // Pop a pointer from the stack
  1324. l_sp += AS_PTR_SIZE;
  1325. l_bc++;
  1326. break;
  1327. case asBC_PshGPtr:
  1328. // Replaces PGA + RDSPtr
  1329. l_sp -= AS_PTR_SIZE;
  1330. *(asPWORD*)l_sp = *(asPWORD*)asBC_PTRARG(l_bc);
  1331. l_bc += 1 + AS_PTR_SIZE;
  1332. break;
  1333. // Push a dword value on the stack
  1334. case asBC_PshC4:
  1335. --l_sp;
  1336. *l_sp = asBC_DWORDARG(l_bc);
  1337. l_bc += 2;
  1338. break;
  1339. // Push the dword value of a variable on the stack
  1340. case asBC_PshV4:
  1341. --l_sp;
  1342. *l_sp = *(l_fp - asBC_SWORDARG0(l_bc));
  1343. l_bc++;
  1344. break;
  1345. // Push the address of a variable on the stack
  1346. case asBC_PSF:
  1347. l_sp -= AS_PTR_SIZE;
  1348. *(asPWORD*)l_sp = asPWORD(l_fp - asBC_SWORDARG0(l_bc));
  1349. l_bc++;
  1350. break;
  1351. // Swap the top 2 pointers on the stack
  1352. case asBC_SwapPtr:
  1353. {
  1354. asPWORD p = *(asPWORD*)l_sp;
  1355. *(asPWORD*)l_sp = *(asPWORD*)(l_sp+AS_PTR_SIZE);
  1356. *(asPWORD*)(l_sp+AS_PTR_SIZE) = p;
  1357. l_bc++;
  1358. }
  1359. break;
  1360. // Do a boolean not operation, modifying the value of the variable
  1361. case asBC_NOT:
  1362. #if AS_SIZEOF_BOOL == 1
  1363. {
  1364. // Set the value to true if it is equal to 0
  1365. // We need to use volatile here to tell the compiler it cannot
  1366. // change the order of read and write operations on the pointer.
  1367. volatile asBYTE *ptr = (asBYTE*)(l_fp - asBC_SWORDARG0(l_bc));
  1368. asBYTE val = (ptr[0] == 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1369. ptr[0] = val; // The result is stored in the lower byte
  1370. ptr[1] = 0; // Make sure the rest of the DWORD is 0
  1371. ptr[2] = 0;
  1372. ptr[3] = 0;
  1373. }
  1374. #else
  1375. *(l_fp - asBC_SWORDARG0(l_bc)) = (*(l_fp - asBC_SWORDARG0(l_bc)) == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  1376. #endif
  1377. l_bc++;
  1378. break;
  1379. // Push the dword value of a global variable on the stack
  1380. case asBC_PshG4:
  1381. --l_sp;
  1382. *l_sp = *(asDWORD*)asBC_PTRARG(l_bc);
  1383. l_bc += 1 + AS_PTR_SIZE;
  1384. break;
  1385. // Load the address of a global variable in the register, then
  1386. // copy the value of the global variable into a local variable
  1387. case asBC_LdGRdR4:
  1388. *(void**)&m_regs.valueRegister = (void*)asBC_PTRARG(l_bc);
  1389. *(l_fp - asBC_SWORDARG0(l_bc)) = **(asDWORD**)&m_regs.valueRegister;
  1390. l_bc += 1+AS_PTR_SIZE;
  1391. break;
  1392. //----------------
  1393. // path control instructions
  1394. // Begin execution of a script function
  1395. case asBC_CALL:
  1396. {
  1397. int i = asBC_INTARG(l_bc);
  1398. l_bc += 2;
  1399. asASSERT( i >= 0 );
  1400. asASSERT( (i & FUNC_IMPORTED) == 0 );
  1401. // Need to move the values back to the context
  1402. m_regs.programPointer = l_bc;
  1403. m_regs.stackPointer = l_sp;
  1404. m_regs.stackFramePointer = l_fp;
  1405. CallScriptFunction(m_engine->scriptFunctions[i]);
  1406. // Extract the values from the context again
  1407. l_bc = m_regs.programPointer;
  1408. l_sp = m_regs.stackPointer;
  1409. l_fp = m_regs.stackFramePointer;
  1410. // If status isn't active anymore then we must stop
  1411. if( m_status != asEXECUTION_ACTIVE )
  1412. return;
  1413. }
  1414. break;
  1415. // Return to the caller, and remove the arguments from the stack
  1416. case asBC_RET:
  1417. {
  1418. // Return if this was the first function, or a nested execution
  1419. if( m_callStack.GetLength() == 0 ||
  1420. m_callStack[m_callStack.GetLength() - CALLSTACK_FRAME_SIZE] == 0 )
  1421. {
  1422. m_status = asEXECUTION_FINISHED;
  1423. return;
  1424. }
  1425. asWORD w = asBC_WORDARG0(l_bc);
  1426. // Read the old framepointer, functionid, and programCounter from the call stack
  1427. PopCallState();
  1428. // Extract the values from the context again
  1429. l_bc = m_regs.programPointer;
  1430. l_sp = m_regs.stackPointer;
  1431. l_fp = m_regs.stackFramePointer;
  1432. // Pop arguments from stack
  1433. l_sp += w;
  1434. }
  1435. break;
  1436. // Jump to a relative position
  1437. case asBC_JMP:
  1438. l_bc += 2 + asBC_INTARG(l_bc);
  1439. break;
  1440. //----------------
  1441. // Conditional jumps
  1442. // Jump to a relative position if the value in the register is 0
  1443. case asBC_JZ:
  1444. if( *(int*)&m_regs.valueRegister == 0 )
  1445. l_bc += asBC_INTARG(l_bc) + 2;
  1446. else
  1447. l_bc += 2;
  1448. break;
  1449. // Jump to a relative position if the value in the register is not 0
  1450. case asBC_JNZ:
  1451. if( *(int*)&m_regs.valueRegister != 0 )
  1452. l_bc += asBC_INTARG(l_bc) + 2;
  1453. else
  1454. l_bc += 2;
  1455. break;
  1456. // Jump to a relative position if the value in the register is negative
  1457. case asBC_JS:
  1458. if( *(int*)&m_regs.valueRegister < 0 )
  1459. l_bc += asBC_INTARG(l_bc) + 2;
  1460. else
  1461. l_bc += 2;
  1462. break;
  1463. // Jump to a relative position if the value in the register it not negative
  1464. case asBC_JNS:
  1465. if( *(int*)&m_regs.valueRegister >= 0 )
  1466. l_bc += asBC_INTARG(l_bc) + 2;
  1467. else
  1468. l_bc += 2;
  1469. break;
  1470. // Jump to a relative position if the value in the register is greater than 0
  1471. case asBC_JP:
  1472. if( *(int*)&m_regs.valueRegister > 0 )
  1473. l_bc += asBC_INTARG(l_bc) + 2;
  1474. else
  1475. l_bc += 2;
  1476. break;
  1477. // Jump to a relative position if the value in the register is not greater than 0
  1478. case asBC_JNP:
  1479. if( *(int*)&m_regs.valueRegister <= 0 )
  1480. l_bc += asBC_INTARG(l_bc) + 2;
  1481. else
  1482. l_bc += 2;
  1483. break;
  1484. //--------------------
  1485. // test instructions
  1486. // If the value in the register is 0, then set the register to 1, else to 0
  1487. case asBC_TZ:
  1488. #if AS_SIZEOF_BOOL == 1
  1489. {
  1490. // Set the value to true if it is equal to 0
  1491. // We need to use volatile here to tell the compiler it cannot
  1492. // change the order of read and write operations on valueRegister.
  1493. volatile int *regPtr = (int*)&m_regs.valueRegister;
  1494. volatile asBYTE *regBptr = (asBYTE*)&m_regs.valueRegister;
  1495. asBYTE val = (regPtr[0] == 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1496. regBptr[0] = val; // The result is stored in the lower byte
  1497. regBptr[1] = 0; // Make sure the rest of the register is 0
  1498. regBptr[2] = 0;
  1499. regBptr[3] = 0;
  1500. regBptr[4] = 0;
  1501. regBptr[5] = 0;
  1502. regBptr[6] = 0;
  1503. regBptr[7] = 0;
  1504. }
  1505. #else
  1506. *(int*)&m_regs.valueRegister = (*(int*)&m_regs.valueRegister == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  1507. #endif
  1508. l_bc++;
  1509. break;
  1510. // If the value in the register is not 0, then set the register to 1, else to 0
  1511. case asBC_TNZ:
  1512. #if AS_SIZEOF_BOOL == 1
  1513. {
  1514. // Set the value to true if it is not equal to 0
  1515. // We need to use volatile here to tell the compiler it cannot
  1516. // change the order of read and write operations on valueRegister.
  1517. volatile int *regPtr = (int*)&m_regs.valueRegister;
  1518. volatile asBYTE *regBptr = (asBYTE*)&m_regs.valueRegister;
  1519. asBYTE val = (regPtr[0] == 0) ? 0 : VALUE_OF_BOOLEAN_TRUE;
  1520. regBptr[0] = val; // The result is stored in the lower byte
  1521. regBptr[1] = 0; // Make sure the rest of the register is 0
  1522. regBptr[2] = 0;
  1523. regBptr[3] = 0;
  1524. regBptr[4] = 0;
  1525. regBptr[5] = 0;
  1526. regBptr[6] = 0;
  1527. regBptr[7] = 0;
  1528. }
  1529. #else
  1530. *(int*)&m_regs.valueRegister = (*(int*)&m_regs.valueRegister == 0 ? 0 : VALUE_OF_BOOLEAN_TRUE);
  1531. #endif
  1532. l_bc++;
  1533. break;
  1534. // If the value in the register is negative, then set the register to 1, else to 0
  1535. case asBC_TS:
  1536. #if AS_SIZEOF_BOOL == 1
  1537. {
  1538. // Set the value to true if it is less than 0
  1539. // We need to use volatile here to tell the compiler it cannot
  1540. // change the order of read and write operations on valueRegister.
  1541. volatile int *regPtr = (int*)&m_regs.valueRegister;
  1542. volatile asBYTE *regBptr = (asBYTE*)&m_regs.valueRegister;
  1543. asBYTE val = (regPtr[0] < 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1544. regBptr[0] = val; // The result is stored in the lower byte
  1545. regBptr[1] = 0; // Make sure the rest of the register is 0
  1546. regBptr[2] = 0;
  1547. regBptr[3] = 0;
  1548. regBptr[4] = 0;
  1549. regBptr[5] = 0;
  1550. regBptr[6] = 0;
  1551. regBptr[7] = 0;
  1552. }
  1553. #else
  1554. *(int*)&m_regs.valueRegister = (*(int*)&m_regs.valueRegister < 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  1555. #endif
  1556. l_bc++;
  1557. break;
  1558. // If the value in the register is not negative, then set the register to 1, else to 0
  1559. case asBC_TNS:
  1560. #if AS_SIZEOF_BOOL == 1
  1561. {
  1562. // Set the value to true if it is not less than 0
  1563. // We need to use volatile here to tell the compiler it cannot
  1564. // change the order of read and write operations on valueRegister.
  1565. volatile int *regPtr = (int*)&m_regs.valueRegister;
  1566. volatile asBYTE *regBptr = (asBYTE*)&m_regs.valueRegister;
  1567. asBYTE val = (regPtr[0] >= 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1568. regBptr[0] = val; // The result is stored in the lower byte
  1569. regBptr[1] = 0; // Make sure the rest of the register is 0
  1570. regBptr[2] = 0;
  1571. regBptr[3] = 0;
  1572. regBptr[4] = 0;
  1573. regBptr[5] = 0;
  1574. regBptr[6] = 0;
  1575. regBptr[7] = 0;
  1576. }
  1577. #else
  1578. *(int*)&m_regs.valueRegister = (*(int*)&m_regs.valueRegister < 0 ? 0 : VALUE_OF_BOOLEAN_TRUE);
  1579. #endif
  1580. l_bc++;
  1581. break;
  1582. // If the value in the register is greater than 0, then set the register to 1, else to 0
  1583. case asBC_TP:
  1584. #if AS_SIZEOF_BOOL == 1
  1585. {
  1586. // Set the value to true if it is greater than 0
  1587. // We need to use volatile here to tell the compiler it cannot
  1588. // change the order of read and write operations on valueRegister.
  1589. volatile int *regPtr = (int*)&m_regs.valueRegister;
  1590. volatile asBYTE *regBptr = (asBYTE*)&m_regs.valueRegister;
  1591. asBYTE val = (regPtr[0] > 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1592. regBptr[0] = val; // The result is stored in the lower byte
  1593. regBptr[1] = 0; // Make sure the rest of the register is 0
  1594. regBptr[2] = 0;
  1595. regBptr[3] = 0;
  1596. regBptr[4] = 0;
  1597. regBptr[5] = 0;
  1598. regBptr[6] = 0;
  1599. regBptr[7] = 0;
  1600. }
  1601. #else
  1602. *(int*)&m_regs.valueRegister = (*(int*)&m_regs.valueRegister > 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  1603. #endif
  1604. l_bc++;
  1605. break;
  1606. // If the value in the register is not greater than 0, then set the register to 1, else to 0
  1607. case asBC_TNP:
  1608. #if AS_SIZEOF_BOOL == 1
  1609. {
  1610. // Set the value to true if it is not greater than 0
  1611. // We need to use volatile here to tell the compiler it cannot
  1612. // change the order of read and write operations on valueRegister.
  1613. volatile int *regPtr = (int*)&m_regs.valueRegister;
  1614. volatile asBYTE *regBptr = (asBYTE*)&m_regs.valueRegister;
  1615. asBYTE val = (regPtr[0] <= 0) ? VALUE_OF_BOOLEAN_TRUE : 0;
  1616. regBptr[0] = val; // The result is stored in the lower byte
  1617. regBptr[1] = 0; // Make sure the rest of the register is 0
  1618. regBptr[2] = 0;
  1619. regBptr[3] = 0;
  1620. regBptr[4] = 0;
  1621. regBptr[5] = 0;
  1622. regBptr[6] = 0;
  1623. regBptr[7] = 0;
  1624. }
  1625. #else
  1626. *(int*)&m_regs.valueRegister = (*(int*)&m_regs.valueRegister > 0 ? 0 : VALUE_OF_BOOLEAN_TRUE);
  1627. #endif
  1628. l_bc++;
  1629. break;
  1630. //--------------------
  1631. // negate value
  1632. // Negate the integer value in the variable
  1633. case asBC_NEGi:
  1634. *(l_fp - asBC_SWORDARG0(l_bc)) = asDWORD(-int(*(l_fp - asBC_SWORDARG0(l_bc))));
  1635. l_bc++;
  1636. break;
  1637. // Negate the float value in the variable
  1638. case asBC_NEGf:
  1639. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = -*(float*)(l_fp - asBC_SWORDARG0(l_bc));
  1640. l_bc++;
  1641. break;
  1642. // Negate the double value in the variable
  1643. case asBC_NEGd:
  1644. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = -*(double*)(l_fp - asBC_SWORDARG0(l_bc));
  1645. l_bc++;
  1646. break;
  1647. //-------------------------
  1648. // Increment value pointed to by address in register
  1649. // Increment the short value pointed to by the register
  1650. case asBC_INCi16:
  1651. (**(short**)&m_regs.valueRegister)++;
  1652. l_bc++;
  1653. break;
  1654. // Increment the byte value pointed to by the register
  1655. case asBC_INCi8:
  1656. (**(char**)&m_regs.valueRegister)++;
  1657. l_bc++;
  1658. break;
  1659. // Decrement the short value pointed to by the register
  1660. case asBC_DECi16:
  1661. (**(short**)&m_regs.valueRegister)--;
  1662. l_bc++;
  1663. break;
  1664. // Decrement the byte value pointed to by the register
  1665. case asBC_DECi8:
  1666. (**(char**)&m_regs.valueRegister)--;
  1667. l_bc++;
  1668. break;
  1669. // Increment the integer value pointed to by the register
  1670. case asBC_INCi:
  1671. ++(**(int**)&m_regs.valueRegister);
  1672. l_bc++;
  1673. break;
  1674. // Decrement the integer value pointed to by the register
  1675. case asBC_DECi:
  1676. --(**(int**)&m_regs.valueRegister);
  1677. l_bc++;
  1678. break;
  1679. // Increment the float value pointed to by the register
  1680. case asBC_INCf:
  1681. ++(**(float**)&m_regs.valueRegister);
  1682. l_bc++;
  1683. break;
  1684. // Decrement the float value pointed to by the register
  1685. case asBC_DECf:
  1686. --(**(float**)&m_regs.valueRegister);
  1687. l_bc++;
  1688. break;
  1689. // Increment the double value pointed to by the register
  1690. case asBC_INCd:
  1691. ++(**(double**)&m_regs.valueRegister);
  1692. l_bc++;
  1693. break;
  1694. // Decrement the double value pointed to by the register
  1695. case asBC_DECd:
  1696. --(**(double**)&m_regs.valueRegister);
  1697. l_bc++;
  1698. break;
  1699. // Increment the local integer variable
  1700. case asBC_IncVi:
  1701. (*(int*)(l_fp - asBC_SWORDARG0(l_bc)))++;
  1702. l_bc++;
  1703. break;
  1704. // Decrement the local integer variable
  1705. case asBC_DecVi:
  1706. (*(int*)(l_fp - asBC_SWORDARG0(l_bc)))--;
  1707. l_bc++;
  1708. break;
  1709. //--------------------
  1710. // bits instructions
  1711. // Do a bitwise not on the value in the variable
  1712. case asBC_BNOT:
  1713. *(l_fp - asBC_SWORDARG0(l_bc)) = ~*(l_fp - asBC_SWORDARG0(l_bc));
  1714. l_bc++;
  1715. break;
  1716. // Do a bitwise and of two variables and store the result in a third variable
  1717. case asBC_BAND:
  1718. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) & *(l_fp - asBC_SWORDARG2(l_bc));
  1719. l_bc += 2;
  1720. break;
  1721. // Do a bitwise or of two variables and store the result in a third variable
  1722. case asBC_BOR:
  1723. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) | *(l_fp - asBC_SWORDARG2(l_bc));
  1724. l_bc += 2;
  1725. break;
  1726. // Do a bitwise xor of two variables and store the result in a third variable
  1727. case asBC_BXOR:
  1728. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) ^ *(l_fp - asBC_SWORDARG2(l_bc));
  1729. l_bc += 2;
  1730. break;
  1731. // Do a logical shift left of two variables and store the result in a third variable
  1732. case asBC_BSLL:
  1733. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) << *(l_fp - asBC_SWORDARG2(l_bc));
  1734. l_bc += 2;
  1735. break;
  1736. // Do a logical shift right of two variables and store the result in a third variable
  1737. case asBC_BSRL:
  1738. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc)) >> *(l_fp - asBC_SWORDARG2(l_bc));
  1739. l_bc += 2;
  1740. break;
  1741. // Do an arithmetic shift right of two variables and store the result in a third variable
  1742. case asBC_BSRA:
  1743. *(l_fp - asBC_SWORDARG0(l_bc)) = int(*(l_fp - asBC_SWORDARG1(l_bc))) >> *(l_fp - asBC_SWORDARG2(l_bc));
  1744. l_bc += 2;
  1745. break;
  1746. case asBC_COPY:
  1747. {
  1748. void *d = (void*)*(asPWORD*)l_sp; l_sp += AS_PTR_SIZE;
  1749. void *s = (void*)*(asPWORD*)l_sp;
  1750. if( s == 0 || d == 0 )
  1751. {
  1752. // Need to move the values back to the context
  1753. m_regs.programPointer = l_bc;
  1754. m_regs.stackPointer = l_sp;
  1755. m_regs.stackFramePointer = l_fp;
  1756. // Raise exception
  1757. SetInternalException(TXT_NULL_POINTER_ACCESS);
  1758. return;
  1759. }
  1760. memcpy(d, s, asBC_WORDARG0(l_bc)*4);
  1761. // replace the pointer on the stack with the lvalue
  1762. *(asPWORD**)l_sp = (asPWORD*)d;
  1763. }
  1764. l_bc += 2;
  1765. break;
  1766. case asBC_PshC8:
  1767. l_sp -= 2;
  1768. *(asQWORD*)l_sp = asBC_QWORDARG(l_bc);
  1769. l_bc += 3;
  1770. break;
  1771. case asBC_PshVPtr:
  1772. l_sp -= AS_PTR_SIZE;
  1773. *(asPWORD*)l_sp = *(asPWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  1774. l_bc++;
  1775. break;
  1776. case asBC_RDSPtr:
  1777. {
  1778. // The pointer must not be null
  1779. asPWORD a = *(asPWORD*)l_sp;
  1780. if( a == 0 )
  1781. {
  1782. m_regs.programPointer = l_bc;
  1783. m_regs.stackPointer = l_sp;
  1784. m_regs.stackFramePointer = l_fp;
  1785. SetInternalException(TXT_NULL_POINTER_ACCESS);
  1786. return;
  1787. }
  1788. // Pop an address from the stack, read a pointer from that address and push it on the stack
  1789. *(asPWORD*)l_sp = *(asPWORD*)a;
  1790. }
  1791. l_bc++;
  1792. break;
  1793. //----------------------------
  1794. // Comparisons
  1795. case asBC_CMPd:
  1796. {
  1797. // Do a comparison of the values, rather than a subtraction
  1798. // in order to get proper behaviour for infinity values.
  1799. double dbl1 = *(double*)(l_fp - asBC_SWORDARG0(l_bc));
  1800. double dbl2 = *(double*)(l_fp - asBC_SWORDARG1(l_bc));
  1801. if( dbl1 == dbl2 ) *(int*)&m_regs.valueRegister = 0;
  1802. else if( dbl1 < dbl2 ) *(int*)&m_regs.valueRegister = -1;
  1803. else *(int*)&m_regs.valueRegister = 1;
  1804. l_bc += 2;
  1805. }
  1806. break;
  1807. case asBC_CMPu:
  1808. {
  1809. asDWORD d1 = *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  1810. asDWORD d2 = *(asDWORD*)(l_fp - asBC_SWORDARG1(l_bc));
  1811. if( d1 == d2 ) *(int*)&m_regs.valueRegister = 0;
  1812. else if( d1 < d2 ) *(int*)&m_regs.valueRegister = -1;
  1813. else *(int*)&m_regs.valueRegister = 1;
  1814. l_bc += 2;
  1815. }
  1816. break;
  1817. case asBC_CMPf:
  1818. {
  1819. // Do a comparison of the values, rather than a subtraction
  1820. // in order to get proper behaviour for infinity values.
  1821. float f1 = *(float*)(l_fp - asBC_SWORDARG0(l_bc));
  1822. float f2 = *(float*)(l_fp - asBC_SWORDARG1(l_bc));
  1823. if( f1 == f2 ) *(int*)&m_regs.valueRegister = 0;
  1824. else if( f1 < f2 ) *(int*)&m_regs.valueRegister = -1;
  1825. else *(int*)&m_regs.valueRegister = 1;
  1826. l_bc += 2;
  1827. }
  1828. break;
  1829. case asBC_CMPi:
  1830. {
  1831. int i1 = *(int*)(l_fp - asBC_SWORDARG0(l_bc));
  1832. int i2 = *(int*)(l_fp - asBC_SWORDARG1(l_bc));
  1833. if( i1 == i2 ) *(int*)&m_regs.valueRegister = 0;
  1834. else if( i1 < i2 ) *(int*)&m_regs.valueRegister = -1;
  1835. else *(int*)&m_regs.valueRegister = 1;
  1836. l_bc += 2;
  1837. }
  1838. break;
  1839. //----------------------------
  1840. // Comparisons with constant value
  1841. case asBC_CMPIi:
  1842. {
  1843. int i1 = *(int*)(l_fp - asBC_SWORDARG0(l_bc));
  1844. int i2 = asBC_INTARG(l_bc);
  1845. if( i1 == i2 ) *(int*)&m_regs.valueRegister = 0;
  1846. else if( i1 < i2 ) *(int*)&m_regs.valueRegister = -1;
  1847. else *(int*)&m_regs.valueRegister = 1;
  1848. l_bc += 2;
  1849. }
  1850. break;
  1851. case asBC_CMPIf:
  1852. {
  1853. // Do a comparison of the values, rather than a subtraction
  1854. // in order to get proper behaviour for infinity values.
  1855. float f1 = *(float*)(l_fp - asBC_SWORDARG0(l_bc));
  1856. float f2 = asBC_FLOATARG(l_bc);
  1857. if( f1 == f2 ) *(int*)&m_regs.valueRegister = 0;
  1858. else if( f1 < f2 ) *(int*)&m_regs.valueRegister = -1;
  1859. else *(int*)&m_regs.valueRegister = 1;
  1860. l_bc += 2;
  1861. }
  1862. break;
  1863. case asBC_CMPIu:
  1864. {
  1865. asDWORD d1 = *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  1866. asDWORD d2 = asBC_DWORDARG(l_bc);
  1867. if( d1 == d2 ) *(int*)&m_regs.valueRegister = 0;
  1868. else if( d1 < d2 ) *(int*)&m_regs.valueRegister = -1;
  1869. else *(int*)&m_regs.valueRegister = 1;
  1870. l_bc += 2;
  1871. }
  1872. break;
  1873. case asBC_JMPP:
  1874. l_bc += 1 + (*(int*)(l_fp - asBC_SWORDARG0(l_bc)))*2;
  1875. break;
  1876. case asBC_PopRPtr:
  1877. *(asPWORD*)&m_regs.valueRegister = *(asPWORD*)l_sp;
  1878. l_sp += AS_PTR_SIZE;
  1879. l_bc++;
  1880. break;
  1881. case asBC_PshRPtr:
  1882. l_sp -= AS_PTR_SIZE;
  1883. *(asPWORD*)l_sp = *(asPWORD*)&m_regs.valueRegister;
  1884. l_bc++;
  1885. break;
  1886. case asBC_STR:
  1887. {
  1888. // Get the string id from the argument
  1889. asWORD w = asBC_WORDARG0(l_bc);
  1890. // Push the string pointer on the stack
  1891. const asCString &b = m_engine->GetConstantString(w);
  1892. l_sp -= AS_PTR_SIZE;
  1893. *(asPWORD*)l_sp = (asPWORD)b.AddressOf();
  1894. // Push the string length on the stack
  1895. --l_sp;
  1896. *l_sp = (asDWORD)b.GetLength();
  1897. l_bc++;
  1898. }
  1899. break;
  1900. case asBC_CALLSYS:
  1901. {
  1902. // Get function ID from the argument
  1903. int i = asBC_INTARG(l_bc);
  1904. // Need to move the values back to the context as the called functions
  1905. // may use the debug interface to inspect the registers
  1906. m_regs.programPointer = l_bc;
  1907. m_regs.stackPointer = l_sp;
  1908. m_regs.stackFramePointer = l_fp;
  1909. l_sp += CallSystemFunction(i, this, 0);
  1910. // Update the program position after the call so that line number is correct
  1911. l_bc += 2;
  1912. if( m_regs.doProcessSuspend )
  1913. {
  1914. // Should the execution be suspended?
  1915. if( m_doSuspend )
  1916. {
  1917. m_regs.programPointer = l_bc;
  1918. m_regs.stackPointer = l_sp;
  1919. m_regs.stackFramePointer = l_fp;
  1920. m_status = asEXECUTION_SUSPENDED;
  1921. return;
  1922. }
  1923. // An exception might have been raised
  1924. if( m_status != asEXECUTION_ACTIVE )
  1925. {
  1926. m_regs.programPointer = l_bc;
  1927. m_regs.stackPointer = l_sp;
  1928. m_regs.stackFramePointer = l_fp;
  1929. return;
  1930. }
  1931. }
  1932. }
  1933. break;
  1934. case asBC_CALLBND:
  1935. {
  1936. // Get the function ID from the stack
  1937. int i = asBC_INTARG(l_bc);
  1938. l_bc += 2;
  1939. asASSERT( i >= 0 );
  1940. asASSERT( i & FUNC_IMPORTED );
  1941. // Need to move the values back to the context
  1942. m_regs.programPointer = l_bc;
  1943. m_regs.stackPointer = l_sp;
  1944. m_regs.stackFramePointer = l_fp;
  1945. int funcId = m_engine->importedFunctions[i & ~FUNC_IMPORTED]->boundFunctionId;
  1946. if( funcId == -1 )
  1947. {
  1948. // Tell the exception handler to clean up the arguments to this function
  1949. m_needToCleanupArgs = true;
  1950. SetInternalException(TXT_UNBOUND_FUNCTION);
  1951. return;
  1952. }
  1953. else
  1954. {
  1955. asCScriptFunction *func = m_engine->GetScriptFunction(funcId);
  1956. CallScriptFunction(func);
  1957. }
  1958. // Extract the values from the context again
  1959. l_bc = m_regs.programPointer;
  1960. l_sp = m_regs.stackPointer;
  1961. l_fp = m_regs.stackFramePointer;
  1962. // If status isn't active anymore then we must stop
  1963. if( m_status != asEXECUTION_ACTIVE )
  1964. return;
  1965. }
  1966. break;
  1967. case asBC_SUSPEND:
  1968. if( m_regs.doProcessSuspend )
  1969. {
  1970. if( m_lineCallback )
  1971. {
  1972. m_regs.programPointer = l_bc;
  1973. m_regs.stackPointer = l_sp;
  1974. m_regs.stackFramePointer = l_fp;
  1975. CallLineCallback();
  1976. }
  1977. if( m_doSuspend )
  1978. {
  1979. l_bc++;
  1980. // Need to move the values back to the context
  1981. m_regs.programPointer = l_bc;
  1982. m_regs.stackPointer = l_sp;
  1983. m_regs.stackFramePointer = l_fp;
  1984. m_status = asEXECUTION_SUSPENDED;
  1985. return;
  1986. }
  1987. }
  1988. l_bc++;
  1989. break;
  1990. case asBC_ALLOC:
  1991. {
  1992. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(l_bc);
  1993. int func = asBC_INTARG(l_bc+AS_PTR_SIZE);
  1994. if( objType->flags & asOBJ_SCRIPT_OBJECT )
  1995. {
  1996. // Need to move the values back to the context as the construction
  1997. // of the script object may reuse the context for nested calls.
  1998. m_regs.programPointer = l_bc;
  1999. m_regs.stackPointer = l_sp;
  2000. m_regs.stackFramePointer = l_fp;
  2001. // Pre-allocate the memory
  2002. asDWORD *mem = (asDWORD*)m_engine->CallAlloc(objType);
  2003. // Pre-initialize the memory by calling the constructor for asCScriptObject
  2004. ScriptObject_Construct(objType, (asCScriptObject*)mem);
  2005. // Call the constructor to initalize the memory
  2006. asCScriptFunction *f = m_engine->scriptFunctions[func];
  2007. asDWORD **a = (asDWORD**)*(asPWORD*)(m_regs.stackPointer + f->GetSpaceNeededForArguments());
  2008. if( a ) *a = mem;
  2009. // Push the object pointer on the stack
  2010. m_regs.stackPointer -= AS_PTR_SIZE;
  2011. *(asPWORD*)m_regs.stackPointer = (asPWORD)mem;
  2012. m_regs.programPointer += 2+AS_PTR_SIZE;
  2013. CallScriptFunction(f);
  2014. // Extract the values from the context again
  2015. l_bc = m_regs.programPointer;
  2016. l_sp = m_regs.stackPointer;
  2017. l_fp = m_regs.stackFramePointer;
  2018. // If status isn't active anymore then we must stop
  2019. if( m_status != asEXECUTION_ACTIVE )
  2020. return;
  2021. }
  2022. else
  2023. {
  2024. // Pre-allocate the memory
  2025. asDWORD *mem = (asDWORD*)m_engine->CallAlloc(objType);
  2026. if( func )
  2027. {
  2028. // Need to move the values back to the context as the called functions
  2029. // may use the debug interface to inspect the registers
  2030. m_regs.programPointer = l_bc;
  2031. m_regs.stackPointer = l_sp;
  2032. m_regs.stackFramePointer = l_fp;
  2033. l_sp += CallSystemFunction(func, this, mem);
  2034. }
  2035. // Pop the variable address from the stack
  2036. asDWORD **a = (asDWORD**)*(asPWORD*)l_sp;
  2037. l_sp += AS_PTR_SIZE;
  2038. if( a ) *a = mem;
  2039. l_bc += 2+AS_PTR_SIZE;
  2040. if( m_regs.doProcessSuspend )
  2041. {
  2042. // Should the execution be suspended?
  2043. if( m_doSuspend )
  2044. {
  2045. m_regs.programPointer = l_bc;
  2046. m_regs.stackPointer = l_sp;
  2047. m_regs.stackFramePointer = l_fp;
  2048. m_status = asEXECUTION_SUSPENDED;
  2049. return;
  2050. }
  2051. // An exception might have been raised
  2052. if( m_status != asEXECUTION_ACTIVE )
  2053. {
  2054. m_regs.programPointer = l_bc;
  2055. m_regs.stackPointer = l_sp;
  2056. m_regs.stackFramePointer = l_fp;
  2057. m_engine->CallFree(mem);
  2058. *a = 0;
  2059. return;
  2060. }
  2061. }
  2062. }
  2063. }
  2064. break;
  2065. case asBC_FREE:
  2066. {
  2067. // Get the variable that holds the object handle/reference
  2068. asPWORD *a = (asPWORD*)asPWORD(l_fp - asBC_SWORDARG0(l_bc));
  2069. if( *a )
  2070. {
  2071. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(l_bc);
  2072. asSTypeBehaviour *beh = &objType->beh;
  2073. // Need to move the values back to the context as the called functions
  2074. // may use the debug interface to inspect the registers
  2075. m_regs.programPointer = l_bc;
  2076. m_regs.stackPointer = l_sp;
  2077. m_regs.stackFramePointer = l_fp;
  2078. if( objType->flags & asOBJ_REF )
  2079. {
  2080. asASSERT( (objType->flags & asOBJ_NOCOUNT) || beh->release );
  2081. if( beh->release )
  2082. m_engine->CallObjectMethod((void*)(asPWORD)*a, beh->release);
  2083. }
  2084. else
  2085. {
  2086. if( beh->destruct )
  2087. m_engine->CallObjectMethod((void*)(asPWORD)*a, beh->destruct);
  2088. else if( objType->flags & asOBJ_LIST_PATTERN )
  2089. m_engine->DestroyList((asBYTE*)(asPWORD)*a, objType);
  2090. m_engine->CallFree((void*)(asPWORD)*a);
  2091. }
  2092. // Clear the variable
  2093. *a = 0;
  2094. }
  2095. }
  2096. l_bc += 1+AS_PTR_SIZE;
  2097. break;
  2098. case asBC_LOADOBJ:
  2099. {
  2100. // Move the object pointer from the object variable into the object register
  2101. void **a = (void**)(l_fp - asBC_SWORDARG0(l_bc));
  2102. m_regs.objectType = 0;
  2103. m_regs.objectRegister = *a;
  2104. *a = 0;
  2105. }
  2106. l_bc++;
  2107. break;
  2108. case asBC_STOREOBJ:
  2109. // Move the object pointer from the object register to the object variable
  2110. *(asPWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = asPWORD(m_regs.objectRegister);
  2111. m_regs.objectRegister = 0;
  2112. l_bc++;
  2113. break;
  2114. case asBC_GETOBJ:
  2115. {
  2116. // Read variable index from location on stack
  2117. asPWORD *a = (asPWORD*)(l_sp + asBC_WORDARG0(l_bc));
  2118. asDWORD offset = *(asDWORD*)a;
  2119. // Move pointer from variable to the same location on the stack
  2120. asPWORD *v = (asPWORD*)(l_fp - offset);
  2121. *a = *v;
  2122. // Clear variable
  2123. *v = 0;
  2124. }
  2125. l_bc++;
  2126. break;
  2127. case asBC_REFCPY:
  2128. {
  2129. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(l_bc);
  2130. asSTypeBehaviour *beh = &objType->beh;
  2131. // Pop address of destination pointer from the stack
  2132. void **d = (void**)*(asPWORD*)l_sp;
  2133. l_sp += AS_PTR_SIZE;
  2134. // Read wanted pointer from the stack
  2135. void *s = (void*)*(asPWORD*)l_sp;
  2136. // Need to move the values back to the context as the called functions
  2137. // may use the debug interface to inspect the registers
  2138. m_regs.programPointer = l_bc;
  2139. m_regs.stackPointer = l_sp;
  2140. m_regs.stackFramePointer = l_fp;
  2141. if( !(objType->flags & asOBJ_NOCOUNT) )
  2142. {
  2143. // Release previous object held by destination pointer
  2144. if( *d != 0 )
  2145. m_engine->CallObjectMethod(*d, beh->release);
  2146. // Increase ref counter of wanted object
  2147. if( s != 0 )
  2148. m_engine->CallObjectMethod(s, beh->addref);
  2149. }
  2150. // Set the new object in the destination
  2151. *d = s;
  2152. }
  2153. l_bc += 1+AS_PTR_SIZE;
  2154. break;
  2155. case asBC_CHKREF:
  2156. {
  2157. // Verify if the pointer on the stack is null
  2158. // This is used when validating a pointer that an operator will work on
  2159. asPWORD a = *(asPWORD*)l_sp;
  2160. if( a == 0 )
  2161. {
  2162. m_regs.programPointer = l_bc;
  2163. m_regs.stackPointer = l_sp;
  2164. m_regs.stackFramePointer = l_fp;
  2165. SetInternalException(TXT_NULL_POINTER_ACCESS);
  2166. return;
  2167. }
  2168. }
  2169. l_bc++;
  2170. break;
  2171. case asBC_GETOBJREF:
  2172. {
  2173. // Get the location on the stack where the reference will be placed
  2174. asPWORD *a = (asPWORD*)(l_sp + asBC_WORDARG0(l_bc));
  2175. // Replace the variable index with the object handle held in the variable
  2176. *(asPWORD**)a = *(asPWORD**)(l_fp - *a);
  2177. }
  2178. l_bc++;
  2179. break;
  2180. case asBC_GETREF:
  2181. {
  2182. // Get the location on the stack where the reference will be placed
  2183. asPWORD *a = (asPWORD*)(l_sp + asBC_WORDARG0(l_bc));
  2184. // Replace the variable index with the address of the variable
  2185. *(asPWORD**)a = (asPWORD*)(l_fp - (int)*a);
  2186. }
  2187. l_bc++;
  2188. break;
  2189. case asBC_PshNull:
  2190. // Push a null pointer on the stack
  2191. l_sp -= AS_PTR_SIZE;
  2192. *(asPWORD*)l_sp = 0;
  2193. l_bc++;
  2194. break;
  2195. case asBC_ClrVPtr:
  2196. // TODO: runtime optimize: Is this instruction really necessary?
  2197. // CallScriptFunction() can clear the null handles upon entry, just as is done for
  2198. // all other object variables
  2199. // Clear pointer variable
  2200. *(asPWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = 0;
  2201. l_bc++;
  2202. break;
  2203. case asBC_OBJTYPE:
  2204. // Push the object type on the stack
  2205. l_sp -= AS_PTR_SIZE;
  2206. *(asPWORD*)l_sp = asBC_PTRARG(l_bc);
  2207. l_bc += 1+AS_PTR_SIZE;
  2208. break;
  2209. case asBC_TYPEID:
  2210. // Equivalent to PshC4, but kept as separate instruction for bytecode serialization
  2211. --l_sp;
  2212. *l_sp = asBC_DWORDARG(l_bc);
  2213. l_bc += 2;
  2214. break;
  2215. case asBC_SetV4:
  2216. *(l_fp - asBC_SWORDARG0(l_bc)) = asBC_DWORDARG(l_bc);
  2217. l_bc += 2;
  2218. break;
  2219. case asBC_SetV8:
  2220. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = asBC_QWORDARG(l_bc);
  2221. l_bc += 3;
  2222. break;
  2223. case asBC_ADDSi:
  2224. {
  2225. // The pointer must not be null
  2226. asPWORD a = *(asPWORD*)l_sp;
  2227. if( a == 0 )
  2228. {
  2229. m_regs.programPointer = l_bc;
  2230. m_regs.stackPointer = l_sp;
  2231. m_regs.stackFramePointer = l_fp;
  2232. SetInternalException(TXT_NULL_POINTER_ACCESS);
  2233. return;
  2234. }
  2235. // Add an offset to the pointer
  2236. *(asPWORD*)l_sp = a + asBC_SWORDARG0(l_bc);
  2237. }
  2238. l_bc += 2;
  2239. break;
  2240. case asBC_CpyVtoV4:
  2241. *(l_fp - asBC_SWORDARG0(l_bc)) = *(l_fp - asBC_SWORDARG1(l_bc));
  2242. l_bc += 2;
  2243. break;
  2244. case asBC_CpyVtoV8:
  2245. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc));
  2246. l_bc += 2;
  2247. break;
  2248. case asBC_CpyVtoR4:
  2249. *(asDWORD*)&m_regs.valueRegister = *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2250. l_bc++;
  2251. break;
  2252. case asBC_CpyVtoR8:
  2253. *(asQWORD*)&m_regs.valueRegister = *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2254. l_bc++;
  2255. break;
  2256. case asBC_CpyVtoG4:
  2257. *(asDWORD*)asBC_PTRARG(l_bc) = *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2258. l_bc += 1 + AS_PTR_SIZE;
  2259. break;
  2260. case asBC_CpyRtoV4:
  2261. *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asDWORD*)&m_regs.valueRegister;
  2262. l_bc++;
  2263. break;
  2264. case asBC_CpyRtoV8:
  2265. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = m_regs.valueRegister;
  2266. l_bc++;
  2267. break;
  2268. case asBC_CpyGtoV4:
  2269. *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asDWORD*)asBC_PTRARG(l_bc);
  2270. l_bc += 1 + AS_PTR_SIZE;
  2271. break;
  2272. case asBC_WRTV1:
  2273. // The pointer in the register points to a byte, and *(l_fp - offset) too
  2274. **(asBYTE**)&m_regs.valueRegister = *(asBYTE*)(l_fp - asBC_SWORDARG0(l_bc));
  2275. l_bc++;
  2276. break;
  2277. case asBC_WRTV2:
  2278. // The pointer in the register points to a word, and *(l_fp - offset) too
  2279. **(asWORD**)&m_regs.valueRegister = *(asWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2280. l_bc++;
  2281. break;
  2282. case asBC_WRTV4:
  2283. **(asDWORD**)&m_regs.valueRegister = *(l_fp - asBC_SWORDARG0(l_bc));
  2284. l_bc++;
  2285. break;
  2286. case asBC_WRTV8:
  2287. **(asQWORD**)&m_regs.valueRegister = *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2288. l_bc++;
  2289. break;
  2290. case asBC_RDR1:
  2291. {
  2292. // The pointer in the register points to a byte, and *(l_fp - offset) will also point to a byte
  2293. asBYTE *bPtr = (asBYTE*)(l_fp - asBC_SWORDARG0(l_bc));
  2294. bPtr[0] = **(asBYTE**)&m_regs.valueRegister; // read the byte
  2295. bPtr[1] = 0; // 0 the rest of the DWORD
  2296. bPtr[2] = 0;
  2297. bPtr[3] = 0;
  2298. }
  2299. l_bc++;
  2300. break;
  2301. case asBC_RDR2:
  2302. {
  2303. // The pointer in the register points to a word, and *(l_fp - offset) will also point to a word
  2304. asWORD *wPtr = (asWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2305. wPtr[0] = **(asWORD**)&m_regs.valueRegister; // read the word
  2306. wPtr[1] = 0; // 0 the rest of the DWORD
  2307. }
  2308. l_bc++;
  2309. break;
  2310. case asBC_RDR4:
  2311. *(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = **(asDWORD**)&m_regs.valueRegister;
  2312. l_bc++;
  2313. break;
  2314. case asBC_RDR8:
  2315. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = **(asQWORD**)&m_regs.valueRegister;
  2316. l_bc++;
  2317. break;
  2318. case asBC_LDG:
  2319. *(asPWORD*)&m_regs.valueRegister = asBC_PTRARG(l_bc);
  2320. l_bc += 1+AS_PTR_SIZE;
  2321. break;
  2322. case asBC_LDV:
  2323. *(asDWORD**)&m_regs.valueRegister = (l_fp - asBC_SWORDARG0(l_bc));
  2324. l_bc++;
  2325. break;
  2326. case asBC_PGA:
  2327. l_sp -= AS_PTR_SIZE;
  2328. *(asPWORD*)l_sp = asBC_PTRARG(l_bc);
  2329. l_bc += 1+AS_PTR_SIZE;
  2330. break;
  2331. case asBC_CmpPtr:
  2332. {
  2333. // TODO: runtime optimize: This instruction should really just be an equals, and return true or false.
  2334. // The instruction is only used for is and !is tests anyway.
  2335. asPWORD p1 = *(asPWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2336. asPWORD p2 = *(asPWORD*)(l_fp - asBC_SWORDARG1(l_bc));
  2337. if( p1 == p2 ) *(int*)&m_regs.valueRegister = 0;
  2338. else if( p1 < p2 ) *(int*)&m_regs.valueRegister = -1;
  2339. else *(int*)&m_regs.valueRegister = 1;
  2340. l_bc += 2;
  2341. }
  2342. break;
  2343. case asBC_VAR:
  2344. l_sp -= AS_PTR_SIZE;
  2345. *(asPWORD*)l_sp = (asPWORD)asBC_SWORDARG0(l_bc);
  2346. l_bc++;
  2347. break;
  2348. //----------------------------
  2349. // Type conversions
  2350. case asBC_iTOf:
  2351. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = float(*(int*)(l_fp - asBC_SWORDARG0(l_bc)));
  2352. l_bc++;
  2353. break;
  2354. case asBC_fTOi:
  2355. *(l_fp - asBC_SWORDARG0(l_bc)) = int(*(float*)(l_fp - asBC_SWORDARG0(l_bc)));
  2356. l_bc++;
  2357. break;
  2358. case asBC_uTOf:
  2359. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = float(*(l_fp - asBC_SWORDARG0(l_bc)));
  2360. l_bc++;
  2361. break;
  2362. case asBC_fTOu:
  2363. // We must cast to int first, because on some compilers the cast of a negative float value to uint result in 0
  2364. *(l_fp - asBC_SWORDARG0(l_bc)) = asUINT(int(*(float*)(l_fp - asBC_SWORDARG0(l_bc))));
  2365. l_bc++;
  2366. break;
  2367. case asBC_sbTOi:
  2368. // *(l_fp - offset) points to a char, and will point to an int afterwards
  2369. *(l_fp - asBC_SWORDARG0(l_bc)) = *(signed char*)(l_fp - asBC_SWORDARG0(l_bc));
  2370. l_bc++;
  2371. break;
  2372. case asBC_swTOi:
  2373. // *(l_fp - offset) points to a short, and will point to an int afterwards
  2374. *(l_fp - asBC_SWORDARG0(l_bc)) = *(short*)(l_fp - asBC_SWORDARG0(l_bc));
  2375. l_bc++;
  2376. break;
  2377. case asBC_ubTOi:
  2378. // (l_fp - offset) points to a byte, and will point to an int afterwards
  2379. *(l_fp - asBC_SWORDARG0(l_bc)) = *(asBYTE*)(l_fp - asBC_SWORDARG0(l_bc));
  2380. l_bc++;
  2381. break;
  2382. case asBC_uwTOi:
  2383. // *(l_fp - offset) points to a word, and will point to an int afterwards
  2384. *(l_fp - asBC_SWORDARG0(l_bc)) = *(asWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2385. l_bc++;
  2386. break;
  2387. case asBC_dTOi:
  2388. *(l_fp - asBC_SWORDARG0(l_bc)) = int(*(double*)(l_fp - asBC_SWORDARG1(l_bc)));
  2389. l_bc += 2;
  2390. break;
  2391. case asBC_dTOu:
  2392. // We must cast to int first, because on some compilers the cast of a negative float value to uint result in 0
  2393. *(l_fp - asBC_SWORDARG0(l_bc)) = asUINT(int(*(double*)(l_fp - asBC_SWORDARG1(l_bc))));
  2394. l_bc += 2;
  2395. break;
  2396. case asBC_dTOf:
  2397. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = float(*(double*)(l_fp - asBC_SWORDARG1(l_bc)));
  2398. l_bc += 2;
  2399. break;
  2400. case asBC_iTOd:
  2401. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = double(*(int*)(l_fp - asBC_SWORDARG1(l_bc)));
  2402. l_bc += 2;
  2403. break;
  2404. case asBC_uTOd:
  2405. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = double(*(asUINT*)(l_fp - asBC_SWORDARG1(l_bc)));
  2406. l_bc += 2;
  2407. break;
  2408. case asBC_fTOd:
  2409. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = double(*(float*)(l_fp - asBC_SWORDARG1(l_bc)));
  2410. l_bc += 2;
  2411. break;
  2412. //------------------------------
  2413. // Math operations
  2414. case asBC_ADDi:
  2415. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) + *(int*)(l_fp - asBC_SWORDARG2(l_bc));
  2416. l_bc += 2;
  2417. break;
  2418. case asBC_SUBi:
  2419. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) - *(int*)(l_fp - asBC_SWORDARG2(l_bc));
  2420. l_bc += 2;
  2421. break;
  2422. case asBC_MULi:
  2423. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) * *(int*)(l_fp - asBC_SWORDARG2(l_bc));
  2424. l_bc += 2;
  2425. break;
  2426. case asBC_DIVi:
  2427. {
  2428. int divider = *(int*)(l_fp - asBC_SWORDARG2(l_bc));
  2429. if( divider == 0 )
  2430. {
  2431. // Need to move the values back to the context
  2432. m_regs.programPointer = l_bc;
  2433. m_regs.stackPointer = l_sp;
  2434. m_regs.stackFramePointer = l_fp;
  2435. // Raise exception
  2436. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2437. return;
  2438. }
  2439. else if( divider == -1 )
  2440. {
  2441. // Need to check if the value that is divided is 0x80000000
  2442. // as dividing it with -1 will cause an overflow exception
  2443. if( *(int*)(l_fp - asBC_SWORDARG1(l_bc)) == int(0x80000000) )
  2444. {
  2445. // Need to move the values back to the context
  2446. m_regs.programPointer = l_bc;
  2447. m_regs.stackPointer = l_sp;
  2448. m_regs.stackFramePointer = l_fp;
  2449. // Raise exception
  2450. SetInternalException(TXT_DIVIDE_OVERFLOW);
  2451. return;
  2452. }
  2453. }
  2454. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) / divider;
  2455. }
  2456. l_bc += 2;
  2457. break;
  2458. case asBC_MODi:
  2459. {
  2460. int divider = *(int*)(l_fp - asBC_SWORDARG2(l_bc));
  2461. if( divider == 0 )
  2462. {
  2463. // Need to move the values back to the context
  2464. m_regs.programPointer = l_bc;
  2465. m_regs.stackPointer = l_sp;
  2466. m_regs.stackFramePointer = l_fp;
  2467. // Raise exception
  2468. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2469. return;
  2470. }
  2471. else if( divider == -1 )
  2472. {
  2473. // Need to check if the value that is divided is 0x80000000
  2474. // as dividing it with -1 will cause an overflow exception
  2475. if( *(int*)(l_fp - asBC_SWORDARG1(l_bc)) == int(0x80000000) )
  2476. {
  2477. // Need to move the values back to the context
  2478. m_regs.programPointer = l_bc;
  2479. m_regs.stackPointer = l_sp;
  2480. m_regs.stackFramePointer = l_fp;
  2481. // Raise exception
  2482. SetInternalException(TXT_DIVIDE_OVERFLOW);
  2483. return;
  2484. }
  2485. }
  2486. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) % divider;
  2487. }
  2488. l_bc += 2;
  2489. break;
  2490. case asBC_ADDf:
  2491. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = *(float*)(l_fp - asBC_SWORDARG1(l_bc)) + *(float*)(l_fp - asBC_SWORDARG2(l_bc));
  2492. l_bc += 2;
  2493. break;
  2494. case asBC_SUBf:
  2495. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = *(float*)(l_fp - asBC_SWORDARG1(l_bc)) - *(float*)(l_fp - asBC_SWORDARG2(l_bc));
  2496. l_bc += 2;
  2497. break;
  2498. case asBC_MULf:
  2499. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = *(float*)(l_fp - asBC_SWORDARG1(l_bc)) * *(float*)(l_fp - asBC_SWORDARG2(l_bc));
  2500. l_bc += 2;
  2501. break;
  2502. case asBC_DIVf:
  2503. {
  2504. float divider = *(float*)(l_fp - asBC_SWORDARG2(l_bc));
  2505. if( divider == 0 )
  2506. {
  2507. // Need to move the values back to the context
  2508. m_regs.programPointer = l_bc;
  2509. m_regs.stackPointer = l_sp;
  2510. m_regs.stackFramePointer = l_fp;
  2511. // Raise exception
  2512. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2513. return;
  2514. }
  2515. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = *(float*)(l_fp - asBC_SWORDARG1(l_bc)) / divider;
  2516. }
  2517. l_bc += 2;
  2518. break;
  2519. case asBC_MODf:
  2520. {
  2521. float divider = *(float*)(l_fp - asBC_SWORDARG2(l_bc));
  2522. if( divider == 0 )
  2523. {
  2524. // Need to move the values back to the context
  2525. m_regs.programPointer = l_bc;
  2526. m_regs.stackPointer = l_sp;
  2527. m_regs.stackFramePointer = l_fp;
  2528. // Raise exception
  2529. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2530. return;
  2531. }
  2532. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = fmodf(*(float*)(l_fp - asBC_SWORDARG1(l_bc)), divider);
  2533. }
  2534. l_bc += 2;
  2535. break;
  2536. case asBC_ADDd:
  2537. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = *(double*)(l_fp - asBC_SWORDARG1(l_bc)) + *(double*)(l_fp - asBC_SWORDARG2(l_bc));
  2538. l_bc += 2;
  2539. break;
  2540. case asBC_SUBd:
  2541. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = *(double*)(l_fp - asBC_SWORDARG1(l_bc)) - *(double*)(l_fp - asBC_SWORDARG2(l_bc));
  2542. l_bc += 2;
  2543. break;
  2544. case asBC_MULd:
  2545. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = *(double*)(l_fp - asBC_SWORDARG1(l_bc)) * *(double*)(l_fp - asBC_SWORDARG2(l_bc));
  2546. l_bc += 2;
  2547. break;
  2548. case asBC_DIVd:
  2549. {
  2550. double divider = *(double*)(l_fp - asBC_SWORDARG2(l_bc));
  2551. if( divider == 0 )
  2552. {
  2553. // Need to move the values back to the context
  2554. m_regs.programPointer = l_bc;
  2555. m_regs.stackPointer = l_sp;
  2556. m_regs.stackFramePointer = l_fp;
  2557. // Raise exception
  2558. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2559. return;
  2560. }
  2561. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = *(double*)(l_fp - asBC_SWORDARG1(l_bc)) / divider;
  2562. l_bc += 2;
  2563. }
  2564. break;
  2565. case asBC_MODd:
  2566. {
  2567. double divider = *(double*)(l_fp - asBC_SWORDARG2(l_bc));
  2568. if( divider == 0 )
  2569. {
  2570. // Need to move the values back to the context
  2571. m_regs.programPointer = l_bc;
  2572. m_regs.stackPointer = l_sp;
  2573. m_regs.stackFramePointer = l_fp;
  2574. // Raise exception
  2575. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2576. return;
  2577. }
  2578. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = fmod(*(double*)(l_fp - asBC_SWORDARG1(l_bc)), divider);
  2579. l_bc += 2;
  2580. }
  2581. break;
  2582. //------------------------------
  2583. // Math operations with constant value
  2584. case asBC_ADDIi:
  2585. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) + asBC_INTARG(l_bc+1);
  2586. l_bc += 3;
  2587. break;
  2588. case asBC_SUBIi:
  2589. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) - asBC_INTARG(l_bc+1);
  2590. l_bc += 3;
  2591. break;
  2592. case asBC_MULIi:
  2593. *(int*)(l_fp - asBC_SWORDARG0(l_bc)) = *(int*)(l_fp - asBC_SWORDARG1(l_bc)) * asBC_INTARG(l_bc+1);
  2594. l_bc += 3;
  2595. break;
  2596. case asBC_ADDIf:
  2597. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = *(float*)(l_fp - asBC_SWORDARG1(l_bc)) + asBC_FLOATARG(l_bc+1);
  2598. l_bc += 3;
  2599. break;
  2600. case asBC_SUBIf:
  2601. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = *(float*)(l_fp - asBC_SWORDARG1(l_bc)) - asBC_FLOATARG(l_bc+1);
  2602. l_bc += 3;
  2603. break;
  2604. case asBC_MULIf:
  2605. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = *(float*)(l_fp - asBC_SWORDARG1(l_bc)) * asBC_FLOATARG(l_bc+1);
  2606. l_bc += 3;
  2607. break;
  2608. //-----------------------------------
  2609. case asBC_SetG4:
  2610. *(asDWORD*)asBC_PTRARG(l_bc) = asBC_DWORDARG(l_bc+AS_PTR_SIZE);
  2611. l_bc += 2 + AS_PTR_SIZE;
  2612. break;
  2613. case asBC_ChkRefS:
  2614. {
  2615. // Verify if the pointer on the stack refers to a non-null value
  2616. // This is used to validate a reference to a handle
  2617. asPWORD *a = (asPWORD*)*(asPWORD*)l_sp;
  2618. if( *a == 0 )
  2619. {
  2620. m_regs.programPointer = l_bc;
  2621. m_regs.stackPointer = l_sp;
  2622. m_regs.stackFramePointer = l_fp;
  2623. SetInternalException(TXT_NULL_POINTER_ACCESS);
  2624. return;
  2625. }
  2626. }
  2627. l_bc++;
  2628. break;
  2629. case asBC_ChkNullV:
  2630. {
  2631. // Verify if variable (on the stack) is not null
  2632. asDWORD *a = *(asDWORD**)(l_fp - asBC_SWORDARG0(l_bc));
  2633. if( a == 0 )
  2634. {
  2635. m_regs.programPointer = l_bc;
  2636. m_regs.stackPointer = l_sp;
  2637. m_regs.stackFramePointer = l_fp;
  2638. SetInternalException(TXT_NULL_POINTER_ACCESS);
  2639. return;
  2640. }
  2641. }
  2642. l_bc++;
  2643. break;
  2644. case asBC_CALLINTF:
  2645. {
  2646. int i = asBC_INTARG(l_bc);
  2647. l_bc += 2;
  2648. asASSERT( i >= 0 );
  2649. asASSERT( (i & FUNC_IMPORTED) == 0 );
  2650. // Need to move the values back to the context
  2651. m_regs.programPointer = l_bc;
  2652. m_regs.stackPointer = l_sp;
  2653. m_regs.stackFramePointer = l_fp;
  2654. CallInterfaceMethod(m_engine->GetScriptFunction(i));
  2655. // Extract the values from the context again
  2656. l_bc = m_regs.programPointer;
  2657. l_sp = m_regs.stackPointer;
  2658. l_fp = m_regs.stackFramePointer;
  2659. // If status isn't active anymore then we must stop
  2660. if( m_status != asEXECUTION_ACTIVE )
  2661. return;
  2662. }
  2663. break;
  2664. case asBC_iTOb:
  2665. {
  2666. // *(l_fp - offset) points to an int, and will point to a byte afterwards
  2667. // We need to use volatile here to tell the compiler not to rearrange
  2668. // read and write operations during optimizations.
  2669. volatile asDWORD val = *(l_fp - asBC_SWORDARG0(l_bc));
  2670. volatile asBYTE *bPtr = (asBYTE*)(l_fp - asBC_SWORDARG0(l_bc));
  2671. bPtr[0] = (asBYTE)val; // write the byte
  2672. bPtr[1] = 0; // 0 the rest of the DWORD
  2673. bPtr[2] = 0;
  2674. bPtr[3] = 0;
  2675. }
  2676. l_bc++;
  2677. break;
  2678. case asBC_iTOw:
  2679. {
  2680. // *(l_fp - offset) points to an int, and will point to word afterwards
  2681. // We need to use volatile here to tell the compiler not to rearrange
  2682. // read and write operations during optimizations.
  2683. volatile asDWORD val = *(l_fp - asBC_SWORDARG0(l_bc));
  2684. volatile asWORD *wPtr = (asWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2685. wPtr[0] = (asWORD)val; // write the word
  2686. wPtr[1] = 0; // 0 the rest of the DWORD
  2687. }
  2688. l_bc++;
  2689. break;
  2690. case asBC_SetV1:
  2691. // TODO: This is exactly the same as SetV4. This is a left over from the time
  2692. // when the bytecode instructions were more tightly packed. It can now
  2693. // be removed. When removing it, make sure the value is correctly converted
  2694. // on big-endian CPUs.
  2695. // The byte is already stored correctly in the argument
  2696. *(l_fp - asBC_SWORDARG0(l_bc)) = asBC_DWORDARG(l_bc);
  2697. l_bc += 2;
  2698. break;
  2699. case asBC_SetV2:
  2700. // TODO: This is exactly the same as SetV4. This is a left over from the time
  2701. // when the bytecode instructions were more tightly packed. It can now
  2702. // be removed. When removing it, make sure the value is correctly converted
  2703. // on big-endian CPUs.
  2704. // The word is already stored correctly in the argument
  2705. *(l_fp - asBC_SWORDARG0(l_bc)) = asBC_DWORDARG(l_bc);
  2706. l_bc += 2;
  2707. break;
  2708. case asBC_Cast:
  2709. // Cast the handle at the top of the stack to the type in the argument
  2710. {
  2711. asDWORD **a = (asDWORD**)*(asPWORD*)l_sp;
  2712. if( a && *a )
  2713. {
  2714. asDWORD typeId = asBC_DWORDARG(l_bc);
  2715. asCScriptObject *obj = (asCScriptObject *)* a;
  2716. asCObjectType *objType = obj->objType;
  2717. asCObjectType *to = m_engine->GetObjectTypeFromTypeId(typeId);
  2718. // This instruction can only be used with script classes and interfaces
  2719. asASSERT( objType->flags & asOBJ_SCRIPT_OBJECT );
  2720. asASSERT( to->flags & asOBJ_SCRIPT_OBJECT );
  2721. if( objType->Implements(to) || objType->DerivesFrom(to) )
  2722. {
  2723. m_regs.objectType = 0;
  2724. m_regs.objectRegister = obj;
  2725. obj->AddRef();
  2726. }
  2727. else
  2728. {
  2729. // The object register should already be null, so there
  2730. // is no need to clear it if the cast is unsuccessful
  2731. asASSERT( m_regs.objectRegister == 0 );
  2732. }
  2733. }
  2734. l_sp += AS_PTR_SIZE;
  2735. }
  2736. l_bc += 2;
  2737. break;
  2738. case asBC_i64TOi:
  2739. *(l_fp - asBC_SWORDARG0(l_bc)) = int(*(asINT64*)(l_fp - asBC_SWORDARG1(l_bc)));
  2740. l_bc += 2;
  2741. break;
  2742. case asBC_uTOi64:
  2743. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = asINT64(*(asUINT*)(l_fp - asBC_SWORDARG1(l_bc)));
  2744. l_bc += 2;
  2745. break;
  2746. case asBC_iTOi64:
  2747. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = asINT64(*(int*)(l_fp - asBC_SWORDARG1(l_bc)));
  2748. l_bc += 2;
  2749. break;
  2750. case asBC_fTOi64:
  2751. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = asINT64(*(float*)(l_fp - asBC_SWORDARG1(l_bc)));
  2752. l_bc += 2;
  2753. break;
  2754. case asBC_dTOi64:
  2755. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = asINT64(*(double*)(l_fp - asBC_SWORDARG0(l_bc)));
  2756. l_bc++;
  2757. break;
  2758. case asBC_fTOu64:
  2759. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = asQWORD(asINT64(*(float*)(l_fp - asBC_SWORDARG1(l_bc))));
  2760. l_bc += 2;
  2761. break;
  2762. case asBC_dTOu64:
  2763. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = asQWORD(asINT64(*(double*)(l_fp - asBC_SWORDARG0(l_bc))));
  2764. l_bc++;
  2765. break;
  2766. case asBC_i64TOf:
  2767. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = float(*(asINT64*)(l_fp - asBC_SWORDARG1(l_bc)));
  2768. l_bc += 2;
  2769. break;
  2770. case asBC_u64TOf:
  2771. #if _MSC_VER <= 1200 // MSVC6
  2772. {
  2773. // MSVC6 doesn't permit UINT64 to double
  2774. asINT64 v = *(asINT64*)(l_fp - asBC_SWORDARG1(l_bc));
  2775. if( v < 0 )
  2776. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = 18446744073709551615.0f+float(v);
  2777. else
  2778. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = float(v);
  2779. }
  2780. #else
  2781. *(float*)(l_fp - asBC_SWORDARG0(l_bc)) = float(*(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)));
  2782. #endif
  2783. l_bc += 2;
  2784. break;
  2785. case asBC_i64TOd:
  2786. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = double(*(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)));
  2787. l_bc++;
  2788. break;
  2789. case asBC_u64TOd:
  2790. #if _MSC_VER <= 1200 // MSVC6
  2791. {
  2792. // MSVC6 doesn't permit UINT64 to double
  2793. asINT64 v = *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc));
  2794. if( v < 0 )
  2795. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = 18446744073709551615.0+double(v);
  2796. else
  2797. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = double(v);
  2798. }
  2799. #else
  2800. *(double*)(l_fp - asBC_SWORDARG0(l_bc)) = double(*(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)));
  2801. #endif
  2802. l_bc++;
  2803. break;
  2804. case asBC_NEGi64:
  2805. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = -*(asINT64*)(l_fp - asBC_SWORDARG0(l_bc));
  2806. l_bc++;
  2807. break;
  2808. case asBC_INCi64:
  2809. ++(**(asQWORD**)&m_regs.valueRegister);
  2810. l_bc++;
  2811. break;
  2812. case asBC_DECi64:
  2813. --(**(asQWORD**)&m_regs.valueRegister);
  2814. l_bc++;
  2815. break;
  2816. case asBC_BNOT64:
  2817. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = ~*(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2818. l_bc++;
  2819. break;
  2820. case asBC_ADDi64:
  2821. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) + *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  2822. l_bc += 2;
  2823. break;
  2824. case asBC_SUBi64:
  2825. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) - *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  2826. l_bc += 2;
  2827. break;
  2828. case asBC_MULi64:
  2829. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) * *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  2830. l_bc += 2;
  2831. break;
  2832. case asBC_DIVi64:
  2833. {
  2834. asINT64 divider = *(asINT64*)(l_fp - asBC_SWORDARG2(l_bc));
  2835. if( divider == 0 )
  2836. {
  2837. // Need to move the values back to the context
  2838. m_regs.programPointer = l_bc;
  2839. m_regs.stackPointer = l_sp;
  2840. m_regs.stackFramePointer = l_fp;
  2841. // Raise exception
  2842. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2843. return;
  2844. }
  2845. else if( divider == -1 )
  2846. {
  2847. // Need to check if the value that is divided is 1<<63
  2848. // as dividing it with -1 will cause an overflow exception
  2849. if( *(asINT64*)(l_fp - asBC_SWORDARG1(l_bc)) == (asINT64(1)<<63) )
  2850. {
  2851. // Need to move the values back to the context
  2852. m_regs.programPointer = l_bc;
  2853. m_regs.stackPointer = l_sp;
  2854. m_regs.stackFramePointer = l_fp;
  2855. // Raise exception
  2856. SetInternalException(TXT_DIVIDE_OVERFLOW);
  2857. return;
  2858. }
  2859. }
  2860. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asINT64*)(l_fp - asBC_SWORDARG1(l_bc)) / divider;
  2861. }
  2862. l_bc += 2;
  2863. break;
  2864. case asBC_MODi64:
  2865. {
  2866. asINT64 divider = *(asINT64*)(l_fp - asBC_SWORDARG2(l_bc));
  2867. if( divider == 0 )
  2868. {
  2869. // Need to move the values back to the context
  2870. m_regs.programPointer = l_bc;
  2871. m_regs.stackPointer = l_sp;
  2872. m_regs.stackFramePointer = l_fp;
  2873. // Raise exception
  2874. SetInternalException(TXT_DIVIDE_BY_ZERO);
  2875. return;
  2876. }
  2877. else if( divider == -1 )
  2878. {
  2879. // Need to check if the value that is divided is 1<<63
  2880. // as dividing it with -1 will cause an overflow exception
  2881. if( *(asINT64*)(l_fp - asBC_SWORDARG1(l_bc)) == (asINT64(1)<<63) )
  2882. {
  2883. // Need to move the values back to the context
  2884. m_regs.programPointer = l_bc;
  2885. m_regs.stackPointer = l_sp;
  2886. m_regs.stackFramePointer = l_fp;
  2887. // Raise exception
  2888. SetInternalException(TXT_DIVIDE_OVERFLOW);
  2889. return;
  2890. }
  2891. }
  2892. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asINT64*)(l_fp - asBC_SWORDARG1(l_bc)) % divider;
  2893. }
  2894. l_bc += 2;
  2895. break;
  2896. case asBC_BAND64:
  2897. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) & *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  2898. l_bc += 2;
  2899. break;
  2900. case asBC_BOR64:
  2901. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) | *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  2902. l_bc += 2;
  2903. break;
  2904. case asBC_BXOR64:
  2905. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) ^ *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  2906. l_bc += 2;
  2907. break;
  2908. case asBC_BSLL64:
  2909. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) << *(l_fp - asBC_SWORDARG2(l_bc));
  2910. l_bc += 2;
  2911. break;
  2912. case asBC_BSRL64:
  2913. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) >> *(l_fp - asBC_SWORDARG2(l_bc));
  2914. l_bc += 2;
  2915. break;
  2916. case asBC_BSRA64:
  2917. *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asINT64*)(l_fp - asBC_SWORDARG1(l_bc)) >> *(l_fp - asBC_SWORDARG2(l_bc));
  2918. l_bc += 2;
  2919. break;
  2920. case asBC_CMPi64:
  2921. {
  2922. asINT64 i1 = *(asINT64*)(l_fp - asBC_SWORDARG0(l_bc));
  2923. asINT64 i2 = *(asINT64*)(l_fp - asBC_SWORDARG1(l_bc));
  2924. if( i1 == i2 ) *(int*)&m_regs.valueRegister = 0;
  2925. else if( i1 < i2 ) *(int*)&m_regs.valueRegister = -1;
  2926. else *(int*)&m_regs.valueRegister = 1;
  2927. l_bc += 2;
  2928. }
  2929. break;
  2930. case asBC_CMPu64:
  2931. {
  2932. asQWORD d1 = *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  2933. asQWORD d2 = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc));
  2934. if( d1 == d2 ) *(int*)&m_regs.valueRegister = 0;
  2935. else if( d1 < d2 ) *(int*)&m_regs.valueRegister = -1;
  2936. else *(int*)&m_regs.valueRegister = 1;
  2937. l_bc += 2;
  2938. }
  2939. break;
  2940. case asBC_ChkNullS:
  2941. {
  2942. // Verify if the pointer on the stack is null
  2943. // This is used for example when validating handles passed as function arguments
  2944. asPWORD a = *(asPWORD*)(l_sp + asBC_WORDARG0(l_bc));
  2945. if( a == 0 )
  2946. {
  2947. m_regs.programPointer = l_bc;
  2948. m_regs.stackPointer = l_sp;
  2949. m_regs.stackFramePointer = l_fp;
  2950. SetInternalException(TXT_NULL_POINTER_ACCESS);
  2951. return;
  2952. }
  2953. }
  2954. l_bc++;
  2955. break;
  2956. case asBC_ClrHi:
  2957. #if AS_SIZEOF_BOOL == 1
  2958. {
  2959. // Clear the upper bytes, so that trash data don't interfere with boolean operations
  2960. // We need to use volatile here to tell the compiler it cannot
  2961. // change the order of read and write operations on the pointer.
  2962. volatile asBYTE *ptr = (asBYTE*)&m_regs.valueRegister;
  2963. ptr[1] = 0; // The boolean value is stored in the lower byte, so we clear the rest
  2964. ptr[2] = 0;
  2965. ptr[3] = 0;
  2966. }
  2967. #else
  2968. // We don't have anything to do here
  2969. #endif
  2970. l_bc++;
  2971. break;
  2972. case asBC_JitEntry:
  2973. {
  2974. if( m_currentFunction->scriptData->jitFunction )
  2975. {
  2976. asPWORD jitArg = asBC_PTRARG(l_bc);
  2977. if( jitArg )
  2978. {
  2979. // Resume JIT operation
  2980. m_regs.programPointer = l_bc;
  2981. m_regs.stackPointer = l_sp;
  2982. m_regs.stackFramePointer = l_fp;
  2983. (m_currentFunction->scriptData->jitFunction)(&m_regs, jitArg);
  2984. l_bc = m_regs.programPointer;
  2985. l_sp = m_regs.stackPointer;
  2986. l_fp = m_regs.stackFramePointer;
  2987. // If status isn't active anymore then we must stop
  2988. if( m_status != asEXECUTION_ACTIVE )
  2989. return;
  2990. break;
  2991. }
  2992. }
  2993. // Not a JIT resume point, treat as nop
  2994. l_bc += 1+AS_PTR_SIZE;
  2995. }
  2996. break;
  2997. case asBC_CallPtr:
  2998. {
  2999. // Get the function pointer from the local variable
  3000. asCScriptFunction *func = *(asCScriptFunction**)(l_fp - asBC_SWORDARG0(l_bc));
  3001. // Need to move the values back to the context
  3002. m_regs.programPointer = l_bc;
  3003. m_regs.stackPointer = l_sp;
  3004. m_regs.stackFramePointer = l_fp;
  3005. if( func == 0 )
  3006. {
  3007. // Need to update the program pointer anyway for the exception handler
  3008. m_regs.programPointer++;
  3009. // Tell the exception handler to clean up the arguments to this method
  3010. m_needToCleanupArgs = true;
  3011. // TODO: funcdef: Should we have a different exception string?
  3012. SetInternalException(TXT_UNBOUND_FUNCTION);
  3013. return;
  3014. }
  3015. else
  3016. {
  3017. if( func->funcType == asFUNC_SCRIPT )
  3018. {
  3019. m_regs.programPointer++;
  3020. CallScriptFunction(func);
  3021. }
  3022. else if( func->funcType == asFUNC_DELEGATE )
  3023. {
  3024. // Push the object pointer on the stack. There is always a reserved space for this so
  3025. // we don't don't need to worry about overflowing the allocated memory buffer
  3026. asASSERT( m_regs.stackPointer - AS_PTR_SIZE >= m_stackBlocks[m_stackIndex] );
  3027. m_regs.stackPointer -= AS_PTR_SIZE;
  3028. *(asPWORD*)m_regs.stackPointer = asPWORD(func->objForDelegate);
  3029. // Call the delegated method
  3030. if( func->funcForDelegate->funcType == asFUNC_SYSTEM )
  3031. {
  3032. m_regs.stackPointer += CallSystemFunction(func->funcForDelegate->id, this, 0);
  3033. // Update program position after the call so the line number
  3034. // is correct in case the system function queries it
  3035. m_regs.programPointer++;
  3036. }
  3037. else
  3038. {
  3039. m_regs.programPointer++;
  3040. // TODO: run-time optimize: The true method could be figured out when creating the delegate
  3041. CallInterfaceMethod(func->funcForDelegate);
  3042. }
  3043. }
  3044. else
  3045. {
  3046. asASSERT( func->funcType == asFUNC_SYSTEM );
  3047. m_regs.stackPointer += CallSystemFunction(func->id, this, 0);
  3048. // Update program position after the call so the line number
  3049. // is correct in case the system function queries it
  3050. m_regs.programPointer++;
  3051. }
  3052. }
  3053. // Extract the values from the context again
  3054. l_bc = m_regs.programPointer;
  3055. l_sp = m_regs.stackPointer;
  3056. l_fp = m_regs.stackFramePointer;
  3057. // If status isn't active anymore then we must stop
  3058. if( m_status != asEXECUTION_ACTIVE )
  3059. return;
  3060. }
  3061. break;
  3062. case asBC_FuncPtr:
  3063. // Push the function pointer on the stack. The pointer is in the argument
  3064. l_sp -= AS_PTR_SIZE;
  3065. *(asPWORD*)l_sp = asBC_PTRARG(l_bc);
  3066. l_bc += 1+AS_PTR_SIZE;
  3067. break;
  3068. case asBC_LoadThisR:
  3069. {
  3070. // PshVPtr 0
  3071. asPWORD tmp = *(asPWORD*)l_fp;
  3072. // Make sure the pointer is not null
  3073. if( tmp == 0 )
  3074. {
  3075. // Need to move the values back to the context
  3076. m_regs.programPointer = l_bc;
  3077. m_regs.stackPointer = l_sp;
  3078. m_regs.stackFramePointer = l_fp;
  3079. // Raise exception
  3080. SetInternalException(TXT_NULL_POINTER_ACCESS);
  3081. return;
  3082. }
  3083. // ADDSi
  3084. tmp = tmp + asBC_SWORDARG0(l_bc);
  3085. // PopRPtr
  3086. *(asPWORD*)&m_regs.valueRegister = tmp;
  3087. l_bc += 2;
  3088. }
  3089. break;
  3090. // Push the qword value of a variable on the stack
  3091. case asBC_PshV8:
  3092. l_sp -= 2;
  3093. *(asQWORD*)l_sp = *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  3094. l_bc++;
  3095. break;
  3096. case asBC_DIVu:
  3097. {
  3098. asUINT divider = *(asUINT*)(l_fp - asBC_SWORDARG2(l_bc));
  3099. if( divider == 0 )
  3100. {
  3101. // Need to move the values back to the context
  3102. m_regs.programPointer = l_bc;
  3103. m_regs.stackPointer = l_sp;
  3104. m_regs.stackFramePointer = l_fp;
  3105. // Raise exception
  3106. SetInternalException(TXT_DIVIDE_BY_ZERO);
  3107. return;
  3108. }
  3109. *(asUINT*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asUINT*)(l_fp - asBC_SWORDARG1(l_bc)) / divider;
  3110. }
  3111. l_bc += 2;
  3112. break;
  3113. case asBC_MODu:
  3114. {
  3115. asUINT divider = *(asUINT*)(l_fp - asBC_SWORDARG2(l_bc));
  3116. if( divider == 0 )
  3117. {
  3118. // Need to move the values back to the context
  3119. m_regs.programPointer = l_bc;
  3120. m_regs.stackPointer = l_sp;
  3121. m_regs.stackFramePointer = l_fp;
  3122. // Raise exception
  3123. SetInternalException(TXT_DIVIDE_BY_ZERO);
  3124. return;
  3125. }
  3126. *(asUINT*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asUINT*)(l_fp - asBC_SWORDARG1(l_bc)) % divider;
  3127. }
  3128. l_bc += 2;
  3129. break;
  3130. case asBC_DIVu64:
  3131. {
  3132. asQWORD divider = *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  3133. if( divider == 0 )
  3134. {
  3135. // Need to move the values back to the context
  3136. m_regs.programPointer = l_bc;
  3137. m_regs.stackPointer = l_sp;
  3138. m_regs.stackFramePointer = l_fp;
  3139. // Raise exception
  3140. SetInternalException(TXT_DIVIDE_BY_ZERO);
  3141. return;
  3142. }
  3143. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) / divider;
  3144. }
  3145. l_bc += 2;
  3146. break;
  3147. case asBC_MODu64:
  3148. {
  3149. asQWORD divider = *(asQWORD*)(l_fp - asBC_SWORDARG2(l_bc));
  3150. if( divider == 0 )
  3151. {
  3152. // Need to move the values back to the context
  3153. m_regs.programPointer = l_bc;
  3154. m_regs.stackPointer = l_sp;
  3155. m_regs.stackFramePointer = l_fp;
  3156. // Raise exception
  3157. SetInternalException(TXT_DIVIDE_BY_ZERO);
  3158. return;
  3159. }
  3160. *(asQWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = *(asQWORD*)(l_fp - asBC_SWORDARG1(l_bc)) % divider;
  3161. }
  3162. l_bc += 2;
  3163. break;
  3164. case asBC_LoadRObjR:
  3165. {
  3166. // PshVPtr x
  3167. asPWORD tmp = *(asPWORD*)(l_fp - asBC_SWORDARG0(l_bc));
  3168. // Make sure the pointer is not null
  3169. if( tmp == 0 )
  3170. {
  3171. // Need to move the values back to the context
  3172. m_regs.programPointer = l_bc;
  3173. m_regs.stackPointer = l_sp;
  3174. m_regs.stackFramePointer = l_fp;
  3175. // Raise exception
  3176. SetInternalException(TXT_NULL_POINTER_ACCESS);
  3177. return;
  3178. }
  3179. // ADDSi y
  3180. tmp = tmp + asBC_SWORDARG1(l_bc);
  3181. // PopRPtr
  3182. *(asPWORD*)&m_regs.valueRegister = tmp;
  3183. l_bc += 3;
  3184. }
  3185. break;
  3186. case asBC_LoadVObjR:
  3187. {
  3188. // PSF x
  3189. asPWORD tmp = (asPWORD)(l_fp - asBC_SWORDARG0(l_bc));
  3190. // ADDSi y
  3191. tmp = tmp + asBC_SWORDARG1(l_bc);
  3192. // PopRPtr
  3193. *(asPWORD*)&m_regs.valueRegister = tmp;
  3194. l_bc += 3;
  3195. }
  3196. break;
  3197. case asBC_RefCpyV:
  3198. // Same as PSF v, REFCPY
  3199. {
  3200. asCObjectType *objType = (asCObjectType*)asBC_PTRARG(l_bc);
  3201. asSTypeBehaviour *beh = &objType->beh;
  3202. // Determine destination from argument
  3203. void **d = (void**)asPWORD(l_fp - asBC_SWORDARG0(l_bc));
  3204. // Read wanted pointer from the stack
  3205. void *s = (void*)*(asPWORD*)l_sp;
  3206. // Need to move the values back to the context as the called functions
  3207. // may use the debug interface to inspect the registers
  3208. m_regs.programPointer = l_bc;
  3209. m_regs.stackPointer = l_sp;
  3210. m_regs.stackFramePointer = l_fp;
  3211. if( !(objType->flags & asOBJ_NOCOUNT) )
  3212. {
  3213. // Release previous object held by destination pointer
  3214. if( *d != 0 )
  3215. m_engine->CallObjectMethod(*d, beh->release);
  3216. // Increase ref counter of wanted object
  3217. if( s != 0 )
  3218. m_engine->CallObjectMethod(s, beh->addref);
  3219. }
  3220. // Set the new object in the destination
  3221. *d = s;
  3222. }
  3223. l_bc += 1+AS_PTR_SIZE;
  3224. break;
  3225. case asBC_JLowZ:
  3226. if( *(asBYTE*)&m_regs.valueRegister == 0 )
  3227. l_bc += asBC_INTARG(l_bc) + 2;
  3228. else
  3229. l_bc += 2;
  3230. break;
  3231. case asBC_JLowNZ:
  3232. if( *(asBYTE*)&m_regs.valueRegister != 0 )
  3233. l_bc += asBC_INTARG(l_bc) + 2;
  3234. else
  3235. l_bc += 2;
  3236. break;
  3237. case asBC_AllocMem:
  3238. // Allocate a buffer and store the pointer in the local variable
  3239. {
  3240. // TODO: runtime optimize: As the list buffers are going to be short lived, it may be interesting
  3241. // to use a memory pool to avoid reallocating the memory all the time
  3242. asUINT size = asBC_DWORDARG(l_bc);
  3243. asBYTE **var = (asBYTE**)(l_fp - asBC_SWORDARG0(l_bc));
  3244. *var = asNEWARRAY(asBYTE, size);
  3245. // Clear the buffer for the pointers that will be placed in it
  3246. memset(*var, 0, size);
  3247. }
  3248. l_bc += 2;
  3249. break;
  3250. case asBC_SetListSize:
  3251. {
  3252. // Set the size element in the buffer
  3253. asBYTE *var = *(asBYTE**)(l_fp - asBC_SWORDARG0(l_bc));
  3254. asUINT off = asBC_DWORDARG(l_bc);
  3255. asUINT size = asBC_DWORDARG(l_bc+1);
  3256. asASSERT( var );
  3257. *(asUINT*)(var+off) = size;
  3258. }
  3259. l_bc += 3;
  3260. break;
  3261. case asBC_PshListElmnt:
  3262. {
  3263. // Push the pointer to the list element on the stack
  3264. // In essence it does the same as PSF, RDSPtr, ADDSi
  3265. asBYTE *var = *(asBYTE**)(l_fp - asBC_SWORDARG0(l_bc));
  3266. asUINT off = asBC_DWORDARG(l_bc);
  3267. asASSERT( var );
  3268. l_sp -= AS_PTR_SIZE;
  3269. *(asPWORD*)l_sp = asPWORD(var+off);
  3270. }
  3271. l_bc += 2;
  3272. break;
  3273. case asBC_SetListType:
  3274. {
  3275. // Set the type id in the buffer
  3276. asBYTE *var = *(asBYTE**)(l_fp - asBC_SWORDARG0(l_bc));
  3277. asUINT off = asBC_DWORDARG(l_bc);
  3278. asUINT type = asBC_DWORDARG(l_bc+1);
  3279. asASSERT( var );
  3280. *(asUINT*)(var+off) = type;
  3281. }
  3282. l_bc += 3;
  3283. break;
  3284. // Don't let the optimizer optimize for size,
  3285. // since it requires extra conditions and jumps
  3286. case 193: l_bc = (asDWORD*)193; break;
  3287. case 194: l_bc = (asDWORD*)194; break;
  3288. case 195: l_bc = (asDWORD*)195; break;
  3289. case 196: l_bc = (asDWORD*)196; break;
  3290. case 197: l_bc = (asDWORD*)197; break;
  3291. case 198: l_bc = (asDWORD*)198; break;
  3292. case 199: l_bc = (asDWORD*)199; break;
  3293. case 200: l_bc = (asDWORD*)200; break;
  3294. case 201: l_bc = (asDWORD*)201; break;
  3295. case 202: l_bc = (asDWORD*)202; break;
  3296. case 203: l_bc = (asDWORD*)203; break;
  3297. case 204: l_bc = (asDWORD*)204; break;
  3298. case 205: l_bc = (asDWORD*)205; break;
  3299. case 206: l_bc = (asDWORD*)206; break;
  3300. case 207: l_bc = (asDWORD*)207; break;
  3301. case 208: l_bc = (asDWORD*)208; break;
  3302. case 209: l_bc = (asDWORD*)209; break;
  3303. case 210: l_bc = (asDWORD*)210; break;
  3304. case 211: l_bc = (asDWORD*)211; break;
  3305. case 212: l_bc = (asDWORD*)212; break;
  3306. case 213: l_bc = (asDWORD*)213; break;
  3307. case 214: l_bc = (asDWORD*)214; break;
  3308. case 215: l_bc = (asDWORD*)215; break;
  3309. case 216: l_bc = (asDWORD*)216; break;
  3310. case 217: l_bc = (asDWORD*)217; break;
  3311. case 218: l_bc = (asDWORD*)218; break;
  3312. case 219: l_bc = (asDWORD*)219; break;
  3313. case 220: l_bc = (asDWORD*)220; break;
  3314. case 221: l_bc = (asDWORD*)221; break;
  3315. case 222: l_bc = (asDWORD*)222; break;
  3316. case 223: l_bc = (asDWORD*)223; break;
  3317. case 224: l_bc = (asDWORD*)224; break;
  3318. case 225: l_bc = (asDWORD*)225; break;
  3319. case 226: l_bc = (asDWORD*)226; break;
  3320. case 227: l_bc = (asDWORD*)227; break;
  3321. case 228: l_bc = (asDWORD*)228; break;
  3322. case 229: l_bc = (asDWORD*)229; break;
  3323. case 230: l_bc = (asDWORD*)230; break;
  3324. case 231: l_bc = (asDWORD*)231; break;
  3325. case 232: l_bc = (asDWORD*)232; break;
  3326. case 233: l_bc = (asDWORD*)233; break;
  3327. case 234: l_bc = (asDWORD*)234; break;
  3328. case 235: l_bc = (asDWORD*)235; break;
  3329. case 236: l_bc = (asDWORD*)236; break;
  3330. case 237: l_bc = (asDWORD*)237; break;
  3331. case 238: l_bc = (asDWORD*)238; break;
  3332. case 239: l_bc = (asDWORD*)239; break;
  3333. case 240: l_bc = (asDWORD*)240; break;
  3334. case 241: l_bc = (asDWORD*)241; break;
  3335. case 242: l_bc = (asDWORD*)242; break;
  3336. case 243: l_bc = (asDWORD*)243; break;
  3337. case 244: l_bc = (asDWORD*)244; break;
  3338. case 245: l_bc = (asDWORD*)245; break;
  3339. case 246: l_bc = (asDWORD*)246; break;
  3340. case 247: l_bc = (asDWORD*)247; break;
  3341. case 248: l_bc = (asDWORD*)248; break;
  3342. case 249: l_bc = (asDWORD*)249; break;
  3343. case 250: l_bc = (asDWORD*)250; break;
  3344. case 251: l_bc = (asDWORD*)251; break;
  3345. case 252: l_bc = (asDWORD*)252; break;
  3346. case 253: l_bc = (asDWORD*)253; break;
  3347. case 254: l_bc = (asDWORD*)254; break;
  3348. case 255: l_bc = (asDWORD*)255; break;
  3349. #ifdef AS_DEBUG
  3350. default:
  3351. asASSERT(false);
  3352. SetInternalException(TXT_UNRECOGNIZED_BYTE_CODE);
  3353. #endif
  3354. #if defined(_MSC_VER) && !defined(AS_DEBUG)
  3355. default:
  3356. // This Microsoft specific code allows the
  3357. // compiler to optimize the switch case as
  3358. // it will know that the code will never
  3359. // reach this point
  3360. __assume(0);
  3361. #endif
  3362. }
  3363. #ifdef AS_DEBUG
  3364. asDWORD instr = *(asBYTE*)old;
  3365. if( instr != asBC_JMP && instr != asBC_JMPP && (instr < asBC_JZ || instr > asBC_JNP) && instr != asBC_JLowZ && instr != asBC_JLowNZ &&
  3366. instr != asBC_CALL && instr != asBC_CALLBND && instr != asBC_CALLINTF && instr != asBC_RET && instr != asBC_ALLOC && instr != asBC_CallPtr &&
  3367. instr != asBC_JitEntry )
  3368. {
  3369. asASSERT( (l_bc - old) == asBCTypeSize[asBCInfo[instr].type] );
  3370. }
  3371. #endif
  3372. }
  3373. }
  3374. int asCContext::SetException(const char *descr)
  3375. {
  3376. // Only allow this if we're executing a CALL byte code
  3377. if( m_callingSystemFunction == 0 ) return asERROR;
  3378. SetInternalException(descr);
  3379. return 0;
  3380. }
  3381. void asCContext::SetInternalException(const char *descr)
  3382. {
  3383. if( m_inExceptionHandler )
  3384. {
  3385. asASSERT(false); // Shouldn't happen
  3386. return; // but if it does, at least this will not crash the application
  3387. }
  3388. m_status = asEXECUTION_EXCEPTION;
  3389. m_regs.doProcessSuspend = true;
  3390. m_exceptionString = descr;
  3391. m_exceptionFunction = m_currentFunction->id;
  3392. if( m_currentFunction->scriptData )
  3393. {
  3394. m_exceptionLine = m_currentFunction->GetLineNumber(int(m_regs.programPointer - m_currentFunction->scriptData->byteCode.AddressOf()), &m_exceptionSectionIdx);
  3395. m_exceptionColumn = m_exceptionLine >> 20;
  3396. m_exceptionLine &= 0xFFFFF;
  3397. }
  3398. else
  3399. {
  3400. m_exceptionSectionIdx = 0;
  3401. m_exceptionLine = 0;
  3402. m_exceptionColumn = 0;
  3403. }
  3404. if( m_exceptionCallback )
  3405. CallExceptionCallback();
  3406. }
  3407. void asCContext::CleanReturnObject()
  3408. {
  3409. if( m_initialFunction && m_initialFunction->DoesReturnOnStack() && m_status == asEXECUTION_FINISHED )
  3410. {
  3411. // If function returns on stack we need to call the destructor on the returned object
  3412. if( m_initialFunction->returnType.GetObjectType()->beh.destruct )
  3413. m_engine->CallObjectMethod(GetReturnObject(), m_initialFunction->returnType.GetObjectType()->beh.destruct);
  3414. return;
  3415. }
  3416. if( m_regs.objectRegister == 0 ) return;
  3417. asASSERT( m_regs.objectType != 0 );
  3418. if( m_regs.objectType )
  3419. {
  3420. // Call the destructor on the object
  3421. asSTypeBehaviour *beh = &((asCObjectType*)m_regs.objectType)->beh;
  3422. if( m_regs.objectType->GetFlags() & asOBJ_REF )
  3423. {
  3424. asASSERT( beh->release || (m_regs.objectType->GetFlags() & asOBJ_NOCOUNT) );
  3425. if( beh->release )
  3426. m_engine->CallObjectMethod(m_regs.objectRegister, beh->release);
  3427. m_regs.objectRegister = 0;
  3428. }
  3429. else
  3430. {
  3431. if( beh->destruct )
  3432. m_engine->CallObjectMethod(m_regs.objectRegister, beh->destruct);
  3433. // Free the memory
  3434. m_engine->CallFree(m_regs.objectRegister);
  3435. m_regs.objectRegister = 0;
  3436. }
  3437. }
  3438. }
  3439. void asCContext::CleanStack()
  3440. {
  3441. m_inExceptionHandler = true;
  3442. // Run the clean up code for each of the functions called
  3443. CleanStackFrame();
  3444. // Set the status to exception so that the stack unwind is done correctly.
  3445. // This shouldn't be done for the current function, which is why we only
  3446. // do this after the first CleanStackFrame() is done.
  3447. m_status = asEXECUTION_EXCEPTION;
  3448. while( m_callStack.GetLength() > 0 )
  3449. {
  3450. // Only clean up until the top most marker for a nested call
  3451. asPWORD *s = m_callStack.AddressOf() + m_callStack.GetLength() - CALLSTACK_FRAME_SIZE;
  3452. if( s[0] == 0 )
  3453. break;
  3454. PopCallState();
  3455. CleanStackFrame();
  3456. }
  3457. m_inExceptionHandler = false;
  3458. }
  3459. // Interface
  3460. bool asCContext::IsVarInScope(asUINT varIndex, asUINT stackLevel)
  3461. {
  3462. // Don't return anything if there is no bytecode, e.g. before calling Execute()
  3463. if( m_regs.programPointer == 0 ) return false;
  3464. if( stackLevel >= GetCallstackSize() ) return false;
  3465. asCScriptFunction *func;
  3466. asUINT pos;
  3467. if( stackLevel == 0 )
  3468. {
  3469. func = m_currentFunction;
  3470. if( func->scriptData == 0 ) return false;
  3471. pos = asUINT(m_regs.programPointer - func->scriptData->byteCode.AddressOf());
  3472. }
  3473. else
  3474. {
  3475. asPWORD *s = m_callStack.AddressOf() + (GetCallstackSize()-stackLevel-1)*CALLSTACK_FRAME_SIZE;
  3476. func = (asCScriptFunction*)s[1];
  3477. if( func->scriptData == 0 ) return false;
  3478. pos = asUINT((asDWORD*)s[2] - func->scriptData->byteCode.AddressOf());
  3479. }
  3480. // First determine if the program position is after the variable declaration
  3481. if( func->scriptData->variables.GetLength() <= varIndex ) return false;
  3482. if( func->scriptData->variables[varIndex]->declaredAtProgramPos > pos ) return false;
  3483. asUINT declaredAt = func->scriptData->variables[varIndex]->declaredAtProgramPos;
  3484. // If the program position is after the variable declaration it is necessary
  3485. // determine if the program position is still inside the statement block where
  3486. // the variable was delcared.
  3487. for( int n = 0; n < (int)func->scriptData->objVariableInfo.GetLength(); n++ )
  3488. {
  3489. if( func->scriptData->objVariableInfo[n].programPos >= declaredAt )
  3490. {
  3491. // If the current block ends between the declaredAt and current
  3492. // program position, then we know the variable is no longer visible
  3493. int level = 0;
  3494. for( ; n < (int)func->scriptData->objVariableInfo.GetLength(); n++ )
  3495. {
  3496. if( func->scriptData->objVariableInfo[n].programPos > pos )
  3497. break;
  3498. if( func->scriptData->objVariableInfo[n].option == asBLOCK_BEGIN ) level++;
  3499. if( func->scriptData->objVariableInfo[n].option == asBLOCK_END && --level < 0 )
  3500. return false;
  3501. }
  3502. break;
  3503. }
  3504. }
  3505. // Variable is visible
  3506. return true;
  3507. }
  3508. // Internal
  3509. void asCContext::DetermineLiveObjects(asCArray<int> &liveObjects, asUINT stackLevel)
  3510. {
  3511. asASSERT( stackLevel < GetCallstackSize() );
  3512. asCScriptFunction *func;
  3513. asUINT pos;
  3514. if( stackLevel == 0 )
  3515. {
  3516. func = m_currentFunction;
  3517. if( func->scriptData == 0 )
  3518. return;
  3519. pos = asUINT(m_regs.programPointer - func->scriptData->byteCode.AddressOf());
  3520. if( m_status == asEXECUTION_EXCEPTION )
  3521. {
  3522. // Don't consider the last instruction as executed, as it failed with an exception
  3523. // It's not actually necessary to decrease the exact size of the instruction. Just
  3524. // before the current position is enough to disconsider it.
  3525. pos--;
  3526. }
  3527. }
  3528. else
  3529. {
  3530. asPWORD *s = m_callStack.AddressOf() + (GetCallstackSize()-stackLevel-1)*CALLSTACK_FRAME_SIZE;
  3531. func = (asCScriptFunction*)s[1];
  3532. if( func->scriptData == 0 )
  3533. return;
  3534. pos = asUINT((asDWORD*)s[2] - func->scriptData->byteCode.AddressOf());
  3535. // Don't consider the last instruction as executed, as the function that was called by it
  3536. // is still being executed. If we consider it as executed already, then a value object
  3537. // returned by value would be considered alive, which it is not.
  3538. pos--;
  3539. }
  3540. // Determine which object variables that are really live ones
  3541. liveObjects.SetLength(func->scriptData->objVariablePos.GetLength());
  3542. memset(liveObjects.AddressOf(), 0, sizeof(int)*liveObjects.GetLength());
  3543. for( int n = 0; n < (int)func->scriptData->objVariableInfo.GetLength(); n++ )
  3544. {
  3545. // Find the first variable info with a larger position than the current
  3546. // As the variable info are always placed on the instruction right after the
  3547. // one that initialized or freed the object, the current position needs to be
  3548. // considered as valid.
  3549. if( func->scriptData->objVariableInfo[n].programPos > pos )
  3550. {
  3551. // We've determined how far the execution ran, now determine which variables are alive
  3552. for( --n; n >= 0; n-- )
  3553. {
  3554. switch( func->scriptData->objVariableInfo[n].option )
  3555. {
  3556. case asOBJ_UNINIT: // Object was destroyed
  3557. {
  3558. // TODO: optimize: This should have been done by the compiler already
  3559. // Which variable is this?
  3560. asUINT var = 0;
  3561. for( asUINT v = 0; v < func->scriptData->objVariablePos.GetLength(); v++ )
  3562. if( func->scriptData->objVariablePos[v] == func->scriptData->objVariableInfo[n].variableOffset )
  3563. {
  3564. var = v;
  3565. break;
  3566. }
  3567. liveObjects[var] -= 1;
  3568. }
  3569. break;
  3570. case asOBJ_INIT: // Object was created
  3571. {
  3572. // Which variable is this?
  3573. asUINT var = 0;
  3574. for( asUINT v = 0; v < func->scriptData->objVariablePos.GetLength(); v++ )
  3575. if( func->scriptData->objVariablePos[v] == func->scriptData->objVariableInfo[n].variableOffset )
  3576. {
  3577. var = v;
  3578. break;
  3579. }
  3580. liveObjects[var] += 1;
  3581. }
  3582. break;
  3583. case asBLOCK_BEGIN: // Start block
  3584. // We should ignore start blocks, since it just means the
  3585. // program was within the block when the exception ocurred
  3586. break;
  3587. case asBLOCK_END: // End block
  3588. // We need to skip the entire block, as the objects created
  3589. // and destroyed inside this block are already out of scope
  3590. {
  3591. int nested = 1;
  3592. while( nested > 0 )
  3593. {
  3594. int option = func->scriptData->objVariableInfo[--n].option;
  3595. if( option == 3 )
  3596. nested++;
  3597. if( option == 2 )
  3598. nested--;
  3599. }
  3600. }
  3601. break;
  3602. }
  3603. }
  3604. // We're done with the investigation
  3605. break;
  3606. }
  3607. }
  3608. }
  3609. void asCContext::CleanArgsOnStack()
  3610. {
  3611. if( !m_needToCleanupArgs )
  3612. return;
  3613. asASSERT( m_currentFunction->scriptData );
  3614. // Find the instruction just before the current program pointer
  3615. asDWORD *instr = m_currentFunction->scriptData->byteCode.AddressOf();
  3616. asDWORD *prevInstr = 0;
  3617. while( instr < m_regs.programPointer )
  3618. {
  3619. prevInstr = instr;
  3620. instr += asBCTypeSize[asBCInfo[*(asBYTE*)(instr)].type];
  3621. }
  3622. // Determine what function was being called
  3623. asCScriptFunction *func = 0;
  3624. asBYTE bc = *(asBYTE*)prevInstr;
  3625. if( bc == asBC_CALL || bc == asBC_CALLSYS || bc == asBC_CALLINTF )
  3626. {
  3627. int funcId = asBC_INTARG(prevInstr);
  3628. func = m_engine->scriptFunctions[funcId];
  3629. }
  3630. else if( bc == asBC_CALLBND )
  3631. {
  3632. int funcId = asBC_INTARG(prevInstr);
  3633. func = m_engine->importedFunctions[funcId & ~FUNC_IMPORTED]->importedFunctionSignature;
  3634. }
  3635. else if( bc == asBC_CallPtr )
  3636. {
  3637. asUINT v;
  3638. int var = asBC_SWORDARG0(prevInstr);
  3639. // Find the funcdef from the local variable
  3640. for( v = 0; v < m_currentFunction->scriptData->objVariablePos.GetLength(); v++ )
  3641. if( m_currentFunction->scriptData->objVariablePos[v] == var )
  3642. {
  3643. func = m_currentFunction->scriptData->funcVariableTypes[v];
  3644. break;
  3645. }
  3646. if( func == 0 )
  3647. {
  3648. // Look in parameters
  3649. int paramPos = 0;
  3650. if( m_currentFunction->objectType )
  3651. paramPos -= AS_PTR_SIZE;
  3652. if( m_currentFunction->DoesReturnOnStack() )
  3653. paramPos -= AS_PTR_SIZE;
  3654. for( v = 0; v < m_currentFunction->parameterTypes.GetLength(); v++ )
  3655. {
  3656. if( var == paramPos )
  3657. {
  3658. func = m_currentFunction->parameterTypes[v].GetFuncDef();
  3659. break;
  3660. }
  3661. paramPos -= m_currentFunction->parameterTypes[v].GetSizeOnStackDWords();
  3662. }
  3663. }
  3664. }
  3665. else
  3666. asASSERT( false );
  3667. asASSERT( func );
  3668. // Clean parameters
  3669. int offset = 0;
  3670. if( func->objectType )
  3671. offset += AS_PTR_SIZE;
  3672. if( func->DoesReturnOnStack() )
  3673. offset += AS_PTR_SIZE;
  3674. for( asUINT n = 0; n < func->parameterTypes.GetLength(); n++ )
  3675. {
  3676. if( func->parameterTypes[n].IsObject() && !func->parameterTypes[n].IsReference() )
  3677. {
  3678. if( *(asPWORD*)&m_regs.stackPointer[offset] )
  3679. {
  3680. // Call the object's destructor
  3681. asSTypeBehaviour *beh = func->parameterTypes[n].GetBehaviour();
  3682. if( func->parameterTypes[n].GetObjectType()->flags & asOBJ_REF )
  3683. {
  3684. asASSERT( (func->parameterTypes[n].GetObjectType()->flags & asOBJ_NOCOUNT) || beh->release );
  3685. if( beh->release )
  3686. m_engine->CallObjectMethod((void*)*(asPWORD*)&m_regs.stackPointer[offset], beh->release);
  3687. *(asPWORD*)&m_regs.stackPointer[offset] = 0;
  3688. }
  3689. else
  3690. {
  3691. if( beh->destruct )
  3692. m_engine->CallObjectMethod((void*)*(asPWORD*)&m_regs.stackPointer[offset], beh->destruct);
  3693. // Free the memory
  3694. m_engine->CallFree((void*)*(asPWORD*)&m_regs.stackPointer[offset]);
  3695. *(asPWORD*)&m_regs.stackPointer[offset] = 0;
  3696. }
  3697. }
  3698. }
  3699. offset += func->parameterTypes[n].GetSizeOnStackDWords();
  3700. }
  3701. m_needToCleanupArgs = false;
  3702. }
  3703. void asCContext::CleanStackFrame()
  3704. {
  3705. // Clean object variables on the stack
  3706. // If the stack memory is not allocated or the program pointer
  3707. // is not set, then there is nothing to clean up on the stack frame
  3708. if( !m_isStackMemoryNotAllocated && m_regs.programPointer )
  3709. {
  3710. // If the exception occurred while calling a function it is necessary
  3711. // to clean up the arguments that were put on the stack.
  3712. CleanArgsOnStack();
  3713. // Restore the stack pointer
  3714. asASSERT( m_currentFunction->scriptData );
  3715. m_regs.stackPointer += m_currentFunction->scriptData->variableSpace;
  3716. // Determine which object variables that are really live ones
  3717. asCArray<int> liveObjects;
  3718. DetermineLiveObjects(liveObjects, 0);
  3719. for( asUINT n = 0; n < m_currentFunction->scriptData->objVariablePos.GetLength(); n++ )
  3720. {
  3721. int pos = m_currentFunction->scriptData->objVariablePos[n];
  3722. if( n < m_currentFunction->scriptData->objVariablesOnHeap )
  3723. {
  3724. // Check if the pointer is initialized
  3725. if( *(asPWORD*)&m_regs.stackFramePointer[-pos] )
  3726. {
  3727. // Call the object's destructor
  3728. asSTypeBehaviour *beh = &m_currentFunction->scriptData->objVariableTypes[n]->beh;
  3729. if( m_currentFunction->scriptData->objVariableTypes[n]->flags & asOBJ_REF )
  3730. {
  3731. asASSERT( (m_currentFunction->scriptData->objVariableTypes[n]->flags & asOBJ_NOCOUNT) || beh->release );
  3732. if( beh->release )
  3733. m_engine->CallObjectMethod((void*)*(asPWORD*)&m_regs.stackFramePointer[-pos], beh->release);
  3734. *(asPWORD*)&m_regs.stackFramePointer[-pos] = 0;
  3735. }
  3736. else
  3737. {
  3738. if( beh->destruct )
  3739. m_engine->CallObjectMethod((void*)*(asPWORD*)&m_regs.stackFramePointer[-pos], beh->destruct);
  3740. else if( m_currentFunction->scriptData->objVariableTypes[n]->flags & asOBJ_LIST_PATTERN )
  3741. m_engine->DestroyList((asBYTE*)*(asPWORD*)&m_regs.stackFramePointer[-pos], m_currentFunction->scriptData->objVariableTypes[n]);
  3742. // Free the memory
  3743. m_engine->CallFree((void*)*(asPWORD*)&m_regs.stackFramePointer[-pos]);
  3744. *(asPWORD*)&m_regs.stackFramePointer[-pos] = 0;
  3745. }
  3746. }
  3747. }
  3748. else
  3749. {
  3750. asASSERT( m_currentFunction->scriptData->objVariableTypes[n]->GetFlags() & asOBJ_VALUE );
  3751. // Only destroy the object if it is truly alive
  3752. if( liveObjects[n] > 0 )
  3753. {
  3754. asSTypeBehaviour *beh = &m_currentFunction->scriptData->objVariableTypes[n]->beh;
  3755. if( beh->destruct )
  3756. m_engine->CallObjectMethod((void*)(asPWORD*)&m_regs.stackFramePointer[-pos], beh->destruct);
  3757. }
  3758. }
  3759. }
  3760. // If the object is a script declared object, then we must release it
  3761. // as the compiler adds a reference at the entry of the function. Make sure
  3762. // the function has actually been entered
  3763. if( m_currentFunction->objectType && m_regs.programPointer != m_currentFunction->scriptData->byteCode.AddressOf() )
  3764. {
  3765. // Methods returning a reference or constructors don't add a reference
  3766. if( !m_currentFunction->returnType.IsReference() && m_currentFunction->name != m_currentFunction->objectType->name )
  3767. {
  3768. asSTypeBehaviour *beh = &m_currentFunction->objectType->beh;
  3769. if( beh->release && *(asPWORD*)&m_regs.stackFramePointer[0] != 0 )
  3770. {
  3771. m_engine->CallObjectMethod((void*)*(asPWORD*)&m_regs.stackFramePointer[0], beh->release);
  3772. *(asPWORD*)&m_regs.stackFramePointer[0] = 0;
  3773. }
  3774. }
  3775. }
  3776. }
  3777. else
  3778. m_isStackMemoryNotAllocated = false;
  3779. // Functions that do not own the object and parameters shouldn't do any clean up
  3780. if( m_currentFunction->dontCleanUpOnException )
  3781. return;
  3782. // Clean object and parameters
  3783. int offset = 0;
  3784. if( m_currentFunction->objectType )
  3785. offset += AS_PTR_SIZE;
  3786. if( m_currentFunction->DoesReturnOnStack() )
  3787. offset += AS_PTR_SIZE;
  3788. for( asUINT n = 0; n < m_currentFunction->parameterTypes.GetLength(); n++ )
  3789. {
  3790. if( m_currentFunction->parameterTypes[n].IsObject() && !m_currentFunction->parameterTypes[n].IsReference() )
  3791. {
  3792. if( *(asPWORD*)&m_regs.stackFramePointer[offset] )
  3793. {
  3794. // Call the object's destructor
  3795. asSTypeBehaviour *beh = m_currentFunction->parameterTypes[n].GetBehaviour();
  3796. if( m_currentFunction->parameterTypes[n].GetObjectType()->flags & asOBJ_REF )
  3797. {
  3798. asASSERT( (m_currentFunction->parameterTypes[n].GetObjectType()->flags & asOBJ_NOCOUNT) || beh->release );
  3799. if( beh->release )
  3800. m_engine->CallObjectMethod((void*)*(asPWORD*)&m_regs.stackFramePointer[offset], beh->release);
  3801. *(asPWORD*)&m_regs.stackFramePointer[offset] = 0;
  3802. }
  3803. else
  3804. {
  3805. if( beh->destruct )
  3806. m_engine->CallObjectMethod((void*)*(asPWORD*)&m_regs.stackFramePointer[offset], beh->destruct);
  3807. // Free the memory
  3808. m_engine->CallFree((void*)*(asPWORD*)&m_regs.stackFramePointer[offset]);
  3809. *(asPWORD*)&m_regs.stackFramePointer[offset] = 0;
  3810. }
  3811. }
  3812. }
  3813. offset += m_currentFunction->parameterTypes[n].GetSizeOnStackDWords();
  3814. }
  3815. }
  3816. // interface
  3817. int asCContext::GetExceptionLineNumber(int *column, const char **sectionName)
  3818. {
  3819. if( GetState() != asEXECUTION_EXCEPTION ) return asERROR;
  3820. if( column ) *column = m_exceptionColumn;
  3821. if( sectionName )
  3822. {
  3823. // The section index can be -1 if the exception was raised in a generated function, e.g. factstub for templates
  3824. if( m_exceptionSectionIdx >= 0 )
  3825. *sectionName = m_engine->scriptSectionNames[m_exceptionSectionIdx]->AddressOf();
  3826. else
  3827. *sectionName = 0;
  3828. }
  3829. return m_exceptionLine;
  3830. }
  3831. // interface
  3832. asIScriptFunction *asCContext::GetExceptionFunction()
  3833. {
  3834. if( GetState() != asEXECUTION_EXCEPTION ) return 0;
  3835. return m_engine->scriptFunctions[m_exceptionFunction];
  3836. }
  3837. // interface
  3838. const char *asCContext::GetExceptionString()
  3839. {
  3840. if( GetState() != asEXECUTION_EXCEPTION ) return 0;
  3841. return m_exceptionString.AddressOf();
  3842. }
  3843. // interface
  3844. asEContextState asCContext::GetState() const
  3845. {
  3846. return m_status;
  3847. }
  3848. // interface
  3849. int asCContext::SetLineCallback(asSFuncPtr callback, void *obj, int callConv)
  3850. {
  3851. m_lineCallback = true;
  3852. m_regs.doProcessSuspend = true;
  3853. m_lineCallbackObj = obj;
  3854. bool isObj = false;
  3855. if( (unsigned)callConv == asCALL_GENERIC )
  3856. {
  3857. m_lineCallback = false;
  3858. m_regs.doProcessSuspend = m_doSuspend;
  3859. return asNOT_SUPPORTED;
  3860. }
  3861. if( (unsigned)callConv >= asCALL_THISCALL )
  3862. {
  3863. isObj = true;
  3864. if( obj == 0 )
  3865. {
  3866. m_lineCallback = false;
  3867. m_regs.doProcessSuspend = m_doSuspend;
  3868. return asINVALID_ARG;
  3869. }
  3870. }
  3871. int r = DetectCallingConvention(isObj, callback, callConv, 0, &m_lineCallbackFunc);
  3872. if( r < 0 ) m_lineCallback = false;
  3873. m_regs.doProcessSuspend = m_doSuspend || m_lineCallback;
  3874. return r;
  3875. }
  3876. void asCContext::CallLineCallback()
  3877. {
  3878. if( m_lineCallbackFunc.callConv < ICC_THISCALL )
  3879. m_engine->CallGlobalFunction(this, m_lineCallbackObj, &m_lineCallbackFunc, 0);
  3880. else
  3881. m_engine->CallObjectMethod(m_lineCallbackObj, this, &m_lineCallbackFunc, 0);
  3882. }
  3883. // interface
  3884. int asCContext::SetExceptionCallback(asSFuncPtr callback, void *obj, int callConv)
  3885. {
  3886. m_exceptionCallback = true;
  3887. m_exceptionCallbackObj = obj;
  3888. bool isObj = false;
  3889. if( (unsigned)callConv == asCALL_GENERIC )
  3890. return asNOT_SUPPORTED;
  3891. if( (unsigned)callConv >= asCALL_THISCALL )
  3892. {
  3893. isObj = true;
  3894. if( obj == 0 )
  3895. {
  3896. m_exceptionCallback = false;
  3897. return asINVALID_ARG;
  3898. }
  3899. }
  3900. int r = DetectCallingConvention(isObj, callback, callConv, 0, &m_exceptionCallbackFunc);
  3901. if( r < 0 ) m_exceptionCallback = false;
  3902. return r;
  3903. }
  3904. void asCContext::CallExceptionCallback()
  3905. {
  3906. if( m_exceptionCallbackFunc.callConv < ICC_THISCALL )
  3907. m_engine->CallGlobalFunction(this, m_exceptionCallbackObj, &m_exceptionCallbackFunc, 0);
  3908. else
  3909. m_engine->CallObjectMethod(m_exceptionCallbackObj, this, &m_exceptionCallbackFunc, 0);
  3910. }
  3911. // interface
  3912. void asCContext::ClearLineCallback()
  3913. {
  3914. m_lineCallback = false;
  3915. m_regs.doProcessSuspend = m_doSuspend;
  3916. }
  3917. // interface
  3918. void asCContext::ClearExceptionCallback()
  3919. {
  3920. m_exceptionCallback = false;
  3921. }
  3922. int asCContext::CallGeneric(int id, void *objectPointer)
  3923. {
  3924. asCScriptFunction *sysFunction = m_engine->scriptFunctions[id];
  3925. asSSystemFunctionInterface *sysFunc = sysFunction->sysFuncIntf;
  3926. void (*func)(asIScriptGeneric*) = (void (*)(asIScriptGeneric*))sysFunc->func;
  3927. int popSize = sysFunc->paramSize;
  3928. asDWORD *args = m_regs.stackPointer;
  3929. // Verify the object pointer if it is a class method
  3930. void *currentObject = 0;
  3931. if( sysFunc->callConv == ICC_GENERIC_METHOD )
  3932. {
  3933. if( objectPointer )
  3934. {
  3935. currentObject = objectPointer;
  3936. // Don't increase the reference of this pointer
  3937. // since it will not have been constructed yet
  3938. }
  3939. else
  3940. {
  3941. // The object pointer should be popped from the context stack
  3942. popSize += AS_PTR_SIZE;
  3943. // Check for null pointer
  3944. currentObject = (void*)*(asPWORD*)(args);
  3945. if( currentObject == 0 )
  3946. {
  3947. SetInternalException(TXT_NULL_POINTER_ACCESS);
  3948. return 0;
  3949. }
  3950. // Add the base offset for multiple inheritance
  3951. currentObject = (void*)(asPWORD(currentObject) + sysFunc->baseOffset);
  3952. // Skip object pointer
  3953. args += AS_PTR_SIZE;
  3954. }
  3955. }
  3956. if( sysFunction->DoesReturnOnStack() )
  3957. {
  3958. // Skip the address where the return value will be stored
  3959. args += AS_PTR_SIZE;
  3960. popSize += AS_PTR_SIZE;
  3961. }
  3962. asCGeneric gen(m_engine, sysFunction, currentObject, args);
  3963. m_callingSystemFunction = sysFunction;
  3964. func(&gen);
  3965. m_callingSystemFunction = 0;
  3966. m_regs.valueRegister = gen.returnVal;
  3967. m_regs.objectRegister = gen.objectRegister;
  3968. m_regs.objectType = sysFunction->returnType.GetObjectType();
  3969. // Clean up function parameters
  3970. int offset = 0;
  3971. for( asUINT n = 0; n < sysFunction->parameterTypes.GetLength(); n++ )
  3972. {
  3973. if( sysFunction->parameterTypes[n].IsObject() && !sysFunction->parameterTypes[n].IsReference() )
  3974. {
  3975. void *obj = *(void**)&args[offset];
  3976. if( obj )
  3977. {
  3978. // Release the object
  3979. asSTypeBehaviour *beh = &sysFunction->parameterTypes[n].GetObjectType()->beh;
  3980. if( sysFunction->parameterTypes[n].GetObjectType()->flags & asOBJ_REF )
  3981. {
  3982. asASSERT( (sysFunction->parameterTypes[n].GetObjectType()->flags & asOBJ_NOCOUNT) || beh->release );
  3983. if( beh->release )
  3984. m_engine->CallObjectMethod(obj, beh->release);
  3985. }
  3986. else
  3987. {
  3988. // Call the destructor then free the memory
  3989. if( beh->destruct )
  3990. m_engine->CallObjectMethod(obj, beh->destruct);
  3991. m_engine->CallFree(obj);
  3992. }
  3993. }
  3994. }
  3995. offset += sysFunction->parameterTypes[n].GetSizeOnStackDWords();
  3996. }
  3997. // Return how much should be popped from the stack
  3998. return popSize;
  3999. }
  4000. // interface
  4001. int asCContext::GetVarCount(asUINT stackLevel)
  4002. {
  4003. asIScriptFunction *func = GetFunction(stackLevel);
  4004. if( func == 0 ) return asINVALID_ARG;
  4005. return func->GetVarCount();
  4006. }
  4007. // interface
  4008. const char *asCContext::GetVarName(asUINT varIndex, asUINT stackLevel)
  4009. {
  4010. asIScriptFunction *func = GetFunction(stackLevel);
  4011. if( func == 0 ) return 0;
  4012. const char *name = 0;
  4013. int r = func->GetVar(varIndex, &name);
  4014. return r >= 0 ? name : 0;
  4015. }
  4016. // interface
  4017. const char *asCContext::GetVarDeclaration(asUINT varIndex, asUINT stackLevel, bool includeNamespace)
  4018. {
  4019. asIScriptFunction *func = GetFunction(stackLevel);
  4020. if( func == 0 ) return 0;
  4021. return func->GetVarDecl(varIndex, includeNamespace);
  4022. }
  4023. // interface
  4024. int asCContext::GetVarTypeId(asUINT varIndex, asUINT stackLevel)
  4025. {
  4026. asIScriptFunction *func = GetFunction(stackLevel);
  4027. if( func == 0 ) return asINVALID_ARG;
  4028. int typeId;
  4029. int r = func->GetVar(varIndex, 0, &typeId);
  4030. return r < 0 ? r : typeId;
  4031. }
  4032. // interface
  4033. void *asCContext::GetAddressOfVar(asUINT varIndex, asUINT stackLevel)
  4034. {
  4035. // Don't return anything if there is no bytecode, e.g. before calling Execute()
  4036. if( m_regs.programPointer == 0 ) return 0;
  4037. if( stackLevel >= GetCallstackSize() ) return 0;
  4038. asCScriptFunction *func;
  4039. asDWORD *sf;
  4040. if( stackLevel == 0 )
  4041. {
  4042. func = m_currentFunction;
  4043. sf = m_regs.stackFramePointer;
  4044. }
  4045. else
  4046. {
  4047. asPWORD *s = m_callStack.AddressOf() + (GetCallstackSize()-stackLevel-1)*CALLSTACK_FRAME_SIZE;
  4048. func = (asCScriptFunction*)s[1];
  4049. sf = (asDWORD*)s[0];
  4050. }
  4051. if( func == 0 )
  4052. return 0;
  4053. if( func->scriptData == 0 )
  4054. return 0;
  4055. if( varIndex >= func->scriptData->variables.GetLength() )
  4056. return 0;
  4057. // For object variables it's necessary to dereference the pointer to get the address of the value
  4058. // Reference parameters must also be dereferenced to give the address of the value
  4059. int pos = func->scriptData->variables[varIndex]->stackOffset;
  4060. if( (func->scriptData->variables[varIndex]->type.IsObject() && !func->scriptData->variables[varIndex]->type.IsObjectHandle()) || (pos <= 0) )
  4061. {
  4062. // Determine if the object is really on the heap
  4063. bool onHeap = false;
  4064. if( func->scriptData->variables[varIndex]->type.IsObject() &&
  4065. !func->scriptData->variables[varIndex]->type.IsObjectHandle() )
  4066. {
  4067. onHeap = true;
  4068. if( func->scriptData->variables[varIndex]->type.GetObjectType()->GetFlags() & asOBJ_VALUE )
  4069. {
  4070. for( asUINT n = 0; n < func->scriptData->objVariablePos.GetLength(); n++ )
  4071. {
  4072. if( func->scriptData->objVariablePos[n] == pos )
  4073. {
  4074. onHeap = n < func->scriptData->objVariablesOnHeap;
  4075. if( !onHeap )
  4076. {
  4077. // If the object on the stack is not initialized return a null pointer instead
  4078. asCArray<int> liveObjects;
  4079. DetermineLiveObjects(liveObjects, stackLevel);
  4080. if( liveObjects[n] <= 0 )
  4081. return 0;
  4082. }
  4083. break;
  4084. }
  4085. }
  4086. }
  4087. }
  4088. // If it wasn't an object on the heap, then check if it is a reference parameter
  4089. if( !onHeap && pos <= 0 )
  4090. {
  4091. // Determine what function argument this position matches
  4092. int stackPos = 0;
  4093. if( func->objectType )
  4094. stackPos -= AS_PTR_SIZE;
  4095. if( func->DoesReturnOnStack() )
  4096. stackPos -= AS_PTR_SIZE;
  4097. for( asUINT n = 0; n < func->parameterTypes.GetLength(); n++ )
  4098. {
  4099. if( stackPos == pos )
  4100. {
  4101. // The right argument was found. Is this a reference parameter?
  4102. if( func->inOutFlags[n] != asTM_NONE )
  4103. onHeap = true;
  4104. break;
  4105. }
  4106. stackPos -= func->parameterTypes[n].GetSizeOnStackDWords();
  4107. }
  4108. }
  4109. if( onHeap )
  4110. return *(void**)(sf - func->scriptData->variables[varIndex]->stackOffset);
  4111. }
  4112. return sf - func->scriptData->variables[varIndex]->stackOffset;
  4113. }
  4114. // interface
  4115. // returns the typeId of the 'this' object at the given call stack level (-1 for current)
  4116. // returns 0 if the function call at the given stack level is not a method
  4117. int asCContext::GetThisTypeId(asUINT stackLevel)
  4118. {
  4119. asIScriptFunction *func = GetFunction(stackLevel);
  4120. if( func == 0 ) return asINVALID_ARG;
  4121. if( func->GetObjectType() == 0 )
  4122. return 0; // not in a method
  4123. // create a datatype
  4124. asCDataType dt = asCDataType::CreateObject((asCObjectType*)func->GetObjectType(), false);
  4125. // return a typeId from the data type
  4126. return m_engine->GetTypeIdFromDataType(dt);
  4127. }
  4128. // interface
  4129. // returns the 'this' object pointer at the given call stack level (-1 for current)
  4130. // returns 0 if the function call at the given stack level is not a method
  4131. void *asCContext::GetThisPointer(asUINT stackLevel)
  4132. {
  4133. if( stackLevel >= GetCallstackSize() )
  4134. return 0;
  4135. asCScriptFunction *func;
  4136. asDWORD *sf;
  4137. if( stackLevel == 0 )
  4138. {
  4139. func = m_currentFunction;
  4140. sf = m_regs.stackFramePointer;
  4141. }
  4142. else
  4143. {
  4144. asPWORD *s = m_callStack.AddressOf() + (GetCallstackSize()-stackLevel-1)*CALLSTACK_FRAME_SIZE;
  4145. func = (asCScriptFunction*)s[1];
  4146. sf = (asDWORD*)s[0];
  4147. }
  4148. if( func == 0 )
  4149. return 0;
  4150. if( func->objectType == 0 )
  4151. return 0; // not in a method
  4152. void *thisPointer = (void*)*(asPWORD*)(sf);
  4153. if( thisPointer == 0 )
  4154. {
  4155. return 0;
  4156. }
  4157. // NOTE: this returns the pointer to the 'this' while the GetVarPointer functions return
  4158. // a pointer to a pointer. I can't imagine someone would want to change the 'this'
  4159. return thisPointer;
  4160. }
  4161. END_AS_NAMESPACE