as_context.cpp 157 KB

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