aasmcpu.pas 213 KB

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