as_context.cpp 127 KB

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