aasmcpu.pas 210 KB

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