aasmcpu.pas 200 KB

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