as_context.cpp 156 KB

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