aasmcpu.pas 175 KB

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