as_context.cpp 111 KB

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