aasmcpu.pas 212 KB

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