XMRObjectTokens.cs 229 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Reflection.Emit;
  33. using System.Text;
  34. using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat;
  35. using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
  36. using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  37. using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
  38. using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
  39. using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  40. using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
  41. /**
  42. * Contains classes that disassemble or decompile an yobj file.
  43. * See xmrengcomp.cx utility program.
  44. */
  45. namespace OpenSim.Region.ScriptEngine.Yengine
  46. {
  47. /*
  48. * Encapsulate object code for a method.
  49. */
  50. public abstract class ObjectTokens
  51. {
  52. public ScriptObjCode scriptObjCode;
  53. public ObjectTokens(ScriptObjCode scriptObjCode)
  54. {
  55. this.scriptObjCode = scriptObjCode;
  56. }
  57. public abstract void Close();
  58. public abstract void BegMethod(DynamicMethod method);
  59. public abstract void EndMethod();
  60. public abstract void DefineLabel(int number, string name);
  61. public abstract void DefineLocal(int number, string name, string type, Type syType);
  62. public abstract void DefineMethod(string methName, Type retType, Type[] argTypes, string[] argNames);
  63. public abstract void MarkLabel(int offset, int number);
  64. public abstract void BegExcBlk(int offset);
  65. public abstract void BegCatBlk(int offset, Type excType);
  66. public abstract void BegFinBlk(int offset);
  67. public abstract void EndExcBlk(int offset);
  68. public abstract void EmitNull(int offset, OpCode opCode);
  69. public abstract void EmitField(int offset, OpCode opCode, FieldInfo field);
  70. public abstract void EmitLocal(int offset, OpCode opCode, int number);
  71. public abstract void EmitType(int offset, OpCode opCode, Type type);
  72. public abstract void EmitLabel(int offset, OpCode opCode, int number);
  73. public abstract void EmitLabels(int offset, OpCode opCode, int[] numbers);
  74. public abstract void EmitMethod(int offset, OpCode opCode, MethodInfo method);
  75. public abstract void EmitCtor(int offset, OpCode opCode, ConstructorInfo ctor);
  76. public abstract void EmitDouble(int offset, OpCode opCode, double value);
  77. public abstract void EmitFloat(int offset, OpCode opCode, float value);
  78. public abstract void EmitInteger(int offset, OpCode opCode, int value);
  79. public abstract void EmitString(int offset, OpCode opCode, string value);
  80. }
  81. /******************\
  82. * DISASSEMBLER *
  83. \******************/
  84. public class OTDisassemble: ObjectTokens
  85. {
  86. private static readonly int OPCSTRWIDTH = 12;
  87. private Dictionary<int, string> labelNames;
  88. private Dictionary<int, string> localNames;
  89. private StringBuilder lbuf = new StringBuilder();
  90. private TextWriter twout;
  91. public OTDisassemble(ScriptObjCode scriptObjCode, TextWriter twout) : base(scriptObjCode)
  92. {
  93. this.twout = twout;
  94. }
  95. public override void Close()
  96. {
  97. twout.WriteLine("TheEnd.");
  98. }
  99. /**
  100. * About to generate object code for this method.
  101. */
  102. public override void BegMethod(DynamicMethod method)
  103. {
  104. labelNames = new Dictionary<int, string>();
  105. localNames = new Dictionary<int, string>();
  106. twout.WriteLine("");
  107. lbuf.Append(method.ReturnType.Name);
  108. lbuf.Append(' ');
  109. lbuf.Append(method.Name);
  110. ParameterInfo[] parms = method.GetParameters();
  111. int nArgs = parms.Length;
  112. lbuf.Append(" (");
  113. for(int i = 0; i < nArgs; i++)
  114. {
  115. if(i > 0)
  116. lbuf.Append(", ");
  117. lbuf.Append(parms[i].ParameterType.Name);
  118. }
  119. lbuf.Append(')');
  120. FlushLine();
  121. lbuf.Append('{');
  122. FlushLine();
  123. }
  124. /**
  125. * Dump out reconstructed source for this method.
  126. */
  127. public override void EndMethod()
  128. {
  129. lbuf.Append('}');
  130. FlushLine();
  131. }
  132. /**
  133. * Add instructions to stream.
  134. */
  135. public override void DefineLabel(int number, string name)
  136. {
  137. labelNames[number] = name + "$" + number;
  138. }
  139. public override void DefineLocal(int number, string name, string type, Type syType)
  140. {
  141. localNames[number] = name + "$" + number;
  142. lbuf.Append(" ");
  143. lbuf.Append(type.PadRight(OPCSTRWIDTH - 1));
  144. lbuf.Append(' ');
  145. lbuf.Append(localNames[number]);
  146. FlushLine();
  147. }
  148. public override void DefineMethod(string methName, Type retType, Type[] argTypes, string[] argNames)
  149. {
  150. }
  151. public override void MarkLabel(int offset, int number)
  152. {
  153. LinePrefix(offset);
  154. lbuf.Append(labelNames[number]);
  155. lbuf.Append(":");
  156. FlushLine();
  157. }
  158. public override void BegExcBlk(int offset)
  159. {
  160. LinePrefix(offset);
  161. lbuf.Append(" BeginExceptionBlock");
  162. FlushLine();
  163. }
  164. public override void BegCatBlk(int offset, Type excType)
  165. {
  166. LinePrefix(offset);
  167. lbuf.Append(" BeginCatchBlock ");
  168. lbuf.Append(excType.Name);
  169. FlushLine();
  170. }
  171. public override void BegFinBlk(int offset)
  172. {
  173. LinePrefix(offset);
  174. lbuf.Append(" BeginFinallyBlock");
  175. FlushLine();
  176. }
  177. public override void EndExcBlk(int offset)
  178. {
  179. LinePrefix(offset);
  180. lbuf.Append(" EndExceptionBlock");
  181. FlushLine();
  182. }
  183. public override void EmitNull(int offset, OpCode opCode)
  184. {
  185. LinePrefix(offset, opCode);
  186. FlushLine();
  187. }
  188. public override void EmitField(int offset, OpCode opCode, FieldInfo field)
  189. {
  190. LinePrefix(offset, opCode);
  191. lbuf.Append(field.DeclaringType.Name);
  192. lbuf.Append(':');
  193. lbuf.Append(field.Name);
  194. lbuf.Append(" -> ");
  195. lbuf.Append(field.FieldType.Name);
  196. lbuf.Append(" (field)");
  197. FlushLine();
  198. }
  199. public override void EmitLocal(int offset, OpCode opCode, int number)
  200. {
  201. LinePrefix(offset, opCode);
  202. lbuf.Append(localNames[number]);
  203. lbuf.Append(" (local)");
  204. FlushLine();
  205. }
  206. public override void EmitType(int offset, OpCode opCode, Type type)
  207. {
  208. LinePrefix(offset, opCode);
  209. lbuf.Append(type.Name);
  210. lbuf.Append(" (type)");
  211. FlushLine();
  212. }
  213. public override void EmitLabel(int offset, OpCode opCode, int number)
  214. {
  215. LinePrefix(offset, opCode);
  216. lbuf.Append(labelNames[number]);
  217. lbuf.Append(" (label)");
  218. FlushLine();
  219. }
  220. public override void EmitLabels(int offset, OpCode opCode, int[] numbers)
  221. {
  222. LinePrefix(offset, opCode);
  223. int lineLen = lbuf.Length;
  224. int nLabels = numbers.Length;
  225. for(int i = 0; i < nLabels; i++)
  226. {
  227. if(i > 0)
  228. {
  229. lbuf.AppendLine();
  230. lbuf.Append(",".PadLeft(lineLen));
  231. }
  232. lbuf.Append(labelNames[numbers[i]]);
  233. }
  234. FlushLine();
  235. }
  236. public override void EmitMethod(int offset, OpCode opCode, MethodInfo method)
  237. {
  238. LinePrefix(offset, opCode);
  239. ParameterInfo[] parms = method.GetParameters();
  240. int nArgs = parms.Length;
  241. if(method.DeclaringType != null)
  242. {
  243. lbuf.Append(method.DeclaringType.Name);
  244. lbuf.Append(':');
  245. }
  246. lbuf.Append(method.Name);
  247. lbuf.Append('(');
  248. for(int i = 0; i < nArgs; i++)
  249. {
  250. if(i > 0)
  251. lbuf.Append(",");
  252. lbuf.Append(parms[i].ParameterType.Name);
  253. }
  254. lbuf.Append(") -> ");
  255. lbuf.Append(method.ReturnType.Name);
  256. FlushLine();
  257. }
  258. public override void EmitCtor(int offset, OpCode opCode, ConstructorInfo ctor)
  259. {
  260. LinePrefix(offset, opCode);
  261. ParameterInfo[] parms = ctor.GetParameters();
  262. int nArgs = parms.Length;
  263. lbuf.Append(ctor.DeclaringType.Name);
  264. lbuf.Append(":(");
  265. for(int i = 0; i < nArgs; i++)
  266. {
  267. if(i > 0)
  268. lbuf.Append(",");
  269. lbuf.Append(parms[i].ParameterType.Name);
  270. }
  271. lbuf.Append(")");
  272. FlushLine();
  273. }
  274. public override void EmitDouble(int offset, OpCode opCode, double value)
  275. {
  276. LinePrefix(offset, opCode);
  277. lbuf.Append(value.ToString());
  278. lbuf.Append(" (double)");
  279. FlushLine();
  280. }
  281. public override void EmitFloat(int offset, OpCode opCode, float value)
  282. {
  283. LinePrefix(offset, opCode);
  284. lbuf.Append(value.ToString());
  285. lbuf.Append(" (float)");
  286. FlushLine();
  287. }
  288. public override void EmitInteger(int offset, OpCode opCode, int value)
  289. {
  290. LinePrefix(offset, opCode);
  291. lbuf.Append(value.ToString());
  292. lbuf.Append(" (int)");
  293. FlushLine();
  294. }
  295. public override void EmitString(int offset, OpCode opCode, string value)
  296. {
  297. LinePrefix(offset, opCode);
  298. lbuf.Append("\"");
  299. lbuf.Append(value);
  300. lbuf.Append("\" (string)");
  301. FlushLine();
  302. }
  303. /**
  304. * Put offset and opcode at beginning of line.
  305. */
  306. private void LinePrefix(int offset, OpCode opCode)
  307. {
  308. LinePrefix(offset);
  309. lbuf.Append(" ");
  310. lbuf.Append(opCode.ToString().PadRight(OPCSTRWIDTH - 1));
  311. lbuf.Append(' ');
  312. }
  313. private void LinePrefix(int offset)
  314. {
  315. lbuf.Append(" ");
  316. lbuf.Append(offset.ToString("X4"));
  317. lbuf.Append(" ");
  318. }
  319. /**
  320. * Flush line buffer to output file.
  321. */
  322. private void FlushLine()
  323. {
  324. if(lbuf.Length > 0)
  325. {
  326. twout.WriteLine(lbuf.ToString());
  327. lbuf.Remove(0, lbuf.Length);
  328. }
  329. }
  330. }
  331. /****************\
  332. * DECOMPILER *
  333. \****************/
  334. /**
  335. * Note: The decompiler does not handle any xmroption extensions
  336. * such as &&&, |||, ? operators and switch statements, as
  337. * they do branches with a non-empty stack, which is way
  338. * beyond this code's ability to analyze.
  339. */
  340. public class OTDecompile: ObjectTokens
  341. {
  342. public const string _mainCallNo = "__mainCallNo$";
  343. public const string _callLabel = "__call_";
  344. public const string _callMode = "callMode";
  345. public const string _checkRunQuick = "CheckRunQuick";
  346. public const string _checkRunStack = "CheckRunStack";
  347. public const string _cmRestore = "__cmRestore";
  348. public const string _doBreak = "dobreak_";
  349. public const string _doCont = "docont_";
  350. public const string _doGblInit = "doGblInit";
  351. public const string _doLoop = "doloop_";
  352. public const string _ehArgs = "ehArgs";
  353. public const string _forBreak = "forbreak_";
  354. public const string _forCont = "forcont_";
  355. public const string _forLoop = "forloop_";
  356. public const string _globalvarinit = "$globalvarinit()";
  357. public const string _heapTrackerPop = "Pop";
  358. public const string _heapTrackerPush = "Push";
  359. public const string _ifDone = "ifdone_";
  360. public const string _ifElse = "ifelse_";
  361. public const string _llAbstemp = "llAbstemp";
  362. public const string _retlbl = "__retlbl";
  363. public const string _retval = "__retval$";
  364. public const string _whileBreak = "whilebreak_";
  365. public const string _whileCont = "whilecont_";
  366. public const string _whileLoop = "whileloop_";
  367. public const string _xmrinst = "__xmrinst";
  368. public const string _xmrinstlocal = "__xmrinst$";
  369. private const string INDENT = " ";
  370. private const string LABELINDENT = " ";
  371. private static Dictionary<string, string> typeTranslator = InitTypeTranslator();
  372. private static Dictionary<string, string> InitTypeTranslator()
  373. {
  374. Dictionary<string, string> d = new Dictionary<string, string>();
  375. d["Boolean"] = "integer";
  376. d["bool"] = "integer";
  377. d["Double"] = "float";
  378. d["double"] = "float";
  379. d["Int32"] = "integer";
  380. d["int"] = "integer";
  381. d["htlist"] = "list";
  382. d["htobject"] = "object";
  383. d["htstring"] = "string";
  384. d["lslfloat"] = "float";
  385. d["lslint"] = "integer";
  386. d["lsllist"] = "list";
  387. d["lslrot"] = "rotation";
  388. d["lslstr"] = "string";
  389. d["lslvec"] = "vector";
  390. d["Quaternion"] = "rotation";
  391. d["String"] = "string";
  392. d["Vector3"] = "vector";
  393. return d;
  394. }
  395. private Dictionary<int, OTLocal> eharglist;
  396. private Dictionary<int, OTLabel> labels;
  397. private Dictionary<int, OTLocal> locals;
  398. private Dictionary<string, string[]> methargnames;
  399. private LinkedList<OTCilInstr> cilinstrs;
  400. private OTStmtBlock topBlock;
  401. private Stack<OTOpnd> opstack;
  402. private Stack<OTStmtBegExcBlk> trystack;
  403. private Stack<OTStmtBlock> blockstack;
  404. private int dupNo;
  405. private DynamicMethod method;
  406. private string laststate;
  407. private TextWriter twout;
  408. public OTDecompile(ScriptObjCode scriptObjCode, TextWriter twout) : base(scriptObjCode)
  409. {
  410. this.twout = twout;
  411. twout.Write("xmroption dollarsigns;");
  412. methargnames = new Dictionary<string, string[]>();
  413. }
  414. public override void Close()
  415. {
  416. if(laststate != null)
  417. {
  418. twout.Write("\n}");
  419. laststate = null;
  420. }
  421. twout.Write('\n');
  422. }
  423. /**
  424. * About to generate object code for this method.
  425. */
  426. public override void BegMethod(DynamicMethod method)
  427. {
  428. this.method = method;
  429. eharglist = new Dictionary<int, OTLocal>();
  430. labels = new Dictionary<int, OTLabel>();
  431. locals = new Dictionary<int, OTLocal>();
  432. cilinstrs = new LinkedList<OTCilInstr>();
  433. opstack = new Stack<OTOpnd>();
  434. trystack = new Stack<OTStmtBegExcBlk>();
  435. blockstack = new Stack<OTStmtBlock>();
  436. dupNo = 0;
  437. }
  438. /**
  439. * Dump out reconstructed source for this method.
  440. */
  441. public override void EndMethod()
  442. {
  443. // Convert CIL code to primitive statements.
  444. // There are a bunch of labels and internal code such as call stack save restore.
  445. topBlock = new OTStmtBlock();
  446. blockstack.Push(topBlock);
  447. for(LinkedListNode<OTCilInstr> link = cilinstrs.First; link != null; link = link.Next)
  448. {
  449. link.Value.BuildStatements(this, link);
  450. }
  451. // Strip out stuff we don't want, such as references to callMode.
  452. // This strips out stack frame capture and restore code.
  453. topBlock.StripStuff(null);
  454. // including a possible final return statement
  455. // - delete if void return value
  456. // - delete if returning __retval cuz we converted all __retval assignments to return statements
  457. if((topBlock.blkstmts.Last != null) && (topBlock.blkstmts.Last.Value is OTStmtRet))
  458. {
  459. OTStmtRet finalret = (OTStmtRet)topBlock.blkstmts.Last.Value;
  460. if((finalret.value == null) ||
  461. ((finalret.value is OTOpndLocal) &&
  462. ((OTOpndLocal)finalret.value).local.name.StartsWith(_retval)))
  463. {
  464. topBlock.blkstmts.RemoveLast();
  465. }
  466. }
  467. // At this point, all behind-the-scenes references are removed except
  468. // that the do/for/if/while blocks are represented by OTStmtCont-style
  469. // if/jumps. So try to convert them to the higher-level structures.
  470. topBlock.DetectDoForIfWhile(null);
  471. // Final strip to get rid of unneeded @forbreak_<suffix>; labels and the like.
  472. topBlock.StripStuff(null);
  473. // Build reference counts so we don't output unneeded declarations,
  474. // especially temps and internal variables.
  475. foreach(OTLocal local in locals.Values)
  476. {
  477. local.nlclreads = 0;
  478. local.nlclwrites = 0;
  479. }
  480. topBlock.CountRefs();
  481. for(IEnumerator<int> localenum = locals.Keys.GetEnumerator(); localenum.MoveNext();)
  482. {
  483. OTLocal local = locals[localenum.Current];
  484. if(((local.nlclreads | local.nlclwrites) == 0) || local.name.StartsWith(_xmrinstlocal))
  485. {
  486. locals.Remove(localenum.Current);
  487. localenum = locals.Keys.GetEnumerator();
  488. }
  489. }
  490. // Strip the $n off of local vars that are not ambiguous.
  491. // Make sure they don't mask globals and arguments as well.
  492. Dictionary<string, int> namecounts = new Dictionary<string, int>();
  493. foreach(Dictionary<int, string> varnames in scriptObjCode.globalVarNames.Values)
  494. {
  495. foreach(string varname in varnames.Values)
  496. {
  497. int count;
  498. if(!namecounts.TryGetValue(varname, out count))
  499. count = 0;
  500. namecounts[varname] = count + 1;
  501. }
  502. }
  503. if(methargnames.ContainsKey(method.Name))
  504. {
  505. foreach(string argname in methargnames[method.Name])
  506. {
  507. int count;
  508. if(!namecounts.TryGetValue(argname, out count))
  509. count = 0;
  510. namecounts[argname] = count + 1;
  511. }
  512. }
  513. foreach(OTLocal local in locals.Values)
  514. {
  515. int i = local.name.LastIndexOf('$');
  516. string name = local.name.Substring(0, i);
  517. int count;
  518. if(!namecounts.TryGetValue(name, out count))
  519. count = 0;
  520. namecounts[name] = count + 1;
  521. }
  522. foreach(OTLocal local in locals.Values)
  523. {
  524. int i = local.name.LastIndexOf('$');
  525. string name = local.name.Substring(0, i);
  526. int count = namecounts[name];
  527. if(count == 1)
  528. local.name = name;
  529. }
  530. // Print out result.
  531. if(method.Name == _globalvarinit)
  532. {
  533. GlobalsDump();
  534. }
  535. else
  536. {
  537. MethodDump();
  538. }
  539. }
  540. /**
  541. * Add instructions to stream.
  542. */
  543. public override void DefineLabel(int number, string name)
  544. {
  545. labels.Add(number, new OTLabel(number, name));
  546. }
  547. public override void DefineLocal(int number, string name, string type, Type syType)
  548. {
  549. locals.Add(number, new OTLocal(number, name, type));
  550. }
  551. public override void DefineMethod(string methName, Type retType, Type[] argTypes, string[] argNames)
  552. {
  553. methargnames[methName] = argNames;
  554. }
  555. public override void MarkLabel(int offset, int number)
  556. {
  557. OTCilInstr label = labels[number];
  558. label.offset = offset;
  559. cilinstrs.AddLast(label);
  560. }
  561. public override void BegExcBlk(int offset)
  562. {
  563. cilinstrs.AddLast(new OTCilBegExcBlk(offset));
  564. }
  565. public override void BegCatBlk(int offset, Type excType)
  566. {
  567. cilinstrs.AddLast(new OTCilBegCatBlk(offset, excType));
  568. }
  569. public override void BegFinBlk(int offset)
  570. {
  571. cilinstrs.AddLast(new OTCilBegFinBlk(offset));
  572. }
  573. public override void EndExcBlk(int offset)
  574. {
  575. cilinstrs.AddLast(new OTCilEndExcBlk(offset));
  576. }
  577. public override void EmitNull(int offset, OpCode opCode)
  578. {
  579. cilinstrs.AddLast(new OTCilNull(offset, opCode));
  580. }
  581. public override void EmitField(int offset, OpCode opCode, FieldInfo field)
  582. {
  583. cilinstrs.AddLast(new OTCilField(offset, opCode, field));
  584. }
  585. public override void EmitLocal(int offset, OpCode opCode, int number)
  586. {
  587. cilinstrs.AddLast(new OTCilLocal(offset, opCode, locals[number]));
  588. }
  589. public override void EmitType(int offset, OpCode opCode, Type type)
  590. {
  591. cilinstrs.AddLast(new OTCilType(offset, opCode, type));
  592. }
  593. public override void EmitLabel(int offset, OpCode opCode, int number)
  594. {
  595. cilinstrs.AddLast(new OTCilLabel(offset, opCode, labels[number]));
  596. }
  597. public override void EmitLabels(int offset, OpCode opCode, int[] numbers)
  598. {
  599. OTLabel[] labelarray = new OTLabel[numbers.Length];
  600. for(int i = 0; i < numbers.Length; i++)
  601. {
  602. labelarray[i] = labels[numbers[i]];
  603. }
  604. cilinstrs.AddLast(new OTCilLabels(offset, opCode, labelarray));
  605. }
  606. public override void EmitMethod(int offset, OpCode opCode, MethodInfo method)
  607. {
  608. cilinstrs.AddLast(new OTCilMethod(offset, opCode, method));
  609. }
  610. public override void EmitCtor(int offset, OpCode opCode, ConstructorInfo ctor)
  611. {
  612. cilinstrs.AddLast(new OTCilCtor(offset, opCode, ctor));
  613. }
  614. public override void EmitDouble(int offset, OpCode opCode, double value)
  615. {
  616. cilinstrs.AddLast(new OTCilDouble(offset, opCode, value));
  617. }
  618. public override void EmitFloat(int offset, OpCode opCode, float value)
  619. {
  620. cilinstrs.AddLast(new OTCilFloat(offset, opCode, value));
  621. }
  622. public override void EmitInteger(int offset, OpCode opCode, int value)
  623. {
  624. cilinstrs.AddLast(new OTCilInteger(offset, opCode, value));
  625. }
  626. public override void EmitString(int offset, OpCode opCode, string value)
  627. {
  628. cilinstrs.AddLast(new OTCilString(offset, opCode, value));
  629. }
  630. /**
  631. * Add the given statement to the end of the currently open block.
  632. */
  633. public void AddLastStmt(OTStmt stmt)
  634. {
  635. blockstack.Peek().blkstmts.AddLast(stmt);
  636. }
  637. /**
  638. * Generate output for $globalvarinit() function.
  639. * Also outputs declarations for global variables.
  640. */
  641. private void GlobalsDump()
  642. {
  643. // Scan $globalvarinit(). It should only have global var assignments in it.
  644. // Also gather up list of variables it initializes.
  645. bool badinit = false;
  646. Dictionary<string, string> inittypes = new Dictionary<string, string>();
  647. foreach(OTStmt stmt in topBlock.blkstmts)
  648. {
  649. if(!(stmt is OTStmtStore))
  650. {
  651. badinit = true;
  652. break;
  653. }
  654. OTStmtStore store = (OTStmtStore)stmt;
  655. if(!(store.varwr is OTOpndGlobal))
  656. {
  657. badinit = true;
  658. break;
  659. }
  660. OTOpndGlobal globalop = (OTOpndGlobal)store.varwr;
  661. inittypes[globalop.PrintableString] = "";
  662. }
  663. // Scan through list of all global variables in the script.
  664. // Output declarations for those what don't have any init statement for them.
  665. // Save the type for those that do have init statements.
  666. bool first = true;
  667. foreach(string iartypename in scriptObjCode.globalVarNames.Keys)
  668. {
  669. Dictionary<int, string> varnames = scriptObjCode.globalVarNames[iartypename];
  670. string typename = iartypename.ToLowerInvariant();
  671. if(typename.StartsWith("iar"))
  672. typename = typename.Substring(3);
  673. if(typename.EndsWith("s"))
  674. typename = typename.Substring(0, typename.Length - 1);
  675. foreach(string varname in varnames.Values)
  676. {
  677. if(!badinit && inittypes.ContainsKey(varname))
  678. {
  679. inittypes[varname] = typename;
  680. }
  681. else
  682. {
  683. if(first)
  684. twout.Write('\n');
  685. twout.Write('\n' + typename + ' ' + varname + ';');
  686. first = false;
  687. }
  688. }
  689. }
  690. // If $globalvarinit() has anything bad in it, output it as a function.
  691. // Otherwise, output it as a series of global declarations with init values.
  692. if(badinit)
  693. {
  694. MethodDump();
  695. }
  696. else
  697. {
  698. foreach(OTStmt stmt in topBlock.blkstmts)
  699. {
  700. OTStmtStore store = (OTStmtStore)stmt;
  701. OTOpndGlobal globalop = (OTOpndGlobal)store.varwr;
  702. string name = globalop.PrintableString;
  703. if(first)
  704. twout.Write('\n');
  705. twout.Write('\n' + inittypes[name] + ' ');
  706. store.PrintStmt(twout, "");
  707. first = false;
  708. }
  709. }
  710. }
  711. /**
  712. * Generate output for other functions.
  713. */
  714. private void MethodDump()
  715. {
  716. string indent;
  717. // Event handlers don't have an argument list as such in the original
  718. // code. Instead they have a series of assignments from ehargs[] to
  719. // local variables. So make those local variables look like they are
  720. // an argument list.
  721. int i = method.Name.IndexOf(' ');
  722. if(i >= 0)
  723. {
  724. // Maybe we have to output the state name.
  725. string statename = method.Name.Substring(0, i);
  726. string eventname = method.Name.Substring(++i);
  727. if(laststate != statename)
  728. {
  729. if(laststate != null)
  730. twout.Write("\n}");
  731. if(statename == "default")
  732. {
  733. twout.Write("\n\ndefault {");
  734. }
  735. else
  736. {
  737. twout.Write("\n\nstate " + statename + " {");
  738. }
  739. laststate = statename;
  740. }
  741. else
  742. {
  743. twout.Write('\n');
  744. }
  745. // Output event name and argument list.
  746. // Remove from locals list so they don't print below.
  747. twout.Write('\n' + INDENT + eventname + " (");
  748. MethodInfo meth = typeof(IEventHandlers).GetMethod(eventname);
  749. i = 0;
  750. foreach(ParameterInfo pi in meth.GetParameters())
  751. {
  752. // skip the first param cuz it's the XMRInstance arg
  753. if(i > 0)
  754. twout.Write(", ");
  755. OTLocal local;
  756. if(eharglist.TryGetValue(i, out local) && locals.ContainsKey(local.number))
  757. {
  758. twout.Write(local.DumpString());
  759. locals.Remove(local.number);
  760. }
  761. else
  762. {
  763. // maybe the assignment was removed
  764. // eg, because the local was write-only (not referenced)
  765. // so substitute in placeholder that won't be referenced
  766. twout.Write(AbbrType(pi.ParameterType) + " arg$" + (i + 1));
  767. }
  768. i++;
  769. }
  770. twout.Write(')');
  771. // Indent method body by 4 spaces.
  772. indent = INDENT;
  773. }
  774. else
  775. {
  776. // Maybe need to close out previous state.
  777. if(laststate != null)
  778. {
  779. twout.Write("\n}");
  780. laststate = null;
  781. }
  782. // Output blank line and return type (if any).
  783. twout.Write("\n\n");
  784. if(method.ReturnType != typeof(void))
  785. {
  786. twout.Write(AbbrType(method.ReturnType) + ' ');
  787. }
  788. // Output method name and argument list.
  789. int j = method.Name.IndexOf('(');
  790. if(j < 0)
  791. {
  792. twout.Write(method.Name);
  793. }
  794. else
  795. {
  796. twout.Write(method.Name.Substring(0, j) + " (");
  797. bool first = true;
  798. j = 0;
  799. foreach(ParameterInfo pi in method.GetParameters())
  800. {
  801. if(j > 0)
  802. { // skip the XMRInstance arg$0 parameter
  803. if(!first)
  804. twout.Write(", ");
  805. twout.Write(AbbrType(pi.ParameterType) + ' ' + MethArgName(j));
  806. first = false;
  807. }
  808. j++;
  809. }
  810. twout.Write(')');
  811. }
  812. // Don't indent method body at all.
  813. indent = "";
  814. }
  815. // Output local variable declarations.
  816. twout.Write('\n' + indent + '{');
  817. bool didOne = false;
  818. foreach(OTLocal local in locals.Values)
  819. {
  820. twout.Write('\n' + indent + INDENT + local.DumpString() + "; // r:" + local.nlclreads + " w:" + local.nlclwrites);
  821. didOne = true;
  822. }
  823. if(didOne)
  824. twout.Write('\n');
  825. // Output statements.
  826. if(topBlock.blkstmts.Count == 0)
  827. {
  828. twout.Write(" }");
  829. }
  830. else
  831. {
  832. topBlock.PrintBodyAndEnd(twout, indent);
  833. }
  834. }
  835. /**
  836. * Get abbreviated type string.
  837. */
  838. public static string AbbrType(Type type)
  839. {
  840. if(type == null)
  841. return "null";
  842. return AbbrType(type.Name);
  843. }
  844. public static string AbbrType(string type)
  845. {
  846. if(type.StartsWith("OpenSim.Region.ScriptEngine.YEngine."))
  847. {
  848. type = type.Substring(38);
  849. int i = type.IndexOf(',');
  850. if(i > 0)
  851. type = type.Substring(0, i);
  852. }
  853. if(typeTranslator.ContainsKey(type))
  854. {
  855. type = typeTranslator[type];
  856. }
  857. return type;
  858. }
  859. /**
  860. * Get current method's argument name.
  861. */
  862. public string MethArgName(int index)
  863. {
  864. string[] argnames;
  865. if(methargnames.TryGetValue(method.Name, out argnames) && (index < argnames.Length))
  866. {
  867. return argnames[index];
  868. }
  869. return "arg$" + index;
  870. }
  871. /**
  872. * Strip svperflvovs (float) cast from rotation/vector values.
  873. */
  874. public static OTOpnd StripFloatCast(OTOpnd op)
  875. {
  876. if(op is OTOpndCast)
  877. {
  878. OTOpndCast opcast = (OTOpndCast)op;
  879. if((opcast.type == typeof(double)) && (opcast.value is OTOpndInt))
  880. {
  881. return opcast.value;
  882. }
  883. }
  884. return op;
  885. }
  886. /**
  887. * Strip svperflvovs Brtrues so we don't end up with stuff like 'if (!! someint) ...'.
  888. */
  889. public static OTOpnd StripBrtrue(OTOpnd op)
  890. {
  891. if(op is OTOpndUnOp)
  892. {
  893. OTOpndUnOp opunop = (OTOpndUnOp)op;
  894. if(opunop.opCode == MyOp.Brtrue)
  895. return opunop.value;
  896. }
  897. return op;
  898. }
  899. /*
  900. * Local variable declaration.
  901. */
  902. private class OTLocal
  903. {
  904. public int number;
  905. public string name;
  906. public string type;
  907. public int nlclreads;
  908. public int nlclwrites;
  909. public OTLocal(int number, string name, string type)
  910. {
  911. this.number = number;
  912. this.name = name.StartsWith("tmp$") ? name : name + "$" + number;
  913. this.type = type;
  914. }
  915. public string DumpString()
  916. {
  917. return AbbrType(type) + ' ' + name;
  918. }
  919. }
  920. /***********************************************\
  921. * Tokens that are one-for-one with CIL code *
  922. \***********************************************/
  923. /*
  924. * Part of instruction stream.
  925. */
  926. public abstract class OTCilInstr
  927. {
  928. public int offset; // cil offset
  929. public OTCilInstr(int offset)
  930. {
  931. this.offset = offset;
  932. }
  933. public abstract string DumpString();
  934. public abstract void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link);
  935. protected void CheckEmptyStack(OTDecompile decompile, string opMnemonic)
  936. {
  937. if(decompile.opstack.Count > 0)
  938. {
  939. Console.Error.WriteLine("CheckEmptyStack: " + decompile.method.Name + " 0x" + offset.ToString("X") + ": " +
  940. opMnemonic + " stack depth " + decompile.opstack.Count);
  941. }
  942. }
  943. }
  944. /*
  945. * Label mark point.
  946. */
  947. private class OTLabel: OTCilInstr
  948. {
  949. public int number;
  950. public string name;
  951. public int lbljumps;
  952. public OTLabel(int number, string name) : base(-1)
  953. {
  954. this.number = number;
  955. this.name = name;
  956. }
  957. public string PrintableName
  958. {
  959. get
  960. {
  961. if(name.StartsWith(_doBreak))
  962. return _doBreak + "$" + number;
  963. if(name.StartsWith(_doCont))
  964. return _doCont + "$" + number;
  965. if(name.StartsWith(_forBreak))
  966. return _forBreak + "$" + number;
  967. if(name.StartsWith(_forCont))
  968. return _forCont + "$" + number;
  969. if(name.StartsWith(_whileBreak))
  970. return _whileBreak + "$" + number;
  971. if(name.StartsWith(_whileCont))
  972. return _whileCont + "$" + number;
  973. return name;
  974. }
  975. }
  976. public override string DumpString()
  977. {
  978. return name + ":";
  979. }
  980. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  981. {
  982. OTStmtLabel.AddLast(decompile, this);
  983. }
  984. }
  985. /*
  986. * 'try {'
  987. */
  988. private class OTCilBegExcBlk: OTCilInstr
  989. {
  990. public LinkedList<OTCilBegCatBlk> catches = new LinkedList<OTCilBegCatBlk>();
  991. public OTCilBegExcBlk(int offset) : base(offset)
  992. {
  993. }
  994. public override string DumpString()
  995. {
  996. return "try {";
  997. }
  998. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  999. {
  1000. CheckEmptyStack(decompile, "try");
  1001. // link the try itself onto outer block
  1002. OTStmtBegExcBlk trystmt = new OTStmtBegExcBlk();
  1003. decompile.AddLastStmt(trystmt);
  1004. // subsequent statements go to the try block
  1005. trystmt.tryblock = new OTStmtBlock();
  1006. decompile.trystack.Push(trystmt);
  1007. decompile.blockstack.Push(trystmt.tryblock);
  1008. }
  1009. }
  1010. /*
  1011. * '} catch (...) {'
  1012. */
  1013. private class OTCilBegCatBlk: OTCilInstr
  1014. {
  1015. public Type excType;
  1016. public OTCilBegCatBlk(int offset, Type excType) : base(offset)
  1017. {
  1018. this.excType = excType;
  1019. }
  1020. public override string DumpString()
  1021. {
  1022. return "} catch (" + AbbrType(excType) + ") {";
  1023. }
  1024. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  1025. {
  1026. CheckEmptyStack(decompile, "catch");
  1027. // link the catch itself onto the try statement
  1028. OTStmtBegExcBlk trystmt = decompile.trystack.Peek();
  1029. OTStmtBegCatBlk catstmt = new OTStmtBegCatBlk(excType);
  1030. trystmt.catches.AddLast(catstmt);
  1031. // start capturing statements into the catch block
  1032. catstmt.tryblock = trystmt;
  1033. catstmt.catchblock = new OTStmtBlock();
  1034. decompile.blockstack.Pop();
  1035. decompile.blockstack.Push(catstmt.catchblock);
  1036. // fill the stack slot with something for the exception argument
  1037. OTOpndDup dup = new OTOpndDup(++decompile.dupNo);
  1038. decompile.opstack.Push(dup);
  1039. }
  1040. }
  1041. /*
  1042. * '} finally {'
  1043. */
  1044. private class OTCilBegFinBlk: OTCilInstr
  1045. {
  1046. public OTCilBegFinBlk(int offset) : base(offset)
  1047. {
  1048. }
  1049. public override string DumpString()
  1050. {
  1051. return "} finally {";
  1052. }
  1053. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  1054. {
  1055. CheckEmptyStack(decompile, "finally");
  1056. // link the finally itself to the try statement
  1057. OTStmtBegExcBlk trystmt = decompile.trystack.Peek();
  1058. OTStmtBegFinBlk finstmt = new OTStmtBegFinBlk();
  1059. trystmt.finblock = finstmt;
  1060. // start capturing statements into the finally block
  1061. finstmt.tryblock = trystmt;
  1062. finstmt.finblock = new OTStmtBlock();
  1063. decompile.blockstack.Pop();
  1064. decompile.blockstack.Push(finstmt.finblock);
  1065. }
  1066. }
  1067. /*
  1068. * '}' end of try
  1069. */
  1070. private class OTCilEndExcBlk: OTCilInstr
  1071. {
  1072. public OTCilEndExcBlk(int offset) : base(offset)
  1073. {
  1074. }
  1075. public override string DumpString()
  1076. {
  1077. return "} // end try";
  1078. }
  1079. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  1080. {
  1081. CheckEmptyStack(decompile, "endtry");
  1082. // pop the try/catch/finally blocks from stacks
  1083. decompile.blockstack.Pop();
  1084. decompile.trystack.Pop();
  1085. // subsequent statements collect following the try
  1086. }
  1087. }
  1088. /*
  1089. * Actual opcodes (instructions).
  1090. */
  1091. private class OTCilNull: OTCilInstr
  1092. {
  1093. public MyOp opCode;
  1094. public OTCilNull(int offset, OpCode opCode) : base(offset)
  1095. {
  1096. this.opCode = MyOp.GetByName(opCode.Name);
  1097. }
  1098. public override string DumpString()
  1099. {
  1100. return opCode.ToString();
  1101. }
  1102. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  1103. {
  1104. switch(opCode.ToString())
  1105. {
  1106. case "conv.i1":
  1107. case "conv.i2":
  1108. case "conv.i4":
  1109. case "conv.i8":
  1110. {
  1111. OTOpnd value = decompile.opstack.Pop();
  1112. decompile.opstack.Push(new OTOpndCast(typeof(int), value));
  1113. break;
  1114. }
  1115. case "conv.r4":
  1116. case "conv.r8":
  1117. {
  1118. OTOpnd value = decompile.opstack.Pop();
  1119. decompile.opstack.Push(new OTOpndCast(typeof(double), value));
  1120. break;
  1121. }
  1122. case "dup":
  1123. {
  1124. OTOpnd value = decompile.opstack.Pop();
  1125. if(!(value is OTOpndDup))
  1126. {
  1127. OTOpndDup dup = new OTOpndDup(++decompile.dupNo);
  1128. OTStmtStore.AddLast(decompile, dup, value);
  1129. value = dup;
  1130. }
  1131. decompile.opstack.Push(value);
  1132. decompile.opstack.Push(value);
  1133. break;
  1134. }
  1135. case "endfinally":
  1136. break;
  1137. case "ldarg.0":
  1138. {
  1139. decompile.opstack.Push(new OTOpndArg(0, false, decompile));
  1140. break;
  1141. }
  1142. case "ldarg.1":
  1143. {
  1144. decompile.opstack.Push(new OTOpndArg(1, false, decompile));
  1145. break;
  1146. }
  1147. case "ldarg.2":
  1148. {
  1149. decompile.opstack.Push(new OTOpndArg(2, false, decompile));
  1150. break;
  1151. }
  1152. case "ldarg.3":
  1153. {
  1154. decompile.opstack.Push(new OTOpndArg(3, false, decompile));
  1155. break;
  1156. }
  1157. case "ldc.i4.0":
  1158. {
  1159. decompile.opstack.Push(new OTOpndInt(0));
  1160. break;
  1161. }
  1162. case "ldc.i4.1":
  1163. {
  1164. decompile.opstack.Push(new OTOpndInt(1));
  1165. break;
  1166. }
  1167. case "ldc.i4.2":
  1168. {
  1169. decompile.opstack.Push(new OTOpndInt(2));
  1170. break;
  1171. }
  1172. case "ldc.i4.3":
  1173. {
  1174. decompile.opstack.Push(new OTOpndInt(3));
  1175. break;
  1176. }
  1177. case "ldc.i4.4":
  1178. {
  1179. decompile.opstack.Push(new OTOpndInt(4));
  1180. break;
  1181. }
  1182. case "ldc.i4.5":
  1183. {
  1184. decompile.opstack.Push(new OTOpndInt(5));
  1185. break;
  1186. }
  1187. case "ldc.i4.6":
  1188. {
  1189. decompile.opstack.Push(new OTOpndInt(6));
  1190. break;
  1191. }
  1192. case "ldc.i4.7":
  1193. {
  1194. decompile.opstack.Push(new OTOpndInt(7));
  1195. break;
  1196. }
  1197. case "ldc.i4.8":
  1198. {
  1199. decompile.opstack.Push(new OTOpndInt(8));
  1200. break;
  1201. }
  1202. case "ldc.i4.m1":
  1203. {
  1204. decompile.opstack.Push(new OTOpndInt(-1));
  1205. break;
  1206. }
  1207. case "ldelem.i4":
  1208. case "ldelem.r4":
  1209. case "ldelem.r8":
  1210. case "ldelem.ref":
  1211. {
  1212. OTOpnd index = decompile.opstack.Pop();
  1213. OTOpnd array = decompile.opstack.Pop();
  1214. decompile.opstack.Push(OTOpndArrayElem.Make(array, index, false, decompile));
  1215. break;
  1216. }
  1217. case "ldnull":
  1218. {
  1219. decompile.opstack.Push(new OTOpndNull());
  1220. break;
  1221. }
  1222. case "neg":
  1223. case "not":
  1224. {
  1225. OTOpnd value = decompile.opstack.Pop();
  1226. decompile.opstack.Push(OTOpndUnOp.Make(opCode, value));
  1227. break;
  1228. }
  1229. case "pop":
  1230. {
  1231. OTStmtVoid.AddLast(decompile, decompile.opstack.Pop());
  1232. break;
  1233. }
  1234. case "ret":
  1235. {
  1236. OTOpnd value = null;
  1237. if(decompile.method.ReturnType != typeof(void))
  1238. {
  1239. value = decompile.opstack.Pop();
  1240. }
  1241. CheckEmptyStack(decompile);
  1242. decompile.AddLastStmt(new OTStmtRet(value));
  1243. break;
  1244. }
  1245. case "stelem.i4":
  1246. case "stelem.r8":
  1247. case "stelem.ref":
  1248. {
  1249. OTOpnd value = decompile.opstack.Pop();
  1250. OTOpnd index = decompile.opstack.Pop();
  1251. OTOpnd array = decompile.opstack.Pop();
  1252. OTStmtStore.AddLast(decompile, OTOpndArrayElem.Make(array, index, false, decompile), value);
  1253. break;
  1254. }
  1255. case "throw":
  1256. {
  1257. OTOpnd value = decompile.opstack.Pop();
  1258. CheckEmptyStack(decompile);
  1259. decompile.AddLastStmt(new OTStmtThrow(value, decompile));
  1260. break;
  1261. }
  1262. case "add":
  1263. case "and":
  1264. case "ceq":
  1265. case "cgt":
  1266. case "cgt.un":
  1267. case "clt":
  1268. case "clt.un":
  1269. case "div":
  1270. case "div.un":
  1271. case "mul":
  1272. case "or":
  1273. case "rem":
  1274. case "rem.un":
  1275. case "shl":
  1276. case "shr":
  1277. case "shr.un":
  1278. case "sub":
  1279. case "xor":
  1280. {
  1281. OTOpnd rite = decompile.opstack.Pop();
  1282. OTOpnd left = decompile.opstack.Pop();
  1283. decompile.opstack.Push(OTOpndBinOp.Make(left, opCode, rite));
  1284. break;
  1285. }
  1286. default:
  1287. throw new Exception("unknown opcode " + opCode.ToString());
  1288. }
  1289. }
  1290. protected void CheckEmptyStack(OTDecompile decompile)
  1291. {
  1292. CheckEmptyStack(decompile, opCode.ToString());
  1293. }
  1294. }
  1295. private class OTCilField: OTCilNull
  1296. {
  1297. public FieldInfo field;
  1298. public OTCilField(int offset, OpCode opCode, FieldInfo field) : base(offset, opCode)
  1299. {
  1300. this.field = field;
  1301. }
  1302. public override string DumpString()
  1303. {
  1304. return opCode.ToString() + ' ' + field.Name;
  1305. }
  1306. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  1307. {
  1308. switch(opCode.ToString())
  1309. {
  1310. case "ldfld":
  1311. {
  1312. OTOpnd obj = decompile.opstack.Pop();
  1313. decompile.opstack.Push(OTOpndField.Make(obj, field));
  1314. break;
  1315. }
  1316. case "ldsfld":
  1317. {
  1318. decompile.opstack.Push(new OTOpndSField(field));
  1319. break;
  1320. }
  1321. case "stfld":
  1322. {
  1323. OTOpnd val = decompile.opstack.Pop();
  1324. OTOpnd obj = decompile.opstack.Pop();
  1325. OTStmtStore.AddLast(decompile, OTOpndField.Make(obj, field), val);
  1326. break;
  1327. }
  1328. case "stsfld":
  1329. {
  1330. OTOpnd val = decompile.opstack.Pop();
  1331. OTStmtStore.AddLast(decompile, new OTOpndSField(field), val);
  1332. break;
  1333. }
  1334. default:
  1335. throw new Exception("unknown opcode " + opCode.ToString());
  1336. }
  1337. }
  1338. }
  1339. private class OTCilLocal: OTCilNull
  1340. {
  1341. public OTLocal local;
  1342. public OTCilLocal(int offset, OpCode opCode, OTLocal local) : base(offset, opCode)
  1343. {
  1344. this.local = local;
  1345. }
  1346. public override string DumpString()
  1347. {
  1348. return opCode.ToString() + ' ' + local.name;
  1349. }
  1350. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  1351. {
  1352. switch(opCode.ToString())
  1353. {
  1354. case "ldloc":
  1355. {
  1356. decompile.opstack.Push(new OTOpndLocal(local));
  1357. break;
  1358. }
  1359. case "ldloca":
  1360. {
  1361. decompile.opstack.Push(new OTOpndLocalRef(local));
  1362. break;
  1363. }
  1364. case "stloc":
  1365. {
  1366. OTOpnd val = decompile.opstack.Pop();
  1367. OTStmtStore.AddLast(decompile, new OTOpndLocal(local), val);
  1368. break;
  1369. }
  1370. default:
  1371. throw new Exception("unknown opcode " + opCode.ToString());
  1372. }
  1373. }
  1374. }
  1375. private class OTCilType: OTCilNull
  1376. {
  1377. public Type type;
  1378. public OTCilType(int offset, OpCode opCode, Type type) : base(offset, opCode)
  1379. {
  1380. this.type = type;
  1381. }
  1382. public override string DumpString()
  1383. {
  1384. return opCode.ToString() + ' ' + AbbrType(type);
  1385. }
  1386. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  1387. {
  1388. switch(opCode.ToString())
  1389. {
  1390. case "box":
  1391. {
  1392. break;
  1393. }
  1394. case "castclass":
  1395. case "unbox.any":
  1396. {
  1397. OTOpnd value = decompile.opstack.Pop();
  1398. decompile.opstack.Push(new OTOpndCast(type, value));
  1399. break;
  1400. }
  1401. case "ldelem":
  1402. {
  1403. OTOpnd index = decompile.opstack.Pop();
  1404. OTOpnd array = decompile.opstack.Pop();
  1405. decompile.opstack.Push(OTOpndArrayElem.Make(array, index, false, decompile));
  1406. break;
  1407. }
  1408. case "ldelema":
  1409. {
  1410. OTOpnd index = decompile.opstack.Pop();
  1411. OTOpnd array = decompile.opstack.Pop();
  1412. decompile.opstack.Push(OTOpndArrayElem.Make(array, index, true, decompile));
  1413. break;
  1414. }
  1415. case "newarr":
  1416. {
  1417. OTOpnd index = decompile.opstack.Pop();
  1418. decompile.opstack.Push(new OTOpndNewarr(type, index));
  1419. break;
  1420. }
  1421. case "stelem":
  1422. {
  1423. OTOpnd value = decompile.opstack.Pop();
  1424. OTOpnd index = decompile.opstack.Pop();
  1425. OTOpnd array = decompile.opstack.Pop();
  1426. OTStmtStore.AddLast(decompile, OTOpndArrayElem.Make(array, index, false, decompile), value);
  1427. break;
  1428. }
  1429. default:
  1430. throw new Exception("unknown opcode " + opCode.ToString());
  1431. }
  1432. }
  1433. }
  1434. private class OTCilLabel: OTCilNull
  1435. {
  1436. public OTLabel label;
  1437. public OTCilLabel(int offset, OpCode opCode, OTLabel label) : base(offset, opCode)
  1438. {
  1439. this.label = label;
  1440. }
  1441. public override string DumpString()
  1442. {
  1443. return opCode.ToString() + ' ' + label.name;
  1444. }
  1445. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  1446. {
  1447. switch(opCode.ToString())
  1448. {
  1449. // We don't handle non-empty stack at branch points.
  1450. //
  1451. // So handle this case specially:
  1452. //
  1453. // dup
  1454. // ldc.i4.0
  1455. // bge.s llAbstemp << we are here
  1456. // neg
  1457. // llAbstemp:
  1458. //
  1459. // becomes:
  1460. //
  1461. // call llAbs
  1462. case "bge.s":
  1463. {
  1464. OTOpnd rite = decompile.opstack.Pop(); // alleged zero
  1465. OTOpnd left = decompile.opstack.Pop(); // alleged dup
  1466. if((label.name == _llAbstemp) && (decompile.opstack.Count > 0))
  1467. {
  1468. LinkedListNode<OTCilInstr> linkneg = link.Next;
  1469. if((left is OTOpndDup) && (rite is OTOpndInt) &&
  1470. (linkneg != null) && (linkneg.Value is OTCilNull) &&
  1471. (((OTCilNull)linkneg.Value).opCode == MyOp.Neg))
  1472. {
  1473. OTOpndInt riteint = (OTOpndInt)rite;
  1474. LinkedListNode<OTCilInstr> linklbl = linkneg.Next;
  1475. if((riteint.value == 0) && (linklbl != null) && (linklbl.Value is OTLabel) &&
  1476. (((OTLabel)linklbl.Value) == label))
  1477. {
  1478. linkneg.List.Remove(linkneg);
  1479. linklbl.List.Remove(linklbl);
  1480. MethodInfo method = typeof(ScriptBaseClass).GetMethod("llAbs");
  1481. OTOpnd[] args = new OTOpnd[] { new OTOpndNull(), decompile.opstack.Pop() };
  1482. OTOpndCall.AddLast(decompile, method, args);
  1483. break;
  1484. }
  1485. }
  1486. }
  1487. CheckEmptyStack(decompile);
  1488. OTOpnd valu = OTOpndBinOp.Make(left, opCode, rite);
  1489. OTStmt jump = OTStmtJump.Make(label);
  1490. decompile.AddLastStmt(new OTStmtCond(valu, jump));
  1491. break;
  1492. }
  1493. case "beq":
  1494. case "bge":
  1495. case "bgt":
  1496. case "ble":
  1497. case "blt":
  1498. case "bne.un":
  1499. case "beq.s":
  1500. case "bgt.s":
  1501. case "ble.s":
  1502. case "blt.s":
  1503. case "bne.un.s":
  1504. {
  1505. OTOpnd rite = decompile.opstack.Pop();
  1506. OTOpnd left = decompile.opstack.Pop();
  1507. CheckEmptyStack(decompile);
  1508. OTOpnd valu = OTOpndBinOp.Make(left, opCode, rite);
  1509. OTStmt jump = OTStmtJump.Make(label);
  1510. decompile.AddLastStmt(new OTStmtCond(valu, jump));
  1511. break;
  1512. }
  1513. case "brfalse":
  1514. case "brfalse.s":
  1515. case "brtrue":
  1516. case "brtrue.s":
  1517. {
  1518. OTOpnd value = decompile.opstack.Pop();
  1519. CheckEmptyStack(decompile);
  1520. OTOpnd valu = OTOpndUnOp.Make(opCode, value);
  1521. OTStmt jump = OTStmtJump.Make(label);
  1522. decompile.AddLastStmt(new OTStmtCond(valu, jump));
  1523. break;
  1524. }
  1525. case "br":
  1526. case "br.s":
  1527. case "leave":
  1528. {
  1529. CheckEmptyStack(decompile);
  1530. OTStmt jump = OTStmtJump.Make(label);
  1531. decompile.AddLastStmt(jump);
  1532. break;
  1533. }
  1534. default:
  1535. throw new Exception("unknown opcode " + opCode.ToString());
  1536. }
  1537. }
  1538. }
  1539. private class OTCilLabels: OTCilNull
  1540. {
  1541. public OTLabel[] labels;
  1542. public OTCilLabels(int offset, OpCode opCode, OTLabel[] labels) : base(offset, opCode)
  1543. {
  1544. this.labels = labels;
  1545. }
  1546. public override string DumpString()
  1547. {
  1548. StringBuilder sb = new StringBuilder();
  1549. sb.Append(opCode.ToString());
  1550. foreach(OTLabel label in labels)
  1551. {
  1552. sb.Append(' ');
  1553. sb.Append(label.name);
  1554. }
  1555. return sb.ToString();
  1556. }
  1557. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  1558. {
  1559. switch(opCode.ToString())
  1560. {
  1561. case "switch":
  1562. {
  1563. OTOpnd value = decompile.opstack.Pop();
  1564. CheckEmptyStack(decompile);
  1565. decompile.AddLastStmt(new OTStmtSwitch(value, labels));
  1566. break;
  1567. }
  1568. default:
  1569. throw new Exception("unknown opcode " + opCode.ToString());
  1570. }
  1571. }
  1572. }
  1573. private class OTCilMethod: OTCilNull
  1574. {
  1575. public MethodInfo method;
  1576. public OTCilMethod(int offset, OpCode opCode, MethodInfo method) : base(offset, opCode)
  1577. {
  1578. this.method = method;
  1579. }
  1580. public override string DumpString()
  1581. {
  1582. return opCode.ToString() + ' ' + method.Name;
  1583. }
  1584. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  1585. {
  1586. switch(opCode.ToString())
  1587. {
  1588. case "call":
  1589. case "callvirt":
  1590. {
  1591. int nargs = method.GetParameters().Length;
  1592. if(!method.IsStatic)
  1593. nargs++;
  1594. OTOpnd[] args = new OTOpnd[nargs];
  1595. for(int i = nargs; --i >= 0;)
  1596. {
  1597. args[i] = decompile.opstack.Pop();
  1598. }
  1599. OTOpndCall.AddLast(decompile, method, args);
  1600. break;
  1601. }
  1602. default:
  1603. throw new Exception("unknown opcode " + opCode.ToString());
  1604. }
  1605. }
  1606. }
  1607. private class OTCilCtor: OTCilNull
  1608. {
  1609. public ConstructorInfo ctor;
  1610. public OTCilCtor(int offset, OpCode opCode, ConstructorInfo ctor) : base(offset, opCode)
  1611. {
  1612. this.ctor = ctor;
  1613. }
  1614. public override string DumpString()
  1615. {
  1616. return opCode.ToString() + ' ' + AbbrType(ctor.DeclaringType);
  1617. }
  1618. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  1619. {
  1620. switch(opCode.ToString())
  1621. {
  1622. case "newobj":
  1623. {
  1624. int nargs = ctor.GetParameters().Length;
  1625. OTOpnd[] args = new OTOpnd[nargs];
  1626. for(int i = nargs; --i >= 0;)
  1627. {
  1628. args[i] = decompile.opstack.Pop();
  1629. }
  1630. decompile.opstack.Push(OTOpndNewobj.Make(ctor, args));
  1631. break;
  1632. }
  1633. default:
  1634. throw new Exception("unknown opcode " + opCode.ToString());
  1635. }
  1636. }
  1637. }
  1638. private class OTCilDouble: OTCilNull
  1639. {
  1640. public double value;
  1641. public OTCilDouble(int offset, OpCode opCode, double value) : base(offset, opCode)
  1642. {
  1643. this.value = value;
  1644. }
  1645. public override string DumpString()
  1646. {
  1647. return opCode.ToString() + ' ' + value;
  1648. }
  1649. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  1650. {
  1651. switch(opCode.ToString())
  1652. {
  1653. case "ldc.r8":
  1654. {
  1655. decompile.opstack.Push(new OTOpndDouble(value));
  1656. break;
  1657. }
  1658. default:
  1659. throw new Exception("unknown opcode " + opCode.ToString());
  1660. }
  1661. }
  1662. }
  1663. private class OTCilFloat: OTCilNull
  1664. {
  1665. public float value;
  1666. public OTCilFloat(int offset, OpCode opCode, float value) : base(offset, opCode)
  1667. {
  1668. this.value = value;
  1669. }
  1670. public override string DumpString()
  1671. {
  1672. return opCode.ToString() + ' ' + value;
  1673. }
  1674. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  1675. {
  1676. switch(opCode.ToString())
  1677. {
  1678. case "ldc.r4":
  1679. {
  1680. decompile.opstack.Push(new OTOpndFloat(value));
  1681. break;
  1682. }
  1683. default:
  1684. throw new Exception("unknown opcode " + opCode.ToString());
  1685. }
  1686. }
  1687. }
  1688. private class OTCilInteger: OTCilNull
  1689. {
  1690. public int value;
  1691. public OTCilInteger(int offset, OpCode opCode, int value) : base(offset, opCode)
  1692. {
  1693. this.value = value;
  1694. }
  1695. public override string DumpString()
  1696. {
  1697. return opCode.ToString() + ' ' + value;
  1698. }
  1699. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  1700. {
  1701. switch(opCode.ToString())
  1702. {
  1703. case "ldarg":
  1704. case "ldarg.s":
  1705. {
  1706. decompile.opstack.Push(new OTOpndArg(value, false, decompile));
  1707. break;
  1708. }
  1709. case "ldarga":
  1710. case "ldarga.s":
  1711. {
  1712. decompile.opstack.Push(new OTOpndArg(value, true, decompile));
  1713. break;
  1714. }
  1715. case "ldc.i4":
  1716. case "ldc.i4.s":
  1717. {
  1718. decompile.opstack.Push(new OTOpndInt(value));
  1719. break;
  1720. }
  1721. case "starg":
  1722. {
  1723. OTOpnd val = decompile.opstack.Pop();
  1724. OTStmtStore.AddLast(decompile, new OTOpndArg(value, false, decompile), val);
  1725. break;
  1726. }
  1727. default:
  1728. throw new Exception("unknown opcode " + opCode.ToString());
  1729. }
  1730. }
  1731. }
  1732. private class OTCilString: OTCilNull
  1733. {
  1734. public string value;
  1735. public OTCilString(int offset, OpCode opCode, string value) : base(offset, opCode)
  1736. {
  1737. this.value = value;
  1738. }
  1739. public override string DumpString()
  1740. {
  1741. StringBuilder sb = new StringBuilder();
  1742. sb.Append(opCode.ToString());
  1743. sb.Append(' ');
  1744. TokenDeclInline.PrintParamString(sb, value);
  1745. return sb.ToString();
  1746. }
  1747. public override void BuildStatements(OTDecompile decompile, LinkedListNode<OTCilInstr> link)
  1748. {
  1749. switch(opCode.ToString())
  1750. {
  1751. case "ldstr":
  1752. {
  1753. decompile.opstack.Push(new OTOpndString(value));
  1754. break;
  1755. }
  1756. default:
  1757. throw new Exception("unknown opcode " + opCode.ToString());
  1758. }
  1759. }
  1760. }
  1761. /***************************************\
  1762. * Tokens what are on operand stack. *
  1763. \***************************************/
  1764. public abstract class OTOpnd
  1765. {
  1766. /**
  1767. * See if it possibly has any side effects.
  1768. */
  1769. public abstract bool HasSideEffects
  1770. {
  1771. get;
  1772. }
  1773. /**
  1774. * Increment reference counts.
  1775. */
  1776. public virtual void CountRefs(bool writing)
  1777. {
  1778. }
  1779. /**
  1780. * If this operand is a 'by reference' operand,
  1781. * return the corresponding 'by value' operand.
  1782. */
  1783. public virtual OTOpnd GetNonByRefOpnd()
  1784. {
  1785. return this;
  1786. }
  1787. /**
  1788. * If this operand is same as oldopnd, replace it with newopnd.
  1789. *
  1790. * This default just does a shallow search which is ok if this operand does not have any sub-operands.
  1791. * But it must be overridden for a deep search if this operand has any sub-operands.
  1792. */
  1793. public virtual OTOpnd ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd, ref bool rc)
  1794. {
  1795. if(SameAs(oldopnd))
  1796. {
  1797. rc = true;
  1798. return newopnd;
  1799. }
  1800. return this;
  1801. }
  1802. /**
  1803. * See if the two operands are the same value.
  1804. * Note that calls might have side-effects so are never the same.
  1805. */
  1806. public abstract bool SameAs(OTOpnd other);
  1807. /**
  1808. * Get a printable string representation of the operand.
  1809. */
  1810. public abstract string PrintableString
  1811. {
  1812. get;
  1813. }
  1814. }
  1815. /**
  1816. * Argument variable.
  1817. */
  1818. private class OTOpndArg: OTOpnd
  1819. {
  1820. public int index;
  1821. public bool byref;
  1822. private OTDecompile decompile;
  1823. public OTOpndArg(int index, bool byref, OTDecompile decompile)
  1824. {
  1825. this.index = index;
  1826. this.byref = byref;
  1827. this.decompile = decompile;
  1828. }
  1829. public override bool HasSideEffects
  1830. {
  1831. get
  1832. {
  1833. return false;
  1834. }
  1835. }
  1836. public override OTOpnd GetNonByRefOpnd()
  1837. {
  1838. if(!byref)
  1839. return this;
  1840. return new OTOpndArg(index, false, decompile);
  1841. }
  1842. public override bool SameAs(OTOpnd other)
  1843. {
  1844. if(!(other is OTOpndArg))
  1845. return false;
  1846. return (((OTOpndArg)other).byref == byref) && (((OTOpndArg)other).index == index);
  1847. }
  1848. public override string PrintableString
  1849. {
  1850. get
  1851. {
  1852. string argname = decompile.MethArgName(index);
  1853. return byref ? ("ref " + argname) : argname;
  1854. }
  1855. }
  1856. }
  1857. /**
  1858. * Element of an array.
  1859. */
  1860. private class OTOpndArrayElem: OTOpnd
  1861. {
  1862. public bool byref;
  1863. public OTOpnd array;
  1864. public OTOpnd index;
  1865. public static OTOpnd Make(OTOpnd array, OTOpnd index, bool byref, OTDecompile decompile)
  1866. {
  1867. // arg$0.glblVars.iar<type>[<intconst>] is a reference to a global variable
  1868. // likewise so is __xmrinst.glblVars.iar<type>[<intconst>]
  1869. if((array is OTOpndField) && (index is OTOpndInt))
  1870. {
  1871. // arrayfield = (arg$0.glblVars).iar<type>
  1872. // arrayfieldobj = arg$0.glblVars
  1873. // iartypename = iar<type>
  1874. OTOpndField arrayfield = (OTOpndField)array;
  1875. OTOpnd arrayfieldobj = arrayfield.obj;
  1876. string iartypename = arrayfield.field.Name;
  1877. // See if they are what they are supposed to be.
  1878. if((arrayfieldobj is OTOpndField) && iartypename.StartsWith("iar"))
  1879. {
  1880. // arrayfieldobjfield = arg$0.glblVars
  1881. OTOpndField arrayfieldobjfield = (OTOpndField)arrayfieldobj;
  1882. // See if the parts are what they are supposed to be.
  1883. if(IsArg0OrXMRInst(arrayfieldobjfield.obj) && (arrayfieldobjfield.field.Name == "glblVars"))
  1884. {
  1885. // Everything matches up, make a global variable instead of an array reference.
  1886. return new OTOpndGlobal(iartypename, ((OTOpndInt)index).value, byref, decompile.scriptObjCode);
  1887. }
  1888. }
  1889. }
  1890. // Other array reference.
  1891. OTOpndArrayElem it = new OTOpndArrayElem();
  1892. it.array = array;
  1893. it.index = index;
  1894. it.byref = byref;
  1895. return it;
  1896. }
  1897. private OTOpndArrayElem()
  1898. {
  1899. }
  1900. public override bool HasSideEffects
  1901. {
  1902. get
  1903. {
  1904. return array.HasSideEffects || index.HasSideEffects;
  1905. }
  1906. }
  1907. public override void CountRefs(bool writing)
  1908. {
  1909. array.CountRefs(false);
  1910. index.CountRefs(false);
  1911. }
  1912. public override OTOpnd GetNonByRefOpnd()
  1913. {
  1914. if(!byref)
  1915. return this;
  1916. OTOpndArrayElem it = new OTOpndArrayElem();
  1917. it.array = array;
  1918. it.index = index;
  1919. return it;
  1920. }
  1921. public override OTOpnd ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd, ref bool rc)
  1922. {
  1923. if(SameAs(oldopnd))
  1924. {
  1925. rc = true;
  1926. return newopnd;
  1927. }
  1928. array = array.ReplaceOperand(oldopnd, newopnd, ref rc);
  1929. index = index.ReplaceOperand(oldopnd, newopnd, ref rc);
  1930. return this;
  1931. }
  1932. public override bool SameAs(OTOpnd other)
  1933. {
  1934. if(!(other is OTOpndArrayElem))
  1935. return false;
  1936. OTOpndArrayElem otherae = (OTOpndArrayElem)other;
  1937. return array.SameAs(otherae.array) && index.SameAs(otherae.index);
  1938. }
  1939. public override string PrintableString
  1940. {
  1941. get
  1942. {
  1943. return (byref ? "ref " : "") + array.PrintableString + "[" + index.PrintableString + "]";
  1944. }
  1945. }
  1946. /**
  1947. * See if the argument is a reference to arg$0 or __xmrinst
  1948. */
  1949. public static bool IsArg0OrXMRInst(OTOpnd obj)
  1950. {
  1951. if(obj is OTOpndArg)
  1952. {
  1953. OTOpndArg objarg = (OTOpndArg)obj;
  1954. return objarg.index == 0;
  1955. }
  1956. if(obj is OTOpndLocal)
  1957. {
  1958. OTOpndLocal objlcl = (OTOpndLocal)obj;
  1959. return objlcl.local.name.StartsWith(_xmrinstlocal);
  1960. }
  1961. return false;
  1962. }
  1963. }
  1964. /**
  1965. * Binary operator.
  1966. */
  1967. private class OTOpndBinOp: OTOpnd
  1968. {
  1969. public OTOpnd left;
  1970. public MyOp opCode;
  1971. public OTOpnd rite;
  1972. private static Dictionary<string, string> xor1ops = InitXor1Ops();
  1973. private static Dictionary<string, string> InitXor1Ops()
  1974. {
  1975. Dictionary<string, string> d = new Dictionary<string, string>();
  1976. d["ceq"] = "cne";
  1977. d["cge"] = "clt";
  1978. d["cgt"] = "cle";
  1979. d["cle"] = "cgt";
  1980. d["clt"] = "cge";
  1981. d["cne"] = "ceq";
  1982. return d;
  1983. }
  1984. public static OTOpnd Make(OTOpnd left, MyOp opCode, OTOpnd rite)
  1985. {
  1986. // ((x clt y) xor 1) => (x cge y) etc
  1987. string xor1op;
  1988. if((left is OTOpndBinOp) && xor1ops.TryGetValue(((OTOpndBinOp)left).opCode.name, out xor1op) &&
  1989. (opCode == MyOp.Xor) &&
  1990. (rite is OTOpndInt) && (((OTOpndInt)rite).value == 1))
  1991. {
  1992. opCode = MyOp.GetByName(xor1op);
  1993. }
  1994. // handle strcmp() cases (see OTOpndStrCmp)
  1995. if(left is OTOpndStrCmp)
  1996. {
  1997. OTOpnd strcmp = ((OTOpndStrCmp)left).MakeBinOp(opCode, rite);
  1998. if(strcmp != null)
  1999. return strcmp;
  2000. }
  2001. // nothing special, make as is
  2002. OTOpndBinOp it = new OTOpndBinOp();
  2003. it.left = left;
  2004. it.opCode = opCode;
  2005. it.rite = rite;
  2006. return it;
  2007. }
  2008. private OTOpndBinOp()
  2009. {
  2010. }
  2011. public override bool HasSideEffects
  2012. {
  2013. get
  2014. {
  2015. return left.HasSideEffects || rite.HasSideEffects;
  2016. }
  2017. }
  2018. public override void CountRefs(bool writing)
  2019. {
  2020. left.CountRefs(false);
  2021. rite.CountRefs(false);
  2022. }
  2023. public override OTOpnd ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd, ref bool rc)
  2024. {
  2025. if(SameAs(oldopnd))
  2026. {
  2027. rc = true;
  2028. return newopnd;
  2029. }
  2030. left = left.ReplaceOperand(oldopnd, newopnd, ref rc);
  2031. rite = rite.ReplaceOperand(oldopnd, newopnd, ref rc);
  2032. return this;
  2033. }
  2034. public override bool SameAs(OTOpnd other)
  2035. {
  2036. if(!(other is OTOpndBinOp))
  2037. return false;
  2038. OTOpndBinOp otherbo = (OTOpndBinOp)other;
  2039. return left.SameAs(otherbo.left) && (opCode.ToString() == otherbo.opCode.ToString()) && rite.SameAs(otherbo.rite);
  2040. }
  2041. public override string PrintableString
  2042. {
  2043. get
  2044. {
  2045. StringBuilder sb = new StringBuilder();
  2046. bool leftneedsparen = ItNeedsParentheses(left, true);
  2047. if(leftneedsparen)
  2048. sb.Append('(');
  2049. sb.Append(left.PrintableString);
  2050. if(leftneedsparen)
  2051. sb.Append(')');
  2052. sb.Append(' ');
  2053. sb.Append(opCode.source);
  2054. sb.Append(' ');
  2055. bool riteneedsparen = ItNeedsParentheses(rite, false);
  2056. if(riteneedsparen)
  2057. sb.Append('(');
  2058. sb.Append(rite.PrintableString);
  2059. if(riteneedsparen)
  2060. sb.Append(')');
  2061. return sb.ToString();
  2062. }
  2063. }
  2064. /**
  2065. * See if source code representation requires parentheses around the given operand.
  2066. * @param it = the other operand to decide about
  2067. * @param itleft = true: 'it' is on the left of this operand (A $ B) # C
  2068. * false: 'it' is on the right of this operand A $ (B # C)
  2069. */
  2070. private bool ItNeedsParentheses(OTOpnd it, bool itleft)
  2071. {
  2072. if(!(it is OTOpndBinOp))
  2073. return false;
  2074. string itop = ((OTOpndBinOp)it).opCode.source;
  2075. string myop = opCode.source;
  2076. // find them in table. higher number is for *, lower is for +.
  2077. int itpi, mypi;
  2078. if(!precedence.TryGetValue(itop, out itpi))
  2079. return true;
  2080. if(!precedence.TryGetValue(myop, out mypi))
  2081. return true;
  2082. int itpiabs = Math.Abs(itpi);
  2083. int mypiabs = Math.Abs(mypi);
  2084. // if its precedence is lower (eg +) than my precedence (eg *), it needs parentheses
  2085. if(itpiabs < mypiabs)
  2086. return true;
  2087. // if its precedence is higher (eg *) than my precedence (eg +), it doesn't needs parentheses
  2088. if(itpiabs > mypiabs)
  2089. return false;
  2090. // if (A $ B) # C, we can safely go without the parentheses
  2091. if(itleft)
  2092. return false;
  2093. // my it
  2094. // A $ (B # C) only works without parentheses for commutative $
  2095. // A - (B + C) and A - (B - C) require parentheses
  2096. // A + (B - C) does not
  2097. return mypi < 0; // neg: things like -, /, etc require parentheses
  2098. // pos: things like +, *, etc do not need parens
  2099. }
  2100. // see MMRScriptReduce.PrecedenceInit()
  2101. private static Dictionary<string, int> precedence = InitPrecedence();
  2102. private static Dictionary<string, int> InitPrecedence()
  2103. {
  2104. Dictionary<string, int> d = new Dictionary<string, int>();
  2105. d["|"] = 140;
  2106. d["^"] = 160;
  2107. d["&"] = 180;
  2108. d["<<"] = -260;
  2109. d[">>"] = -260;
  2110. d["+"] = 280;
  2111. d["-"] = -280;
  2112. d["*"] = 320;
  2113. d["/"] = -320;
  2114. d["%"] = -320;
  2115. return d;
  2116. }
  2117. }
  2118. /**
  2119. * Call with or without return value.
  2120. */
  2121. private class OTOpndCall: OTOpnd
  2122. {
  2123. private static Dictionary<string, MethodInfo> mathmeths = InitMathMeths();
  2124. private static Dictionary<string, MethodInfo> InitMathMeths()
  2125. {
  2126. Dictionary<string, MethodInfo> d = new Dictionary<string, MethodInfo>();
  2127. d["Acos"] = typeof(ScriptBaseClass).GetMethod("llAcos");
  2128. d["Asin"] = typeof(ScriptBaseClass).GetMethod("llAsin");
  2129. d["Atan"] = typeof(ScriptBaseClass).GetMethod("llAtan");
  2130. d["Cos"] = typeof(ScriptBaseClass).GetMethod("llCos");
  2131. d["Abs"] = typeof(ScriptBaseClass).GetMethod("llFabs");
  2132. d["Log"] = typeof(ScriptBaseClass).GetMethod("llLog");
  2133. d["Log10"] = typeof(ScriptBaseClass).GetMethod("llLog10");
  2134. d["Round"] = typeof(ScriptBaseClass).GetMethod("llRound");
  2135. d["Sin"] = typeof(ScriptBaseClass).GetMethod("llSin");
  2136. d["Sqrt"] = typeof(ScriptBaseClass).GetMethod("llSqrt");
  2137. d["Tan"] = typeof(ScriptBaseClass).GetMethod("llTan");
  2138. return d;
  2139. }
  2140. public MethodInfo method;
  2141. public OTOpnd[] args;
  2142. // pushes on stack for return-value functions
  2143. // pushes to end of instruction stream for return-void functions
  2144. public static void AddLast(OTDecompile decompile, MethodInfo method, OTOpnd[] args)
  2145. {
  2146. int nargs = args.Length;
  2147. // heap tracker push is just the single arg value as far as we're concerned
  2148. if((nargs == 1) && (method.Name == _heapTrackerPush) && method.DeclaringType.Name.StartsWith("HeapTracker"))
  2149. {
  2150. decompile.opstack.Push(args[0]);
  2151. return;
  2152. }
  2153. // heap tracker pop is just a store as far as we're concerned
  2154. if((nargs == 2) && (method.Name == _heapTrackerPop) && method.DeclaringType.Name.StartsWith("HeapTracker"))
  2155. {
  2156. OTStmtStore.AddLast(decompile, args[0], args[1]);
  2157. return;
  2158. }
  2159. // string.Compare() is its own thing cuz it has to decompile many ways
  2160. if((nargs == 2) && (method.DeclaringType == typeof(string)) && (method.Name == "Compare"))
  2161. {
  2162. decompile.opstack.Push(new OTOpndStrCmp(args[0], args[1]));
  2163. return;
  2164. }
  2165. // ObjectToString, etc, should appear as casts
  2166. if((nargs == 1) && (method.DeclaringType == typeof(TypeCast)) && method.Name.EndsWith("ToBool"))
  2167. {
  2168. MethodInfo meth = typeof(XMRInstAbstract).GetMethod("xmr" + method.Name);
  2169. AddLast(decompile, meth, new OTOpnd[] { new OTOpndNull(), args[0] });
  2170. return;
  2171. }
  2172. if((nargs == 1) && (method.DeclaringType == typeof(TypeCast)) && method.Name.EndsWith("ToFloat"))
  2173. {
  2174. decompile.opstack.Push(new OTOpndCast(typeof(double), args[0]));
  2175. return;
  2176. }
  2177. if((nargs == 1) && (method.DeclaringType == typeof(TypeCast)) && method.Name.EndsWith("ToInteger"))
  2178. {
  2179. decompile.opstack.Push(new OTOpndCast(typeof(int), args[0]));
  2180. return;
  2181. }
  2182. if((nargs == 1) && (method.DeclaringType == typeof(TypeCast)) && method.Name.EndsWith("ToList"))
  2183. {
  2184. decompile.opstack.Push(new OTOpndCast(typeof(LSL_List), args[0]));
  2185. return;
  2186. }
  2187. if((nargs == 1) && (method.DeclaringType == typeof(TypeCast)) && method.Name.EndsWith("ToRotation"))
  2188. {
  2189. decompile.opstack.Push(new OTOpndCast(typeof(LSL_Rotation), args[0]));
  2190. return;
  2191. }
  2192. if((nargs == 1) && (method.DeclaringType == typeof(TypeCast)) && method.Name.EndsWith("ToString"))
  2193. {
  2194. decompile.opstack.Push(new OTOpndCast(typeof(string), args[0]));
  2195. return;
  2196. }
  2197. if((nargs == 1) && (method.DeclaringType == typeof(TypeCast)) && method.Name.EndsWith("ToVector"))
  2198. {
  2199. decompile.opstack.Push(new OTOpndCast(typeof(LSL_Vector), args[0]));
  2200. return;
  2201. }
  2202. if((method.DeclaringType == typeof(XMRInstAbstract)) && (method.Name == "xmrHeapLeft"))
  2203. {
  2204. AddLast(decompile, typeof(ScriptBaseClass).GetMethod("llGetFreeMemory"), new OTOpnd[] { new OTOpndNull() });
  2205. return;
  2206. }
  2207. // pop to entry in the list/object/string array
  2208. if(PopToGlobalArray(decompile, method, args))
  2209. return;
  2210. // strip off event handler argument unwrapper calls
  2211. if((nargs == 1) && (method.DeclaringType == typeof(TypeCast)) && method.Name.StartsWith("EHArgUnwrap"))
  2212. {
  2213. decompile.opstack.Push(args[0]);
  2214. return;
  2215. }
  2216. // translate Math method to ll method
  2217. MethodInfo mathmeth;
  2218. if((method.DeclaringType == typeof(Math)) && mathmeths.TryGetValue(method.Name, out mathmeth))
  2219. {
  2220. AddLast(decompile, mathmeth, new OTOpnd[] { new OTOpndNull(), args[0] });
  2221. return;
  2222. }
  2223. if((method.DeclaringType == typeof(Math)) && (method.Name == "Atan2"))
  2224. {
  2225. AddLast(decompile, typeof(ScriptBaseClass).GetMethod("llAtan2"), new OTOpnd[] { new OTOpndNull(), args[0], args[1] });
  2226. return;
  2227. }
  2228. if((method.DeclaringType == typeof(Math)) && (method.Name == "Pow"))
  2229. {
  2230. AddLast(decompile, typeof(ScriptBaseClass).GetMethod("llPow"), new OTOpnd[] { new OTOpndNull(), args[0], args[1] });
  2231. return;
  2232. }
  2233. // string concat should be a bunch of adds
  2234. if((method.Name == "Concat") && (method.DeclaringType == typeof(string)))
  2235. {
  2236. int k = args.Length;
  2237. while(k > 1)
  2238. {
  2239. int j = 0;
  2240. int i;
  2241. for(i = 0; i + 2 <= k; i += 2)
  2242. {
  2243. args[j++] = OTOpndBinOp.Make(args[i + 0], MyOp.Add, args[i + 1]);
  2244. }
  2245. while(i < k)
  2246. args[j++] = args[i++];
  2247. k = j;
  2248. }
  2249. if(k > 0)
  2250. decompile.opstack.Push(args[0]);
  2251. return;
  2252. }
  2253. // bunch of calls for rotation and vector arithmetic
  2254. if((method.DeclaringType == typeof(BinOpStr)) && BinOpStrCall(decompile, method, args))
  2255. return;
  2256. if((method.DeclaringType == typeof(ScriptCodeGen)) && (method.Name == "LSLRotationNegate"))
  2257. {
  2258. decompile.opstack.Push(OTOpndUnOp.Make(MyOp.Neg, args[0]));
  2259. return;
  2260. }
  2261. if((method.DeclaringType == typeof(ScriptCodeGen)) && (method.Name == "LSLVectorNegate"))
  2262. {
  2263. decompile.opstack.Push(OTOpndUnOp.Make(MyOp.Neg, args[0]));
  2264. return;
  2265. }
  2266. // otherwise process it as a call
  2267. OTOpndCall call = new OTOpndCall();
  2268. call.method = method;
  2269. call.args = args;
  2270. if(method.ReturnType == typeof(void))
  2271. {
  2272. OTStmtVoid.AddLast(decompile, call);
  2273. }
  2274. else
  2275. {
  2276. decompile.opstack.Push(call);
  2277. }
  2278. }
  2279. public override bool HasSideEffects
  2280. {
  2281. get
  2282. {
  2283. return true;
  2284. }
  2285. }
  2286. /**
  2287. * Handle a call to XMRInstArrays.Pop<List,Object,String>
  2288. * by converting it to a store directly into the array.
  2289. */
  2290. private static bool PopToGlobalArray(OTDecompile decompile, MethodInfo method, OTOpnd[] args)
  2291. {
  2292. if(method.DeclaringType != typeof(XMRInstArrays))
  2293. return false;
  2294. if(args.Length != 3)
  2295. return false;
  2296. string array = null;
  2297. if(method.Name == "PopList")
  2298. array = "iarLists";
  2299. if(method.Name == "PopObject")
  2300. array = "iarObjects";
  2301. if(method.Name == "PopString")
  2302. array = "iarStrings";
  2303. if(array == null)
  2304. return false;
  2305. // make token that points to the iar<whatever> array
  2306. FieldInfo field = typeof(XMRInstArrays).GetField(array);
  2307. OTOpnd arrayfield = OTOpndField.Make(args[0], field);
  2308. // make token that points to the element to be popped to
  2309. OTOpnd element = OTOpndArrayElem.Make(arrayfield, args[1], false, decompile);
  2310. // make a statement to store value in that element
  2311. OTStmtStore.AddLast(decompile, element, args[2]);
  2312. return true;
  2313. }
  2314. /**
  2315. * BinOpStr has a bunch of calls to do funky arithmetic.
  2316. * Instead of generating a call, put back the original source.
  2317. */
  2318. private static bool BinOpStrCall(OTDecompile decompile, MethodInfo method, OTOpnd[] args)
  2319. {
  2320. switch(method.Name)
  2321. {
  2322. case "MethFloatAddList":
  2323. case "MethIntAddList":
  2324. case "MethKeyAddList":
  2325. case "MethListAddFloat":
  2326. case "MethListAddInt":
  2327. case "MethListAddKey":
  2328. case "MethListAddList":
  2329. case "MethListAddObj":
  2330. case "MethListAddRot":
  2331. case "MethListAddStr":
  2332. case "MethListAddVec":
  2333. case "MethObjAddList":
  2334. case "MethRotAddList":
  2335. case "MethRotAddRot":
  2336. case "MethStrAddList":
  2337. case "MethVecAddList":
  2338. case "MethVecAddVec":
  2339. {
  2340. decompile.opstack.Push(OTOpndBinOp.Make(args[0], MyOp.Add, args[1]));
  2341. return true;
  2342. }
  2343. case "MethListEqList":
  2344. case "MethRotEqRot":
  2345. case "MethVecEqVec":
  2346. {
  2347. decompile.opstack.Push(OTOpndBinOp.Make(args[0], MyOp.Ceq, args[1]));
  2348. return true;
  2349. }
  2350. case "MethListNeList":
  2351. case "MethRotNeRot":
  2352. case "MethVecNeVec":
  2353. {
  2354. decompile.opstack.Push(OTOpndBinOp.Make(args[0], MyOp.Cne, args[1]));
  2355. return true;
  2356. }
  2357. case "MethRotSubRot":
  2358. case "MethVecSubVec":
  2359. {
  2360. decompile.opstack.Push(OTOpndBinOp.Make(args[0], MyOp.Sub, args[1]));
  2361. return true;
  2362. }
  2363. case "MethFloatMulVec":
  2364. case "MethIntMulVec":
  2365. case "MethRotMulRot":
  2366. case "MethVecMulFloat":
  2367. case "MethVecMulInt":
  2368. case "MethVecMulRot":
  2369. case "MethVecMulVec":
  2370. {
  2371. decompile.opstack.Push(OTOpndBinOp.Make(args[0], MyOp.Mul, args[1]));
  2372. return true;
  2373. }
  2374. case "MethRotDivRot":
  2375. case "MethVecDivFloat":
  2376. case "MethVecDivInt":
  2377. case "MethVecDivRot":
  2378. {
  2379. decompile.opstack.Push(OTOpndBinOp.Make(args[0], MyOp.Div, args[1]));
  2380. return true;
  2381. }
  2382. default:
  2383. return false;
  2384. }
  2385. }
  2386. private OTOpndCall()
  2387. {
  2388. }
  2389. public override void CountRefs(bool writing)
  2390. {
  2391. foreach(OTOpnd arg in args)
  2392. {
  2393. arg.CountRefs(false);
  2394. }
  2395. }
  2396. public override OTOpnd ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd, ref bool rc)
  2397. {
  2398. for(int i = 0; i < args.Length; i++)
  2399. {
  2400. args[i] = args[i].ReplaceOperand(oldopnd, newopnd, ref rc);
  2401. }
  2402. return this;
  2403. }
  2404. public override bool SameAs(OTOpnd other)
  2405. {
  2406. return false;
  2407. }
  2408. public override string PrintableString
  2409. {
  2410. get
  2411. {
  2412. StringBuilder sb = new StringBuilder();
  2413. // GetByKey(a,i) => a[i]
  2414. if((method.DeclaringType == typeof(XMR_Array)) && (method.Name == "GetByKey") && (args.Length == 2))
  2415. {
  2416. sb.Append(args[0].PrintableString);
  2417. sb.Append('[');
  2418. sb.Append(args[1].PrintableString);
  2419. sb.Append(']');
  2420. return sb.ToString();
  2421. }
  2422. // SetByKey(a,i,v) => a[i] = v
  2423. if((method.DeclaringType == typeof(XMR_Array)) && (method.Name == "SetByKey") && (args.Length == 3))
  2424. {
  2425. sb.Append(args[0].PrintableString);
  2426. sb.Append('[');
  2427. sb.Append(args[1].PrintableString);
  2428. sb.Append("] = ");
  2429. sb.Append(args[2].PrintableString);
  2430. return sb.ToString();
  2431. }
  2432. // CompValuListEl.GetElementFromList accesses list elements like an array.
  2433. if((method.DeclaringType == typeof(CompValuListEl)) && (method.Name == "GetElementFromList"))
  2434. {
  2435. sb.Append(args[0].PrintableString);
  2436. sb.Append('[');
  2437. sb.Append(args[1].PrintableString);
  2438. sb.Append(']');
  2439. return sb.ToString();
  2440. }
  2441. // methods that are part of ScriptBaseClass are LSL functions such as llSay()
  2442. // so we want to skip outputting "arg$0," as it is the hidden "this" argument.
  2443. // and there are also XMRInstAbstract functions such as xmrEventDequeue().
  2444. int starti = 0;
  2445. if((method.DeclaringType == typeof(ScriptBaseClass)) && !method.IsStatic)
  2446. starti = 1;
  2447. if((method.DeclaringType == typeof(XMRInstAbstract)) && !method.IsStatic)
  2448. starti = 1;
  2449. // likewise, method that have null as the declaring type are script-defined
  2450. // dynamic methods which have a hidden "this" argument passed as "arg$0".
  2451. if(method.DeclaringType == null)
  2452. starti = 1;
  2453. // all others we want to show the type name (such as Math.Abs, String.Compare, etc)
  2454. if(starti == 0)
  2455. {
  2456. sb.Append(AbbrType(method.DeclaringType));
  2457. sb.Append('.');
  2458. }
  2459. // script-defined functions have the param types as part of their name
  2460. // so strip them off here so they don't clutter things up
  2461. int i = method.Name.IndexOf('(');
  2462. if(i < 0)
  2463. sb.Append(method.Name);
  2464. else
  2465. sb.Append(method.Name.Substring(0, i));
  2466. // now add the call arguments
  2467. sb.Append(" (");
  2468. bool first = true;
  2469. foreach(OTOpnd arg in args)
  2470. {
  2471. if(--starti < 0)
  2472. {
  2473. if(!first)
  2474. sb.Append(", ");
  2475. sb.Append(arg.PrintableString);
  2476. first = false;
  2477. }
  2478. }
  2479. sb.Append(')');
  2480. return sb.ToString();
  2481. }
  2482. }
  2483. }
  2484. /**
  2485. * Cast value to the given type.
  2486. */
  2487. private class OTOpndCast: OTOpnd
  2488. {
  2489. public Type type;
  2490. public OTOpnd value;
  2491. public OTOpndCast(Type type, OTOpnd value)
  2492. {
  2493. this.type = type;
  2494. this.value = value;
  2495. }
  2496. public override bool HasSideEffects
  2497. {
  2498. get
  2499. {
  2500. return value.HasSideEffects;
  2501. }
  2502. }
  2503. public override void CountRefs(bool writing)
  2504. {
  2505. value.CountRefs(false);
  2506. }
  2507. public override OTOpnd ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd, ref bool rc)
  2508. {
  2509. if(SameAs(oldopnd))
  2510. {
  2511. rc = true;
  2512. return newopnd;
  2513. }
  2514. value = value.ReplaceOperand(oldopnd, newopnd, ref rc);
  2515. return this;
  2516. }
  2517. public override bool SameAs(OTOpnd other)
  2518. {
  2519. if(!(other is OTOpndCast))
  2520. return false;
  2521. OTOpndCast othercast = (OTOpndCast)other;
  2522. return (type == othercast.type) && value.SameAs(othercast.value);
  2523. }
  2524. public override string PrintableString
  2525. {
  2526. get
  2527. {
  2528. StringBuilder sb = new StringBuilder();
  2529. sb.Append('(');
  2530. sb.Append(AbbrType(type));
  2531. sb.Append(") ");
  2532. if(value is OTOpndBinOp)
  2533. sb.Append('(');
  2534. sb.Append(value.PrintableString);
  2535. if(value is OTOpndBinOp)
  2536. sb.Append(')');
  2537. return sb.ToString();
  2538. }
  2539. }
  2540. }
  2541. /**
  2542. * Duplicate stack value without re-performing computation.
  2543. * Semantics just like local var except it doesn't have a declaration.
  2544. */
  2545. private class OTOpndDup: OTOpnd
  2546. {
  2547. public int index;
  2548. public int ndupreads;
  2549. public OTOpndDup(int index)
  2550. {
  2551. this.index = index;
  2552. }
  2553. public override bool HasSideEffects
  2554. {
  2555. get
  2556. {
  2557. return false;
  2558. }
  2559. }
  2560. public override void CountRefs(bool writing)
  2561. {
  2562. if(!writing)
  2563. ndupreads++;
  2564. }
  2565. public override bool SameAs(OTOpnd other)
  2566. {
  2567. if(!(other is OTOpndDup))
  2568. return false;
  2569. return ((OTOpndDup)other).index == index;
  2570. }
  2571. public override string PrintableString
  2572. {
  2573. get
  2574. {
  2575. return "dup$" + index;
  2576. }
  2577. }
  2578. }
  2579. /**
  2580. * Field of an object.
  2581. */
  2582. private class OTOpndField: OTOpnd
  2583. {
  2584. public OTOpnd obj;
  2585. public FieldInfo field;
  2586. public static OTOpnd Make(OTOpnd obj, FieldInfo field)
  2587. {
  2588. // LSL_Float.value => the object itself
  2589. if((field.DeclaringType == typeof(LSL_Float)) && (field.Name == "value"))
  2590. {
  2591. return obj;
  2592. }
  2593. // LSL_Integer.value => the object itself
  2594. if((field.DeclaringType == typeof(LSL_Integer)) && (field.Name == "value"))
  2595. {
  2596. return obj;
  2597. }
  2598. // LSL_String.m_string => the object itself
  2599. if((field.DeclaringType == typeof(LSL_String)) && (field.Name == "m_string"))
  2600. {
  2601. return obj;
  2602. }
  2603. // some other field, output code to access it
  2604. // sometimes the object comes as by reference (value types), so we might need to deref it first
  2605. OTOpndField it = new OTOpndField();
  2606. it.obj = obj.GetNonByRefOpnd();
  2607. it.field = field;
  2608. return it;
  2609. }
  2610. private OTOpndField()
  2611. {
  2612. }
  2613. public override bool HasSideEffects
  2614. {
  2615. get
  2616. {
  2617. return obj.HasSideEffects;
  2618. }
  2619. }
  2620. public override void CountRefs(bool writing)
  2621. {
  2622. // the field may be getting written to, but the object is being read
  2623. obj.CountRefs(false);
  2624. }
  2625. public override OTOpnd ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd, ref bool rc)
  2626. {
  2627. if(SameAs(oldopnd))
  2628. {
  2629. rc = true;
  2630. return newopnd;
  2631. }
  2632. obj = obj.ReplaceOperand(oldopnd, newopnd, ref rc);
  2633. return this;
  2634. }
  2635. public override bool SameAs(OTOpnd other)
  2636. {
  2637. if(!(other is OTOpndField))
  2638. return false;
  2639. OTOpndField otherfield = (OTOpndField)other;
  2640. return (field.Name == otherfield.field.Name) && obj.SameAs(otherfield.obj);
  2641. }
  2642. public override string PrintableString
  2643. {
  2644. get
  2645. {
  2646. StringBuilder sb = new StringBuilder();
  2647. if(obj is OTOpndBinOp)
  2648. sb.Append('(');
  2649. sb.Append(obj.PrintableString);
  2650. if(obj is OTOpndBinOp)
  2651. sb.Append(')');
  2652. sb.Append('.');
  2653. sb.Append(field.Name);
  2654. return sb.ToString();
  2655. }
  2656. }
  2657. }
  2658. /**
  2659. * Script-level global variable.
  2660. */
  2661. private class OTOpndGlobal: OTOpnd
  2662. {
  2663. public string iartypename;
  2664. public int iararrayidx;
  2665. public bool byref;
  2666. public ScriptObjCode scriptObjCode;
  2667. public OTOpndGlobal(string iartypename, int iararrayidx, bool byref, ScriptObjCode scriptObjCode)
  2668. {
  2669. this.iartypename = iartypename;
  2670. this.iararrayidx = iararrayidx;
  2671. this.byref = byref;
  2672. this.scriptObjCode = scriptObjCode;
  2673. }
  2674. public override bool HasSideEffects
  2675. {
  2676. get
  2677. {
  2678. return false;
  2679. }
  2680. }
  2681. public override OTOpnd GetNonByRefOpnd()
  2682. {
  2683. if(!byref)
  2684. return this;
  2685. return new OTOpndGlobal(iartypename, iararrayidx, false, scriptObjCode);
  2686. }
  2687. public override bool SameAs(OTOpnd other)
  2688. {
  2689. if(!(other is OTOpndGlobal))
  2690. return false;
  2691. OTOpndGlobal otherglobal = (OTOpndGlobal)other;
  2692. return (iartypename == otherglobal.iartypename) && (iararrayidx == otherglobal.iararrayidx);
  2693. }
  2694. public override string PrintableString
  2695. {
  2696. get
  2697. {
  2698. return (byref ? "ref " : "") + scriptObjCode.globalVarNames[iartypename][iararrayidx];
  2699. }
  2700. }
  2701. }
  2702. /**
  2703. * List initialization.
  2704. */
  2705. private class OTOpndListIni: OTOpnd
  2706. {
  2707. public OTOpnd[] values;
  2708. /**
  2709. * Try to detect list initialization building idiom:
  2710. * dup$<n> = newarr object[<m>] << link points here
  2711. * dup$<n>[0] = bla
  2712. * dup$<n>[1] = bla
  2713. * ...
  2714. * ... newobj list (dup$<n>) ...
  2715. */
  2716. public static bool Detect(LinkedListNode<OTStmt> link)
  2717. {
  2718. if(link == null)
  2719. return false;
  2720. /*
  2721. * Check for 'dup$<n> = newarr object[<m>]' and get listsize from <m>.
  2722. */
  2723. OTStmtStore store = (OTStmtStore)link.Value;
  2724. if(!(store.varwr is OTOpndDup))
  2725. return false;
  2726. if(!(store.value is OTOpndNewarr))
  2727. return false;
  2728. OTOpndDup storevar = (OTOpndDup)store.varwr;
  2729. OTOpndNewarr storeval = (OTOpndNewarr)store.value;
  2730. if(storeval.type != typeof(object))
  2731. return false;
  2732. if(!(storeval.index is OTOpndInt))
  2733. return false;
  2734. int listsize = ((OTOpndInt)storeval.index).value;
  2735. // Good chance of having list initializer, malloc an object to hold it.
  2736. OTOpndListIni it = new OTOpndListIni();
  2737. it.values = new OTOpnd[listsize];
  2738. // There should be exactly listsize statements following that of the form:
  2739. // dup$<n>[<i>] = bla
  2740. // If so, save the bla values in the values[] array.
  2741. LinkedListNode<OTStmt> vallink = link;
  2742. for(int i = 0; i < listsize; i++)
  2743. {
  2744. vallink = vallink.Next;
  2745. if(vallink == null)
  2746. return false;
  2747. if(!(vallink.Value is OTStmtStore))
  2748. return false;
  2749. OTStmtStore valstore = (OTStmtStore)vallink.Value;
  2750. if(!(valstore.varwr is OTOpndArrayElem))
  2751. return false;
  2752. OTOpndArrayElem varelem = (OTOpndArrayElem)valstore.varwr;
  2753. if(varelem.array != storevar)
  2754. return false;
  2755. if(!(varelem.index is OTOpndInt))
  2756. return false;
  2757. if(((OTOpndInt)varelem.index).value != i)
  2758. return false;
  2759. it.values[i] = valstore.value;
  2760. }
  2761. // The next statement should have a 'newobj list (dup$<n>)' in it somewhere
  2762. // that we want to replace with 'it'.
  2763. ConstructorInfo protoctor = typeof(LSL_List).GetConstructor(new Type[] { typeof(object[]) });
  2764. OTOpnd[] protoargs = new OTOpnd[] { storevar };
  2765. OTOpnd proto = OTOpndNewobj.Make(protoctor, protoargs);
  2766. vallink = vallink.Next;
  2767. bool rc = vallink.Value.ReplaceOperand(proto, it);
  2768. // If successful, delete 'dup$n =' and all 'dup$n[i] =' statements.
  2769. if(rc)
  2770. {
  2771. do
  2772. {
  2773. LinkedListNode<OTStmt> nextlink = link.Next;
  2774. link.List.Remove(link);
  2775. link = nextlink;
  2776. } while(link != vallink);
  2777. }
  2778. return rc;
  2779. }
  2780. public override bool HasSideEffects
  2781. {
  2782. get
  2783. {
  2784. foreach(OTOpnd value in values)
  2785. {
  2786. if(value.HasSideEffects)
  2787. return true;
  2788. }
  2789. return false;
  2790. }
  2791. }
  2792. public override void CountRefs(bool writing)
  2793. {
  2794. foreach(OTOpnd value in values)
  2795. {
  2796. value.CountRefs(false);
  2797. }
  2798. }
  2799. public override OTOpnd ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd, ref bool rc)
  2800. {
  2801. if(SameAs(oldopnd))
  2802. {
  2803. rc = true;
  2804. return newopnd;
  2805. }
  2806. for(int i = 0; i < values.Length; i++)
  2807. {
  2808. values[i] = values[i].ReplaceOperand(oldopnd, newopnd, ref rc);
  2809. }
  2810. return this;
  2811. }
  2812. public override bool SameAs(OTOpnd other)
  2813. {
  2814. if(!(other is OTOpndListIni))
  2815. return false;
  2816. OTOpndListIni otherli = (OTOpndListIni)other;
  2817. if(otherli.values.Length != values.Length)
  2818. return false;
  2819. for(int i = 0; i < values.Length; i++)
  2820. {
  2821. if(!values[i].SameAs(otherli.values[i]))
  2822. return false;
  2823. }
  2824. return true;
  2825. }
  2826. public override string PrintableString
  2827. {
  2828. get
  2829. {
  2830. StringBuilder sb = new StringBuilder();
  2831. sb.Append('[');
  2832. for(int i = 0; i < values.Length; i++)
  2833. {
  2834. if(i > 0)
  2835. sb.Append(',');
  2836. sb.Append(' ');
  2837. sb.Append(values[i].PrintableString);
  2838. }
  2839. sb.Append(" ]");
  2840. return sb.ToString();
  2841. }
  2842. }
  2843. }
  2844. /**
  2845. * Local variable.
  2846. */
  2847. private class OTOpndLocal: OTOpnd
  2848. {
  2849. public OTLocal local;
  2850. public OTOpndLocal(OTLocal local)
  2851. {
  2852. this.local = local;
  2853. }
  2854. public override bool HasSideEffects
  2855. {
  2856. get
  2857. {
  2858. return false;
  2859. }
  2860. }
  2861. public override void CountRefs(bool writing)
  2862. {
  2863. if(writing)
  2864. local.nlclwrites++;
  2865. else
  2866. local.nlclreads++;
  2867. }
  2868. public override bool SameAs(OTOpnd other)
  2869. {
  2870. if(!(other is OTOpndLocal))
  2871. return false;
  2872. OTOpndLocal otherlocal = (OTOpndLocal)other;
  2873. return local == otherlocal.local;
  2874. }
  2875. public override string PrintableString
  2876. {
  2877. get
  2878. {
  2879. return local.name;
  2880. }
  2881. }
  2882. }
  2883. private class OTOpndLocalRef: OTOpnd
  2884. {
  2885. public OTLocal local;
  2886. public OTOpndLocalRef(OTLocal local)
  2887. {
  2888. this.local = local;
  2889. }
  2890. public override bool HasSideEffects
  2891. {
  2892. get
  2893. {
  2894. return true;
  2895. }
  2896. }
  2897. public override void CountRefs(bool writing)
  2898. {
  2899. local.nlclreads++;
  2900. local.nlclwrites++;
  2901. }
  2902. public override OTOpnd GetNonByRefOpnd()
  2903. {
  2904. return new OTOpndLocal(local);
  2905. }
  2906. public override bool SameAs(OTOpnd other)
  2907. {
  2908. if(!(other is OTOpndLocal))
  2909. return false;
  2910. OTOpndLocal otherlocal = (OTOpndLocal)other;
  2911. return local == otherlocal.local;
  2912. }
  2913. public override string PrintableString
  2914. {
  2915. get
  2916. {
  2917. return "ref " + local.name;
  2918. }
  2919. }
  2920. }
  2921. /**
  2922. * New C#-level array.
  2923. */
  2924. private class OTOpndNewarr: OTOpnd
  2925. {
  2926. public Type type;
  2927. public OTOpnd index;
  2928. public OTOpndNewarr(Type type, OTOpnd index)
  2929. {
  2930. this.type = type;
  2931. this.index = index;
  2932. }
  2933. public override bool HasSideEffects
  2934. {
  2935. get
  2936. {
  2937. return index.HasSideEffects;
  2938. }
  2939. }
  2940. public override void CountRefs(bool writing)
  2941. {
  2942. index.CountRefs(false);
  2943. }
  2944. public override OTOpnd ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd, ref bool rc)
  2945. {
  2946. if(SameAs(oldopnd))
  2947. {
  2948. rc = true;
  2949. return newopnd;
  2950. }
  2951. index = index.ReplaceOperand(oldopnd, newopnd, ref rc);
  2952. return this;
  2953. }
  2954. public override bool SameAs(OTOpnd other)
  2955. {
  2956. return false;
  2957. }
  2958. public override string PrintableString
  2959. {
  2960. get
  2961. {
  2962. return "newarr " + type.Name + "[" + index.PrintableString + "]";
  2963. }
  2964. }
  2965. }
  2966. /**
  2967. * New C#-level object.
  2968. */
  2969. private class OTOpndNewobj: OTOpnd
  2970. {
  2971. public ConstructorInfo ctor;
  2972. public OTOpnd[] args;
  2973. public static OTOpnd Make(ConstructorInfo ctor, OTOpnd[] args)
  2974. {
  2975. // newobj LSL_Float (x) => x
  2976. if((ctor.DeclaringType == typeof(LSL_Float)) && (args.Length == 1))
  2977. {
  2978. Type ptype = ctor.GetParameters()[0].ParameterType;
  2979. if(ptype == typeof(string))
  2980. {
  2981. return new OTOpndCast(typeof(double), args[0]);
  2982. }
  2983. return args[0];
  2984. }
  2985. // newobj LSL_Integer (x) => x
  2986. if((ctor.DeclaringType == typeof(LSL_Integer)) && (args.Length == 1))
  2987. {
  2988. Type ptype = ctor.GetParameters()[0].ParameterType;
  2989. if(ptype == typeof(string))
  2990. {
  2991. return new OTOpndCast(typeof(int), args[0]);
  2992. }
  2993. return args[0];
  2994. }
  2995. // newobj LSL_String (x) => x
  2996. if((ctor.DeclaringType == typeof(LSL_String)) && (args.Length == 1))
  2997. {
  2998. return args[0];
  2999. }
  3000. // newobj LSL_Rotation (x, y, z, w) => <x, y, z, w>
  3001. if((ctor.DeclaringType == typeof(LSL_Rotation)) && (args.Length == 4))
  3002. {
  3003. return new OTOpndRot(args[0], args[1], args[2], args[3]);
  3004. }
  3005. // newobj LSL_Vector (x, y, z) => <x, y, z>
  3006. if((ctor.DeclaringType == typeof(LSL_Vector)) && (args.Length == 3))
  3007. {
  3008. return new OTOpndVec(args[0], args[1], args[2]);
  3009. }
  3010. // newobj LSL_Rotation (string) => (rotation) string
  3011. if((ctor.DeclaringType == typeof(LSL_Rotation)) && (args.Length == 1))
  3012. {
  3013. return new OTOpndCast(typeof(LSL_Rotation), args[0]);
  3014. }
  3015. // newobj LSL_Vector (string) => (rotation) string
  3016. if((ctor.DeclaringType == typeof(LSL_Vector)) && (args.Length == 1))
  3017. {
  3018. return new OTOpndCast(typeof(LSL_Vector), args[0]);
  3019. }
  3020. // newobj LSL_List (newarr object[0]) => [ ]
  3021. if((ctor.DeclaringType == typeof(LSL_List)) && (args.Length == 1) && (args[0] is OTOpndNewarr))
  3022. {
  3023. OTOpndNewarr arg0 = (OTOpndNewarr)args[0];
  3024. if((arg0.type == typeof(object)) && (arg0.index is OTOpndInt) && (((OTOpndInt)arg0.index).value == 0))
  3025. {
  3026. OTOpndListIni listini = new OTOpndListIni();
  3027. listini.values = new OTOpnd[0];
  3028. return listini;
  3029. }
  3030. }
  3031. // something else, output as is
  3032. OTOpndNewobj it = new OTOpndNewobj();
  3033. it.ctor = ctor;
  3034. it.args = args;
  3035. return it;
  3036. }
  3037. private OTOpndNewobj()
  3038. {
  3039. }
  3040. public override bool HasSideEffects
  3041. {
  3042. get
  3043. {
  3044. foreach(OTOpnd arg in args)
  3045. {
  3046. if(arg.HasSideEffects)
  3047. return true;
  3048. }
  3049. return false;
  3050. }
  3051. }
  3052. public override void CountRefs(bool writing)
  3053. {
  3054. foreach(OTOpnd arg in args)
  3055. {
  3056. arg.CountRefs(false);
  3057. }
  3058. }
  3059. public override OTOpnd ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd, ref bool rc)
  3060. {
  3061. if(SameAs(oldopnd))
  3062. {
  3063. rc = true;
  3064. return newopnd;
  3065. }
  3066. for(int i = 0; i < args.Length; i++)
  3067. {
  3068. args[i] = args[i].ReplaceOperand(oldopnd, newopnd, ref rc);
  3069. }
  3070. return this;
  3071. }
  3072. public override bool SameAs(OTOpnd other)
  3073. {
  3074. if(!(other is OTOpndNewobj))
  3075. return false;
  3076. OTOpndNewobj otherno = (OTOpndNewobj)other;
  3077. if(otherno.ctor.DeclaringType != ctor.DeclaringType)
  3078. return false;
  3079. if(otherno.args.Length != args.Length)
  3080. return false;
  3081. for(int i = 0; i < args.Length; i++)
  3082. {
  3083. if(!args[i].SameAs(otherno.args[i]))
  3084. return false;
  3085. }
  3086. return true;
  3087. }
  3088. public override string PrintableString
  3089. {
  3090. get
  3091. {
  3092. StringBuilder sb = new StringBuilder();
  3093. sb.Append("newobj ");
  3094. sb.Append(ctor.DeclaringType.Name);
  3095. sb.Append(" (");
  3096. bool first = true;
  3097. foreach(OTOpnd arg in args)
  3098. {
  3099. if(!first)
  3100. sb.Append(", ");
  3101. sb.Append(arg.PrintableString);
  3102. first = false;
  3103. }
  3104. sb.Append(')');
  3105. return sb.ToString();
  3106. }
  3107. }
  3108. }
  3109. /**
  3110. * Rotation value.
  3111. */
  3112. private class OTOpndRot: OTOpnd
  3113. {
  3114. private OTOpnd x, y, z, w;
  3115. public OTOpndRot(OTOpnd x, OTOpnd y, OTOpnd z, OTOpnd w)
  3116. {
  3117. this.x = StripFloatCast(x);
  3118. this.y = StripFloatCast(y);
  3119. this.z = StripFloatCast(z);
  3120. this.w = StripFloatCast(w);
  3121. }
  3122. public override bool HasSideEffects
  3123. {
  3124. get
  3125. {
  3126. return x.HasSideEffects || y.HasSideEffects || z.HasSideEffects || w.HasSideEffects;
  3127. }
  3128. }
  3129. public override void CountRefs(bool writing)
  3130. {
  3131. x.CountRefs(false);
  3132. y.CountRefs(false);
  3133. z.CountRefs(false);
  3134. w.CountRefs(false);
  3135. }
  3136. public override OTOpnd ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd, ref bool rc)
  3137. {
  3138. if(SameAs(oldopnd))
  3139. {
  3140. rc = true;
  3141. return newopnd;
  3142. }
  3143. x = x.ReplaceOperand(oldopnd, newopnd, ref rc);
  3144. y = y.ReplaceOperand(oldopnd, newopnd, ref rc);
  3145. z = z.ReplaceOperand(oldopnd, newopnd, ref rc);
  3146. w = w.ReplaceOperand(oldopnd, newopnd, ref rc);
  3147. return this;
  3148. }
  3149. public override bool SameAs(OTOpnd other)
  3150. {
  3151. if(!(other is OTOpndRot))
  3152. return false;
  3153. OTOpndRot otherv = (OTOpndRot)other;
  3154. return otherv.x.SameAs(x) && otherv.y.SameAs(y) && otherv.z.SameAs(z) && otherv.w.SameAs(w);
  3155. }
  3156. public override string PrintableString
  3157. {
  3158. get
  3159. {
  3160. return "<" + x.PrintableString + ", " + y.PrintableString + ", " + z.PrintableString + ", " + w.PrintableString + ">";
  3161. }
  3162. }
  3163. }
  3164. /**
  3165. * Static field.
  3166. */
  3167. private class OTOpndSField: OTOpnd
  3168. {
  3169. private FieldInfo field;
  3170. public OTOpndSField(FieldInfo field)
  3171. {
  3172. this.field = field;
  3173. }
  3174. public override bool HasSideEffects
  3175. {
  3176. get
  3177. {
  3178. return false;
  3179. }
  3180. }
  3181. public override bool SameAs(OTOpnd other)
  3182. {
  3183. if(!(other is OTOpndSField))
  3184. return false;
  3185. OTOpndSField othersfield = (OTOpndSField)other;
  3186. return (field.Name == othersfield.field.Name) && (field.DeclaringType == othersfield.field.DeclaringType);
  3187. }
  3188. public override string PrintableString
  3189. {
  3190. get
  3191. {
  3192. if(field.DeclaringType == typeof(ScriptBaseClass))
  3193. return field.Name;
  3194. return field.DeclaringType.Name + "." + field.Name;
  3195. }
  3196. }
  3197. }
  3198. /**
  3199. * Call to string.Compare().
  3200. * See use cases in BinOpStr:
  3201. * strcmp (a, b) ceq 0
  3202. * (strcmp (a, b) ceq 0) xor 1 => we translate to: strcmp (a, b) cne 0
  3203. * strcmp (a, b) clt 0
  3204. * strcmp (a, b) clt 1 // <=
  3205. * strcmp (a, b) cgt 0
  3206. * strcmp (a, b) cgt -1 // >=
  3207. * ...but then optimized by ScriptCollector if followed by br{false,true}:
  3208. * ceq + xor 1 + brtrue => bne.un
  3209. * ceq + xor 1 + brfalse => beq
  3210. * ceq + brtrue => beq
  3211. * ceq + brfalse => bne.un
  3212. * cgt + brtrue => bgt
  3213. * cgt + brfalse => ble
  3214. * clt + brtrue => blt
  3215. * clt + brfalse => bge
  3216. * So we end up with these cases:
  3217. * strcmp (a, b) ceq 0
  3218. * strcmp (a, b) cne 0
  3219. * strcmp (a, b) clt 0
  3220. * strcmp (a, b) clt 1
  3221. * strcmp (a, b) cgt 0
  3222. * strcmp (a, b) cgt -1
  3223. * strcmp (a, b) beq 0
  3224. * strcmp (a, b) bne.un 0
  3225. * strcmp (a, b) bgt 0
  3226. * strcmp (a, b) ble 0
  3227. * strcmp (a, b) bgt -1
  3228. * strcmp (a, b) ble -1
  3229. * strcmp (a, b) blt 0
  3230. * strcmp (a, b) bge 0
  3231. * strcmp (a, b) blt 1
  3232. * strcmp (a, b) bge 1
  3233. * ... so we pretty them up in OTOpndBinOp
  3234. */
  3235. private class OTOpndStrCmp: OTOpnd
  3236. {
  3237. private static Dictionary<string, string> binops = InitBinops();
  3238. private static Dictionary<string, string> InitBinops()
  3239. {
  3240. Dictionary<string, string> d = new Dictionary<string, string>();
  3241. d["ceq 0"] = "ceq";
  3242. d["cne 0"] = "cne";
  3243. d["clt 0"] = "clt";
  3244. d["clt 1"] = "cle";
  3245. d["cgt 0"] = "cgt";
  3246. d["cgt -1"] = "cge";
  3247. d["beq 0"] = "ceq";
  3248. d["bne.un 0"] = "cne";
  3249. d["bgt 0"] = "cgt";
  3250. d["ble 0"] = "cle";
  3251. d["bgt -1"] = "cge";
  3252. d["ble -1"] = "clt";
  3253. d["blt 0"] = "clt";
  3254. d["bge 0"] = "cge";
  3255. d["blt 1"] = "cle";
  3256. d["bge 1"] = "cgt";
  3257. return d;
  3258. }
  3259. private OTOpnd arg0;
  3260. private OTOpnd arg1;
  3261. public OTOpndStrCmp(OTOpnd arg0, OTOpnd arg1)
  3262. {
  3263. this.arg0 = arg0;
  3264. this.arg1 = arg1;
  3265. }
  3266. /**
  3267. * Try to make something a script writer would recognize.
  3268. * If we can't, then we leave it as a call to xmrStringCompare().
  3269. * this = some strcmp(a,b)
  3270. * opCode = hopefully some cxx or bxx from above table
  3271. * rite = hopefully some constant from above table
  3272. */
  3273. public OTOpnd MakeBinOp(MyOp opCode, OTOpnd rite)
  3274. {
  3275. if(!(rite is OTOpndInt))
  3276. return null;
  3277. int riteint = ((OTOpndInt)rite).value;
  3278. string key = opCode.name + ' ' + riteint;
  3279. string cxxopname;
  3280. if(!binops.TryGetValue(key, out cxxopname))
  3281. return null;
  3282. return OTOpndBinOp.Make(arg0, MyOp.GetByName(cxxopname), arg1);
  3283. }
  3284. public OTOpnd MakeUnOp(MyOp opCode)
  3285. {
  3286. if(opCode == MyOp.Brfalse)
  3287. return OTOpndBinOp.Make(arg0, MyOp.Ceq, arg1);
  3288. if(opCode == MyOp.Brtrue)
  3289. return OTOpndBinOp.Make(arg0, MyOp.Cne, arg1);
  3290. return null;
  3291. }
  3292. public override bool HasSideEffects
  3293. {
  3294. get
  3295. {
  3296. return false;
  3297. }
  3298. }
  3299. public override void CountRefs(bool writing)
  3300. {
  3301. arg0.CountRefs(writing);
  3302. arg1.CountRefs(writing);
  3303. }
  3304. public override OTOpnd ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd, ref bool rc)
  3305. {
  3306. if(SameAs(oldopnd))
  3307. {
  3308. rc = true;
  3309. return newopnd;
  3310. }
  3311. arg0 = arg0.ReplaceOperand(oldopnd, newopnd, ref rc);
  3312. arg1 = arg1.ReplaceOperand(oldopnd, newopnd, ref rc);
  3313. return this;
  3314. }
  3315. public override bool SameAs(OTOpnd other)
  3316. {
  3317. if(!(other is OTOpndStrCmp))
  3318. return false;
  3319. return arg0.SameAs(((OTOpndStrCmp)other).arg0) && arg1.SameAs(((OTOpndStrCmp)other).arg1);
  3320. }
  3321. public override string PrintableString
  3322. {
  3323. get
  3324. {
  3325. return "xmrStringCompare (" + arg0.PrintableString + ", " + arg1.PrintableString + ")";
  3326. }
  3327. }
  3328. }
  3329. /**
  3330. * Unary operator.
  3331. */
  3332. private class OTOpndUnOp: OTOpnd
  3333. {
  3334. public MyOp opCode;
  3335. public OTOpnd value;
  3336. private static Dictionary<string, string> brfops = InitBrfOps();
  3337. private static Dictionary<string, string> InitBrfOps()
  3338. {
  3339. Dictionary<string, string> d = new Dictionary<string, string>();
  3340. d["beq"] = "cne";
  3341. d["bge"] = "clt";
  3342. d["bgt"] = "cle";
  3343. d["ble"] = "cgt";
  3344. d["blt"] = "cge";
  3345. d["bne.un"] = "ceq";
  3346. d["ceq"] = "cne";
  3347. d["cge"] = "clt";
  3348. d["cgt"] = "cle";
  3349. d["cle"] = "cgt";
  3350. d["clt"] = "cge";
  3351. d["cne"] = "ceq";
  3352. return d;
  3353. }
  3354. public static OTOpnd Make(MyOp opCode, OTOpnd value)
  3355. {
  3356. // (brfalse (brfalse (x))) => (brtrue (x))
  3357. if((opCode == MyOp.Brfalse) && (value is OTOpndUnOp) && (((OTOpndUnOp)value).opCode == MyOp.Brfalse))
  3358. {
  3359. ((OTOpndUnOp)value).opCode = MyOp.Brtrue;
  3360. return value;
  3361. }
  3362. // (brfalse (brtrue (x))) => (brfalse (x))
  3363. if((opCode == MyOp.Brfalse) && (value is OTOpndUnOp) && (((OTOpndUnOp)value).opCode == MyOp.Brtrue))
  3364. {
  3365. ((OTOpndUnOp)value).opCode = MyOp.Brfalse;
  3366. return value;
  3367. }
  3368. // (brtrue (brfalse (x))) => (brfalse (x))
  3369. if((opCode == MyOp.Brtrue) && (value is OTOpndUnOp) && (((OTOpndUnOp)value).opCode == MyOp.Brfalse))
  3370. {
  3371. return value;
  3372. }
  3373. // (brtrue (brtrue (x))) => (brtrue (x))
  3374. if((opCode == MyOp.Brtrue) && (value is OTOpndUnOp) && (((OTOpndUnOp)value).opCode == MyOp.Brtrue))
  3375. {
  3376. return value;
  3377. }
  3378. // (brfalse (x beq y)) => (x bne y) etc
  3379. string brfop;
  3380. if((opCode == MyOp.Brfalse) && (value is OTOpndBinOp) && brfops.TryGetValue(((OTOpndBinOp)value).opCode.name, out brfop))
  3381. {
  3382. ((OTOpndBinOp)value).opCode = MyOp.GetByName(brfop);
  3383. return value;
  3384. }
  3385. // (brtrue (x beq y)) => (x beq y) etc
  3386. if((opCode == MyOp.Brtrue) && (value is OTOpndBinOp) && brfops.ContainsKey(((OTOpndBinOp)value).opCode.name))
  3387. {
  3388. return value;
  3389. }
  3390. // strcmp() can be a special case
  3391. if(value is OTOpndStrCmp)
  3392. {
  3393. OTOpnd strcmp = ((OTOpndStrCmp)value).MakeUnOp(opCode);
  3394. if(strcmp != null)
  3395. return strcmp;
  3396. }
  3397. // nothing special, save opcode and value
  3398. OTOpndUnOp it = new OTOpndUnOp();
  3399. it.opCode = opCode;
  3400. it.value = value;
  3401. return it;
  3402. }
  3403. private OTOpndUnOp()
  3404. {
  3405. }
  3406. public override bool HasSideEffects
  3407. {
  3408. get
  3409. {
  3410. return value.HasSideEffects;
  3411. }
  3412. }
  3413. public override void CountRefs(bool writing)
  3414. {
  3415. value.CountRefs(false);
  3416. }
  3417. public override OTOpnd ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd, ref bool rc)
  3418. {
  3419. if(SameAs(oldopnd))
  3420. {
  3421. rc = true;
  3422. return newopnd;
  3423. }
  3424. value = value.ReplaceOperand(oldopnd, newopnd, ref rc);
  3425. return this;
  3426. }
  3427. public override bool SameAs(OTOpnd other)
  3428. {
  3429. if(!(other is OTOpndUnOp))
  3430. return false;
  3431. OTOpndUnOp otherop = (OTOpndUnOp)other;
  3432. return (opCode.ToString() == otherop.opCode.ToString()) && value.SameAs(otherop.value);
  3433. }
  3434. public override string PrintableString
  3435. {
  3436. get
  3437. {
  3438. StringBuilder sb = new StringBuilder();
  3439. sb.Append(opCode.source);
  3440. sb.Append(' ');
  3441. if(value is OTOpndBinOp)
  3442. sb.Append('(');
  3443. sb.Append(value.PrintableString);
  3444. if(value is OTOpndBinOp)
  3445. sb.Append(')');
  3446. return sb.ToString();
  3447. }
  3448. }
  3449. }
  3450. /**
  3451. * Vector value.
  3452. */
  3453. private class OTOpndVec: OTOpnd
  3454. {
  3455. private OTOpnd x, y, z;
  3456. public OTOpndVec(OTOpnd x, OTOpnd y, OTOpnd z)
  3457. {
  3458. this.x = StripFloatCast(x);
  3459. this.y = StripFloatCast(y);
  3460. this.z = StripFloatCast(z);
  3461. }
  3462. public override bool HasSideEffects
  3463. {
  3464. get
  3465. {
  3466. return x.HasSideEffects || y.HasSideEffects || z.HasSideEffects;
  3467. }
  3468. }
  3469. public override void CountRefs(bool writing)
  3470. {
  3471. x.CountRefs(false);
  3472. y.CountRefs(false);
  3473. z.CountRefs(false);
  3474. }
  3475. public override OTOpnd ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd, ref bool rc)
  3476. {
  3477. if(SameAs(oldopnd))
  3478. {
  3479. rc = true;
  3480. return newopnd;
  3481. }
  3482. x = x.ReplaceOperand(oldopnd, newopnd, ref rc);
  3483. y = y.ReplaceOperand(oldopnd, newopnd, ref rc);
  3484. z = z.ReplaceOperand(oldopnd, newopnd, ref rc);
  3485. return this;
  3486. }
  3487. public override bool SameAs(OTOpnd other)
  3488. {
  3489. if(!(other is OTOpndVec))
  3490. return false;
  3491. OTOpndVec otherv = (OTOpndVec)other;
  3492. return otherv.x.SameAs(x) && otherv.y.SameAs(y) && otherv.z.SameAs(z);
  3493. }
  3494. public override string PrintableString
  3495. {
  3496. get
  3497. {
  3498. return "<" + x.PrintableString + ", " + y.PrintableString + ", " + z.PrintableString + ">";
  3499. }
  3500. }
  3501. }
  3502. /**
  3503. * Constants.
  3504. */
  3505. private class OTOpndDouble: OTOpnd
  3506. {
  3507. public double value;
  3508. public OTOpndDouble(double value)
  3509. {
  3510. this.value = value;
  3511. }
  3512. public override bool HasSideEffects
  3513. {
  3514. get
  3515. {
  3516. return false;
  3517. }
  3518. }
  3519. public override bool SameAs(OTOpnd other)
  3520. {
  3521. if(!(other is OTOpndDouble))
  3522. return false;
  3523. return ((OTOpndDouble)other).value == value;
  3524. }
  3525. public override string PrintableString
  3526. {
  3527. get
  3528. {
  3529. string s = value.ToString();
  3530. long i;
  3531. if(long.TryParse(s, out i))
  3532. {
  3533. s += ".0";
  3534. }
  3535. return s;
  3536. }
  3537. }
  3538. }
  3539. private class OTOpndFloat: OTOpnd
  3540. {
  3541. public float value;
  3542. public OTOpndFloat(float value)
  3543. {
  3544. this.value = value;
  3545. }
  3546. public override bool HasSideEffects
  3547. {
  3548. get
  3549. {
  3550. return false;
  3551. }
  3552. }
  3553. public override bool SameAs(OTOpnd other)
  3554. {
  3555. if(!(other is OTOpndFloat))
  3556. return false;
  3557. return ((OTOpndFloat)other).value == value;
  3558. }
  3559. public override string PrintableString
  3560. {
  3561. get
  3562. {
  3563. string s = value.ToString();
  3564. long i;
  3565. if(long.TryParse(s, out i))
  3566. {
  3567. s += ".0";
  3568. }
  3569. return s;
  3570. }
  3571. }
  3572. }
  3573. private class OTOpndInt: OTOpnd
  3574. {
  3575. public int value;
  3576. public OTOpndInt(int value)
  3577. {
  3578. this.value = value;
  3579. }
  3580. public override bool HasSideEffects
  3581. {
  3582. get
  3583. {
  3584. return false;
  3585. }
  3586. }
  3587. public override bool SameAs(OTOpnd other)
  3588. {
  3589. if(!(other is OTOpndInt))
  3590. return false;
  3591. return ((OTOpndInt)other).value == value;
  3592. }
  3593. public override string PrintableString
  3594. {
  3595. get
  3596. {
  3597. return value.ToString();
  3598. }
  3599. }
  3600. }
  3601. private class OTOpndNull: OTOpnd
  3602. {
  3603. public override bool HasSideEffects
  3604. {
  3605. get
  3606. {
  3607. return false;
  3608. }
  3609. }
  3610. public override bool SameAs(OTOpnd other)
  3611. {
  3612. return other is OTOpndNull;
  3613. }
  3614. public override string PrintableString
  3615. {
  3616. get
  3617. {
  3618. return "undef";
  3619. }
  3620. }
  3621. }
  3622. private class OTOpndString: OTOpnd
  3623. {
  3624. public string value;
  3625. public OTOpndString(string value)
  3626. {
  3627. this.value = value;
  3628. }
  3629. public override bool HasSideEffects
  3630. {
  3631. get
  3632. {
  3633. return false;
  3634. }
  3635. }
  3636. public override bool SameAs(OTOpnd other)
  3637. {
  3638. if(!(other is OTOpndString))
  3639. return false;
  3640. return ((OTOpndString)other).value == value;
  3641. }
  3642. public override string PrintableString
  3643. {
  3644. get
  3645. {
  3646. StringBuilder sb = new StringBuilder();
  3647. TokenDeclInline.PrintParamString(sb, value);
  3648. return sb.ToString();
  3649. }
  3650. }
  3651. }
  3652. /****************************************\
  3653. * Tokens what are in statement list. *
  3654. \****************************************/
  3655. public abstract class OTStmt
  3656. {
  3657. /**
  3658. * Increment reference counts.
  3659. */
  3660. public abstract void CountRefs();
  3661. /**
  3662. * Strip out any of the behind-the-scenes code such as stack capture/restore.
  3663. * By default, there is no change.
  3664. */
  3665. public virtual bool StripStuff(LinkedListNode<OTStmt> link)
  3666. {
  3667. return false;
  3668. }
  3669. /**
  3670. * Replace the oldopnd operand with the newopnd operand if it is present.
  3671. * Return whether or not it was found and replaced.
  3672. */
  3673. public abstract bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd);
  3674. /**
  3675. * Detect and modify for do/for/if/while structures.
  3676. */
  3677. public virtual bool DetectDoForIfWhile(LinkedListNode<OTStmt> link)
  3678. {
  3679. return false;
  3680. }
  3681. /**
  3682. * If this statement is the old statement, replace it with the given new statement.
  3683. * Also search any sub-ordinate statements.
  3684. * **NOTE**: minimally implemented to replace a Jump with a Break or Continue
  3685. */
  3686. public abstract OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt);
  3687. /**
  3688. * Print the statement out on the given printer with the given indenting.
  3689. * The first line is already indented, subsequent lines must be indented as given.
  3690. * This method should leave the printer at the end of the line.
  3691. */
  3692. public abstract void PrintStmt(TextWriter twout, string indent);
  3693. /**
  3694. * Strip all statements following this statement
  3695. * because this statement jumps somewhere.
  3696. */
  3697. protected bool StripStuffForTerminal(LinkedListNode<OTStmt> link)
  3698. {
  3699. // strip all statements following jump until seeing some label
  3700. bool rc = false;
  3701. if(link != null)
  3702. {
  3703. LinkedListNode<OTStmt> nextlink;
  3704. while((nextlink = link.Next) != null)
  3705. {
  3706. if(nextlink.Value is OTStmtLabel)
  3707. break;
  3708. nextlink.List.Remove(nextlink);
  3709. rc = true;
  3710. }
  3711. }
  3712. return rc;
  3713. }
  3714. }
  3715. /**************************\
  3716. * Primitive statements *
  3717. \**************************/
  3718. /**
  3719. * Begin catch block (catch).
  3720. */
  3721. private class OTStmtBegCatBlk: OTStmt
  3722. {
  3723. public OTStmtBegExcBlk tryblock;
  3724. public OTStmtBlock catchblock;
  3725. private Type excType;
  3726. public OTStmtBegCatBlk(Type excType)
  3727. {
  3728. this.excType = excType;
  3729. }
  3730. public override void CountRefs()
  3731. {
  3732. catchblock.CountRefs();
  3733. }
  3734. public override bool StripStuff(LinkedListNode<OTStmt> link)
  3735. {
  3736. return catchblock.StripStuff(null);
  3737. }
  3738. public override bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd)
  3739. {
  3740. return catchblock.ReplaceOperand(oldopnd, newopnd);
  3741. }
  3742. public override bool DetectDoForIfWhile(LinkedListNode<OTStmt> link)
  3743. {
  3744. return catchblock.DetectDoForIfWhile(link);
  3745. }
  3746. public override OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt)
  3747. {
  3748. catchblock = (OTStmtBlock)catchblock.ReplaceStatement(oldstmt, newstmt);
  3749. return this;
  3750. }
  3751. /**
  3752. * Print out the catch block including its enclosed statements.
  3753. */
  3754. public override void PrintStmt(TextWriter twout, string indent)
  3755. {
  3756. twout.Write("catch (" + excType.Name + ") ");
  3757. catchblock.PrintStmt(twout, indent);
  3758. }
  3759. }
  3760. /**
  3761. * Begin exception block (try).
  3762. */
  3763. private class OTStmtBegExcBlk: OTStmt
  3764. {
  3765. // statements within the try { } not including any catch or finally
  3766. public OTStmtBlock tryblock;
  3767. // list of all catch { } blocks associated with this try { }
  3768. public LinkedList<OTStmtBegCatBlk> catches = new LinkedList<OTStmtBegCatBlk>();
  3769. // possible single finally { } associated with this try
  3770. public OTStmtBegFinBlk finblock; // might be null
  3771. public override void CountRefs()
  3772. {
  3773. tryblock.CountRefs();
  3774. foreach(OTStmtBegCatBlk catblock in catches)
  3775. {
  3776. catblock.CountRefs();
  3777. }
  3778. if(finblock != null)
  3779. finblock.CountRefs();
  3780. }
  3781. /**
  3782. * Strip behind-the-scenes info from all the sub-blocks.
  3783. */
  3784. public override bool StripStuff(LinkedListNode<OTStmt> link)
  3785. {
  3786. // strip behind-the-scenes info from all the sub-blocks.
  3787. bool rc = tryblock.StripStuff(null);
  3788. foreach(OTStmtBegCatBlk catblk in catches)
  3789. {
  3790. rc |= catblk.StripStuff(null);
  3791. }
  3792. if(finblock != null)
  3793. rc |= finblock.StripStuff(null);
  3794. if(rc)
  3795. return true;
  3796. // change:
  3797. // try {
  3798. // ...
  3799. // }
  3800. // to:
  3801. // {
  3802. // ...
  3803. // }
  3804. // note that an empty catch () { } has meaning so can't be stripped
  3805. // empty finally { } blocks strips itself from the try
  3806. if((catches.Count == 0) && (finblock == null) && (link != null))
  3807. {
  3808. link.List.AddAfter(link, tryblock);
  3809. tryblock = null;
  3810. link.List.Remove(link);
  3811. return true;
  3812. }
  3813. return false;
  3814. }
  3815. public override bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd)
  3816. {
  3817. bool rc = tryblock.ReplaceOperand(oldopnd, newopnd);
  3818. foreach(OTStmtBegCatBlk catblk in catches)
  3819. {
  3820. rc |= catblk.ReplaceOperand(oldopnd, newopnd);
  3821. }
  3822. if(finblock != null)
  3823. rc |= finblock.ReplaceOperand(oldopnd, newopnd);
  3824. return rc;
  3825. }
  3826. public override bool DetectDoForIfWhile(LinkedListNode<OTStmt> link)
  3827. {
  3828. bool rc = tryblock.DetectDoForIfWhile(link);
  3829. foreach(OTStmtBegCatBlk catblk in catches)
  3830. {
  3831. rc |= catblk.DetectDoForIfWhile(link);
  3832. }
  3833. if(finblock != null)
  3834. rc |= finblock.DetectDoForIfWhile(link);
  3835. return rc;
  3836. }
  3837. /**
  3838. * Assume we will never try to replace the try block itself.
  3839. * But go through all our sub-ordinates statements.
  3840. */
  3841. public override OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt)
  3842. {
  3843. tryblock = (OTStmtBlock)tryblock.ReplaceStatement(oldstmt, newstmt);
  3844. for(LinkedListNode<OTStmtBegCatBlk> catlink = catches.First; catlink != null; catlink = catlink.Next)
  3845. {
  3846. catlink.Value = (OTStmtBegCatBlk)catlink.Value.ReplaceStatement(oldstmt, newstmt);
  3847. }
  3848. if(finblock != null)
  3849. finblock = (OTStmtBegFinBlk)finblock.ReplaceStatement(oldstmt, newstmt);
  3850. return this;
  3851. }
  3852. /**
  3853. * Print out the try block including its enclosed statements.
  3854. * And since the try is the only thing pushed to the outer block,
  3855. * we also print out all the catch and finally blocks.
  3856. */
  3857. public override void PrintStmt(TextWriter twout, string indent)
  3858. {
  3859. twout.Write("try ");
  3860. tryblock.PrintStmt(twout, indent);
  3861. foreach(OTStmtBegCatBlk catblk in catches)
  3862. {
  3863. twout.Write(' ');
  3864. catblk.PrintStmt(twout, indent);
  3865. }
  3866. if(finblock != null)
  3867. {
  3868. twout.Write(' ');
  3869. finblock.PrintStmt(twout, indent);
  3870. }
  3871. }
  3872. }
  3873. /**
  3874. * Begin finally block (finally).
  3875. */
  3876. private class OTStmtBegFinBlk: OTStmt
  3877. {
  3878. public OTStmtBegExcBlk tryblock;
  3879. public OTStmtBlock finblock;
  3880. public override void CountRefs()
  3881. {
  3882. finblock.CountRefs();
  3883. }
  3884. /**
  3885. * Strip behind-the-scene parts from the finally block.
  3886. */
  3887. public override bool StripStuff(LinkedListNode<OTStmt> link)
  3888. {
  3889. // strip behind-the-scenes parts from finally block itself
  3890. if(finblock.StripStuff(null))
  3891. return true;
  3892. // if finblock is empty, delete the finally from the try
  3893. if(finblock.blkstmts.Count == 0)
  3894. {
  3895. tryblock.finblock = null;
  3896. return true;
  3897. }
  3898. return false;
  3899. }
  3900. public override bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd)
  3901. {
  3902. return finblock.ReplaceOperand(oldopnd, newopnd);
  3903. }
  3904. public override bool DetectDoForIfWhile(LinkedListNode<OTStmt> link)
  3905. {
  3906. return finblock.DetectDoForIfWhile(link);
  3907. }
  3908. /**
  3909. * Assume we will never try to replace the finally block itself.
  3910. * But go through all our sub-ordinates statements.
  3911. */
  3912. public override OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt)
  3913. {
  3914. finblock = (OTStmtBlock)finblock.ReplaceStatement(oldstmt, newstmt);
  3915. return this;
  3916. }
  3917. /**
  3918. * Print out the finally block including its enclosed statements.
  3919. */
  3920. public override void PrintStmt(TextWriter twout, string indent)
  3921. {
  3922. twout.Write("finally ");
  3923. finblock.PrintStmt(twout, indent);
  3924. }
  3925. }
  3926. /**
  3927. * Simple if jump/break/continue statement.
  3928. */
  3929. private class OTStmtCond: OTStmt
  3930. {
  3931. public OTOpnd valu;
  3932. public OTStmt stmt; // jump, break, continue only
  3933. public OTStmtCond(OTOpnd valu, OTStmt stmt)
  3934. {
  3935. this.valu = valu;
  3936. this.stmt = stmt;
  3937. }
  3938. public override void CountRefs()
  3939. {
  3940. valu.CountRefs(false);
  3941. stmt.CountRefs();
  3942. }
  3943. public override bool StripStuff(LinkedListNode<OTStmt> link)
  3944. {
  3945. // we assume that callMode is always CallMode_NORMAL, ie, not doing a stack capture or restore
  3946. // so the 'if (arg$0.callMode bne.un 0) ...' is deleted
  3947. // and the 'if (arg$0.callMode bne.un 1) ...' becomes unconditional
  3948. // it can also be __xmrinst.callMode instead of arg$0
  3949. if(valu is OTOpndBinOp)
  3950. {
  3951. OTOpndBinOp binop = (OTOpndBinOp)valu;
  3952. if((binop.left is OTOpndField) && (binop.opCode.ToString() == "bne.un") && (binop.rite is OTOpndInt))
  3953. {
  3954. OTOpndField leftfield = (OTOpndField)binop.left;
  3955. if(leftfield.field.Name == _callMode)
  3956. {
  3957. bool ok = false;
  3958. if(leftfield.obj is OTOpndArg)
  3959. {
  3960. ok = ((OTOpndArg)leftfield.obj).index == 0;
  3961. }
  3962. if(leftfield.obj is OTOpndLocal)
  3963. {
  3964. ok = ((OTOpndLocal)leftfield.obj).local.name.StartsWith(_xmrinstlocal);
  3965. }
  3966. if(ok)
  3967. {
  3968. OTOpndInt riteint = (OTOpndInt)binop.rite;
  3969. // delete 'if ((arg$0).callMode bne.un 0) ...'
  3970. if(riteint.value == XMRInstAbstract.CallMode_NORMAL)
  3971. {
  3972. link.List.Remove(link);
  3973. return true;
  3974. }
  3975. // make 'if ((arg$0).callMode bne.un 1) ...' unconditional
  3976. if(riteint.value == XMRInstAbstract.CallMode_SAVE)
  3977. {
  3978. link.Value = stmt;
  3979. return true;
  3980. }
  3981. }
  3982. }
  3983. }
  3984. }
  3985. // similarly we assume that doGblInit is always 0 to eliminate the code at beginning of default state_entry()
  3986. // so the 'if (brfalse __xmrinst.doGblInit) ...' is made unconditional
  3987. if(valu is OTOpndUnOp)
  3988. {
  3989. OTOpndUnOp unop = (OTOpndUnOp)valu;
  3990. if((unop.opCode == MyOp.Brfalse) && (unop.value is OTOpndField))
  3991. {
  3992. OTOpndField valuefield = (OTOpndField)unop.value;
  3993. if(valuefield.field.Name == _doGblInit)
  3994. {
  3995. bool ok = false;
  3996. if(valuefield.obj is OTOpndLocal)
  3997. {
  3998. ok = ((OTOpndLocal)valuefield.obj).local.name.StartsWith(_xmrinstlocal);
  3999. }
  4000. if(ok)
  4001. {
  4002. // make 'if (brfalse __xmrinst.doGblInit) ...' unconditional
  4003. link.Value = stmt;
  4004. return true;
  4005. }
  4006. }
  4007. }
  4008. }
  4009. return false;
  4010. }
  4011. public override bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd)
  4012. {
  4013. bool rc = stmt.ReplaceOperand(oldopnd, newopnd);
  4014. valu = valu.ReplaceOperand(oldopnd, newopnd, ref rc);
  4015. return rc;
  4016. }
  4017. /**
  4018. * Maybe this simple if statement is part of a script-level if/then/else statement.
  4019. */
  4020. public override bool DetectDoForIfWhile(LinkedListNode<OTStmt> link)
  4021. {
  4022. return OTStmtIf.Detect(link);
  4023. }
  4024. /**
  4025. * Assume we won't replace the if statement itself.
  4026. * But search all our sub-ordinate statements.
  4027. */
  4028. public override OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt)
  4029. {
  4030. stmt = stmt.ReplaceStatement(oldstmt, newstmt);
  4031. return this;
  4032. }
  4033. public override void PrintStmt(TextWriter twout, string indent)
  4034. {
  4035. twout.Write("if (" + StripBrtrue(valu).PrintableString + ") ");
  4036. stmt.PrintStmt(twout, indent);
  4037. }
  4038. /**
  4039. * Scan forward for a given label definition.
  4040. * Put intervening statements in a statement block.
  4041. * @param link = start scanning after this statement
  4042. * @param label = look for this label definition
  4043. * @param block = where to return intervening statement block
  4044. * @returns null: label definition not found
  4045. * else: label definition statement
  4046. */
  4047. private static LinkedListNode<OTStmt> ScanForLabel(LinkedListNode<OTStmt> link,
  4048. OTLabel label, out OTStmtBlock block)
  4049. {
  4050. block = new OTStmtBlock();
  4051. while((link = link.Next) != null)
  4052. {
  4053. if(link.Value is OTStmtLabel)
  4054. {
  4055. if(((OTStmtLabel)link.Value).label == label)
  4056. break;
  4057. }
  4058. block.blkstmts.AddLast(link.Value);
  4059. }
  4060. return link;
  4061. }
  4062. /**
  4063. * Strip statements after link up to and including donelink.
  4064. */
  4065. private static void StripInterveningStatements(LinkedListNode<OTStmt> link, LinkedListNode<OTStmt> donelink)
  4066. {
  4067. LinkedListNode<OTStmt> striplink;
  4068. do
  4069. {
  4070. striplink = link.Next;
  4071. striplink.List.Remove(striplink);
  4072. } while(striplink != donelink);
  4073. }
  4074. }
  4075. /**
  4076. * Jump to a label.
  4077. */
  4078. private class OTStmtJump: OTStmt
  4079. {
  4080. public OTLabel label;
  4081. public static OTStmt Make(OTLabel label)
  4082. {
  4083. // jumps to __retlbl are return statements
  4084. // note that is is safe to say it is a valueless return because
  4085. // valued returns are done with this construct:
  4086. // __retval = ....;
  4087. // jump __retlbl;
  4088. // and those __retval = statements have been changed to return statements already
  4089. if(label.name.StartsWith(_retlbl))
  4090. return new OTStmtRet(null);
  4091. // other jumps are really jumps
  4092. OTStmtJump it = new OTStmtJump();
  4093. it.label = label;
  4094. return it;
  4095. }
  4096. private OTStmtJump()
  4097. {
  4098. }
  4099. public override void CountRefs()
  4100. {
  4101. label.lbljumps++;
  4102. }
  4103. public override bool StripStuff(LinkedListNode<OTStmt> link)
  4104. {
  4105. if(link == null)
  4106. return false;
  4107. // strip statements following unconditional jump until next label
  4108. bool rc = StripStuffForTerminal(link);
  4109. // if we (now) have:
  4110. // jump label;
  4111. // @label;
  4112. // ... delete this jump
  4113. if(link.Next != null)
  4114. {
  4115. OTStmtLabel nextlabel = (OTStmtLabel)link.Next.Value;
  4116. if(nextlabel.label == label)
  4117. {
  4118. link.List.Remove(link);
  4119. rc = true;
  4120. }
  4121. }
  4122. return rc;
  4123. }
  4124. public override bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd)
  4125. {
  4126. return false;
  4127. }
  4128. /**
  4129. * This is actually what ReplaceStatement() is currently used for.
  4130. * It replaces a jump with a break or a continue.
  4131. */
  4132. public override OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt)
  4133. {
  4134. if((oldstmt is OTStmtJump) && (((OTStmtJump)oldstmt).label == label))
  4135. return newstmt;
  4136. return this;
  4137. }
  4138. public override void PrintStmt(TextWriter twout, string indent)
  4139. {
  4140. twout.Write("jump " + label.PrintableName + ';');
  4141. }
  4142. }
  4143. /**
  4144. * Label definition point.
  4145. */
  4146. private class OTStmtLabel: OTStmt
  4147. {
  4148. public OTLabel label;
  4149. private OTDecompile decompile;
  4150. public static void AddLast(OTDecompile decompile, OTLabel label)
  4151. {
  4152. OTStmtLabel it = new OTStmtLabel();
  4153. it.label = label;
  4154. it.decompile = decompile;
  4155. decompile.AddLastStmt(it);
  4156. }
  4157. private OTStmtLabel()
  4158. {
  4159. }
  4160. public override void CountRefs()
  4161. {
  4162. // don't increment label.lbljumps
  4163. // cuz we don't want the positioning
  4164. // to count as a reference, only jumps
  4165. // to the label should count
  4166. }
  4167. public override bool StripStuff(LinkedListNode<OTStmt> link)
  4168. {
  4169. // if label has nothing jumping to it, remove the label
  4170. if(link != null)
  4171. {
  4172. label.lbljumps = 0;
  4173. decompile.topBlock.CountRefs();
  4174. if(label.lbljumps == 0)
  4175. {
  4176. link.List.Remove(link);
  4177. return true;
  4178. }
  4179. }
  4180. return false;
  4181. }
  4182. public override bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd)
  4183. {
  4184. return false;
  4185. }
  4186. public override bool DetectDoForIfWhile(LinkedListNode<OTStmt> link)
  4187. {
  4188. if(OTStmtDo.Detect(link))
  4189. return true;
  4190. if(OTStmtFor.Detect(link, true))
  4191. return true;
  4192. if(OTStmtFor.Detect(link, false))
  4193. return true;
  4194. return false;
  4195. }
  4196. public override OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt)
  4197. {
  4198. return this;
  4199. }
  4200. public override void PrintStmt(TextWriter twout, string indent)
  4201. {
  4202. twout.Write("@" + label.PrintableName + ';');
  4203. }
  4204. }
  4205. /**
  4206. * Return with or without value.
  4207. */
  4208. private class OTStmtRet: OTStmt
  4209. {
  4210. public OTOpnd value; // might be null
  4211. public OTStmtRet(OTOpnd value)
  4212. {
  4213. this.value = value;
  4214. }
  4215. public override void CountRefs()
  4216. {
  4217. if(value != null)
  4218. value.CountRefs(false);
  4219. }
  4220. public override bool StripStuff(LinkedListNode<OTStmt> link)
  4221. {
  4222. return StripStuffForTerminal(link);
  4223. }
  4224. public override bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd)
  4225. {
  4226. bool rc = false;
  4227. if(value != null)
  4228. value = value.ReplaceOperand(oldopnd, newopnd, ref rc);
  4229. return rc;
  4230. }
  4231. public override OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt)
  4232. {
  4233. return this;
  4234. }
  4235. public override void PrintStmt(TextWriter twout, string indent)
  4236. {
  4237. if(value == null)
  4238. {
  4239. twout.Write("return;");
  4240. }
  4241. else
  4242. {
  4243. twout.Write("return " + value.PrintableString + ';');
  4244. }
  4245. }
  4246. }
  4247. /**
  4248. * Store value in variable.
  4249. */
  4250. private class OTStmtStore: OTStmt
  4251. {
  4252. public OTOpnd varwr;
  4253. public OTOpnd value;
  4254. private OTDecompile decompile;
  4255. public static void AddLast(OTDecompile decompile, OTOpnd varwr, OTOpnd value)
  4256. {
  4257. OTStmtStore it = new OTStmtStore(varwr, value, decompile);
  4258. decompile.AddLastStmt(it);
  4259. }
  4260. public OTStmtStore(OTOpnd varwr, OTOpnd value, OTDecompile decompile)
  4261. {
  4262. this.varwr = varwr;
  4263. this.value = value;
  4264. this.decompile = decompile;
  4265. }
  4266. public override void CountRefs()
  4267. {
  4268. varwr.CountRefs(true);
  4269. value.CountRefs(false);
  4270. }
  4271. public override bool StripStuff(LinkedListNode<OTStmt> link)
  4272. {
  4273. // strip out stores to __mainCallNo
  4274. if(varwr is OTOpndLocal)
  4275. {
  4276. OTOpndLocal local = (OTOpndLocal)varwr;
  4277. if(local.local.name.StartsWith(_mainCallNo))
  4278. {
  4279. link.List.Remove(link);
  4280. return true;
  4281. }
  4282. }
  4283. // strip out stores to local vars where the var is not read
  4284. // but convert the value to an OTStmtVoid in case it is a call
  4285. if(varwr is OTOpndLocal)
  4286. {
  4287. OTOpndLocal local = (OTOpndLocal)varwr;
  4288. local.local.nlclreads = 0;
  4289. decompile.topBlock.CountRefs();
  4290. if(local.local.nlclreads == 0)
  4291. {
  4292. OTStmt voidstmt = OTStmtVoid.Make(value);
  4293. if(voidstmt == null)
  4294. link.List.Remove(link);
  4295. else
  4296. link.Value = voidstmt;
  4297. return true;
  4298. }
  4299. }
  4300. // strip out bla = newobj HeapTrackerList (...);
  4301. if(value is OTOpndNewobj)
  4302. {
  4303. OTOpndNewobj valueno = (OTOpndNewobj)value;
  4304. if(valueno.ctor.DeclaringType == typeof(HeapTrackerList))
  4305. {
  4306. link.List.Remove(link);
  4307. return true;
  4308. }
  4309. }
  4310. // strip out bla = newobj HeapTrackerObject (...);
  4311. if(value is OTOpndNewobj)
  4312. {
  4313. OTOpndNewobj valueno = (OTOpndNewobj)value;
  4314. if(valueno.ctor.DeclaringType == typeof(HeapTrackerObject))
  4315. {
  4316. link.List.Remove(link);
  4317. return true;
  4318. }
  4319. }
  4320. // strip out bla = newobj HeapTrackerString (...);
  4321. if(value is OTOpndNewobj)
  4322. {
  4323. OTOpndNewobj valueno = (OTOpndNewobj)value;
  4324. if(valueno.ctor.DeclaringType == typeof(HeapTrackerString))
  4325. {
  4326. link.List.Remove(link);
  4327. return true;
  4328. }
  4329. }
  4330. // convert tmp$n = bla bla;
  4331. // .... tmp$n ....;
  4332. // to
  4333. // .... bla bla ....;
  4334. // gets rid of vast majority of temps
  4335. if(varwr is OTOpndLocal)
  4336. {
  4337. OTOpndLocal temp = (OTOpndLocal)varwr;
  4338. if(temp.local.name.StartsWith("tmp$"))
  4339. {
  4340. temp.local.nlclreads = 0;
  4341. temp.local.nlclwrites = 0;
  4342. decompile.topBlock.CountRefs();
  4343. if((temp.local.nlclreads == 1) && (temp.local.nlclwrites == 1) && (link.Next != null))
  4344. {
  4345. OTStmt nextstmt = link.Next.Value;
  4346. if(!(nextstmt is OTStmtBlock))
  4347. {
  4348. if(nextstmt.ReplaceOperand(varwr, value))
  4349. {
  4350. link.List.Remove(link);
  4351. return true;
  4352. }
  4353. }
  4354. }
  4355. // also try to convert:
  4356. // tmp$n = ... asdf ... << we are here (link)
  4357. // lcl = tmp$n; << nextstore
  4358. // ... qwer tmp$n ...
  4359. // ... no further references to tmp$n
  4360. // to:
  4361. // lcl = ... asdf ...
  4362. // ... qwer lcl ...
  4363. if((temp.local.nlclreads == 2) && (temp.local.nlclwrites == 1) &&
  4364. (link.Next != null) && (link.Next.Value is OTStmtStore))
  4365. {
  4366. OTStmtStore nextstore = (OTStmtStore)link.Next.Value;
  4367. if((nextstore.varwr is OTOpndLocal) && (nextstore.value is OTOpndLocal) && (link.Next.Next != null))
  4368. {
  4369. OTOpndLocal localopnd = (OTOpndLocal)nextstore.varwr;
  4370. OTOpndLocal tempopnd = (OTOpndLocal)nextstore.value;
  4371. if(tempopnd.local == temp.local)
  4372. {
  4373. OTStmt finalstmt = link.Next.Next.Value;
  4374. if(finalstmt.ReplaceOperand(tempopnd, localopnd))
  4375. {
  4376. nextstore.value = value;
  4377. link.List.Remove(link);
  4378. return true;
  4379. }
  4380. }
  4381. }
  4382. }
  4383. }
  4384. }
  4385. // convert:
  4386. // dup$n = ... asdf ... << we are here
  4387. // lcl = dup$n;
  4388. // ... qwer dup$n ...
  4389. // ... no further references to dup$n
  4390. // to:
  4391. // lcl = ... asdf ...
  4392. // ... qwer lcl ...
  4393. if((varwr is OTOpndDup) && (link != null))
  4394. {
  4395. OTOpndDup vardup = (OTOpndDup)varwr;
  4396. LinkedListNode<OTStmt> nextlink = link.Next;
  4397. vardup.ndupreads = 0;
  4398. decompile.topBlock.CountRefs();
  4399. if((vardup.ndupreads == 2) && (nextlink != null) && (nextlink.Value is OTStmtStore))
  4400. {
  4401. // point to the supposed lcl = dup$n statement
  4402. OTStmtStore nextstore = (OTStmtStore)nextlink.Value;
  4403. LinkedListNode<OTStmt> nextlink2 = nextlink.Next;
  4404. if((nextstore.varwr is OTOpndLocal) && (nextstore.value == vardup) && (nextlink2 != null))
  4405. {
  4406. // get the local var being written and point to the ... qwer dup$n ... statement
  4407. OTOpndLocal varlcl = (OTOpndLocal)nextstore.varwr;
  4408. OTStmt nextstmt2 = nextlink2.Value;
  4409. // try to replace dup$n in qwer with lcl
  4410. if(nextstmt2.ReplaceOperand(vardup, varlcl))
  4411. {
  4412. // successful, replace dup$n in asdf with lcl
  4413. // and delete the lcl = dup$n statement
  4414. varwr = varlcl;
  4415. nextlink.List.Remove(nextlink);
  4416. return true;
  4417. }
  4418. }
  4419. }
  4420. }
  4421. // convert:
  4422. // dup$n = ... asdf ... << we are here
  4423. // ... qwer dup$n ...
  4424. // ... no further references to dup$n
  4425. // to:
  4426. // ... qwer ... asdf ... ...
  4427. if((varwr is OTOpndDup) && (link != null))
  4428. {
  4429. OTOpndDup vardup = (OTOpndDup)varwr;
  4430. LinkedListNode<OTStmt> nextlink = link.Next;
  4431. vardup.ndupreads = 0;
  4432. decompile.topBlock.CountRefs();
  4433. if((vardup.ndupreads == 1) && (nextlink != null))
  4434. {
  4435. // point to the ... qwer dup$n ... statement
  4436. OTStmt nextstmt = nextlink.Value;
  4437. // try to replace dup$n in qwer with ... asdf ...
  4438. if(nextstmt.ReplaceOperand(vardup, value))
  4439. {
  4440. // successful, delete the dup$n = ... asdf ... statement
  4441. link.List.Remove(link);
  4442. return true;
  4443. }
  4444. }
  4445. }
  4446. // look for list initialization [ ... ]
  4447. if(OTOpndListIni.Detect(link))
  4448. return true;
  4449. // __xmrinst = (XMRInstAbstract) arg$0 indicates this is an event handler
  4450. // so strip it out and set the flag
  4451. if((varwr is OTOpndLocal) && (value is OTOpndCast))
  4452. {
  4453. OTOpndLocal lcl = (OTOpndLocal)varwr;
  4454. OTOpndCast cast = (OTOpndCast)value;
  4455. if(lcl.local.name.StartsWith(_xmrinstlocal) && (cast.value is OTOpndArg))
  4456. {
  4457. link.List.Remove(link);
  4458. return true;
  4459. }
  4460. }
  4461. // local = [ (optional cast) ] __xmrinst.ehArgs[n] is a definition of event handler arg #n
  4462. // if found, make it event handler arg list definition
  4463. OTOpnd valuenocast = value;
  4464. if(valuenocast is OTOpndCast)
  4465. valuenocast = ((OTOpndCast)value).value;
  4466. if((varwr is OTOpndLocal) && (valuenocast is OTOpndArrayElem))
  4467. {
  4468. OTOpndArrayElem array = (OTOpndArrayElem)valuenocast;
  4469. if((array.array is OTOpndField) && (array.index is OTOpndInt))
  4470. {
  4471. OTOpndField arrayfield = (OTOpndField)array.array;
  4472. if((arrayfield.obj is OTOpndLocal) &&
  4473. ((OTOpndLocal)arrayfield.obj).local.name.StartsWith(_xmrinstlocal) &&
  4474. (arrayfield.field.Name == _ehArgs))
  4475. {
  4476. int index = ((OTOpndInt)array.index).value;
  4477. decompile.eharglist[index] = ((OTOpndLocal)varwr).local;
  4478. link.List.Remove(link);
  4479. return true;
  4480. }
  4481. }
  4482. }
  4483. // __retval$n = ...; => return ...;
  4484. if(varwr is OTOpndLocal)
  4485. {
  4486. OTOpndLocal lcl = (OTOpndLocal)varwr;
  4487. if(lcl.local.name.StartsWith(_retval))
  4488. {
  4489. link.Value = new OTStmtRet(value);
  4490. return true;
  4491. }
  4492. }
  4493. return false;
  4494. }
  4495. public override bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd)
  4496. {
  4497. bool rc = false;
  4498. if(value != null)
  4499. value = value.ReplaceOperand(oldopnd, newopnd, ref rc);
  4500. return rc;
  4501. }
  4502. public override OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt)
  4503. {
  4504. return this;
  4505. }
  4506. public override void PrintStmt(TextWriter twout, string indent)
  4507. {
  4508. // print x = x + 1 as x += 1, but don't print x = x < 3 as x <= 3
  4509. if(value is OTOpndBinOp)
  4510. {
  4511. OTOpndBinOp valuebo = (OTOpndBinOp)value;
  4512. if(varwr.SameAs(valuebo.left) && " add and div mul or rem shl shr sub xor ".Contains(' ' + valuebo.opCode.name + ' '))
  4513. {
  4514. twout.Write(varwr.PrintableString + ' ' + valuebo.opCode.source + "= " + valuebo.rite.PrintableString + ';');
  4515. return;
  4516. }
  4517. }
  4518. twout.Write(varwr.PrintableString + " = " + value.PrintableString + ';');
  4519. }
  4520. }
  4521. /**
  4522. * Dispatch to a table of labels.
  4523. */
  4524. private class OTStmtSwitch: OTStmt
  4525. {
  4526. private OTOpnd index;
  4527. private OTLabel[] labels;
  4528. public OTStmtSwitch(OTOpnd index, OTLabel[] labels)
  4529. {
  4530. this.index = index;
  4531. this.labels = labels;
  4532. }
  4533. public override void CountRefs()
  4534. {
  4535. index.CountRefs(false);
  4536. foreach(OTLabel label in labels)
  4537. {
  4538. label.lbljumps++;
  4539. }
  4540. }
  4541. public override bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd)
  4542. {
  4543. bool rc = false;
  4544. if(index != null)
  4545. index = index.ReplaceOperand(oldopnd, newopnd, ref rc);
  4546. return rc;
  4547. }
  4548. public override OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt)
  4549. {
  4550. return this;
  4551. }
  4552. public override void PrintStmt(TextWriter twout, string indent)
  4553. {
  4554. twout.Write("switch (" + index.PrintableString + ") {\n");
  4555. for(int i = 0; i < labels.Length; i++)
  4556. {
  4557. twout.Write(indent + INDENT + "case " + i + ": jump " + labels[i].name + ";\n");
  4558. }
  4559. twout.Write(indent + '}');
  4560. }
  4561. }
  4562. /**
  4563. * Throw an exception.
  4564. */
  4565. private class OTStmtThrow: OTStmt
  4566. {
  4567. private OTOpnd value;
  4568. private OTDecompile decompile;
  4569. public OTStmtThrow(OTOpnd value, OTDecompile decompile)
  4570. {
  4571. this.value = value;
  4572. this.decompile = decompile;
  4573. }
  4574. public override void CountRefs()
  4575. {
  4576. value.CountRefs(false);
  4577. }
  4578. public override bool StripStuff(LinkedListNode<OTStmt> link)
  4579. {
  4580. return StripStuffForTerminal(link);
  4581. }
  4582. public override bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd)
  4583. {
  4584. bool rc = false;
  4585. if(value != null)
  4586. value = value.ReplaceOperand(oldopnd, newopnd, ref rc);
  4587. return rc;
  4588. }
  4589. public override OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt)
  4590. {
  4591. return this;
  4592. }
  4593. public override void PrintStmt(TextWriter twout, string indent)
  4594. {
  4595. // throw newobj ScriptUndefinedStateException ("x") => state x
  4596. if(value is OTOpndNewobj)
  4597. {
  4598. OTOpndNewobj valueno = (OTOpndNewobj)value;
  4599. if((valueno.ctor.DeclaringType == typeof(ScriptUndefinedStateException)) &&
  4600. (valueno.args.Length == 1) && (valueno.args[0] is OTOpndString))
  4601. {
  4602. OTOpndString arg0 = (OTOpndString)valueno.args[0];
  4603. twout.Write("state " + arg0.value + "; /* throws undefined state exception */");
  4604. return;
  4605. }
  4606. }
  4607. // throw newobj ScriptChangeStateException (n) => state n
  4608. if(value is OTOpndNewobj)
  4609. {
  4610. OTOpndNewobj valueno = (OTOpndNewobj)value;
  4611. if((valueno.ctor.DeclaringType == typeof(ScriptChangeStateException)) &&
  4612. (valueno.args.Length == 1) && (valueno.args[0] is OTOpndInt))
  4613. {
  4614. OTOpndInt arg0 = (OTOpndInt)valueno.args[0];
  4615. twout.Write("state " + decompile.scriptObjCode.stateNames[arg0.value] + ';');
  4616. return;
  4617. }
  4618. }
  4619. // throwing something else, output as is
  4620. twout.Write("throw " + value.PrintableString + ';');
  4621. }
  4622. }
  4623. /**
  4624. * Call with void return, or really anything that we discard the value of after computing it.
  4625. */
  4626. private class OTStmtVoid: OTStmt
  4627. {
  4628. private OTOpnd value;
  4629. public static void AddLast(OTDecompile decompile, OTOpnd value)
  4630. {
  4631. OTStmt it = OTStmtVoid.Make(value);
  4632. if(it != null)
  4633. decompile.AddLastStmt(it);
  4634. }
  4635. public static OTStmt Make(OTOpnd value)
  4636. {
  4637. if(!value.HasSideEffects)
  4638. return null;
  4639. OTStmtVoid it = new OTStmtVoid();
  4640. it.value = value;
  4641. return it;
  4642. }
  4643. private OTStmtVoid()
  4644. {
  4645. }
  4646. public override void CountRefs()
  4647. {
  4648. value.CountRefs(false);
  4649. }
  4650. public override bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd)
  4651. {
  4652. bool rc = false;
  4653. value = value.ReplaceOperand(oldopnd, newopnd, ref rc);
  4654. return rc;
  4655. }
  4656. public override bool StripStuff(LinkedListNode<OTStmt> link)
  4657. {
  4658. // strip out calls to CheckRunQuick() and CheckRunStack()
  4659. if(value is OTOpndCall)
  4660. {
  4661. OTOpndCall call = (OTOpndCall)value;
  4662. MethodInfo method = call.method;
  4663. if((method.Name == _checkRunQuick) || (method.Name == _checkRunStack))
  4664. {
  4665. link.List.Remove(link);
  4666. return true;
  4667. }
  4668. }
  4669. return false;
  4670. }
  4671. public override OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt)
  4672. {
  4673. return this;
  4674. }
  4675. public override void PrintStmt(TextWriter twout, string indent)
  4676. {
  4677. twout.Write(value.PrintableString + ';');
  4678. }
  4679. }
  4680. /***************************\
  4681. * Structured statements *
  4682. \***************************/
  4683. /**
  4684. * Block of statements.
  4685. */
  4686. private class OTStmtBlock: OTStmt
  4687. {
  4688. public LinkedList<OTStmt> blkstmts = new LinkedList<OTStmt>();
  4689. public override void CountRefs()
  4690. {
  4691. foreach(OTStmt stmt in blkstmts)
  4692. {
  4693. stmt.CountRefs();
  4694. }
  4695. }
  4696. /**
  4697. * Scrub out all references to behind-the-scenes parts and simplify.
  4698. */
  4699. public override bool StripStuff(LinkedListNode<OTStmt> link)
  4700. {
  4701. // loop through all sub-statements to strip out behind-the-scenes references
  4702. bool rc = false;
  4703. loop:
  4704. for(LinkedListNode<OTStmt> stmtlink = blkstmts.First; stmtlink != null; stmtlink = stmtlink.Next)
  4705. {
  4706. if(stmtlink.Value.StripStuff(stmtlink))
  4707. {
  4708. rc = true;
  4709. goto loop;
  4710. }
  4711. }
  4712. if(rc)
  4713. return true;
  4714. // try to merge this block into outer block
  4715. // change:
  4716. // {
  4717. // ...
  4718. // { << link points here
  4719. // ...
  4720. // }
  4721. // ...
  4722. // }
  4723. // to:
  4724. // {
  4725. // ...
  4726. // ...
  4727. // ...
  4728. // }
  4729. if(link != null)
  4730. {
  4731. LinkedListNode<OTStmt> nextlink;
  4732. while((nextlink = blkstmts.Last) != null)
  4733. {
  4734. nextlink.List.Remove(nextlink);
  4735. link.List.AddAfter(link, nextlink);
  4736. }
  4737. link.List.Remove(link);
  4738. return true;
  4739. }
  4740. return rc;
  4741. }
  4742. public override bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd)
  4743. {
  4744. bool rc = false;
  4745. foreach(OTStmt stmt in blkstmts)
  4746. {
  4747. rc |= stmt.ReplaceOperand(oldopnd, newopnd);
  4748. }
  4749. return rc;
  4750. }
  4751. /**
  4752. * Check each statement in the block to see if it starts a do/for/if/while statement.
  4753. */
  4754. public override bool DetectDoForIfWhile(LinkedListNode<OTStmt> link)
  4755. {
  4756. bool rc = false;
  4757. loop:
  4758. for(link = blkstmts.First; link != null; link = link.Next)
  4759. {
  4760. if(link.Value.DetectDoForIfWhile(link))
  4761. {
  4762. rc = true;
  4763. goto loop;
  4764. }
  4765. }
  4766. return rc;
  4767. }
  4768. /**
  4769. * Assume we will never try to replace the block itself.
  4770. * But go through all our sub-ordinates statements.
  4771. */
  4772. public override OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt)
  4773. {
  4774. for(LinkedListNode<OTStmt> childlink = blkstmts.First; childlink != null; childlink = childlink.Next)
  4775. {
  4776. childlink.Value = childlink.Value.ReplaceStatement(oldstmt, newstmt);
  4777. }
  4778. return this;
  4779. }
  4780. /**
  4781. * Print out the block including its enclosed statements.
  4782. */
  4783. public override void PrintStmt(TextWriter twout, string indent)
  4784. {
  4785. switch(blkstmts.Count)
  4786. {
  4787. case 0:
  4788. {
  4789. twout.Write("{ }");
  4790. break;
  4791. }
  4792. ////case 1: {
  4793. //// blkstmts.First.Value.PrintStmt (twout, indent);
  4794. //// break;
  4795. ////}
  4796. default:
  4797. {
  4798. twout.Write('{');
  4799. PrintBodyAndEnd(twout, indent);
  4800. break;
  4801. }
  4802. }
  4803. }
  4804. public void PrintBodyAndEnd(TextWriter twout, string indent)
  4805. {
  4806. string newindent = indent + INDENT;
  4807. foreach(OTStmt stmt in blkstmts)
  4808. {
  4809. twout.Write('\n' + indent);
  4810. if(!(stmt is OTStmtLabel))
  4811. twout.Write(INDENT);
  4812. else
  4813. twout.Write(LABELINDENT);
  4814. stmt.PrintStmt(twout, newindent);
  4815. }
  4816. twout.Write('\n' + indent + '}');
  4817. }
  4818. }
  4819. /**
  4820. * 'do' statement.
  4821. */
  4822. private class OTStmtDo: OTStmt
  4823. {
  4824. private OTOpnd dotest;
  4825. private OTStmtBlock dobody;
  4826. /**
  4827. * See if we have a do loop...
  4828. * @doloop_<suffix>; << link points here
  4829. * ... <dobody> ...
  4830. * [ if (dotest) ] jump doloop_<suffix>;
  4831. */
  4832. public static bool Detect(LinkedListNode<OTStmt> link)
  4833. {
  4834. // see if we have label starting with 'doloop_'
  4835. OTLabel looplabel = ((OTStmtLabel)link.Value).label;
  4836. if(!looplabel.name.StartsWith(_doLoop))
  4837. return false;
  4838. // good chance we have a do loop
  4839. OTStmtDo it = new OTStmtDo();
  4840. // scan ahead looking for the terminating cond/jump loop
  4841. // also gather up the statements for the do body block
  4842. it.dobody = new OTStmtBlock();
  4843. LinkedListNode<OTStmt> nextlink;
  4844. for(nextlink = link.Next; nextlink != null; nextlink = nextlink.Next)
  4845. {
  4846. OTStmt nextstmt = nextlink.Value;
  4847. // add statement to do body
  4848. it.dobody.blkstmts.AddLast(nextlink.Value);
  4849. // check for something what jumps to loop label
  4850. // that gives us the end of the loop
  4851. OTStmt maybejump = nextstmt;
  4852. if(nextstmt is OTStmtCond)
  4853. {
  4854. maybejump = ((OTStmtCond)nextstmt).stmt;
  4855. }
  4856. if((maybejump is OTStmtJump) && (((OTStmtJump)maybejump).label == looplabel))
  4857. {
  4858. break;
  4859. }
  4860. }
  4861. // make sure we found the jump back to the loop label
  4862. if(nextlink == null)
  4863. return false;
  4864. // remove all statements from caller's block including the continue label if any
  4865. // but leave the break label alone it will be removed later if unreferenced
  4866. // and leave the initial loop label intact for now
  4867. for(LinkedListNode<OTStmt> remlink = null; (remlink = link.Next) != null;)
  4868. {
  4869. link.List.Remove(remlink);
  4870. if(remlink == nextlink)
  4871. break;
  4872. }
  4873. // take test condition from last statement of body
  4874. // it should be an cond/jump or just a jump to the loop label
  4875. LinkedListNode<OTStmt> lastlink = it.dobody.blkstmts.Last;
  4876. OTStmt laststmt = lastlink.Value;
  4877. if(laststmt is OTStmtCond)
  4878. {
  4879. it.dotest = ((OTStmtCond)laststmt).valu;
  4880. }
  4881. else
  4882. {
  4883. it.dotest = new OTOpndInt(1);
  4884. }
  4885. lastlink.List.Remove(lastlink);
  4886. // finally replace the loop label with the whole do statement
  4887. link.Value = it;
  4888. // tell caller we made a change
  4889. return true;
  4890. }
  4891. public override void CountRefs()
  4892. {
  4893. if(dotest != null)
  4894. dotest.CountRefs(false);
  4895. if(dobody != null)
  4896. dobody.CountRefs();
  4897. }
  4898. public override bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd)
  4899. {
  4900. return dobody.ReplaceOperand(oldopnd, newopnd);
  4901. }
  4902. public override bool DetectDoForIfWhile(LinkedListNode<OTStmt> link)
  4903. {
  4904. return dobody.DetectDoForIfWhile(link);
  4905. }
  4906. /**
  4907. * Assume we won't replace the do statement itself.
  4908. * But search all our sub-ordinate statements.
  4909. */
  4910. public override OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt)
  4911. {
  4912. dobody = (OTStmtBlock)dobody.ReplaceStatement(oldstmt, newstmt);
  4913. return this;
  4914. }
  4915. public override void PrintStmt(TextWriter twout, string indent)
  4916. {
  4917. // output do body
  4918. twout.Write("do ");
  4919. dobody.PrintStmt(twout, indent);
  4920. // output while part
  4921. twout.Write(" while (" + StripBrtrue(dotest).PrintableString + ");");
  4922. }
  4923. }
  4924. /**
  4925. * 'for' or 'while' statement.
  4926. */
  4927. private class OTStmtFor: OTStmt
  4928. {
  4929. private bool iswhile;
  4930. private OTOpnd fortest;
  4931. private OTStmtBlock forbody;
  4932. private OTStmt forinit;
  4933. private OTStmt forstep;
  4934. /**
  4935. * See if we have a for or while loop...
  4936. * <forinit>
  4937. * @forloop_<suffix>; << link points here
  4938. * [ if (<fortest>) jump forbreak_<suffix>; ]
  4939. * ... <forbody> ...
  4940. * jump forloop_<suffix>;
  4941. * [ @forbreak_<suffix>; ]
  4942. */
  4943. public static bool Detect(LinkedListNode<OTStmt> link, bool iswhile)
  4944. {
  4945. string loopname = iswhile ? _whileLoop : _forLoop;
  4946. string breakname = iswhile ? _whileBreak : _forBreak;
  4947. // see if we have label starting with 'forloop_'
  4948. OTLabel looplabel = ((OTStmtLabel)link.Value).label;
  4949. if(!looplabel.name.StartsWith(loopname))
  4950. return false;
  4951. // good chance we have a for loop
  4952. OTStmtFor it = new OTStmtFor();
  4953. it.iswhile = iswhile;
  4954. // all labels end with this suffix
  4955. string suffix = looplabel.name.Substring(loopname.Length);
  4956. // scan ahead looking for the 'jump forloop_<suffix>;' statement
  4957. // also gather up the statements for the for body block
  4958. it.forbody = new OTStmtBlock();
  4959. LinkedListNode<OTStmt> lastlink;
  4960. for(lastlink = link; (lastlink = lastlink.Next) != null;)
  4961. {
  4962. // check for jump forloop that tells us where loop ends
  4963. if(lastlink.Value is OTStmtJump)
  4964. {
  4965. OTStmtJump lastjump = (OTStmtJump)lastlink.Value;
  4966. if(lastjump.label == looplabel)
  4967. break;
  4968. }
  4969. // add to body block
  4970. it.forbody.blkstmts.AddLast(lastlink.Value);
  4971. }
  4972. // make sure we found the 'jump forloop' where the for loop ends
  4973. if(lastlink == null)
  4974. return false;
  4975. // remove all statements from caller's block including final jump
  4976. // but leave the loop label in place
  4977. for(LinkedListNode<OTStmt> nextlink = null; (nextlink = link.Next) != null;)
  4978. {
  4979. link.List.Remove(nextlink);
  4980. if(nextlink == lastlink)
  4981. break;
  4982. }
  4983. // if statement before loop label is an assignment, use it for the init statement
  4984. if(!iswhile && (link.Previous != null) && (link.Previous.Value is OTStmtStore))
  4985. {
  4986. it.forinit = link.Previous.Value;
  4987. link.List.Remove(link.Previous);
  4988. }
  4989. // if first statement of for body is 'if (...) jump breaklabel' use it for the test value
  4990. if((it.forbody.blkstmts.First != null) && (it.forbody.blkstmts.First.Value is OTStmtCond))
  4991. {
  4992. OTStmtCond condstmt = (OTStmtCond)it.forbody.blkstmts.First.Value;
  4993. if((condstmt.stmt is OTStmtJump) && (((OTStmtJump)condstmt.stmt).label.name == breakname + suffix))
  4994. {
  4995. it.fortest = OTOpndUnOp.Make(MyOp.Brfalse, condstmt.valu);
  4996. it.forbody.blkstmts.RemoveFirst();
  4997. }
  4998. }
  4999. // if last statement of body is an assigment,
  5000. // use the assignment as the step statement
  5001. if(!iswhile && (it.forbody.blkstmts.Last != null) &&
  5002. (it.forbody.blkstmts.Last.Value is OTStmtStore))
  5003. {
  5004. LinkedListNode<OTStmt> storelink = it.forbody.blkstmts.Last;
  5005. storelink.List.Remove(storelink);
  5006. it.forstep = storelink.Value;
  5007. }
  5008. // finally replace the loop label with the whole for statement
  5009. link.Value = it;
  5010. // tell caller we made a change
  5011. return true;
  5012. }
  5013. public override void CountRefs()
  5014. {
  5015. if(fortest != null)
  5016. fortest.CountRefs(false);
  5017. if(forbody != null)
  5018. forbody.CountRefs();
  5019. if(forinit != null)
  5020. forinit.CountRefs();
  5021. if(forstep != null)
  5022. forstep.CountRefs();
  5023. }
  5024. public override bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd)
  5025. {
  5026. return forbody.ReplaceOperand(oldopnd, newopnd) |
  5027. ((forinit != null) && forinit.ReplaceOperand(oldopnd, newopnd)) |
  5028. ((forstep != null) && forstep.ReplaceOperand(oldopnd, newopnd));
  5029. }
  5030. public override bool DetectDoForIfWhile(LinkedListNode<OTStmt> link)
  5031. {
  5032. return forbody.DetectDoForIfWhile(link) |
  5033. ((forinit != null) && forinit.DetectDoForIfWhile(link)) |
  5034. ((forstep != null) && forstep.DetectDoForIfWhile(link));
  5035. }
  5036. /**
  5037. * Assume we won't replace the for statement itself.
  5038. * But search all our sub-ordinate statements.
  5039. */
  5040. public override OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt)
  5041. {
  5042. forbody = (OTStmtBlock)forbody.ReplaceStatement(oldstmt, newstmt);
  5043. if(forinit != null)
  5044. forinit = forinit.ReplaceStatement(oldstmt, newstmt);
  5045. if(forstep != null)
  5046. forstep = forstep.ReplaceStatement(oldstmt, newstmt);
  5047. return this;
  5048. }
  5049. public override void PrintStmt(TextWriter twout, string indent)
  5050. {
  5051. if(iswhile)
  5052. {
  5053. twout.Write("while (");
  5054. if(fortest == null)
  5055. {
  5056. twout.Write("TRUE");
  5057. }
  5058. else
  5059. {
  5060. twout.Write(StripBrtrue(fortest).PrintableString);
  5061. }
  5062. }
  5063. else
  5064. {
  5065. twout.Write("for (");
  5066. if(forinit != null)
  5067. {
  5068. forinit.PrintStmt(twout, indent + INDENT);
  5069. }
  5070. else
  5071. {
  5072. twout.Write(';');
  5073. }
  5074. if(fortest != null)
  5075. {
  5076. twout.Write(' ' + StripBrtrue(fortest).PrintableString);
  5077. }
  5078. twout.Write(';');
  5079. if(forstep != null)
  5080. {
  5081. StringWriter sw = new StringWriter();
  5082. sw.Write(' ');
  5083. forstep.PrintStmt(sw, indent + INDENT);
  5084. StringBuilder sb = sw.GetStringBuilder();
  5085. int sl = sb.Length;
  5086. if((sl > 0) && (sb[sl - 1] == ';'))
  5087. sb.Remove(--sl, 1);
  5088. twout.Write(sb.ToString());
  5089. }
  5090. }
  5091. twout.Write(") ");
  5092. forbody.PrintStmt(twout, indent);
  5093. }
  5094. }
  5095. /**
  5096. * if/then/else block.
  5097. */
  5098. private class OTStmtIf: OTStmt
  5099. {
  5100. private OTOpnd testvalu;
  5101. private OTStmt thenstmt;
  5102. private OTStmt elsestmt; // might be null
  5103. /**
  5104. * Try to detect a structured if statement.
  5105. *
  5106. * if (condition) jump ifdone_<suffix>; << link points here
  5107. * ... then body ...
  5108. * @ifdone_<suffix>;
  5109. *
  5110. * if (condition) jump ifelse_<suffix>;
  5111. * ... then body ...
  5112. * jump ifdone_<suffix>; << optional if true body doesn't fall through
  5113. * @ifelse_<suffix>;
  5114. * ... else body ...
  5115. * @ifdone_<suffix>;
  5116. */
  5117. public static bool Detect(LinkedListNode<OTStmt> link)
  5118. {
  5119. OTStmtCond condstmt = (OTStmtCond)link.Value;
  5120. if(!(condstmt.stmt is OTStmtJump))
  5121. return false;
  5122. OTStmtJump jumpstmt = (OTStmtJump)condstmt.stmt;
  5123. if(jumpstmt.label.name.StartsWith(_ifDone))
  5124. {
  5125. // then-only if
  5126. // skip forward to find the ifdone_<suffix> label
  5127. // also save the intervening statements for the then body
  5128. OTStmtBlock thenbody;
  5129. LinkedListNode<OTStmt> donelink = ScanForLabel(link, jumpstmt.label, out thenbody);
  5130. // make sure we found matching label
  5131. if(donelink == null)
  5132. return false;
  5133. // replace the jump ifdone_<suffix> with the <then body>
  5134. OTStmtIf it = new OTStmtIf();
  5135. it.thenstmt = thenbody;
  5136. // replace the test value with the opposite
  5137. it.testvalu = OTOpndUnOp.Make(MyOp.Brfalse, condstmt.valu);
  5138. condstmt.valu = null;
  5139. // strip out the true body statements from the main code including the ifdone_<suffix> label
  5140. StripInterveningStatements(link, donelink);
  5141. // replace the simple conditional with the if/then/else block
  5142. link.Value = it;
  5143. // tell caller we changed something
  5144. return true;
  5145. }
  5146. if(jumpstmt.label.name.StartsWith(_ifElse))
  5147. {
  5148. string suffix = jumpstmt.label.name.Substring(_ifElse.Length);
  5149. // if/then/else
  5150. OTStmtIf it = new OTStmtIf();
  5151. // skip forward to find the ifelse_<suffix> label
  5152. // also save the intervening statements for the true body
  5153. OTStmtBlock thenbody;
  5154. LinkedListNode<OTStmt> elselink = ScanForLabel(link, jumpstmt.label, out thenbody);
  5155. // make sure we found matching label
  5156. if(elselink != null)
  5157. {
  5158. // the last statement of the then body might be a jump ifdone_<suffix>
  5159. LinkedListNode<OTStmt> lastthenlink = thenbody.blkstmts.Last;
  5160. if((lastthenlink != null) && (lastthenlink.Value is OTStmtJump))
  5161. {
  5162. OTStmtJump jumpifdone = (OTStmtJump)lastthenlink.Value;
  5163. if(jumpifdone.label.name == _ifDone + suffix)
  5164. {
  5165. lastthenlink.List.Remove(lastthenlink);
  5166. // skip forward to find the ifdone_<suffix> label
  5167. // also save the intervening statements for the else body
  5168. OTStmtBlock elsebody;
  5169. LinkedListNode<OTStmt> donelink = ScanForLabel(elselink, jumpifdone.label, out elsebody);
  5170. if(donelink != null)
  5171. {
  5172. // replace the jump ifdone_<suffix> with the <true body>
  5173. it.thenstmt = thenbody;
  5174. // save the else body as well
  5175. it.elsestmt = elsebody;
  5176. // replace the test value with the opposite
  5177. it.testvalu = OTOpndUnOp.Make(MyOp.Brfalse, condstmt.valu);
  5178. condstmt.valu = null;
  5179. // strip out the true and else body statements from the main code including the ifdone_<suffix> label
  5180. StripInterveningStatements(link, donelink);
  5181. // replace the simple conditional with the if/then/else block
  5182. link.Value = it;
  5183. // tell caller we changed something
  5184. return true;
  5185. }
  5186. }
  5187. }
  5188. // missing the jump _ifDone_<suffix>, so make it a simple if/then
  5189. // if (condition) jump ifelse_<suffix>; << link
  5190. // ... then body ... << encapsulated in block thenbody
  5191. // @ifelse_<suffix>; << elselink
  5192. // ... else body ... << still inline and leave it there
  5193. // @ifdone_<suffix>; << strip this out
  5194. // replace the jump ifelse_<suffix> with the <true body>
  5195. it.thenstmt = thenbody;
  5196. // replace the test value with the opposite
  5197. it.testvalu = OTOpndUnOp.Make(MyOp.Brfalse, condstmt.valu);
  5198. condstmt.valu = null;
  5199. // strip out the then body statements from the main code including the ifelse_<suffix> label
  5200. StripInterveningStatements(link, elselink);
  5201. // there's a dangling unused ifdone_<suffix> label ahead that has to be stripped
  5202. for(LinkedListNode<OTStmt> donelink = link; (donelink = donelink.Next) != null;)
  5203. {
  5204. if((donelink.Value is OTStmtLabel) && (((OTStmtLabel)donelink.Value).label.name == _ifDone + suffix))
  5205. {
  5206. donelink.List.Remove(donelink);
  5207. break;
  5208. }
  5209. }
  5210. // replace the simple conditional with the if/then/else block
  5211. link.Value = it;
  5212. // tell caller we changed something
  5213. return true;
  5214. }
  5215. }
  5216. return false;
  5217. }
  5218. private OTStmtIf()
  5219. {
  5220. }
  5221. public override void CountRefs()
  5222. {
  5223. if(testvalu != null)
  5224. testvalu.CountRefs(false);
  5225. if(thenstmt != null)
  5226. thenstmt.CountRefs();
  5227. if(elsestmt != null)
  5228. elsestmt.CountRefs();
  5229. }
  5230. public override bool ReplaceOperand(OTOpnd oldopnd, OTOpnd newopnd)
  5231. {
  5232. bool rc = thenstmt.ReplaceOperand(oldopnd, newopnd);
  5233. testvalu = testvalu.ReplaceOperand(oldopnd, newopnd, ref rc);
  5234. return rc;
  5235. }
  5236. public override bool DetectDoForIfWhile(LinkedListNode<OTStmt> link)
  5237. {
  5238. return ((thenstmt != null) && thenstmt.DetectDoForIfWhile(link)) |
  5239. ((elsestmt != null) && elsestmt.DetectDoForIfWhile(link));
  5240. }
  5241. /**
  5242. * Assume we won't replace the if statement itself.
  5243. * But search all our sub-ordinate statements.
  5244. */
  5245. public override OTStmt ReplaceStatement(OTStmt oldstmt, OTStmt newstmt)
  5246. {
  5247. thenstmt = thenstmt.ReplaceStatement(oldstmt, newstmt);
  5248. if(elsestmt != null)
  5249. elsestmt = elsestmt.ReplaceStatement(oldstmt, newstmt);
  5250. return this;
  5251. }
  5252. public override void PrintStmt(TextWriter twout, string indent)
  5253. {
  5254. twout.Write("if (" + StripBrtrue(testvalu).PrintableString + ") ");
  5255. OTStmt thenst = ReduceStmtBody(thenstmt, false);
  5256. thenst.PrintStmt(twout, indent);
  5257. if(elsestmt != null)
  5258. {
  5259. twout.Write('\n' + indent + "else ");
  5260. OTStmt elsest = ReduceStmtBody(elsestmt, true);
  5261. elsest.PrintStmt(twout, indent);
  5262. }
  5263. }
  5264. // strip block off a single jump so it prints inline instead of with braces around it
  5265. // also, if this is part of else, strip block for ifs to make else if statement
  5266. private static OTStmt ReduceStmtBody(OTStmt statement, bool stripif)
  5267. {
  5268. OTStmt onestmt = statement;
  5269. if((onestmt is OTStmtBlock) && (((OTStmtBlock)onestmt).blkstmts.Count == 1))
  5270. {
  5271. onestmt = ((OTStmtBlock)onestmt).blkstmts.First.Value;
  5272. if((onestmt is OTStmtJump) || (stripif && (onestmt is OTStmtIf)))
  5273. {
  5274. return onestmt;
  5275. }
  5276. }
  5277. return statement;
  5278. }
  5279. /**
  5280. * Scan forward for a given label definition.
  5281. * Put intervening statements in a statement block.
  5282. * @param link = start scanning after this statement
  5283. * @param label = look for this label definition
  5284. * @param block = where to return intervening statement block
  5285. * @returns null: label definition not found
  5286. * else: label definition statement
  5287. */
  5288. private static LinkedListNode<OTStmt> ScanForLabel(LinkedListNode<OTStmt> link,
  5289. OTLabel label, out OTStmtBlock block)
  5290. {
  5291. block = new OTStmtBlock();
  5292. while((link = link.Next) != null)
  5293. {
  5294. if(link.Value is OTStmtLabel)
  5295. {
  5296. if(((OTStmtLabel)link.Value).label == label)
  5297. break;
  5298. }
  5299. block.blkstmts.AddLast(link.Value);
  5300. }
  5301. return link;
  5302. }
  5303. /**
  5304. * Strip statements after link up to and including donelink.
  5305. */
  5306. private static void StripInterveningStatements(LinkedListNode<OTStmt> link, LinkedListNode<OTStmt> donelink)
  5307. {
  5308. LinkedListNode<OTStmt> striplink;
  5309. do
  5310. {
  5311. striplink = link.Next;
  5312. striplink.List.Remove(striplink);
  5313. } while(striplink != donelink);
  5314. }
  5315. }
  5316. private class MyOp
  5317. {
  5318. public int index;
  5319. public OpCode sysop;
  5320. public string name;
  5321. public string source;
  5322. private static Dictionary<string, MyOp> myopsbyname = new Dictionary<string, MyOp>();
  5323. private static int nextindex = 0;
  5324. public MyOp(OpCode sysop)
  5325. {
  5326. this.index = nextindex++;
  5327. this.sysop = sysop;
  5328. this.name = sysop.Name;
  5329. myopsbyname.Add(name, this);
  5330. }
  5331. public MyOp(OpCode sysop, string source)
  5332. {
  5333. this.index = nextindex++;
  5334. this.sysop = sysop;
  5335. this.name = sysop.Name;
  5336. this.source = source;
  5337. myopsbyname.Add(name, this);
  5338. }
  5339. public MyOp(string name)
  5340. {
  5341. this.index = nextindex++;
  5342. this.name = name;
  5343. myopsbyname.Add(name, this);
  5344. }
  5345. public MyOp(string name, string source)
  5346. {
  5347. this.index = nextindex++;
  5348. this.name = name;
  5349. this.source = source;
  5350. myopsbyname.Add(name, this);
  5351. }
  5352. public static MyOp GetByName(string name)
  5353. {
  5354. return myopsbyname[name];
  5355. }
  5356. public override string ToString()
  5357. {
  5358. return name;
  5359. }
  5360. // these copied from OpCodes.cs
  5361. public static readonly MyOp Nop = new MyOp(OpCodes.Nop);
  5362. public static readonly MyOp Break = new MyOp(OpCodes.Break);
  5363. public static readonly MyOp Ldarg_0 = new MyOp(OpCodes.Ldarg_0);
  5364. public static readonly MyOp Ldarg_1 = new MyOp(OpCodes.Ldarg_1);
  5365. public static readonly MyOp Ldarg_2 = new MyOp(OpCodes.Ldarg_2);
  5366. public static readonly MyOp Ldarg_3 = new MyOp(OpCodes.Ldarg_3);
  5367. public static readonly MyOp Ldloc_0 = new MyOp(OpCodes.Ldloc_0);
  5368. public static readonly MyOp Ldloc_1 = new MyOp(OpCodes.Ldloc_1);
  5369. public static readonly MyOp Ldloc_2 = new MyOp(OpCodes.Ldloc_2);
  5370. public static readonly MyOp Ldloc_3 = new MyOp(OpCodes.Ldloc_3);
  5371. public static readonly MyOp Stloc_0 = new MyOp(OpCodes.Stloc_0);
  5372. public static readonly MyOp Stloc_1 = new MyOp(OpCodes.Stloc_1);
  5373. public static readonly MyOp Stloc_2 = new MyOp(OpCodes.Stloc_2);
  5374. public static readonly MyOp Stloc_3 = new MyOp(OpCodes.Stloc_3);
  5375. public static readonly MyOp Ldarg_S = new MyOp(OpCodes.Ldarg_S);
  5376. public static readonly MyOp Ldarga_S = new MyOp(OpCodes.Ldarga_S);
  5377. public static readonly MyOp Starg_S = new MyOp(OpCodes.Starg_S);
  5378. public static readonly MyOp Ldloc_S = new MyOp(OpCodes.Ldloc_S);
  5379. public static readonly MyOp Ldloca_S = new MyOp(OpCodes.Ldloca_S);
  5380. public static readonly MyOp Stloc_S = new MyOp(OpCodes.Stloc_S);
  5381. public static readonly MyOp Ldnull = new MyOp(OpCodes.Ldnull);
  5382. public static readonly MyOp Ldc_I4_M1 = new MyOp(OpCodes.Ldc_I4_M1);
  5383. public static readonly MyOp Ldc_I4_0 = new MyOp(OpCodes.Ldc_I4_0);
  5384. public static readonly MyOp Ldc_I4_1 = new MyOp(OpCodes.Ldc_I4_1);
  5385. public static readonly MyOp Ldc_I4_2 = new MyOp(OpCodes.Ldc_I4_2);
  5386. public static readonly MyOp Ldc_I4_3 = new MyOp(OpCodes.Ldc_I4_3);
  5387. public static readonly MyOp Ldc_I4_4 = new MyOp(OpCodes.Ldc_I4_4);
  5388. public static readonly MyOp Ldc_I4_5 = new MyOp(OpCodes.Ldc_I4_5);
  5389. public static readonly MyOp Ldc_I4_6 = new MyOp(OpCodes.Ldc_I4_6);
  5390. public static readonly MyOp Ldc_I4_7 = new MyOp(OpCodes.Ldc_I4_7);
  5391. public static readonly MyOp Ldc_I4_8 = new MyOp(OpCodes.Ldc_I4_8);
  5392. public static readonly MyOp Ldc_I4_S = new MyOp(OpCodes.Ldc_I4_S);
  5393. public static readonly MyOp Ldc_I4 = new MyOp(OpCodes.Ldc_I4);
  5394. public static readonly MyOp Ldc_I8 = new MyOp(OpCodes.Ldc_I8);
  5395. public static readonly MyOp Ldc_R4 = new MyOp(OpCodes.Ldc_R4);
  5396. public static readonly MyOp Ldc_R8 = new MyOp(OpCodes.Ldc_R8);
  5397. public static readonly MyOp Dup = new MyOp(OpCodes.Dup);
  5398. public static readonly MyOp Pop = new MyOp(OpCodes.Pop);
  5399. public static readonly MyOp Jmp = new MyOp(OpCodes.Jmp);
  5400. public static readonly MyOp Call = new MyOp(OpCodes.Call);
  5401. public static readonly MyOp Calli = new MyOp(OpCodes.Calli);
  5402. public static readonly MyOp Ret = new MyOp(OpCodes.Ret);
  5403. public static readonly MyOp Br_S = new MyOp(OpCodes.Br_S);
  5404. public static readonly MyOp Brfalse_S = new MyOp(OpCodes.Brfalse_S);
  5405. public static readonly MyOp Brtrue_S = new MyOp(OpCodes.Brtrue_S);
  5406. public static readonly MyOp Beq_S = new MyOp(OpCodes.Beq_S, "==");
  5407. public static readonly MyOp Bge_S = new MyOp(OpCodes.Bge_S, ">=");
  5408. public static readonly MyOp Bgt_S = new MyOp(OpCodes.Bgt_S, ">");
  5409. public static readonly MyOp Ble_S = new MyOp(OpCodes.Ble_S, "<=");
  5410. public static readonly MyOp Blt_S = new MyOp(OpCodes.Blt_S, "<");
  5411. public static readonly MyOp Bne_Un_S = new MyOp(OpCodes.Bne_Un_S, "!=");
  5412. public static readonly MyOp Bge_Un_S = new MyOp(OpCodes.Bge_Un_S);
  5413. public static readonly MyOp Bgt_Un_S = new MyOp(OpCodes.Bgt_Un_S);
  5414. public static readonly MyOp Ble_Un_S = new MyOp(OpCodes.Ble_Un_S);
  5415. public static readonly MyOp Blt_Un_S = new MyOp(OpCodes.Blt_Un_S);
  5416. public static readonly MyOp Br = new MyOp(OpCodes.Br);
  5417. public static readonly MyOp Brfalse = new MyOp(OpCodes.Brfalse, "!");
  5418. public static readonly MyOp Brtrue = new MyOp(OpCodes.Brtrue, "!!");
  5419. public static readonly MyOp Beq = new MyOp(OpCodes.Beq, "==");
  5420. public static readonly MyOp Bge = new MyOp(OpCodes.Bge, ">=");
  5421. public static readonly MyOp Bgt = new MyOp(OpCodes.Bgt, ">");
  5422. public static readonly MyOp Ble = new MyOp(OpCodes.Ble, "<=");
  5423. public static readonly MyOp Blt = new MyOp(OpCodes.Blt, "<");
  5424. public static readonly MyOp Bne_Un = new MyOp(OpCodes.Bne_Un, "!=");
  5425. public static readonly MyOp Bge_Un = new MyOp(OpCodes.Bge_Un);
  5426. public static readonly MyOp Bgt_Un = new MyOp(OpCodes.Bgt_Un);
  5427. public static readonly MyOp Ble_Un = new MyOp(OpCodes.Ble_Un);
  5428. public static readonly MyOp Blt_Un = new MyOp(OpCodes.Blt_Un);
  5429. public static readonly MyOp Switch = new MyOp(OpCodes.Switch);
  5430. public static readonly MyOp Ldind_I1 = new MyOp(OpCodes.Ldind_I1);
  5431. public static readonly MyOp Ldind_U1 = new MyOp(OpCodes.Ldind_U1);
  5432. public static readonly MyOp Ldind_I2 = new MyOp(OpCodes.Ldind_I2);
  5433. public static readonly MyOp Ldind_U2 = new MyOp(OpCodes.Ldind_U2);
  5434. public static readonly MyOp Ldind_I4 = new MyOp(OpCodes.Ldind_I4);
  5435. public static readonly MyOp Ldind_U4 = new MyOp(OpCodes.Ldind_U4);
  5436. public static readonly MyOp Ldind_I8 = new MyOp(OpCodes.Ldind_I8);
  5437. public static readonly MyOp Ldind_I = new MyOp(OpCodes.Ldind_I);
  5438. public static readonly MyOp Ldind_R4 = new MyOp(OpCodes.Ldind_R4);
  5439. public static readonly MyOp Ldind_R8 = new MyOp(OpCodes.Ldind_R8);
  5440. public static readonly MyOp Ldind_Ref = new MyOp(OpCodes.Ldind_Ref);
  5441. public static readonly MyOp Stind_Ref = new MyOp(OpCodes.Stind_Ref);
  5442. public static readonly MyOp Stind_I1 = new MyOp(OpCodes.Stind_I1);
  5443. public static readonly MyOp Stind_I2 = new MyOp(OpCodes.Stind_I2);
  5444. public static readonly MyOp Stind_I4 = new MyOp(OpCodes.Stind_I4);
  5445. public static readonly MyOp Stind_I8 = new MyOp(OpCodes.Stind_I8);
  5446. public static readonly MyOp Stind_R4 = new MyOp(OpCodes.Stind_R4);
  5447. public static readonly MyOp Stind_R8 = new MyOp(OpCodes.Stind_R8);
  5448. public static readonly MyOp Add = new MyOp(OpCodes.Add, "+");
  5449. public static readonly MyOp Sub = new MyOp(OpCodes.Sub, "-");
  5450. public static readonly MyOp Mul = new MyOp(OpCodes.Mul, "*");
  5451. public static readonly MyOp Div = new MyOp(OpCodes.Div, "/");
  5452. public static readonly MyOp Div_Un = new MyOp(OpCodes.Div_Un);
  5453. public static readonly MyOp Rem = new MyOp(OpCodes.Rem, "%");
  5454. public static readonly MyOp Rem_Un = new MyOp(OpCodes.Rem_Un);
  5455. public static readonly MyOp And = new MyOp(OpCodes.And, "&");
  5456. public static readonly MyOp Or = new MyOp(OpCodes.Or, "|");
  5457. public static readonly MyOp Xor = new MyOp(OpCodes.Xor, "^");
  5458. public static readonly MyOp Shl = new MyOp(OpCodes.Shl, "<<");
  5459. public static readonly MyOp Shr = new MyOp(OpCodes.Shr, ">>");
  5460. public static readonly MyOp Shr_Un = new MyOp(OpCodes.Shr_Un);
  5461. public static readonly MyOp Neg = new MyOp(OpCodes.Neg, "-");
  5462. public static readonly MyOp Not = new MyOp(OpCodes.Not, "~");
  5463. public static readonly MyOp Conv_I1 = new MyOp(OpCodes.Conv_I1);
  5464. public static readonly MyOp Conv_I2 = new MyOp(OpCodes.Conv_I2);
  5465. public static readonly MyOp Conv_I4 = new MyOp(OpCodes.Conv_I4);
  5466. public static readonly MyOp Conv_I8 = new MyOp(OpCodes.Conv_I8);
  5467. public static readonly MyOp Conv_R4 = new MyOp(OpCodes.Conv_R4);
  5468. public static readonly MyOp Conv_R8 = new MyOp(OpCodes.Conv_R8);
  5469. public static readonly MyOp Conv_U4 = new MyOp(OpCodes.Conv_U4);
  5470. public static readonly MyOp Conv_U8 = new MyOp(OpCodes.Conv_U8);
  5471. public static readonly MyOp Callvirt = new MyOp(OpCodes.Callvirt);
  5472. public static readonly MyOp Cpobj = new MyOp(OpCodes.Cpobj);
  5473. public static readonly MyOp Ldobj = new MyOp(OpCodes.Ldobj);
  5474. public static readonly MyOp Ldstr = new MyOp(OpCodes.Ldstr);
  5475. public static readonly MyOp Newobj = new MyOp(OpCodes.Newobj);
  5476. public static readonly MyOp Castclass = new MyOp(OpCodes.Castclass);
  5477. public static readonly MyOp Isinst = new MyOp(OpCodes.Isinst);
  5478. public static readonly MyOp Conv_R_Un = new MyOp(OpCodes.Conv_R_Un);
  5479. public static readonly MyOp Unbox = new MyOp(OpCodes.Unbox);
  5480. public static readonly MyOp Throw = new MyOp(OpCodes.Throw);
  5481. public static readonly MyOp Ldfld = new MyOp(OpCodes.Ldfld);
  5482. public static readonly MyOp Ldflda = new MyOp(OpCodes.Ldflda);
  5483. public static readonly MyOp Stfld = new MyOp(OpCodes.Stfld);
  5484. public static readonly MyOp Ldsfld = new MyOp(OpCodes.Ldsfld);
  5485. public static readonly MyOp Ldsflda = new MyOp(OpCodes.Ldsflda);
  5486. public static readonly MyOp Stsfld = new MyOp(OpCodes.Stsfld);
  5487. public static readonly MyOp Stobj = new MyOp(OpCodes.Stobj);
  5488. public static readonly MyOp Conv_Ovf_I1_Un = new MyOp(OpCodes.Conv_Ovf_I1_Un);
  5489. public static readonly MyOp Conv_Ovf_I2_Un = new MyOp(OpCodes.Conv_Ovf_I2_Un);
  5490. public static readonly MyOp Conv_Ovf_I4_Un = new MyOp(OpCodes.Conv_Ovf_I4_Un);
  5491. public static readonly MyOp Conv_Ovf_I8_Un = new MyOp(OpCodes.Conv_Ovf_I8_Un);
  5492. public static readonly MyOp Conv_Ovf_U1_Un = new MyOp(OpCodes.Conv_Ovf_U1_Un);
  5493. public static readonly MyOp Conv_Ovf_U2_Un = new MyOp(OpCodes.Conv_Ovf_U2_Un);
  5494. public static readonly MyOp Conv_Ovf_U4_Un = new MyOp(OpCodes.Conv_Ovf_U4_Un);
  5495. public static readonly MyOp Conv_Ovf_U8_Un = new MyOp(OpCodes.Conv_Ovf_U8_Un);
  5496. public static readonly MyOp Conv_Ovf_I_Un = new MyOp(OpCodes.Conv_Ovf_I_Un);
  5497. public static readonly MyOp Conv_Ovf_U_Un = new MyOp(OpCodes.Conv_Ovf_U_Un);
  5498. public static readonly MyOp Box = new MyOp(OpCodes.Box);
  5499. public static readonly MyOp Newarr = new MyOp(OpCodes.Newarr);
  5500. public static readonly MyOp Ldlen = new MyOp(OpCodes.Ldlen);
  5501. public static readonly MyOp Ldelema = new MyOp(OpCodes.Ldelema);
  5502. public static readonly MyOp Ldelem_I1 = new MyOp(OpCodes.Ldelem_I1);
  5503. public static readonly MyOp Ldelem_U1 = new MyOp(OpCodes.Ldelem_U1);
  5504. public static readonly MyOp Ldelem_I2 = new MyOp(OpCodes.Ldelem_I2);
  5505. public static readonly MyOp Ldelem_U2 = new MyOp(OpCodes.Ldelem_U2);
  5506. public static readonly MyOp Ldelem_I4 = new MyOp(OpCodes.Ldelem_I4);
  5507. public static readonly MyOp Ldelem_U4 = new MyOp(OpCodes.Ldelem_U4);
  5508. public static readonly MyOp Ldelem_I8 = new MyOp(OpCodes.Ldelem_I8);
  5509. public static readonly MyOp Ldelem_I = new MyOp(OpCodes.Ldelem_I);
  5510. public static readonly MyOp Ldelem_R4 = new MyOp(OpCodes.Ldelem_R4);
  5511. public static readonly MyOp Ldelem_R8 = new MyOp(OpCodes.Ldelem_R8);
  5512. public static readonly MyOp Ldelem_Ref = new MyOp(OpCodes.Ldelem_Ref);
  5513. public static readonly MyOp Stelem_I = new MyOp(OpCodes.Stelem_I);
  5514. public static readonly MyOp Stelem_I1 = new MyOp(OpCodes.Stelem_I1);
  5515. public static readonly MyOp Stelem_I2 = new MyOp(OpCodes.Stelem_I2);
  5516. public static readonly MyOp Stelem_I4 = new MyOp(OpCodes.Stelem_I4);
  5517. public static readonly MyOp Stelem_I8 = new MyOp(OpCodes.Stelem_I8);
  5518. public static readonly MyOp Stelem_R4 = new MyOp(OpCodes.Stelem_R4);
  5519. public static readonly MyOp Stelem_R8 = new MyOp(OpCodes.Stelem_R8);
  5520. public static readonly MyOp Stelem_Ref = new MyOp(OpCodes.Stelem_Ref);
  5521. public static readonly MyOp Ldelem = new MyOp(OpCodes.Ldelem);
  5522. public static readonly MyOp Stelem = new MyOp(OpCodes.Stelem);
  5523. public static readonly MyOp Unbox_Any = new MyOp(OpCodes.Unbox_Any);
  5524. public static readonly MyOp Conv_Ovf_I1 = new MyOp(OpCodes.Conv_Ovf_I1);
  5525. public static readonly MyOp Conv_Ovf_U1 = new MyOp(OpCodes.Conv_Ovf_U1);
  5526. public static readonly MyOp Conv_Ovf_I2 = new MyOp(OpCodes.Conv_Ovf_I2);
  5527. public static readonly MyOp Conv_Ovf_U2 = new MyOp(OpCodes.Conv_Ovf_U2);
  5528. public static readonly MyOp Conv_Ovf_I4 = new MyOp(OpCodes.Conv_Ovf_I4);
  5529. public static readonly MyOp Conv_Ovf_U4 = new MyOp(OpCodes.Conv_Ovf_U4);
  5530. public static readonly MyOp Conv_Ovf_I8 = new MyOp(OpCodes.Conv_Ovf_I8);
  5531. public static readonly MyOp Conv_Ovf_U8 = new MyOp(OpCodes.Conv_Ovf_U8);
  5532. public static readonly MyOp Refanyval = new MyOp(OpCodes.Refanyval);
  5533. public static readonly MyOp Ckfinite = new MyOp(OpCodes.Ckfinite);
  5534. public static readonly MyOp Mkrefany = new MyOp(OpCodes.Mkrefany);
  5535. public static readonly MyOp Ldtoken = new MyOp(OpCodes.Ldtoken);
  5536. public static readonly MyOp Conv_U2 = new MyOp(OpCodes.Conv_U2);
  5537. public static readonly MyOp Conv_U1 = new MyOp(OpCodes.Conv_U1);
  5538. public static readonly MyOp Conv_I = new MyOp(OpCodes.Conv_I);
  5539. public static readonly MyOp Conv_Ovf_I = new MyOp(OpCodes.Conv_Ovf_I);
  5540. public static readonly MyOp Conv_Ovf_U = new MyOp(OpCodes.Conv_Ovf_U);
  5541. public static readonly MyOp Add_Ovf = new MyOp(OpCodes.Add_Ovf);
  5542. public static readonly MyOp Add_Ovf_Un = new MyOp(OpCodes.Add_Ovf_Un);
  5543. public static readonly MyOp Mul_Ovf = new MyOp(OpCodes.Mul_Ovf);
  5544. public static readonly MyOp Mul_Ovf_Un = new MyOp(OpCodes.Mul_Ovf_Un);
  5545. public static readonly MyOp Sub_Ovf = new MyOp(OpCodes.Sub_Ovf);
  5546. public static readonly MyOp Sub_Ovf_Un = new MyOp(OpCodes.Sub_Ovf_Un);
  5547. public static readonly MyOp Endfinally = new MyOp(OpCodes.Endfinally);
  5548. public static readonly MyOp Leave = new MyOp(OpCodes.Leave);
  5549. public static readonly MyOp Leave_S = new MyOp(OpCodes.Leave_S);
  5550. public static readonly MyOp Stind_I = new MyOp(OpCodes.Stind_I);
  5551. public static readonly MyOp Conv_U = new MyOp(OpCodes.Conv_U);
  5552. public static readonly MyOp Prefix7 = new MyOp(OpCodes.Prefix7);
  5553. public static readonly MyOp Prefix6 = new MyOp(OpCodes.Prefix6);
  5554. public static readonly MyOp Prefix5 = new MyOp(OpCodes.Prefix5);
  5555. public static readonly MyOp Prefix4 = new MyOp(OpCodes.Prefix4);
  5556. public static readonly MyOp Prefix3 = new MyOp(OpCodes.Prefix3);
  5557. public static readonly MyOp Prefix2 = new MyOp(OpCodes.Prefix2);
  5558. public static readonly MyOp Prefix1 = new MyOp(OpCodes.Prefix1);
  5559. public static readonly MyOp Prefixref = new MyOp(OpCodes.Prefixref);
  5560. public static readonly MyOp Arglist = new MyOp(OpCodes.Arglist);
  5561. public static readonly MyOp Ceq = new MyOp(OpCodes.Ceq, "==");
  5562. public static readonly MyOp Cgt = new MyOp(OpCodes.Cgt, ">");
  5563. public static readonly MyOp Cgt_Un = new MyOp(OpCodes.Cgt_Un);
  5564. public static readonly MyOp Clt = new MyOp(OpCodes.Clt, "<");
  5565. public static readonly MyOp Clt_Un = new MyOp(OpCodes.Clt_Un);
  5566. public static readonly MyOp Ldftn = new MyOp(OpCodes.Ldftn);
  5567. public static readonly MyOp Ldvirtftn = new MyOp(OpCodes.Ldvirtftn);
  5568. public static readonly MyOp Ldarg = new MyOp(OpCodes.Ldarg);
  5569. public static readonly MyOp Ldarga = new MyOp(OpCodes.Ldarga);
  5570. public static readonly MyOp Starg = new MyOp(OpCodes.Starg);
  5571. public static readonly MyOp Ldloc = new MyOp(OpCodes.Ldloc);
  5572. public static readonly MyOp Ldloca = new MyOp(OpCodes.Ldloca);
  5573. public static readonly MyOp Stloc = new MyOp(OpCodes.Stloc);
  5574. public static readonly MyOp Localloc = new MyOp(OpCodes.Localloc);
  5575. public static readonly MyOp Endfilter = new MyOp(OpCodes.Endfilter);
  5576. public static readonly MyOp Unaligned = new MyOp(OpCodes.Unaligned);
  5577. public static readonly MyOp Volatile = new MyOp(OpCodes.Volatile);
  5578. public static readonly MyOp Tailcall = new MyOp(OpCodes.Tailcall);
  5579. public static readonly MyOp Initobj = new MyOp(OpCodes.Initobj);
  5580. public static readonly MyOp Constrained = new MyOp(OpCodes.Constrained);
  5581. public static readonly MyOp Cpblk = new MyOp(OpCodes.Cpblk);
  5582. public static readonly MyOp Initblk = new MyOp(OpCodes.Initblk);
  5583. public static readonly MyOp Rethrow = new MyOp(OpCodes.Rethrow);
  5584. public static readonly MyOp Sizeof = new MyOp(OpCodes.Sizeof);
  5585. public static readonly MyOp Refanytype = new MyOp(OpCodes.Refanytype);
  5586. public static readonly MyOp Readonly = new MyOp(OpCodes.Readonly);
  5587. // used internally
  5588. public static readonly MyOp Cge = new MyOp("cge", ">=");
  5589. public static readonly MyOp Cle = new MyOp("cle", "<=");
  5590. public static readonly MyOp Cne = new MyOp("cne", "!=");
  5591. }
  5592. }
  5593. }