as_context.cpp 158 KB

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