2
0

aasmcpu.pas 213 KB

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