as_context.cpp 134 KB

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