as_context.cpp 162 KB

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