as_context.cpp 125 KB

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