aasmcpu.pas 214 KB

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