aasmcpu.pas 214 KB

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