aasmcpu.pas 201 KB

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