aasmcpu.pas 210 KB

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