as_context.cpp 160 KB

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