aasmcpu.pas 202 KB

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