as_compiler.cpp 345 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 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. // Modified by Lasse Öörni for Urho3D
  24. //
  25. // as_compiler.cpp
  26. //
  27. // The class that does the actual compilation of the functions
  28. //
  29. #include <math.h> // fmodf()
  30. #include "as_config.h"
  31. #include "as_compiler.h"
  32. #include "as_tokendef.h"
  33. #include "as_tokenizer.h"
  34. #include "as_string_util.h"
  35. #include "as_texts.h"
  36. #include "as_parser.h"
  37. BEGIN_AS_NAMESPACE
  38. // TODO: I must correct the interpretation of a references to objects in the compiler.
  39. // A reference should mean that a pointer to the object is on the stack.
  40. // No expression should end up as non-references to objects, as the actual object is
  41. // never put on the stack.
  42. // Local variables are declared as non-references, but the expression should be a reference to the variable.
  43. // Function parameters of called functions can also be non-references, but in that case it means the
  44. // object will be passed by value (currently on the heap, which will be moved to the application stack).
  45. asCCompiler::asCCompiler(asCScriptEngine *engine) : byteCode(engine)
  46. {
  47. builder = 0;
  48. script = 0;
  49. variables = 0;
  50. isProcessingDeferredParams = false;
  51. isCompilingDefaultArg = false;
  52. noCodeOutput = 0;
  53. }
  54. asCCompiler::~asCCompiler()
  55. {
  56. while( variables )
  57. {
  58. asCVariableScope *var = variables;
  59. variables = variables->parent;
  60. asDELETE(var,asCVariableScope);
  61. }
  62. }
  63. void asCCompiler::Reset(asCBuilder *builder, asCScriptCode *script, asCScriptFunction *outFunc)
  64. {
  65. this->builder = builder;
  66. this->engine = builder->engine;
  67. this->script = script;
  68. this->outFunc = outFunc;
  69. hasCompileErrors = false;
  70. m_isConstructor = false;
  71. m_isConstructorCalled = false;
  72. nextLabel = 0;
  73. breakLabels.SetLength(0);
  74. continueLabels.SetLength(0);
  75. byteCode.ClearAll();
  76. }
  77. int asCCompiler::CompileDefaultConstructor(asCBuilder *builder, asCScriptCode *script, asCScriptFunction *outFunc)
  78. {
  79. Reset(builder, script, outFunc);
  80. // If the class is derived from another, then the base class' default constructor must be called
  81. if( outFunc->objectType->derivedFrom )
  82. {
  83. // Call the base class' default constructor
  84. byteCode.InstrSHORT(asBC_PSF, 0);
  85. byteCode.Instr(asBC_RDSPTR);
  86. byteCode.Call(asBC_CALL, outFunc->objectType->derivedFrom->beh.construct, AS_PTR_SIZE);
  87. }
  88. // Pop the object pointer from the stack
  89. byteCode.Ret(AS_PTR_SIZE);
  90. FinalizeFunction();
  91. #ifdef AS_DEBUG
  92. // DEBUG: output byte code
  93. byteCode.DebugOutput(("__" + outFunc->objectType->name + "_" + outFunc->name + "__dc.txt").AddressOf(), engine, outFunc);
  94. #endif
  95. return 0;
  96. }
  97. int asCCompiler::CompileFactory(asCBuilder *builder, asCScriptCode *script, asCScriptFunction *outFunc)
  98. {
  99. Reset(builder, script, outFunc);
  100. unsigned int n;
  101. // Find the corresponding constructor
  102. asCDataType dt = asCDataType::CreateObject(outFunc->returnType.GetObjectType(), false);
  103. int constructor = 0;
  104. for( n = 0; n < dt.GetBehaviour()->factories.GetLength(); n++ )
  105. {
  106. if( dt.GetBehaviour()->factories[n] == outFunc->id )
  107. {
  108. constructor = dt.GetBehaviour()->constructors[n];
  109. break;
  110. }
  111. }
  112. // Allocate the class and instanciate it with the constructor
  113. int varOffset = AllocateVariable(dt, true);
  114. byteCode.Push(AS_PTR_SIZE);
  115. byteCode.InstrSHORT(asBC_PSF, (short)varOffset);
  116. // Copy all arguments to the top of the stack
  117. int argDwords = (int)outFunc->GetSpaceNeededForArguments();
  118. for( int a = argDwords-1; a >= 0; a-- )
  119. byteCode.InstrSHORT(asBC_PshV4, short(-a));
  120. byteCode.Alloc(asBC_ALLOC, dt.GetObjectType(), constructor, argDwords + AS_PTR_SIZE);
  121. // Return a handle to the newly created object
  122. byteCode.InstrSHORT(asBC_LOADOBJ, (short)varOffset);
  123. byteCode.Ret(argDwords);
  124. FinalizeFunction();
  125. // Tell the virtual machine not to clean up parameters on exception
  126. outFunc->dontCleanUpOnException = true;
  127. /*
  128. #ifdef AS_DEBUG
  129. // DEBUG: output byte code
  130. asCString args;
  131. args.Format("%d", outFunc->parameterTypes.GetLength());
  132. byteCode.DebugOutput(("__" + outFunc->name + "__factory" + args + ".txt").AddressOf(), engine);
  133. #endif
  134. */
  135. return 0;
  136. }
  137. // Entry
  138. int asCCompiler::CompileTemplateFactoryStub(asCBuilder *builder, int trueFactoryId, asCObjectType *objType, asCScriptFunction *outFunc)
  139. {
  140. Reset(builder, 0, outFunc);
  141. asCScriptFunction *descr = builder->GetFunctionDescription(trueFactoryId);
  142. byteCode.InstrPTR(asBC_OBJTYPE, objType);
  143. byteCode.Call(asBC_CALLSYS, trueFactoryId, descr->GetSpaceNeededForArguments());
  144. byteCode.Ret(outFunc->GetSpaceNeededForArguments());
  145. FinalizeFunction();
  146. // Tell the virtual machine not to clean up the object on exception
  147. outFunc->dontCleanUpOnException = true;
  148. return 0;
  149. }
  150. // Entry
  151. int asCCompiler::CompileFunction(asCBuilder *builder, asCScriptCode *script, sExplicitSignature *signature, asCScriptNode *func, asCScriptFunction *outFunc)
  152. {
  153. // TODO: The compiler should take the return type and parameter types from the
  154. // outFunc, instead of interpreting the script nodes again. The builder
  155. // must pass the list of parameter names. Making this change we can
  156. // eliminate large parts of this function and the sExplicitSignature structure
  157. Reset(builder, script, outFunc);
  158. int buildErrors = builder->numErrors;
  159. int stackPos = 0;
  160. if( outFunc->objectType )
  161. stackPos = -AS_PTR_SIZE; // The first parameter is the pointer to the object
  162. // Reserve a label for the cleanup code
  163. nextLabel++;
  164. // Add the first variable scope, which the parameters and
  165. // variables declared in the outermost statement block is
  166. // part of.
  167. AddVariableScope();
  168. asCScriptNode *node;
  169. bool isDestructor = false;
  170. asCDataType returnType;
  171. if( !signature )
  172. {
  173. // Skip the private keyword if it is there
  174. node = func->firstChild;
  175. if( node->nodeType == snUndefined && node->tokenType == ttPrivate )
  176. node = node->next;
  177. //----------------------------------------------
  178. // Examine return type
  179. if( node->nodeType == snDataType )
  180. {
  181. returnType = builder->CreateDataTypeFromNode(node, script);
  182. returnType = builder->ModifyDataTypeFromNode(returnType, node->next, script, 0, 0);
  183. // Make sure the return type is instanciable or is void
  184. if( !returnType.CanBeInstanciated() &&
  185. returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  186. {
  187. asCString str;
  188. str.Format(TXT_DATA_TYPE_CANT_BE_s, returnType.Format().AddressOf());
  189. Error(str.AddressOf(), func->firstChild);
  190. }
  191. }
  192. else
  193. {
  194. returnType = asCDataType::CreatePrimitive(ttVoid, false);
  195. if( node->tokenType == ttBitNot )
  196. isDestructor = true;
  197. else
  198. m_isConstructor = true;
  199. }
  200. }
  201. else
  202. {
  203. node = func;
  204. returnType = signature->returnType;
  205. }
  206. #ifndef AS_OLD
  207. // If the return type is a value type returned by value the address of the
  208. // location where the value will be stored is pushed on the stack before
  209. // the arguments
  210. if( !(isDestructor || m_isConstructor) && outFunc->DoesReturnOnStack() )
  211. stackPos -= AS_PTR_SIZE;
  212. #endif
  213. asCVariableScope vs(0);
  214. if( !signature )
  215. {
  216. //----------------------------------------------
  217. // Declare parameters
  218. // Find first parameter
  219. while( node && node->nodeType != snParameterList )
  220. node = node->next;
  221. // Register parameters from last to first, otherwise they will be destroyed in the wrong order
  222. if( node ) node = node->firstChild;
  223. while( node )
  224. {
  225. // Get the parameter type
  226. asCDataType type = builder->CreateDataTypeFromNode(node, script);
  227. asETypeModifiers inoutFlag = asTM_NONE;
  228. type = builder->ModifyDataTypeFromNode(type, node->next, script, &inoutFlag, 0);
  229. // Is the data type allowed?
  230. if( (type.IsReference() && inoutFlag != asTM_INOUTREF && !type.CanBeInstanciated()) ||
  231. (!type.IsReference() && !type.CanBeInstanciated()) )
  232. {
  233. asCString str;
  234. str.Format(TXT_PARAMETER_CANT_BE_s, type.Format().AddressOf());
  235. Error(str.AddressOf(), node);
  236. }
  237. // If the parameter has a name then declare it as variable
  238. node = node->next->next;
  239. if( node && node->nodeType == snIdentifier )
  240. {
  241. asCString name(&script->code[node->tokenPos], node->tokenLength);
  242. if( vs.DeclareVariable(name.AddressOf(), type, stackPos, true) < 0 )
  243. Error(TXT_PARAMETER_ALREADY_DECLARED, node);
  244. // Add marker for variable declaration
  245. byteCode.VarDecl((int)outFunc->variables.GetLength());
  246. outFunc->AddVariable(name, type, stackPos);
  247. node = node->next;
  248. // Skip the default arg
  249. if( node && node->nodeType == snExpression )
  250. node = node->next;
  251. }
  252. else
  253. vs.DeclareVariable("", type, stackPos, true);
  254. // Move to next parameter
  255. stackPos -= type.GetSizeOnStackDWords();
  256. }
  257. }
  258. else
  259. {
  260. asCArray<asCDataType> &args = signature->argTypes;
  261. asCArray<asETypeModifiers> &inoutFlags = signature->argModifiers;
  262. asCArray<asCString> &argNames = signature->argNames;
  263. asASSERT(args.GetLength() == argNames.GetLength());
  264. for( int k = 0; k < (int)args.GetLength(); k++ )
  265. {
  266. asCDataType type = args[k];
  267. asETypeModifiers inoutFlag = inoutFlags[k];
  268. if( (type.IsReference() && inoutFlag != asTM_INOUTREF && !type.CanBeInstanciated()) ||
  269. (!type.IsReference() && !type.CanBeInstanciated()) )
  270. {
  271. asCString str;
  272. str.Format(TXT_PARAMETER_CANT_BE_s, type.Format().AddressOf());
  273. Error(str.AddressOf(), node);
  274. }
  275. if( 0 != argNames[k].Compare("") )
  276. {
  277. if( vs.DeclareVariable(argNames[k].AddressOf(), type, stackPos, true) < 0 )
  278. Error(TXT_PARAMETER_ALREADY_DECLARED, node);
  279. // Add marker for variable declaration
  280. byteCode.VarDecl((int)outFunc->variables.GetLength());
  281. outFunc->AddVariable(argNames[k], type, stackPos);
  282. }
  283. else
  284. vs.DeclareVariable("", type, stackPos, true);
  285. // Move to next parameter
  286. stackPos -= type.GetSizeOnStackDWords();
  287. }
  288. }
  289. int n;
  290. for( n = (int)vs.variables.GetLength() - 1; n >= 0; n-- )
  291. {
  292. variables->DeclareVariable(vs.variables[n]->name.AddressOf(), vs.variables[n]->type, vs.variables[n]->stackOffset, vs.variables[n]->onHeap);
  293. }
  294. // Is the return type allowed?
  295. if( (returnType.GetSizeOnStackDWords() == 0 && returnType != asCDataType::CreatePrimitive(ttVoid, false)) ||
  296. (returnType.IsReference() && !returnType.CanBeInstanciated()) )
  297. {
  298. asCString str;
  299. str.Format(TXT_RETURN_CANT_BE_s, returnType.Format().AddressOf());
  300. Error(str.AddressOf(), func);
  301. }
  302. variables->DeclareVariable("return", returnType, stackPos, true);
  303. //--------------------------------------------
  304. // Compile the statement block
  305. // We need to parse the statement block now
  306. asCScriptNode *blockBegin;
  307. if( !signature )
  308. blockBegin = func->lastChild;
  309. else
  310. blockBegin = func;
  311. // TODO: memory: We can parse the statement block one statement at a time, thus save even more memory
  312. asCParser parser(builder);
  313. int r = parser.ParseStatementBlock(script, blockBegin);
  314. if( r < 0 ) return -1;
  315. asCScriptNode *block = parser.GetScriptNode();
  316. bool hasReturn;
  317. asCByteCode bc(engine);
  318. LineInstr(&bc, blockBegin->tokenPos);
  319. CompileStatementBlock(block, false, &hasReturn, &bc);
  320. LineInstr(&bc, blockBegin->tokenPos + blockBegin->tokenLength);
  321. // Make sure there is a return in all paths (if not return type is void)
  322. if( returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  323. {
  324. if( hasReturn == false )
  325. Error(TXT_NOT_ALL_PATHS_RETURN, blockBegin);
  326. }
  327. //------------------------------------------------
  328. // Concatenate the bytecode
  329. // Insert a JitEntry at the start of the function for JIT compilers
  330. byteCode.InstrPTR(asBC_JitEntry, 0);
  331. // Count total variable size
  332. int varSize = GetVariableOffset((int)variableAllocations.GetLength()) - 1;
  333. byteCode.Push(varSize);
  334. if( outFunc->objectType )
  335. {
  336. // Call the base class' default constructor unless called manually in the code
  337. if( m_isConstructor && !m_isConstructorCalled && outFunc->objectType->derivedFrom )
  338. {
  339. byteCode.InstrSHORT(asBC_PSF, 0);
  340. byteCode.Instr(asBC_RDSPTR);
  341. byteCode.Call(asBC_CALL, outFunc->objectType->derivedFrom->beh.construct, AS_PTR_SIZE);
  342. }
  343. // Increase the reference for the object pointer, so that it is guaranteed to live during the entire call
  344. // TODO: optimize: This is probably not necessary for constructors as no outside reference to the object is created yet
  345. byteCode.InstrSHORT(asBC_PSF, 0);
  346. byteCode.Instr(asBC_RDSPTR);
  347. byteCode.Call(asBC_CALLSYS, outFunc->objectType->beh.addref, AS_PTR_SIZE);
  348. }
  349. // Add the code for the statement block
  350. byteCode.AddCode(&bc);
  351. // Deallocate all local variables
  352. for( n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  353. {
  354. sVariable *v = variables->variables[n];
  355. if( v->stackOffset > 0 )
  356. {
  357. // Call variables destructors
  358. if( v->name != "return" && v->name != "return address" )
  359. CallDestructor(v->type, v->stackOffset, v->onHeap, &byteCode);
  360. DeallocateVariable(v->stackOffset);
  361. }
  362. }
  363. // This is the label that return statements jump to
  364. // in order to exit the function
  365. byteCode.Label(0);
  366. // Call destructors for function parameters
  367. for( n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  368. {
  369. sVariable *v = variables->variables[n];
  370. if( v->stackOffset <= 0 )
  371. {
  372. // Call variable destructors here, for variables not yet destroyed
  373. if( v->name != "return" && v->name != "return address" )
  374. CallDestructor(v->type, v->stackOffset, v->onHeap, &byteCode);
  375. }
  376. // Do not deallocate parameters
  377. }
  378. // Release the object pointer again
  379. if( outFunc->objectType )
  380. {
  381. byteCode.InstrW_PTR(asBC_FREE, 0, outFunc->objectType);
  382. }
  383. // If there are compile errors, there is no reason to build the final code
  384. if( hasCompileErrors || builder->numErrors != buildErrors )
  385. return -1;
  386. // At this point there should be no variables allocated
  387. asASSERT(variableAllocations.GetLength() == freeVariables.GetLength());
  388. // Remove the variable scope
  389. RemoveVariableScope();
  390. // This POP is not necessary as the return will clean up the stack frame anyway.
  391. // The bytecode optimizer would remove this POP, however by not including it here
  392. // it is guaranteed it doesn't have to be adjusted by the asCRestore class when
  393. // a types are of a different size than originally compiled for.
  394. // byteCode.Pop(varSize);
  395. byteCode.Ret(-stackPos);
  396. FinalizeFunction();
  397. #ifdef AS_DEBUG
  398. // DEBUG: output byte code
  399. if( outFunc->objectType )
  400. byteCode.DebugOutput(("__" + outFunc->objectType->name + "_" + outFunc->name + ".txt").AddressOf(), engine, outFunc);
  401. else
  402. byteCode.DebugOutput(("__" + outFunc->name + ".txt").AddressOf(), engine, outFunc);
  403. #endif
  404. return 0;
  405. }
  406. int asCCompiler::CallCopyConstructor(asCDataType &type, int offset, bool isObjectOnHeap, asCByteCode *bc, asSExprContext *arg, asCScriptNode *node, bool isGlobalVar, bool derefDest)
  407. {
  408. if( !type.IsObject() )
  409. return 0;
  410. // CallCopyConstructor should not be called for object handles.
  411. asASSERT(!type.IsObjectHandle() || (type.GetObjectType() && (type.GetObjectType()->flags & asOBJ_ASHANDLE)) );
  412. asCArray<asSExprContext*> args;
  413. args.PushLast(arg);
  414. // The reference parameter must be pushed on the stack
  415. asASSERT( arg->type.dataType.GetObjectType() == type.GetObjectType() );
  416. // Since we're calling the copy constructor, we have to trust the function to not do
  417. // anything stupid otherwise we will just enter a loop, as we try to make temporary
  418. // copies of the argument in order to guarantee safety.
  419. if( type.GetObjectType()->flags & asOBJ_REF )
  420. {
  421. asSExprContext ctx(engine);
  422. int func = 0;
  423. asSTypeBehaviour *beh = type.GetBehaviour();
  424. if( beh ) func = beh->copyfactory;
  425. if( func > 0 )
  426. {
  427. if( !isGlobalVar )
  428. {
  429. // Call factory and store the handle in the given variable
  430. PerformFunctionCall(func, &ctx, false, &args, type.GetObjectType(), true, offset);
  431. // Pop the reference left by the function call
  432. ctx.bc.Pop(AS_PTR_SIZE);
  433. }
  434. else
  435. {
  436. // Call factory
  437. PerformFunctionCall(func, &ctx, false, &args, type.GetObjectType());
  438. // Store the returned handle in the global variable
  439. ctx.bc.Instr(asBC_RDSPTR);
  440. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  441. ctx.bc.InstrPTR(asBC_REFCPY, type.GetObjectType());
  442. ctx.bc.Pop(AS_PTR_SIZE);
  443. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  444. }
  445. bc->AddCode(&ctx.bc);
  446. return 0;
  447. }
  448. }
  449. else
  450. {
  451. asSTypeBehaviour *beh = type.GetBehaviour();
  452. int func = beh ? beh->copyconstruct : 0;
  453. if( func > 0 )
  454. {
  455. // Push the address where the object will be stored on the stack, before the argument
  456. // TODO: When the context is serializable this probably has to be changed, since this
  457. // pointer can remain on the stack while the context is suspended. There is no
  458. // risk the pointer becomes invalid though, there is just no easy way to serialize it.
  459. asCByteCode tmp(engine);
  460. if( isGlobalVar )
  461. tmp.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  462. else if( isObjectOnHeap )
  463. tmp.InstrSHORT(asBC_PSF, (short)offset);
  464. tmp.AddCode(bc);
  465. bc->AddCode(&tmp);
  466. // When the object is allocated on the stack the object pointer
  467. // must be pushed on the stack after the arguments
  468. if( !isObjectOnHeap )
  469. {
  470. asASSERT( !isGlobalVar );
  471. bc->InstrSHORT(asBC_PSF, (short)offset);
  472. if( derefDest )
  473. {
  474. // The variable is a reference to the real location, so we need to dereference it
  475. bc->Instr(asBC_RDSPTR);
  476. }
  477. }
  478. asSExprContext ctx(engine);
  479. PerformFunctionCall(func, &ctx, isObjectOnHeap, &args, type.GetObjectType());
  480. bc->AddCode(&ctx.bc);
  481. // TODO: value on stack: This probably needs to be done in PerformFunctionCall
  482. // Mark the object as initialized
  483. if( !isObjectOnHeap )
  484. bc->ObjInfo(offset, asOBJ_INIT);
  485. return 0;
  486. }
  487. }
  488. // Class has no copy constructor/factory.
  489. asCString str;
  490. str.Format(TXT_NO_COPY_CONSTRUCTOR_FOR_s, type.GetObjectType()->GetName());
  491. Error(str.AddressOf(), node);
  492. return -1;
  493. }
  494. int asCCompiler::CallDefaultConstructor(asCDataType &type, int offset, bool isObjectOnHeap, asCByteCode *bc, asCScriptNode *node, bool isGlobalVar, bool deferDest)
  495. {
  496. if( !type.IsObject() ||
  497. (type.IsObjectHandle() && !(type.GetObjectType()->flags & asOBJ_ASHANDLE)) )
  498. return 0;
  499. if( type.GetObjectType()->flags & asOBJ_REF )
  500. {
  501. asSExprContext ctx(engine);
  502. ctx.exprNode = node;
  503. int func = 0;
  504. asSTypeBehaviour *beh = type.GetBehaviour();
  505. if( beh ) func = beh->factory;
  506. if( func > 0 )
  507. {
  508. if( !isGlobalVar )
  509. {
  510. // Call factory and store the handle in the given variable
  511. PerformFunctionCall(func, &ctx, false, 0, type.GetObjectType(), true, offset);
  512. // Pop the reference left by the function call
  513. ctx.bc.Pop(AS_PTR_SIZE);
  514. }
  515. else
  516. {
  517. // Call factory
  518. PerformFunctionCall(func, &ctx, false, 0, type.GetObjectType());
  519. // Store the returned handle in the global variable
  520. ctx.bc.Instr(asBC_RDSPTR);
  521. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  522. ctx.bc.InstrPTR(asBC_REFCPY, type.GetObjectType());
  523. ctx.bc.Pop(AS_PTR_SIZE);
  524. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  525. }
  526. bc->AddCode(&ctx.bc);
  527. return 0;
  528. }
  529. }
  530. else
  531. {
  532. asSTypeBehaviour *beh = type.GetBehaviour();
  533. int func = 0;
  534. if( beh ) func = beh->construct;
  535. // Allocate and initialize with the default constructor
  536. if( func != 0 || (type.GetObjectType()->flags & asOBJ_POD) )
  537. {
  538. if( !isObjectOnHeap )
  539. {
  540. asASSERT( !isGlobalVar );
  541. // There is nothing to do if there is no function,
  542. // as the memory is already allocated on the stack
  543. if( func )
  544. {
  545. // Call the constructor as a normal function
  546. bc->InstrSHORT(asBC_PSF, (short)offset);
  547. if( deferDest )
  548. bc->Instr(asBC_RDSPTR);
  549. asSExprContext ctx(engine);
  550. PerformFunctionCall(func, &ctx, false, 0, type.GetObjectType());
  551. bc->AddCode(&ctx.bc);
  552. // TODO: value on stack: This probably needs to be done in PerformFunctionCall
  553. // Mark the object as initialized
  554. bc->ObjInfo(offset, asOBJ_INIT);
  555. }
  556. }
  557. else
  558. {
  559. if( isGlobalVar )
  560. bc->InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  561. else
  562. bc->InstrSHORT(asBC_PSF, (short)offset);
  563. bc->Alloc(asBC_ALLOC, type.GetObjectType(), func, AS_PTR_SIZE);
  564. }
  565. return 0;
  566. }
  567. }
  568. // Class has no default factory/constructor.
  569. asCString str;
  570. // TODO: funcdef: asCDataType should have a GetTypeName()
  571. if( type.GetFuncDef() )
  572. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, type.GetFuncDef()->GetName());
  573. else
  574. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, type.GetObjectType()->GetName());
  575. Error(str.AddressOf(), node);
  576. return -1;
  577. }
  578. void asCCompiler::CallDestructor(asCDataType &type, int offset, bool isObjectOnHeap, asCByteCode *bc)
  579. {
  580. if( !type.IsReference() )
  581. {
  582. // Call destructor for the data type
  583. if( type.IsObject() )
  584. {
  585. // ASHANDLE is really a value type and shouldn't be deallocated. Just the destructor should be called
  586. if( isObjectOnHeap || (type.IsObjectHandle() && !(type.GetObjectType()->flags & asOBJ_ASHANDLE)) )
  587. {
  588. // Free the memory
  589. bc->InstrW_PTR(asBC_FREE, (short)offset, type.GetObjectType());
  590. }
  591. else
  592. {
  593. asASSERT( type.GetObjectType()->GetFlags() & asOBJ_VALUE );
  594. if( type.GetBehaviour()->destruct )
  595. {
  596. // Call the destructor as a regular function
  597. bc->InstrSHORT(asBC_PSF, (short)offset);
  598. asSExprContext ctx(engine);
  599. PerformFunctionCall(type.GetBehaviour()->destruct, &ctx);
  600. bc->AddCode(&ctx.bc);
  601. }
  602. // TODO: Value on stack: This probably needs to be done in PerformFunctionCall
  603. // Mark the object as destroyed
  604. bc->ObjInfo(offset, asOBJ_UNINIT);
  605. }
  606. }
  607. }
  608. }
  609. void asCCompiler::LineInstr(asCByteCode *bc, size_t pos)
  610. {
  611. int r, c;
  612. script->ConvertPosToRowCol(pos, &r, &c);
  613. bc->Line(r, c);
  614. }
  615. void asCCompiler::CompileStatementBlock(asCScriptNode *block, bool ownVariableScope, bool *hasReturn, asCByteCode *bc)
  616. {
  617. *hasReturn = false;
  618. bool isFinished = false;
  619. bool hasWarned = false;
  620. if( ownVariableScope )
  621. {
  622. bc->Block(true);
  623. AddVariableScope();
  624. }
  625. asCScriptNode *node = block->firstChild;
  626. while( node )
  627. {
  628. if( !hasWarned && (*hasReturn || isFinished) )
  629. {
  630. hasWarned = true;
  631. Warning(TXT_UNREACHABLE_CODE, node);
  632. }
  633. if( node->nodeType == snBreak || node->nodeType == snContinue )
  634. isFinished = true;
  635. asCByteCode statement(engine);
  636. if( node->nodeType == snDeclaration )
  637. CompileDeclaration(node, &statement);
  638. else
  639. CompileStatement(node, hasReturn, &statement);
  640. LineInstr(bc, node->tokenPos);
  641. bc->AddCode(&statement);
  642. if( !hasCompileErrors )
  643. {
  644. asASSERT( tempVariables.GetLength() == 0 );
  645. asASSERT( reservedVariables.GetLength() == 0 );
  646. }
  647. node = node->next;
  648. }
  649. if( ownVariableScope )
  650. {
  651. // Deallocate variables in this block, in reverse order
  652. for( int n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  653. {
  654. sVariable *v = variables->variables[n];
  655. // Call variable destructors here, for variables not yet destroyed
  656. // If the block is terminated with a break, continue, or
  657. // return the variables are already destroyed
  658. if( !isFinished && !*hasReturn )
  659. CallDestructor(v->type, v->stackOffset, v->onHeap, bc);
  660. // Don't deallocate function parameters
  661. if( v->stackOffset > 0 )
  662. DeallocateVariable(v->stackOffset);
  663. }
  664. RemoveVariableScope();
  665. bc->Block(false);
  666. }
  667. }
  668. // Entry
  669. int asCCompiler::CompileGlobalVariable(asCBuilder *builder, asCScriptCode *script, asCScriptNode *node, sGlobalVariableDescription *gvar, asCScriptFunction *outFunc)
  670. {
  671. Reset(builder, script, outFunc);
  672. // Add a variable scope (even though variables can't be declared)
  673. AddVariableScope();
  674. asSExprContext ctx(engine);
  675. gvar->isPureConstant = false;
  676. // Parse the initialization nodes
  677. asCParser parser(builder);
  678. if( node )
  679. {
  680. int r = parser.ParseGlobalVarInit(script, node);
  681. if( r < 0 )
  682. return r;
  683. node = parser.GetScriptNode();
  684. }
  685. // Compile the expression
  686. if( node && node->nodeType == snArgList )
  687. {
  688. // Make sure that it is a registered type, and that it isn't a pointer
  689. if( gvar->datatype.GetObjectType() == 0 || gvar->datatype.IsObjectHandle() )
  690. {
  691. Error(TXT_MUST_BE_OBJECT, node);
  692. }
  693. else
  694. {
  695. // Compile the arguments
  696. asCArray<asSExprContext *> args;
  697. if( CompileArgumentList(node, args) >= 0 )
  698. {
  699. // Find all constructors
  700. asCArray<int> funcs;
  701. asSTypeBehaviour *beh = gvar->datatype.GetBehaviour();
  702. if( beh )
  703. {
  704. if( gvar->datatype.GetObjectType()->flags & asOBJ_REF )
  705. funcs = beh->factories;
  706. else
  707. funcs = beh->constructors;
  708. }
  709. asCString str = gvar->datatype.Format();
  710. MatchFunctions(funcs, args, node, str.AddressOf());
  711. if( funcs.GetLength() == 1 )
  712. {
  713. int r = asSUCCESS;
  714. // Add the default values for arguments not explicitly supplied
  715. asCScriptFunction *func = (funcs[0] & 0xFFFF0000) == 0 ? engine->scriptFunctions[funcs[0]] : 0;
  716. if( func && args.GetLength() < (asUINT)func->GetParamCount() )
  717. r = CompileDefaultArgs(node, args, func);
  718. if( r == asSUCCESS )
  719. {
  720. if( gvar->datatype.GetObjectType()->flags & asOBJ_REF )
  721. {
  722. MakeFunctionCall(&ctx, funcs[0], 0, args, node);
  723. // Store the returned handle in the global variable
  724. ctx.bc.Instr(asBC_RDSPTR);
  725. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[gvar->index]->GetAddressOfValue());
  726. ctx.bc.InstrPTR(asBC_REFCPY, gvar->datatype.GetObjectType());
  727. ctx.bc.Pop(AS_PTR_SIZE);
  728. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  729. }
  730. else
  731. {
  732. // Push the address of the location where the variable will be stored on the stack.
  733. // This reference is safe, because the addresses of the global variables cannot change.
  734. // TODO: When serialization of the context is implemented this will probably have to change,
  735. // because this pointer may be on the stack while the context is suspended, and may
  736. // be difficult to serialize as the context doesn't know that the value represents a
  737. // pointer.
  738. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[gvar->index]->GetAddressOfValue());
  739. PrepareFunctionCall(funcs[0], &ctx.bc, args);
  740. MoveArgsToStack(funcs[0], &ctx.bc, args, false);
  741. PerformFunctionCall(funcs[0], &ctx, true, &args, gvar->datatype.GetObjectType());
  742. }
  743. }
  744. }
  745. }
  746. // Cleanup
  747. for( asUINT n = 0; n < args.GetLength(); n++ )
  748. if( args[n] )
  749. {
  750. asDELETE(args[n],asSExprContext);
  751. }
  752. }
  753. }
  754. else if( node && node->nodeType == snInitList )
  755. {
  756. asCTypeInfo ti;
  757. ti.Set(gvar->datatype);
  758. ti.isVariable = false;
  759. ti.isTemporary = false;
  760. ti.stackOffset = (short)gvar->index;
  761. ti.isLValue = true;
  762. CompileInitList(&ti, node, &ctx.bc);
  763. node = node->next;
  764. }
  765. else if( node )
  766. {
  767. // Compile the right hand expression
  768. asSExprContext expr(engine);
  769. int r = CompileAssignment(node, &expr); if( r < 0 ) return r;
  770. // Assign the value to the variable
  771. if( gvar->datatype.IsPrimitive() )
  772. {
  773. if( gvar->datatype.IsReadOnly() && expr.type.isConstant )
  774. {
  775. ImplicitConversion(&expr, gvar->datatype, node, asIC_IMPLICIT_CONV);
  776. gvar->isPureConstant = true;
  777. gvar->constantValue = expr.type.qwordValue;
  778. }
  779. asSExprContext lctx(engine);
  780. lctx.type.Set(gvar->datatype);
  781. lctx.type.dataType.MakeReference(true);
  782. lctx.type.dataType.MakeReadOnly(false);
  783. lctx.type.isLValue = true;
  784. // If it is an enum value that is being compiled, then
  785. // we skip this, as the bytecode won't be used anyway
  786. if( !gvar->isEnumValue )
  787. lctx.bc.InstrPTR(asBC_LDG, engine->globalProperties[gvar->index]->GetAddressOfValue());
  788. DoAssignment(&ctx, &lctx, &expr, node, node, ttAssignment, node);
  789. }
  790. else
  791. {
  792. // TODO: optimize: Here we should look for the best matching constructor, instead of
  793. // just the copy constructor. Only if no appropriate constructor is
  794. // available should the assignment operator be used.
  795. if( (!gvar->datatype.IsObjectHandle() || gvar->datatype.GetObjectType()->flags & asOBJ_ASHANDLE) )
  796. {
  797. // Call the default constructor to have a valid object for the assignment
  798. CallDefaultConstructor(gvar->datatype, gvar->index, true, &ctx.bc, gvar->idNode, true);
  799. }
  800. asSExprContext lexpr(engine);
  801. lexpr.type.Set(gvar->datatype);
  802. lexpr.type.dataType.MakeReference(true);
  803. lexpr.type.dataType.MakeReadOnly(false);
  804. lexpr.type.stackOffset = -1;
  805. lexpr.type.isLValue = true;
  806. if( gvar->datatype.IsObjectHandle() )
  807. lexpr.type.isExplicitHandle = true;
  808. lexpr.bc.InstrPTR(asBC_PGA, engine->globalProperties[gvar->index]->GetAddressOfValue());
  809. // If left expression resolves into a registered type
  810. // check if the assignment operator is overloaded, and check
  811. // the type of the right hand expression. If none is found
  812. // the default action is a direct copy if it is the same type
  813. // and a simple assignment.
  814. bool assigned = false;
  815. if( lexpr.type.dataType.IsObject() && (!lexpr.type.isExplicitHandle || (lexpr.type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE)) )
  816. {
  817. assigned = CompileOverloadedDualOperator(node, &lexpr, &expr, &ctx);
  818. if( assigned )
  819. {
  820. // Pop the resulting value
  821. ctx.bc.Pop(ctx.type.dataType.GetSizeOnStackDWords());
  822. // Release the argument
  823. ProcessDeferredParams(&ctx);
  824. }
  825. }
  826. if( !assigned )
  827. {
  828. PrepareForAssignment(&lexpr.type.dataType, &expr, node, false);
  829. // If the expression is constant and the variable also is constant
  830. // then mark the variable as pure constant. This will allow the compiler
  831. // to optimize expressions with this variable.
  832. if( gvar->datatype.IsReadOnly() && expr.type.isConstant )
  833. {
  834. gvar->isPureConstant = true;
  835. gvar->constantValue = expr.type.qwordValue;
  836. }
  837. // Add expression code to bytecode
  838. MergeExprBytecode(&ctx, &expr);
  839. // Add byte code for storing value of expression in variable
  840. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[gvar->index]->GetAddressOfValue());
  841. PerformAssignment(&lexpr.type, &expr.type, &ctx.bc, node);
  842. // Release temporary variables used by expression
  843. ReleaseTemporaryVariable(expr.type, &ctx.bc);
  844. ctx.bc.Pop(expr.type.dataType.GetSizeOnStackDWords());
  845. }
  846. }
  847. }
  848. else if( gvar->datatype.IsObject() && (!gvar->datatype.IsObjectHandle() || gvar->datatype.GetObjectType()->flags & asOBJ_ASHANDLE) )
  849. {
  850. // Call the default constructor in case no explicit initialization is given
  851. CallDefaultConstructor(gvar->datatype, gvar->index, true, &ctx.bc, gvar->idNode, true);
  852. }
  853. // Concatenate the bytecode
  854. int varSize = GetVariableOffset((int)variableAllocations.GetLength()) - 1;
  855. // Add information on the line number for the global variable
  856. size_t pos = 0;
  857. if( gvar->idNode )
  858. pos = gvar->idNode->tokenPos;
  859. else if( gvar->nextNode )
  860. pos = gvar->nextNode->tokenPos;
  861. LineInstr(&byteCode, pos);
  862. // We need to push zeroes on the stack to guarantee
  863. // that temporary object handles are clear
  864. int n;
  865. for( n = 0; n < varSize; n++ )
  866. byteCode.InstrINT(asBC_PshC4, 0);
  867. byteCode.AddCode(&ctx.bc);
  868. // Deallocate variables in this block, in reverse order
  869. for( n = (int)variables->variables.GetLength() - 1; n >= 0; --n )
  870. {
  871. sVariable *v = variables->variables[n];
  872. // Call variable destructors here, for variables not yet destroyed
  873. CallDestructor(v->type, v->stackOffset, v->onHeap, &byteCode);
  874. DeallocateVariable(v->stackOffset);
  875. }
  876. if( hasCompileErrors ) return -1;
  877. // At this point there should be no variables allocated
  878. asASSERT(variableAllocations.GetLength() == freeVariables.GetLength());
  879. // Remove the variable scope again
  880. RemoveVariableScope();
  881. byteCode.Ret(0);
  882. FinalizeFunction();
  883. #ifdef AS_DEBUG
  884. // DEBUG: output byte code
  885. byteCode.DebugOutput(("___init_" + gvar->name + ".txt").AddressOf(), engine, outFunc);
  886. #endif
  887. return 0;
  888. }
  889. void asCCompiler::FinalizeFunction()
  890. {
  891. asUINT n;
  892. // Tell the bytecode which variables are temporary
  893. for( n = 0; n < variableIsTemporary.GetLength(); n++ )
  894. {
  895. if( variableIsTemporary[n] )
  896. byteCode.DefineTemporaryVariable(GetVariableOffset(n));
  897. }
  898. // Finalize the bytecode
  899. byteCode.Finalize();
  900. byteCode.ExtractObjectVariableInfo(outFunc);
  901. // Compile the list of object variables for the exception handler
  902. for( n = 0; n < variableAllocations.GetLength(); n++ )
  903. {
  904. if( variableAllocations[n].IsObject() && !variableAllocations[n].IsReference() )
  905. {
  906. outFunc->objVariableTypes.PushLast(variableAllocations[n].GetObjectType());
  907. outFunc->objVariablePos.PushLast(GetVariableOffset(n));
  908. outFunc->objVariableIsOnHeap.PushLast(variableIsOnHeap[n]);
  909. }
  910. }
  911. // Copy byte code to the function
  912. outFunc->byteCode.SetLength(byteCode.GetSize());
  913. byteCode.Output(outFunc->byteCode.AddressOf());
  914. outFunc->AddReferences();
  915. outFunc->stackNeeded = byteCode.largestStackUsed;
  916. outFunc->lineNumbers = byteCode.lineNumbers;
  917. }
  918. void asCCompiler::PrepareArgument(asCDataType *paramType, asSExprContext *ctx, asCScriptNode *node, bool isFunction, int refType, bool isMakingCopy)
  919. {
  920. asCDataType param = *paramType;
  921. if( paramType->GetTokenType() == ttQuestion )
  922. {
  923. // Since the function is expecting a var type ?, then we don't want to convert the argument to anything else
  924. param = ctx->type.dataType;
  925. param.MakeHandle(ctx->type.isExplicitHandle);
  926. param.MakeReference(paramType->IsReference());
  927. param.MakeReadOnly(paramType->IsReadOnly());
  928. }
  929. else
  930. param = *paramType;
  931. asCDataType dt = param;
  932. // Need to protect arguments by reference
  933. if( isFunction && dt.IsReference() )
  934. {
  935. if( paramType->GetTokenType() == ttQuestion )
  936. {
  937. asCByteCode tmpBC(engine);
  938. // Place the type id on the stack as a hidden parameter
  939. tmpBC.InstrDWORD(asBC_TYPEID, engine->GetTypeIdFromDataType(param));
  940. // Insert the code before the expression code
  941. tmpBC.AddCode(&ctx->bc);
  942. ctx->bc.AddCode(&tmpBC);
  943. }
  944. // Allocate a temporary variable of the same type as the argument
  945. dt.MakeReference(false);
  946. dt.MakeReadOnly(false);
  947. int offset;
  948. if( refType == 1 ) // &in
  949. {
  950. ProcessPropertyGetAccessor(ctx, node);
  951. // If the reference is const, then it is not necessary to make a copy if the value already is a variable
  952. // Even if the same variable is passed in another argument as non-const then there is no problem
  953. if( dt.IsPrimitive() || dt.IsNullHandle() )
  954. {
  955. IsVariableInitialized(&ctx->type, node);
  956. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  957. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV, true);
  958. if( !(param.IsReadOnly() && ctx->type.isVariable) )
  959. ConvertToTempVariable(ctx);
  960. PushVariableOnStack(ctx, true);
  961. ctx->type.dataType.MakeReadOnly(param.IsReadOnly());
  962. }
  963. else
  964. {
  965. IsVariableInitialized(&ctx->type, node);
  966. if( !isMakingCopy )
  967. {
  968. ImplicitConversion(ctx, param, node, asIC_IMPLICIT_CONV, true);
  969. if( !ctx->type.dataType.IsEqualExceptRef(param) )
  970. {
  971. asCString str;
  972. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format().AddressOf(), param.Format().AddressOf());
  973. Error(str.AddressOf(), node);
  974. ctx->type.Set(param);
  975. }
  976. }
  977. // If the argument already is a temporary
  978. // variable we don't need to allocate another
  979. // If the parameter is read-only and the object already is a local
  980. // variable then it is not necessary to make a copy either
  981. if( !ctx->type.isTemporary && !(param.IsReadOnly() && ctx->type.isVariable) && !isMakingCopy )
  982. {
  983. // Make sure the variable is not used in the expression
  984. offset = AllocateVariableNotIn(dt, true, false, ctx);
  985. // TODO: copy: Use copy constructor if available. See PrepareTemporaryObject()
  986. // Allocate and construct the temporary object
  987. asCByteCode tmpBC(engine);
  988. CallDefaultConstructor(dt, offset, IsVariableOnHeap(offset), &tmpBC, node);
  989. // Insert the code before the expression code
  990. tmpBC.AddCode(&ctx->bc);
  991. ctx->bc.AddCode(&tmpBC);
  992. // Assign the evaluated expression to the temporary variable
  993. PrepareForAssignment(&dt, ctx, node, true);
  994. dt.MakeReference(IsVariableOnHeap(offset));
  995. asCTypeInfo type;
  996. type.Set(dt);
  997. type.isTemporary = true;
  998. type.stackOffset = (short)offset;
  999. if( dt.IsObjectHandle() )
  1000. type.isExplicitHandle = true;
  1001. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  1002. PerformAssignment(&type, &ctx->type, &ctx->bc, node);
  1003. ctx->bc.Pop(ctx->type.dataType.GetSizeOnStackDWords());
  1004. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  1005. ctx->type = type;
  1006. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  1007. if( dt.IsObject() && !dt.IsObjectHandle() )
  1008. ctx->bc.Instr(asBC_RDSPTR);
  1009. if( paramType->IsReadOnly() )
  1010. ctx->type.dataType.MakeReadOnly(true);
  1011. }
  1012. else if( isMakingCopy )
  1013. {
  1014. // We must guarantee that the address to the value is on the stack
  1015. if( ctx->type.dataType.IsObject() &&
  1016. !ctx->type.dataType.IsObjectHandle() &&
  1017. ctx->type.dataType.IsReference() )
  1018. Dereference(ctx, true);
  1019. }
  1020. }
  1021. }
  1022. else if( refType == 2 ) // &out
  1023. {
  1024. // Make sure the variable is not used in the expression
  1025. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1026. if( dt.IsPrimitive() )
  1027. {
  1028. ctx->type.SetVariable(dt, offset, true);
  1029. PushVariableOnStack(ctx, true);
  1030. }
  1031. else
  1032. {
  1033. // Allocate and construct the temporary object
  1034. asCByteCode tmpBC(engine);
  1035. CallDefaultConstructor(dt, offset, IsVariableOnHeap(offset), &tmpBC, node);
  1036. // Insert the code before the expression code
  1037. tmpBC.AddCode(&ctx->bc);
  1038. ctx->bc.AddCode(&tmpBC);
  1039. dt.MakeReference((!dt.IsObject() || dt.IsObjectHandle()));
  1040. asCTypeInfo type;
  1041. type.Set(dt);
  1042. type.isTemporary = true;
  1043. type.stackOffset = (short)offset;
  1044. ctx->type = type;
  1045. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  1046. if( dt.IsObject() && !dt.IsObjectHandle() )
  1047. ctx->bc.Instr(asBC_RDSPTR);
  1048. }
  1049. // After the function returns the temporary variable will
  1050. // be assigned to the expression, if it is a valid lvalue
  1051. }
  1052. else if( refType == asTM_INOUTREF )
  1053. {
  1054. ProcessPropertyGetAccessor(ctx, node);
  1055. // Literal constants cannot be passed to inout ref arguments
  1056. if( !ctx->type.isVariable && ctx->type.isConstant )
  1057. {
  1058. Error(TXT_NOT_VALID_REFERENCE, node);
  1059. }
  1060. // Only objects that support object handles
  1061. // can be guaranteed to be safe. Local variables are
  1062. // already safe, so there is no need to add an extra
  1063. // references
  1064. if( !engine->ep.allowUnsafeReferences &&
  1065. !ctx->type.isVariable &&
  1066. ctx->type.dataType.IsObject() &&
  1067. !ctx->type.dataType.IsObjectHandle() &&
  1068. ctx->type.dataType.GetBehaviour()->addref &&
  1069. ctx->type.dataType.GetBehaviour()->release )
  1070. {
  1071. // Store a handle to the object as local variable
  1072. asSExprContext tmp(engine);
  1073. asCDataType dt = ctx->type.dataType;
  1074. dt.MakeHandle(true);
  1075. dt.MakeReference(false);
  1076. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1077. // Copy the handle
  1078. if( !ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsReference() )
  1079. ctx->bc.Instr(asBC_RDSPTR);
  1080. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1081. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetObjectType());
  1082. ctx->bc.Pop(AS_PTR_SIZE);
  1083. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1084. dt.MakeHandle(false);
  1085. dt.MakeReference(true);
  1086. // Release previous temporary variable stored in the context (if any)
  1087. if( ctx->type.isTemporary )
  1088. {
  1089. ReleaseTemporaryVariable(ctx->type.stackOffset, &ctx->bc);
  1090. }
  1091. ctx->type.SetVariable(dt, offset, true);
  1092. }
  1093. // Make sure the reference to the value is on the stack
  1094. // For objects, the reference needs to be dereferenced so the pointer on the stack is to the actual object
  1095. // For handles, the reference shouldn't be changed because the pointer on the stack should be to the handle
  1096. if( ctx->type.dataType.IsObject() && ctx->type.dataType.IsReference() && !paramType->IsObjectHandle() )
  1097. Dereference(ctx, true);
  1098. else if( ctx->type.isVariable && !ctx->type.dataType.IsObject() )
  1099. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  1100. else if( ctx->type.dataType.IsPrimitive() )
  1101. ctx->bc.Instr(asBC_PshRPtr);
  1102. }
  1103. }
  1104. else
  1105. {
  1106. ProcessPropertyGetAccessor(ctx, node);
  1107. if( dt.IsPrimitive() )
  1108. {
  1109. IsVariableInitialized(&ctx->type, node);
  1110. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  1111. // Implicitly convert primitives to the parameter type
  1112. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV);
  1113. if( ctx->type.isVariable )
  1114. {
  1115. PushVariableOnStack(ctx, dt.IsReference());
  1116. }
  1117. else if( ctx->type.isConstant )
  1118. {
  1119. ConvertToVariable(ctx);
  1120. PushVariableOnStack(ctx, dt.IsReference());
  1121. }
  1122. }
  1123. else
  1124. {
  1125. IsVariableInitialized(&ctx->type, node);
  1126. // Implicitly convert primitives to the parameter type
  1127. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV);
  1128. // Was the conversion successful?
  1129. if( !ctx->type.dataType.IsEqualExceptRef(dt) )
  1130. {
  1131. asCString str;
  1132. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format().AddressOf(), dt.Format().AddressOf());
  1133. Error(str.AddressOf(), node);
  1134. ctx->type.Set(dt);
  1135. }
  1136. if( dt.IsObjectHandle() )
  1137. ctx->type.isExplicitHandle = true;
  1138. if( dt.IsObject() )
  1139. {
  1140. if( !dt.IsReference() )
  1141. {
  1142. // Objects passed by value must be placed in temporary variables
  1143. // so that they are guaranteed to not be referenced anywhere else.
  1144. // The object must also be allocated on the heap, as the memory will
  1145. // be deleted by in as_callfunc_xxx.
  1146. // TODO: value on stack: How can we avoid this unnecessary allocation?
  1147. PrepareTemporaryObject(node, ctx, true);
  1148. // The implicit conversion shouldn't convert the object to
  1149. // non-reference yet. It will be dereferenced just before the call.
  1150. // Otherwise the object might be missed by the exception handler.
  1151. dt.MakeReference(true);
  1152. }
  1153. else
  1154. {
  1155. // An object passed by reference should place the pointer to
  1156. // the object on the stack.
  1157. dt.MakeReference(false);
  1158. }
  1159. }
  1160. }
  1161. }
  1162. // Don't put any pointer on the stack yet
  1163. if( param.IsReference() || param.IsObject() )
  1164. {
  1165. // &inout parameter may leave the reference on the stack already
  1166. if( refType != 3 )
  1167. {
  1168. asASSERT( ctx->type.isVariable || ctx->type.isTemporary || isMakingCopy );
  1169. if( ctx->type.isVariable || ctx->type.isTemporary )
  1170. {
  1171. ctx->bc.Pop(AS_PTR_SIZE);
  1172. ctx->bc.InstrSHORT(asBC_VAR, ctx->type.stackOffset);
  1173. ProcessDeferredParams(ctx);
  1174. }
  1175. }
  1176. }
  1177. }
  1178. void asCCompiler::PrepareFunctionCall(int funcId, asCByteCode *bc, asCArray<asSExprContext *> &args)
  1179. {
  1180. // When a match has been found, compile the final byte code using correct parameter types
  1181. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  1182. // If the function being called is the opAssign or copy constructor for the same type
  1183. // as the argument, then we should avoid making temporary copy of the argument
  1184. bool makingCopy = false;
  1185. if( descr->parameterTypes.GetLength() == 1 &&
  1186. descr->parameterTypes[0].IsEqualExceptRefAndConst(args[0]->type.dataType) &&
  1187. ((descr->name == "opAssign" && descr->objectType && descr->objectType == args[0]->type.dataType.GetObjectType()) ||
  1188. (args[0]->type.dataType.GetObjectType() && descr->name == args[0]->type.dataType.GetObjectType()->name)) )
  1189. makingCopy = true;
  1190. // Add code for arguments
  1191. asSExprContext e(engine);
  1192. for( int n = (int)args.GetLength()-1; n >= 0; n-- )
  1193. {
  1194. // Make sure PrepareArgument doesn't use any variable that is already
  1195. // being used by any of the following argument expressions
  1196. int l = reservedVariables.GetLength();
  1197. for( int m = n-1; m >= 0; m-- )
  1198. args[m]->bc.GetVarsUsed(reservedVariables);
  1199. PrepareArgument2(&e, args[n], &descr->parameterTypes[n], true, descr->inOutFlags[n], makingCopy);
  1200. reservedVariables.SetLength(l);
  1201. }
  1202. bc->AddCode(&e.bc);
  1203. }
  1204. void asCCompiler::MoveArgsToStack(int funcId, asCByteCode *bc, asCArray<asSExprContext *> &args, bool addOneToOffset)
  1205. {
  1206. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  1207. int offset = 0;
  1208. if( addOneToOffset )
  1209. offset += AS_PTR_SIZE;
  1210. #ifndef AS_OLD
  1211. // The address of where the return value should be stored is push on top of the arguments
  1212. if( descr->DoesReturnOnStack() )
  1213. offset += AS_PTR_SIZE;
  1214. #endif
  1215. // If the function being called is the opAssign or copy constructor for the same type
  1216. // as the argument, then we should avoid making temporary copy of the argument
  1217. bool makingCopy = false;
  1218. if( descr->parameterTypes.GetLength() == 1 &&
  1219. descr->parameterTypes[0].IsEqualExceptRefAndConst(args[0]->type.dataType) &&
  1220. ((descr->name == "opAssign" && descr->objectType && descr->objectType == args[0]->type.dataType.GetObjectType()) ||
  1221. (args[0]->type.dataType.GetObjectType() && descr->name == args[0]->type.dataType.GetObjectType()->name)) )
  1222. makingCopy = true;
  1223. // Move the objects that are sent by value to the stack just before the call
  1224. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  1225. {
  1226. if( descr->parameterTypes[n].IsReference() )
  1227. {
  1228. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() )
  1229. {
  1230. if( descr->inOutFlags[n] != asTM_INOUTREF )
  1231. {
  1232. asASSERT( args[n]->type.isVariable || args[n]->type.isTemporary || makingCopy );
  1233. if( (args[n]->type.isVariable || args[n]->type.isTemporary) )
  1234. {
  1235. if( !IsVariableOnHeap(args[n]->type.stackOffset) )
  1236. // TODO: optimize: Actually the reference can be pushed on the stack directly
  1237. // as the value allocated on the stack is guaranteed to be safe
  1238. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1239. else
  1240. bc->InstrWORD(asBC_GETOBJREF, (asWORD)offset);
  1241. }
  1242. }
  1243. if( args[n]->type.dataType.IsObjectHandle() )
  1244. bc->InstrWORD(asBC_ChkNullS, (asWORD)offset);
  1245. }
  1246. else if( descr->inOutFlags[n] != asTM_INOUTREF )
  1247. {
  1248. if( descr->parameterTypes[n].GetTokenType() == ttQuestion &&
  1249. args[n]->type.dataType.IsObject() && !args[n]->type.dataType.IsObjectHandle() )
  1250. {
  1251. // Send the object as a reference to the object,
  1252. // and not to the variable holding the object
  1253. if( !IsVariableOnHeap(args[n]->type.stackOffset) )
  1254. // TODO: optimize: Actually the reference can be pushed on the stack directly
  1255. // as the value allocated on the stack is guaranteed to be safe
  1256. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1257. else
  1258. bc->InstrWORD(asBC_GETOBJREF, (asWORD)offset);
  1259. }
  1260. else
  1261. {
  1262. if( args[n]->type.dataType.GetObjectType() &&
  1263. (args[n]->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE) &&
  1264. args[n]->type.isVariable &&
  1265. IsVariableOnHeap(args[n]->type.stackOffset) )
  1266. bc->InstrWORD(asBC_GETOBJREF, (asWORD)offset);
  1267. else
  1268. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1269. }
  1270. }
  1271. }
  1272. else if( descr->parameterTypes[n].IsObject() )
  1273. {
  1274. // TODO: value on stack: What can we do to avoid this unnecessary allocation?
  1275. // The object must be allocated on the heap, because this memory will be deleted in as_callfunc_xxx
  1276. asASSERT(IsVariableOnHeap(args[n]->type.stackOffset));
  1277. bc->InstrWORD(asBC_GETOBJ, (asWORD)offset);
  1278. // The temporary variable must not be freed as it will no longer hold an object
  1279. DeallocateVariable(args[n]->type.stackOffset);
  1280. args[n]->type.isTemporary = false;
  1281. }
  1282. offset += descr->parameterTypes[n].GetSizeOnStackDWords();
  1283. }
  1284. }
  1285. int asCCompiler::CompileArgumentList(asCScriptNode *node, asCArray<asSExprContext*> &args)
  1286. {
  1287. asASSERT(node->nodeType == snArgList);
  1288. // Count arguments
  1289. asCScriptNode *arg = node->firstChild;
  1290. int argCount = 0;
  1291. while( arg )
  1292. {
  1293. argCount++;
  1294. arg = arg->next;
  1295. }
  1296. // Prepare the arrays
  1297. args.SetLength(argCount);
  1298. int n;
  1299. for( n = 0; n < argCount; n++ )
  1300. args[n] = 0;
  1301. n = argCount-1;
  1302. // Compile the arguments in reverse order (as they will be pushed on the stack)
  1303. bool anyErrors = false;
  1304. arg = node->lastChild;
  1305. while( arg )
  1306. {
  1307. asSExprContext expr(engine);
  1308. int r = CompileAssignment(arg, &expr);
  1309. if( r < 0 ) anyErrors = true;
  1310. args[n] = asNEW(asSExprContext)(engine);
  1311. MergeExprBytecodeAndType(args[n], &expr);
  1312. n--;
  1313. arg = arg->prev;
  1314. }
  1315. return anyErrors ? -1 : 0;
  1316. }
  1317. int asCCompiler::CompileDefaultArgs(asCScriptNode *node, asCArray<asSExprContext*> &args, asCScriptFunction *func)
  1318. {
  1319. bool anyErrors = false;
  1320. asCArray<int> varsUsed;
  1321. int explicitArgs = (int)args.GetLength();
  1322. for( int p = 0; p < explicitArgs; p++ )
  1323. args[p]->bc.GetVarsUsed(varsUsed);
  1324. // Compile the arguments in reverse order (as they will be pushed on the stack)
  1325. args.SetLength(func->parameterTypes.GetLength());
  1326. for( asUINT c = explicitArgs; c < args.GetLength(); c++ )
  1327. args[c] = 0;
  1328. for( int n = (int)func->parameterTypes.GetLength() - 1; n >= explicitArgs; n-- )
  1329. {
  1330. if( func->defaultArgs[n] == 0 ) { anyErrors = true; continue; }
  1331. // Parse the default arg string
  1332. asCParser parser(builder);
  1333. asCScriptCode code;
  1334. code.SetCode("default arg", func->defaultArgs[n]->AddressOf(), false);
  1335. int r = parser.ParseExpression(&code);
  1336. if( r < 0 ) { anyErrors = true; continue; }
  1337. asCScriptNode *arg = parser.GetScriptNode();
  1338. // Temporarily set the script code to the default arg expression
  1339. asCScriptCode *origScript = script;
  1340. script = &code;
  1341. // Don't allow the expression to access local variables
  1342. // TODO: namespace: The default arg should see the symbols declared in the same scope as the function
  1343. isCompilingDefaultArg = true;
  1344. asSExprContext expr(engine);
  1345. r = CompileExpression(arg, &expr);
  1346. isCompilingDefaultArg = false;
  1347. script = origScript;
  1348. if( r < 0 )
  1349. {
  1350. asCString msg;
  1351. msg.Format(TXT_FAILED_TO_COMPILE_DEF_ARG_d_IN_FUNC_s, n, func->GetDeclaration());
  1352. Error(msg.AddressOf(), node);
  1353. anyErrors = true;
  1354. continue;
  1355. }
  1356. args[n] = asNEW(asSExprContext)(engine);
  1357. MergeExprBytecodeAndType(args[n], &expr);
  1358. // Make sure the default arg expression doesn't end up
  1359. // with a variable that is used in a previous expression
  1360. if( args[n]->type.isVariable )
  1361. {
  1362. int offset = args[n]->type.stackOffset;
  1363. if( varsUsed.Exists(offset) )
  1364. {
  1365. // Release the current temporary variable
  1366. ReleaseTemporaryVariable(args[n]->type, 0);
  1367. asCDataType dt = args[n]->type.dataType;
  1368. dt.MakeReference(false);
  1369. int newOffset = AllocateVariable(dt, true, IsVariableOnHeap(offset));
  1370. asASSERT( IsVariableOnHeap(offset) == IsVariableOnHeap(newOffset) );
  1371. args[n]->bc.ExchangeVar(offset, newOffset);
  1372. args[n]->type.stackOffset = (short)newOffset;
  1373. args[n]->type.isTemporary = true;
  1374. args[n]->type.isVariable = true;
  1375. }
  1376. }
  1377. }
  1378. return anyErrors ? -1 : 0;
  1379. }
  1380. void asCCompiler::MatchFunctions(asCArray<int> &funcs, asCArray<asSExprContext*> &args, asCScriptNode *node, const char *name, asCObjectType *objectType, bool isConstMethod, bool silent, bool allowObjectConstruct, const asCString &scope)
  1381. {
  1382. asCArray<int> origFuncs = funcs; // Keep the original list for error message
  1383. asUINT n;
  1384. if( funcs.GetLength() > 0 )
  1385. {
  1386. // Check the number of parameters in the found functions
  1387. for( n = 0; n < funcs.GetLength(); ++n )
  1388. {
  1389. asCScriptFunction *desc = builder->GetFunctionDescription(funcs[n]);
  1390. if( desc->parameterTypes.GetLength() != args.GetLength() )
  1391. {
  1392. bool noMatch = true;
  1393. if( args.GetLength() < desc->parameterTypes.GetLength() )
  1394. {
  1395. // Count the number of default args
  1396. asUINT defaultArgs = 0;
  1397. for( asUINT d = 0; d < desc->defaultArgs.GetLength(); d++ )
  1398. if( desc->defaultArgs[d] )
  1399. defaultArgs++;
  1400. if( args.GetLength() >= desc->parameterTypes.GetLength() - defaultArgs )
  1401. noMatch = false;
  1402. }
  1403. if( noMatch )
  1404. {
  1405. // remove it from the list
  1406. if( n == funcs.GetLength()-1 )
  1407. funcs.PopLast();
  1408. else
  1409. funcs[n] = funcs.PopLast();
  1410. n--;
  1411. }
  1412. }
  1413. }
  1414. // Match functions with the parameters, and discard those that do not match
  1415. asCArray<int> matchingFuncs = funcs;
  1416. for( n = 0; n < args.GetLength(); ++n )
  1417. {
  1418. asCArray<int> tempFuncs;
  1419. MatchArgument(funcs, tempFuncs, &args[n]->type, n, allowObjectConstruct);
  1420. // Intersect the found functions with the list of matching functions
  1421. for( asUINT f = 0; f < matchingFuncs.GetLength(); f++ )
  1422. {
  1423. asUINT c;
  1424. for( c = 0; c < tempFuncs.GetLength(); c++ )
  1425. {
  1426. if( matchingFuncs[f] == tempFuncs[c] )
  1427. break;
  1428. }
  1429. // Was the function a match?
  1430. if( c == tempFuncs.GetLength() )
  1431. {
  1432. // No, remove it from the list
  1433. if( f == matchingFuncs.GetLength()-1 )
  1434. matchingFuncs.PopLast();
  1435. else
  1436. matchingFuncs[f] = matchingFuncs.PopLast();
  1437. f--;
  1438. }
  1439. }
  1440. }
  1441. funcs = matchingFuncs;
  1442. }
  1443. if( !isConstMethod )
  1444. FilterConst(funcs);
  1445. if( funcs.GetLength() != 1 && !silent )
  1446. {
  1447. // Build a readable string of the function with parameter types
  1448. asCString str;
  1449. if( scope != "" )
  1450. {
  1451. if( scope == "::" )
  1452. str = scope;
  1453. else
  1454. str = scope + "::";
  1455. }
  1456. str += name;
  1457. str += "(";
  1458. if( args.GetLength() )
  1459. str += args[0]->type.dataType.Format();
  1460. for( n = 1; n < args.GetLength(); n++ )
  1461. str += ", " + args[n]->type.dataType.Format();
  1462. str += ")";
  1463. if( isConstMethod )
  1464. str += " const";
  1465. if( objectType && scope == "" )
  1466. str = objectType->name + "::" + str;
  1467. if( funcs.GetLength() == 0 )
  1468. {
  1469. str.Format(TXT_NO_MATCHING_SIGNATURES_TO_s, str.AddressOf());
  1470. Error(str.AddressOf(), node);
  1471. // Print the list of candidates
  1472. if( origFuncs.GetLength() > 0 )
  1473. {
  1474. int r = 0, c = 0;
  1475. asASSERT( node );
  1476. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1477. builder->WriteInfo(script->name.AddressOf(), TXT_CANDIDATES_ARE, r, c, false);
  1478. PrintMatchingFuncs(origFuncs, node);
  1479. }
  1480. }
  1481. else
  1482. {
  1483. str.Format(TXT_MULTIPLE_MATCHING_SIGNATURES_TO_s, str.AddressOf());
  1484. Error(str.AddressOf(), node);
  1485. PrintMatchingFuncs(funcs, node);
  1486. }
  1487. }
  1488. }
  1489. void asCCompiler::CompileDeclaration(asCScriptNode *decl, asCByteCode *bc)
  1490. {
  1491. // Get the data type
  1492. asCDataType type = builder->CreateDataTypeFromNode(decl->firstChild, script);
  1493. // Declare all variables in this declaration
  1494. asCScriptNode *node = decl->firstChild->next;
  1495. while( node )
  1496. {
  1497. // Is the type allowed?
  1498. if( !type.CanBeInstanciated() )
  1499. {
  1500. asCString str;
  1501. // TODO: Change to "'type' cannot be declared as variable"
  1502. str.Format(TXT_DATA_TYPE_CANT_BE_s, type.Format().AddressOf());
  1503. Error(str.AddressOf(), node);
  1504. // Use int instead to avoid further problems
  1505. type = asCDataType::CreatePrimitive(ttInt, false);
  1506. }
  1507. // A shared object may not declare variables of non-shared types
  1508. if( outFunc->objectType && outFunc->objectType->IsShared() )
  1509. {
  1510. asCObjectType *ot = type.GetObjectType();
  1511. if( ot && !ot->IsShared() )
  1512. {
  1513. asCString msg;
  1514. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, ot->name.AddressOf());
  1515. Error(msg.AddressOf(), decl);
  1516. }
  1517. }
  1518. // Get the name of the identifier
  1519. asCString name(&script->code[node->tokenPos], node->tokenLength);
  1520. // Verify that the name isn't used by a dynamic data type
  1521. if( engine->GetObjectType(name.AddressOf()) != 0 )
  1522. {
  1523. asCString str;
  1524. str.Format(TXT_ILLEGAL_VARIABLE_NAME_s, name.AddressOf());
  1525. Error(str.AddressOf(), node);
  1526. }
  1527. int offset = AllocateVariable(type, false);
  1528. if( variables->DeclareVariable(name.AddressOf(), type, offset, IsVariableOnHeap(offset)) < 0 )
  1529. {
  1530. asCString str;
  1531. str.Format(TXT_s_ALREADY_DECLARED, name.AddressOf());
  1532. Error(str.AddressOf(), node);
  1533. // Don't continue after this error, as it will just
  1534. // lead to more errors that are likely false
  1535. return;
  1536. }
  1537. // Add marker that the variable has been declared
  1538. bc->VarDecl((int)outFunc->variables.GetLength());
  1539. outFunc->AddVariable(name, type, offset);
  1540. // Keep the node for the variable decl
  1541. asCScriptNode *varNode = node;
  1542. node = node->next;
  1543. if( node && node->nodeType == snArgList )
  1544. {
  1545. // Make sure that it is a registered type, and that is isn't a pointer
  1546. if( type.GetObjectType() == 0 || type.IsObjectHandle() )
  1547. {
  1548. Error(TXT_MUST_BE_OBJECT, node);
  1549. }
  1550. else
  1551. {
  1552. // Compile the arguments
  1553. asCArray<asSExprContext *> args;
  1554. if( CompileArgumentList(node, args) >= 0 )
  1555. {
  1556. // Find all constructors
  1557. asCArray<int> funcs;
  1558. asSTypeBehaviour *beh = type.GetBehaviour();
  1559. if( beh )
  1560. {
  1561. if( type.GetObjectType()->flags & asOBJ_REF )
  1562. funcs = beh->factories;
  1563. else
  1564. funcs = beh->constructors;
  1565. }
  1566. asCString str = type.Format();
  1567. MatchFunctions(funcs, args, node, str.AddressOf());
  1568. if( funcs.GetLength() == 1 )
  1569. {
  1570. int r = asSUCCESS;
  1571. // Add the default values for arguments not explicitly supplied
  1572. asCScriptFunction *func = (funcs[0] & 0xFFFF0000) == 0 ? engine->scriptFunctions[funcs[0]] : 0;
  1573. if( func && args.GetLength() < (asUINT)func->GetParamCount() )
  1574. r = CompileDefaultArgs(node, args, func);
  1575. if( r == asSUCCESS )
  1576. {
  1577. sVariable *v = variables->GetVariable(name.AddressOf());
  1578. asSExprContext ctx(engine);
  1579. if( v->type.GetObjectType() && (v->type.GetObjectType()->flags & asOBJ_REF) )
  1580. {
  1581. MakeFunctionCall(&ctx, funcs[0], 0, args, node, true, v->stackOffset);
  1582. // Pop the reference left by the function call
  1583. ctx.bc.Pop(AS_PTR_SIZE);
  1584. }
  1585. else
  1586. {
  1587. // When the object is allocated on the heap, the address where the
  1588. // reference will be stored must be pushed on the stack before the
  1589. // arguments. This reference on the stack is safe, even if the script
  1590. // is suspended during the evaluation of the arguments.
  1591. if( v->onHeap )
  1592. ctx.bc.InstrSHORT(asBC_PSF, (short)v->stackOffset);
  1593. PrepareFunctionCall(funcs[0], &ctx.bc, args);
  1594. MoveArgsToStack(funcs[0], &ctx.bc, args, false);
  1595. // When the object is allocated on the stack, the address to the
  1596. // object is pushed on the stack after the arguments as the object pointer
  1597. if( !v->onHeap )
  1598. ctx.bc.InstrSHORT(asBC_PSF, (short)v->stackOffset);
  1599. PerformFunctionCall(funcs[0], &ctx, v->onHeap, &args, type.GetObjectType());
  1600. // TODO: value on stack: This probably has to be done in PerformFunctionCall
  1601. // Mark the object as initialized
  1602. ctx.bc.ObjInfo(v->stackOffset, asOBJ_INIT);
  1603. }
  1604. bc->AddCode(&ctx.bc);
  1605. }
  1606. }
  1607. }
  1608. // Cleanup
  1609. for( asUINT n = 0; n < args.GetLength(); n++ )
  1610. if( args[n] )
  1611. {
  1612. asDELETE(args[n],asSExprContext);
  1613. }
  1614. }
  1615. node = node->next;
  1616. }
  1617. else if( node && node->nodeType == snInitList )
  1618. {
  1619. sVariable *v = variables->GetVariable(name.AddressOf());
  1620. asCTypeInfo ti;
  1621. ti.Set(type);
  1622. ti.isVariable = true;
  1623. ti.isTemporary = false;
  1624. ti.stackOffset = (short)v->stackOffset;
  1625. ti.isLValue = true;
  1626. CompileInitList(&ti, node, bc);
  1627. node = node->next;
  1628. }
  1629. else if( node && node->nodeType == snAssignment )
  1630. {
  1631. asSExprContext ctx(engine);
  1632. // TODO: copy: Here we should look for the best matching constructor, instead of
  1633. // just the copy constructor. Only if no appropriate constructor is
  1634. // available should the assignment operator be used.
  1635. // Call the default constructor here
  1636. CallDefaultConstructor(type, offset, IsVariableOnHeap(offset), &ctx.bc, varNode);
  1637. // Compile the expression
  1638. asSExprContext expr(engine);
  1639. int r = CompileAssignment(node, &expr);
  1640. if( r >= 0 )
  1641. {
  1642. if( type.IsPrimitive() )
  1643. {
  1644. if( type.IsReadOnly() && expr.type.isConstant )
  1645. {
  1646. ImplicitConversion(&expr, type, node, asIC_IMPLICIT_CONV);
  1647. sVariable *v = variables->GetVariable(name.AddressOf());
  1648. v->isPureConstant = true;
  1649. v->constantValue = expr.type.qwordValue;
  1650. }
  1651. asSExprContext lctx(engine);
  1652. lctx.type.SetVariable(type, offset, false);
  1653. lctx.type.dataType.MakeReadOnly(false);
  1654. lctx.type.isLValue = true;
  1655. DoAssignment(&ctx, &lctx, &expr, node, node, ttAssignment, node);
  1656. ProcessDeferredParams(&ctx);
  1657. }
  1658. else
  1659. {
  1660. // TODO: optimize: We can use a copy constructor here
  1661. sVariable *v = variables->GetVariable(name.AddressOf());
  1662. asSExprContext lexpr(engine);
  1663. lexpr.type.Set(type);
  1664. lexpr.type.dataType.MakeReference(v->onHeap);
  1665. // Allow initialization of constant variables
  1666. lexpr.type.dataType.MakeReadOnly(false);
  1667. if( type.IsObjectHandle() )
  1668. lexpr.type.isExplicitHandle = true;
  1669. lexpr.bc.InstrSHORT(asBC_PSF, (short)v->stackOffset);
  1670. lexpr.type.stackOffset = (short)v->stackOffset;
  1671. lexpr.type.isVariable = true;
  1672. lexpr.type.isLValue = true;
  1673. // If left expression resolves into a registered type
  1674. // check if the assignment operator is overloaded, and check
  1675. // the type of the right hand expression. If none is found
  1676. // the default action is a direct copy if it is the same type
  1677. // and a simple assignment.
  1678. bool assigned = false;
  1679. if( lexpr.type.dataType.IsObject() && (!lexpr.type.isExplicitHandle || (lexpr.type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE)) )
  1680. {
  1681. assigned = CompileOverloadedDualOperator(node, &lexpr, &expr, &ctx);
  1682. if( assigned )
  1683. {
  1684. // Pop the resulting value
  1685. ctx.bc.Pop(ctx.type.dataType.GetSizeOnStackDWords());
  1686. // Release the argument
  1687. ProcessDeferredParams(&ctx);
  1688. // Release temporary variable that may be allocated by the overloaded operator
  1689. ReleaseTemporaryVariable(ctx.type, &ctx.bc);
  1690. }
  1691. }
  1692. if( !assigned )
  1693. {
  1694. PrepareForAssignment(&lexpr.type.dataType, &expr, node, false);
  1695. // If the expression is constant and the variable also is constant
  1696. // then mark the variable as pure constant. This will allow the compiler
  1697. // to optimize expressions with this variable.
  1698. if( v->type.IsReadOnly() && expr.type.isConstant )
  1699. {
  1700. v->isPureConstant = true;
  1701. v->constantValue = expr.type.qwordValue;
  1702. }
  1703. // Add expression code to bytecode
  1704. MergeExprBytecode(&ctx, &expr);
  1705. // Add byte code for storing value of expression in variable
  1706. ctx.bc.AddCode(&lexpr.bc);
  1707. lexpr.type.stackOffset = (short)v->stackOffset;
  1708. PerformAssignment(&lexpr.type, &expr.type, &ctx.bc, node->prev);
  1709. // Release temporary variables used by expression
  1710. ReleaseTemporaryVariable(expr.type, &ctx.bc);
  1711. ctx.bc.Pop(expr.type.dataType.GetSizeOnStackDWords());
  1712. ProcessDeferredParams(&ctx);
  1713. }
  1714. }
  1715. }
  1716. node = node->next;
  1717. bc->AddCode(&ctx.bc);
  1718. // TODO: Can't this leave deferred output params without being compiled?
  1719. }
  1720. else
  1721. {
  1722. // Call the default constructor here if no explicit initialization is done
  1723. CallDefaultConstructor(type, offset, IsVariableOnHeap(offset), bc, varNode);
  1724. }
  1725. }
  1726. }
  1727. void asCCompiler::CompileInitList(asCTypeInfo *var, asCScriptNode *node, asCByteCode *bc)
  1728. {
  1729. // Check if the type supports initialization lists
  1730. if( var->dataType.GetObjectType() == 0 ||
  1731. var->dataType.GetBehaviour()->listFactory == 0 ||
  1732. var->dataType.IsObjectHandle() )
  1733. {
  1734. asCString str;
  1735. str.Format(TXT_INIT_LIST_CANNOT_BE_USED_WITH_s, var->dataType.Format().AddressOf());
  1736. Error(str.AddressOf(), node);
  1737. return;
  1738. }
  1739. // Count the number of elements and initialize the array with the correct size
  1740. int countElements = 0;
  1741. asCScriptNode *el = node->firstChild;
  1742. while( el )
  1743. {
  1744. countElements++;
  1745. el = el->next;
  1746. }
  1747. // Construct the array with the size elements
  1748. // TODO: value on stack: This needs to support value types on the stack as well
  1749. // Find the list factory
  1750. // TODO: initlist: Add support for value types as well
  1751. int funcId = var->dataType.GetBehaviour()->listFactory;
  1752. asCArray<asSExprContext *> args;
  1753. asSExprContext arg1(engine);
  1754. arg1.bc.InstrDWORD(asBC_PshC4, countElements);
  1755. arg1.type.Set(asCDataType::CreatePrimitive(ttUInt, false));
  1756. args.PushLast(&arg1);
  1757. asSExprContext ctx(engine);
  1758. PrepareFunctionCall(funcId, &ctx.bc, args);
  1759. MoveArgsToStack(funcId, &ctx.bc, args, false);
  1760. if( var->isVariable )
  1761. {
  1762. // Call factory and store the handle in the given variable
  1763. PerformFunctionCall(funcId, &ctx, false, &args, 0, true, var->stackOffset);
  1764. ctx.bc.Pop(AS_PTR_SIZE);
  1765. }
  1766. else
  1767. {
  1768. PerformFunctionCall(funcId, &ctx, false, &args);
  1769. // Store the returned handle in the global variable
  1770. ctx.bc.Instr(asBC_RDSPTR);
  1771. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[var->stackOffset]->GetAddressOfValue());
  1772. ctx.bc.InstrPTR(asBC_REFCPY, var->dataType.GetObjectType());
  1773. ctx.bc.Pop(AS_PTR_SIZE);
  1774. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  1775. }
  1776. bc->AddCode(&ctx.bc);
  1777. // TODO: initlist: Should we have a special indexing operator for this? How can we support
  1778. // initialization lists with different types for different elements? Maybe
  1779. // by using the variable arguments the initialization can be done with one
  1780. // call, passing all the elements as arguments. The registered function can
  1781. // then traverse them however it wants.
  1782. // Find the indexing operator that is not read-only that will be used for all elements
  1783. asCDataType retType;
  1784. retType = var->dataType.GetSubType();
  1785. retType.MakeReference(true);
  1786. retType.MakeReadOnly(false);
  1787. funcId = 0;
  1788. for( asUINT n = 0; n < var->dataType.GetObjectType()->methods.GetLength(); n++ )
  1789. {
  1790. asCScriptFunction *desc = builder->GetFunctionDescription(var->dataType.GetObjectType()->methods[n]);
  1791. if( !desc->isReadOnly &&
  1792. desc->parameterTypes.GetLength() == 1 &&
  1793. (desc->parameterTypes[0] == asCDataType::CreatePrimitive(ttUInt, false) ||
  1794. desc->parameterTypes[0] == asCDataType::CreatePrimitive(ttInt, false)) &&
  1795. desc->returnType == retType &&
  1796. desc->name == "opIndex" )
  1797. {
  1798. funcId = var->dataType.GetObjectType()->methods[n];
  1799. break;
  1800. }
  1801. }
  1802. if( funcId == 0 )
  1803. {
  1804. Error(TXT_NO_APPROPRIATE_INDEX_OPERATOR, node);
  1805. return;
  1806. }
  1807. asUINT index = 0;
  1808. el = node->firstChild;
  1809. while( el )
  1810. {
  1811. if( el->nodeType == snAssignment || el->nodeType == snInitList )
  1812. {
  1813. asSExprContext lctx(engine);
  1814. asSExprContext rctx(engine);
  1815. if( el->nodeType == snAssignment )
  1816. {
  1817. // Compile the assignment expression
  1818. CompileAssignment(el, &rctx);
  1819. }
  1820. else if( el->nodeType == snInitList )
  1821. {
  1822. int offset = AllocateVariable(var->dataType.GetSubType(), true);
  1823. rctx.type.Set(var->dataType.GetSubType());
  1824. rctx.type.isVariable = true;
  1825. rctx.type.isTemporary = true;
  1826. rctx.type.stackOffset = (short)offset;
  1827. CompileInitList(&rctx.type, el, &rctx.bc);
  1828. // Put the object on the stack
  1829. rctx.bc.InstrSHORT(asBC_PSF, rctx.type.stackOffset);
  1830. // It is a reference that we place on the stack
  1831. rctx.type.dataType.MakeReference(true);
  1832. }
  1833. // Compile the lvalue
  1834. lctx.bc.InstrDWORD(asBC_PshC4, index);
  1835. if( var->isVariable )
  1836. lctx.bc.InstrSHORT(asBC_PSF, var->stackOffset);
  1837. else
  1838. lctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[var->stackOffset]->GetAddressOfValue());
  1839. lctx.bc.Instr(asBC_RDSPTR);
  1840. lctx.bc.Call(asBC_CALLSYS, funcId, 1+AS_PTR_SIZE);
  1841. if( !var->dataType.GetSubType().IsPrimitive() )
  1842. lctx.bc.Instr(asBC_PshRPtr);
  1843. lctx.type.Set(var->dataType.GetSubType());
  1844. if( !lctx.type.dataType.IsObject() || lctx.type.dataType.IsObjectHandle() )
  1845. lctx.type.dataType.MakeReference(true);
  1846. // If the element type is handles, then we're expected to do handle assignments
  1847. if( lctx.type.dataType.IsObjectHandle() )
  1848. lctx.type.isExplicitHandle = true;
  1849. lctx.type.isLValue = true;
  1850. asSExprContext ctx(engine);
  1851. DoAssignment(&ctx, &lctx, &rctx, el, el, ttAssignment, el);
  1852. if( !lctx.type.dataType.IsPrimitive() )
  1853. ctx.bc.Pop(AS_PTR_SIZE);
  1854. // Release temporary variables used by expression
  1855. ReleaseTemporaryVariable(ctx.type, &ctx.bc);
  1856. ProcessDeferredParams(&ctx);
  1857. bc->AddCode(&ctx.bc);
  1858. }
  1859. el = el->next;
  1860. index++;
  1861. }
  1862. }
  1863. void asCCompiler::CompileStatement(asCScriptNode *statement, bool *hasReturn, asCByteCode *bc)
  1864. {
  1865. *hasReturn = false;
  1866. if( statement->nodeType == snStatementBlock )
  1867. CompileStatementBlock(statement, true, hasReturn, bc);
  1868. else if( statement->nodeType == snIf )
  1869. CompileIfStatement(statement, hasReturn, bc);
  1870. else if( statement->nodeType == snFor )
  1871. CompileForStatement(statement, bc);
  1872. else if( statement->nodeType == snWhile )
  1873. CompileWhileStatement(statement, bc);
  1874. else if( statement->nodeType == snDoWhile )
  1875. CompileDoWhileStatement(statement, bc);
  1876. else if( statement->nodeType == snExpressionStatement )
  1877. CompileExpressionStatement(statement, bc);
  1878. else if( statement->nodeType == snBreak )
  1879. CompileBreakStatement(statement, bc);
  1880. else if( statement->nodeType == snContinue )
  1881. CompileContinueStatement(statement, bc);
  1882. else if( statement->nodeType == snSwitch )
  1883. CompileSwitchStatement(statement, hasReturn, bc);
  1884. else if( statement->nodeType == snReturn )
  1885. {
  1886. CompileReturnStatement(statement, bc);
  1887. *hasReturn = true;
  1888. }
  1889. }
  1890. void asCCompiler::CompileSwitchStatement(asCScriptNode *snode, bool *, asCByteCode *bc)
  1891. {
  1892. // TODO: inheritance: Must guarantee that all options in the switch case call a constructor, or that none call it.
  1893. // Reserve label for break statements
  1894. int breakLabel = nextLabel++;
  1895. breakLabels.PushLast(breakLabel);
  1896. // Add a variable scope that will be used by CompileBreak
  1897. // to know where to stop deallocating variables
  1898. AddVariableScope(true, false);
  1899. //---------------------------
  1900. // Compile the switch expression
  1901. //-------------------------------
  1902. // Compile the switch expression
  1903. asSExprContext expr(engine);
  1904. CompileAssignment(snode->firstChild, &expr);
  1905. // Verify that the expression is a primitive type
  1906. if( !expr.type.dataType.IsIntegerType() && !expr.type.dataType.IsUnsignedType() && !expr.type.dataType.IsEnumType() )
  1907. {
  1908. Error(TXT_SWITCH_MUST_BE_INTEGRAL, snode->firstChild);
  1909. return;
  1910. }
  1911. ProcessPropertyGetAccessor(&expr, snode);
  1912. // TODO: Need to support 64bit integers
  1913. // Convert the expression to a 32bit variable
  1914. asCDataType to;
  1915. if( expr.type.dataType.IsIntegerType() || expr.type.dataType.IsEnumType() )
  1916. to.SetTokenType(ttInt);
  1917. else if( expr.type.dataType.IsUnsignedType() )
  1918. to.SetTokenType(ttUInt);
  1919. // Make sure the value is in a variable
  1920. if( expr.type.dataType.IsReference() )
  1921. ConvertToVariable(&expr);
  1922. ImplicitConversion(&expr, to, snode->firstChild, asIC_IMPLICIT_CONV, true);
  1923. ConvertToVariable(&expr);
  1924. int offset = expr.type.stackOffset;
  1925. ProcessDeferredParams(&expr);
  1926. //-------------------------------
  1927. // Determine case values and labels
  1928. //--------------------------------
  1929. // Remember the first label so that we can later pass the
  1930. // correct label to each CompileCase()
  1931. int firstCaseLabel = nextLabel;
  1932. int defaultLabel = 0;
  1933. asCArray<int> caseValues;
  1934. asCArray<int> caseLabels;
  1935. // Compile all case comparisons and make them jump to the right label
  1936. asCScriptNode *cnode = snode->firstChild->next;
  1937. while( cnode )
  1938. {
  1939. // Each case should have a constant expression
  1940. if( cnode->firstChild && cnode->firstChild->nodeType == snExpression )
  1941. {
  1942. // Compile expression
  1943. asSExprContext c(engine);
  1944. CompileExpression(cnode->firstChild, &c);
  1945. // Verify that the result is a constant
  1946. if( !c.type.isConstant )
  1947. Error(TXT_SWITCH_CASE_MUST_BE_CONSTANT, cnode->firstChild);
  1948. // Verify that the result is an integral number
  1949. if( !c.type.dataType.IsIntegerType() && !c.type.dataType.IsUnsignedType() && !c.type.dataType.IsEnumType() )
  1950. Error(TXT_SWITCH_MUST_BE_INTEGRAL, cnode->firstChild);
  1951. ImplicitConversion(&c, to, cnode->firstChild, asIC_IMPLICIT_CONV, true);
  1952. // Has this case been declared already?
  1953. if( caseValues.IndexOf(c.type.intValue) >= 0 )
  1954. {
  1955. Error(TXT_DUPLICATE_SWITCH_CASE, cnode->firstChild);
  1956. }
  1957. // TODO: Optimize: We can insert the numbers sorted already
  1958. // Store constant for later use
  1959. caseValues.PushLast(c.type.intValue);
  1960. // Reserve label for this case
  1961. caseLabels.PushLast(nextLabel++);
  1962. }
  1963. else
  1964. {
  1965. // Is default the last case?
  1966. if( cnode->next )
  1967. {
  1968. Error(TXT_DEFAULT_MUST_BE_LAST, cnode);
  1969. break;
  1970. }
  1971. // Reserve label for this case
  1972. defaultLabel = nextLabel++;
  1973. }
  1974. cnode = cnode->next;
  1975. }
  1976. // check for empty switch
  1977. if (caseValues.GetLength() == 0)
  1978. {
  1979. Error(TXT_EMPTY_SWITCH, snode);
  1980. return;
  1981. }
  1982. if( defaultLabel == 0 )
  1983. defaultLabel = breakLabel;
  1984. //---------------------------------
  1985. // Output the optimized case comparisons
  1986. // with jumps to the case code
  1987. //------------------------------------
  1988. // Sort the case values by increasing value. Do the sort together with the labels
  1989. // A simple bubble sort is sufficient since we don't expect a huge number of values
  1990. for( asUINT fwd = 1; fwd < caseValues.GetLength(); fwd++ )
  1991. {
  1992. for( int bck = fwd - 1; bck >= 0; bck-- )
  1993. {
  1994. int bckp = bck + 1;
  1995. if( caseValues[bck] > caseValues[bckp] )
  1996. {
  1997. // Swap the values in both arrays
  1998. int swap = caseValues[bckp];
  1999. caseValues[bckp] = caseValues[bck];
  2000. caseValues[bck] = swap;
  2001. swap = caseLabels[bckp];
  2002. caseLabels[bckp] = caseLabels[bck];
  2003. caseLabels[bck] = swap;
  2004. }
  2005. else
  2006. break;
  2007. }
  2008. }
  2009. // Find ranges of consecutive numbers
  2010. asCArray<int> ranges;
  2011. ranges.PushLast(0);
  2012. asUINT n;
  2013. for( n = 1; n < caseValues.GetLength(); ++n )
  2014. {
  2015. // We can join numbers that are less than 5 numbers
  2016. // apart since the output code will still be smaller
  2017. if( caseValues[n] > caseValues[n-1] + 5 )
  2018. ranges.PushLast(n);
  2019. }
  2020. // If the value is larger than the largest case value, jump to default
  2021. int tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  2022. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[caseValues.GetLength()-1]);
  2023. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  2024. expr.bc.InstrDWORD(asBC_JP, defaultLabel);
  2025. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  2026. // TODO: optimize: We could possibly optimize this even more by doing a
  2027. // binary search instead of a linear search through the ranges
  2028. // For each range
  2029. int range;
  2030. for( range = 0; range < (int)ranges.GetLength(); range++ )
  2031. {
  2032. // Find the largest value in this range
  2033. int maxRange = caseValues[ranges[range]];
  2034. int index = ranges[range];
  2035. for( ; (index < (int)caseValues.GetLength()) && (caseValues[index] <= maxRange + 5); index++ )
  2036. maxRange = caseValues[index];
  2037. // If there are only 2 numbers then it is better to compare them directly
  2038. if( index - ranges[range] > 2 )
  2039. {
  2040. // If the value is smaller than the smallest case value in the range, jump to default
  2041. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  2042. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[ranges[range]]);
  2043. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  2044. expr.bc.InstrDWORD(asBC_JS, defaultLabel);
  2045. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  2046. int nextRangeLabel = nextLabel++;
  2047. // If this is the last range we don't have to make this test
  2048. if( range < (int)ranges.GetLength() - 1 )
  2049. {
  2050. // If the value is larger than the largest case value in the range, jump to the next range
  2051. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  2052. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, maxRange);
  2053. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  2054. expr.bc.InstrDWORD(asBC_JP, nextRangeLabel);
  2055. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  2056. }
  2057. // Jump forward according to the value
  2058. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  2059. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[ranges[range]]);
  2060. expr.bc.InstrW_W_W(asBC_SUBi, tmpOffset, offset, tmpOffset);
  2061. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  2062. expr.bc.JmpP(tmpOffset, maxRange - caseValues[ranges[range]]);
  2063. // Add the list of jumps to the correct labels (any holes, jump to default)
  2064. index = ranges[range];
  2065. for( int n = caseValues[index]; n <= maxRange; n++ )
  2066. {
  2067. if( caseValues[index] == n )
  2068. expr.bc.InstrINT(asBC_JMP, caseLabels[index++]);
  2069. else
  2070. expr.bc.InstrINT(asBC_JMP, defaultLabel);
  2071. }
  2072. expr.bc.Label((short)nextRangeLabel);
  2073. }
  2074. else
  2075. {
  2076. // Simply make a comparison with each value
  2077. int n;
  2078. for( n = ranges[range]; n < index; ++n )
  2079. {
  2080. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  2081. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[n]);
  2082. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  2083. expr.bc.InstrDWORD(asBC_JZ, caseLabels[n]);
  2084. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  2085. }
  2086. }
  2087. }
  2088. // Catch any value that falls trough
  2089. expr.bc.InstrINT(asBC_JMP, defaultLabel);
  2090. // Release the temporary variable previously stored
  2091. ReleaseTemporaryVariable(expr.type, &expr.bc);
  2092. //----------------------------------
  2093. // Output case implementations
  2094. //----------------------------------
  2095. // Compile case implementations, each one with the label before it
  2096. cnode = snode->firstChild->next;
  2097. while( cnode )
  2098. {
  2099. // Each case should have a constant expression
  2100. if( cnode->firstChild && cnode->firstChild->nodeType == snExpression )
  2101. {
  2102. expr.bc.Label((short)firstCaseLabel++);
  2103. CompileCase(cnode->firstChild->next, &expr.bc);
  2104. }
  2105. else
  2106. {
  2107. expr.bc.Label((short)defaultLabel);
  2108. // Is default the last case?
  2109. if( cnode->next )
  2110. {
  2111. // We've already reported this error
  2112. break;
  2113. }
  2114. CompileCase(cnode->firstChild, &expr.bc);
  2115. }
  2116. cnode = cnode->next;
  2117. }
  2118. //--------------------------------
  2119. bc->AddCode(&expr.bc);
  2120. // Add break label
  2121. bc->Label((short)breakLabel);
  2122. breakLabels.PopLast();
  2123. RemoveVariableScope();
  2124. }
  2125. void asCCompiler::CompileCase(asCScriptNode *node, asCByteCode *bc)
  2126. {
  2127. bool isFinished = false;
  2128. bool hasReturn = false;
  2129. while( node )
  2130. {
  2131. if( hasReturn || isFinished )
  2132. {
  2133. Warning(TXT_UNREACHABLE_CODE, node);
  2134. break;
  2135. }
  2136. if( node->nodeType == snBreak || node->nodeType == snContinue )
  2137. isFinished = true;
  2138. asCByteCode statement(engine);
  2139. if( node->nodeType == snDeclaration )
  2140. {
  2141. Error(TXT_DECL_IN_SWITCH, node);
  2142. // Compile it anyway to avoid further compiler errors
  2143. CompileDeclaration(node, &statement);
  2144. }
  2145. else
  2146. CompileStatement(node, &hasReturn, &statement);
  2147. LineInstr(bc, node->tokenPos);
  2148. bc->AddCode(&statement);
  2149. if( !hasCompileErrors )
  2150. asASSERT( tempVariables.GetLength() == 0 );
  2151. node = node->next;
  2152. }
  2153. }
  2154. void asCCompiler::CompileIfStatement(asCScriptNode *inode, bool *hasReturn, asCByteCode *bc)
  2155. {
  2156. // We will use one label for the if statement
  2157. // and possibly another for the else statement
  2158. int afterLabel = nextLabel++;
  2159. // Compile the expression
  2160. asSExprContext expr(engine);
  2161. CompileAssignment(inode->firstChild, &expr);
  2162. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  2163. {
  2164. Error(TXT_EXPR_MUST_BE_BOOL, inode->firstChild);
  2165. expr.type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), 1);
  2166. }
  2167. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  2168. ProcessDeferredParams(&expr);
  2169. if( !expr.type.isConstant )
  2170. {
  2171. ProcessPropertyGetAccessor(&expr, inode);
  2172. ConvertToVariable(&expr);
  2173. // Add byte code from the expression
  2174. bc->AddCode(&expr.bc);
  2175. // Add a test
  2176. bc->InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  2177. bc->Instr(asBC_ClrHi);
  2178. bc->InstrDWORD(asBC_JZ, afterLabel);
  2179. ReleaseTemporaryVariable(expr.type, bc);
  2180. }
  2181. else if( expr.type.dwordValue == 0 )
  2182. {
  2183. // Jump to the else case
  2184. bc->InstrINT(asBC_JMP, afterLabel);
  2185. // TODO: Should we warn that the expression will always go to the else?
  2186. }
  2187. // Compile the if statement
  2188. bool origIsConstructorCalled = m_isConstructorCalled;
  2189. bool hasReturn1;
  2190. asCByteCode ifBC(engine);
  2191. CompileStatement(inode->firstChild->next, &hasReturn1, &ifBC);
  2192. // Add the byte code
  2193. LineInstr(bc, inode->firstChild->next->tokenPos);
  2194. bc->AddCode(&ifBC);
  2195. if( inode->firstChild->next->nodeType == snExpressionStatement && inode->firstChild->next->firstChild == 0 )
  2196. {
  2197. // Don't allow if( expr );
  2198. Error(TXT_IF_WITH_EMPTY_STATEMENT, inode->firstChild->next);
  2199. }
  2200. // If one of the statements call the constructor, the other must as well
  2201. // otherwise it is possible the constructor is never called
  2202. bool constructorCall1 = false;
  2203. bool constructorCall2 = false;
  2204. if( !origIsConstructorCalled && m_isConstructorCalled )
  2205. constructorCall1 = true;
  2206. // Do we have an else statement?
  2207. if( inode->firstChild->next != inode->lastChild )
  2208. {
  2209. // Reset the constructor called flag so the else statement can call the constructor too
  2210. m_isConstructorCalled = origIsConstructorCalled;
  2211. int afterElse = 0;
  2212. if( !hasReturn1 )
  2213. {
  2214. afterElse = nextLabel++;
  2215. // Add jump to after the else statement
  2216. bc->InstrINT(asBC_JMP, afterElse);
  2217. }
  2218. // Add label for the else statement
  2219. bc->Label((short)afterLabel);
  2220. bool hasReturn2;
  2221. asCByteCode elseBC(engine);
  2222. CompileStatement(inode->lastChild, &hasReturn2, &elseBC);
  2223. // Add byte code for the else statement
  2224. LineInstr(bc, inode->lastChild->tokenPos);
  2225. bc->AddCode(&elseBC);
  2226. if( inode->lastChild->nodeType == snExpressionStatement && inode->lastChild->firstChild == 0 )
  2227. {
  2228. // Don't allow if( expr ) {} else;
  2229. Error(TXT_ELSE_WITH_EMPTY_STATEMENT, inode->lastChild);
  2230. }
  2231. if( !hasReturn1 )
  2232. {
  2233. // Add label for the end of else statement
  2234. bc->Label((short)afterElse);
  2235. }
  2236. // The if statement only has return if both alternatives have
  2237. *hasReturn = hasReturn1 && hasReturn2;
  2238. if( !origIsConstructorCalled && m_isConstructorCalled )
  2239. constructorCall2 = true;
  2240. }
  2241. else
  2242. {
  2243. // Add label for the end of if statement
  2244. bc->Label((short)afterLabel);
  2245. *hasReturn = false;
  2246. }
  2247. // Make sure both or neither conditions call a constructor
  2248. if( (constructorCall1 && !constructorCall2) ||
  2249. (constructorCall2 && !constructorCall1) )
  2250. {
  2251. Error(TXT_BOTH_CONDITIONS_MUST_CALL_CONSTRUCTOR, inode);
  2252. }
  2253. m_isConstructorCalled = origIsConstructorCalled || constructorCall1 || constructorCall2;
  2254. }
  2255. void asCCompiler::CompileForStatement(asCScriptNode *fnode, asCByteCode *bc)
  2256. {
  2257. // TODO: optimize: We should be able to remove the static JMP to the beginning of the loop by rearranging the
  2258. // byte code a bit.
  2259. //
  2260. // init
  2261. // jump to before
  2262. // begin:
  2263. // statements
  2264. // continue:
  2265. // next
  2266. // before:
  2267. // condition
  2268. // if loop jump to begin
  2269. // break:
  2270. // Add a variable scope that will be used by CompileBreak/Continue to know where to stop deallocating variables
  2271. AddVariableScope(true, true);
  2272. // We will use three labels for the for loop
  2273. int beforeLabel = nextLabel++;
  2274. int afterLabel = nextLabel++;
  2275. int continueLabel = nextLabel++;
  2276. continueLabels.PushLast(continueLabel);
  2277. breakLabels.PushLast(afterLabel);
  2278. //---------------------------------------
  2279. // Compile the initialization statement
  2280. asCByteCode initBC(engine);
  2281. if( fnode->firstChild->nodeType == snDeclaration )
  2282. CompileDeclaration(fnode->firstChild, &initBC);
  2283. else
  2284. CompileExpressionStatement(fnode->firstChild, &initBC);
  2285. //-----------------------------------
  2286. // Compile the condition statement
  2287. asSExprContext expr(engine);
  2288. asCScriptNode *second = fnode->firstChild->next;
  2289. if( second->firstChild )
  2290. {
  2291. int r = CompileAssignment(second->firstChild, &expr);
  2292. if( r >= 0 )
  2293. {
  2294. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  2295. Error(TXT_EXPR_MUST_BE_BOOL, second);
  2296. else
  2297. {
  2298. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  2299. ProcessDeferredParams(&expr);
  2300. ProcessPropertyGetAccessor(&expr, second);
  2301. // If expression is false exit the loop
  2302. ConvertToVariable(&expr);
  2303. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  2304. expr.bc.Instr(asBC_ClrHi);
  2305. expr.bc.InstrDWORD(asBC_JZ, afterLabel);
  2306. ReleaseTemporaryVariable(expr.type, &expr.bc);
  2307. }
  2308. }
  2309. }
  2310. //---------------------------
  2311. // Compile the increment statement
  2312. asCByteCode nextBC(engine);
  2313. asCScriptNode *third = second->next;
  2314. if( third->nodeType == snExpressionStatement )
  2315. CompileExpressionStatement(third, &nextBC);
  2316. //------------------------------
  2317. // Compile loop statement
  2318. bool hasReturn;
  2319. asCByteCode forBC(engine);
  2320. CompileStatement(fnode->lastChild, &hasReturn, &forBC);
  2321. //-------------------------------
  2322. // Join the code pieces
  2323. bc->AddCode(&initBC);
  2324. bc->Label((short)beforeLabel);
  2325. // Add a suspend bytecode inside the loop to guarantee
  2326. // that the application can suspend the execution
  2327. bc->Instr(asBC_SUSPEND);
  2328. bc->InstrPTR(asBC_JitEntry, 0);
  2329. bc->AddCode(&expr.bc);
  2330. LineInstr(bc, fnode->lastChild->tokenPos);
  2331. bc->AddCode(&forBC);
  2332. bc->Label((short)continueLabel);
  2333. bc->AddCode(&nextBC);
  2334. bc->InstrINT(asBC_JMP, beforeLabel);
  2335. bc->Label((short)afterLabel);
  2336. continueLabels.PopLast();
  2337. breakLabels.PopLast();
  2338. // Deallocate variables in this block, in reverse order
  2339. for( int n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  2340. {
  2341. sVariable *v = variables->variables[n];
  2342. // Call variable destructors here, for variables not yet destroyed
  2343. CallDestructor(v->type, v->stackOffset, v->onHeap, bc);
  2344. // Don't deallocate function parameters
  2345. if( v->stackOffset > 0 )
  2346. DeallocateVariable(v->stackOffset);
  2347. }
  2348. RemoveVariableScope();
  2349. }
  2350. void asCCompiler::CompileWhileStatement(asCScriptNode *wnode, asCByteCode *bc)
  2351. {
  2352. // Add a variable scope that will be used by CompileBreak/Continue to know where to stop deallocating variables
  2353. AddVariableScope(true, true);
  2354. // We will use two labels for the while loop
  2355. int beforeLabel = nextLabel++;
  2356. int afterLabel = nextLabel++;
  2357. continueLabels.PushLast(beforeLabel);
  2358. breakLabels.PushLast(afterLabel);
  2359. // Add label before the expression
  2360. bc->Label((short)beforeLabel);
  2361. // Compile expression
  2362. asSExprContext expr(engine);
  2363. CompileAssignment(wnode->firstChild, &expr);
  2364. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  2365. Error(TXT_EXPR_MUST_BE_BOOL, wnode->firstChild);
  2366. else
  2367. {
  2368. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  2369. ProcessDeferredParams(&expr);
  2370. ProcessPropertyGetAccessor(&expr, wnode);
  2371. // Add byte code for the expression
  2372. ConvertToVariable(&expr);
  2373. bc->AddCode(&expr.bc);
  2374. // Jump to end of statement if expression is false
  2375. bc->InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  2376. bc->Instr(asBC_ClrHi);
  2377. bc->InstrDWORD(asBC_JZ, afterLabel);
  2378. ReleaseTemporaryVariable(expr.type, bc);
  2379. }
  2380. // Add a suspend bytecode inside the loop to guarantee
  2381. // that the application can suspend the execution
  2382. bc->Instr(asBC_SUSPEND);
  2383. bc->InstrPTR(asBC_JitEntry, 0);
  2384. // Compile statement
  2385. bool hasReturn;
  2386. asCByteCode whileBC(engine);
  2387. CompileStatement(wnode->lastChild, &hasReturn, &whileBC);
  2388. // Add byte code for the statement
  2389. LineInstr(bc, wnode->lastChild->tokenPos);
  2390. bc->AddCode(&whileBC);
  2391. // Jump to the expression
  2392. bc->InstrINT(asBC_JMP, beforeLabel);
  2393. // Add label after the statement
  2394. bc->Label((short)afterLabel);
  2395. continueLabels.PopLast();
  2396. breakLabels.PopLast();
  2397. RemoveVariableScope();
  2398. }
  2399. void asCCompiler::CompileDoWhileStatement(asCScriptNode *wnode, asCByteCode *bc)
  2400. {
  2401. // Add a variable scope that will be used by CompileBreak/Continue to know where to stop deallocating variables
  2402. AddVariableScope(true, true);
  2403. // We will use two labels for the while loop
  2404. int beforeLabel = nextLabel++;
  2405. int beforeTest = nextLabel++;
  2406. int afterLabel = nextLabel++;
  2407. continueLabels.PushLast(beforeTest);
  2408. breakLabels.PushLast(afterLabel);
  2409. // Add label before the statement
  2410. bc->Label((short)beforeLabel);
  2411. // Compile statement
  2412. bool hasReturn;
  2413. asCByteCode whileBC(engine);
  2414. CompileStatement(wnode->firstChild, &hasReturn, &whileBC);
  2415. // Add byte code for the statement
  2416. LineInstr(bc, wnode->firstChild->tokenPos);
  2417. bc->AddCode(&whileBC);
  2418. // Add label before the expression
  2419. bc->Label((short)beforeTest);
  2420. // Add a suspend bytecode inside the loop to guarantee
  2421. // that the application can suspend the execution
  2422. bc->Instr(asBC_SUSPEND);
  2423. bc->InstrPTR(asBC_JitEntry, 0);
  2424. // Add a line instruction
  2425. LineInstr(bc, wnode->lastChild->tokenPos);
  2426. // Compile expression
  2427. asSExprContext expr(engine);
  2428. CompileAssignment(wnode->lastChild, &expr);
  2429. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  2430. Error(TXT_EXPR_MUST_BE_BOOL, wnode->firstChild);
  2431. else
  2432. {
  2433. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  2434. ProcessDeferredParams(&expr);
  2435. ProcessPropertyGetAccessor(&expr, wnode);
  2436. // Add byte code for the expression
  2437. ConvertToVariable(&expr);
  2438. bc->AddCode(&expr.bc);
  2439. // Jump to next iteration if expression is true
  2440. bc->InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  2441. bc->Instr(asBC_ClrHi);
  2442. bc->InstrDWORD(asBC_JNZ, beforeLabel);
  2443. ReleaseTemporaryVariable(expr.type, bc);
  2444. }
  2445. // Add label after the statement
  2446. bc->Label((short)afterLabel);
  2447. continueLabels.PopLast();
  2448. breakLabels.PopLast();
  2449. RemoveVariableScope();
  2450. }
  2451. void asCCompiler::CompileBreakStatement(asCScriptNode *node, asCByteCode *bc)
  2452. {
  2453. if( breakLabels.GetLength() == 0 )
  2454. {
  2455. Error(TXT_INVALID_BREAK, node);
  2456. return;
  2457. }
  2458. // Add destructor calls for all variables that will go out of scope
  2459. // Put this clean up in a block to allow exception handler to understand them
  2460. bc->Block(true);
  2461. asCVariableScope *vs = variables;
  2462. while( !vs->isBreakScope )
  2463. {
  2464. for( int n = (int)vs->variables.GetLength() - 1; n >= 0; n-- )
  2465. CallDestructor(vs->variables[n]->type, vs->variables[n]->stackOffset, vs->variables[n]->onHeap, bc);
  2466. vs = vs->parent;
  2467. }
  2468. bc->Block(false);
  2469. bc->InstrINT(asBC_JMP, breakLabels[breakLabels.GetLength()-1]);
  2470. }
  2471. void asCCompiler::CompileContinueStatement(asCScriptNode *node, asCByteCode *bc)
  2472. {
  2473. if( continueLabels.GetLength() == 0 )
  2474. {
  2475. Error(TXT_INVALID_CONTINUE, node);
  2476. return;
  2477. }
  2478. // Add destructor calls for all variables that will go out of scope
  2479. // Put this clean up in a block to allow exception handler to understand them
  2480. bc->Block(true);
  2481. asCVariableScope *vs = variables;
  2482. while( !vs->isContinueScope )
  2483. {
  2484. for( int n = (int)vs->variables.GetLength() - 1; n >= 0; n-- )
  2485. CallDestructor(vs->variables[n]->type, vs->variables[n]->stackOffset, vs->variables[n]->onHeap, bc);
  2486. vs = vs->parent;
  2487. }
  2488. bc->Block(false);
  2489. bc->InstrINT(asBC_JMP, continueLabels[continueLabels.GetLength()-1]);
  2490. }
  2491. void asCCompiler::CompileExpressionStatement(asCScriptNode *enode, asCByteCode *bc)
  2492. {
  2493. if( enode->firstChild )
  2494. {
  2495. // Compile the expression
  2496. asSExprContext expr(engine);
  2497. CompileAssignment(enode->firstChild, &expr);
  2498. // Pop the value from the stack
  2499. if( !expr.type.dataType.IsPrimitive() )
  2500. expr.bc.Pop(expr.type.dataType.GetSizeOnStackDWords());
  2501. // Release temporary variables used by expression
  2502. ReleaseTemporaryVariable(expr.type, &expr.bc);
  2503. ProcessDeferredParams(&expr);
  2504. bc->AddCode(&expr.bc);
  2505. }
  2506. }
  2507. void asCCompiler::PrepareTemporaryObject(asCScriptNode *node, asSExprContext *ctx, bool forceOnHeap)
  2508. {
  2509. // If the object already is stored in temporary variable then nothing needs to be done
  2510. // Note, a type can be temporary without being a variable, in which case it is holding off
  2511. // on releasing a previously used object.
  2512. if( ctx->type.isTemporary && ctx->type.isVariable &&
  2513. !(forceOnHeap && !IsVariableOnHeap(ctx->type.stackOffset)) )
  2514. {
  2515. // If the temporary object is currently not a reference
  2516. // the expression needs to be reevaluated to a reference
  2517. if( !ctx->type.dataType.IsReference() )
  2518. {
  2519. ctx->bc.Pop(AS_PTR_SIZE);
  2520. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  2521. ctx->type.dataType.MakeReference(true);
  2522. }
  2523. return;
  2524. }
  2525. // Allocate temporary variable
  2526. asCDataType dt = ctx->type.dataType;
  2527. dt.MakeReference(false);
  2528. dt.MakeReadOnly(false);
  2529. int offset = AllocateVariable(dt, true, forceOnHeap);
  2530. // Objects stored on the stack are not considered references
  2531. dt.MakeReference(IsVariableOnHeap(offset));
  2532. asCTypeInfo lvalue;
  2533. lvalue.Set(dt);
  2534. lvalue.isTemporary = true;
  2535. lvalue.stackOffset = (short)offset;
  2536. lvalue.isVariable = true;
  2537. lvalue.isExplicitHandle = ctx->type.isExplicitHandle;
  2538. if( (!dt.IsObjectHandle() || (dt.GetObjectType() && (dt.GetObjectType()->flags & asOBJ_ASHANDLE))) &&
  2539. dt.GetObjectType() && (dt.GetBehaviour()->copyconstruct || dt.GetBehaviour()->copyfactory) )
  2540. {
  2541. PrepareForAssignment(&lvalue.dataType, ctx, node, true);
  2542. // Use the copy constructor/factory when available
  2543. CallCopyConstructor(dt, offset, IsVariableOnHeap(offset), &ctx->bc, ctx, node);
  2544. }
  2545. else
  2546. {
  2547. // Allocate and construct the temporary object
  2548. int r = CallDefaultConstructor(dt, offset, IsVariableOnHeap(offset), &ctx->bc, node);
  2549. if( r < 0 )
  2550. {
  2551. Error(TXT_FAILED_TO_CREATE_TEMP_OBJ, node);
  2552. }
  2553. else
  2554. {
  2555. // Assign the object to the temporary variable
  2556. PrepareForAssignment(&lvalue.dataType, ctx, node, true);
  2557. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  2558. r = PerformAssignment(&lvalue, &ctx->type, &ctx->bc, node);
  2559. if( r < 0 )
  2560. {
  2561. Error(TXT_FAILED_TO_CREATE_TEMP_OBJ, node);
  2562. }
  2563. // Pop the original reference
  2564. ctx->bc.Pop(AS_PTR_SIZE);
  2565. }
  2566. }
  2567. // If the expression was holding off on releasing a
  2568. // previously used object, we need to release it now
  2569. if( ctx->type.isTemporary )
  2570. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  2571. // Push the reference to the temporary variable on the stack
  2572. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  2573. lvalue.dataType.MakeReference(IsVariableOnHeap(offset));
  2574. ctx->type = lvalue;
  2575. }
  2576. void asCCompiler::CompileReturnStatement(asCScriptNode *rnode, asCByteCode *bc)
  2577. {
  2578. // Get return type and location
  2579. sVariable *v = variables->GetVariable("return");
  2580. // Basic validations
  2581. if( v->type.GetSizeOnStackDWords() > 0 && !rnode->firstChild )
  2582. {
  2583. Error(TXT_MUST_RETURN_VALUE, rnode);
  2584. return;
  2585. }
  2586. else if( v->type.GetSizeOnStackDWords() == 0 && rnode->firstChild )
  2587. {
  2588. Error(TXT_CANT_RETURN_VALUE, rnode);
  2589. return;
  2590. }
  2591. // Compile the expression
  2592. if( rnode->firstChild )
  2593. {
  2594. // Compile the expression
  2595. asSExprContext expr(engine);
  2596. int r = CompileAssignment(rnode->firstChild, &expr);
  2597. if( r < 0 ) return;
  2598. if( v->type.IsReference() )
  2599. {
  2600. // The expression that gives the reference must not use any of the
  2601. // variables that must be destroyed upon exit, because then it means
  2602. // reference will stay alive while the clean-up is done, which could
  2603. // potentially mean that the reference is invalidated by the clean-up.
  2604. //
  2605. // When the function is returning a reference, the clean-up of the
  2606. // variables must be done before the evaluation of the expression.
  2607. //
  2608. // A reference to a global variable, or a class member for class methods
  2609. // should be allowed to be returned.
  2610. if( !(expr.type.dataType.IsReference() ||
  2611. (expr.type.dataType.IsObject() && !expr.type.dataType.IsObjectHandle())) )
  2612. {
  2613. // Clean up the potential deferred parameters
  2614. ProcessDeferredParams(&expr);
  2615. Error(TXT_NOT_VALID_REFERENCE, rnode);
  2616. return;
  2617. }
  2618. // No references to local variables, temporary variables, or parameters
  2619. // are allowed to be returned, since they go out of scope when the function
  2620. // returns. Even reference parameters are disallowed, since it is not possible
  2621. // to know the scope of them. The exception is the 'this' pointer, which
  2622. // is treated by the compiler as a local variable, but isn't really so.
  2623. if( (expr.type.isVariable && !(expr.type.stackOffset == 0 && outFunc->objectType)) || expr.type.isTemporary )
  2624. {
  2625. // Clean up the potential deferred parameters
  2626. ProcessDeferredParams(&expr);
  2627. Error(TXT_CANNOT_RETURN_REF_TO_LOCAL, rnode);
  2628. return;
  2629. }
  2630. // The type must match exactly as we cannot convert
  2631. // the reference without loosing the original value
  2632. if( !(v->type == expr.type.dataType ||
  2633. (expr.type.dataType.IsObject() && !expr.type.dataType.IsObjectHandle() && v->type.IsEqualExceptRef(expr.type.dataType))) )
  2634. {
  2635. // Clean up the potential deferred parameters
  2636. ProcessDeferredParams(&expr);
  2637. asCString str;
  2638. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, expr.type.dataType.Format().AddressOf(), v->type.Format().AddressOf());
  2639. Error(str.AddressOf(), rnode);
  2640. return;
  2641. }
  2642. // The expression must not have any deferred expressions, because the evaluation
  2643. // of these cannot be done without keeping the reference which is not safe
  2644. if( expr.deferredParams.GetLength() )
  2645. {
  2646. // Clean up the potential deferred parameters
  2647. ProcessDeferredParams(&expr);
  2648. Error(TXT_REF_CANT_BE_RETURNED_DEFERRED_PARAM, rnode);
  2649. return;
  2650. }
  2651. // Make sure the expression isn't using any local variables that
  2652. // will need to be cleaned up before the function completes
  2653. asCArray<int> usedVars;
  2654. expr.bc.GetVarsUsed(usedVars);
  2655. for( asUINT n = 0; n < usedVars.GetLength(); n++ )
  2656. {
  2657. int var = GetVariableSlot(usedVars[n]);
  2658. if( var != -1 )
  2659. {
  2660. asCDataType dt = variableAllocations[var];
  2661. if( dt.IsObject() )
  2662. {
  2663. ProcessDeferredParams(&expr);
  2664. Error(TXT_REF_CANT_BE_RETURNED_LOCAL_VARS, rnode);
  2665. return;
  2666. }
  2667. }
  2668. }
  2669. // All objects in the function must be cleaned up before the expression
  2670. // is evaluated, otherwise there is a possibility that the cleanup will
  2671. // invalidate the reference.
  2672. // Destroy the local variables before loading
  2673. // the reference into the register. This will
  2674. // be done before the expression is evaluated.
  2675. DestroyVariables(bc);
  2676. // For primitives the reference is already in the register,
  2677. // but for non-primitives the reference is on the stack so we
  2678. // need to load it into the register
  2679. if( !expr.type.dataType.IsPrimitive() )
  2680. {
  2681. if( (!expr.type.dataType.IsObjectHandle() || (expr.type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE)) &&
  2682. expr.type.dataType.IsReference() )
  2683. expr.bc.Instr(asBC_RDSPTR);
  2684. expr.bc.Instr(asBC_PopRPtr);
  2685. }
  2686. // There are no temporaries to release so we're done
  2687. }
  2688. else // if( !v->type.IsReference() )
  2689. {
  2690. ProcessPropertyGetAccessor(&expr, rnode);
  2691. // Prepare the value for assignment
  2692. IsVariableInitialized(&expr.type, rnode->firstChild);
  2693. if( v->type.IsPrimitive() )
  2694. {
  2695. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  2696. // Implicitly convert the value to the return type
  2697. ImplicitConversion(&expr, v->type, rnode->firstChild, asIC_IMPLICIT_CONV);
  2698. // Verify that the conversion was successful
  2699. if( expr.type.dataType != v->type )
  2700. {
  2701. asCString str;
  2702. str.Format(TXT_NO_CONVERSION_s_TO_s, expr.type.dataType.Format().AddressOf(), v->type.Format().AddressOf());
  2703. Error(str.AddressOf(), rnode);
  2704. return;
  2705. }
  2706. else
  2707. {
  2708. ConvertToVariable(&expr);
  2709. // Clean up the local variables and process deferred parameters
  2710. DestroyVariables(&expr.bc);
  2711. ProcessDeferredParams(&expr);
  2712. ReleaseTemporaryVariable(expr.type, &expr.bc);
  2713. // Load the variable in the register
  2714. if( v->type.GetSizeOnStackDWords() == 1 )
  2715. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  2716. else
  2717. expr.bc.InstrSHORT(asBC_CpyVtoR8, expr.type.stackOffset);
  2718. }
  2719. }
  2720. else if( v->type.IsObject() )
  2721. {
  2722. #ifndef AS_OLD
  2723. // Value types are returned on the stack, in a location
  2724. // that has been reserved by the calling function.
  2725. if( outFunc->DoesReturnOnStack() )
  2726. {
  2727. // TODO: optimize: If the return type has a constructor that takes the type of the expression,
  2728. // it should be called directly instead of first converting the expression and
  2729. // then copy the value.
  2730. if( !v->type.IsEqualExceptRefAndConst(expr.type.dataType) )
  2731. {
  2732. ImplicitConversion(&expr, v->type, rnode->firstChild, asIC_IMPLICIT_CONV);
  2733. if( !v->type.IsEqualExceptRefAndConst(expr.type.dataType) )
  2734. {
  2735. asCString str;
  2736. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, expr.type.dataType.Format().AddressOf(), v->type.Format().AddressOf());
  2737. Error(str.AddressOf(), rnode->firstChild);
  2738. return;
  2739. }
  2740. }
  2741. int offset = outFunc->objectType ? -AS_PTR_SIZE : 0;
  2742. if( v->type.GetObjectType()->beh.copyconstruct )
  2743. {
  2744. PrepareForAssignment(&v->type, &expr, rnode->firstChild, false);
  2745. CallCopyConstructor(v->type, offset, false, &expr.bc, &expr, rnode->firstChild, false, true);
  2746. }
  2747. else
  2748. {
  2749. // If the copy constructor doesn't exist, then a manual assignment needs to be done instead.
  2750. CallDefaultConstructor(v->type, offset, false, &expr.bc, rnode->firstChild, false, true);
  2751. PrepareForAssignment(&v->type, &expr, rnode->firstChild, false);
  2752. expr.bc.InstrSHORT(asBC_PSF, (short)offset);
  2753. expr.bc.Instr(asBC_RDSPTR);
  2754. asSExprContext lexpr(engine);
  2755. lexpr.type.Set(v->type);
  2756. lexpr.type.isLValue = true;
  2757. PerformAssignment(&lexpr.type, &expr.type, &expr.bc, rnode->firstChild);
  2758. expr.bc.Pop(AS_PTR_SIZE);
  2759. // Release any temporary variable
  2760. ReleaseTemporaryVariable(expr.type, &expr.bc);
  2761. }
  2762. // Clean up the local variables and process deferred parameters
  2763. DestroyVariables(&expr.bc);
  2764. ProcessDeferredParams(&expr);
  2765. }
  2766. else
  2767. #endif
  2768. {
  2769. #ifndef AS_OLD
  2770. asASSERT( v->type.GetObjectType()->flags & asOBJ_REF );
  2771. #endif
  2772. // Prepare the expression to be loaded into the object
  2773. // register. This will place the reference in local variable
  2774. PrepareArgument(&v->type, &expr, rnode->firstChild, false, 0);
  2775. // Pop the reference to the temporary variable
  2776. expr.bc.Pop(AS_PTR_SIZE);
  2777. // Clean up the local variables and process deferred parameters
  2778. DestroyVariables(&expr.bc);
  2779. ProcessDeferredParams(&expr);
  2780. // Load the object pointer into the object register
  2781. // LOADOBJ also clears the address in the variable
  2782. expr.bc.InstrSHORT(asBC_LOADOBJ, expr.type.stackOffset);
  2783. // LOADOBJ cleared the address in the variable so the object will not be freed
  2784. // here, but the temporary variable must still be freed so the slot can be reused
  2785. // By releasing without the bytecode we do just that.
  2786. ReleaseTemporaryVariable(expr.type, 0);
  2787. }
  2788. }
  2789. }
  2790. bc->AddCode(&expr.bc);
  2791. }
  2792. else
  2793. {
  2794. // For functions that don't return anything
  2795. // we just detroy the local variables
  2796. DestroyVariables(bc);
  2797. }
  2798. // Jump to the end of the function
  2799. bc->InstrINT(asBC_JMP, 0);
  2800. }
  2801. void asCCompiler::DestroyVariables(asCByteCode *bc)
  2802. {
  2803. // Call destructor on all variables except for the function parameters
  2804. // Put the clean-up in a block to allow exception handler to understand this
  2805. bc->Block(true);
  2806. asCVariableScope *vs = variables;
  2807. while( vs )
  2808. {
  2809. for( int n = (int)vs->variables.GetLength() - 1; n >= 0; n-- )
  2810. if( vs->variables[n]->stackOffset > 0 )
  2811. CallDestructor(vs->variables[n]->type, vs->variables[n]->stackOffset, vs->variables[n]->onHeap, bc);
  2812. vs = vs->parent;
  2813. }
  2814. bc->Block(false);
  2815. }
  2816. void asCCompiler::AddVariableScope(bool isBreakScope, bool isContinueScope)
  2817. {
  2818. variables = asNEW(asCVariableScope)(variables);
  2819. variables->isBreakScope = isBreakScope;
  2820. variables->isContinueScope = isContinueScope;
  2821. }
  2822. void asCCompiler::RemoveVariableScope()
  2823. {
  2824. if( variables )
  2825. {
  2826. asCVariableScope *var = variables;
  2827. variables = variables->parent;
  2828. asDELETE(var,asCVariableScope);
  2829. }
  2830. }
  2831. void asCCompiler::Error(const char *msg, asCScriptNode *node)
  2832. {
  2833. asCString str;
  2834. int r = 0, c = 0;
  2835. asASSERT( node );
  2836. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2837. builder->WriteError(script->name.AddressOf(), msg, r, c);
  2838. hasCompileErrors = true;
  2839. }
  2840. void asCCompiler::Warning(const char *msg, asCScriptNode *node)
  2841. {
  2842. asCString str;
  2843. int r = 0, c = 0;
  2844. asASSERT( node );
  2845. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2846. builder->WriteWarning(script->name.AddressOf(), msg, r, c);
  2847. }
  2848. void asCCompiler::Information(const char *msg, asCScriptNode *node)
  2849. {
  2850. asCString str;
  2851. int r = 0, c = 0;
  2852. asASSERT( node );
  2853. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2854. builder->WriteInfo(script->name.AddressOf(), msg, r, c, false);
  2855. }
  2856. void asCCompiler::PrintMatchingFuncs(asCArray<int> &funcs, asCScriptNode *node)
  2857. {
  2858. int r = 0, c = 0;
  2859. asASSERT( node );
  2860. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2861. for( unsigned int n = 0; n < funcs.GetLength(); n++ )
  2862. {
  2863. asIScriptFunction *func = builder->GetFunctionDescription(funcs[n]);
  2864. builder->WriteInfo(script->name.AddressOf(), func->GetDeclaration(true), r, c, false);
  2865. }
  2866. }
  2867. int asCCompiler::AllocateVariableNotIn(const asCDataType &type, bool isTemporary, bool forceOnHeap, asSExprContext *ctx)
  2868. {
  2869. int l = reservedVariables.GetLength();
  2870. ctx->bc.GetVarsUsed(reservedVariables);
  2871. int var = AllocateVariable(type, isTemporary, forceOnHeap);
  2872. reservedVariables.SetLength(l);
  2873. return var;
  2874. }
  2875. int asCCompiler::AllocateVariable(const asCDataType &type, bool isTemporary, bool forceOnHeap)
  2876. {
  2877. asCDataType t(type);
  2878. if( t.IsPrimitive() && t.GetSizeOnStackDWords() == 1 )
  2879. t.SetTokenType(ttInt);
  2880. if( t.IsPrimitive() && t.GetSizeOnStackDWords() == 2 )
  2881. t.SetTokenType(ttDouble);
  2882. // Only null handles have the token type unrecognized token
  2883. asASSERT( t.IsObjectHandle() || t.GetTokenType() != ttUnrecognizedToken );
  2884. bool isOnHeap = true;
  2885. // TODO: Remove this once the bugs with value types on stack is fixed
  2886. // forceOnHeap = true;
  2887. if( t.IsPrimitive() ||
  2888. (t.GetObjectType() && (t.GetObjectType()->GetFlags() & asOBJ_VALUE) && !forceOnHeap) )
  2889. {
  2890. // Primitives and value types (unless overridden) are allocated on the stack
  2891. isOnHeap = false;
  2892. }
  2893. // Find a free location with the same type
  2894. for( asUINT n = 0; n < freeVariables.GetLength(); n++ )
  2895. {
  2896. int slot = freeVariables[n];
  2897. if( variableAllocations[slot].IsEqualExceptConst(t) &&
  2898. variableIsTemporary[slot] == isTemporary &&
  2899. variableIsOnHeap[slot] == isOnHeap )
  2900. {
  2901. // We can't return by slot, must count variable sizes
  2902. int offset = GetVariableOffset(slot);
  2903. // Verify that it is not in the list of reserved variables
  2904. bool isUsed = false;
  2905. if( reservedVariables.GetLength() )
  2906. isUsed = reservedVariables.Exists(offset);
  2907. if( !isUsed )
  2908. {
  2909. if( n != freeVariables.GetLength() - 1 )
  2910. freeVariables[n] = freeVariables.PopLast();
  2911. else
  2912. freeVariables.PopLast();
  2913. if( isTemporary )
  2914. tempVariables.PushLast(offset);
  2915. return offset;
  2916. }
  2917. }
  2918. }
  2919. variableAllocations.PushLast(t);
  2920. variableIsTemporary.PushLast(isTemporary);
  2921. variableIsOnHeap.PushLast(isOnHeap);
  2922. int offset = GetVariableOffset((int)variableAllocations.GetLength()-1);
  2923. if( isTemporary )
  2924. tempVariables.PushLast(offset);
  2925. return offset;
  2926. }
  2927. int asCCompiler::GetVariableOffset(int varIndex)
  2928. {
  2929. // Return offset to the last dword on the stack
  2930. int varOffset = 1;
  2931. for( int n = 0; n < varIndex; n++ )
  2932. {
  2933. if( !variableIsOnHeap[n] && variableAllocations[n].IsObject() )
  2934. varOffset += variableAllocations[n].GetSizeInMemoryDWords();
  2935. else
  2936. varOffset += variableAllocations[n].GetSizeOnStackDWords();
  2937. }
  2938. if( varIndex < (int)variableAllocations.GetLength() )
  2939. {
  2940. int size;
  2941. if( !variableIsOnHeap[varIndex] && variableAllocations[varIndex].IsObject() )
  2942. size = variableAllocations[varIndex].GetSizeInMemoryDWords();
  2943. else
  2944. size = variableAllocations[varIndex].GetSizeOnStackDWords();
  2945. if( size > 1 )
  2946. varOffset += size-1;
  2947. }
  2948. return varOffset;
  2949. }
  2950. int asCCompiler::GetVariableSlot(int offset)
  2951. {
  2952. int varOffset = 1;
  2953. for( asUINT n = 0; n < variableAllocations.GetLength(); n++ )
  2954. {
  2955. if( !variableIsOnHeap[n] && variableAllocations[n].IsObject() )
  2956. varOffset += -1 + variableAllocations[n].GetSizeInMemoryDWords();
  2957. else
  2958. varOffset += -1 + variableAllocations[n].GetSizeOnStackDWords();
  2959. if( varOffset == offset )
  2960. return n;
  2961. varOffset++;
  2962. }
  2963. return -1;
  2964. }
  2965. bool asCCompiler::IsVariableOnHeap(int offset)
  2966. {
  2967. int varSlot = GetVariableSlot(offset);
  2968. if( varSlot < 0 )
  2969. {
  2970. // This happens for function arguments that are considered as on the heap
  2971. return true;
  2972. }
  2973. return variableIsOnHeap[varSlot];
  2974. }
  2975. void asCCompiler::DeallocateVariable(int offset)
  2976. {
  2977. // Remove temporary variable
  2978. int n;
  2979. for( n = 0; n < (int)tempVariables.GetLength(); n++ )
  2980. {
  2981. if( offset == tempVariables[n] )
  2982. {
  2983. if( n == (int)tempVariables.GetLength()-1 )
  2984. tempVariables.PopLast();
  2985. else
  2986. tempVariables[n] = tempVariables.PopLast();
  2987. break;
  2988. }
  2989. }
  2990. n = GetVariableSlot(offset);
  2991. if( n != -1 )
  2992. {
  2993. freeVariables.PushLast(n);
  2994. return;
  2995. }
  2996. // We might get here if the variable was implicitly declared
  2997. // because it was use before a formal declaration, in this case
  2998. // the offset is 0x7FFF
  2999. asASSERT(offset == 0x7FFF);
  3000. }
  3001. void asCCompiler::ReleaseTemporaryVariable(asCTypeInfo &t, asCByteCode *bc)
  3002. {
  3003. if( t.isTemporary )
  3004. {
  3005. ReleaseTemporaryVariable(t.stackOffset, bc);
  3006. t.isTemporary = false;
  3007. }
  3008. }
  3009. void asCCompiler::ReleaseTemporaryVariable(int offset, asCByteCode *bc)
  3010. {
  3011. if( bc )
  3012. {
  3013. // We need to call the destructor on the true variable type
  3014. int n = GetVariableSlot(offset);
  3015. asASSERT( n >= 0 );
  3016. if( n >= 0 )
  3017. {
  3018. asCDataType dt = variableAllocations[n];
  3019. bool isOnHeap = variableIsOnHeap[n];
  3020. // Call destructor
  3021. CallDestructor(dt, offset, isOnHeap, bc);
  3022. }
  3023. }
  3024. DeallocateVariable(offset);
  3025. }
  3026. void asCCompiler::Dereference(asSExprContext *ctx, bool generateCode)
  3027. {
  3028. if( ctx->type.dataType.IsReference() )
  3029. {
  3030. if( ctx->type.dataType.IsObject() )
  3031. {
  3032. ctx->type.dataType.MakeReference(false);
  3033. if( generateCode )
  3034. {
  3035. ctx->bc.Instr(asBC_CHKREF);
  3036. ctx->bc.Instr(asBC_RDSPTR);
  3037. }
  3038. }
  3039. else
  3040. {
  3041. // This should never happen as primitives are treated differently
  3042. asASSERT(false);
  3043. }
  3044. }
  3045. }
  3046. bool asCCompiler::IsVariableInitialized(asCTypeInfo *type, asCScriptNode *node)
  3047. {
  3048. // Temporary variables are assumed to be initialized
  3049. if( type->isTemporary ) return true;
  3050. // Verify that it is a variable
  3051. if( !type->isVariable ) return true;
  3052. // Find the variable
  3053. sVariable *v = variables->GetVariableByOffset(type->stackOffset);
  3054. // The variable isn't found if it is a constant, in which case it is guaranteed to be initialized
  3055. if( v == 0 ) return true;
  3056. if( v->isInitialized ) return true;
  3057. // Complex types don't need this test
  3058. if( v->type.IsObject() ) return true;
  3059. // Mark as initialized so that the user will not be bothered again
  3060. v->isInitialized = true;
  3061. // Write warning
  3062. asCString str;
  3063. str.Format(TXT_s_NOT_INITIALIZED, (const char *)v->name.AddressOf());
  3064. Warning(str.AddressOf(), node);
  3065. return false;
  3066. }
  3067. void asCCompiler::PrepareOperand(asSExprContext *ctx, asCScriptNode *node)
  3068. {
  3069. // Check if the variable is initialized (if it indeed is a variable)
  3070. IsVariableInitialized(&ctx->type, node);
  3071. asCDataType to = ctx->type.dataType;
  3072. to.MakeReference(false);
  3073. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV);
  3074. ProcessDeferredParams(ctx);
  3075. }
  3076. void asCCompiler::PrepareForAssignment(asCDataType *lvalue, asSExprContext *rctx, asCScriptNode *node, bool toTemporary, asSExprContext *lvalueExpr)
  3077. {
  3078. ProcessPropertyGetAccessor(rctx, node);
  3079. // Make sure the rvalue is initialized if it is a variable
  3080. IsVariableInitialized(&rctx->type, node);
  3081. if( lvalue->IsPrimitive() )
  3082. {
  3083. if( rctx->type.dataType.IsPrimitive() )
  3084. {
  3085. if( rctx->type.dataType.IsReference() )
  3086. {
  3087. // Cannot do implicit conversion of references so we first convert the reference to a variable
  3088. ConvertToVariableNotIn(rctx, lvalueExpr);
  3089. }
  3090. }
  3091. // Implicitly convert the value to the right type
  3092. int l = reservedVariables.GetLength();
  3093. if( lvalueExpr ) lvalueExpr->bc.GetVarsUsed(reservedVariables);
  3094. ImplicitConversion(rctx, *lvalue, node, asIC_IMPLICIT_CONV);
  3095. reservedVariables.SetLength(l);
  3096. // Check data type
  3097. if( !lvalue->IsEqualExceptRefAndConst(rctx->type.dataType) )
  3098. {
  3099. asCString str;
  3100. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format().AddressOf(), lvalue->Format().AddressOf());
  3101. Error(str.AddressOf(), node);
  3102. rctx->type.SetDummy();
  3103. }
  3104. // Make sure the rvalue is a variable
  3105. if( !rctx->type.isVariable )
  3106. ConvertToVariableNotIn(rctx, lvalueExpr);
  3107. }
  3108. else
  3109. {
  3110. asCDataType to = *lvalue;
  3111. to.MakeReference(false);
  3112. // TODO: ImplicitConversion should know to do this by itself
  3113. // First convert to a handle which will do a reference cast
  3114. if( !lvalue->IsObjectHandle() &&
  3115. (lvalue->GetObjectType()->flags & asOBJ_SCRIPT_OBJECT) )
  3116. to.MakeHandle(true);
  3117. // Don't allow the implicit conversion to create an object
  3118. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true, !toTemporary);
  3119. if( !lvalue->IsObjectHandle() &&
  3120. (lvalue->GetObjectType()->flags & asOBJ_SCRIPT_OBJECT) )
  3121. {
  3122. // Then convert to a reference, which will validate the handle
  3123. to.MakeHandle(false);
  3124. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true, !toTemporary);
  3125. }
  3126. // Check data type
  3127. if( !lvalue->IsEqualExceptRefAndConst(rctx->type.dataType) )
  3128. {
  3129. asCString str;
  3130. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format().AddressOf(), lvalue->Format().AddressOf());
  3131. Error(str.AddressOf(), node);
  3132. }
  3133. else
  3134. {
  3135. // If the assignment will be made with the copy behaviour then the rvalue must not be a reference
  3136. if( lvalue->IsObject() )
  3137. asASSERT(!rctx->type.dataType.IsReference());
  3138. }
  3139. }
  3140. }
  3141. bool asCCompiler::IsLValue(asCTypeInfo &type)
  3142. {
  3143. if( !type.isLValue ) return false;
  3144. if( type.dataType.IsReadOnly() ) return false;
  3145. if( !type.dataType.IsObject() && !type.isVariable && !type.dataType.IsReference() ) return false;
  3146. return true;
  3147. }
  3148. int asCCompiler::PerformAssignment(asCTypeInfo *lvalue, asCTypeInfo *rvalue, asCByteCode *bc, asCScriptNode *node)
  3149. {
  3150. if( lvalue->dataType.IsReadOnly() )
  3151. {
  3152. Error(TXT_REF_IS_READ_ONLY, node);
  3153. return -1;
  3154. }
  3155. if( lvalue->dataType.IsPrimitive() )
  3156. {
  3157. if( lvalue->isVariable )
  3158. {
  3159. // Copy the value between the variables directly
  3160. if( lvalue->dataType.GetSizeInMemoryDWords() == 1 )
  3161. bc->InstrW_W(asBC_CpyVtoV4, lvalue->stackOffset, rvalue->stackOffset);
  3162. else
  3163. bc->InstrW_W(asBC_CpyVtoV8, lvalue->stackOffset, rvalue->stackOffset);
  3164. // Mark variable as initialized
  3165. sVariable *v = variables->GetVariableByOffset(lvalue->stackOffset);
  3166. if( v ) v->isInitialized = true;
  3167. }
  3168. else if( lvalue->dataType.IsReference() )
  3169. {
  3170. // Copy the value of the variable to the reference in the register
  3171. int s = lvalue->dataType.GetSizeInMemoryBytes();
  3172. if( s == 1 )
  3173. bc->InstrSHORT(asBC_WRTV1, rvalue->stackOffset);
  3174. else if( s == 2 )
  3175. bc->InstrSHORT(asBC_WRTV2, rvalue->stackOffset);
  3176. else if( s == 4 )
  3177. bc->InstrSHORT(asBC_WRTV4, rvalue->stackOffset);
  3178. else if( s == 8 )
  3179. bc->InstrSHORT(asBC_WRTV8, rvalue->stackOffset);
  3180. }
  3181. else
  3182. {
  3183. Error(TXT_NOT_VALID_LVALUE, node);
  3184. return -1;
  3185. }
  3186. }
  3187. else if( !lvalue->isExplicitHandle )
  3188. {
  3189. asSExprContext ctx(engine);
  3190. ctx.type = *lvalue;
  3191. Dereference(&ctx, true);
  3192. *lvalue = ctx.type;
  3193. bc->AddCode(&ctx.bc);
  3194. // TODO: Can't this leave deferred output params unhandled?
  3195. // TODO: Should find the opAssign method that implements the default copy behaviour.
  3196. // The beh->copy member will be removed.
  3197. asSTypeBehaviour *beh = lvalue->dataType.GetBehaviour();
  3198. if( beh->copy )
  3199. {
  3200. // Call the copy operator
  3201. bc->Call(asBC_CALLSYS, (asDWORD)beh->copy, 2*AS_PTR_SIZE);
  3202. bc->Instr(asBC_PshRPtr);
  3203. }
  3204. else
  3205. {
  3206. // Default copy operator
  3207. if( lvalue->dataType.GetSizeInMemoryDWords() == 0 ||
  3208. !(lvalue->dataType.GetObjectType()->flags & asOBJ_POD) )
  3209. {
  3210. asCString msg;
  3211. msg.Format(TXT_NO_DEFAULT_COPY_OP_FOR_s, lvalue->dataType.GetObjectType()->name.AddressOf());
  3212. Error(msg.AddressOf(), node);
  3213. return -1;
  3214. }
  3215. // Copy larger data types from a reference
  3216. bc->InstrSHORT_DW(asBC_COPY, (short)lvalue->dataType.GetSizeInMemoryDWords(), engine->GetTypeIdFromDataType(lvalue->dataType));
  3217. }
  3218. }
  3219. else
  3220. {
  3221. // TODO: The object handle can be stored in a variable as well
  3222. if( !lvalue->dataType.IsReference() )
  3223. {
  3224. Error(TXT_NOT_VALID_REFERENCE, node);
  3225. return -1;
  3226. }
  3227. // TODO: optimize: Convert to register based
  3228. bc->InstrPTR(asBC_REFCPY, lvalue->dataType.GetObjectType());
  3229. // Mark variable as initialized
  3230. if( variables )
  3231. {
  3232. sVariable *v = variables->GetVariableByOffset(lvalue->stackOffset);
  3233. if( v ) v->isInitialized = true;
  3234. }
  3235. }
  3236. return 0;
  3237. }
  3238. bool asCCompiler::CompileRefCast(asSExprContext *ctx, const asCDataType &to, bool isExplicit, asCScriptNode *node, bool generateCode)
  3239. {
  3240. bool conversionDone = false;
  3241. asCArray<int> ops;
  3242. asUINT n;
  3243. if( ctx->type.dataType.GetObjectType()->flags & asOBJ_SCRIPT_OBJECT )
  3244. {
  3245. // We need it to be a reference
  3246. if( !ctx->type.dataType.IsReference() )
  3247. {
  3248. asCDataType to = ctx->type.dataType;
  3249. to.MakeReference(true);
  3250. ImplicitConversion(ctx, to, 0, isExplicit ? asIC_EXPLICIT_REF_CAST : asIC_IMPLICIT_CONV, generateCode);
  3251. }
  3252. if( isExplicit )
  3253. {
  3254. // Allow dynamic cast between object handles (only for script objects).
  3255. // At run time this may result in a null handle,
  3256. // which when used will throw an exception
  3257. conversionDone = true;
  3258. if( generateCode )
  3259. {
  3260. ctx->bc.InstrDWORD(asBC_Cast, engine->GetTypeIdFromDataType(to));
  3261. // Allocate a temporary variable for the returned object
  3262. int returnOffset = AllocateVariable(to, true);
  3263. // Move the pointer from the object register to the temporary variable
  3264. ctx->bc.InstrSHORT(asBC_STOREOBJ, (short)returnOffset);
  3265. ctx->bc.InstrSHORT(asBC_PSF, (short)returnOffset);
  3266. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3267. ctx->type.SetVariable(to, returnOffset, true);
  3268. ctx->type.dataType.MakeReference(true);
  3269. }
  3270. else
  3271. {
  3272. ctx->type.dataType = to;
  3273. ctx->type.dataType.MakeReference(true);
  3274. }
  3275. }
  3276. else
  3277. {
  3278. if( ctx->type.dataType.GetObjectType()->DerivesFrom(to.GetObjectType()) )
  3279. {
  3280. conversionDone = true;
  3281. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3282. }
  3283. }
  3284. }
  3285. else
  3286. {
  3287. // Find a suitable registered behaviour
  3288. asSTypeBehaviour *beh = &ctx->type.dataType.GetObjectType()->beh;
  3289. for( n = 0; n < beh->operators.GetLength(); n+= 2 )
  3290. {
  3291. if( (isExplicit && asBEHAVE_REF_CAST == beh->operators[n]) ||
  3292. asBEHAVE_IMPLICIT_REF_CAST == beh->operators[n] )
  3293. {
  3294. int funcId = beh->operators[n+1];
  3295. // Is the operator for the output type?
  3296. asCScriptFunction *func = engine->scriptFunctions[funcId];
  3297. if( func->returnType.GetObjectType() != to.GetObjectType() )
  3298. continue;
  3299. ops.PushLast(funcId);
  3300. }
  3301. }
  3302. // It shouldn't be possible to have more than one
  3303. asASSERT( ops.GetLength() <= 1 );
  3304. // Should only have one behaviour for each output type
  3305. if( ops.GetLength() == 1 )
  3306. {
  3307. if( generateCode )
  3308. {
  3309. // TODO: optimize: Instead of producing bytecode for checking if the handle is
  3310. // null, we can create a special CALLSYS instruction that checks
  3311. // if the object pointer is null and if so sets the object register
  3312. // to null directly without executing the function.
  3313. //
  3314. // Alternatively I could force the ref cast behaviours be global
  3315. // functions with 1 parameter, even though they should still be
  3316. // registered with RegisterObjectBehaviour()
  3317. // Add code to avoid calling the cast behaviour if the handle is already null,
  3318. // because that will raise a null pointer exception due to the cast behaviour
  3319. // being a class method, and the this pointer cannot be null.
  3320. if( ctx->type.isVariable )
  3321. ctx->bc.Pop(AS_PTR_SIZE);
  3322. else
  3323. {
  3324. Dereference(ctx, true);
  3325. ConvertToVariable(ctx);
  3326. }
  3327. #ifdef AS_64BIT_PTR
  3328. int offset = AllocateVariable(asCDataType::CreatePrimitive(ttUInt64, false), true);
  3329. ctx->bc.InstrW_QW(asBC_SetV8, (asWORD)offset, 0);
  3330. ctx->bc.InstrW_W(asBC_CMPi64, ctx->type.stackOffset, offset);
  3331. DeallocateVariable(offset);
  3332. #else
  3333. int offset = AllocateVariable(asCDataType::CreatePrimitive(ttUInt, false), true);
  3334. ctx->bc.InstrW_DW(asBC_SetV4, (asWORD)offset, 0);
  3335. ctx->bc.InstrW_W(asBC_CMPi, ctx->type.stackOffset, offset);
  3336. DeallocateVariable(offset);
  3337. #endif
  3338. int afterLabel = nextLabel++;
  3339. ctx->bc.InstrDWORD(asBC_JZ, afterLabel);
  3340. // Call the cast operator
  3341. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  3342. ctx->bc.Instr(asBC_RDSPTR);
  3343. ctx->type.dataType.MakeReference(false);
  3344. asCTypeInfo objType = ctx->type;
  3345. asCArray<asSExprContext *> args;
  3346. MakeFunctionCall(ctx, ops[0], objType.dataType.GetObjectType(), args, node);
  3347. ctx->bc.Pop(AS_PTR_SIZE);
  3348. int endLabel = nextLabel++;
  3349. ctx->bc.InstrINT(asBC_JMP, endLabel);
  3350. ctx->bc.Label((short)afterLabel);
  3351. // Make a NULL pointer
  3352. #ifdef AS_64BIT_PTR
  3353. ctx->bc.InstrW_QW(asBC_SetV8, ctx->type.stackOffset, 0);
  3354. #else
  3355. ctx->bc.InstrW_DW(asBC_SetV4, ctx->type.stackOffset, 0);
  3356. #endif
  3357. ctx->bc.Label((short)endLabel);
  3358. // Since we're receiving a handle, we can release the original variable
  3359. ReleaseTemporaryVariable(objType, &ctx->bc);
  3360. // Push the reference to the handle on the stack
  3361. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  3362. }
  3363. else
  3364. {
  3365. asCScriptFunction *func = engine->scriptFunctions[ops[0]];
  3366. ctx->type.Set(func->returnType);
  3367. }
  3368. }
  3369. else if( ops.GetLength() == 0 )
  3370. {
  3371. // Check for the generic ref cast behaviour
  3372. for( n = 0; n < beh->operators.GetLength(); n+= 2 )
  3373. {
  3374. if( (isExplicit && asBEHAVE_REF_CAST == beh->operators[n]) ||
  3375. asBEHAVE_IMPLICIT_REF_CAST == beh->operators[n] )
  3376. {
  3377. int funcId = beh->operators[n+1];
  3378. // Does the operator take the ?&out parameter?
  3379. asCScriptFunction *func = engine->scriptFunctions[funcId];
  3380. if( func->parameterTypes.GetLength() != 1 ||
  3381. func->parameterTypes[0].GetTokenType() != ttQuestion ||
  3382. func->inOutFlags[0] != asTM_OUTREF )
  3383. continue;
  3384. ops.PushLast(funcId);
  3385. }
  3386. }
  3387. // It shouldn't be possible to have more than one
  3388. asASSERT( ops.GetLength() <= 1 );
  3389. if( ops.GetLength() == 1 )
  3390. {
  3391. if( generateCode )
  3392. {
  3393. asASSERT(to.IsObjectHandle());
  3394. // Allocate a temporary variable of the requested handle type
  3395. int stackOffset = AllocateVariableNotIn(to, true, false, ctx);
  3396. // Pass the reference of that variable to the function as output parameter
  3397. asCDataType toRef(to);
  3398. toRef.MakeReference(true);
  3399. asCArray<asSExprContext *> args;
  3400. asSExprContext arg(engine);
  3401. arg.bc.InstrSHORT(asBC_PSF, (short)stackOffset);
  3402. // Don't mark the variable as temporary, so it won't be freed too early
  3403. arg.type.SetVariable(toRef, stackOffset, false);
  3404. arg.type.isLValue = true;
  3405. arg.type.isExplicitHandle = true;
  3406. args.PushLast(&arg);
  3407. // Call the behaviour method
  3408. MakeFunctionCall(ctx, ops[0], ctx->type.dataType.GetObjectType(), args, node);
  3409. // Use the reference to the variable as the result of the expression
  3410. // Now we can mark the variable as temporary
  3411. ctx->type.SetVariable(toRef, stackOffset, true);
  3412. ctx->bc.InstrSHORT(asBC_PSF, (short)stackOffset);
  3413. }
  3414. else
  3415. {
  3416. // All casts are legal
  3417. ctx->type.Set(to);
  3418. }
  3419. }
  3420. }
  3421. }
  3422. return conversionDone;
  3423. }
  3424. void asCCompiler::ImplicitConvPrimitiveToPrimitive(asSExprContext *ctx, const asCDataType &toOrig, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  3425. {
  3426. asCDataType to = toOrig;
  3427. to.MakeReference(false);
  3428. asASSERT( !ctx->type.dataType.IsReference() );
  3429. // Start by implicitly converting constant values
  3430. if( ctx->type.isConstant )
  3431. ImplicitConversionConstant(ctx, to, node, convType);
  3432. // A primitive is const or not
  3433. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  3434. if( to == ctx->type.dataType )
  3435. return;
  3436. // Allow implicit conversion between numbers
  3437. if( generateCode )
  3438. {
  3439. // Convert smaller types to 32bit first
  3440. int s = ctx->type.dataType.GetSizeInMemoryBytes();
  3441. if( s < 4 )
  3442. {
  3443. ConvertToTempVariable(ctx);
  3444. if( ctx->type.dataType.IsIntegerType() )
  3445. {
  3446. if( s == 1 )
  3447. ctx->bc.InstrSHORT(asBC_sbTOi, ctx->type.stackOffset);
  3448. else if( s == 2 )
  3449. ctx->bc.InstrSHORT(asBC_swTOi, ctx->type.stackOffset);
  3450. ctx->type.dataType.SetTokenType(ttInt);
  3451. }
  3452. else if( ctx->type.dataType.IsUnsignedType() )
  3453. {
  3454. if( s == 1 )
  3455. ctx->bc.InstrSHORT(asBC_ubTOi, ctx->type.stackOffset);
  3456. else if( s == 2 )
  3457. ctx->bc.InstrSHORT(asBC_uwTOi, ctx->type.stackOffset);
  3458. ctx->type.dataType.SetTokenType(ttUInt);
  3459. }
  3460. }
  3461. if( (to.IsIntegerType() && to.GetSizeInMemoryDWords() == 1) ||
  3462. (to.IsEnumType() && convType == asIC_EXPLICIT_VAL_CAST) )
  3463. {
  3464. if( ctx->type.dataType.IsIntegerType() ||
  3465. ctx->type.dataType.IsUnsignedType() ||
  3466. ctx->type.dataType.IsEnumType() )
  3467. {
  3468. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  3469. {
  3470. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3471. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3472. }
  3473. else
  3474. {
  3475. ConvertToTempVariable(ctx);
  3476. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3477. int offset = AllocateVariable(to, true);
  3478. ctx->bc.InstrW_W(asBC_i64TOi, offset, ctx->type.stackOffset);
  3479. ctx->type.SetVariable(to, offset, true);
  3480. }
  3481. }
  3482. else if( ctx->type.dataType.IsFloatType() )
  3483. {
  3484. ConvertToTempVariable(ctx);
  3485. ctx->bc.InstrSHORT(asBC_fTOi, ctx->type.stackOffset);
  3486. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3487. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3488. }
  3489. else if( ctx->type.dataType.IsDoubleType() )
  3490. {
  3491. ConvertToTempVariable(ctx);
  3492. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3493. int offset = AllocateVariable(to, true);
  3494. ctx->bc.InstrW_W(asBC_dTOi, offset, ctx->type.stackOffset);
  3495. ctx->type.SetVariable(to, offset, true);
  3496. }
  3497. // Convert to smaller integer if necessary
  3498. int s = to.GetSizeInMemoryBytes();
  3499. if( s < 4 )
  3500. {
  3501. ConvertToTempVariable(ctx);
  3502. if( s == 1 )
  3503. ctx->bc.InstrSHORT(asBC_iTOb, ctx->type.stackOffset);
  3504. else if( s == 2 )
  3505. ctx->bc.InstrSHORT(asBC_iTOw, ctx->type.stackOffset);
  3506. }
  3507. }
  3508. if( to.IsIntegerType() && to.GetSizeInMemoryDWords() == 2 )
  3509. {
  3510. if( ctx->type.dataType.IsIntegerType() ||
  3511. ctx->type.dataType.IsUnsignedType() ||
  3512. ctx->type.dataType.IsEnumType() )
  3513. {
  3514. if( ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  3515. {
  3516. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3517. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3518. }
  3519. else
  3520. {
  3521. ConvertToTempVariable(ctx);
  3522. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3523. int offset = AllocateVariable(to, true);
  3524. if( ctx->type.dataType.IsUnsignedType() )
  3525. ctx->bc.InstrW_W(asBC_uTOi64, offset, ctx->type.stackOffset);
  3526. else
  3527. ctx->bc.InstrW_W(asBC_iTOi64, offset, ctx->type.stackOffset);
  3528. ctx->type.SetVariable(to, offset, true);
  3529. }
  3530. }
  3531. else if( ctx->type.dataType.IsFloatType() )
  3532. {
  3533. ConvertToTempVariable(ctx);
  3534. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3535. int offset = AllocateVariable(to, true);
  3536. ctx->bc.InstrW_W(asBC_fTOi64, offset, ctx->type.stackOffset);
  3537. ctx->type.SetVariable(to, offset, true);
  3538. }
  3539. else if( ctx->type.dataType.IsDoubleType() )
  3540. {
  3541. ConvertToTempVariable(ctx);
  3542. ctx->bc.InstrSHORT(asBC_dTOi64, ctx->type.stackOffset);
  3543. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3544. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3545. }
  3546. }
  3547. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 1 )
  3548. {
  3549. if( ctx->type.dataType.IsIntegerType() ||
  3550. ctx->type.dataType.IsUnsignedType() ||
  3551. ctx->type.dataType.IsEnumType() )
  3552. {
  3553. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  3554. {
  3555. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3556. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3557. }
  3558. else
  3559. {
  3560. ConvertToTempVariable(ctx);
  3561. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3562. int offset = AllocateVariable(to, true);
  3563. ctx->bc.InstrW_W(asBC_i64TOi, offset, ctx->type.stackOffset);
  3564. ctx->type.SetVariable(to, offset, true);
  3565. }
  3566. }
  3567. else if( ctx->type.dataType.IsFloatType() )
  3568. {
  3569. ConvertToTempVariable(ctx);
  3570. ctx->bc.InstrSHORT(asBC_fTOu, ctx->type.stackOffset);
  3571. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3572. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3573. }
  3574. else if( ctx->type.dataType.IsDoubleType() )
  3575. {
  3576. ConvertToTempVariable(ctx);
  3577. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3578. int offset = AllocateVariable(to, true);
  3579. ctx->bc.InstrW_W(asBC_dTOu, offset, ctx->type.stackOffset);
  3580. ctx->type.SetVariable(to, offset, true);
  3581. }
  3582. // Convert to smaller integer if necessary
  3583. int s = to.GetSizeInMemoryBytes();
  3584. if( s < 4 )
  3585. {
  3586. ConvertToTempVariable(ctx);
  3587. if( s == 1 )
  3588. ctx->bc.InstrSHORT(asBC_iTOb, ctx->type.stackOffset);
  3589. else if( s == 2 )
  3590. ctx->bc.InstrSHORT(asBC_iTOw, ctx->type.stackOffset);
  3591. }
  3592. }
  3593. if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 2 )
  3594. {
  3595. if( ctx->type.dataType.IsIntegerType() ||
  3596. ctx->type.dataType.IsUnsignedType() ||
  3597. ctx->type.dataType.IsEnumType() )
  3598. {
  3599. if( ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  3600. {
  3601. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3602. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3603. }
  3604. else
  3605. {
  3606. ConvertToTempVariable(ctx);
  3607. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3608. int offset = AllocateVariable(to, true);
  3609. if( ctx->type.dataType.IsUnsignedType() )
  3610. ctx->bc.InstrW_W(asBC_uTOi64, offset, ctx->type.stackOffset);
  3611. else
  3612. ctx->bc.InstrW_W(asBC_iTOi64, offset, ctx->type.stackOffset);
  3613. ctx->type.SetVariable(to, offset, true);
  3614. }
  3615. }
  3616. else if( ctx->type.dataType.IsFloatType() )
  3617. {
  3618. ConvertToTempVariable(ctx);
  3619. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3620. int offset = AllocateVariable(to, true);
  3621. ctx->bc.InstrW_W(asBC_fTOu64, offset, ctx->type.stackOffset);
  3622. ctx->type.SetVariable(to, offset, true);
  3623. }
  3624. else if( ctx->type.dataType.IsDoubleType() )
  3625. {
  3626. ConvertToTempVariable(ctx);
  3627. ctx->bc.InstrSHORT(asBC_dTOu64, ctx->type.stackOffset);
  3628. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3629. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3630. }
  3631. }
  3632. else if( to.IsFloatType() )
  3633. {
  3634. if( (ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsEnumType()) && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  3635. {
  3636. ConvertToTempVariable(ctx);
  3637. ctx->bc.InstrSHORT(asBC_iTOf, ctx->type.stackOffset);
  3638. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3639. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3640. }
  3641. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  3642. {
  3643. ConvertToTempVariable(ctx);
  3644. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3645. int offset = AllocateVariable(to, true);
  3646. ctx->bc.InstrW_W(asBC_i64TOf, offset, ctx->type.stackOffset);
  3647. ctx->type.SetVariable(to, offset, true);
  3648. }
  3649. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  3650. {
  3651. ConvertToTempVariable(ctx);
  3652. ctx->bc.InstrSHORT(asBC_uTOf, ctx->type.stackOffset);
  3653. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3654. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3655. }
  3656. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  3657. {
  3658. ConvertToTempVariable(ctx);
  3659. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3660. int offset = AllocateVariable(to, true);
  3661. ctx->bc.InstrW_W(asBC_u64TOf, offset, ctx->type.stackOffset);
  3662. ctx->type.SetVariable(to, offset, true);
  3663. }
  3664. else if( ctx->type.dataType.IsDoubleType() )
  3665. {
  3666. ConvertToTempVariable(ctx);
  3667. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3668. int offset = AllocateVariable(to, true);
  3669. ctx->bc.InstrW_W(asBC_dTOf, offset, ctx->type.stackOffset);
  3670. ctx->type.SetVariable(to, offset, true);
  3671. }
  3672. }
  3673. else if( to.IsDoubleType() )
  3674. {
  3675. if( (ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsEnumType()) && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  3676. {
  3677. ConvertToTempVariable(ctx);
  3678. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3679. int offset = AllocateVariable(to, true);
  3680. ctx->bc.InstrW_W(asBC_iTOd, offset, ctx->type.stackOffset);
  3681. ctx->type.SetVariable(to, offset, true);
  3682. }
  3683. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  3684. {
  3685. ConvertToTempVariable(ctx);
  3686. ctx->bc.InstrSHORT(asBC_i64TOd, ctx->type.stackOffset);
  3687. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3688. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3689. }
  3690. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  3691. {
  3692. ConvertToTempVariable(ctx);
  3693. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3694. int offset = AllocateVariable(to, true);
  3695. ctx->bc.InstrW_W(asBC_uTOd, offset, ctx->type.stackOffset);
  3696. ctx->type.SetVariable(to, offset, true);
  3697. }
  3698. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  3699. {
  3700. ConvertToTempVariable(ctx);
  3701. ctx->bc.InstrSHORT(asBC_u64TOd, ctx->type.stackOffset);
  3702. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3703. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3704. }
  3705. else if( ctx->type.dataType.IsFloatType() )
  3706. {
  3707. ConvertToTempVariable(ctx);
  3708. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3709. int offset = AllocateVariable(to, true);
  3710. ctx->bc.InstrW_W(asBC_fTOd, offset, ctx->type.stackOffset);
  3711. ctx->type.SetVariable(to, offset, true);
  3712. }
  3713. }
  3714. }
  3715. else
  3716. {
  3717. if( (to.IsIntegerType() || to.IsUnsignedType() ||
  3718. to.IsFloatType() || to.IsDoubleType() ||
  3719. (to.IsEnumType() && convType == asIC_EXPLICIT_VAL_CAST)) &&
  3720. (ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsUnsignedType() ||
  3721. ctx->type.dataType.IsFloatType() || ctx->type.dataType.IsDoubleType() ||
  3722. ctx->type.dataType.IsEnumType()) )
  3723. {
  3724. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3725. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3726. }
  3727. }
  3728. // Primitive types on the stack, can be const or non-const
  3729. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  3730. }
  3731. void asCCompiler::ImplicitConversion(asSExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode, bool allowObjectConstruct)
  3732. {
  3733. asASSERT( ctx->type.dataType.GetTokenType() != ttUnrecognizedToken ||
  3734. ctx->type.dataType.IsNullHandle() );
  3735. // No conversion from void to any other type
  3736. if( ctx->type.dataType.GetTokenType() == ttVoid )
  3737. return;
  3738. // Do we want a var type?
  3739. if( to.GetTokenType() == ttQuestion )
  3740. {
  3741. // Any type can be converted to a var type, but only when not generating code
  3742. asASSERT( !generateCode );
  3743. ctx->type.dataType = to;
  3744. return;
  3745. }
  3746. // Do we want a primitive?
  3747. else if( to.IsPrimitive() )
  3748. {
  3749. if( !ctx->type.dataType.IsPrimitive() )
  3750. ImplicitConvObjectToPrimitive(ctx, to, node, convType, generateCode);
  3751. else
  3752. ImplicitConvPrimitiveToPrimitive(ctx, to, node, convType, generateCode);
  3753. }
  3754. else // The target is a complex type
  3755. {
  3756. if( ctx->type.dataType.IsPrimitive() )
  3757. ImplicitConvPrimitiveToObject(ctx, to, node, convType, generateCode, allowObjectConstruct);
  3758. else if( ctx->type.IsNullConstant() || ctx->type.dataType.GetObjectType() )
  3759. ImplicitConvObjectToObject(ctx, to, node, convType, generateCode, allowObjectConstruct);
  3760. }
  3761. }
  3762. void asCCompiler::ImplicitConvObjectToPrimitive(asSExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  3763. {
  3764. if( ctx->type.isExplicitHandle )
  3765. {
  3766. // An explicit handle cannot be converted to a primitive
  3767. if( convType != asIC_IMPLICIT_CONV && node )
  3768. {
  3769. asCString str;
  3770. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  3771. Error(str.AddressOf(), node);
  3772. }
  3773. return;
  3774. }
  3775. // TODO: Must use the const cast behaviour if the object is read-only
  3776. // Find matching value cast behaviours
  3777. // Here we're only interested in those that convert the type to a primitive type
  3778. asCArray<int> funcs;
  3779. asSTypeBehaviour *beh = ctx->type.dataType.GetBehaviour();
  3780. if( beh )
  3781. {
  3782. if( convType == asIC_EXPLICIT_VAL_CAST )
  3783. {
  3784. for( unsigned int n = 0; n < beh->operators.GetLength(); n += 2 )
  3785. {
  3786. // accept both implicit and explicit cast
  3787. if( (beh->operators[n] == asBEHAVE_VALUE_CAST ||
  3788. beh->operators[n] == asBEHAVE_IMPLICIT_VALUE_CAST) &&
  3789. builder->GetFunctionDescription(beh->operators[n+1])->returnType.IsPrimitive() )
  3790. funcs.PushLast(beh->operators[n+1]);
  3791. }
  3792. }
  3793. else
  3794. {
  3795. for( unsigned int n = 0; n < beh->operators.GetLength(); n += 2 )
  3796. {
  3797. // accept only implicit cast
  3798. if( beh->operators[n] == asBEHAVE_IMPLICIT_VALUE_CAST &&
  3799. builder->GetFunctionDescription(beh->operators[n+1])->returnType.IsPrimitive() )
  3800. funcs.PushLast(beh->operators[n+1]);
  3801. }
  3802. }
  3803. }
  3804. // This matrix describes the priorities of the types to search for, for each target type
  3805. // The first column is the target type, the priorities goes from left to right
  3806. eTokenType matchMtx[10][10] =
  3807. {
  3808. {ttDouble, ttFloat, ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8},
  3809. {ttFloat, ttDouble, ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8},
  3810. {ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8, ttDouble, ttFloat},
  3811. {ttUInt64, ttInt64, ttUInt, ttInt, ttUInt16, ttInt16, ttUInt8, ttInt8, ttDouble, ttFloat},
  3812. {ttInt, ttUInt, ttInt64, ttUInt64, ttInt16, ttUInt16, ttInt8, ttUInt8, ttDouble, ttFloat},
  3813. {ttUInt, ttInt, ttUInt64, ttInt64, ttUInt16, ttInt16, ttUInt8, ttInt8, ttDouble, ttFloat},
  3814. {ttInt16, ttUInt16, ttInt, ttUInt, ttInt64, ttUInt64, ttInt8, ttUInt8, ttDouble, ttFloat},
  3815. {ttUInt16, ttInt16, ttUInt, ttInt, ttUInt64, ttInt64, ttUInt8, ttInt8, ttDouble, ttFloat},
  3816. {ttInt8, ttUInt8, ttInt16, ttUInt16, ttInt, ttUInt, ttInt64, ttUInt64, ttDouble, ttFloat},
  3817. {ttUInt8, ttInt8, ttUInt16, ttInt16, ttUInt, ttInt, ttUInt64, ttInt64, ttDouble, ttFloat},
  3818. };
  3819. // Which row to use?
  3820. eTokenType *row = 0;
  3821. for( unsigned int type = 0; type < 10; type++ )
  3822. {
  3823. if( to.GetTokenType() == matchMtx[type][0] )
  3824. {
  3825. row = &matchMtx[type][0];
  3826. break;
  3827. }
  3828. }
  3829. // Find the best matching cast operator
  3830. int funcId = 0;
  3831. if( row )
  3832. {
  3833. asCDataType target(to);
  3834. // Priority goes from left to right in the matrix
  3835. for( unsigned int attempt = 0; attempt < 10 && funcId == 0; attempt++ )
  3836. {
  3837. target.SetTokenType(row[attempt]);
  3838. for( unsigned int n = 0; n < funcs.GetLength(); n++ )
  3839. {
  3840. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[n]);
  3841. if( descr->returnType.IsEqualExceptConst(target) )
  3842. {
  3843. funcId = funcs[n];
  3844. break;
  3845. }
  3846. }
  3847. }
  3848. }
  3849. // Did we find a suitable function?
  3850. if( funcId != 0 )
  3851. {
  3852. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  3853. if( generateCode )
  3854. {
  3855. asCTypeInfo objType = ctx->type;
  3856. Dereference(ctx, true);
  3857. PerformFunctionCall(funcId, ctx);
  3858. ReleaseTemporaryVariable(objType, &ctx->bc);
  3859. }
  3860. else
  3861. ctx->type.Set(descr->returnType);
  3862. // Allow one more implicit conversion to another primitive type
  3863. ImplicitConversion(ctx, to, node, convType, generateCode, false);
  3864. }
  3865. else
  3866. {
  3867. if( convType != asIC_IMPLICIT_CONV && node )
  3868. {
  3869. asCString str;
  3870. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  3871. Error(str.AddressOf(), node);
  3872. }
  3873. }
  3874. }
  3875. void asCCompiler::ImplicitConvObjectToObject(asSExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode, bool allowObjectConstruct)
  3876. {
  3877. // Convert null to any object type handle, but not to a non-handle type
  3878. if( ctx->type.IsNullConstant() )
  3879. {
  3880. if( to.IsObjectHandle() )
  3881. ctx->type.dataType = to;
  3882. return;
  3883. }
  3884. asASSERT(ctx->type.dataType.GetObjectType());
  3885. // First attempt to convert the base type without instanciating another instance
  3886. if( to.GetObjectType() != ctx->type.dataType.GetObjectType() )
  3887. {
  3888. // If the to type is an interface and the from type implements it, then we can convert it immediately
  3889. if( ctx->type.dataType.GetObjectType()->Implements(to.GetObjectType()) )
  3890. {
  3891. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3892. }
  3893. // If the to type is a class and the from type derives from it, then we can convert it immediately
  3894. if( ctx->type.dataType.GetObjectType()->DerivesFrom(to.GetObjectType()) )
  3895. {
  3896. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3897. }
  3898. // If the types are not equal yet, then we may still be able to find a reference cast
  3899. if( ctx->type.dataType.GetObjectType() != to.GetObjectType() )
  3900. {
  3901. // A ref cast must not remove the constness
  3902. bool isConst = false;
  3903. if( (ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsHandleToConst()) ||
  3904. (!ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsReadOnly()) )
  3905. isConst = true;
  3906. // We may still be able to find an implicit ref cast behaviour
  3907. CompileRefCast(ctx, to, convType == asIC_EXPLICIT_REF_CAST, node, generateCode);
  3908. ctx->type.dataType.MakeHandleToConst(isConst);
  3909. }
  3910. }
  3911. // If the base type is still different, and we are allowed to instance
  3912. // another object then we can try an implicit value cast
  3913. if( to.GetObjectType() != ctx->type.dataType.GetObjectType() && allowObjectConstruct )
  3914. {
  3915. // TODO: Implement support for implicit constructor/factory
  3916. asCArray<int> funcs;
  3917. asSTypeBehaviour *beh = ctx->type.dataType.GetBehaviour();
  3918. if( beh )
  3919. {
  3920. if( convType == asIC_EXPLICIT_VAL_CAST )
  3921. {
  3922. for( unsigned int n = 0; n < beh->operators.GetLength(); n += 2 )
  3923. {
  3924. // accept both implicit and explicit cast
  3925. if( (beh->operators[n] == asBEHAVE_VALUE_CAST ||
  3926. beh->operators[n] == asBEHAVE_IMPLICIT_VALUE_CAST) &&
  3927. builder->GetFunctionDescription(beh->operators[n+1])->returnType.GetObjectType() == to.GetObjectType() )
  3928. funcs.PushLast(beh->operators[n+1]);
  3929. }
  3930. }
  3931. else
  3932. {
  3933. for( unsigned int n = 0; n < beh->operators.GetLength(); n += 2 )
  3934. {
  3935. // accept only implicit cast
  3936. if( beh->operators[n] == asBEHAVE_IMPLICIT_VALUE_CAST &&
  3937. builder->GetFunctionDescription(beh->operators[n+1])->returnType.GetObjectType() == to.GetObjectType() )
  3938. funcs.PushLast(beh->operators[n+1]);
  3939. }
  3940. }
  3941. }
  3942. // TODO: If there are multiple valid value casts, then we must choose the most appropriate one
  3943. asASSERT( funcs.GetLength() <= 1 );
  3944. if( funcs.GetLength() == 1 )
  3945. {
  3946. asCScriptFunction *f = builder->GetFunctionDescription(funcs[0]);
  3947. if( generateCode )
  3948. {
  3949. asCTypeInfo objType = ctx->type;
  3950. Dereference(ctx, true);
  3951. bool useVariable = false;
  3952. int stackOffset = 0;
  3953. #ifndef AS_OLD
  3954. if( f->DoesReturnOnStack() )
  3955. {
  3956. useVariable = true;
  3957. stackOffset = AllocateVariable(f->returnType, true);
  3958. // Push the pointer to the pre-allocated space for the return value
  3959. ctx->bc.InstrSHORT(asBC_PSF, short(stackOffset));
  3960. // The object pointer is already on the stack, but should be the top
  3961. // one, so we need to swap the pointers in order to get the correct
  3962. #if AS_PTR_SIZE == 1
  3963. ctx->bc.Instr(asBC_SWAP4);
  3964. #else
  3965. ctx->bc.Instr(asBC_SWAP8);
  3966. #endif
  3967. }
  3968. #endif
  3969. PerformFunctionCall(funcs[0], ctx, false, 0, 0, useVariable, stackOffset);
  3970. ReleaseTemporaryVariable(objType, &ctx->bc);
  3971. }
  3972. else
  3973. ctx->type.Set(f->returnType);
  3974. }
  3975. }
  3976. // If we still haven't converted the base type to the correct type, then there is no need to continue
  3977. if( to.GetObjectType() != ctx->type.dataType.GetObjectType() )
  3978. return;
  3979. // Convert matching function types
  3980. if( to.GetFuncDef() && ctx->type.dataType.GetFuncDef() &&
  3981. to.GetFuncDef() != ctx->type.dataType.GetFuncDef() )
  3982. {
  3983. asCScriptFunction *toFunc = to.GetFuncDef();
  3984. asCScriptFunction *fromFunc = ctx->type.dataType.GetFuncDef();
  3985. if( toFunc->IsSignatureExceptNameEqual(fromFunc) )
  3986. {
  3987. ctx->type.dataType.SetFuncDef(toFunc);
  3988. }
  3989. }
  3990. if( to.IsObjectHandle() )
  3991. {
  3992. // reference to handle -> handle
  3993. // reference -> handle
  3994. // object -> handle
  3995. // handle -> reference to handle
  3996. // reference -> reference to handle
  3997. // object -> reference to handle
  3998. // TODO: If the type is handle, then we can't use IsReadOnly to determine the constness of the basetype
  3999. // If the rvalue is a handle to a const object, then
  4000. // the lvalue must also be a handle to a const object
  4001. if( ctx->type.dataType.IsReadOnly() && !to.IsReadOnly() )
  4002. {
  4003. if( convType != asIC_IMPLICIT_CONV )
  4004. {
  4005. asASSERT(node);
  4006. asCString str;
  4007. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  4008. Error(str.AddressOf(), node);
  4009. }
  4010. }
  4011. if( !ctx->type.dataType.IsObjectHandle() )
  4012. {
  4013. // An object type can be directly converted to a handle of the same type
  4014. if( ctx->type.dataType.SupportHandles() )
  4015. {
  4016. ctx->type.dataType.MakeHandle(true);
  4017. }
  4018. if( ctx->type.dataType.IsObjectHandle() )
  4019. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  4020. if( to.IsHandleToConst() && ctx->type.dataType.IsObjectHandle() )
  4021. ctx->type.dataType.MakeHandleToConst(true);
  4022. }
  4023. else
  4024. {
  4025. // A handle to non-const can be converted to a
  4026. // handle to const, but not the other way
  4027. if( to.IsHandleToConst() )
  4028. ctx->type.dataType.MakeHandleToConst(true);
  4029. // A const handle can be converted to a non-const
  4030. // handle and vice versa as the handle is just a value
  4031. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  4032. }
  4033. if( to.IsReference() && !ctx->type.dataType.IsReference() )
  4034. {
  4035. if( generateCode )
  4036. {
  4037. // If the input type is a handle, then a simple ref copy is enough
  4038. bool isExplicitHandle = ctx->type.isExplicitHandle;
  4039. ctx->type.isExplicitHandle = ctx->type.dataType.IsObjectHandle();
  4040. // If the input type is read-only we'll need to temporarily
  4041. // remove this constness, otherwise the assignment will fail
  4042. bool typeIsReadOnly = ctx->type.dataType.IsReadOnly();
  4043. ctx->type.dataType.MakeReadOnly(false);
  4044. // If the object already is a temporary variable, then the copy
  4045. // doesn't have to be made as it is already a unique object
  4046. PrepareTemporaryObject(node, ctx);
  4047. ctx->type.dataType.MakeReadOnly(typeIsReadOnly);
  4048. ctx->type.isExplicitHandle = isExplicitHandle;
  4049. }
  4050. // A non-reference can be converted to a reference,
  4051. // by putting the value in a temporary variable
  4052. ctx->type.dataType.MakeReference(true);
  4053. // Since it is a new temporary variable it doesn't have to be const
  4054. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  4055. }
  4056. else if( !to.IsReference() && ctx->type.dataType.IsReference() )
  4057. {
  4058. // ASHANDLE is really a value type, even though it looks
  4059. // like a handle, so we shouldn't dereference it
  4060. if( !(ctx->type.dataType.GetObjectType() && (ctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE)) ||
  4061. (ctx->type.isVariable && IsVariableOnHeap(ctx->type.stackOffset)) )
  4062. Dereference(ctx, generateCode);
  4063. else
  4064. ctx->type.dataType.MakeReference(false);
  4065. }
  4066. }
  4067. else
  4068. {
  4069. if( !to.IsReference() )
  4070. {
  4071. // reference to handle -> object
  4072. // handle -> object
  4073. // reference -> object
  4074. // An implicit handle can be converted to an object by adding a check for null pointer
  4075. if( ctx->type.dataType.IsObjectHandle() && !ctx->type.isExplicitHandle )
  4076. {
  4077. if( generateCode )
  4078. ctx->bc.Instr(asBC_CHKREF);
  4079. ctx->type.dataType.MakeHandle(false);
  4080. }
  4081. // A const object can be converted to a non-const object through a copy
  4082. if( ctx->type.dataType.IsReadOnly() && !to.IsReadOnly() &&
  4083. allowObjectConstruct )
  4084. {
  4085. // Does the object type allow a copy to be made?
  4086. if( ctx->type.dataType.CanBeCopied() )
  4087. {
  4088. if( generateCode )
  4089. {
  4090. // Make a temporary object with the copy
  4091. PrepareTemporaryObject(node, ctx);
  4092. }
  4093. // In case the object was already in a temporary variable, then the function
  4094. // didn't really do anything so we need to remove the constness here
  4095. ctx->type.dataType.MakeReadOnly(false);
  4096. }
  4097. }
  4098. if( ctx->type.dataType.IsReference() )
  4099. {
  4100. Dereference(ctx, generateCode);
  4101. // TODO: Can't this leave unhandled deferred output params?
  4102. }
  4103. // A non-const object can be converted to a const object directly
  4104. if( !ctx->type.dataType.IsReadOnly() && to.IsReadOnly() )
  4105. {
  4106. ctx->type.dataType.MakeReadOnly(true);
  4107. }
  4108. }
  4109. else
  4110. {
  4111. // reference to handle -> reference
  4112. // handle -> reference
  4113. // object -> reference
  4114. if( ctx->type.dataType.IsReference() )
  4115. {
  4116. if( ctx->type.isExplicitHandle && ctx->type.dataType.GetObjectType() && (ctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE) )
  4117. {
  4118. // ASHANDLE objects are really value types, so explicit handle can be removed
  4119. ctx->type.isExplicitHandle = false;
  4120. ctx->type.dataType.MakeHandle(false);
  4121. }
  4122. // A reference to a handle can be converted to a reference to an object
  4123. // by first reading the address, then verifying that it is not null
  4124. if( !to.IsObjectHandle() && ctx->type.dataType.IsObjectHandle() && !ctx->type.isExplicitHandle )
  4125. {
  4126. ctx->type.dataType.MakeHandle(false);
  4127. if( generateCode )
  4128. ctx->bc.Instr(asBC_ChkRefS);
  4129. }
  4130. // A reference to a non-const can be converted to a reference to a const
  4131. if( to.IsReadOnly() )
  4132. ctx->type.dataType.MakeReadOnly(true);
  4133. else if( ctx->type.dataType.IsReadOnly() )
  4134. {
  4135. // A reference to a const can be converted to a reference to a
  4136. // non-const by copying the object to a temporary variable
  4137. ctx->type.dataType.MakeReadOnly(false);
  4138. if( generateCode )
  4139. {
  4140. // If the object already is a temporary variable, then the copy
  4141. // doesn't have to be made as it is already a unique object
  4142. PrepareTemporaryObject(node, ctx);
  4143. }
  4144. }
  4145. }
  4146. else
  4147. {
  4148. // A value type allocated on the stack is differentiated
  4149. // by it not being a reference. But it can be handled as
  4150. // reference by pushing the pointer on the stack
  4151. if( (ctx->type.dataType.GetObjectType()->GetFlags() & asOBJ_VALUE) &&
  4152. (ctx->type.isVariable || ctx->type.isTemporary) &&
  4153. !IsVariableOnHeap(ctx->type.stackOffset) )
  4154. {
  4155. // Actually the pointer is already pushed on the stack in
  4156. // CompileVariableAccess, so we don't need to do anything else
  4157. }
  4158. else if( generateCode )
  4159. {
  4160. // A non-reference can be converted to a reference,
  4161. // by putting the value in a temporary variable
  4162. // If the input type is read-only we'll need to temporarily
  4163. // remove this constness, otherwise the assignment will fail
  4164. bool typeIsReadOnly = ctx->type.dataType.IsReadOnly();
  4165. ctx->type.dataType.MakeReadOnly(false);
  4166. // If the object already is a temporary variable, then the copy
  4167. // doesn't have to be made as it is already a unique object
  4168. PrepareTemporaryObject(node, ctx);
  4169. ctx->type.dataType.MakeReadOnly(typeIsReadOnly);
  4170. }
  4171. // A handle can be converted to a reference, by checking for a null pointer
  4172. if( ctx->type.dataType.IsObjectHandle() )
  4173. {
  4174. if( generateCode )
  4175. ctx->bc.InstrSHORT(asBC_ChkNullV, ctx->type.stackOffset);
  4176. ctx->type.dataType.MakeHandle(false);
  4177. ctx->type.dataType.MakeReference(true);
  4178. // TODO: Make sure a handle to const isn't converted to non-const reference
  4179. }
  4180. else
  4181. {
  4182. // This may look strange as the conversion was to make the expression a reference
  4183. // but a value type allocated on the stack is a reference even without the type
  4184. // being marked as such.
  4185. ctx->type.dataType.MakeReference(IsVariableOnHeap(ctx->type.stackOffset));
  4186. }
  4187. // TODO: If the variable is an object allocated on the stack, this is not true
  4188. // Since it is a new temporary variable it doesn't have to be const
  4189. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  4190. }
  4191. }
  4192. }
  4193. }
  4194. void asCCompiler::ImplicitConvPrimitiveToObject(asSExprContext * /*ctx*/, const asCDataType & /*to*/, asCScriptNode * /*node*/, EImplicitConv /*isExplicit*/, bool /*generateCode*/, bool /*allowObjectConstruct*/)
  4195. {
  4196. // TODO: This function should call the constructor/factory that has been marked as available
  4197. // for implicit conversions. The code will likely be similar to CallCopyConstructor()
  4198. }
  4199. void asCCompiler::ImplicitConversionConstant(asSExprContext *from, const asCDataType &to, asCScriptNode *node, EImplicitConv convType)
  4200. {
  4201. asASSERT(from->type.isConstant);
  4202. // TODO: node should be the node of the value that is
  4203. // converted (not the operator that provokes the implicit
  4204. // conversion)
  4205. // If the base type is correct there is no more to do
  4206. if( to.IsEqualExceptRefAndConst(from->type.dataType) ) return;
  4207. // References cannot be constants
  4208. if( from->type.dataType.IsReference() ) return;
  4209. if( (to.IsIntegerType() && to.GetSizeInMemoryDWords() == 1) ||
  4210. (to.IsEnumType() && convType == asIC_EXPLICIT_VAL_CAST) )
  4211. {
  4212. if( from->type.dataType.IsFloatType() ||
  4213. from->type.dataType.IsDoubleType() ||
  4214. from->type.dataType.IsUnsignedType() ||
  4215. from->type.dataType.IsIntegerType() ||
  4216. from->type.dataType.IsEnumType() )
  4217. {
  4218. // Transform the value
  4219. // Float constants can be implicitly converted to int
  4220. if( from->type.dataType.IsFloatType() )
  4221. {
  4222. float fc = from->type.floatValue;
  4223. int ic = int(fc);
  4224. if( float(ic) != fc )
  4225. {
  4226. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4227. }
  4228. from->type.intValue = ic;
  4229. }
  4230. // Double constants can be implicitly converted to int
  4231. else if( from->type.dataType.IsDoubleType() )
  4232. {
  4233. double fc = from->type.doubleValue;
  4234. int ic = int(fc);
  4235. if( double(ic) != fc )
  4236. {
  4237. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4238. }
  4239. from->type.intValue = ic;
  4240. }
  4241. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  4242. {
  4243. // Verify that it is possible to convert to signed without getting negative
  4244. if( from->type.intValue < 0 )
  4245. {
  4246. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  4247. }
  4248. // Convert to 32bit
  4249. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4250. from->type.intValue = from->type.byteValue;
  4251. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4252. from->type.intValue = from->type.wordValue;
  4253. }
  4254. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  4255. {
  4256. // Convert to 32bit
  4257. from->type.intValue = int(from->type.qwordValue);
  4258. }
  4259. else if( from->type.dataType.IsIntegerType() &&
  4260. from->type.dataType.GetSizeInMemoryBytes() < 4 )
  4261. {
  4262. // Convert to 32bit
  4263. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4264. from->type.intValue = (signed char)from->type.byteValue;
  4265. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4266. from->type.intValue = (short)from->type.wordValue;
  4267. }
  4268. else if( from->type.dataType.IsEnumType() )
  4269. {
  4270. // Enum type is already an integer type
  4271. }
  4272. // Set the resulting type
  4273. if( to.IsEnumType() )
  4274. from->type.dataType = to;
  4275. else
  4276. from->type.dataType = asCDataType::CreatePrimitive(ttInt, true);
  4277. }
  4278. // Check if a downsize is necessary
  4279. if( to.IsIntegerType() &&
  4280. from->type.dataType.IsIntegerType() &&
  4281. from->type.dataType.GetSizeInMemoryBytes() > to.GetSizeInMemoryBytes() )
  4282. {
  4283. // Verify if it is possible
  4284. if( to.GetSizeInMemoryBytes() == 1 )
  4285. {
  4286. if( char(from->type.intValue) != from->type.intValue )
  4287. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  4288. from->type.byteValue = char(from->type.intValue);
  4289. }
  4290. else if( to.GetSizeInMemoryBytes() == 2 )
  4291. {
  4292. if( short(from->type.intValue) != from->type.intValue )
  4293. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  4294. from->type.wordValue = short(from->type.intValue);
  4295. }
  4296. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4297. }
  4298. }
  4299. else if( to.IsIntegerType() && to.GetSizeInMemoryDWords() == 2 )
  4300. {
  4301. // Float constants can be implicitly converted to int
  4302. if( from->type.dataType.IsFloatType() )
  4303. {
  4304. float fc = from->type.floatValue;
  4305. asINT64 ic = asINT64(fc);
  4306. if( float(ic) != fc )
  4307. {
  4308. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4309. }
  4310. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  4311. from->type.qwordValue = ic;
  4312. }
  4313. // Double constants can be implicitly converted to int
  4314. else if( from->type.dataType.IsDoubleType() )
  4315. {
  4316. double fc = from->type.doubleValue;
  4317. asINT64 ic = asINT64(fc);
  4318. if( double(ic) != fc )
  4319. {
  4320. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4321. }
  4322. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  4323. from->type.qwordValue = ic;
  4324. }
  4325. else if( from->type.dataType.IsUnsignedType() )
  4326. {
  4327. // Convert to 64bit
  4328. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4329. from->type.qwordValue = from->type.byteValue;
  4330. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4331. from->type.qwordValue = from->type.wordValue;
  4332. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  4333. from->type.qwordValue = from->type.dwordValue;
  4334. else if( from->type.dataType.GetSizeInMemoryBytes() == 8 )
  4335. {
  4336. if( asINT64(from->type.qwordValue) < 0 )
  4337. {
  4338. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  4339. }
  4340. }
  4341. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  4342. }
  4343. else if( from->type.dataType.IsEnumType() )
  4344. {
  4345. from->type.qwordValue = from->type.intValue;
  4346. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  4347. }
  4348. else if( from->type.dataType.IsIntegerType() )
  4349. {
  4350. // Convert to 64bit
  4351. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4352. from->type.qwordValue = (signed char)from->type.byteValue;
  4353. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4354. from->type.qwordValue = (short)from->type.wordValue;
  4355. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  4356. from->type.qwordValue = from->type.intValue;
  4357. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  4358. }
  4359. }
  4360. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 1 )
  4361. {
  4362. if( from->type.dataType.IsFloatType() )
  4363. {
  4364. float fc = from->type.floatValue;
  4365. // Some compilers set the value to 0 when converting a negative float to unsigned int.
  4366. // To maintain a consistent behaviour across compilers we convert to int first.
  4367. asUINT uic = asUINT(int(fc));
  4368. if( float(uic) != fc )
  4369. {
  4370. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4371. }
  4372. from->type.dataType = asCDataType::CreatePrimitive(ttUInt, true);
  4373. from->type.intValue = uic;
  4374. // Try once more, in case of a smaller type
  4375. ImplicitConversionConstant(from, to, node, convType);
  4376. }
  4377. else if( from->type.dataType.IsDoubleType() )
  4378. {
  4379. double fc = from->type.doubleValue;
  4380. // Some compilers set the value to 0 when converting a negative double to unsigned int.
  4381. // To maintain a consistent behaviour across compilers we convert to int first.
  4382. asUINT uic = asUINT(int(fc));
  4383. if( double(uic) != fc )
  4384. {
  4385. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4386. }
  4387. from->type.dataType = asCDataType::CreatePrimitive(ttUInt, true);
  4388. from->type.intValue = uic;
  4389. // Try once more, in case of a smaller type
  4390. ImplicitConversionConstant(from, to, node, convType);
  4391. }
  4392. else if( from->type.dataType.IsEnumType() )
  4393. {
  4394. from->type.dataType = asCDataType::CreatePrimitive(ttUInt, true);
  4395. // Try once more, in case of a smaller type
  4396. ImplicitConversionConstant(from, to, node, convType);
  4397. }
  4398. else if( from->type.dataType.IsIntegerType() )
  4399. {
  4400. // Verify that it is possible to convert to unsigned without loosing negative
  4401. if( from->type.intValue < 0 )
  4402. {
  4403. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  4404. }
  4405. // Convert to 32bit
  4406. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4407. from->type.intValue = (signed char)from->type.byteValue;
  4408. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4409. from->type.intValue = (short)from->type.wordValue;
  4410. from->type.dataType = asCDataType::CreatePrimitive(ttUInt, true);
  4411. // Try once more, in case of a smaller type
  4412. ImplicitConversionConstant(from, to, node, convType);
  4413. }
  4414. else if( from->type.dataType.IsUnsignedType() &&
  4415. from->type.dataType.GetSizeInMemoryBytes() < 4 )
  4416. {
  4417. // Convert to 32bit
  4418. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4419. from->type.dwordValue = from->type.byteValue;
  4420. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4421. from->type.dwordValue = from->type.wordValue;
  4422. from->type.dataType = asCDataType::CreatePrimitive(ttUInt, true);
  4423. // Try once more, in case of a smaller type
  4424. ImplicitConversionConstant(from, to, node, convType);
  4425. }
  4426. else if( from->type.dataType.IsUnsignedType() &&
  4427. from->type.dataType.GetSizeInMemoryBytes() > to.GetSizeInMemoryBytes() )
  4428. {
  4429. // Verify if it is possible
  4430. if( to.GetSizeInMemoryBytes() == 1 )
  4431. {
  4432. if( asBYTE(from->type.dwordValue) != from->type.dwordValue )
  4433. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  4434. from->type.byteValue = asBYTE(from->type.dwordValue);
  4435. }
  4436. else if( to.GetSizeInMemoryBytes() == 2 )
  4437. {
  4438. if( asWORD(from->type.dwordValue) != from->type.dwordValue )
  4439. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  4440. from->type.wordValue = asWORD(from->type.dwordValue);
  4441. }
  4442. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4443. }
  4444. }
  4445. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 2 )
  4446. {
  4447. if( from->type.dataType.IsFloatType() )
  4448. {
  4449. float fc = from->type.floatValue;
  4450. // Convert first to int64 then to uint64 to avoid negative float becoming 0 on gnuc base compilers
  4451. asQWORD uic = asQWORD(asINT64(fc));
  4452. #if !defined(_MSC_VER) || _MSC_VER > 1200 // MSVC++ 6
  4453. // MSVC6 doesn't support this conversion
  4454. if( float(uic) != fc )
  4455. {
  4456. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4457. }
  4458. #endif
  4459. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  4460. from->type.qwordValue = uic;
  4461. }
  4462. else if( from->type.dataType.IsDoubleType() )
  4463. {
  4464. double fc = from->type.doubleValue;
  4465. // Convert first to int64 then to uint64 to avoid negative float becoming 0 on gnuc base compilers
  4466. asQWORD uic = asQWORD(asINT64(fc));
  4467. #if !defined(_MSC_VER) || _MSC_VER > 1200 // MSVC++ 6
  4468. // MSVC6 doesn't support this conversion
  4469. if( double(uic) != fc )
  4470. {
  4471. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4472. }
  4473. #endif
  4474. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  4475. from->type.qwordValue = uic;
  4476. }
  4477. else if( from->type.dataType.IsEnumType() )
  4478. {
  4479. from->type.qwordValue = (asINT64)from->type.intValue;
  4480. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  4481. }
  4482. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  4483. {
  4484. // Convert to 64bit
  4485. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4486. from->type.qwordValue = (asINT64)(signed char)from->type.byteValue;
  4487. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4488. from->type.qwordValue = (asINT64)(short)from->type.wordValue;
  4489. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  4490. from->type.qwordValue = (asINT64)from->type.intValue;
  4491. // Verify that it is possible to convert to unsigned without loosing negative
  4492. if( asINT64(from->type.qwordValue) < 0 )
  4493. {
  4494. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  4495. }
  4496. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  4497. }
  4498. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  4499. {
  4500. // Verify that it is possible to convert to unsigned without loosing negative
  4501. if( asINT64(from->type.qwordValue) < 0 )
  4502. {
  4503. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  4504. }
  4505. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  4506. }
  4507. else if( from->type.dataType.IsUnsignedType() )
  4508. {
  4509. // Convert to 64bit
  4510. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4511. from->type.qwordValue = from->type.byteValue;
  4512. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4513. from->type.qwordValue = from->type.wordValue;
  4514. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  4515. from->type.qwordValue = from->type.dwordValue;
  4516. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  4517. }
  4518. }
  4519. else if( to.IsFloatType() )
  4520. {
  4521. if( from->type.dataType.IsDoubleType() )
  4522. {
  4523. double ic = from->type.doubleValue;
  4524. float fc = float(ic);
  4525. // Don't bother warning about this
  4526. // if( double(fc) != ic )
  4527. // {
  4528. // asCString str;
  4529. // str.Format(TXT_POSSIBLE_LOSS_OF_PRECISION);
  4530. // if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(str.AddressOf(), node);
  4531. // }
  4532. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4533. from->type.floatValue = fc;
  4534. }
  4535. else if( from->type.dataType.IsEnumType() )
  4536. {
  4537. float fc = float(from->type.intValue);
  4538. if( int(fc) != from->type.intValue )
  4539. {
  4540. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4541. }
  4542. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4543. from->type.floatValue = fc;
  4544. }
  4545. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  4546. {
  4547. // Must properly convert value in case the from value is smaller
  4548. int ic;
  4549. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4550. ic = (signed char)from->type.byteValue;
  4551. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4552. ic = (short)from->type.wordValue;
  4553. else
  4554. ic = from->type.intValue;
  4555. float fc = float(ic);
  4556. if( int(fc) != ic )
  4557. {
  4558. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4559. }
  4560. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4561. from->type.floatValue = fc;
  4562. }
  4563. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  4564. {
  4565. float fc = float(asINT64(from->type.qwordValue));
  4566. if( asINT64(fc) != asINT64(from->type.qwordValue) )
  4567. {
  4568. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4569. }
  4570. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4571. from->type.floatValue = fc;
  4572. }
  4573. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  4574. {
  4575. // Must properly convert value in case the from value is smaller
  4576. unsigned int uic;
  4577. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4578. uic = from->type.byteValue;
  4579. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4580. uic = from->type.wordValue;
  4581. else
  4582. uic = from->type.dwordValue;
  4583. float fc = float(uic);
  4584. if( (unsigned int)(fc) != uic )
  4585. {
  4586. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4587. }
  4588. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4589. from->type.floatValue = fc;
  4590. }
  4591. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  4592. {
  4593. float fc = float((asINT64)from->type.qwordValue);
  4594. if( asQWORD(fc) != from->type.qwordValue )
  4595. {
  4596. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4597. }
  4598. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4599. from->type.floatValue = fc;
  4600. }
  4601. }
  4602. else if( to.IsDoubleType() )
  4603. {
  4604. if( from->type.dataType.IsFloatType() )
  4605. {
  4606. float ic = from->type.floatValue;
  4607. double fc = double(ic);
  4608. // Don't check for float->double
  4609. // if( float(fc) != ic )
  4610. // {
  4611. // acCString str;
  4612. // str.Format(TXT_NOT_EXACT_g_g_g, ic, fc, float(fc));
  4613. // if( !isExplicit ) Warning(str, node);
  4614. // }
  4615. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4616. from->type.doubleValue = fc;
  4617. }
  4618. else if( from->type.dataType.IsEnumType() )
  4619. {
  4620. double fc = double(from->type.intValue);
  4621. if( int(fc) != from->type.intValue )
  4622. {
  4623. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4624. }
  4625. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4626. from->type.doubleValue = fc;
  4627. }
  4628. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  4629. {
  4630. // Must properly convert value in case the from value is smaller
  4631. int ic;
  4632. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4633. ic = (signed char)from->type.byteValue;
  4634. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4635. ic = (short)from->type.wordValue;
  4636. else
  4637. ic = from->type.intValue;
  4638. double fc = double(ic);
  4639. if( int(fc) != ic )
  4640. {
  4641. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4642. }
  4643. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4644. from->type.doubleValue = fc;
  4645. }
  4646. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  4647. {
  4648. double fc = double(asINT64(from->type.qwordValue));
  4649. if( asINT64(fc) != asINT64(from->type.qwordValue) )
  4650. {
  4651. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4652. }
  4653. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4654. from->type.doubleValue = fc;
  4655. }
  4656. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  4657. {
  4658. // Must properly convert value in case the from value is smaller
  4659. unsigned int uic;
  4660. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4661. uic = from->type.byteValue;
  4662. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4663. uic = from->type.wordValue;
  4664. else
  4665. uic = from->type.dwordValue;
  4666. double fc = double(uic);
  4667. if( (unsigned int)(fc) != uic )
  4668. {
  4669. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4670. }
  4671. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4672. from->type.doubleValue = fc;
  4673. }
  4674. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  4675. {
  4676. double fc = double((asINT64)from->type.qwordValue);
  4677. if( asQWORD(fc) != from->type.qwordValue )
  4678. {
  4679. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4680. }
  4681. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4682. from->type.doubleValue = fc;
  4683. }
  4684. }
  4685. }
  4686. int asCCompiler::DoAssignment(asSExprContext *ctx, asSExprContext *lctx, asSExprContext *rctx, asCScriptNode *lexpr, asCScriptNode *rexpr, int op, asCScriptNode *opNode)
  4687. {
  4688. // Implicit handle types should always be treated as handles in assignments
  4689. if (lctx->type.dataType.GetObjectType() && (lctx->type.dataType.GetObjectType()->flags & asOBJ_IMPLICIT_HANDLE) )
  4690. {
  4691. lctx->type.dataType.MakeHandle(true);
  4692. lctx->type.isExplicitHandle = true;
  4693. }
  4694. // Urho3D: if there is a handle type, and it does not have an overloaded assignment operator, convert to an explicit handle
  4695. // for scripting convenience. (For the Urho3D handle types, value assignment is not supported)
  4696. if (lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle && !lctx->type.dataType.GetBehaviour()->copy)
  4697. lctx->type.isExplicitHandle = true;
  4698. // If the left hand expression is a property accessor, then that should be used
  4699. // to do the assignment instead of the ordinary operator. The exception is when
  4700. // the property accessor is for a handle property, and the operation is a value
  4701. // assignment.
  4702. if( (lctx->property_get || lctx->property_set) &&
  4703. !(lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle) )
  4704. {
  4705. if( op != ttAssignment )
  4706. {
  4707. // TODO: getset: We may actually be able to support this, if we can
  4708. // guarantee that the object reference will stay valid
  4709. // between the calls to the get and set accessors.
  4710. // Process the property to free the memory
  4711. ProcessPropertySetAccessor(lctx, rctx, opNode);
  4712. // Compound assignments are not allowed for properties
  4713. Error(TXT_COMPOUND_ASGN_WITH_PROP, opNode);
  4714. return -1;
  4715. }
  4716. // It is not allowed to do a handle assignment on a property
  4717. // accessor that doesn't take a handle in the set accessor.
  4718. if( lctx->property_set && lctx->type.isExplicitHandle )
  4719. {
  4720. // set_opIndex has 2 arguments, where as normal setters have only 1
  4721. asCArray<asCDataType>& parameterTypes =
  4722. builder->GetFunctionDescription(lctx->property_set)->parameterTypes;
  4723. if( !parameterTypes[parameterTypes.GetLength() - 1].IsObjectHandle() )
  4724. {
  4725. // Process the property to free the memory
  4726. ProcessPropertySetAccessor(lctx, rctx, opNode);
  4727. Error(TXT_HANDLE_ASSIGN_ON_NON_HANDLE_PROP, opNode);
  4728. return -1;
  4729. }
  4730. }
  4731. MergeExprBytecodeAndType(ctx, lctx);
  4732. return ProcessPropertySetAccessor(ctx, rctx, opNode);
  4733. }
  4734. else if( lctx->property_get && lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle )
  4735. {
  4736. // Get the handle to the object that will be used for the value assignment
  4737. ProcessPropertyGetAccessor(lctx, opNode);
  4738. }
  4739. if( lctx->type.dataType.IsPrimitive() )
  4740. {
  4741. if( !lctx->type.isLValue )
  4742. {
  4743. Error(TXT_NOT_LVALUE, lexpr);
  4744. return -1;
  4745. }
  4746. if( op != ttAssignment )
  4747. {
  4748. // Compute the operator before the assignment
  4749. asCTypeInfo lvalue = lctx->type;
  4750. if( lctx->type.isTemporary && !lctx->type.isVariable )
  4751. {
  4752. // The temporary variable must not be freed until the
  4753. // assignment has been performed. lvalue still holds
  4754. // the information about the temporary variable
  4755. lctx->type.isTemporary = false;
  4756. }
  4757. asSExprContext o(engine);
  4758. CompileOperator(opNode, lctx, rctx, &o);
  4759. MergeExprBytecode(rctx, &o);
  4760. rctx->type = o.type;
  4761. // Convert the rvalue to the right type and validate it
  4762. PrepareForAssignment(&lvalue.dataType, rctx, rexpr, false);
  4763. MergeExprBytecode(ctx, rctx);
  4764. lctx->type = lvalue;
  4765. // The lvalue continues the same, either it was a variable, or a reference in the register
  4766. }
  4767. else
  4768. {
  4769. // Convert the rvalue to the right type and validate it
  4770. PrepareForAssignment(&lctx->type.dataType, rctx, rexpr, false, lctx);
  4771. MergeExprBytecode(ctx, rctx);
  4772. MergeExprBytecode(ctx, lctx);
  4773. }
  4774. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  4775. PerformAssignment(&lctx->type, &rctx->type, &ctx->bc, opNode);
  4776. ctx->type = lctx->type;
  4777. }
  4778. else if( lctx->type.isExplicitHandle )
  4779. {
  4780. if( !lctx->type.isLValue )
  4781. {
  4782. Error(TXT_NOT_LVALUE, lexpr);
  4783. return -1;
  4784. }
  4785. // Object handles don't have any compound assignment operators
  4786. if( op != ttAssignment )
  4787. {
  4788. asCString str;
  4789. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format().AddressOf());
  4790. Error(str.AddressOf(), lexpr);
  4791. return -1;
  4792. }
  4793. if( lctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE )
  4794. {
  4795. // The object is a value type but that should be treated as a handle
  4796. // TODO: handle: Make sure the right hand value is a handle
  4797. if( CompileOverloadedDualOperator(opNode, lctx, rctx, ctx) )
  4798. {
  4799. // An overloaded assignment operator was found (or a compilation error occured)
  4800. return 0;
  4801. }
  4802. // The object must implement the opAssign method
  4803. Error(TXT_NO_APPROPRIATE_OPASSIGN, opNode);
  4804. return -1;
  4805. }
  4806. else
  4807. {
  4808. asCDataType dt = lctx->type.dataType;
  4809. dt.MakeReference(false);
  4810. PrepareArgument(&dt, rctx, rexpr, true, 1);
  4811. if( !dt.IsEqualExceptRefAndConst(rctx->type.dataType) )
  4812. {
  4813. asCString str;
  4814. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format().AddressOf(), lctx->type.dataType.Format().AddressOf());
  4815. Error(str.AddressOf(), rexpr);
  4816. return -1;
  4817. }
  4818. MergeExprBytecode(ctx, rctx);
  4819. MergeExprBytecode(ctx, lctx);
  4820. ctx->bc.InstrWORD(asBC_GETOBJREF, AS_PTR_SIZE);
  4821. PerformAssignment(&lctx->type, &rctx->type, &ctx->bc, opNode);
  4822. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  4823. ctx->type = rctx->type;
  4824. }
  4825. }
  4826. else // if( lctx->type.dataType.IsObject() )
  4827. {
  4828. // An ASHANDLE type must not allow a value assignment, as
  4829. // the opAssign operator is used for the handle assignment
  4830. if( lctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE )
  4831. {
  4832. asCString str;
  4833. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format().AddressOf());
  4834. Error(str.AddressOf(), lexpr);
  4835. return -1;
  4836. }
  4837. // The lvalue reference may be marked as a temporary, if for example
  4838. // it was originated as a handle returned from a function. In such
  4839. // cases it must be possible to assign values to it anyway.
  4840. if( lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle )
  4841. {
  4842. // Convert the handle to a object reference
  4843. asCDataType to;
  4844. to = lctx->type.dataType;
  4845. to.MakeHandle(false);
  4846. ImplicitConversion(lctx, to, lexpr, asIC_IMPLICIT_CONV);
  4847. lctx->type.isLValue = true; // Handle may not have been an lvalue, but the dereferenced object is
  4848. }
  4849. // Check for overloaded assignment operator
  4850. if( CompileOverloadedDualOperator(opNode, lctx, rctx, ctx) )
  4851. {
  4852. // An overloaded assignment operator was found (or a compilation error occured)
  4853. return 0;
  4854. }
  4855. // No registered operator was found. In case the operation is a direct
  4856. // assignment and the rvalue is the same type as the lvalue, then we can
  4857. // still use the byte-for-byte copy to do the assignment
  4858. if( op != ttAssignment )
  4859. {
  4860. asCString str;
  4861. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format().AddressOf());
  4862. Error(str.AddressOf(), lexpr);
  4863. return -1;
  4864. }
  4865. // If the left hand expression is simple, i.e. without any
  4866. // function calls or allocations of memory, then we can avoid
  4867. // doing a copy of the right hand expression (done by PrepareArgument).
  4868. // Instead the reference to the value can be placed directly on the
  4869. // stack.
  4870. //
  4871. // This optimization should only be done for value types, where
  4872. // the application developer is responsible for making the
  4873. // implementation safe against unwanted destruction of the input
  4874. // reference before the time.
  4875. bool simpleExpr = (lctx->type.dataType.GetObjectType()->GetFlags() & asOBJ_VALUE) && lctx->bc.IsSimpleExpression();
  4876. // Implicitly convert the rvalue to the type of the lvalue
  4877. if( !lctx->type.dataType.IsEqualExceptRefAndConst(rctx->type.dataType) )
  4878. simpleExpr = false;
  4879. if( !simpleExpr )
  4880. {
  4881. asCDataType dt = lctx->type.dataType;
  4882. dt.MakeReference(true);
  4883. dt.MakeReadOnly(true);
  4884. PrepareArgument(&dt, rctx, rexpr, true, 1);
  4885. if( !dt.IsEqualExceptRefAndConst(rctx->type.dataType) )
  4886. {
  4887. asCString str;
  4888. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format().AddressOf(), lctx->type.dataType.Format().AddressOf());
  4889. Error(str.AddressOf(), rexpr);
  4890. return -1;
  4891. }
  4892. }
  4893. else if( rctx->type.dataType.IsReference() && (!(rctx->type.isVariable || rctx->type.isTemporary) || IsVariableOnHeap(rctx->type.stackOffset)) )
  4894. rctx->bc.Instr(asBC_RDSPTR);
  4895. MergeExprBytecode(ctx, rctx);
  4896. MergeExprBytecode(ctx, lctx);
  4897. if( !simpleExpr )
  4898. {
  4899. if( (rctx->type.isVariable || rctx->type.isTemporary) && !IsVariableOnHeap(rctx->type.stackOffset) )
  4900. // TODO: optimize: Actually the reference can be pushed on the stack directly
  4901. // as the value allocated on the stack is guaranteed to be safe
  4902. ctx->bc.InstrWORD(asBC_GETREF, AS_PTR_SIZE);
  4903. else
  4904. ctx->bc.InstrWORD(asBC_GETOBJREF, AS_PTR_SIZE);
  4905. }
  4906. PerformAssignment(&lctx->type, &rctx->type, &ctx->bc, opNode);
  4907. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  4908. ctx->type = lctx->type;
  4909. }
  4910. return 0;
  4911. }
  4912. int asCCompiler::CompileAssignment(asCScriptNode *expr, asSExprContext *ctx)
  4913. {
  4914. asCScriptNode *lexpr = expr->firstChild;
  4915. if( lexpr->next )
  4916. {
  4917. // Compile the two expression terms
  4918. asSExprContext lctx(engine), rctx(engine);
  4919. int rr = CompileAssignment(lexpr->next->next, &rctx);
  4920. int lr = CompileCondition(lexpr, &lctx);
  4921. if( lr >= 0 && rr >= 0 )
  4922. return DoAssignment(ctx, &lctx, &rctx, lexpr, lexpr->next->next, lexpr->next->tokenType, lexpr->next);
  4923. // Since the operands failed, the assignment was not computed
  4924. ctx->type.SetDummy();
  4925. return -1;
  4926. }
  4927. return CompileCondition(lexpr, ctx);
  4928. }
  4929. int asCCompiler::CompileCondition(asCScriptNode *expr, asSExprContext *ctx)
  4930. {
  4931. asCTypeInfo ctype;
  4932. // Compile the conditional expression
  4933. asCScriptNode *cexpr = expr->firstChild;
  4934. if( cexpr->next )
  4935. {
  4936. //-------------------------------
  4937. // Compile the condition
  4938. asSExprContext e(engine);
  4939. int r = CompileExpression(cexpr, &e);
  4940. if( r < 0 )
  4941. e.type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  4942. if( r >= 0 && !e.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  4943. {
  4944. Error(TXT_EXPR_MUST_BE_BOOL, cexpr);
  4945. e.type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  4946. }
  4947. ctype = e.type;
  4948. ProcessPropertyGetAccessor(&e, cexpr);
  4949. if( e.type.dataType.IsReference() ) ConvertToVariable(&e);
  4950. ProcessDeferredParams(&e);
  4951. //-------------------------------
  4952. // Compile the left expression
  4953. asSExprContext le(engine);
  4954. int lr = CompileAssignment(cexpr->next, &le);
  4955. //-------------------------------
  4956. // Compile the right expression
  4957. asSExprContext re(engine);
  4958. int rr = CompileAssignment(cexpr->next->next, &re);
  4959. if( lr >= 0 && rr >= 0 )
  4960. {
  4961. ProcessPropertyGetAccessor(&le, cexpr->next);
  4962. ProcessPropertyGetAccessor(&re, cexpr->next->next);
  4963. bool isExplicitHandle = le.type.isExplicitHandle || re.type.isExplicitHandle;
  4964. // Allow a 0 or null in the first case to be implicitly converted to the second type
  4965. if( le.type.isConstant && le.type.intValue == 0 && le.type.dataType.IsUnsignedType() )
  4966. {
  4967. asCDataType to = re.type.dataType;
  4968. to.MakeReference(false);
  4969. to.MakeReadOnly(true);
  4970. ImplicitConversionConstant(&le, to, cexpr->next, asIC_IMPLICIT_CONV);
  4971. }
  4972. else if( le.type.IsNullConstant() )
  4973. {
  4974. asCDataType to = re.type.dataType;
  4975. to.MakeHandle(true);
  4976. ImplicitConversion(&le, to, cexpr->next, asIC_IMPLICIT_CONV);
  4977. }
  4978. //---------------------------------
  4979. // Output the byte code
  4980. int afterLabel = nextLabel++;
  4981. int elseLabel = nextLabel++;
  4982. // If left expression is void, then we don't need to store the result
  4983. if( le.type.dataType.IsEqualExceptConst(asCDataType::CreatePrimitive(ttVoid, false)) )
  4984. {
  4985. // Put the code for the condition expression on the output
  4986. MergeExprBytecode(ctx, &e);
  4987. // Added the branch decision
  4988. ctx->type = e.type;
  4989. ConvertToVariable(ctx);
  4990. ctx->bc.InstrSHORT(asBC_CpyVtoR4, ctx->type.stackOffset);
  4991. ctx->bc.Instr(asBC_ClrHi);
  4992. ctx->bc.InstrDWORD(asBC_JZ, elseLabel);
  4993. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4994. // Add the left expression
  4995. MergeExprBytecode(ctx, &le);
  4996. ctx->bc.InstrINT(asBC_JMP, afterLabel);
  4997. // Add the right expression
  4998. ctx->bc.Label((short)elseLabel);
  4999. MergeExprBytecode(ctx, &re);
  5000. ctx->bc.Label((short)afterLabel);
  5001. // Make sure both expressions have the same type
  5002. if( le.type.dataType != re.type.dataType )
  5003. Error(TXT_BOTH_MUST_BE_SAME, expr);
  5004. // Set the type of the result
  5005. ctx->type = le.type;
  5006. }
  5007. else
  5008. {
  5009. // Allocate temporary variable and copy the result to that one
  5010. asCTypeInfo temp;
  5011. temp = le.type;
  5012. temp.dataType.MakeReference(false);
  5013. temp.dataType.MakeReadOnly(false);
  5014. // Make sure the variable isn't used in the initial expression
  5015. int offset = AllocateVariableNotIn(temp.dataType, true, false, &e);
  5016. temp.SetVariable(temp.dataType, offset, true);
  5017. // TODO: copy: Use copy constructor if available. See PrepareTemporaryObject()
  5018. CallDefaultConstructor(temp.dataType, offset, IsVariableOnHeap(offset), &ctx->bc, expr);
  5019. // Put the code for the condition expression on the output
  5020. MergeExprBytecode(ctx, &e);
  5021. // Add the branch decision
  5022. ctx->type = e.type;
  5023. ConvertToVariable(ctx);
  5024. ctx->bc.InstrSHORT(asBC_CpyVtoR4, ctx->type.stackOffset);
  5025. ctx->bc.Instr(asBC_ClrHi);
  5026. ctx->bc.InstrDWORD(asBC_JZ, elseLabel);
  5027. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5028. // Assign the result of the left expression to the temporary variable
  5029. asCTypeInfo rtemp;
  5030. rtemp = temp;
  5031. if( rtemp.dataType.IsObjectHandle() )
  5032. rtemp.isExplicitHandle = true;
  5033. PrepareForAssignment(&rtemp.dataType, &le, cexpr->next, true);
  5034. MergeExprBytecode(ctx, &le);
  5035. if( !rtemp.dataType.IsPrimitive() )
  5036. {
  5037. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  5038. rtemp.dataType.MakeReference(IsVariableOnHeap(offset));
  5039. }
  5040. PerformAssignment(&rtemp, &le.type, &ctx->bc, cexpr->next);
  5041. if( !rtemp.dataType.IsPrimitive() )
  5042. ctx->bc.Pop(le.type.dataType.GetSizeOnStackDWords()); // Pop the original value
  5043. // Release the old temporary variable
  5044. ReleaseTemporaryVariable(le.type, &ctx->bc);
  5045. ctx->bc.InstrINT(asBC_JMP, afterLabel);
  5046. // Start of the right expression
  5047. ctx->bc.Label((short)elseLabel);
  5048. // Copy the result to the same temporary variable
  5049. PrepareForAssignment(&rtemp.dataType, &re, cexpr->next, true);
  5050. MergeExprBytecode(ctx, &re);
  5051. if( !rtemp.dataType.IsPrimitive() )
  5052. {
  5053. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  5054. rtemp.dataType.MakeReference(IsVariableOnHeap(offset));
  5055. }
  5056. PerformAssignment(&rtemp, &re.type, &ctx->bc, cexpr->next);
  5057. if( !rtemp.dataType.IsPrimitive() )
  5058. ctx->bc.Pop(le.type.dataType.GetSizeOnStackDWords()); // Pop the original value
  5059. // Release the old temporary variable
  5060. ReleaseTemporaryVariable(re.type, &ctx->bc);
  5061. ctx->bc.Label((short)afterLabel);
  5062. // Make sure both expressions have the same type
  5063. if( le.type.dataType != re.type.dataType )
  5064. Error(TXT_BOTH_MUST_BE_SAME, expr);
  5065. // Set the temporary variable as output
  5066. ctx->type = rtemp;
  5067. ctx->type.isExplicitHandle = isExplicitHandle;
  5068. if( !ctx->type.dataType.IsPrimitive() )
  5069. {
  5070. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  5071. ctx->type.dataType.MakeReference(IsVariableOnHeap(offset));
  5072. }
  5073. // Make sure the output isn't marked as being a literal constant
  5074. ctx->type.isConstant = false;
  5075. }
  5076. }
  5077. else
  5078. {
  5079. ctx->type.SetDummy();
  5080. return -1;
  5081. }
  5082. }
  5083. else
  5084. return CompileExpression(cexpr, ctx);
  5085. return 0;
  5086. }
  5087. int asCCompiler::CompileExpression(asCScriptNode *expr, asSExprContext *ctx)
  5088. {
  5089. asASSERT(expr->nodeType == snExpression);
  5090. // Count the nodes
  5091. int count = 0;
  5092. asCScriptNode *node = expr->firstChild;
  5093. while( node )
  5094. {
  5095. count++;
  5096. node = node->next;
  5097. }
  5098. // Convert to polish post fix, i.e: a+b => ab+
  5099. asCArray<asCScriptNode *> stack(count);
  5100. asCArray<asCScriptNode *> stack2(count);
  5101. asCArray<asCScriptNode *> postfix(count);
  5102. node = expr->firstChild;
  5103. while( node )
  5104. {
  5105. int precedence = GetPrecedence(node);
  5106. while( stack.GetLength() > 0 &&
  5107. precedence <= GetPrecedence(stack[stack.GetLength()-1]) )
  5108. stack2.PushLast(stack.PopLast());
  5109. stack.PushLast(node);
  5110. node = node->next;
  5111. }
  5112. while( stack.GetLength() > 0 )
  5113. stack2.PushLast(stack.PopLast());
  5114. // We need to swap operands so that the left
  5115. // operand is always computed before the right
  5116. SwapPostFixOperands(stack2, postfix);
  5117. // Compile the postfix formatted expression
  5118. return CompilePostFixExpression(&postfix, ctx);
  5119. }
  5120. void asCCompiler::SwapPostFixOperands(asCArray<asCScriptNode *> &postfix, asCArray<asCScriptNode *> &target)
  5121. {
  5122. if( postfix.GetLength() == 0 ) return;
  5123. asCScriptNode *node = postfix.PopLast();
  5124. if( node->nodeType == snExprTerm )
  5125. {
  5126. target.PushLast(node);
  5127. return;
  5128. }
  5129. SwapPostFixOperands(postfix, target);
  5130. SwapPostFixOperands(postfix, target);
  5131. target.PushLast(node);
  5132. }
  5133. int asCCompiler::CompilePostFixExpression(asCArray<asCScriptNode *> *postfix, asSExprContext *ctx)
  5134. {
  5135. // Shouldn't send any byte code
  5136. asASSERT(ctx->bc.GetLastInstr() == -1);
  5137. // Set the context to a dummy type to avoid further
  5138. // errors in case the expression fails to compile
  5139. ctx->type.SetDummy();
  5140. // Pop the last node
  5141. asCScriptNode *node = postfix->PopLast();
  5142. ctx->exprNode = node;
  5143. // If term, compile the term
  5144. if( node->nodeType == snExprTerm )
  5145. return CompileExpressionTerm(node, ctx);
  5146. // Compile the two expression terms
  5147. asSExprContext r(engine), l(engine);
  5148. int ret;
  5149. ret = CompilePostFixExpression(postfix, &l); if( ret < 0 ) return ret;
  5150. ret = CompilePostFixExpression(postfix, &r); if( ret < 0 ) return ret;
  5151. // Compile the operation
  5152. return CompileOperator(node, &l, &r, ctx);
  5153. }
  5154. int asCCompiler::CompileExpressionTerm(asCScriptNode *node, asSExprContext *ctx)
  5155. {
  5156. // Shouldn't send any byte code
  5157. asASSERT(ctx->bc.GetLastInstr() == -1);
  5158. // Set the type as a dummy by default, in case of any compiler errors
  5159. ctx->type.SetDummy();
  5160. // Compile the value node
  5161. asCScriptNode *vnode = node->firstChild;
  5162. while( vnode->nodeType != snExprValue )
  5163. vnode = vnode->next;
  5164. asSExprContext v(engine);
  5165. int r = CompileExpressionValue(vnode, &v); if( r < 0 ) return r;
  5166. // Compile post fix operators
  5167. asCScriptNode *pnode = vnode->next;
  5168. while( pnode )
  5169. {
  5170. r = CompileExpressionPostOp(pnode, &v); if( r < 0 ) return r;
  5171. pnode = pnode->next;
  5172. }
  5173. // Compile pre fix operators
  5174. pnode = vnode->prev;
  5175. while( pnode )
  5176. {
  5177. r = CompileExpressionPreOp(pnode, &v); if( r < 0 ) return r;
  5178. pnode = pnode->prev;
  5179. }
  5180. // Return the byte code and final type description
  5181. MergeExprBytecodeAndType(ctx, &v);
  5182. return 0;
  5183. }
  5184. int asCCompiler::CompileVariableAccess(const asCString &name, const asCString &scope, asSExprContext *ctx, asCScriptNode *errNode, bool isOptional, bool noFunction, asCObjectType *objType)
  5185. {
  5186. bool found = false;
  5187. // It is a local variable or parameter?
  5188. // This is not accessible by default arg expressions
  5189. sVariable *v = 0;
  5190. if( !isCompilingDefaultArg && scope == "" && !objType )
  5191. v = variables->GetVariable(name.AddressOf());
  5192. if( v )
  5193. {
  5194. found = true;
  5195. if( v->isPureConstant )
  5196. ctx->type.SetConstantQW(v->type, v->constantValue);
  5197. else if( v->type.IsPrimitive() )
  5198. {
  5199. if( v->type.IsReference() )
  5200. {
  5201. // Copy the reference into the register
  5202. #if AS_PTR_SIZE == 1
  5203. ctx->bc.InstrSHORT(asBC_CpyVtoR4, (short)v->stackOffset);
  5204. #else
  5205. ctx->bc.InstrSHORT(asBC_CpyVtoR8, (short)v->stackOffset);
  5206. #endif
  5207. ctx->type.Set(v->type);
  5208. }
  5209. else
  5210. ctx->type.SetVariable(v->type, v->stackOffset, false);
  5211. ctx->type.isLValue = true;
  5212. }
  5213. else
  5214. {
  5215. ctx->bc.InstrSHORT(asBC_PSF, (short)v->stackOffset);
  5216. ctx->type.SetVariable(v->type, v->stackOffset, false);
  5217. // If the variable is allocated on the heap we have a reference,
  5218. // otherwise the actual object pointer is pushed on the stack.
  5219. if( v->onHeap || v->type.IsObjectHandle() ) ctx->type.dataType.MakeReference(true);
  5220. // Implicitly dereference handle parameters sent by reference
  5221. if( v->type.IsReference() && (!v->type.IsObject() || v->type.IsObjectHandle()) )
  5222. ctx->bc.Instr(asBC_RDSPTR);
  5223. ctx->type.isLValue = true;
  5224. }
  5225. }
  5226. // Is it a class member?
  5227. // This is not accessible by default arg expressions
  5228. if( !isCompilingDefaultArg && !found && ((objType) || (outFunc && outFunc->objectType && scope == "")) )
  5229. {
  5230. if( name == THIS_TOKEN && !objType )
  5231. {
  5232. asCDataType dt = asCDataType::CreateObject(outFunc->objectType, outFunc->isReadOnly);
  5233. // The object pointer is located at stack position 0
  5234. ctx->bc.InstrSHORT(asBC_PSF, 0);
  5235. ctx->type.SetVariable(dt, 0, false);
  5236. ctx->type.dataType.MakeReference(true);
  5237. ctx->type.isLValue = true;
  5238. found = true;
  5239. }
  5240. if( !found )
  5241. {
  5242. // See if there are any matching property accessors
  5243. asSExprContext access(engine);
  5244. if( objType )
  5245. access.type.Set(asCDataType::CreateObject(objType, false));
  5246. else
  5247. access.type.Set(asCDataType::CreateObject(outFunc->objectType, outFunc->isReadOnly));
  5248. access.type.dataType.MakeReference(true);
  5249. int r = 0;
  5250. if( errNode->next && errNode->next->tokenType == ttOpenBracket )
  5251. {
  5252. // This is an index access, check if there is a property accessor that takes an index arg
  5253. asSExprContext dummyArg(engine);
  5254. r = FindPropertyAccessor(name, &access, &dummyArg, errNode, true);
  5255. }
  5256. if( r == 0 )
  5257. {
  5258. // Normal property access
  5259. r = FindPropertyAccessor(name, &access, errNode, true);
  5260. }
  5261. if( r < 0 ) return -1;
  5262. if( access.property_get || access.property_set )
  5263. {
  5264. if( !objType )
  5265. {
  5266. // Prepare the bytecode for the member access
  5267. // This is only done when accessing through the implicit this pointer
  5268. ctx->bc.InstrSHORT(asBC_PSF, 0);
  5269. }
  5270. MergeExprBytecodeAndType(ctx, &access);
  5271. found = true;
  5272. }
  5273. }
  5274. if( !found )
  5275. {
  5276. asCDataType dt;
  5277. if( objType )
  5278. dt = asCDataType::CreateObject(objType, false);
  5279. else
  5280. dt = asCDataType::CreateObject(outFunc->objectType, false);
  5281. asCObjectProperty *prop = builder->GetObjectProperty(dt, name.AddressOf());
  5282. if( prop )
  5283. {
  5284. if( !objType )
  5285. {
  5286. // The object pointer is located at stack position 0
  5287. // This is only done when accessing through the implicit this pointer
  5288. ctx->bc.InstrSHORT(asBC_PSF, 0);
  5289. ctx->type.SetVariable(dt, 0, false);
  5290. ctx->type.dataType.MakeReference(true);
  5291. Dereference(ctx, true);
  5292. }
  5293. // TODO: This is the same as what is in CompileExpressionPostOp
  5294. // Put the offset on the stack
  5295. ctx->bc.InstrSHORT_DW(asBC_ADDSi, (short)prop->byteOffset, engine->GetTypeIdFromDataType(dt));
  5296. if( prop->type.IsReference() )
  5297. ctx->bc.Instr(asBC_RDSPTR);
  5298. // Reference to primitive must be stored in the temp register
  5299. if( prop->type.IsPrimitive() )
  5300. {
  5301. // TODO: optimize: The ADD offset command should store the reference in the register directly
  5302. ctx->bc.Instr(asBC_PopRPtr);
  5303. }
  5304. // Set the new type (keeping info about temp variable)
  5305. ctx->type.dataType = prop->type;
  5306. ctx->type.dataType.MakeReference(true);
  5307. ctx->type.isVariable = false;
  5308. ctx->type.isLValue = true;
  5309. if( ctx->type.dataType.IsObject() && !ctx->type.dataType.IsObjectHandle() )
  5310. {
  5311. // Objects that are members are not references
  5312. ctx->type.dataType.MakeReference(false);
  5313. }
  5314. // If the object reference is const, the property will also be const
  5315. ctx->type.dataType.MakeReadOnly(outFunc->isReadOnly);
  5316. found = true;
  5317. }
  5318. }
  5319. }
  5320. // Is it a global property?
  5321. if( !found && (scope == "" || scope == "::") && !objType )
  5322. {
  5323. // See if there are any matching global property accessors
  5324. asSExprContext access(engine);
  5325. int r = 0;
  5326. if( errNode->next && errNode->next->tokenType == ttOpenBracket )
  5327. {
  5328. // This is an index access, check if there is a property accessor that takes an index arg
  5329. asSExprContext dummyArg(engine);
  5330. r = FindPropertyAccessor(name, &access, &dummyArg, errNode);
  5331. }
  5332. if( r == 0 )
  5333. {
  5334. // Normal property access
  5335. r = FindPropertyAccessor(name, &access, errNode);
  5336. }
  5337. if( r < 0 ) return -1;
  5338. if( access.property_get || access.property_set )
  5339. {
  5340. // Prepare the bytecode for the function call
  5341. MergeExprBytecodeAndType(ctx, &access);
  5342. found = true;
  5343. }
  5344. // See if there is any matching global property
  5345. if( !found )
  5346. {
  5347. bool isCompiled = true;
  5348. bool isPureConstant = false;
  5349. bool isAppProp = false;
  5350. asQWORD constantValue;
  5351. asCGlobalProperty *prop = builder->GetGlobalProperty(name.AddressOf(), &isCompiled, &isPureConstant, &constantValue, &isAppProp);
  5352. if( prop )
  5353. {
  5354. found = true;
  5355. // Verify that the global property has been compiled already
  5356. if( isCompiled )
  5357. {
  5358. if( ctx->type.dataType.GetObjectType() && (ctx->type.dataType.GetObjectType()->flags & asOBJ_IMPLICIT_HANDLE) )
  5359. {
  5360. ctx->type.dataType.MakeHandle(true);
  5361. ctx->type.isExplicitHandle = true;
  5362. }
  5363. // If the global property is a pure constant
  5364. // we can allow the compiler to optimize it. Pure
  5365. // constants are global constant variables that were
  5366. // initialized by literal constants.
  5367. if( isPureConstant )
  5368. ctx->type.SetConstantQW(prop->type, constantValue);
  5369. else
  5370. {
  5371. // A shared type must not access global vars, unless they
  5372. // too are shared, e.g. application registered vars
  5373. if( outFunc->objectType && outFunc->objectType->IsShared() )
  5374. {
  5375. if( !isAppProp )
  5376. {
  5377. asCString str;
  5378. str.Format(TXT_SHARED_CANNOT_ACCESS_NON_SHARED_VAR_s, prop->name.AddressOf());
  5379. Error(str.AddressOf(), errNode);
  5380. // Allow the compilation to continue to catch other problems
  5381. }
  5382. }
  5383. ctx->type.Set(prop->type);
  5384. ctx->type.dataType.MakeReference(true);
  5385. ctx->type.isLValue = true;
  5386. if( ctx->type.dataType.IsPrimitive() )
  5387. {
  5388. // Load the address of the variable into the register
  5389. ctx->bc.InstrPTR(asBC_LDG, engine->globalProperties[prop->id]->GetAddressOfValue());
  5390. }
  5391. else
  5392. {
  5393. // Push the address of the variable on the stack
  5394. ctx->bc.InstrPTR(asBC_PGA, engine->globalProperties[prop->id]->GetAddressOfValue());
  5395. // If the object is a value type, then we must validate the existance,
  5396. // as it could potentially be accessed before it is initialized.
  5397. if( ctx->type.dataType.GetObjectType()->flags & asOBJ_VALUE ||
  5398. !ctx->type.dataType.IsObjectHandle() )
  5399. {
  5400. // TODO: optimize: This is not necessary for application registered properties
  5401. ctx->bc.Instr(asBC_ChkRefS);
  5402. }
  5403. }
  5404. }
  5405. }
  5406. else
  5407. {
  5408. asCString str;
  5409. str.Format(TXT_UNINITIALIZED_GLOBAL_VAR_s, prop->name.AddressOf());
  5410. Error(str.AddressOf(), errNode);
  5411. return -1;
  5412. }
  5413. }
  5414. }
  5415. }
  5416. // Is it the name of a global function?
  5417. if( !noFunction && !found && (scope == "" || scope == "::") && !objType )
  5418. {
  5419. asCArray<int> funcs;
  5420. builder->GetFunctionDescriptions(name.AddressOf(), funcs);
  5421. if( funcs.GetLength() > 1 )
  5422. {
  5423. // TODO: funcdef: If multiple functions are found, then the compiler should defer the decision
  5424. // to which one it should use until the value is actually used.
  5425. //
  5426. // - assigning the function pointer to a variable
  5427. // - performing an explicit cast
  5428. // - passing the function pointer to a function as parameter
  5429. asCString str;
  5430. str.Format(TXT_MULTIPLE_MATCHING_SIGNATURES_TO_s, name.AddressOf());
  5431. Error(str.AddressOf(), errNode);
  5432. return -1;
  5433. }
  5434. else if( funcs.GetLength() == 1 )
  5435. {
  5436. found = true;
  5437. // A shared object may not access global functions unless they too are shared (e.g. registered functions)
  5438. if( !builder->GetFunctionDescription(funcs[0])->IsShared() &&
  5439. outFunc->objectType && outFunc->objectType->IsShared() )
  5440. {
  5441. asCString msg;
  5442. msg.Format(TXT_SHARED_CANNOT_CALL_NON_SHARED_FUNC_s, builder->GetFunctionDescription(funcs[0])->GetDeclaration());
  5443. Error(msg.AddressOf(), errNode);
  5444. return -1;
  5445. }
  5446. // Push the function pointer on the stack
  5447. ctx->bc.InstrPTR(asBC_FuncPtr, builder->GetFunctionDescription(funcs[0]));
  5448. ctx->type.Set(asCDataType::CreateFuncDef(builder->GetFunctionDescription(funcs[0])));
  5449. }
  5450. }
  5451. // Is it an enum value?
  5452. if( !found && !objType )
  5453. {
  5454. asCObjectType *scopeType = 0;
  5455. if( scope != "" )
  5456. {
  5457. // resolve the type before the scope
  5458. scopeType = builder->GetObjectType( scope.AddressOf() );
  5459. }
  5460. asDWORD value = 0;
  5461. asCDataType dt;
  5462. if( scopeType && builder->GetEnumValueFromObjectType(scopeType, name.AddressOf(), dt, value) )
  5463. {
  5464. // scoped enum value found
  5465. found = true;
  5466. }
  5467. else if( scope == "" && !engine->ep.requireEnumScope )
  5468. {
  5469. // look for the enum value with no namespace
  5470. int e = builder->GetEnumValue(name.AddressOf(), dt, value);
  5471. if( e )
  5472. {
  5473. found = true;
  5474. if( e == 2 )
  5475. {
  5476. Error(TXT_FOUND_MULTIPLE_ENUM_VALUES, errNode);
  5477. }
  5478. }
  5479. }
  5480. if( found )
  5481. {
  5482. // Even if the enum type is not shared, and we're compiling a shared object,
  5483. // the use of the values are still allowed, since they are treated as constants.
  5484. // an enum value was resolved
  5485. ctx->type.SetConstantDW(dt, value);
  5486. }
  5487. }
  5488. // The name doesn't match any variable
  5489. if( !found )
  5490. {
  5491. // Give dummy value
  5492. ctx->type.SetDummy();
  5493. if( !isOptional )
  5494. {
  5495. // Prepend the scope to the name for the error message
  5496. asCString ename;
  5497. if( scope != "" && scope != "::" )
  5498. ename = scope + "::";
  5499. else
  5500. ename = scope;
  5501. ename += name;
  5502. asCString str;
  5503. str.Format(TXT_s_NOT_DECLARED, ename.AddressOf());
  5504. Error(str.AddressOf(), errNode);
  5505. // Declare the variable now so that it will not be reported again
  5506. variables->DeclareVariable(name.AddressOf(), asCDataType::CreatePrimitive(ttInt, false), 0x7FFF, true);
  5507. // Mark the variable as initialized so that the user will not be bother by it again
  5508. sVariable *v = variables->GetVariable(name.AddressOf());
  5509. asASSERT(v);
  5510. if( v ) v->isInitialized = true;
  5511. }
  5512. // Return -1 to signal that the variable wasn't found
  5513. return -1;
  5514. }
  5515. return 0;
  5516. }
  5517. int asCCompiler::CompileExpressionValue(asCScriptNode *node, asSExprContext *ctx)
  5518. {
  5519. // Shouldn't receive any byte code
  5520. asASSERT(ctx->bc.GetLastInstr() == -1);
  5521. asCScriptNode *vnode = node->firstChild;
  5522. ctx->exprNode = vnode;
  5523. if( vnode->nodeType == snVariableAccess )
  5524. {
  5525. // Determine the scope resolution of the variable
  5526. asCString scope = GetScopeFromNode(vnode);
  5527. // Determine the name of the variable
  5528. vnode = vnode->lastChild;
  5529. asASSERT(vnode->nodeType == snIdentifier );
  5530. asCString name(&script->code[vnode->tokenPos], vnode->tokenLength);
  5531. return CompileVariableAccess(name, scope, ctx, node);
  5532. }
  5533. else if( vnode->nodeType == snConstant )
  5534. {
  5535. if( vnode->tokenType == ttIntConstant )
  5536. {
  5537. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  5538. asQWORD val = asStringScanUInt64(value.AddressOf(), 10, 0);
  5539. // Do we need 64 bits?
  5540. if( val>>32 )
  5541. ctx->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), val);
  5542. else
  5543. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), asDWORD(val));
  5544. }
  5545. else if( vnode->tokenType == ttBitsConstant )
  5546. {
  5547. asCString value(&script->code[vnode->tokenPos+2], vnode->tokenLength-2);
  5548. // TODO: Check for overflow
  5549. asQWORD val = asStringScanUInt64(value.AddressOf(), 16, 0);
  5550. // Do we need 64 bits?
  5551. if( val>>32 )
  5552. ctx->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), val);
  5553. else
  5554. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), asDWORD(val));
  5555. }
  5556. else if( vnode->tokenType == ttFloatConstant )
  5557. {
  5558. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  5559. // TODO: Check for overflow
  5560. size_t numScanned;
  5561. float v = float(asStringScanDouble(value.AddressOf(), &numScanned));
  5562. ctx->type.SetConstantF(asCDataType::CreatePrimitive(ttFloat, true), v);
  5563. #ifndef AS_USE_DOUBLE_AS_FLOAT
  5564. // Don't check this if we have double as float, because then the whole token would be scanned (i.e. no f suffix)
  5565. asASSERT(numScanned == vnode->tokenLength - 1);
  5566. #endif
  5567. }
  5568. else if( vnode->tokenType == ttDoubleConstant )
  5569. {
  5570. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  5571. // TODO: Check for overflow
  5572. size_t numScanned;
  5573. double v = asStringScanDouble(value.AddressOf(), &numScanned);
  5574. ctx->type.SetConstantD(asCDataType::CreatePrimitive(ttDouble, true), v);
  5575. asASSERT(numScanned == vnode->tokenLength);
  5576. }
  5577. else if( vnode->tokenType == ttTrue ||
  5578. vnode->tokenType == ttFalse )
  5579. {
  5580. #if AS_SIZEOF_BOOL == 1
  5581. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), vnode->tokenType == ttTrue ? VALUE_OF_BOOLEAN_TRUE : 0);
  5582. #else
  5583. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), vnode->tokenType == ttTrue ? VALUE_OF_BOOLEAN_TRUE : 0);
  5584. #endif
  5585. }
  5586. else if( vnode->tokenType == ttStringConstant ||
  5587. vnode->tokenType == ttMultilineStringConstant ||
  5588. vnode->tokenType == ttHeredocStringConstant )
  5589. {
  5590. asCString str;
  5591. asCScriptNode *snode = vnode->firstChild;
  5592. if( script->code[snode->tokenPos] == '\'' && engine->ep.useCharacterLiterals )
  5593. {
  5594. // Treat the single quoted string as a single character literal
  5595. str.Assign(&script->code[snode->tokenPos+1], snode->tokenLength-2);
  5596. asDWORD val = 0;
  5597. if( str.GetLength() && (unsigned char)str[0] > 127 && engine->ep.scanner == 1 )
  5598. {
  5599. // This is the start of a UTF8 encoded character. We need to decode it
  5600. val = asStringDecodeUTF8(str.AddressOf(), 0);
  5601. if( val == (asDWORD)-1 )
  5602. Error(TXT_INVALID_CHAR_LITERAL, vnode);
  5603. }
  5604. else
  5605. {
  5606. val = ProcessStringConstant(str, snode);
  5607. if( val == (asDWORD)-1 )
  5608. Error(TXT_INVALID_CHAR_LITERAL, vnode);
  5609. }
  5610. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), val);
  5611. }
  5612. else
  5613. {
  5614. // Process the string constants
  5615. while( snode )
  5616. {
  5617. asCString cat;
  5618. if( snode->tokenType == ttStringConstant )
  5619. {
  5620. cat.Assign(&script->code[snode->tokenPos+1], snode->tokenLength-2);
  5621. ProcessStringConstant(cat, snode);
  5622. }
  5623. else if( snode->tokenType == ttMultilineStringConstant )
  5624. {
  5625. if( !engine->ep.allowMultilineStrings )
  5626. Error(TXT_MULTILINE_STRINGS_NOT_ALLOWED, snode);
  5627. cat.Assign(&script->code[snode->tokenPos+1], snode->tokenLength-2);
  5628. ProcessStringConstant(cat, snode);
  5629. }
  5630. else if( snode->tokenType == ttHeredocStringConstant )
  5631. {
  5632. cat.Assign(&script->code[snode->tokenPos+3], snode->tokenLength-6);
  5633. ProcessHeredocStringConstant(cat, snode);
  5634. }
  5635. str += cat;
  5636. snode = snode->next;
  5637. }
  5638. // Call the string factory function to create a string object
  5639. asCScriptFunction *descr = engine->stringFactory;
  5640. if( descr == 0 )
  5641. {
  5642. // Error
  5643. Error(TXT_STRINGS_NOT_RECOGNIZED, vnode);
  5644. // Give dummy value
  5645. ctx->type.SetDummy();
  5646. return -1;
  5647. }
  5648. else
  5649. {
  5650. // Register the constant string with the engine
  5651. int id = engine->AddConstantString(str.AddressOf(), str.GetLength());
  5652. ctx->bc.InstrWORD(asBC_STR, (asWORD)id);
  5653. bool useVariable = false;
  5654. int stackOffset = 0;
  5655. #ifndef AS_OLD
  5656. if( descr->DoesReturnOnStack() )
  5657. {
  5658. useVariable = true;
  5659. stackOffset = AllocateVariable(descr->returnType, true);
  5660. ctx->bc.InstrSHORT(asBC_PSF, short(stackOffset));
  5661. }
  5662. #endif
  5663. PerformFunctionCall(descr->id, ctx, false, 0, 0, useVariable, stackOffset);
  5664. }
  5665. }
  5666. }
  5667. else if( vnode->tokenType == ttNull )
  5668. {
  5669. #ifndef AS_64BIT_PTR
  5670. ctx->bc.InstrDWORD(asBC_PshC4, 0);
  5671. #else
  5672. ctx->bc.InstrQWORD(asBC_PshC8, 0);
  5673. #endif
  5674. ctx->type.SetNullConstant();
  5675. }
  5676. else
  5677. asASSERT(false);
  5678. }
  5679. else if( vnode->nodeType == snFunctionCall )
  5680. {
  5681. // Determine the scope resolution
  5682. asCString scope = GetScopeFromNode(vnode);
  5683. if( outFunc && outFunc->objectType && scope != "::" )
  5684. {
  5685. // TODO: funcdef: There may be a local variable of a function type with the same name
  5686. // Check if a class method is being called
  5687. asCScriptNode *nm = vnode->lastChild->prev;
  5688. asCString name;
  5689. name.Assign(&script->code[nm->tokenPos], nm->tokenLength);
  5690. asCArray<int> funcs;
  5691. // If we're compiling a constructor and the name of the function called
  5692. // is 'super' then the base class' constructor is being called.
  5693. // super cannot be called from another scope, i.e. must not be prefixed
  5694. if( m_isConstructor && name == SUPER_TOKEN && nm->prev == 0 )
  5695. {
  5696. // Actually it is the base class' constructor that is being called,
  5697. // but as we won't use the actual function ids here we can take the
  5698. // object's own constructors and avoid the need to check if the
  5699. // object actually derives from any other class
  5700. funcs = outFunc->objectType->beh.constructors;
  5701. // Must not allow calling constructors multiple times
  5702. if( continueLabels.GetLength() > 0 )
  5703. {
  5704. // If a continue label is set we are in a loop
  5705. Error(TXT_CANNOT_CALL_CONSTRUCTOR_IN_LOOPS, vnode);
  5706. }
  5707. else if( breakLabels.GetLength() > 0 )
  5708. {
  5709. // TODO: inheritance: Should eventually allow constructors in switch statements
  5710. // If a break label is set we are either in a loop or a switch statements
  5711. Error(TXT_CANNOT_CALL_CONSTRUCTOR_IN_SWITCH, vnode);
  5712. }
  5713. else if( m_isConstructorCalled )
  5714. {
  5715. Error(TXT_CANNOT_CALL_CONSTRUCTOR_TWICE, vnode);
  5716. }
  5717. m_isConstructorCalled = true;
  5718. }
  5719. else
  5720. builder->GetObjectMethodDescriptions(name.AddressOf(), outFunc->objectType, funcs, false);
  5721. if( funcs.GetLength() )
  5722. {
  5723. asCDataType dt = asCDataType::CreateObject(outFunc->objectType, false);
  5724. // The object pointer is located at stack position 0
  5725. ctx->bc.InstrSHORT(asBC_PSF, 0);
  5726. ctx->type.SetVariable(dt, 0, false);
  5727. ctx->type.dataType.MakeReference(true);
  5728. // TODO: optimize: This adds a CHKREF. Is that really necessary?
  5729. Dereference(ctx, true);
  5730. return CompileFunctionCall(vnode, ctx, outFunc->objectType, false, scope);
  5731. }
  5732. }
  5733. return CompileFunctionCall(vnode, ctx, 0, false, scope);
  5734. }
  5735. else if( vnode->nodeType == snConstructCall )
  5736. {
  5737. CompileConstructCall(vnode, ctx);
  5738. }
  5739. else if( vnode->nodeType == snAssignment )
  5740. {
  5741. asSExprContext e(engine);
  5742. int r = CompileAssignment(vnode, &e);
  5743. if( r < 0 )
  5744. {
  5745. ctx->type.SetDummy();
  5746. return r;
  5747. }
  5748. MergeExprBytecodeAndType(ctx, &e);
  5749. }
  5750. else if( vnode->nodeType == snCast )
  5751. {
  5752. // Implement the cast operator
  5753. CompileConversion(vnode, ctx);
  5754. }
  5755. else
  5756. asASSERT(false);
  5757. return 0;
  5758. }
  5759. asCString asCCompiler::GetScopeFromNode(asCScriptNode *node)
  5760. {
  5761. asCString scope;
  5762. asCScriptNode *sn = node->firstChild;
  5763. if( sn->tokenType == ttScope )
  5764. {
  5765. // Global scope
  5766. scope = "::";
  5767. sn = sn->next;
  5768. }
  5769. else if( sn->next && sn->next->tokenType == ttScope )
  5770. {
  5771. scope.Assign(&script->code[sn->tokenPos], sn->tokenLength);
  5772. sn = sn->next->next;
  5773. }
  5774. if( scope != "" )
  5775. {
  5776. // We don't support multiple levels of scope yet
  5777. if( sn->next && sn->next->tokenType == ttScope )
  5778. {
  5779. Error(TXT_INVALID_SCOPE, sn->next);
  5780. }
  5781. }
  5782. return scope;
  5783. }
  5784. asUINT asCCompiler::ProcessStringConstant(asCString &cstr, asCScriptNode *node, bool processEscapeSequences)
  5785. {
  5786. int charLiteral = -1;
  5787. // Process escape sequences
  5788. asCArray<char> str((int)cstr.GetLength());
  5789. for( asUINT n = 0; n < cstr.GetLength(); n++ )
  5790. {
  5791. #ifdef AS_DOUBLEBYTE_CHARSET
  5792. // Double-byte charset is only allowed for ASCII and not UTF16 encoded strings
  5793. if( (cstr[n] & 0x80) && engine->ep.scanner == 0 && engine->ep.stringEncoding != 1 )
  5794. {
  5795. // This is the lead character of a double byte character
  5796. // include the trail character without checking it's value.
  5797. str.PushLast(cstr[n]);
  5798. n++;
  5799. str.PushLast(cstr[n]);
  5800. continue;
  5801. }
  5802. #endif
  5803. asUINT val;
  5804. if( processEscapeSequences && cstr[n] == '\\' )
  5805. {
  5806. ++n;
  5807. if( n == cstr.GetLength() )
  5808. {
  5809. if( charLiteral == -1 ) charLiteral = 0;
  5810. return charLiteral;
  5811. }
  5812. // TODO: Consider deprecating use of hexadecimal escape sequences,
  5813. // as they do not guarantee proper unicode sequences
  5814. if( cstr[n] == 'x' || cstr[n] == 'X' )
  5815. {
  5816. ++n;
  5817. if( n == cstr.GetLength() ) break;
  5818. val = 0;
  5819. int c = engine->ep.stringEncoding == 1 ? 4 : 2;
  5820. for( ; c > 0 && n < cstr.GetLength(); c--, n++ )
  5821. {
  5822. if( cstr[n] >= '0' && cstr[n] <= '9' )
  5823. val = val*16 + cstr[n] - '0';
  5824. else if( cstr[n] >= 'a' && cstr[n] <= 'f' )
  5825. val = val*16 + cstr[n] - 'a' + 10;
  5826. else if( cstr[n] >= 'A' && cstr[n] <= 'F' )
  5827. val = val*16 + cstr[n] - 'A' + 10;
  5828. else
  5829. break;
  5830. }
  5831. // Rewind one, since the loop will increment it again
  5832. n--;
  5833. // Hexadecimal escape sequences produce exact value, even if it is not proper unicode chars
  5834. if( engine->ep.stringEncoding == 0 )
  5835. {
  5836. str.PushLast((asBYTE)val);
  5837. }
  5838. else
  5839. {
  5840. #ifndef AS_BIG_ENDIAN
  5841. str.PushLast((asBYTE)val);
  5842. str.PushLast((asBYTE)(val>>8));
  5843. #else
  5844. str.PushLast((asBYTE)(val>>8));
  5845. str.PushLast((asBYTE)val);
  5846. #endif
  5847. }
  5848. if( charLiteral == -1 ) charLiteral = val;
  5849. continue;
  5850. }
  5851. else if( cstr[n] == 'u' || cstr[n] == 'U' )
  5852. {
  5853. // \u expects 4 hex digits
  5854. // \U expects 8 hex digits
  5855. bool expect2 = cstr[n] == 'u';
  5856. int c = expect2 ? 4 : 8;
  5857. val = 0;
  5858. for( ; c > 0; c-- )
  5859. {
  5860. ++n;
  5861. if( n == cstr.GetLength() ) break;
  5862. if( cstr[n] >= '0' && cstr[n] <= '9' )
  5863. val = val*16 + cstr[n] - '0';
  5864. else if( cstr[n] >= 'a' && cstr[n] <= 'f' )
  5865. val = val*16 + cstr[n] - 'a' + 10;
  5866. else if( cstr[n] >= 'A' && cstr[n] <= 'F' )
  5867. val = val*16 + cstr[n] - 'A' + 10;
  5868. else
  5869. break;
  5870. }
  5871. if( c != 0 )
  5872. {
  5873. // Give warning about invalid code point
  5874. // TODO: Need code position for warning
  5875. asCString msg;
  5876. msg.Format(TXT_INVALID_UNICODE_FORMAT_EXPECTED_d, expect2 ? 4 : 8);
  5877. Warning(msg.AddressOf(), node);
  5878. continue;
  5879. }
  5880. }
  5881. else
  5882. {
  5883. if( cstr[n] == '"' )
  5884. val = '"';
  5885. else if( cstr[n] == '\'' )
  5886. val = '\'';
  5887. else if( cstr[n] == 'n' )
  5888. val = '\n';
  5889. else if( cstr[n] == 'r' )
  5890. val = '\r';
  5891. else if( cstr[n] == 't' )
  5892. val = '\t';
  5893. else if( cstr[n] == '0' )
  5894. val = '\0';
  5895. else if( cstr[n] == '\\' )
  5896. val = '\\';
  5897. else
  5898. {
  5899. // Invalid escape sequence
  5900. Warning(TXT_INVALID_ESCAPE_SEQUENCE, node);
  5901. continue;
  5902. }
  5903. }
  5904. }
  5905. else
  5906. {
  5907. if( engine->ep.scanner == 1 && (cstr[n] & 0x80) )
  5908. {
  5909. unsigned int len;
  5910. val = asStringDecodeUTF8(&cstr[n], &len);
  5911. if( val == 0xFFFFFFFF )
  5912. {
  5913. // Incorrect UTF8 encoding. Use only the first byte
  5914. // TODO: Need code position for warning
  5915. Warning(TXT_INVALID_UNICODE_SEQUENCE_IN_SRC, node);
  5916. val = (unsigned char)cstr[n];
  5917. }
  5918. else
  5919. n += len-1;
  5920. }
  5921. else
  5922. val = (unsigned char)cstr[n];
  5923. }
  5924. // Add the character to the final string
  5925. char encodedValue[5];
  5926. int len;
  5927. if( engine->ep.scanner == 1 && engine->ep.stringEncoding == 0 )
  5928. {
  5929. // Convert to UTF8 encoded
  5930. len = asStringEncodeUTF8(val, encodedValue);
  5931. }
  5932. else if( engine->ep.stringEncoding == 1 )
  5933. {
  5934. // Convert to 16bit wide character string (even if the script is scanned as ASCII)
  5935. len = asStringEncodeUTF16(val, encodedValue);
  5936. }
  5937. else
  5938. {
  5939. // Do not convert ASCII characters
  5940. encodedValue[0] = (asBYTE)val;
  5941. len = 1;
  5942. }
  5943. if( len < 0 )
  5944. {
  5945. // Give warning about invalid code point
  5946. // TODO: Need code position for warning
  5947. Warning(TXT_INVALID_UNICODE_VALUE, node);
  5948. }
  5949. else
  5950. {
  5951. // Add the encoded value to the final string
  5952. str.Concatenate(encodedValue, len);
  5953. if( charLiteral == -1 ) charLiteral = val;
  5954. }
  5955. }
  5956. cstr.Assign(str.AddressOf(), str.GetLength());
  5957. return charLiteral;
  5958. }
  5959. void asCCompiler::ProcessHeredocStringConstant(asCString &str, asCScriptNode *node)
  5960. {
  5961. // Remove first line if it only contains whitespace
  5962. int start;
  5963. for( start = 0; start < (int)str.GetLength(); start++ )
  5964. {
  5965. if( str[start] == '\n' )
  5966. {
  5967. // Remove the linebreak as well
  5968. start++;
  5969. break;
  5970. }
  5971. if( str[start] != ' ' &&
  5972. str[start] != '\t' &&
  5973. str[start] != '\r' )
  5974. {
  5975. // Don't remove anything
  5976. start = 0;
  5977. break;
  5978. }
  5979. }
  5980. // Remove last line break and the line after that if it only contains whitespaces
  5981. int end;
  5982. for( end = (int)str.GetLength() - 1; end >= 0; end-- )
  5983. {
  5984. if( str[end] == '\n' )
  5985. break;
  5986. if( str[end] != ' ' &&
  5987. str[end] != '\t' &&
  5988. str[end] != '\r' )
  5989. {
  5990. // Don't remove anything
  5991. end = (int)str.GetLength();
  5992. break;
  5993. }
  5994. }
  5995. if( end < 0 ) end = 0;
  5996. asCString tmp;
  5997. if( end > start )
  5998. tmp.Assign(&str[start], end-start);
  5999. ProcessStringConstant(tmp, node, false);
  6000. str = tmp;
  6001. }
  6002. void asCCompiler::CompileConversion(asCScriptNode *node, asSExprContext *ctx)
  6003. {
  6004. asSExprContext expr(engine);
  6005. asCDataType to;
  6006. bool anyErrors = false;
  6007. EImplicitConv convType;
  6008. if( node->nodeType == snConstructCall )
  6009. {
  6010. convType = asIC_EXPLICIT_VAL_CAST;
  6011. // Verify that there is only one argument
  6012. if( node->lastChild->firstChild == 0 ||
  6013. node->lastChild->firstChild != node->lastChild->lastChild )
  6014. {
  6015. Error(TXT_ONLY_ONE_ARGUMENT_IN_CAST, node->lastChild);
  6016. expr.type.SetDummy();
  6017. anyErrors = true;
  6018. }
  6019. else
  6020. {
  6021. // Compile the expression
  6022. int r = CompileAssignment(node->lastChild->firstChild, &expr);
  6023. if( r < 0 )
  6024. anyErrors = true;
  6025. }
  6026. // Determine the requested type
  6027. to = builder->CreateDataTypeFromNode(node->firstChild, script);
  6028. to.MakeReadOnly(true); // Default to const
  6029. asASSERT(to.IsPrimitive());
  6030. }
  6031. else
  6032. {
  6033. convType = asIC_EXPLICIT_REF_CAST;
  6034. // Compile the expression
  6035. int r = CompileAssignment(node->lastChild, &expr);
  6036. if( r < 0 )
  6037. anyErrors = true;
  6038. // Determine the requested type
  6039. to = builder->CreateDataTypeFromNode(node->firstChild, script);
  6040. to = builder->ModifyDataTypeFromNode(to, node->firstChild->next, script, 0, 0);
  6041. // If the type support object handles, then use it
  6042. if( to.SupportHandles() )
  6043. {
  6044. to.MakeHandle(true);
  6045. }
  6046. else if( !to.IsObjectHandle() )
  6047. {
  6048. // The cast<type> operator can only be used for reference casts
  6049. Error(TXT_ILLEGAL_TARGET_TYPE_FOR_REF_CAST, node->firstChild);
  6050. anyErrors = true;
  6051. }
  6052. }
  6053. // Do not allow casting to non shared type if we're compiling a shared method
  6054. if( outFunc->objectType && outFunc->objectType->IsShared() &&
  6055. to.GetObjectType() && !to.GetObjectType()->IsShared() )
  6056. {
  6057. asCString msg;
  6058. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, to.GetObjectType()->name.AddressOf());
  6059. Error(msg.AddressOf(), node);
  6060. anyErrors = true;
  6061. }
  6062. if( anyErrors )
  6063. {
  6064. // Assume that the error can be fixed and allow the compilation to continue
  6065. ctx->type.SetConstantDW(to, 0);
  6066. return;
  6067. }
  6068. ProcessPropertyGetAccessor(&expr, node);
  6069. // We don't want a reference
  6070. if( expr.type.dataType.IsReference() )
  6071. {
  6072. if( expr.type.dataType.IsObject() )
  6073. {
  6074. // ASHANDLE is actually a value type, even though it looks like a handle
  6075. // For this reason we shouldn't dereference it, unless it is on the heap
  6076. if( !(expr.type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE) ||
  6077. (expr.type.isVariable && IsVariableOnHeap(expr.type.stackOffset)) )
  6078. Dereference(&expr, true);
  6079. }
  6080. else
  6081. ConvertToVariable(&expr);
  6082. }
  6083. ImplicitConversion(&expr, to, node, convType);
  6084. IsVariableInitialized(&expr.type, node);
  6085. // If no type conversion is really tried ignore it
  6086. if( to == expr.type.dataType )
  6087. {
  6088. // This will keep information about constant type
  6089. MergeExprBytecode(ctx, &expr);
  6090. ctx->type = expr.type;
  6091. return;
  6092. }
  6093. if( to.IsEqualExceptConst(expr.type.dataType) && to.IsPrimitive() )
  6094. {
  6095. MergeExprBytecode(ctx, &expr);
  6096. ctx->type = expr.type;
  6097. ctx->type.dataType.MakeReadOnly(true);
  6098. return;
  6099. }
  6100. // The implicit conversion already does most of the conversions permitted,
  6101. // here we'll only treat those conversions that require an explicit cast.
  6102. bool conversionOK = false;
  6103. if( !expr.type.isConstant )
  6104. {
  6105. if( !expr.type.dataType.IsObject() )
  6106. ConvertToTempVariable(&expr);
  6107. if( to.IsObjectHandle() &&
  6108. expr.type.dataType.IsObjectHandle() &&
  6109. !(!to.IsHandleToConst() && expr.type.dataType.IsHandleToConst()) )
  6110. {
  6111. conversionOK = CompileRefCast(&expr, to, true, node);
  6112. MergeExprBytecode(ctx, &expr);
  6113. ctx->type = expr.type;
  6114. }
  6115. }
  6116. if( conversionOK )
  6117. return;
  6118. // Conversion not available
  6119. ctx->type.SetDummy();
  6120. asCString strTo, strFrom;
  6121. strTo = to.Format();
  6122. strFrom = expr.type.dataType.Format();
  6123. asCString msg;
  6124. msg.Format(TXT_NO_CONVERSION_s_TO_s, strFrom.AddressOf(), strTo.AddressOf());
  6125. Error(msg.AddressOf(), node);
  6126. }
  6127. void asCCompiler::AfterFunctionCall(int funcID, asCArray<asSExprContext*> &args, asSExprContext *ctx, bool deferAll)
  6128. {
  6129. asCScriptFunction *descr = builder->GetFunctionDescription(funcID);
  6130. // Parameters that are sent by reference should be assigned
  6131. // to the evaluated expression if it is an lvalue
  6132. // Evaluate the arguments from last to first
  6133. int n = (int)descr->parameterTypes.GetLength() - 1;
  6134. for( ; n >= 0; n-- )
  6135. {
  6136. if( (descr->parameterTypes[n].IsReference() && (descr->inOutFlags[n] & asTM_OUTREF)) ||
  6137. (descr->parameterTypes[n].IsObject() && deferAll) )
  6138. {
  6139. asASSERT( !(descr->parameterTypes[n].IsReference() && (descr->inOutFlags[n] == asTM_OUTREF)) || args[n]->origExpr );
  6140. // For &inout, only store the argument if it is for a temporary variable
  6141. if( engine->ep.allowUnsafeReferences ||
  6142. descr->inOutFlags[n] != asTM_INOUTREF || args[n]->type.isTemporary )
  6143. {
  6144. // Store the argument for later processing
  6145. asSDeferredParam outParam;
  6146. outParam.argNode = args[n]->exprNode;
  6147. outParam.argType = args[n]->type;
  6148. outParam.argInOutFlags = descr->inOutFlags[n];
  6149. outParam.origExpr = args[n]->origExpr;
  6150. ctx->deferredParams.PushLast(outParam);
  6151. }
  6152. }
  6153. else
  6154. {
  6155. // Release the temporary variable now
  6156. ReleaseTemporaryVariable(args[n]->type, &ctx->bc);
  6157. }
  6158. // Move the argument's deferred expressions over to the final expression
  6159. for( asUINT m = 0; m < args[n]->deferredParams.GetLength(); m++ )
  6160. {
  6161. ctx->deferredParams.PushLast(args[n]->deferredParams[m]);
  6162. args[n]->deferredParams[m].origExpr = 0;
  6163. }
  6164. args[n]->deferredParams.SetLength(0);
  6165. }
  6166. }
  6167. void asCCompiler::ProcessDeferredParams(asSExprContext *ctx)
  6168. {
  6169. if( isProcessingDeferredParams ) return;
  6170. isProcessingDeferredParams = true;
  6171. for( asUINT n = 0; n < ctx->deferredParams.GetLength(); n++ )
  6172. {
  6173. asSDeferredParam outParam = ctx->deferredParams[n];
  6174. if( outParam.argInOutFlags < asTM_OUTREF ) // &in, or not reference
  6175. {
  6176. // Just release the variable
  6177. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  6178. }
  6179. else if( outParam.argInOutFlags == asTM_OUTREF )
  6180. {
  6181. asSExprContext *expr = outParam.origExpr;
  6182. outParam.origExpr = 0;
  6183. if( outParam.argType.dataType.IsObjectHandle() )
  6184. {
  6185. // Implicitly convert the value to a handle
  6186. if( expr->type.dataType.IsObjectHandle() )
  6187. expr->type.isExplicitHandle = true;
  6188. }
  6189. // Verify that the expression result in a lvalue, or a property accessor
  6190. if( IsLValue(expr->type) || expr->property_get || expr->property_set )
  6191. {
  6192. asSExprContext rctx(engine);
  6193. rctx.type = outParam.argType;
  6194. if( rctx.type.dataType.IsPrimitive() )
  6195. rctx.type.dataType.MakeReference(false);
  6196. else
  6197. {
  6198. rctx.bc.InstrSHORT(asBC_PSF, outParam.argType.stackOffset);
  6199. rctx.type.dataType.MakeReference(IsVariableOnHeap(outParam.argType.stackOffset));
  6200. if( expr->type.isExplicitHandle )
  6201. rctx.type.isExplicitHandle = true;
  6202. }
  6203. asSExprContext o(engine);
  6204. DoAssignment(&o, expr, &rctx, outParam.argNode, outParam.argNode, ttAssignment, outParam.argNode);
  6205. if( !o.type.dataType.IsPrimitive() ) o.bc.Pop(AS_PTR_SIZE);
  6206. MergeExprBytecode(ctx, &o);
  6207. }
  6208. else
  6209. {
  6210. // We must still evaluate the expression
  6211. MergeExprBytecode(ctx, expr);
  6212. if( !expr->type.isConstant || expr->type.IsNullConstant() )
  6213. ctx->bc.Pop(expr->type.dataType.GetSizeOnStackDWords());
  6214. // Give a warning, except if the argument is null which indicate the argument is really to be ignored
  6215. if( !expr->type.IsNullConstant() )
  6216. Warning(TXT_ARG_NOT_LVALUE, outParam.argNode);
  6217. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  6218. }
  6219. ReleaseTemporaryVariable(expr->type, &ctx->bc);
  6220. // Delete the original expression context
  6221. asDELETE(expr,asSExprContext);
  6222. }
  6223. else // &inout
  6224. {
  6225. if( outParam.argType.isTemporary )
  6226. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  6227. else if( !outParam.argType.isVariable )
  6228. {
  6229. if( outParam.argType.dataType.IsObject() &&
  6230. outParam.argType.dataType.GetBehaviour()->addref &&
  6231. outParam.argType.dataType.GetBehaviour()->release )
  6232. {
  6233. // Release the object handle that was taken to guarantee the reference
  6234. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  6235. }
  6236. }
  6237. }
  6238. }
  6239. ctx->deferredParams.SetLength(0);
  6240. isProcessingDeferredParams = false;
  6241. }
  6242. void asCCompiler::CompileConstructCall(asCScriptNode *node, asSExprContext *ctx)
  6243. {
  6244. // The first node is a datatype node
  6245. asCString name;
  6246. asCTypeInfo tempObj;
  6247. bool onHeap = true;
  6248. asCArray<int> funcs;
  6249. // It is possible that the name is really a constructor
  6250. asCDataType dt;
  6251. dt = builder->CreateDataTypeFromNode(node->firstChild, script);
  6252. if( dt.IsPrimitive() )
  6253. {
  6254. // This is a cast to a primitive type
  6255. CompileConversion(node, ctx);
  6256. return;
  6257. }
  6258. // Do not allow constructing non-shared types in shared functions
  6259. if( outFunc->objectType && outFunc->objectType->IsShared() &&
  6260. dt.GetObjectType() && !dt.GetObjectType()->IsShared() )
  6261. {
  6262. asCString msg;
  6263. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, dt.GetObjectType()->name.AddressOf());
  6264. Error(msg.AddressOf(), node);
  6265. }
  6266. // Compile the arguments
  6267. asCArray<asSExprContext *> args;
  6268. asCArray<asCTypeInfo> temporaryVariables;
  6269. if( CompileArgumentList(node->lastChild, args) >= 0 )
  6270. {
  6271. // Check for a value cast behaviour
  6272. if( args.GetLength() == 1 && args[0]->type.dataType.GetObjectType() )
  6273. {
  6274. asSExprContext conv(engine);
  6275. conv.type = args[0]->type;
  6276. ImplicitConversion(&conv, dt, node->lastChild, asIC_EXPLICIT_VAL_CAST, false);
  6277. if( conv.type.dataType.IsEqualExceptRef(dt) )
  6278. {
  6279. ImplicitConversion(args[0], dt, node->lastChild, asIC_EXPLICIT_VAL_CAST);
  6280. ctx->bc.AddCode(&args[0]->bc);
  6281. ctx->type = args[0]->type;
  6282. asDELETE(args[0],asSExprContext);
  6283. return;
  6284. }
  6285. }
  6286. // Check for possible constructor/factory
  6287. name = dt.Format();
  6288. asSTypeBehaviour *beh = dt.GetBehaviour();
  6289. if( !(dt.GetObjectType()->flags & asOBJ_REF) )
  6290. {
  6291. funcs = beh->constructors;
  6292. // Value types and script types are allocated through the constructor
  6293. tempObj.dataType = dt;
  6294. tempObj.stackOffset = (short)AllocateVariable(dt, true);
  6295. tempObj.dataType.MakeReference(true);
  6296. tempObj.isTemporary = true;
  6297. tempObj.isVariable = true;
  6298. onHeap = IsVariableOnHeap(tempObj.stackOffset);
  6299. // Push the address of the object on the stack
  6300. if( onHeap )
  6301. ctx->bc.InstrSHORT(asBC_VAR, tempObj.stackOffset);
  6302. }
  6303. else
  6304. {
  6305. funcs = beh->factories;
  6306. }
  6307. // Special case: Allow calling func(void) with a void expression.
  6308. if( args.GetLength() == 1 && args[0]->type.dataType == asCDataType::CreatePrimitive(ttVoid, false) )
  6309. {
  6310. // Evaluate the expression before the function call
  6311. MergeExprBytecode(ctx, args[0]);
  6312. asDELETE(args[0],asSExprContext);
  6313. args.SetLength(0);
  6314. }
  6315. // Special case: If this is an object constructor and there are no arguments use the default constructor.
  6316. // If none has been registered, just allocate the variable and push it on the stack.
  6317. if( args.GetLength() == 0 )
  6318. {
  6319. asSTypeBehaviour *beh = tempObj.dataType.GetBehaviour();
  6320. if( beh && beh->construct == 0 && !(dt.GetObjectType()->flags & asOBJ_REF) )
  6321. {
  6322. // Call the default constructor
  6323. ctx->type = tempObj;
  6324. if( onHeap )
  6325. {
  6326. asASSERT(ctx->bc.GetLastInstr() == asBC_VAR);
  6327. ctx->bc.RemoveLastInstr();
  6328. }
  6329. CallDefaultConstructor(tempObj.dataType, tempObj.stackOffset, IsVariableOnHeap(tempObj.stackOffset), &ctx->bc, node);
  6330. // Push the reference on the stack
  6331. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  6332. return;
  6333. }
  6334. }
  6335. MatchFunctions(funcs, args, node, name.AddressOf(), NULL, false);
  6336. if( funcs.GetLength() != 1 )
  6337. {
  6338. // The error was reported by MatchFunctions()
  6339. // Dummy value
  6340. ctx->type.SetDummy();
  6341. }
  6342. else
  6343. {
  6344. int r = asSUCCESS;
  6345. // Add the default values for arguments not explicitly supplied
  6346. asCScriptFunction *func = (funcs[0] & 0xFFFF0000) == 0 ? engine->scriptFunctions[funcs[0]] : 0;
  6347. if( func && args.GetLength() < (asUINT)func->GetParamCount() )
  6348. r = CompileDefaultArgs(node, args, func);
  6349. if( r == asSUCCESS )
  6350. {
  6351. asCByteCode objBC(engine);
  6352. PrepareFunctionCall(funcs[0], &ctx->bc, args);
  6353. MoveArgsToStack(funcs[0], &ctx->bc, args, false);
  6354. if( !(dt.GetObjectType()->flags & asOBJ_REF) )
  6355. {
  6356. // If the object is allocated on the stack, then call the constructor as a normal function
  6357. if( onHeap )
  6358. {
  6359. int offset = 0;
  6360. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[0]);
  6361. for( asUINT n = 0; n < args.GetLength(); n++ )
  6362. offset += descr->parameterTypes[n].GetSizeOnStackDWords();
  6363. ctx->bc.InstrWORD(asBC_GETREF, (asWORD)offset);
  6364. }
  6365. else
  6366. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  6367. PerformFunctionCall(funcs[0], ctx, onHeap, &args, tempObj.dataType.GetObjectType());
  6368. // Add tag that the object has been initialized
  6369. ctx->bc.ObjInfo(tempObj.stackOffset, asOBJ_INIT);
  6370. // The constructor doesn't return anything,
  6371. // so we have to manually inform the type of
  6372. // the return value
  6373. ctx->type = tempObj;
  6374. if( !onHeap )
  6375. ctx->type.dataType.MakeReference(false);
  6376. // Push the address of the object on the stack again
  6377. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  6378. }
  6379. else
  6380. {
  6381. // Call the factory to create the reference type
  6382. PerformFunctionCall(funcs[0], ctx, false, &args);
  6383. }
  6384. }
  6385. }
  6386. }
  6387. else
  6388. {
  6389. // Failed to compile the argument list, set the result to the dummy type
  6390. ctx->type.SetDummy();
  6391. }
  6392. // Cleanup
  6393. for( asUINT n = 0; n < args.GetLength(); n++ )
  6394. if( args[n] )
  6395. {
  6396. asDELETE(args[n],asSExprContext);
  6397. }
  6398. }
  6399. int asCCompiler::CompileFunctionCall(asCScriptNode *node, asSExprContext *ctx, asCObjectType *objectType, bool objIsConst, const asCString &scope)
  6400. {
  6401. asCString name;
  6402. asCTypeInfo tempObj;
  6403. asCArray<int> funcs;
  6404. int r = -1;
  6405. asCScriptNode *nm = node->lastChild->prev;
  6406. name.Assign(&script->code[nm->tokenPos], nm->tokenLength);
  6407. // First check for a local variable of a function type
  6408. // Must not allow function names, nor global variables to be returned in this instance
  6409. asSExprContext funcPtr(engine);
  6410. if( objectType == 0 )
  6411. r = CompileVariableAccess(name, scope, &funcPtr, node, true, true);
  6412. if( r < 0 )
  6413. {
  6414. if( objectType )
  6415. {
  6416. // If we're compiling a constructor and the name of the function is super then
  6417. // the constructor of the base class is being called.
  6418. // super cannot be prefixed with a scope operator
  6419. if( m_isConstructor && name == SUPER_TOKEN && nm->prev == 0 )
  6420. {
  6421. // If the class is not derived from anyone else, calling super should give an error
  6422. if( objectType->derivedFrom )
  6423. funcs = objectType->derivedFrom->beh.constructors;
  6424. }
  6425. else
  6426. builder->GetObjectMethodDescriptions(name.AddressOf(), objectType, funcs, objIsConst, scope);
  6427. // It is still possible that there is a class member of a function type
  6428. if( funcs.GetLength() == 0 )
  6429. CompileVariableAccess(name, scope, &funcPtr, node, true, true, objectType);
  6430. }
  6431. else
  6432. {
  6433. builder->GetFunctionDescriptions(name.AddressOf(), funcs);
  6434. // TODO: funcdef: It is still possible that there is a global variable of a function type
  6435. }
  6436. }
  6437. else if( !funcPtr.type.dataType.GetFuncDef() )
  6438. {
  6439. // The variable is not a function
  6440. asCString msg;
  6441. msg.Format(TXT_NOT_A_FUNC_s_IS_VAR, name.AddressOf());
  6442. Error(msg.AddressOf(), node);
  6443. return -1;
  6444. }
  6445. if( funcs.GetLength() == 0 && funcPtr.type.dataType.GetFuncDef() )
  6446. {
  6447. funcs.PushLast(funcPtr.type.dataType.GetFuncDef()->id);
  6448. }
  6449. // Compile the arguments
  6450. asCArray<asSExprContext *> args;
  6451. asCArray<asCTypeInfo> temporaryVariables;
  6452. if( CompileArgumentList(node->lastChild, args) >= 0 )
  6453. {
  6454. // Special case: Allow calling func(void) with a void expression.
  6455. if( args.GetLength() == 1 && args[0]->type.dataType == asCDataType::CreatePrimitive(ttVoid, false) )
  6456. {
  6457. // Evaluate the expression before the function call
  6458. MergeExprBytecode(ctx, args[0]);
  6459. asDELETE(args[0],asSExprContext);
  6460. args.SetLength(0);
  6461. }
  6462. MatchFunctions(funcs, args, node, name.AddressOf(), objectType, objIsConst, false, true, scope);
  6463. if( funcs.GetLength() != 1 )
  6464. {
  6465. // The error was reported by MatchFunctions()
  6466. // Dummy value
  6467. ctx->type.SetDummy();
  6468. }
  6469. else
  6470. {
  6471. int r = asSUCCESS;
  6472. // Add the default values for arguments not explicitly supplied
  6473. asCScriptFunction *func = (funcs[0] & 0xFFFF0000) == 0 ? engine->scriptFunctions[funcs[0]] : 0;
  6474. if( func && args.GetLength() < (asUINT)func->GetParamCount() )
  6475. r = CompileDefaultArgs(node, args, func);
  6476. // TODO: funcdef: Do we have to make sure the handle is stored in a temporary variable, or
  6477. // is it enough to make sure it is in a local variable?
  6478. // For function pointer we must guarantee that the function is safe, i.e.
  6479. // by first storing the function pointer in a local variable (if it isn't already in one)
  6480. if( r == asSUCCESS )
  6481. {
  6482. if( (funcs[0] & 0xFFFF0000) == 0 && engine->scriptFunctions[funcs[0]]->funcType == asFUNC_FUNCDEF )
  6483. {
  6484. if( objectType )
  6485. {
  6486. Dereference(ctx, true); // Dereference the object pointer to access the member
  6487. // The actual function should be called as if a global function
  6488. objectType = 0;
  6489. }
  6490. Dereference(&funcPtr, true);
  6491. ConvertToVariable(&funcPtr);
  6492. ctx->bc.AddCode(&funcPtr.bc);
  6493. if( !funcPtr.type.isTemporary )
  6494. ctx->bc.Pop(AS_PTR_SIZE);
  6495. }
  6496. MakeFunctionCall(ctx, funcs[0], objectType, args, node, false, 0, funcPtr.type.stackOffset);
  6497. // If the function pointer was copied to a local variable for the call, then
  6498. // release it again (temporary local variable)
  6499. if( (funcs[0] & 0xFFFF0000) == 0 && engine->scriptFunctions[funcs[0]]->funcType == asFUNC_FUNCDEF )
  6500. {
  6501. ReleaseTemporaryVariable(funcPtr.type, &ctx->bc);
  6502. }
  6503. }
  6504. }
  6505. }
  6506. else
  6507. {
  6508. // Failed to compile the argument list, set the dummy type and continue compilation
  6509. ctx->type.SetDummy();
  6510. }
  6511. // Cleanup
  6512. for( asUINT n = 0; n < args.GetLength(); n++ )
  6513. if( args[n] )
  6514. {
  6515. asDELETE(args[n],asSExprContext);
  6516. }
  6517. return 0;
  6518. }
  6519. int asCCompiler::CompileExpressionPreOp(asCScriptNode *node, asSExprContext *ctx)
  6520. {
  6521. int op = node->tokenType;
  6522. IsVariableInitialized(&ctx->type, node);
  6523. if( op == ttHandle )
  6524. {
  6525. // Verify that the type allow its handle to be taken
  6526. if( ctx->type.isExplicitHandle ||
  6527. !ctx->type.dataType.IsObject() ||
  6528. !((ctx->type.dataType.GetObjectType()->beh.addref && ctx->type.dataType.GetObjectType()->beh.release) || ctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE) )
  6529. {
  6530. Error(TXT_OBJECT_HANDLE_NOT_SUPPORTED, node);
  6531. return -1;
  6532. }
  6533. // Objects that are not local variables are not references
  6534. if( !ctx->type.dataType.IsReference() && !(ctx->type.dataType.IsObject() && !ctx->type.isVariable) )
  6535. {
  6536. Error(TXT_NOT_VALID_REFERENCE, node);
  6537. return -1;
  6538. }
  6539. // If this is really an object then the handle created is a const handle
  6540. bool makeConst = !ctx->type.dataType.IsObjectHandle();
  6541. // Mark the type as an object handle
  6542. ctx->type.dataType.MakeHandle(true);
  6543. ctx->type.isExplicitHandle = true;
  6544. if( makeConst )
  6545. ctx->type.dataType.MakeReadOnly(true);
  6546. }
  6547. else if( (op == ttMinus || op == ttBitNot || op == ttInc || op == ttDec) && ctx->type.dataType.IsObject() )
  6548. {
  6549. // Look for the appropriate method
  6550. const char *opName = 0;
  6551. switch( op )
  6552. {
  6553. case ttMinus: opName = "opNeg"; break;
  6554. case ttBitNot: opName = "opCom"; break;
  6555. case ttInc: opName = "opPreInc"; break;
  6556. case ttDec: opName = "opPreDec"; break;
  6557. }
  6558. if( opName )
  6559. {
  6560. // TODO: Should convert this to something similar to CompileOverloadedDualOperator2
  6561. ProcessPropertyGetAccessor(ctx, node);
  6562. // Is it a const value?
  6563. bool isConst = false;
  6564. if( ctx->type.dataType.IsObjectHandle() )
  6565. isConst = ctx->type.dataType.IsHandleToConst();
  6566. else
  6567. isConst = ctx->type.dataType.IsReadOnly();
  6568. // TODO: If the value isn't const, then first try to find the non const method, and if not found try to find the const method
  6569. // Find the correct method
  6570. asCArray<int> funcs;
  6571. asCObjectType *ot = ctx->type.dataType.GetObjectType();
  6572. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  6573. {
  6574. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  6575. if( func->name == opName &&
  6576. func->parameterTypes.GetLength() == 0 &&
  6577. (!isConst || func->isReadOnly) )
  6578. {
  6579. funcs.PushLast(func->id);
  6580. }
  6581. }
  6582. // Did we find the method?
  6583. if( funcs.GetLength() == 1 )
  6584. {
  6585. asCTypeInfo objType = ctx->type;
  6586. asCArray<asSExprContext *> args;
  6587. MakeFunctionCall(ctx, funcs[0], objType.dataType.GetObjectType(), args, node);
  6588. ReleaseTemporaryVariable(objType, &ctx->bc);
  6589. return 0;
  6590. }
  6591. else if( funcs.GetLength() == 0 )
  6592. {
  6593. asCString str;
  6594. str = asCString(opName) + "()";
  6595. if( isConst )
  6596. str += " const";
  6597. str.Format(TXT_FUNCTION_s_NOT_FOUND, str.AddressOf());
  6598. Error(str.AddressOf(), node);
  6599. ctx->type.SetDummy();
  6600. return -1;
  6601. }
  6602. else if( funcs.GetLength() > 1 )
  6603. {
  6604. Error(TXT_MORE_THAN_ONE_MATCHING_OP, node);
  6605. PrintMatchingFuncs(funcs, node);
  6606. ctx->type.SetDummy();
  6607. return -1;
  6608. }
  6609. }
  6610. }
  6611. else if( op == ttPlus || op == ttMinus )
  6612. {
  6613. ProcessPropertyGetAccessor(ctx, node);
  6614. asCDataType to = ctx->type.dataType;
  6615. // TODO: The case -2147483648 gives an unecessary warning of changed sign for implicit conversion
  6616. if( ctx->type.dataType.IsUnsignedType() || ctx->type.dataType.IsEnumType() )
  6617. {
  6618. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  6619. to = asCDataType::CreatePrimitive(ttInt8, false);
  6620. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  6621. to = asCDataType::CreatePrimitive(ttInt16, false);
  6622. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 4 )
  6623. to = asCDataType::CreatePrimitive(ttInt, false);
  6624. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 8 )
  6625. to = asCDataType::CreatePrimitive(ttInt64, false);
  6626. else
  6627. {
  6628. Error(TXT_INVALID_TYPE, node);
  6629. return -1;
  6630. }
  6631. }
  6632. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  6633. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV);
  6634. if( !ctx->type.isConstant )
  6635. {
  6636. ConvertToTempVariable(ctx);
  6637. asASSERT(!ctx->type.isLValue);
  6638. if( op == ttMinus )
  6639. {
  6640. if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  6641. ctx->bc.InstrSHORT(asBC_NEGi, ctx->type.stackOffset);
  6642. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  6643. ctx->bc.InstrSHORT(asBC_NEGi64, ctx->type.stackOffset);
  6644. else if( ctx->type.dataType.IsFloatType() )
  6645. ctx->bc.InstrSHORT(asBC_NEGf, ctx->type.stackOffset);
  6646. else if( ctx->type.dataType.IsDoubleType() )
  6647. ctx->bc.InstrSHORT(asBC_NEGd, ctx->type.stackOffset);
  6648. else
  6649. {
  6650. Error(TXT_ILLEGAL_OPERATION, node);
  6651. return -1;
  6652. }
  6653. return 0;
  6654. }
  6655. }
  6656. else
  6657. {
  6658. if( op == ttMinus )
  6659. {
  6660. if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  6661. ctx->type.intValue = -ctx->type.intValue;
  6662. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  6663. ctx->type.qwordValue = -(asINT64)ctx->type.qwordValue;
  6664. else if( ctx->type.dataType.IsFloatType() )
  6665. ctx->type.floatValue = -ctx->type.floatValue;
  6666. else if( ctx->type.dataType.IsDoubleType() )
  6667. ctx->type.doubleValue = -ctx->type.doubleValue;
  6668. else
  6669. {
  6670. Error(TXT_ILLEGAL_OPERATION, node);
  6671. return -1;
  6672. }
  6673. return 0;
  6674. }
  6675. }
  6676. if( op == ttPlus )
  6677. {
  6678. if( !ctx->type.dataType.IsIntegerType() &&
  6679. !ctx->type.dataType.IsFloatType() &&
  6680. !ctx->type.dataType.IsDoubleType() )
  6681. {
  6682. Error(TXT_ILLEGAL_OPERATION, node);
  6683. return -1;
  6684. }
  6685. }
  6686. }
  6687. else if( op == ttNot )
  6688. {
  6689. if( ctx->type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  6690. {
  6691. if( ctx->type.isConstant )
  6692. {
  6693. ctx->type.dwordValue = (ctx->type.dwordValue == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  6694. return 0;
  6695. }
  6696. ProcessPropertyGetAccessor(ctx, node);
  6697. ConvertToTempVariable(ctx);
  6698. asASSERT(!ctx->type.isLValue);
  6699. ctx->bc.InstrSHORT(asBC_NOT, ctx->type.stackOffset);
  6700. }
  6701. else
  6702. {
  6703. Error(TXT_ILLEGAL_OPERATION, node);
  6704. return -1;
  6705. }
  6706. }
  6707. else if( op == ttBitNot )
  6708. {
  6709. ProcessPropertyGetAccessor(ctx, node);
  6710. asCDataType to = ctx->type.dataType;
  6711. if( ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsEnumType() )
  6712. {
  6713. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  6714. to = asCDataType::CreatePrimitive(ttUInt8, false);
  6715. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  6716. to = asCDataType::CreatePrimitive(ttUInt16, false);
  6717. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 4 )
  6718. to = asCDataType::CreatePrimitive(ttUInt, false);
  6719. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 8 )
  6720. to = asCDataType::CreatePrimitive(ttUInt64, false);
  6721. else
  6722. {
  6723. Error(TXT_INVALID_TYPE, node);
  6724. return -1;
  6725. }
  6726. }
  6727. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  6728. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV);
  6729. if( ctx->type.dataType.IsUnsignedType() )
  6730. {
  6731. if( ctx->type.isConstant )
  6732. {
  6733. ctx->type.qwordValue = ~ctx->type.qwordValue;
  6734. return 0;
  6735. }
  6736. ConvertToTempVariable(ctx);
  6737. asASSERT(!ctx->type.isLValue);
  6738. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  6739. ctx->bc.InstrSHORT(asBC_BNOT, ctx->type.stackOffset);
  6740. else
  6741. ctx->bc.InstrSHORT(asBC_BNOT64, ctx->type.stackOffset);
  6742. }
  6743. else
  6744. {
  6745. Error(TXT_ILLEGAL_OPERATION, node);
  6746. return -1;
  6747. }
  6748. }
  6749. else if( op == ttInc || op == ttDec )
  6750. {
  6751. // Need a reference to the primitive that will be updated
  6752. // The result of this expression is the same reference as before
  6753. // Make sure the reference isn't a temporary variable
  6754. if( ctx->type.isTemporary )
  6755. {
  6756. Error(TXT_REF_IS_TEMP, node);
  6757. return -1;
  6758. }
  6759. if( ctx->type.dataType.IsReadOnly() )
  6760. {
  6761. Error(TXT_REF_IS_READ_ONLY, node);
  6762. return -1;
  6763. }
  6764. if( ctx->property_get || ctx->property_set )
  6765. {
  6766. Error(TXT_INVALID_REF_PROP_ACCESS, node);
  6767. return -1;
  6768. }
  6769. if( !ctx->type.isLValue )
  6770. {
  6771. Error(TXT_NOT_LVALUE, node);
  6772. return -1;
  6773. }
  6774. if( ctx->type.isVariable && !ctx->type.dataType.IsReference() )
  6775. ConvertToReference(ctx);
  6776. else if( !ctx->type.dataType.IsReference() )
  6777. {
  6778. Error(TXT_NOT_VALID_REFERENCE, node);
  6779. return -1;
  6780. }
  6781. if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt64, false)) ||
  6782. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt64, false)) )
  6783. {
  6784. if( op == ttInc )
  6785. ctx->bc.Instr(asBC_INCi64);
  6786. else
  6787. ctx->bc.Instr(asBC_DECi64);
  6788. }
  6789. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt, false)) ||
  6790. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt, false)) )
  6791. {
  6792. if( op == ttInc )
  6793. ctx->bc.Instr(asBC_INCi);
  6794. else
  6795. ctx->bc.Instr(asBC_DECi);
  6796. }
  6797. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt16, false)) ||
  6798. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt16, false)) )
  6799. {
  6800. if( op == ttInc )
  6801. ctx->bc.Instr(asBC_INCi16);
  6802. else
  6803. ctx->bc.Instr(asBC_DECi16);
  6804. }
  6805. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt8, false)) ||
  6806. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt8, false)) )
  6807. {
  6808. if( op == ttInc )
  6809. ctx->bc.Instr(asBC_INCi8);
  6810. else
  6811. ctx->bc.Instr(asBC_DECi8);
  6812. }
  6813. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttFloat, false)) )
  6814. {
  6815. if( op == ttInc )
  6816. ctx->bc.Instr(asBC_INCf);
  6817. else
  6818. ctx->bc.Instr(asBC_DECf);
  6819. }
  6820. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttDouble, false)) )
  6821. {
  6822. if( op == ttInc )
  6823. ctx->bc.Instr(asBC_INCd);
  6824. else
  6825. ctx->bc.Instr(asBC_DECd);
  6826. }
  6827. else
  6828. {
  6829. Error(TXT_ILLEGAL_OPERATION, node);
  6830. return -1;
  6831. }
  6832. }
  6833. else
  6834. {
  6835. // Unknown operator
  6836. asASSERT(false);
  6837. return -1;
  6838. }
  6839. return 0;
  6840. }
  6841. void asCCompiler::ConvertToReference(asSExprContext *ctx)
  6842. {
  6843. if( ctx->type.isVariable && !ctx->type.dataType.IsReference() )
  6844. {
  6845. ctx->bc.InstrSHORT(asBC_LDV, ctx->type.stackOffset);
  6846. ctx->type.dataType.MakeReference(true);
  6847. ctx->type.SetVariable(ctx->type.dataType, ctx->type.stackOffset, ctx->type.isTemporary);
  6848. }
  6849. }
  6850. int asCCompiler::FindPropertyAccessor(const asCString &name, asSExprContext *ctx, asCScriptNode *node, bool isThisAccess)
  6851. {
  6852. return FindPropertyAccessor(name, ctx, 0, node, isThisAccess);
  6853. }
  6854. int asCCompiler::FindPropertyAccessor(const asCString &name, asSExprContext *ctx, asSExprContext *arg, asCScriptNode *node, bool isThisAccess)
  6855. {
  6856. if( engine->ep.propertyAccessorMode == 0 )
  6857. {
  6858. // Property accessors have been disabled by the application
  6859. return 0;
  6860. }
  6861. int getId = 0, setId = 0;
  6862. asCString getName = "get_" + name;
  6863. asCString setName = "set_" + name;
  6864. asCArray<int> multipleGetFuncs, multipleSetFuncs;
  6865. if( ctx->type.dataType.IsObject() )
  6866. {
  6867. // Check if the object has any methods with the corresponding accessor name(s)
  6868. asCObjectType *ot = ctx->type.dataType.GetObjectType();
  6869. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  6870. {
  6871. asCScriptFunction *f = engine->scriptFunctions[ot->methods[n]];
  6872. // TODO: The type of the parameter should match the argument (unless the arg is a dummy)
  6873. if( f->name == getName && (int)f->parameterTypes.GetLength() == (arg?1:0) )
  6874. {
  6875. if( getId == 0 )
  6876. getId = ot->methods[n];
  6877. else
  6878. {
  6879. if( multipleGetFuncs.GetLength() == 0 )
  6880. multipleGetFuncs.PushLast(getId);
  6881. multipleGetFuncs.PushLast(ot->methods[n]);
  6882. }
  6883. }
  6884. // TODO: getset: If the parameter is a reference, it must not be an out reference. Should we allow inout ref?
  6885. if( f->name == setName && (int)f->parameterTypes.GetLength() == (arg?2:1) )
  6886. {
  6887. if( setId == 0 )
  6888. setId = ot->methods[n];
  6889. else
  6890. {
  6891. if( multipleSetFuncs.GetLength() == 0 )
  6892. multipleSetFuncs.PushLast(setId);
  6893. multipleSetFuncs.PushLast(ot->methods[n]);
  6894. }
  6895. }
  6896. }
  6897. }
  6898. else
  6899. {
  6900. // Look for appropriate global functions.
  6901. asCArray<int> funcs;
  6902. asUINT n;
  6903. builder->GetFunctionDescriptions(getName.AddressOf(), funcs);
  6904. for( n = 0; n < funcs.GetLength(); n++ )
  6905. {
  6906. asCScriptFunction *f = builder->GetFunctionDescription(funcs[n]);
  6907. // TODO: The type of the parameter should match the argument (unless the arg is a dummy)
  6908. if( (int)f->parameterTypes.GetLength() == (arg?1:0) )
  6909. {
  6910. if( getId == 0 )
  6911. getId = funcs[n];
  6912. else
  6913. {
  6914. if( multipleGetFuncs.GetLength() == 0 )
  6915. multipleGetFuncs.PushLast(getId);
  6916. multipleGetFuncs.PushLast(funcs[n]);
  6917. }
  6918. }
  6919. }
  6920. funcs.SetLength(0);
  6921. builder->GetFunctionDescriptions(setName.AddressOf(), funcs);
  6922. for( n = 0; n < funcs.GetLength(); n++ )
  6923. {
  6924. asCScriptFunction *f = builder->GetFunctionDescription(funcs[n]);
  6925. // TODO: getset: If the parameter is a reference, it must not be an out reference. Should we allow inout ref?
  6926. if( (int)f->parameterTypes.GetLength() == (arg?2:1) )
  6927. {
  6928. if( setId == 0 )
  6929. setId = funcs[n];
  6930. else
  6931. {
  6932. if( multipleSetFuncs.GetLength() == 0 )
  6933. multipleSetFuncs.PushLast(getId);
  6934. multipleSetFuncs.PushLast(funcs[n]);
  6935. }
  6936. }
  6937. }
  6938. }
  6939. // Check for multiple matches
  6940. if( multipleGetFuncs.GetLength() > 0 )
  6941. {
  6942. asCString str;
  6943. str.Format(TXT_MULTIPLE_PROP_GET_ACCESSOR_FOR_s, name.AddressOf());
  6944. Error(str.AddressOf(), node);
  6945. PrintMatchingFuncs(multipleGetFuncs, node);
  6946. return -1;
  6947. }
  6948. if( multipleSetFuncs.GetLength() > 0 )
  6949. {
  6950. asCString str;
  6951. str.Format(TXT_MULTIPLE_PROP_SET_ACCESSOR_FOR_s, name.AddressOf());
  6952. Error(str.AddressOf(), node);
  6953. PrintMatchingFuncs(multipleSetFuncs, node);
  6954. return -1;
  6955. }
  6956. // Check for type compatibility between get and set accessor
  6957. if( getId && setId )
  6958. {
  6959. asCScriptFunction *getFunc = builder->GetFunctionDescription(getId);
  6960. asCScriptFunction *setFunc = builder->GetFunctionDescription(setId);
  6961. // It is permitted for a getter to return a handle and the setter to take a reference
  6962. int idx = (arg?1:0);
  6963. if( !getFunc->returnType.IsEqualExceptRefAndConst(setFunc->parameterTypes[idx]) &&
  6964. !((getFunc->returnType.IsObjectHandle() && !setFunc->parameterTypes[idx].IsObjectHandle()) &&
  6965. (getFunc->returnType.GetObjectType() == setFunc->parameterTypes[idx].GetObjectType())) )
  6966. {
  6967. asCString str;
  6968. str.Format(TXT_GET_SET_ACCESSOR_TYPE_MISMATCH_FOR_s, name.AddressOf());
  6969. Error(str.AddressOf(), node);
  6970. asCArray<int> funcs;
  6971. funcs.PushLast(getId);
  6972. funcs.PushLast(setId);
  6973. PrintMatchingFuncs(funcs, node);
  6974. return -1;
  6975. }
  6976. }
  6977. // Check if we are within one of the accessors
  6978. int realGetId = getId;
  6979. int realSetId = setId;
  6980. if( outFunc->objectType && isThisAccess )
  6981. {
  6982. // The property accessors would be virtual functions, so we need to find the real implementation
  6983. asCScriptFunction *getFunc = getId ? builder->GetFunctionDescription(getId) : 0;
  6984. if( getFunc &&
  6985. getFunc->funcType == asFUNC_VIRTUAL &&
  6986. outFunc->objectType->DerivesFrom(getFunc->objectType) )
  6987. realGetId = outFunc->objectType->virtualFunctionTable[getFunc->vfTableIdx]->id;
  6988. asCScriptFunction *setFunc = setId ? builder->GetFunctionDescription(setId) : 0;
  6989. if( setFunc &&
  6990. setFunc->funcType == asFUNC_VIRTUAL &&
  6991. outFunc->objectType->DerivesFrom(setFunc->objectType) )
  6992. realSetId = outFunc->objectType->virtualFunctionTable[setFunc->vfTableIdx]->id;
  6993. }
  6994. // Avoid recursive call, by not treating this as a property accessor call.
  6995. // This will also allow having the real property with the same name as the accessors.
  6996. if( (isThisAccess || outFunc->objectType == 0) &&
  6997. ((realGetId && realGetId == outFunc->id) ||
  6998. (realSetId && realSetId == outFunc->id)) )
  6999. {
  7000. getId = 0;
  7001. setId = 0;
  7002. }
  7003. // Check if the application has disabled script written property accessors
  7004. if( engine->ep.propertyAccessorMode == 1 )
  7005. {
  7006. if( getId && builder->GetFunctionDescription(getId)->funcType != asFUNC_SYSTEM )
  7007. getId = 0;
  7008. if( setId && builder->GetFunctionDescription(setId)->funcType != asFUNC_SYSTEM )
  7009. setId = 0;
  7010. }
  7011. if( getId || setId )
  7012. {
  7013. // Property accessors were found, but we don't know which is to be used yet, so
  7014. // we just prepare the bytecode for the method call, and then store the function ids
  7015. // so that the right one can be used when we get there.
  7016. ctx->property_get = getId;
  7017. ctx->property_set = setId;
  7018. if( ctx->type.dataType.IsObject() )
  7019. {
  7020. // If the object is read-only then we need to remember that
  7021. if( (!ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsReadOnly()) ||
  7022. (ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsHandleToConst()) )
  7023. ctx->property_const = true;
  7024. else
  7025. ctx->property_const = false;
  7026. // If the object is a handle then we need to remember that
  7027. ctx->property_handle = ctx->type.dataType.IsObjectHandle();
  7028. ctx->property_ref = ctx->type.dataType.IsReference();
  7029. }
  7030. // The setter's parameter type is used as the property type,
  7031. // unless only the getter is available
  7032. asCDataType dt;
  7033. if( setId )
  7034. dt = builder->GetFunctionDescription(setId)->parameterTypes[(arg?1:0)];
  7035. else
  7036. dt = builder->GetFunctionDescription(getId)->returnType;
  7037. // Just change the type, the context must still maintain information
  7038. // about previous variable offset and the indicator of temporary variable.
  7039. int offset = ctx->type.stackOffset;
  7040. bool isTemp = ctx->type.isTemporary;
  7041. ctx->type.Set(dt);
  7042. ctx->type.stackOffset = (short)offset;
  7043. ctx->type.isTemporary = isTemp;
  7044. ctx->exprNode = node;
  7045. // Store the argument for later use
  7046. if( arg )
  7047. {
  7048. ctx->property_arg = asNEW(asSExprContext)(engine);
  7049. MergeExprBytecodeAndType(ctx->property_arg, arg);
  7050. }
  7051. return 1;
  7052. }
  7053. // No accessor was found
  7054. return 0;
  7055. }
  7056. int asCCompiler::ProcessPropertySetAccessor(asSExprContext *ctx, asSExprContext *arg, asCScriptNode *node)
  7057. {
  7058. // TODO: A lot of this code is similar to ProcessPropertyGetAccessor. Can we unify them?
  7059. if( !ctx->property_set )
  7060. {
  7061. Error(TXT_PROPERTY_HAS_NO_SET_ACCESSOR, node);
  7062. return -1;
  7063. }
  7064. asCTypeInfo objType = ctx->type;
  7065. asCScriptFunction *func = builder->GetFunctionDescription(ctx->property_set);
  7066. // Make sure the arg match the property
  7067. asCArray<int> funcs;
  7068. funcs.PushLast(ctx->property_set);
  7069. asCArray<asSExprContext *> args;
  7070. if( ctx->property_arg )
  7071. args.PushLast(ctx->property_arg);
  7072. args.PushLast(arg);
  7073. MatchFunctions(funcs, args, node, func->GetName(), func->objectType, ctx->property_const);
  7074. if( funcs.GetLength() == 0 )
  7075. {
  7076. // MatchFunctions already reported the error
  7077. if( ctx->property_arg )
  7078. {
  7079. asDELETE(ctx->property_arg, asSExprContext);
  7080. ctx->property_arg = 0;
  7081. }
  7082. return -1;
  7083. }
  7084. if( func->objectType )
  7085. {
  7086. // Setup the context with the original type so the method call gets built correctly
  7087. ctx->type.dataType = asCDataType::CreateObject(func->objectType, ctx->property_const);
  7088. if( ctx->property_handle ) ctx->type.dataType.MakeHandle(true);
  7089. if( ctx->property_ref ) ctx->type.dataType.MakeReference(true);
  7090. // Don't allow the call if the object is read-only and the property accessor is not const
  7091. if( ctx->property_const && !func->isReadOnly )
  7092. {
  7093. Error(TXT_NON_CONST_METHOD_ON_CONST_OBJ, node);
  7094. asCArray<int> funcs;
  7095. funcs.PushLast(ctx->property_set);
  7096. PrintMatchingFuncs(funcs, node);
  7097. }
  7098. }
  7099. // Call the accessor
  7100. MakeFunctionCall(ctx, ctx->property_set, func->objectType, args, node);
  7101. if( func->objectType )
  7102. {
  7103. // TODO: This is from CompileExpressionPostOp, can we unify the code?
  7104. if( !objType.isTemporary ||
  7105. !ctx->type.dataType.IsReference() ||
  7106. ctx->type.isVariable ) // If the resulting type is a variable, then the reference is not a member
  7107. {
  7108. // As the method didn't return a reference to a member
  7109. // we can safely release the original object now
  7110. ReleaseTemporaryVariable(objType, &ctx->bc);
  7111. }
  7112. }
  7113. ctx->property_get = 0;
  7114. ctx->property_set = 0;
  7115. if( ctx->property_arg )
  7116. {
  7117. asDELETE(ctx->property_arg, asSExprContext);
  7118. ctx->property_arg = 0;
  7119. }
  7120. return 0;
  7121. }
  7122. void asCCompiler::ProcessPropertyGetAccessor(asSExprContext *ctx, asCScriptNode *node)
  7123. {
  7124. // If no property accessor has been prepared then don't do anything
  7125. if( !ctx->property_get && !ctx->property_set )
  7126. return;
  7127. if( !ctx->property_get )
  7128. {
  7129. // Raise error on missing accessor
  7130. Error(TXT_PROPERTY_HAS_NO_GET_ACCESSOR, node);
  7131. ctx->type.SetDummy();
  7132. return;
  7133. }
  7134. asCTypeInfo objType = ctx->type;
  7135. asCScriptFunction *func = builder->GetFunctionDescription(ctx->property_get);
  7136. // Make sure the arg match the property
  7137. asCArray<int> funcs;
  7138. funcs.PushLast(ctx->property_get);
  7139. asCArray<asSExprContext *> args;
  7140. if( ctx->property_arg )
  7141. args.PushLast(ctx->property_arg);
  7142. MatchFunctions(funcs, args, node, func->GetName(), func->objectType, ctx->property_const);
  7143. if( funcs.GetLength() == 0 )
  7144. {
  7145. // MatchFunctions already reported the error
  7146. if( ctx->property_arg )
  7147. {
  7148. asDELETE(ctx->property_arg, asSExprContext);
  7149. ctx->property_arg = 0;
  7150. }
  7151. ctx->type.SetDummy();
  7152. return;
  7153. }
  7154. if( func->objectType )
  7155. {
  7156. // Setup the context with the original type so the method call gets built correctly
  7157. ctx->type.dataType = asCDataType::CreateObject(func->objectType, ctx->property_const);
  7158. if( ctx->property_handle ) ctx->type.dataType.MakeHandle(true);
  7159. if( ctx->property_ref ) ctx->type.dataType.MakeReference(true);
  7160. // Don't allow the call if the object is read-only and the property accessor is not const
  7161. if( ctx->property_const && !func->isReadOnly )
  7162. {
  7163. Error(TXT_NON_CONST_METHOD_ON_CONST_OBJ, node);
  7164. asCArray<int> funcs;
  7165. funcs.PushLast(ctx->property_get);
  7166. PrintMatchingFuncs(funcs, node);
  7167. }
  7168. }
  7169. // Call the accessor
  7170. MakeFunctionCall(ctx, ctx->property_get, func->objectType, args, node);
  7171. if( func->objectType )
  7172. {
  7173. // TODO: This is from CompileExpressionPostOp, can we unify the code?
  7174. if( !objType.isTemporary ||
  7175. !ctx->type.dataType.IsReference() ||
  7176. ctx->type.isVariable ) // If the resulting type is a variable, then the reference is not a member
  7177. {
  7178. // As the method didn't return a reference to a member
  7179. // we can safely release the original object now
  7180. ReleaseTemporaryVariable(objType, &ctx->bc);
  7181. }
  7182. }
  7183. ctx->property_get = 0;
  7184. ctx->property_set = 0;
  7185. if( ctx->property_arg )
  7186. {
  7187. asDELETE(ctx->property_arg, asSExprContext);
  7188. ctx->property_arg = 0;
  7189. }
  7190. }
  7191. int asCCompiler::CompileExpressionPostOp(asCScriptNode *node, asSExprContext *ctx)
  7192. {
  7193. int op = node->tokenType;
  7194. // Check if the variable is initialized (if it indeed is a variable)
  7195. IsVariableInitialized(&ctx->type, node);
  7196. if( (op == ttInc || op == ttDec) && ctx->type.dataType.IsObject() )
  7197. {
  7198. const char *opName = 0;
  7199. switch( op )
  7200. {
  7201. case ttInc: opName = "opPostInc"; break;
  7202. case ttDec: opName = "opPostDec"; break;
  7203. }
  7204. if( opName )
  7205. {
  7206. // TODO: Should convert this to something similar to CompileOverloadedDualOperator2
  7207. ProcessPropertyGetAccessor(ctx, node);
  7208. // Is it a const value?
  7209. bool isConst = false;
  7210. if( ctx->type.dataType.IsObjectHandle() )
  7211. isConst = ctx->type.dataType.IsHandleToConst();
  7212. else
  7213. isConst = ctx->type.dataType.IsReadOnly();
  7214. // TODO: If the value isn't const, then first try to find the non const method, and if not found try to find the const method
  7215. // Find the correct method
  7216. asCArray<int> funcs;
  7217. asCObjectType *ot = ctx->type.dataType.GetObjectType();
  7218. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  7219. {
  7220. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  7221. if( func->name == opName &&
  7222. func->parameterTypes.GetLength() == 0 &&
  7223. (!isConst || func->isReadOnly) )
  7224. {
  7225. funcs.PushLast(func->id);
  7226. }
  7227. }
  7228. // Did we find the method?
  7229. if( funcs.GetLength() == 1 )
  7230. {
  7231. asCTypeInfo objType = ctx->type;
  7232. asCArray<asSExprContext *> args;
  7233. MakeFunctionCall(ctx, funcs[0], objType.dataType.GetObjectType(), args, node);
  7234. ReleaseTemporaryVariable(objType, &ctx->bc);
  7235. return 0;
  7236. }
  7237. else if( funcs.GetLength() == 0 )
  7238. {
  7239. asCString str;
  7240. str = asCString(opName) + "()";
  7241. if( isConst )
  7242. str += " const";
  7243. str.Format(TXT_FUNCTION_s_NOT_FOUND, str.AddressOf());
  7244. Error(str.AddressOf(), node);
  7245. ctx->type.SetDummy();
  7246. return -1;
  7247. }
  7248. else if( funcs.GetLength() > 1 )
  7249. {
  7250. Error(TXT_MORE_THAN_ONE_MATCHING_OP, node);
  7251. PrintMatchingFuncs(funcs, node);
  7252. ctx->type.SetDummy();
  7253. return -1;
  7254. }
  7255. }
  7256. }
  7257. else if( op == ttInc || op == ttDec )
  7258. {
  7259. // Make sure the reference isn't a temporary variable
  7260. if( ctx->type.isTemporary )
  7261. {
  7262. Error(TXT_REF_IS_TEMP, node);
  7263. return -1;
  7264. }
  7265. if( ctx->type.dataType.IsReadOnly() )
  7266. {
  7267. Error(TXT_REF_IS_READ_ONLY, node);
  7268. return -1;
  7269. }
  7270. if( ctx->property_get || ctx->property_set )
  7271. {
  7272. Error(TXT_INVALID_REF_PROP_ACCESS, node);
  7273. return -1;
  7274. }
  7275. if( !ctx->type.isLValue )
  7276. {
  7277. Error(TXT_NOT_LVALUE, node);
  7278. return -1;
  7279. }
  7280. if( ctx->type.isVariable && !ctx->type.dataType.IsReference() )
  7281. ConvertToReference(ctx);
  7282. else if( !ctx->type.dataType.IsReference() )
  7283. {
  7284. Error(TXT_NOT_VALID_REFERENCE, node);
  7285. return -1;
  7286. }
  7287. // Copy the value to a temp before changing it
  7288. ConvertToTempVariable(ctx);
  7289. asASSERT(!ctx->type.isLValue);
  7290. // Increment the value pointed to by the reference still in the register
  7291. asEBCInstr iInc = asBC_INCi, iDec = asBC_DECi;
  7292. if( ctx->type.dataType.IsDoubleType() )
  7293. {
  7294. iInc = asBC_INCd;
  7295. iDec = asBC_DECd;
  7296. }
  7297. else if( ctx->type.dataType.IsFloatType() )
  7298. {
  7299. iInc = asBC_INCf;
  7300. iDec = asBC_DECf;
  7301. }
  7302. else if( ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsUnsignedType() )
  7303. {
  7304. if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt16, false)) ||
  7305. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt16, false)) )
  7306. {
  7307. iInc = asBC_INCi16;
  7308. iDec = asBC_DECi16;
  7309. }
  7310. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt8, false)) ||
  7311. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt8, false)) )
  7312. {
  7313. iInc = asBC_INCi8;
  7314. iDec = asBC_DECi8;
  7315. }
  7316. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt64, false)) ||
  7317. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt64, false)) )
  7318. {
  7319. iInc = asBC_INCi64;
  7320. iDec = asBC_DECi64;
  7321. }
  7322. }
  7323. else
  7324. {
  7325. Error(TXT_ILLEGAL_OPERATION, node);
  7326. return -1;
  7327. }
  7328. if( op == ttInc ) ctx->bc.Instr(iInc); else ctx->bc.Instr(iDec);
  7329. }
  7330. else if( op == ttDot )
  7331. {
  7332. if( node->firstChild->nodeType == snIdentifier )
  7333. {
  7334. ProcessPropertyGetAccessor(ctx, node);
  7335. // Get the property name
  7336. asCString name(&script->code[node->firstChild->tokenPos], node->firstChild->tokenLength);
  7337. // We need to look for get/set property accessors.
  7338. // If found, the context stores information on the get/set accessors
  7339. // until it is known which is to be used.
  7340. int r = 0;
  7341. if( node->next && node->next->tokenType == ttOpenBracket )
  7342. {
  7343. // The property accessor should take an index arg
  7344. asSExprContext dummyArg(engine);
  7345. r = FindPropertyAccessor(name, ctx, &dummyArg, node);
  7346. }
  7347. if( r == 0 )
  7348. r = FindPropertyAccessor(name, ctx, node);
  7349. if( r != 0 )
  7350. return r;
  7351. if( !ctx->type.dataType.IsPrimitive() )
  7352. Dereference(ctx, true);
  7353. if( ctx->type.dataType.IsObjectHandle() )
  7354. {
  7355. // Convert the handle to a normal object
  7356. asCDataType dt = ctx->type.dataType;
  7357. dt.MakeHandle(false);
  7358. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV);
  7359. // The handle may not have been an lvalue, but the dereferenced object is
  7360. ctx->type.isLValue = true;
  7361. }
  7362. // Find the property offset and type
  7363. if( ctx->type.dataType.IsObject() )
  7364. {
  7365. bool isConst = ctx->type.dataType.IsReadOnly();
  7366. asCObjectProperty *prop = builder->GetObjectProperty(ctx->type.dataType, name.AddressOf());
  7367. if( prop )
  7368. {
  7369. // Is the property access allowed?
  7370. if( prop->isPrivate && (!outFunc || outFunc->objectType != ctx->type.dataType.GetObjectType()) )
  7371. {
  7372. asCString msg;
  7373. msg.Format(TXT_PRIVATE_PROP_ACCESS_s, name.AddressOf());
  7374. Error(msg.AddressOf(), node);
  7375. }
  7376. // Put the offset on the stack
  7377. ctx->bc.InstrSHORT_DW(asBC_ADDSi, (short)prop->byteOffset, engine->GetTypeIdFromDataType(asCDataType::CreateObject(ctx->type.dataType.GetObjectType(), false)));
  7378. if( prop->type.IsReference() )
  7379. ctx->bc.Instr(asBC_RDSPTR);
  7380. // Reference to primitive must be stored in the temp register
  7381. if( prop->type.IsPrimitive() )
  7382. {
  7383. ctx->bc.Instr(asBC_PopRPtr);
  7384. }
  7385. // Set the new type (keeping info about temp variable)
  7386. ctx->type.dataType = prop->type;
  7387. ctx->type.dataType.MakeReference(true);
  7388. ctx->type.isVariable = false;
  7389. if( ctx->type.dataType.IsObject() && !ctx->type.dataType.IsObjectHandle() )
  7390. {
  7391. // Objects that are members are not references
  7392. ctx->type.dataType.MakeReference(false);
  7393. }
  7394. ctx->type.dataType.MakeReadOnly(isConst ? true : prop->type.IsReadOnly());
  7395. }
  7396. else
  7397. {
  7398. asCString str;
  7399. str.Format(TXT_s_NOT_MEMBER_OF_s, name.AddressOf(), ctx->type.dataType.Format().AddressOf());
  7400. Error(str.AddressOf(), node);
  7401. return -1;
  7402. }
  7403. }
  7404. else
  7405. {
  7406. asCString str;
  7407. str.Format(TXT_s_NOT_MEMBER_OF_s, name.AddressOf(), ctx->type.dataType.Format().AddressOf());
  7408. Error(str.AddressOf(), node);
  7409. return -1;
  7410. }
  7411. }
  7412. else
  7413. {
  7414. // Make sure it is an object we are accessing
  7415. if( !ctx->type.dataType.IsObject() )
  7416. {
  7417. asCString str;
  7418. str.Format(TXT_ILLEGAL_OPERATION_ON_s, ctx->type.dataType.Format().AddressOf());
  7419. Error(str.AddressOf(), node);
  7420. return -1;
  7421. }
  7422. // Process the get property accessor
  7423. ProcessPropertyGetAccessor(ctx, node);
  7424. bool isConst = false;
  7425. if( ctx->type.dataType.IsObjectHandle() )
  7426. isConst = ctx->type.dataType.IsHandleToConst();
  7427. else
  7428. isConst = ctx->type.dataType.IsReadOnly();
  7429. asCObjectType *trueObj = ctx->type.dataType.GetObjectType();
  7430. asCTypeInfo objType = ctx->type;
  7431. // Compile function call
  7432. int r = CompileFunctionCall(node->firstChild, ctx, trueObj, isConst);
  7433. if( r < 0 ) return r;
  7434. // If the method returned a reference, then we can't release the original
  7435. // object yet, because the reference may be to a member of it
  7436. if( !objType.isTemporary ||
  7437. !(ctx->type.dataType.IsReference() || (ctx->type.dataType.IsObject() && !ctx->type.dataType.IsObjectHandle())) ||
  7438. ctx->type.isVariable ) // If the resulting type is a variable, then the reference is not a member
  7439. {
  7440. // As the method didn't return a reference to a member
  7441. // we can safely release the original object now
  7442. ReleaseTemporaryVariable(objType, &ctx->bc);
  7443. }
  7444. }
  7445. }
  7446. else if( op == ttOpenBracket )
  7447. {
  7448. // If the property access takes an index arg, then we should use that instead of processing it now
  7449. asCString propertyName;
  7450. if( (ctx->property_get && builder->GetFunctionDescription(ctx->property_get)->GetParamCount() == 1) ||
  7451. (ctx->property_set && builder->GetFunctionDescription(ctx->property_set)->GetParamCount() == 2) )
  7452. {
  7453. // Determine the name of the property accessor
  7454. asCScriptFunction *func = 0;
  7455. if( ctx->property_get )
  7456. func = builder->GetFunctionDescription(ctx->property_get);
  7457. else
  7458. func = builder->GetFunctionDescription(ctx->property_set);
  7459. propertyName = func->GetName();
  7460. propertyName = propertyName.SubString(4);
  7461. // Set the original type of the expression so we can re-evaluate the property accessor
  7462. if( func->objectType )
  7463. {
  7464. ctx->type.dataType = asCDataType::CreateObject(func->objectType, ctx->property_const);
  7465. if( ctx->property_handle ) ctx->type.dataType.MakeHandle(true);
  7466. if( ctx->property_ref ) ctx->type.dataType.MakeReference(true);
  7467. }
  7468. ctx->property_get = ctx->property_set = 0;
  7469. if( ctx->property_arg )
  7470. {
  7471. asDELETE(ctx->property_arg, asSExprContext);
  7472. ctx->property_arg = 0;
  7473. }
  7474. }
  7475. else
  7476. {
  7477. if( !ctx->type.dataType.IsObject() )
  7478. {
  7479. asCString str;
  7480. str.Format(TXT_OBJECT_DOESNT_SUPPORT_INDEX_OP, ctx->type.dataType.Format().AddressOf());
  7481. Error(str.AddressOf(), node);
  7482. return -1;
  7483. }
  7484. ProcessPropertyGetAccessor(ctx, node);
  7485. }
  7486. Dereference(ctx, true);
  7487. // Compile the expression
  7488. asSExprContext expr(engine);
  7489. CompileAssignment(node->firstChild, &expr);
  7490. // Check for the existence of the opIndex method
  7491. asSExprContext lctx(engine);
  7492. MergeExprBytecodeAndType(&lctx, ctx);
  7493. int r = 0;
  7494. if( propertyName == "" )
  7495. r = CompileOverloadedDualOperator2(node, "opIndex", &lctx, &expr, ctx);
  7496. if( r == 0 )
  7497. {
  7498. // Check for accessors methods for the opIndex
  7499. r = FindPropertyAccessor(propertyName == "" ? "opIndex" : propertyName.AddressOf(), &lctx, &expr, node);
  7500. if( r == 0 )
  7501. {
  7502. asCString str;
  7503. str.Format(TXT_OBJECT_DOESNT_SUPPORT_INDEX_OP, ctx->type.dataType.Format().AddressOf());
  7504. Error(str.AddressOf(), node);
  7505. return -1;
  7506. }
  7507. else if( r < 0 )
  7508. return -1;
  7509. MergeExprBytecodeAndType(ctx, &lctx);
  7510. }
  7511. }
  7512. return 0;
  7513. }
  7514. int asCCompiler::GetPrecedence(asCScriptNode *op)
  7515. {
  7516. // x * y, x / y, x % y
  7517. // x + y, x - y
  7518. // x <= y, x < y, x >= y, x > y
  7519. // x = =y, x != y, x xor y, x is y, x !is y
  7520. // x and y
  7521. // x or y
  7522. // The following are not used in this function,
  7523. // but should have lower precedence than the above
  7524. // x ? y : z
  7525. // x = y
  7526. // The expression term have the highest precedence
  7527. if( op->nodeType == snExprTerm )
  7528. return 1;
  7529. // Evaluate operators by token
  7530. int tokenType = op->tokenType;
  7531. if( tokenType == ttStar || tokenType == ttSlash || tokenType == ttPercent )
  7532. return 0;
  7533. if( tokenType == ttPlus || tokenType == ttMinus )
  7534. return -1;
  7535. if( tokenType == ttBitShiftLeft ||
  7536. tokenType == ttBitShiftRight ||
  7537. tokenType == ttBitShiftRightArith )
  7538. return -2;
  7539. if( tokenType == ttAmp )
  7540. return -3;
  7541. if( tokenType == ttBitXor )
  7542. return -4;
  7543. if( tokenType == ttBitOr )
  7544. return -5;
  7545. if( tokenType == ttLessThanOrEqual ||
  7546. tokenType == ttLessThan ||
  7547. tokenType == ttGreaterThanOrEqual ||
  7548. tokenType == ttGreaterThan )
  7549. return -6;
  7550. if( tokenType == ttEqual || tokenType == ttNotEqual || tokenType == ttXor || tokenType == ttIs || tokenType == ttNotIs )
  7551. return -7;
  7552. if( tokenType == ttAnd )
  7553. return -8;
  7554. if( tokenType == ttOr )
  7555. return -9;
  7556. // Unknown operator
  7557. asASSERT(false);
  7558. return 0;
  7559. }
  7560. int asCCompiler::MatchArgument(asCArray<int> &funcs, asCArray<int> &matches, const asCTypeInfo *argType, int paramNum, bool allowObjectConstruct)
  7561. {
  7562. bool isExactMatch = false;
  7563. bool isMatchExceptConst = false;
  7564. bool isMatchWithBaseType = false;
  7565. bool isMatchExceptSign = false;
  7566. bool isMatchNotVarType = false;
  7567. asUINT n;
  7568. matches.SetLength(0);
  7569. for( n = 0; n < funcs.GetLength(); n++ )
  7570. {
  7571. asCScriptFunction *desc = builder->GetFunctionDescription(funcs[n]);
  7572. // Does the function have arguments enough?
  7573. if( (int)desc->parameterTypes.GetLength() <= paramNum )
  7574. continue;
  7575. // Can we make the match by implicit conversion?
  7576. asSExprContext ti(engine);
  7577. ti.type = *argType;
  7578. if( argType->dataType.IsPrimitive() ) ti.type.dataType.MakeReference(false);
  7579. ImplicitConversion(&ti, desc->parameterTypes[paramNum], 0, asIC_IMPLICIT_CONV, false, allowObjectConstruct);
  7580. // If the function parameter is an inout-reference then it must not be possible to call the
  7581. // function with an incorrect argument type, even though the type can normally be converted.
  7582. if( desc->parameterTypes[paramNum].IsReference() &&
  7583. desc->inOutFlags[paramNum] == asTM_INOUTREF &&
  7584. desc->parameterTypes[paramNum].GetTokenType() != ttQuestion )
  7585. {
  7586. if( desc->parameterTypes[paramNum].IsPrimitive() &&
  7587. desc->parameterTypes[paramNum].GetTokenType() != argType->dataType.GetTokenType() )
  7588. continue;
  7589. if( desc->parameterTypes[paramNum].IsEnumType() &&
  7590. desc->parameterTypes[paramNum].GetObjectType() != argType->dataType.GetObjectType() )
  7591. continue;
  7592. }
  7593. // How well does the argument match the function parameter?
  7594. if( desc->parameterTypes[paramNum].IsEqualExceptRef(ti.type.dataType) )
  7595. {
  7596. // Is it an exact match?
  7597. if( argType->dataType.IsEqualExceptRef(ti.type.dataType) )
  7598. {
  7599. if( !isExactMatch ) matches.SetLength(0);
  7600. isExactMatch = true;
  7601. matches.PushLast(funcs[n]);
  7602. continue;
  7603. }
  7604. if( !isExactMatch )
  7605. {
  7606. // Is it a match except const?
  7607. if( argType->dataType.IsEqualExceptRefAndConst(ti.type.dataType) )
  7608. {
  7609. if( !isMatchExceptConst ) matches.SetLength(0);
  7610. isMatchExceptConst = true;
  7611. matches.PushLast(funcs[n]);
  7612. continue;
  7613. }
  7614. if( !isMatchExceptConst )
  7615. {
  7616. // Is it a size promotion, e.g. int8 -> int?
  7617. if( argType->dataType.IsSamePrimitiveBaseType(ti.type.dataType) ||
  7618. (argType->dataType.IsEnumType() && ti.type.dataType.IsIntegerType()) )
  7619. {
  7620. if( !isMatchWithBaseType ) matches.SetLength(0);
  7621. isMatchWithBaseType = true;
  7622. matches.PushLast(funcs[n]);
  7623. continue;
  7624. }
  7625. if( !isMatchWithBaseType )
  7626. {
  7627. // Conversion between signed and unsigned integer is better than between integer and float
  7628. // Is it a match except for sign?
  7629. if( (argType->dataType.IsIntegerType() && ti.type.dataType.IsUnsignedType()) ||
  7630. (argType->dataType.IsUnsignedType() && ti.type.dataType.IsIntegerType()) ||
  7631. (argType->dataType.IsEnumType() && ti.type.dataType.IsUnsignedType()) )
  7632. {
  7633. if( !isMatchExceptSign ) matches.SetLength(0);
  7634. isMatchExceptSign = true;
  7635. matches.PushLast(funcs[n]);
  7636. continue;
  7637. }
  7638. if( !isMatchExceptSign )
  7639. {
  7640. // If there was any match without a var type it has higher priority
  7641. if( desc->parameterTypes[paramNum].GetTokenType() != ttQuestion )
  7642. {
  7643. if( !isMatchNotVarType ) matches.SetLength(0);
  7644. isMatchNotVarType = true;
  7645. matches.PushLast(funcs[n]);
  7646. continue;
  7647. }
  7648. // Implicit conversion to ?& has the smallest priority
  7649. if( !isMatchNotVarType )
  7650. matches.PushLast(funcs[n]);
  7651. }
  7652. }
  7653. }
  7654. }
  7655. }
  7656. }
  7657. return (int)matches.GetLength();
  7658. }
  7659. void asCCompiler::PrepareArgument2(asSExprContext *ctx, asSExprContext *arg, asCDataType *paramType, bool isFunction, int refType, bool isMakingCopy)
  7660. {
  7661. // Reference parameters whose value won't be used don't evaluate the expression
  7662. if( paramType->IsReference() && !(refType & asTM_INREF) )
  7663. {
  7664. // Store the original bytecode so that it can be reused when processing the deferred output parameter
  7665. asSExprContext *orig = asNEW(asSExprContext)(engine);
  7666. MergeExprBytecodeAndType(orig, arg);
  7667. arg->origExpr = orig;
  7668. }
  7669. PrepareArgument(paramType, arg, arg->exprNode, isFunction, refType, isMakingCopy);
  7670. // arg still holds the original expression for output parameters
  7671. ctx->bc.AddCode(&arg->bc);
  7672. }
  7673. bool asCCompiler::CompileOverloadedDualOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  7674. {
  7675. ctx->exprNode = node;
  7676. // What type of operator is it?
  7677. int token = node->tokenType;
  7678. if( token == ttUnrecognizedToken )
  7679. {
  7680. // This happens when the compiler is inferring an assignment
  7681. // operation from another action, for example in preparing a value
  7682. // as a function argument
  7683. token = ttAssignment;
  7684. }
  7685. // boolean operators are not overloadable
  7686. if( token == ttAnd ||
  7687. token == ttOr ||
  7688. token == ttXor )
  7689. return false;
  7690. // Dual operators can also be implemented as class methods
  7691. if( token == ttEqual ||
  7692. token == ttNotEqual )
  7693. {
  7694. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  7695. // Find the matching opEquals method
  7696. int r = CompileOverloadedDualOperator2(node, "opEquals", lctx, rctx, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  7697. if( r == 0 )
  7698. {
  7699. // Try again by switching the order of the operands
  7700. r = CompileOverloadedDualOperator2(node, "opEquals", rctx, lctx, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  7701. }
  7702. if( r == 1 )
  7703. {
  7704. if( token == ttNotEqual )
  7705. ctx->bc.InstrSHORT(asBC_NOT, ctx->type.stackOffset);
  7706. // Success, don't continue
  7707. return true;
  7708. }
  7709. else if( r < 0 )
  7710. {
  7711. // Compiler error, don't continue
  7712. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  7713. return true;
  7714. }
  7715. }
  7716. if( token == ttEqual ||
  7717. token == ttNotEqual ||
  7718. token == ttLessThan ||
  7719. token == ttLessThanOrEqual ||
  7720. token == ttGreaterThan ||
  7721. token == ttGreaterThanOrEqual )
  7722. {
  7723. bool swappedOrder = false;
  7724. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  7725. // Find the matching opCmp method
  7726. int r = CompileOverloadedDualOperator2(node, "opCmp", lctx, rctx, ctx, true, asCDataType::CreatePrimitive(ttInt, false));
  7727. if( r == 0 )
  7728. {
  7729. // Try again by switching the order of the operands
  7730. swappedOrder = true;
  7731. r = CompileOverloadedDualOperator2(node, "opCmp", rctx, lctx, ctx, true, asCDataType::CreatePrimitive(ttInt, false));
  7732. }
  7733. if( r == 1 )
  7734. {
  7735. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  7736. int a = AllocateVariable(asCDataType::CreatePrimitive(ttBool, false), true);
  7737. ctx->bc.InstrW_DW(asBC_CMPIi, ctx->type.stackOffset, 0);
  7738. if( token == ttEqual )
  7739. ctx->bc.Instr(asBC_TZ);
  7740. else if( token == ttNotEqual )
  7741. ctx->bc.Instr(asBC_TNZ);
  7742. else if( (token == ttLessThan && !swappedOrder) ||
  7743. (token == ttGreaterThan && swappedOrder) )
  7744. ctx->bc.Instr(asBC_TS);
  7745. else if( (token == ttLessThanOrEqual && !swappedOrder) ||
  7746. (token == ttGreaterThanOrEqual && swappedOrder) )
  7747. ctx->bc.Instr(asBC_TNP);
  7748. else if( (token == ttGreaterThan && !swappedOrder) ||
  7749. (token == ttLessThan && swappedOrder) )
  7750. ctx->bc.Instr(asBC_TP);
  7751. else if( (token == ttGreaterThanOrEqual && !swappedOrder) ||
  7752. (token == ttLessThanOrEqual && swappedOrder) )
  7753. ctx->bc.Instr(asBC_TNS);
  7754. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  7755. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, false), a, true);
  7756. // Success, don't continue
  7757. return true;
  7758. }
  7759. else if( r < 0 )
  7760. {
  7761. // Compiler error, don't continue
  7762. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  7763. return true;
  7764. }
  7765. }
  7766. // The rest of the operators are not commutative, and doesn't require specific return type
  7767. const char *op = 0, *op_r = 0;
  7768. switch( token )
  7769. {
  7770. case ttPlus: op = "opAdd"; op_r = "opAdd_r"; break;
  7771. case ttMinus: op = "opSub"; op_r = "opSub_r"; break;
  7772. case ttStar: op = "opMul"; op_r = "opMul_r"; break;
  7773. case ttSlash: op = "opDiv"; op_r = "opDiv_r"; break;
  7774. case ttPercent: op = "opMod"; op_r = "opMod_r"; break;
  7775. case ttBitOr: op = "opOr"; op_r = "opOr_r"; break;
  7776. case ttAmp: op = "opAnd"; op_r = "opAnd_r"; break;
  7777. case ttBitXor: op = "opXor"; op_r = "opXor_r"; break;
  7778. case ttBitShiftLeft: op = "opShl"; op_r = "opShl_r"; break;
  7779. case ttBitShiftRight: op = "opShr"; op_r = "opShr_r"; break;
  7780. case ttBitShiftRightArith: op = "opUShr"; op_r = "opUShr_r"; break;
  7781. }
  7782. // TODO: Might be interesting to support a concatenation operator, e.g. ~
  7783. if( op && op_r )
  7784. {
  7785. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  7786. // Find the matching operator method
  7787. int r = CompileOverloadedDualOperator2(node, op, lctx, rctx, ctx);
  7788. if( r == 0 )
  7789. {
  7790. // Try again by switching the order of the operands, and using the reversed operator
  7791. r = CompileOverloadedDualOperator2(node, op_r, rctx, lctx, ctx);
  7792. }
  7793. if( r == 1 )
  7794. {
  7795. // Success, don't continue
  7796. return true;
  7797. }
  7798. else if( r < 0 )
  7799. {
  7800. // Compiler error, don't continue
  7801. ctx->type.SetDummy();
  7802. return true;
  7803. }
  7804. }
  7805. // Assignment operators
  7806. op = 0;
  7807. switch( token )
  7808. {
  7809. case ttAssignment: op = "opAssign"; break;
  7810. case ttAddAssign: op = "opAddAssign"; break;
  7811. case ttSubAssign: op = "opSubAssign"; break;
  7812. case ttMulAssign: op = "opMulAssign"; break;
  7813. case ttDivAssign: op = "opDivAssign"; break;
  7814. case ttModAssign: op = "opModAssign"; break;
  7815. case ttOrAssign: op = "opOrAssign"; break;
  7816. case ttAndAssign: op = "opAndAssign"; break;
  7817. case ttXorAssign: op = "opXorAssign"; break;
  7818. case ttShiftLeftAssign: op = "opShlAssign"; break;
  7819. case ttShiftRightLAssign: op = "opShrAssign"; break;
  7820. case ttShiftRightAAssign: op = "opUShrAssign"; break;
  7821. }
  7822. if( op )
  7823. {
  7824. // TODO: Shouldn't accept const lvalue with the assignment operators
  7825. // Find the matching operator method
  7826. int r = CompileOverloadedDualOperator2(node, op, lctx, rctx, ctx);
  7827. if( r == 1 )
  7828. {
  7829. // Success, don't continue
  7830. return true;
  7831. }
  7832. else if( r < 0 )
  7833. {
  7834. // Compiler error, don't continue
  7835. ctx->type.SetDummy();
  7836. return true;
  7837. }
  7838. }
  7839. // No suitable operator was found
  7840. return false;
  7841. }
  7842. // Returns negative on compile error
  7843. // zero on no matching operator
  7844. // one on matching operator
  7845. int asCCompiler::CompileOverloadedDualOperator2(asCScriptNode *node, const char *methodName, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx, bool specificReturn, const asCDataType &returnType)
  7846. {
  7847. // Find the matching method
  7848. if( lctx->type.dataType.IsObject() &&
  7849. (!lctx->type.isExplicitHandle ||
  7850. lctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE) )
  7851. {
  7852. // Is the left value a const?
  7853. bool isConst = false;
  7854. if( lctx->type.dataType.IsObjectHandle() )
  7855. isConst = lctx->type.dataType.IsHandleToConst();
  7856. else
  7857. isConst = lctx->type.dataType.IsReadOnly();
  7858. asCArray<int> funcs;
  7859. asCObjectType *ot = lctx->type.dataType.GetObjectType();
  7860. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  7861. {
  7862. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  7863. if( func->name == methodName &&
  7864. (!specificReturn || func->returnType == returnType) &&
  7865. func->parameterTypes.GetLength() == 1 &&
  7866. (!isConst || func->isReadOnly) )
  7867. {
  7868. // Make sure the method is accessible by the module
  7869. if( builder->module->accessMask & func->accessMask )
  7870. {
  7871. #ifdef AS_DEPRECATED
  7872. // deprecated since 2011-10-04
  7873. asCConfigGroup *group = engine->FindConfigGroupForFunction(func->id);
  7874. if( !group || group->HasModuleAccess(builder->module->name.AddressOf()) )
  7875. #endif
  7876. funcs.PushLast(func->id);
  7877. }
  7878. }
  7879. }
  7880. // Which is the best matching function?
  7881. asCArray<int> ops;
  7882. MatchArgument(funcs, ops, &rctx->type, 0);
  7883. // If the object is not const, then we need to prioritize non-const methods
  7884. if( !isConst )
  7885. FilterConst(ops);
  7886. // Did we find an operator?
  7887. if( ops.GetLength() == 1 )
  7888. {
  7889. // Process the lctx expression as get accessor
  7890. ProcessPropertyGetAccessor(lctx, node);
  7891. // Merge the bytecode so that it forms lvalue.methodName(rvalue)
  7892. asCTypeInfo objType = lctx->type;
  7893. asCArray<asSExprContext *> args;
  7894. args.PushLast(rctx);
  7895. MergeExprBytecode(ctx, lctx);
  7896. ctx->type = lctx->type;
  7897. MakeFunctionCall(ctx, ops[0], objType.dataType.GetObjectType(), args, node);
  7898. // If the method returned a reference, then we can't release the original
  7899. // object yet, because the reference may be to a member of it
  7900. if( !objType.isTemporary ||
  7901. !(ctx->type.dataType.IsReference() || (ctx->type.dataType.IsObject() && !ctx->type.dataType.IsObjectHandle())) ||
  7902. ctx->type.isVariable ) // If the resulting type is a variable, then the reference is not to a member
  7903. {
  7904. // As the index operator didn't return a reference to a
  7905. // member we can release the original object now
  7906. ReleaseTemporaryVariable(objType, &ctx->bc);
  7907. }
  7908. // Found matching operator
  7909. return 1;
  7910. }
  7911. else if( ops.GetLength() > 1 )
  7912. {
  7913. Error(TXT_MORE_THAN_ONE_MATCHING_OP, node);
  7914. PrintMatchingFuncs(ops, node);
  7915. ctx->type.SetDummy();
  7916. // Compiler error
  7917. return -1;
  7918. }
  7919. }
  7920. // No matching operator
  7921. return 0;
  7922. }
  7923. void asCCompiler::MakeFunctionCall(asSExprContext *ctx, int funcId, asCObjectType *objectType, asCArray<asSExprContext*> &args, asCScriptNode * /*node*/, bool useVariable, int stackOffset, int funcPtrVar)
  7924. {
  7925. if( objectType )
  7926. {
  7927. // The ASHANDLE type is really a value type, so if it is a
  7928. // local variable on the stack it must not be dereferenced
  7929. if( !(objectType->flags & asOBJ_ASHANDLE) ||
  7930. !(ctx->type.isVariable && !IsVariableOnHeap(ctx->type.stackOffset)) )
  7931. Dereference(ctx, true);
  7932. // This following warning was removed as there may be valid reasons
  7933. // for calling non-const methods on temporary objects, and we shouldn't
  7934. // warn when there is no way of removing the warning.
  7935. /*
  7936. // Warn if the method is non-const and the object is temporary
  7937. // since the changes will be lost when the object is destroyed.
  7938. // If the object is accessed through a handle, then it is assumed
  7939. // the object is not temporary, even though the handle is.
  7940. if( ctx->type.isTemporary &&
  7941. !ctx->type.dataType.IsObjectHandle() &&
  7942. !engine->scriptFunctions[funcId]->isReadOnly )
  7943. {
  7944. Warning("A non-const method is called on temporary object. Changes to the object may be lost.", node);
  7945. Information(engine->scriptFunctions[funcId]->GetDeclaration(), node);
  7946. }
  7947. */ }
  7948. asCByteCode objBC(engine);
  7949. objBC.AddCode(&ctx->bc);
  7950. PrepareFunctionCall(funcId, &ctx->bc, args);
  7951. // Verify if any of the args variable offsets are used in the other code.
  7952. // If they are exchange the offset for a new one
  7953. asUINT n;
  7954. for( n = 0; n < args.GetLength(); n++ )
  7955. {
  7956. if( args[n]->type.isTemporary && objBC.IsVarUsed(args[n]->type.stackOffset) )
  7957. {
  7958. // Release the current temporary variable
  7959. ReleaseTemporaryVariable(args[n]->type, 0);
  7960. asCDataType dt = args[n]->type.dataType;
  7961. dt.MakeReference(false);
  7962. int l = reservedVariables.GetLength();
  7963. objBC.GetVarsUsed(reservedVariables);
  7964. ctx->bc.GetVarsUsed(reservedVariables);
  7965. int newOffset = AllocateVariable(dt, true, IsVariableOnHeap(args[n]->type.stackOffset));
  7966. reservedVariables.SetLength(l);
  7967. asASSERT( IsVariableOnHeap(args[n]->type.stackOffset) == IsVariableOnHeap(newOffset) );
  7968. ctx->bc.ExchangeVar(args[n]->type.stackOffset, newOffset);
  7969. args[n]->type.stackOffset = (short)newOffset;
  7970. args[n]->type.isTemporary = true;
  7971. args[n]->type.isVariable = true;
  7972. }
  7973. }
  7974. #ifndef AS_OLD
  7975. // If the function will return a value type on the stack, then we must allocate space
  7976. // for that here and push the address on the stack as a hidden argument to the function
  7977. asCScriptFunction *func = builder->GetFunctionDescription(funcId);
  7978. if( func->DoesReturnOnStack() )
  7979. {
  7980. asASSERT(!useVariable);
  7981. useVariable = true;
  7982. stackOffset = AllocateVariable(func->returnType, true);
  7983. ctx->bc.InstrSHORT(asBC_PSF, short(stackOffset));
  7984. }
  7985. #endif
  7986. ctx->bc.AddCode(&objBC);
  7987. MoveArgsToStack(funcId, &ctx->bc, args, objectType ? true : false);
  7988. PerformFunctionCall(funcId, ctx, false, &args, 0, useVariable, stackOffset, funcPtrVar);
  7989. }
  7990. int asCCompiler::CompileOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  7991. {
  7992. IsVariableInitialized(&lctx->type, node);
  7993. IsVariableInitialized(&rctx->type, node);
  7994. if( lctx->type.isExplicitHandle || rctx->type.isExplicitHandle ||
  7995. node->tokenType == ttIs || node->tokenType == ttNotIs )
  7996. {
  7997. CompileOperatorOnHandles(node, lctx, rctx, ctx);
  7998. return 0;
  7999. }
  8000. else
  8001. {
  8002. // Compile an overloaded operator for the two operands
  8003. if( CompileOverloadedDualOperator(node, lctx, rctx, ctx) )
  8004. return 0;
  8005. // If both operands are objects, then we shouldn't continue
  8006. if( lctx->type.dataType.IsObject() && rctx->type.dataType.IsObject() )
  8007. {
  8008. asCString str;
  8009. str.Format(TXT_NO_MATCHING_OP_FOUND_FOR_TYPES_s_AND_s, lctx->type.dataType.Format().AddressOf(), rctx->type.dataType.Format().AddressOf());
  8010. Error(str.AddressOf(), node);
  8011. ctx->type.SetDummy();
  8012. return -1;
  8013. }
  8014. // Process the property get accessors (if any)
  8015. ProcessPropertyGetAccessor(lctx, node);
  8016. ProcessPropertyGetAccessor(rctx, node);
  8017. // Make sure we have two variables or constants
  8018. if( lctx->type.dataType.IsReference() ) ConvertToVariableNotIn(lctx, rctx);
  8019. if( rctx->type.dataType.IsReference() ) ConvertToVariableNotIn(rctx, lctx);
  8020. // Make sure lctx doesn't end up with a variable used in rctx
  8021. if( lctx->type.isTemporary && rctx->bc.IsVarUsed(lctx->type.stackOffset) )
  8022. {
  8023. int offset = AllocateVariableNotIn(lctx->type.dataType, true, false, rctx);
  8024. rctx->bc.ExchangeVar(lctx->type.stackOffset, offset);
  8025. ReleaseTemporaryVariable(offset, 0);
  8026. }
  8027. // Math operators
  8028. // + - * / % += -= *= /= %=
  8029. int op = node->tokenType;
  8030. if( op == ttPlus || op == ttAddAssign ||
  8031. op == ttMinus || op == ttSubAssign ||
  8032. op == ttStar || op == ttMulAssign ||
  8033. op == ttSlash || op == ttDivAssign ||
  8034. op == ttPercent || op == ttModAssign )
  8035. {
  8036. CompileMathOperator(node, lctx, rctx, ctx);
  8037. return 0;
  8038. }
  8039. // Bitwise operators
  8040. // << >> >>> & | ^ <<= >>= >>>= &= |= ^=
  8041. if( op == ttAmp || op == ttAndAssign ||
  8042. op == ttBitOr || op == ttOrAssign ||
  8043. op == ttBitXor || op == ttXorAssign ||
  8044. op == ttBitShiftLeft || op == ttShiftLeftAssign ||
  8045. op == ttBitShiftRight || op == ttShiftRightLAssign ||
  8046. op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  8047. {
  8048. CompileBitwiseOperator(node, lctx, rctx, ctx);
  8049. return 0;
  8050. }
  8051. // Comparison operators
  8052. // == != < > <= >=
  8053. if( op == ttEqual || op == ttNotEqual ||
  8054. op == ttLessThan || op == ttLessThanOrEqual ||
  8055. op == ttGreaterThan || op == ttGreaterThanOrEqual )
  8056. {
  8057. CompileComparisonOperator(node, lctx, rctx, ctx);
  8058. return 0;
  8059. }
  8060. // Boolean operators
  8061. // && || ^^
  8062. if( op == ttAnd || op == ttOr || op == ttXor )
  8063. {
  8064. CompileBooleanOperator(node, lctx, rctx, ctx);
  8065. return 0;
  8066. }
  8067. }
  8068. asASSERT(false);
  8069. return -1;
  8070. }
  8071. void asCCompiler::ConvertToTempVariableNotIn(asSExprContext *ctx, asSExprContext *exclude)
  8072. {
  8073. int l = reservedVariables.GetLength();
  8074. if( exclude ) exclude->bc.GetVarsUsed(reservedVariables);
  8075. ConvertToTempVariable(ctx);
  8076. reservedVariables.SetLength(l);
  8077. }
  8078. void asCCompiler::ConvertToTempVariable(asSExprContext *ctx)
  8079. {
  8080. // This is only used for primitive types and null handles
  8081. asASSERT( ctx->type.dataType.IsPrimitive() || ctx->type.dataType.IsNullHandle() );
  8082. ConvertToVariable(ctx);
  8083. if( !ctx->type.isTemporary )
  8084. {
  8085. if( ctx->type.dataType.IsPrimitive() )
  8086. {
  8087. // Copy the variable to a temporary variable
  8088. int offset = AllocateVariable(ctx->type.dataType, true);
  8089. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8090. ctx->bc.InstrW_W(asBC_CpyVtoV4, offset, ctx->type.stackOffset);
  8091. else
  8092. ctx->bc.InstrW_W(asBC_CpyVtoV8, offset, ctx->type.stackOffset);
  8093. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  8094. }
  8095. else
  8096. {
  8097. // We should never get here
  8098. asASSERT(false);
  8099. }
  8100. }
  8101. }
  8102. void asCCompiler::ConvertToVariable(asSExprContext *ctx)
  8103. {
  8104. // We should never get here while the context is still an unprocessed property accessor
  8105. asASSERT(ctx->property_get == 0 && ctx->property_set == 0);
  8106. int offset;
  8107. if( !ctx->type.isVariable &&
  8108. (ctx->type.dataType.IsObjectHandle() ||
  8109. (ctx->type.dataType.IsObject() && ctx->type.dataType.SupportHandles())) )
  8110. {
  8111. offset = AllocateVariable(ctx->type.dataType, true);
  8112. if( ctx->type.IsNullConstant() )
  8113. {
  8114. if( ctx->bc.GetLastInstr() == asBC_PshC4 || ctx->bc.GetLastInstr() == asBC_PshC8 )
  8115. ctx->bc.Pop(AS_PTR_SIZE); // Pop the null constant pushed onto the stack
  8116. #ifdef AS_64BIT_PTR
  8117. ctx->bc.InstrSHORT_QW(asBC_SetV8, (short)offset, 0);
  8118. #else
  8119. ctx->bc.InstrSHORT_DW(asBC_SetV4, (short)offset, 0);
  8120. #endif
  8121. }
  8122. else
  8123. {
  8124. // Copy the object handle to a variable
  8125. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  8126. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetObjectType());
  8127. ctx->bc.Pop(AS_PTR_SIZE);
  8128. }
  8129. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  8130. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  8131. ctx->type.dataType.MakeHandle(true);
  8132. }
  8133. else if( (!ctx->type.isVariable || ctx->type.dataType.IsReference()) &&
  8134. ctx->type.dataType.IsPrimitive() )
  8135. {
  8136. if( ctx->type.isConstant )
  8137. {
  8138. offset = AllocateVariable(ctx->type.dataType, true);
  8139. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  8140. ctx->bc.InstrSHORT_B(asBC_SetV1, (short)offset, ctx->type.byteValue);
  8141. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  8142. ctx->bc.InstrSHORT_W(asBC_SetV2, (short)offset, ctx->type.wordValue);
  8143. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 4 )
  8144. ctx->bc.InstrSHORT_DW(asBC_SetV4, (short)offset, ctx->type.dwordValue);
  8145. else
  8146. ctx->bc.InstrSHORT_QW(asBC_SetV8, (short)offset, ctx->type.qwordValue);
  8147. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  8148. return;
  8149. }
  8150. else
  8151. {
  8152. asASSERT(ctx->type.dataType.IsPrimitive());
  8153. asASSERT(ctx->type.dataType.IsReference());
  8154. ctx->type.dataType.MakeReference(false);
  8155. offset = AllocateVariable(ctx->type.dataType, true);
  8156. // Read the value from the address in the register directly into the variable
  8157. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  8158. ctx->bc.InstrSHORT(asBC_RDR1, (short)offset);
  8159. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  8160. ctx->bc.InstrSHORT(asBC_RDR2, (short)offset);
  8161. else if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8162. ctx->bc.InstrSHORT(asBC_RDR4, (short)offset);
  8163. else
  8164. ctx->bc.InstrSHORT(asBC_RDR8, (short)offset);
  8165. }
  8166. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  8167. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  8168. }
  8169. }
  8170. void asCCompiler::ConvertToVariableNotIn(asSExprContext *ctx, asSExprContext *exclude)
  8171. {
  8172. int l = reservedVariables.GetLength();
  8173. if( exclude ) exclude->bc.GetVarsUsed(reservedVariables);
  8174. ConvertToVariable(ctx);
  8175. reservedVariables.SetLength(l);
  8176. }
  8177. void asCCompiler::CompileMathOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  8178. {
  8179. // TODO: If a constant is only using 32bits, then a 32bit operation is preferred
  8180. // Implicitly convert the operands to a number type
  8181. asCDataType to;
  8182. if( lctx->type.dataType.IsDoubleType() || rctx->type.dataType.IsDoubleType() )
  8183. to.SetTokenType(ttDouble);
  8184. else if( lctx->type.dataType.IsFloatType() || rctx->type.dataType.IsFloatType() )
  8185. to.SetTokenType(ttFloat);
  8186. else if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 || rctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  8187. {
  8188. if( lctx->type.dataType.IsIntegerType() || rctx->type.dataType.IsIntegerType() )
  8189. to.SetTokenType(ttInt64);
  8190. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  8191. to.SetTokenType(ttUInt64);
  8192. }
  8193. else
  8194. {
  8195. if( lctx->type.dataType.IsIntegerType() || rctx->type.dataType.IsIntegerType() ||
  8196. lctx->type.dataType.IsEnumType() || rctx->type.dataType.IsEnumType() )
  8197. to.SetTokenType(ttInt);
  8198. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  8199. to.SetTokenType(ttUInt);
  8200. }
  8201. // If doing an operation with double constant and float variable, the constant should be converted to float
  8202. if( (lctx->type.isConstant && lctx->type.dataType.IsDoubleType() && !rctx->type.isConstant && rctx->type.dataType.IsFloatType()) ||
  8203. (rctx->type.isConstant && rctx->type.dataType.IsDoubleType() && !lctx->type.isConstant && lctx->type.dataType.IsFloatType()) )
  8204. to.SetTokenType(ttFloat);
  8205. // Do the actual conversion
  8206. int l = reservedVariables.GetLength();
  8207. rctx->bc.GetVarsUsed(reservedVariables);
  8208. lctx->bc.GetVarsUsed(reservedVariables);
  8209. if( lctx->type.dataType.IsReference() )
  8210. ConvertToVariable(lctx);
  8211. if( rctx->type.dataType.IsReference() )
  8212. ConvertToVariable(rctx);
  8213. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV, true);
  8214. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true);
  8215. reservedVariables.SetLength(l);
  8216. // Verify that the conversion was successful
  8217. if( !lctx->type.dataType.IsIntegerType() &&
  8218. !lctx->type.dataType.IsUnsignedType() &&
  8219. !lctx->type.dataType.IsFloatType() &&
  8220. !lctx->type.dataType.IsDoubleType() )
  8221. {
  8222. asCString str;
  8223. str.Format(TXT_NO_CONVERSION_s_TO_MATH_TYPE, lctx->type.dataType.Format().AddressOf());
  8224. Error(str.AddressOf(), node);
  8225. ctx->type.SetDummy();
  8226. return;
  8227. }
  8228. if( !rctx->type.dataType.IsIntegerType() &&
  8229. !rctx->type.dataType.IsUnsignedType() &&
  8230. !rctx->type.dataType.IsFloatType() &&
  8231. !rctx->type.dataType.IsDoubleType() )
  8232. {
  8233. asCString str;
  8234. str.Format(TXT_NO_CONVERSION_s_TO_MATH_TYPE, rctx->type.dataType.Format().AddressOf());
  8235. Error(str.AddressOf(), node);
  8236. ctx->type.SetDummy();
  8237. return;
  8238. }
  8239. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  8240. // Verify if we are dividing with a constant zero
  8241. int op = node->tokenType;
  8242. if( rctx->type.isConstant && rctx->type.qwordValue == 0 &&
  8243. (op == ttSlash || op == ttDivAssign ||
  8244. op == ttPercent || op == ttModAssign) )
  8245. {
  8246. Error(TXT_DIVIDE_BY_ZERO, node);
  8247. }
  8248. if( !isConstant )
  8249. {
  8250. ConvertToVariableNotIn(lctx, rctx);
  8251. ConvertToVariableNotIn(rctx, lctx);
  8252. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  8253. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  8254. if( op == ttAddAssign || op == ttSubAssign ||
  8255. op == ttMulAssign || op == ttDivAssign ||
  8256. op == ttModAssign )
  8257. {
  8258. // Merge the operands in the different order so that they are evaluated correctly
  8259. MergeExprBytecode(ctx, rctx);
  8260. MergeExprBytecode(ctx, lctx);
  8261. }
  8262. else
  8263. {
  8264. MergeExprBytecode(ctx, lctx);
  8265. MergeExprBytecode(ctx, rctx);
  8266. }
  8267. ProcessDeferredParams(ctx);
  8268. asEBCInstr instruction = asBC_ADDi;
  8269. if( lctx->type.dataType.IsIntegerType() ||
  8270. lctx->type.dataType.IsUnsignedType() )
  8271. {
  8272. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8273. {
  8274. if( op == ttPlus || op == ttAddAssign )
  8275. instruction = asBC_ADDi;
  8276. else if( op == ttMinus || op == ttSubAssign )
  8277. instruction = asBC_SUBi;
  8278. else if( op == ttStar || op == ttMulAssign )
  8279. instruction = asBC_MULi;
  8280. else if( op == ttSlash || op == ttDivAssign )
  8281. {
  8282. if( lctx->type.dataType.IsIntegerType() )
  8283. instruction = asBC_DIVi;
  8284. else
  8285. instruction = asBC_DIVu;
  8286. }
  8287. else if( op == ttPercent || op == ttModAssign )
  8288. {
  8289. if( lctx->type.dataType.IsIntegerType() )
  8290. instruction = asBC_MODi;
  8291. else
  8292. instruction = asBC_MODu;
  8293. }
  8294. }
  8295. else
  8296. {
  8297. if( op == ttPlus || op == ttAddAssign )
  8298. instruction = asBC_ADDi64;
  8299. else if( op == ttMinus || op == ttSubAssign )
  8300. instruction = asBC_SUBi64;
  8301. else if( op == ttStar || op == ttMulAssign )
  8302. instruction = asBC_MULi64;
  8303. else if( op == ttSlash || op == ttDivAssign )
  8304. {
  8305. if( lctx->type.dataType.IsIntegerType() )
  8306. instruction = asBC_DIVi64;
  8307. else
  8308. instruction = asBC_DIVu64;
  8309. }
  8310. else if( op == ttPercent || op == ttModAssign )
  8311. {
  8312. if( lctx->type.dataType.IsIntegerType() )
  8313. instruction = asBC_MODi64;
  8314. else
  8315. instruction = asBC_MODu64;
  8316. }
  8317. }
  8318. }
  8319. else if( lctx->type.dataType.IsFloatType() )
  8320. {
  8321. if( op == ttPlus || op == ttAddAssign )
  8322. instruction = asBC_ADDf;
  8323. else if( op == ttMinus || op == ttSubAssign )
  8324. instruction = asBC_SUBf;
  8325. else if( op == ttStar || op == ttMulAssign )
  8326. instruction = asBC_MULf;
  8327. else if( op == ttSlash || op == ttDivAssign )
  8328. instruction = asBC_DIVf;
  8329. else if( op == ttPercent || op == ttModAssign )
  8330. instruction = asBC_MODf;
  8331. }
  8332. else if( lctx->type.dataType.IsDoubleType() )
  8333. {
  8334. if( op == ttPlus || op == ttAddAssign )
  8335. instruction = asBC_ADDd;
  8336. else if( op == ttMinus || op == ttSubAssign )
  8337. instruction = asBC_SUBd;
  8338. else if( op == ttStar || op == ttMulAssign )
  8339. instruction = asBC_MULd;
  8340. else if( op == ttSlash || op == ttDivAssign )
  8341. instruction = asBC_DIVd;
  8342. else if( op == ttPercent || op == ttModAssign )
  8343. instruction = asBC_MODd;
  8344. }
  8345. else
  8346. {
  8347. // Shouldn't be possible
  8348. asASSERT(false);
  8349. }
  8350. // Do the operation
  8351. int a = AllocateVariable(lctx->type.dataType, true);
  8352. int b = lctx->type.stackOffset;
  8353. int c = rctx->type.stackOffset;
  8354. ctx->bc.InstrW_W_W(instruction, a, b, c);
  8355. ctx->type.SetVariable(lctx->type.dataType, a, true);
  8356. }
  8357. else
  8358. {
  8359. // Both values are constants
  8360. if( lctx->type.dataType.IsIntegerType() ||
  8361. lctx->type.dataType.IsUnsignedType() )
  8362. {
  8363. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8364. {
  8365. int v = 0;
  8366. if( op == ttPlus )
  8367. v = lctx->type.intValue + rctx->type.intValue;
  8368. else if( op == ttMinus )
  8369. v = lctx->type.intValue - rctx->type.intValue;
  8370. else if( op == ttStar )
  8371. v = lctx->type.intValue * rctx->type.intValue;
  8372. else if( op == ttSlash )
  8373. {
  8374. if( rctx->type.intValue == 0 )
  8375. v = 0;
  8376. else
  8377. if( lctx->type.dataType.IsIntegerType() )
  8378. v = lctx->type.intValue / rctx->type.intValue;
  8379. else
  8380. v = lctx->type.dwordValue / rctx->type.dwordValue;
  8381. }
  8382. else if( op == ttPercent )
  8383. {
  8384. if( rctx->type.intValue == 0 )
  8385. v = 0;
  8386. else
  8387. if( lctx->type.dataType.IsIntegerType() )
  8388. v = lctx->type.intValue % rctx->type.intValue;
  8389. else
  8390. v = lctx->type.dwordValue % rctx->type.dwordValue;
  8391. }
  8392. ctx->type.SetConstantDW(lctx->type.dataType, v);
  8393. // If the right value is greater than the left value in a minus operation, then we need to convert the type to int
  8394. if( lctx->type.dataType.GetTokenType() == ttUInt && op == ttMinus && lctx->type.intValue < rctx->type.intValue )
  8395. ctx->type.dataType.SetTokenType(ttInt);
  8396. }
  8397. else
  8398. {
  8399. asQWORD v = 0;
  8400. if( op == ttPlus )
  8401. v = lctx->type.qwordValue + rctx->type.qwordValue;
  8402. else if( op == ttMinus )
  8403. v = lctx->type.qwordValue - rctx->type.qwordValue;
  8404. else if( op == ttStar )
  8405. v = lctx->type.qwordValue * rctx->type.qwordValue;
  8406. else if( op == ttSlash )
  8407. {
  8408. if( rctx->type.qwordValue == 0 )
  8409. v = 0;
  8410. else
  8411. if( lctx->type.dataType.IsIntegerType() )
  8412. v = asINT64(lctx->type.qwordValue) / asINT64(rctx->type.qwordValue);
  8413. else
  8414. v = lctx->type.qwordValue / rctx->type.qwordValue;
  8415. }
  8416. else if( op == ttPercent )
  8417. {
  8418. if( rctx->type.qwordValue == 0 )
  8419. v = 0;
  8420. else
  8421. if( lctx->type.dataType.IsIntegerType() )
  8422. v = asINT64(lctx->type.qwordValue) % asINT64(rctx->type.qwordValue);
  8423. else
  8424. v = lctx->type.qwordValue % rctx->type.qwordValue;
  8425. }
  8426. ctx->type.SetConstantQW(lctx->type.dataType, v);
  8427. // If the right value is greater than the left value in a minus operation, then we need to convert the type to int
  8428. if( lctx->type.dataType.GetTokenType() == ttUInt64 && op == ttMinus && lctx->type.qwordValue < rctx->type.qwordValue )
  8429. ctx->type.dataType.SetTokenType(ttInt64);
  8430. }
  8431. }
  8432. else if( lctx->type.dataType.IsFloatType() )
  8433. {
  8434. float v = 0.0f;
  8435. if( op == ttPlus )
  8436. v = lctx->type.floatValue + rctx->type.floatValue;
  8437. else if( op == ttMinus )
  8438. v = lctx->type.floatValue - rctx->type.floatValue;
  8439. else if( op == ttStar )
  8440. v = lctx->type.floatValue * rctx->type.floatValue;
  8441. else if( op == ttSlash )
  8442. {
  8443. if( rctx->type.floatValue == 0 )
  8444. v = 0;
  8445. else
  8446. v = lctx->type.floatValue / rctx->type.floatValue;
  8447. }
  8448. else if( op == ttPercent )
  8449. {
  8450. if( rctx->type.floatValue == 0 )
  8451. v = 0;
  8452. else
  8453. v = fmodf(lctx->type.floatValue, rctx->type.floatValue);
  8454. }
  8455. ctx->type.SetConstantF(lctx->type.dataType, v);
  8456. }
  8457. else if( lctx->type.dataType.IsDoubleType() )
  8458. {
  8459. double v = 0.0;
  8460. if( op == ttPlus )
  8461. v = lctx->type.doubleValue + rctx->type.doubleValue;
  8462. else if( op == ttMinus )
  8463. v = lctx->type.doubleValue - rctx->type.doubleValue;
  8464. else if( op == ttStar )
  8465. v = lctx->type.doubleValue * rctx->type.doubleValue;
  8466. else if( op == ttSlash )
  8467. {
  8468. if( rctx->type.doubleValue == 0 )
  8469. v = 0;
  8470. else
  8471. v = lctx->type.doubleValue / rctx->type.doubleValue;
  8472. }
  8473. else if( op == ttPercent )
  8474. {
  8475. if( rctx->type.doubleValue == 0 )
  8476. v = 0;
  8477. else
  8478. v = fmod(lctx->type.doubleValue, rctx->type.doubleValue);
  8479. }
  8480. ctx->type.SetConstantD(lctx->type.dataType, v);
  8481. }
  8482. else
  8483. {
  8484. // Shouldn't be possible
  8485. asASSERT(false);
  8486. }
  8487. }
  8488. }
  8489. void asCCompiler::CompileBitwiseOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  8490. {
  8491. // TODO: If a constant is only using 32bits, then a 32bit operation is preferred
  8492. int op = node->tokenType;
  8493. if( op == ttAmp || op == ttAndAssign ||
  8494. op == ttBitOr || op == ttOrAssign ||
  8495. op == ttBitXor || op == ttXorAssign )
  8496. {
  8497. // Convert left hand operand to integer if it's not already one
  8498. asCDataType to;
  8499. if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 ||
  8500. rctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  8501. to.SetTokenType(ttUInt64);
  8502. else
  8503. to.SetTokenType(ttUInt);
  8504. // Do the actual conversion
  8505. int l = reservedVariables.GetLength();
  8506. rctx->bc.GetVarsUsed(reservedVariables);
  8507. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV, true);
  8508. reservedVariables.SetLength(l);
  8509. // Verify that the conversion was successful
  8510. if( !lctx->type.dataType.IsUnsignedType() )
  8511. {
  8512. asCString str;
  8513. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  8514. Error(str.AddressOf(), node);
  8515. }
  8516. // Convert right hand operand to same type as left hand operand
  8517. l = reservedVariables.GetLength();
  8518. lctx->bc.GetVarsUsed(reservedVariables);
  8519. ImplicitConversion(rctx, lctx->type.dataType, node, asIC_IMPLICIT_CONV, true);
  8520. reservedVariables.SetLength(l);
  8521. if( !rctx->type.dataType.IsEqualExceptRef(lctx->type.dataType) )
  8522. {
  8523. asCString str;
  8524. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format().AddressOf(), lctx->type.dataType.Format().AddressOf());
  8525. Error(str.AddressOf(), node);
  8526. }
  8527. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  8528. if( !isConstant )
  8529. {
  8530. ConvertToVariableNotIn(lctx, rctx);
  8531. ConvertToVariableNotIn(rctx, lctx);
  8532. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  8533. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  8534. if( op == ttAndAssign || op == ttOrAssign || op == ttXorAssign )
  8535. {
  8536. // Compound assignments execute the right hand value first
  8537. MergeExprBytecode(ctx, rctx);
  8538. MergeExprBytecode(ctx, lctx);
  8539. }
  8540. else
  8541. {
  8542. MergeExprBytecode(ctx, lctx);
  8543. MergeExprBytecode(ctx, rctx);
  8544. }
  8545. ProcessDeferredParams(ctx);
  8546. asEBCInstr instruction = asBC_BAND;
  8547. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8548. {
  8549. if( op == ttAmp || op == ttAndAssign )
  8550. instruction = asBC_BAND;
  8551. else if( op == ttBitOr || op == ttOrAssign )
  8552. instruction = asBC_BOR;
  8553. else if( op == ttBitXor || op == ttXorAssign )
  8554. instruction = asBC_BXOR;
  8555. }
  8556. else
  8557. {
  8558. if( op == ttAmp || op == ttAndAssign )
  8559. instruction = asBC_BAND64;
  8560. else if( op == ttBitOr || op == ttOrAssign )
  8561. instruction = asBC_BOR64;
  8562. else if( op == ttBitXor || op == ttXorAssign )
  8563. instruction = asBC_BXOR64;
  8564. }
  8565. // Do the operation
  8566. int a = AllocateVariable(lctx->type.dataType, true);
  8567. int b = lctx->type.stackOffset;
  8568. int c = rctx->type.stackOffset;
  8569. ctx->bc.InstrW_W_W(instruction, a, b, c);
  8570. ctx->type.SetVariable(lctx->type.dataType, a, true);
  8571. }
  8572. else
  8573. {
  8574. if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  8575. {
  8576. asQWORD v = 0;
  8577. if( op == ttAmp )
  8578. v = lctx->type.qwordValue & rctx->type.qwordValue;
  8579. else if( op == ttBitOr )
  8580. v = lctx->type.qwordValue | rctx->type.qwordValue;
  8581. else if( op == ttBitXor )
  8582. v = lctx->type.qwordValue ^ rctx->type.qwordValue;
  8583. // Remember the result
  8584. ctx->type.SetConstantQW(lctx->type.dataType, v);
  8585. }
  8586. else
  8587. {
  8588. asDWORD v = 0;
  8589. if( op == ttAmp )
  8590. v = lctx->type.dwordValue & rctx->type.dwordValue;
  8591. else if( op == ttBitOr )
  8592. v = lctx->type.dwordValue | rctx->type.dwordValue;
  8593. else if( op == ttBitXor )
  8594. v = lctx->type.dwordValue ^ rctx->type.dwordValue;
  8595. // Remember the result
  8596. ctx->type.SetConstantDW(lctx->type.dataType, v);
  8597. }
  8598. }
  8599. }
  8600. else if( op == ttBitShiftLeft || op == ttShiftLeftAssign ||
  8601. op == ttBitShiftRight || op == ttShiftRightLAssign ||
  8602. op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  8603. {
  8604. // Don't permit object to primitive conversion, since we don't know which integer type is the correct one
  8605. if( lctx->type.dataType.IsObject() )
  8606. {
  8607. asCString str;
  8608. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format().AddressOf());
  8609. Error(str.AddressOf(), node);
  8610. // Set an integer value and allow the compiler to continue
  8611. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), 0);
  8612. return;
  8613. }
  8614. // Convert left hand operand to integer if it's not already one
  8615. asCDataType to = lctx->type.dataType;
  8616. if( lctx->type.dataType.IsUnsignedType() &&
  8617. lctx->type.dataType.GetSizeInMemoryBytes() < 4 )
  8618. {
  8619. to = asCDataType::CreatePrimitive(ttUInt, false);
  8620. }
  8621. else if( !lctx->type.dataType.IsUnsignedType() )
  8622. {
  8623. asCDataType to;
  8624. if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  8625. to.SetTokenType(ttInt64);
  8626. else
  8627. to.SetTokenType(ttInt);
  8628. }
  8629. // Do the actual conversion
  8630. int l = reservedVariables.GetLength();
  8631. rctx->bc.GetVarsUsed(reservedVariables);
  8632. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV, true);
  8633. reservedVariables.SetLength(l);
  8634. // Verify that the conversion was successful
  8635. if( lctx->type.dataType != to )
  8636. {
  8637. asCString str;
  8638. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  8639. Error(str.AddressOf(), node);
  8640. }
  8641. // Right operand must be 32bit uint
  8642. l = reservedVariables.GetLength();
  8643. lctx->bc.GetVarsUsed(reservedVariables);
  8644. ImplicitConversion(rctx, asCDataType::CreatePrimitive(ttUInt, true), node, asIC_IMPLICIT_CONV, true);
  8645. reservedVariables.SetLength(l);
  8646. if( !rctx->type.dataType.IsUnsignedType() )
  8647. {
  8648. asCString str;
  8649. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format().AddressOf(), "uint");
  8650. Error(str.AddressOf(), node);
  8651. }
  8652. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  8653. if( !isConstant )
  8654. {
  8655. ConvertToVariableNotIn(lctx, rctx);
  8656. ConvertToVariableNotIn(rctx, lctx);
  8657. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  8658. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  8659. if( op == ttShiftLeftAssign || op == ttShiftRightLAssign || op == ttShiftRightAAssign )
  8660. {
  8661. // Compound assignments execute the right hand value first
  8662. MergeExprBytecode(ctx, rctx);
  8663. MergeExprBytecode(ctx, lctx);
  8664. }
  8665. else
  8666. {
  8667. MergeExprBytecode(ctx, lctx);
  8668. MergeExprBytecode(ctx, rctx);
  8669. }
  8670. ProcessDeferredParams(ctx);
  8671. asEBCInstr instruction = asBC_BSLL;
  8672. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8673. {
  8674. if( op == ttBitShiftLeft || op == ttShiftLeftAssign )
  8675. instruction = asBC_BSLL;
  8676. else if( op == ttBitShiftRight || op == ttShiftRightLAssign )
  8677. instruction = asBC_BSRL;
  8678. else if( op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  8679. instruction = asBC_BSRA;
  8680. }
  8681. else
  8682. {
  8683. if( op == ttBitShiftLeft || op == ttShiftLeftAssign )
  8684. instruction = asBC_BSLL64;
  8685. else if( op == ttBitShiftRight || op == ttShiftRightLAssign )
  8686. instruction = asBC_BSRL64;
  8687. else if( op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  8688. instruction = asBC_BSRA64;
  8689. }
  8690. // Do the operation
  8691. int a = AllocateVariable(lctx->type.dataType, true);
  8692. int b = lctx->type.stackOffset;
  8693. int c = rctx->type.stackOffset;
  8694. ctx->bc.InstrW_W_W(instruction, a, b, c);
  8695. ctx->type.SetVariable(lctx->type.dataType, a, true);
  8696. }
  8697. else
  8698. {
  8699. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8700. {
  8701. asDWORD v = 0;
  8702. if( op == ttBitShiftLeft )
  8703. v = lctx->type.dwordValue << rctx->type.dwordValue;
  8704. else if( op == ttBitShiftRight )
  8705. v = lctx->type.dwordValue >> rctx->type.dwordValue;
  8706. else if( op == ttBitShiftRightArith )
  8707. v = lctx->type.intValue >> rctx->type.dwordValue;
  8708. ctx->type.SetConstantDW(lctx->type.dataType, v);
  8709. }
  8710. else
  8711. {
  8712. asQWORD v = 0;
  8713. if( op == ttBitShiftLeft )
  8714. v = lctx->type.qwordValue << rctx->type.dwordValue;
  8715. else if( op == ttBitShiftRight )
  8716. v = lctx->type.qwordValue >> rctx->type.dwordValue;
  8717. else if( op == ttBitShiftRightArith )
  8718. v = asINT64(lctx->type.qwordValue) >> rctx->type.dwordValue;
  8719. ctx->type.SetConstantQW(lctx->type.dataType, v);
  8720. }
  8721. }
  8722. }
  8723. }
  8724. void asCCompiler::CompileComparisonOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  8725. {
  8726. // Both operands must be of the same type
  8727. // Implicitly convert the operands to a number type
  8728. asCDataType to;
  8729. if( lctx->type.dataType.IsDoubleType() || rctx->type.dataType.IsDoubleType() )
  8730. to.SetTokenType(ttDouble);
  8731. else if( lctx->type.dataType.IsFloatType() || rctx->type.dataType.IsFloatType() )
  8732. to.SetTokenType(ttFloat);
  8733. else if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 || rctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  8734. {
  8735. if( lctx->type.dataType.IsIntegerType() || rctx->type.dataType.IsIntegerType() )
  8736. to.SetTokenType(ttInt64);
  8737. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  8738. to.SetTokenType(ttUInt64);
  8739. }
  8740. else
  8741. {
  8742. if( lctx->type.dataType.IsIntegerType() || rctx->type.dataType.IsIntegerType() ||
  8743. lctx->type.dataType.IsEnumType() || rctx->type.dataType.IsEnumType() )
  8744. to.SetTokenType(ttInt);
  8745. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  8746. to.SetTokenType(ttUInt);
  8747. else if( lctx->type.dataType.IsBooleanType() || rctx->type.dataType.IsBooleanType() )
  8748. to.SetTokenType(ttBool);
  8749. }
  8750. // If doing an operation with double constant and float variable, the constant should be converted to float
  8751. if( (lctx->type.isConstant && lctx->type.dataType.IsDoubleType() && !rctx->type.isConstant && rctx->type.dataType.IsFloatType()) ||
  8752. (rctx->type.isConstant && rctx->type.dataType.IsDoubleType() && !lctx->type.isConstant && lctx->type.dataType.IsFloatType()) )
  8753. to.SetTokenType(ttFloat);
  8754. // Is it an operation on signed values?
  8755. bool signMismatch = false;
  8756. if( !lctx->type.dataType.IsUnsignedType() || !rctx->type.dataType.IsUnsignedType() )
  8757. {
  8758. if( lctx->type.dataType.GetTokenType() == ttUInt64 )
  8759. {
  8760. if( !lctx->type.isConstant )
  8761. signMismatch = true;
  8762. else if( lctx->type.qwordValue & (I64(1)<<63) )
  8763. signMismatch = true;
  8764. }
  8765. if( lctx->type.dataType.GetTokenType() == ttUInt )
  8766. {
  8767. if( !lctx->type.isConstant )
  8768. signMismatch = true;
  8769. else if( lctx->type.dwordValue & (1<<31) )
  8770. signMismatch = true;
  8771. }
  8772. if( rctx->type.dataType.GetTokenType() == ttUInt64 )
  8773. {
  8774. if( !rctx->type.isConstant )
  8775. signMismatch = true;
  8776. else if( rctx->type.qwordValue & (I64(1)<<63) )
  8777. signMismatch = true;
  8778. }
  8779. if( rctx->type.dataType.GetTokenType() == ttUInt )
  8780. {
  8781. if( !rctx->type.isConstant )
  8782. signMismatch = true;
  8783. else if( rctx->type.dwordValue & (1<<31) )
  8784. signMismatch = true;
  8785. }
  8786. }
  8787. // Check for signed/unsigned mismatch
  8788. if( signMismatch )
  8789. Warning(TXT_SIGNED_UNSIGNED_MISMATCH, node);
  8790. // Do the actual conversion
  8791. int l = reservedVariables.GetLength();
  8792. rctx->bc.GetVarsUsed(reservedVariables);
  8793. if( lctx->type.dataType.IsReference() )
  8794. ConvertToVariable(lctx);
  8795. if( rctx->type.dataType.IsReference() )
  8796. ConvertToVariable(rctx);
  8797. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV);
  8798. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV);
  8799. reservedVariables.SetLength(l);
  8800. // Verify that the conversion was successful
  8801. bool ok = true;
  8802. if( !lctx->type.dataType.IsEqualExceptConst(to) )
  8803. {
  8804. asCString str;
  8805. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  8806. Error(str.AddressOf(), node);
  8807. ok = false;
  8808. }
  8809. if( !rctx->type.dataType.IsEqualExceptConst(to) )
  8810. {
  8811. asCString str;
  8812. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  8813. Error(str.AddressOf(), node);
  8814. ok = false;
  8815. }
  8816. if( !ok )
  8817. {
  8818. // It wasn't possible to get two valid operands, so we just return
  8819. // a boolean result and let the compiler continue.
  8820. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  8821. return;
  8822. }
  8823. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  8824. int op = node->tokenType;
  8825. if( !isConstant )
  8826. {
  8827. if( to.IsBooleanType() )
  8828. {
  8829. int op = node->tokenType;
  8830. if( op == ttEqual || op == ttNotEqual )
  8831. {
  8832. // Must convert to temporary variable, because we are changing the value before comparison
  8833. ConvertToTempVariableNotIn(lctx, rctx);
  8834. ConvertToTempVariableNotIn(rctx, lctx);
  8835. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  8836. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  8837. // Make sure they are equal if not false
  8838. lctx->bc.InstrWORD(asBC_NOT, lctx->type.stackOffset);
  8839. rctx->bc.InstrWORD(asBC_NOT, rctx->type.stackOffset);
  8840. MergeExprBytecode(ctx, lctx);
  8841. MergeExprBytecode(ctx, rctx);
  8842. ProcessDeferredParams(ctx);
  8843. int a = AllocateVariable(asCDataType::CreatePrimitive(ttBool, true), true);
  8844. int b = lctx->type.stackOffset;
  8845. int c = rctx->type.stackOffset;
  8846. if( op == ttEqual )
  8847. {
  8848. ctx->bc.InstrW_W(asBC_CMPi,b,c);
  8849. ctx->bc.Instr(asBC_TZ);
  8850. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  8851. }
  8852. else if( op == ttNotEqual )
  8853. {
  8854. ctx->bc.InstrW_W(asBC_CMPi,b,c);
  8855. ctx->bc.Instr(asBC_TNZ);
  8856. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  8857. }
  8858. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  8859. }
  8860. else
  8861. {
  8862. // TODO: Use TXT_ILLEGAL_OPERATION_ON
  8863. Error(TXT_ILLEGAL_OPERATION, node);
  8864. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), 0);
  8865. }
  8866. }
  8867. else
  8868. {
  8869. ConvertToVariableNotIn(lctx, rctx);
  8870. ConvertToVariableNotIn(rctx, lctx);
  8871. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  8872. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  8873. MergeExprBytecode(ctx, lctx);
  8874. MergeExprBytecode(ctx, rctx);
  8875. ProcessDeferredParams(ctx);
  8876. asEBCInstr iCmp = asBC_CMPi, iT = asBC_TZ;
  8877. if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8878. iCmp = asBC_CMPi;
  8879. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8880. iCmp = asBC_CMPu;
  8881. else if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  8882. iCmp = asBC_CMPi64;
  8883. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  8884. iCmp = asBC_CMPu64;
  8885. else if( lctx->type.dataType.IsFloatType() )
  8886. iCmp = asBC_CMPf;
  8887. else if( lctx->type.dataType.IsDoubleType() )
  8888. iCmp = asBC_CMPd;
  8889. else
  8890. asASSERT(false);
  8891. if( op == ttEqual )
  8892. iT = asBC_TZ;
  8893. else if( op == ttNotEqual )
  8894. iT = asBC_TNZ;
  8895. else if( op == ttLessThan )
  8896. iT = asBC_TS;
  8897. else if( op == ttLessThanOrEqual )
  8898. iT = asBC_TNP;
  8899. else if( op == ttGreaterThan )
  8900. iT = asBC_TP;
  8901. else if( op == ttGreaterThanOrEqual )
  8902. iT = asBC_TNS;
  8903. int a = AllocateVariable(asCDataType::CreatePrimitive(ttBool, true), true);
  8904. int b = lctx->type.stackOffset;
  8905. int c = rctx->type.stackOffset;
  8906. ctx->bc.InstrW_W(iCmp, b, c);
  8907. ctx->bc.Instr(iT);
  8908. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  8909. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  8910. }
  8911. }
  8912. else
  8913. {
  8914. if( to.IsBooleanType() )
  8915. {
  8916. int op = node->tokenType;
  8917. if( op == ttEqual || op == ttNotEqual )
  8918. {
  8919. // Make sure they are equal if not false
  8920. if( lctx->type.dwordValue != 0 ) lctx->type.dwordValue = VALUE_OF_BOOLEAN_TRUE;
  8921. if( rctx->type.dwordValue != 0 ) rctx->type.dwordValue = VALUE_OF_BOOLEAN_TRUE;
  8922. asDWORD v = 0;
  8923. if( op == ttEqual )
  8924. {
  8925. v = lctx->type.intValue - rctx->type.intValue;
  8926. if( v == 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  8927. }
  8928. else if( op == ttNotEqual )
  8929. {
  8930. v = lctx->type.intValue - rctx->type.intValue;
  8931. if( v != 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  8932. }
  8933. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), v);
  8934. }
  8935. else
  8936. {
  8937. // TODO: Use TXT_ILLEGAL_OPERATION_ON
  8938. Error(TXT_ILLEGAL_OPERATION, node);
  8939. }
  8940. }
  8941. else
  8942. {
  8943. int i = 0;
  8944. if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8945. {
  8946. int v = lctx->type.intValue - rctx->type.intValue;
  8947. if( v < 0 ) i = -1;
  8948. if( v > 0 ) i = 1;
  8949. }
  8950. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8951. {
  8952. asDWORD v1 = lctx->type.dwordValue;
  8953. asDWORD v2 = rctx->type.dwordValue;
  8954. if( v1 < v2 ) i = -1;
  8955. if( v1 > v2 ) i = 1;
  8956. }
  8957. else if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  8958. {
  8959. asINT64 v = asINT64(lctx->type.qwordValue) - asINT64(rctx->type.qwordValue);
  8960. if( v < 0 ) i = -1;
  8961. if( v > 0 ) i = 1;
  8962. }
  8963. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  8964. {
  8965. asQWORD v1 = lctx->type.qwordValue;
  8966. asQWORD v2 = rctx->type.qwordValue;
  8967. if( v1 < v2 ) i = -1;
  8968. if( v1 > v2 ) i = 1;
  8969. }
  8970. else if( lctx->type.dataType.IsFloatType() )
  8971. {
  8972. float v = lctx->type.floatValue - rctx->type.floatValue;
  8973. if( v < 0 ) i = -1;
  8974. if( v > 0 ) i = 1;
  8975. }
  8976. else if( lctx->type.dataType.IsDoubleType() )
  8977. {
  8978. double v = lctx->type.doubleValue - rctx->type.doubleValue;
  8979. if( v < 0 ) i = -1;
  8980. if( v > 0 ) i = 1;
  8981. }
  8982. if( op == ttEqual )
  8983. i = (i == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  8984. else if( op == ttNotEqual )
  8985. i = (i != 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  8986. else if( op == ttLessThan )
  8987. i = (i < 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  8988. else if( op == ttLessThanOrEqual )
  8989. i = (i <= 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  8990. else if( op == ttGreaterThan )
  8991. i = (i > 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  8992. else if( op == ttGreaterThanOrEqual )
  8993. i = (i >= 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  8994. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), i);
  8995. }
  8996. }
  8997. }
  8998. void asCCompiler::PushVariableOnStack(asSExprContext *ctx, bool asReference)
  8999. {
  9000. // Put the result on the stack
  9001. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  9002. if( asReference )
  9003. ctx->type.dataType.MakeReference(true);
  9004. else
  9005. {
  9006. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  9007. ctx->bc.Instr(asBC_RDS4);
  9008. else
  9009. ctx->bc.Instr(asBC_RDS8);
  9010. }
  9011. }
  9012. void asCCompiler::CompileBooleanOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  9013. {
  9014. // Both operands must be booleans
  9015. asCDataType to;
  9016. to.SetTokenType(ttBool);
  9017. // Do the actual conversion
  9018. int l = reservedVariables.GetLength();
  9019. rctx->bc.GetVarsUsed(reservedVariables);
  9020. lctx->bc.GetVarsUsed(reservedVariables);
  9021. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV);
  9022. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV);
  9023. reservedVariables.SetLength(l);
  9024. // Verify that the conversion was successful
  9025. if( !lctx->type.dataType.IsBooleanType() )
  9026. {
  9027. asCString str;
  9028. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format().AddressOf(), "bool");
  9029. Error(str.AddressOf(), node);
  9030. // Force the conversion to allow compilation to proceed
  9031. lctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  9032. }
  9033. if( !rctx->type.dataType.IsBooleanType() )
  9034. {
  9035. asCString str;
  9036. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format().AddressOf(), "bool");
  9037. Error(str.AddressOf(), node);
  9038. // Force the conversion to allow compilation to proceed
  9039. rctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  9040. }
  9041. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  9042. ctx->type.Set(asCDataType::CreatePrimitive(ttBool, true));
  9043. // What kind of operator is it?
  9044. int op = node->tokenType;
  9045. if( op == ttXor )
  9046. {
  9047. if( !isConstant )
  9048. {
  9049. // Must convert to temporary variable, because we are changing the value before comparison
  9050. ConvertToTempVariableNotIn(lctx, rctx);
  9051. ConvertToTempVariableNotIn(rctx, lctx);
  9052. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  9053. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  9054. // Make sure they are equal if not false
  9055. lctx->bc.InstrWORD(asBC_NOT, lctx->type.stackOffset);
  9056. rctx->bc.InstrWORD(asBC_NOT, rctx->type.stackOffset);
  9057. MergeExprBytecode(ctx, lctx);
  9058. MergeExprBytecode(ctx, rctx);
  9059. ProcessDeferredParams(ctx);
  9060. int a = AllocateVariable(ctx->type.dataType, true);
  9061. int b = lctx->type.stackOffset;
  9062. int c = rctx->type.stackOffset;
  9063. ctx->bc.InstrW_W_W(asBC_BXOR,a,b,c);
  9064. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  9065. }
  9066. else
  9067. {
  9068. // Make sure they are equal if not false
  9069. #if AS_SIZEOF_BOOL == 1
  9070. if( lctx->type.byteValue != 0 ) lctx->type.byteValue = VALUE_OF_BOOLEAN_TRUE;
  9071. if( rctx->type.byteValue != 0 ) rctx->type.byteValue = VALUE_OF_BOOLEAN_TRUE;
  9072. asBYTE v = 0;
  9073. v = lctx->type.byteValue - rctx->type.byteValue;
  9074. if( v != 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  9075. ctx->type.isConstant = true;
  9076. ctx->type.byteValue = v;
  9077. #else
  9078. if( lctx->type.dwordValue != 0 ) lctx->type.dwordValue = VALUE_OF_BOOLEAN_TRUE;
  9079. if( rctx->type.dwordValue != 0 ) rctx->type.dwordValue = VALUE_OF_BOOLEAN_TRUE;
  9080. asDWORD v = 0;
  9081. v = lctx->type.intValue - rctx->type.intValue;
  9082. if( v != 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  9083. ctx->type.isConstant = true;
  9084. ctx->type.dwordValue = v;
  9085. #endif
  9086. }
  9087. }
  9088. else if( op == ttAnd ||
  9089. op == ttOr )
  9090. {
  9091. if( !isConstant )
  9092. {
  9093. // If or-operator and first value is 1 the second value shouldn't be calculated
  9094. // if and-operator and first value is 0 the second value shouldn't be calculated
  9095. ConvertToVariable(lctx);
  9096. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  9097. MergeExprBytecode(ctx, lctx);
  9098. int offset = AllocateVariable(asCDataType::CreatePrimitive(ttBool, false), true);
  9099. int label1 = nextLabel++;
  9100. int label2 = nextLabel++;
  9101. if( op == ttAnd )
  9102. {
  9103. ctx->bc.InstrSHORT(asBC_CpyVtoR4, lctx->type.stackOffset);
  9104. ctx->bc.Instr(asBC_ClrHi);
  9105. ctx->bc.InstrDWORD(asBC_JNZ, label1);
  9106. ctx->bc.InstrW_DW(asBC_SetV4, (asWORD)offset, 0);
  9107. ctx->bc.InstrINT(asBC_JMP, label2);
  9108. }
  9109. else if( op == ttOr )
  9110. {
  9111. ctx->bc.InstrSHORT(asBC_CpyVtoR4, lctx->type.stackOffset);
  9112. ctx->bc.Instr(asBC_ClrHi);
  9113. ctx->bc.InstrDWORD(asBC_JZ, label1);
  9114. #if AS_SIZEOF_BOOL == 1
  9115. ctx->bc.InstrSHORT_B(asBC_SetV1, (short)offset, VALUE_OF_BOOLEAN_TRUE);
  9116. #else
  9117. ctx->bc.InstrSHORT_DW(asBC_SetV4, (short)offset, VALUE_OF_BOOLEAN_TRUE);
  9118. #endif
  9119. ctx->bc.InstrINT(asBC_JMP, label2);
  9120. }
  9121. ctx->bc.Label((short)label1);
  9122. ConvertToVariable(rctx);
  9123. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  9124. rctx->bc.InstrW_W(asBC_CpyVtoV4, offset, rctx->type.stackOffset);
  9125. MergeExprBytecode(ctx, rctx);
  9126. ctx->bc.Label((short)label2);
  9127. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, false), offset, true);
  9128. }
  9129. else
  9130. {
  9131. #if AS_SIZEOF_BOOL == 1
  9132. asBYTE v = 0;
  9133. if( op == ttAnd )
  9134. v = lctx->type.byteValue && rctx->type.byteValue;
  9135. else if( op == ttOr )
  9136. v = lctx->type.byteValue || rctx->type.byteValue;
  9137. // Remember the result
  9138. ctx->type.isConstant = true;
  9139. ctx->type.byteValue = v;
  9140. #else
  9141. asDWORD v = 0;
  9142. if( op == ttAnd )
  9143. v = lctx->type.dwordValue && rctx->type.dwordValue;
  9144. else if( op == ttOr )
  9145. v = lctx->type.dwordValue || rctx->type.dwordValue;
  9146. // Remember the result
  9147. ctx->type.isConstant = true;
  9148. ctx->type.dwordValue = v;
  9149. #endif
  9150. }
  9151. }
  9152. }
  9153. void asCCompiler::CompileOperatorOnHandles(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  9154. {
  9155. // Process the property accessor as get
  9156. ProcessPropertyGetAccessor(lctx, node);
  9157. ProcessPropertyGetAccessor(rctx, node);
  9158. // Make sure lctx doesn't end up with a variable used in rctx
  9159. if( lctx->type.isTemporary && rctx->bc.IsVarUsed(lctx->type.stackOffset) )
  9160. {
  9161. asCArray<int> vars;
  9162. rctx->bc.GetVarsUsed(vars);
  9163. int offset = AllocateVariable(lctx->type.dataType, true);
  9164. rctx->bc.ExchangeVar(lctx->type.stackOffset, offset);
  9165. ReleaseTemporaryVariable(offset, 0);
  9166. }
  9167. // Warn if not both operands are explicit handles
  9168. if( (node->tokenType == ttEqual || node->tokenType == ttNotEqual) &&
  9169. ((!lctx->type.isExplicitHandle && !(lctx->type.dataType.GetObjectType() && (lctx->type.dataType.GetObjectType()->flags & asOBJ_IMPLICIT_HANDLE))) ||
  9170. (!rctx->type.isExplicitHandle && !(rctx->type.dataType.GetObjectType() && (rctx->type.dataType.GetObjectType()->flags & asOBJ_IMPLICIT_HANDLE)))) )
  9171. {
  9172. Warning(TXT_HANDLE_COMPARISON, node);
  9173. }
  9174. // If one of the operands is a value type used as handle, we should look for the opEquals method
  9175. if( ((lctx->type.dataType.GetObjectType() && (lctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE)) ||
  9176. (rctx->type.dataType.GetObjectType() && (rctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE))) &&
  9177. (node->tokenType == ttEqual || node->tokenType == ttIs ||
  9178. node->tokenType == ttNotEqual || node->tokenType == ttNotIs) )
  9179. {
  9180. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  9181. // Find the matching opEquals method
  9182. int r = CompileOverloadedDualOperator2(node, "opEquals", lctx, rctx, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  9183. if( r == 0 )
  9184. {
  9185. // Try again by switching the order of the operands
  9186. r = CompileOverloadedDualOperator2(node, "opEquals", rctx, lctx, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  9187. }
  9188. if( r == 1 )
  9189. {
  9190. if( node->tokenType == ttNotEqual || node->tokenType == ttNotIs )
  9191. ctx->bc.InstrSHORT(asBC_NOT, ctx->type.stackOffset);
  9192. // Success, don't continue
  9193. return;
  9194. }
  9195. else if( r == 0 )
  9196. {
  9197. // Couldn't find opEquals method
  9198. Error(TXT_NO_APPROPRIATE_OPEQUALS, node);
  9199. }
  9200. // Compiler error, don't continue
  9201. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  9202. return;
  9203. }
  9204. // Implicitly convert null to the other type
  9205. asCDataType to;
  9206. if( lctx->type.IsNullConstant() )
  9207. to = rctx->type.dataType;
  9208. else if( rctx->type.IsNullConstant() )
  9209. to = lctx->type.dataType;
  9210. else
  9211. {
  9212. // TODO: Use the common base type
  9213. to = lctx->type.dataType;
  9214. }
  9215. // Need to pop the value if it is a null constant
  9216. if( lctx->type.IsNullConstant() )
  9217. lctx->bc.Pop(AS_PTR_SIZE);
  9218. if( rctx->type.IsNullConstant() )
  9219. rctx->bc.Pop(AS_PTR_SIZE);
  9220. // Convert both sides to explicit handles
  9221. to.MakeHandle(true);
  9222. to.MakeReference(false);
  9223. // Do the conversion
  9224. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV);
  9225. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV);
  9226. // Both operands must be of the same type
  9227. // Verify that the conversion was successful
  9228. if( !lctx->type.dataType.IsEqualExceptConst(to) )
  9229. {
  9230. asCString str;
  9231. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  9232. Error(str.AddressOf(), node);
  9233. }
  9234. if( !rctx->type.dataType.IsEqualExceptConst(to) )
  9235. {
  9236. asCString str;
  9237. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  9238. Error(str.AddressOf(), node);
  9239. }
  9240. ctx->type.Set(asCDataType::CreatePrimitive(ttBool, true));
  9241. int op = node->tokenType;
  9242. if( op == ttEqual || op == ttNotEqual || op == ttIs || op == ttNotIs )
  9243. {
  9244. // If the object handle already is in a variable we must manually pop it from the stack
  9245. if( lctx->type.isVariable )
  9246. lctx->bc.Pop(AS_PTR_SIZE);
  9247. if( rctx->type.isVariable )
  9248. rctx->bc.Pop(AS_PTR_SIZE);
  9249. // TODO: optimize: Treat the object handles as two integers, i.e. don't do REFCPY
  9250. ConvertToVariableNotIn(lctx, rctx);
  9251. ConvertToVariable(rctx);
  9252. MergeExprBytecode(ctx, lctx);
  9253. MergeExprBytecode(ctx, rctx);
  9254. int a = AllocateVariable(ctx->type.dataType, true);
  9255. int b = lctx->type.stackOffset;
  9256. int c = rctx->type.stackOffset;
  9257. // TODO: When saving the bytecode we must be able to determine that this is
  9258. // a comparison with a pointer, so that the instruction can be adapted
  9259. // to the pointer size on the platform that will execute it.
  9260. #ifdef AS_64BIT_PTR
  9261. ctx->bc.InstrW_W(asBC_CMPi64, b, c);
  9262. #else
  9263. ctx->bc.InstrW_W(asBC_CMPi, b, c);
  9264. #endif
  9265. if( op == ttEqual || op == ttIs )
  9266. ctx->bc.Instr(asBC_TZ);
  9267. else if( op == ttNotEqual || op == ttNotIs )
  9268. ctx->bc.Instr(asBC_TNZ);
  9269. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  9270. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  9271. ReleaseTemporaryVariable(lctx->type, &ctx->bc);
  9272. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  9273. ProcessDeferredParams(ctx);
  9274. }
  9275. else
  9276. {
  9277. // TODO: Use TXT_ILLEGAL_OPERATION_ON
  9278. Error(TXT_ILLEGAL_OPERATION, node);
  9279. }
  9280. }
  9281. void asCCompiler::PerformFunctionCall(int funcId, asSExprContext *ctx, bool isConstructor, asCArray<asSExprContext*> *args, asCObjectType *objType, bool useVariable, int varOffset, int funcPtrVar)
  9282. {
  9283. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  9284. // A shared object may not call non-shared functions
  9285. if( outFunc->objectType && outFunc->objectType->IsShared() && !descr->IsShared() )
  9286. {
  9287. asCString msg;
  9288. msg.Format(TXT_SHARED_CANNOT_CALL_NON_SHARED_FUNC_s, descr->GetDeclarationStr().AddressOf());
  9289. Error(msg.AddressOf(), ctx->exprNode);
  9290. }
  9291. // Check if the function is private
  9292. if( descr->isPrivate && descr->GetObjectType() != outFunc->GetObjectType() )
  9293. {
  9294. asCString msg;
  9295. msg.Format(TXT_PRIVATE_METHOD_CALL_s, descr->GetDeclarationStr().AddressOf());
  9296. Error(msg.AddressOf(), ctx->exprNode);
  9297. }
  9298. int argSize = descr->GetSpaceNeededForArguments();
  9299. if( descr->objectType && descr->returnType.IsReference() &&
  9300. !ctx->type.isVariable && (ctx->type.dataType.IsObjectHandle() || ctx->type.dataType.SupportHandles()) &&
  9301. !(ctx->type.dataType.GetObjectType()->GetFlags() & asOBJ_SCOPED) &&
  9302. !(ctx->type.dataType.GetObjectType()->GetFlags() & asOBJ_ASHANDLE) )
  9303. {
  9304. // The class method we're calling is returning a reference, which may be to a member of the object.
  9305. // In order to guarantee the lifetime of the reference, we must hold a local reference to the object.
  9306. // TODO: optimize: This can be avoided for local variables (non-handles) as they have a well defined life time
  9307. int tempRef = AllocateVariable(ctx->type.dataType, true);
  9308. ctx->bc.InstrSHORT(asBC_PSF, (short)tempRef);
  9309. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetObjectType());
  9310. // Add the release of this reference, as a deferred expression
  9311. asSDeferredParam deferred;
  9312. deferred.origExpr = 0;
  9313. deferred.argInOutFlags = asTM_INREF;
  9314. deferred.argNode = 0;
  9315. deferred.argType.SetVariable(ctx->type.dataType, tempRef, true);
  9316. ctx->deferredParams.PushLast(deferred);
  9317. // Forget the current type
  9318. ctx->type.SetDummy();
  9319. }
  9320. if( isConstructor )
  9321. {
  9322. // Sometimes the value types are allocated on the heap,
  9323. // which is when this way of constructing them is used.
  9324. asASSERT(useVariable == false);
  9325. ctx->bc.Alloc(asBC_ALLOC, objType, descr->id, argSize+AS_PTR_SIZE);
  9326. // The instruction has already moved the returned object to the variable
  9327. ctx->type.Set(asCDataType::CreatePrimitive(ttVoid, false));
  9328. ctx->type.isLValue = false;
  9329. // Clean up arguments
  9330. if( args )
  9331. AfterFunctionCall(funcId, *args, ctx, false);
  9332. ProcessDeferredParams(ctx);
  9333. return;
  9334. }
  9335. else
  9336. {
  9337. if( descr->objectType )
  9338. argSize += AS_PTR_SIZE;
  9339. #ifndef AS_OLD
  9340. // If the function returns an object by value the address of the location
  9341. // where the value should be stored is passed as an argument too
  9342. if( descr->DoesReturnOnStack() )
  9343. {
  9344. argSize += AS_PTR_SIZE;
  9345. }
  9346. #endif
  9347. if( descr->funcType == asFUNC_IMPORTED )
  9348. ctx->bc.Call(asBC_CALLBND , descr->id, argSize);
  9349. // TODO: Maybe we need two different byte codes
  9350. else if( descr->funcType == asFUNC_INTERFACE || descr->funcType == asFUNC_VIRTUAL )
  9351. ctx->bc.Call(asBC_CALLINTF, descr->id, argSize);
  9352. else if( descr->funcType == asFUNC_SCRIPT )
  9353. ctx->bc.Call(asBC_CALL , descr->id, argSize);
  9354. else if( descr->funcType == asFUNC_SYSTEM )
  9355. ctx->bc.Call(asBC_CALLSYS , descr->id, argSize);
  9356. else if( descr->funcType == asFUNC_FUNCDEF )
  9357. ctx->bc.CallPtr(asBC_CallPtr, funcPtrVar, argSize);
  9358. }
  9359. if( descr->returnType.IsObject() && !descr->returnType.IsReference() )
  9360. {
  9361. int returnOffset = 0;
  9362. #ifndef AS_OLD
  9363. if( descr->DoesReturnOnStack() )
  9364. {
  9365. asASSERT( useVariable );
  9366. // The variable was allocated before the function was called
  9367. returnOffset = varOffset;
  9368. ctx->type.SetVariable(descr->returnType, returnOffset, true);
  9369. // The variable was initialized by the function, so we need to mark it as initialized here
  9370. ctx->bc.ObjInfo(varOffset, asOBJ_INIT);
  9371. }
  9372. else
  9373. #endif
  9374. {
  9375. if( useVariable )
  9376. {
  9377. // Use the given variable
  9378. returnOffset = varOffset;
  9379. ctx->type.SetVariable(descr->returnType, returnOffset, false);
  9380. }
  9381. else
  9382. {
  9383. // Allocate a temporary variable for the returned object
  9384. // The returned object will actually be allocated on the heap, so
  9385. // we must force the allocation of the variable to do the same
  9386. returnOffset = AllocateVariable(descr->returnType, true, true);
  9387. ctx->type.SetVariable(descr->returnType, returnOffset, true);
  9388. }
  9389. // Move the pointer from the object register to the temporary variable
  9390. ctx->bc.InstrSHORT(asBC_STOREOBJ, (short)returnOffset);
  9391. }
  9392. ctx->type.dataType.MakeReference(IsVariableOnHeap(returnOffset));
  9393. ctx->type.isLValue = false; // It is a reference, but not an lvalue
  9394. // Clean up arguments
  9395. if( args )
  9396. AfterFunctionCall(funcId, *args, ctx, false);
  9397. ProcessDeferredParams(ctx);
  9398. ctx->bc.InstrSHORT(asBC_PSF, (short)returnOffset);
  9399. }
  9400. else if( descr->returnType.IsReference() )
  9401. {
  9402. asASSERT(useVariable == false);
  9403. // We cannot clean up the arguments yet, because the
  9404. // reference might be pointing to one of them.
  9405. if( args )
  9406. AfterFunctionCall(funcId, *args, ctx, true);
  9407. // Do not process the output parameters yet, because it
  9408. // might invalidate the returned reference
  9409. // If the context holds a variable that needs cleanup
  9410. // store it as a deferred parameter so it will be cleaned up
  9411. // afterwards.
  9412. if( ctx->type.isTemporary )
  9413. {
  9414. asSDeferredParam defer;
  9415. defer.argNode = 0;
  9416. defer.argType = ctx->type;
  9417. defer.argInOutFlags = asTM_INOUTREF;
  9418. defer.origExpr = 0;
  9419. ctx->deferredParams.PushLast(defer);
  9420. }
  9421. ctx->type.Set(descr->returnType);
  9422. if( !descr->returnType.IsPrimitive() )
  9423. {
  9424. ctx->bc.Instr(asBC_PshRPtr);
  9425. if( descr->returnType.IsObject() &&
  9426. (!descr->returnType.IsObjectHandle() || (descr->returnType.GetObjectType()->flags & asOBJ_ASHANDLE)) )
  9427. {
  9428. // We are getting the pointer to the object
  9429. // not a pointer to a object variable
  9430. ctx->type.dataType.MakeReference(false);
  9431. }
  9432. }
  9433. // A returned reference can be used as lvalue
  9434. ctx->type.isLValue = true;
  9435. }
  9436. else
  9437. {
  9438. asASSERT(useVariable == false);
  9439. if( descr->returnType.GetSizeInMemoryBytes() )
  9440. {
  9441. // Allocate a temporary variable to hold the value, but make sure
  9442. // the temporary variable isn't used in any of the deferred arguments
  9443. int l = reservedVariables.GetLength();
  9444. for( asUINT n = 0; args && n < args->GetLength(); n++ )
  9445. {
  9446. asSExprContext *expr = (*args)[n]->origExpr;
  9447. if( expr )
  9448. expr->bc.GetVarsUsed(reservedVariables);
  9449. }
  9450. int offset = AllocateVariable(descr->returnType, true);
  9451. reservedVariables.SetLength(l);
  9452. ctx->type.SetVariable(descr->returnType, offset, true);
  9453. // Move the value from the return register to the variable
  9454. if( descr->returnType.GetSizeOnStackDWords() == 1 )
  9455. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)offset);
  9456. else if( descr->returnType.GetSizeOnStackDWords() == 2 )
  9457. ctx->bc.InstrSHORT(asBC_CpyRtoV8, (short)offset);
  9458. }
  9459. else
  9460. ctx->type.Set(descr->returnType);
  9461. ctx->type.isLValue = false;
  9462. // Clean up arguments
  9463. if( args )
  9464. AfterFunctionCall(funcId, *args, ctx, false);
  9465. ProcessDeferredParams(ctx);
  9466. }
  9467. }
  9468. // This only merges the bytecode, but doesn't modify the type of the final context
  9469. void asCCompiler::MergeExprBytecode(asSExprContext *before, asSExprContext *after)
  9470. {
  9471. before->bc.AddCode(&after->bc);
  9472. for( asUINT n = 0; n < after->deferredParams.GetLength(); n++ )
  9473. {
  9474. before->deferredParams.PushLast(after->deferredParams[n]);
  9475. after->deferredParams[n].origExpr = 0;
  9476. }
  9477. after->deferredParams.SetLength(0);
  9478. }
  9479. // This merges both bytecode and the type of the final context
  9480. void asCCompiler::MergeExprBytecodeAndType(asSExprContext *before, asSExprContext *after)
  9481. {
  9482. MergeExprBytecode(before, after);
  9483. before->type = after->type;
  9484. before->property_get = after->property_get;
  9485. before->property_set = after->property_set;
  9486. before->property_const = after->property_const;
  9487. before->property_handle = after->property_handle;
  9488. before->property_ref = after->property_ref;
  9489. before->property_arg = after->property_arg;
  9490. before->exprNode = after->exprNode;
  9491. after->property_arg = 0;
  9492. // Do not copy the origExpr member
  9493. }
  9494. void asCCompiler::FilterConst(asCArray<int> &funcs)
  9495. {
  9496. if( funcs.GetLength() == 0 ) return;
  9497. // This is only done for object methods
  9498. asCScriptFunction *desc = builder->GetFunctionDescription(funcs[0]);
  9499. if( desc->objectType == 0 ) return;
  9500. // Check if there are any non-const matches
  9501. asUINT n;
  9502. bool foundNonConst = false;
  9503. for( n = 0; n < funcs.GetLength(); n++ )
  9504. {
  9505. desc = builder->GetFunctionDescription(funcs[n]);
  9506. if( !desc->isReadOnly )
  9507. {
  9508. foundNonConst = true;
  9509. break;
  9510. }
  9511. }
  9512. if( foundNonConst )
  9513. {
  9514. // Remove all const methods
  9515. for( n = 0; n < funcs.GetLength(); n++ )
  9516. {
  9517. desc = builder->GetFunctionDescription(funcs[n]);
  9518. if( desc->isReadOnly )
  9519. {
  9520. if( n == funcs.GetLength() - 1 )
  9521. funcs.PopLast();
  9522. else
  9523. funcs[n] = funcs.PopLast();
  9524. n--;
  9525. }
  9526. }
  9527. }
  9528. }
  9529. END_AS_NAMESPACE