as_context.cpp 138 KB

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