aasmcpu.pas 181 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950
  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) and
  2076. (not inIT) and
  2077. (not lastinIT)) then
  2078. begin
  2079. Matches:=0;
  2080. exit;
  2081. end;
  2082. end
  2083. else if p^.code[0]=#$63 then
  2084. begin
  2085. if inIT then
  2086. begin
  2087. Matches:=0;
  2088. exit;
  2089. end;
  2090. end
  2091. else if p^.code[0]=#$64 then
  2092. begin
  2093. if (opcode=A_MUL) then
  2094. begin
  2095. if (ops<>3) or
  2096. (oper[2]^.typ<>top_reg) or
  2097. (oper[0]^.reg<>oper[2]^.reg) then
  2098. begin
  2099. matches:=0;
  2100. exit;
  2101. end;
  2102. end;
  2103. end;
  2104. { Check operand sizes }
  2105. { as default an untyped size can get all the sizes, this is different
  2106. from nasm, but else we need to do a lot checking which opcodes want
  2107. size or not with the automatic size generation }
  2108. (*
  2109. asize:=longint($ffffffff);
  2110. if (p^.flags and IF_SB)<>0 then
  2111. asize:=OT_BITS8
  2112. else if (p^.flags and IF_SW)<>0 then
  2113. asize:=OT_BITS16
  2114. else if (p^.flags and IF_SD)<>0 then
  2115. asize:=OT_BITS32;
  2116. if (p^.flags and IF_ARMASK)<>0 then
  2117. begin
  2118. siz[0]:=0;
  2119. siz[1]:=0;
  2120. siz[2]:=0;
  2121. if (p^.flags and IF_AR0)<>0 then
  2122. siz[0]:=asize
  2123. else if (p^.flags and IF_AR1)<>0 then
  2124. siz[1]:=asize
  2125. else if (p^.flags and IF_AR2)<>0 then
  2126. siz[2]:=asize;
  2127. end
  2128. else
  2129. begin
  2130. { we can leave because the size for all operands is forced to be
  2131. the same
  2132. but not if IF_SB IF_SW or IF_SD is set PM }
  2133. if asize=-1 then
  2134. exit;
  2135. siz[0]:=asize;
  2136. siz[1]:=asize;
  2137. siz[2]:=asize;
  2138. end;
  2139. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  2140. begin
  2141. if (p^.flags and IF_SM2)<>0 then
  2142. oprs:=2
  2143. else
  2144. oprs:=p^.ops;
  2145. for i:=0 to oprs-1 do
  2146. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  2147. begin
  2148. for j:=0 to oprs-1 do
  2149. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  2150. break;
  2151. end;
  2152. end
  2153. else
  2154. oprs:=2;
  2155. { Check operand sizes }
  2156. for i:=0 to p^.ops-1 do
  2157. begin
  2158. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  2159. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  2160. { Immediates can always include smaller size }
  2161. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  2162. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  2163. Matches:=2;
  2164. end;
  2165. *)
  2166. end;
  2167. function taicpu.calcsize(p:PInsEntry):shortint;
  2168. begin
  2169. result:=4;
  2170. end;
  2171. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  2172. begin
  2173. Result:=False; { unimplemented }
  2174. end;
  2175. procedure taicpu.Swapoperands;
  2176. begin
  2177. end;
  2178. function taicpu.FindInsentry(objdata:TObjData):boolean;
  2179. var
  2180. i : longint;
  2181. begin
  2182. result:=false;
  2183. { Things which may only be done once, not when a second pass is done to
  2184. optimize }
  2185. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  2186. begin
  2187. { create the .ot fields }
  2188. create_ot(objdata);
  2189. BuildArmMasks;
  2190. { set the file postion }
  2191. current_filepos:=fileinfo;
  2192. end
  2193. else
  2194. begin
  2195. { we've already an insentry so it's valid }
  2196. result:=true;
  2197. exit;
  2198. end;
  2199. { Lookup opcode in the table }
  2200. InsSize:=-1;
  2201. i:=instabcache^[opcode];
  2202. if i=-1 then
  2203. begin
  2204. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  2205. exit;
  2206. end;
  2207. insentry:=@instab[i];
  2208. while (insentry^.opcode=opcode) do
  2209. begin
  2210. if matches(insentry)=100 then
  2211. begin
  2212. result:=true;
  2213. exit;
  2214. end;
  2215. inc(i);
  2216. insentry:=@instab[i];
  2217. end;
  2218. if (ops=3) and (opcode=a_sub) then writeln(oppostfix,',',oper[2]^.val);
  2219. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2220. { No instruction found, set insentry to nil and inssize to -1 }
  2221. insentry:=nil;
  2222. inssize:=-1;
  2223. end;
  2224. procedure taicpu.gencode(objdata:TObjData);
  2225. const
  2226. CondVal : array[TAsmCond] of byte=(
  2227. $E, $0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $A,
  2228. $B, $C, $D, $E, 0);
  2229. var
  2230. bytes, rd, rm, rn, d, m, n : dword;
  2231. bytelen : longint;
  2232. dp_operation : boolean;
  2233. i_field : byte;
  2234. currsym : TObjSymbol;
  2235. offset : longint;
  2236. refoper : poper;
  2237. msb : longint;
  2238. r: byte;
  2239. procedure setshifterop(op : byte);
  2240. var
  2241. r : byte;
  2242. imm : dword;
  2243. count : integer;
  2244. begin
  2245. case oper[op]^.typ of
  2246. top_const:
  2247. begin
  2248. i_field:=1;
  2249. if oper[op]^.val and $ff=oper[op]^.val then
  2250. bytes:=bytes or dword(oper[op]^.val)
  2251. else
  2252. begin
  2253. { calc rotate and adjust imm }
  2254. count:=0;
  2255. r:=0;
  2256. imm:=dword(oper[op]^.val);
  2257. repeat
  2258. imm:=RolDWord(imm, 2);
  2259. inc(r);
  2260. inc(count);
  2261. if count > 32 then
  2262. begin
  2263. message1(asmw_e_invalid_opcode_and_operands, 'invalid shifter imm');
  2264. exit;
  2265. end;
  2266. until (imm and $ff)=imm;
  2267. bytes:=bytes or (r shl 8) or imm;
  2268. end;
  2269. end;
  2270. top_reg:
  2271. begin
  2272. i_field:=0;
  2273. bytes:=bytes or getsupreg(oper[op]^.reg);
  2274. { does a real shifter op follow? }
  2275. if (op+1<opercnt) and (oper[op+1]^.typ=top_shifterop) then
  2276. with oper[op+1]^.shifterop^ do
  2277. begin
  2278. bytes:=bytes or (shiftimm shl 7);
  2279. if shiftmode<>SM_RRX then
  2280. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2281. else
  2282. bytes:=bytes or (3 shl 5);
  2283. if getregtype(rs) <> R_INVALIDREGISTER then
  2284. begin
  2285. bytes:=bytes or (1 shl 4);
  2286. bytes:=bytes or (getsupreg(rs) shl 8);
  2287. end
  2288. end;
  2289. end;
  2290. else
  2291. internalerror(2005091103);
  2292. end;
  2293. end;
  2294. function MakeRegList(reglist: tcpuregisterset): word;
  2295. var
  2296. i, w: word;
  2297. begin
  2298. result:=0;
  2299. w:=1;
  2300. for i:=RS_R0 to RS_R15 do
  2301. begin
  2302. if i in reglist then
  2303. result:=result or w;
  2304. w:=w shl 1
  2305. end;
  2306. end;
  2307. function getcoproc(reg: tregister): byte;
  2308. begin
  2309. if reg=NR_p15 then
  2310. result:=15
  2311. else
  2312. begin
  2313. Message1(asmw_e_invalid_opcode_and_operands,'Invalid coprocessor port');
  2314. result:=0;
  2315. end;
  2316. end;
  2317. function getcoprocreg(reg: tregister): byte;
  2318. begin
  2319. result:=getsupreg(reg)-getsupreg(NR_CR0);
  2320. end;
  2321. function getmmreg(reg: tregister): byte;
  2322. begin
  2323. case reg of
  2324. NR_D0: result:=0;
  2325. NR_D1: result:=1;
  2326. NR_D2: result:=2;
  2327. NR_D3: result:=3;
  2328. NR_D4: result:=4;
  2329. NR_D5: result:=5;
  2330. NR_D6: result:=6;
  2331. NR_D7: result:=7;
  2332. NR_D8: result:=8;
  2333. NR_D9: result:=9;
  2334. NR_D10: result:=10;
  2335. NR_D11: result:=11;
  2336. NR_D12: result:=12;
  2337. NR_D13: result:=13;
  2338. NR_D14: result:=14;
  2339. NR_D15: result:=15;
  2340. NR_D16: result:=16;
  2341. NR_D17: result:=17;
  2342. NR_D18: result:=18;
  2343. NR_D19: result:=19;
  2344. NR_D20: result:=20;
  2345. NR_D21: result:=21;
  2346. NR_D22: result:=22;
  2347. NR_D23: result:=23;
  2348. NR_D24: result:=24;
  2349. NR_D25: result:=25;
  2350. NR_D26: result:=26;
  2351. NR_D27: result:=27;
  2352. NR_D28: result:=28;
  2353. NR_D29: result:=29;
  2354. NR_D30: result:=30;
  2355. NR_D31: result:=31;
  2356. NR_S0: result:=0;
  2357. NR_S1: result:=1;
  2358. NR_S2: result:=2;
  2359. NR_S3: result:=3;
  2360. NR_S4: result:=4;
  2361. NR_S5: result:=5;
  2362. NR_S6: result:=6;
  2363. NR_S7: result:=7;
  2364. NR_S8: result:=8;
  2365. NR_S9: result:=9;
  2366. NR_S10: result:=10;
  2367. NR_S11: result:=11;
  2368. NR_S12: result:=12;
  2369. NR_S13: result:=13;
  2370. NR_S14: result:=14;
  2371. NR_S15: result:=15;
  2372. NR_S16: result:=16;
  2373. NR_S17: result:=17;
  2374. NR_S18: result:=18;
  2375. NR_S19: result:=19;
  2376. NR_S20: result:=20;
  2377. NR_S21: result:=21;
  2378. NR_S22: result:=22;
  2379. NR_S23: result:=23;
  2380. NR_S24: result:=24;
  2381. NR_S25: result:=25;
  2382. NR_S26: result:=26;
  2383. NR_S27: result:=27;
  2384. NR_S28: result:=28;
  2385. NR_S29: result:=29;
  2386. NR_S30: result:=30;
  2387. NR_S31: result:=31;
  2388. else
  2389. result:=0;
  2390. end;
  2391. end;
  2392. procedure encodethumbimm(imm: longword);
  2393. var
  2394. imm12, tmp: tcgint;
  2395. shift: integer;
  2396. found: boolean;
  2397. begin
  2398. found:=true;
  2399. if (imm and $FF) = imm then
  2400. imm12:=imm
  2401. else if ((imm shr 16)=(imm and $FFFF)) and
  2402. ((imm and $FF00FF00) = 0) then
  2403. imm12:=(imm and $ff) or ($1 shl 8)
  2404. else if ((imm shr 16)=(imm and $FFFF)) and
  2405. ((imm and $00FF00FF) = 0) then
  2406. imm12:=((imm shr 8) and $ff) or ($2 shl 8)
  2407. else if ((imm shr 16)=(imm and $FFFF)) and
  2408. (((imm shr 8) and $FF)=(imm and $FF)) then
  2409. imm12:=(imm and $ff) or ($3 shl 8)
  2410. else
  2411. begin
  2412. found:=false;
  2413. for shift:=1 to 31 do
  2414. begin
  2415. tmp:=RolDWord(imm,shift);
  2416. if ((tmp and $FF)=tmp) and
  2417. ((tmp and $80)=$80) then
  2418. begin
  2419. imm12:=(tmp and $7F) or (shift shl 7);
  2420. found:=true;
  2421. break;
  2422. end;
  2423. end;
  2424. end;
  2425. if found then
  2426. begin
  2427. bytes:=bytes or (imm12 and $FF);
  2428. bytes:=bytes or (((imm12 shr 8) and $7) shl 12);
  2429. bytes:=bytes or (((imm12 shr 11) and $1) shl 26);
  2430. end
  2431. else
  2432. Message1(asmw_e_value_exceeds_bounds, IntToStr(imm));
  2433. end;
  2434. procedure setthumbshift(op: byte; is_sat: boolean = false);
  2435. var
  2436. shift,typ: byte;
  2437. begin
  2438. case oper[op]^.shifterop^.shiftmode of
  2439. SM_LSL: begin typ:=0; shift:=oper[op]^.shifterop^.shiftimm; end;
  2440. SM_LSR: begin typ:=1; shift:=oper[op]^.shifterop^.shiftimm; if shift=32 then shift:=0; end;
  2441. SM_ASR: begin typ:=2; shift:=oper[op]^.shifterop^.shiftimm; if shift=32 then shift:=0; end;
  2442. SM_ROR: begin typ:=3; shift:=oper[op]^.shifterop^.shiftimm; if shift=0 then message(asmw_e_invalid_opcode_and_operands); end;
  2443. SM_RRX: begin typ:=3; shift:=oper[op]^.shifterop^.shiftimm; shift:=0; end;
  2444. end;
  2445. if is_sat then
  2446. begin
  2447. bytes:=bytes or ((typ and 1) shl 5);
  2448. bytes:=bytes or ((typ shr 1) shl 21);
  2449. end
  2450. else
  2451. bytes:=bytes or (typ shl 4);
  2452. bytes:=bytes or (shift and $3) shl 6;
  2453. bytes:=bytes or ((shift and $1C) shr 2) shl 12;
  2454. end;
  2455. begin
  2456. bytes:=$0;
  2457. bytelen:=4;
  2458. i_field:=0;
  2459. { evaluate and set condition code }
  2460. bytes:=bytes or (CondVal[condition] shl 28);
  2461. { condition code allowed? }
  2462. { setup rest of the instruction }
  2463. case insentry^.code[0] of
  2464. #$01: // B/BL
  2465. begin
  2466. { set instruction code }
  2467. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2468. { set offset }
  2469. if oper[0]^.typ=top_const then
  2470. bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
  2471. else
  2472. begin
  2473. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  2474. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  2475. begin
  2476. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24);
  2477. bytes:=bytes or $fffffe; // TODO: Not sure this is right, but it matches the output of gas
  2478. end
  2479. else
  2480. bytes:=bytes or (((currsym.offset-insoffset-8) shr 2) and $ffffff);
  2481. end;
  2482. end;
  2483. #$02:
  2484. begin
  2485. { set instruction code }
  2486. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2487. { set code }
  2488. bytes:=bytes or (oper[0]^.val and $FFFFFF);
  2489. end;
  2490. #$03:
  2491. begin // BLX/BX
  2492. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2493. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2494. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2495. bytes:=bytes or ord(insentry^.code[4]);
  2496. bytes:=bytes or getsupreg(oper[0]^.reg);
  2497. end;
  2498. #$04..#$07: // SUB
  2499. begin
  2500. { set instruction code }
  2501. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2502. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2503. { set destination }
  2504. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2505. { set Rn }
  2506. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  2507. { create shifter op }
  2508. setshifterop(2);
  2509. { set I field }
  2510. bytes:=bytes or (i_field shl 25);
  2511. { set S if necessary }
  2512. if oppostfix=PF_S then
  2513. bytes:=bytes or (1 shl 20);
  2514. end;
  2515. #$08,#$0A,#$0B: // MOV
  2516. begin
  2517. { set instruction code }
  2518. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2519. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2520. { set destination }
  2521. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2522. { create shifter op }
  2523. setshifterop(1);
  2524. { set I field }
  2525. bytes:=bytes or (i_field shl 25);
  2526. { set S if necessary }
  2527. if oppostfix=PF_S then
  2528. bytes:=bytes or (1 shl 20);
  2529. end;
  2530. #$0C,#$0E,#$0F: // CMP
  2531. begin
  2532. { set instruction code }
  2533. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2534. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2535. { set destination }
  2536. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  2537. { create shifter op }
  2538. setshifterop(1);
  2539. { set I field }
  2540. bytes:=bytes or (i_field shl 25);
  2541. { always set S bit }
  2542. bytes:=bytes or (1 shl 20);
  2543. end;
  2544. #$10: // MRS
  2545. begin
  2546. { set instruction code }
  2547. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2548. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2549. { set destination }
  2550. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2551. case oper[1]^.reg of
  2552. NR_APSR,NR_CPSR:;
  2553. else
  2554. Message(asmw_e_invalid_opcode_and_operands);
  2555. end;
  2556. end;
  2557. #$12,#$13: // MSR
  2558. begin
  2559. { set instruction code }
  2560. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2561. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2562. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2563. { set destination }
  2564. if oper[0]^.typ=top_specialreg then
  2565. begin
  2566. if oper[0]^.specialreg<>NR_CPSR then
  2567. Message1(asmw_e_invalid_opcode_and_operands, 'Can only use CPSR in this form');
  2568. if srF in oper[0]^.specialflags then
  2569. bytes:=bytes or (2 shl 18);
  2570. if srS in oper[0]^.specialflags then
  2571. bytes:=bytes or (1 shl 18);
  2572. end
  2573. else
  2574. case oper[0]^.reg of
  2575. NR_APSR_nzcvq: bytes:=bytes or (2 shl 18);
  2576. NR_APSR_g: bytes:=bytes or (1 shl 18);
  2577. NR_APSR_nzcvqg: bytes:=bytes or (3 shl 18);
  2578. else
  2579. Message1(asmw_e_invalid_opcode_and_operands, 'Invalid combination APSR bits used');
  2580. end;
  2581. setshifterop(1);
  2582. end;
  2583. #$14: // MUL/MLA r1,r2,r3
  2584. begin
  2585. { set instruction code }
  2586. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2587. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2588. bytes:=bytes or ord(insentry^.code[3]);
  2589. { set regs }
  2590. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2591. bytes:=bytes or getsupreg(oper[1]^.reg);
  2592. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2593. end;
  2594. #$15: // MUL/MLA r1,r2,r3,r4
  2595. begin
  2596. { set instruction code }
  2597. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2598. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2599. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2600. { set regs }
  2601. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2602. bytes:=bytes or getsupreg(oper[1]^.reg);
  2603. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2604. if ops>3 then
  2605. bytes:=bytes or getsupreg(oper[3]^.reg) shl 12
  2606. else
  2607. bytes:=bytes or ord(insentry^.code[4]) shl 12;
  2608. if oppostfix in [PF_R,PF_X] then
  2609. bytes:=bytes or (1 shl 5);
  2610. end;
  2611. #$16: // MULL r1,r2,r3,r4
  2612. begin
  2613. { set instruction code }
  2614. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2615. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2616. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2617. { set regs }
  2618. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2619. if (ops=3) and (opcode=A_PKHTB) then
  2620. begin
  2621. bytes:=bytes or getsupreg(oper[1]^.reg);
  2622. bytes:=bytes or getsupreg(oper[2]^.reg) shl 16;
  2623. end
  2624. else
  2625. begin
  2626. bytes:=bytes or getsupreg(oper[1]^.reg) shl 16;
  2627. bytes:=bytes or getsupreg(oper[2]^.reg);
  2628. end;
  2629. if ops=4 then
  2630. begin
  2631. if oper[3]^.typ=top_shifterop then
  2632. begin
  2633. if opcode in [A_PKHBT,A_PKHTB] then
  2634. begin
  2635. if ((opcode=A_PKHTB) and
  2636. (oper[3]^.shifterop^.shiftmode <> SM_ASR)) or
  2637. ((opcode=A_PKHBT) and
  2638. (oper[3]^.shifterop^.shiftmode <> SM_LSL)) or
  2639. (oper[3]^.shifterop^.rs<>NR_NO) then
  2640. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2641. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
  2642. end
  2643. else
  2644. begin
  2645. if (oper[3]^.shifterop^.shiftmode<>sm_ror) or
  2646. (oper[3]^.shifterop^.rs<>NR_NO) or
  2647. (not (oper[3]^.shifterop^.shiftimm in [0,8,16,24])) then
  2648. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2649. bytes:=bytes or (((oper[3]^.shifterop^.shiftimm shr 3) and $3) shl 10);
  2650. end;
  2651. end
  2652. else
  2653. bytes:=bytes or getsupreg(oper[3]^.reg) shl 8;
  2654. end;
  2655. if PF_S=oppostfix then
  2656. bytes:=bytes or (1 shl 20);
  2657. if PF_X=oppostfix then
  2658. bytes:=bytes or (1 shl 5);
  2659. end;
  2660. #$17: // LDR/STR
  2661. begin
  2662. { set instruction code }
  2663. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2664. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2665. { set Rn and Rd }
  2666. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2667. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  2668. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  2669. begin
  2670. { set offset }
  2671. offset:=0;
  2672. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  2673. if assigned(currsym) then
  2674. offset:=currsym.offset-insoffset-8;
  2675. offset:=offset+oper[1]^.ref^.offset;
  2676. if offset>=0 then
  2677. begin
  2678. { set U flag }
  2679. bytes:=bytes or (1 shl 23);
  2680. bytes:=bytes or offset
  2681. end
  2682. else
  2683. begin
  2684. offset:=-offset;
  2685. bytes:=bytes or offset
  2686. end;
  2687. end
  2688. else
  2689. begin
  2690. { set U flag }
  2691. if oper[1]^.ref^.signindex>=0 then
  2692. bytes:=bytes or (1 shl 23);
  2693. { set I flag }
  2694. bytes:=bytes or (1 shl 25);
  2695. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  2696. { set shift }
  2697. with oper[1]^.ref^ do
  2698. if shiftmode<>SM_None then
  2699. begin
  2700. bytes:=bytes or (shiftimm shl 7);
  2701. if shiftmode<>SM_RRX then
  2702. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2703. else
  2704. bytes:=bytes or (3 shl 5);
  2705. end
  2706. end;
  2707. { set W bit }
  2708. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  2709. bytes:=bytes or (1 shl 21);
  2710. { set P bit if necessary }
  2711. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  2712. bytes:=bytes or (1 shl 24);
  2713. end;
  2714. #$18: // LDREX/STREX
  2715. begin
  2716. { set instruction code }
  2717. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2718. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2719. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2720. bytes:=bytes or ord(insentry^.code[4]);
  2721. { set Rn and Rd }
  2722. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2723. if (ops=3) then
  2724. begin
  2725. if opcode<>A_LDREXD then
  2726. bytes:=bytes or getsupreg(oper[1]^.reg);
  2727. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  2728. end
  2729. else if (ops=4) then // STREXD
  2730. begin
  2731. if opcode<>A_LDREXD then
  2732. bytes:=bytes or getsupreg(oper[1]^.reg);
  2733. bytes:=bytes or (getsupreg(oper[3]^.ref^.base) shl 16);
  2734. end
  2735. else
  2736. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 16);
  2737. end;
  2738. #$19: // LDRD/STRD
  2739. begin
  2740. { set instruction code }
  2741. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2742. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2743. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2744. bytes:=bytes or ord(insentry^.code[4]);
  2745. { set Rn and Rd }
  2746. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2747. refoper:=oper[1];
  2748. if ops=3 then
  2749. refoper:=oper[2];
  2750. bytes:=bytes or getsupreg(refoper^.ref^.base) shl 16;
  2751. if getregtype(refoper^.ref^.index)=R_INVALIDREGISTER then
  2752. begin
  2753. bytes:=bytes or (1 shl 22);
  2754. { set offset }
  2755. offset:=0;
  2756. currsym:=objdata.symbolref(refoper^.ref^.symbol);
  2757. if assigned(currsym) then
  2758. offset:=currsym.offset-insoffset-8;
  2759. offset:=offset+refoper^.ref^.offset;
  2760. if offset>=0 then
  2761. begin
  2762. { set U flag }
  2763. bytes:=bytes or (1 shl 23);
  2764. bytes:=bytes or (offset and $F);
  2765. bytes:=bytes or ((offset and $F0) shl 4);
  2766. end
  2767. else
  2768. begin
  2769. offset:=-offset;
  2770. bytes:=bytes or (offset and $F);
  2771. bytes:=bytes or ((offset and $F0) shl 4);
  2772. end;
  2773. end
  2774. else
  2775. begin
  2776. { set U flag }
  2777. if refoper^.ref^.signindex>=0 then
  2778. bytes:=bytes or (1 shl 23);
  2779. bytes:=bytes or getsupreg(refoper^.ref^.index);
  2780. end;
  2781. { set W bit }
  2782. if refoper^.ref^.addressmode=AM_PREINDEXED then
  2783. bytes:=bytes or (1 shl 21);
  2784. { set P bit if necessary }
  2785. if refoper^.ref^.addressmode<>AM_POSTINDEXED then
  2786. bytes:=bytes or (1 shl 24);
  2787. end;
  2788. #$1A: // QADD/QSUB
  2789. begin
  2790. { set instruction code }
  2791. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2792. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2793. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2794. { set regs }
  2795. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2796. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0;
  2797. bytes:=bytes or getsupreg(oper[2]^.reg) shl 16;
  2798. end;
  2799. #$1B:
  2800. begin
  2801. { set instruction code }
  2802. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2803. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2804. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2805. { set regs }
  2806. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2807. bytes:=bytes or getsupreg(oper[1]^.reg);
  2808. if ops=3 then
  2809. begin
  2810. if (oper[2]^.shifterop^.shiftmode<>sm_ror) or
  2811. (oper[2]^.shifterop^.rs<>NR_NO) or
  2812. (not (oper[2]^.shifterop^.shiftimm in [0,8,16,24])) then
  2813. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2814. bytes:=bytes or (((oper[2]^.shifterop^.shiftimm shr 3) and $3) shl 10);
  2815. end;
  2816. end;
  2817. #$1C: // MCR/MRC
  2818. begin
  2819. { set instruction code }
  2820. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2821. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2822. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2823. { set regs and operands }
  2824. bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
  2825. bytes:=bytes or ((oper[1]^.val and $7) shl 21);
  2826. bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
  2827. bytes:=bytes or getcoprocreg(oper[3]^.reg) shl 16;
  2828. bytes:=bytes or getcoprocreg(oper[4]^.reg);
  2829. if ops > 5 then
  2830. bytes:=bytes or ((oper[5]^.val and $7) shl 5);
  2831. end;
  2832. #$1D: // MCRR/MRRC
  2833. begin
  2834. { set instruction code }
  2835. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2836. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2837. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2838. { set regs and operands }
  2839. bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
  2840. bytes:=bytes or ((oper[1]^.val and $7) shl 4);
  2841. bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
  2842. bytes:=bytes or getsupreg(oper[3]^.reg) shl 16;
  2843. bytes:=bytes or getcoprocreg(oper[4]^.reg);
  2844. end;
  2845. #$1E: // LDRHT/STRHT
  2846. begin
  2847. { set instruction code }
  2848. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2849. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2850. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2851. bytes:=bytes or ord(insentry^.code[4]);
  2852. { set Rn and Rd }
  2853. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2854. refoper:=oper[1];
  2855. bytes:=bytes or getsupreg(refoper^.ref^.base) shl 16;
  2856. if getregtype(refoper^.ref^.index)=R_INVALIDREGISTER then
  2857. begin
  2858. bytes:=bytes or (1 shl 22);
  2859. { set offset }
  2860. offset:=0;
  2861. currsym:=objdata.symbolref(refoper^.ref^.symbol);
  2862. if assigned(currsym) then
  2863. offset:=currsym.offset-insoffset-8;
  2864. offset:=offset+refoper^.ref^.offset;
  2865. if offset>=0 then
  2866. begin
  2867. { set U flag }
  2868. bytes:=bytes or (1 shl 23);
  2869. bytes:=bytes or (offset and $F);
  2870. bytes:=bytes or ((offset and $F0) shl 4);
  2871. end
  2872. else
  2873. begin
  2874. offset:=-offset;
  2875. bytes:=bytes or (offset and $F);
  2876. bytes:=bytes or ((offset and $F0) shl 4);
  2877. end;
  2878. end
  2879. else
  2880. begin
  2881. { set U flag }
  2882. if refoper^.ref^.signindex>=0 then
  2883. bytes:=bytes or (1 shl 23);
  2884. bytes:=bytes or getsupreg(refoper^.ref^.index);
  2885. end;
  2886. end;
  2887. #$22: // LDRH/STRH
  2888. begin
  2889. { set instruction code }
  2890. bytes:=bytes or (ord(insentry^.code[1]) shl 16);
  2891. bytes:=bytes or ord(insentry^.code[2]);
  2892. { src/dest register (Rd) }
  2893. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2894. { base register (Rn) }
  2895. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  2896. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  2897. begin
  2898. bytes:=bytes or (1 shl 22); // with immediate offset
  2899. if oper[1]^.ref^.offset < 0 then
  2900. begin
  2901. bytes:=bytes or ((-oper[1]^.ref^.offset) and $f0 shl 4);
  2902. bytes:=bytes or ((-oper[1]^.ref^.offset) and $f);
  2903. end
  2904. else
  2905. begin
  2906. { set U bit }
  2907. bytes:=bytes or (1 shl 23);
  2908. bytes:=bytes or (oper[1]^.ref^.offset and $f0 shl 4);
  2909. bytes:=bytes or (oper[1]^.ref^.offset and $f);
  2910. end;
  2911. end
  2912. else
  2913. begin
  2914. { set U flag }
  2915. bytes:=bytes or (1 shl 23);
  2916. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  2917. end;
  2918. { set W bit }
  2919. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  2920. bytes:=bytes or (1 shl 21);
  2921. { set P bit if necessary }
  2922. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  2923. bytes:=bytes or (1 shl 24);
  2924. end;
  2925. #$25: // PLD/PLI
  2926. begin
  2927. { set instruction code }
  2928. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2929. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2930. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2931. bytes:=bytes or ord(insentry^.code[4]);
  2932. { set Rn and Rd }
  2933. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  2934. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  2935. begin
  2936. { set offset }
  2937. offset:=0;
  2938. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  2939. if assigned(currsym) then
  2940. offset:=currsym.offset-insoffset-8;
  2941. offset:=offset+oper[0]^.ref^.offset;
  2942. if offset>=0 then
  2943. begin
  2944. { set U flag }
  2945. bytes:=bytes or (1 shl 23);
  2946. bytes:=bytes or offset
  2947. end
  2948. else
  2949. begin
  2950. offset:=-offset;
  2951. bytes:=bytes or offset
  2952. end;
  2953. end
  2954. else
  2955. begin
  2956. bytes:=bytes or (1 shl 25);
  2957. { set U flag }
  2958. if oper[0]^.ref^.signindex>=0 then
  2959. bytes:=bytes or (1 shl 23);
  2960. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  2961. { set shift }
  2962. with oper[0]^.ref^ do
  2963. if shiftmode<>SM_None then
  2964. begin
  2965. bytes:=bytes or (shiftimm shl 7);
  2966. if shiftmode<>SM_RRX then
  2967. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2968. else
  2969. bytes:=bytes or (3 shl 5);
  2970. end
  2971. end;
  2972. end;
  2973. #$26: // LDM/STM
  2974. begin
  2975. { set instruction code }
  2976. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  2977. if ops>1 then
  2978. begin
  2979. if oper[0]^.typ=top_ref then
  2980. begin
  2981. { set W bit }
  2982. if oper[0]^.ref^.addressmode=AM_PREINDEXED then
  2983. bytes:=bytes or (1 shl 21);
  2984. { set Rn }
  2985. bytes:=bytes or (getsupreg(oper[0]^.ref^.index) shl 16);
  2986. end
  2987. else { typ=top_reg }
  2988. begin
  2989. { set Rn }
  2990. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  2991. end;
  2992. { reglist }
  2993. bytes:=bytes or MakeRegList(oper[1]^.regset^);
  2994. end
  2995. else
  2996. begin
  2997. { push/pop }
  2998. { Set W and Rn to SP }
  2999. if opcode=A_PUSH then
  3000. bytes:=bytes or (1 shl 21);
  3001. bytes:=bytes or ($D shl 16);
  3002. { reglist }
  3003. bytes:=bytes or MakeRegList(oper[0]^.regset^);
  3004. end;
  3005. { set P bit }
  3006. if (opcode=A_LDM) and (oppostfix in [PF_ED,PF_EA,PF_IB,PF_DB])
  3007. or (opcode=A_STM) and (oppostfix in [PF_FA,PF_FD,PF_IB,PF_DB])
  3008. or (opcode=A_PUSH) then
  3009. bytes:=bytes or (1 shl 24);
  3010. { set U bit }
  3011. if (opcode=A_LDM) and (oppostfix in [PF_None,PF_ED,PF_FD,PF_IB,PF_IA])
  3012. or (opcode=A_STM) and (oppostfix in [PF_None,PF_FA,PF_EA,PF_IB,PF_IA])
  3013. or (opcode=A_POP) then
  3014. bytes:=bytes or (1 shl 23);
  3015. end;
  3016. #$27: // SWP/SWPB
  3017. begin
  3018. { set instruction code }
  3019. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  3020. bytes:=bytes or (ord(insentry^.code[2]) shl 4);
  3021. { set regs }
  3022. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3023. bytes:=bytes or getsupreg(oper[1]^.reg);
  3024. if ops=3 then
  3025. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  3026. end;
  3027. #$28: // BX/BLX
  3028. begin
  3029. { set instruction code }
  3030. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3031. { set offset }
  3032. if oper[0]^.typ=top_const then
  3033. bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
  3034. else
  3035. begin
  3036. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  3037. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  3038. begin
  3039. bytes:=bytes or $fffffe; // TODO: Not sure this is right, but it matches the output of gas
  3040. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24_THUMB);
  3041. end
  3042. else
  3043. begin
  3044. offset:=((currsym.offset-insoffset-8) and $3fffffe);
  3045. bytes:=bytes or ((offset shr 2) and $ffffff);
  3046. bytes:=bytes or ((offset shr 1) and $1) shl 24;
  3047. end;
  3048. end;
  3049. end;
  3050. #$29: // SUB
  3051. begin
  3052. { set instruction code }
  3053. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3054. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3055. { set regs }
  3056. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3057. { set S if necessary }
  3058. if oppostfix=PF_S then
  3059. bytes:=bytes or (1 shl 20);
  3060. end;
  3061. #$2A:
  3062. begin
  3063. { set instruction code }
  3064. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3065. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3066. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3067. bytes:=bytes or ord(insentry^.code[4]);
  3068. { set opers }
  3069. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3070. if opcode in [A_SSAT, A_SSAT16] then
  3071. bytes:=bytes or (((oper[1]^.val-1) and $1F) shl 16)
  3072. else
  3073. bytes:=bytes or ((oper[1]^.val and $1F) shl 16);
  3074. bytes:=bytes or getsupreg(oper[2]^.reg);
  3075. if (ops>3) and
  3076. (oper[3]^.typ=top_shifterop) and
  3077. (oper[3]^.shifterop^.rs=NR_NO) then
  3078. begin
  3079. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
  3080. if oper[3]^.shifterop^.shiftmode=SM_ASR then
  3081. bytes:=bytes or (1 shl 6)
  3082. else if oper[3]^.shifterop^.shiftmode<>SM_LSL then
  3083. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  3084. end;
  3085. end;
  3086. #$2B: // SETEND
  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. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3092. bytes:=bytes or ord(insentry^.code[4]);
  3093. { set endian specifier }
  3094. bytes:=bytes or ((oper[0]^.val and 1) shl 9);
  3095. end;
  3096. #$2C: // MOVW
  3097. begin
  3098. { set instruction code }
  3099. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3100. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3101. { set destination }
  3102. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3103. { set imm }
  3104. bytes:=bytes or (oper[1]^.val and $FFF);
  3105. bytes:=bytes or ((oper[1]^.val and $F000) shl 4);
  3106. end;
  3107. #$2D: // BFX
  3108. begin
  3109. { set instruction code }
  3110. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3111. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3112. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3113. bytes:=bytes or ord(insentry^.code[4]);
  3114. if ops=3 then
  3115. begin
  3116. msb:=(oper[1]^.val+oper[2]^.val-1);
  3117. { set destination }
  3118. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3119. { set immediates }
  3120. bytes:=bytes or ((oper[1]^.val and $1F) shl 7);
  3121. bytes:=bytes or ((msb and $1F) shl 16);
  3122. end
  3123. else
  3124. begin
  3125. if opcode in [A_BFC,A_BFI] then
  3126. msb:=(oper[2]^.val+oper[3]^.val-1)
  3127. else
  3128. msb:=oper[3]^.val-1;
  3129. { set destination }
  3130. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3131. bytes:=bytes or getsupreg(oper[1]^.reg);
  3132. { set immediates }
  3133. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  3134. bytes:=bytes or ((msb and $1F) shl 16);
  3135. end;
  3136. end;
  3137. #$2E: // Cache stuff
  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. { set code }
  3145. bytes:=bytes or (oper[0]^.val and $F);
  3146. end;
  3147. #$2F: // Nop
  3148. begin
  3149. { set instruction code }
  3150. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3151. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3152. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3153. bytes:=bytes or ord(insentry^.code[4]);
  3154. end;
  3155. #$30: // Shifts
  3156. begin
  3157. { set instruction code }
  3158. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3159. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3160. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3161. bytes:=bytes or ord(insentry^.code[4]);
  3162. { set destination }
  3163. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3164. bytes:=bytes or getsupreg(oper[1]^.reg);
  3165. if ops>2 then
  3166. begin
  3167. { set shift }
  3168. if oper[2]^.typ=top_reg then
  3169. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 8)
  3170. else
  3171. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  3172. end;
  3173. { set S if necessary }
  3174. if oppostfix=PF_S then
  3175. bytes:=bytes or (1 shl 20);
  3176. end;
  3177. #$31: // BKPT
  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 0);
  3183. { set imm }
  3184. bytes:=bytes or (oper[0]^.val and $FFF0) shl 4;
  3185. bytes:=bytes or (oper[0]^.val and $F);
  3186. end;
  3187. #$32: // CLZ/REV
  3188. begin
  3189. { set instruction code }
  3190. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3191. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3192. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3193. bytes:=bytes or ord(insentry^.code[4]);
  3194. { set regs }
  3195. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3196. bytes:=bytes or getsupreg(oper[1]^.reg);
  3197. end;
  3198. #$33:
  3199. begin
  3200. { set instruction code }
  3201. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3202. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3203. { set regs }
  3204. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3205. if oper[1]^.typ=top_ref then
  3206. begin
  3207. { set offset }
  3208. offset:=0;
  3209. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3210. if assigned(currsym) then
  3211. offset:=currsym.offset-insoffset-8;
  3212. offset:=offset+oper[1]^.ref^.offset;
  3213. if offset>=0 then
  3214. begin
  3215. { set U flag }
  3216. bytes:=bytes or (1 shl 23);
  3217. bytes:=bytes or offset
  3218. end
  3219. else
  3220. begin
  3221. bytes:=bytes or (1 shl 22);
  3222. offset:=-offset;
  3223. bytes:=bytes or offset
  3224. end;
  3225. end
  3226. else
  3227. begin
  3228. if is_shifter_const(oper[1]^.val,r) then
  3229. begin
  3230. setshifterop(1);
  3231. bytes:=bytes or (1 shl 23);
  3232. end
  3233. else
  3234. begin
  3235. bytes:=bytes or (1 shl 22);
  3236. oper[1]^.val:=-oper[1]^.val;
  3237. setshifterop(1);
  3238. end;
  3239. end;
  3240. end;
  3241. #$40: // VMOV
  3242. begin
  3243. { set instruction code }
  3244. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3245. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3246. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3247. bytes:=bytes or ord(insentry^.code[4]);
  3248. { set regs }
  3249. Rd:=0;
  3250. Rn:=0;
  3251. Rm:=0;
  3252. case oppostfix of
  3253. PF_None:
  3254. begin
  3255. if ops=4 then
  3256. begin
  3257. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3258. (getregtype(oper[2]^.reg)=R_INTREGISTER) then
  3259. begin
  3260. Rd:=getmmreg(oper[0]^.reg);
  3261. Rm:=getsupreg(oper[2]^.reg);
  3262. Rn:=getsupreg(oper[3]^.reg);
  3263. end
  3264. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3265. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  3266. begin
  3267. Rm:=getsupreg(oper[0]^.reg);
  3268. Rn:=getsupreg(oper[1]^.reg);
  3269. Rd:=getmmreg(oper[2]^.reg);
  3270. end
  3271. else
  3272. message(asmw_e_invalid_opcode_and_operands);
  3273. bytes:=bytes or (((Rd and $1E) shr 1) shl 0);
  3274. bytes:=bytes or ((Rd and $1) shl 5);
  3275. bytes:=bytes or (Rm shl 12);
  3276. bytes:=bytes or (Rn shl 16);
  3277. end
  3278. else if ops=3 then
  3279. begin
  3280. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3281. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  3282. begin
  3283. Rd:=getmmreg(oper[0]^.reg);
  3284. Rm:=getsupreg(oper[1]^.reg);
  3285. Rn:=getsupreg(oper[2]^.reg);
  3286. end
  3287. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3288. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  3289. begin
  3290. Rm:=getsupreg(oper[0]^.reg);
  3291. Rn:=getsupreg(oper[1]^.reg);
  3292. Rd:=getmmreg(oper[2]^.reg);
  3293. end
  3294. else
  3295. message(asmw_e_invalid_opcode_and_operands);
  3296. bytes:=bytes or ((Rd and $F) shl 0);
  3297. bytes:=bytes or ((Rd and $10) shl 1);
  3298. bytes:=bytes or (Rm shl 12);
  3299. bytes:=bytes or (Rn shl 16);
  3300. end
  3301. else if ops=2 then
  3302. begin
  3303. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3304. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  3305. begin
  3306. Rd:=getmmreg(oper[0]^.reg);
  3307. Rm:=getsupreg(oper[1]^.reg);
  3308. end
  3309. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3310. (getregtype(oper[1]^.reg)=R_MMREGISTER) then
  3311. begin
  3312. Rm:=getsupreg(oper[0]^.reg);
  3313. Rd:=getmmreg(oper[1]^.reg);
  3314. end
  3315. else
  3316. message(asmw_e_invalid_opcode_and_operands);
  3317. bytes:=bytes or (((Rd and $1E) shr 1) shl 16);
  3318. bytes:=bytes or ((Rd and $1) shl 7);
  3319. bytes:=bytes or (Rm shl 12);
  3320. end;
  3321. end;
  3322. PF_F32:
  3323. begin
  3324. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) or
  3325. (getregtype(oper[1]^.reg)<>R_MMREGISTER) then
  3326. Message(asmw_e_invalid_opcode_and_operands);
  3327. Rd:=getmmreg(oper[0]^.reg);
  3328. Rm:=getmmreg(oper[1]^.reg);
  3329. bytes:=bytes or (((Rd and $1E) shr 1) shl 12);
  3330. bytes:=bytes or ((Rd and $1) shl 22);
  3331. bytes:=bytes or (((Rm and $1E) shr 1) shl 0);
  3332. bytes:=bytes or ((Rm and $1) shl 5);
  3333. end;
  3334. PF_F64:
  3335. begin
  3336. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) or
  3337. (getregtype(oper[1]^.reg)<>R_MMREGISTER) then
  3338. Message(asmw_e_invalid_opcode_and_operands);
  3339. Rd:=getmmreg(oper[0]^.reg);
  3340. Rm:=getmmreg(oper[1]^.reg);
  3341. bytes:=bytes or (1 shl 8);
  3342. bytes:=bytes or ((Rd and $F) shl 12);
  3343. bytes:=bytes or (((Rd and $10) shr 4) shl 22);
  3344. bytes:=bytes or (Rm and $F);
  3345. bytes:=bytes or ((Rm and $10) shl 1);
  3346. end;
  3347. end;
  3348. end;
  3349. #$41: // VMRS/VMSR
  3350. begin
  3351. { set instruction code }
  3352. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3353. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3354. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3355. bytes:=bytes or ord(insentry^.code[4]);
  3356. { set regs }
  3357. if opcode=A_VMRS then
  3358. begin
  3359. case oper[1]^.reg of
  3360. NR_FPSID: Rn:=$0;
  3361. NR_FPSCR: Rn:=$1;
  3362. NR_MVFR1: Rn:=$6;
  3363. NR_MVFR0: Rn:=$7;
  3364. NR_FPEXC: Rn:=$8;
  3365. else
  3366. Rn:=0;
  3367. message(asmw_e_invalid_opcode_and_operands);
  3368. end;
  3369. bytes:=bytes or (Rn shl 16);
  3370. if oper[0]^.reg=NR_APSR_nzcv then
  3371. bytes:=bytes or ($F shl 12)
  3372. else
  3373. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3374. end
  3375. else
  3376. begin
  3377. case oper[0]^.reg of
  3378. NR_FPSID: Rn:=$0;
  3379. NR_FPSCR: Rn:=$1;
  3380. NR_FPEXC: Rn:=$8;
  3381. else
  3382. Rn:=0;
  3383. message(asmw_e_invalid_opcode_and_operands);
  3384. end;
  3385. bytes:=bytes or (Rn shl 16);
  3386. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 12);
  3387. end;
  3388. end;
  3389. #$42: // VMUL
  3390. begin
  3391. { set instruction code }
  3392. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3393. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3394. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3395. bytes:=bytes or ord(insentry^.code[4]);
  3396. { set regs }
  3397. if ops=3 then
  3398. begin
  3399. Rd:=getmmreg(oper[0]^.reg);
  3400. Rn:=getmmreg(oper[1]^.reg);
  3401. Rm:=getmmreg(oper[2]^.reg);
  3402. end
  3403. else if oper[1]^.typ=top_const then
  3404. begin
  3405. Rd:=getmmreg(oper[0]^.reg);
  3406. Rn:=0;
  3407. Rm:=0;
  3408. end
  3409. else
  3410. begin
  3411. Rd:=getmmreg(oper[0]^.reg);
  3412. Rn:=0;
  3413. Rm:=getmmreg(oper[1]^.reg);
  3414. end;
  3415. if oppostfix=PF_F32 then
  3416. begin
  3417. D:=rd and $1; Rd:=Rd shr 1;
  3418. N:=rn and $1; Rn:=Rn shr 1;
  3419. M:=rm and $1; Rm:=Rm shr 1;
  3420. end
  3421. else
  3422. begin
  3423. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3424. N:=(rn shr 4) and $1; Rn:=Rn and $F;
  3425. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3426. bytes:=bytes or (1 shl 8);
  3427. end;
  3428. bytes:=bytes or (Rd shl 12);
  3429. bytes:=bytes or (Rn shl 16);
  3430. bytes:=bytes or (Rm shl 0);
  3431. bytes:=bytes or (D shl 22);
  3432. bytes:=bytes or (N shl 7);
  3433. bytes:=bytes or (M shl 5);
  3434. end;
  3435. #$43: // VCVT
  3436. begin
  3437. { set instruction code }
  3438. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3439. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3440. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3441. bytes:=bytes or ord(insentry^.code[4]);
  3442. { set regs }
  3443. Rd:=getmmreg(oper[0]^.reg);
  3444. Rm:=getmmreg(oper[1]^.reg);
  3445. if (ops=2) and
  3446. (oppostfix in [PF_F32F64,PF_F64F32]) then
  3447. begin
  3448. if oppostfix=PF_F32F64 then
  3449. begin
  3450. bytes:=bytes or (1 shl 8);
  3451. D:=rd and $1; Rd:=Rd shr 1;
  3452. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3453. end
  3454. else
  3455. begin
  3456. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3457. M:=rm and $1; Rm:=Rm shr 1;
  3458. end;
  3459. bytes:=bytes and $FFF0FFFF;
  3460. bytes:=bytes or ($7 shl 16);
  3461. bytes:=bytes or (Rd shl 12);
  3462. bytes:=bytes or (Rm shl 0);
  3463. bytes:=bytes or (D shl 22);
  3464. bytes:=bytes or (M shl 5);
  3465. end
  3466. else if ops=2 then
  3467. begin
  3468. case oppostfix of
  3469. PF_S32F64,
  3470. PF_U32F64,
  3471. PF_F64S32,
  3472. PF_F64U32:
  3473. bytes:=bytes or (1 shl 8);
  3474. end;
  3475. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64] then
  3476. begin
  3477. case oppostfix of
  3478. PF_S32F64,
  3479. PF_S32F32:
  3480. bytes:=bytes or (1 shl 16);
  3481. end;
  3482. bytes:=bytes or (1 shl 18);
  3483. D:=rd and $1; Rd:=Rd shr 1;
  3484. if oppostfix in [PF_S32F64,PF_U32F64] then
  3485. begin
  3486. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3487. end
  3488. else
  3489. begin
  3490. M:=rm and $1; Rm:=Rm shr 1;
  3491. end;
  3492. end
  3493. else
  3494. begin
  3495. case oppostfix of
  3496. PF_F64S32,
  3497. PF_F32S32:
  3498. bytes:=bytes or (1 shl 7);
  3499. else
  3500. bytes:=bytes and $FFFFFF7F;
  3501. end;
  3502. M:=rm and $1; Rm:=Rm shr 1;
  3503. if oppostfix in [PF_F64S32,PF_F64U32] then
  3504. begin
  3505. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3506. end
  3507. else
  3508. begin
  3509. D:=rd and $1; Rd:=Rd shr 1;
  3510. end
  3511. end;
  3512. bytes:=bytes or (Rd shl 12);
  3513. bytes:=bytes or (Rm shl 0);
  3514. bytes:=bytes or (D shl 22);
  3515. bytes:=bytes or (M shl 5);
  3516. end
  3517. else
  3518. begin
  3519. if rd<>rm then
  3520. message(asmw_e_invalid_opcode_and_operands);
  3521. case oppostfix of
  3522. PF_S32F32,PF_U32F32,
  3523. PF_F32S32,PF_F32U32,
  3524. PF_S32F64,PF_U32F64,
  3525. PF_F64S32,PF_F64U32:
  3526. begin
  3527. if not (oper[2]^.val in [1..32]) then
  3528. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 1-32');
  3529. bytes:=bytes or (1 shl 7);
  3530. rn:=32;
  3531. end;
  3532. PF_S16F64,PF_U16F64,
  3533. PF_F64S16,PF_F64U16,
  3534. PF_S16F32,PF_U16F32,
  3535. PF_F32S16,PF_F32U16:
  3536. begin
  3537. if not (oper[2]^.val in [0..16]) then
  3538. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 0-16');
  3539. rn:=16;
  3540. end;
  3541. else
  3542. Rn:=0;
  3543. message(asmw_e_invalid_opcode_and_operands);
  3544. end;
  3545. case oppostfix of
  3546. PF_S16F64,PF_U16F64,
  3547. PF_S32F64,PF_U32F64,
  3548. PF_F64S16,PF_F64U16,
  3549. PF_F64S32,PF_F64U32:
  3550. begin
  3551. bytes:=bytes or (1 shl 8);
  3552. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3553. end;
  3554. else
  3555. begin
  3556. D:=rd and $1; Rd:=Rd shr 1;
  3557. end;
  3558. end;
  3559. case oppostfix of
  3560. PF_U16F64,PF_U16F32,
  3561. PF_U32F32,PF_U32F64,
  3562. PF_F64U16,PF_F32U16,
  3563. PF_F32U32,PF_F64U32:
  3564. bytes:=bytes or (1 shl 16);
  3565. end;
  3566. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64,PF_S16F32,PF_S16F64,PF_U16F32,PF_U16F64] then
  3567. bytes:=bytes or (1 shl 18);
  3568. bytes:=bytes or (Rd shl 12);
  3569. bytes:=bytes or (D shl 22);
  3570. rn:=rn-oper[2]^.val;
  3571. bytes:=bytes or ((rn and $1) shl 5);
  3572. bytes:=bytes or ((rn and $1E) shr 1);
  3573. end;
  3574. end;
  3575. #$44: // VLDM/VSTM/VPUSH/VPOP
  3576. begin
  3577. { set instruction code }
  3578. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3579. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3580. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3581. { set regs }
  3582. if ops=2 then
  3583. begin
  3584. if oper[0]^.typ=top_ref then
  3585. begin
  3586. Rn:=getsupreg(oper[0]^.ref^.index);
  3587. if oper[0]^.ref^.addressmode<>AM_OFFSET then
  3588. begin
  3589. { set W }
  3590. bytes:=bytes or (1 shl 21);
  3591. end
  3592. else if oppostfix = PF_DB then
  3593. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  3594. end
  3595. else
  3596. begin
  3597. Rn:=getsupreg(oper[0]^.reg);
  3598. if oppostfix = PF_DB then
  3599. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  3600. end;
  3601. bytes:=bytes or (Rn shl 16);
  3602. { Set PU bits }
  3603. case oppostfix of
  3604. PF_None,
  3605. PF_IA:
  3606. bytes:=bytes or (1 shl 23);
  3607. PF_DB:
  3608. bytes:=bytes or (2 shl 23);
  3609. end;
  3610. dp_operation:=(oper[1]^.subreg=R_SUBFD);
  3611. if oper[1]^.regset^=[] then
  3612. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  3613. rd:=0;
  3614. for r:=0 to 31 do
  3615. if r in oper[1]^.regset^ then
  3616. begin
  3617. rd:=r;
  3618. break;
  3619. end;
  3620. rn:=32-rd;
  3621. for r:=rd+1 to 31 do
  3622. if not(r in oper[1]^.regset^) then
  3623. begin
  3624. rn:=r-rd;
  3625. break;
  3626. end;
  3627. if dp_operation then
  3628. begin
  3629. bytes:=bytes or (1 shl 8);
  3630. bytes:=bytes or (rn*2);
  3631. bytes:=bytes or ((rd and $F) shl 12);
  3632. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3633. end
  3634. else
  3635. begin
  3636. bytes:=bytes or rn;
  3637. bytes:=bytes or ((rd and $1) shl 22);
  3638. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3639. end;
  3640. end
  3641. else { VPUSH/VPOP }
  3642. begin
  3643. dp_operation:=(oper[0]^.subreg=R_SUBFD);
  3644. if oper[0]^.regset^=[] then
  3645. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  3646. rd:=0;
  3647. for r:=0 to 31 do
  3648. if r in oper[0]^.regset^ then
  3649. begin
  3650. rd:=r;
  3651. break;
  3652. end;
  3653. rn:=32-rd;
  3654. for r:=rd+1 to 31 do
  3655. if not(r in oper[0]^.regset^) then
  3656. begin
  3657. rn:=r-rd;
  3658. break;
  3659. end;
  3660. if dp_operation then
  3661. begin
  3662. bytes:=bytes or (1 shl 8);
  3663. bytes:=bytes or (rn*2);
  3664. bytes:=bytes or ((rd and $F) shl 12);
  3665. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3666. end
  3667. else
  3668. begin
  3669. bytes:=bytes or rn;
  3670. bytes:=bytes or ((rd and $1) shl 22);
  3671. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3672. end;
  3673. end;
  3674. end;
  3675. #$45: // VLDR/VSTR
  3676. begin
  3677. { set instruction code }
  3678. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3679. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3680. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3681. { set regs }
  3682. rd:=getmmreg(oper[0]^.reg);
  3683. if getsubreg(oper[0]^.reg)=R_SUBFD then
  3684. begin
  3685. bytes:=bytes or (1 shl 8);
  3686. bytes:=bytes or ((rd and $F) shl 12);
  3687. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3688. end
  3689. else
  3690. begin
  3691. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3692. bytes:=bytes or ((rd and $1) shl 22);
  3693. end;
  3694. { set ref }
  3695. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  3696. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  3697. begin
  3698. { set offset }
  3699. offset:=0;
  3700. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3701. if assigned(currsym) then
  3702. offset:=currsym.offset-insoffset-8;
  3703. offset:=offset+oper[1]^.ref^.offset;
  3704. offset:=offset div 4;
  3705. if offset>=0 then
  3706. begin
  3707. { set U flag }
  3708. bytes:=bytes or (1 shl 23);
  3709. bytes:=bytes or offset
  3710. end
  3711. else
  3712. begin
  3713. offset:=-offset;
  3714. bytes:=bytes or offset
  3715. end;
  3716. end
  3717. else
  3718. message(asmw_e_invalid_opcode_and_operands);
  3719. end;
  3720. #$60..#$61: { Thumb }
  3721. begin
  3722. bytelen:=2;
  3723. bytes:=0;
  3724. { set opcode }
  3725. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3726. bytes:=bytes or ord(insentry^.code[2]);
  3727. { set regs }
  3728. if ops>=2 then
  3729. begin
  3730. if oper[1]^.typ=top_reg then
  3731. begin
  3732. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7) or ((getsupreg(oper[0]^.reg) shr 3) shl 7);
  3733. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  3734. if ops=3 then
  3735. begin
  3736. case oper[2]^.typ of
  3737. top_const:
  3738. bytes:=bytes or ((oper[2]^.val and $1F) shl 6);
  3739. top_reg:
  3740. bytes:=bytes or ((getsupreg(oper[2]^.reg) and $7) shl 6);
  3741. end;
  3742. end;
  3743. end
  3744. else if oper[1]^.typ=top_const then
  3745. begin
  3746. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $7) shl 8);
  3747. bytes:=bytes or (oper[1]^.val and $FF);
  3748. end;
  3749. end
  3750. else if ops=1 then
  3751. begin
  3752. if oper[0]^.typ=top_const then
  3753. bytes:=bytes or (oper[0]^.val and $FF);
  3754. end;
  3755. end;
  3756. #$62..#$63: { Thumb branches }
  3757. begin
  3758. bytelen:=2;
  3759. bytes:=0;
  3760. { set opcode }
  3761. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3762. bytes:=bytes or ord(insentry^.code[2]);
  3763. if insentry^.code[0]=#$63 then
  3764. bytes:=bytes or (CondVal[condition] shl 8);
  3765. if oper[0]^.typ=top_const then
  3766. begin
  3767. if insentry^.code[0]=#$63 then
  3768. bytes:=bytes or (((oper[0]^.val shr 1)-1) and $FF)
  3769. else
  3770. bytes:=bytes or (((oper[0]^.val shr 1)-1) and $3FF);
  3771. end
  3772. else if oper[0]^.typ=top_reg then
  3773. begin
  3774. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 3);
  3775. end
  3776. else if oper[0]^.typ=top_ref then
  3777. begin
  3778. offset:=0;
  3779. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  3780. if assigned(currsym) then
  3781. offset:=currsym.offset-insoffset-8;
  3782. offset:=offset+oper[0]^.ref^.offset;
  3783. if insentry^.code[0]=#$63 then
  3784. bytes:=bytes or (((offset+4) shr 1) and $FF)
  3785. else
  3786. bytes:=bytes or (((offset+4) shr 1) and $7FF);
  3787. end
  3788. end;
  3789. #$64: { Thumb: Special encodings }
  3790. begin
  3791. bytelen:=2;
  3792. bytes:=0;
  3793. { set opcode }
  3794. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3795. bytes:=bytes or ord(insentry^.code[2]);
  3796. case opcode of
  3797. A_SUB:
  3798. if(ops=3) then
  3799. bytes:=bytes or ((oper[2]^.val shr 2) and $7F);
  3800. A_MUL:
  3801. if (ops=3) then
  3802. begin
  3803. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3804. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  3805. end;
  3806. A_ADD:
  3807. begin
  3808. if ops=2 then
  3809. begin
  3810. bytes:=bytes or (getsupreg(oper[1]^.reg) shl $3);
  3811. end
  3812. else if (oper[0]^.reg<>NR_STACK_POINTER_REG) and
  3813. (oper[2]^.typ=top_const) then
  3814. begin
  3815. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7) shl 8;
  3816. bytes:=bytes or ((oper[2]^.val shr 2) and $7F);
  3817. end
  3818. else if (oper[0]^.reg<>NR_STACK_POINTER_REG) and
  3819. (oper[2]^.typ=top_reg) then
  3820. begin
  3821. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3822. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $8) shr 3) shl 7;
  3823. end
  3824. else
  3825. bytes:=bytes or ((oper[2]^.val shr 2) and $7F);
  3826. end;
  3827. end;
  3828. end;
  3829. #$65: { Thumb load/store }
  3830. begin
  3831. bytelen:=2;
  3832. bytes:=0;
  3833. { set opcode }
  3834. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3835. bytes:=bytes or ord(insentry^.code[2]);
  3836. { set regs }
  3837. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3838. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 3);
  3839. bytes:=bytes or (getsupreg(oper[1]^.ref^.index) shl 6);
  3840. end;
  3841. #$66: { Thumb load/store }
  3842. begin
  3843. bytelen:=2;
  3844. bytes:=0;
  3845. { set opcode }
  3846. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3847. bytes:=bytes or ord(insentry^.code[2]);
  3848. { set regs }
  3849. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3850. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 3);
  3851. bytes:=bytes or (((oper[1]^.ref^.offset shr ord(insentry^.code[3])) and $1F) shl 6);
  3852. end;
  3853. #$67: { Thumb load/store }
  3854. begin
  3855. bytelen:=2;
  3856. bytes:=0;
  3857. { set opcode }
  3858. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3859. bytes:=bytes or ord(insentry^.code[2]);
  3860. { set regs }
  3861. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  3862. if oper[1]^.typ=top_ref then
  3863. bytes:=bytes or ((oper[1]^.ref^.offset shr ord(insentry^.code[3])) and $FF)
  3864. else
  3865. bytes:=bytes or ((oper[1]^.val shr ord(insentry^.code[3])) and $FF);
  3866. end;
  3867. #$68: { Thumb CB[N]Z }
  3868. begin
  3869. bytelen:=2;
  3870. bytes:=0;
  3871. { set opcode }
  3872. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3873. { set opers }
  3874. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3875. if oper[1]^.typ=top_ref then
  3876. begin
  3877. offset:=0;
  3878. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3879. if assigned(currsym) then
  3880. offset:=currsym.offset-insoffset-8;
  3881. offset:=offset+oper[1]^.ref^.offset;
  3882. offset:=offset div 2;
  3883. end
  3884. else
  3885. offset:=oper[1]^.val div 2;
  3886. bytes:=bytes or ((offset) and $1F) shl 3;
  3887. bytes:=bytes or ((offset shr 5) and 1) shl 9;
  3888. end;
  3889. #$69: { Thumb: Push/Pop/Stm/Ldm }
  3890. begin
  3891. bytelen:=2;
  3892. bytes:=0;
  3893. { set opcode }
  3894. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3895. case opcode of
  3896. A_PUSH:
  3897. begin
  3898. for r:=0 to 7 do
  3899. if r in oper[0]^.regset^ then
  3900. bytes:=bytes or (1 shl r);
  3901. if RS_R14 in oper[0]^.regset^ then
  3902. bytes:=bytes or (1 shl 8);
  3903. end;
  3904. A_POP:
  3905. begin
  3906. for r:=0 to 7 do
  3907. if r in oper[0]^.regset^ then
  3908. bytes:=bytes or (1 shl r);
  3909. if RS_R15 in oper[0]^.regset^ then
  3910. bytes:=bytes or (1 shl 8);
  3911. end;
  3912. A_STM:
  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. A_LDM:
  3923. begin
  3924. for r:=0 to 7 do
  3925. if r in oper[1]^.regset^ then
  3926. bytes:=bytes or (1 shl r);
  3927. if oper[0]^.typ=top_ref then
  3928. bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 8)
  3929. else
  3930. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  3931. end;
  3932. end;
  3933. end;
  3934. #$6A: { Thumb: IT }
  3935. begin
  3936. bytelen:=2;
  3937. bytes:=0;
  3938. { set opcode }
  3939. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3940. bytes:=bytes or (ord(insentry^.code[2]) shl 0);
  3941. bytes:=bytes or (CondVal[oper[0]^.cc] shl 4);
  3942. i_field:=(bytes shr 4) and 1;
  3943. i_field:=(i_field shl 1) or i_field;
  3944. i_field:=(i_field shl 2) or i_field;
  3945. bytes:=bytes or ((i_field and ord(insentry^.code[3])) xor (ord(insentry^.code[3]) shr 4));
  3946. end;
  3947. #$80: { Thumb-2: Dataprocessing }
  3948. begin
  3949. bytes:=0;
  3950. { set instruction code }
  3951. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3952. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3953. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3954. bytes:=bytes or ord(insentry^.code[4]);
  3955. if ops=1 then
  3956. begin
  3957. if oper[0]^.typ=top_reg then
  3958. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16)
  3959. else if oper[0]^.typ=top_const then
  3960. bytes:=bytes or (oper[0]^.val and $F);
  3961. end
  3962. else if (ops=2) and
  3963. (opcode in [A_CMP,A_CMN,A_TEQ,A_TST]) then
  3964. begin
  3965. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  3966. if oper[1]^.typ=top_const then
  3967. encodethumbimm(oper[1]^.val)
  3968. else if oper[1]^.typ=top_reg then
  3969. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  3970. end
  3971. else if (ops=3) and
  3972. (opcode in [A_CMP,A_CMN,A_TEQ,A_TST]) then
  3973. begin
  3974. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  3975. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  3976. if oper[2]^.typ=top_shifterop then
  3977. setthumbshift(2)
  3978. else if oper[2]^.typ=top_reg then
  3979. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 12);
  3980. end
  3981. else if (ops=2) and
  3982. (opcode in [A_REV,A_RBIT,A_REV16,A_REVSH,A_CLZ]) then
  3983. begin
  3984. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  3985. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  3986. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  3987. end
  3988. else if ops=2 then
  3989. begin
  3990. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  3991. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  3992. if oper[1]^.typ=top_const then
  3993. encodethumbimm(oper[1]^.val)
  3994. else if oper[1]^.typ=top_reg then
  3995. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  3996. end
  3997. else if ops=3 then
  3998. begin
  3999. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4000. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4001. if oper[2]^.typ=top_const then
  4002. encodethumbimm(oper[2]^.val)
  4003. else if oper[2]^.typ=top_reg then
  4004. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4005. end
  4006. else if ops=4 then
  4007. begin
  4008. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4009. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4010. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4011. if oper[3]^.typ=top_shifterop then
  4012. setthumbshift(3)
  4013. else if oper[3]^.typ=top_reg then
  4014. bytes:=bytes or (getsupreg(oper[3]^.reg) shl 12);
  4015. end;
  4016. if oppostfix=PF_S then
  4017. bytes:=bytes or (1 shl 20)
  4018. else if oppostfix=PF_X then
  4019. bytes:=bytes or (1 shl 4)
  4020. else if oppostfix=PF_R then
  4021. bytes:=bytes or (1 shl 4);
  4022. end;
  4023. #$81: { Thumb-2: Dataprocessing misc }
  4024. begin
  4025. bytes:=0;
  4026. { set instruction code }
  4027. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4028. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4029. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4030. bytes:=bytes or ord(insentry^.code[4]);
  4031. if ops=3 then
  4032. begin
  4033. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4034. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4035. if oper[2]^.typ=top_const then
  4036. begin
  4037. bytes:=bytes or (oper[2]^.val and $FF);
  4038. bytes:=bytes or ((oper[2]^.val and $700) shr 8) shl 12;
  4039. bytes:=bytes or ((oper[2]^.val and $800) shr 11) shl 26;
  4040. end;
  4041. end
  4042. else if ops=2 then
  4043. begin
  4044. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4045. if oper[1]^.typ=top_const then
  4046. begin
  4047. offset:=oper[1]^.val;
  4048. end
  4049. else if oper[1]^.typ=top_ref then
  4050. begin
  4051. offset:=0;
  4052. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4053. if assigned(currsym) then
  4054. offset:=currsym.offset-insoffset-8;
  4055. offset:=offset+oper[1]^.ref^.offset;
  4056. offset:=offset;
  4057. end;
  4058. bytes:=bytes or (offset and $FF);
  4059. bytes:=bytes or ((offset and $700) shr 8) shl 12;
  4060. bytes:=bytes or ((offset and $800) shr 11) shl 26;
  4061. bytes:=bytes or ((offset and $F000) shr 12) shl 16;
  4062. end;
  4063. if oppostfix=PF_S then
  4064. bytes:=bytes or (1 shl 20);
  4065. end;
  4066. #$82: { Thumb-2: Shifts }
  4067. begin
  4068. bytes:=0;
  4069. { set instruction code }
  4070. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4071. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4072. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4073. bytes:=bytes or ord(insentry^.code[4]);
  4074. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4075. if oper[1]^.typ=top_reg then
  4076. begin
  4077. offset:=2;
  4078. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4079. end
  4080. else
  4081. begin
  4082. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 0);
  4083. offset:=1;
  4084. end;
  4085. if oper[offset]^.typ=top_const then
  4086. begin
  4087. bytes:=bytes or (oper[offset]^.val and $3) shl 6;
  4088. bytes:=bytes or (oper[offset]^.val and $1C) shl 10;
  4089. end
  4090. else if oper[offset]^.typ=top_reg then
  4091. bytes:=bytes or (getsupreg(oper[offset]^.reg) shl 16);
  4092. if (ops>=(offset+2)) and
  4093. (oper[offset+1]^.typ=top_const) then
  4094. bytes:=bytes or (oper[offset+1]^.val and $1F);
  4095. if oppostfix=PF_S then
  4096. bytes:=bytes or (1 shl 20);
  4097. end;
  4098. #$84: { Thumb-2: Shifts(width-1) }
  4099. begin
  4100. bytes:=0;
  4101. { set instruction code }
  4102. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4103. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4104. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4105. bytes:=bytes or ord(insentry^.code[4]);
  4106. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4107. if oper[1]^.typ=top_reg then
  4108. begin
  4109. offset:=2;
  4110. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4111. end
  4112. else
  4113. offset:=1;
  4114. if oper[offset]^.typ=top_const then
  4115. begin
  4116. bytes:=bytes or (oper[offset]^.val and $3) shl 6;
  4117. bytes:=bytes or (oper[offset]^.val and $1C) shl 10;
  4118. end;
  4119. if (ops>=(offset+2)) and
  4120. (oper[offset+1]^.typ=top_const) then
  4121. begin
  4122. if opcode in [A_BFI,A_BFC] then
  4123. i_field:=oper[offset+1]^.val+oper[offset]^.val-1
  4124. else
  4125. i_field:=oper[offset+1]^.val-1;
  4126. bytes:=bytes or (i_field and $1F);
  4127. end;
  4128. if oppostfix=PF_S then
  4129. bytes:=bytes or (1 shl 20);
  4130. end;
  4131. #$83: { Thumb-2: Saturation }
  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. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4140. bytes:=bytes or (oper[1]^.val and $1F);
  4141. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 16);
  4142. if ops=4 then
  4143. setthumbshift(3,true);
  4144. end;
  4145. #$85: { Thumb-2: Long multiplications }
  4146. begin
  4147. bytes:=0;
  4148. { set instruction code }
  4149. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4150. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4151. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4152. bytes:=bytes or ord(insentry^.code[4]);
  4153. if ops=4 then
  4154. begin
  4155. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  4156. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 8);
  4157. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 16);
  4158. bytes:=bytes or (getsupreg(oper[3]^.reg) shl 0);
  4159. end;
  4160. if oppostfix=PF_S then
  4161. bytes:=bytes or (1 shl 20)
  4162. else if oppostfix=PF_X then
  4163. bytes:=bytes or (1 shl 4);
  4164. end;
  4165. #$86: { Thumb-2: Extension ops }
  4166. begin
  4167. bytes:=0;
  4168. { set instruction code }
  4169. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4170. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4171. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4172. bytes:=bytes or ord(insentry^.code[4]);
  4173. if ops=2 then
  4174. begin
  4175. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4176. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4177. end
  4178. else if ops=3 then
  4179. begin
  4180. if oper[2]^.typ=top_shifterop then
  4181. begin
  4182. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4183. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4184. bytes:=bytes or ((oper[2]^.shifterop^.shiftimm shr 3) shl 4);
  4185. end
  4186. else
  4187. begin
  4188. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4189. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4190. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4191. end;
  4192. end
  4193. else if ops=4 then
  4194. begin
  4195. if oper[3]^.typ=top_shifterop then
  4196. begin
  4197. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4198. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4199. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4200. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm shr 3) shl 4);
  4201. end;
  4202. end;
  4203. end;
  4204. #$87: { Thumb-2: PLD/PLI }
  4205. begin
  4206. { set instruction code }
  4207. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4208. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4209. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4210. bytes:=bytes or ord(insentry^.code[4]);
  4211. { set Rn and Rd }
  4212. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  4213. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  4214. begin
  4215. { set offset }
  4216. offset:=0;
  4217. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  4218. if assigned(currsym) then
  4219. offset:=currsym.offset-insoffset-8;
  4220. offset:=offset+oper[0]^.ref^.offset;
  4221. if offset>=0 then
  4222. begin
  4223. { set U flag }
  4224. bytes:=bytes or (1 shl 23);
  4225. bytes:=bytes or (offset and $FFF);
  4226. end
  4227. else
  4228. begin
  4229. bytes:=bytes or ($3 shl 10);
  4230. offset:=-offset;
  4231. bytes:=bytes or (offset and $FF);
  4232. end;
  4233. end
  4234. else
  4235. begin
  4236. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  4237. { set shift }
  4238. with oper[0]^.ref^ do
  4239. if shiftmode=SM_LSL then
  4240. bytes:=bytes or (shiftimm shl 4);
  4241. end;
  4242. end;
  4243. #$88: { Thumb-2: LDR/STR }
  4244. begin
  4245. { set instruction code }
  4246. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4247. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4248. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4249. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4250. { set Rn and Rd }
  4251. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4252. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4253. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  4254. begin
  4255. { set offset }
  4256. offset:=0;
  4257. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4258. if assigned(currsym) then
  4259. offset:=currsym.offset-insoffset-8;
  4260. offset:=(offset+oper[1]^.ref^.offset) shr ord(insentry^.code[5]);
  4261. if offset>=0 then
  4262. begin
  4263. if not (opcode in [A_LDRT,A_LDRSBT,A_LDRSHT,A_LDRBT,A_LDRHT]) then
  4264. bytes:=bytes or (1 shl 23);
  4265. { set U flag }
  4266. if (oper[1]^.ref^.addressmode<>AM_OFFSET) then
  4267. bytes:=bytes or (1 shl 9);
  4268. bytes:=bytes or offset
  4269. end
  4270. else
  4271. begin
  4272. bytes:=bytes or (1 shl 11);
  4273. offset:=-offset;
  4274. bytes:=bytes or offset
  4275. end;
  4276. end
  4277. else
  4278. begin
  4279. { set I flag }
  4280. bytes:=bytes or (1 shl 25);
  4281. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  4282. { set shift }
  4283. with oper[1]^.ref^ do
  4284. if shiftmode<>SM_None then
  4285. bytes:=bytes or (shiftimm shl 4);
  4286. end;
  4287. if not (opcode in [A_LDRT,A_LDRSBT,A_LDRSHT,A_LDRBT,A_LDRHT]) then
  4288. begin
  4289. { set W bit }
  4290. if oper[1]^.ref^.addressmode<>AM_OFFSET then
  4291. bytes:=bytes or (1 shl 8);
  4292. { set P bit if necessary }
  4293. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  4294. bytes:=bytes or (1 shl 10);
  4295. end;
  4296. end;
  4297. #$89: { Thumb-2: LDRD/STRD }
  4298. begin
  4299. { set instruction code }
  4300. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4301. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4302. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4303. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4304. { set Rn and Rd }
  4305. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4306. bytes:=bytes or getsupreg(oper[1]^.reg) shl 8;
  4307. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4308. if getregtype(oper[2]^.ref^.index)=R_INVALIDREGISTER then
  4309. begin
  4310. { set offset }
  4311. offset:=0;
  4312. currsym:=objdata.symbolref(oper[2]^.ref^.symbol);
  4313. if assigned(currsym) then
  4314. offset:=currsym.offset-insoffset-8;
  4315. offset:=(offset+oper[2]^.ref^.offset) div 4;
  4316. if offset>=0 then
  4317. begin
  4318. { set U flag }
  4319. bytes:=bytes or (1 shl 23);
  4320. bytes:=bytes or offset
  4321. end
  4322. else
  4323. begin
  4324. offset:=-offset;
  4325. bytes:=bytes or offset
  4326. end;
  4327. end
  4328. else
  4329. begin
  4330. message(asmw_e_invalid_opcode_and_operands);
  4331. end;
  4332. { set W bit }
  4333. if oper[2]^.ref^.addressmode<>AM_OFFSET then
  4334. bytes:=bytes or (1 shl 21);
  4335. { set P bit if necessary }
  4336. if oper[2]^.ref^.addressmode<>AM_POSTINDEXED then
  4337. bytes:=bytes or (1 shl 24);
  4338. end;
  4339. #$8A: { Thumb-2: LDREX }
  4340. begin
  4341. { set instruction code }
  4342. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4343. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4344. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4345. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4346. { set Rn and Rd }
  4347. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4348. if (ops=2) and (opcode in [A_LDREX]) then
  4349. begin
  4350. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4351. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  4352. begin
  4353. { set offset }
  4354. offset:=0;
  4355. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4356. if assigned(currsym) then
  4357. offset:=currsym.offset-insoffset-8;
  4358. offset:=(offset+oper[1]^.ref^.offset) div 4;
  4359. if offset>=0 then
  4360. begin
  4361. bytes:=bytes or offset
  4362. end
  4363. else
  4364. begin
  4365. message(asmw_e_invalid_opcode_and_operands);
  4366. end;
  4367. end
  4368. else
  4369. begin
  4370. message(asmw_e_invalid_opcode_and_operands);
  4371. end;
  4372. end
  4373. else if (ops=2) then
  4374. begin
  4375. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4376. end
  4377. else
  4378. begin
  4379. bytes:=bytes or getsupreg(oper[1]^.reg) shl 8;
  4380. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4381. end;
  4382. end;
  4383. #$8B: { Thumb-2: STREX }
  4384. begin
  4385. { set instruction code }
  4386. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4387. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4388. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4389. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4390. { set Rn and Rd }
  4391. if (ops=3) and (opcode in [A_STREX]) then
  4392. begin
  4393. bytes:=bytes or getsupreg(oper[0]^.reg) shl 8;
  4394. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4395. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4396. if getregtype(oper[2]^.ref^.index)=R_INVALIDREGISTER then
  4397. begin
  4398. { set offset }
  4399. offset:=0;
  4400. currsym:=objdata.symbolref(oper[2]^.ref^.symbol);
  4401. if assigned(currsym) then
  4402. offset:=currsym.offset-insoffset-8;
  4403. offset:=(offset+oper[2]^.ref^.offset) div 4;
  4404. if offset>=0 then
  4405. begin
  4406. bytes:=bytes or offset
  4407. end
  4408. else
  4409. begin
  4410. message(asmw_e_invalid_opcode_and_operands);
  4411. end;
  4412. end
  4413. else
  4414. begin
  4415. message(asmw_e_invalid_opcode_and_operands);
  4416. end;
  4417. end
  4418. else if (ops=3) then
  4419. begin
  4420. bytes:=bytes or getsupreg(oper[0]^.reg) shl 0;
  4421. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4422. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4423. end
  4424. else
  4425. begin
  4426. bytes:=bytes or getsupreg(oper[0]^.reg) shl 0;
  4427. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4428. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  4429. bytes:=bytes or getsupreg(oper[3]^.ref^.base) shl 16;
  4430. end;
  4431. end;
  4432. #$8C: { Thumb-2: LDM/STM }
  4433. begin
  4434. { set instruction code }
  4435. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4436. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4437. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4438. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4439. if oper[0]^.typ=top_reg then
  4440. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16)
  4441. else
  4442. begin
  4443. bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 16);
  4444. if oper[0]^.ref^.addressmode<>AM_OFFSET then
  4445. bytes:=bytes or (1 shl 21);
  4446. end;
  4447. for r:=0 to 15 do
  4448. if r in oper[1]^.regset^ then
  4449. bytes:=bytes or (1 shl r);
  4450. case oppostfix of
  4451. PF_None,PF_IA,PF_FD: bytes:=bytes or ($1 shl 23);
  4452. PF_DB,PF_EA: bytes:=bytes or ($2 shl 23);
  4453. end;
  4454. end;
  4455. #$8D: { Thumb-2: BL/BLX }
  4456. begin
  4457. { set instruction code }
  4458. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4459. bytes:=bytes or (ord(insentry^.code[2]) shl 8);
  4460. { set offset }
  4461. if oper[0]^.typ=top_const then
  4462. offset:=(oper[0]^.val shr 1) and $FFFFFF
  4463. else
  4464. begin
  4465. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  4466. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  4467. begin
  4468. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24);
  4469. offset:=$FFFFFE
  4470. end
  4471. else
  4472. offset:=((currsym.offset-insoffset-8) shr 1) and $FFFFFF;
  4473. end;
  4474. bytes:=bytes or ((offset shr 00) and $7FF) shl 0;
  4475. bytes:=bytes or ((offset shr 11) and $3FF) shl 16;
  4476. bytes:=bytes or (((offset shr 21) xor (offset shr 23) xor 1) and $1) shl 11;
  4477. bytes:=bytes or (((offset shr 22) xor (offset shr 23) xor 1) and $1) shl 13;
  4478. bytes:=bytes or ((offset shr 23) and $1) shl 26;
  4479. end;
  4480. #$8E: { Thumb-2: TBB/TBH }
  4481. begin
  4482. { set instruction code }
  4483. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4484. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4485. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4486. bytes:=bytes or ord(insentry^.code[4]);
  4487. { set Rn and Rm }
  4488. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  4489. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  4490. message(asmw_e_invalid_effective_address)
  4491. else
  4492. begin
  4493. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  4494. if (opcode=A_TBH) and
  4495. (oper[0]^.ref^.shiftmode<>SM_LSL) and
  4496. (oper[0]^.ref^.shiftimm<>1) then
  4497. message(asmw_e_invalid_effective_address);
  4498. end;
  4499. end;
  4500. #$fe: // No written data
  4501. begin
  4502. exit;
  4503. end;
  4504. #$ff:
  4505. internalerror(2005091101);
  4506. else
  4507. begin
  4508. writeln(ord(insentry^.code[0]), ' - ', opcode);
  4509. internalerror(2005091102);
  4510. end;
  4511. end;
  4512. { Todo: Decide whether the code above should take care of writing data in an order that makes senes }
  4513. if (insentry^.code[0] in [#$80..#$90]) and (bytelen=4) then
  4514. bytes:=((bytes shr 16) and $FFFF) or ((bytes and $FFFF) shl 16);
  4515. { we're finished, write code }
  4516. objdata.writebytes(bytes,bytelen);
  4517. end;
  4518. begin
  4519. cai_align:=tai_align;
  4520. end.