as_context.cpp 153 KB

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