ARMDisassembler.cpp 231 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755
  1. //===- ARMDisassembler.cpp - Disassembler for ARM/Thumb ISA ---------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "ARMBaseInstrInfo.h"
  9. #include "MCTargetDesc/ARMAddressingModes.h"
  10. #include "MCTargetDesc/ARMBaseInfo.h"
  11. #include "MCTargetDesc/ARMMCTargetDesc.h"
  12. #include "TargetInfo/ARMTargetInfo.h"
  13. #include "Utils/ARMBaseInfo.h"
  14. #include "llvm/MC/MCContext.h"
  15. #include "llvm/MC/MCDisassembler/MCDisassembler.h"
  16. #include "llvm/MC/MCFixedLenDisassembler.h"
  17. #include "llvm/MC/MCInst.h"
  18. #include "llvm/MC/MCInstrDesc.h"
  19. #include "llvm/MC/MCSubtargetInfo.h"
  20. #include "llvm/MC/SubtargetFeature.h"
  21. #include "llvm/MC/TargetRegistry.h"
  22. #include "llvm/Support/Compiler.h"
  23. #include "llvm/Support/ErrorHandling.h"
  24. #include "llvm/Support/MathExtras.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <algorithm>
  27. #include <cassert>
  28. #include <cstdint>
  29. #include <vector>
  30. using namespace llvm;
  31. #define DEBUG_TYPE "arm-disassembler"
  32. using DecodeStatus = MCDisassembler::DecodeStatus;
  33. namespace {
  34. // Handles the condition code status of instructions in IT blocks
  35. class ITStatus
  36. {
  37. public:
  38. // Returns the condition code for instruction in IT block
  39. unsigned getITCC() {
  40. unsigned CC = ARMCC::AL;
  41. if (instrInITBlock())
  42. CC = ITStates.back();
  43. return CC;
  44. }
  45. // Advances the IT block state to the next T or E
  46. void advanceITState() {
  47. ITStates.pop_back();
  48. }
  49. // Returns true if the current instruction is in an IT block
  50. bool instrInITBlock() {
  51. return !ITStates.empty();
  52. }
  53. // Returns true if current instruction is the last instruction in an IT block
  54. bool instrLastInITBlock() {
  55. return ITStates.size() == 1;
  56. }
  57. // Called when decoding an IT instruction. Sets the IT state for
  58. // the following instructions that for the IT block. Firstcond
  59. // corresponds to the field in the IT instruction encoding; Mask
  60. // is in the MCOperand format in which 1 means 'else' and 0 'then'.
  61. void setITState(char Firstcond, char Mask) {
  62. // (3 - the number of trailing zeros) is the number of then / else.
  63. unsigned NumTZ = countTrailingZeros<uint8_t>(Mask);
  64. unsigned char CCBits = static_cast<unsigned char>(Firstcond & 0xf);
  65. assert(NumTZ <= 3 && "Invalid IT mask!");
  66. // push condition codes onto the stack the correct order for the pops
  67. for (unsigned Pos = NumTZ+1; Pos <= 3; ++Pos) {
  68. unsigned Else = (Mask >> Pos) & 1;
  69. ITStates.push_back(CCBits ^ Else);
  70. }
  71. ITStates.push_back(CCBits);
  72. }
  73. private:
  74. std::vector<unsigned char> ITStates;
  75. };
  76. class VPTStatus
  77. {
  78. public:
  79. unsigned getVPTPred() {
  80. unsigned Pred = ARMVCC::None;
  81. if (instrInVPTBlock())
  82. Pred = VPTStates.back();
  83. return Pred;
  84. }
  85. void advanceVPTState() {
  86. VPTStates.pop_back();
  87. }
  88. bool instrInVPTBlock() {
  89. return !VPTStates.empty();
  90. }
  91. bool instrLastInVPTBlock() {
  92. return VPTStates.size() == 1;
  93. }
  94. void setVPTState(char Mask) {
  95. // (3 - the number of trailing zeros) is the number of then / else.
  96. unsigned NumTZ = countTrailingZeros<uint8_t>(Mask);
  97. assert(NumTZ <= 3 && "Invalid VPT mask!");
  98. // push predicates onto the stack the correct order for the pops
  99. for (unsigned Pos = NumTZ+1; Pos <= 3; ++Pos) {
  100. bool T = ((Mask >> Pos) & 1) == 0;
  101. if (T)
  102. VPTStates.push_back(ARMVCC::Then);
  103. else
  104. VPTStates.push_back(ARMVCC::Else);
  105. }
  106. VPTStates.push_back(ARMVCC::Then);
  107. }
  108. private:
  109. SmallVector<unsigned char, 4> VPTStates;
  110. };
  111. /// ARM disassembler for all ARM platforms.
  112. class ARMDisassembler : public MCDisassembler {
  113. public:
  114. ARMDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx) :
  115. MCDisassembler(STI, Ctx) {
  116. }
  117. ~ARMDisassembler() override = default;
  118. DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
  119. ArrayRef<uint8_t> Bytes, uint64_t Address,
  120. raw_ostream &CStream) const override;
  121. private:
  122. DecodeStatus getARMInstruction(MCInst &Instr, uint64_t &Size,
  123. ArrayRef<uint8_t> Bytes, uint64_t Address,
  124. raw_ostream &CStream) const;
  125. DecodeStatus getThumbInstruction(MCInst &Instr, uint64_t &Size,
  126. ArrayRef<uint8_t> Bytes, uint64_t Address,
  127. raw_ostream &CStream) const;
  128. mutable ITStatus ITBlock;
  129. mutable VPTStatus VPTBlock;
  130. DecodeStatus AddThumbPredicate(MCInst&) const;
  131. void UpdateThumbVFPPredicate(DecodeStatus &, MCInst&) const;
  132. };
  133. } // end anonymous namespace
  134. static bool Check(DecodeStatus &Out, DecodeStatus In) {
  135. switch (In) {
  136. case MCDisassembler::Success:
  137. // Out stays the same.
  138. return true;
  139. case MCDisassembler::SoftFail:
  140. Out = In;
  141. return true;
  142. case MCDisassembler::Fail:
  143. Out = In;
  144. return false;
  145. }
  146. llvm_unreachable("Invalid DecodeStatus!");
  147. }
  148. // Forward declare these because the autogenerated code will reference them.
  149. // Definitions are further down.
  150. static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo,
  151. uint64_t Address, const void *Decoder);
  152. static DecodeStatus DecodeCLRMGPRRegisterClass(MCInst &Inst, unsigned RegNo,
  153. uint64_t Address, const void *Decoder);
  154. static DecodeStatus DecodetGPROddRegisterClass(MCInst &Inst, unsigned RegNo,
  155. uint64_t Address, const void *Decoder);
  156. static DecodeStatus DecodetGPREvenRegisterClass(MCInst &Inst, unsigned RegNo,
  157. uint64_t Address, const void *Decoder);
  158. static DecodeStatus
  159. DecodeGPRwithAPSR_NZCVnospRegisterClass(MCInst &Inst, unsigned RegNo,
  160. uint64_t Address, const void *Decoder);
  161. static DecodeStatus DecodeGPRnopcRegisterClass(MCInst &Inst, unsigned RegNo,
  162. uint64_t Address,
  163. const void *Decoder);
  164. static DecodeStatus DecodeGPRnospRegisterClass(MCInst &Inst, unsigned RegNo,
  165. uint64_t Address,
  166. const void *Decoder);
  167. static DecodeStatus DecodeGPRwithAPSRRegisterClass(MCInst &Inst,
  168. unsigned RegNo, uint64_t Address,
  169. const void *Decoder);
  170. static DecodeStatus DecodeGPRwithZRRegisterClass(MCInst &Inst,
  171. unsigned RegNo, uint64_t Address,
  172. const void *Decoder);
  173. static DecodeStatus DecodeGPRwithZRnospRegisterClass(
  174. MCInst &Inst, unsigned RegNo, uint64_t Address, const void *Decoder);
  175. static DecodeStatus DecodetGPRRegisterClass(MCInst &Inst, unsigned RegNo,
  176. uint64_t Address, const void *Decoder);
  177. static DecodeStatus DecodetcGPRRegisterClass(MCInst &Inst, unsigned RegNo,
  178. uint64_t Address, const void *Decoder);
  179. static DecodeStatus DecoderGPRRegisterClass(MCInst &Inst, unsigned RegNo,
  180. uint64_t Address, const void *Decoder);
  181. static DecodeStatus DecodeGPRPairRegisterClass(MCInst &Inst, unsigned RegNo,
  182. uint64_t Address, const void *Decoder);
  183. static DecodeStatus DecodeGPRPairnospRegisterClass(MCInst &Inst, unsigned RegNo,
  184. uint64_t Address, const void *Decoder);
  185. static DecodeStatus DecodeGPRspRegisterClass(MCInst &Inst, unsigned RegNo,
  186. uint64_t Address,
  187. const void *Decoder);
  188. static DecodeStatus DecodeHPRRegisterClass(MCInst &Inst, unsigned RegNo,
  189. uint64_t Address, const void *Decoder);
  190. static DecodeStatus DecodeSPRRegisterClass(MCInst &Inst, unsigned RegNo,
  191. uint64_t Address, const void *Decoder);
  192. static DecodeStatus DecodeDPRRegisterClass(MCInst &Inst, unsigned RegNo,
  193. uint64_t Address, const void *Decoder);
  194. static DecodeStatus DecodeDPR_8RegisterClass(MCInst &Inst, unsigned RegNo,
  195. uint64_t Address, const void *Decoder);
  196. static DecodeStatus DecodeSPR_8RegisterClass(MCInst &Inst, unsigned RegNo,
  197. uint64_t Address, const void *Decoder);
  198. static DecodeStatus DecodeDPR_VFP2RegisterClass(MCInst &Inst,
  199. unsigned RegNo,
  200. uint64_t Address,
  201. const void *Decoder);
  202. static DecodeStatus DecodeQPRRegisterClass(MCInst &Inst, unsigned RegNo,
  203. uint64_t Address, const void *Decoder);
  204. static DecodeStatus DecodeMQPRRegisterClass(MCInst &Inst, unsigned RegNo,
  205. uint64_t Address, const void *Decoder);
  206. static DecodeStatus DecodeMQQPRRegisterClass(MCInst &Inst, unsigned RegNo,
  207. uint64_t Address,
  208. const void *Decoder);
  209. static DecodeStatus DecodeMQQQQPRRegisterClass(MCInst &Inst, unsigned RegNo,
  210. uint64_t Address,
  211. const void *Decoder);
  212. static DecodeStatus DecodeDPairRegisterClass(MCInst &Inst, unsigned RegNo,
  213. uint64_t Address, const void *Decoder);
  214. static DecodeStatus DecodeDPairSpacedRegisterClass(MCInst &Inst,
  215. unsigned RegNo, uint64_t Address,
  216. const void *Decoder);
  217. static DecodeStatus DecodePredicateOperand(MCInst &Inst, unsigned Val,
  218. uint64_t Address, const void *Decoder);
  219. static DecodeStatus DecodeCCOutOperand(MCInst &Inst, unsigned Val,
  220. uint64_t Address, const void *Decoder);
  221. static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Val,
  222. uint64_t Address, const void *Decoder);
  223. static DecodeStatus DecodeSPRRegListOperand(MCInst &Inst, unsigned Val,
  224. uint64_t Address, const void *Decoder);
  225. static DecodeStatus DecodeDPRRegListOperand(MCInst &Inst, unsigned Val,
  226. uint64_t Address, const void *Decoder);
  227. static DecodeStatus DecodeBitfieldMaskOperand(MCInst &Inst, unsigned Insn,
  228. uint64_t Address, const void *Decoder);
  229. static DecodeStatus DecodeCopMemInstruction(MCInst &Inst, unsigned Insn,
  230. uint64_t Address, const void *Decoder);
  231. static DecodeStatus DecodeAddrMode2IdxInstruction(MCInst &Inst,
  232. unsigned Insn,
  233. uint64_t Address,
  234. const void *Decoder);
  235. static DecodeStatus DecodeSORegMemOperand(MCInst &Inst, unsigned Insn,
  236. uint64_t Address, const void *Decoder);
  237. static DecodeStatus DecodeAddrMode3Instruction(MCInst &Inst,unsigned Insn,
  238. uint64_t Address, const void *Decoder);
  239. static DecodeStatus DecodeSORegImmOperand(MCInst &Inst, unsigned Insn,
  240. uint64_t Address, const void *Decoder);
  241. static DecodeStatus DecodeSORegRegOperand(MCInst &Inst, unsigned Insn,
  242. uint64_t Address, const void *Decoder);
  243. static DecodeStatus DecodeMemMultipleWritebackInstruction(MCInst & Inst,
  244. unsigned Insn,
  245. uint64_t Adddress,
  246. const void *Decoder);
  247. static DecodeStatus DecodeT2MOVTWInstruction(MCInst &Inst, unsigned Insn,
  248. uint64_t Address, const void *Decoder);
  249. static DecodeStatus DecodeArmMOVTWInstruction(MCInst &Inst, unsigned Insn,
  250. uint64_t Address, const void *Decoder);
  251. static DecodeStatus DecodeSMLAInstruction(MCInst &Inst, unsigned Insn,
  252. uint64_t Address, const void *Decoder);
  253. static DecodeStatus DecodeHINTInstruction(MCInst &Inst, unsigned Insn,
  254. uint64_t Address, const void *Decoder);
  255. static DecodeStatus DecodeCPSInstruction(MCInst &Inst, unsigned Insn,
  256. uint64_t Address, const void *Decoder);
  257. static DecodeStatus DecodeTSTInstruction(MCInst &Inst, unsigned Insn,
  258. uint64_t Address, const void *Decoder);
  259. static DecodeStatus DecodeSETPANInstruction(MCInst &Inst, unsigned Insn,
  260. uint64_t Address, const void *Decoder);
  261. static DecodeStatus DecodeT2CPSInstruction(MCInst &Inst, unsigned Insn,
  262. uint64_t Address, const void *Decoder);
  263. static DecodeStatus DecodeT2HintSpaceInstruction(MCInst &Inst, unsigned Insn,
  264. uint64_t Address,
  265. const void *Decoder);
  266. static DecodeStatus DecodeAddrModeImm12Operand(MCInst &Inst, unsigned Val,
  267. uint64_t Address, const void *Decoder);
  268. static DecodeStatus DecodeAddrMode5Operand(MCInst &Inst, unsigned Val,
  269. uint64_t Address, const void *Decoder);
  270. static DecodeStatus DecodeAddrMode5FP16Operand(MCInst &Inst, unsigned Val,
  271. uint64_t Address, const void *Decoder);
  272. static DecodeStatus DecodeAddrMode7Operand(MCInst &Inst, unsigned Val,
  273. uint64_t Address, const void *Decoder);
  274. static DecodeStatus DecodeT2BInstruction(MCInst &Inst, unsigned Insn,
  275. uint64_t Address, const void *Decoder);
  276. static DecodeStatus DecodeBranchImmInstruction(MCInst &Inst,unsigned Insn,
  277. uint64_t Address, const void *Decoder);
  278. static DecodeStatus DecodeAddrMode6Operand(MCInst &Inst, unsigned Val,
  279. uint64_t Address, const void *Decoder);
  280. static DecodeStatus DecodeVLDST1Instruction(MCInst &Inst, unsigned Val,
  281. uint64_t Address, const void *Decoder);
  282. static DecodeStatus DecodeVLDST2Instruction(MCInst &Inst, unsigned Val,
  283. uint64_t Address, const void *Decoder);
  284. static DecodeStatus DecodeVLDST3Instruction(MCInst &Inst, unsigned Val,
  285. uint64_t Address, const void *Decoder);
  286. static DecodeStatus DecodeVLDST4Instruction(MCInst &Inst, unsigned Val,
  287. uint64_t Address, const void *Decoder);
  288. static DecodeStatus DecodeVLDInstruction(MCInst &Inst, unsigned Val,
  289. uint64_t Address, const void *Decoder);
  290. static DecodeStatus DecodeVSTInstruction(MCInst &Inst, unsigned Val,
  291. uint64_t Address, const void *Decoder);
  292. static DecodeStatus DecodeVLD1DupInstruction(MCInst &Inst, unsigned Val,
  293. uint64_t Address, const void *Decoder);
  294. static DecodeStatus DecodeVLD2DupInstruction(MCInst &Inst, unsigned Val,
  295. uint64_t Address, const void *Decoder);
  296. static DecodeStatus DecodeVLD3DupInstruction(MCInst &Inst, unsigned Val,
  297. uint64_t Address, const void *Decoder);
  298. static DecodeStatus DecodeVLD4DupInstruction(MCInst &Inst, unsigned Val,
  299. uint64_t Address, const void *Decoder);
  300. static DecodeStatus DecodeVMOVModImmInstruction(MCInst &Inst,unsigned Val,
  301. uint64_t Address, const void *Decoder);
  302. static DecodeStatus DecodeMVEModImmInstruction(MCInst &Inst,unsigned Val,
  303. uint64_t Address, const void *Decoder);
  304. static DecodeStatus DecodeMVEVADCInstruction(MCInst &Inst, unsigned Insn,
  305. uint64_t Address, const void *Decoder);
  306. static DecodeStatus DecodeVSHLMaxInstruction(MCInst &Inst, unsigned Val,
  307. uint64_t Address, const void *Decoder);
  308. static DecodeStatus DecodeShiftRight8Imm(MCInst &Inst, unsigned Val,
  309. uint64_t Address, const void *Decoder);
  310. static DecodeStatus DecodeShiftRight16Imm(MCInst &Inst, unsigned Val,
  311. uint64_t Address, const void *Decoder);
  312. static DecodeStatus DecodeShiftRight32Imm(MCInst &Inst, unsigned Val,
  313. uint64_t Address, const void *Decoder);
  314. static DecodeStatus DecodeShiftRight64Imm(MCInst &Inst, unsigned Val,
  315. uint64_t Address, const void *Decoder);
  316. static DecodeStatus DecodeTBLInstruction(MCInst &Inst, unsigned Insn,
  317. uint64_t Address, const void *Decoder);
  318. static DecodeStatus DecodePostIdxReg(MCInst &Inst, unsigned Insn,
  319. uint64_t Address, const void *Decoder);
  320. static DecodeStatus DecodeMveAddrModeRQ(MCInst &Inst, unsigned Insn,
  321. uint64_t Address, const void *Decoder);
  322. template<int shift>
  323. static DecodeStatus DecodeMveAddrModeQ(MCInst &Inst, unsigned Insn,
  324. uint64_t Address, const void *Decoder);
  325. static DecodeStatus DecodeCoprocessor(MCInst &Inst, unsigned Insn,
  326. uint64_t Address, const void *Decoder);
  327. static DecodeStatus DecodeMemBarrierOption(MCInst &Inst, unsigned Insn,
  328. uint64_t Address, const void *Decoder);
  329. static DecodeStatus DecodeInstSyncBarrierOption(MCInst &Inst, unsigned Insn,
  330. uint64_t Address, const void *Decoder);
  331. static DecodeStatus DecodeMSRMask(MCInst &Inst, unsigned Insn,
  332. uint64_t Address, const void *Decoder);
  333. static DecodeStatus DecodeBankedReg(MCInst &Inst, unsigned Insn,
  334. uint64_t Address, const void *Decoder);
  335. static DecodeStatus DecodeDoubleRegLoad(MCInst &Inst, unsigned Insn,
  336. uint64_t Address, const void *Decoder);
  337. static DecodeStatus DecodeDoubleRegStore(MCInst &Inst, unsigned Insn,
  338. uint64_t Address, const void *Decoder);
  339. static DecodeStatus DecodeLDRPreImm(MCInst &Inst, unsigned Insn,
  340. uint64_t Address, const void *Decoder);
  341. static DecodeStatus DecodeLDRPreReg(MCInst &Inst, unsigned Insn,
  342. uint64_t Address, const void *Decoder);
  343. static DecodeStatus DecodeSTRPreImm(MCInst &Inst, unsigned Insn,
  344. uint64_t Address, const void *Decoder);
  345. static DecodeStatus DecodeSTRPreReg(MCInst &Inst, unsigned Insn,
  346. uint64_t Address, const void *Decoder);
  347. static DecodeStatus DecodeVLD1LN(MCInst &Inst, unsigned Insn,
  348. uint64_t Address, const void *Decoder);
  349. static DecodeStatus DecodeVLD2LN(MCInst &Inst, unsigned Insn,
  350. uint64_t Address, const void *Decoder);
  351. static DecodeStatus DecodeVLD3LN(MCInst &Inst, unsigned Insn,
  352. uint64_t Address, const void *Decoder);
  353. static DecodeStatus DecodeVLD4LN(MCInst &Inst, unsigned Insn,
  354. uint64_t Address, const void *Decoder);
  355. static DecodeStatus DecodeVST1LN(MCInst &Inst, unsigned Insn,
  356. uint64_t Address, const void *Decoder);
  357. static DecodeStatus DecodeVST2LN(MCInst &Inst, unsigned Insn,
  358. uint64_t Address, const void *Decoder);
  359. static DecodeStatus DecodeVST3LN(MCInst &Inst, unsigned Insn,
  360. uint64_t Address, const void *Decoder);
  361. static DecodeStatus DecodeVST4LN(MCInst &Inst, unsigned Insn,
  362. uint64_t Address, const void *Decoder);
  363. static DecodeStatus DecodeVMOVSRR(MCInst &Inst, unsigned Insn,
  364. uint64_t Address, const void *Decoder);
  365. static DecodeStatus DecodeVMOVRRS(MCInst &Inst, unsigned Insn,
  366. uint64_t Address, const void *Decoder);
  367. static DecodeStatus DecodeSwap(MCInst &Inst, unsigned Insn,
  368. uint64_t Address, const void *Decoder);
  369. static DecodeStatus DecodeVCVTD(MCInst &Inst, unsigned Insn,
  370. uint64_t Address, const void *Decoder);
  371. static DecodeStatus DecodeVCVTQ(MCInst &Inst, unsigned Insn,
  372. uint64_t Address, const void *Decoder);
  373. static DecodeStatus DecodeVCVTImmOperand(MCInst &Inst, unsigned Insn,
  374. uint64_t Address, const void *Decoder);
  375. static DecodeStatus DecodeNEONComplexLane64Instruction(MCInst &Inst,
  376. unsigned Val,
  377. uint64_t Address,
  378. const void *Decoder);
  379. static DecodeStatus DecodeThumbAddSpecialReg(MCInst &Inst, uint16_t Insn,
  380. uint64_t Address, const void *Decoder);
  381. static DecodeStatus DecodeThumbBROperand(MCInst &Inst, unsigned Val,
  382. uint64_t Address, const void *Decoder);
  383. static DecodeStatus DecodeT2BROperand(MCInst &Inst, unsigned Val,
  384. uint64_t Address, const void *Decoder);
  385. static DecodeStatus DecodeThumbCmpBROperand(MCInst &Inst, unsigned Val,
  386. uint64_t Address, const void *Decoder);
  387. static DecodeStatus DecodeThumbAddrModeRR(MCInst &Inst, unsigned Val,
  388. uint64_t Address, const void *Decoder);
  389. static DecodeStatus DecodeThumbAddrModeIS(MCInst &Inst, unsigned Val,
  390. uint64_t Address, const void *Decoder);
  391. static DecodeStatus DecodeThumbAddrModePC(MCInst &Inst, unsigned Val,
  392. uint64_t Address, const void *Decoder);
  393. static DecodeStatus DecodeThumbAddrModeSP(MCInst &Inst, unsigned Val,
  394. uint64_t Address, const void *Decoder);
  395. static DecodeStatus DecodeT2AddrModeSOReg(MCInst &Inst, unsigned Val,
  396. uint64_t Address, const void *Decoder);
  397. static DecodeStatus DecodeT2LoadShift(MCInst &Inst, unsigned Val,
  398. uint64_t Address, const void *Decoder);
  399. static DecodeStatus DecodeT2LoadImm8(MCInst &Inst, unsigned Insn,
  400. uint64_t Address, const void* Decoder);
  401. static DecodeStatus DecodeT2LoadImm12(MCInst &Inst, unsigned Insn,
  402. uint64_t Address, const void* Decoder);
  403. static DecodeStatus DecodeT2LoadT(MCInst &Inst, unsigned Insn,
  404. uint64_t Address, const void* Decoder);
  405. static DecodeStatus DecodeT2LoadLabel(MCInst &Inst, unsigned Insn,
  406. uint64_t Address, const void* Decoder);
  407. static DecodeStatus DecodeT2Imm8S4(MCInst &Inst, unsigned Val,
  408. uint64_t Address, const void *Decoder);
  409. static DecodeStatus DecodeT2Imm7S4(MCInst &Inst, unsigned Val,
  410. uint64_t Address, const void *Decoder);
  411. static DecodeStatus DecodeT2AddrModeImm8s4(MCInst &Inst, unsigned Val,
  412. uint64_t Address, const void *Decoder);
  413. static DecodeStatus DecodeT2AddrModeImm7s4(MCInst &Inst, unsigned Val,
  414. uint64_t Address,
  415. const void *Decoder);
  416. static DecodeStatus DecodeT2AddrModeImm0_1020s4(MCInst &Inst,unsigned Val,
  417. uint64_t Address, const void *Decoder);
  418. static DecodeStatus DecodeT2Imm8(MCInst &Inst, unsigned Val,
  419. uint64_t Address, const void *Decoder);
  420. template<int shift>
  421. static DecodeStatus DecodeT2Imm7(MCInst &Inst, unsigned Val,
  422. uint64_t Address, const void *Decoder);
  423. static DecodeStatus DecodeT2AddrModeImm8(MCInst &Inst, unsigned Val,
  424. uint64_t Address, const void *Decoder);
  425. template<int shift>
  426. static DecodeStatus DecodeTAddrModeImm7(MCInst &Inst, unsigned Val,
  427. uint64_t Address, const void *Decoder);
  428. template<int shift, int WriteBack>
  429. static DecodeStatus DecodeT2AddrModeImm7(MCInst &Inst, unsigned Val,
  430. uint64_t Address, const void *Decoder);
  431. static DecodeStatus DecodeThumbAddSPImm(MCInst &Inst, uint16_t Val,
  432. uint64_t Address, const void *Decoder);
  433. static DecodeStatus DecodeThumbAddSPReg(MCInst &Inst, uint16_t Insn,
  434. uint64_t Address, const void *Decoder);
  435. static DecodeStatus DecodeThumbCPS(MCInst &Inst, uint16_t Insn,
  436. uint64_t Address, const void *Decoder);
  437. static DecodeStatus DecodeQADDInstruction(MCInst &Inst, unsigned Insn,
  438. uint64_t Address, const void *Decoder);
  439. static DecodeStatus DecodeThumbBLXOffset(MCInst &Inst, unsigned Insn,
  440. uint64_t Address, const void *Decoder);
  441. static DecodeStatus DecodeT2AddrModeImm12(MCInst &Inst, unsigned Val,
  442. uint64_t Address, const void *Decoder);
  443. static DecodeStatus DecodeThumbTableBranch(MCInst &Inst, unsigned Val,
  444. uint64_t Address, const void *Decoder);
  445. static DecodeStatus DecodeThumb2BCCInstruction(MCInst &Inst, unsigned Val,
  446. uint64_t Address, const void *Decoder);
  447. static DecodeStatus DecodeT2SOImm(MCInst &Inst, unsigned Val,
  448. uint64_t Address, const void *Decoder);
  449. static DecodeStatus DecodeThumbBCCTargetOperand(MCInst &Inst,unsigned Val,
  450. uint64_t Address, const void *Decoder);
  451. static DecodeStatus DecodeThumbBLTargetOperand(MCInst &Inst, unsigned Val,
  452. uint64_t Address, const void *Decoder);
  453. static DecodeStatus DecodeIT(MCInst &Inst, unsigned Val,
  454. uint64_t Address, const void *Decoder);
  455. static DecodeStatus DecodeT2LDRDPreInstruction(MCInst &Inst,unsigned Insn,
  456. uint64_t Address, const void *Decoder);
  457. static DecodeStatus DecodeT2STRDPreInstruction(MCInst &Inst,unsigned Insn,
  458. uint64_t Address, const void *Decoder);
  459. static DecodeStatus DecodeT2Adr(MCInst &Inst, unsigned Val,
  460. uint64_t Address, const void *Decoder);
  461. static DecodeStatus DecodeT2LdStPre(MCInst &Inst, unsigned Val,
  462. uint64_t Address, const void *Decoder);
  463. static DecodeStatus DecodeT2ShifterImmOperand(MCInst &Inst, unsigned Val,
  464. uint64_t Address, const void *Decoder);
  465. static DecodeStatus DecodeLDR(MCInst &Inst, unsigned Val,
  466. uint64_t Address, const void *Decoder);
  467. static DecodeStatus DecoderForMRRC2AndMCRR2(MCInst &Inst, unsigned Val,
  468. uint64_t Address, const void *Decoder);
  469. static DecodeStatus DecodeForVMRSandVMSR(MCInst &Inst, unsigned Val,
  470. uint64_t Address, const void *Decoder);
  471. template <bool isSigned, bool isNeg, bool zeroPermitted, int size>
  472. static DecodeStatus DecodeBFLabelOperand(MCInst &Inst, unsigned val,
  473. uint64_t Address, const void *Decoder);
  474. static DecodeStatus DecodeBFAfterTargetOperand(MCInst &Inst, unsigned val,
  475. uint64_t Address,
  476. const void *Decoder);
  477. static DecodeStatus DecodePredNoALOperand(MCInst &Inst, unsigned Val,
  478. uint64_t Address,
  479. const void *Decoder);
  480. static DecodeStatus DecodeLOLoop(MCInst &Inst, unsigned Insn, uint64_t Address,
  481. const void *Decoder);
  482. static DecodeStatus DecodeLongShiftOperand(MCInst &Inst, unsigned Val,
  483. uint64_t Address,
  484. const void *Decoder);
  485. static DecodeStatus DecodeVSCCLRM(MCInst &Inst, unsigned Insn, uint64_t Address,
  486. const void *Decoder);
  487. static DecodeStatus DecodeVPTMaskOperand(MCInst &Inst, unsigned Val,
  488. uint64_t Address, const void *Decoder);
  489. static DecodeStatus DecodeVpredROperand(MCInst &Inst, unsigned Val,
  490. uint64_t Address, const void *Decoder);
  491. static DecodeStatus DecodeRestrictedIPredicateOperand(MCInst &Inst, unsigned Val,
  492. uint64_t Address,
  493. const void *Decoder);
  494. static DecodeStatus DecodeRestrictedSPredicateOperand(MCInst &Inst, unsigned Val,
  495. uint64_t Address,
  496. const void *Decoder);
  497. static DecodeStatus DecodeRestrictedUPredicateOperand(MCInst &Inst, unsigned Val,
  498. uint64_t Address,
  499. const void *Decoder);
  500. static DecodeStatus DecodeRestrictedFPPredicateOperand(MCInst &Inst,
  501. unsigned Val,
  502. uint64_t Address,
  503. const void *Decoder);
  504. template<bool Writeback>
  505. static DecodeStatus DecodeVSTRVLDR_SYSREG(MCInst &Inst, unsigned Insn,
  506. uint64_t Address,
  507. const void *Decoder);
  508. template<int shift>
  509. static DecodeStatus DecodeMVE_MEM_1_pre(MCInst &Inst, unsigned Val,
  510. uint64_t Address, const void *Decoder);
  511. template<int shift>
  512. static DecodeStatus DecodeMVE_MEM_2_pre(MCInst &Inst, unsigned Val,
  513. uint64_t Address, const void *Decoder);
  514. template<int shift>
  515. static DecodeStatus DecodeMVE_MEM_3_pre(MCInst &Inst, unsigned Val,
  516. uint64_t Address, const void *Decoder);
  517. template<unsigned MinLog, unsigned MaxLog>
  518. static DecodeStatus DecodePowerTwoOperand(MCInst &Inst, unsigned Val,
  519. uint64_t Address,
  520. const void *Decoder);
  521. template<unsigned start>
  522. static DecodeStatus DecodeMVEPairVectorIndexOperand(MCInst &Inst, unsigned Val,
  523. uint64_t Address,
  524. const void *Decoder);
  525. static DecodeStatus DecodeMVEVMOVQtoDReg(MCInst &Inst, unsigned Insn,
  526. uint64_t Address,
  527. const void *Decoder);
  528. static DecodeStatus DecodeMVEVMOVDRegtoQ(MCInst &Inst, unsigned Insn,
  529. uint64_t Address,
  530. const void *Decoder);
  531. static DecodeStatus DecodeMVEVCVTt1fp(MCInst &Inst, unsigned Insn,
  532. uint64_t Address, const void *Decoder);
  533. typedef DecodeStatus OperandDecoder(MCInst &Inst, unsigned Val,
  534. uint64_t Address, const void *Decoder);
  535. template<bool scalar, OperandDecoder predicate_decoder>
  536. static DecodeStatus DecodeMVEVCMP(MCInst &Inst, unsigned Insn,
  537. uint64_t Address, const void *Decoder);
  538. static DecodeStatus DecodeMveVCTP(MCInst &Inst, unsigned Insn,
  539. uint64_t Address, const void *Decoder);
  540. static DecodeStatus DecodeMVEVPNOT(MCInst &Inst, unsigned Insn,
  541. uint64_t Address, const void *Decoder);
  542. static DecodeStatus DecodeMVEOverlappingLongShift(MCInst &Inst, unsigned Insn,
  543. uint64_t Address,
  544. const void *Decoder);
  545. static DecodeStatus DecodeT2AddSubSPImm(MCInst &Inst, unsigned Insn,
  546. uint64_t Address, const void *Decoder);
  547. #include "ARMGenDisassemblerTables.inc"
  548. static MCDisassembler *createARMDisassembler(const Target &T,
  549. const MCSubtargetInfo &STI,
  550. MCContext &Ctx) {
  551. return new ARMDisassembler(STI, Ctx);
  552. }
  553. // Post-decoding checks
  554. static DecodeStatus checkDecodedInstruction(MCInst &MI, uint64_t &Size,
  555. uint64_t Address, raw_ostream &CS,
  556. uint32_t Insn,
  557. DecodeStatus Result) {
  558. switch (MI.getOpcode()) {
  559. case ARM::HVC: {
  560. // HVC is undefined if condition = 0xf otherwise upredictable
  561. // if condition != 0xe
  562. uint32_t Cond = (Insn >> 28) & 0xF;
  563. if (Cond == 0xF)
  564. return MCDisassembler::Fail;
  565. if (Cond != 0xE)
  566. return MCDisassembler::SoftFail;
  567. return Result;
  568. }
  569. case ARM::t2ADDri:
  570. case ARM::t2ADDri12:
  571. case ARM::t2ADDrr:
  572. case ARM::t2ADDrs:
  573. case ARM::t2SUBri:
  574. case ARM::t2SUBri12:
  575. case ARM::t2SUBrr:
  576. case ARM::t2SUBrs:
  577. if (MI.getOperand(0).getReg() == ARM::SP &&
  578. MI.getOperand(1).getReg() != ARM::SP)
  579. return MCDisassembler::SoftFail;
  580. return Result;
  581. default: return Result;
  582. }
  583. }
  584. DecodeStatus ARMDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
  585. ArrayRef<uint8_t> Bytes,
  586. uint64_t Address,
  587. raw_ostream &CS) const {
  588. if (STI.getFeatureBits()[ARM::ModeThumb])
  589. return getThumbInstruction(MI, Size, Bytes, Address, CS);
  590. return getARMInstruction(MI, Size, Bytes, Address, CS);
  591. }
  592. DecodeStatus ARMDisassembler::getARMInstruction(MCInst &MI, uint64_t &Size,
  593. ArrayRef<uint8_t> Bytes,
  594. uint64_t Address,
  595. raw_ostream &CS) const {
  596. CommentStream = &CS;
  597. assert(!STI.getFeatureBits()[ARM::ModeThumb] &&
  598. "Asked to disassemble an ARM instruction but Subtarget is in Thumb "
  599. "mode!");
  600. // We want to read exactly 4 bytes of data.
  601. if (Bytes.size() < 4) {
  602. Size = 0;
  603. return MCDisassembler::Fail;
  604. }
  605. // Encoded as a small-endian 32-bit word in the stream.
  606. uint32_t Insn =
  607. (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | (Bytes[0] << 0);
  608. // Calling the auto-generated decoder function.
  609. DecodeStatus Result =
  610. decodeInstruction(DecoderTableARM32, MI, Insn, Address, this, STI);
  611. if (Result != MCDisassembler::Fail) {
  612. Size = 4;
  613. return checkDecodedInstruction(MI, Size, Address, CS, Insn, Result);
  614. }
  615. struct DecodeTable {
  616. const uint8_t *P;
  617. bool DecodePred;
  618. };
  619. const DecodeTable Tables[] = {
  620. {DecoderTableVFP32, false}, {DecoderTableVFPV832, false},
  621. {DecoderTableNEONData32, true}, {DecoderTableNEONLoadStore32, true},
  622. {DecoderTableNEONDup32, true}, {DecoderTablev8NEON32, false},
  623. {DecoderTablev8Crypto32, false},
  624. };
  625. for (auto Table : Tables) {
  626. Result = decodeInstruction(Table.P, MI, Insn, Address, this, STI);
  627. if (Result != MCDisassembler::Fail) {
  628. Size = 4;
  629. // Add a fake predicate operand, because we share these instruction
  630. // definitions with Thumb2 where these instructions are predicable.
  631. if (Table.DecodePred && !DecodePredicateOperand(MI, 0xE, Address, this))
  632. return MCDisassembler::Fail;
  633. return Result;
  634. }
  635. }
  636. Result =
  637. decodeInstruction(DecoderTableCoProc32, MI, Insn, Address, this, STI);
  638. if (Result != MCDisassembler::Fail) {
  639. Size = 4;
  640. return checkDecodedInstruction(MI, Size, Address, CS, Insn, Result);
  641. }
  642. Size = 4;
  643. return MCDisassembler::Fail;
  644. }
  645. namespace llvm {
  646. extern const MCInstrDesc ARMInsts[];
  647. } // end namespace llvm
  648. /// tryAddingSymbolicOperand - trys to add a symbolic operand in place of the
  649. /// immediate Value in the MCInst. The immediate Value has had any PC
  650. /// adjustment made by the caller. If the instruction is a branch instruction
  651. /// then isBranch is true, else false. If the getOpInfo() function was set as
  652. /// part of the setupForSymbolicDisassembly() call then that function is called
  653. /// to get any symbolic information at the Address for this instruction. If
  654. /// that returns non-zero then the symbolic information it returns is used to
  655. /// create an MCExpr and that is added as an operand to the MCInst. If
  656. /// getOpInfo() returns zero and isBranch is true then a symbol look up for
  657. /// Value is done and if a symbol is found an MCExpr is created with that, else
  658. /// an MCExpr with Value is created. This function returns true if it adds an
  659. /// operand to the MCInst and false otherwise.
  660. static bool tryAddingSymbolicOperand(uint64_t Address, int32_t Value,
  661. bool isBranch, uint64_t InstSize,
  662. MCInst &MI, const void *Decoder) {
  663. const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
  664. // FIXME: Does it make sense for value to be negative?
  665. return Dis->tryAddingSymbolicOperand(MI, (uint32_t)Value, Address, isBranch,
  666. /* Offset */ 0, InstSize);
  667. }
  668. /// tryAddingPcLoadReferenceComment - trys to add a comment as to what is being
  669. /// referenced by a load instruction with the base register that is the Pc.
  670. /// These can often be values in a literal pool near the Address of the
  671. /// instruction. The Address of the instruction and its immediate Value are
  672. /// used as a possible literal pool entry. The SymbolLookUp call back will
  673. /// return the name of a symbol referenced by the literal pool's entry if
  674. /// the referenced address is that of a symbol. Or it will return a pointer to
  675. /// a literal 'C' string if the referenced address of the literal pool's entry
  676. /// is an address into a section with 'C' string literals.
  677. static void tryAddingPcLoadReferenceComment(uint64_t Address, int Value,
  678. const void *Decoder) {
  679. const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
  680. Dis->tryAddingPcLoadReferenceComment(Value, Address);
  681. }
  682. // Thumb1 instructions don't have explicit S bits. Rather, they
  683. // implicitly set CPSR. Since it's not represented in the encoding, the
  684. // auto-generated decoder won't inject the CPSR operand. We need to fix
  685. // that as a post-pass.
  686. static void AddThumb1SBit(MCInst &MI, bool InITBlock) {
  687. const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
  688. unsigned short NumOps = ARMInsts[MI.getOpcode()].NumOperands;
  689. MCInst::iterator I = MI.begin();
  690. for (unsigned i = 0; i < NumOps; ++i, ++I) {
  691. if (I == MI.end()) break;
  692. if (OpInfo[i].isOptionalDef() && OpInfo[i].RegClass == ARM::CCRRegClassID) {
  693. if (i > 0 && OpInfo[i-1].isPredicate()) continue;
  694. MI.insert(I, MCOperand::createReg(InITBlock ? 0 : ARM::CPSR));
  695. return;
  696. }
  697. }
  698. MI.insert(I, MCOperand::createReg(InITBlock ? 0 : ARM::CPSR));
  699. }
  700. static bool isVectorPredicable(unsigned Opcode) {
  701. const MCOperandInfo *OpInfo = ARMInsts[Opcode].OpInfo;
  702. unsigned short NumOps = ARMInsts[Opcode].NumOperands;
  703. for (unsigned i = 0; i < NumOps; ++i) {
  704. if (ARM::isVpred(OpInfo[i].OperandType))
  705. return true;
  706. }
  707. return false;
  708. }
  709. // Most Thumb instructions don't have explicit predicates in the
  710. // encoding, but rather get their predicates from IT context. We need
  711. // to fix up the predicate operands using this context information as a
  712. // post-pass.
  713. MCDisassembler::DecodeStatus
  714. ARMDisassembler::AddThumbPredicate(MCInst &MI) const {
  715. MCDisassembler::DecodeStatus S = Success;
  716. const FeatureBitset &FeatureBits = getSubtargetInfo().getFeatureBits();
  717. // A few instructions actually have predicates encoded in them. Don't
  718. // try to overwrite it if we're seeing one of those.
  719. switch (MI.getOpcode()) {
  720. case ARM::tBcc:
  721. case ARM::t2Bcc:
  722. case ARM::tCBZ:
  723. case ARM::tCBNZ:
  724. case ARM::tCPS:
  725. case ARM::t2CPS3p:
  726. case ARM::t2CPS2p:
  727. case ARM::t2CPS1p:
  728. case ARM::t2CSEL:
  729. case ARM::t2CSINC:
  730. case ARM::t2CSINV:
  731. case ARM::t2CSNEG:
  732. case ARM::tMOVSr:
  733. case ARM::tSETEND:
  734. // Some instructions (mostly conditional branches) are not
  735. // allowed in IT blocks.
  736. if (ITBlock.instrInITBlock())
  737. S = SoftFail;
  738. else
  739. return Success;
  740. break;
  741. case ARM::t2HINT:
  742. if (MI.getOperand(0).getImm() == 0x10 && (FeatureBits[ARM::FeatureRAS]) != 0)
  743. S = SoftFail;
  744. break;
  745. case ARM::tB:
  746. case ARM::t2B:
  747. case ARM::t2TBB:
  748. case ARM::t2TBH:
  749. // Some instructions (mostly unconditional branches) can
  750. // only appears at the end of, or outside of, an IT.
  751. if (ITBlock.instrInITBlock() && !ITBlock.instrLastInITBlock())
  752. S = SoftFail;
  753. break;
  754. default:
  755. break;
  756. }
  757. // Warn on non-VPT predicable instruction in a VPT block and a VPT
  758. // predicable instruction in an IT block
  759. if ((!isVectorPredicable(MI.getOpcode()) && VPTBlock.instrInVPTBlock()) ||
  760. (isVectorPredicable(MI.getOpcode()) && ITBlock.instrInITBlock()))
  761. S = SoftFail;
  762. // If we're in an IT/VPT block, base the predicate on that. Otherwise,
  763. // assume a predicate of AL.
  764. unsigned CC = ARMCC::AL;
  765. unsigned VCC = ARMVCC::None;
  766. if (ITBlock.instrInITBlock()) {
  767. CC = ITBlock.getITCC();
  768. ITBlock.advanceITState();
  769. } else if (VPTBlock.instrInVPTBlock()) {
  770. VCC = VPTBlock.getVPTPred();
  771. VPTBlock.advanceVPTState();
  772. }
  773. const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
  774. unsigned short NumOps = ARMInsts[MI.getOpcode()].NumOperands;
  775. MCInst::iterator CCI = MI.begin();
  776. for (unsigned i = 0; i < NumOps; ++i, ++CCI) {
  777. if (OpInfo[i].isPredicate() || CCI == MI.end()) break;
  778. }
  779. if (ARMInsts[MI.getOpcode()].isPredicable()) {
  780. CCI = MI.insert(CCI, MCOperand::createImm(CC));
  781. ++CCI;
  782. if (CC == ARMCC::AL)
  783. MI.insert(CCI, MCOperand::createReg(0));
  784. else
  785. MI.insert(CCI, MCOperand::createReg(ARM::CPSR));
  786. } else if (CC != ARMCC::AL) {
  787. Check(S, SoftFail);
  788. }
  789. MCInst::iterator VCCI = MI.begin();
  790. unsigned VCCPos;
  791. for (VCCPos = 0; VCCPos < NumOps; ++VCCPos, ++VCCI) {
  792. if (ARM::isVpred(OpInfo[VCCPos].OperandType) || VCCI == MI.end()) break;
  793. }
  794. if (isVectorPredicable(MI.getOpcode())) {
  795. VCCI = MI.insert(VCCI, MCOperand::createImm(VCC));
  796. ++VCCI;
  797. if (VCC == ARMVCC::None)
  798. VCCI = MI.insert(VCCI, MCOperand::createReg(0));
  799. else
  800. VCCI = MI.insert(VCCI, MCOperand::createReg(ARM::P0));
  801. ++VCCI;
  802. VCCI = MI.insert(VCCI, MCOperand::createReg(0));
  803. ++VCCI;
  804. if (OpInfo[VCCPos].OperandType == ARM::OPERAND_VPRED_R) {
  805. int TiedOp = ARMInsts[MI.getOpcode()].getOperandConstraint(
  806. VCCPos + 3, MCOI::TIED_TO);
  807. assert(TiedOp >= 0 &&
  808. "Inactive register in vpred_r is not tied to an output!");
  809. // Copy the operand to ensure it's not invalidated when MI grows.
  810. MI.insert(VCCI, MCOperand(MI.getOperand(TiedOp)));
  811. }
  812. } else if (VCC != ARMVCC::None) {
  813. Check(S, SoftFail);
  814. }
  815. return S;
  816. }
  817. // Thumb VFP instructions are a special case. Because we share their
  818. // encodings between ARM and Thumb modes, and they are predicable in ARM
  819. // mode, the auto-generated decoder will give them an (incorrect)
  820. // predicate operand. We need to rewrite these operands based on the IT
  821. // context as a post-pass.
  822. void ARMDisassembler::UpdateThumbVFPPredicate(
  823. DecodeStatus &S, MCInst &MI) const {
  824. unsigned CC;
  825. CC = ITBlock.getITCC();
  826. if (CC == 0xF)
  827. CC = ARMCC::AL;
  828. if (ITBlock.instrInITBlock())
  829. ITBlock.advanceITState();
  830. else if (VPTBlock.instrInVPTBlock()) {
  831. CC = VPTBlock.getVPTPred();
  832. VPTBlock.advanceVPTState();
  833. }
  834. const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
  835. MCInst::iterator I = MI.begin();
  836. unsigned short NumOps = ARMInsts[MI.getOpcode()].NumOperands;
  837. for (unsigned i = 0; i < NumOps; ++i, ++I) {
  838. if (OpInfo[i].isPredicate() ) {
  839. if (CC != ARMCC::AL && !ARMInsts[MI.getOpcode()].isPredicable())
  840. Check(S, SoftFail);
  841. I->setImm(CC);
  842. ++I;
  843. if (CC == ARMCC::AL)
  844. I->setReg(0);
  845. else
  846. I->setReg(ARM::CPSR);
  847. return;
  848. }
  849. }
  850. }
  851. DecodeStatus ARMDisassembler::getThumbInstruction(MCInst &MI, uint64_t &Size,
  852. ArrayRef<uint8_t> Bytes,
  853. uint64_t Address,
  854. raw_ostream &CS) const {
  855. CommentStream = &CS;
  856. assert(STI.getFeatureBits()[ARM::ModeThumb] &&
  857. "Asked to disassemble in Thumb mode but Subtarget is in ARM mode!");
  858. // We want to read exactly 2 bytes of data.
  859. if (Bytes.size() < 2) {
  860. Size = 0;
  861. return MCDisassembler::Fail;
  862. }
  863. uint16_t Insn16 = (Bytes[1] << 8) | Bytes[0];
  864. DecodeStatus Result =
  865. decodeInstruction(DecoderTableThumb16, MI, Insn16, Address, this, STI);
  866. if (Result != MCDisassembler::Fail) {
  867. Size = 2;
  868. Check(Result, AddThumbPredicate(MI));
  869. return Result;
  870. }
  871. Result = decodeInstruction(DecoderTableThumbSBit16, MI, Insn16, Address, this,
  872. STI);
  873. if (Result) {
  874. Size = 2;
  875. bool InITBlock = ITBlock.instrInITBlock();
  876. Check(Result, AddThumbPredicate(MI));
  877. AddThumb1SBit(MI, InITBlock);
  878. return Result;
  879. }
  880. Result =
  881. decodeInstruction(DecoderTableThumb216, MI, Insn16, Address, this, STI);
  882. if (Result != MCDisassembler::Fail) {
  883. Size = 2;
  884. // Nested IT blocks are UNPREDICTABLE. Must be checked before we add
  885. // the Thumb predicate.
  886. if (MI.getOpcode() == ARM::t2IT && ITBlock.instrInITBlock())
  887. Result = MCDisassembler::SoftFail;
  888. Check(Result, AddThumbPredicate(MI));
  889. // If we find an IT instruction, we need to parse its condition
  890. // code and mask operands so that we can apply them correctly
  891. // to the subsequent instructions.
  892. if (MI.getOpcode() == ARM::t2IT) {
  893. unsigned Firstcond = MI.getOperand(0).getImm();
  894. unsigned Mask = MI.getOperand(1).getImm();
  895. ITBlock.setITState(Firstcond, Mask);
  896. // An IT instruction that would give a 'NV' predicate is unpredictable.
  897. if (Firstcond == ARMCC::AL && !isPowerOf2_32(Mask))
  898. CS << "unpredictable IT predicate sequence";
  899. }
  900. return Result;
  901. }
  902. // We want to read exactly 4 bytes of data.
  903. if (Bytes.size() < 4) {
  904. Size = 0;
  905. return MCDisassembler::Fail;
  906. }
  907. uint32_t Insn32 =
  908. (Bytes[3] << 8) | (Bytes[2] << 0) | (Bytes[1] << 24) | (Bytes[0] << 16);
  909. Result =
  910. decodeInstruction(DecoderTableMVE32, MI, Insn32, Address, this, STI);
  911. if (Result != MCDisassembler::Fail) {
  912. Size = 4;
  913. // Nested VPT blocks are UNPREDICTABLE. Must be checked before we add
  914. // the VPT predicate.
  915. if (isVPTOpcode(MI.getOpcode()) && VPTBlock.instrInVPTBlock())
  916. Result = MCDisassembler::SoftFail;
  917. Check(Result, AddThumbPredicate(MI));
  918. if (isVPTOpcode(MI.getOpcode())) {
  919. unsigned Mask = MI.getOperand(0).getImm();
  920. VPTBlock.setVPTState(Mask);
  921. }
  922. return Result;
  923. }
  924. Result =
  925. decodeInstruction(DecoderTableThumb32, MI, Insn32, Address, this, STI);
  926. if (Result != MCDisassembler::Fail) {
  927. Size = 4;
  928. bool InITBlock = ITBlock.instrInITBlock();
  929. Check(Result, AddThumbPredicate(MI));
  930. AddThumb1SBit(MI, InITBlock);
  931. return Result;
  932. }
  933. Result =
  934. decodeInstruction(DecoderTableThumb232, MI, Insn32, Address, this, STI);
  935. if (Result != MCDisassembler::Fail) {
  936. Size = 4;
  937. Check(Result, AddThumbPredicate(MI));
  938. return checkDecodedInstruction(MI, Size, Address, CS, Insn32, Result);
  939. }
  940. if (fieldFromInstruction(Insn32, 28, 4) == 0xE) {
  941. Result =
  942. decodeInstruction(DecoderTableVFP32, MI, Insn32, Address, this, STI);
  943. if (Result != MCDisassembler::Fail) {
  944. Size = 4;
  945. UpdateThumbVFPPredicate(Result, MI);
  946. return Result;
  947. }
  948. }
  949. Result =
  950. decodeInstruction(DecoderTableVFPV832, MI, Insn32, Address, this, STI);
  951. if (Result != MCDisassembler::Fail) {
  952. Size = 4;
  953. return Result;
  954. }
  955. if (fieldFromInstruction(Insn32, 28, 4) == 0xE) {
  956. Result = decodeInstruction(DecoderTableNEONDup32, MI, Insn32, Address, this,
  957. STI);
  958. if (Result != MCDisassembler::Fail) {
  959. Size = 4;
  960. Check(Result, AddThumbPredicate(MI));
  961. return Result;
  962. }
  963. }
  964. if (fieldFromInstruction(Insn32, 24, 8) == 0xF9) {
  965. uint32_t NEONLdStInsn = Insn32;
  966. NEONLdStInsn &= 0xF0FFFFFF;
  967. NEONLdStInsn |= 0x04000000;
  968. Result = decodeInstruction(DecoderTableNEONLoadStore32, MI, NEONLdStInsn,
  969. Address, this, STI);
  970. if (Result != MCDisassembler::Fail) {
  971. Size = 4;
  972. Check(Result, AddThumbPredicate(MI));
  973. return Result;
  974. }
  975. }
  976. if (fieldFromInstruction(Insn32, 24, 4) == 0xF) {
  977. uint32_t NEONDataInsn = Insn32;
  978. NEONDataInsn &= 0xF0FFFFFF; // Clear bits 27-24
  979. NEONDataInsn |= (NEONDataInsn & 0x10000000) >> 4; // Move bit 28 to bit 24
  980. NEONDataInsn |= 0x12000000; // Set bits 28 and 25
  981. Result = decodeInstruction(DecoderTableNEONData32, MI, NEONDataInsn,
  982. Address, this, STI);
  983. if (Result != MCDisassembler::Fail) {
  984. Size = 4;
  985. Check(Result, AddThumbPredicate(MI));
  986. return Result;
  987. }
  988. uint32_t NEONCryptoInsn = Insn32;
  989. NEONCryptoInsn &= 0xF0FFFFFF; // Clear bits 27-24
  990. NEONCryptoInsn |= (NEONCryptoInsn & 0x10000000) >> 4; // Move bit 28 to bit 24
  991. NEONCryptoInsn |= 0x12000000; // Set bits 28 and 25
  992. Result = decodeInstruction(DecoderTablev8Crypto32, MI, NEONCryptoInsn,
  993. Address, this, STI);
  994. if (Result != MCDisassembler::Fail) {
  995. Size = 4;
  996. return Result;
  997. }
  998. uint32_t NEONv8Insn = Insn32;
  999. NEONv8Insn &= 0xF3FFFFFF; // Clear bits 27-26
  1000. Result = decodeInstruction(DecoderTablev8NEON32, MI, NEONv8Insn, Address,
  1001. this, STI);
  1002. if (Result != MCDisassembler::Fail) {
  1003. Size = 4;
  1004. return Result;
  1005. }
  1006. }
  1007. uint32_t Coproc = fieldFromInstruction(Insn32, 8, 4);
  1008. const uint8_t *DecoderTable = ARM::isCDECoproc(Coproc, STI)
  1009. ? DecoderTableThumb2CDE32
  1010. : DecoderTableThumb2CoProc32;
  1011. Result =
  1012. decodeInstruction(DecoderTable, MI, Insn32, Address, this, STI);
  1013. if (Result != MCDisassembler::Fail) {
  1014. Size = 4;
  1015. Check(Result, AddThumbPredicate(MI));
  1016. return Result;
  1017. }
  1018. Size = 0;
  1019. return MCDisassembler::Fail;
  1020. }
  1021. extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeARMDisassembler() {
  1022. TargetRegistry::RegisterMCDisassembler(getTheARMLETarget(),
  1023. createARMDisassembler);
  1024. TargetRegistry::RegisterMCDisassembler(getTheARMBETarget(),
  1025. createARMDisassembler);
  1026. TargetRegistry::RegisterMCDisassembler(getTheThumbLETarget(),
  1027. createARMDisassembler);
  1028. TargetRegistry::RegisterMCDisassembler(getTheThumbBETarget(),
  1029. createARMDisassembler);
  1030. }
  1031. static const uint16_t GPRDecoderTable[] = {
  1032. ARM::R0, ARM::R1, ARM::R2, ARM::R3,
  1033. ARM::R4, ARM::R5, ARM::R6, ARM::R7,
  1034. ARM::R8, ARM::R9, ARM::R10, ARM::R11,
  1035. ARM::R12, ARM::SP, ARM::LR, ARM::PC
  1036. };
  1037. static const uint16_t CLRMGPRDecoderTable[] = {
  1038. ARM::R0, ARM::R1, ARM::R2, ARM::R3,
  1039. ARM::R4, ARM::R5, ARM::R6, ARM::R7,
  1040. ARM::R8, ARM::R9, ARM::R10, ARM::R11,
  1041. ARM::R12, 0, ARM::LR, ARM::APSR
  1042. };
  1043. static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, unsigned RegNo,
  1044. uint64_t Address, const void *Decoder) {
  1045. if (RegNo > 15)
  1046. return MCDisassembler::Fail;
  1047. unsigned Register = GPRDecoderTable[RegNo];
  1048. Inst.addOperand(MCOperand::createReg(Register));
  1049. return MCDisassembler::Success;
  1050. }
  1051. static DecodeStatus DecodeCLRMGPRRegisterClass(MCInst &Inst, unsigned RegNo,
  1052. uint64_t Address,
  1053. const void *Decoder) {
  1054. if (RegNo > 15)
  1055. return MCDisassembler::Fail;
  1056. unsigned Register = CLRMGPRDecoderTable[RegNo];
  1057. if (Register == 0)
  1058. return MCDisassembler::Fail;
  1059. Inst.addOperand(MCOperand::createReg(Register));
  1060. return MCDisassembler::Success;
  1061. }
  1062. static DecodeStatus
  1063. DecodeGPRnopcRegisterClass(MCInst &Inst, unsigned RegNo,
  1064. uint64_t Address, const void *Decoder) {
  1065. DecodeStatus S = MCDisassembler::Success;
  1066. if (RegNo == 15)
  1067. S = MCDisassembler::SoftFail;
  1068. Check(S, DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder));
  1069. return S;
  1070. }
  1071. static DecodeStatus DecodeGPRnospRegisterClass(MCInst &Inst, unsigned RegNo,
  1072. uint64_t Address,
  1073. const void *Decoder) {
  1074. DecodeStatus S = MCDisassembler::Success;
  1075. if (RegNo == 13)
  1076. S = MCDisassembler::SoftFail;
  1077. Check(S, DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder));
  1078. return S;
  1079. }
  1080. static DecodeStatus
  1081. DecodeGPRwithAPSRRegisterClass(MCInst &Inst, unsigned RegNo,
  1082. uint64_t Address, const void *Decoder) {
  1083. DecodeStatus S = MCDisassembler::Success;
  1084. if (RegNo == 15)
  1085. {
  1086. Inst.addOperand(MCOperand::createReg(ARM::APSR_NZCV));
  1087. return MCDisassembler::Success;
  1088. }
  1089. Check(S, DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder));
  1090. return S;
  1091. }
  1092. static DecodeStatus
  1093. DecodeGPRwithZRRegisterClass(MCInst &Inst, unsigned RegNo,
  1094. uint64_t Address, const void *Decoder) {
  1095. DecodeStatus S = MCDisassembler::Success;
  1096. if (RegNo == 15)
  1097. {
  1098. Inst.addOperand(MCOperand::createReg(ARM::ZR));
  1099. return MCDisassembler::Success;
  1100. }
  1101. if (RegNo == 13)
  1102. Check(S, MCDisassembler::SoftFail);
  1103. Check(S, DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder));
  1104. return S;
  1105. }
  1106. static DecodeStatus
  1107. DecodeGPRwithZRnospRegisterClass(MCInst &Inst, unsigned RegNo,
  1108. uint64_t Address, const void *Decoder) {
  1109. DecodeStatus S = MCDisassembler::Success;
  1110. if (RegNo == 13)
  1111. return MCDisassembler::Fail;
  1112. Check(S, DecodeGPRwithZRRegisterClass(Inst, RegNo, Address, Decoder));
  1113. return S;
  1114. }
  1115. static DecodeStatus DecodetGPRRegisterClass(MCInst &Inst, unsigned RegNo,
  1116. uint64_t Address, const void *Decoder) {
  1117. if (RegNo > 7)
  1118. return MCDisassembler::Fail;
  1119. return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
  1120. }
  1121. static const uint16_t GPRPairDecoderTable[] = {
  1122. ARM::R0_R1, ARM::R2_R3, ARM::R4_R5, ARM::R6_R7,
  1123. ARM::R8_R9, ARM::R10_R11, ARM::R12_SP
  1124. };
  1125. static DecodeStatus DecodeGPRPairRegisterClass(MCInst &Inst, unsigned RegNo,
  1126. uint64_t Address, const void *Decoder) {
  1127. DecodeStatus S = MCDisassembler::Success;
  1128. // According to the Arm ARM RegNo = 14 is undefined, but we return fail
  1129. // rather than SoftFail as there is no GPRPair table entry for index 7.
  1130. if (RegNo > 13)
  1131. return MCDisassembler::Fail;
  1132. if (RegNo & 1)
  1133. S = MCDisassembler::SoftFail;
  1134. unsigned RegisterPair = GPRPairDecoderTable[RegNo/2];
  1135. Inst.addOperand(MCOperand::createReg(RegisterPair));
  1136. return S;
  1137. }
  1138. static DecodeStatus DecodeGPRPairnospRegisterClass(MCInst &Inst, unsigned RegNo,
  1139. uint64_t Address, const void *Decoder) {
  1140. if (RegNo > 13)
  1141. return MCDisassembler::Fail;
  1142. unsigned RegisterPair = GPRPairDecoderTable[RegNo/2];
  1143. Inst.addOperand(MCOperand::createReg(RegisterPair));
  1144. if ((RegNo & 1) || RegNo > 10)
  1145. return MCDisassembler::SoftFail;
  1146. return MCDisassembler::Success;
  1147. }
  1148. static DecodeStatus DecodeGPRspRegisterClass(MCInst &Inst, unsigned RegNo,
  1149. uint64_t Address,
  1150. const void *Decoder) {
  1151. if (RegNo != 13)
  1152. return MCDisassembler::Fail;
  1153. unsigned Register = GPRDecoderTable[RegNo];
  1154. Inst.addOperand(MCOperand::createReg(Register));
  1155. return MCDisassembler::Success;
  1156. }
  1157. static DecodeStatus DecodetcGPRRegisterClass(MCInst &Inst, unsigned RegNo,
  1158. uint64_t Address, const void *Decoder) {
  1159. unsigned Register = 0;
  1160. switch (RegNo) {
  1161. case 0:
  1162. Register = ARM::R0;
  1163. break;
  1164. case 1:
  1165. Register = ARM::R1;
  1166. break;
  1167. case 2:
  1168. Register = ARM::R2;
  1169. break;
  1170. case 3:
  1171. Register = ARM::R3;
  1172. break;
  1173. case 9:
  1174. Register = ARM::R9;
  1175. break;
  1176. case 12:
  1177. Register = ARM::R12;
  1178. break;
  1179. default:
  1180. return MCDisassembler::Fail;
  1181. }
  1182. Inst.addOperand(MCOperand::createReg(Register));
  1183. return MCDisassembler::Success;
  1184. }
  1185. static DecodeStatus DecoderGPRRegisterClass(MCInst &Inst, unsigned RegNo,
  1186. uint64_t Address, const void *Decoder) {
  1187. DecodeStatus S = MCDisassembler::Success;
  1188. const FeatureBitset &featureBits =
  1189. ((const MCDisassembler*)Decoder)->getSubtargetInfo().getFeatureBits();
  1190. if ((RegNo == 13 && !featureBits[ARM::HasV8Ops]) || RegNo == 15)
  1191. S = MCDisassembler::SoftFail;
  1192. Check(S, DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder));
  1193. return S;
  1194. }
  1195. static const uint16_t SPRDecoderTable[] = {
  1196. ARM::S0, ARM::S1, ARM::S2, ARM::S3,
  1197. ARM::S4, ARM::S5, ARM::S6, ARM::S7,
  1198. ARM::S8, ARM::S9, ARM::S10, ARM::S11,
  1199. ARM::S12, ARM::S13, ARM::S14, ARM::S15,
  1200. ARM::S16, ARM::S17, ARM::S18, ARM::S19,
  1201. ARM::S20, ARM::S21, ARM::S22, ARM::S23,
  1202. ARM::S24, ARM::S25, ARM::S26, ARM::S27,
  1203. ARM::S28, ARM::S29, ARM::S30, ARM::S31
  1204. };
  1205. static DecodeStatus DecodeSPRRegisterClass(MCInst &Inst, unsigned RegNo,
  1206. uint64_t Address, const void *Decoder) {
  1207. if (RegNo > 31)
  1208. return MCDisassembler::Fail;
  1209. unsigned Register = SPRDecoderTable[RegNo];
  1210. Inst.addOperand(MCOperand::createReg(Register));
  1211. return MCDisassembler::Success;
  1212. }
  1213. static DecodeStatus DecodeHPRRegisterClass(MCInst &Inst, unsigned RegNo,
  1214. uint64_t Address, const void *Decoder) {
  1215. return DecodeSPRRegisterClass(Inst, RegNo, Address, Decoder);
  1216. }
  1217. static const uint16_t DPRDecoderTable[] = {
  1218. ARM::D0, ARM::D1, ARM::D2, ARM::D3,
  1219. ARM::D4, ARM::D5, ARM::D6, ARM::D7,
  1220. ARM::D8, ARM::D9, ARM::D10, ARM::D11,
  1221. ARM::D12, ARM::D13, ARM::D14, ARM::D15,
  1222. ARM::D16, ARM::D17, ARM::D18, ARM::D19,
  1223. ARM::D20, ARM::D21, ARM::D22, ARM::D23,
  1224. ARM::D24, ARM::D25, ARM::D26, ARM::D27,
  1225. ARM::D28, ARM::D29, ARM::D30, ARM::D31
  1226. };
  1227. static DecodeStatus DecodeDPRRegisterClass(MCInst &Inst, unsigned RegNo,
  1228. uint64_t Address, const void *Decoder) {
  1229. const FeatureBitset &featureBits =
  1230. ((const MCDisassembler*)Decoder)->getSubtargetInfo().getFeatureBits();
  1231. bool hasD32 = featureBits[ARM::FeatureD32];
  1232. if (RegNo > 31 || (!hasD32 && RegNo > 15))
  1233. return MCDisassembler::Fail;
  1234. unsigned Register = DPRDecoderTable[RegNo];
  1235. Inst.addOperand(MCOperand::createReg(Register));
  1236. return MCDisassembler::Success;
  1237. }
  1238. static DecodeStatus DecodeDPR_8RegisterClass(MCInst &Inst, unsigned RegNo,
  1239. uint64_t Address, const void *Decoder) {
  1240. if (RegNo > 7)
  1241. return MCDisassembler::Fail;
  1242. return DecodeDPRRegisterClass(Inst, RegNo, Address, Decoder);
  1243. }
  1244. static DecodeStatus DecodeSPR_8RegisterClass(MCInst &Inst, unsigned RegNo,
  1245. uint64_t Address, const void *Decoder) {
  1246. if (RegNo > 15)
  1247. return MCDisassembler::Fail;
  1248. return DecodeSPRRegisterClass(Inst, RegNo, Address, Decoder);
  1249. }
  1250. static DecodeStatus
  1251. DecodeDPR_VFP2RegisterClass(MCInst &Inst, unsigned RegNo,
  1252. uint64_t Address, const void *Decoder) {
  1253. if (RegNo > 15)
  1254. return MCDisassembler::Fail;
  1255. return DecodeDPRRegisterClass(Inst, RegNo, Address, Decoder);
  1256. }
  1257. static const uint16_t QPRDecoderTable[] = {
  1258. ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
  1259. ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7,
  1260. ARM::Q8, ARM::Q9, ARM::Q10, ARM::Q11,
  1261. ARM::Q12, ARM::Q13, ARM::Q14, ARM::Q15
  1262. };
  1263. static DecodeStatus DecodeQPRRegisterClass(MCInst &Inst, unsigned RegNo,
  1264. uint64_t Address, const void *Decoder) {
  1265. if (RegNo > 31 || (RegNo & 1) != 0)
  1266. return MCDisassembler::Fail;
  1267. RegNo >>= 1;
  1268. unsigned Register = QPRDecoderTable[RegNo];
  1269. Inst.addOperand(MCOperand::createReg(Register));
  1270. return MCDisassembler::Success;
  1271. }
  1272. static const uint16_t DPairDecoderTable[] = {
  1273. ARM::Q0, ARM::D1_D2, ARM::Q1, ARM::D3_D4, ARM::Q2, ARM::D5_D6,
  1274. ARM::Q3, ARM::D7_D8, ARM::Q4, ARM::D9_D10, ARM::Q5, ARM::D11_D12,
  1275. ARM::Q6, ARM::D13_D14, ARM::Q7, ARM::D15_D16, ARM::Q8, ARM::D17_D18,
  1276. ARM::Q9, ARM::D19_D20, ARM::Q10, ARM::D21_D22, ARM::Q11, ARM::D23_D24,
  1277. ARM::Q12, ARM::D25_D26, ARM::Q13, ARM::D27_D28, ARM::Q14, ARM::D29_D30,
  1278. ARM::Q15
  1279. };
  1280. static DecodeStatus DecodeDPairRegisterClass(MCInst &Inst, unsigned RegNo,
  1281. uint64_t Address, const void *Decoder) {
  1282. if (RegNo > 30)
  1283. return MCDisassembler::Fail;
  1284. unsigned Register = DPairDecoderTable[RegNo];
  1285. Inst.addOperand(MCOperand::createReg(Register));
  1286. return MCDisassembler::Success;
  1287. }
  1288. static const uint16_t DPairSpacedDecoderTable[] = {
  1289. ARM::D0_D2, ARM::D1_D3, ARM::D2_D4, ARM::D3_D5,
  1290. ARM::D4_D6, ARM::D5_D7, ARM::D6_D8, ARM::D7_D9,
  1291. ARM::D8_D10, ARM::D9_D11, ARM::D10_D12, ARM::D11_D13,
  1292. ARM::D12_D14, ARM::D13_D15, ARM::D14_D16, ARM::D15_D17,
  1293. ARM::D16_D18, ARM::D17_D19, ARM::D18_D20, ARM::D19_D21,
  1294. ARM::D20_D22, ARM::D21_D23, ARM::D22_D24, ARM::D23_D25,
  1295. ARM::D24_D26, ARM::D25_D27, ARM::D26_D28, ARM::D27_D29,
  1296. ARM::D28_D30, ARM::D29_D31
  1297. };
  1298. static DecodeStatus DecodeDPairSpacedRegisterClass(MCInst &Inst,
  1299. unsigned RegNo,
  1300. uint64_t Address,
  1301. const void *Decoder) {
  1302. if (RegNo > 29)
  1303. return MCDisassembler::Fail;
  1304. unsigned Register = DPairSpacedDecoderTable[RegNo];
  1305. Inst.addOperand(MCOperand::createReg(Register));
  1306. return MCDisassembler::Success;
  1307. }
  1308. static DecodeStatus DecodePredicateOperand(MCInst &Inst, unsigned Val,
  1309. uint64_t Address, const void *Decoder) {
  1310. DecodeStatus S = MCDisassembler::Success;
  1311. if (Val == 0xF) return MCDisassembler::Fail;
  1312. // AL predicate is not allowed on Thumb1 branches.
  1313. if (Inst.getOpcode() == ARM::tBcc && Val == 0xE)
  1314. return MCDisassembler::Fail;
  1315. if (Val != ARMCC::AL && !ARMInsts[Inst.getOpcode()].isPredicable())
  1316. Check(S, MCDisassembler::SoftFail);
  1317. Inst.addOperand(MCOperand::createImm(Val));
  1318. if (Val == ARMCC::AL) {
  1319. Inst.addOperand(MCOperand::createReg(0));
  1320. } else
  1321. Inst.addOperand(MCOperand::createReg(ARM::CPSR));
  1322. return S;
  1323. }
  1324. static DecodeStatus DecodeCCOutOperand(MCInst &Inst, unsigned Val,
  1325. uint64_t Address, const void *Decoder) {
  1326. if (Val)
  1327. Inst.addOperand(MCOperand::createReg(ARM::CPSR));
  1328. else
  1329. Inst.addOperand(MCOperand::createReg(0));
  1330. return MCDisassembler::Success;
  1331. }
  1332. static DecodeStatus DecodeSORegImmOperand(MCInst &Inst, unsigned Val,
  1333. uint64_t Address, const void *Decoder) {
  1334. DecodeStatus S = MCDisassembler::Success;
  1335. unsigned Rm = fieldFromInstruction(Val, 0, 4);
  1336. unsigned type = fieldFromInstruction(Val, 5, 2);
  1337. unsigned imm = fieldFromInstruction(Val, 7, 5);
  1338. // Register-immediate
  1339. if (!Check(S, DecoderGPRRegisterClass(Inst, Rm, Address, Decoder)))
  1340. return MCDisassembler::Fail;
  1341. ARM_AM::ShiftOpc Shift = ARM_AM::lsl;
  1342. switch (type) {
  1343. case 0:
  1344. Shift = ARM_AM::lsl;
  1345. break;
  1346. case 1:
  1347. Shift = ARM_AM::lsr;
  1348. break;
  1349. case 2:
  1350. Shift = ARM_AM::asr;
  1351. break;
  1352. case 3:
  1353. Shift = ARM_AM::ror;
  1354. break;
  1355. }
  1356. if (Shift == ARM_AM::ror && imm == 0)
  1357. Shift = ARM_AM::rrx;
  1358. unsigned Op = Shift | (imm << 3);
  1359. Inst.addOperand(MCOperand::createImm(Op));
  1360. return S;
  1361. }
  1362. static DecodeStatus DecodeSORegRegOperand(MCInst &Inst, unsigned Val,
  1363. uint64_t Address, const void *Decoder) {
  1364. DecodeStatus S = MCDisassembler::Success;
  1365. unsigned Rm = fieldFromInstruction(Val, 0, 4);
  1366. unsigned type = fieldFromInstruction(Val, 5, 2);
  1367. unsigned Rs = fieldFromInstruction(Val, 8, 4);
  1368. // Register-register
  1369. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder)))
  1370. return MCDisassembler::Fail;
  1371. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rs, Address, Decoder)))
  1372. return MCDisassembler::Fail;
  1373. ARM_AM::ShiftOpc Shift = ARM_AM::lsl;
  1374. switch (type) {
  1375. case 0:
  1376. Shift = ARM_AM::lsl;
  1377. break;
  1378. case 1:
  1379. Shift = ARM_AM::lsr;
  1380. break;
  1381. case 2:
  1382. Shift = ARM_AM::asr;
  1383. break;
  1384. case 3:
  1385. Shift = ARM_AM::ror;
  1386. break;
  1387. }
  1388. Inst.addOperand(MCOperand::createImm(Shift));
  1389. return S;
  1390. }
  1391. static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Val,
  1392. uint64_t Address, const void *Decoder) {
  1393. DecodeStatus S = MCDisassembler::Success;
  1394. bool NeedDisjointWriteback = false;
  1395. unsigned WritebackReg = 0;
  1396. bool CLRM = false;
  1397. switch (Inst.getOpcode()) {
  1398. default:
  1399. break;
  1400. case ARM::LDMIA_UPD:
  1401. case ARM::LDMDB_UPD:
  1402. case ARM::LDMIB_UPD:
  1403. case ARM::LDMDA_UPD:
  1404. case ARM::t2LDMIA_UPD:
  1405. case ARM::t2LDMDB_UPD:
  1406. case ARM::t2STMIA_UPD:
  1407. case ARM::t2STMDB_UPD:
  1408. NeedDisjointWriteback = true;
  1409. WritebackReg = Inst.getOperand(0).getReg();
  1410. break;
  1411. case ARM::t2CLRM:
  1412. CLRM = true;
  1413. break;
  1414. }
  1415. // Empty register lists are not allowed.
  1416. if (Val == 0) return MCDisassembler::Fail;
  1417. for (unsigned i = 0; i < 16; ++i) {
  1418. if (Val & (1 << i)) {
  1419. if (CLRM) {
  1420. if (!Check(S, DecodeCLRMGPRRegisterClass(Inst, i, Address, Decoder))) {
  1421. return MCDisassembler::Fail;
  1422. }
  1423. } else {
  1424. if (!Check(S, DecodeGPRRegisterClass(Inst, i, Address, Decoder)))
  1425. return MCDisassembler::Fail;
  1426. // Writeback not allowed if Rn is in the target list.
  1427. if (NeedDisjointWriteback && WritebackReg == Inst.end()[-1].getReg())
  1428. Check(S, MCDisassembler::SoftFail);
  1429. }
  1430. }
  1431. }
  1432. return S;
  1433. }
  1434. static DecodeStatus DecodeSPRRegListOperand(MCInst &Inst, unsigned Val,
  1435. uint64_t Address, const void *Decoder) {
  1436. DecodeStatus S = MCDisassembler::Success;
  1437. unsigned Vd = fieldFromInstruction(Val, 8, 5);
  1438. unsigned regs = fieldFromInstruction(Val, 0, 8);
  1439. // In case of unpredictable encoding, tweak the operands.
  1440. if (regs == 0 || (Vd + regs) > 32) {
  1441. regs = Vd + regs > 32 ? 32 - Vd : regs;
  1442. regs = std::max( 1u, regs);
  1443. S = MCDisassembler::SoftFail;
  1444. }
  1445. if (!Check(S, DecodeSPRRegisterClass(Inst, Vd, Address, Decoder)))
  1446. return MCDisassembler::Fail;
  1447. for (unsigned i = 0; i < (regs - 1); ++i) {
  1448. if (!Check(S, DecodeSPRRegisterClass(Inst, ++Vd, Address, Decoder)))
  1449. return MCDisassembler::Fail;
  1450. }
  1451. return S;
  1452. }
  1453. static DecodeStatus DecodeDPRRegListOperand(MCInst &Inst, unsigned Val,
  1454. uint64_t Address, const void *Decoder) {
  1455. DecodeStatus S = MCDisassembler::Success;
  1456. unsigned Vd = fieldFromInstruction(Val, 8, 5);
  1457. unsigned regs = fieldFromInstruction(Val, 1, 7);
  1458. // In case of unpredictable encoding, tweak the operands.
  1459. if (regs == 0 || regs > 16 || (Vd + regs) > 32) {
  1460. regs = Vd + regs > 32 ? 32 - Vd : regs;
  1461. regs = std::max( 1u, regs);
  1462. regs = std::min(16u, regs);
  1463. S = MCDisassembler::SoftFail;
  1464. }
  1465. if (!Check(S, DecodeDPRRegisterClass(Inst, Vd, Address, Decoder)))
  1466. return MCDisassembler::Fail;
  1467. for (unsigned i = 0; i < (regs - 1); ++i) {
  1468. if (!Check(S, DecodeDPRRegisterClass(Inst, ++Vd, Address, Decoder)))
  1469. return MCDisassembler::Fail;
  1470. }
  1471. return S;
  1472. }
  1473. static DecodeStatus DecodeBitfieldMaskOperand(MCInst &Inst, unsigned Val,
  1474. uint64_t Address, const void *Decoder) {
  1475. // This operand encodes a mask of contiguous zeros between a specified MSB
  1476. // and LSB. To decode it, we create the mask of all bits MSB-and-lower,
  1477. // the mask of all bits LSB-and-lower, and then xor them to create
  1478. // the mask of that's all ones on [msb, lsb]. Finally we not it to
  1479. // create the final mask.
  1480. unsigned msb = fieldFromInstruction(Val, 5, 5);
  1481. unsigned lsb = fieldFromInstruction(Val, 0, 5);
  1482. DecodeStatus S = MCDisassembler::Success;
  1483. if (lsb > msb) {
  1484. Check(S, MCDisassembler::SoftFail);
  1485. // The check above will cause the warning for the "potentially undefined
  1486. // instruction encoding" but we can't build a bad MCOperand value here
  1487. // with a lsb > msb or else printing the MCInst will cause a crash.
  1488. lsb = msb;
  1489. }
  1490. uint32_t msb_mask = 0xFFFFFFFF;
  1491. if (msb != 31) msb_mask = (1U << (msb+1)) - 1;
  1492. uint32_t lsb_mask = (1U << lsb) - 1;
  1493. Inst.addOperand(MCOperand::createImm(~(msb_mask ^ lsb_mask)));
  1494. return S;
  1495. }
  1496. static DecodeStatus DecodeCopMemInstruction(MCInst &Inst, unsigned Insn,
  1497. uint64_t Address, const void *Decoder) {
  1498. DecodeStatus S = MCDisassembler::Success;
  1499. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  1500. unsigned CRd = fieldFromInstruction(Insn, 12, 4);
  1501. unsigned coproc = fieldFromInstruction(Insn, 8, 4);
  1502. unsigned imm = fieldFromInstruction(Insn, 0, 8);
  1503. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  1504. unsigned U = fieldFromInstruction(Insn, 23, 1);
  1505. const FeatureBitset &featureBits =
  1506. ((const MCDisassembler*)Decoder)->getSubtargetInfo().getFeatureBits();
  1507. switch (Inst.getOpcode()) {
  1508. case ARM::LDC_OFFSET:
  1509. case ARM::LDC_PRE:
  1510. case ARM::LDC_POST:
  1511. case ARM::LDC_OPTION:
  1512. case ARM::LDCL_OFFSET:
  1513. case ARM::LDCL_PRE:
  1514. case ARM::LDCL_POST:
  1515. case ARM::LDCL_OPTION:
  1516. case ARM::STC_OFFSET:
  1517. case ARM::STC_PRE:
  1518. case ARM::STC_POST:
  1519. case ARM::STC_OPTION:
  1520. case ARM::STCL_OFFSET:
  1521. case ARM::STCL_PRE:
  1522. case ARM::STCL_POST:
  1523. case ARM::STCL_OPTION:
  1524. case ARM::t2LDC_OFFSET:
  1525. case ARM::t2LDC_PRE:
  1526. case ARM::t2LDC_POST:
  1527. case ARM::t2LDC_OPTION:
  1528. case ARM::t2LDCL_OFFSET:
  1529. case ARM::t2LDCL_PRE:
  1530. case ARM::t2LDCL_POST:
  1531. case ARM::t2LDCL_OPTION:
  1532. case ARM::t2STC_OFFSET:
  1533. case ARM::t2STC_PRE:
  1534. case ARM::t2STC_POST:
  1535. case ARM::t2STC_OPTION:
  1536. case ARM::t2STCL_OFFSET:
  1537. case ARM::t2STCL_PRE:
  1538. case ARM::t2STCL_POST:
  1539. case ARM::t2STCL_OPTION:
  1540. case ARM::t2LDC2_OFFSET:
  1541. case ARM::t2LDC2L_OFFSET:
  1542. case ARM::t2LDC2_PRE:
  1543. case ARM::t2LDC2L_PRE:
  1544. case ARM::t2STC2_OFFSET:
  1545. case ARM::t2STC2L_OFFSET:
  1546. case ARM::t2STC2_PRE:
  1547. case ARM::t2STC2L_PRE:
  1548. case ARM::LDC2_OFFSET:
  1549. case ARM::LDC2L_OFFSET:
  1550. case ARM::LDC2_PRE:
  1551. case ARM::LDC2L_PRE:
  1552. case ARM::STC2_OFFSET:
  1553. case ARM::STC2L_OFFSET:
  1554. case ARM::STC2_PRE:
  1555. case ARM::STC2L_PRE:
  1556. case ARM::t2LDC2_OPTION:
  1557. case ARM::t2STC2_OPTION:
  1558. case ARM::t2LDC2_POST:
  1559. case ARM::t2LDC2L_POST:
  1560. case ARM::t2STC2_POST:
  1561. case ARM::t2STC2L_POST:
  1562. case ARM::LDC2_POST:
  1563. case ARM::LDC2L_POST:
  1564. case ARM::STC2_POST:
  1565. case ARM::STC2L_POST:
  1566. if (coproc == 0xA || coproc == 0xB ||
  1567. (featureBits[ARM::HasV8_1MMainlineOps] &&
  1568. (coproc == 0x8 || coproc == 0x9 || coproc == 0xA || coproc == 0xB ||
  1569. coproc == 0xE || coproc == 0xF)))
  1570. return MCDisassembler::Fail;
  1571. break;
  1572. default:
  1573. break;
  1574. }
  1575. if (featureBits[ARM::HasV8Ops] && (coproc != 14))
  1576. return MCDisassembler::Fail;
  1577. Inst.addOperand(MCOperand::createImm(coproc));
  1578. Inst.addOperand(MCOperand::createImm(CRd));
  1579. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  1580. return MCDisassembler::Fail;
  1581. switch (Inst.getOpcode()) {
  1582. case ARM::t2LDC2_OFFSET:
  1583. case ARM::t2LDC2L_OFFSET:
  1584. case ARM::t2LDC2_PRE:
  1585. case ARM::t2LDC2L_PRE:
  1586. case ARM::t2STC2_OFFSET:
  1587. case ARM::t2STC2L_OFFSET:
  1588. case ARM::t2STC2_PRE:
  1589. case ARM::t2STC2L_PRE:
  1590. case ARM::LDC2_OFFSET:
  1591. case ARM::LDC2L_OFFSET:
  1592. case ARM::LDC2_PRE:
  1593. case ARM::LDC2L_PRE:
  1594. case ARM::STC2_OFFSET:
  1595. case ARM::STC2L_OFFSET:
  1596. case ARM::STC2_PRE:
  1597. case ARM::STC2L_PRE:
  1598. case ARM::t2LDC_OFFSET:
  1599. case ARM::t2LDCL_OFFSET:
  1600. case ARM::t2LDC_PRE:
  1601. case ARM::t2LDCL_PRE:
  1602. case ARM::t2STC_OFFSET:
  1603. case ARM::t2STCL_OFFSET:
  1604. case ARM::t2STC_PRE:
  1605. case ARM::t2STCL_PRE:
  1606. case ARM::LDC_OFFSET:
  1607. case ARM::LDCL_OFFSET:
  1608. case ARM::LDC_PRE:
  1609. case ARM::LDCL_PRE:
  1610. case ARM::STC_OFFSET:
  1611. case ARM::STCL_OFFSET:
  1612. case ARM::STC_PRE:
  1613. case ARM::STCL_PRE:
  1614. imm = ARM_AM::getAM5Opc(U ? ARM_AM::add : ARM_AM::sub, imm);
  1615. Inst.addOperand(MCOperand::createImm(imm));
  1616. break;
  1617. case ARM::t2LDC2_POST:
  1618. case ARM::t2LDC2L_POST:
  1619. case ARM::t2STC2_POST:
  1620. case ARM::t2STC2L_POST:
  1621. case ARM::LDC2_POST:
  1622. case ARM::LDC2L_POST:
  1623. case ARM::STC2_POST:
  1624. case ARM::STC2L_POST:
  1625. case ARM::t2LDC_POST:
  1626. case ARM::t2LDCL_POST:
  1627. case ARM::t2STC_POST:
  1628. case ARM::t2STCL_POST:
  1629. case ARM::LDC_POST:
  1630. case ARM::LDCL_POST:
  1631. case ARM::STC_POST:
  1632. case ARM::STCL_POST:
  1633. imm |= U << 8;
  1634. LLVM_FALLTHROUGH;
  1635. default:
  1636. // The 'option' variant doesn't encode 'U' in the immediate since
  1637. // the immediate is unsigned [0,255].
  1638. Inst.addOperand(MCOperand::createImm(imm));
  1639. break;
  1640. }
  1641. switch (Inst.getOpcode()) {
  1642. case ARM::LDC_OFFSET:
  1643. case ARM::LDC_PRE:
  1644. case ARM::LDC_POST:
  1645. case ARM::LDC_OPTION:
  1646. case ARM::LDCL_OFFSET:
  1647. case ARM::LDCL_PRE:
  1648. case ARM::LDCL_POST:
  1649. case ARM::LDCL_OPTION:
  1650. case ARM::STC_OFFSET:
  1651. case ARM::STC_PRE:
  1652. case ARM::STC_POST:
  1653. case ARM::STC_OPTION:
  1654. case ARM::STCL_OFFSET:
  1655. case ARM::STCL_PRE:
  1656. case ARM::STCL_POST:
  1657. case ARM::STCL_OPTION:
  1658. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  1659. return MCDisassembler::Fail;
  1660. break;
  1661. default:
  1662. break;
  1663. }
  1664. return S;
  1665. }
  1666. static DecodeStatus
  1667. DecodeAddrMode2IdxInstruction(MCInst &Inst, unsigned Insn,
  1668. uint64_t Address, const void *Decoder) {
  1669. DecodeStatus S = MCDisassembler::Success;
  1670. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  1671. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  1672. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  1673. unsigned imm = fieldFromInstruction(Insn, 0, 12);
  1674. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  1675. unsigned reg = fieldFromInstruction(Insn, 25, 1);
  1676. unsigned P = fieldFromInstruction(Insn, 24, 1);
  1677. unsigned W = fieldFromInstruction(Insn, 21, 1);
  1678. // On stores, the writeback operand precedes Rt.
  1679. switch (Inst.getOpcode()) {
  1680. case ARM::STR_POST_IMM:
  1681. case ARM::STR_POST_REG:
  1682. case ARM::STRB_POST_IMM:
  1683. case ARM::STRB_POST_REG:
  1684. case ARM::STRT_POST_REG:
  1685. case ARM::STRT_POST_IMM:
  1686. case ARM::STRBT_POST_REG:
  1687. case ARM::STRBT_POST_IMM:
  1688. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  1689. return MCDisassembler::Fail;
  1690. break;
  1691. default:
  1692. break;
  1693. }
  1694. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
  1695. return MCDisassembler::Fail;
  1696. // On loads, the writeback operand comes after Rt.
  1697. switch (Inst.getOpcode()) {
  1698. case ARM::LDR_POST_IMM:
  1699. case ARM::LDR_POST_REG:
  1700. case ARM::LDRB_POST_IMM:
  1701. case ARM::LDRB_POST_REG:
  1702. case ARM::LDRBT_POST_REG:
  1703. case ARM::LDRBT_POST_IMM:
  1704. case ARM::LDRT_POST_REG:
  1705. case ARM::LDRT_POST_IMM:
  1706. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  1707. return MCDisassembler::Fail;
  1708. break;
  1709. default:
  1710. break;
  1711. }
  1712. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  1713. return MCDisassembler::Fail;
  1714. ARM_AM::AddrOpc Op = ARM_AM::add;
  1715. if (!fieldFromInstruction(Insn, 23, 1))
  1716. Op = ARM_AM::sub;
  1717. bool writeback = (P == 0) || (W == 1);
  1718. unsigned idx_mode = 0;
  1719. if (P && writeback)
  1720. idx_mode = ARMII::IndexModePre;
  1721. else if (!P && writeback)
  1722. idx_mode = ARMII::IndexModePost;
  1723. if (writeback && (Rn == 15 || Rn == Rt))
  1724. S = MCDisassembler::SoftFail; // UNPREDICTABLE
  1725. if (reg) {
  1726. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder)))
  1727. return MCDisassembler::Fail;
  1728. ARM_AM::ShiftOpc Opc = ARM_AM::lsl;
  1729. switch( fieldFromInstruction(Insn, 5, 2)) {
  1730. case 0:
  1731. Opc = ARM_AM::lsl;
  1732. break;
  1733. case 1:
  1734. Opc = ARM_AM::lsr;
  1735. break;
  1736. case 2:
  1737. Opc = ARM_AM::asr;
  1738. break;
  1739. case 3:
  1740. Opc = ARM_AM::ror;
  1741. break;
  1742. default:
  1743. return MCDisassembler::Fail;
  1744. }
  1745. unsigned amt = fieldFromInstruction(Insn, 7, 5);
  1746. if (Opc == ARM_AM::ror && amt == 0)
  1747. Opc = ARM_AM::rrx;
  1748. unsigned imm = ARM_AM::getAM2Opc(Op, amt, Opc, idx_mode);
  1749. Inst.addOperand(MCOperand::createImm(imm));
  1750. } else {
  1751. Inst.addOperand(MCOperand::createReg(0));
  1752. unsigned tmp = ARM_AM::getAM2Opc(Op, imm, ARM_AM::lsl, idx_mode);
  1753. Inst.addOperand(MCOperand::createImm(tmp));
  1754. }
  1755. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  1756. return MCDisassembler::Fail;
  1757. return S;
  1758. }
  1759. static DecodeStatus DecodeSORegMemOperand(MCInst &Inst, unsigned Val,
  1760. uint64_t Address, const void *Decoder) {
  1761. DecodeStatus S = MCDisassembler::Success;
  1762. unsigned Rn = fieldFromInstruction(Val, 13, 4);
  1763. unsigned Rm = fieldFromInstruction(Val, 0, 4);
  1764. unsigned type = fieldFromInstruction(Val, 5, 2);
  1765. unsigned imm = fieldFromInstruction(Val, 7, 5);
  1766. unsigned U = fieldFromInstruction(Val, 12, 1);
  1767. ARM_AM::ShiftOpc ShOp = ARM_AM::lsl;
  1768. switch (type) {
  1769. case 0:
  1770. ShOp = ARM_AM::lsl;
  1771. break;
  1772. case 1:
  1773. ShOp = ARM_AM::lsr;
  1774. break;
  1775. case 2:
  1776. ShOp = ARM_AM::asr;
  1777. break;
  1778. case 3:
  1779. ShOp = ARM_AM::ror;
  1780. break;
  1781. }
  1782. if (ShOp == ARM_AM::ror && imm == 0)
  1783. ShOp = ARM_AM::rrx;
  1784. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  1785. return MCDisassembler::Fail;
  1786. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  1787. return MCDisassembler::Fail;
  1788. unsigned shift;
  1789. if (U)
  1790. shift = ARM_AM::getAM2Opc(ARM_AM::add, imm, ShOp);
  1791. else
  1792. shift = ARM_AM::getAM2Opc(ARM_AM::sub, imm, ShOp);
  1793. Inst.addOperand(MCOperand::createImm(shift));
  1794. return S;
  1795. }
  1796. static DecodeStatus
  1797. DecodeAddrMode3Instruction(MCInst &Inst, unsigned Insn,
  1798. uint64_t Address, const void *Decoder) {
  1799. DecodeStatus S = MCDisassembler::Success;
  1800. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  1801. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  1802. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  1803. unsigned type = fieldFromInstruction(Insn, 22, 1);
  1804. unsigned imm = fieldFromInstruction(Insn, 8, 4);
  1805. unsigned U = ((~fieldFromInstruction(Insn, 23, 1)) & 1) << 8;
  1806. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  1807. unsigned W = fieldFromInstruction(Insn, 21, 1);
  1808. unsigned P = fieldFromInstruction(Insn, 24, 1);
  1809. unsigned Rt2 = Rt + 1;
  1810. bool writeback = (W == 1) | (P == 0);
  1811. // For {LD,ST}RD, Rt must be even, else undefined.
  1812. switch (Inst.getOpcode()) {
  1813. case ARM::STRD:
  1814. case ARM::STRD_PRE:
  1815. case ARM::STRD_POST:
  1816. case ARM::LDRD:
  1817. case ARM::LDRD_PRE:
  1818. case ARM::LDRD_POST:
  1819. if (Rt & 0x1) S = MCDisassembler::SoftFail;
  1820. break;
  1821. default:
  1822. break;
  1823. }
  1824. switch (Inst.getOpcode()) {
  1825. case ARM::STRD:
  1826. case ARM::STRD_PRE:
  1827. case ARM::STRD_POST:
  1828. if (P == 0 && W == 1)
  1829. S = MCDisassembler::SoftFail;
  1830. if (writeback && (Rn == 15 || Rn == Rt || Rn == Rt2))
  1831. S = MCDisassembler::SoftFail;
  1832. if (type && Rm == 15)
  1833. S = MCDisassembler::SoftFail;
  1834. if (Rt2 == 15)
  1835. S = MCDisassembler::SoftFail;
  1836. if (!type && fieldFromInstruction(Insn, 8, 4))
  1837. S = MCDisassembler::SoftFail;
  1838. break;
  1839. case ARM::STRH:
  1840. case ARM::STRH_PRE:
  1841. case ARM::STRH_POST:
  1842. if (Rt == 15)
  1843. S = MCDisassembler::SoftFail;
  1844. if (writeback && (Rn == 15 || Rn == Rt))
  1845. S = MCDisassembler::SoftFail;
  1846. if (!type && Rm == 15)
  1847. S = MCDisassembler::SoftFail;
  1848. break;
  1849. case ARM::LDRD:
  1850. case ARM::LDRD_PRE:
  1851. case ARM::LDRD_POST:
  1852. if (type && Rn == 15) {
  1853. if (Rt2 == 15)
  1854. S = MCDisassembler::SoftFail;
  1855. break;
  1856. }
  1857. if (P == 0 && W == 1)
  1858. S = MCDisassembler::SoftFail;
  1859. if (!type && (Rt2 == 15 || Rm == 15 || Rm == Rt || Rm == Rt2))
  1860. S = MCDisassembler::SoftFail;
  1861. if (!type && writeback && Rn == 15)
  1862. S = MCDisassembler::SoftFail;
  1863. if (writeback && (Rn == Rt || Rn == Rt2))
  1864. S = MCDisassembler::SoftFail;
  1865. break;
  1866. case ARM::LDRH:
  1867. case ARM::LDRH_PRE:
  1868. case ARM::LDRH_POST:
  1869. if (type && Rn == 15) {
  1870. if (Rt == 15)
  1871. S = MCDisassembler::SoftFail;
  1872. break;
  1873. }
  1874. if (Rt == 15)
  1875. S = MCDisassembler::SoftFail;
  1876. if (!type && Rm == 15)
  1877. S = MCDisassembler::SoftFail;
  1878. if (!type && writeback && (Rn == 15 || Rn == Rt))
  1879. S = MCDisassembler::SoftFail;
  1880. break;
  1881. case ARM::LDRSH:
  1882. case ARM::LDRSH_PRE:
  1883. case ARM::LDRSH_POST:
  1884. case ARM::LDRSB:
  1885. case ARM::LDRSB_PRE:
  1886. case ARM::LDRSB_POST:
  1887. if (type && Rn == 15) {
  1888. if (Rt == 15)
  1889. S = MCDisassembler::SoftFail;
  1890. break;
  1891. }
  1892. if (type && (Rt == 15 || (writeback && Rn == Rt)))
  1893. S = MCDisassembler::SoftFail;
  1894. if (!type && (Rt == 15 || Rm == 15))
  1895. S = MCDisassembler::SoftFail;
  1896. if (!type && writeback && (Rn == 15 || Rn == Rt))
  1897. S = MCDisassembler::SoftFail;
  1898. break;
  1899. default:
  1900. break;
  1901. }
  1902. if (writeback) { // Writeback
  1903. if (P)
  1904. U |= ARMII::IndexModePre << 9;
  1905. else
  1906. U |= ARMII::IndexModePost << 9;
  1907. // On stores, the writeback operand precedes Rt.
  1908. switch (Inst.getOpcode()) {
  1909. case ARM::STRD:
  1910. case ARM::STRD_PRE:
  1911. case ARM::STRD_POST:
  1912. case ARM::STRH:
  1913. case ARM::STRH_PRE:
  1914. case ARM::STRH_POST:
  1915. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  1916. return MCDisassembler::Fail;
  1917. break;
  1918. default:
  1919. break;
  1920. }
  1921. }
  1922. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
  1923. return MCDisassembler::Fail;
  1924. switch (Inst.getOpcode()) {
  1925. case ARM::STRD:
  1926. case ARM::STRD_PRE:
  1927. case ARM::STRD_POST:
  1928. case ARM::LDRD:
  1929. case ARM::LDRD_PRE:
  1930. case ARM::LDRD_POST:
  1931. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt+1, Address, Decoder)))
  1932. return MCDisassembler::Fail;
  1933. break;
  1934. default:
  1935. break;
  1936. }
  1937. if (writeback) {
  1938. // On loads, the writeback operand comes after Rt.
  1939. switch (Inst.getOpcode()) {
  1940. case ARM::LDRD:
  1941. case ARM::LDRD_PRE:
  1942. case ARM::LDRD_POST:
  1943. case ARM::LDRH:
  1944. case ARM::LDRH_PRE:
  1945. case ARM::LDRH_POST:
  1946. case ARM::LDRSH:
  1947. case ARM::LDRSH_PRE:
  1948. case ARM::LDRSH_POST:
  1949. case ARM::LDRSB:
  1950. case ARM::LDRSB_PRE:
  1951. case ARM::LDRSB_POST:
  1952. case ARM::LDRHTr:
  1953. case ARM::LDRSBTr:
  1954. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  1955. return MCDisassembler::Fail;
  1956. break;
  1957. default:
  1958. break;
  1959. }
  1960. }
  1961. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  1962. return MCDisassembler::Fail;
  1963. if (type) {
  1964. Inst.addOperand(MCOperand::createReg(0));
  1965. Inst.addOperand(MCOperand::createImm(U | (imm << 4) | Rm));
  1966. } else {
  1967. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  1968. return MCDisassembler::Fail;
  1969. Inst.addOperand(MCOperand::createImm(U));
  1970. }
  1971. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  1972. return MCDisassembler::Fail;
  1973. return S;
  1974. }
  1975. static DecodeStatus DecodeRFEInstruction(MCInst &Inst, unsigned Insn,
  1976. uint64_t Address, const void *Decoder) {
  1977. DecodeStatus S = MCDisassembler::Success;
  1978. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  1979. unsigned mode = fieldFromInstruction(Insn, 23, 2);
  1980. switch (mode) {
  1981. case 0:
  1982. mode = ARM_AM::da;
  1983. break;
  1984. case 1:
  1985. mode = ARM_AM::ia;
  1986. break;
  1987. case 2:
  1988. mode = ARM_AM::db;
  1989. break;
  1990. case 3:
  1991. mode = ARM_AM::ib;
  1992. break;
  1993. }
  1994. Inst.addOperand(MCOperand::createImm(mode));
  1995. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  1996. return MCDisassembler::Fail;
  1997. return S;
  1998. }
  1999. static DecodeStatus DecodeQADDInstruction(MCInst &Inst, unsigned Insn,
  2000. uint64_t Address, const void *Decoder) {
  2001. DecodeStatus S = MCDisassembler::Success;
  2002. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  2003. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  2004. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  2005. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  2006. if (pred == 0xF)
  2007. return DecodeCPSInstruction(Inst, Insn, Address, Decoder);
  2008. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder)))
  2009. return MCDisassembler::Fail;
  2010. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder)))
  2011. return MCDisassembler::Fail;
  2012. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
  2013. return MCDisassembler::Fail;
  2014. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  2015. return MCDisassembler::Fail;
  2016. return S;
  2017. }
  2018. static DecodeStatus DecodeMemMultipleWritebackInstruction(MCInst &Inst,
  2019. unsigned Insn,
  2020. uint64_t Address, const void *Decoder) {
  2021. DecodeStatus S = MCDisassembler::Success;
  2022. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  2023. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  2024. unsigned reglist = fieldFromInstruction(Insn, 0, 16);
  2025. if (pred == 0xF) {
  2026. // Ambiguous with RFE and SRS
  2027. switch (Inst.getOpcode()) {
  2028. case ARM::LDMDA:
  2029. Inst.setOpcode(ARM::RFEDA);
  2030. break;
  2031. case ARM::LDMDA_UPD:
  2032. Inst.setOpcode(ARM::RFEDA_UPD);
  2033. break;
  2034. case ARM::LDMDB:
  2035. Inst.setOpcode(ARM::RFEDB);
  2036. break;
  2037. case ARM::LDMDB_UPD:
  2038. Inst.setOpcode(ARM::RFEDB_UPD);
  2039. break;
  2040. case ARM::LDMIA:
  2041. Inst.setOpcode(ARM::RFEIA);
  2042. break;
  2043. case ARM::LDMIA_UPD:
  2044. Inst.setOpcode(ARM::RFEIA_UPD);
  2045. break;
  2046. case ARM::LDMIB:
  2047. Inst.setOpcode(ARM::RFEIB);
  2048. break;
  2049. case ARM::LDMIB_UPD:
  2050. Inst.setOpcode(ARM::RFEIB_UPD);
  2051. break;
  2052. case ARM::STMDA:
  2053. Inst.setOpcode(ARM::SRSDA);
  2054. break;
  2055. case ARM::STMDA_UPD:
  2056. Inst.setOpcode(ARM::SRSDA_UPD);
  2057. break;
  2058. case ARM::STMDB:
  2059. Inst.setOpcode(ARM::SRSDB);
  2060. break;
  2061. case ARM::STMDB_UPD:
  2062. Inst.setOpcode(ARM::SRSDB_UPD);
  2063. break;
  2064. case ARM::STMIA:
  2065. Inst.setOpcode(ARM::SRSIA);
  2066. break;
  2067. case ARM::STMIA_UPD:
  2068. Inst.setOpcode(ARM::SRSIA_UPD);
  2069. break;
  2070. case ARM::STMIB:
  2071. Inst.setOpcode(ARM::SRSIB);
  2072. break;
  2073. case ARM::STMIB_UPD:
  2074. Inst.setOpcode(ARM::SRSIB_UPD);
  2075. break;
  2076. default:
  2077. return MCDisassembler::Fail;
  2078. }
  2079. // For stores (which become SRS's, the only operand is the mode.
  2080. if (fieldFromInstruction(Insn, 20, 1) == 0) {
  2081. // Check SRS encoding constraints
  2082. if (!(fieldFromInstruction(Insn, 22, 1) == 1 &&
  2083. fieldFromInstruction(Insn, 20, 1) == 0))
  2084. return MCDisassembler::Fail;
  2085. Inst.addOperand(
  2086. MCOperand::createImm(fieldFromInstruction(Insn, 0, 4)));
  2087. return S;
  2088. }
  2089. return DecodeRFEInstruction(Inst, Insn, Address, Decoder);
  2090. }
  2091. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  2092. return MCDisassembler::Fail;
  2093. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  2094. return MCDisassembler::Fail; // Tied
  2095. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  2096. return MCDisassembler::Fail;
  2097. if (!Check(S, DecodeRegListOperand(Inst, reglist, Address, Decoder)))
  2098. return MCDisassembler::Fail;
  2099. return S;
  2100. }
  2101. // Check for UNPREDICTABLE predicated ESB instruction
  2102. static DecodeStatus DecodeHINTInstruction(MCInst &Inst, unsigned Insn,
  2103. uint64_t Address, const void *Decoder) {
  2104. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  2105. unsigned imm8 = fieldFromInstruction(Insn, 0, 8);
  2106. const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
  2107. const FeatureBitset &FeatureBits = Dis->getSubtargetInfo().getFeatureBits();
  2108. DecodeStatus S = MCDisassembler::Success;
  2109. Inst.addOperand(MCOperand::createImm(imm8));
  2110. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  2111. return MCDisassembler::Fail;
  2112. // ESB is unpredictable if pred != AL. Without the RAS extension, it is a NOP,
  2113. // so all predicates should be allowed.
  2114. if (imm8 == 0x10 && pred != 0xe && ((FeatureBits[ARM::FeatureRAS]) != 0))
  2115. S = MCDisassembler::SoftFail;
  2116. return S;
  2117. }
  2118. static DecodeStatus DecodeCPSInstruction(MCInst &Inst, unsigned Insn,
  2119. uint64_t Address, const void *Decoder) {
  2120. unsigned imod = fieldFromInstruction(Insn, 18, 2);
  2121. unsigned M = fieldFromInstruction(Insn, 17, 1);
  2122. unsigned iflags = fieldFromInstruction(Insn, 6, 3);
  2123. unsigned mode = fieldFromInstruction(Insn, 0, 5);
  2124. DecodeStatus S = MCDisassembler::Success;
  2125. // This decoder is called from multiple location that do not check
  2126. // the full encoding is valid before they do.
  2127. if (fieldFromInstruction(Insn, 5, 1) != 0 ||
  2128. fieldFromInstruction(Insn, 16, 1) != 0 ||
  2129. fieldFromInstruction(Insn, 20, 8) != 0x10)
  2130. return MCDisassembler::Fail;
  2131. // imod == '01' --> UNPREDICTABLE
  2132. // NOTE: Even though this is technically UNPREDICTABLE, we choose to
  2133. // return failure here. The '01' imod value is unprintable, so there's
  2134. // nothing useful we could do even if we returned UNPREDICTABLE.
  2135. if (imod == 1) return MCDisassembler::Fail;
  2136. if (imod && M) {
  2137. Inst.setOpcode(ARM::CPS3p);
  2138. Inst.addOperand(MCOperand::createImm(imod));
  2139. Inst.addOperand(MCOperand::createImm(iflags));
  2140. Inst.addOperand(MCOperand::createImm(mode));
  2141. } else if (imod && !M) {
  2142. Inst.setOpcode(ARM::CPS2p);
  2143. Inst.addOperand(MCOperand::createImm(imod));
  2144. Inst.addOperand(MCOperand::createImm(iflags));
  2145. if (mode) S = MCDisassembler::SoftFail;
  2146. } else if (!imod && M) {
  2147. Inst.setOpcode(ARM::CPS1p);
  2148. Inst.addOperand(MCOperand::createImm(mode));
  2149. if (iflags) S = MCDisassembler::SoftFail;
  2150. } else {
  2151. // imod == '00' && M == '0' --> UNPREDICTABLE
  2152. Inst.setOpcode(ARM::CPS1p);
  2153. Inst.addOperand(MCOperand::createImm(mode));
  2154. S = MCDisassembler::SoftFail;
  2155. }
  2156. return S;
  2157. }
  2158. static DecodeStatus DecodeT2CPSInstruction(MCInst &Inst, unsigned Insn,
  2159. uint64_t Address, const void *Decoder) {
  2160. unsigned imod = fieldFromInstruction(Insn, 9, 2);
  2161. unsigned M = fieldFromInstruction(Insn, 8, 1);
  2162. unsigned iflags = fieldFromInstruction(Insn, 5, 3);
  2163. unsigned mode = fieldFromInstruction(Insn, 0, 5);
  2164. DecodeStatus S = MCDisassembler::Success;
  2165. // imod == '01' --> UNPREDICTABLE
  2166. // NOTE: Even though this is technically UNPREDICTABLE, we choose to
  2167. // return failure here. The '01' imod value is unprintable, so there's
  2168. // nothing useful we could do even if we returned UNPREDICTABLE.
  2169. if (imod == 1) return MCDisassembler::Fail;
  2170. if (imod && M) {
  2171. Inst.setOpcode(ARM::t2CPS3p);
  2172. Inst.addOperand(MCOperand::createImm(imod));
  2173. Inst.addOperand(MCOperand::createImm(iflags));
  2174. Inst.addOperand(MCOperand::createImm(mode));
  2175. } else if (imod && !M) {
  2176. Inst.setOpcode(ARM::t2CPS2p);
  2177. Inst.addOperand(MCOperand::createImm(imod));
  2178. Inst.addOperand(MCOperand::createImm(iflags));
  2179. if (mode) S = MCDisassembler::SoftFail;
  2180. } else if (!imod && M) {
  2181. Inst.setOpcode(ARM::t2CPS1p);
  2182. Inst.addOperand(MCOperand::createImm(mode));
  2183. if (iflags) S = MCDisassembler::SoftFail;
  2184. } else {
  2185. // imod == '00' && M == '0' --> this is a HINT instruction
  2186. int imm = fieldFromInstruction(Insn, 0, 8);
  2187. // HINT are defined only for immediate in [0..4]
  2188. if(imm > 4) return MCDisassembler::Fail;
  2189. Inst.setOpcode(ARM::t2HINT);
  2190. Inst.addOperand(MCOperand::createImm(imm));
  2191. }
  2192. return S;
  2193. }
  2194. static DecodeStatus DecodeT2HintSpaceInstruction(MCInst &Inst, unsigned Insn,
  2195. uint64_t Address,
  2196. const void *Decoder) {
  2197. unsigned imm = fieldFromInstruction(Insn, 0, 8);
  2198. unsigned Opcode = ARM::t2HINT;
  2199. if (imm == 0x0D) {
  2200. Opcode = ARM::t2PACBTI;
  2201. } else if (imm == 0x1D) {
  2202. Opcode = ARM::t2PAC;
  2203. } else if (imm == 0x2D) {
  2204. Opcode = ARM::t2AUT;
  2205. } else if (imm == 0x0F) {
  2206. Opcode = ARM::t2BTI;
  2207. }
  2208. Inst.setOpcode(Opcode);
  2209. if (Opcode == ARM::t2HINT) {
  2210. Inst.addOperand(MCOperand::createImm(imm));
  2211. }
  2212. return MCDisassembler::Success;
  2213. }
  2214. static DecodeStatus DecodeT2MOVTWInstruction(MCInst &Inst, unsigned Insn,
  2215. uint64_t Address, const void *Decoder) {
  2216. DecodeStatus S = MCDisassembler::Success;
  2217. unsigned Rd = fieldFromInstruction(Insn, 8, 4);
  2218. unsigned imm = 0;
  2219. imm |= (fieldFromInstruction(Insn, 0, 8) << 0);
  2220. imm |= (fieldFromInstruction(Insn, 12, 3) << 8);
  2221. imm |= (fieldFromInstruction(Insn, 16, 4) << 12);
  2222. imm |= (fieldFromInstruction(Insn, 26, 1) << 11);
  2223. if (Inst.getOpcode() == ARM::t2MOVTi16)
  2224. if (!Check(S, DecoderGPRRegisterClass(Inst, Rd, Address, Decoder)))
  2225. return MCDisassembler::Fail;
  2226. if (!Check(S, DecoderGPRRegisterClass(Inst, Rd, Address, Decoder)))
  2227. return MCDisassembler::Fail;
  2228. if (!tryAddingSymbolicOperand(Address, imm, false, 4, Inst, Decoder))
  2229. Inst.addOperand(MCOperand::createImm(imm));
  2230. return S;
  2231. }
  2232. static DecodeStatus DecodeArmMOVTWInstruction(MCInst &Inst, unsigned Insn,
  2233. uint64_t Address, const void *Decoder) {
  2234. DecodeStatus S = MCDisassembler::Success;
  2235. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  2236. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  2237. unsigned imm = 0;
  2238. imm |= (fieldFromInstruction(Insn, 0, 12) << 0);
  2239. imm |= (fieldFromInstruction(Insn, 16, 4) << 12);
  2240. if (Inst.getOpcode() == ARM::MOVTi16)
  2241. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder)))
  2242. return MCDisassembler::Fail;
  2243. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder)))
  2244. return MCDisassembler::Fail;
  2245. if (!tryAddingSymbolicOperand(Address, imm, false, 4, Inst, Decoder))
  2246. Inst.addOperand(MCOperand::createImm(imm));
  2247. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  2248. return MCDisassembler::Fail;
  2249. return S;
  2250. }
  2251. static DecodeStatus DecodeSMLAInstruction(MCInst &Inst, unsigned Insn,
  2252. uint64_t Address, const void *Decoder) {
  2253. DecodeStatus S = MCDisassembler::Success;
  2254. unsigned Rd = fieldFromInstruction(Insn, 16, 4);
  2255. unsigned Rn = fieldFromInstruction(Insn, 0, 4);
  2256. unsigned Rm = fieldFromInstruction(Insn, 8, 4);
  2257. unsigned Ra = fieldFromInstruction(Insn, 12, 4);
  2258. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  2259. if (pred == 0xF)
  2260. return DecodeCPSInstruction(Inst, Insn, Address, Decoder);
  2261. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder)))
  2262. return MCDisassembler::Fail;
  2263. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
  2264. return MCDisassembler::Fail;
  2265. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder)))
  2266. return MCDisassembler::Fail;
  2267. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Ra, Address, Decoder)))
  2268. return MCDisassembler::Fail;
  2269. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  2270. return MCDisassembler::Fail;
  2271. return S;
  2272. }
  2273. static DecodeStatus DecodeTSTInstruction(MCInst &Inst, unsigned Insn,
  2274. uint64_t Address, const void *Decoder) {
  2275. DecodeStatus S = MCDisassembler::Success;
  2276. unsigned Pred = fieldFromInstruction(Insn, 28, 4);
  2277. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  2278. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  2279. if (Pred == 0xF)
  2280. return DecodeSETPANInstruction(Inst, Insn, Address, Decoder);
  2281. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  2282. return MCDisassembler::Fail;
  2283. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  2284. return MCDisassembler::Fail;
  2285. if (!Check(S, DecodePredicateOperand(Inst, Pred, Address, Decoder)))
  2286. return MCDisassembler::Fail;
  2287. return S;
  2288. }
  2289. static DecodeStatus DecodeSETPANInstruction(MCInst &Inst, unsigned Insn,
  2290. uint64_t Address, const void *Decoder) {
  2291. DecodeStatus S = MCDisassembler::Success;
  2292. unsigned Imm = fieldFromInstruction(Insn, 9, 1);
  2293. const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
  2294. const FeatureBitset &FeatureBits = Dis->getSubtargetInfo().getFeatureBits();
  2295. if (!FeatureBits[ARM::HasV8_1aOps] ||
  2296. !FeatureBits[ARM::HasV8Ops])
  2297. return MCDisassembler::Fail;
  2298. // Decoder can be called from DecodeTST, which does not check the full
  2299. // encoding is valid.
  2300. if (fieldFromInstruction(Insn, 20,12) != 0xf11 ||
  2301. fieldFromInstruction(Insn, 4,4) != 0)
  2302. return MCDisassembler::Fail;
  2303. if (fieldFromInstruction(Insn, 10,10) != 0 ||
  2304. fieldFromInstruction(Insn, 0,4) != 0)
  2305. S = MCDisassembler::SoftFail;
  2306. Inst.setOpcode(ARM::SETPAN);
  2307. Inst.addOperand(MCOperand::createImm(Imm));
  2308. return S;
  2309. }
  2310. static DecodeStatus DecodeAddrModeImm12Operand(MCInst &Inst, unsigned Val,
  2311. uint64_t Address, const void *Decoder) {
  2312. DecodeStatus S = MCDisassembler::Success;
  2313. unsigned add = fieldFromInstruction(Val, 12, 1);
  2314. unsigned imm = fieldFromInstruction(Val, 0, 12);
  2315. unsigned Rn = fieldFromInstruction(Val, 13, 4);
  2316. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  2317. return MCDisassembler::Fail;
  2318. if (!add) imm *= -1;
  2319. if (imm == 0 && !add) imm = INT32_MIN;
  2320. Inst.addOperand(MCOperand::createImm(imm));
  2321. if (Rn == 15)
  2322. tryAddingPcLoadReferenceComment(Address, Address + imm + 8, Decoder);
  2323. return S;
  2324. }
  2325. static DecodeStatus DecodeAddrMode5Operand(MCInst &Inst, unsigned Val,
  2326. uint64_t Address, const void *Decoder) {
  2327. DecodeStatus S = MCDisassembler::Success;
  2328. unsigned Rn = fieldFromInstruction(Val, 9, 4);
  2329. // U == 1 to add imm, 0 to subtract it.
  2330. unsigned U = fieldFromInstruction(Val, 8, 1);
  2331. unsigned imm = fieldFromInstruction(Val, 0, 8);
  2332. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  2333. return MCDisassembler::Fail;
  2334. if (U)
  2335. Inst.addOperand(MCOperand::createImm(ARM_AM::getAM5Opc(ARM_AM::add, imm)));
  2336. else
  2337. Inst.addOperand(MCOperand::createImm(ARM_AM::getAM5Opc(ARM_AM::sub, imm)));
  2338. return S;
  2339. }
  2340. static DecodeStatus DecodeAddrMode5FP16Operand(MCInst &Inst, unsigned Val,
  2341. uint64_t Address, const void *Decoder) {
  2342. DecodeStatus S = MCDisassembler::Success;
  2343. unsigned Rn = fieldFromInstruction(Val, 9, 4);
  2344. // U == 1 to add imm, 0 to subtract it.
  2345. unsigned U = fieldFromInstruction(Val, 8, 1);
  2346. unsigned imm = fieldFromInstruction(Val, 0, 8);
  2347. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  2348. return MCDisassembler::Fail;
  2349. if (U)
  2350. Inst.addOperand(MCOperand::createImm(ARM_AM::getAM5FP16Opc(ARM_AM::add, imm)));
  2351. else
  2352. Inst.addOperand(MCOperand::createImm(ARM_AM::getAM5FP16Opc(ARM_AM::sub, imm)));
  2353. return S;
  2354. }
  2355. static DecodeStatus DecodeAddrMode7Operand(MCInst &Inst, unsigned Val,
  2356. uint64_t Address, const void *Decoder) {
  2357. return DecodeGPRRegisterClass(Inst, Val, Address, Decoder);
  2358. }
  2359. static DecodeStatus
  2360. DecodeT2BInstruction(MCInst &Inst, unsigned Insn,
  2361. uint64_t Address, const void *Decoder) {
  2362. DecodeStatus Status = MCDisassembler::Success;
  2363. // Note the J1 and J2 values are from the encoded instruction. So here
  2364. // change them to I1 and I2 values via as documented:
  2365. // I1 = NOT(J1 EOR S);
  2366. // I2 = NOT(J2 EOR S);
  2367. // and build the imm32 with one trailing zero as documented:
  2368. // imm32 = SignExtend(S:I1:I2:imm10:imm11:'0', 32);
  2369. unsigned S = fieldFromInstruction(Insn, 26, 1);
  2370. unsigned J1 = fieldFromInstruction(Insn, 13, 1);
  2371. unsigned J2 = fieldFromInstruction(Insn, 11, 1);
  2372. unsigned I1 = !(J1 ^ S);
  2373. unsigned I2 = !(J2 ^ S);
  2374. unsigned imm10 = fieldFromInstruction(Insn, 16, 10);
  2375. unsigned imm11 = fieldFromInstruction(Insn, 0, 11);
  2376. unsigned tmp = (S << 23) | (I1 << 22) | (I2 << 21) | (imm10 << 11) | imm11;
  2377. int imm32 = SignExtend32<25>(tmp << 1);
  2378. if (!tryAddingSymbolicOperand(Address, Address + imm32 + 4,
  2379. true, 4, Inst, Decoder))
  2380. Inst.addOperand(MCOperand::createImm(imm32));
  2381. return Status;
  2382. }
  2383. static DecodeStatus
  2384. DecodeBranchImmInstruction(MCInst &Inst, unsigned Insn,
  2385. uint64_t Address, const void *Decoder) {
  2386. DecodeStatus S = MCDisassembler::Success;
  2387. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  2388. unsigned imm = fieldFromInstruction(Insn, 0, 24) << 2;
  2389. if (pred == 0xF) {
  2390. Inst.setOpcode(ARM::BLXi);
  2391. imm |= fieldFromInstruction(Insn, 24, 1) << 1;
  2392. if (!tryAddingSymbolicOperand(Address, Address + SignExtend32<26>(imm) + 8,
  2393. true, 4, Inst, Decoder))
  2394. Inst.addOperand(MCOperand::createImm(SignExtend32<26>(imm)));
  2395. return S;
  2396. }
  2397. if (!tryAddingSymbolicOperand(Address, Address + SignExtend32<26>(imm) + 8,
  2398. true, 4, Inst, Decoder))
  2399. Inst.addOperand(MCOperand::createImm(SignExtend32<26>(imm)));
  2400. // We already have BL_pred for BL w/ predicate, no need to add addition
  2401. // predicate opreands for BL
  2402. if (Inst.getOpcode() != ARM::BL)
  2403. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  2404. return MCDisassembler::Fail;
  2405. return S;
  2406. }
  2407. static DecodeStatus DecodeAddrMode6Operand(MCInst &Inst, unsigned Val,
  2408. uint64_t Address, const void *Decoder) {
  2409. DecodeStatus S = MCDisassembler::Success;
  2410. unsigned Rm = fieldFromInstruction(Val, 0, 4);
  2411. unsigned align = fieldFromInstruction(Val, 4, 2);
  2412. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  2413. return MCDisassembler::Fail;
  2414. if (!align)
  2415. Inst.addOperand(MCOperand::createImm(0));
  2416. else
  2417. Inst.addOperand(MCOperand::createImm(4 << align));
  2418. return S;
  2419. }
  2420. static DecodeStatus DecodeVLDInstruction(MCInst &Inst, unsigned Insn,
  2421. uint64_t Address, const void *Decoder) {
  2422. DecodeStatus S = MCDisassembler::Success;
  2423. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  2424. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  2425. unsigned wb = fieldFromInstruction(Insn, 16, 4);
  2426. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  2427. Rn |= fieldFromInstruction(Insn, 4, 2) << 4;
  2428. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  2429. // First output register
  2430. switch (Inst.getOpcode()) {
  2431. case ARM::VLD1q16: case ARM::VLD1q32: case ARM::VLD1q64: case ARM::VLD1q8:
  2432. case ARM::VLD1q16wb_fixed: case ARM::VLD1q16wb_register:
  2433. case ARM::VLD1q32wb_fixed: case ARM::VLD1q32wb_register:
  2434. case ARM::VLD1q64wb_fixed: case ARM::VLD1q64wb_register:
  2435. case ARM::VLD1q8wb_fixed: case ARM::VLD1q8wb_register:
  2436. case ARM::VLD2d16: case ARM::VLD2d32: case ARM::VLD2d8:
  2437. case ARM::VLD2d16wb_fixed: case ARM::VLD2d16wb_register:
  2438. case ARM::VLD2d32wb_fixed: case ARM::VLD2d32wb_register:
  2439. case ARM::VLD2d8wb_fixed: case ARM::VLD2d8wb_register:
  2440. if (!Check(S, DecodeDPairRegisterClass(Inst, Rd, Address, Decoder)))
  2441. return MCDisassembler::Fail;
  2442. break;
  2443. case ARM::VLD2b16:
  2444. case ARM::VLD2b32:
  2445. case ARM::VLD2b8:
  2446. case ARM::VLD2b16wb_fixed:
  2447. case ARM::VLD2b16wb_register:
  2448. case ARM::VLD2b32wb_fixed:
  2449. case ARM::VLD2b32wb_register:
  2450. case ARM::VLD2b8wb_fixed:
  2451. case ARM::VLD2b8wb_register:
  2452. if (!Check(S, DecodeDPairSpacedRegisterClass(Inst, Rd, Address, Decoder)))
  2453. return MCDisassembler::Fail;
  2454. break;
  2455. default:
  2456. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  2457. return MCDisassembler::Fail;
  2458. }
  2459. // Second output register
  2460. switch (Inst.getOpcode()) {
  2461. case ARM::VLD3d8:
  2462. case ARM::VLD3d16:
  2463. case ARM::VLD3d32:
  2464. case ARM::VLD3d8_UPD:
  2465. case ARM::VLD3d16_UPD:
  2466. case ARM::VLD3d32_UPD:
  2467. case ARM::VLD4d8:
  2468. case ARM::VLD4d16:
  2469. case ARM::VLD4d32:
  2470. case ARM::VLD4d8_UPD:
  2471. case ARM::VLD4d16_UPD:
  2472. case ARM::VLD4d32_UPD:
  2473. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+1)%32, Address, Decoder)))
  2474. return MCDisassembler::Fail;
  2475. break;
  2476. case ARM::VLD3q8:
  2477. case ARM::VLD3q16:
  2478. case ARM::VLD3q32:
  2479. case ARM::VLD3q8_UPD:
  2480. case ARM::VLD3q16_UPD:
  2481. case ARM::VLD3q32_UPD:
  2482. case ARM::VLD4q8:
  2483. case ARM::VLD4q16:
  2484. case ARM::VLD4q32:
  2485. case ARM::VLD4q8_UPD:
  2486. case ARM::VLD4q16_UPD:
  2487. case ARM::VLD4q32_UPD:
  2488. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder)))
  2489. return MCDisassembler::Fail;
  2490. break;
  2491. default:
  2492. break;
  2493. }
  2494. // Third output register
  2495. switch(Inst.getOpcode()) {
  2496. case ARM::VLD3d8:
  2497. case ARM::VLD3d16:
  2498. case ARM::VLD3d32:
  2499. case ARM::VLD3d8_UPD:
  2500. case ARM::VLD3d16_UPD:
  2501. case ARM::VLD3d32_UPD:
  2502. case ARM::VLD4d8:
  2503. case ARM::VLD4d16:
  2504. case ARM::VLD4d32:
  2505. case ARM::VLD4d8_UPD:
  2506. case ARM::VLD4d16_UPD:
  2507. case ARM::VLD4d32_UPD:
  2508. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder)))
  2509. return MCDisassembler::Fail;
  2510. break;
  2511. case ARM::VLD3q8:
  2512. case ARM::VLD3q16:
  2513. case ARM::VLD3q32:
  2514. case ARM::VLD3q8_UPD:
  2515. case ARM::VLD3q16_UPD:
  2516. case ARM::VLD3q32_UPD:
  2517. case ARM::VLD4q8:
  2518. case ARM::VLD4q16:
  2519. case ARM::VLD4q32:
  2520. case ARM::VLD4q8_UPD:
  2521. case ARM::VLD4q16_UPD:
  2522. case ARM::VLD4q32_UPD:
  2523. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+4)%32, Address, Decoder)))
  2524. return MCDisassembler::Fail;
  2525. break;
  2526. default:
  2527. break;
  2528. }
  2529. // Fourth output register
  2530. switch (Inst.getOpcode()) {
  2531. case ARM::VLD4d8:
  2532. case ARM::VLD4d16:
  2533. case ARM::VLD4d32:
  2534. case ARM::VLD4d8_UPD:
  2535. case ARM::VLD4d16_UPD:
  2536. case ARM::VLD4d32_UPD:
  2537. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+3)%32, Address, Decoder)))
  2538. return MCDisassembler::Fail;
  2539. break;
  2540. case ARM::VLD4q8:
  2541. case ARM::VLD4q16:
  2542. case ARM::VLD4q32:
  2543. case ARM::VLD4q8_UPD:
  2544. case ARM::VLD4q16_UPD:
  2545. case ARM::VLD4q32_UPD:
  2546. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+6)%32, Address, Decoder)))
  2547. return MCDisassembler::Fail;
  2548. break;
  2549. default:
  2550. break;
  2551. }
  2552. // Writeback operand
  2553. switch (Inst.getOpcode()) {
  2554. case ARM::VLD1d8wb_fixed:
  2555. case ARM::VLD1d16wb_fixed:
  2556. case ARM::VLD1d32wb_fixed:
  2557. case ARM::VLD1d64wb_fixed:
  2558. case ARM::VLD1d8wb_register:
  2559. case ARM::VLD1d16wb_register:
  2560. case ARM::VLD1d32wb_register:
  2561. case ARM::VLD1d64wb_register:
  2562. case ARM::VLD1q8wb_fixed:
  2563. case ARM::VLD1q16wb_fixed:
  2564. case ARM::VLD1q32wb_fixed:
  2565. case ARM::VLD1q64wb_fixed:
  2566. case ARM::VLD1q8wb_register:
  2567. case ARM::VLD1q16wb_register:
  2568. case ARM::VLD1q32wb_register:
  2569. case ARM::VLD1q64wb_register:
  2570. case ARM::VLD1d8Twb_fixed:
  2571. case ARM::VLD1d8Twb_register:
  2572. case ARM::VLD1d16Twb_fixed:
  2573. case ARM::VLD1d16Twb_register:
  2574. case ARM::VLD1d32Twb_fixed:
  2575. case ARM::VLD1d32Twb_register:
  2576. case ARM::VLD1d64Twb_fixed:
  2577. case ARM::VLD1d64Twb_register:
  2578. case ARM::VLD1d8Qwb_fixed:
  2579. case ARM::VLD1d8Qwb_register:
  2580. case ARM::VLD1d16Qwb_fixed:
  2581. case ARM::VLD1d16Qwb_register:
  2582. case ARM::VLD1d32Qwb_fixed:
  2583. case ARM::VLD1d32Qwb_register:
  2584. case ARM::VLD1d64Qwb_fixed:
  2585. case ARM::VLD1d64Qwb_register:
  2586. case ARM::VLD2d8wb_fixed:
  2587. case ARM::VLD2d16wb_fixed:
  2588. case ARM::VLD2d32wb_fixed:
  2589. case ARM::VLD2q8wb_fixed:
  2590. case ARM::VLD2q16wb_fixed:
  2591. case ARM::VLD2q32wb_fixed:
  2592. case ARM::VLD2d8wb_register:
  2593. case ARM::VLD2d16wb_register:
  2594. case ARM::VLD2d32wb_register:
  2595. case ARM::VLD2q8wb_register:
  2596. case ARM::VLD2q16wb_register:
  2597. case ARM::VLD2q32wb_register:
  2598. case ARM::VLD2b8wb_fixed:
  2599. case ARM::VLD2b16wb_fixed:
  2600. case ARM::VLD2b32wb_fixed:
  2601. case ARM::VLD2b8wb_register:
  2602. case ARM::VLD2b16wb_register:
  2603. case ARM::VLD2b32wb_register:
  2604. Inst.addOperand(MCOperand::createImm(0));
  2605. break;
  2606. case ARM::VLD3d8_UPD:
  2607. case ARM::VLD3d16_UPD:
  2608. case ARM::VLD3d32_UPD:
  2609. case ARM::VLD3q8_UPD:
  2610. case ARM::VLD3q16_UPD:
  2611. case ARM::VLD3q32_UPD:
  2612. case ARM::VLD4d8_UPD:
  2613. case ARM::VLD4d16_UPD:
  2614. case ARM::VLD4d32_UPD:
  2615. case ARM::VLD4q8_UPD:
  2616. case ARM::VLD4q16_UPD:
  2617. case ARM::VLD4q32_UPD:
  2618. if (!Check(S, DecodeGPRRegisterClass(Inst, wb, Address, Decoder)))
  2619. return MCDisassembler::Fail;
  2620. break;
  2621. default:
  2622. break;
  2623. }
  2624. // AddrMode6 Base (register+alignment)
  2625. if (!Check(S, DecodeAddrMode6Operand(Inst, Rn, Address, Decoder)))
  2626. return MCDisassembler::Fail;
  2627. // AddrMode6 Offset (register)
  2628. switch (Inst.getOpcode()) {
  2629. default:
  2630. // The below have been updated to have explicit am6offset split
  2631. // between fixed and register offset. For those instructions not
  2632. // yet updated, we need to add an additional reg0 operand for the
  2633. // fixed variant.
  2634. //
  2635. // The fixed offset encodes as Rm == 0xd, so we check for that.
  2636. if (Rm == 0xd) {
  2637. Inst.addOperand(MCOperand::createReg(0));
  2638. break;
  2639. }
  2640. // Fall through to handle the register offset variant.
  2641. LLVM_FALLTHROUGH;
  2642. case ARM::VLD1d8wb_fixed:
  2643. case ARM::VLD1d16wb_fixed:
  2644. case ARM::VLD1d32wb_fixed:
  2645. case ARM::VLD1d64wb_fixed:
  2646. case ARM::VLD1d8Twb_fixed:
  2647. case ARM::VLD1d16Twb_fixed:
  2648. case ARM::VLD1d32Twb_fixed:
  2649. case ARM::VLD1d64Twb_fixed:
  2650. case ARM::VLD1d8Qwb_fixed:
  2651. case ARM::VLD1d16Qwb_fixed:
  2652. case ARM::VLD1d32Qwb_fixed:
  2653. case ARM::VLD1d64Qwb_fixed:
  2654. case ARM::VLD1d8wb_register:
  2655. case ARM::VLD1d16wb_register:
  2656. case ARM::VLD1d32wb_register:
  2657. case ARM::VLD1d64wb_register:
  2658. case ARM::VLD1q8wb_fixed:
  2659. case ARM::VLD1q16wb_fixed:
  2660. case ARM::VLD1q32wb_fixed:
  2661. case ARM::VLD1q64wb_fixed:
  2662. case ARM::VLD1q8wb_register:
  2663. case ARM::VLD1q16wb_register:
  2664. case ARM::VLD1q32wb_register:
  2665. case ARM::VLD1q64wb_register:
  2666. // The fixed offset post-increment encodes Rm == 0xd. The no-writeback
  2667. // variant encodes Rm == 0xf. Anything else is a register offset post-
  2668. // increment and we need to add the register operand to the instruction.
  2669. if (Rm != 0xD && Rm != 0xF &&
  2670. !Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  2671. return MCDisassembler::Fail;
  2672. break;
  2673. case ARM::VLD2d8wb_fixed:
  2674. case ARM::VLD2d16wb_fixed:
  2675. case ARM::VLD2d32wb_fixed:
  2676. case ARM::VLD2b8wb_fixed:
  2677. case ARM::VLD2b16wb_fixed:
  2678. case ARM::VLD2b32wb_fixed:
  2679. case ARM::VLD2q8wb_fixed:
  2680. case ARM::VLD2q16wb_fixed:
  2681. case ARM::VLD2q32wb_fixed:
  2682. break;
  2683. }
  2684. return S;
  2685. }
  2686. static DecodeStatus DecodeVLDST1Instruction(MCInst &Inst, unsigned Insn,
  2687. uint64_t Address, const void *Decoder) {
  2688. unsigned type = fieldFromInstruction(Insn, 8, 4);
  2689. unsigned align = fieldFromInstruction(Insn, 4, 2);
  2690. if (type == 6 && (align & 2)) return MCDisassembler::Fail;
  2691. if (type == 7 && (align & 2)) return MCDisassembler::Fail;
  2692. if (type == 10 && align == 3) return MCDisassembler::Fail;
  2693. unsigned load = fieldFromInstruction(Insn, 21, 1);
  2694. return load ? DecodeVLDInstruction(Inst, Insn, Address, Decoder)
  2695. : DecodeVSTInstruction(Inst, Insn, Address, Decoder);
  2696. }
  2697. static DecodeStatus DecodeVLDST2Instruction(MCInst &Inst, unsigned Insn,
  2698. uint64_t Address, const void *Decoder) {
  2699. unsigned size = fieldFromInstruction(Insn, 6, 2);
  2700. if (size == 3) return MCDisassembler::Fail;
  2701. unsigned type = fieldFromInstruction(Insn, 8, 4);
  2702. unsigned align = fieldFromInstruction(Insn, 4, 2);
  2703. if (type == 8 && align == 3) return MCDisassembler::Fail;
  2704. if (type == 9 && align == 3) return MCDisassembler::Fail;
  2705. unsigned load = fieldFromInstruction(Insn, 21, 1);
  2706. return load ? DecodeVLDInstruction(Inst, Insn, Address, Decoder)
  2707. : DecodeVSTInstruction(Inst, Insn, Address, Decoder);
  2708. }
  2709. static DecodeStatus DecodeVLDST3Instruction(MCInst &Inst, unsigned Insn,
  2710. uint64_t Address, const void *Decoder) {
  2711. unsigned size = fieldFromInstruction(Insn, 6, 2);
  2712. if (size == 3) return MCDisassembler::Fail;
  2713. unsigned align = fieldFromInstruction(Insn, 4, 2);
  2714. if (align & 2) return MCDisassembler::Fail;
  2715. unsigned load = fieldFromInstruction(Insn, 21, 1);
  2716. return load ? DecodeVLDInstruction(Inst, Insn, Address, Decoder)
  2717. : DecodeVSTInstruction(Inst, Insn, Address, Decoder);
  2718. }
  2719. static DecodeStatus DecodeVLDST4Instruction(MCInst &Inst, unsigned Insn,
  2720. uint64_t Address, const void *Decoder) {
  2721. unsigned size = fieldFromInstruction(Insn, 6, 2);
  2722. if (size == 3) return MCDisassembler::Fail;
  2723. unsigned load = fieldFromInstruction(Insn, 21, 1);
  2724. return load ? DecodeVLDInstruction(Inst, Insn, Address, Decoder)
  2725. : DecodeVSTInstruction(Inst, Insn, Address, Decoder);
  2726. }
  2727. static DecodeStatus DecodeVSTInstruction(MCInst &Inst, unsigned Insn,
  2728. uint64_t Address, const void *Decoder) {
  2729. DecodeStatus S = MCDisassembler::Success;
  2730. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  2731. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  2732. unsigned wb = fieldFromInstruction(Insn, 16, 4);
  2733. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  2734. Rn |= fieldFromInstruction(Insn, 4, 2) << 4;
  2735. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  2736. // Writeback Operand
  2737. switch (Inst.getOpcode()) {
  2738. case ARM::VST1d8wb_fixed:
  2739. case ARM::VST1d16wb_fixed:
  2740. case ARM::VST1d32wb_fixed:
  2741. case ARM::VST1d64wb_fixed:
  2742. case ARM::VST1d8wb_register:
  2743. case ARM::VST1d16wb_register:
  2744. case ARM::VST1d32wb_register:
  2745. case ARM::VST1d64wb_register:
  2746. case ARM::VST1q8wb_fixed:
  2747. case ARM::VST1q16wb_fixed:
  2748. case ARM::VST1q32wb_fixed:
  2749. case ARM::VST1q64wb_fixed:
  2750. case ARM::VST1q8wb_register:
  2751. case ARM::VST1q16wb_register:
  2752. case ARM::VST1q32wb_register:
  2753. case ARM::VST1q64wb_register:
  2754. case ARM::VST1d8Twb_fixed:
  2755. case ARM::VST1d16Twb_fixed:
  2756. case ARM::VST1d32Twb_fixed:
  2757. case ARM::VST1d64Twb_fixed:
  2758. case ARM::VST1d8Twb_register:
  2759. case ARM::VST1d16Twb_register:
  2760. case ARM::VST1d32Twb_register:
  2761. case ARM::VST1d64Twb_register:
  2762. case ARM::VST1d8Qwb_fixed:
  2763. case ARM::VST1d16Qwb_fixed:
  2764. case ARM::VST1d32Qwb_fixed:
  2765. case ARM::VST1d64Qwb_fixed:
  2766. case ARM::VST1d8Qwb_register:
  2767. case ARM::VST1d16Qwb_register:
  2768. case ARM::VST1d32Qwb_register:
  2769. case ARM::VST1d64Qwb_register:
  2770. case ARM::VST2d8wb_fixed:
  2771. case ARM::VST2d16wb_fixed:
  2772. case ARM::VST2d32wb_fixed:
  2773. case ARM::VST2d8wb_register:
  2774. case ARM::VST2d16wb_register:
  2775. case ARM::VST2d32wb_register:
  2776. case ARM::VST2q8wb_fixed:
  2777. case ARM::VST2q16wb_fixed:
  2778. case ARM::VST2q32wb_fixed:
  2779. case ARM::VST2q8wb_register:
  2780. case ARM::VST2q16wb_register:
  2781. case ARM::VST2q32wb_register:
  2782. case ARM::VST2b8wb_fixed:
  2783. case ARM::VST2b16wb_fixed:
  2784. case ARM::VST2b32wb_fixed:
  2785. case ARM::VST2b8wb_register:
  2786. case ARM::VST2b16wb_register:
  2787. case ARM::VST2b32wb_register:
  2788. if (Rm == 0xF)
  2789. return MCDisassembler::Fail;
  2790. Inst.addOperand(MCOperand::createImm(0));
  2791. break;
  2792. case ARM::VST3d8_UPD:
  2793. case ARM::VST3d16_UPD:
  2794. case ARM::VST3d32_UPD:
  2795. case ARM::VST3q8_UPD:
  2796. case ARM::VST3q16_UPD:
  2797. case ARM::VST3q32_UPD:
  2798. case ARM::VST4d8_UPD:
  2799. case ARM::VST4d16_UPD:
  2800. case ARM::VST4d32_UPD:
  2801. case ARM::VST4q8_UPD:
  2802. case ARM::VST4q16_UPD:
  2803. case ARM::VST4q32_UPD:
  2804. if (!Check(S, DecodeGPRRegisterClass(Inst, wb, Address, Decoder)))
  2805. return MCDisassembler::Fail;
  2806. break;
  2807. default:
  2808. break;
  2809. }
  2810. // AddrMode6 Base (register+alignment)
  2811. if (!Check(S, DecodeAddrMode6Operand(Inst, Rn, Address, Decoder)))
  2812. return MCDisassembler::Fail;
  2813. // AddrMode6 Offset (register)
  2814. switch (Inst.getOpcode()) {
  2815. default:
  2816. if (Rm == 0xD)
  2817. Inst.addOperand(MCOperand::createReg(0));
  2818. else if (Rm != 0xF) {
  2819. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  2820. return MCDisassembler::Fail;
  2821. }
  2822. break;
  2823. case ARM::VST1d8wb_fixed:
  2824. case ARM::VST1d16wb_fixed:
  2825. case ARM::VST1d32wb_fixed:
  2826. case ARM::VST1d64wb_fixed:
  2827. case ARM::VST1q8wb_fixed:
  2828. case ARM::VST1q16wb_fixed:
  2829. case ARM::VST1q32wb_fixed:
  2830. case ARM::VST1q64wb_fixed:
  2831. case ARM::VST1d8Twb_fixed:
  2832. case ARM::VST1d16Twb_fixed:
  2833. case ARM::VST1d32Twb_fixed:
  2834. case ARM::VST1d64Twb_fixed:
  2835. case ARM::VST1d8Qwb_fixed:
  2836. case ARM::VST1d16Qwb_fixed:
  2837. case ARM::VST1d32Qwb_fixed:
  2838. case ARM::VST1d64Qwb_fixed:
  2839. case ARM::VST2d8wb_fixed:
  2840. case ARM::VST2d16wb_fixed:
  2841. case ARM::VST2d32wb_fixed:
  2842. case ARM::VST2q8wb_fixed:
  2843. case ARM::VST2q16wb_fixed:
  2844. case ARM::VST2q32wb_fixed:
  2845. case ARM::VST2b8wb_fixed:
  2846. case ARM::VST2b16wb_fixed:
  2847. case ARM::VST2b32wb_fixed:
  2848. break;
  2849. }
  2850. // First input register
  2851. switch (Inst.getOpcode()) {
  2852. case ARM::VST1q16:
  2853. case ARM::VST1q32:
  2854. case ARM::VST1q64:
  2855. case ARM::VST1q8:
  2856. case ARM::VST1q16wb_fixed:
  2857. case ARM::VST1q16wb_register:
  2858. case ARM::VST1q32wb_fixed:
  2859. case ARM::VST1q32wb_register:
  2860. case ARM::VST1q64wb_fixed:
  2861. case ARM::VST1q64wb_register:
  2862. case ARM::VST1q8wb_fixed:
  2863. case ARM::VST1q8wb_register:
  2864. case ARM::VST2d16:
  2865. case ARM::VST2d32:
  2866. case ARM::VST2d8:
  2867. case ARM::VST2d16wb_fixed:
  2868. case ARM::VST2d16wb_register:
  2869. case ARM::VST2d32wb_fixed:
  2870. case ARM::VST2d32wb_register:
  2871. case ARM::VST2d8wb_fixed:
  2872. case ARM::VST2d8wb_register:
  2873. if (!Check(S, DecodeDPairRegisterClass(Inst, Rd, Address, Decoder)))
  2874. return MCDisassembler::Fail;
  2875. break;
  2876. case ARM::VST2b16:
  2877. case ARM::VST2b32:
  2878. case ARM::VST2b8:
  2879. case ARM::VST2b16wb_fixed:
  2880. case ARM::VST2b16wb_register:
  2881. case ARM::VST2b32wb_fixed:
  2882. case ARM::VST2b32wb_register:
  2883. case ARM::VST2b8wb_fixed:
  2884. case ARM::VST2b8wb_register:
  2885. if (!Check(S, DecodeDPairSpacedRegisterClass(Inst, Rd, Address, Decoder)))
  2886. return MCDisassembler::Fail;
  2887. break;
  2888. default:
  2889. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  2890. return MCDisassembler::Fail;
  2891. }
  2892. // Second input register
  2893. switch (Inst.getOpcode()) {
  2894. case ARM::VST3d8:
  2895. case ARM::VST3d16:
  2896. case ARM::VST3d32:
  2897. case ARM::VST3d8_UPD:
  2898. case ARM::VST3d16_UPD:
  2899. case ARM::VST3d32_UPD:
  2900. case ARM::VST4d8:
  2901. case ARM::VST4d16:
  2902. case ARM::VST4d32:
  2903. case ARM::VST4d8_UPD:
  2904. case ARM::VST4d16_UPD:
  2905. case ARM::VST4d32_UPD:
  2906. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+1)%32, Address, Decoder)))
  2907. return MCDisassembler::Fail;
  2908. break;
  2909. case ARM::VST3q8:
  2910. case ARM::VST3q16:
  2911. case ARM::VST3q32:
  2912. case ARM::VST3q8_UPD:
  2913. case ARM::VST3q16_UPD:
  2914. case ARM::VST3q32_UPD:
  2915. case ARM::VST4q8:
  2916. case ARM::VST4q16:
  2917. case ARM::VST4q32:
  2918. case ARM::VST4q8_UPD:
  2919. case ARM::VST4q16_UPD:
  2920. case ARM::VST4q32_UPD:
  2921. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder)))
  2922. return MCDisassembler::Fail;
  2923. break;
  2924. default:
  2925. break;
  2926. }
  2927. // Third input register
  2928. switch (Inst.getOpcode()) {
  2929. case ARM::VST3d8:
  2930. case ARM::VST3d16:
  2931. case ARM::VST3d32:
  2932. case ARM::VST3d8_UPD:
  2933. case ARM::VST3d16_UPD:
  2934. case ARM::VST3d32_UPD:
  2935. case ARM::VST4d8:
  2936. case ARM::VST4d16:
  2937. case ARM::VST4d32:
  2938. case ARM::VST4d8_UPD:
  2939. case ARM::VST4d16_UPD:
  2940. case ARM::VST4d32_UPD:
  2941. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2)%32, Address, Decoder)))
  2942. return MCDisassembler::Fail;
  2943. break;
  2944. case ARM::VST3q8:
  2945. case ARM::VST3q16:
  2946. case ARM::VST3q32:
  2947. case ARM::VST3q8_UPD:
  2948. case ARM::VST3q16_UPD:
  2949. case ARM::VST3q32_UPD:
  2950. case ARM::VST4q8:
  2951. case ARM::VST4q16:
  2952. case ARM::VST4q32:
  2953. case ARM::VST4q8_UPD:
  2954. case ARM::VST4q16_UPD:
  2955. case ARM::VST4q32_UPD:
  2956. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+4)%32, Address, Decoder)))
  2957. return MCDisassembler::Fail;
  2958. break;
  2959. default:
  2960. break;
  2961. }
  2962. // Fourth input register
  2963. switch (Inst.getOpcode()) {
  2964. case ARM::VST4d8:
  2965. case ARM::VST4d16:
  2966. case ARM::VST4d32:
  2967. case ARM::VST4d8_UPD:
  2968. case ARM::VST4d16_UPD:
  2969. case ARM::VST4d32_UPD:
  2970. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+3)%32, Address, Decoder)))
  2971. return MCDisassembler::Fail;
  2972. break;
  2973. case ARM::VST4q8:
  2974. case ARM::VST4q16:
  2975. case ARM::VST4q32:
  2976. case ARM::VST4q8_UPD:
  2977. case ARM::VST4q16_UPD:
  2978. case ARM::VST4q32_UPD:
  2979. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+6)%32, Address, Decoder)))
  2980. return MCDisassembler::Fail;
  2981. break;
  2982. default:
  2983. break;
  2984. }
  2985. return S;
  2986. }
  2987. static DecodeStatus DecodeVLD1DupInstruction(MCInst &Inst, unsigned Insn,
  2988. uint64_t Address, const void *Decoder) {
  2989. DecodeStatus S = MCDisassembler::Success;
  2990. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  2991. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  2992. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  2993. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  2994. unsigned align = fieldFromInstruction(Insn, 4, 1);
  2995. unsigned size = fieldFromInstruction(Insn, 6, 2);
  2996. if (size == 0 && align == 1)
  2997. return MCDisassembler::Fail;
  2998. align *= (1 << size);
  2999. switch (Inst.getOpcode()) {
  3000. case ARM::VLD1DUPq16: case ARM::VLD1DUPq32: case ARM::VLD1DUPq8:
  3001. case ARM::VLD1DUPq16wb_fixed: case ARM::VLD1DUPq16wb_register:
  3002. case ARM::VLD1DUPq32wb_fixed: case ARM::VLD1DUPq32wb_register:
  3003. case ARM::VLD1DUPq8wb_fixed: case ARM::VLD1DUPq8wb_register:
  3004. if (!Check(S, DecodeDPairRegisterClass(Inst, Rd, Address, Decoder)))
  3005. return MCDisassembler::Fail;
  3006. break;
  3007. default:
  3008. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  3009. return MCDisassembler::Fail;
  3010. break;
  3011. }
  3012. if (Rm != 0xF) {
  3013. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3014. return MCDisassembler::Fail;
  3015. }
  3016. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3017. return MCDisassembler::Fail;
  3018. Inst.addOperand(MCOperand::createImm(align));
  3019. // The fixed offset post-increment encodes Rm == 0xd. The no-writeback
  3020. // variant encodes Rm == 0xf. Anything else is a register offset post-
  3021. // increment and we need to add the register operand to the instruction.
  3022. if (Rm != 0xD && Rm != 0xF &&
  3023. !Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  3024. return MCDisassembler::Fail;
  3025. return S;
  3026. }
  3027. static DecodeStatus DecodeVLD2DupInstruction(MCInst &Inst, unsigned Insn,
  3028. uint64_t Address, const void *Decoder) {
  3029. DecodeStatus S = MCDisassembler::Success;
  3030. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  3031. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  3032. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  3033. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  3034. unsigned align = fieldFromInstruction(Insn, 4, 1);
  3035. unsigned size = 1 << fieldFromInstruction(Insn, 6, 2);
  3036. align *= 2*size;
  3037. switch (Inst.getOpcode()) {
  3038. case ARM::VLD2DUPd16: case ARM::VLD2DUPd32: case ARM::VLD2DUPd8:
  3039. case ARM::VLD2DUPd16wb_fixed: case ARM::VLD2DUPd16wb_register:
  3040. case ARM::VLD2DUPd32wb_fixed: case ARM::VLD2DUPd32wb_register:
  3041. case ARM::VLD2DUPd8wb_fixed: case ARM::VLD2DUPd8wb_register:
  3042. if (!Check(S, DecodeDPairRegisterClass(Inst, Rd, Address, Decoder)))
  3043. return MCDisassembler::Fail;
  3044. break;
  3045. case ARM::VLD2DUPd16x2: case ARM::VLD2DUPd32x2: case ARM::VLD2DUPd8x2:
  3046. case ARM::VLD2DUPd16x2wb_fixed: case ARM::VLD2DUPd16x2wb_register:
  3047. case ARM::VLD2DUPd32x2wb_fixed: case ARM::VLD2DUPd32x2wb_register:
  3048. case ARM::VLD2DUPd8x2wb_fixed: case ARM::VLD2DUPd8x2wb_register:
  3049. if (!Check(S, DecodeDPairSpacedRegisterClass(Inst, Rd, Address, Decoder)))
  3050. return MCDisassembler::Fail;
  3051. break;
  3052. default:
  3053. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  3054. return MCDisassembler::Fail;
  3055. break;
  3056. }
  3057. if (Rm != 0xF)
  3058. Inst.addOperand(MCOperand::createImm(0));
  3059. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3060. return MCDisassembler::Fail;
  3061. Inst.addOperand(MCOperand::createImm(align));
  3062. if (Rm != 0xD && Rm != 0xF) {
  3063. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  3064. return MCDisassembler::Fail;
  3065. }
  3066. return S;
  3067. }
  3068. static DecodeStatus DecodeVLD3DupInstruction(MCInst &Inst, unsigned Insn,
  3069. uint64_t Address, const void *Decoder) {
  3070. DecodeStatus S = MCDisassembler::Success;
  3071. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  3072. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  3073. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  3074. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  3075. unsigned inc = fieldFromInstruction(Insn, 5, 1) + 1;
  3076. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  3077. return MCDisassembler::Fail;
  3078. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+inc)%32, Address, Decoder)))
  3079. return MCDisassembler::Fail;
  3080. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2*inc)%32, Address, Decoder)))
  3081. return MCDisassembler::Fail;
  3082. if (Rm != 0xF) {
  3083. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3084. return MCDisassembler::Fail;
  3085. }
  3086. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3087. return MCDisassembler::Fail;
  3088. Inst.addOperand(MCOperand::createImm(0));
  3089. if (Rm == 0xD)
  3090. Inst.addOperand(MCOperand::createReg(0));
  3091. else if (Rm != 0xF) {
  3092. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  3093. return MCDisassembler::Fail;
  3094. }
  3095. return S;
  3096. }
  3097. static DecodeStatus DecodeVLD4DupInstruction(MCInst &Inst, unsigned Insn,
  3098. uint64_t Address, const void *Decoder) {
  3099. DecodeStatus S = MCDisassembler::Success;
  3100. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  3101. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  3102. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  3103. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  3104. unsigned size = fieldFromInstruction(Insn, 6, 2);
  3105. unsigned inc = fieldFromInstruction(Insn, 5, 1) + 1;
  3106. unsigned align = fieldFromInstruction(Insn, 4, 1);
  3107. if (size == 0x3) {
  3108. if (align == 0)
  3109. return MCDisassembler::Fail;
  3110. align = 16;
  3111. } else {
  3112. if (size == 2) {
  3113. align *= 8;
  3114. } else {
  3115. size = 1 << size;
  3116. align *= 4*size;
  3117. }
  3118. }
  3119. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  3120. return MCDisassembler::Fail;
  3121. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+inc)%32, Address, Decoder)))
  3122. return MCDisassembler::Fail;
  3123. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+2*inc)%32, Address, Decoder)))
  3124. return MCDisassembler::Fail;
  3125. if (!Check(S, DecodeDPRRegisterClass(Inst, (Rd+3*inc)%32, Address, Decoder)))
  3126. return MCDisassembler::Fail;
  3127. if (Rm != 0xF) {
  3128. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3129. return MCDisassembler::Fail;
  3130. }
  3131. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3132. return MCDisassembler::Fail;
  3133. Inst.addOperand(MCOperand::createImm(align));
  3134. if (Rm == 0xD)
  3135. Inst.addOperand(MCOperand::createReg(0));
  3136. else if (Rm != 0xF) {
  3137. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  3138. return MCDisassembler::Fail;
  3139. }
  3140. return S;
  3141. }
  3142. static DecodeStatus
  3143. DecodeVMOVModImmInstruction(MCInst &Inst, unsigned Insn,
  3144. uint64_t Address, const void *Decoder) {
  3145. DecodeStatus S = MCDisassembler::Success;
  3146. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  3147. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  3148. unsigned imm = fieldFromInstruction(Insn, 0, 4);
  3149. imm |= fieldFromInstruction(Insn, 16, 3) << 4;
  3150. imm |= fieldFromInstruction(Insn, 24, 1) << 7;
  3151. imm |= fieldFromInstruction(Insn, 8, 4) << 8;
  3152. imm |= fieldFromInstruction(Insn, 5, 1) << 12;
  3153. unsigned Q = fieldFromInstruction(Insn, 6, 1);
  3154. if (Q) {
  3155. if (!Check(S, DecodeQPRRegisterClass(Inst, Rd, Address, Decoder)))
  3156. return MCDisassembler::Fail;
  3157. } else {
  3158. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  3159. return MCDisassembler::Fail;
  3160. }
  3161. Inst.addOperand(MCOperand::createImm(imm));
  3162. switch (Inst.getOpcode()) {
  3163. case ARM::VORRiv4i16:
  3164. case ARM::VORRiv2i32:
  3165. case ARM::VBICiv4i16:
  3166. case ARM::VBICiv2i32:
  3167. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  3168. return MCDisassembler::Fail;
  3169. break;
  3170. case ARM::VORRiv8i16:
  3171. case ARM::VORRiv4i32:
  3172. case ARM::VBICiv8i16:
  3173. case ARM::VBICiv4i32:
  3174. if (!Check(S, DecodeQPRRegisterClass(Inst, Rd, Address, Decoder)))
  3175. return MCDisassembler::Fail;
  3176. break;
  3177. default:
  3178. break;
  3179. }
  3180. return S;
  3181. }
  3182. static DecodeStatus
  3183. DecodeMVEModImmInstruction(MCInst &Inst, unsigned Insn,
  3184. uint64_t Address, const void *Decoder) {
  3185. DecodeStatus S = MCDisassembler::Success;
  3186. unsigned Qd = ((fieldFromInstruction(Insn, 22, 1) << 3) |
  3187. fieldFromInstruction(Insn, 13, 3));
  3188. unsigned cmode = fieldFromInstruction(Insn, 8, 4);
  3189. unsigned imm = fieldFromInstruction(Insn, 0, 4);
  3190. imm |= fieldFromInstruction(Insn, 16, 3) << 4;
  3191. imm |= fieldFromInstruction(Insn, 28, 1) << 7;
  3192. imm |= cmode << 8;
  3193. imm |= fieldFromInstruction(Insn, 5, 1) << 12;
  3194. if (cmode == 0xF && Inst.getOpcode() == ARM::MVE_VMVNimmi32)
  3195. return MCDisassembler::Fail;
  3196. if (!Check(S, DecodeMQPRRegisterClass(Inst, Qd, Address, Decoder)))
  3197. return MCDisassembler::Fail;
  3198. Inst.addOperand(MCOperand::createImm(imm));
  3199. Inst.addOperand(MCOperand::createImm(ARMVCC::None));
  3200. Inst.addOperand(MCOperand::createReg(0));
  3201. Inst.addOperand(MCOperand::createImm(0));
  3202. return S;
  3203. }
  3204. static DecodeStatus DecodeMVEVADCInstruction(MCInst &Inst, unsigned Insn,
  3205. uint64_t Address, const void *Decoder) {
  3206. DecodeStatus S = MCDisassembler::Success;
  3207. unsigned Qd = fieldFromInstruction(Insn, 13, 3);
  3208. Qd |= fieldFromInstruction(Insn, 22, 1) << 3;
  3209. if (!Check(S, DecodeMQPRRegisterClass(Inst, Qd, Address, Decoder)))
  3210. return MCDisassembler::Fail;
  3211. Inst.addOperand(MCOperand::createReg(ARM::FPSCR_NZCV));
  3212. unsigned Qn = fieldFromInstruction(Insn, 17, 3);
  3213. Qn |= fieldFromInstruction(Insn, 7, 1) << 3;
  3214. if (!Check(S, DecodeMQPRRegisterClass(Inst, Qn, Address, Decoder)))
  3215. return MCDisassembler::Fail;
  3216. unsigned Qm = fieldFromInstruction(Insn, 1, 3);
  3217. Qm |= fieldFromInstruction(Insn, 5, 1) << 3;
  3218. if (!Check(S, DecodeMQPRRegisterClass(Inst, Qm, Address, Decoder)))
  3219. return MCDisassembler::Fail;
  3220. if (!fieldFromInstruction(Insn, 12, 1)) // I bit clear => need input FPSCR
  3221. Inst.addOperand(MCOperand::createReg(ARM::FPSCR_NZCV));
  3222. Inst.addOperand(MCOperand::createImm(Qd));
  3223. return S;
  3224. }
  3225. static DecodeStatus DecodeVSHLMaxInstruction(MCInst &Inst, unsigned Insn,
  3226. uint64_t Address, const void *Decoder) {
  3227. DecodeStatus S = MCDisassembler::Success;
  3228. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  3229. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  3230. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  3231. Rm |= fieldFromInstruction(Insn, 5, 1) << 4;
  3232. unsigned size = fieldFromInstruction(Insn, 18, 2);
  3233. if (!Check(S, DecodeQPRRegisterClass(Inst, Rd, Address, Decoder)))
  3234. return MCDisassembler::Fail;
  3235. if (!Check(S, DecodeDPRRegisterClass(Inst, Rm, Address, Decoder)))
  3236. return MCDisassembler::Fail;
  3237. Inst.addOperand(MCOperand::createImm(8 << size));
  3238. return S;
  3239. }
  3240. static DecodeStatus DecodeShiftRight8Imm(MCInst &Inst, unsigned Val,
  3241. uint64_t Address, const void *Decoder) {
  3242. Inst.addOperand(MCOperand::createImm(8 - Val));
  3243. return MCDisassembler::Success;
  3244. }
  3245. static DecodeStatus DecodeShiftRight16Imm(MCInst &Inst, unsigned Val,
  3246. uint64_t Address, const void *Decoder) {
  3247. Inst.addOperand(MCOperand::createImm(16 - Val));
  3248. return MCDisassembler::Success;
  3249. }
  3250. static DecodeStatus DecodeShiftRight32Imm(MCInst &Inst, unsigned Val,
  3251. uint64_t Address, const void *Decoder) {
  3252. Inst.addOperand(MCOperand::createImm(32 - Val));
  3253. return MCDisassembler::Success;
  3254. }
  3255. static DecodeStatus DecodeShiftRight64Imm(MCInst &Inst, unsigned Val,
  3256. uint64_t Address, const void *Decoder) {
  3257. Inst.addOperand(MCOperand::createImm(64 - Val));
  3258. return MCDisassembler::Success;
  3259. }
  3260. static DecodeStatus DecodeTBLInstruction(MCInst &Inst, unsigned Insn,
  3261. uint64_t Address, const void *Decoder) {
  3262. DecodeStatus S = MCDisassembler::Success;
  3263. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  3264. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  3265. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  3266. Rn |= fieldFromInstruction(Insn, 7, 1) << 4;
  3267. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  3268. Rm |= fieldFromInstruction(Insn, 5, 1) << 4;
  3269. unsigned op = fieldFromInstruction(Insn, 6, 1);
  3270. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  3271. return MCDisassembler::Fail;
  3272. if (op) {
  3273. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  3274. return MCDisassembler::Fail; // Writeback
  3275. }
  3276. switch (Inst.getOpcode()) {
  3277. case ARM::VTBL2:
  3278. case ARM::VTBX2:
  3279. if (!Check(S, DecodeDPairRegisterClass(Inst, Rn, Address, Decoder)))
  3280. return MCDisassembler::Fail;
  3281. break;
  3282. default:
  3283. if (!Check(S, DecodeDPRRegisterClass(Inst, Rn, Address, Decoder)))
  3284. return MCDisassembler::Fail;
  3285. }
  3286. if (!Check(S, DecodeDPRRegisterClass(Inst, Rm, Address, Decoder)))
  3287. return MCDisassembler::Fail;
  3288. return S;
  3289. }
  3290. static DecodeStatus DecodeThumbAddSpecialReg(MCInst &Inst, uint16_t Insn,
  3291. uint64_t Address, const void *Decoder) {
  3292. DecodeStatus S = MCDisassembler::Success;
  3293. unsigned dst = fieldFromInstruction(Insn, 8, 3);
  3294. unsigned imm = fieldFromInstruction(Insn, 0, 8);
  3295. if (!Check(S, DecodetGPRRegisterClass(Inst, dst, Address, Decoder)))
  3296. return MCDisassembler::Fail;
  3297. switch(Inst.getOpcode()) {
  3298. default:
  3299. return MCDisassembler::Fail;
  3300. case ARM::tADR:
  3301. break; // tADR does not explicitly represent the PC as an operand.
  3302. case ARM::tADDrSPi:
  3303. Inst.addOperand(MCOperand::createReg(ARM::SP));
  3304. break;
  3305. }
  3306. Inst.addOperand(MCOperand::createImm(imm));
  3307. return S;
  3308. }
  3309. static DecodeStatus DecodeThumbBROperand(MCInst &Inst, unsigned Val,
  3310. uint64_t Address, const void *Decoder) {
  3311. if (!tryAddingSymbolicOperand(Address, Address + SignExtend32<12>(Val<<1) + 4,
  3312. true, 2, Inst, Decoder))
  3313. Inst.addOperand(MCOperand::createImm(SignExtend32<12>(Val << 1)));
  3314. return MCDisassembler::Success;
  3315. }
  3316. static DecodeStatus DecodeT2BROperand(MCInst &Inst, unsigned Val,
  3317. uint64_t Address, const void *Decoder) {
  3318. if (!tryAddingSymbolicOperand(Address, Address + SignExtend32<21>(Val) + 4,
  3319. true, 4, Inst, Decoder))
  3320. Inst.addOperand(MCOperand::createImm(SignExtend32<21>(Val)));
  3321. return MCDisassembler::Success;
  3322. }
  3323. static DecodeStatus DecodeThumbCmpBROperand(MCInst &Inst, unsigned Val,
  3324. uint64_t Address, const void *Decoder) {
  3325. if (!tryAddingSymbolicOperand(Address, Address + (Val<<1) + 4,
  3326. true, 2, Inst, Decoder))
  3327. Inst.addOperand(MCOperand::createImm(Val << 1));
  3328. return MCDisassembler::Success;
  3329. }
  3330. static DecodeStatus DecodeThumbAddrModeRR(MCInst &Inst, unsigned Val,
  3331. uint64_t Address, const void *Decoder) {
  3332. DecodeStatus S = MCDisassembler::Success;
  3333. unsigned Rn = fieldFromInstruction(Val, 0, 3);
  3334. unsigned Rm = fieldFromInstruction(Val, 3, 3);
  3335. if (!Check(S, DecodetGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3336. return MCDisassembler::Fail;
  3337. if (!Check(S, DecodetGPRRegisterClass(Inst, Rm, Address, Decoder)))
  3338. return MCDisassembler::Fail;
  3339. return S;
  3340. }
  3341. static DecodeStatus DecodeThumbAddrModeIS(MCInst &Inst, unsigned Val,
  3342. uint64_t Address, const void *Decoder) {
  3343. DecodeStatus S = MCDisassembler::Success;
  3344. unsigned Rn = fieldFromInstruction(Val, 0, 3);
  3345. unsigned imm = fieldFromInstruction(Val, 3, 5);
  3346. if (!Check(S, DecodetGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3347. return MCDisassembler::Fail;
  3348. Inst.addOperand(MCOperand::createImm(imm));
  3349. return S;
  3350. }
  3351. static DecodeStatus DecodeThumbAddrModePC(MCInst &Inst, unsigned Val,
  3352. uint64_t Address, const void *Decoder) {
  3353. unsigned imm = Val << 2;
  3354. Inst.addOperand(MCOperand::createImm(imm));
  3355. tryAddingPcLoadReferenceComment(Address, (Address & ~2u) + imm + 4, Decoder);
  3356. return MCDisassembler::Success;
  3357. }
  3358. static DecodeStatus DecodeThumbAddrModeSP(MCInst &Inst, unsigned Val,
  3359. uint64_t Address, const void *Decoder) {
  3360. Inst.addOperand(MCOperand::createReg(ARM::SP));
  3361. Inst.addOperand(MCOperand::createImm(Val));
  3362. return MCDisassembler::Success;
  3363. }
  3364. static DecodeStatus DecodeT2AddrModeSOReg(MCInst &Inst, unsigned Val,
  3365. uint64_t Address, const void *Decoder) {
  3366. DecodeStatus S = MCDisassembler::Success;
  3367. unsigned Rn = fieldFromInstruction(Val, 6, 4);
  3368. unsigned Rm = fieldFromInstruction(Val, 2, 4);
  3369. unsigned imm = fieldFromInstruction(Val, 0, 2);
  3370. // Thumb stores cannot use PC as dest register.
  3371. switch (Inst.getOpcode()) {
  3372. case ARM::t2STRHs:
  3373. case ARM::t2STRBs:
  3374. case ARM::t2STRs:
  3375. if (Rn == 15)
  3376. return MCDisassembler::Fail;
  3377. break;
  3378. default:
  3379. break;
  3380. }
  3381. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3382. return MCDisassembler::Fail;
  3383. if (!Check(S, DecoderGPRRegisterClass(Inst, Rm, Address, Decoder)))
  3384. return MCDisassembler::Fail;
  3385. Inst.addOperand(MCOperand::createImm(imm));
  3386. return S;
  3387. }
  3388. static DecodeStatus DecodeT2LoadShift(MCInst &Inst, unsigned Insn,
  3389. uint64_t Address, const void *Decoder) {
  3390. DecodeStatus S = MCDisassembler::Success;
  3391. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  3392. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  3393. const FeatureBitset &featureBits =
  3394. ((const MCDisassembler*)Decoder)->getSubtargetInfo().getFeatureBits();
  3395. bool hasMP = featureBits[ARM::FeatureMP];
  3396. bool hasV7Ops = featureBits[ARM::HasV7Ops];
  3397. if (Rn == 15) {
  3398. switch (Inst.getOpcode()) {
  3399. case ARM::t2LDRBs:
  3400. Inst.setOpcode(ARM::t2LDRBpci);
  3401. break;
  3402. case ARM::t2LDRHs:
  3403. Inst.setOpcode(ARM::t2LDRHpci);
  3404. break;
  3405. case ARM::t2LDRSHs:
  3406. Inst.setOpcode(ARM::t2LDRSHpci);
  3407. break;
  3408. case ARM::t2LDRSBs:
  3409. Inst.setOpcode(ARM::t2LDRSBpci);
  3410. break;
  3411. case ARM::t2LDRs:
  3412. Inst.setOpcode(ARM::t2LDRpci);
  3413. break;
  3414. case ARM::t2PLDs:
  3415. Inst.setOpcode(ARM::t2PLDpci);
  3416. break;
  3417. case ARM::t2PLIs:
  3418. Inst.setOpcode(ARM::t2PLIpci);
  3419. break;
  3420. default:
  3421. return MCDisassembler::Fail;
  3422. }
  3423. return DecodeT2LoadLabel(Inst, Insn, Address, Decoder);
  3424. }
  3425. if (Rt == 15) {
  3426. switch (Inst.getOpcode()) {
  3427. case ARM::t2LDRSHs:
  3428. return MCDisassembler::Fail;
  3429. case ARM::t2LDRHs:
  3430. Inst.setOpcode(ARM::t2PLDWs);
  3431. break;
  3432. case ARM::t2LDRSBs:
  3433. Inst.setOpcode(ARM::t2PLIs);
  3434. break;
  3435. default:
  3436. break;
  3437. }
  3438. }
  3439. switch (Inst.getOpcode()) {
  3440. case ARM::t2PLDs:
  3441. break;
  3442. case ARM::t2PLIs:
  3443. if (!hasV7Ops)
  3444. return MCDisassembler::Fail;
  3445. break;
  3446. case ARM::t2PLDWs:
  3447. if (!hasV7Ops || !hasMP)
  3448. return MCDisassembler::Fail;
  3449. break;
  3450. default:
  3451. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
  3452. return MCDisassembler::Fail;
  3453. }
  3454. unsigned addrmode = fieldFromInstruction(Insn, 4, 2);
  3455. addrmode |= fieldFromInstruction(Insn, 0, 4) << 2;
  3456. addrmode |= fieldFromInstruction(Insn, 16, 4) << 6;
  3457. if (!Check(S, DecodeT2AddrModeSOReg(Inst, addrmode, Address, Decoder)))
  3458. return MCDisassembler::Fail;
  3459. return S;
  3460. }
  3461. static DecodeStatus DecodeT2LoadImm8(MCInst &Inst, unsigned Insn,
  3462. uint64_t Address, const void* Decoder) {
  3463. DecodeStatus S = MCDisassembler::Success;
  3464. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  3465. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  3466. unsigned U = fieldFromInstruction(Insn, 9, 1);
  3467. unsigned imm = fieldFromInstruction(Insn, 0, 8);
  3468. imm |= (U << 8);
  3469. imm |= (Rn << 9);
  3470. unsigned add = fieldFromInstruction(Insn, 9, 1);
  3471. const FeatureBitset &featureBits =
  3472. ((const MCDisassembler*)Decoder)->getSubtargetInfo().getFeatureBits();
  3473. bool hasMP = featureBits[ARM::FeatureMP];
  3474. bool hasV7Ops = featureBits[ARM::HasV7Ops];
  3475. if (Rn == 15) {
  3476. switch (Inst.getOpcode()) {
  3477. case ARM::t2LDRi8:
  3478. Inst.setOpcode(ARM::t2LDRpci);
  3479. break;
  3480. case ARM::t2LDRBi8:
  3481. Inst.setOpcode(ARM::t2LDRBpci);
  3482. break;
  3483. case ARM::t2LDRSBi8:
  3484. Inst.setOpcode(ARM::t2LDRSBpci);
  3485. break;
  3486. case ARM::t2LDRHi8:
  3487. Inst.setOpcode(ARM::t2LDRHpci);
  3488. break;
  3489. case ARM::t2LDRSHi8:
  3490. Inst.setOpcode(ARM::t2LDRSHpci);
  3491. break;
  3492. case ARM::t2PLDi8:
  3493. Inst.setOpcode(ARM::t2PLDpci);
  3494. break;
  3495. case ARM::t2PLIi8:
  3496. Inst.setOpcode(ARM::t2PLIpci);
  3497. break;
  3498. default:
  3499. return MCDisassembler::Fail;
  3500. }
  3501. return DecodeT2LoadLabel(Inst, Insn, Address, Decoder);
  3502. }
  3503. if (Rt == 15) {
  3504. switch (Inst.getOpcode()) {
  3505. case ARM::t2LDRSHi8:
  3506. return MCDisassembler::Fail;
  3507. case ARM::t2LDRHi8:
  3508. if (!add)
  3509. Inst.setOpcode(ARM::t2PLDWi8);
  3510. break;
  3511. case ARM::t2LDRSBi8:
  3512. Inst.setOpcode(ARM::t2PLIi8);
  3513. break;
  3514. default:
  3515. break;
  3516. }
  3517. }
  3518. switch (Inst.getOpcode()) {
  3519. case ARM::t2PLDi8:
  3520. break;
  3521. case ARM::t2PLIi8:
  3522. if (!hasV7Ops)
  3523. return MCDisassembler::Fail;
  3524. break;
  3525. case ARM::t2PLDWi8:
  3526. if (!hasV7Ops || !hasMP)
  3527. return MCDisassembler::Fail;
  3528. break;
  3529. default:
  3530. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
  3531. return MCDisassembler::Fail;
  3532. }
  3533. if (!Check(S, DecodeT2AddrModeImm8(Inst, imm, Address, Decoder)))
  3534. return MCDisassembler::Fail;
  3535. return S;
  3536. }
  3537. static DecodeStatus DecodeT2LoadImm12(MCInst &Inst, unsigned Insn,
  3538. uint64_t Address, const void* Decoder) {
  3539. DecodeStatus S = MCDisassembler::Success;
  3540. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  3541. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  3542. unsigned imm = fieldFromInstruction(Insn, 0, 12);
  3543. imm |= (Rn << 13);
  3544. const FeatureBitset &featureBits =
  3545. ((const MCDisassembler*)Decoder)->getSubtargetInfo().getFeatureBits();
  3546. bool hasMP = featureBits[ARM::FeatureMP];
  3547. bool hasV7Ops = featureBits[ARM::HasV7Ops];
  3548. if (Rn == 15) {
  3549. switch (Inst.getOpcode()) {
  3550. case ARM::t2LDRi12:
  3551. Inst.setOpcode(ARM::t2LDRpci);
  3552. break;
  3553. case ARM::t2LDRHi12:
  3554. Inst.setOpcode(ARM::t2LDRHpci);
  3555. break;
  3556. case ARM::t2LDRSHi12:
  3557. Inst.setOpcode(ARM::t2LDRSHpci);
  3558. break;
  3559. case ARM::t2LDRBi12:
  3560. Inst.setOpcode(ARM::t2LDRBpci);
  3561. break;
  3562. case ARM::t2LDRSBi12:
  3563. Inst.setOpcode(ARM::t2LDRSBpci);
  3564. break;
  3565. case ARM::t2PLDi12:
  3566. Inst.setOpcode(ARM::t2PLDpci);
  3567. break;
  3568. case ARM::t2PLIi12:
  3569. Inst.setOpcode(ARM::t2PLIpci);
  3570. break;
  3571. default:
  3572. return MCDisassembler::Fail;
  3573. }
  3574. return DecodeT2LoadLabel(Inst, Insn, Address, Decoder);
  3575. }
  3576. if (Rt == 15) {
  3577. switch (Inst.getOpcode()) {
  3578. case ARM::t2LDRSHi12:
  3579. return MCDisassembler::Fail;
  3580. case ARM::t2LDRHi12:
  3581. Inst.setOpcode(ARM::t2PLDWi12);
  3582. break;
  3583. case ARM::t2LDRSBi12:
  3584. Inst.setOpcode(ARM::t2PLIi12);
  3585. break;
  3586. default:
  3587. break;
  3588. }
  3589. }
  3590. switch (Inst.getOpcode()) {
  3591. case ARM::t2PLDi12:
  3592. break;
  3593. case ARM::t2PLIi12:
  3594. if (!hasV7Ops)
  3595. return MCDisassembler::Fail;
  3596. break;
  3597. case ARM::t2PLDWi12:
  3598. if (!hasV7Ops || !hasMP)
  3599. return MCDisassembler::Fail;
  3600. break;
  3601. default:
  3602. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
  3603. return MCDisassembler::Fail;
  3604. }
  3605. if (!Check(S, DecodeT2AddrModeImm12(Inst, imm, Address, Decoder)))
  3606. return MCDisassembler::Fail;
  3607. return S;
  3608. }
  3609. static DecodeStatus DecodeT2LoadT(MCInst &Inst, unsigned Insn,
  3610. uint64_t Address, const void* Decoder) {
  3611. DecodeStatus S = MCDisassembler::Success;
  3612. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  3613. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  3614. unsigned imm = fieldFromInstruction(Insn, 0, 8);
  3615. imm |= (Rn << 9);
  3616. if (Rn == 15) {
  3617. switch (Inst.getOpcode()) {
  3618. case ARM::t2LDRT:
  3619. Inst.setOpcode(ARM::t2LDRpci);
  3620. break;
  3621. case ARM::t2LDRBT:
  3622. Inst.setOpcode(ARM::t2LDRBpci);
  3623. break;
  3624. case ARM::t2LDRHT:
  3625. Inst.setOpcode(ARM::t2LDRHpci);
  3626. break;
  3627. case ARM::t2LDRSBT:
  3628. Inst.setOpcode(ARM::t2LDRSBpci);
  3629. break;
  3630. case ARM::t2LDRSHT:
  3631. Inst.setOpcode(ARM::t2LDRSHpci);
  3632. break;
  3633. default:
  3634. return MCDisassembler::Fail;
  3635. }
  3636. return DecodeT2LoadLabel(Inst, Insn, Address, Decoder);
  3637. }
  3638. if (!Check(S, DecoderGPRRegisterClass(Inst, Rt, Address, Decoder)))
  3639. return MCDisassembler::Fail;
  3640. if (!Check(S, DecodeT2AddrModeImm8(Inst, imm, Address, Decoder)))
  3641. return MCDisassembler::Fail;
  3642. return S;
  3643. }
  3644. static DecodeStatus DecodeT2LoadLabel(MCInst &Inst, unsigned Insn,
  3645. uint64_t Address, const void* Decoder) {
  3646. DecodeStatus S = MCDisassembler::Success;
  3647. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  3648. unsigned U = fieldFromInstruction(Insn, 23, 1);
  3649. int imm = fieldFromInstruction(Insn, 0, 12);
  3650. const FeatureBitset &featureBits =
  3651. ((const MCDisassembler*)Decoder)->getSubtargetInfo().getFeatureBits();
  3652. bool hasV7Ops = featureBits[ARM::HasV7Ops];
  3653. if (Rt == 15) {
  3654. switch (Inst.getOpcode()) {
  3655. case ARM::t2LDRBpci:
  3656. case ARM::t2LDRHpci:
  3657. Inst.setOpcode(ARM::t2PLDpci);
  3658. break;
  3659. case ARM::t2LDRSBpci:
  3660. Inst.setOpcode(ARM::t2PLIpci);
  3661. break;
  3662. case ARM::t2LDRSHpci:
  3663. return MCDisassembler::Fail;
  3664. default:
  3665. break;
  3666. }
  3667. }
  3668. switch(Inst.getOpcode()) {
  3669. case ARM::t2PLDpci:
  3670. break;
  3671. case ARM::t2PLIpci:
  3672. if (!hasV7Ops)
  3673. return MCDisassembler::Fail;
  3674. break;
  3675. default:
  3676. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
  3677. return MCDisassembler::Fail;
  3678. }
  3679. if (!U) {
  3680. // Special case for #-0.
  3681. if (imm == 0)
  3682. imm = INT32_MIN;
  3683. else
  3684. imm = -imm;
  3685. }
  3686. Inst.addOperand(MCOperand::createImm(imm));
  3687. return S;
  3688. }
  3689. static DecodeStatus DecodeT2Imm8S4(MCInst &Inst, unsigned Val,
  3690. uint64_t Address, const void *Decoder) {
  3691. if (Val == 0)
  3692. Inst.addOperand(MCOperand::createImm(INT32_MIN));
  3693. else {
  3694. int imm = Val & 0xFF;
  3695. if (!(Val & 0x100)) imm *= -1;
  3696. Inst.addOperand(MCOperand::createImm(imm * 4));
  3697. }
  3698. return MCDisassembler::Success;
  3699. }
  3700. static DecodeStatus DecodeT2Imm7S4(MCInst &Inst, unsigned Val, uint64_t Address,
  3701. const void *Decoder) {
  3702. if (Val == 0)
  3703. Inst.addOperand(MCOperand::createImm(INT32_MIN));
  3704. else {
  3705. int imm = Val & 0x7F;
  3706. if (!(Val & 0x80))
  3707. imm *= -1;
  3708. Inst.addOperand(MCOperand::createImm(imm * 4));
  3709. }
  3710. return MCDisassembler::Success;
  3711. }
  3712. static DecodeStatus DecodeT2AddrModeImm8s4(MCInst &Inst, unsigned Val,
  3713. uint64_t Address, const void *Decoder) {
  3714. DecodeStatus S = MCDisassembler::Success;
  3715. unsigned Rn = fieldFromInstruction(Val, 9, 4);
  3716. unsigned imm = fieldFromInstruction(Val, 0, 9);
  3717. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3718. return MCDisassembler::Fail;
  3719. if (!Check(S, DecodeT2Imm8S4(Inst, imm, Address, Decoder)))
  3720. return MCDisassembler::Fail;
  3721. return S;
  3722. }
  3723. static DecodeStatus DecodeT2AddrModeImm7s4(MCInst &Inst, unsigned Val,
  3724. uint64_t Address,
  3725. const void *Decoder) {
  3726. DecodeStatus S = MCDisassembler::Success;
  3727. unsigned Rn = fieldFromInstruction(Val, 8, 4);
  3728. unsigned imm = fieldFromInstruction(Val, 0, 8);
  3729. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
  3730. return MCDisassembler::Fail;
  3731. if (!Check(S, DecodeT2Imm7S4(Inst, imm, Address, Decoder)))
  3732. return MCDisassembler::Fail;
  3733. return S;
  3734. }
  3735. static DecodeStatus DecodeT2AddrModeImm0_1020s4(MCInst &Inst,unsigned Val,
  3736. uint64_t Address, const void *Decoder) {
  3737. DecodeStatus S = MCDisassembler::Success;
  3738. unsigned Rn = fieldFromInstruction(Val, 8, 4);
  3739. unsigned imm = fieldFromInstruction(Val, 0, 8);
  3740. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
  3741. return MCDisassembler::Fail;
  3742. Inst.addOperand(MCOperand::createImm(imm));
  3743. return S;
  3744. }
  3745. static DecodeStatus DecodeT2Imm8(MCInst &Inst, unsigned Val,
  3746. uint64_t Address, const void *Decoder) {
  3747. int imm = Val & 0xFF;
  3748. if (Val == 0)
  3749. imm = INT32_MIN;
  3750. else if (!(Val & 0x100))
  3751. imm *= -1;
  3752. Inst.addOperand(MCOperand::createImm(imm));
  3753. return MCDisassembler::Success;
  3754. }
  3755. template<int shift>
  3756. static DecodeStatus DecodeT2Imm7(MCInst &Inst, unsigned Val,
  3757. uint64_t Address, const void *Decoder) {
  3758. int imm = Val & 0x7F;
  3759. if (Val == 0)
  3760. imm = INT32_MIN;
  3761. else if (!(Val & 0x80))
  3762. imm *= -1;
  3763. if (imm != INT32_MIN)
  3764. imm *= (1U << shift);
  3765. Inst.addOperand(MCOperand::createImm(imm));
  3766. return MCDisassembler::Success;
  3767. }
  3768. static DecodeStatus DecodeT2AddrModeImm8(MCInst &Inst, unsigned Val,
  3769. uint64_t Address, const void *Decoder) {
  3770. DecodeStatus S = MCDisassembler::Success;
  3771. unsigned Rn = fieldFromInstruction(Val, 9, 4);
  3772. unsigned imm = fieldFromInstruction(Val, 0, 9);
  3773. // Thumb stores cannot use PC as dest register.
  3774. switch (Inst.getOpcode()) {
  3775. case ARM::t2STRT:
  3776. case ARM::t2STRBT:
  3777. case ARM::t2STRHT:
  3778. case ARM::t2STRi8:
  3779. case ARM::t2STRHi8:
  3780. case ARM::t2STRBi8:
  3781. if (Rn == 15)
  3782. return MCDisassembler::Fail;
  3783. break;
  3784. default:
  3785. break;
  3786. }
  3787. // Some instructions always use an additive offset.
  3788. switch (Inst.getOpcode()) {
  3789. case ARM::t2LDRT:
  3790. case ARM::t2LDRBT:
  3791. case ARM::t2LDRHT:
  3792. case ARM::t2LDRSBT:
  3793. case ARM::t2LDRSHT:
  3794. case ARM::t2STRT:
  3795. case ARM::t2STRBT:
  3796. case ARM::t2STRHT:
  3797. imm |= 0x100;
  3798. break;
  3799. default:
  3800. break;
  3801. }
  3802. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3803. return MCDisassembler::Fail;
  3804. if (!Check(S, DecodeT2Imm8(Inst, imm, Address, Decoder)))
  3805. return MCDisassembler::Fail;
  3806. return S;
  3807. }
  3808. template<int shift>
  3809. static DecodeStatus DecodeTAddrModeImm7(MCInst &Inst, unsigned Val,
  3810. uint64_t Address,
  3811. const void *Decoder) {
  3812. DecodeStatus S = MCDisassembler::Success;
  3813. unsigned Rn = fieldFromInstruction(Val, 8, 3);
  3814. unsigned imm = fieldFromInstruction(Val, 0, 8);
  3815. if (!Check(S, DecodetGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3816. return MCDisassembler::Fail;
  3817. if (!Check(S, DecodeT2Imm7<shift>(Inst, imm, Address, Decoder)))
  3818. return MCDisassembler::Fail;
  3819. return S;
  3820. }
  3821. template<int shift, int WriteBack>
  3822. static DecodeStatus DecodeT2AddrModeImm7(MCInst &Inst, unsigned Val,
  3823. uint64_t Address,
  3824. const void *Decoder) {
  3825. DecodeStatus S = MCDisassembler::Success;
  3826. unsigned Rn = fieldFromInstruction(Val, 8, 4);
  3827. unsigned imm = fieldFromInstruction(Val, 0, 8);
  3828. if (WriteBack) {
  3829. if (!Check(S, DecoderGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3830. return MCDisassembler::Fail;
  3831. } else if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
  3832. return MCDisassembler::Fail;
  3833. if (!Check(S, DecodeT2Imm7<shift>(Inst, imm, Address, Decoder)))
  3834. return MCDisassembler::Fail;
  3835. return S;
  3836. }
  3837. static DecodeStatus DecodeT2LdStPre(MCInst &Inst, unsigned Insn,
  3838. uint64_t Address, const void *Decoder) {
  3839. DecodeStatus S = MCDisassembler::Success;
  3840. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  3841. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  3842. unsigned addr = fieldFromInstruction(Insn, 0, 8);
  3843. addr |= fieldFromInstruction(Insn, 9, 1) << 8;
  3844. addr |= Rn << 9;
  3845. unsigned load = fieldFromInstruction(Insn, 20, 1);
  3846. if (Rn == 15) {
  3847. switch (Inst.getOpcode()) {
  3848. case ARM::t2LDR_PRE:
  3849. case ARM::t2LDR_POST:
  3850. Inst.setOpcode(ARM::t2LDRpci);
  3851. break;
  3852. case ARM::t2LDRB_PRE:
  3853. case ARM::t2LDRB_POST:
  3854. Inst.setOpcode(ARM::t2LDRBpci);
  3855. break;
  3856. case ARM::t2LDRH_PRE:
  3857. case ARM::t2LDRH_POST:
  3858. Inst.setOpcode(ARM::t2LDRHpci);
  3859. break;
  3860. case ARM::t2LDRSB_PRE:
  3861. case ARM::t2LDRSB_POST:
  3862. if (Rt == 15)
  3863. Inst.setOpcode(ARM::t2PLIpci);
  3864. else
  3865. Inst.setOpcode(ARM::t2LDRSBpci);
  3866. break;
  3867. case ARM::t2LDRSH_PRE:
  3868. case ARM::t2LDRSH_POST:
  3869. Inst.setOpcode(ARM::t2LDRSHpci);
  3870. break;
  3871. default:
  3872. return MCDisassembler::Fail;
  3873. }
  3874. return DecodeT2LoadLabel(Inst, Insn, Address, Decoder);
  3875. }
  3876. if (!load) {
  3877. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3878. return MCDisassembler::Fail;
  3879. }
  3880. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
  3881. return MCDisassembler::Fail;
  3882. if (load) {
  3883. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3884. return MCDisassembler::Fail;
  3885. }
  3886. if (!Check(S, DecodeT2AddrModeImm8(Inst, addr, Address, Decoder)))
  3887. return MCDisassembler::Fail;
  3888. return S;
  3889. }
  3890. static DecodeStatus DecodeT2AddrModeImm12(MCInst &Inst, unsigned Val,
  3891. uint64_t Address, const void *Decoder) {
  3892. DecodeStatus S = MCDisassembler::Success;
  3893. unsigned Rn = fieldFromInstruction(Val, 13, 4);
  3894. unsigned imm = fieldFromInstruction(Val, 0, 12);
  3895. // Thumb stores cannot use PC as dest register.
  3896. switch (Inst.getOpcode()) {
  3897. case ARM::t2STRi12:
  3898. case ARM::t2STRBi12:
  3899. case ARM::t2STRHi12:
  3900. if (Rn == 15)
  3901. return MCDisassembler::Fail;
  3902. break;
  3903. default:
  3904. break;
  3905. }
  3906. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  3907. return MCDisassembler::Fail;
  3908. Inst.addOperand(MCOperand::createImm(imm));
  3909. return S;
  3910. }
  3911. static DecodeStatus DecodeThumbAddSPImm(MCInst &Inst, uint16_t Insn,
  3912. uint64_t Address, const void *Decoder) {
  3913. unsigned imm = fieldFromInstruction(Insn, 0, 7);
  3914. Inst.addOperand(MCOperand::createReg(ARM::SP));
  3915. Inst.addOperand(MCOperand::createReg(ARM::SP));
  3916. Inst.addOperand(MCOperand::createImm(imm));
  3917. return MCDisassembler::Success;
  3918. }
  3919. static DecodeStatus DecodeThumbAddSPReg(MCInst &Inst, uint16_t Insn,
  3920. uint64_t Address, const void *Decoder) {
  3921. DecodeStatus S = MCDisassembler::Success;
  3922. if (Inst.getOpcode() == ARM::tADDrSP) {
  3923. unsigned Rdm = fieldFromInstruction(Insn, 0, 3);
  3924. Rdm |= fieldFromInstruction(Insn, 7, 1) << 3;
  3925. if (!Check(S, DecodeGPRRegisterClass(Inst, Rdm, Address, Decoder)))
  3926. return MCDisassembler::Fail;
  3927. Inst.addOperand(MCOperand::createReg(ARM::SP));
  3928. if (!Check(S, DecodeGPRRegisterClass(Inst, Rdm, Address, Decoder)))
  3929. return MCDisassembler::Fail;
  3930. } else if (Inst.getOpcode() == ARM::tADDspr) {
  3931. unsigned Rm = fieldFromInstruction(Insn, 3, 4);
  3932. Inst.addOperand(MCOperand::createReg(ARM::SP));
  3933. Inst.addOperand(MCOperand::createReg(ARM::SP));
  3934. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  3935. return MCDisassembler::Fail;
  3936. }
  3937. return S;
  3938. }
  3939. static DecodeStatus DecodeThumbCPS(MCInst &Inst, uint16_t Insn,
  3940. uint64_t Address, const void *Decoder) {
  3941. unsigned imod = fieldFromInstruction(Insn, 4, 1) | 0x2;
  3942. unsigned flags = fieldFromInstruction(Insn, 0, 3);
  3943. Inst.addOperand(MCOperand::createImm(imod));
  3944. Inst.addOperand(MCOperand::createImm(flags));
  3945. return MCDisassembler::Success;
  3946. }
  3947. static DecodeStatus DecodePostIdxReg(MCInst &Inst, unsigned Insn,
  3948. uint64_t Address, const void *Decoder) {
  3949. DecodeStatus S = MCDisassembler::Success;
  3950. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  3951. unsigned add = fieldFromInstruction(Insn, 4, 1);
  3952. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rm, Address, Decoder)))
  3953. return MCDisassembler::Fail;
  3954. Inst.addOperand(MCOperand::createImm(add));
  3955. return S;
  3956. }
  3957. static DecodeStatus DecodeMveAddrModeRQ(MCInst &Inst, unsigned Insn,
  3958. uint64_t Address, const void *Decoder) {
  3959. DecodeStatus S = MCDisassembler::Success;
  3960. unsigned Rn = fieldFromInstruction(Insn, 3, 4);
  3961. unsigned Qm = fieldFromInstruction(Insn, 0, 3);
  3962. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
  3963. return MCDisassembler::Fail;
  3964. if (!Check(S, DecodeMQPRRegisterClass(Inst, Qm, Address, Decoder)))
  3965. return MCDisassembler::Fail;
  3966. return S;
  3967. }
  3968. template<int shift>
  3969. static DecodeStatus DecodeMveAddrModeQ(MCInst &Inst, unsigned Insn,
  3970. uint64_t Address, const void *Decoder) {
  3971. DecodeStatus S = MCDisassembler::Success;
  3972. unsigned Qm = fieldFromInstruction(Insn, 8, 3);
  3973. int imm = fieldFromInstruction(Insn, 0, 7);
  3974. if (!Check(S, DecodeMQPRRegisterClass(Inst, Qm, Address, Decoder)))
  3975. return MCDisassembler::Fail;
  3976. if(!fieldFromInstruction(Insn, 7, 1)) {
  3977. if (imm == 0)
  3978. imm = INT32_MIN; // indicate -0
  3979. else
  3980. imm *= -1;
  3981. }
  3982. if (imm != INT32_MIN)
  3983. imm *= (1U << shift);
  3984. Inst.addOperand(MCOperand::createImm(imm));
  3985. return S;
  3986. }
  3987. static DecodeStatus DecodeThumbBLXOffset(MCInst &Inst, unsigned Val,
  3988. uint64_t Address, const void *Decoder) {
  3989. // Val is passed in as S:J1:J2:imm10H:imm10L:'0'
  3990. // Note only one trailing zero not two. Also the J1 and J2 values are from
  3991. // the encoded instruction. So here change to I1 and I2 values via:
  3992. // I1 = NOT(J1 EOR S);
  3993. // I2 = NOT(J2 EOR S);
  3994. // and build the imm32 with two trailing zeros as documented:
  3995. // imm32 = SignExtend(S:I1:I2:imm10H:imm10L:'00', 32);
  3996. unsigned S = (Val >> 23) & 1;
  3997. unsigned J1 = (Val >> 22) & 1;
  3998. unsigned J2 = (Val >> 21) & 1;
  3999. unsigned I1 = !(J1 ^ S);
  4000. unsigned I2 = !(J2 ^ S);
  4001. unsigned tmp = (Val & ~0x600000) | (I1 << 22) | (I2 << 21);
  4002. int imm32 = SignExtend32<25>(tmp << 1);
  4003. if (!tryAddingSymbolicOperand(Address,
  4004. (Address & ~2u) + imm32 + 4,
  4005. true, 4, Inst, Decoder))
  4006. Inst.addOperand(MCOperand::createImm(imm32));
  4007. return MCDisassembler::Success;
  4008. }
  4009. static DecodeStatus DecodeCoprocessor(MCInst &Inst, unsigned Val,
  4010. uint64_t Address, const void *Decoder) {
  4011. if (Val == 0xA || Val == 0xB)
  4012. return MCDisassembler::Fail;
  4013. const FeatureBitset &featureBits =
  4014. ((const MCDisassembler*)Decoder)->getSubtargetInfo().getFeatureBits();
  4015. if (!isValidCoprocessorNumber(Val, featureBits))
  4016. return MCDisassembler::Fail;
  4017. Inst.addOperand(MCOperand::createImm(Val));
  4018. return MCDisassembler::Success;
  4019. }
  4020. static DecodeStatus
  4021. DecodeThumbTableBranch(MCInst &Inst, unsigned Insn,
  4022. uint64_t Address, const void *Decoder) {
  4023. const FeatureBitset &FeatureBits =
  4024. ((const MCDisassembler*)Decoder)->getSubtargetInfo().getFeatureBits();
  4025. DecodeStatus S = MCDisassembler::Success;
  4026. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4027. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  4028. if (Rn == 13 && !FeatureBits[ARM::HasV8Ops]) S = MCDisassembler::SoftFail;
  4029. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4030. return MCDisassembler::Fail;
  4031. if (!Check(S, DecoderGPRRegisterClass(Inst, Rm, Address, Decoder)))
  4032. return MCDisassembler::Fail;
  4033. return S;
  4034. }
  4035. static DecodeStatus
  4036. DecodeThumb2BCCInstruction(MCInst &Inst, unsigned Insn,
  4037. uint64_t Address, const void *Decoder) {
  4038. DecodeStatus S = MCDisassembler::Success;
  4039. unsigned pred = fieldFromInstruction(Insn, 22, 4);
  4040. if (pred == 0xE || pred == 0xF) {
  4041. unsigned opc = fieldFromInstruction(Insn, 4, 28);
  4042. switch (opc) {
  4043. default:
  4044. return MCDisassembler::Fail;
  4045. case 0xf3bf8f4:
  4046. Inst.setOpcode(ARM::t2DSB);
  4047. break;
  4048. case 0xf3bf8f5:
  4049. Inst.setOpcode(ARM::t2DMB);
  4050. break;
  4051. case 0xf3bf8f6:
  4052. Inst.setOpcode(ARM::t2ISB);
  4053. break;
  4054. }
  4055. unsigned imm = fieldFromInstruction(Insn, 0, 4);
  4056. return DecodeMemBarrierOption(Inst, imm, Address, Decoder);
  4057. }
  4058. unsigned brtarget = fieldFromInstruction(Insn, 0, 11) << 1;
  4059. brtarget |= fieldFromInstruction(Insn, 11, 1) << 19;
  4060. brtarget |= fieldFromInstruction(Insn, 13, 1) << 18;
  4061. brtarget |= fieldFromInstruction(Insn, 16, 6) << 12;
  4062. brtarget |= fieldFromInstruction(Insn, 26, 1) << 20;
  4063. if (!Check(S, DecodeT2BROperand(Inst, brtarget, Address, Decoder)))
  4064. return MCDisassembler::Fail;
  4065. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  4066. return MCDisassembler::Fail;
  4067. return S;
  4068. }
  4069. // Decode a shifted immediate operand. These basically consist
  4070. // of an 8-bit value, and a 4-bit directive that specifies either
  4071. // a splat operation or a rotation.
  4072. static DecodeStatus DecodeT2SOImm(MCInst &Inst, unsigned Val,
  4073. uint64_t Address, const void *Decoder) {
  4074. unsigned ctrl = fieldFromInstruction(Val, 10, 2);
  4075. if (ctrl == 0) {
  4076. unsigned byte = fieldFromInstruction(Val, 8, 2);
  4077. unsigned imm = fieldFromInstruction(Val, 0, 8);
  4078. switch (byte) {
  4079. case 0:
  4080. Inst.addOperand(MCOperand::createImm(imm));
  4081. break;
  4082. case 1:
  4083. Inst.addOperand(MCOperand::createImm((imm << 16) | imm));
  4084. break;
  4085. case 2:
  4086. Inst.addOperand(MCOperand::createImm((imm << 24) | (imm << 8)));
  4087. break;
  4088. case 3:
  4089. Inst.addOperand(MCOperand::createImm((imm << 24) | (imm << 16) |
  4090. (imm << 8) | imm));
  4091. break;
  4092. }
  4093. } else {
  4094. unsigned unrot = fieldFromInstruction(Val, 0, 7) | 0x80;
  4095. unsigned rot = fieldFromInstruction(Val, 7, 5);
  4096. unsigned imm = (unrot >> rot) | (unrot << ((32-rot)&31));
  4097. Inst.addOperand(MCOperand::createImm(imm));
  4098. }
  4099. return MCDisassembler::Success;
  4100. }
  4101. static DecodeStatus
  4102. DecodeThumbBCCTargetOperand(MCInst &Inst, unsigned Val,
  4103. uint64_t Address, const void *Decoder) {
  4104. if (!tryAddingSymbolicOperand(Address, Address + SignExtend32<9>(Val<<1) + 4,
  4105. true, 2, Inst, Decoder))
  4106. Inst.addOperand(MCOperand::createImm(SignExtend32<9>(Val << 1)));
  4107. return MCDisassembler::Success;
  4108. }
  4109. static DecodeStatus DecodeThumbBLTargetOperand(MCInst &Inst, unsigned Val,
  4110. uint64_t Address,
  4111. const void *Decoder) {
  4112. // Val is passed in as S:J1:J2:imm10:imm11
  4113. // Note no trailing zero after imm11. Also the J1 and J2 values are from
  4114. // the encoded instruction. So here change to I1 and I2 values via:
  4115. // I1 = NOT(J1 EOR S);
  4116. // I2 = NOT(J2 EOR S);
  4117. // and build the imm32 with one trailing zero as documented:
  4118. // imm32 = SignExtend(S:I1:I2:imm10:imm11:'0', 32);
  4119. unsigned S = (Val >> 23) & 1;
  4120. unsigned J1 = (Val >> 22) & 1;
  4121. unsigned J2 = (Val >> 21) & 1;
  4122. unsigned I1 = !(J1 ^ S);
  4123. unsigned I2 = !(J2 ^ S);
  4124. unsigned tmp = (Val & ~0x600000) | (I1 << 22) | (I2 << 21);
  4125. int imm32 = SignExtend32<25>(tmp << 1);
  4126. if (!tryAddingSymbolicOperand(Address, Address + imm32 + 4,
  4127. true, 4, Inst, Decoder))
  4128. Inst.addOperand(MCOperand::createImm(imm32));
  4129. return MCDisassembler::Success;
  4130. }
  4131. static DecodeStatus DecodeMemBarrierOption(MCInst &Inst, unsigned Val,
  4132. uint64_t Address, const void *Decoder) {
  4133. if (Val & ~0xf)
  4134. return MCDisassembler::Fail;
  4135. Inst.addOperand(MCOperand::createImm(Val));
  4136. return MCDisassembler::Success;
  4137. }
  4138. static DecodeStatus DecodeInstSyncBarrierOption(MCInst &Inst, unsigned Val,
  4139. uint64_t Address, const void *Decoder) {
  4140. if (Val & ~0xf)
  4141. return MCDisassembler::Fail;
  4142. Inst.addOperand(MCOperand::createImm(Val));
  4143. return MCDisassembler::Success;
  4144. }
  4145. static DecodeStatus DecodeMSRMask(MCInst &Inst, unsigned Val,
  4146. uint64_t Address, const void *Decoder) {
  4147. DecodeStatus S = MCDisassembler::Success;
  4148. const FeatureBitset &FeatureBits =
  4149. ((const MCDisassembler*)Decoder)->getSubtargetInfo().getFeatureBits();
  4150. if (FeatureBits[ARM::FeatureMClass]) {
  4151. unsigned ValLow = Val & 0xff;
  4152. // Validate the SYSm value first.
  4153. switch (ValLow) {
  4154. case 0: // apsr
  4155. case 1: // iapsr
  4156. case 2: // eapsr
  4157. case 3: // xpsr
  4158. case 5: // ipsr
  4159. case 6: // epsr
  4160. case 7: // iepsr
  4161. case 8: // msp
  4162. case 9: // psp
  4163. case 16: // primask
  4164. case 20: // control
  4165. break;
  4166. case 17: // basepri
  4167. case 18: // basepri_max
  4168. case 19: // faultmask
  4169. if (!(FeatureBits[ARM::HasV7Ops]))
  4170. // Values basepri, basepri_max and faultmask are only valid for v7m.
  4171. return MCDisassembler::Fail;
  4172. break;
  4173. case 0x8a: // msplim_ns
  4174. case 0x8b: // psplim_ns
  4175. case 0x91: // basepri_ns
  4176. case 0x93: // faultmask_ns
  4177. if (!(FeatureBits[ARM::HasV8MMainlineOps]))
  4178. return MCDisassembler::Fail;
  4179. LLVM_FALLTHROUGH;
  4180. case 10: // msplim
  4181. case 11: // psplim
  4182. case 0x88: // msp_ns
  4183. case 0x89: // psp_ns
  4184. case 0x90: // primask_ns
  4185. case 0x94: // control_ns
  4186. case 0x98: // sp_ns
  4187. if (!(FeatureBits[ARM::Feature8MSecExt]))
  4188. return MCDisassembler::Fail;
  4189. break;
  4190. case 0x20: // pac_key_p_0
  4191. case 0x21: // pac_key_p_1
  4192. case 0x22: // pac_key_p_2
  4193. case 0x23: // pac_key_p_3
  4194. case 0x24: // pac_key_u_0
  4195. case 0x25: // pac_key_u_1
  4196. case 0x26: // pac_key_u_2
  4197. case 0x27: // pac_key_u_3
  4198. case 0xa0: // pac_key_p_0_ns
  4199. case 0xa1: // pac_key_p_1_ns
  4200. case 0xa2: // pac_key_p_2_ns
  4201. case 0xa3: // pac_key_p_3_ns
  4202. case 0xa4: // pac_key_u_0_ns
  4203. case 0xa5: // pac_key_u_1_ns
  4204. case 0xa6: // pac_key_u_2_ns
  4205. case 0xa7: // pac_key_u_3_ns
  4206. if (!(FeatureBits[ARM::FeaturePACBTI]))
  4207. return MCDisassembler::Fail;
  4208. break;
  4209. default:
  4210. // Architecturally defined as unpredictable
  4211. S = MCDisassembler::SoftFail;
  4212. break;
  4213. }
  4214. if (Inst.getOpcode() == ARM::t2MSR_M) {
  4215. unsigned Mask = fieldFromInstruction(Val, 10, 2);
  4216. if (!(FeatureBits[ARM::HasV7Ops])) {
  4217. // The ARMv6-M MSR bits {11-10} can be only 0b10, other values are
  4218. // unpredictable.
  4219. if (Mask != 2)
  4220. S = MCDisassembler::SoftFail;
  4221. }
  4222. else {
  4223. // The ARMv7-M architecture stores an additional 2-bit mask value in
  4224. // MSR bits {11-10}. The mask is used only with apsr, iapsr, eapsr and
  4225. // xpsr, it has to be 0b10 in other cases. Bit mask{1} indicates if
  4226. // the NZCVQ bits should be moved by the instruction. Bit mask{0}
  4227. // indicates the move for the GE{3:0} bits, the mask{0} bit can be set
  4228. // only if the processor includes the DSP extension.
  4229. if (Mask == 0 || (Mask != 2 && ValLow > 3) ||
  4230. (!(FeatureBits[ARM::FeatureDSP]) && (Mask & 1)))
  4231. S = MCDisassembler::SoftFail;
  4232. }
  4233. }
  4234. } else {
  4235. // A/R class
  4236. if (Val == 0)
  4237. return MCDisassembler::Fail;
  4238. }
  4239. Inst.addOperand(MCOperand::createImm(Val));
  4240. return S;
  4241. }
  4242. static DecodeStatus DecodeBankedReg(MCInst &Inst, unsigned Val,
  4243. uint64_t Address, const void *Decoder) {
  4244. unsigned R = fieldFromInstruction(Val, 5, 1);
  4245. unsigned SysM = fieldFromInstruction(Val, 0, 5);
  4246. // The table of encodings for these banked registers comes from B9.2.3 of the
  4247. // ARM ARM. There are patterns, but nothing regular enough to make this logic
  4248. // neater. So by fiat, these values are UNPREDICTABLE:
  4249. if (!ARMBankedReg::lookupBankedRegByEncoding((R << 5) | SysM))
  4250. return MCDisassembler::Fail;
  4251. Inst.addOperand(MCOperand::createImm(Val));
  4252. return MCDisassembler::Success;
  4253. }
  4254. static DecodeStatus DecodeDoubleRegLoad(MCInst &Inst, unsigned Insn,
  4255. uint64_t Address, const void *Decoder) {
  4256. DecodeStatus S = MCDisassembler::Success;
  4257. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  4258. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4259. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  4260. if (Rn == 0xF)
  4261. S = MCDisassembler::SoftFail;
  4262. if (!Check(S, DecodeGPRPairRegisterClass(Inst, Rt, Address, Decoder)))
  4263. return MCDisassembler::Fail;
  4264. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4265. return MCDisassembler::Fail;
  4266. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  4267. return MCDisassembler::Fail;
  4268. return S;
  4269. }
  4270. static DecodeStatus DecodeDoubleRegStore(MCInst &Inst, unsigned Insn,
  4271. uint64_t Address,
  4272. const void *Decoder) {
  4273. DecodeStatus S = MCDisassembler::Success;
  4274. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  4275. unsigned Rt = fieldFromInstruction(Insn, 0, 4);
  4276. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4277. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  4278. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rd, Address, Decoder)))
  4279. return MCDisassembler::Fail;
  4280. if (Rn == 0xF || Rd == Rn || Rd == Rt || Rd == Rt+1)
  4281. S = MCDisassembler::SoftFail;
  4282. if (!Check(S, DecodeGPRPairRegisterClass(Inst, Rt, Address, Decoder)))
  4283. return MCDisassembler::Fail;
  4284. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4285. return MCDisassembler::Fail;
  4286. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  4287. return MCDisassembler::Fail;
  4288. return S;
  4289. }
  4290. static DecodeStatus DecodeLDRPreImm(MCInst &Inst, unsigned Insn,
  4291. uint64_t Address, const void *Decoder) {
  4292. DecodeStatus S = MCDisassembler::Success;
  4293. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4294. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  4295. unsigned imm = fieldFromInstruction(Insn, 0, 12);
  4296. imm |= fieldFromInstruction(Insn, 16, 4) << 13;
  4297. imm |= fieldFromInstruction(Insn, 23, 1) << 12;
  4298. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  4299. if (Rn == 0xF || Rn == Rt) S = MCDisassembler::SoftFail;
  4300. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
  4301. return MCDisassembler::Fail;
  4302. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4303. return MCDisassembler::Fail;
  4304. if (!Check(S, DecodeAddrModeImm12Operand(Inst, imm, Address, Decoder)))
  4305. return MCDisassembler::Fail;
  4306. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  4307. return MCDisassembler::Fail;
  4308. return S;
  4309. }
  4310. static DecodeStatus DecodeLDRPreReg(MCInst &Inst, unsigned Insn,
  4311. uint64_t Address, const void *Decoder) {
  4312. DecodeStatus S = MCDisassembler::Success;
  4313. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4314. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  4315. unsigned imm = fieldFromInstruction(Insn, 0, 12);
  4316. imm |= fieldFromInstruction(Insn, 16, 4) << 13;
  4317. imm |= fieldFromInstruction(Insn, 23, 1) << 12;
  4318. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  4319. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  4320. if (Rn == 0xF || Rn == Rt) S = MCDisassembler::SoftFail;
  4321. if (Rm == 0xF) S = MCDisassembler::SoftFail;
  4322. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
  4323. return MCDisassembler::Fail;
  4324. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4325. return MCDisassembler::Fail;
  4326. if (!Check(S, DecodeSORegMemOperand(Inst, imm, Address, Decoder)))
  4327. return MCDisassembler::Fail;
  4328. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  4329. return MCDisassembler::Fail;
  4330. return S;
  4331. }
  4332. static DecodeStatus DecodeSTRPreImm(MCInst &Inst, unsigned Insn,
  4333. uint64_t Address, const void *Decoder) {
  4334. DecodeStatus S = MCDisassembler::Success;
  4335. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4336. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  4337. unsigned imm = fieldFromInstruction(Insn, 0, 12);
  4338. imm |= fieldFromInstruction(Insn, 16, 4) << 13;
  4339. imm |= fieldFromInstruction(Insn, 23, 1) << 12;
  4340. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  4341. if (Rn == 0xF || Rn == Rt) S = MCDisassembler::SoftFail;
  4342. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4343. return MCDisassembler::Fail;
  4344. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
  4345. return MCDisassembler::Fail;
  4346. if (!Check(S, DecodeAddrModeImm12Operand(Inst, imm, Address, Decoder)))
  4347. return MCDisassembler::Fail;
  4348. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  4349. return MCDisassembler::Fail;
  4350. return S;
  4351. }
  4352. static DecodeStatus DecodeSTRPreReg(MCInst &Inst, unsigned Insn,
  4353. uint64_t Address, const void *Decoder) {
  4354. DecodeStatus S = MCDisassembler::Success;
  4355. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4356. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  4357. unsigned imm = fieldFromInstruction(Insn, 0, 12);
  4358. imm |= fieldFromInstruction(Insn, 16, 4) << 13;
  4359. imm |= fieldFromInstruction(Insn, 23, 1) << 12;
  4360. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  4361. if (Rn == 0xF || Rn == Rt) S = MCDisassembler::SoftFail;
  4362. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4363. return MCDisassembler::Fail;
  4364. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
  4365. return MCDisassembler::Fail;
  4366. if (!Check(S, DecodeSORegMemOperand(Inst, imm, Address, Decoder)))
  4367. return MCDisassembler::Fail;
  4368. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  4369. return MCDisassembler::Fail;
  4370. return S;
  4371. }
  4372. static DecodeStatus DecodeVLD1LN(MCInst &Inst, unsigned Insn,
  4373. uint64_t Address, const void *Decoder) {
  4374. DecodeStatus S = MCDisassembler::Success;
  4375. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4376. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  4377. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  4378. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  4379. unsigned size = fieldFromInstruction(Insn, 10, 2);
  4380. unsigned align = 0;
  4381. unsigned index = 0;
  4382. switch (size) {
  4383. default:
  4384. return MCDisassembler::Fail;
  4385. case 0:
  4386. if (fieldFromInstruction(Insn, 4, 1))
  4387. return MCDisassembler::Fail; // UNDEFINED
  4388. index = fieldFromInstruction(Insn, 5, 3);
  4389. break;
  4390. case 1:
  4391. if (fieldFromInstruction(Insn, 5, 1))
  4392. return MCDisassembler::Fail; // UNDEFINED
  4393. index = fieldFromInstruction(Insn, 6, 2);
  4394. if (fieldFromInstruction(Insn, 4, 1))
  4395. align = 2;
  4396. break;
  4397. case 2:
  4398. if (fieldFromInstruction(Insn, 6, 1))
  4399. return MCDisassembler::Fail; // UNDEFINED
  4400. index = fieldFromInstruction(Insn, 7, 1);
  4401. switch (fieldFromInstruction(Insn, 4, 2)) {
  4402. case 0 :
  4403. align = 0; break;
  4404. case 3:
  4405. align = 4; break;
  4406. default:
  4407. return MCDisassembler::Fail;
  4408. }
  4409. break;
  4410. }
  4411. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  4412. return MCDisassembler::Fail;
  4413. if (Rm != 0xF) { // Writeback
  4414. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4415. return MCDisassembler::Fail;
  4416. }
  4417. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4418. return MCDisassembler::Fail;
  4419. Inst.addOperand(MCOperand::createImm(align));
  4420. if (Rm != 0xF) {
  4421. if (Rm != 0xD) {
  4422. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  4423. return MCDisassembler::Fail;
  4424. } else
  4425. Inst.addOperand(MCOperand::createReg(0));
  4426. }
  4427. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  4428. return MCDisassembler::Fail;
  4429. Inst.addOperand(MCOperand::createImm(index));
  4430. return S;
  4431. }
  4432. static DecodeStatus DecodeVST1LN(MCInst &Inst, unsigned Insn,
  4433. uint64_t Address, const void *Decoder) {
  4434. DecodeStatus S = MCDisassembler::Success;
  4435. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4436. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  4437. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  4438. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  4439. unsigned size = fieldFromInstruction(Insn, 10, 2);
  4440. unsigned align = 0;
  4441. unsigned index = 0;
  4442. switch (size) {
  4443. default:
  4444. return MCDisassembler::Fail;
  4445. case 0:
  4446. if (fieldFromInstruction(Insn, 4, 1))
  4447. return MCDisassembler::Fail; // UNDEFINED
  4448. index = fieldFromInstruction(Insn, 5, 3);
  4449. break;
  4450. case 1:
  4451. if (fieldFromInstruction(Insn, 5, 1))
  4452. return MCDisassembler::Fail; // UNDEFINED
  4453. index = fieldFromInstruction(Insn, 6, 2);
  4454. if (fieldFromInstruction(Insn, 4, 1))
  4455. align = 2;
  4456. break;
  4457. case 2:
  4458. if (fieldFromInstruction(Insn, 6, 1))
  4459. return MCDisassembler::Fail; // UNDEFINED
  4460. index = fieldFromInstruction(Insn, 7, 1);
  4461. switch (fieldFromInstruction(Insn, 4, 2)) {
  4462. case 0:
  4463. align = 0; break;
  4464. case 3:
  4465. align = 4; break;
  4466. default:
  4467. return MCDisassembler::Fail;
  4468. }
  4469. break;
  4470. }
  4471. if (Rm != 0xF) { // Writeback
  4472. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4473. return MCDisassembler::Fail;
  4474. }
  4475. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4476. return MCDisassembler::Fail;
  4477. Inst.addOperand(MCOperand::createImm(align));
  4478. if (Rm != 0xF) {
  4479. if (Rm != 0xD) {
  4480. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  4481. return MCDisassembler::Fail;
  4482. } else
  4483. Inst.addOperand(MCOperand::createReg(0));
  4484. }
  4485. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  4486. return MCDisassembler::Fail;
  4487. Inst.addOperand(MCOperand::createImm(index));
  4488. return S;
  4489. }
  4490. static DecodeStatus DecodeVLD2LN(MCInst &Inst, unsigned Insn,
  4491. uint64_t Address, const void *Decoder) {
  4492. DecodeStatus S = MCDisassembler::Success;
  4493. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4494. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  4495. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  4496. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  4497. unsigned size = fieldFromInstruction(Insn, 10, 2);
  4498. unsigned align = 0;
  4499. unsigned index = 0;
  4500. unsigned inc = 1;
  4501. switch (size) {
  4502. default:
  4503. return MCDisassembler::Fail;
  4504. case 0:
  4505. index = fieldFromInstruction(Insn, 5, 3);
  4506. if (fieldFromInstruction(Insn, 4, 1))
  4507. align = 2;
  4508. break;
  4509. case 1:
  4510. index = fieldFromInstruction(Insn, 6, 2);
  4511. if (fieldFromInstruction(Insn, 4, 1))
  4512. align = 4;
  4513. if (fieldFromInstruction(Insn, 5, 1))
  4514. inc = 2;
  4515. break;
  4516. case 2:
  4517. if (fieldFromInstruction(Insn, 5, 1))
  4518. return MCDisassembler::Fail; // UNDEFINED
  4519. index = fieldFromInstruction(Insn, 7, 1);
  4520. if (fieldFromInstruction(Insn, 4, 1) != 0)
  4521. align = 8;
  4522. if (fieldFromInstruction(Insn, 6, 1))
  4523. inc = 2;
  4524. break;
  4525. }
  4526. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  4527. return MCDisassembler::Fail;
  4528. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
  4529. return MCDisassembler::Fail;
  4530. if (Rm != 0xF) { // Writeback
  4531. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4532. return MCDisassembler::Fail;
  4533. }
  4534. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4535. return MCDisassembler::Fail;
  4536. Inst.addOperand(MCOperand::createImm(align));
  4537. if (Rm != 0xF) {
  4538. if (Rm != 0xD) {
  4539. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  4540. return MCDisassembler::Fail;
  4541. } else
  4542. Inst.addOperand(MCOperand::createReg(0));
  4543. }
  4544. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  4545. return MCDisassembler::Fail;
  4546. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
  4547. return MCDisassembler::Fail;
  4548. Inst.addOperand(MCOperand::createImm(index));
  4549. return S;
  4550. }
  4551. static DecodeStatus DecodeVST2LN(MCInst &Inst, unsigned Insn,
  4552. uint64_t Address, const void *Decoder) {
  4553. DecodeStatus S = MCDisassembler::Success;
  4554. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4555. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  4556. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  4557. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  4558. unsigned size = fieldFromInstruction(Insn, 10, 2);
  4559. unsigned align = 0;
  4560. unsigned index = 0;
  4561. unsigned inc = 1;
  4562. switch (size) {
  4563. default:
  4564. return MCDisassembler::Fail;
  4565. case 0:
  4566. index = fieldFromInstruction(Insn, 5, 3);
  4567. if (fieldFromInstruction(Insn, 4, 1))
  4568. align = 2;
  4569. break;
  4570. case 1:
  4571. index = fieldFromInstruction(Insn, 6, 2);
  4572. if (fieldFromInstruction(Insn, 4, 1))
  4573. align = 4;
  4574. if (fieldFromInstruction(Insn, 5, 1))
  4575. inc = 2;
  4576. break;
  4577. case 2:
  4578. if (fieldFromInstruction(Insn, 5, 1))
  4579. return MCDisassembler::Fail; // UNDEFINED
  4580. index = fieldFromInstruction(Insn, 7, 1);
  4581. if (fieldFromInstruction(Insn, 4, 1) != 0)
  4582. align = 8;
  4583. if (fieldFromInstruction(Insn, 6, 1))
  4584. inc = 2;
  4585. break;
  4586. }
  4587. if (Rm != 0xF) { // Writeback
  4588. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4589. return MCDisassembler::Fail;
  4590. }
  4591. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4592. return MCDisassembler::Fail;
  4593. Inst.addOperand(MCOperand::createImm(align));
  4594. if (Rm != 0xF) {
  4595. if (Rm != 0xD) {
  4596. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  4597. return MCDisassembler::Fail;
  4598. } else
  4599. Inst.addOperand(MCOperand::createReg(0));
  4600. }
  4601. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  4602. return MCDisassembler::Fail;
  4603. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
  4604. return MCDisassembler::Fail;
  4605. Inst.addOperand(MCOperand::createImm(index));
  4606. return S;
  4607. }
  4608. static DecodeStatus DecodeVLD3LN(MCInst &Inst, unsigned Insn,
  4609. uint64_t Address, const void *Decoder) {
  4610. DecodeStatus S = MCDisassembler::Success;
  4611. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4612. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  4613. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  4614. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  4615. unsigned size = fieldFromInstruction(Insn, 10, 2);
  4616. unsigned align = 0;
  4617. unsigned index = 0;
  4618. unsigned inc = 1;
  4619. switch (size) {
  4620. default:
  4621. return MCDisassembler::Fail;
  4622. case 0:
  4623. if (fieldFromInstruction(Insn, 4, 1))
  4624. return MCDisassembler::Fail; // UNDEFINED
  4625. index = fieldFromInstruction(Insn, 5, 3);
  4626. break;
  4627. case 1:
  4628. if (fieldFromInstruction(Insn, 4, 1))
  4629. return MCDisassembler::Fail; // UNDEFINED
  4630. index = fieldFromInstruction(Insn, 6, 2);
  4631. if (fieldFromInstruction(Insn, 5, 1))
  4632. inc = 2;
  4633. break;
  4634. case 2:
  4635. if (fieldFromInstruction(Insn, 4, 2))
  4636. return MCDisassembler::Fail; // UNDEFINED
  4637. index = fieldFromInstruction(Insn, 7, 1);
  4638. if (fieldFromInstruction(Insn, 6, 1))
  4639. inc = 2;
  4640. break;
  4641. }
  4642. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  4643. return MCDisassembler::Fail;
  4644. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
  4645. return MCDisassembler::Fail;
  4646. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
  4647. return MCDisassembler::Fail;
  4648. if (Rm != 0xF) { // Writeback
  4649. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4650. return MCDisassembler::Fail;
  4651. }
  4652. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4653. return MCDisassembler::Fail;
  4654. Inst.addOperand(MCOperand::createImm(align));
  4655. if (Rm != 0xF) {
  4656. if (Rm != 0xD) {
  4657. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  4658. return MCDisassembler::Fail;
  4659. } else
  4660. Inst.addOperand(MCOperand::createReg(0));
  4661. }
  4662. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  4663. return MCDisassembler::Fail;
  4664. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
  4665. return MCDisassembler::Fail;
  4666. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
  4667. return MCDisassembler::Fail;
  4668. Inst.addOperand(MCOperand::createImm(index));
  4669. return S;
  4670. }
  4671. static DecodeStatus DecodeVST3LN(MCInst &Inst, unsigned Insn,
  4672. uint64_t Address, const void *Decoder) {
  4673. DecodeStatus S = MCDisassembler::Success;
  4674. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4675. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  4676. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  4677. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  4678. unsigned size = fieldFromInstruction(Insn, 10, 2);
  4679. unsigned align = 0;
  4680. unsigned index = 0;
  4681. unsigned inc = 1;
  4682. switch (size) {
  4683. default:
  4684. return MCDisassembler::Fail;
  4685. case 0:
  4686. if (fieldFromInstruction(Insn, 4, 1))
  4687. return MCDisassembler::Fail; // UNDEFINED
  4688. index = fieldFromInstruction(Insn, 5, 3);
  4689. break;
  4690. case 1:
  4691. if (fieldFromInstruction(Insn, 4, 1))
  4692. return MCDisassembler::Fail; // UNDEFINED
  4693. index = fieldFromInstruction(Insn, 6, 2);
  4694. if (fieldFromInstruction(Insn, 5, 1))
  4695. inc = 2;
  4696. break;
  4697. case 2:
  4698. if (fieldFromInstruction(Insn, 4, 2))
  4699. return MCDisassembler::Fail; // UNDEFINED
  4700. index = fieldFromInstruction(Insn, 7, 1);
  4701. if (fieldFromInstruction(Insn, 6, 1))
  4702. inc = 2;
  4703. break;
  4704. }
  4705. if (Rm != 0xF) { // Writeback
  4706. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4707. return MCDisassembler::Fail;
  4708. }
  4709. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4710. return MCDisassembler::Fail;
  4711. Inst.addOperand(MCOperand::createImm(align));
  4712. if (Rm != 0xF) {
  4713. if (Rm != 0xD) {
  4714. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  4715. return MCDisassembler::Fail;
  4716. } else
  4717. Inst.addOperand(MCOperand::createReg(0));
  4718. }
  4719. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  4720. return MCDisassembler::Fail;
  4721. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
  4722. return MCDisassembler::Fail;
  4723. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
  4724. return MCDisassembler::Fail;
  4725. Inst.addOperand(MCOperand::createImm(index));
  4726. return S;
  4727. }
  4728. static DecodeStatus DecodeVLD4LN(MCInst &Inst, unsigned Insn,
  4729. uint64_t Address, const void *Decoder) {
  4730. DecodeStatus S = MCDisassembler::Success;
  4731. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4732. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  4733. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  4734. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  4735. unsigned size = fieldFromInstruction(Insn, 10, 2);
  4736. unsigned align = 0;
  4737. unsigned index = 0;
  4738. unsigned inc = 1;
  4739. switch (size) {
  4740. default:
  4741. return MCDisassembler::Fail;
  4742. case 0:
  4743. if (fieldFromInstruction(Insn, 4, 1))
  4744. align = 4;
  4745. index = fieldFromInstruction(Insn, 5, 3);
  4746. break;
  4747. case 1:
  4748. if (fieldFromInstruction(Insn, 4, 1))
  4749. align = 8;
  4750. index = fieldFromInstruction(Insn, 6, 2);
  4751. if (fieldFromInstruction(Insn, 5, 1))
  4752. inc = 2;
  4753. break;
  4754. case 2:
  4755. switch (fieldFromInstruction(Insn, 4, 2)) {
  4756. case 0:
  4757. align = 0; break;
  4758. case 3:
  4759. return MCDisassembler::Fail;
  4760. default:
  4761. align = 4 << fieldFromInstruction(Insn, 4, 2); break;
  4762. }
  4763. index = fieldFromInstruction(Insn, 7, 1);
  4764. if (fieldFromInstruction(Insn, 6, 1))
  4765. inc = 2;
  4766. break;
  4767. }
  4768. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  4769. return MCDisassembler::Fail;
  4770. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
  4771. return MCDisassembler::Fail;
  4772. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
  4773. return MCDisassembler::Fail;
  4774. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+3*inc, Address, Decoder)))
  4775. return MCDisassembler::Fail;
  4776. if (Rm != 0xF) { // Writeback
  4777. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4778. return MCDisassembler::Fail;
  4779. }
  4780. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4781. return MCDisassembler::Fail;
  4782. Inst.addOperand(MCOperand::createImm(align));
  4783. if (Rm != 0xF) {
  4784. if (Rm != 0xD) {
  4785. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  4786. return MCDisassembler::Fail;
  4787. } else
  4788. Inst.addOperand(MCOperand::createReg(0));
  4789. }
  4790. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  4791. return MCDisassembler::Fail;
  4792. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
  4793. return MCDisassembler::Fail;
  4794. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
  4795. return MCDisassembler::Fail;
  4796. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+3*inc, Address, Decoder)))
  4797. return MCDisassembler::Fail;
  4798. Inst.addOperand(MCOperand::createImm(index));
  4799. return S;
  4800. }
  4801. static DecodeStatus DecodeVST4LN(MCInst &Inst, unsigned Insn,
  4802. uint64_t Address, const void *Decoder) {
  4803. DecodeStatus S = MCDisassembler::Success;
  4804. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4805. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  4806. unsigned Rd = fieldFromInstruction(Insn, 12, 4);
  4807. Rd |= fieldFromInstruction(Insn, 22, 1) << 4;
  4808. unsigned size = fieldFromInstruction(Insn, 10, 2);
  4809. unsigned align = 0;
  4810. unsigned index = 0;
  4811. unsigned inc = 1;
  4812. switch (size) {
  4813. default:
  4814. return MCDisassembler::Fail;
  4815. case 0:
  4816. if (fieldFromInstruction(Insn, 4, 1))
  4817. align = 4;
  4818. index = fieldFromInstruction(Insn, 5, 3);
  4819. break;
  4820. case 1:
  4821. if (fieldFromInstruction(Insn, 4, 1))
  4822. align = 8;
  4823. index = fieldFromInstruction(Insn, 6, 2);
  4824. if (fieldFromInstruction(Insn, 5, 1))
  4825. inc = 2;
  4826. break;
  4827. case 2:
  4828. switch (fieldFromInstruction(Insn, 4, 2)) {
  4829. case 0:
  4830. align = 0; break;
  4831. case 3:
  4832. return MCDisassembler::Fail;
  4833. default:
  4834. align = 4 << fieldFromInstruction(Insn, 4, 2); break;
  4835. }
  4836. index = fieldFromInstruction(Insn, 7, 1);
  4837. if (fieldFromInstruction(Insn, 6, 1))
  4838. inc = 2;
  4839. break;
  4840. }
  4841. if (Rm != 0xF) { // Writeback
  4842. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4843. return MCDisassembler::Fail;
  4844. }
  4845. if (!Check(S, DecodeGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4846. return MCDisassembler::Fail;
  4847. Inst.addOperand(MCOperand::createImm(align));
  4848. if (Rm != 0xF) {
  4849. if (Rm != 0xD) {
  4850. if (!Check(S, DecodeGPRRegisterClass(Inst, Rm, Address, Decoder)))
  4851. return MCDisassembler::Fail;
  4852. } else
  4853. Inst.addOperand(MCOperand::createReg(0));
  4854. }
  4855. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd, Address, Decoder)))
  4856. return MCDisassembler::Fail;
  4857. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+inc, Address, Decoder)))
  4858. return MCDisassembler::Fail;
  4859. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+2*inc, Address, Decoder)))
  4860. return MCDisassembler::Fail;
  4861. if (!Check(S, DecodeDPRRegisterClass(Inst, Rd+3*inc, Address, Decoder)))
  4862. return MCDisassembler::Fail;
  4863. Inst.addOperand(MCOperand::createImm(index));
  4864. return S;
  4865. }
  4866. static DecodeStatus DecodeVMOVSRR(MCInst &Inst, unsigned Insn,
  4867. uint64_t Address, const void *Decoder) {
  4868. DecodeStatus S = MCDisassembler::Success;
  4869. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  4870. unsigned Rt2 = fieldFromInstruction(Insn, 16, 4);
  4871. unsigned Rm = fieldFromInstruction(Insn, 5, 1);
  4872. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  4873. Rm |= fieldFromInstruction(Insn, 0, 4) << 1;
  4874. if (Rt == 0xF || Rt2 == 0xF || Rm == 0x1F)
  4875. S = MCDisassembler::SoftFail;
  4876. if (!Check(S, DecodeSPRRegisterClass(Inst, Rm , Address, Decoder)))
  4877. return MCDisassembler::Fail;
  4878. if (!Check(S, DecodeSPRRegisterClass(Inst, Rm+1, Address, Decoder)))
  4879. return MCDisassembler::Fail;
  4880. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt , Address, Decoder)))
  4881. return MCDisassembler::Fail;
  4882. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt2 , Address, Decoder)))
  4883. return MCDisassembler::Fail;
  4884. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  4885. return MCDisassembler::Fail;
  4886. return S;
  4887. }
  4888. static DecodeStatus DecodeVMOVRRS(MCInst &Inst, unsigned Insn,
  4889. uint64_t Address, const void *Decoder) {
  4890. DecodeStatus S = MCDisassembler::Success;
  4891. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  4892. unsigned Rt2 = fieldFromInstruction(Insn, 16, 4);
  4893. unsigned Rm = fieldFromInstruction(Insn, 5, 1);
  4894. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  4895. Rm |= fieldFromInstruction(Insn, 0, 4) << 1;
  4896. if (Rt == 0xF || Rt2 == 0xF || Rm == 0x1F)
  4897. S = MCDisassembler::SoftFail;
  4898. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt , Address, Decoder)))
  4899. return MCDisassembler::Fail;
  4900. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt2 , Address, Decoder)))
  4901. return MCDisassembler::Fail;
  4902. if (!Check(S, DecodeSPRRegisterClass(Inst, Rm , Address, Decoder)))
  4903. return MCDisassembler::Fail;
  4904. if (!Check(S, DecodeSPRRegisterClass(Inst, Rm+1, Address, Decoder)))
  4905. return MCDisassembler::Fail;
  4906. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  4907. return MCDisassembler::Fail;
  4908. return S;
  4909. }
  4910. static DecodeStatus DecodeIT(MCInst &Inst, unsigned Insn,
  4911. uint64_t Address, const void *Decoder) {
  4912. DecodeStatus S = MCDisassembler::Success;
  4913. unsigned pred = fieldFromInstruction(Insn, 4, 4);
  4914. unsigned mask = fieldFromInstruction(Insn, 0, 4);
  4915. if (pred == 0xF) {
  4916. pred = 0xE;
  4917. S = MCDisassembler::SoftFail;
  4918. }
  4919. if (mask == 0x0)
  4920. return MCDisassembler::Fail;
  4921. // IT masks are encoded as a sequence of replacement low-order bits
  4922. // for the condition code. So if the low bit of the starting
  4923. // condition code is 1, then we have to flip all the bits above the
  4924. // terminating bit (which is the lowest 1 bit).
  4925. if (pred & 1) {
  4926. unsigned LowBit = mask & -mask;
  4927. unsigned BitsAboveLowBit = 0xF & (-LowBit << 1);
  4928. mask ^= BitsAboveLowBit;
  4929. }
  4930. Inst.addOperand(MCOperand::createImm(pred));
  4931. Inst.addOperand(MCOperand::createImm(mask));
  4932. return S;
  4933. }
  4934. static DecodeStatus
  4935. DecodeT2LDRDPreInstruction(MCInst &Inst, unsigned Insn,
  4936. uint64_t Address, const void *Decoder) {
  4937. DecodeStatus S = MCDisassembler::Success;
  4938. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  4939. unsigned Rt2 = fieldFromInstruction(Insn, 8, 4);
  4940. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4941. unsigned addr = fieldFromInstruction(Insn, 0, 8);
  4942. unsigned W = fieldFromInstruction(Insn, 21, 1);
  4943. unsigned U = fieldFromInstruction(Insn, 23, 1);
  4944. unsigned P = fieldFromInstruction(Insn, 24, 1);
  4945. bool writeback = (W == 1) | (P == 0);
  4946. addr |= (U << 8) | (Rn << 9);
  4947. if (writeback && (Rn == Rt || Rn == Rt2))
  4948. Check(S, MCDisassembler::SoftFail);
  4949. if (Rt == Rt2)
  4950. Check(S, MCDisassembler::SoftFail);
  4951. // Rt
  4952. if (!Check(S, DecoderGPRRegisterClass(Inst, Rt, Address, Decoder)))
  4953. return MCDisassembler::Fail;
  4954. // Rt2
  4955. if (!Check(S, DecoderGPRRegisterClass(Inst, Rt2, Address, Decoder)))
  4956. return MCDisassembler::Fail;
  4957. // Writeback operand
  4958. if (!Check(S, DecoderGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4959. return MCDisassembler::Fail;
  4960. // addr
  4961. if (!Check(S, DecodeT2AddrModeImm8s4(Inst, addr, Address, Decoder)))
  4962. return MCDisassembler::Fail;
  4963. return S;
  4964. }
  4965. static DecodeStatus
  4966. DecodeT2STRDPreInstruction(MCInst &Inst, unsigned Insn,
  4967. uint64_t Address, const void *Decoder) {
  4968. DecodeStatus S = MCDisassembler::Success;
  4969. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  4970. unsigned Rt2 = fieldFromInstruction(Insn, 8, 4);
  4971. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  4972. unsigned addr = fieldFromInstruction(Insn, 0, 8);
  4973. unsigned W = fieldFromInstruction(Insn, 21, 1);
  4974. unsigned U = fieldFromInstruction(Insn, 23, 1);
  4975. unsigned P = fieldFromInstruction(Insn, 24, 1);
  4976. bool writeback = (W == 1) | (P == 0);
  4977. addr |= (U << 8) | (Rn << 9);
  4978. if (writeback && (Rn == Rt || Rn == Rt2))
  4979. Check(S, MCDisassembler::SoftFail);
  4980. // Writeback operand
  4981. if (!Check(S, DecoderGPRRegisterClass(Inst, Rn, Address, Decoder)))
  4982. return MCDisassembler::Fail;
  4983. // Rt
  4984. if (!Check(S, DecoderGPRRegisterClass(Inst, Rt, Address, Decoder)))
  4985. return MCDisassembler::Fail;
  4986. // Rt2
  4987. if (!Check(S, DecoderGPRRegisterClass(Inst, Rt2, Address, Decoder)))
  4988. return MCDisassembler::Fail;
  4989. // addr
  4990. if (!Check(S, DecodeT2AddrModeImm8s4(Inst, addr, Address, Decoder)))
  4991. return MCDisassembler::Fail;
  4992. return S;
  4993. }
  4994. static DecodeStatus DecodeT2Adr(MCInst &Inst, uint32_t Insn,
  4995. uint64_t Address, const void *Decoder) {
  4996. unsigned sign1 = fieldFromInstruction(Insn, 21, 1);
  4997. unsigned sign2 = fieldFromInstruction(Insn, 23, 1);
  4998. if (sign1 != sign2) return MCDisassembler::Fail;
  4999. const unsigned Rd = fieldFromInstruction(Insn, 8, 4);
  5000. assert(Inst.getNumOperands() == 0 && "We should receive an empty Inst");
  5001. DecodeStatus S = DecoderGPRRegisterClass(Inst, Rd, Address, Decoder);
  5002. unsigned Val = fieldFromInstruction(Insn, 0, 8);
  5003. Val |= fieldFromInstruction(Insn, 12, 3) << 8;
  5004. Val |= fieldFromInstruction(Insn, 26, 1) << 11;
  5005. // If sign, then it is decreasing the address.
  5006. if (sign1) {
  5007. // Following ARMv7 Architecture Manual, when the offset
  5008. // is zero, it is decoded as a subw, not as a adr.w
  5009. if (!Val) {
  5010. Inst.setOpcode(ARM::t2SUBri12);
  5011. Inst.addOperand(MCOperand::createReg(ARM::PC));
  5012. } else
  5013. Val = -Val;
  5014. }
  5015. Inst.addOperand(MCOperand::createImm(Val));
  5016. return S;
  5017. }
  5018. static DecodeStatus DecodeT2ShifterImmOperand(MCInst &Inst, uint32_t Val,
  5019. uint64_t Address,
  5020. const void *Decoder) {
  5021. DecodeStatus S = MCDisassembler::Success;
  5022. // Shift of "asr #32" is not allowed in Thumb2 mode.
  5023. if (Val == 0x20) S = MCDisassembler::Fail;
  5024. Inst.addOperand(MCOperand::createImm(Val));
  5025. return S;
  5026. }
  5027. static DecodeStatus DecodeSwap(MCInst &Inst, unsigned Insn,
  5028. uint64_t Address, const void *Decoder) {
  5029. unsigned Rt = fieldFromInstruction(Insn, 12, 4);
  5030. unsigned Rt2 = fieldFromInstruction(Insn, 0, 4);
  5031. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  5032. unsigned pred = fieldFromInstruction(Insn, 28, 4);
  5033. if (pred == 0xF)
  5034. return DecodeCPSInstruction(Inst, Insn, Address, Decoder);
  5035. DecodeStatus S = MCDisassembler::Success;
  5036. if (Rt == Rn || Rn == Rt2)
  5037. S = MCDisassembler::SoftFail;
  5038. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt, Address, Decoder)))
  5039. return MCDisassembler::Fail;
  5040. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt2, Address, Decoder)))
  5041. return MCDisassembler::Fail;
  5042. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
  5043. return MCDisassembler::Fail;
  5044. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  5045. return MCDisassembler::Fail;
  5046. return S;
  5047. }
  5048. static DecodeStatus DecodeVCVTD(MCInst &Inst, unsigned Insn,
  5049. uint64_t Address, const void *Decoder) {
  5050. const FeatureBitset &featureBits =
  5051. ((const MCDisassembler *)Decoder)->getSubtargetInfo().getFeatureBits();
  5052. bool hasFullFP16 = featureBits[ARM::FeatureFullFP16];
  5053. unsigned Vd = (fieldFromInstruction(Insn, 12, 4) << 0);
  5054. Vd |= (fieldFromInstruction(Insn, 22, 1) << 4);
  5055. unsigned Vm = (fieldFromInstruction(Insn, 0, 4) << 0);
  5056. Vm |= (fieldFromInstruction(Insn, 5, 1) << 4);
  5057. unsigned imm = fieldFromInstruction(Insn, 16, 6);
  5058. unsigned cmode = fieldFromInstruction(Insn, 8, 4);
  5059. unsigned op = fieldFromInstruction(Insn, 5, 1);
  5060. DecodeStatus S = MCDisassembler::Success;
  5061. // If the top 3 bits of imm are clear, this is a VMOV (immediate)
  5062. if (!(imm & 0x38)) {
  5063. if (cmode == 0xF) {
  5064. if (op == 1) return MCDisassembler::Fail;
  5065. Inst.setOpcode(ARM::VMOVv2f32);
  5066. }
  5067. if (hasFullFP16) {
  5068. if (cmode == 0xE) {
  5069. if (op == 1) {
  5070. Inst.setOpcode(ARM::VMOVv1i64);
  5071. } else {
  5072. Inst.setOpcode(ARM::VMOVv8i8);
  5073. }
  5074. }
  5075. if (cmode == 0xD) {
  5076. if (op == 1) {
  5077. Inst.setOpcode(ARM::VMVNv2i32);
  5078. } else {
  5079. Inst.setOpcode(ARM::VMOVv2i32);
  5080. }
  5081. }
  5082. if (cmode == 0xC) {
  5083. if (op == 1) {
  5084. Inst.setOpcode(ARM::VMVNv2i32);
  5085. } else {
  5086. Inst.setOpcode(ARM::VMOVv2i32);
  5087. }
  5088. }
  5089. }
  5090. return DecodeVMOVModImmInstruction(Inst, Insn, Address, Decoder);
  5091. }
  5092. if (!(imm & 0x20)) return MCDisassembler::Fail;
  5093. if (!Check(S, DecodeDPRRegisterClass(Inst, Vd, Address, Decoder)))
  5094. return MCDisassembler::Fail;
  5095. if (!Check(S, DecodeDPRRegisterClass(Inst, Vm, Address, Decoder)))
  5096. return MCDisassembler::Fail;
  5097. Inst.addOperand(MCOperand::createImm(64 - imm));
  5098. return S;
  5099. }
  5100. static DecodeStatus DecodeVCVTQ(MCInst &Inst, unsigned Insn,
  5101. uint64_t Address, const void *Decoder) {
  5102. const FeatureBitset &featureBits =
  5103. ((const MCDisassembler *)Decoder)->getSubtargetInfo().getFeatureBits();
  5104. bool hasFullFP16 = featureBits[ARM::FeatureFullFP16];
  5105. unsigned Vd = (fieldFromInstruction(Insn, 12, 4) << 0);
  5106. Vd |= (fieldFromInstruction(Insn, 22, 1) << 4);
  5107. unsigned Vm = (fieldFromInstruction(Insn, 0, 4) << 0);
  5108. Vm |= (fieldFromInstruction(Insn, 5, 1) << 4);
  5109. unsigned imm = fieldFromInstruction(Insn, 16, 6);
  5110. unsigned cmode = fieldFromInstruction(Insn, 8, 4);
  5111. unsigned op = fieldFromInstruction(Insn, 5, 1);
  5112. DecodeStatus S = MCDisassembler::Success;
  5113. // If the top 3 bits of imm are clear, this is a VMOV (immediate)
  5114. if (!(imm & 0x38)) {
  5115. if (cmode == 0xF) {
  5116. if (op == 1) return MCDisassembler::Fail;
  5117. Inst.setOpcode(ARM::VMOVv4f32);
  5118. }
  5119. if (hasFullFP16) {
  5120. if (cmode == 0xE) {
  5121. if (op == 1) {
  5122. Inst.setOpcode(ARM::VMOVv2i64);
  5123. } else {
  5124. Inst.setOpcode(ARM::VMOVv16i8);
  5125. }
  5126. }
  5127. if (cmode == 0xD) {
  5128. if (op == 1) {
  5129. Inst.setOpcode(ARM::VMVNv4i32);
  5130. } else {
  5131. Inst.setOpcode(ARM::VMOVv4i32);
  5132. }
  5133. }
  5134. if (cmode == 0xC) {
  5135. if (op == 1) {
  5136. Inst.setOpcode(ARM::VMVNv4i32);
  5137. } else {
  5138. Inst.setOpcode(ARM::VMOVv4i32);
  5139. }
  5140. }
  5141. }
  5142. return DecodeVMOVModImmInstruction(Inst, Insn, Address, Decoder);
  5143. }
  5144. if (!(imm & 0x20)) return MCDisassembler::Fail;
  5145. if (!Check(S, DecodeQPRRegisterClass(Inst, Vd, Address, Decoder)))
  5146. return MCDisassembler::Fail;
  5147. if (!Check(S, DecodeQPRRegisterClass(Inst, Vm, Address, Decoder)))
  5148. return MCDisassembler::Fail;
  5149. Inst.addOperand(MCOperand::createImm(64 - imm));
  5150. return S;
  5151. }
  5152. static DecodeStatus DecodeNEONComplexLane64Instruction(MCInst &Inst,
  5153. unsigned Insn,
  5154. uint64_t Address,
  5155. const void *Decoder) {
  5156. unsigned Vd = (fieldFromInstruction(Insn, 12, 4) << 0);
  5157. Vd |= (fieldFromInstruction(Insn, 22, 1) << 4);
  5158. unsigned Vn = (fieldFromInstruction(Insn, 16, 4) << 0);
  5159. Vn |= (fieldFromInstruction(Insn, 7, 1) << 4);
  5160. unsigned Vm = (fieldFromInstruction(Insn, 0, 4) << 0);
  5161. Vm |= (fieldFromInstruction(Insn, 5, 1) << 4);
  5162. unsigned q = (fieldFromInstruction(Insn, 6, 1) << 0);
  5163. unsigned rotate = (fieldFromInstruction(Insn, 20, 2) << 0);
  5164. DecodeStatus S = MCDisassembler::Success;
  5165. auto DestRegDecoder = q ? DecodeQPRRegisterClass : DecodeDPRRegisterClass;
  5166. if (!Check(S, DestRegDecoder(Inst, Vd, Address, Decoder)))
  5167. return MCDisassembler::Fail;
  5168. if (!Check(S, DestRegDecoder(Inst, Vd, Address, Decoder)))
  5169. return MCDisassembler::Fail;
  5170. if (!Check(S, DestRegDecoder(Inst, Vn, Address, Decoder)))
  5171. return MCDisassembler::Fail;
  5172. if (!Check(S, DecodeDPRRegisterClass(Inst, Vm, Address, Decoder)))
  5173. return MCDisassembler::Fail;
  5174. // The lane index does not have any bits in the encoding, because it can only
  5175. // be 0.
  5176. Inst.addOperand(MCOperand::createImm(0));
  5177. Inst.addOperand(MCOperand::createImm(rotate));
  5178. return S;
  5179. }
  5180. static DecodeStatus DecodeLDR(MCInst &Inst, unsigned Val,
  5181. uint64_t Address, const void *Decoder) {
  5182. DecodeStatus S = MCDisassembler::Success;
  5183. unsigned Rn = fieldFromInstruction(Val, 16, 4);
  5184. unsigned Rt = fieldFromInstruction(Val, 12, 4);
  5185. unsigned Rm = fieldFromInstruction(Val, 0, 4);
  5186. Rm |= (fieldFromInstruction(Val, 23, 1) << 4);
  5187. unsigned Cond = fieldFromInstruction(Val, 28, 4);
  5188. if (fieldFromInstruction(Val, 8, 4) != 0 || Rn == Rt)
  5189. S = MCDisassembler::SoftFail;
  5190. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt, Address, Decoder)))
  5191. return MCDisassembler::Fail;
  5192. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
  5193. return MCDisassembler::Fail;
  5194. if (!Check(S, DecodeAddrMode7Operand(Inst, Rn, Address, Decoder)))
  5195. return MCDisassembler::Fail;
  5196. if (!Check(S, DecodePostIdxReg(Inst, Rm, Address, Decoder)))
  5197. return MCDisassembler::Fail;
  5198. if (!Check(S, DecodePredicateOperand(Inst, Cond, Address, Decoder)))
  5199. return MCDisassembler::Fail;
  5200. return S;
  5201. }
  5202. static DecodeStatus DecoderForMRRC2AndMCRR2(MCInst &Inst, unsigned Val,
  5203. uint64_t Address, const void *Decoder) {
  5204. DecodeStatus S = MCDisassembler::Success;
  5205. unsigned CRm = fieldFromInstruction(Val, 0, 4);
  5206. unsigned opc1 = fieldFromInstruction(Val, 4, 4);
  5207. unsigned cop = fieldFromInstruction(Val, 8, 4);
  5208. unsigned Rt = fieldFromInstruction(Val, 12, 4);
  5209. unsigned Rt2 = fieldFromInstruction(Val, 16, 4);
  5210. if ((cop & ~0x1) == 0xa)
  5211. return MCDisassembler::Fail;
  5212. if (Rt == Rt2)
  5213. S = MCDisassembler::SoftFail;
  5214. // We have to check if the instruction is MRRC2
  5215. // or MCRR2 when constructing the operands for
  5216. // Inst. Reason is because MRRC2 stores to two
  5217. // registers so it's tablegen desc has has two
  5218. // outputs whereas MCRR doesn't store to any
  5219. // registers so all of it's operands are listed
  5220. // as inputs, therefore the operand order for
  5221. // MRRC2 needs to be [Rt, Rt2, cop, opc1, CRm]
  5222. // and MCRR2 operand order is [cop, opc1, Rt, Rt2, CRm]
  5223. if (Inst.getOpcode() == ARM::MRRC2) {
  5224. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt, Address, Decoder)))
  5225. return MCDisassembler::Fail;
  5226. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt2, Address, Decoder)))
  5227. return MCDisassembler::Fail;
  5228. }
  5229. Inst.addOperand(MCOperand::createImm(cop));
  5230. Inst.addOperand(MCOperand::createImm(opc1));
  5231. if (Inst.getOpcode() == ARM::MCRR2) {
  5232. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt, Address, Decoder)))
  5233. return MCDisassembler::Fail;
  5234. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rt2, Address, Decoder)))
  5235. return MCDisassembler::Fail;
  5236. }
  5237. Inst.addOperand(MCOperand::createImm(CRm));
  5238. return S;
  5239. }
  5240. static DecodeStatus DecodeForVMRSandVMSR(MCInst &Inst, unsigned Val,
  5241. uint64_t Address,
  5242. const void *Decoder) {
  5243. const FeatureBitset &featureBits =
  5244. ((const MCDisassembler *)Decoder)->getSubtargetInfo().getFeatureBits();
  5245. DecodeStatus S = MCDisassembler::Success;
  5246. // Add explicit operand for the destination sysreg, for cases where
  5247. // we have to model it for code generation purposes.
  5248. switch (Inst.getOpcode()) {
  5249. case ARM::VMSR_FPSCR_NZCVQC:
  5250. Inst.addOperand(MCOperand::createReg(ARM::FPSCR_NZCV));
  5251. break;
  5252. case ARM::VMSR_P0:
  5253. Inst.addOperand(MCOperand::createReg(ARM::VPR));
  5254. break;
  5255. }
  5256. if (Inst.getOpcode() != ARM::FMSTAT) {
  5257. unsigned Rt = fieldFromInstruction(Val, 12, 4);
  5258. if (featureBits[ARM::ModeThumb] && !featureBits[ARM::HasV8Ops]) {
  5259. if (Rt == 13 || Rt == 15)
  5260. S = MCDisassembler::SoftFail;
  5261. Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder));
  5262. } else
  5263. Check(S, DecodeGPRnopcRegisterClass(Inst, Rt, Address, Decoder));
  5264. }
  5265. // Add explicit operand for the source sysreg, similarly to above.
  5266. switch (Inst.getOpcode()) {
  5267. case ARM::VMRS_FPSCR_NZCVQC:
  5268. Inst.addOperand(MCOperand::createReg(ARM::FPSCR_NZCV));
  5269. break;
  5270. case ARM::VMRS_P0:
  5271. Inst.addOperand(MCOperand::createReg(ARM::VPR));
  5272. break;
  5273. }
  5274. if (featureBits[ARM::ModeThumb]) {
  5275. Inst.addOperand(MCOperand::createImm(ARMCC::AL));
  5276. Inst.addOperand(MCOperand::createReg(0));
  5277. } else {
  5278. unsigned pred = fieldFromInstruction(Val, 28, 4);
  5279. if (!Check(S, DecodePredicateOperand(Inst, pred, Address, Decoder)))
  5280. return MCDisassembler::Fail;
  5281. }
  5282. return S;
  5283. }
  5284. template <bool isSigned, bool isNeg, bool zeroPermitted, int size>
  5285. static DecodeStatus DecodeBFLabelOperand(MCInst &Inst, unsigned Val,
  5286. uint64_t Address,
  5287. const void *Decoder) {
  5288. DecodeStatus S = MCDisassembler::Success;
  5289. if (Val == 0 && !zeroPermitted)
  5290. S = MCDisassembler::Fail;
  5291. uint64_t DecVal;
  5292. if (isSigned)
  5293. DecVal = SignExtend32<size + 1>(Val << 1);
  5294. else
  5295. DecVal = (Val << 1);
  5296. if (!tryAddingSymbolicOperand(Address, Address + DecVal + 4, true, 4, Inst,
  5297. Decoder))
  5298. Inst.addOperand(MCOperand::createImm(isNeg ? -DecVal : DecVal));
  5299. return S;
  5300. }
  5301. static DecodeStatus DecodeBFAfterTargetOperand(MCInst &Inst, unsigned Val,
  5302. uint64_t Address,
  5303. const void *Decoder) {
  5304. uint64_t LocImm = Inst.getOperand(0).getImm();
  5305. Val = LocImm + (2 << Val);
  5306. if (!tryAddingSymbolicOperand(Address, Address + Val + 4, true, 4, Inst,
  5307. Decoder))
  5308. Inst.addOperand(MCOperand::createImm(Val));
  5309. return MCDisassembler::Success;
  5310. }
  5311. static DecodeStatus DecodePredNoALOperand(MCInst &Inst, unsigned Val,
  5312. uint64_t Address,
  5313. const void *Decoder) {
  5314. if (Val >= ARMCC::AL) // also exclude the non-condition NV
  5315. return MCDisassembler::Fail;
  5316. Inst.addOperand(MCOperand::createImm(Val));
  5317. return MCDisassembler::Success;
  5318. }
  5319. static DecodeStatus DecodeLOLoop(MCInst &Inst, unsigned Insn, uint64_t Address,
  5320. const void *Decoder) {
  5321. DecodeStatus S = MCDisassembler::Success;
  5322. if (Inst.getOpcode() == ARM::MVE_LCTP)
  5323. return S;
  5324. unsigned Imm = fieldFromInstruction(Insn, 11, 1) |
  5325. fieldFromInstruction(Insn, 1, 10) << 1;
  5326. switch (Inst.getOpcode()) {
  5327. case ARM::t2LEUpdate:
  5328. case ARM::MVE_LETP:
  5329. Inst.addOperand(MCOperand::createReg(ARM::LR));
  5330. Inst.addOperand(MCOperand::createReg(ARM::LR));
  5331. LLVM_FALLTHROUGH;
  5332. case ARM::t2LE:
  5333. if (!Check(S, DecodeBFLabelOperand<false, true, true, 11>(
  5334. Inst, Imm, Address, Decoder)))
  5335. return MCDisassembler::Fail;
  5336. break;
  5337. case ARM::t2WLS:
  5338. case ARM::MVE_WLSTP_8:
  5339. case ARM::MVE_WLSTP_16:
  5340. case ARM::MVE_WLSTP_32:
  5341. case ARM::MVE_WLSTP_64:
  5342. Inst.addOperand(MCOperand::createReg(ARM::LR));
  5343. if (!Check(S,
  5344. DecoderGPRRegisterClass(Inst, fieldFromInstruction(Insn, 16, 4),
  5345. Address, Decoder)) ||
  5346. !Check(S, DecodeBFLabelOperand<false, false, true, 11>(
  5347. Inst, Imm, Address, Decoder)))
  5348. return MCDisassembler::Fail;
  5349. break;
  5350. case ARM::t2DLS:
  5351. case ARM::MVE_DLSTP_8:
  5352. case ARM::MVE_DLSTP_16:
  5353. case ARM::MVE_DLSTP_32:
  5354. case ARM::MVE_DLSTP_64:
  5355. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  5356. if (Rn == 0xF) {
  5357. // Enforce all the rest of the instruction bits in LCTP, which
  5358. // won't have been reliably checked based on LCTP's own tablegen
  5359. // record, because we came to this decode by a roundabout route.
  5360. uint32_t CanonicalLCTP = 0xF00FE001, SBZMask = 0x00300FFE;
  5361. if ((Insn & ~SBZMask) != CanonicalLCTP)
  5362. return MCDisassembler::Fail; // a mandatory bit is wrong: hard fail
  5363. if (Insn != CanonicalLCTP)
  5364. Check(S, MCDisassembler::SoftFail); // an SBZ bit is wrong: soft fail
  5365. Inst.setOpcode(ARM::MVE_LCTP);
  5366. } else {
  5367. Inst.addOperand(MCOperand::createReg(ARM::LR));
  5368. if (!Check(S, DecoderGPRRegisterClass(Inst,
  5369. fieldFromInstruction(Insn, 16, 4),
  5370. Address, Decoder)))
  5371. return MCDisassembler::Fail;
  5372. }
  5373. break;
  5374. }
  5375. return S;
  5376. }
  5377. static DecodeStatus DecodeLongShiftOperand(MCInst &Inst, unsigned Val,
  5378. uint64_t Address,
  5379. const void *Decoder) {
  5380. DecodeStatus S = MCDisassembler::Success;
  5381. if (Val == 0)
  5382. Val = 32;
  5383. Inst.addOperand(MCOperand::createImm(Val));
  5384. return S;
  5385. }
  5386. static DecodeStatus DecodetGPROddRegisterClass(MCInst &Inst, unsigned RegNo,
  5387. uint64_t Address, const void *Decoder) {
  5388. if ((RegNo) + 1 > 11)
  5389. return MCDisassembler::Fail;
  5390. unsigned Register = GPRDecoderTable[(RegNo) + 1];
  5391. Inst.addOperand(MCOperand::createReg(Register));
  5392. return MCDisassembler::Success;
  5393. }
  5394. static DecodeStatus DecodetGPREvenRegisterClass(MCInst &Inst, unsigned RegNo,
  5395. uint64_t Address, const void *Decoder) {
  5396. if ((RegNo) > 14)
  5397. return MCDisassembler::Fail;
  5398. unsigned Register = GPRDecoderTable[(RegNo)];
  5399. Inst.addOperand(MCOperand::createReg(Register));
  5400. return MCDisassembler::Success;
  5401. }
  5402. static DecodeStatus
  5403. DecodeGPRwithAPSR_NZCVnospRegisterClass(MCInst &Inst, unsigned RegNo,
  5404. uint64_t Address, const void *Decoder) {
  5405. if (RegNo == 15) {
  5406. Inst.addOperand(MCOperand::createReg(ARM::APSR_NZCV));
  5407. return MCDisassembler::Success;
  5408. }
  5409. unsigned Register = GPRDecoderTable[RegNo];
  5410. Inst.addOperand(MCOperand::createReg(Register));
  5411. if (RegNo == 13)
  5412. return MCDisassembler::SoftFail;
  5413. return MCDisassembler::Success;
  5414. }
  5415. static DecodeStatus DecodeVSCCLRM(MCInst &Inst, unsigned Insn, uint64_t Address,
  5416. const void *Decoder) {
  5417. DecodeStatus S = MCDisassembler::Success;
  5418. Inst.addOperand(MCOperand::createImm(ARMCC::AL));
  5419. Inst.addOperand(MCOperand::createReg(0));
  5420. if (Inst.getOpcode() == ARM::VSCCLRMD) {
  5421. unsigned reglist = (fieldFromInstruction(Insn, 1, 7) << 1) |
  5422. (fieldFromInstruction(Insn, 12, 4) << 8) |
  5423. (fieldFromInstruction(Insn, 22, 1) << 12);
  5424. if (!Check(S, DecodeDPRRegListOperand(Inst, reglist, Address, Decoder))) {
  5425. return MCDisassembler::Fail;
  5426. }
  5427. } else {
  5428. unsigned reglist = fieldFromInstruction(Insn, 0, 8) |
  5429. (fieldFromInstruction(Insn, 22, 1) << 8) |
  5430. (fieldFromInstruction(Insn, 12, 4) << 9);
  5431. if (!Check(S, DecodeSPRRegListOperand(Inst, reglist, Address, Decoder))) {
  5432. return MCDisassembler::Fail;
  5433. }
  5434. }
  5435. Inst.addOperand(MCOperand::createReg(ARM::VPR));
  5436. return S;
  5437. }
  5438. static DecodeStatus DecodeMQPRRegisterClass(MCInst &Inst, unsigned RegNo,
  5439. uint64_t Address,
  5440. const void *Decoder) {
  5441. if (RegNo > 7)
  5442. return MCDisassembler::Fail;
  5443. unsigned Register = QPRDecoderTable[RegNo];
  5444. Inst.addOperand(MCOperand::createReg(Register));
  5445. return MCDisassembler::Success;
  5446. }
  5447. static const uint16_t QQPRDecoderTable[] = {
  5448. ARM::Q0_Q1, ARM::Q1_Q2, ARM::Q2_Q3, ARM::Q3_Q4,
  5449. ARM::Q4_Q5, ARM::Q5_Q6, ARM::Q6_Q7
  5450. };
  5451. static DecodeStatus DecodeMQQPRRegisterClass(MCInst &Inst, unsigned RegNo,
  5452. uint64_t Address,
  5453. const void *Decoder) {
  5454. if (RegNo > 6)
  5455. return MCDisassembler::Fail;
  5456. unsigned Register = QQPRDecoderTable[RegNo];
  5457. Inst.addOperand(MCOperand::createReg(Register));
  5458. return MCDisassembler::Success;
  5459. }
  5460. static const uint16_t QQQQPRDecoderTable[] = {
  5461. ARM::Q0_Q1_Q2_Q3, ARM::Q1_Q2_Q3_Q4, ARM::Q2_Q3_Q4_Q5,
  5462. ARM::Q3_Q4_Q5_Q6, ARM::Q4_Q5_Q6_Q7
  5463. };
  5464. static DecodeStatus DecodeMQQQQPRRegisterClass(MCInst &Inst, unsigned RegNo,
  5465. uint64_t Address,
  5466. const void *Decoder) {
  5467. if (RegNo > 4)
  5468. return MCDisassembler::Fail;
  5469. unsigned Register = QQQQPRDecoderTable[RegNo];
  5470. Inst.addOperand(MCOperand::createReg(Register));
  5471. return MCDisassembler::Success;
  5472. }
  5473. static DecodeStatus DecodeVPTMaskOperand(MCInst &Inst, unsigned Val,
  5474. uint64_t Address,
  5475. const void *Decoder) {
  5476. DecodeStatus S = MCDisassembler::Success;
  5477. // Parse VPT mask and encode it in the MCInst as an immediate with the same
  5478. // format as the it_mask. That is, from the second 'e|t' encode 'e' as 1 and
  5479. // 't' as 0 and finish with a 1.
  5480. unsigned Imm = 0;
  5481. // We always start with a 't'.
  5482. unsigned CurBit = 0;
  5483. for (int i = 3; i >= 0; --i) {
  5484. // If the bit we are looking at is not the same as last one, invert the
  5485. // CurBit, if it is the same leave it as is.
  5486. CurBit ^= (Val >> i) & 1U;
  5487. // Encode the CurBit at the right place in the immediate.
  5488. Imm |= (CurBit << i);
  5489. // If we are done, finish the encoding with a 1.
  5490. if ((Val & ~(~0U << i)) == 0) {
  5491. Imm |= 1U << i;
  5492. break;
  5493. }
  5494. }
  5495. Inst.addOperand(MCOperand::createImm(Imm));
  5496. return S;
  5497. }
  5498. static DecodeStatus DecodeVpredROperand(MCInst &Inst, unsigned RegNo,
  5499. uint64_t Address, const void *Decoder) {
  5500. // The vpred_r operand type includes an MQPR register field derived
  5501. // from the encoding. But we don't actually want to add an operand
  5502. // to the MCInst at this stage, because AddThumbPredicate will do it
  5503. // later, and will infer the register number from the TIED_TO
  5504. // constraint. So this is a deliberately empty decoder method that
  5505. // will inhibit the auto-generated disassembly code from adding an
  5506. // operand at all.
  5507. return MCDisassembler::Success;
  5508. }
  5509. static DecodeStatus DecodeRestrictedIPredicateOperand(MCInst &Inst,
  5510. unsigned Val,
  5511. uint64_t Address,
  5512. const void *Decoder) {
  5513. Inst.addOperand(MCOperand::createImm((Val & 0x1) == 0 ? ARMCC::EQ : ARMCC::NE));
  5514. return MCDisassembler::Success;
  5515. }
  5516. static DecodeStatus DecodeRestrictedSPredicateOperand(MCInst &Inst,
  5517. unsigned Val,
  5518. uint64_t Address,
  5519. const void *Decoder) {
  5520. unsigned Code;
  5521. switch (Val & 0x3) {
  5522. case 0:
  5523. Code = ARMCC::GE;
  5524. break;
  5525. case 1:
  5526. Code = ARMCC::LT;
  5527. break;
  5528. case 2:
  5529. Code = ARMCC::GT;
  5530. break;
  5531. case 3:
  5532. Code = ARMCC::LE;
  5533. break;
  5534. }
  5535. Inst.addOperand(MCOperand::createImm(Code));
  5536. return MCDisassembler::Success;
  5537. }
  5538. static DecodeStatus DecodeRestrictedUPredicateOperand(MCInst &Inst,
  5539. unsigned Val,
  5540. uint64_t Address,
  5541. const void *Decoder) {
  5542. Inst.addOperand(MCOperand::createImm((Val & 0x1) == 0 ? ARMCC::HS : ARMCC::HI));
  5543. return MCDisassembler::Success;
  5544. }
  5545. static DecodeStatus DecodeRestrictedFPPredicateOperand(MCInst &Inst, unsigned Val,
  5546. uint64_t Address,
  5547. const void *Decoder) {
  5548. unsigned Code;
  5549. switch (Val) {
  5550. default:
  5551. return MCDisassembler::Fail;
  5552. case 0:
  5553. Code = ARMCC::EQ;
  5554. break;
  5555. case 1:
  5556. Code = ARMCC::NE;
  5557. break;
  5558. case 4:
  5559. Code = ARMCC::GE;
  5560. break;
  5561. case 5:
  5562. Code = ARMCC::LT;
  5563. break;
  5564. case 6:
  5565. Code = ARMCC::GT;
  5566. break;
  5567. case 7:
  5568. Code = ARMCC::LE;
  5569. break;
  5570. }
  5571. Inst.addOperand(MCOperand::createImm(Code));
  5572. return MCDisassembler::Success;
  5573. }
  5574. static DecodeStatus DecodeVCVTImmOperand(MCInst &Inst, unsigned Val,
  5575. uint64_t Address, const void *Decoder) {
  5576. DecodeStatus S = MCDisassembler::Success;
  5577. unsigned DecodedVal = 64 - Val;
  5578. switch (Inst.getOpcode()) {
  5579. case ARM::MVE_VCVTf16s16_fix:
  5580. case ARM::MVE_VCVTs16f16_fix:
  5581. case ARM::MVE_VCVTf16u16_fix:
  5582. case ARM::MVE_VCVTu16f16_fix:
  5583. if (DecodedVal > 16)
  5584. return MCDisassembler::Fail;
  5585. break;
  5586. case ARM::MVE_VCVTf32s32_fix:
  5587. case ARM::MVE_VCVTs32f32_fix:
  5588. case ARM::MVE_VCVTf32u32_fix:
  5589. case ARM::MVE_VCVTu32f32_fix:
  5590. if (DecodedVal > 32)
  5591. return MCDisassembler::Fail;
  5592. break;
  5593. }
  5594. Inst.addOperand(MCOperand::createImm(64 - Val));
  5595. return S;
  5596. }
  5597. static unsigned FixedRegForVSTRVLDR_SYSREG(unsigned Opcode) {
  5598. switch (Opcode) {
  5599. case ARM::VSTR_P0_off:
  5600. case ARM::VSTR_P0_pre:
  5601. case ARM::VSTR_P0_post:
  5602. case ARM::VLDR_P0_off:
  5603. case ARM::VLDR_P0_pre:
  5604. case ARM::VLDR_P0_post:
  5605. return ARM::P0;
  5606. default:
  5607. return 0;
  5608. }
  5609. }
  5610. template<bool Writeback>
  5611. static DecodeStatus DecodeVSTRVLDR_SYSREG(MCInst &Inst, unsigned Val,
  5612. uint64_t Address,
  5613. const void *Decoder) {
  5614. switch (Inst.getOpcode()) {
  5615. case ARM::VSTR_FPSCR_pre:
  5616. case ARM::VSTR_FPSCR_NZCVQC_pre:
  5617. case ARM::VLDR_FPSCR_pre:
  5618. case ARM::VLDR_FPSCR_NZCVQC_pre:
  5619. case ARM::VSTR_FPSCR_off:
  5620. case ARM::VSTR_FPSCR_NZCVQC_off:
  5621. case ARM::VLDR_FPSCR_off:
  5622. case ARM::VLDR_FPSCR_NZCVQC_off:
  5623. case ARM::VSTR_FPSCR_post:
  5624. case ARM::VSTR_FPSCR_NZCVQC_post:
  5625. case ARM::VLDR_FPSCR_post:
  5626. case ARM::VLDR_FPSCR_NZCVQC_post:
  5627. const FeatureBitset &featureBits =
  5628. ((const MCDisassembler *)Decoder)->getSubtargetInfo().getFeatureBits();
  5629. if (!featureBits[ARM::HasMVEIntegerOps] && !featureBits[ARM::FeatureVFP2])
  5630. return MCDisassembler::Fail;
  5631. }
  5632. DecodeStatus S = MCDisassembler::Success;
  5633. if (unsigned Sysreg = FixedRegForVSTRVLDR_SYSREG(Inst.getOpcode()))
  5634. Inst.addOperand(MCOperand::createReg(Sysreg));
  5635. unsigned Rn = fieldFromInstruction(Val, 16, 4);
  5636. unsigned addr = fieldFromInstruction(Val, 0, 7) |
  5637. (fieldFromInstruction(Val, 23, 1) << 7) | (Rn << 8);
  5638. if (Writeback) {
  5639. if (!Check(S, DecodeGPRnopcRegisterClass(Inst, Rn, Address, Decoder)))
  5640. return MCDisassembler::Fail;
  5641. }
  5642. if (!Check(S, DecodeT2AddrModeImm7s4(Inst, addr, Address, Decoder)))
  5643. return MCDisassembler::Fail;
  5644. Inst.addOperand(MCOperand::createImm(ARMCC::AL));
  5645. Inst.addOperand(MCOperand::createReg(0));
  5646. return S;
  5647. }
  5648. static inline DecodeStatus DecodeMVE_MEM_pre(
  5649. MCInst &Inst, unsigned Val, uint64_t Address, const void *Decoder,
  5650. unsigned Rn, OperandDecoder RnDecoder, OperandDecoder AddrDecoder) {
  5651. DecodeStatus S = MCDisassembler::Success;
  5652. unsigned Qd = fieldFromInstruction(Val, 13, 3);
  5653. unsigned addr = fieldFromInstruction(Val, 0, 7) |
  5654. (fieldFromInstruction(Val, 23, 1) << 7) | (Rn << 8);
  5655. if (!Check(S, RnDecoder(Inst, Rn, Address, Decoder)))
  5656. return MCDisassembler::Fail;
  5657. if (!Check(S, DecodeMQPRRegisterClass(Inst, Qd, Address, Decoder)))
  5658. return MCDisassembler::Fail;
  5659. if (!Check(S, AddrDecoder(Inst, addr, Address, Decoder)))
  5660. return MCDisassembler::Fail;
  5661. return S;
  5662. }
  5663. template <int shift>
  5664. static DecodeStatus DecodeMVE_MEM_1_pre(MCInst &Inst, unsigned Val,
  5665. uint64_t Address, const void *Decoder) {
  5666. return DecodeMVE_MEM_pre(Inst, Val, Address, Decoder,
  5667. fieldFromInstruction(Val, 16, 3),
  5668. DecodetGPRRegisterClass,
  5669. DecodeTAddrModeImm7<shift>);
  5670. }
  5671. template <int shift>
  5672. static DecodeStatus DecodeMVE_MEM_2_pre(MCInst &Inst, unsigned Val,
  5673. uint64_t Address, const void *Decoder) {
  5674. return DecodeMVE_MEM_pre(Inst, Val, Address, Decoder,
  5675. fieldFromInstruction(Val, 16, 4),
  5676. DecoderGPRRegisterClass,
  5677. DecodeT2AddrModeImm7<shift,1>);
  5678. }
  5679. template <int shift>
  5680. static DecodeStatus DecodeMVE_MEM_3_pre(MCInst &Inst, unsigned Val,
  5681. uint64_t Address, const void *Decoder) {
  5682. return DecodeMVE_MEM_pre(Inst, Val, Address, Decoder,
  5683. fieldFromInstruction(Val, 17, 3),
  5684. DecodeMQPRRegisterClass,
  5685. DecodeMveAddrModeQ<shift>);
  5686. }
  5687. template<unsigned MinLog, unsigned MaxLog>
  5688. static DecodeStatus DecodePowerTwoOperand(MCInst &Inst, unsigned Val,
  5689. uint64_t Address,
  5690. const void *Decoder) {
  5691. DecodeStatus S = MCDisassembler::Success;
  5692. if (Val < MinLog || Val > MaxLog)
  5693. return MCDisassembler::Fail;
  5694. Inst.addOperand(MCOperand::createImm(1LL << Val));
  5695. return S;
  5696. }
  5697. template<unsigned start>
  5698. static DecodeStatus DecodeMVEPairVectorIndexOperand(MCInst &Inst, unsigned Val,
  5699. uint64_t Address,
  5700. const void *Decoder) {
  5701. DecodeStatus S = MCDisassembler::Success;
  5702. Inst.addOperand(MCOperand::createImm(start + Val));
  5703. return S;
  5704. }
  5705. static DecodeStatus DecodeMVEVMOVQtoDReg(MCInst &Inst, unsigned Insn,
  5706. uint64_t Address, const void *Decoder) {
  5707. DecodeStatus S = MCDisassembler::Success;
  5708. unsigned Rt = fieldFromInstruction(Insn, 0, 4);
  5709. unsigned Rt2 = fieldFromInstruction(Insn, 16, 4);
  5710. unsigned Qd = ((fieldFromInstruction(Insn, 22, 1) << 3) |
  5711. fieldFromInstruction(Insn, 13, 3));
  5712. unsigned index = fieldFromInstruction(Insn, 4, 1);
  5713. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
  5714. return MCDisassembler::Fail;
  5715. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt2, Address, Decoder)))
  5716. return MCDisassembler::Fail;
  5717. if (!Check(S, DecodeMQPRRegisterClass(Inst, Qd, Address, Decoder)))
  5718. return MCDisassembler::Fail;
  5719. if (!Check(S, DecodeMVEPairVectorIndexOperand<2>(Inst, index, Address, Decoder)))
  5720. return MCDisassembler::Fail;
  5721. if (!Check(S, DecodeMVEPairVectorIndexOperand<0>(Inst, index, Address, Decoder)))
  5722. return MCDisassembler::Fail;
  5723. return S;
  5724. }
  5725. static DecodeStatus DecodeMVEVMOVDRegtoQ(MCInst &Inst, unsigned Insn,
  5726. uint64_t Address, const void *Decoder) {
  5727. DecodeStatus S = MCDisassembler::Success;
  5728. unsigned Rt = fieldFromInstruction(Insn, 0, 4);
  5729. unsigned Rt2 = fieldFromInstruction(Insn, 16, 4);
  5730. unsigned Qd = ((fieldFromInstruction(Insn, 22, 1) << 3) |
  5731. fieldFromInstruction(Insn, 13, 3));
  5732. unsigned index = fieldFromInstruction(Insn, 4, 1);
  5733. if (!Check(S, DecodeMQPRRegisterClass(Inst, Qd, Address, Decoder)))
  5734. return MCDisassembler::Fail;
  5735. if (!Check(S, DecodeMQPRRegisterClass(Inst, Qd, Address, Decoder)))
  5736. return MCDisassembler::Fail;
  5737. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt, Address, Decoder)))
  5738. return MCDisassembler::Fail;
  5739. if (!Check(S, DecodeGPRRegisterClass(Inst, Rt2, Address, Decoder)))
  5740. return MCDisassembler::Fail;
  5741. if (!Check(S, DecodeMVEPairVectorIndexOperand<2>(Inst, index, Address, Decoder)))
  5742. return MCDisassembler::Fail;
  5743. if (!Check(S, DecodeMVEPairVectorIndexOperand<0>(Inst, index, Address, Decoder)))
  5744. return MCDisassembler::Fail;
  5745. return S;
  5746. }
  5747. static DecodeStatus DecodeMVEOverlappingLongShift(
  5748. MCInst &Inst, unsigned Insn, uint64_t Address, const void *Decoder) {
  5749. DecodeStatus S = MCDisassembler::Success;
  5750. unsigned RdaLo = fieldFromInstruction(Insn, 17, 3) << 1;
  5751. unsigned RdaHi = fieldFromInstruction(Insn, 9, 3) << 1;
  5752. unsigned Rm = fieldFromInstruction(Insn, 12, 4);
  5753. if (RdaHi == 14) {
  5754. // This value of RdaHi (really indicating pc, because RdaHi has to
  5755. // be an odd-numbered register, so the low bit will be set by the
  5756. // decode function below) indicates that we must decode as SQRSHR
  5757. // or UQRSHL, which both have a single Rda register field with all
  5758. // four bits.
  5759. unsigned Rda = fieldFromInstruction(Insn, 16, 4);
  5760. switch (Inst.getOpcode()) {
  5761. case ARM::MVE_ASRLr:
  5762. case ARM::MVE_SQRSHRL:
  5763. Inst.setOpcode(ARM::MVE_SQRSHR);
  5764. break;
  5765. case ARM::MVE_LSLLr:
  5766. case ARM::MVE_UQRSHLL:
  5767. Inst.setOpcode(ARM::MVE_UQRSHL);
  5768. break;
  5769. default:
  5770. llvm_unreachable("Unexpected starting opcode!");
  5771. }
  5772. // Rda as output parameter
  5773. if (!Check(S, DecoderGPRRegisterClass(Inst, Rda, Address, Decoder)))
  5774. return MCDisassembler::Fail;
  5775. // Rda again as input parameter
  5776. if (!Check(S, DecoderGPRRegisterClass(Inst, Rda, Address, Decoder)))
  5777. return MCDisassembler::Fail;
  5778. // Rm, the amount to shift by
  5779. if (!Check(S, DecoderGPRRegisterClass(Inst, Rm, Address, Decoder)))
  5780. return MCDisassembler::Fail;
  5781. if (fieldFromInstruction (Insn, 6, 3) != 4)
  5782. return MCDisassembler::SoftFail;
  5783. if (Rda == Rm)
  5784. return MCDisassembler::SoftFail;
  5785. return S;
  5786. }
  5787. // Otherwise, we decode as whichever opcode our caller has already
  5788. // put into Inst. Those all look the same:
  5789. // RdaLo,RdaHi as output parameters
  5790. if (!Check(S, DecodetGPREvenRegisterClass(Inst, RdaLo, Address, Decoder)))
  5791. return MCDisassembler::Fail;
  5792. if (!Check(S, DecodetGPROddRegisterClass(Inst, RdaHi, Address, Decoder)))
  5793. return MCDisassembler::Fail;
  5794. // RdaLo,RdaHi again as input parameters
  5795. if (!Check(S, DecodetGPREvenRegisterClass(Inst, RdaLo, Address, Decoder)))
  5796. return MCDisassembler::Fail;
  5797. if (!Check(S, DecodetGPROddRegisterClass(Inst, RdaHi, Address, Decoder)))
  5798. return MCDisassembler::Fail;
  5799. // Rm, the amount to shift by
  5800. if (!Check(S, DecoderGPRRegisterClass(Inst, Rm, Address, Decoder)))
  5801. return MCDisassembler::Fail;
  5802. if (Inst.getOpcode() == ARM::MVE_SQRSHRL ||
  5803. Inst.getOpcode() == ARM::MVE_UQRSHLL) {
  5804. unsigned Saturate = fieldFromInstruction(Insn, 7, 1);
  5805. // Saturate, the bit position for saturation
  5806. Inst.addOperand(MCOperand::createImm(Saturate));
  5807. }
  5808. return S;
  5809. }
  5810. static DecodeStatus DecodeMVEVCVTt1fp(MCInst &Inst, unsigned Insn, uint64_t Address,
  5811. const void *Decoder) {
  5812. DecodeStatus S = MCDisassembler::Success;
  5813. unsigned Qd = ((fieldFromInstruction(Insn, 22, 1) << 3) |
  5814. fieldFromInstruction(Insn, 13, 3));
  5815. unsigned Qm = ((fieldFromInstruction(Insn, 5, 1) << 3) |
  5816. fieldFromInstruction(Insn, 1, 3));
  5817. unsigned imm6 = fieldFromInstruction(Insn, 16, 6);
  5818. if (!Check(S, DecodeMQPRRegisterClass(Inst, Qd, Address, Decoder)))
  5819. return MCDisassembler::Fail;
  5820. if (!Check(S, DecodeMQPRRegisterClass(Inst, Qm, Address, Decoder)))
  5821. return MCDisassembler::Fail;
  5822. if (!Check(S, DecodeVCVTImmOperand(Inst, imm6, Address, Decoder)))
  5823. return MCDisassembler::Fail;
  5824. return S;
  5825. }
  5826. template<bool scalar, OperandDecoder predicate_decoder>
  5827. static DecodeStatus DecodeMVEVCMP(MCInst &Inst, unsigned Insn, uint64_t Address,
  5828. const void *Decoder) {
  5829. DecodeStatus S = MCDisassembler::Success;
  5830. Inst.addOperand(MCOperand::createReg(ARM::VPR));
  5831. unsigned Qn = fieldFromInstruction(Insn, 17, 3);
  5832. if (!Check(S, DecodeMQPRRegisterClass(Inst, Qn, Address, Decoder)))
  5833. return MCDisassembler::Fail;
  5834. unsigned fc;
  5835. if (scalar) {
  5836. fc = fieldFromInstruction(Insn, 12, 1) << 2 |
  5837. fieldFromInstruction(Insn, 7, 1) |
  5838. fieldFromInstruction(Insn, 5, 1) << 1;
  5839. unsigned Rm = fieldFromInstruction(Insn, 0, 4);
  5840. if (!Check(S, DecodeGPRwithZRRegisterClass(Inst, Rm, Address, Decoder)))
  5841. return MCDisassembler::Fail;
  5842. } else {
  5843. fc = fieldFromInstruction(Insn, 12, 1) << 2 |
  5844. fieldFromInstruction(Insn, 7, 1) |
  5845. fieldFromInstruction(Insn, 0, 1) << 1;
  5846. unsigned Qm = fieldFromInstruction(Insn, 5, 1) << 4 |
  5847. fieldFromInstruction(Insn, 1, 3);
  5848. if (!Check(S, DecodeMQPRRegisterClass(Inst, Qm, Address, Decoder)))
  5849. return MCDisassembler::Fail;
  5850. }
  5851. if (!Check(S, predicate_decoder(Inst, fc, Address, Decoder)))
  5852. return MCDisassembler::Fail;
  5853. Inst.addOperand(MCOperand::createImm(ARMVCC::None));
  5854. Inst.addOperand(MCOperand::createReg(0));
  5855. Inst.addOperand(MCOperand::createImm(0));
  5856. return S;
  5857. }
  5858. static DecodeStatus DecodeMveVCTP(MCInst &Inst, unsigned Insn, uint64_t Address,
  5859. const void *Decoder) {
  5860. DecodeStatus S = MCDisassembler::Success;
  5861. Inst.addOperand(MCOperand::createReg(ARM::VPR));
  5862. unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  5863. if (!Check(S, DecoderGPRRegisterClass(Inst, Rn, Address, Decoder)))
  5864. return MCDisassembler::Fail;
  5865. return S;
  5866. }
  5867. static DecodeStatus DecodeMVEVPNOT(MCInst &Inst, unsigned Insn, uint64_t Address,
  5868. const void *Decoder) {
  5869. DecodeStatus S = MCDisassembler::Success;
  5870. Inst.addOperand(MCOperand::createReg(ARM::VPR));
  5871. Inst.addOperand(MCOperand::createReg(ARM::VPR));
  5872. return S;
  5873. }
  5874. static DecodeStatus DecodeT2AddSubSPImm(MCInst &Inst, unsigned Insn,
  5875. uint64_t Address, const void *Decoder) {
  5876. const unsigned Rd = fieldFromInstruction(Insn, 8, 4);
  5877. const unsigned Rn = fieldFromInstruction(Insn, 16, 4);
  5878. const unsigned Imm12 = fieldFromInstruction(Insn, 26, 1) << 11 |
  5879. fieldFromInstruction(Insn, 12, 3) << 8 |
  5880. fieldFromInstruction(Insn, 0, 8);
  5881. const unsigned TypeT3 = fieldFromInstruction(Insn, 25, 1);
  5882. unsigned sign1 = fieldFromInstruction(Insn, 21, 1);
  5883. unsigned sign2 = fieldFromInstruction(Insn, 23, 1);
  5884. unsigned S = fieldFromInstruction(Insn, 20, 1);
  5885. if (sign1 != sign2)
  5886. return MCDisassembler::Fail;
  5887. // T3 does a zext of imm12, where T2 does a ThumbExpandImm (T2SOImm)
  5888. DecodeStatus DS = MCDisassembler::Success;
  5889. if ((!Check(DS,
  5890. DecodeGPRspRegisterClass(Inst, Rd, Address, Decoder))) || // dst
  5891. (!Check(DS, DecodeGPRspRegisterClass(Inst, Rn, Address, Decoder))))
  5892. return MCDisassembler::Fail;
  5893. if (TypeT3) {
  5894. Inst.setOpcode(sign1 ? ARM::t2SUBspImm12 : ARM::t2ADDspImm12);
  5895. Inst.addOperand(MCOperand::createImm(Imm12)); // zext imm12
  5896. } else {
  5897. Inst.setOpcode(sign1 ? ARM::t2SUBspImm : ARM::t2ADDspImm);
  5898. if (!Check(DS, DecodeT2SOImm(Inst, Imm12, Address, Decoder))) // imm12
  5899. return MCDisassembler::Fail;
  5900. if (!Check(DS, DecodeCCOutOperand(Inst, S, Address, Decoder))) // cc_out
  5901. return MCDisassembler::Fail;
  5902. }
  5903. return DS;
  5904. }