as_context.cpp 151 KB

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