aasmcpu.pas 214 KB

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