aasmcpu.pas 216 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. Contains the assembler object for the ARM
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,globals,verbose,
  22. aasmbase,aasmtai,aasmdata,aasmsym,
  23. ogbase,
  24. symtype,
  25. cpubase,cpuinfo,cgbase,cgutils,
  26. sysutils;
  27. const
  28. { "mov reg,reg" source operand number }
  29. O_MOV_SOURCE = 1;
  30. { "mov reg,reg" source operand number }
  31. O_MOV_DEST = 0;
  32. { Operand types }
  33. OT_NONE = $00000000;
  34. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  35. OT_BITS16 = $00000002;
  36. OT_BITS32 = $00000004;
  37. OT_BITS64 = $00000008; { FPU only }
  38. OT_BITS80 = $00000010;
  39. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  40. OT_NEAR = $00000040;
  41. OT_SHORT = $00000080;
  42. OT_BITSTINY = $00000100; { fpu constant }
  43. OT_BITSSHIFTER =
  44. $00000200;
  45. OT_SIZE_MASK = $000003FF; { all the size attributes }
  46. OT_NON_SIZE = $0FFFF800;
  47. OT_OPT_SIZE = $F0000000;
  48. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  49. OT_TO = $00000200; { operand is followed by a colon }
  50. { reverse effect in FADD, FSUB &c }
  51. OT_COLON = $00000400;
  52. OT_SHIFTEROP = $00000800;
  53. OT_REGISTER = $00001000;
  54. OT_IMMEDIATE = $00002000;
  55. OT_REGLIST = $00008000;
  56. OT_IMM8 = $00002001;
  57. OT_IMM24 = $00002002;
  58. OT_IMM32 = $00002004;
  59. OT_IMM64 = $00002008;
  60. OT_IMM80 = $00002010;
  61. OT_IMMTINY = $00002100;
  62. OT_IMMSHIFTER= $00002200;
  63. OT_IMMEDIATEZERO = $10002200;
  64. OT_IMMEDIATEMM = $00002400;
  65. OT_IMMEDIATE24 = OT_IMM24;
  66. OT_SHIFTIMM = OT_SHIFTEROP or OT_IMMSHIFTER;
  67. OT_SHIFTIMMEDIATE = OT_SHIFTIMM;
  68. OT_IMMEDIATESHIFTER = OT_IMMSHIFTER;
  69. OT_IMMEDIATEFPU = OT_IMMTINY;
  70. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  71. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  72. OT_REG8 = $00201001;
  73. OT_REG16 = $00201002;
  74. OT_REG32 = $00201004;
  75. OT_REGLO = $10201004; { lower reg (r0-r7) }
  76. OT_REGSP = $20201004;
  77. OT_REG64 = $00201008;
  78. OT_VREG = $00201010; { vector register }
  79. OT_REGF = $00201020; { coproc register }
  80. OT_REGS = $00201040; { special register with mask }
  81. OT_MEMORY = $00204000; { register number in 'basereg' }
  82. OT_MEM8 = $00204001;
  83. OT_MEM16 = $00204002;
  84. OT_MEM32 = $00204004;
  85. OT_MEM64 = $00204008;
  86. OT_MEM80 = $00204010;
  87. { word/byte load/store }
  88. OT_AM2 = $00010000;
  89. { misc ld/st operations, thumb reg indexed }
  90. OT_AM3 = $00020000;
  91. { multiple ld/st operations or thumb imm indexed }
  92. OT_AM4 = $00040000;
  93. { co proc. ld/st operations or thumb sp+imm indexed }
  94. OT_AM5 = $00080000;
  95. { exclusive ld/st operations or thumb pc+imm indexed }
  96. OT_AM6 = $00100000;
  97. OT_AMMASK = $001f0000;
  98. { IT instruction }
  99. OT_CONDITION = $00200000;
  100. OT_MODEFLAGS = $00400000;
  101. OT_MEMORYAM2 = OT_MEMORY or OT_AM2;
  102. OT_MEMORYAM3 = OT_MEMORY or OT_AM3;
  103. OT_MEMORYAM4 = OT_MEMORY or OT_AM4;
  104. OT_MEMORYAM5 = OT_MEMORY or OT_AM5;
  105. OT_MEMORYAM6 = OT_MEMORY or OT_AM6;
  106. OT_FPUREG = $01000000; { floating point stack registers }
  107. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  108. { a mask for the following }
  109. OT_MEM_OFFS = $00604000; { special type of EA }
  110. { simple [address] offset }
  111. OT_ONENESS = $00800000; { special type of immediate operand }
  112. { so UNITY == IMMEDIATE | ONENESS }
  113. OT_UNITY = $00802000; { for shift/rotate instructions }
  114. instabentries = {$i armnop.inc}
  115. maxinfolen = 5;
  116. IF_NONE = $00000000;
  117. IF_EXTENSIONS = $0000000F;
  118. IF_NEON = $00000001;
  119. IF_ARMMASK = $000F0000;
  120. IF_ARM32 = $00010000;
  121. IF_THUMB = $00020000;
  122. IF_THUMB32 = $00040000;
  123. IF_WIDE = $00080000;
  124. IF_ARMvMASK = $0FF00000;
  125. IF_ARMv4 = $00100000;
  126. IF_ARMv4T = $00200000;
  127. IF_ARMv5 = $00300000;
  128. IF_ARMv5T = $00400000;
  129. IF_ARMv5TE = $00500000;
  130. IF_ARMv5TEJ = $00600000;
  131. IF_ARMv6 = $00700000;
  132. IF_ARMv6K = $00800000;
  133. IF_ARMv6T2 = $00900000;
  134. IF_ARMv6Z = $00A00000;
  135. IF_ARMv6M = $00B00000;
  136. IF_ARMv7 = $00C00000;
  137. IF_ARMv7A = $00D00000;
  138. IF_ARMv7R = $00E00000;
  139. IF_ARMv7M = $00F00000;
  140. IF_ARMv7EM = $01000000;
  141. IF_FPMASK = $c0000F00;
  142. IF_FPA = $00000100;
  143. IF_VFPv2 = $00000200;
  144. IF_VFPv3 = $00000400;
  145. IF_VFPv4 = $00000800;
  146. IF_VFPv5 = $80000000;
  147. { if the instruction can change in a second pass }
  148. IF_PASS2 = $80000000;
  149. type
  150. TInsTabCache=array[TasmOp] of longint;
  151. PInsTabCache=^TInsTabCache;
  152. tinsentry = record
  153. opcode : tasmop;
  154. ops : byte;
  155. optypes : array[0..5] of longint;
  156. code : array[0..maxinfolen] of char;
  157. flags : longword;
  158. end;
  159. pinsentry=^tinsentry;
  160. taicpuflag = (cf_wideformat,cf_inIT,cf_lastinIT,cf_thumb);
  161. taicpuflags = set of taicpuflag;
  162. const
  163. InsTab : array[0..instabentries-1] of TInsEntry={$i armtab.inc}
  164. var
  165. InsTabCache : PInsTabCache;
  166. type
  167. taicpu = class(tai_cpu_abstract_sym)
  168. oppostfix : TOpPostfix;
  169. roundingmode : troundingmode;
  170. flags : taicpuflags;
  171. procedure loadshifterop(opidx:longint;const so:tshifterop);
  172. procedure loadregset(opidx:longint; regsetregtype: tregistertype; regsetsubregtype: tsubregister; const s:tcpuregisterset; ausermode: boolean=false);
  173. procedure loadconditioncode(opidx:longint;const acond:tasmcond);
  174. procedure loadmodeflags(opidx:longint;const _modeflags:tcpumodeflags);
  175. procedure loadspecialreg(opidx:longint;const areg:tregister; const aflags:tspecialregflags);
  176. procedure loadrealconst(opidx:longint;const _value:bestreal);
  177. constructor op_none(op : tasmop);
  178. constructor op_reg(op : tasmop;_op1 : tregister);
  179. constructor op_ref(op : tasmop;const _op1 : treference);
  180. constructor op_const(op : tasmop;_op1 : longint);
  181. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  182. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  183. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  184. constructor op_regset(op:tasmop; regtype: tregistertype; subreg: tsubregister; _op1: tcpuregisterset);
  185. constructor op_ref_regset(op:tasmop; _op1: treference; regtype: tregistertype; subreg: tsubregister; _op2: tcpuregisterset);
  186. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  187. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  188. constructor op_reg_const_const(op : tasmop;_op1 : tregister; _op2,_op3: aint);
  189. constructor op_reg_reg_const_const(op : tasmop;_op1,_op2 : tregister; _op3,_op4: aint);
  190. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  191. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  192. constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  193. constructor op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  194. { SFM/LFM }
  195. constructor op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  196. { ITxxx }
  197. constructor op_cond(op: tasmop; cond: tasmcond);
  198. { CPSxx }
  199. constructor op_modeflags(op: tasmop; _modeflags: tcpumodeflags);
  200. constructor op_modeflags_const(op: tasmop; _modeflags: tcpumodeflags; a: aint);
  201. { MSR }
  202. constructor op_specialreg_reg(op: tasmop; specialreg: tregister; specialregflags: tspecialregflags; _op2: tregister);
  203. { *M*LL }
  204. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  205. constructor op_reg_realconst(op : tasmop;_op1: tregister;_op2: bestreal);
  206. { this is for Jmp instructions }
  207. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  208. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  209. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  210. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  211. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  212. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  213. function spilling_get_operation_type(opnr: longint): topertype;override;
  214. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;override;
  215. { assembler }
  216. public
  217. { the next will reset all instructions that can change in pass 2 }
  218. procedure ResetPass1;override;
  219. procedure ResetPass2;override;
  220. function CheckIfValid:boolean;
  221. function GetString:string;
  222. function Pass1(objdata:TObjData):longint;override;
  223. procedure Pass2(objdata:TObjData);override;
  224. protected
  225. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  226. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  227. procedure ppubuildderefimploper(var o:toper);override;
  228. procedure ppuderefoper(var o:toper);override;
  229. private
  230. { arm version info }
  231. fArmVMask,
  232. fArmMask : longword;
  233. { next fields are filled in pass1, so pass2 is faster }
  234. inssize : shortint;
  235. insoffset : longint;
  236. LastInsOffset : longint; { need to be public to be reset }
  237. insentry : PInsEntry;
  238. procedure BuildArmMasks(objdata:TObjData);
  239. function InsEnd:longint;
  240. procedure create_ot(objdata:TObjData);
  241. function Matches(p:PInsEntry):longint;
  242. function calcsize(p:PInsEntry):shortint;
  243. procedure gencode(objdata:TObjData);
  244. function NeedAddrPrefix(opidx:byte):boolean;
  245. procedure Swapoperands;
  246. function FindInsentry(objdata:TObjData):boolean;
  247. end;
  248. tai_align = class(tai_align_abstract)
  249. { nothing to add }
  250. end;
  251. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  252. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  253. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  254. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  255. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  256. { inserts pc relative symbols at places where they are reachable
  257. and transforms special instructions to valid instruction encodings }
  258. procedure finalizearmcode(list,listtoinsert : TAsmList);
  259. { inserts .pdata section and dummy function prolog needed for arm-wince exception handling }
  260. procedure InsertPData;
  261. procedure InitAsm;
  262. procedure DoneAsm;
  263. implementation
  264. uses
  265. itcpugas,aoptcpu,
  266. systems,symdef;
  267. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  268. begin
  269. allocate_oper(opidx+1);
  270. with oper[opidx]^ do
  271. begin
  272. if typ<>top_shifterop then
  273. begin
  274. clearop(opidx);
  275. new(shifterop);
  276. end;
  277. shifterop^:=so;
  278. typ:=top_shifterop;
  279. if assigned(add_reg_instruction_hook) then
  280. add_reg_instruction_hook(self,shifterop^.rs);
  281. end;
  282. end;
  283. procedure taicpu.loadrealconst(opidx:longint;const _value:bestreal);
  284. begin
  285. allocate_oper(opidx+1);
  286. with oper[opidx]^ do
  287. begin
  288. if typ<>top_realconst then
  289. clearop(opidx);
  290. val_real:=_value;
  291. typ:=top_realconst;
  292. end;
  293. end;
  294. procedure taicpu.loadregset(opidx:longint; regsetregtype: tregistertype; regsetsubregtype: tsubregister; const s:tcpuregisterset; ausermode: boolean);
  295. var
  296. i : byte;
  297. begin
  298. allocate_oper(opidx+1);
  299. with oper[opidx]^ do
  300. begin
  301. if typ<>top_regset then
  302. begin
  303. clearop(opidx);
  304. new(regset);
  305. end;
  306. regset^:=s;
  307. regtyp:=regsetregtype;
  308. subreg:=regsetsubregtype;
  309. usermode:=ausermode;
  310. typ:=top_regset;
  311. case regsetregtype of
  312. R_INTREGISTER:
  313. for i:=RS_R0 to RS_R15 do
  314. begin
  315. if assigned(add_reg_instruction_hook) and (i in regset^) then
  316. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,regsetsubregtype));
  317. end;
  318. R_MMREGISTER:
  319. { both RS_S0 and RS_D0 range from 0 to 31 }
  320. for i:=RS_D0 to RS_D31 do
  321. begin
  322. if assigned(add_reg_instruction_hook) and (i in regset^) then
  323. add_reg_instruction_hook(self,newreg(R_MMREGISTER,i,regsetsubregtype));
  324. end;
  325. else
  326. internalerror(2019050932);
  327. end;
  328. end;
  329. end;
  330. procedure taicpu.loadconditioncode(opidx:longint;const acond:tasmcond);
  331. begin
  332. allocate_oper(opidx+1);
  333. with oper[opidx]^ do
  334. begin
  335. if typ<>top_conditioncode then
  336. clearop(opidx);
  337. cc:=acond;
  338. typ:=top_conditioncode;
  339. end;
  340. end;
  341. procedure taicpu.loadmodeflags(opidx: longint; const _modeflags: tcpumodeflags);
  342. begin
  343. allocate_oper(opidx+1);
  344. with oper[opidx]^ do
  345. begin
  346. if typ<>top_modeflags then
  347. clearop(opidx);
  348. modeflags:=_modeflags;
  349. typ:=top_modeflags;
  350. end;
  351. end;
  352. procedure taicpu.loadspecialreg(opidx: longint; const areg: tregister; const aflags: tspecialregflags);
  353. begin
  354. allocate_oper(opidx+1);
  355. with oper[opidx]^ do
  356. begin
  357. if typ<>top_specialreg then
  358. clearop(opidx);
  359. specialreg:=areg;
  360. specialflags:=aflags;
  361. typ:=top_specialreg;
  362. end;
  363. end;
  364. {*****************************************************************************
  365. taicpu Constructors
  366. *****************************************************************************}
  367. constructor taicpu.op_none(op : tasmop);
  368. begin
  369. inherited create(op);
  370. end;
  371. { for pld }
  372. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  373. begin
  374. inherited create(op);
  375. ops:=1;
  376. loadref(0,_op1);
  377. end;
  378. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  379. begin
  380. inherited create(op);
  381. ops:=1;
  382. loadreg(0,_op1);
  383. end;
  384. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  385. begin
  386. inherited create(op);
  387. ops:=1;
  388. loadconst(0,aint(_op1));
  389. end;
  390. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  391. begin
  392. inherited create(op);
  393. ops:=2;
  394. loadreg(0,_op1);
  395. loadreg(1,_op2);
  396. end;
  397. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  398. begin
  399. inherited create(op);
  400. ops:=2;
  401. loadreg(0,_op1);
  402. loadconst(1,aint(_op2));
  403. end;
  404. constructor taicpu.op_regset(op: tasmop; regtype: tregistertype; subreg: tsubregister; _op1: tcpuregisterset);
  405. begin
  406. inherited create(op);
  407. ops:=1;
  408. loadregset(0,regtype,subreg,_op1);
  409. end;
  410. constructor taicpu.op_ref_regset(op:tasmop; _op1: treference; regtype: tregistertype; subreg: tsubregister; _op2: tcpuregisterset);
  411. begin
  412. inherited create(op);
  413. ops:=2;
  414. loadref(0,_op1);
  415. loadregset(1,regtype,subreg,_op2);
  416. end;
  417. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  418. begin
  419. inherited create(op);
  420. ops:=2;
  421. loadreg(0,_op1);
  422. loadref(1,_op2);
  423. end;
  424. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  425. begin
  426. inherited create(op);
  427. ops:=3;
  428. loadreg(0,_op1);
  429. loadreg(1,_op2);
  430. loadreg(2,_op3);
  431. end;
  432. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  433. begin
  434. inherited create(op);
  435. ops:=4;
  436. loadreg(0,_op1);
  437. loadreg(1,_op2);
  438. loadreg(2,_op3);
  439. loadreg(3,_op4);
  440. end;
  441. constructor taicpu.op_reg_realconst(op : tasmop; _op1 : tregister; _op2 : bestreal);
  442. begin
  443. inherited create(op);
  444. ops:=2;
  445. loadreg(0,_op1);
  446. loadrealconst(1,_op2);
  447. end;
  448. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  449. begin
  450. inherited create(op);
  451. ops:=3;
  452. loadreg(0,_op1);
  453. loadreg(1,_op2);
  454. loadconst(2,aint(_op3));
  455. end;
  456. constructor taicpu.op_reg_const_const(op : tasmop;_op1 : tregister; _op2,_op3: aint);
  457. begin
  458. inherited create(op);
  459. ops:=3;
  460. loadreg(0,_op1);
  461. loadconst(1,aint(_op2));
  462. loadconst(2,aint(_op3));
  463. end;
  464. constructor taicpu.op_reg_reg_const_const(op: tasmop; _op1, _op2: tregister; _op3, _op4: aint);
  465. begin
  466. inherited create(op);
  467. ops:=4;
  468. loadreg(0,_op1);
  469. loadreg(1,_op2);
  470. loadconst(2,aint(_op3));
  471. loadconst(3,aint(_op4));
  472. end;
  473. constructor taicpu.op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  474. begin
  475. inherited create(op);
  476. ops:=3;
  477. loadreg(0,_op1);
  478. loadconst(1,_op2);
  479. loadref(2,_op3);
  480. end;
  481. constructor taicpu.op_cond(op: tasmop; cond: tasmcond);
  482. begin
  483. inherited create(op);
  484. ops:=1;
  485. loadconditioncode(0, cond);
  486. end;
  487. constructor taicpu.op_modeflags(op: tasmop; _modeflags: tcpumodeflags);
  488. begin
  489. inherited create(op);
  490. ops := 1;
  491. loadmodeflags(0,_modeflags);
  492. end;
  493. constructor taicpu.op_modeflags_const(op: tasmop; _modeflags: tcpumodeflags; a: aint);
  494. begin
  495. inherited create(op);
  496. ops := 2;
  497. loadmodeflags(0,_modeflags);
  498. loadconst(1,a);
  499. end;
  500. constructor taicpu.op_specialreg_reg(op: tasmop; specialreg: tregister; specialregflags: tspecialregflags; _op2: tregister);
  501. begin
  502. inherited create(op);
  503. ops:=2;
  504. loadspecialreg(0,specialreg,specialregflags);
  505. loadreg(1,_op2);
  506. end;
  507. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  508. begin
  509. inherited create(op);
  510. ops:=3;
  511. loadreg(0,_op1);
  512. loadreg(1,_op2);
  513. loadsymbol(0,_op3,_op3ofs);
  514. end;
  515. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  516. begin
  517. inherited create(op);
  518. ops:=3;
  519. loadreg(0,_op1);
  520. loadreg(1,_op2);
  521. loadref(2,_op3);
  522. end;
  523. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  524. begin
  525. inherited create(op);
  526. ops:=3;
  527. loadreg(0,_op1);
  528. loadreg(1,_op2);
  529. loadshifterop(2,_op3);
  530. end;
  531. constructor taicpu.op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  532. begin
  533. inherited create(op);
  534. ops:=4;
  535. loadreg(0,_op1);
  536. loadreg(1,_op2);
  537. loadreg(2,_op3);
  538. loadshifterop(3,_op4);
  539. end;
  540. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  541. begin
  542. inherited create(op);
  543. condition:=cond;
  544. ops:=1;
  545. loadsymbol(0,_op1,0);
  546. end;
  547. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  548. begin
  549. inherited create(op);
  550. ops:=1;
  551. loadsymbol(0,_op1,0);
  552. end;
  553. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  554. begin
  555. inherited create(op);
  556. ops:=1;
  557. loadsymbol(0,_op1,_op1ofs);
  558. end;
  559. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  560. begin
  561. inherited create(op);
  562. ops:=2;
  563. loadreg(0,_op1);
  564. loadsymbol(1,_op2,_op2ofs);
  565. end;
  566. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  567. begin
  568. inherited create(op);
  569. ops:=2;
  570. loadsymbol(0,_op1,_op1ofs);
  571. loadref(1,_op2);
  572. end;
  573. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  574. begin
  575. { allow the register allocator to remove unnecessary moves }
  576. result:=(
  577. ((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  578. ((opcode=A_MVF) and (regtype = R_FPUREGISTER)) or
  579. ((opcode in [A_FCPYS, A_FCPYD]) and (regtype = R_MMREGISTER)) or
  580. ((opcode in [A_VMOV]) and (regtype = R_MMREGISTER) and (oppostfix in [PF_F32,PF_F64]))
  581. ) and
  582. ((oppostfix in [PF_None,PF_D]) or (opcode = A_VMOV)) and
  583. (condition=C_None) and
  584. (ops=2) and
  585. (oper[0]^.typ=top_reg) and
  586. (oper[1]^.typ=top_reg) and
  587. (oper[0]^.reg=oper[1]^.reg);
  588. end;
  589. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  590. begin
  591. case getregtype(r) of
  592. R_INTREGISTER :
  593. result:=taicpu.op_reg_ref(A_LDR,r,ref);
  594. R_FPUREGISTER :
  595. { use lfm because we don't know the current internal format
  596. and avoid exceptions
  597. }
  598. result:=taicpu.op_reg_const_ref(A_LFM,r,1,ref);
  599. R_MMREGISTER :
  600. result:=taicpu.op_reg_ref(A_VLDR,r,ref);
  601. else
  602. internalerror(2004010415);
  603. end;
  604. end;
  605. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  606. begin
  607. case getregtype(r) of
  608. R_INTREGISTER :
  609. result:=taicpu.op_reg_ref(A_STR,r,ref);
  610. R_FPUREGISTER :
  611. { use sfm because we don't know the current internal format
  612. and avoid exceptions
  613. }
  614. result:=taicpu.op_reg_const_ref(A_SFM,r,1,ref);
  615. R_MMREGISTER :
  616. result:=taicpu.op_reg_ref(A_VSTR,r,ref);
  617. else
  618. internalerror(2004010416);
  619. end;
  620. end;
  621. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  622. begin
  623. if GenerateThumbCode then
  624. case opcode of
  625. A_ADC,A_ADD,A_AND,A_BIC,
  626. A_EOR,A_CLZ,A_RBIT,
  627. A_LDR,A_LDRB,A_LDRBT,A_LDRH,A_LDRSB,
  628. A_LDRSH,A_LDRT,
  629. A_MOV,A_MVN,A_MLA,A_MUL,
  630. A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
  631. A_SWP,A_SWPB,
  632. A_LDF,A_FLT,A_FIX,
  633. A_ADF,A_DVF,A_FDV,A_FML,
  634. A_RFS,A_RFC,A_RDF,
  635. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  636. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
  637. A_LFM,
  638. A_FLDS,A_FLDD,
  639. A_FMRX,A_FMXR,A_FMSTAT,
  640. A_FMSR,A_FMRS,A_FMDRR,
  641. A_FCPYS,A_FCPYD,A_FCVTSD,A_FCVTDS,
  642. A_FABSS,A_FABSD,A_FSQRTS,A_FSQRTD,A_FMULS,A_FMULD,
  643. A_FADDS,A_FADDD,A_FSUBS,A_FSUBD,A_FDIVS,A_FDIVD,
  644. A_FMACS,A_FMACD,A_FMSCS,A_FMSCD,A_FNMACS,A_FNMACD,
  645. A_FNMSCS,A_FNMSCD,A_FNMULS,A_FNMULD,
  646. A_FMDHR,A_FMRDH,A_FMDLR,A_FMRDL,
  647. A_FNEGS,A_FNEGD,
  648. A_FSITOS,A_FSITOD,A_FTOSIS,A_FTOSID,
  649. A_FTOUIS,A_FTOUID,A_FUITOS,A_FUITOD,
  650. A_SXTB16,A_UXTB16,
  651. A_UXTB,A_UXTH,A_SXTB,A_SXTH,
  652. A_NEG,
  653. A_VABS,A_VADD,A_VCVT,A_VDIV,A_VLDR,A_VMOV,A_VMUL,A_VNEG,A_VSQRT,A_VSUB,
  654. A_MRS,A_MSR:
  655. if opnr=0 then
  656. result:=operand_readwrite
  657. else
  658. result:=operand_read;
  659. A_BKPT,A_B,A_BL,A_BLX,A_BX,
  660. A_CMN,A_CMP,A_TEQ,A_TST,
  661. A_CMF,A_CMFE,A_WFS,A_CNF,
  662. A_FCMPS,A_FCMPD,A_FCMPES,A_FCMPED,A_FCMPEZS,A_FCMPEZD,
  663. A_FCMPZS,A_FCMPZD,
  664. A_VCMP,A_VCMPE:
  665. result:=operand_read;
  666. A_SMLAL,A_UMLAL:
  667. if opnr in [0,1] then
  668. result:=operand_readwrite
  669. else
  670. result:=operand_read;
  671. A_SMULL,A_UMULL,
  672. A_FMRRD:
  673. if opnr in [0,1] then
  674. result:=operand_readwrite
  675. else
  676. result:=operand_read;
  677. A_STR,A_STRB,A_STRBT,
  678. A_STRH,A_STRT,A_STF,A_SFM,
  679. A_FSTS,A_FSTD,
  680. A_VSTR:
  681. { important is what happens with the involved registers }
  682. if opnr=0 then
  683. result := operand_read
  684. else
  685. { check for pre/post indexed }
  686. result := operand_read;
  687. //Thumb2
  688. A_LSL, A_LSR, A_ROR, A_ASR, A_SDIV, A_UDIV, A_MOVW, A_MOVT, A_MLS, A_BFI,
  689. A_SMMLA,A_SMMLS:
  690. if opnr in [0] then
  691. result:=operand_readwrite
  692. else
  693. result:=operand_read;
  694. A_BFC:
  695. if opnr in [0] then
  696. result:=operand_readwrite
  697. else
  698. result:=operand_read;
  699. A_LDREX:
  700. if opnr in [0] then
  701. result:=operand_readwrite
  702. else
  703. result:=operand_read;
  704. A_STREX:
  705. result:=operand_write;
  706. else
  707. internalerror(200403151);
  708. end
  709. else
  710. case opcode of
  711. A_ADC,A_ADD,A_AND,A_BIC,A_ORN,
  712. A_EOR,A_CLZ,A_RBIT,
  713. A_LDR,A_LDRB,A_LDRBT,A_LDRH,A_LDRSB,
  714. A_LDRSH,A_LDRT,
  715. A_MOV,A_MVN,A_MLA,A_MUL,
  716. A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
  717. A_SWP,A_SWPB,
  718. A_LDF,A_FLT,A_FIX,
  719. A_ADF,A_DVF,A_FDV,A_FML,
  720. A_RFS,A_RFC,A_RDF,
  721. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  722. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
  723. A_LFM,
  724. A_FLDS,A_FLDD,
  725. A_FMRX,A_FMXR,A_FMSTAT,
  726. A_FMSR,A_FMRS,A_FMDRR,
  727. A_FCPYS,A_FCPYD,A_FCVTSD,A_FCVTDS,
  728. A_FABSS,A_FABSD,A_FSQRTS,A_FSQRTD,A_FMULS,A_FMULD,
  729. A_FADDS,A_FADDD,A_FSUBS,A_FSUBD,A_FDIVS,A_FDIVD,
  730. A_FMACS,A_FMACD,A_FMSCS,A_FMSCD,A_FNMACS,A_FNMACD,
  731. A_FNMSCS,A_FNMSCD,A_FNMULS,A_FNMULD,
  732. A_FMDHR,A_FMRDH,A_FMDLR,A_FMRDL,
  733. A_FNEGS,A_FNEGD,
  734. A_FSITOS,A_FSITOD,A_FTOSIS,A_FTOSID,
  735. A_FTOUIS,A_FTOUID,A_FUITOS,A_FUITOD,
  736. A_SXTB16,A_UXTB16,
  737. A_UXTB,A_UXTH,A_SXTB,A_SXTH,
  738. A_NEG,
  739. A_VABS,A_VADD,A_VCVT,A_VDIV,A_VLDR,A_VMOV,A_VMUL,A_VNEG,A_VSQRT,A_VSUB,
  740. A_VEOR,
  741. A_VMRS,A_VMSR,
  742. A_MRS,A_MSR:
  743. if opnr=0 then
  744. result:=operand_write
  745. else
  746. result:=operand_read;
  747. A_BKPT,A_B,A_BL,A_BLX,A_BX,
  748. A_CMN,A_CMP,A_TEQ,A_TST,
  749. A_CMF,A_CMFE,A_WFS,A_CNF,
  750. A_FCMPS,A_FCMPD,A_FCMPES,A_FCMPED,A_FCMPEZS,A_FCMPEZD,
  751. A_FCMPZS,A_FCMPZD,
  752. A_VCMP,A_VCMPE:
  753. result:=operand_read;
  754. A_SMLAL,A_UMLAL:
  755. if opnr in [0,1] then
  756. result:=operand_readwrite
  757. else
  758. result:=operand_read;
  759. A_SMULL,A_UMULL,
  760. A_FMRRD:
  761. if opnr in [0,1] then
  762. result:=operand_write
  763. else
  764. result:=operand_read;
  765. A_STR,A_STRB,A_STRBT,
  766. A_STRH,A_STRT,A_STF,A_SFM,
  767. A_FSTS,A_FSTD,
  768. A_VSTR:
  769. { important is what happens with the involved registers }
  770. if opnr=0 then
  771. result := operand_read
  772. else
  773. { check for pre/post indexed }
  774. result := operand_read;
  775. //Thumb2
  776. A_LSL, A_LSR, A_ROR, A_ASR, A_SDIV, A_UDIV, A_MOVW, A_MOVT, A_MLS, A_BFI,
  777. A_QADD,
  778. A_PKHTB,A_PKHBT,
  779. A_SMMLA,A_SMMLS,A_SMUAD,A_SMUSD:
  780. if opnr in [0] then
  781. result:=operand_write
  782. else
  783. result:=operand_read;
  784. A_VFMA,A_VFMS,A_VFNMA,A_VFNMS,
  785. A_BFC:
  786. if opnr in [0] then
  787. result:=operand_readwrite
  788. else
  789. result:=operand_read;
  790. A_LDREX:
  791. if opnr in [0] then
  792. result:=operand_write
  793. else
  794. result:=operand_read;
  795. A_STREX:
  796. result:=operand_write;
  797. else
  798. begin
  799. writeln(opcode);
  800. internalerror(2004031502);
  801. end;
  802. end;
  803. end;
  804. function taicpu.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  805. begin
  806. result := operand_read;
  807. if (oper[opnr]^.ref^.base = reg) and
  808. (oper[opnr]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) then
  809. result := operand_readwrite;
  810. end;
  811. procedure BuildInsTabCache;
  812. var
  813. i : longint;
  814. begin
  815. new(instabcache);
  816. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  817. i:=0;
  818. while (i<InsTabEntries) do
  819. begin
  820. if InsTabCache^[InsTab[i].Opcode]=-1 then
  821. InsTabCache^[InsTab[i].Opcode]:=i;
  822. inc(i);
  823. end;
  824. end;
  825. procedure InitAsm;
  826. begin
  827. if not assigned(instabcache) then
  828. BuildInsTabCache;
  829. end;
  830. procedure DoneAsm;
  831. begin
  832. if assigned(instabcache) then
  833. begin
  834. dispose(instabcache);
  835. instabcache:=nil;
  836. end;
  837. end;
  838. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  839. begin
  840. i.oppostfix:=pf;
  841. result:=i;
  842. end;
  843. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  844. begin
  845. i.roundingmode:=rm;
  846. result:=i;
  847. end;
  848. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  849. begin
  850. i.condition:=c;
  851. result:=i;
  852. end;
  853. Function SimpleGetNextInstruction(Current: tai; Var Next: tai): Boolean;
  854. Begin
  855. Current:=tai(Current.Next);
  856. While Assigned(Current) And (Current.typ In SkipInstr) Do
  857. Current:=tai(Current.Next);
  858. Next:=Current;
  859. If Assigned(Next) And Not(Next.typ In SkipInstr) Then
  860. Result:=True
  861. Else
  862. Begin
  863. Next:=Nil;
  864. Result:=False;
  865. End;
  866. End;
  867. (*
  868. function armconstequal(hp1,hp2: tai): boolean;
  869. begin
  870. result:=false;
  871. if hp1.typ<>hp2.typ then
  872. exit;
  873. case hp1.typ of
  874. tai_const:
  875. result:=
  876. (tai_const(hp2).sym=tai_const(hp).sym) and
  877. (tai_const(hp2).value=tai_const(hp).value) and
  878. (tai(hp2.previous).typ=ait_label);
  879. tai_const:
  880. result:=
  881. (tai_const(hp2).sym=tai_const(hp).sym) and
  882. (tai_const(hp2).value=tai_const(hp).value) and
  883. (tai(hp2.previous).typ=ait_label);
  884. end;
  885. end;
  886. *)
  887. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  888. var
  889. limit: longint;
  890. { FLD/FST VFP instructions have a limit of +/- 1024, not 4096, this
  891. function checks the next count instructions if the limit must be
  892. decreased }
  893. procedure CheckLimit(hp : tai;count : integer);
  894. var
  895. i : Integer;
  896. begin
  897. for i:=1 to count do
  898. if SimpleGetNextInstruction(hp,hp) and
  899. (tai(hp).typ=ait_instruction) and
  900. ((taicpu(hp).opcode=A_FLDS) or
  901. (taicpu(hp).opcode=A_FLDD) or
  902. (taicpu(hp).opcode=A_VLDR) or
  903. (taicpu(hp).opcode=A_LDF) or
  904. (taicpu(hp).opcode=A_STF)) then
  905. limit:=254;
  906. end;
  907. function is_case_dispatch(hp: taicpu): boolean;
  908. begin
  909. result:=
  910. ((taicpu(hp).opcode in [A_ADD,A_LDR]) and
  911. not(GenerateThumbCode or GenerateThumb2Code) and
  912. (taicpu(hp).oper[0]^.typ=top_reg) and
  913. (taicpu(hp).oper[0]^.reg=NR_PC)) or
  914. ((taicpu(hp).opcode=A_MOV) and (GenerateThumbCode) and
  915. (taicpu(hp).oper[0]^.typ=top_reg) and
  916. (taicpu(hp).oper[0]^.reg=NR_PC)) or
  917. (taicpu(hp).opcode=A_TBH) or
  918. (taicpu(hp).opcode=A_TBB);
  919. end;
  920. var
  921. curinspos,
  922. penalty,
  923. lastinspos,
  924. { increased for every data element > 4 bytes inserted }
  925. extradataoffset,
  926. curop : longint;
  927. curtai,
  928. inserttai : tai;
  929. curdatatai,hp,hp2 : tai;
  930. curdata : TAsmList;
  931. l : tasmlabel;
  932. doinsert,
  933. removeref : boolean;
  934. multiplier : byte;
  935. begin
  936. curdata:=TAsmList.create;
  937. lastinspos:=-1;
  938. curinspos:=0;
  939. extradataoffset:=0;
  940. if GenerateThumbCode then
  941. begin
  942. multiplier:=2;
  943. limit:=504;
  944. end
  945. else
  946. begin
  947. limit:=1016;
  948. multiplier:=1;
  949. end;
  950. curtai:=tai(list.first);
  951. doinsert:=false;
  952. while assigned(curtai) do
  953. begin
  954. { instruction? }
  955. case curtai.typ of
  956. ait_instruction:
  957. begin
  958. { walk through all operand of the instruction }
  959. for curop:=0 to taicpu(curtai).ops-1 do
  960. begin
  961. { reference? }
  962. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  963. begin
  964. { pc relative symbol? }
  965. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  966. if assigned(curdatatai) then
  967. begin
  968. { create a new copy of a data entry on arm thumb if the entry has been inserted already
  969. before because arm thumb does not allow pc relative negative offsets }
  970. if (GenerateThumbCode) and
  971. tai_label(curdatatai).inserted then
  972. begin
  973. current_asmdata.getjumplabel(l);
  974. hp:=tai_label.create(l);
  975. listtoinsert.Concat(hp);
  976. hp2:=tai(curdatatai.Next.GetCopy);
  977. hp2.Next:=nil;
  978. hp2.Previous:=nil;
  979. listtoinsert.Concat(hp2);
  980. taicpu(curtai).oper[curop]^.ref^.symboldata:=hp;
  981. taicpu(curtai).oper[curop]^.ref^.symbol:=l;
  982. l.increfs;
  983. curdatatai:=hp;
  984. end;
  985. { move only if we're at the first reference of a label }
  986. if not(tai_label(curdatatai).moved) then
  987. begin
  988. tai_label(curdatatai).moved:=true;
  989. { check if symbol already used. }
  990. { if yes, reuse the symbol }
  991. hp:=tai(curdatatai.next);
  992. removeref:=false;
  993. if assigned(hp) then
  994. begin
  995. case hp.typ of
  996. ait_const:
  997. begin
  998. if (tai_const(hp).consttype=aitconst_64bit) then
  999. inc(extradataoffset,multiplier);
  1000. end;
  1001. ait_realconst:
  1002. begin
  1003. inc(extradataoffset,multiplier*(((tai_realconst(hp).savesize-4)+3) div 4));
  1004. end;
  1005. else
  1006. ;
  1007. end;
  1008. { check if the same constant has been already inserted into the currently handled list,
  1009. if yes, reuse it }
  1010. if (hp.typ=ait_const) then
  1011. begin
  1012. hp2:=tai(curdata.first);
  1013. while assigned(hp2) do
  1014. begin
  1015. if (hp2.typ=ait_const) and (tai_const(hp2).sym=tai_const(hp).sym)
  1016. and (tai_const(hp2).value=tai_const(hp).value) and (tai(hp2.previous).typ=ait_label) and
  1017. { gottpoff and tlsgd symbols are PC relative, so we cannot reuse them }
  1018. (not(tai_const(hp2).consttype in [aitconst_gottpoff,aitconst_tlsgd,aitconst_tlsdesc])) then
  1019. begin
  1020. with taicpu(curtai).oper[curop]^.ref^ do
  1021. begin
  1022. symbol.decrefs;
  1023. symboldata:=hp2.previous;
  1024. symbol:=tai_label(hp2.previous).labsym;
  1025. symbol.increfs;
  1026. end;
  1027. if not tai_label(curdatatai).labsym.is_used then
  1028. removeref:=true;
  1029. break;
  1030. end;
  1031. hp2:=tai(hp2.next);
  1032. end;
  1033. end;
  1034. end;
  1035. { move or remove symbol reference }
  1036. repeat
  1037. hp:=tai(curdatatai.next);
  1038. listtoinsert.remove(curdatatai);
  1039. if removeref then
  1040. curdatatai.free
  1041. else
  1042. curdata.concat(curdatatai);
  1043. curdatatai:=hp;
  1044. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  1045. if lastinspos=-1 then
  1046. lastinspos:=curinspos;
  1047. end;
  1048. end;
  1049. end;
  1050. end;
  1051. inc(curinspos,multiplier);
  1052. end;
  1053. ait_align:
  1054. begin
  1055. { code is always 4 byte aligned, so we don't have to take care of .align 2 which would
  1056. requires also incrementing curinspos by 1 }
  1057. inc(curinspos,(tai_align(curtai).aligntype div 4)*multiplier);
  1058. end;
  1059. ait_const:
  1060. begin
  1061. inc(curinspos,multiplier);
  1062. if (tai_const(curtai).consttype=aitconst_64bit) then
  1063. inc(curinspos,multiplier);
  1064. end;
  1065. ait_realconst:
  1066. begin
  1067. inc(curinspos,multiplier*((tai_realconst(hp).savesize+3) div 4));
  1068. end;
  1069. else
  1070. ;
  1071. end;
  1072. { special case for case jump tables }
  1073. penalty:=0;
  1074. if SimpleGetNextInstruction(curtai,hp) and
  1075. (tai(hp).typ=ait_instruction) then
  1076. begin
  1077. case taicpu(hp).opcode of
  1078. A_MOV,
  1079. A_LDR,
  1080. A_ADD,
  1081. A_TBH,
  1082. A_TBB:
  1083. { approximation if we hit a case jump table }
  1084. if is_case_dispatch(taicpu(hp)) then
  1085. begin
  1086. penalty:=multiplier;
  1087. hp:=tai(hp.next);
  1088. { skip register allocations and comments inserted by the optimizer as well as a label and align
  1089. as jump tables for thumb might have }
  1090. while assigned(hp) and (hp.typ in [ait_comment,ait_regalloc,ait_label,ait_align]) do
  1091. hp:=tai(hp.next);
  1092. while assigned(hp) and (hp.typ=ait_const) do
  1093. begin
  1094. inc(penalty,multiplier);
  1095. hp:=tai(hp.next);
  1096. end;
  1097. end;
  1098. A_IT:
  1099. begin
  1100. if GenerateThumb2Code then
  1101. penalty:=multiplier;
  1102. { check if the next instruction fits as well
  1103. or if we splitted after the it so split before }
  1104. CheckLimit(hp,1);
  1105. end;
  1106. A_ITE,
  1107. A_ITT:
  1108. begin
  1109. if GenerateThumb2Code then
  1110. penalty:=2*multiplier;
  1111. { check if the next two instructions fit as well
  1112. or if we splitted them so split before }
  1113. CheckLimit(hp,2);
  1114. end;
  1115. A_ITEE,
  1116. A_ITTE,
  1117. A_ITET,
  1118. A_ITTT:
  1119. begin
  1120. if GenerateThumb2Code then
  1121. penalty:=3*multiplier;
  1122. { check if the next three instructions fit as well
  1123. or if we splitted them so split before }
  1124. CheckLimit(hp,3);
  1125. end;
  1126. A_ITEEE,
  1127. A_ITTEE,
  1128. A_ITETE,
  1129. A_ITTTE,
  1130. A_ITEET,
  1131. A_ITTET,
  1132. A_ITETT,
  1133. A_ITTTT:
  1134. begin
  1135. if GenerateThumb2Code then
  1136. penalty:=4*multiplier;
  1137. { check if the next three instructions fit as well
  1138. or if we splitted them so split before }
  1139. CheckLimit(hp,4);
  1140. end;
  1141. else
  1142. ;
  1143. end;
  1144. end;
  1145. CheckLimit(curtai,1);
  1146. { don't miss an insert }
  1147. doinsert:=doinsert or
  1148. (not(curdata.empty) and
  1149. (curinspos-lastinspos+penalty+extradataoffset>limit));
  1150. { split only at real instructions else the test below fails }
  1151. if doinsert and (curtai.typ=ait_instruction) and
  1152. (
  1153. { don't split loads of pc to lr and the following move }
  1154. not(
  1155. (taicpu(curtai).opcode=A_MOV) and
  1156. (taicpu(curtai).oper[0]^.typ=top_reg) and
  1157. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  1158. (taicpu(curtai).oper[1]^.typ=top_reg) and
  1159. (taicpu(curtai).oper[1]^.reg=NR_PC)
  1160. )
  1161. ) and
  1162. (
  1163. { do not insert data after a B instruction due to their limited range }
  1164. not((GenerateThumbCode) and
  1165. (taicpu(curtai).opcode=A_B)
  1166. )
  1167. ) then
  1168. begin
  1169. lastinspos:=-1;
  1170. extradataoffset:=0;
  1171. if GenerateThumbCode then
  1172. limit:=502
  1173. else
  1174. limit:=1016;
  1175. { if this is an add/tbh/tbb-based jumptable, go back to the
  1176. previous instruction, because inserting data between the
  1177. dispatch instruction and the table would mess up the
  1178. addresses }
  1179. inserttai:=curtai;
  1180. if is_case_dispatch(taicpu(inserttai)) and
  1181. ((taicpu(inserttai).opcode=A_ADD) or
  1182. (taicpu(inserttai).opcode=A_TBH) or
  1183. (taicpu(inserttai).opcode=A_TBB)) then
  1184. begin
  1185. repeat
  1186. inserttai:=tai(inserttai.previous);
  1187. until inserttai.typ=ait_instruction;
  1188. { if it's an add-based jump table, then also skip the
  1189. pc-relative load }
  1190. if taicpu(curtai).opcode=A_ADD then
  1191. repeat
  1192. inserttai:=tai(inserttai.previous);
  1193. until inserttai.typ=ait_instruction;
  1194. end
  1195. else
  1196. { on arm thumb, insert the data always after all labels etc. following an instruction so it
  1197. is prevent that a bxx yyy; bl xxx; yyyy: sequence gets separated ( we never insert on arm thumb after
  1198. bxx) and the distance of bxx gets too long }
  1199. if GenerateThumbCode then
  1200. while assigned(tai(inserttai.Next)) and (tai(inserttai.Next).typ in SkipInstr+[ait_label]) do
  1201. inserttai:=tai(inserttai.next);
  1202. doinsert:=false;
  1203. current_asmdata.getjumplabel(l);
  1204. { align jump in thumb .text section to 4 bytes }
  1205. if not(curdata.empty) and (GenerateThumbCode) then
  1206. curdata.Insert(tai_align.Create(4));
  1207. curdata.insert(taicpu.op_sym(A_B,l));
  1208. curdata.concat(tai_label.create(l));
  1209. { mark all labels as inserted, arm thumb
  1210. needs this, so data referencing an already inserted label can be
  1211. duplicated because arm thumb does not allow negative pc relative offset }
  1212. hp2:=tai(curdata.first);
  1213. while assigned(hp2) do
  1214. begin
  1215. if hp2.typ=ait_label then
  1216. tai_label(hp2).inserted:=true;
  1217. hp2:=tai(hp2.next);
  1218. end;
  1219. { continue with the last inserted label because we use later
  1220. on SimpleGetNextInstruction, so if we used curtai.next (which
  1221. is then equal curdata.last.previous) we could over see one
  1222. instruction }
  1223. hp:=tai(curdata.Last);
  1224. list.insertlistafter(inserttai,curdata);
  1225. curtai:=hp;
  1226. end
  1227. else
  1228. curtai:=tai(curtai.next);
  1229. end;
  1230. { align jump in thumb .text section to 4 bytes }
  1231. if not(curdata.empty) and (GenerateThumbCode or GenerateThumb2Code) then
  1232. curdata.Insert(tai_align.Create(4));
  1233. list.concatlist(curdata);
  1234. curdata.free;
  1235. end;
  1236. procedure ensurethumb2encodings(list: TAsmList);
  1237. var
  1238. curtai: tai;
  1239. op2reg: TRegister;
  1240. begin
  1241. { Do Thumb-2 16bit -> 32bit transformations }
  1242. curtai:=tai(list.first);
  1243. while assigned(curtai) do
  1244. begin
  1245. case curtai.typ of
  1246. ait_instruction:
  1247. begin
  1248. case taicpu(curtai).opcode of
  1249. A_ADD:
  1250. begin
  1251. { Set wide flag for ADD Rd,Rn,Rm where registers are over R7(high register set) }
  1252. if taicpu(curtai).ops = 3 then
  1253. begin
  1254. if taicpu(curtai).oper[2]^.typ in [top_reg,top_shifterop] then
  1255. begin
  1256. if taicpu(curtai).oper[2]^.typ = top_reg then
  1257. op2reg := taicpu(curtai).oper[2]^.reg
  1258. else if taicpu(curtai).oper[2]^.shifterop^.rs <> NR_NO then
  1259. op2reg := taicpu(curtai).oper[2]^.shifterop^.rs
  1260. else
  1261. op2reg := NR_NO;
  1262. if op2reg <> NR_NO then
  1263. begin
  1264. if (taicpu(curtai).oper[0]^.reg >= NR_R8) or
  1265. (taicpu(curtai).oper[1]^.reg >= NR_R8) or
  1266. (op2reg >= NR_R8) then
  1267. begin
  1268. include(taicpu(curtai).flags,cf_wideformat);
  1269. { Handle special cases where register rules are violated by optimizer/user }
  1270. { if d == 13 || (d == 15 && S == ‘0’) || n == 15 || m IN [13,15] then UNPREDICTABLE; }
  1271. { Transform ADD.W Rx, Ry, R13 into ADD.W Rx, R13, Ry }
  1272. if (op2reg = NR_R13) and (taicpu(curtai).oper[2]^.typ = top_reg) then
  1273. begin
  1274. taicpu(curtai).oper[2]^.reg := taicpu(curtai).oper[1]^.reg;
  1275. taicpu(curtai).oper[1]^.reg := op2reg;
  1276. end;
  1277. end;
  1278. end;
  1279. end;
  1280. end;
  1281. end;
  1282. else;
  1283. end;
  1284. end;
  1285. else
  1286. ;
  1287. end;
  1288. curtai:=tai(curtai.Next);
  1289. end;
  1290. end;
  1291. procedure ensurethumbencodings(list: TAsmList);
  1292. var
  1293. curtai: tai;
  1294. begin
  1295. { Do Thumb 16bit transformations to form valid instruction forms }
  1296. curtai:=tai(list.first);
  1297. while assigned(curtai) do
  1298. begin
  1299. case curtai.typ of
  1300. ait_instruction:
  1301. begin
  1302. case taicpu(curtai).opcode of
  1303. A_STM:
  1304. begin
  1305. if (taicpu(curtai).ops=2) and
  1306. (taicpu(curtai).oper[0]^.typ=top_ref) and
  1307. (taicpu(curtai).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  1308. (taicpu(curtai).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  1309. (taicpu(curtai).oppostfix in [PF_FD,PF_DB]) then
  1310. begin
  1311. taicpu(curtai).oppostfix:=PF_None;
  1312. taicpu(curtai).loadregset(0, taicpu(curtai).oper[1]^.regtyp, taicpu(curtai).oper[1]^.subreg, taicpu(curtai).oper[1]^.regset^);
  1313. taicpu(curtai).ops:=1;
  1314. taicpu(curtai).opcode:=A_PUSH;
  1315. end;
  1316. end;
  1317. A_LDM:
  1318. begin
  1319. if (taicpu(curtai).ops=2) and
  1320. (taicpu(curtai).oper[0]^.typ=top_ref) and
  1321. (taicpu(curtai).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  1322. (taicpu(curtai).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  1323. (taicpu(curtai).oppostfix in [PF_FD,PF_IA]) then
  1324. begin
  1325. taicpu(curtai).oppostfix:=PF_None;
  1326. taicpu(curtai).loadregset(0, taicpu(curtai).oper[1]^.regtyp, taicpu(curtai).oper[1]^.subreg, taicpu(curtai).oper[1]^.regset^);
  1327. taicpu(curtai).ops:=1;
  1328. taicpu(curtai).opcode:=A_POP;
  1329. end;
  1330. end;
  1331. A_ADD,
  1332. A_AND,A_EOR,A_ORR,A_BIC,
  1333. A_LSL,A_LSR,A_ASR,A_ROR,
  1334. A_ADC,A_SBC:
  1335. begin
  1336. if (taicpu(curtai).ops = 3) and
  1337. (taicpu(curtai).oper[2]^.typ=top_reg) and
  1338. (taicpu(curtai).oper[0]^.reg=taicpu(curtai).oper[1]^.reg) and
  1339. (taicpu(curtai).oper[0]^.reg<>NR_STACK_POINTER_REG) then
  1340. begin
  1341. taicpu(curtai).oper[1]^.reg:=taicpu(curtai).oper[2]^.reg;
  1342. taicpu(curtai).ops:=2;
  1343. end;
  1344. end;
  1345. else
  1346. ;
  1347. end;
  1348. end;
  1349. else
  1350. ;
  1351. end;
  1352. curtai:=tai(curtai.Next);
  1353. end;
  1354. end;
  1355. function getMergedInstruction(FirstOp,LastOp:TAsmOp;InvertLast:boolean) : TAsmOp;
  1356. const
  1357. opTable: array[A_IT..A_ITTTT] of string =
  1358. ('T','TE','TT','TEE','TTE','TET','TTT',
  1359. 'TEEE','TTEE','TETE','TTTE',
  1360. 'TEET','TTET','TETT','TTTT');
  1361. invertedOpTable: array[A_IT..A_ITTTT] of string =
  1362. ('E','ET','EE','ETT','EET','ETE','EEE',
  1363. 'ETTT','EETT','ETET','EEET',
  1364. 'ETTE','EETE','ETEE','EEEE');
  1365. var
  1366. resStr : string;
  1367. i : TAsmOp;
  1368. begin
  1369. if InvertLast then
  1370. resStr := opTable[FirstOp]+invertedOpTable[LastOp]
  1371. else
  1372. resStr := opTable[FirstOp]+opTable[LastOp];
  1373. if length(resStr) > 4 then
  1374. internalerror(2012100805);
  1375. for i := low(opTable) to high(opTable) do
  1376. if opTable[i] = resStr then
  1377. exit(i);
  1378. internalerror(2012100806);
  1379. end;
  1380. procedure foldITInstructions(list: TAsmList);
  1381. var
  1382. curtai,hp1 : tai;
  1383. levels,i : LongInt;
  1384. begin
  1385. curtai:=tai(list.First);
  1386. while assigned(curtai) do
  1387. begin
  1388. case curtai.typ of
  1389. ait_instruction:
  1390. begin
  1391. if IsIT(taicpu(curtai).opcode) then
  1392. begin
  1393. levels := GetITLevels(taicpu(curtai).opcode);
  1394. if levels < 4 then
  1395. begin
  1396. i:=levels;
  1397. hp1:=tai(curtai.Next);
  1398. while assigned(hp1) and
  1399. (i > 0) do
  1400. begin
  1401. if hp1.typ=ait_instruction then
  1402. begin
  1403. dec(i);
  1404. if (i = 0) and
  1405. mustbelast(hp1) then
  1406. begin
  1407. hp1:=nil;
  1408. break;
  1409. end;
  1410. end;
  1411. hp1:=tai(hp1.Next);
  1412. end;
  1413. if assigned(hp1) then
  1414. begin
  1415. // We are pointing at the first instruction after the IT block
  1416. while assigned(hp1) and
  1417. (hp1.typ<>ait_instruction) do
  1418. hp1:=tai(hp1.Next);
  1419. if assigned(hp1) and
  1420. (hp1.typ=ait_instruction) and
  1421. IsIT(taicpu(hp1).opcode) then
  1422. begin
  1423. if (levels+GetITLevels(taicpu(hp1).opcode) <= 4) and
  1424. ((taicpu(curtai).oper[0]^.cc=taicpu(hp1).oper[0]^.cc) or
  1425. (taicpu(curtai).oper[0]^.cc=inverse_cond(taicpu(hp1).oper[0]^.cc))) then
  1426. begin
  1427. taicpu(curtai).opcode:=getMergedInstruction(taicpu(curtai).opcode,
  1428. taicpu(hp1).opcode,
  1429. taicpu(curtai).oper[0]^.cc=inverse_cond(taicpu(hp1).oper[0]^.cc));
  1430. list.Remove(hp1);
  1431. hp1.Free;
  1432. end;
  1433. end;
  1434. end;
  1435. end;
  1436. end;
  1437. end
  1438. else
  1439. ;
  1440. end;
  1441. curtai:=tai(curtai.Next);
  1442. end;
  1443. end;
  1444. {$push}
  1445. { Disable range and overflow checking here }
  1446. {$R-}{$Q-}
  1447. procedure fix_invalid_imms(list: TAsmList);
  1448. var
  1449. curtai: tai;
  1450. sh: byte;
  1451. begin
  1452. curtai:=tai(list.First);
  1453. while assigned(curtai) do
  1454. begin
  1455. case curtai.typ of
  1456. ait_instruction:
  1457. begin
  1458. if (taicpu(curtai).opcode in [A_AND,A_BIC]) and
  1459. (taicpu(curtai).ops=3) and
  1460. (taicpu(curtai).oper[2]^.typ=top_const) and
  1461. (not is_shifter_const(taicpu(curtai).oper[2]^.val,sh)) and
  1462. is_shifter_const((not taicpu(curtai).oper[2]^.val) and $FFFFFFFF,sh) then
  1463. begin
  1464. case taicpu(curtai).opcode of
  1465. A_AND: taicpu(curtai).opcode:=A_BIC;
  1466. A_BIC: taicpu(curtai).opcode:=A_AND;
  1467. else
  1468. internalerror(2019050931);
  1469. end;
  1470. taicpu(curtai).oper[2]^.val:=(not taicpu(curtai).oper[2]^.val) and $FFFFFFFF;
  1471. end
  1472. else if (taicpu(curtai).opcode in [A_SUB,A_ADD]) and
  1473. (taicpu(curtai).ops=3) and
  1474. (taicpu(curtai).oper[2]^.typ=top_const) and
  1475. (not is_shifter_const(taicpu(curtai).oper[2]^.val,sh)) and
  1476. is_shifter_const(-taicpu(curtai).oper[2]^.val,sh) then
  1477. begin
  1478. case taicpu(curtai).opcode of
  1479. A_ADD: taicpu(curtai).opcode:=A_SUB;
  1480. A_SUB: taicpu(curtai).opcode:=A_ADD;
  1481. else
  1482. internalerror(2019050930);
  1483. end;
  1484. taicpu(curtai).oper[2]^.val:=-taicpu(curtai).oper[2]^.val;
  1485. end;
  1486. end;
  1487. else
  1488. ;
  1489. end;
  1490. curtai:=tai(curtai.Next);
  1491. end;
  1492. end;
  1493. {$pop}
  1494. procedure gather_it_info(list: TAsmList);
  1495. var
  1496. curtai: tai;
  1497. in_it: boolean;
  1498. it_count: longint;
  1499. begin
  1500. in_it:=false;
  1501. it_count:=0;
  1502. curtai:=tai(list.First);
  1503. while assigned(curtai) do
  1504. begin
  1505. case curtai.typ of
  1506. ait_instruction:
  1507. begin
  1508. case taicpu(curtai).opcode of
  1509. A_IT..A_ITTTT:
  1510. begin
  1511. if in_it then
  1512. Message1(asmw_e_invalid_opcode_and_operands, 'ITxx instruction is inside another ITxx instruction')
  1513. else
  1514. begin
  1515. in_it:=true;
  1516. it_count:=GetITLevels(taicpu(curtai).opcode);
  1517. end;
  1518. end;
  1519. else
  1520. begin
  1521. if in_it then
  1522. include(taicpu(curtai).flags,cf_inIT)
  1523. else
  1524. exclude(taicpu(curtai).flags,cf_inIT);
  1525. if in_it and (it_count=1) then
  1526. include(taicpu(curtai).flags,cf_lastinIT)
  1527. else
  1528. exclude(taicpu(curtai).flags,cf_lastinIT);
  1529. if in_it then
  1530. begin
  1531. dec(it_count);
  1532. if it_count <= 0 then
  1533. in_it:=false;
  1534. end;
  1535. end;
  1536. end;
  1537. end;
  1538. else
  1539. ;
  1540. end;
  1541. curtai:=tai(curtai.Next);
  1542. end;
  1543. end;
  1544. { Expands pseudo instructions ( mov r1,r2,lsl #4 -> lsl r1,r2,#4) }
  1545. procedure expand_instructions(list: TAsmList);
  1546. var
  1547. curtai: tai;
  1548. begin
  1549. curtai:=tai(list.First);
  1550. while assigned(curtai) do
  1551. begin
  1552. case curtai.typ of
  1553. ait_instruction:
  1554. begin
  1555. case taicpu(curtai).opcode of
  1556. A_MOV:
  1557. begin
  1558. if (taicpu(curtai).ops=3) and
  1559. (taicpu(curtai).oper[2]^.typ=top_shifterop) then
  1560. begin
  1561. case taicpu(curtai).oper[2]^.shifterop^.shiftmode of
  1562. SM_NONE: ;
  1563. SM_LSL: taicpu(curtai).opcode:=A_LSL;
  1564. SM_LSR: taicpu(curtai).opcode:=A_LSR;
  1565. SM_ASR: taicpu(curtai).opcode:=A_ASR;
  1566. SM_ROR: taicpu(curtai).opcode:=A_ROR;
  1567. SM_RRX: taicpu(curtai).opcode:=A_RRX;
  1568. end;
  1569. if taicpu(curtai).oper[2]^.shifterop^.shiftmode=SM_RRX then
  1570. taicpu(curtai).ops:=2;
  1571. if taicpu(curtai).oper[2]^.shifterop^.rs=NR_NO then
  1572. taicpu(curtai).loadconst(2, taicpu(curtai).oper[2]^.shifterop^.shiftimm)
  1573. else
  1574. taicpu(curtai).loadreg(2, taicpu(curtai).oper[2]^.shifterop^.rs);
  1575. end;
  1576. end;
  1577. A_NEG:
  1578. begin
  1579. taicpu(curtai).opcode:=A_RSB;
  1580. taicpu(curtai).oppostfix:=PF_S; // NEG should always set flags (according to documentation NEG<c> = RSBS<c>)
  1581. if taicpu(curtai).ops=2 then
  1582. begin
  1583. taicpu(curtai).loadconst(2,0);
  1584. taicpu(curtai).ops:=3;
  1585. end
  1586. else
  1587. begin
  1588. taicpu(curtai).loadconst(1,0);
  1589. taicpu(curtai).ops:=2;
  1590. end;
  1591. end;
  1592. A_SWI:
  1593. begin
  1594. taicpu(curtai).opcode:=A_SVC;
  1595. end;
  1596. else
  1597. ;
  1598. end;
  1599. end;
  1600. else
  1601. ;
  1602. end;
  1603. curtai:=tai(curtai.Next);
  1604. end;
  1605. end;
  1606. procedure finalizearmcode(list, listtoinsert: TAsmList);
  1607. begin
  1608. { Don't expand pseudo instructions when using GAS, it breaks on some thumb instructions }
  1609. if target_asm.id<>as_gas then
  1610. expand_instructions(list);
  1611. { Do Thumb-2 16bit -> 32bit transformations }
  1612. if GenerateThumb2Code then
  1613. begin
  1614. ensurethumbencodings(list);
  1615. ensurethumb2encodings(list);
  1616. foldITInstructions(list);
  1617. end
  1618. else if GenerateThumbCode then
  1619. ensurethumbencodings(list);
  1620. gather_it_info(list);
  1621. fix_invalid_imms(list);
  1622. insertpcrelativedata(list, listtoinsert);
  1623. end;
  1624. procedure InsertPData;
  1625. var
  1626. prolog: TAsmList;
  1627. begin
  1628. prolog:=TAsmList.create;
  1629. new_section(prolog,sec_code,'FPC_EH_PROLOG',sizeof(pint),secorder_begin);
  1630. prolog.concat(Tai_const.Createname('_ARM_ExceptionHandler', 0));
  1631. prolog.concat(Tai_const.Create_32bit(0));
  1632. prolog.concat(Tai_symbol.Createname_global('FPC_EH_CODE_START',AT_METADATA,0,voidpointertype));
  1633. { dummy function }
  1634. prolog.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14));
  1635. current_asmdata.asmlists[al_start].insertList(prolog);
  1636. prolog.Free;
  1637. new_section(current_asmdata.asmlists[al_end],sec_pdata,'',sizeof(pint));
  1638. current_asmdata.asmlists[al_end].concat(Tai_const.Createname('FPC_EH_CODE_START', 0));
  1639. current_asmdata.asmlists[al_end].concat(Tai_const.Create_32bit(longint($ffffff01)));
  1640. end;
  1641. (*
  1642. Floating point instruction format information, taken from the linux kernel
  1643. ARM Floating Point Instruction Classes
  1644. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1645. |c o n d|1 1 0 P|U|u|W|L| Rn |v| Fd |0|0|0|1| o f f s e t | CPDT
  1646. |c o n d|1 1 0 P|U|w|W|L| Rn |x| Fd |0|0|1|0| o f f s e t | CPDT (copro 2)
  1647. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1648. |c o n d|1 1 1 0|a|b|c|d|e| Fn |j| Fd |0|0|0|1|f|g|h|0|i| Fm | CPDO
  1649. |c o n d|1 1 1 0|a|b|c|L|e| Fn | Rd |0|0|0|1|f|g|h|1|i| Fm | CPRT
  1650. |c o n d|1 1 1 0|a|b|c|1|e| Fn |1|1|1|1|0|0|0|1|f|g|h|1|i| Fm | comparisons
  1651. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1652. CPDT data transfer instructions
  1653. LDF, STF, LFM (copro 2), SFM (copro 2)
  1654. CPDO dyadic arithmetic instructions
  1655. ADF, MUF, SUF, RSF, DVF, RDF,
  1656. POW, RPW, RMF, FML, FDV, FRD, POL
  1657. CPDO monadic arithmetic instructions
  1658. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  1659. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  1660. CPRT joint arithmetic/data transfer instructions
  1661. FIX (arithmetic followed by load/store)
  1662. FLT (load/store followed by arithmetic)
  1663. CMF, CNF CMFE, CNFE (comparisons)
  1664. WFS, RFS (write/read floating point status register)
  1665. WFC, RFC (write/read floating point control register)
  1666. cond condition codes
  1667. P pre/post index bit: 0 = postindex, 1 = preindex
  1668. U up/down bit: 0 = stack grows down, 1 = stack grows up
  1669. W write back bit: 1 = update base register (Rn)
  1670. L load/store bit: 0 = store, 1 = load
  1671. Rn base register
  1672. Rd destination/source register
  1673. Fd floating point destination register
  1674. Fn floating point source register
  1675. Fm floating point source register or floating point constant
  1676. uv transfer length (TABLE 1)
  1677. wx register count (TABLE 2)
  1678. abcd arithmetic opcode (TABLES 3 & 4)
  1679. ef destination size (rounding precision) (TABLE 5)
  1680. gh rounding mode (TABLE 6)
  1681. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  1682. i constant bit: 1 = constant (TABLE 6)
  1683. */
  1684. /*
  1685. TABLE 1
  1686. +-------------------------+---+---+---------+---------+
  1687. | Precision | u | v | FPSR.EP | length |
  1688. +-------------------------+---+---+---------+---------+
  1689. | Single | 0 | 0 | x | 1 words |
  1690. | Double | 1 | 1 | x | 2 words |
  1691. | Extended | 1 | 1 | x | 3 words |
  1692. | Packed decimal | 1 | 1 | 0 | 3 words |
  1693. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  1694. +-------------------------+---+---+---------+---------+
  1695. Note: x = don't care
  1696. */
  1697. /*
  1698. TABLE 2
  1699. +---+---+---------------------------------+
  1700. | w | x | Number of registers to transfer |
  1701. +---+---+---------------------------------+
  1702. | 0 | 1 | 1 |
  1703. | 1 | 0 | 2 |
  1704. | 1 | 1 | 3 |
  1705. | 0 | 0 | 4 |
  1706. +---+---+---------------------------------+
  1707. */
  1708. /*
  1709. TABLE 3: Dyadic Floating Point Opcodes
  1710. +---+---+---+---+----------+-----------------------+-----------------------+
  1711. | a | b | c | d | Mnemonic | Description | Operation |
  1712. +---+---+---+---+----------+-----------------------+-----------------------+
  1713. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  1714. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  1715. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  1716. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  1717. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  1718. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  1719. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  1720. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  1721. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  1722. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  1723. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  1724. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  1725. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  1726. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  1727. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  1728. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  1729. +---+---+---+---+----------+-----------------------+-----------------------+
  1730. Note: POW, RPW, POL are deprecated, and are available for backwards
  1731. compatibility only.
  1732. */
  1733. /*
  1734. TABLE 4: Monadic Floating Point Opcodes
  1735. +---+---+---+---+----------+-----------------------+-----------------------+
  1736. | a | b | c | d | Mnemonic | Description | Operation |
  1737. +---+---+---+---+----------+-----------------------+-----------------------+
  1738. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  1739. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  1740. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  1741. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  1742. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  1743. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  1744. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  1745. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  1746. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  1747. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  1748. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  1749. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  1750. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  1751. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  1752. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  1753. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  1754. +---+---+---+---+----------+-----------------------+-----------------------+
  1755. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  1756. available for backwards compatibility only.
  1757. */
  1758. /*
  1759. TABLE 5
  1760. +-------------------------+---+---+
  1761. | Rounding Precision | e | f |
  1762. +-------------------------+---+---+
  1763. | IEEE Single precision | 0 | 0 |
  1764. | IEEE Double precision | 0 | 1 |
  1765. | IEEE Extended precision | 1 | 0 |
  1766. | undefined (trap) | 1 | 1 |
  1767. +-------------------------+---+---+
  1768. */
  1769. /*
  1770. TABLE 5
  1771. +---------------------------------+---+---+
  1772. | Rounding Mode | g | h |
  1773. +---------------------------------+---+---+
  1774. | Round to nearest (default) | 0 | 0 |
  1775. | Round toward plus infinity | 0 | 1 |
  1776. | Round toward negative infinity | 1 | 0 |
  1777. | Round toward zero | 1 | 1 |
  1778. +---------------------------------+---+---+
  1779. *)
  1780. function taicpu.GetString:string;
  1781. var
  1782. i : longint;
  1783. s : string;
  1784. addsize : boolean;
  1785. begin
  1786. s:='['+gas_op2str[opcode];
  1787. for i:=0 to ops-1 do
  1788. begin
  1789. with oper[i]^ do
  1790. begin
  1791. if i=0 then
  1792. s:=s+' '
  1793. else
  1794. s:=s+',';
  1795. { type }
  1796. addsize:=false;
  1797. if (ot and OT_VREG)=OT_VREG then
  1798. s:=s+'vreg'
  1799. else
  1800. if (ot and OT_FPUREG)=OT_FPUREG then
  1801. s:=s+'fpureg'
  1802. else
  1803. if (ot and OT_REGS)=OT_REGS then
  1804. s:=s+'sreg'
  1805. else
  1806. if (ot and OT_REGF)=OT_REGF then
  1807. s:=s+'creg'
  1808. else
  1809. if (ot and OT_REGISTER)=OT_REGISTER then
  1810. begin
  1811. s:=s+'reg';
  1812. addsize:=true;
  1813. end
  1814. else
  1815. if (ot and OT_REGLIST)=OT_REGLIST then
  1816. begin
  1817. s:=s+'reglist';
  1818. addsize:=false;
  1819. end
  1820. else
  1821. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  1822. begin
  1823. s:=s+'imm';
  1824. addsize:=true;
  1825. end
  1826. else
  1827. if (ot and OT_MEMORY)=OT_MEMORY then
  1828. begin
  1829. s:=s+'mem';
  1830. addsize:=true;
  1831. if (ot and OT_AM2)<>0 then
  1832. s:=s+' am2 '
  1833. else if (ot and OT_AM6)<>0 then
  1834. s:=s+' am2 ';
  1835. end
  1836. else
  1837. if (ot and OT_SHIFTEROP)=OT_SHIFTEROP then
  1838. begin
  1839. s:=s+'shifterop';
  1840. addsize:=false;
  1841. end
  1842. else
  1843. s:=s+'???';
  1844. { size }
  1845. if addsize then
  1846. begin
  1847. if (ot and OT_BITS8)<>0 then
  1848. s:=s+'8'
  1849. else
  1850. if (ot and OT_BITS16)<>0 then
  1851. s:=s+'24'
  1852. else
  1853. if (ot and OT_BITS32)<>0 then
  1854. s:=s+'32'
  1855. else
  1856. if (ot and OT_BITSSHIFTER)<>0 then
  1857. s:=s+'shifter'
  1858. else
  1859. s:=s+'??';
  1860. { signed }
  1861. if (ot and OT_SIGNED)<>0 then
  1862. s:=s+'s';
  1863. end;
  1864. end;
  1865. end;
  1866. GetString:=s+']';
  1867. end;
  1868. procedure taicpu.ResetPass1;
  1869. begin
  1870. { we need to reset everything here, because the choosen insentry
  1871. can be invalid for a new situation where the previously optimized
  1872. insentry is not correct }
  1873. InsEntry:=nil;
  1874. InsSize:=0;
  1875. LastInsOffset:=-1;
  1876. end;
  1877. procedure taicpu.ResetPass2;
  1878. begin
  1879. { we are here in a second pass, check if the instruction can be optimized }
  1880. if assigned(InsEntry) and
  1881. ((InsEntry^.flags and IF_PASS2)<>0) then
  1882. begin
  1883. InsEntry:=nil;
  1884. InsSize:=0;
  1885. end;
  1886. LastInsOffset:=-1;
  1887. end;
  1888. function taicpu.CheckIfValid:boolean;
  1889. begin
  1890. Result:=False; { unimplemented }
  1891. end;
  1892. function taicpu.Pass1(objdata:TObjData):longint;
  1893. var
  1894. ldr2op : array[PF_B..PF_T] of tasmop = (
  1895. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  1896. str2op : array[PF_B..PF_T] of tasmop = (
  1897. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  1898. begin
  1899. Pass1:=0;
  1900. { Save the old offset and set the new offset }
  1901. InsOffset:=ObjData.CurrObjSec.Size;
  1902. { Error? }
  1903. if (Insentry=nil) and (InsSize=-1) then
  1904. exit;
  1905. { set the file postion }
  1906. current_filepos:=fileinfo;
  1907. { tranlate LDR+postfix to complete opcode }
  1908. if (opcode=A_LDR) and (oppostfix=PF_D) then
  1909. begin
  1910. opcode:=A_LDRD;
  1911. oppostfix:=PF_None;
  1912. end
  1913. else if (opcode=A_LDR) and (oppostfix<>PF_None) then
  1914. begin
  1915. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  1916. opcode:=ldr2op[oppostfix]
  1917. else
  1918. internalerror(2005091001);
  1919. if opcode=A_None then
  1920. internalerror(2005091004);
  1921. { postfix has been added to opcode }
  1922. oppostfix:=PF_None;
  1923. end
  1924. else if (opcode=A_STR) and (oppostfix=PF_D) then
  1925. begin
  1926. opcode:=A_STRD;
  1927. oppostfix:=PF_None;
  1928. end
  1929. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  1930. begin
  1931. if (oppostfix in [low(str2op)..high(str2op)]) then
  1932. opcode:=str2op[oppostfix]
  1933. else
  1934. internalerror(2005091002);
  1935. if opcode=A_None then
  1936. internalerror(2005091003);
  1937. { postfix has been added to opcode }
  1938. oppostfix:=PF_None;
  1939. end;
  1940. { Get InsEntry }
  1941. if FindInsEntry(objdata) then
  1942. begin
  1943. InsSize:=4;
  1944. if insentry^.code[0] in [#$60..#$6C] then
  1945. InsSize:=2;
  1946. LastInsOffset:=InsOffset;
  1947. Pass1:=InsSize;
  1948. exit;
  1949. end;
  1950. LastInsOffset:=-1;
  1951. end;
  1952. procedure taicpu.Pass2(objdata:TObjData);
  1953. begin
  1954. { error in pass1 ? }
  1955. if insentry=nil then
  1956. exit;
  1957. current_filepos:=fileinfo;
  1958. { Generate the instruction }
  1959. GenCode(objdata);
  1960. end;
  1961. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1962. begin
  1963. end;
  1964. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  1965. begin
  1966. end;
  1967. procedure taicpu.ppubuildderefimploper(var o:toper);
  1968. begin
  1969. end;
  1970. procedure taicpu.ppuderefoper(var o:toper);
  1971. begin
  1972. end;
  1973. procedure taicpu.BuildArmMasks(objdata:TObjData);
  1974. const
  1975. Masks: array[tcputype] of longint =
  1976. (
  1977. IF_NONE,
  1978. IF_ARMv4,
  1979. IF_ARMv4,
  1980. IF_ARMv4,
  1981. IF_ARMv4T or IF_ARMv4,
  1982. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T,
  1983. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE,
  1984. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ,
  1985. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6,
  1986. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K,
  1987. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K or IF_ARMv6T2,
  1988. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K or IF_ARMv6T2 or IF_ARMv6Z,
  1989. IF_ARMv4T or IF_ARMv5T or IF_ARMv6M,
  1990. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K or IF_ARMv6T2 or IF_ARMv6Z or IF_ARMv7,
  1991. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K or IF_ARMv6T2 or IF_ARMv6Z or IF_ARMv7 or IF_ARMv7A,
  1992. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K or IF_ARMv6T2 or IF_ARMv6Z or IF_ARMv7 or IF_ARMv7A or IF_ARMv7R,
  1993. IF_ARMv4T or IF_ARMv5T or IF_ARMv6T2 or IF_ARMv7M,
  1994. IF_ARMv4T or IF_ARMv5T or IF_ARMv6T2 or IF_ARMv7M or IF_ARMv7EM
  1995. );
  1996. FPUMasks: array[tfputype] of longword =
  1997. (
  1998. { fpu_none } IF_NONE,
  1999. { fpu_soft } IF_NONE,
  2000. { fpu_libgcc } IF_NONE,
  2001. { fpu_fpa } IF_FPA,
  2002. { fpu_fpa10 } IF_FPA,
  2003. { fpu_fpa11 } IF_FPA,
  2004. { fpu_vfpv2 } IF_VFPv2,
  2005. { fpu_vfpv3 } IF_VFPv2 or IF_VFPv3,
  2006. { fpu_neon_vfpv3 } IF_VFPv2 or IF_VFPv3 or IF_NEON,
  2007. { fpu_vfpv3_d16 } IF_VFPv2 or IF_VFPv3,
  2008. { fpu_fpv4_s16 } IF_NONE,
  2009. { fpu_vfpv4 } IF_VFPv2 or IF_VFPv3 or IF_VFPv4,
  2010. { fpu_vfpv4 } IF_VFPv2 or IF_VFPv3 or IF_VFPv4,
  2011. { fpu_neon_vfpv4 } IF_VFPv2 or IF_VFPv3 or IF_VFPv4 or IF_NEON,
  2012. { fpu_fpv5_d16 } IF_VFPv2 or IF_VFPv3 or IF_VFPv4 or IF_VFPv5,
  2013. { fpu_fpv5_sp_d16} IF_VFPv2 or IF_VFPv3 or IF_VFPv4 or IF_VFPv5,
  2014. { fpu_fp_armv8 } IF_VFPv2 or IF_VFPv3 or IF_VFPv4 or IF_VFPv5
  2015. );
  2016. begin
  2017. fArmVMask:=Masks[current_settings.cputype] or FPUMasks[current_settings.fputype];
  2018. if cf_thumb in flags then
  2019. begin
  2020. fArmMask:=IF_THUMB;
  2021. if CPUARM_HAS_THUMB2 in cpu_capabilities[current_settings.cputype] then
  2022. fArmMask:=fArmMask or IF_THUMB32;
  2023. end
  2024. else
  2025. fArmMask:=IF_ARM32;
  2026. end;
  2027. function taicpu.InsEnd:longint;
  2028. begin
  2029. Result:=0; { unimplemented }
  2030. end;
  2031. procedure taicpu.create_ot(objdata:TObjData);
  2032. var
  2033. i,l,relsize : longint;
  2034. dummy : byte;
  2035. currsym : TObjSymbol;
  2036. begin
  2037. if ops=0 then
  2038. exit;
  2039. { update oper[].ot field }
  2040. for i:=0 to ops-1 do
  2041. with oper[i]^ do
  2042. begin
  2043. case typ of
  2044. top_regset:
  2045. begin
  2046. ot:=OT_REGLIST;
  2047. end;
  2048. top_reg :
  2049. begin
  2050. case getregtype(reg) of
  2051. R_INTREGISTER:
  2052. begin
  2053. ot:=OT_REG32 or OT_SHIFTEROP;
  2054. if getsupreg(reg)<8 then
  2055. ot:=ot or OT_REGLO
  2056. else if reg=NR_STACK_POINTER_REG then
  2057. ot:=ot or OT_REGSP;
  2058. end;
  2059. R_FPUREGISTER:
  2060. ot:=OT_FPUREG;
  2061. R_MMREGISTER:
  2062. ot:=OT_VREG;
  2063. R_SPECIALREGISTER:
  2064. ot:=OT_REGF;
  2065. else
  2066. internalerror(2005090901);
  2067. end;
  2068. end;
  2069. top_ref :
  2070. begin
  2071. if ref^.refaddr=addr_no then
  2072. begin
  2073. { create ot field }
  2074. { we should get the size here dependend on the
  2075. instruction }
  2076. if (ot and OT_SIZE_MASK)=0 then
  2077. ot:=OT_MEMORY or OT_BITS32
  2078. else
  2079. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  2080. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  2081. ot:=ot or OT_MEM_OFFS;
  2082. { if we need to fix a reference, we do it here }
  2083. { pc relative addressing }
  2084. if (ref^.base=NR_NO) and
  2085. (ref^.index=NR_NO) and
  2086. (ref^.shiftmode=SM_None)
  2087. { at least we should check if the destination symbol
  2088. is in a text section }
  2089. { and
  2090. (ref^.symbol^.owner="text") } then
  2091. ref^.base:=NR_PC;
  2092. { determine possible address modes }
  2093. if GenerateThumbCode or
  2094. GenerateThumb2Code then
  2095. begin
  2096. if (ref^.addressmode<>AM_OFFSET) then
  2097. ot:=ot or OT_AM2
  2098. else if (ref^.base=NR_PC) then
  2099. ot:=ot or OT_AM6
  2100. else if (ref^.base=NR_STACK_POINTER_REG) then
  2101. ot:=ot or OT_AM5
  2102. else if ref^.index=NR_NO then
  2103. ot:=ot or OT_AM4
  2104. else
  2105. ot:=ot or OT_AM3;
  2106. end;
  2107. if (ref^.base<>NR_NO) and
  2108. (opcode in [A_LDREX,A_LDREXB,A_LDREXH,A_LDREXD,
  2109. A_STREX,A_STREXB,A_STREXH,A_STREXD]) and
  2110. (
  2111. (ref^.addressmode=AM_OFFSET) and
  2112. (ref^.index=NR_NO) and
  2113. (ref^.shiftmode=SM_None) and
  2114. (ref^.offset=0)
  2115. ) then
  2116. ot:=ot or OT_AM6
  2117. else if (ref^.base<>NR_NO) and
  2118. (
  2119. (
  2120. (ref^.index=NR_NO) and
  2121. (ref^.shiftmode=SM_None) and
  2122. (ref^.offset>=-4097) and
  2123. (ref^.offset<=4097)
  2124. ) or
  2125. (
  2126. (ref^.shiftmode=SM_None) and
  2127. (ref^.offset=0)
  2128. ) or
  2129. (
  2130. (ref^.index<>NR_NO) and
  2131. (ref^.shiftmode<>SM_None) and
  2132. (ref^.shiftimm<=32) and
  2133. (ref^.offset=0)
  2134. )
  2135. ) then
  2136. ot:=ot or OT_AM2;
  2137. if (ref^.index<>NR_NO) and
  2138. (oppostfix in [PF_None,PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA,
  2139. PF_IAD,PF_DBD,PF_FDD,PF_EAD,
  2140. PF_IAS,PF_DBS,PF_FDS,PF_EAS,
  2141. PF_IAX,PF_DBX,PF_FDX,PF_EAX]) and
  2142. (
  2143. (ref^.base=NR_NO) and
  2144. (ref^.shiftmode=SM_None) and
  2145. (ref^.offset=0)
  2146. ) then
  2147. ot:=ot or OT_AM4;
  2148. end
  2149. else
  2150. begin
  2151. l:=ref^.offset;
  2152. currsym:=ObjData.symbolref(ref^.symbol);
  2153. if assigned(currsym) then
  2154. inc(l,currsym.address);
  2155. relsize:=(InsOffset+2)-l;
  2156. if (relsize<-33554428) or (relsize>33554428) then
  2157. ot:=OT_IMM32
  2158. else
  2159. ot:=OT_IMM24;
  2160. end;
  2161. end;
  2162. top_local :
  2163. begin
  2164. { we should get the size here dependend on the
  2165. instruction }
  2166. if (ot and OT_SIZE_MASK)=0 then
  2167. ot:=OT_MEMORY or OT_BITS32
  2168. else
  2169. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  2170. end;
  2171. top_const :
  2172. begin
  2173. ot:=OT_IMMEDIATE;
  2174. if (val=0) then
  2175. ot:=ot_immediatezero
  2176. else if is_shifter_const(val,dummy) then
  2177. ot:=OT_IMMSHIFTER
  2178. else if GenerateThumb2Code and is_thumb32_imm(val) then
  2179. ot:=OT_IMMSHIFTER
  2180. else
  2181. ot:=OT_IMM32
  2182. end;
  2183. top_none :
  2184. begin
  2185. { generated when there was an error in the
  2186. assembler reader. It never happends when generating
  2187. assembler }
  2188. end;
  2189. top_shifterop:
  2190. begin
  2191. ot:=OT_SHIFTEROP;
  2192. end;
  2193. top_conditioncode:
  2194. begin
  2195. ot:=OT_CONDITION;
  2196. end;
  2197. top_specialreg:
  2198. begin
  2199. ot:=OT_REGS;
  2200. end;
  2201. top_modeflags:
  2202. begin
  2203. ot:=OT_MODEFLAGS;
  2204. end;
  2205. top_realconst:
  2206. begin
  2207. ot:=OT_IMMEDIATEMM;
  2208. end;
  2209. else
  2210. internalerror(2004022623);
  2211. end;
  2212. end;
  2213. end;
  2214. function taicpu.Matches(p:PInsEntry):longint;
  2215. { * IF_SM stands for Size Match: any operand whose size is not
  2216. * explicitly specified by the template is `really' intended to be
  2217. * the same size as the first size-specified operand.
  2218. * Non-specification is tolerated in the input instruction, but
  2219. * _wrong_ specification is not.
  2220. *
  2221. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  2222. * three-operand instructions such as SHLD: it implies that the
  2223. * first two operands must match in size, but that the third is
  2224. * required to be _unspecified_.
  2225. *
  2226. * IF_SB invokes Size Byte: operands with unspecified size in the
  2227. * template are really bytes, and so no non-byte specification in
  2228. * the input instruction will be tolerated. IF_SW similarly invokes
  2229. * Size Word, and IF_SD invokes Size Doubleword.
  2230. *
  2231. * (The default state if neither IF_SM nor IF_SM2 is specified is
  2232. * that any operand with unspecified size in the template is
  2233. * required to have unspecified size in the instruction too...)
  2234. }
  2235. var
  2236. i{,j,asize,oprs} : longint;
  2237. {siz : array[0..3] of longint;}
  2238. begin
  2239. Matches:=100;
  2240. { Check the opcode and operands }
  2241. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  2242. begin
  2243. Matches:=0;
  2244. exit;
  2245. end;
  2246. { check ARM instruction version }
  2247. if (p^.flags and fArmVMask)=0 then
  2248. begin
  2249. Matches:=0;
  2250. exit;
  2251. end;
  2252. { check ARM instruction type }
  2253. if (p^.flags and fArmMask)=0 then
  2254. begin
  2255. Matches:=0;
  2256. exit;
  2257. end;
  2258. { Check wideformat flag }
  2259. if (cf_wideformat in flags) and ((p^.flags and IF_WIDE)=0) then
  2260. begin
  2261. matches:=0;
  2262. exit;
  2263. end;
  2264. { Check that no spurious colons or TOs are present }
  2265. for i:=0 to p^.ops-1 do
  2266. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  2267. begin
  2268. Matches:=0;
  2269. exit;
  2270. end;
  2271. { Check that the operand flags all match up }
  2272. for i:=0 to p^.ops-1 do
  2273. begin
  2274. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  2275. ((p^.optypes[i] and OT_SIZE_MASK) and
  2276. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  2277. begin
  2278. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  2279. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  2280. begin
  2281. Matches:=0;
  2282. exit;
  2283. end
  2284. else if ((p^.optypes[i] and OT_OPT_SIZE)<>0) and
  2285. ((p^.optypes[i] and OT_OPT_SIZE)<>(oper[i]^.ot and OT_OPT_SIZE)) then
  2286. begin
  2287. Matches:=0;
  2288. exit;
  2289. end
  2290. else
  2291. Matches:=1;
  2292. end;
  2293. end;
  2294. { check postfixes:
  2295. the existance of a certain postfix requires a
  2296. particular code }
  2297. { update condition flags
  2298. or floating point single }
  2299. if (oppostfix=PF_S) and
  2300. not(p^.code[0] in [#$04..#$0F,#$14..#$16,#$29,#$30,#$60..#$6B,#$80..#$82,#$A0..#$A2,#$44,#$94,#$42,#$92]) then
  2301. begin
  2302. Matches:=0;
  2303. exit;
  2304. end;
  2305. { floating point size }
  2306. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  2307. not(p^.code[0] in [
  2308. // FPA
  2309. #$A0..#$A2,
  2310. // old-school VFP
  2311. #$42,#$92,
  2312. // vldm/vstm
  2313. #$44,#$94]) then
  2314. begin
  2315. Matches:=0;
  2316. exit;
  2317. end;
  2318. { multiple load/store address modes }
  2319. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  2320. not(p^.code[0] in [
  2321. // ldr,str,ldrb,strb
  2322. #$17,
  2323. // stm,ldm
  2324. #$26,#$69,#$8C,
  2325. // vldm/vstm
  2326. #$44,#$94
  2327. ]) then
  2328. begin
  2329. Matches:=0;
  2330. exit;
  2331. end;
  2332. { we shouldn't see any opsize prefixes here }
  2333. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  2334. begin
  2335. Matches:=0;
  2336. exit;
  2337. end;
  2338. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  2339. begin
  2340. Matches:=0;
  2341. exit;
  2342. end;
  2343. { Check thumb flags }
  2344. if p^.code[0] in [#$60..#$61] then
  2345. begin
  2346. if (p^.code[0]=#$60) and
  2347. (GenerateThumb2Code and
  2348. ((not(cf_inIT in flags)) and (oppostfix<>PF_S)) or
  2349. ((cf_inIT in flags) and (condition=C_None))) then
  2350. begin
  2351. Matches:=0;
  2352. exit;
  2353. end
  2354. else if (p^.code[0]=#$61) and
  2355. (oppostfix=PF_S) then
  2356. begin
  2357. Matches:=0;
  2358. exit;
  2359. end;
  2360. end
  2361. else if p^.code[0]=#$62 then
  2362. begin
  2363. if GenerateThumb2Code and
  2364. (condition<>C_None) and
  2365. (not(cf_inIT in flags)) and
  2366. (not(cf_lastinIT in flags)) then
  2367. begin
  2368. Matches:=0;
  2369. exit;
  2370. end;
  2371. end
  2372. else if p^.code[0]=#$63 then
  2373. begin
  2374. if cf_inIT in flags then
  2375. begin
  2376. Matches:=0;
  2377. exit;
  2378. end;
  2379. end
  2380. else if p^.code[0]=#$64 then
  2381. begin
  2382. if (opcode=A_MUL) then
  2383. begin
  2384. if (ops=3) and
  2385. ((oper[2]^.typ<>top_reg) or
  2386. (oper[0]^.reg<>oper[2]^.reg)) then
  2387. begin
  2388. matches:=0;
  2389. exit;
  2390. end;
  2391. end;
  2392. end
  2393. else if p^.code[0]=#$6B then
  2394. begin
  2395. if (cf_inIT in flags) or
  2396. (oppostfix<>PF_S) then
  2397. begin
  2398. Matches:=0;
  2399. exit;
  2400. end;
  2401. end;
  2402. { Check operand sizes }
  2403. { as default an untyped size can get all the sizes, this is different
  2404. from nasm, but else we need to do a lot checking which opcodes want
  2405. size or not with the automatic size generation }
  2406. (*
  2407. asize:=longint($ffffffff);
  2408. if (p^.flags and IF_SB)<>0 then
  2409. asize:=OT_BITS8
  2410. else if (p^.flags and IF_SW)<>0 then
  2411. asize:=OT_BITS16
  2412. else if (p^.flags and IF_SD)<>0 then
  2413. asize:=OT_BITS32;
  2414. if (p^.flags and IF_ARMASK)<>0 then
  2415. begin
  2416. siz[0]:=0;
  2417. siz[1]:=0;
  2418. siz[2]:=0;
  2419. if (p^.flags and IF_AR0)<>0 then
  2420. siz[0]:=asize
  2421. else if (p^.flags and IF_AR1)<>0 then
  2422. siz[1]:=asize
  2423. else if (p^.flags and IF_AR2)<>0 then
  2424. siz[2]:=asize;
  2425. end
  2426. else
  2427. begin
  2428. { we can leave because the size for all operands is forced to be
  2429. the same
  2430. but not if IF_SB IF_SW or IF_SD is set PM }
  2431. if asize=-1 then
  2432. exit;
  2433. siz[0]:=asize;
  2434. siz[1]:=asize;
  2435. siz[2]:=asize;
  2436. end;
  2437. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  2438. begin
  2439. if (p^.flags and IF_SM2)<>0 then
  2440. oprs:=2
  2441. else
  2442. oprs:=p^.ops;
  2443. for i:=0 to oprs-1 do
  2444. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  2445. begin
  2446. for j:=0 to oprs-1 do
  2447. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  2448. break;
  2449. end;
  2450. end
  2451. else
  2452. oprs:=2;
  2453. { Check operand sizes }
  2454. for i:=0 to p^.ops-1 do
  2455. begin
  2456. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  2457. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  2458. { Immediates can always include smaller size }
  2459. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  2460. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  2461. Matches:=2;
  2462. end;
  2463. *)
  2464. end;
  2465. function taicpu.calcsize(p:PInsEntry):shortint;
  2466. begin
  2467. result:=4;
  2468. end;
  2469. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  2470. begin
  2471. Result:=False; { unimplemented }
  2472. end;
  2473. procedure taicpu.Swapoperands;
  2474. begin
  2475. end;
  2476. function taicpu.FindInsentry(objdata:TObjData):boolean;
  2477. var
  2478. i : longint;
  2479. begin
  2480. result:=false;
  2481. { Things which may only be done once, not when a second pass is done to
  2482. optimize }
  2483. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  2484. begin
  2485. { create the .ot fields }
  2486. create_ot(objdata);
  2487. BuildArmMasks(objdata);
  2488. { set the file postion }
  2489. current_filepos:=fileinfo;
  2490. end
  2491. else
  2492. begin
  2493. { we've already an insentry so it's valid }
  2494. result:=true;
  2495. exit;
  2496. end;
  2497. { Lookup opcode in the table }
  2498. InsSize:=-1;
  2499. i:=instabcache^[opcode];
  2500. if i=-1 then
  2501. begin
  2502. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  2503. exit;
  2504. end;
  2505. insentry:=@instab[i];
  2506. while (insentry^.opcode=opcode) do
  2507. begin
  2508. if matches(insentry)=100 then
  2509. begin
  2510. result:=true;
  2511. exit;
  2512. end;
  2513. inc(i);
  2514. insentry:=@instab[i];
  2515. end;
  2516. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2517. { No instruction found, set insentry to nil and inssize to -1 }
  2518. insentry:=nil;
  2519. inssize:=-1;
  2520. end;
  2521. procedure taicpu.gencode(objdata:TObjData);
  2522. const
  2523. CondVal : array[TAsmCond] of byte=(
  2524. $E, $0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $A,
  2525. $B, $C, $D, $E, 0);
  2526. var
  2527. bytes, rd, rm, rn, d, m, n : dword;
  2528. bytelen : longint;
  2529. dp_operation : boolean;
  2530. i_field : byte;
  2531. currsym : TObjSymbol;
  2532. offset : longint;
  2533. refoper : poper;
  2534. msb : longint;
  2535. r: byte;
  2536. imm : dword;
  2537. count : integer;
  2538. singlerec : tcompsinglerec;
  2539. doublerec : tcompdoublerec;
  2540. procedure setshifterop(op : byte);
  2541. var
  2542. r : byte;
  2543. imm : dword;
  2544. count : integer;
  2545. begin
  2546. case oper[op]^.typ of
  2547. top_const:
  2548. begin
  2549. i_field:=1;
  2550. if oper[op]^.val and $ff=oper[op]^.val then
  2551. bytes:=bytes or dword(oper[op]^.val)
  2552. else
  2553. begin
  2554. { calc rotate and adjust imm }
  2555. count:=0;
  2556. r:=0;
  2557. imm:=dword(oper[op]^.val);
  2558. repeat
  2559. imm:=RolDWord(imm, 2);
  2560. inc(r);
  2561. inc(count);
  2562. if count > 32 then
  2563. begin
  2564. message1(asmw_e_invalid_opcode_and_operands, 'invalid shifter imm');
  2565. exit;
  2566. end;
  2567. until (imm and $ff)=imm;
  2568. bytes:=bytes or (r shl 8) or imm;
  2569. end;
  2570. end;
  2571. top_reg:
  2572. begin
  2573. i_field:=0;
  2574. bytes:=bytes or getsupreg(oper[op]^.reg);
  2575. { does a real shifter op follow? }
  2576. if (op+1<opercnt) and (oper[op+1]^.typ=top_shifterop) then
  2577. with oper[op+1]^.shifterop^ do
  2578. begin
  2579. bytes:=bytes or ((shiftimm and $1F) shl 7);
  2580. if shiftmode<>SM_RRX then
  2581. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2582. else
  2583. bytes:=bytes or (3 shl 5);
  2584. if getregtype(rs) <> R_INVALIDREGISTER then
  2585. begin
  2586. bytes:=bytes or (1 shl 4);
  2587. bytes:=bytes or (getsupreg(rs) shl 8);
  2588. end
  2589. end;
  2590. end;
  2591. else
  2592. internalerror(2005091103);
  2593. end;
  2594. end;
  2595. function MakeRegList(reglist: tcpuregisterset): word;
  2596. var
  2597. i, w: integer;
  2598. begin
  2599. result:=0;
  2600. w:=0;
  2601. for i:=RS_R0 to RS_R15 do
  2602. begin
  2603. if i in reglist then
  2604. result:=result or (1 shl w);
  2605. inc(w);
  2606. end;
  2607. end;
  2608. function getcoproc(reg: tregister): byte;
  2609. begin
  2610. if reg=NR_p15 then
  2611. result:=15
  2612. else
  2613. begin
  2614. Message1(asmw_e_invalid_opcode_and_operands,'Invalid coprocessor port');
  2615. result:=0;
  2616. end;
  2617. end;
  2618. function getcoprocreg(reg: tregister): byte;
  2619. var
  2620. tmpr: tregister;
  2621. begin
  2622. { FIXME: temp variable r is needed here to avoid Internal error 20060521 }
  2623. { while compiling the compiler. }
  2624. tmpr:=NR_CR0;
  2625. result:=getsupreg(reg)-getsupreg(tmpr);
  2626. end;
  2627. function getmmreg(reg: tregister): byte;
  2628. begin
  2629. case reg of
  2630. NR_D0: result:=0;
  2631. NR_D1: result:=1;
  2632. NR_D2: result:=2;
  2633. NR_D3: result:=3;
  2634. NR_D4: result:=4;
  2635. NR_D5: result:=5;
  2636. NR_D6: result:=6;
  2637. NR_D7: result:=7;
  2638. NR_D8: result:=8;
  2639. NR_D9: result:=9;
  2640. NR_D10: result:=10;
  2641. NR_D11: result:=11;
  2642. NR_D12: result:=12;
  2643. NR_D13: result:=13;
  2644. NR_D14: result:=14;
  2645. NR_D15: result:=15;
  2646. NR_D16: result:=16;
  2647. NR_D17: result:=17;
  2648. NR_D18: result:=18;
  2649. NR_D19: result:=19;
  2650. NR_D20: result:=20;
  2651. NR_D21: result:=21;
  2652. NR_D22: result:=22;
  2653. NR_D23: result:=23;
  2654. NR_D24: result:=24;
  2655. NR_D25: result:=25;
  2656. NR_D26: result:=26;
  2657. NR_D27: result:=27;
  2658. NR_D28: result:=28;
  2659. NR_D29: result:=29;
  2660. NR_D30: result:=30;
  2661. NR_D31: result:=31;
  2662. NR_S0: result:=0;
  2663. NR_S1: result:=1;
  2664. NR_S2: result:=2;
  2665. NR_S3: result:=3;
  2666. NR_S4: result:=4;
  2667. NR_S5: result:=5;
  2668. NR_S6: result:=6;
  2669. NR_S7: result:=7;
  2670. NR_S8: result:=8;
  2671. NR_S9: result:=9;
  2672. NR_S10: result:=10;
  2673. NR_S11: result:=11;
  2674. NR_S12: result:=12;
  2675. NR_S13: result:=13;
  2676. NR_S14: result:=14;
  2677. NR_S15: result:=15;
  2678. NR_S16: result:=16;
  2679. NR_S17: result:=17;
  2680. NR_S18: result:=18;
  2681. NR_S19: result:=19;
  2682. NR_S20: result:=20;
  2683. NR_S21: result:=21;
  2684. NR_S22: result:=22;
  2685. NR_S23: result:=23;
  2686. NR_S24: result:=24;
  2687. NR_S25: result:=25;
  2688. NR_S26: result:=26;
  2689. NR_S27: result:=27;
  2690. NR_S28: result:=28;
  2691. NR_S29: result:=29;
  2692. NR_S30: result:=30;
  2693. NR_S31: result:=31;
  2694. else
  2695. result:=0;
  2696. end;
  2697. end;
  2698. procedure encodethumbimm(imm: longword);
  2699. var
  2700. imm12, tmp: tcgint;
  2701. shift: integer;
  2702. found: boolean;
  2703. begin
  2704. found:=true;
  2705. if (imm and $FF) = imm then
  2706. imm12:=imm
  2707. else if ((imm shr 16)=(imm and $FFFF)) and
  2708. ((imm and $FF00FF00) = 0) then
  2709. imm12:=(imm and $ff) or ($1 shl 8)
  2710. else if ((imm shr 16)=(imm and $FFFF)) and
  2711. ((imm and $00FF00FF) = 0) then
  2712. imm12:=((imm shr 8) and $ff) or ($2 shl 8)
  2713. else if ((imm shr 16)=(imm and $FFFF)) and
  2714. (((imm shr 8) and $FF)=(imm and $FF)) then
  2715. imm12:=(imm and $ff) or ($3 shl 8)
  2716. else
  2717. begin
  2718. found:=false;
  2719. imm12:=0;
  2720. for shift:=1 to 31 do
  2721. begin
  2722. tmp:=RolDWord(imm,shift);
  2723. if ((tmp and $FF)=tmp) and
  2724. ((tmp and $80)=$80) then
  2725. begin
  2726. imm12:=(tmp and $7F) or (shift shl 7);
  2727. found:=true;
  2728. break;
  2729. end;
  2730. end;
  2731. end;
  2732. if found then
  2733. begin
  2734. bytes:=bytes or (imm12 and $FF);
  2735. bytes:=bytes or (((imm12 shr 8) and $7) shl 12);
  2736. bytes:=bytes or (((imm12 shr 11) and $1) shl 26);
  2737. end
  2738. else
  2739. Message1(asmw_e_value_exceeds_bounds, IntToStr(imm));
  2740. end;
  2741. procedure setthumbshift(op: byte; is_sat: boolean = false);
  2742. var
  2743. shift,typ: byte;
  2744. begin
  2745. shift:=0;
  2746. typ:=0;
  2747. case oper[op]^.shifterop^.shiftmode of
  2748. SM_None: ;
  2749. SM_LSL: begin typ:=0; shift:=oper[op]^.shifterop^.shiftimm; end;
  2750. SM_LSR: begin typ:=1; shift:=oper[op]^.shifterop^.shiftimm; if shift=32 then shift:=0; end;
  2751. SM_ASR: begin typ:=2; shift:=oper[op]^.shifterop^.shiftimm; if shift=32 then shift:=0; end;
  2752. SM_ROR: begin typ:=3; shift:=oper[op]^.shifterop^.shiftimm; if shift=0 then message(asmw_e_invalid_opcode_and_operands); end;
  2753. SM_RRX: begin typ:=3; shift:=0; end;
  2754. end;
  2755. if is_sat then
  2756. begin
  2757. bytes:=bytes or ((typ and 1) shl 5);
  2758. bytes:=bytes or ((typ shr 1) shl 21);
  2759. end
  2760. else
  2761. bytes:=bytes or (typ shl 4);
  2762. bytes:=bytes or (shift and $3) shl 6;
  2763. bytes:=bytes or ((shift and $1C) shr 2) shl 12;
  2764. end;
  2765. begin
  2766. bytes:=$0;
  2767. bytelen:=4;
  2768. i_field:=0;
  2769. { evaluate and set condition code }
  2770. bytes:=bytes or (CondVal[condition] shl 28);
  2771. { condition code allowed? }
  2772. { setup rest of the instruction }
  2773. case insentry^.code[0] of
  2774. #$01: // B/BL
  2775. begin
  2776. { set instruction code }
  2777. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2778. { set offset }
  2779. if oper[0]^.typ=top_const then
  2780. bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
  2781. else
  2782. begin
  2783. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  2784. { tlscall is not relative so ignore the offset }
  2785. if oper[0]^.ref^.refaddr<>addr_tlscall then
  2786. bytes:=bytes or (((oper[0]^.ref^.offset-8) shr 2) and $ffffff);
  2787. if (opcode<>A_BL) or (condition<>C_None) then
  2788. objdata.writereloc(aint(bytes),4,currsym,RELOC_RELATIVE_24)
  2789. else
  2790. case oper[0]^.ref^.refaddr of
  2791. addr_pic:
  2792. objdata.writereloc(aint(bytes),4,currsym,RELOC_ARM_CALL);
  2793. addr_full:
  2794. objdata.writereloc(aint(bytes),4,currsym,RELOC_RELATIVE_CALL);
  2795. addr_tlscall:
  2796. objdata.writereloc(aint(bytes),4,currsym,RELOC_TLS_CALL);
  2797. else
  2798. Internalerror(2019092903);
  2799. end;
  2800. exit;
  2801. end;
  2802. end;
  2803. #$02:
  2804. begin
  2805. { set instruction code }
  2806. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2807. { set code }
  2808. bytes:=bytes or (oper[0]^.val and $FFFFFF);
  2809. end;
  2810. #$03:
  2811. begin // BLX/BX
  2812. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2813. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2814. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2815. bytes:=bytes or ord(insentry^.code[4]);
  2816. bytes:=bytes or getsupreg(oper[0]^.reg);
  2817. end;
  2818. #$04..#$07: // SUB
  2819. begin
  2820. { set instruction code }
  2821. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2822. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2823. { set destination }
  2824. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2825. { set Rn }
  2826. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  2827. { create shifter op }
  2828. setshifterop(2);
  2829. { set I field }
  2830. bytes:=bytes or (i_field shl 25);
  2831. { set S if necessary }
  2832. if oppostfix=PF_S then
  2833. bytes:=bytes or (1 shl 20);
  2834. end;
  2835. #$08,#$0A,#$0B: // MOV
  2836. begin
  2837. { set instruction code }
  2838. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2839. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2840. { set destination }
  2841. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2842. { create shifter op }
  2843. setshifterop(1);
  2844. { set I field }
  2845. bytes:=bytes or (i_field shl 25);
  2846. { set S if necessary }
  2847. if oppostfix=PF_S then
  2848. bytes:=bytes or (1 shl 20);
  2849. end;
  2850. #$0C,#$0E,#$0F: // CMP
  2851. begin
  2852. { set instruction code }
  2853. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2854. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2855. { set destination }
  2856. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  2857. { create shifter op }
  2858. setshifterop(1);
  2859. { set I field }
  2860. bytes:=bytes or (i_field shl 25);
  2861. { always set S bit }
  2862. bytes:=bytes or (1 shl 20);
  2863. end;
  2864. #$10: // MRS
  2865. begin
  2866. { set instruction code }
  2867. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2868. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2869. { set destination }
  2870. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2871. case oper[1]^.reg of
  2872. NR_APSR,NR_CPSR:;
  2873. NR_SPSR:
  2874. begin
  2875. bytes:=bytes or (1 shl 22);
  2876. end;
  2877. else
  2878. Message(asmw_e_invalid_opcode_and_operands);
  2879. end;
  2880. end;
  2881. #$12,#$13: // MSR
  2882. begin
  2883. { set instruction code }
  2884. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2885. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2886. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2887. { set destination }
  2888. if oper[0]^.typ=top_specialreg then
  2889. begin
  2890. if (oper[0]^.specialreg<>NR_CPSR) and
  2891. (oper[0]^.specialreg<>NR_SPSR) then
  2892. Message1(asmw_e_invalid_opcode_and_operands, '"Invalid special reg"');
  2893. if srC in oper[0]^.specialflags then
  2894. bytes:=bytes or (1 shl 16);
  2895. if srX in oper[0]^.specialflags then
  2896. bytes:=bytes or (1 shl 17);
  2897. if srS in oper[0]^.specialflags then
  2898. bytes:=bytes or (1 shl 18);
  2899. if srF in oper[0]^.specialflags then
  2900. bytes:=bytes or (1 shl 19);
  2901. { Set R bit }
  2902. if oper[0]^.specialreg=NR_SPSR then
  2903. bytes:=bytes or (1 shl 22);
  2904. end
  2905. else
  2906. case oper[0]^.reg of
  2907. NR_APSR_nzcvq: bytes:=bytes or (2 shl 18);
  2908. NR_APSR_g: bytes:=bytes or (1 shl 18);
  2909. NR_APSR_nzcvqg: bytes:=bytes or (3 shl 18);
  2910. else
  2911. Message1(asmw_e_invalid_opcode_and_operands, 'Invalid combination APSR bits used');
  2912. end;
  2913. setshifterop(1);
  2914. end;
  2915. #$14: // MUL/MLA r1,r2,r3
  2916. begin
  2917. { set instruction code }
  2918. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2919. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2920. bytes:=bytes or ord(insentry^.code[3]);
  2921. { set regs }
  2922. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2923. bytes:=bytes or getsupreg(oper[1]^.reg);
  2924. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2925. if oppostfix in [PF_S] then
  2926. bytes:=bytes or (1 shl 20);
  2927. end;
  2928. #$15: // MUL/MLA r1,r2,r3,r4
  2929. begin
  2930. { set instruction code }
  2931. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2932. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2933. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2934. { set regs }
  2935. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2936. bytes:=bytes or getsupreg(oper[1]^.reg);
  2937. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2938. if ops>3 then
  2939. bytes:=bytes or getsupreg(oper[3]^.reg) shl 12
  2940. else
  2941. bytes:=bytes or ord(insentry^.code[4]) shl 12;
  2942. if oppostfix in [PF_R,PF_X] then
  2943. bytes:=bytes or (1 shl 5);
  2944. if oppostfix in [PF_S] then
  2945. bytes:=bytes or (1 shl 20);
  2946. end;
  2947. #$16: // MULL r1,r2,r3,r4
  2948. begin
  2949. { set instruction code }
  2950. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2951. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2952. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2953. { set regs }
  2954. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2955. if (ops=3) and (opcode=A_PKHTB) then
  2956. begin
  2957. bytes:=bytes or getsupreg(oper[1]^.reg);
  2958. bytes:=bytes or getsupreg(oper[2]^.reg) shl 16;
  2959. end
  2960. else
  2961. begin
  2962. bytes:=bytes or getsupreg(oper[1]^.reg) shl 16;
  2963. bytes:=bytes or getsupreg(oper[2]^.reg);
  2964. end;
  2965. if ops=4 then
  2966. begin
  2967. if oper[3]^.typ=top_shifterop then
  2968. begin
  2969. if opcode in [A_PKHBT,A_PKHTB] then
  2970. begin
  2971. if ((opcode=A_PKHTB) and
  2972. (oper[3]^.shifterop^.shiftmode <> SM_ASR)) or
  2973. ((opcode=A_PKHBT) and
  2974. (oper[3]^.shifterop^.shiftmode <> SM_LSL)) or
  2975. (oper[3]^.shifterop^.rs<>NR_NO) then
  2976. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2977. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
  2978. end
  2979. else
  2980. begin
  2981. if (oper[3]^.shifterop^.shiftmode<>sm_ror) or
  2982. (oper[3]^.shifterop^.rs<>NR_NO) or
  2983. (not (oper[3]^.shifterop^.shiftimm in [0,8,16,24])) then
  2984. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2985. bytes:=bytes or (((oper[3]^.shifterop^.shiftimm shr 3) and $3) shl 10);
  2986. end;
  2987. end
  2988. else
  2989. bytes:=bytes or getsupreg(oper[3]^.reg) shl 8;
  2990. end;
  2991. if PF_S=oppostfix then
  2992. bytes:=bytes or (1 shl 20);
  2993. if PF_X=oppostfix then
  2994. bytes:=bytes or (1 shl 5);
  2995. end;
  2996. #$17: // LDR/STR
  2997. begin
  2998. { set instruction code }
  2999. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3000. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3001. { set Rn and Rd }
  3002. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  3003. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  3004. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  3005. begin
  3006. { set offset }
  3007. offset:=0;
  3008. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3009. if assigned(currsym) then
  3010. offset:=currsym.offset-insoffset-8;
  3011. offset:=offset+oper[1]^.ref^.offset;
  3012. if offset>=0 then
  3013. { set U flag }
  3014. bytes:=bytes or (1 shl 23)
  3015. else
  3016. offset:=-offset;
  3017. bytes:=bytes or (offset and $FFF);
  3018. end
  3019. else
  3020. begin
  3021. { set U flag }
  3022. if oper[1]^.ref^.signindex>=0 then
  3023. bytes:=bytes or (1 shl 23);
  3024. { set I flag }
  3025. bytes:=bytes or (1 shl 25);
  3026. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  3027. { set shift }
  3028. with oper[1]^.ref^ do
  3029. if shiftmode<>SM_None then
  3030. begin
  3031. bytes:=bytes or ((shiftimm and $1F) shl 7);
  3032. if shiftmode<>SM_RRX then
  3033. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  3034. else
  3035. bytes:=bytes or (3 shl 5);
  3036. end
  3037. end;
  3038. { set W bit }
  3039. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  3040. bytes:=bytes or (1 shl 21);
  3041. { set P bit if necessary }
  3042. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  3043. bytes:=bytes or (1 shl 24);
  3044. end;
  3045. #$18: // LDREX/STREX
  3046. begin
  3047. { set instruction code }
  3048. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3049. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3050. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3051. bytes:=bytes or ord(insentry^.code[4]);
  3052. { set Rn and Rd }
  3053. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  3054. if (ops=3) then
  3055. begin
  3056. if opcode<>A_LDREXD then
  3057. bytes:=bytes or getsupreg(oper[1]^.reg);
  3058. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  3059. end
  3060. else if (ops=4) then // STREXD
  3061. begin
  3062. if opcode<>A_LDREXD then
  3063. bytes:=bytes or getsupreg(oper[1]^.reg);
  3064. bytes:=bytes or (getsupreg(oper[3]^.ref^.base) shl 16);
  3065. end
  3066. else
  3067. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 16);
  3068. end;
  3069. #$19: // LDRD/STRD
  3070. begin
  3071. { set instruction code }
  3072. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3073. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3074. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3075. bytes:=bytes or ord(insentry^.code[4]);
  3076. { set Rn and Rd }
  3077. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  3078. refoper:=oper[1];
  3079. if ops=3 then
  3080. refoper:=oper[2];
  3081. bytes:=bytes or getsupreg(refoper^.ref^.base) shl 16;
  3082. if getregtype(refoper^.ref^.index)=R_INVALIDREGISTER then
  3083. begin
  3084. bytes:=bytes or (1 shl 22);
  3085. { set offset }
  3086. offset:=0;
  3087. currsym:=objdata.symbolref(refoper^.ref^.symbol);
  3088. if assigned(currsym) then
  3089. offset:=currsym.offset-insoffset-8;
  3090. offset:=offset+refoper^.ref^.offset;
  3091. if offset>=0 then
  3092. { set U flag }
  3093. bytes:=bytes or (1 shl 23)
  3094. else
  3095. offset:=-offset;
  3096. bytes:=bytes or (offset and $F);
  3097. bytes:=bytes or ((offset and $F0) shl 4);
  3098. end
  3099. else
  3100. begin
  3101. { set U flag }
  3102. if refoper^.ref^.signindex>=0 then
  3103. bytes:=bytes or (1 shl 23);
  3104. bytes:=bytes or getsupreg(refoper^.ref^.index);
  3105. end;
  3106. { set W bit }
  3107. if refoper^.ref^.addressmode=AM_PREINDEXED then
  3108. bytes:=bytes or (1 shl 21);
  3109. { set P bit if necessary }
  3110. if refoper^.ref^.addressmode<>AM_POSTINDEXED then
  3111. bytes:=bytes or (1 shl 24);
  3112. end;
  3113. #$1A: // QADD/QSUB
  3114. begin
  3115. { set instruction code }
  3116. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  3117. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  3118. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  3119. { set regs }
  3120. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  3121. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0;
  3122. bytes:=bytes or getsupreg(oper[2]^.reg) shl 16;
  3123. end;
  3124. #$1B:
  3125. begin
  3126. { set instruction code }
  3127. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  3128. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  3129. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  3130. { set regs }
  3131. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  3132. bytes:=bytes or getsupreg(oper[1]^.reg);
  3133. if ops=3 then
  3134. begin
  3135. if (oper[2]^.shifterop^.shiftmode<>sm_ror) or
  3136. (oper[2]^.shifterop^.rs<>NR_NO) or
  3137. (not (oper[2]^.shifterop^.shiftimm in [0,8,16,24])) then
  3138. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  3139. bytes:=bytes or (((oper[2]^.shifterop^.shiftimm shr 3) and $3) shl 10);
  3140. end;
  3141. end;
  3142. #$1C: // MCR/MRC
  3143. begin
  3144. { set instruction code }
  3145. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  3146. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  3147. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  3148. { set regs and operands }
  3149. bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
  3150. bytes:=bytes or ((oper[1]^.val and $7) shl 21);
  3151. bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
  3152. bytes:=bytes or getcoprocreg(oper[3]^.reg) shl 16;
  3153. bytes:=bytes or getcoprocreg(oper[4]^.reg);
  3154. if ops > 5 then
  3155. bytes:=bytes or ((oper[5]^.val and $7) shl 5);
  3156. end;
  3157. #$1D: // MCRR/MRRC
  3158. begin
  3159. { set instruction code }
  3160. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  3161. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  3162. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  3163. { set regs and operands }
  3164. bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
  3165. bytes:=bytes or ((oper[1]^.val and $7) shl 4);
  3166. bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
  3167. bytes:=bytes or getsupreg(oper[3]^.reg) shl 16;
  3168. bytes:=bytes or getcoprocreg(oper[4]^.reg);
  3169. end;
  3170. #$1E: // LDRHT/STRHT
  3171. begin
  3172. { set instruction code }
  3173. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3174. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3175. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3176. bytes:=bytes or ord(insentry^.code[4]);
  3177. { set Rn and Rd }
  3178. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  3179. refoper:=oper[1];
  3180. bytes:=bytes or getsupreg(refoper^.ref^.base) shl 16;
  3181. if getregtype(refoper^.ref^.index)=R_INVALIDREGISTER then
  3182. begin
  3183. bytes:=bytes or (1 shl 22);
  3184. { set offset }
  3185. offset:=0;
  3186. currsym:=objdata.symbolref(refoper^.ref^.symbol);
  3187. if assigned(currsym) then
  3188. offset:=currsym.offset-insoffset-8;
  3189. offset:=offset+refoper^.ref^.offset;
  3190. if offset>=0 then
  3191. { set U flag }
  3192. bytes:=bytes or (1 shl 23)
  3193. else
  3194. offset:=-offset;
  3195. bytes:=bytes or (offset and $F);
  3196. bytes:=bytes or ((offset and $F0) shl 4);
  3197. end
  3198. else
  3199. begin
  3200. { set U flag }
  3201. if refoper^.ref^.signindex>=0 then
  3202. bytes:=bytes or (1 shl 23);
  3203. bytes:=bytes or getsupreg(refoper^.ref^.index);
  3204. end;
  3205. end;
  3206. #$22: // LDRH/STRH
  3207. begin
  3208. { set instruction code }
  3209. bytes:=bytes or (ord(insentry^.code[1]) shl 16);
  3210. bytes:=bytes or ord(insentry^.code[2]);
  3211. { src/dest register (Rd) }
  3212. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  3213. { base register (Rn) }
  3214. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  3215. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  3216. begin
  3217. bytes:=bytes or (1 shl 22); // with immediate offset
  3218. offset:=oper[1]^.ref^.offset;
  3219. if offset>=0 then
  3220. { set U flag }
  3221. bytes:=bytes or (1 shl 23)
  3222. else
  3223. offset:=-offset;
  3224. bytes:=bytes or (offset and $F);
  3225. bytes:=bytes or ((offset and $F0) shl 4);
  3226. end
  3227. else
  3228. begin
  3229. { set U flag }
  3230. if oper[1]^.ref^.signindex>=0 then
  3231. bytes:=bytes or (1 shl 23);
  3232. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  3233. end;
  3234. { set W bit }
  3235. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  3236. bytes:=bytes or (1 shl 21);
  3237. { set P bit if necessary }
  3238. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  3239. bytes:=bytes or (1 shl 24);
  3240. end;
  3241. #$25: // PLD/PLI
  3242. begin
  3243. { set instruction code }
  3244. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3245. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3246. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3247. bytes:=bytes or ord(insentry^.code[4]);
  3248. { set Rn and Rd }
  3249. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  3250. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  3251. begin
  3252. { set offset }
  3253. offset:=0;
  3254. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  3255. if assigned(currsym) then
  3256. offset:=currsym.offset-insoffset-8;
  3257. offset:=offset+oper[0]^.ref^.offset;
  3258. if offset>=0 then
  3259. begin
  3260. { set U flag }
  3261. bytes:=bytes or (1 shl 23);
  3262. bytes:=bytes or offset
  3263. end
  3264. else
  3265. begin
  3266. offset:=-offset;
  3267. bytes:=bytes or offset
  3268. end;
  3269. end
  3270. else
  3271. begin
  3272. bytes:=bytes or (1 shl 25);
  3273. { set U flag }
  3274. if oper[0]^.ref^.signindex>=0 then
  3275. bytes:=bytes or (1 shl 23);
  3276. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  3277. { set shift }
  3278. with oper[0]^.ref^ do
  3279. if shiftmode<>SM_None then
  3280. begin
  3281. bytes:=bytes or ((shiftimm and $1F) shl 7);
  3282. if shiftmode<>SM_RRX then
  3283. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  3284. else
  3285. bytes:=bytes or (3 shl 5);
  3286. end
  3287. end;
  3288. end;
  3289. #$26: // LDM/STM
  3290. begin
  3291. { set instruction code }
  3292. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  3293. if ops>1 then
  3294. begin
  3295. if oper[0]^.typ=top_ref then
  3296. begin
  3297. { set W bit }
  3298. if oper[0]^.ref^.addressmode=AM_PREINDEXED then
  3299. bytes:=bytes or (1 shl 21);
  3300. { set Rn }
  3301. bytes:=bytes or (getsupreg(oper[0]^.ref^.index) shl 16);
  3302. end
  3303. else { typ=top_reg }
  3304. begin
  3305. { set Rn }
  3306. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  3307. end;
  3308. if oper[1]^.usermode then
  3309. begin
  3310. if (oper[0]^.typ=top_ref) then
  3311. begin
  3312. if (opcode=A_LDM) and
  3313. (RS_PC in oper[1]^.regset^) then
  3314. begin
  3315. // Valid exception return
  3316. end
  3317. else
  3318. Message(asmw_e_invalid_opcode_and_operands);
  3319. end;
  3320. bytes:=bytes or (1 shl 22);
  3321. end;
  3322. { reglist }
  3323. bytes:=bytes or MakeRegList(oper[1]^.regset^);
  3324. end
  3325. else
  3326. begin
  3327. { push/pop }
  3328. { Set W and Rn to SP }
  3329. if opcode=A_PUSH then
  3330. bytes:=bytes or (1 shl 21);
  3331. bytes:=bytes or ($D shl 16);
  3332. { reglist }
  3333. bytes:=bytes or MakeRegList(oper[0]^.regset^);
  3334. end;
  3335. { set P bit }
  3336. if (opcode=A_LDM) and (oppostfix in [PF_ED,PF_EA,PF_IB,PF_DB])
  3337. or (opcode=A_STM) and (oppostfix in [PF_FA,PF_FD,PF_IB,PF_DB])
  3338. or (opcode=A_PUSH) then
  3339. bytes:=bytes or (1 shl 24);
  3340. { set U bit }
  3341. if (opcode=A_LDM) and (oppostfix in [PF_None,PF_ED,PF_FD,PF_IB,PF_IA])
  3342. or (opcode=A_STM) and (oppostfix in [PF_None,PF_FA,PF_EA,PF_IB,PF_IA])
  3343. or (opcode=A_POP) then
  3344. bytes:=bytes or (1 shl 23);
  3345. end;
  3346. #$27: // SWP/SWPB
  3347. begin
  3348. { set instruction code }
  3349. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  3350. bytes:=bytes or (ord(insentry^.code[2]) shl 4);
  3351. { set regs }
  3352. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3353. bytes:=bytes or getsupreg(oper[1]^.reg);
  3354. if ops=3 then
  3355. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  3356. end;
  3357. #$28: // BX/BLX
  3358. begin
  3359. { set instruction code }
  3360. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3361. { set offset }
  3362. if oper[0]^.typ=top_const then
  3363. bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
  3364. else
  3365. begin
  3366. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  3367. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  3368. begin
  3369. bytes:=bytes or $fffffe; // TODO: Not sure this is right, but it matches the output of gas
  3370. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24_THUMB);
  3371. end
  3372. else
  3373. begin
  3374. offset:=((currsym.offset-insoffset-8) and $3fffffe);
  3375. { Turn BLX into BL if the destination isn't odd, could happen with recursion }
  3376. if not odd(offset shr 1) then
  3377. bytes:=(bytes and $EB000000) or $EB000000;
  3378. bytes:=bytes or ((offset shr 2) and $ffffff);
  3379. bytes:=bytes or ((offset shr 1) and $1) shl 24;
  3380. end;
  3381. end;
  3382. end;
  3383. #$29: // SUB
  3384. begin
  3385. { set instruction code }
  3386. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3387. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3388. { set regs }
  3389. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3390. { set S if necessary }
  3391. if oppostfix=PF_S then
  3392. bytes:=bytes or (1 shl 20);
  3393. end;
  3394. #$2A:
  3395. begin
  3396. { set instruction code }
  3397. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3398. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3399. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3400. bytes:=bytes or ord(insentry^.code[4]);
  3401. { set opers }
  3402. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3403. if opcode in [A_SSAT, A_SSAT16] then
  3404. bytes:=bytes or (((oper[1]^.val-1) and $1F) shl 16)
  3405. else
  3406. bytes:=bytes or ((oper[1]^.val and $1F) shl 16);
  3407. bytes:=bytes or getsupreg(oper[2]^.reg);
  3408. if (ops>3) and
  3409. (oper[3]^.typ=top_shifterop) and
  3410. (oper[3]^.shifterop^.rs=NR_NO) then
  3411. begin
  3412. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
  3413. if oper[3]^.shifterop^.shiftmode=SM_ASR then
  3414. bytes:=bytes or (1 shl 6)
  3415. else if oper[3]^.shifterop^.shiftmode<>SM_LSL then
  3416. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  3417. end;
  3418. end;
  3419. #$2B: // SETEND
  3420. begin
  3421. { set instruction code }
  3422. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3423. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3424. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3425. bytes:=bytes or ord(insentry^.code[4]);
  3426. { set endian specifier }
  3427. bytes:=bytes or ((oper[0]^.val and 1) shl 9);
  3428. end;
  3429. #$2C: // MOVW
  3430. begin
  3431. { set instruction code }
  3432. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3433. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3434. { set destination }
  3435. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3436. { set imm }
  3437. bytes:=bytes or (oper[1]^.val and $FFF);
  3438. bytes:=bytes or ((oper[1]^.val and $F000) shl 4);
  3439. end;
  3440. #$2D: // BFX
  3441. begin
  3442. { set instruction code }
  3443. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3444. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3445. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3446. bytes:=bytes or ord(insentry^.code[4]);
  3447. if ops=3 then
  3448. begin
  3449. msb:=(oper[1]^.val+oper[2]^.val-1);
  3450. { set destination }
  3451. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3452. { set immediates }
  3453. bytes:=bytes or ((oper[1]^.val and $1F) shl 7);
  3454. bytes:=bytes or ((msb and $1F) shl 16);
  3455. end
  3456. else
  3457. begin
  3458. if opcode in [A_BFC,A_BFI] then
  3459. msb:=(oper[2]^.val+oper[3]^.val-1)
  3460. else
  3461. msb:=oper[3]^.val-1;
  3462. { set destination }
  3463. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3464. bytes:=bytes or getsupreg(oper[1]^.reg);
  3465. { set immediates }
  3466. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  3467. bytes:=bytes or ((msb and $1F) shl 16);
  3468. end;
  3469. end;
  3470. #$2E: // Cache stuff
  3471. begin
  3472. { set instruction code }
  3473. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3474. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3475. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3476. bytes:=bytes or ord(insentry^.code[4]);
  3477. { set code }
  3478. bytes:=bytes or (oper[0]^.val and $F);
  3479. end;
  3480. #$2F: // Nop
  3481. begin
  3482. { set instruction code }
  3483. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3484. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3485. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3486. bytes:=bytes or ord(insentry^.code[4]);
  3487. end;
  3488. #$30: // Shifts
  3489. begin
  3490. { set instruction code }
  3491. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3492. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3493. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3494. bytes:=bytes or ord(insentry^.code[4]);
  3495. { set destination }
  3496. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3497. bytes:=bytes or getsupreg(oper[1]^.reg);
  3498. if ops>2 then
  3499. begin
  3500. { set shift }
  3501. if oper[2]^.typ=top_reg then
  3502. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 8)
  3503. else
  3504. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  3505. end;
  3506. { set S if necessary }
  3507. if oppostfix=PF_S then
  3508. bytes:=bytes or (1 shl 20);
  3509. end;
  3510. #$31: // BKPT
  3511. begin
  3512. { set instruction code }
  3513. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3514. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3515. bytes:=bytes or (ord(insentry^.code[3]) shl 0);
  3516. { set imm }
  3517. bytes:=bytes or (oper[0]^.val and $FFF0) shl 4;
  3518. bytes:=bytes or (oper[0]^.val and $F);
  3519. end;
  3520. #$32: // CLZ/REV
  3521. begin
  3522. { set instruction code }
  3523. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3524. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3525. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3526. bytes:=bytes or ord(insentry^.code[4]);
  3527. { set regs }
  3528. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3529. bytes:=bytes or getsupreg(oper[1]^.reg);
  3530. end;
  3531. #$33:
  3532. begin
  3533. { set instruction code }
  3534. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3535. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3536. { set regs }
  3537. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3538. if oper[1]^.typ=top_ref then
  3539. begin
  3540. { set offset }
  3541. offset:=0;
  3542. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3543. if assigned(currsym) then
  3544. offset:=currsym.offset-insoffset-8;
  3545. offset:=offset+oper[1]^.ref^.offset;
  3546. if opcode = A_ADR then
  3547. begin
  3548. { The encoding for an ADR instruction is that of an ADD instruction,
  3549. so the offset has to abide by immediate shifter rules, otherwise
  3550. it can't be encoded }
  3551. if is_shifter_const(offset,r) then
  3552. begin
  3553. bytes:=bytes or (1 shl 23);
  3554. end
  3555. else
  3556. begin
  3557. bytes:=bytes or (1 shl 22);
  3558. offset:=-offset;
  3559. end;
  3560. { calc rotate and adjust imm }
  3561. count:=0;
  3562. r:=0;
  3563. imm:=dword(offset);
  3564. repeat
  3565. imm:=RolDWord(imm, 2);
  3566. inc(r);
  3567. inc(count);
  3568. if count > 32 then
  3569. begin
  3570. message1(asmw_e_invalid_opcode_and_operands, 'invalid shifter imm (offset)');
  3571. exit;
  3572. end;
  3573. until (imm and $ff)=imm;
  3574. bytes:=bytes or (r shl 8) or imm;
  3575. end
  3576. else
  3577. begin
  3578. if offset>=0 then
  3579. begin
  3580. { set U flag }
  3581. bytes:=bytes or (1 shl 23);
  3582. bytes:=bytes or offset
  3583. end
  3584. else
  3585. begin
  3586. bytes:=bytes or (1 shl 22);
  3587. offset:=-offset;
  3588. bytes:=bytes or offset
  3589. end;
  3590. end;
  3591. end
  3592. else
  3593. begin
  3594. if is_shifter_const(oper[1]^.val,r) then
  3595. begin
  3596. setshifterop(1);
  3597. bytes:=bytes or (1 shl 23);
  3598. end
  3599. else
  3600. begin
  3601. bytes:=bytes or (1 shl 22);
  3602. oper[1]^.val:=-oper[1]^.val;
  3603. setshifterop(1);
  3604. end;
  3605. end;
  3606. end;
  3607. #$40,#$90: // VMOV
  3608. begin
  3609. { set instruction code }
  3610. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3611. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3612. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3613. bytes:=bytes or ord(insentry^.code[4]);
  3614. { set regs }
  3615. Rd:=0;
  3616. Rn:=0;
  3617. Rm:=0;
  3618. case oppostfix of
  3619. PF_None:
  3620. begin
  3621. if ops=4 then
  3622. begin
  3623. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3624. (getregtype(oper[2]^.reg)=R_INTREGISTER) then
  3625. begin
  3626. Rd:=getmmreg(oper[0]^.reg);
  3627. Rm:=getsupreg(oper[2]^.reg);
  3628. Rn:=getsupreg(oper[3]^.reg);
  3629. end
  3630. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3631. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  3632. begin
  3633. Rm:=getsupreg(oper[0]^.reg);
  3634. Rn:=getsupreg(oper[1]^.reg);
  3635. Rd:=getmmreg(oper[2]^.reg);
  3636. end
  3637. else
  3638. message(asmw_e_invalid_opcode_and_operands);
  3639. bytes:=bytes or (((Rd and $1E) shr 1) shl 0);
  3640. bytes:=bytes or ((Rd and $1) shl 5);
  3641. bytes:=bytes or (Rm shl 12);
  3642. bytes:=bytes or (Rn shl 16);
  3643. end
  3644. else if ops=3 then
  3645. begin
  3646. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3647. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  3648. begin
  3649. Rd:=getmmreg(oper[0]^.reg);
  3650. Rm:=getsupreg(oper[1]^.reg);
  3651. Rn:=getsupreg(oper[2]^.reg);
  3652. end
  3653. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3654. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  3655. begin
  3656. Rm:=getsupreg(oper[0]^.reg);
  3657. Rn:=getsupreg(oper[1]^.reg);
  3658. Rd:=getmmreg(oper[2]^.reg);
  3659. end
  3660. else
  3661. message(asmw_e_invalid_opcode_and_operands);
  3662. bytes:=bytes or ((Rd and $F) shl 0);
  3663. bytes:=bytes or ((Rd and $10) shl 1);
  3664. bytes:=bytes or (Rm shl 12);
  3665. bytes:=bytes or (Rn shl 16);
  3666. end
  3667. else if ops=2 then
  3668. begin
  3669. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3670. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  3671. begin
  3672. Rd:=getmmreg(oper[0]^.reg);
  3673. Rm:=getsupreg(oper[1]^.reg);
  3674. end
  3675. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3676. (getregtype(oper[1]^.reg)=R_MMREGISTER) then
  3677. begin
  3678. Rm:=getsupreg(oper[0]^.reg);
  3679. Rd:=getmmreg(oper[1]^.reg);
  3680. end
  3681. else
  3682. message(asmw_e_invalid_opcode_and_operands);
  3683. bytes:=bytes or (((Rd and $1E) shr 1) shl 16);
  3684. bytes:=bytes or ((Rd and $1) shl 7);
  3685. bytes:=bytes or (Rm shl 12);
  3686. end;
  3687. end;
  3688. PF_F32:
  3689. begin
  3690. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) then
  3691. Message(asmw_e_invalid_opcode_and_operands);
  3692. case oper[1]^.typ of
  3693. top_realconst:
  3694. begin
  3695. if not(IsVFPFloatImmediate(s32real,oper[1]^.val_real)) then
  3696. Message(asmw_e_invalid_opcode_and_operands);
  3697. singlerec.value:=oper[1]^.val_real;
  3698. singlerec:=tcompsinglerec(NtoLE(DWord(singlerec)));
  3699. bytes:=bytes or ((singlerec.bytes[2] shr 3) and $f);
  3700. bytes:=bytes or (DWord((singlerec.bytes[2] shr 7) and $1) shl 16) or (DWord(singlerec.bytes[3] and $3) shl 17) or (DWord((singlerec.bytes[3] shr 7) and $1) shl 19);
  3701. end;
  3702. top_reg:
  3703. begin
  3704. if getregtype(oper[1]^.reg)<>R_MMREGISTER then
  3705. Message(asmw_e_invalid_opcode_and_operands);
  3706. Rm:=getmmreg(oper[1]^.reg);
  3707. bytes:=bytes or (((Rm and $1E) shr 1) shl 0);
  3708. bytes:=bytes or ((Rm and $1) shl 5);
  3709. end;
  3710. else
  3711. Message(asmw_e_invalid_opcode_and_operands);
  3712. end;
  3713. Rd:=getmmreg(oper[0]^.reg);
  3714. bytes:=bytes or (((Rd and $1E) shr 1) shl 12);
  3715. bytes:=bytes or ((Rd and $1) shl 22);
  3716. end;
  3717. PF_F64:
  3718. begin
  3719. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) then
  3720. Message(asmw_e_invalid_opcode_and_operands);
  3721. case oper[1]^.typ of
  3722. top_realconst:
  3723. begin
  3724. if not(IsVFPFloatImmediate(s64real,oper[1]^.val_real)) then
  3725. Message(asmw_e_invalid_opcode_and_operands);
  3726. doublerec.value:=oper[1]^.val_real;
  3727. doublerec:=tcompdoublerec(NtoLE(QWord(doublerec)));
  3728. // 32c: eeb41b00 vmov.f64 d1, #64 ; 0x40
  3729. // 32c: eeb61b00 vmov.f64 d1, #96 ; 0x60
  3730. bytes:=bytes or (doublerec.bytes[6] and $f);
  3731. bytes:=bytes or (DWord((doublerec.bytes[6] shr 4) and $7) shl 16) or (DWord((doublerec.bytes[7] shr 7) and $1) shl 19);
  3732. end;
  3733. top_reg:
  3734. begin
  3735. if getregtype(oper[1]^.reg)<>R_MMREGISTER then
  3736. Message(asmw_e_invalid_opcode_and_operands);
  3737. Rm:=getmmreg(oper[1]^.reg);
  3738. bytes:=bytes or (Rm and $F);
  3739. bytes:=bytes or ((Rm and $10) shl 1);
  3740. end;
  3741. else
  3742. Message(asmw_e_invalid_opcode_and_operands);
  3743. end;
  3744. Rd:=getmmreg(oper[0]^.reg);
  3745. bytes:=bytes or (1 shl 8);
  3746. bytes:=bytes or ((Rd and $F) shl 12);
  3747. bytes:=bytes or (((Rd and $10) shr 4) shl 22);
  3748. end;
  3749. else
  3750. Message(asmw_e_invalid_opcode_and_operands);
  3751. end;
  3752. end;
  3753. #$41,#$91: // VMRS/VMSR
  3754. begin
  3755. { set instruction code }
  3756. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3757. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3758. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3759. bytes:=bytes or ord(insentry^.code[4]);
  3760. { set regs }
  3761. if (opcode=A_VMRS) or
  3762. (opcode=A_FMRX) then
  3763. begin
  3764. case oper[1]^.reg of
  3765. NR_FPSID: Rn:=$0;
  3766. NR_FPSCR: Rn:=$1;
  3767. NR_MVFR1: Rn:=$6;
  3768. NR_MVFR0: Rn:=$7;
  3769. NR_FPEXC: Rn:=$8;
  3770. else
  3771. Rn:=0;
  3772. message(asmw_e_invalid_opcode_and_operands);
  3773. end;
  3774. bytes:=bytes or (Rn shl 16);
  3775. if oper[0]^.reg=NR_APSR_nzcv then
  3776. bytes:=bytes or ($F shl 12)
  3777. else
  3778. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3779. end
  3780. else
  3781. begin
  3782. case oper[0]^.reg of
  3783. NR_FPSID: Rn:=$0;
  3784. NR_FPSCR: Rn:=$1;
  3785. NR_FPEXC: Rn:=$8;
  3786. else
  3787. Rn:=0;
  3788. message(asmw_e_invalid_opcode_and_operands);
  3789. end;
  3790. bytes:=bytes or (Rn shl 16);
  3791. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 12);
  3792. end;
  3793. end;
  3794. #$42,#$92: // VMUL
  3795. begin
  3796. { set instruction code }
  3797. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3798. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3799. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3800. bytes:=bytes or ord(insentry^.code[4]);
  3801. { set regs }
  3802. if ops=3 then
  3803. begin
  3804. Rd:=getmmreg(oper[0]^.reg);
  3805. Rn:=getmmreg(oper[1]^.reg);
  3806. Rm:=getmmreg(oper[2]^.reg);
  3807. end
  3808. else if ops=1 then
  3809. begin
  3810. Rd:=getmmreg(oper[0]^.reg);
  3811. Rn:=0;
  3812. Rm:=0;
  3813. end
  3814. else if oper[1]^.typ=top_const then
  3815. begin
  3816. Rd:=getmmreg(oper[0]^.reg);
  3817. Rn:=0;
  3818. Rm:=0;
  3819. end
  3820. else
  3821. begin
  3822. Rd:=getmmreg(oper[0]^.reg);
  3823. Rn:=0;
  3824. Rm:=getmmreg(oper[1]^.reg);
  3825. end;
  3826. if (oppostfix=PF_F32) or (insentry^.code[5]=#1) then
  3827. begin
  3828. D:=rd and $1; Rd:=Rd shr 1;
  3829. N:=rn and $1; Rn:=Rn shr 1;
  3830. M:=rm and $1; Rm:=Rm shr 1;
  3831. end
  3832. else
  3833. begin
  3834. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3835. N:=(rn shr 4) and $1; Rn:=Rn and $F;
  3836. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3837. bytes:=bytes or (1 shl 8);
  3838. end;
  3839. bytes:=bytes or (Rd shl 12);
  3840. bytes:=bytes or (Rn shl 16);
  3841. bytes:=bytes or (Rm shl 0);
  3842. bytes:=bytes or (D shl 22);
  3843. bytes:=bytes or (N shl 7);
  3844. bytes:=bytes or (M shl 5);
  3845. end;
  3846. #$43,#$93: // VCVT
  3847. begin
  3848. { set instruction code }
  3849. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3850. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3851. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3852. bytes:=bytes or ord(insentry^.code[4]);
  3853. { set regs }
  3854. Rd:=getmmreg(oper[0]^.reg);
  3855. Rm:=getmmreg(oper[1]^.reg);
  3856. if (ops=2) and
  3857. (oppostfix in [PF_F32F64,PF_F64F32]) then
  3858. begin
  3859. if oppostfix=PF_F32F64 then
  3860. begin
  3861. bytes:=bytes or (1 shl 8);
  3862. D:=rd and $1; Rd:=Rd shr 1;
  3863. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3864. end
  3865. else
  3866. begin
  3867. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3868. M:=rm and $1; Rm:=Rm shr 1;
  3869. end;
  3870. bytes:=bytes and $FFF0FFFF;
  3871. bytes:=bytes or ($7 shl 16);
  3872. bytes:=bytes or (Rd shl 12);
  3873. bytes:=bytes or (Rm shl 0);
  3874. bytes:=bytes or (D shl 22);
  3875. bytes:=bytes or (M shl 5);
  3876. end
  3877. else if (ops=2) and
  3878. (oppostfix=PF_None) then
  3879. begin
  3880. d:=0;
  3881. case getsubreg(oper[0]^.reg) of
  3882. R_SUBNONE:
  3883. rd:=getsupreg(oper[0]^.reg);
  3884. R_SUBFS:
  3885. begin
  3886. rd:=getmmreg(oper[0]^.reg);
  3887. d:=rd and 1;
  3888. rd:=rd shr 1;
  3889. end;
  3890. R_SUBFD:
  3891. begin
  3892. rd:=getmmreg(oper[0]^.reg);
  3893. d:=(rd shr 4) and 1;
  3894. rd:=rd and $F;
  3895. end;
  3896. else
  3897. internalerror(2019050929);
  3898. end;
  3899. m:=0;
  3900. case getsubreg(oper[1]^.reg) of
  3901. R_SUBNONE:
  3902. rm:=getsupreg(oper[1]^.reg);
  3903. R_SUBFS:
  3904. begin
  3905. rm:=getmmreg(oper[1]^.reg);
  3906. m:=rm and 1;
  3907. rm:=rm shr 1;
  3908. end;
  3909. R_SUBFD:
  3910. begin
  3911. rm:=getmmreg(oper[1]^.reg);
  3912. m:=(rm shr 4) and 1;
  3913. rm:=rm and $F;
  3914. end;
  3915. else
  3916. internalerror(2019050928);
  3917. end;
  3918. bytes:=bytes or (Rd shl 12);
  3919. bytes:=bytes or (Rm shl 0);
  3920. bytes:=bytes or (D shl 22);
  3921. bytes:=bytes or (M shl 5);
  3922. end
  3923. else if ops=2 then
  3924. begin
  3925. case oppostfix of
  3926. PF_S32F64,
  3927. PF_U32F64,
  3928. PF_F64S32,
  3929. PF_F64U32:
  3930. bytes:=bytes or (1 shl 8);
  3931. else
  3932. ;
  3933. end;
  3934. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64] then
  3935. begin
  3936. case oppostfix of
  3937. PF_S32F64,
  3938. PF_S32F32:
  3939. bytes:=bytes or (1 shl 16);
  3940. else
  3941. ;
  3942. end;
  3943. bytes:=bytes or (1 shl 18);
  3944. D:=rd and $1; Rd:=Rd shr 1;
  3945. if oppostfix in [PF_S32F64,PF_U32F64] then
  3946. begin
  3947. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3948. end
  3949. else
  3950. begin
  3951. M:=rm and $1; Rm:=Rm shr 1;
  3952. end;
  3953. end
  3954. else
  3955. begin
  3956. case oppostfix of
  3957. PF_F64S32,
  3958. PF_F32S32:
  3959. bytes:=bytes or (1 shl 7);
  3960. else
  3961. bytes:=bytes and $FFFFFF7F;
  3962. end;
  3963. M:=rm and $1; Rm:=Rm shr 1;
  3964. if oppostfix in [PF_F64S32,PF_F64U32] then
  3965. begin
  3966. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3967. end
  3968. else
  3969. begin
  3970. D:=rd and $1; Rd:=Rd shr 1;
  3971. end
  3972. end;
  3973. bytes:=bytes or (Rd shl 12);
  3974. bytes:=bytes or (Rm shl 0);
  3975. bytes:=bytes or (D shl 22);
  3976. bytes:=bytes or (M shl 5);
  3977. end
  3978. else
  3979. begin
  3980. if rd<>rm then
  3981. message(asmw_e_invalid_opcode_and_operands);
  3982. case oppostfix of
  3983. PF_S32F32,PF_U32F32,
  3984. PF_F32S32,PF_F32U32,
  3985. PF_S32F64,PF_U32F64,
  3986. PF_F64S32,PF_F64U32:
  3987. begin
  3988. if not (oper[2]^.val in [1..32]) then
  3989. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 1-32');
  3990. bytes:=bytes or (1 shl 7);
  3991. rn:=32;
  3992. end;
  3993. PF_S16F64,PF_U16F64,
  3994. PF_F64S16,PF_F64U16,
  3995. PF_S16F32,PF_U16F32,
  3996. PF_F32S16,PF_F32U16:
  3997. begin
  3998. if not (oper[2]^.val in [0..16]) then
  3999. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 0-16');
  4000. rn:=16;
  4001. end;
  4002. else
  4003. Rn:=0;
  4004. message(asmw_e_invalid_opcode_and_operands);
  4005. end;
  4006. case oppostfix of
  4007. PF_S16F64,PF_U16F64,
  4008. PF_S32F64,PF_U32F64,
  4009. PF_F64S16,PF_F64U16,
  4010. PF_F64S32,PF_F64U32:
  4011. begin
  4012. bytes:=bytes or (1 shl 8);
  4013. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  4014. end;
  4015. else
  4016. begin
  4017. D:=rd and $1; Rd:=Rd shr 1;
  4018. end;
  4019. end;
  4020. case oppostfix of
  4021. PF_U16F64,PF_U16F32,
  4022. PF_U32F32,PF_U32F64,
  4023. PF_F64U16,PF_F32U16,
  4024. PF_F32U32,PF_F64U32:
  4025. bytes:=bytes or (1 shl 16);
  4026. else
  4027. ;
  4028. end;
  4029. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64,PF_S16F32,PF_S16F64,PF_U16F32,PF_U16F64] then
  4030. bytes:=bytes or (1 shl 18);
  4031. bytes:=bytes or (Rd shl 12);
  4032. bytes:=bytes or (D shl 22);
  4033. rn:=rn-oper[2]^.val;
  4034. bytes:=bytes or ((rn and $1) shl 5);
  4035. bytes:=bytes or ((rn and $1E) shr 1);
  4036. end;
  4037. end;
  4038. #$44,#$94: // VLDM/VSTM/VPUSH/VPOP
  4039. begin
  4040. { set instruction code }
  4041. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4042. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4043. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4044. { set regs }
  4045. if ops=2 then
  4046. begin
  4047. if oper[0]^.typ=top_ref then
  4048. begin
  4049. Rn:=getsupreg(oper[0]^.ref^.index);
  4050. if oper[0]^.ref^.addressmode<>AM_OFFSET then
  4051. begin
  4052. { set W }
  4053. bytes:=bytes or (1 shl 21);
  4054. end
  4055. else if oppostfix in [PF_DB,PF_DBS,PF_DBD,PF_DBX] then
  4056. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  4057. end
  4058. else
  4059. begin
  4060. Rn:=getsupreg(oper[0]^.reg);
  4061. if oppostfix in [PF_DB,PF_DBS,PF_DBD,PF_DBX] then
  4062. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  4063. end;
  4064. bytes:=bytes or (Rn shl 16);
  4065. { Set PU bits }
  4066. case oppostfix of
  4067. PF_None,
  4068. PF_IA,PF_IAS,PF_IAD,PF_IAX:
  4069. bytes:=bytes or (1 shl 23);
  4070. PF_DB,PF_DBS,PF_DBD,PF_DBX:
  4071. bytes:=bytes or (2 shl 23);
  4072. else
  4073. ;
  4074. end;
  4075. case oppostfix of
  4076. PF_IAX,PF_DBX,PF_FDX,PF_EAX:
  4077. begin
  4078. bytes:=bytes or (1 shl 8);
  4079. bytes:=bytes or (1 shl 0); // Offset is odd
  4080. end;
  4081. else
  4082. ;
  4083. end;
  4084. dp_operation:=(oper[1]^.subreg=R_SUBFD);
  4085. if oper[1]^.regset^=[] then
  4086. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  4087. rd:=0;
  4088. for r:=0 to 31 do
  4089. if r in oper[1]^.regset^ then
  4090. begin
  4091. rd:=r;
  4092. break;
  4093. end;
  4094. rn:=32-rd;
  4095. for r:=rd+1 to 31 do
  4096. if not(r in oper[1]^.regset^) then
  4097. begin
  4098. rn:=r-rd;
  4099. break;
  4100. end;
  4101. if dp_operation then
  4102. begin
  4103. bytes:=bytes or (1 shl 8);
  4104. bytes:=bytes or (rn*2);
  4105. bytes:=bytes or ((rd and $F) shl 12);
  4106. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  4107. end
  4108. else
  4109. begin
  4110. bytes:=bytes or rn;
  4111. bytes:=bytes or ((rd and $1) shl 22);
  4112. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  4113. end;
  4114. end
  4115. else { VPUSH/VPOP }
  4116. begin
  4117. dp_operation:=(oper[0]^.subreg=R_SUBFD);
  4118. if oper[0]^.regset^=[] then
  4119. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  4120. rd:=0;
  4121. for r:=0 to 31 do
  4122. if r in oper[0]^.regset^ then
  4123. begin
  4124. rd:=r;
  4125. break;
  4126. end;
  4127. rn:=32-rd;
  4128. for r:=rd+1 to 31 do
  4129. if not(r in oper[0]^.regset^) then
  4130. begin
  4131. rn:=r-rd;
  4132. break;
  4133. end;
  4134. if dp_operation then
  4135. begin
  4136. bytes:=bytes or (1 shl 8);
  4137. bytes:=bytes or (rn*2);
  4138. bytes:=bytes or ((rd and $F) shl 12);
  4139. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  4140. end
  4141. else
  4142. begin
  4143. bytes:=bytes or rn;
  4144. bytes:=bytes or ((rd and $1) shl 22);
  4145. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  4146. end;
  4147. end;
  4148. end;
  4149. #$45,#$95: // VLDR/VSTR
  4150. begin
  4151. { set instruction code }
  4152. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4153. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4154. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4155. { set regs }
  4156. rd:=getmmreg(oper[0]^.reg);
  4157. if getsubreg(oper[0]^.reg)=R_SUBFD then
  4158. begin
  4159. bytes:=bytes or (1 shl 8);
  4160. bytes:=bytes or ((rd and $F) shl 12);
  4161. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  4162. end
  4163. else
  4164. begin
  4165. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  4166. bytes:=bytes or ((rd and $1) shl 22);
  4167. end;
  4168. { set ref }
  4169. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4170. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  4171. begin
  4172. { set offset }
  4173. offset:=0;
  4174. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4175. if assigned(currsym) then
  4176. offset:=currsym.offset-insoffset-8;
  4177. offset:=offset+oper[1]^.ref^.offset;
  4178. offset:=offset div 4;
  4179. if offset>=0 then
  4180. begin
  4181. { set U flag }
  4182. bytes:=bytes or (1 shl 23);
  4183. bytes:=bytes or offset
  4184. end
  4185. else
  4186. begin
  4187. offset:=-offset;
  4188. bytes:=bytes or offset
  4189. end;
  4190. end
  4191. else
  4192. message(asmw_e_invalid_opcode_and_operands);
  4193. end;
  4194. #$46: { System instructions }
  4195. begin
  4196. { set instruction code }
  4197. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4198. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4199. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4200. { set regs }
  4201. if (oper[0]^.typ=top_modeflags) then
  4202. begin
  4203. if mfA in oper[0]^.modeflags then bytes:=bytes or (1 shl 8);
  4204. if mfI in oper[0]^.modeflags then bytes:=bytes or (1 shl 7);
  4205. if mfF in oper[0]^.modeflags then bytes:=bytes or (1 shl 6);
  4206. end;
  4207. if (ops=2) then
  4208. bytes:=bytes or (oper[1]^.val and $1F)
  4209. else if (ops=1) and
  4210. (oper[0]^.typ=top_const) then
  4211. bytes:=bytes or (oper[0]^.val and $1F);
  4212. end;
  4213. #$60: { Thumb }
  4214. begin
  4215. bytelen:=2;
  4216. bytes:=0;
  4217. { set opcode }
  4218. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4219. bytes:=bytes or ord(insentry^.code[2]);
  4220. { set regs }
  4221. if ops=2 then
  4222. begin
  4223. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4224. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 3);
  4225. if (oper[1]^.typ=top_reg) then
  4226. bytes:=bytes or ((getsupreg(oper[1]^.reg) and $7) shl 6)
  4227. else
  4228. bytes:=bytes or ((oper[1]^.val and $1F) shl 6);
  4229. end
  4230. else if ops=3 then
  4231. begin
  4232. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4233. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  4234. if (oper[2]^.typ=top_reg) then
  4235. bytes:=bytes or ((getsupreg(oper[2]^.reg) and $7) shl 6)
  4236. else
  4237. bytes:=bytes or ((oper[2]^.val and $1F) shl 6);
  4238. end
  4239. else if ops=1 then
  4240. begin
  4241. if oper[0]^.typ=top_const then
  4242. bytes:=bytes or (oper[0]^.val and $FF);
  4243. end;
  4244. end;
  4245. #$61: { Thumb }
  4246. begin
  4247. bytelen:=2;
  4248. bytes:=0;
  4249. { set opcode }
  4250. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4251. bytes:=bytes or ord(insentry^.code[2]);
  4252. { set regs }
  4253. if ops=2 then
  4254. begin
  4255. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4256. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $8) shr 3) shl 7;
  4257. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  4258. end
  4259. else if ops=1 then
  4260. begin
  4261. if oper[0]^.typ=top_const then
  4262. bytes:=bytes or (oper[0]^.val and $FF);
  4263. end;
  4264. end;
  4265. #$62..#$63: { Thumb branches }
  4266. begin
  4267. bytelen:=2;
  4268. bytes:=0;
  4269. { set opcode }
  4270. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4271. bytes:=bytes or ord(insentry^.code[2]);
  4272. if insentry^.code[0]=#$63 then
  4273. bytes:=bytes or (CondVal[condition] shl 8);
  4274. if oper[0]^.typ=top_const then
  4275. begin
  4276. if insentry^.code[0]=#$63 then
  4277. bytes:=bytes or (((oper[0]^.val shr 1)-1) and $FF)
  4278. else
  4279. bytes:=bytes or (((oper[0]^.val shr 1)-1) and $3FF);
  4280. end
  4281. else if oper[0]^.typ=top_reg then
  4282. begin
  4283. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 3);
  4284. end
  4285. else if oper[0]^.typ=top_ref then
  4286. begin
  4287. offset:=0;
  4288. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  4289. if assigned(currsym) then
  4290. offset:=currsym.offset-insoffset-8;
  4291. offset:=offset+oper[0]^.ref^.offset;
  4292. if insentry^.code[0]=#$63 then
  4293. bytes:=bytes or (((offset+4) shr 1) and $FF)
  4294. else
  4295. bytes:=bytes or (((offset+4) shr 1) and $7FF);
  4296. end
  4297. end;
  4298. #$64: { Thumb: Special encodings }
  4299. begin
  4300. bytelen:=2;
  4301. bytes:=0;
  4302. { set opcode }
  4303. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4304. bytes:=bytes or ord(insentry^.code[2]);
  4305. case opcode of
  4306. A_SUB:
  4307. begin
  4308. if (ops=3) and
  4309. (oper[2]^.typ=top_const) then
  4310. bytes:=bytes or ((oper[2]^.val shr 2) and $7F)
  4311. else if (ops=2) and
  4312. (oper[1]^.typ=top_const) then
  4313. bytes:=bytes or ((oper[1]^.val shr 2) and $7F);
  4314. end;
  4315. A_MUL:
  4316. if (ops in [2,3]) then
  4317. begin
  4318. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4319. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  4320. end;
  4321. A_ADD:
  4322. begin
  4323. if ops=2 then
  4324. begin
  4325. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4326. bytes:=bytes or (getsupreg(oper[1]^.reg) shl $3);
  4327. end
  4328. else if (oper[0]^.reg<>NR_STACK_POINTER_REG) and
  4329. (oper[2]^.typ=top_const) then
  4330. begin
  4331. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7) shl 8;
  4332. bytes:=bytes or ((oper[2]^.val shr 2) and $7F);
  4333. end
  4334. else if (oper[0]^.reg<>NR_STACK_POINTER_REG) and
  4335. (oper[2]^.typ=top_reg) then
  4336. begin
  4337. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4338. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $8) shr 3) shl 7;
  4339. end
  4340. else
  4341. begin
  4342. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4343. bytes:=bytes or ((oper[2]^.val shr 2) and $7F);
  4344. end;
  4345. end;
  4346. else
  4347. internalerror(2019050926);
  4348. end;
  4349. end;
  4350. #$65: { Thumb load/store }
  4351. begin
  4352. bytelen:=2;
  4353. bytes:=0;
  4354. { set opcode }
  4355. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4356. bytes:=bytes or ord(insentry^.code[2]);
  4357. { set regs }
  4358. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4359. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 3);
  4360. bytes:=bytes or (getsupreg(oper[1]^.ref^.index) shl 6);
  4361. end;
  4362. #$66: { Thumb load/store }
  4363. begin
  4364. bytelen:=2;
  4365. bytes:=0;
  4366. { set opcode }
  4367. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4368. bytes:=bytes or ord(insentry^.code[2]);
  4369. { set regs }
  4370. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4371. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 3);
  4372. { set offset }
  4373. offset:=0;
  4374. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4375. if assigned(currsym) then
  4376. offset:=currsym.offset-(insoffset+4) and (not longword(3));
  4377. offset:=(offset+oper[1]^.ref^.offset);
  4378. bytes:=bytes or (((offset shr ord(insentry^.code[3])) and $1F) shl 6);
  4379. end;
  4380. #$67: { Thumb load/store }
  4381. begin
  4382. bytelen:=2;
  4383. bytes:=0;
  4384. { set opcode }
  4385. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4386. bytes:=bytes or ord(insentry^.code[2]);
  4387. { set regs }
  4388. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4389. if oper[1]^.typ=top_ref then
  4390. begin
  4391. { set offset }
  4392. offset:=0;
  4393. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4394. if assigned(currsym) then
  4395. offset:=currsym.offset-(insoffset+4) and (not longword(3));
  4396. offset:=(offset+oper[1]^.ref^.offset);
  4397. bytes:=bytes or ((offset shr ord(insentry^.code[3])) and $FF);
  4398. end
  4399. else
  4400. bytes:=bytes or ((oper[1]^.val shr ord(insentry^.code[3])) and $FF);
  4401. end;
  4402. #$68: { Thumb CB[N]Z }
  4403. begin
  4404. bytelen:=2;
  4405. bytes:=0;
  4406. { set opcode }
  4407. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4408. { set opers }
  4409. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4410. if oper[1]^.typ=top_ref then
  4411. begin
  4412. offset:=0;
  4413. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4414. if assigned(currsym) then
  4415. offset:=currsym.offset-insoffset-8;
  4416. offset:=offset+oper[1]^.ref^.offset;
  4417. offset:=offset div 2;
  4418. end
  4419. else
  4420. offset:=oper[1]^.val div 2;
  4421. bytes:=bytes or ((offset) and $1F) shl 3;
  4422. bytes:=bytes or ((offset shr 5) and 1) shl 9;
  4423. end;
  4424. #$69: { Thumb: Push/Pop/Stm/Ldm }
  4425. begin
  4426. bytelen:=2;
  4427. bytes:=0;
  4428. { set opcode }
  4429. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4430. case opcode of
  4431. A_PUSH:
  4432. begin
  4433. for r:=0 to 7 do
  4434. if r in oper[0]^.regset^ then
  4435. bytes:=bytes or (1 shl r);
  4436. if RS_R14 in oper[0]^.regset^ then
  4437. bytes:=bytes or (1 shl 8);
  4438. end;
  4439. A_POP:
  4440. begin
  4441. for r:=0 to 7 do
  4442. if r in oper[0]^.regset^ then
  4443. bytes:=bytes or (1 shl r);
  4444. if RS_R15 in oper[0]^.regset^ then
  4445. bytes:=bytes or (1 shl 8);
  4446. end;
  4447. A_STM:
  4448. begin
  4449. for r:=0 to 7 do
  4450. if r in oper[1]^.regset^ then
  4451. bytes:=bytes or (1 shl r);
  4452. if oper[0]^.typ=top_ref then
  4453. bytes:=bytes or (getsupreg(oper[0]^.ref^.index) shl 8)
  4454. else
  4455. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4456. end;
  4457. A_LDM:
  4458. begin
  4459. for r:=0 to 7 do
  4460. if r in oper[1]^.regset^ then
  4461. bytes:=bytes or (1 shl r);
  4462. if oper[0]^.typ=top_ref then
  4463. bytes:=bytes or (getsupreg(oper[0]^.ref^.index) shl 8)
  4464. else
  4465. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4466. end;
  4467. else
  4468. internalerror(2019050925);
  4469. end;
  4470. end;
  4471. #$6A: { Thumb: IT }
  4472. begin
  4473. bytelen:=2;
  4474. bytes:=0;
  4475. { set opcode }
  4476. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4477. bytes:=bytes or (ord(insentry^.code[2]) shl 0);
  4478. bytes:=bytes or (CondVal[oper[0]^.cc] shl 4);
  4479. i_field:=(bytes shr 4) and 1;
  4480. i_field:=(i_field shl 1) or i_field;
  4481. i_field:=(i_field shl 2) or i_field;
  4482. bytes:=bytes or ((i_field and ord(insentry^.code[3])) xor (ord(insentry^.code[3]) shr 4));
  4483. end;
  4484. #$6B: { Thumb: Data processing (misc) }
  4485. begin
  4486. bytelen:=2;
  4487. bytes:=0;
  4488. { set opcode }
  4489. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4490. bytes:=bytes or ord(insentry^.code[2]);
  4491. { set regs }
  4492. if ops>=2 then
  4493. begin
  4494. if oper[1]^.typ=top_const then
  4495. begin
  4496. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $7) shl 8);
  4497. bytes:=bytes or (oper[1]^.val and $FF);
  4498. end
  4499. else if oper[1]^.typ=top_reg then
  4500. begin
  4501. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4502. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  4503. end;
  4504. end
  4505. else if ops=1 then
  4506. begin
  4507. if oper[0]^.typ=top_const then
  4508. bytes:=bytes or (oper[0]^.val and $FF);
  4509. end;
  4510. end;
  4511. #$6C: { Thumb: CPS }
  4512. begin
  4513. bytelen:=2;
  4514. bytes:=0;
  4515. { set opcode }
  4516. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4517. bytes:=bytes or ord(insentry^.code[2]);
  4518. if mfA in oper[0]^.modeflags then bytes:=bytes or (1 shl 2);
  4519. if mfI in oper[0]^.modeflags then bytes:=bytes or (1 shl 1);
  4520. if mfF in oper[0]^.modeflags then bytes:=bytes or (1 shl 0);
  4521. end;
  4522. #$80: { Thumb-2: Dataprocessing }
  4523. begin
  4524. bytes:=0;
  4525. { set instruction code }
  4526. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4527. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4528. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4529. bytes:=bytes or ord(insentry^.code[4]);
  4530. if ops=1 then
  4531. begin
  4532. if oper[0]^.typ=top_reg then
  4533. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16)
  4534. else if oper[0]^.typ=top_const then
  4535. bytes:=bytes or (oper[0]^.val and $F);
  4536. end
  4537. else if (ops=2) and
  4538. (opcode in [A_CMP,A_CMN,A_TEQ,A_TST]) then
  4539. begin
  4540. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4541. if oper[1]^.typ=top_const then
  4542. encodethumbimm(oper[1]^.val)
  4543. else if oper[1]^.typ=top_reg then
  4544. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4545. end
  4546. else if (ops=3) and
  4547. (opcode in [A_CMP,A_CMN,A_TEQ,A_TST]) then
  4548. begin
  4549. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4550. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4551. if oper[2]^.typ=top_shifterop then
  4552. setthumbshift(2)
  4553. else if oper[2]^.typ=top_reg then
  4554. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 12);
  4555. end
  4556. else if (ops=2) and
  4557. (opcode in [A_REV,A_RBIT,A_REV16,A_REVSH,A_CLZ]) then
  4558. begin
  4559. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4560. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4561. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4562. end
  4563. else if ops=2 then
  4564. begin
  4565. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4566. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4567. if oper[1]^.typ=top_const then
  4568. encodethumbimm(oper[1]^.val)
  4569. else if oper[1]^.typ=top_reg then
  4570. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4571. end
  4572. else if ops=3 then
  4573. begin
  4574. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4575. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4576. if oper[2]^.typ=top_const then
  4577. encodethumbimm(oper[2]^.val)
  4578. else if oper[2]^.typ=top_reg then
  4579. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4580. end
  4581. else if ops=4 then
  4582. begin
  4583. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4584. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4585. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4586. if oper[3]^.typ=top_shifterop then
  4587. setthumbshift(3)
  4588. else if oper[3]^.typ=top_reg then
  4589. bytes:=bytes or (getsupreg(oper[3]^.reg) shl 12);
  4590. end;
  4591. if oppostfix=PF_S then
  4592. bytes:=bytes or (1 shl 20)
  4593. else if oppostfix=PF_X then
  4594. bytes:=bytes or (1 shl 4)
  4595. else if oppostfix=PF_R then
  4596. bytes:=bytes or (1 shl 4);
  4597. end;
  4598. #$81: { Thumb-2: Dataprocessing misc }
  4599. begin
  4600. bytes:=0;
  4601. { set instruction code }
  4602. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4603. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4604. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4605. bytes:=bytes or ord(insentry^.code[4]);
  4606. if ops=3 then
  4607. begin
  4608. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4609. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4610. if oper[2]^.typ=top_const then
  4611. begin
  4612. bytes:=bytes or (oper[2]^.val and $FF);
  4613. bytes:=bytes or ((oper[2]^.val and $700) shr 8) shl 12;
  4614. bytes:=bytes or ((oper[2]^.val and $800) shr 11) shl 26;
  4615. end;
  4616. end
  4617. else if ops=2 then
  4618. begin
  4619. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4620. offset:=0;
  4621. if oper[1]^.typ=top_const then
  4622. begin
  4623. offset:=oper[1]^.val;
  4624. end
  4625. else if oper[1]^.typ=top_ref then
  4626. begin
  4627. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4628. if assigned(currsym) then
  4629. offset:=currsym.offset-insoffset-8;
  4630. offset:=offset+oper[1]^.ref^.offset;
  4631. offset:=offset;
  4632. end;
  4633. bytes:=bytes or (offset and $FF);
  4634. bytes:=bytes or ((offset and $700) shr 8) shl 12;
  4635. bytes:=bytes or ((offset and $800) shr 11) shl 26;
  4636. bytes:=bytes or ((offset and $F000) shr 12) shl 16;
  4637. end;
  4638. if oppostfix=PF_S then
  4639. bytes:=bytes or (1 shl 20);
  4640. end;
  4641. #$82: { Thumb-2: Shifts }
  4642. begin
  4643. bytes:=0;
  4644. { set instruction code }
  4645. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4646. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4647. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4648. bytes:=bytes or ord(insentry^.code[4]);
  4649. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4650. if oper[1]^.typ=top_reg then
  4651. begin
  4652. offset:=2;
  4653. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4654. end
  4655. else
  4656. begin
  4657. offset:=1;
  4658. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 0);
  4659. end;
  4660. if oper[offset]^.typ=top_const then
  4661. begin
  4662. bytes:=bytes or (oper[offset]^.val and $3) shl 6;
  4663. bytes:=bytes or (oper[offset]^.val and $1C) shl 10;
  4664. end
  4665. else if oper[offset]^.typ=top_reg then
  4666. bytes:=bytes or (getsupreg(oper[offset]^.reg) shl 16);
  4667. if (ops>=(offset+2)) and
  4668. (oper[offset+1]^.typ=top_const) then
  4669. bytes:=bytes or (oper[offset+1]^.val and $1F);
  4670. if oppostfix=PF_S then
  4671. bytes:=bytes or (1 shl 20);
  4672. end;
  4673. #$84: { Thumb-2: Shifts(width-1) }
  4674. begin
  4675. bytes:=0;
  4676. { set instruction code }
  4677. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4678. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4679. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4680. bytes:=bytes or ord(insentry^.code[4]);
  4681. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4682. if oper[1]^.typ=top_reg then
  4683. begin
  4684. offset:=2;
  4685. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4686. end
  4687. else
  4688. offset:=1;
  4689. if oper[offset]^.typ=top_const then
  4690. begin
  4691. bytes:=bytes or (oper[offset]^.val and $3) shl 6;
  4692. bytes:=bytes or (oper[offset]^.val and $1C) shl 10;
  4693. end;
  4694. if (ops>=(offset+2)) and
  4695. (oper[offset+1]^.typ=top_const) then
  4696. begin
  4697. if opcode in [A_BFI,A_BFC] then
  4698. i_field:=oper[offset+1]^.val+oper[offset]^.val-1
  4699. else
  4700. i_field:=oper[offset+1]^.val-1;
  4701. bytes:=bytes or (i_field and $1F);
  4702. end;
  4703. if oppostfix=PF_S then
  4704. bytes:=bytes or (1 shl 20);
  4705. end;
  4706. #$83: { Thumb-2: Saturation }
  4707. begin
  4708. bytes:=0;
  4709. { set instruction code }
  4710. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4711. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4712. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4713. bytes:=bytes or ord(insentry^.code[4]);
  4714. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4715. bytes:=bytes or (oper[1]^.val and $1F);
  4716. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 16);
  4717. if ops=4 then
  4718. setthumbshift(3,true);
  4719. end;
  4720. #$85: { Thumb-2: Long multiplications }
  4721. begin
  4722. bytes:=0;
  4723. { set instruction code }
  4724. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4725. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4726. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4727. bytes:=bytes or ord(insentry^.code[4]);
  4728. if ops=4 then
  4729. begin
  4730. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  4731. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 8);
  4732. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 16);
  4733. bytes:=bytes or (getsupreg(oper[3]^.reg) shl 0);
  4734. end;
  4735. if oppostfix=PF_S then
  4736. bytes:=bytes or (1 shl 20)
  4737. else if oppostfix=PF_X then
  4738. bytes:=bytes or (1 shl 4);
  4739. end;
  4740. #$86: { Thumb-2: Extension ops }
  4741. begin
  4742. bytes:=0;
  4743. { set instruction code }
  4744. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4745. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4746. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4747. bytes:=bytes or ord(insentry^.code[4]);
  4748. if ops=2 then
  4749. begin
  4750. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4751. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4752. end
  4753. else if ops=3 then
  4754. begin
  4755. if oper[2]^.typ=top_shifterop then
  4756. begin
  4757. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4758. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4759. bytes:=bytes or ((oper[2]^.shifterop^.shiftimm shr 3) shl 4);
  4760. end
  4761. else
  4762. begin
  4763. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4764. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4765. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4766. end;
  4767. end
  4768. else if ops=4 then
  4769. begin
  4770. if oper[3]^.typ=top_shifterop then
  4771. begin
  4772. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4773. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4774. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4775. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm shr 3) shl 4);
  4776. end;
  4777. end;
  4778. end;
  4779. #$87: { Thumb-2: PLD/PLI }
  4780. begin
  4781. { set instruction code }
  4782. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4783. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4784. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4785. bytes:=bytes or ord(insentry^.code[4]);
  4786. { set Rn and Rd }
  4787. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  4788. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  4789. begin
  4790. { set offset }
  4791. offset:=0;
  4792. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  4793. if assigned(currsym) then
  4794. offset:=currsym.offset-insoffset-8;
  4795. offset:=offset+oper[0]^.ref^.offset;
  4796. if offset>=0 then
  4797. begin
  4798. { set U flag }
  4799. bytes:=bytes or (1 shl 23);
  4800. bytes:=bytes or (offset and $FFF);
  4801. end
  4802. else
  4803. begin
  4804. bytes:=bytes or ($3 shl 10);
  4805. offset:=-offset;
  4806. bytes:=bytes or (offset and $FF);
  4807. end;
  4808. end
  4809. else
  4810. begin
  4811. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  4812. { set shift }
  4813. with oper[0]^.ref^ do
  4814. if shiftmode=SM_LSL then
  4815. bytes:=bytes or ((shiftimm and $1F) shl 4);
  4816. end;
  4817. end;
  4818. #$88: { Thumb-2: LDR/STR }
  4819. begin
  4820. { set instruction code }
  4821. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4822. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4823. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4824. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4825. { set Rn and Rd }
  4826. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4827. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4828. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  4829. begin
  4830. { set offset }
  4831. offset:=0;
  4832. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4833. if assigned(currsym) then
  4834. offset:=currsym.offset-insoffset-8;
  4835. offset:=(offset+oper[1]^.ref^.offset) shr ord(insentry^.code[5]);
  4836. if offset>=0 then
  4837. begin
  4838. if (offset>255) and
  4839. (not (opcode in [A_LDRT,A_LDRSBT,A_LDRSHT,A_LDRBT,A_LDRHT])) then
  4840. bytes:=bytes or (1 shl 23);
  4841. { set U flag }
  4842. if (oper[1]^.ref^.addressmode<>AM_OFFSET) then
  4843. begin
  4844. bytes:=bytes or (1 shl 9);
  4845. bytes:=bytes or (1 shl 11);
  4846. end;
  4847. bytes:=bytes or offset
  4848. end
  4849. else
  4850. begin
  4851. bytes:=bytes or (1 shl 11);
  4852. offset:=-offset;
  4853. bytes:=bytes or offset
  4854. end;
  4855. end
  4856. else
  4857. begin
  4858. { set I flag }
  4859. bytes:=bytes or (1 shl 25);
  4860. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  4861. { set shift }
  4862. with oper[1]^.ref^ do
  4863. if shiftmode<>SM_None then
  4864. bytes:=bytes or ((shiftimm and $1F) shl 4);
  4865. end;
  4866. if not (opcode in [A_LDRT,A_LDRSBT,A_LDRSHT,A_LDRBT,A_LDRHT]) then
  4867. begin
  4868. { set W bit }
  4869. if oper[1]^.ref^.addressmode<>AM_OFFSET then
  4870. bytes:=bytes or (1 shl 8);
  4871. { set P bit if necessary }
  4872. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  4873. bytes:=bytes or (1 shl 10);
  4874. end;
  4875. end;
  4876. #$89: { Thumb-2: LDRD/STRD }
  4877. begin
  4878. { set instruction code }
  4879. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4880. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4881. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4882. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4883. { set Rn and Rd }
  4884. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4885. bytes:=bytes or getsupreg(oper[1]^.reg) shl 8;
  4886. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4887. if getregtype(oper[2]^.ref^.index)=R_INVALIDREGISTER then
  4888. begin
  4889. { set offset }
  4890. offset:=0;
  4891. currsym:=objdata.symbolref(oper[2]^.ref^.symbol);
  4892. if assigned(currsym) then
  4893. offset:=currsym.offset-insoffset-8;
  4894. offset:=(offset+oper[2]^.ref^.offset) div 4;
  4895. if offset>=0 then
  4896. begin
  4897. { set U flag }
  4898. bytes:=bytes or (1 shl 23);
  4899. bytes:=bytes or offset
  4900. end
  4901. else
  4902. begin
  4903. offset:=-offset;
  4904. bytes:=bytes or offset
  4905. end;
  4906. end
  4907. else
  4908. begin
  4909. message(asmw_e_invalid_opcode_and_operands);
  4910. end;
  4911. { set W bit }
  4912. if oper[2]^.ref^.addressmode<>AM_OFFSET then
  4913. bytes:=bytes or (1 shl 21);
  4914. { set P bit if necessary }
  4915. if oper[2]^.ref^.addressmode<>AM_POSTINDEXED then
  4916. bytes:=bytes or (1 shl 24);
  4917. end;
  4918. #$8A: { Thumb-2: LDREX }
  4919. begin
  4920. { set instruction code }
  4921. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4922. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4923. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4924. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4925. { set Rn and Rd }
  4926. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4927. if (ops=2) and (opcode in [A_LDREX]) then
  4928. begin
  4929. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4930. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  4931. begin
  4932. { set offset }
  4933. offset:=0;
  4934. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4935. if assigned(currsym) then
  4936. offset:=currsym.offset-insoffset-8;
  4937. offset:=(offset+oper[1]^.ref^.offset) div 4;
  4938. if offset>=0 then
  4939. begin
  4940. bytes:=bytes or offset
  4941. end
  4942. else
  4943. begin
  4944. message(asmw_e_invalid_opcode_and_operands);
  4945. end;
  4946. end
  4947. else
  4948. begin
  4949. message(asmw_e_invalid_opcode_and_operands);
  4950. end;
  4951. end
  4952. else if (ops=2) then
  4953. begin
  4954. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4955. end
  4956. else
  4957. begin
  4958. bytes:=bytes or getsupreg(oper[1]^.reg) shl 8;
  4959. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4960. end;
  4961. end;
  4962. #$8B: { Thumb-2: STREX }
  4963. begin
  4964. { set instruction code }
  4965. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4966. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4967. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4968. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4969. { set Rn and Rd }
  4970. if (ops=3) and (opcode in [A_STREX]) then
  4971. begin
  4972. bytes:=bytes or getsupreg(oper[0]^.reg) shl 8;
  4973. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4974. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4975. if getregtype(oper[2]^.ref^.index)=R_INVALIDREGISTER then
  4976. begin
  4977. { set offset }
  4978. offset:=0;
  4979. currsym:=objdata.symbolref(oper[2]^.ref^.symbol);
  4980. if assigned(currsym) then
  4981. offset:=currsym.offset-insoffset-8;
  4982. offset:=(offset+oper[2]^.ref^.offset) div 4;
  4983. if offset>=0 then
  4984. begin
  4985. bytes:=bytes or offset
  4986. end
  4987. else
  4988. begin
  4989. message(asmw_e_invalid_opcode_and_operands);
  4990. end;
  4991. end
  4992. else
  4993. begin
  4994. message(asmw_e_invalid_opcode_and_operands);
  4995. end;
  4996. end
  4997. else if (ops=3) then
  4998. begin
  4999. bytes:=bytes or getsupreg(oper[0]^.reg) shl 0;
  5000. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  5001. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  5002. end
  5003. else
  5004. begin
  5005. bytes:=bytes or getsupreg(oper[0]^.reg) shl 0;
  5006. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  5007. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  5008. bytes:=bytes or getsupreg(oper[3]^.ref^.base) shl 16;
  5009. end;
  5010. end;
  5011. #$8C: { Thumb-2: LDM/STM }
  5012. begin
  5013. { set instruction code }
  5014. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  5015. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  5016. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  5017. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  5018. if oper[0]^.typ=top_reg then
  5019. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16)
  5020. else
  5021. begin
  5022. bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 16);
  5023. if oper[0]^.ref^.addressmode<>AM_OFFSET then
  5024. bytes:=bytes or (1 shl 21);
  5025. end;
  5026. for r:=0 to 15 do
  5027. if r in oper[1]^.regset^ then
  5028. bytes:=bytes or (1 shl r);
  5029. case oppostfix of
  5030. PF_None,PF_IA,PF_FD: bytes:=bytes or ($1 shl 23);
  5031. PF_DB,PF_EA: bytes:=bytes or ($2 shl 23);
  5032. else
  5033. message1(asmw_e_invalid_opcode_and_operands, '"Invalid Postfix"');
  5034. end;
  5035. end;
  5036. #$8D: { Thumb-2: BL/BLX }
  5037. begin
  5038. { set instruction code }
  5039. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  5040. bytes:=bytes or (ord(insentry^.code[2]) shl 8);
  5041. { set offset }
  5042. if oper[0]^.typ=top_const then
  5043. offset:=(oper[0]^.val shr 1) and $FFFFFF
  5044. else
  5045. begin
  5046. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  5047. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  5048. begin
  5049. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24_THUMB);
  5050. offset:=$FFFFFE
  5051. end
  5052. else
  5053. offset:=((currsym.offset-insoffset-8) shr 1) and $FFFFFF;
  5054. end;
  5055. bytes:=bytes or ((offset shr 00) and $7FF) shl 0;
  5056. bytes:=bytes or ((offset shr 11) and $3FF) shl 16;
  5057. bytes:=bytes or (((offset shr 21) xor (offset shr 23) xor 1) and $1) shl 11;
  5058. bytes:=bytes or (((offset shr 22) xor (offset shr 23) xor 1) and $1) shl 13;
  5059. bytes:=bytes or ((offset shr 23) and $1) shl 26;
  5060. end;
  5061. #$8E: { Thumb-2: TBB/TBH }
  5062. begin
  5063. { set instruction code }
  5064. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  5065. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  5066. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  5067. bytes:=bytes or ord(insentry^.code[4]);
  5068. { set Rn and Rm }
  5069. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  5070. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  5071. message(asmw_e_invalid_effective_address)
  5072. else
  5073. begin
  5074. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  5075. if (opcode=A_TBH) and
  5076. (oper[0]^.ref^.shiftmode<>SM_LSL) and
  5077. (oper[0]^.ref^.shiftimm<>1) then
  5078. message(asmw_e_invalid_effective_address);
  5079. end;
  5080. end;
  5081. #$8F: { Thumb-2: CPSxx }
  5082. begin
  5083. { set opcode }
  5084. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  5085. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  5086. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  5087. bytes:=bytes or ord(insentry^.code[4]);
  5088. if (oper[0]^.typ=top_modeflags) then
  5089. begin
  5090. if mfA in oper[0]^.modeflags then bytes:=bytes or (1 shl 7);
  5091. if mfI in oper[0]^.modeflags then bytes:=bytes or (1 shl 6);
  5092. if mfF in oper[0]^.modeflags then bytes:=bytes or (1 shl 5);
  5093. end;
  5094. if (ops=2) then
  5095. bytes:=bytes or (oper[1]^.val and $1F)
  5096. else if (ops=1) and
  5097. (oper[0]^.typ=top_const) then
  5098. bytes:=bytes or (oper[0]^.val and $1F);
  5099. end;
  5100. #$96: { Thumb-2: MSR/MRS }
  5101. begin
  5102. { set instruction code }
  5103. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  5104. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  5105. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  5106. bytes:=bytes or ord(insentry^.code[4]);
  5107. if opcode=A_MRS then
  5108. begin
  5109. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  5110. case oper[1]^.reg of
  5111. NR_MSP: bytes:=bytes or $08;
  5112. NR_PSP: bytes:=bytes or $09;
  5113. NR_IPSR: bytes:=bytes or $05;
  5114. NR_EPSR: bytes:=bytes or $06;
  5115. NR_APSR: bytes:=bytes or $00;
  5116. NR_PRIMASK: bytes:=bytes or $10;
  5117. NR_BASEPRI: bytes:=bytes or $11;
  5118. NR_BASEPRI_MAX: bytes:=bytes or $12;
  5119. NR_FAULTMASK: bytes:=bytes or $13;
  5120. NR_CONTROL: bytes:=bytes or $14;
  5121. else
  5122. Message(asmw_e_invalid_opcode_and_operands);
  5123. end;
  5124. end
  5125. else
  5126. begin
  5127. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  5128. case oper[0]^.reg of
  5129. NR_APSR,
  5130. NR_APSR_nzcvqg: bytes:=bytes or $C00;
  5131. NR_APSR_g: bytes:=bytes or $400;
  5132. NR_APSR_nzcvq: bytes:=bytes or $800;
  5133. NR_MSP: bytes:=bytes or $08;
  5134. NR_PSP: bytes:=bytes or $09;
  5135. NR_PRIMASK: bytes:=bytes or $10;
  5136. NR_BASEPRI: bytes:=bytes or $11;
  5137. NR_BASEPRI_MAX: bytes:=bytes or $12;
  5138. NR_FAULTMASK: bytes:=bytes or $13;
  5139. NR_CONTROL: bytes:=bytes or $14;
  5140. else
  5141. Message(asmw_e_invalid_opcode_and_operands);
  5142. end;
  5143. end;
  5144. end;
  5145. #$A0: { FPA: CPDT(LDF/STF) }
  5146. begin
  5147. { set instruction code }
  5148. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  5149. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  5150. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  5151. bytes:=bytes or ord(insentry^.code[4]);
  5152. if ops=2 then
  5153. begin
  5154. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  5155. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  5156. bytes:=bytes or ((oper[1]^.ref^.offset shr 2) and $FF);
  5157. if oper[1]^.ref^.offset>=0 then
  5158. bytes:=bytes or (1 shl 23);
  5159. if oper[1]^.ref^.addressmode<>AM_OFFSET then
  5160. bytes:=bytes or (1 shl 21);
  5161. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  5162. bytes:=bytes or (1 shl 24);
  5163. case oppostfix of
  5164. PF_S: bytes:=bytes or (0 shl 22) or (0 shl 15);
  5165. PF_D: bytes:=bytes or (0 shl 22) or (1 shl 15);
  5166. PF_E: bytes:=bytes or (1 shl 22) or (0 shl 15);
  5167. PF_P: bytes:=bytes or (1 shl 22) or (1 shl 15);
  5168. PF_EP: ;
  5169. else
  5170. message1(asmw_e_invalid_opcode_and_operands, '"Invalid postfix"');
  5171. end;
  5172. end
  5173. else
  5174. begin
  5175. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  5176. case oper[1]^.val of
  5177. 1: bytes:=bytes or (1 shl 15);
  5178. 2: bytes:=bytes or (1 shl 22);
  5179. 3: bytes:=bytes or (1 shl 22) or (1 shl 15);
  5180. 4: ;
  5181. else
  5182. message1(asmw_e_invalid_opcode_and_operands, 'Invalid count for LFM/SFM');
  5183. end;
  5184. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  5185. bytes:=bytes or ((oper[2]^.ref^.offset shr 2) and $FF);
  5186. if oper[2]^.ref^.offset>=0 then
  5187. bytes:=bytes or (1 shl 23);
  5188. if oper[2]^.ref^.addressmode<>AM_OFFSET then
  5189. bytes:=bytes or (1 shl 21);
  5190. if oper[2]^.ref^.addressmode=AM_PREINDEXED then
  5191. bytes:=bytes or (1 shl 24);
  5192. end;
  5193. end;
  5194. #$A1: { FPA: CPDO }
  5195. begin
  5196. { set instruction code }
  5197. bytes:=bytes or ($E shl 24);
  5198. bytes:=bytes or (ord(insentry^.code[1]) shl 15);
  5199. bytes:=bytes or ((ord(insentry^.code[2]) shr 1) shl 20);
  5200. bytes:=bytes or (1 shl 8);
  5201. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  5202. if ops=2 then
  5203. begin
  5204. if oper[1]^.typ=top_reg then
  5205. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0
  5206. else
  5207. case oper[1]^.val of
  5208. 0: bytes:=bytes or $8;
  5209. 1: bytes:=bytes or $9;
  5210. 2: bytes:=bytes or $A;
  5211. 3: bytes:=bytes or $B;
  5212. 4: bytes:=bytes or $C;
  5213. 5: bytes:=bytes or $D;
  5214. //0.5: bytes:=bytes or $E;
  5215. 10: bytes:=bytes or $F;
  5216. else
  5217. Message(asmw_e_invalid_opcode_and_operands);
  5218. end;
  5219. end
  5220. else
  5221. begin
  5222. bytes:=bytes or getsupreg(oper[1]^.reg) shl 16;
  5223. if oper[2]^.typ=top_reg then
  5224. bytes:=bytes or getsupreg(oper[2]^.reg) shl 0
  5225. else
  5226. case oper[2]^.val of
  5227. 0: bytes:=bytes or $8;
  5228. 1: bytes:=bytes or $9;
  5229. 2: bytes:=bytes or $A;
  5230. 3: bytes:=bytes or $B;
  5231. 4: bytes:=bytes or $C;
  5232. 5: bytes:=bytes or $D;
  5233. //0.5: bytes:=bytes or $E;
  5234. 10: bytes:=bytes or $F;
  5235. else
  5236. Message(asmw_e_invalid_opcode_and_operands);
  5237. end;
  5238. end;
  5239. case roundingmode of
  5240. RM_NONE: ;
  5241. RM_P: bytes:=bytes or (1 shl 5);
  5242. RM_M: bytes:=bytes or (2 shl 5);
  5243. RM_Z: bytes:=bytes or (3 shl 5);
  5244. end;
  5245. case oppostfix of
  5246. PF_S: bytes:=bytes or (0 shl 19) or (0 shl 7);
  5247. PF_D: bytes:=bytes or (0 shl 19) or (1 shl 7);
  5248. PF_E: bytes:=bytes or (1 shl 19) or (0 shl 7);
  5249. else
  5250. message1(asmw_e_invalid_opcode_and_operands, 'Precision cannot be undefined');
  5251. end;
  5252. end;
  5253. #$A2: { FPA: CPDO }
  5254. begin
  5255. { set instruction code }
  5256. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  5257. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  5258. bytes:=bytes or ($11 shl 4);
  5259. case opcode of
  5260. A_FLT:
  5261. begin
  5262. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  5263. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 12);
  5264. case roundingmode of
  5265. RM_NONE: ;
  5266. RM_P: bytes:=bytes or (1 shl 5);
  5267. RM_M: bytes:=bytes or (2 shl 5);
  5268. RM_Z: bytes:=bytes or (3 shl 5);
  5269. end;
  5270. case oppostfix of
  5271. PF_S: bytes:=bytes or (0 shl 19) or (0 shl 7);
  5272. PF_D: bytes:=bytes or (0 shl 19) or (1 shl 7);
  5273. PF_E: bytes:=bytes or (1 shl 19) or (0 shl 7);
  5274. else
  5275. message1(asmw_e_invalid_opcode_and_operands, 'Precision cannot be undefined');
  5276. end;
  5277. end;
  5278. A_FIX:
  5279. begin
  5280. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  5281. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  5282. case roundingmode of
  5283. RM_NONE: ;
  5284. RM_P: bytes:=bytes or (1 shl 5);
  5285. RM_M: bytes:=bytes or (2 shl 5);
  5286. RM_Z: bytes:=bytes or (3 shl 5);
  5287. end;
  5288. end;
  5289. A_WFS,A_RFS,A_WFC,A_RFC:
  5290. begin
  5291. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  5292. end;
  5293. A_CMF,A_CNF,A_CMFE,A_CNFE:
  5294. begin
  5295. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  5296. if oper[1]^.typ=top_reg then
  5297. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0
  5298. else
  5299. case oper[1]^.val of
  5300. 0: bytes:=bytes or $8;
  5301. 1: bytes:=bytes or $9;
  5302. 2: bytes:=bytes or $A;
  5303. 3: bytes:=bytes or $B;
  5304. 4: bytes:=bytes or $C;
  5305. 5: bytes:=bytes or $D;
  5306. //0.5: bytes:=bytes or $E;
  5307. 10: bytes:=bytes or $F;
  5308. else
  5309. Message(asmw_e_invalid_opcode_and_operands);
  5310. end;
  5311. end;
  5312. else
  5313. Message1(asmw_e_invalid_opcode_and_operands, '"Unsupported opcode"');
  5314. end;
  5315. end;
  5316. #$fe: // No written data
  5317. begin
  5318. exit;
  5319. end;
  5320. #$ff:
  5321. internalerror(2005091101);
  5322. else
  5323. begin
  5324. writeln(ord(insentry^.code[0]), ' - ', opcode);
  5325. internalerror(2005091102);
  5326. end;
  5327. end;
  5328. { Todo: Decide whether the code above should take care of writing data in an order that makes senes }
  5329. if (insentry^.code[0] in [#$80..#$96]) and (bytelen=4) then
  5330. bytes:=((bytes shr 16) and $FFFF) or ((bytes and $FFFF) shl 16);
  5331. { we're finished, write code }
  5332. objdata.writebytes(bytes,bytelen);
  5333. end;
  5334. begin
  5335. cai_align:=tai_align;
  5336. end.