aasmcpu.pas 207 KB

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