as_context.cpp 154 KB

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