aasmcpu.pas 180 KB

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