X86AsmParser.cpp 180 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132
  1. //===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
  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 "MCTargetDesc/X86BaseInfo.h"
  9. #include "MCTargetDesc/X86IntelInstPrinter.h"
  10. #include "MCTargetDesc/X86MCExpr.h"
  11. #include "MCTargetDesc/X86TargetStreamer.h"
  12. #include "TargetInfo/X86TargetInfo.h"
  13. #include "X86AsmParserCommon.h"
  14. #include "X86Operand.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/SmallString.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/ADT/StringSwitch.h"
  19. #include "llvm/ADT/Twine.h"
  20. #include "llvm/MC/MCContext.h"
  21. #include "llvm/MC/MCExpr.h"
  22. #include "llvm/MC/MCInst.h"
  23. #include "llvm/MC/MCInstrInfo.h"
  24. #include "llvm/MC/MCParser/MCAsmLexer.h"
  25. #include "llvm/MC/MCParser/MCAsmParser.h"
  26. #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
  27. #include "llvm/MC/MCParser/MCTargetAsmParser.h"
  28. #include "llvm/MC/MCRegisterInfo.h"
  29. #include "llvm/MC/MCSection.h"
  30. #include "llvm/MC/MCStreamer.h"
  31. #include "llvm/MC/MCSubtargetInfo.h"
  32. #include "llvm/MC/MCSymbol.h"
  33. #include "llvm/MC/TargetRegistry.h"
  34. #include "llvm/Support/CommandLine.h"
  35. #include "llvm/Support/Compiler.h"
  36. #include "llvm/Support/SourceMgr.h"
  37. #include "llvm/Support/raw_ostream.h"
  38. #include <algorithm>
  39. #include <memory>
  40. using namespace llvm;
  41. static cl::opt<bool> LVIInlineAsmHardening(
  42. "x86-experimental-lvi-inline-asm-hardening",
  43. cl::desc("Harden inline assembly code that may be vulnerable to Load Value"
  44. " Injection (LVI). This feature is experimental."), cl::Hidden);
  45. static bool checkScale(unsigned Scale, StringRef &ErrMsg) {
  46. if (Scale != 1 && Scale != 2 && Scale != 4 && Scale != 8) {
  47. ErrMsg = "scale factor in address must be 1, 2, 4 or 8";
  48. return true;
  49. }
  50. return false;
  51. }
  52. namespace {
  53. static const char OpPrecedence[] = {
  54. 0, // IC_OR
  55. 1, // IC_XOR
  56. 2, // IC_AND
  57. 4, // IC_LSHIFT
  58. 4, // IC_RSHIFT
  59. 5, // IC_PLUS
  60. 5, // IC_MINUS
  61. 6, // IC_MULTIPLY
  62. 6, // IC_DIVIDE
  63. 6, // IC_MOD
  64. 7, // IC_NOT
  65. 8, // IC_NEG
  66. 9, // IC_RPAREN
  67. 10, // IC_LPAREN
  68. 0, // IC_IMM
  69. 0, // IC_REGISTER
  70. 3, // IC_EQ
  71. 3, // IC_NE
  72. 3, // IC_LT
  73. 3, // IC_LE
  74. 3, // IC_GT
  75. 3 // IC_GE
  76. };
  77. class X86AsmParser : public MCTargetAsmParser {
  78. ParseInstructionInfo *InstInfo;
  79. bool Code16GCC;
  80. unsigned ForcedDataPrefix = 0;
  81. enum VEXEncoding {
  82. VEXEncoding_Default,
  83. VEXEncoding_VEX,
  84. VEXEncoding_VEX2,
  85. VEXEncoding_VEX3,
  86. VEXEncoding_EVEX,
  87. };
  88. VEXEncoding ForcedVEXEncoding = VEXEncoding_Default;
  89. enum DispEncoding {
  90. DispEncoding_Default,
  91. DispEncoding_Disp8,
  92. DispEncoding_Disp32,
  93. };
  94. DispEncoding ForcedDispEncoding = DispEncoding_Default;
  95. private:
  96. SMLoc consumeToken() {
  97. MCAsmParser &Parser = getParser();
  98. SMLoc Result = Parser.getTok().getLoc();
  99. Parser.Lex();
  100. return Result;
  101. }
  102. X86TargetStreamer &getTargetStreamer() {
  103. assert(getParser().getStreamer().getTargetStreamer() &&
  104. "do not have a target streamer");
  105. MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
  106. return static_cast<X86TargetStreamer &>(TS);
  107. }
  108. unsigned MatchInstruction(const OperandVector &Operands, MCInst &Inst,
  109. uint64_t &ErrorInfo, FeatureBitset &MissingFeatures,
  110. bool matchingInlineAsm, unsigned VariantID = 0) {
  111. // In Code16GCC mode, match as 32-bit.
  112. if (Code16GCC)
  113. SwitchMode(X86::Mode32Bit);
  114. unsigned rv = MatchInstructionImpl(Operands, Inst, ErrorInfo,
  115. MissingFeatures, matchingInlineAsm,
  116. VariantID);
  117. if (Code16GCC)
  118. SwitchMode(X86::Mode16Bit);
  119. return rv;
  120. }
  121. enum InfixCalculatorTok {
  122. IC_OR = 0,
  123. IC_XOR,
  124. IC_AND,
  125. IC_LSHIFT,
  126. IC_RSHIFT,
  127. IC_PLUS,
  128. IC_MINUS,
  129. IC_MULTIPLY,
  130. IC_DIVIDE,
  131. IC_MOD,
  132. IC_NOT,
  133. IC_NEG,
  134. IC_RPAREN,
  135. IC_LPAREN,
  136. IC_IMM,
  137. IC_REGISTER,
  138. IC_EQ,
  139. IC_NE,
  140. IC_LT,
  141. IC_LE,
  142. IC_GT,
  143. IC_GE
  144. };
  145. enum IntelOperatorKind {
  146. IOK_INVALID = 0,
  147. IOK_LENGTH,
  148. IOK_SIZE,
  149. IOK_TYPE,
  150. };
  151. enum MasmOperatorKind {
  152. MOK_INVALID = 0,
  153. MOK_LENGTHOF,
  154. MOK_SIZEOF,
  155. MOK_TYPE,
  156. };
  157. class InfixCalculator {
  158. typedef std::pair< InfixCalculatorTok, int64_t > ICToken;
  159. SmallVector<InfixCalculatorTok, 4> InfixOperatorStack;
  160. SmallVector<ICToken, 4> PostfixStack;
  161. bool isUnaryOperator(InfixCalculatorTok Op) const {
  162. return Op == IC_NEG || Op == IC_NOT;
  163. }
  164. public:
  165. int64_t popOperand() {
  166. assert (!PostfixStack.empty() && "Poped an empty stack!");
  167. ICToken Op = PostfixStack.pop_back_val();
  168. if (!(Op.first == IC_IMM || Op.first == IC_REGISTER))
  169. return -1; // The invalid Scale value will be caught later by checkScale
  170. return Op.second;
  171. }
  172. void pushOperand(InfixCalculatorTok Op, int64_t Val = 0) {
  173. assert ((Op == IC_IMM || Op == IC_REGISTER) &&
  174. "Unexpected operand!");
  175. PostfixStack.push_back(std::make_pair(Op, Val));
  176. }
  177. void popOperator() { InfixOperatorStack.pop_back(); }
  178. void pushOperator(InfixCalculatorTok Op) {
  179. // Push the new operator if the stack is empty.
  180. if (InfixOperatorStack.empty()) {
  181. InfixOperatorStack.push_back(Op);
  182. return;
  183. }
  184. // Push the new operator if it has a higher precedence than the operator
  185. // on the top of the stack or the operator on the top of the stack is a
  186. // left parentheses.
  187. unsigned Idx = InfixOperatorStack.size() - 1;
  188. InfixCalculatorTok StackOp = InfixOperatorStack[Idx];
  189. if (OpPrecedence[Op] > OpPrecedence[StackOp] || StackOp == IC_LPAREN) {
  190. InfixOperatorStack.push_back(Op);
  191. return;
  192. }
  193. // The operator on the top of the stack has higher precedence than the
  194. // new operator.
  195. unsigned ParenCount = 0;
  196. while (true) {
  197. // Nothing to process.
  198. if (InfixOperatorStack.empty())
  199. break;
  200. Idx = InfixOperatorStack.size() - 1;
  201. StackOp = InfixOperatorStack[Idx];
  202. if (!(OpPrecedence[StackOp] >= OpPrecedence[Op] || ParenCount))
  203. break;
  204. // If we have an even parentheses count and we see a left parentheses,
  205. // then stop processing.
  206. if (!ParenCount && StackOp == IC_LPAREN)
  207. break;
  208. if (StackOp == IC_RPAREN) {
  209. ++ParenCount;
  210. InfixOperatorStack.pop_back();
  211. } else if (StackOp == IC_LPAREN) {
  212. --ParenCount;
  213. InfixOperatorStack.pop_back();
  214. } else {
  215. InfixOperatorStack.pop_back();
  216. PostfixStack.push_back(std::make_pair(StackOp, 0));
  217. }
  218. }
  219. // Push the new operator.
  220. InfixOperatorStack.push_back(Op);
  221. }
  222. int64_t execute() {
  223. // Push any remaining operators onto the postfix stack.
  224. while (!InfixOperatorStack.empty()) {
  225. InfixCalculatorTok StackOp = InfixOperatorStack.pop_back_val();
  226. if (StackOp != IC_LPAREN && StackOp != IC_RPAREN)
  227. PostfixStack.push_back(std::make_pair(StackOp, 0));
  228. }
  229. if (PostfixStack.empty())
  230. return 0;
  231. SmallVector<ICToken, 16> OperandStack;
  232. for (unsigned i = 0, e = PostfixStack.size(); i != e; ++i) {
  233. ICToken Op = PostfixStack[i];
  234. if (Op.first == IC_IMM || Op.first == IC_REGISTER) {
  235. OperandStack.push_back(Op);
  236. } else if (isUnaryOperator(Op.first)) {
  237. assert (OperandStack.size() > 0 && "Too few operands.");
  238. ICToken Operand = OperandStack.pop_back_val();
  239. assert (Operand.first == IC_IMM &&
  240. "Unary operation with a register!");
  241. switch (Op.first) {
  242. default:
  243. report_fatal_error("Unexpected operator!");
  244. break;
  245. case IC_NEG:
  246. OperandStack.push_back(std::make_pair(IC_IMM, -Operand.second));
  247. break;
  248. case IC_NOT:
  249. OperandStack.push_back(std::make_pair(IC_IMM, ~Operand.second));
  250. break;
  251. }
  252. } else {
  253. assert (OperandStack.size() > 1 && "Too few operands.");
  254. int64_t Val;
  255. ICToken Op2 = OperandStack.pop_back_val();
  256. ICToken Op1 = OperandStack.pop_back_val();
  257. switch (Op.first) {
  258. default:
  259. report_fatal_error("Unexpected operator!");
  260. break;
  261. case IC_PLUS:
  262. Val = Op1.second + Op2.second;
  263. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  264. break;
  265. case IC_MINUS:
  266. Val = Op1.second - Op2.second;
  267. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  268. break;
  269. case IC_MULTIPLY:
  270. assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
  271. "Multiply operation with an immediate and a register!");
  272. Val = Op1.second * Op2.second;
  273. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  274. break;
  275. case IC_DIVIDE:
  276. assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
  277. "Divide operation with an immediate and a register!");
  278. assert (Op2.second != 0 && "Division by zero!");
  279. Val = Op1.second / Op2.second;
  280. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  281. break;
  282. case IC_MOD:
  283. assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
  284. "Modulo operation with an immediate and a register!");
  285. Val = Op1.second % Op2.second;
  286. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  287. break;
  288. case IC_OR:
  289. assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
  290. "Or operation with an immediate and a register!");
  291. Val = Op1.second | Op2.second;
  292. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  293. break;
  294. case IC_XOR:
  295. assert(Op1.first == IC_IMM && Op2.first == IC_IMM &&
  296. "Xor operation with an immediate and a register!");
  297. Val = Op1.second ^ Op2.second;
  298. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  299. break;
  300. case IC_AND:
  301. assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
  302. "And operation with an immediate and a register!");
  303. Val = Op1.second & Op2.second;
  304. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  305. break;
  306. case IC_LSHIFT:
  307. assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
  308. "Left shift operation with an immediate and a register!");
  309. Val = Op1.second << Op2.second;
  310. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  311. break;
  312. case IC_RSHIFT:
  313. assert (Op1.first == IC_IMM && Op2.first == IC_IMM &&
  314. "Right shift operation with an immediate and a register!");
  315. Val = Op1.second >> Op2.second;
  316. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  317. break;
  318. case IC_EQ:
  319. assert(Op1.first == IC_IMM && Op2.first == IC_IMM &&
  320. "Equals operation with an immediate and a register!");
  321. Val = (Op1.second == Op2.second) ? -1 : 0;
  322. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  323. break;
  324. case IC_NE:
  325. assert(Op1.first == IC_IMM && Op2.first == IC_IMM &&
  326. "Not-equals operation with an immediate and a register!");
  327. Val = (Op1.second != Op2.second) ? -1 : 0;
  328. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  329. break;
  330. case IC_LT:
  331. assert(Op1.first == IC_IMM && Op2.first == IC_IMM &&
  332. "Less-than operation with an immediate and a register!");
  333. Val = (Op1.second < Op2.second) ? -1 : 0;
  334. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  335. break;
  336. case IC_LE:
  337. assert(Op1.first == IC_IMM && Op2.first == IC_IMM &&
  338. "Less-than-or-equal operation with an immediate and a "
  339. "register!");
  340. Val = (Op1.second <= Op2.second) ? -1 : 0;
  341. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  342. break;
  343. case IC_GT:
  344. assert(Op1.first == IC_IMM && Op2.first == IC_IMM &&
  345. "Greater-than operation with an immediate and a register!");
  346. Val = (Op1.second > Op2.second) ? -1 : 0;
  347. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  348. break;
  349. case IC_GE:
  350. assert(Op1.first == IC_IMM && Op2.first == IC_IMM &&
  351. "Greater-than-or-equal operation with an immediate and a "
  352. "register!");
  353. Val = (Op1.second >= Op2.second) ? -1 : 0;
  354. OperandStack.push_back(std::make_pair(IC_IMM, Val));
  355. break;
  356. }
  357. }
  358. }
  359. assert (OperandStack.size() == 1 && "Expected a single result.");
  360. return OperandStack.pop_back_val().second;
  361. }
  362. };
  363. enum IntelExprState {
  364. IES_INIT,
  365. IES_OR,
  366. IES_XOR,
  367. IES_AND,
  368. IES_EQ,
  369. IES_NE,
  370. IES_LT,
  371. IES_LE,
  372. IES_GT,
  373. IES_GE,
  374. IES_LSHIFT,
  375. IES_RSHIFT,
  376. IES_PLUS,
  377. IES_MINUS,
  378. IES_OFFSET,
  379. IES_CAST,
  380. IES_NOT,
  381. IES_MULTIPLY,
  382. IES_DIVIDE,
  383. IES_MOD,
  384. IES_LBRAC,
  385. IES_RBRAC,
  386. IES_LPAREN,
  387. IES_RPAREN,
  388. IES_REGISTER,
  389. IES_INTEGER,
  390. IES_IDENTIFIER,
  391. IES_ERROR
  392. };
  393. class IntelExprStateMachine {
  394. IntelExprState State, PrevState;
  395. unsigned BaseReg, IndexReg, TmpReg, Scale;
  396. int64_t Imm;
  397. const MCExpr *Sym;
  398. StringRef SymName;
  399. InfixCalculator IC;
  400. InlineAsmIdentifierInfo Info;
  401. short BracCount;
  402. bool MemExpr;
  403. bool OffsetOperator;
  404. SMLoc OffsetOperatorLoc;
  405. AsmTypeInfo CurType;
  406. bool setSymRef(const MCExpr *Val, StringRef ID, StringRef &ErrMsg) {
  407. if (Sym) {
  408. ErrMsg = "cannot use more than one symbol in memory operand";
  409. return true;
  410. }
  411. Sym = Val;
  412. SymName = ID;
  413. return false;
  414. }
  415. public:
  416. IntelExprStateMachine()
  417. : State(IES_INIT), PrevState(IES_ERROR), BaseReg(0), IndexReg(0),
  418. TmpReg(0), Scale(0), Imm(0), Sym(nullptr), BracCount(0),
  419. MemExpr(false), OffsetOperator(false) {}
  420. void addImm(int64_t imm) { Imm += imm; }
  421. short getBracCount() const { return BracCount; }
  422. bool isMemExpr() const { return MemExpr; }
  423. bool isOffsetOperator() const { return OffsetOperator; }
  424. SMLoc getOffsetLoc() const { return OffsetOperatorLoc; }
  425. unsigned getBaseReg() const { return BaseReg; }
  426. unsigned getIndexReg() const { return IndexReg; }
  427. unsigned getScale() const { return Scale; }
  428. const MCExpr *getSym() const { return Sym; }
  429. StringRef getSymName() const { return SymName; }
  430. StringRef getType() const { return CurType.Name; }
  431. unsigned getSize() const { return CurType.Size; }
  432. unsigned getElementSize() const { return CurType.ElementSize; }
  433. unsigned getLength() const { return CurType.Length; }
  434. int64_t getImm() { return Imm + IC.execute(); }
  435. bool isValidEndState() const {
  436. return State == IES_RBRAC || State == IES_INTEGER;
  437. }
  438. bool hadError() const { return State == IES_ERROR; }
  439. const InlineAsmIdentifierInfo &getIdentifierInfo() const { return Info; }
  440. void onOr() {
  441. IntelExprState CurrState = State;
  442. switch (State) {
  443. default:
  444. State = IES_ERROR;
  445. break;
  446. case IES_INTEGER:
  447. case IES_RPAREN:
  448. case IES_REGISTER:
  449. State = IES_OR;
  450. IC.pushOperator(IC_OR);
  451. break;
  452. }
  453. PrevState = CurrState;
  454. }
  455. void onXor() {
  456. IntelExprState CurrState = State;
  457. switch (State) {
  458. default:
  459. State = IES_ERROR;
  460. break;
  461. case IES_INTEGER:
  462. case IES_RPAREN:
  463. case IES_REGISTER:
  464. State = IES_XOR;
  465. IC.pushOperator(IC_XOR);
  466. break;
  467. }
  468. PrevState = CurrState;
  469. }
  470. void onAnd() {
  471. IntelExprState CurrState = State;
  472. switch (State) {
  473. default:
  474. State = IES_ERROR;
  475. break;
  476. case IES_INTEGER:
  477. case IES_RPAREN:
  478. case IES_REGISTER:
  479. State = IES_AND;
  480. IC.pushOperator(IC_AND);
  481. break;
  482. }
  483. PrevState = CurrState;
  484. }
  485. void onEq() {
  486. IntelExprState CurrState = State;
  487. switch (State) {
  488. default:
  489. State = IES_ERROR;
  490. break;
  491. case IES_INTEGER:
  492. case IES_RPAREN:
  493. case IES_REGISTER:
  494. State = IES_EQ;
  495. IC.pushOperator(IC_EQ);
  496. break;
  497. }
  498. PrevState = CurrState;
  499. }
  500. void onNE() {
  501. IntelExprState CurrState = State;
  502. switch (State) {
  503. default:
  504. State = IES_ERROR;
  505. break;
  506. case IES_INTEGER:
  507. case IES_RPAREN:
  508. case IES_REGISTER:
  509. State = IES_NE;
  510. IC.pushOperator(IC_NE);
  511. break;
  512. }
  513. PrevState = CurrState;
  514. }
  515. void onLT() {
  516. IntelExprState CurrState = State;
  517. switch (State) {
  518. default:
  519. State = IES_ERROR;
  520. break;
  521. case IES_INTEGER:
  522. case IES_RPAREN:
  523. case IES_REGISTER:
  524. State = IES_LT;
  525. IC.pushOperator(IC_LT);
  526. break;
  527. }
  528. PrevState = CurrState;
  529. }
  530. void onLE() {
  531. IntelExprState CurrState = State;
  532. switch (State) {
  533. default:
  534. State = IES_ERROR;
  535. break;
  536. case IES_INTEGER:
  537. case IES_RPAREN:
  538. case IES_REGISTER:
  539. State = IES_LE;
  540. IC.pushOperator(IC_LE);
  541. break;
  542. }
  543. PrevState = CurrState;
  544. }
  545. void onGT() {
  546. IntelExprState CurrState = State;
  547. switch (State) {
  548. default:
  549. State = IES_ERROR;
  550. break;
  551. case IES_INTEGER:
  552. case IES_RPAREN:
  553. case IES_REGISTER:
  554. State = IES_GT;
  555. IC.pushOperator(IC_GT);
  556. break;
  557. }
  558. PrevState = CurrState;
  559. }
  560. void onGE() {
  561. IntelExprState CurrState = State;
  562. switch (State) {
  563. default:
  564. State = IES_ERROR;
  565. break;
  566. case IES_INTEGER:
  567. case IES_RPAREN:
  568. case IES_REGISTER:
  569. State = IES_GE;
  570. IC.pushOperator(IC_GE);
  571. break;
  572. }
  573. PrevState = CurrState;
  574. }
  575. void onLShift() {
  576. IntelExprState CurrState = State;
  577. switch (State) {
  578. default:
  579. State = IES_ERROR;
  580. break;
  581. case IES_INTEGER:
  582. case IES_RPAREN:
  583. case IES_REGISTER:
  584. State = IES_LSHIFT;
  585. IC.pushOperator(IC_LSHIFT);
  586. break;
  587. }
  588. PrevState = CurrState;
  589. }
  590. void onRShift() {
  591. IntelExprState CurrState = State;
  592. switch (State) {
  593. default:
  594. State = IES_ERROR;
  595. break;
  596. case IES_INTEGER:
  597. case IES_RPAREN:
  598. case IES_REGISTER:
  599. State = IES_RSHIFT;
  600. IC.pushOperator(IC_RSHIFT);
  601. break;
  602. }
  603. PrevState = CurrState;
  604. }
  605. bool onPlus(StringRef &ErrMsg) {
  606. IntelExprState CurrState = State;
  607. switch (State) {
  608. default:
  609. State = IES_ERROR;
  610. break;
  611. case IES_INTEGER:
  612. case IES_RPAREN:
  613. case IES_REGISTER:
  614. case IES_OFFSET:
  615. State = IES_PLUS;
  616. IC.pushOperator(IC_PLUS);
  617. if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
  618. // If we already have a BaseReg, then assume this is the IndexReg with
  619. // no explicit scale.
  620. if (!BaseReg) {
  621. BaseReg = TmpReg;
  622. } else {
  623. if (IndexReg) {
  624. ErrMsg = "BaseReg/IndexReg already set!";
  625. return true;
  626. }
  627. IndexReg = TmpReg;
  628. Scale = 0;
  629. }
  630. }
  631. break;
  632. }
  633. PrevState = CurrState;
  634. return false;
  635. }
  636. bool onMinus(StringRef &ErrMsg) {
  637. IntelExprState CurrState = State;
  638. switch (State) {
  639. default:
  640. State = IES_ERROR;
  641. break;
  642. case IES_OR:
  643. case IES_XOR:
  644. case IES_AND:
  645. case IES_EQ:
  646. case IES_NE:
  647. case IES_LT:
  648. case IES_LE:
  649. case IES_GT:
  650. case IES_GE:
  651. case IES_LSHIFT:
  652. case IES_RSHIFT:
  653. case IES_PLUS:
  654. case IES_NOT:
  655. case IES_MULTIPLY:
  656. case IES_DIVIDE:
  657. case IES_MOD:
  658. case IES_LPAREN:
  659. case IES_RPAREN:
  660. case IES_LBRAC:
  661. case IES_RBRAC:
  662. case IES_INTEGER:
  663. case IES_REGISTER:
  664. case IES_INIT:
  665. case IES_OFFSET:
  666. State = IES_MINUS;
  667. // push minus operator if it is not a negate operator
  668. if (CurrState == IES_REGISTER || CurrState == IES_RPAREN ||
  669. CurrState == IES_INTEGER || CurrState == IES_RBRAC ||
  670. CurrState == IES_OFFSET)
  671. IC.pushOperator(IC_MINUS);
  672. else if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
  673. // We have negate operator for Scale: it's illegal
  674. ErrMsg = "Scale can't be negative";
  675. return true;
  676. } else
  677. IC.pushOperator(IC_NEG);
  678. if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
  679. // If we already have a BaseReg, then assume this is the IndexReg with
  680. // no explicit scale.
  681. if (!BaseReg) {
  682. BaseReg = TmpReg;
  683. } else {
  684. if (IndexReg) {
  685. ErrMsg = "BaseReg/IndexReg already set!";
  686. return true;
  687. }
  688. IndexReg = TmpReg;
  689. Scale = 0;
  690. }
  691. }
  692. break;
  693. }
  694. PrevState = CurrState;
  695. return false;
  696. }
  697. void onNot() {
  698. IntelExprState CurrState = State;
  699. switch (State) {
  700. default:
  701. State = IES_ERROR;
  702. break;
  703. case IES_OR:
  704. case IES_XOR:
  705. case IES_AND:
  706. case IES_EQ:
  707. case IES_NE:
  708. case IES_LT:
  709. case IES_LE:
  710. case IES_GT:
  711. case IES_GE:
  712. case IES_LSHIFT:
  713. case IES_RSHIFT:
  714. case IES_PLUS:
  715. case IES_MINUS:
  716. case IES_NOT:
  717. case IES_MULTIPLY:
  718. case IES_DIVIDE:
  719. case IES_MOD:
  720. case IES_LPAREN:
  721. case IES_LBRAC:
  722. case IES_INIT:
  723. State = IES_NOT;
  724. IC.pushOperator(IC_NOT);
  725. break;
  726. }
  727. PrevState = CurrState;
  728. }
  729. bool onRegister(unsigned Reg, StringRef &ErrMsg) {
  730. IntelExprState CurrState = State;
  731. switch (State) {
  732. default:
  733. State = IES_ERROR;
  734. break;
  735. case IES_PLUS:
  736. case IES_LPAREN:
  737. case IES_LBRAC:
  738. State = IES_REGISTER;
  739. TmpReg = Reg;
  740. IC.pushOperand(IC_REGISTER);
  741. break;
  742. case IES_MULTIPLY:
  743. // Index Register - Scale * Register
  744. if (PrevState == IES_INTEGER) {
  745. if (IndexReg) {
  746. ErrMsg = "BaseReg/IndexReg already set!";
  747. return true;
  748. }
  749. State = IES_REGISTER;
  750. IndexReg = Reg;
  751. // Get the scale and replace the 'Scale * Register' with '0'.
  752. Scale = IC.popOperand();
  753. if (checkScale(Scale, ErrMsg))
  754. return true;
  755. IC.pushOperand(IC_IMM);
  756. IC.popOperator();
  757. } else {
  758. State = IES_ERROR;
  759. }
  760. break;
  761. }
  762. PrevState = CurrState;
  763. return false;
  764. }
  765. bool onIdentifierExpr(const MCExpr *SymRef, StringRef SymRefName,
  766. const InlineAsmIdentifierInfo &IDInfo,
  767. const AsmTypeInfo &Type, bool ParsingMSInlineAsm,
  768. StringRef &ErrMsg) {
  769. // InlineAsm: Treat an enum value as an integer
  770. if (ParsingMSInlineAsm)
  771. if (IDInfo.isKind(InlineAsmIdentifierInfo::IK_EnumVal))
  772. return onInteger(IDInfo.Enum.EnumVal, ErrMsg);
  773. // Treat a symbolic constant like an integer
  774. if (auto *CE = dyn_cast<MCConstantExpr>(SymRef))
  775. return onInteger(CE->getValue(), ErrMsg);
  776. PrevState = State;
  777. switch (State) {
  778. default:
  779. State = IES_ERROR;
  780. break;
  781. case IES_CAST:
  782. case IES_PLUS:
  783. case IES_MINUS:
  784. case IES_NOT:
  785. case IES_INIT:
  786. case IES_LBRAC:
  787. case IES_LPAREN:
  788. if (setSymRef(SymRef, SymRefName, ErrMsg))
  789. return true;
  790. MemExpr = true;
  791. State = IES_INTEGER;
  792. IC.pushOperand(IC_IMM);
  793. if (ParsingMSInlineAsm)
  794. Info = IDInfo;
  795. setTypeInfo(Type);
  796. break;
  797. }
  798. return false;
  799. }
  800. bool onInteger(int64_t TmpInt, StringRef &ErrMsg) {
  801. IntelExprState CurrState = State;
  802. switch (State) {
  803. default:
  804. State = IES_ERROR;
  805. break;
  806. case IES_PLUS:
  807. case IES_MINUS:
  808. case IES_NOT:
  809. case IES_OR:
  810. case IES_XOR:
  811. case IES_AND:
  812. case IES_EQ:
  813. case IES_NE:
  814. case IES_LT:
  815. case IES_LE:
  816. case IES_GT:
  817. case IES_GE:
  818. case IES_LSHIFT:
  819. case IES_RSHIFT:
  820. case IES_DIVIDE:
  821. case IES_MOD:
  822. case IES_MULTIPLY:
  823. case IES_LPAREN:
  824. case IES_INIT:
  825. case IES_LBRAC:
  826. State = IES_INTEGER;
  827. if (PrevState == IES_REGISTER && CurrState == IES_MULTIPLY) {
  828. // Index Register - Register * Scale
  829. if (IndexReg) {
  830. ErrMsg = "BaseReg/IndexReg already set!";
  831. return true;
  832. }
  833. IndexReg = TmpReg;
  834. Scale = TmpInt;
  835. if (checkScale(Scale, ErrMsg))
  836. return true;
  837. // Get the scale and replace the 'Register * Scale' with '0'.
  838. IC.popOperator();
  839. } else {
  840. IC.pushOperand(IC_IMM, TmpInt);
  841. }
  842. break;
  843. }
  844. PrevState = CurrState;
  845. return false;
  846. }
  847. void onStar() {
  848. PrevState = State;
  849. switch (State) {
  850. default:
  851. State = IES_ERROR;
  852. break;
  853. case IES_INTEGER:
  854. case IES_REGISTER:
  855. case IES_RPAREN:
  856. State = IES_MULTIPLY;
  857. IC.pushOperator(IC_MULTIPLY);
  858. break;
  859. }
  860. }
  861. void onDivide() {
  862. PrevState = State;
  863. switch (State) {
  864. default:
  865. State = IES_ERROR;
  866. break;
  867. case IES_INTEGER:
  868. case IES_RPAREN:
  869. State = IES_DIVIDE;
  870. IC.pushOperator(IC_DIVIDE);
  871. break;
  872. }
  873. }
  874. void onMod() {
  875. PrevState = State;
  876. switch (State) {
  877. default:
  878. State = IES_ERROR;
  879. break;
  880. case IES_INTEGER:
  881. case IES_RPAREN:
  882. State = IES_MOD;
  883. IC.pushOperator(IC_MOD);
  884. break;
  885. }
  886. }
  887. bool onLBrac() {
  888. if (BracCount)
  889. return true;
  890. PrevState = State;
  891. switch (State) {
  892. default:
  893. State = IES_ERROR;
  894. break;
  895. case IES_RBRAC:
  896. case IES_INTEGER:
  897. case IES_RPAREN:
  898. State = IES_PLUS;
  899. IC.pushOperator(IC_PLUS);
  900. CurType.Length = 1;
  901. CurType.Size = CurType.ElementSize;
  902. break;
  903. case IES_INIT:
  904. case IES_CAST:
  905. assert(!BracCount && "BracCount should be zero on parsing's start");
  906. State = IES_LBRAC;
  907. break;
  908. }
  909. MemExpr = true;
  910. BracCount++;
  911. return false;
  912. }
  913. bool onRBrac() {
  914. IntelExprState CurrState = State;
  915. switch (State) {
  916. default:
  917. State = IES_ERROR;
  918. break;
  919. case IES_INTEGER:
  920. case IES_OFFSET:
  921. case IES_REGISTER:
  922. case IES_RPAREN:
  923. if (BracCount-- != 1)
  924. return true;
  925. State = IES_RBRAC;
  926. if (CurrState == IES_REGISTER && PrevState != IES_MULTIPLY) {
  927. // If we already have a BaseReg, then assume this is the IndexReg with
  928. // no explicit scale.
  929. if (!BaseReg) {
  930. BaseReg = TmpReg;
  931. } else {
  932. assert (!IndexReg && "BaseReg/IndexReg already set!");
  933. IndexReg = TmpReg;
  934. Scale = 0;
  935. }
  936. }
  937. break;
  938. }
  939. PrevState = CurrState;
  940. return false;
  941. }
  942. void onLParen() {
  943. IntelExprState CurrState = State;
  944. switch (State) {
  945. default:
  946. State = IES_ERROR;
  947. break;
  948. case IES_PLUS:
  949. case IES_MINUS:
  950. case IES_NOT:
  951. case IES_OR:
  952. case IES_XOR:
  953. case IES_AND:
  954. case IES_EQ:
  955. case IES_NE:
  956. case IES_LT:
  957. case IES_LE:
  958. case IES_GT:
  959. case IES_GE:
  960. case IES_LSHIFT:
  961. case IES_RSHIFT:
  962. case IES_MULTIPLY:
  963. case IES_DIVIDE:
  964. case IES_MOD:
  965. case IES_LPAREN:
  966. case IES_INIT:
  967. case IES_LBRAC:
  968. State = IES_LPAREN;
  969. IC.pushOperator(IC_LPAREN);
  970. break;
  971. }
  972. PrevState = CurrState;
  973. }
  974. void onRParen() {
  975. PrevState = State;
  976. switch (State) {
  977. default:
  978. State = IES_ERROR;
  979. break;
  980. case IES_INTEGER:
  981. case IES_OFFSET:
  982. case IES_REGISTER:
  983. case IES_RBRAC:
  984. case IES_RPAREN:
  985. State = IES_RPAREN;
  986. IC.pushOperator(IC_RPAREN);
  987. break;
  988. }
  989. }
  990. bool onOffset(const MCExpr *Val, SMLoc OffsetLoc, StringRef ID,
  991. const InlineAsmIdentifierInfo &IDInfo,
  992. bool ParsingMSInlineAsm, StringRef &ErrMsg) {
  993. PrevState = State;
  994. switch (State) {
  995. default:
  996. ErrMsg = "unexpected offset operator expression";
  997. return true;
  998. case IES_PLUS:
  999. case IES_INIT:
  1000. case IES_LBRAC:
  1001. if (setSymRef(Val, ID, ErrMsg))
  1002. return true;
  1003. OffsetOperator = true;
  1004. OffsetOperatorLoc = OffsetLoc;
  1005. State = IES_OFFSET;
  1006. // As we cannot yet resolve the actual value (offset), we retain
  1007. // the requested semantics by pushing a '0' to the operands stack
  1008. IC.pushOperand(IC_IMM);
  1009. if (ParsingMSInlineAsm) {
  1010. Info = IDInfo;
  1011. }
  1012. break;
  1013. }
  1014. return false;
  1015. }
  1016. void onCast(AsmTypeInfo Info) {
  1017. PrevState = State;
  1018. switch (State) {
  1019. default:
  1020. State = IES_ERROR;
  1021. break;
  1022. case IES_LPAREN:
  1023. setTypeInfo(Info);
  1024. State = IES_CAST;
  1025. break;
  1026. }
  1027. }
  1028. void setTypeInfo(AsmTypeInfo Type) { CurType = Type; }
  1029. };
  1030. bool Error(SMLoc L, const Twine &Msg, SMRange Range = None,
  1031. bool MatchingInlineAsm = false) {
  1032. MCAsmParser &Parser = getParser();
  1033. if (MatchingInlineAsm) {
  1034. if (!getLexer().isAtStartOfStatement())
  1035. Parser.eatToEndOfStatement();
  1036. return false;
  1037. }
  1038. return Parser.Error(L, Msg, Range);
  1039. }
  1040. bool MatchRegisterByName(unsigned &RegNo, StringRef RegName, SMLoc StartLoc,
  1041. SMLoc EndLoc);
  1042. bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc,
  1043. bool RestoreOnFailure);
  1044. std::unique_ptr<X86Operand> DefaultMemSIOperand(SMLoc Loc);
  1045. std::unique_ptr<X86Operand> DefaultMemDIOperand(SMLoc Loc);
  1046. bool IsSIReg(unsigned Reg);
  1047. unsigned GetSIDIForRegClass(unsigned RegClassID, unsigned Reg, bool IsSIReg);
  1048. void
  1049. AddDefaultSrcDestOperands(OperandVector &Operands,
  1050. std::unique_ptr<llvm::MCParsedAsmOperand> &&Src,
  1051. std::unique_ptr<llvm::MCParsedAsmOperand> &&Dst);
  1052. bool VerifyAndAdjustOperands(OperandVector &OrigOperands,
  1053. OperandVector &FinalOperands);
  1054. bool ParseOperand(OperandVector &Operands);
  1055. bool ParseATTOperand(OperandVector &Operands);
  1056. bool ParseIntelOperand(OperandVector &Operands);
  1057. bool ParseIntelOffsetOperator(const MCExpr *&Val, StringRef &ID,
  1058. InlineAsmIdentifierInfo &Info, SMLoc &End);
  1059. bool ParseIntelDotOperator(IntelExprStateMachine &SM, SMLoc &End);
  1060. unsigned IdentifyIntelInlineAsmOperator(StringRef Name);
  1061. unsigned ParseIntelInlineAsmOperator(unsigned OpKind);
  1062. unsigned IdentifyMasmOperator(StringRef Name);
  1063. bool ParseMasmOperator(unsigned OpKind, int64_t &Val);
  1064. bool ParseRoundingModeOp(SMLoc Start, OperandVector &Operands);
  1065. bool ParseIntelNamedOperator(StringRef Name, IntelExprStateMachine &SM,
  1066. bool &ParseError, SMLoc &End);
  1067. bool ParseMasmNamedOperator(StringRef Name, IntelExprStateMachine &SM,
  1068. bool &ParseError, SMLoc &End);
  1069. void RewriteIntelExpression(IntelExprStateMachine &SM, SMLoc Start,
  1070. SMLoc End);
  1071. bool ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End);
  1072. bool ParseIntelInlineAsmIdentifier(const MCExpr *&Val, StringRef &Identifier,
  1073. InlineAsmIdentifierInfo &Info,
  1074. bool IsUnevaluatedOperand, SMLoc &End,
  1075. bool IsParsingOffsetOperator = false);
  1076. bool ParseMemOperand(unsigned SegReg, const MCExpr *Disp, SMLoc StartLoc,
  1077. SMLoc EndLoc, OperandVector &Operands);
  1078. X86::CondCode ParseConditionCode(StringRef CCode);
  1079. bool ParseIntelMemoryOperandSize(unsigned &Size);
  1080. bool CreateMemForMSInlineAsm(unsigned SegReg, const MCExpr *Disp,
  1081. unsigned BaseReg, unsigned IndexReg,
  1082. unsigned Scale, SMLoc Start, SMLoc End,
  1083. unsigned Size, StringRef Identifier,
  1084. const InlineAsmIdentifierInfo &Info,
  1085. OperandVector &Operands);
  1086. bool parseDirectiveArch();
  1087. bool parseDirectiveNops(SMLoc L);
  1088. bool parseDirectiveEven(SMLoc L);
  1089. bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
  1090. /// CodeView FPO data directives.
  1091. bool parseDirectiveFPOProc(SMLoc L);
  1092. bool parseDirectiveFPOSetFrame(SMLoc L);
  1093. bool parseDirectiveFPOPushReg(SMLoc L);
  1094. bool parseDirectiveFPOStackAlloc(SMLoc L);
  1095. bool parseDirectiveFPOStackAlign(SMLoc L);
  1096. bool parseDirectiveFPOEndPrologue(SMLoc L);
  1097. bool parseDirectiveFPOEndProc(SMLoc L);
  1098. /// SEH directives.
  1099. bool parseSEHRegisterNumber(unsigned RegClassID, unsigned &RegNo);
  1100. bool parseDirectiveSEHPushReg(SMLoc);
  1101. bool parseDirectiveSEHSetFrame(SMLoc);
  1102. bool parseDirectiveSEHSaveReg(SMLoc);
  1103. bool parseDirectiveSEHSaveXMM(SMLoc);
  1104. bool parseDirectiveSEHPushFrame(SMLoc);
  1105. unsigned checkTargetMatchPredicate(MCInst &Inst) override;
  1106. bool validateInstruction(MCInst &Inst, const OperandVector &Ops);
  1107. bool processInstruction(MCInst &Inst, const OperandVector &Ops);
  1108. // Load Value Injection (LVI) Mitigations for machine code
  1109. void emitWarningForSpecialLVIInstruction(SMLoc Loc);
  1110. void applyLVICFIMitigation(MCInst &Inst, MCStreamer &Out);
  1111. void applyLVILoadHardeningMitigation(MCInst &Inst, MCStreamer &Out);
  1112. /// Wrapper around MCStreamer::emitInstruction(). Possibly adds
  1113. /// instrumentation around Inst.
  1114. void emitInstruction(MCInst &Inst, OperandVector &Operands, MCStreamer &Out);
  1115. bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
  1116. OperandVector &Operands, MCStreamer &Out,
  1117. uint64_t &ErrorInfo,
  1118. bool MatchingInlineAsm) override;
  1119. void MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op, OperandVector &Operands,
  1120. MCStreamer &Out, bool MatchingInlineAsm);
  1121. bool ErrorMissingFeature(SMLoc IDLoc, const FeatureBitset &MissingFeatures,
  1122. bool MatchingInlineAsm);
  1123. bool MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode,
  1124. OperandVector &Operands, MCStreamer &Out,
  1125. uint64_t &ErrorInfo,
  1126. bool MatchingInlineAsm);
  1127. bool MatchAndEmitIntelInstruction(SMLoc IDLoc, unsigned &Opcode,
  1128. OperandVector &Operands, MCStreamer &Out,
  1129. uint64_t &ErrorInfo,
  1130. bool MatchingInlineAsm);
  1131. bool OmitRegisterFromClobberLists(unsigned RegNo) override;
  1132. /// Parses AVX512 specific operand primitives: masked registers ({%k<NUM>}, {z})
  1133. /// and memory broadcasting ({1to<NUM>}) primitives, updating Operands vector if required.
  1134. /// return false if no parsing errors occurred, true otherwise.
  1135. bool HandleAVX512Operand(OperandVector &Operands);
  1136. bool ParseZ(std::unique_ptr<X86Operand> &Z, const SMLoc &StartLoc);
  1137. bool is64BitMode() const {
  1138. // FIXME: Can tablegen auto-generate this?
  1139. return getSTI().getFeatureBits()[X86::Mode64Bit];
  1140. }
  1141. bool is32BitMode() const {
  1142. // FIXME: Can tablegen auto-generate this?
  1143. return getSTI().getFeatureBits()[X86::Mode32Bit];
  1144. }
  1145. bool is16BitMode() const {
  1146. // FIXME: Can tablegen auto-generate this?
  1147. return getSTI().getFeatureBits()[X86::Mode16Bit];
  1148. }
  1149. void SwitchMode(unsigned mode) {
  1150. MCSubtargetInfo &STI = copySTI();
  1151. FeatureBitset AllModes({X86::Mode64Bit, X86::Mode32Bit, X86::Mode16Bit});
  1152. FeatureBitset OldMode = STI.getFeatureBits() & AllModes;
  1153. FeatureBitset FB = ComputeAvailableFeatures(
  1154. STI.ToggleFeature(OldMode.flip(mode)));
  1155. setAvailableFeatures(FB);
  1156. assert(FeatureBitset({mode}) == (STI.getFeatureBits() & AllModes));
  1157. }
  1158. unsigned getPointerWidth() {
  1159. if (is16BitMode()) return 16;
  1160. if (is32BitMode()) return 32;
  1161. if (is64BitMode()) return 64;
  1162. llvm_unreachable("invalid mode");
  1163. }
  1164. bool isParsingIntelSyntax() {
  1165. return getParser().getAssemblerDialect();
  1166. }
  1167. /// @name Auto-generated Matcher Functions
  1168. /// {
  1169. #define GET_ASSEMBLER_HEADER
  1170. #include "X86GenAsmMatcher.inc"
  1171. /// }
  1172. public:
  1173. enum X86MatchResultTy {
  1174. Match_Unsupported = FIRST_TARGET_MATCH_RESULT_TY,
  1175. #define GET_OPERAND_DIAGNOSTIC_TYPES
  1176. #include "X86GenAsmMatcher.inc"
  1177. };
  1178. X86AsmParser(const MCSubtargetInfo &sti, MCAsmParser &Parser,
  1179. const MCInstrInfo &mii, const MCTargetOptions &Options)
  1180. : MCTargetAsmParser(Options, sti, mii), InstInfo(nullptr),
  1181. Code16GCC(false) {
  1182. Parser.addAliasForDirective(".word", ".2byte");
  1183. // Initialize the set of available features.
  1184. setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
  1185. }
  1186. bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
  1187. OperandMatchResultTy tryParseRegister(unsigned &RegNo, SMLoc &StartLoc,
  1188. SMLoc &EndLoc) override;
  1189. bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
  1190. bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
  1191. SMLoc NameLoc, OperandVector &Operands) override;
  1192. bool ParseDirective(AsmToken DirectiveID) override;
  1193. };
  1194. } // end anonymous namespace
  1195. /// @name Auto-generated Match Functions
  1196. /// {
  1197. static unsigned MatchRegisterName(StringRef Name);
  1198. /// }
  1199. static bool CheckBaseRegAndIndexRegAndScale(unsigned BaseReg, unsigned IndexReg,
  1200. unsigned Scale, bool Is64BitMode,
  1201. StringRef &ErrMsg) {
  1202. // If we have both a base register and an index register make sure they are
  1203. // both 64-bit or 32-bit registers.
  1204. // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
  1205. if (BaseReg != 0 &&
  1206. !(BaseReg == X86::RIP || BaseReg == X86::EIP ||
  1207. X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) ||
  1208. X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) ||
  1209. X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg))) {
  1210. ErrMsg = "invalid base+index expression";
  1211. return true;
  1212. }
  1213. if (IndexReg != 0 &&
  1214. !(IndexReg == X86::EIZ || IndexReg == X86::RIZ ||
  1215. X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
  1216. X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
  1217. X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg) ||
  1218. X86MCRegisterClasses[X86::VR128XRegClassID].contains(IndexReg) ||
  1219. X86MCRegisterClasses[X86::VR256XRegClassID].contains(IndexReg) ||
  1220. X86MCRegisterClasses[X86::VR512RegClassID].contains(IndexReg))) {
  1221. ErrMsg = "invalid base+index expression";
  1222. return true;
  1223. }
  1224. if (((BaseReg == X86::RIP || BaseReg == X86::EIP) && IndexReg != 0) ||
  1225. IndexReg == X86::EIP || IndexReg == X86::RIP ||
  1226. IndexReg == X86::ESP || IndexReg == X86::RSP) {
  1227. ErrMsg = "invalid base+index expression";
  1228. return true;
  1229. }
  1230. // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed,
  1231. // and then only in non-64-bit modes.
  1232. if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
  1233. (Is64BitMode || (BaseReg != X86::BX && BaseReg != X86::BP &&
  1234. BaseReg != X86::SI && BaseReg != X86::DI))) {
  1235. ErrMsg = "invalid 16-bit base register";
  1236. return true;
  1237. }
  1238. if (BaseReg == 0 &&
  1239. X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) {
  1240. ErrMsg = "16-bit memory operand may not include only index register";
  1241. return true;
  1242. }
  1243. if (BaseReg != 0 && IndexReg != 0) {
  1244. if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
  1245. (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
  1246. X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
  1247. IndexReg == X86::EIZ)) {
  1248. ErrMsg = "base register is 64-bit, but index register is not";
  1249. return true;
  1250. }
  1251. if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
  1252. (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
  1253. X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg) ||
  1254. IndexReg == X86::RIZ)) {
  1255. ErrMsg = "base register is 32-bit, but index register is not";
  1256. return true;
  1257. }
  1258. if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg)) {
  1259. if (X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) ||
  1260. X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) {
  1261. ErrMsg = "base register is 16-bit, but index register is not";
  1262. return true;
  1263. }
  1264. if ((BaseReg != X86::BX && BaseReg != X86::BP) ||
  1265. (IndexReg != X86::SI && IndexReg != X86::DI)) {
  1266. ErrMsg = "invalid 16-bit base/index register combination";
  1267. return true;
  1268. }
  1269. }
  1270. }
  1271. // RIP/EIP-relative addressing is only supported in 64-bit mode.
  1272. if (!Is64BitMode && BaseReg != 0 &&
  1273. (BaseReg == X86::RIP || BaseReg == X86::EIP)) {
  1274. ErrMsg = "IP-relative addressing requires 64-bit mode";
  1275. return true;
  1276. }
  1277. return checkScale(Scale, ErrMsg);
  1278. }
  1279. bool X86AsmParser::MatchRegisterByName(unsigned &RegNo, StringRef RegName,
  1280. SMLoc StartLoc, SMLoc EndLoc) {
  1281. // If we encounter a %, ignore it. This code handles registers with and
  1282. // without the prefix, unprefixed registers can occur in cfi directives.
  1283. RegName.consume_front("%");
  1284. RegNo = MatchRegisterName(RegName);
  1285. // If the match failed, try the register name as lowercase.
  1286. if (RegNo == 0)
  1287. RegNo = MatchRegisterName(RegName.lower());
  1288. // The "flags" and "mxcsr" registers cannot be referenced directly.
  1289. // Treat it as an identifier instead.
  1290. if (isParsingMSInlineAsm() && isParsingIntelSyntax() &&
  1291. (RegNo == X86::EFLAGS || RegNo == X86::MXCSR))
  1292. RegNo = 0;
  1293. if (!is64BitMode()) {
  1294. // FIXME: This should be done using Requires<Not64BitMode> and
  1295. // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
  1296. // checked.
  1297. if (RegNo == X86::RIZ || RegNo == X86::RIP ||
  1298. X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
  1299. X86II::isX86_64NonExtLowByteReg(RegNo) ||
  1300. X86II::isX86_64ExtendedReg(RegNo)) {
  1301. return Error(StartLoc,
  1302. "register %" + RegName + " is only available in 64-bit mode",
  1303. SMRange(StartLoc, EndLoc));
  1304. }
  1305. }
  1306. // If this is "db[0-15]", match it as an alias
  1307. // for dr[0-15].
  1308. if (RegNo == 0 && RegName.startswith("db")) {
  1309. if (RegName.size() == 3) {
  1310. switch (RegName[2]) {
  1311. case '0':
  1312. RegNo = X86::DR0;
  1313. break;
  1314. case '1':
  1315. RegNo = X86::DR1;
  1316. break;
  1317. case '2':
  1318. RegNo = X86::DR2;
  1319. break;
  1320. case '3':
  1321. RegNo = X86::DR3;
  1322. break;
  1323. case '4':
  1324. RegNo = X86::DR4;
  1325. break;
  1326. case '5':
  1327. RegNo = X86::DR5;
  1328. break;
  1329. case '6':
  1330. RegNo = X86::DR6;
  1331. break;
  1332. case '7':
  1333. RegNo = X86::DR7;
  1334. break;
  1335. case '8':
  1336. RegNo = X86::DR8;
  1337. break;
  1338. case '9':
  1339. RegNo = X86::DR9;
  1340. break;
  1341. }
  1342. } else if (RegName.size() == 4 && RegName[2] == '1') {
  1343. switch (RegName[3]) {
  1344. case '0':
  1345. RegNo = X86::DR10;
  1346. break;
  1347. case '1':
  1348. RegNo = X86::DR11;
  1349. break;
  1350. case '2':
  1351. RegNo = X86::DR12;
  1352. break;
  1353. case '3':
  1354. RegNo = X86::DR13;
  1355. break;
  1356. case '4':
  1357. RegNo = X86::DR14;
  1358. break;
  1359. case '5':
  1360. RegNo = X86::DR15;
  1361. break;
  1362. }
  1363. }
  1364. }
  1365. if (RegNo == 0) {
  1366. if (isParsingIntelSyntax())
  1367. return true;
  1368. return Error(StartLoc, "invalid register name", SMRange(StartLoc, EndLoc));
  1369. }
  1370. return false;
  1371. }
  1372. bool X86AsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
  1373. SMLoc &EndLoc, bool RestoreOnFailure) {
  1374. MCAsmParser &Parser = getParser();
  1375. MCAsmLexer &Lexer = getLexer();
  1376. RegNo = 0;
  1377. SmallVector<AsmToken, 5> Tokens;
  1378. auto OnFailure = [RestoreOnFailure, &Lexer, &Tokens]() {
  1379. if (RestoreOnFailure) {
  1380. while (!Tokens.empty()) {
  1381. Lexer.UnLex(Tokens.pop_back_val());
  1382. }
  1383. }
  1384. };
  1385. const AsmToken &PercentTok = Parser.getTok();
  1386. StartLoc = PercentTok.getLoc();
  1387. // If we encounter a %, ignore it. This code handles registers with and
  1388. // without the prefix, unprefixed registers can occur in cfi directives.
  1389. if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent)) {
  1390. Tokens.push_back(PercentTok);
  1391. Parser.Lex(); // Eat percent token.
  1392. }
  1393. const AsmToken &Tok = Parser.getTok();
  1394. EndLoc = Tok.getEndLoc();
  1395. if (Tok.isNot(AsmToken::Identifier)) {
  1396. OnFailure();
  1397. if (isParsingIntelSyntax()) return true;
  1398. return Error(StartLoc, "invalid register name",
  1399. SMRange(StartLoc, EndLoc));
  1400. }
  1401. if (MatchRegisterByName(RegNo, Tok.getString(), StartLoc, EndLoc)) {
  1402. OnFailure();
  1403. return true;
  1404. }
  1405. // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
  1406. if (RegNo == X86::ST0) {
  1407. Tokens.push_back(Tok);
  1408. Parser.Lex(); // Eat 'st'
  1409. // Check to see if we have '(4)' after %st.
  1410. if (Lexer.isNot(AsmToken::LParen))
  1411. return false;
  1412. // Lex the paren.
  1413. Tokens.push_back(Parser.getTok());
  1414. Parser.Lex();
  1415. const AsmToken &IntTok = Parser.getTok();
  1416. if (IntTok.isNot(AsmToken::Integer)) {
  1417. OnFailure();
  1418. return Error(IntTok.getLoc(), "expected stack index");
  1419. }
  1420. switch (IntTok.getIntVal()) {
  1421. case 0: RegNo = X86::ST0; break;
  1422. case 1: RegNo = X86::ST1; break;
  1423. case 2: RegNo = X86::ST2; break;
  1424. case 3: RegNo = X86::ST3; break;
  1425. case 4: RegNo = X86::ST4; break;
  1426. case 5: RegNo = X86::ST5; break;
  1427. case 6: RegNo = X86::ST6; break;
  1428. case 7: RegNo = X86::ST7; break;
  1429. default:
  1430. OnFailure();
  1431. return Error(IntTok.getLoc(), "invalid stack index");
  1432. }
  1433. // Lex IntTok
  1434. Tokens.push_back(IntTok);
  1435. Parser.Lex();
  1436. if (Lexer.isNot(AsmToken::RParen)) {
  1437. OnFailure();
  1438. return Error(Parser.getTok().getLoc(), "expected ')'");
  1439. }
  1440. EndLoc = Parser.getTok().getEndLoc();
  1441. Parser.Lex(); // Eat ')'
  1442. return false;
  1443. }
  1444. EndLoc = Parser.getTok().getEndLoc();
  1445. if (RegNo == 0) {
  1446. OnFailure();
  1447. if (isParsingIntelSyntax()) return true;
  1448. return Error(StartLoc, "invalid register name",
  1449. SMRange(StartLoc, EndLoc));
  1450. }
  1451. Parser.Lex(); // Eat identifier token.
  1452. return false;
  1453. }
  1454. bool X86AsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
  1455. SMLoc &EndLoc) {
  1456. return ParseRegister(RegNo, StartLoc, EndLoc, /*RestoreOnFailure=*/false);
  1457. }
  1458. OperandMatchResultTy X86AsmParser::tryParseRegister(unsigned &RegNo,
  1459. SMLoc &StartLoc,
  1460. SMLoc &EndLoc) {
  1461. bool Result =
  1462. ParseRegister(RegNo, StartLoc, EndLoc, /*RestoreOnFailure=*/true);
  1463. bool PendingErrors = getParser().hasPendingError();
  1464. getParser().clearPendingErrors();
  1465. if (PendingErrors)
  1466. return MatchOperand_ParseFail;
  1467. if (Result)
  1468. return MatchOperand_NoMatch;
  1469. return MatchOperand_Success;
  1470. }
  1471. std::unique_ptr<X86Operand> X86AsmParser::DefaultMemSIOperand(SMLoc Loc) {
  1472. bool Parse32 = is32BitMode() || Code16GCC;
  1473. unsigned Basereg = is64BitMode() ? X86::RSI : (Parse32 ? X86::ESI : X86::SI);
  1474. const MCExpr *Disp = MCConstantExpr::create(0, getContext());
  1475. return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp,
  1476. /*BaseReg=*/Basereg, /*IndexReg=*/0, /*Scale=*/1,
  1477. Loc, Loc, 0);
  1478. }
  1479. std::unique_ptr<X86Operand> X86AsmParser::DefaultMemDIOperand(SMLoc Loc) {
  1480. bool Parse32 = is32BitMode() || Code16GCC;
  1481. unsigned Basereg = is64BitMode() ? X86::RDI : (Parse32 ? X86::EDI : X86::DI);
  1482. const MCExpr *Disp = MCConstantExpr::create(0, getContext());
  1483. return X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp,
  1484. /*BaseReg=*/Basereg, /*IndexReg=*/0, /*Scale=*/1,
  1485. Loc, Loc, 0);
  1486. }
  1487. bool X86AsmParser::IsSIReg(unsigned Reg) {
  1488. switch (Reg) {
  1489. default: llvm_unreachable("Only (R|E)SI and (R|E)DI are expected!");
  1490. case X86::RSI:
  1491. case X86::ESI:
  1492. case X86::SI:
  1493. return true;
  1494. case X86::RDI:
  1495. case X86::EDI:
  1496. case X86::DI:
  1497. return false;
  1498. }
  1499. }
  1500. unsigned X86AsmParser::GetSIDIForRegClass(unsigned RegClassID, unsigned Reg,
  1501. bool IsSIReg) {
  1502. switch (RegClassID) {
  1503. default: llvm_unreachable("Unexpected register class");
  1504. case X86::GR64RegClassID:
  1505. return IsSIReg ? X86::RSI : X86::RDI;
  1506. case X86::GR32RegClassID:
  1507. return IsSIReg ? X86::ESI : X86::EDI;
  1508. case X86::GR16RegClassID:
  1509. return IsSIReg ? X86::SI : X86::DI;
  1510. }
  1511. }
  1512. void X86AsmParser::AddDefaultSrcDestOperands(
  1513. OperandVector& Operands, std::unique_ptr<llvm::MCParsedAsmOperand> &&Src,
  1514. std::unique_ptr<llvm::MCParsedAsmOperand> &&Dst) {
  1515. if (isParsingIntelSyntax()) {
  1516. Operands.push_back(std::move(Dst));
  1517. Operands.push_back(std::move(Src));
  1518. }
  1519. else {
  1520. Operands.push_back(std::move(Src));
  1521. Operands.push_back(std::move(Dst));
  1522. }
  1523. }
  1524. bool X86AsmParser::VerifyAndAdjustOperands(OperandVector &OrigOperands,
  1525. OperandVector &FinalOperands) {
  1526. if (OrigOperands.size() > 1) {
  1527. // Check if sizes match, OrigOperands also contains the instruction name
  1528. assert(OrigOperands.size() == FinalOperands.size() + 1 &&
  1529. "Operand size mismatch");
  1530. SmallVector<std::pair<SMLoc, std::string>, 2> Warnings;
  1531. // Verify types match
  1532. int RegClassID = -1;
  1533. for (unsigned int i = 0; i < FinalOperands.size(); ++i) {
  1534. X86Operand &OrigOp = static_cast<X86Operand &>(*OrigOperands[i + 1]);
  1535. X86Operand &FinalOp = static_cast<X86Operand &>(*FinalOperands[i]);
  1536. if (FinalOp.isReg() &&
  1537. (!OrigOp.isReg() || FinalOp.getReg() != OrigOp.getReg()))
  1538. // Return false and let a normal complaint about bogus operands happen
  1539. return false;
  1540. if (FinalOp.isMem()) {
  1541. if (!OrigOp.isMem())
  1542. // Return false and let a normal complaint about bogus operands happen
  1543. return false;
  1544. unsigned OrigReg = OrigOp.Mem.BaseReg;
  1545. unsigned FinalReg = FinalOp.Mem.BaseReg;
  1546. // If we've already encounterd a register class, make sure all register
  1547. // bases are of the same register class
  1548. if (RegClassID != -1 &&
  1549. !X86MCRegisterClasses[RegClassID].contains(OrigReg)) {
  1550. return Error(OrigOp.getStartLoc(),
  1551. "mismatching source and destination index registers");
  1552. }
  1553. if (X86MCRegisterClasses[X86::GR64RegClassID].contains(OrigReg))
  1554. RegClassID = X86::GR64RegClassID;
  1555. else if (X86MCRegisterClasses[X86::GR32RegClassID].contains(OrigReg))
  1556. RegClassID = X86::GR32RegClassID;
  1557. else if (X86MCRegisterClasses[X86::GR16RegClassID].contains(OrigReg))
  1558. RegClassID = X86::GR16RegClassID;
  1559. else
  1560. // Unexpected register class type
  1561. // Return false and let a normal complaint about bogus operands happen
  1562. return false;
  1563. bool IsSI = IsSIReg(FinalReg);
  1564. FinalReg = GetSIDIForRegClass(RegClassID, FinalReg, IsSI);
  1565. if (FinalReg != OrigReg) {
  1566. std::string RegName = IsSI ? "ES:(R|E)SI" : "ES:(R|E)DI";
  1567. Warnings.push_back(std::make_pair(
  1568. OrigOp.getStartLoc(),
  1569. "memory operand is only for determining the size, " + RegName +
  1570. " will be used for the location"));
  1571. }
  1572. FinalOp.Mem.Size = OrigOp.Mem.Size;
  1573. FinalOp.Mem.SegReg = OrigOp.Mem.SegReg;
  1574. FinalOp.Mem.BaseReg = FinalReg;
  1575. }
  1576. }
  1577. // Produce warnings only if all the operands passed the adjustment - prevent
  1578. // legal cases like "movsd (%rax), %xmm0" mistakenly produce warnings
  1579. for (auto &WarningMsg : Warnings) {
  1580. Warning(WarningMsg.first, WarningMsg.second);
  1581. }
  1582. // Remove old operands
  1583. for (unsigned int i = 0; i < FinalOperands.size(); ++i)
  1584. OrigOperands.pop_back();
  1585. }
  1586. // OrigOperands.append(FinalOperands.begin(), FinalOperands.end());
  1587. for (unsigned int i = 0; i < FinalOperands.size(); ++i)
  1588. OrigOperands.push_back(std::move(FinalOperands[i]));
  1589. return false;
  1590. }
  1591. bool X86AsmParser::ParseOperand(OperandVector &Operands) {
  1592. if (isParsingIntelSyntax())
  1593. return ParseIntelOperand(Operands);
  1594. return ParseATTOperand(Operands);
  1595. }
  1596. bool X86AsmParser::CreateMemForMSInlineAsm(
  1597. unsigned SegReg, const MCExpr *Disp, unsigned BaseReg, unsigned IndexReg,
  1598. unsigned Scale, SMLoc Start, SMLoc End, unsigned Size, StringRef Identifier,
  1599. const InlineAsmIdentifierInfo &Info, OperandVector &Operands) {
  1600. // If we found a decl other than a VarDecl, then assume it is a FuncDecl or
  1601. // some other label reference.
  1602. if (Info.isKind(InlineAsmIdentifierInfo::IK_Label)) {
  1603. // Insert an explicit size if the user didn't have one.
  1604. if (!Size) {
  1605. Size = getPointerWidth();
  1606. InstInfo->AsmRewrites->emplace_back(AOK_SizeDirective, Start,
  1607. /*Len=*/0, Size);
  1608. }
  1609. // Create an absolute memory reference in order to match against
  1610. // instructions taking a PC relative operand.
  1611. Operands.push_back(X86Operand::CreateMem(getPointerWidth(), Disp, Start,
  1612. End, Size, Identifier,
  1613. Info.Label.Decl));
  1614. return false;
  1615. }
  1616. // We either have a direct symbol reference, or an offset from a symbol. The
  1617. // parser always puts the symbol on the LHS, so look there for size
  1618. // calculation purposes.
  1619. unsigned FrontendSize = 0;
  1620. void *Decl = nullptr;
  1621. bool IsGlobalLV = false;
  1622. if (Info.isKind(InlineAsmIdentifierInfo::IK_Var)) {
  1623. // Size is in terms of bits in this context.
  1624. FrontendSize = Info.Var.Type * 8;
  1625. Decl = Info.Var.Decl;
  1626. IsGlobalLV = Info.Var.IsGlobalLV;
  1627. }
  1628. // It is widely common for MS InlineAsm to use a global variable and one/two
  1629. // registers in a mmory expression, and though unaccessible via rip/eip.
  1630. if (IsGlobalLV && (BaseReg || IndexReg)) {
  1631. Operands.push_back(X86Operand::CreateMem(getPointerWidth(), Disp, Start,
  1632. End, Size, Identifier, Decl,
  1633. FrontendSize));
  1634. return false;
  1635. }
  1636. // Otherwise, we set the base register to a non-zero value
  1637. // if we don't know the actual value at this time. This is necessary to
  1638. // get the matching correct in some cases.
  1639. BaseReg = BaseReg ? BaseReg : 1;
  1640. Operands.push_back(X86Operand::CreateMem(
  1641. getPointerWidth(), SegReg, Disp, BaseReg, IndexReg, Scale, Start, End,
  1642. Size,
  1643. /*DefaultBaseReg=*/X86::RIP, Identifier, Decl, FrontendSize));
  1644. return false;
  1645. }
  1646. // Some binary bitwise operators have a named synonymous
  1647. // Query a candidate string for being such a named operator
  1648. // and if so - invoke the appropriate handler
  1649. bool X86AsmParser::ParseIntelNamedOperator(StringRef Name,
  1650. IntelExprStateMachine &SM,
  1651. bool &ParseError, SMLoc &End) {
  1652. // A named operator should be either lower or upper case, but not a mix...
  1653. // except in MASM, which uses full case-insensitivity.
  1654. if (Name.compare(Name.lower()) && Name.compare(Name.upper()) &&
  1655. !getParser().isParsingMasm())
  1656. return false;
  1657. if (Name.equals_insensitive("not")) {
  1658. SM.onNot();
  1659. } else if (Name.equals_insensitive("or")) {
  1660. SM.onOr();
  1661. } else if (Name.equals_insensitive("shl")) {
  1662. SM.onLShift();
  1663. } else if (Name.equals_insensitive("shr")) {
  1664. SM.onRShift();
  1665. } else if (Name.equals_insensitive("xor")) {
  1666. SM.onXor();
  1667. } else if (Name.equals_insensitive("and")) {
  1668. SM.onAnd();
  1669. } else if (Name.equals_insensitive("mod")) {
  1670. SM.onMod();
  1671. } else if (Name.equals_insensitive("offset")) {
  1672. SMLoc OffsetLoc = getTok().getLoc();
  1673. const MCExpr *Val = nullptr;
  1674. StringRef ID;
  1675. InlineAsmIdentifierInfo Info;
  1676. ParseError = ParseIntelOffsetOperator(Val, ID, Info, End);
  1677. if (ParseError)
  1678. return true;
  1679. StringRef ErrMsg;
  1680. ParseError =
  1681. SM.onOffset(Val, OffsetLoc, ID, Info, isParsingMSInlineAsm(), ErrMsg);
  1682. if (ParseError)
  1683. return Error(SMLoc::getFromPointer(Name.data()), ErrMsg);
  1684. } else {
  1685. return false;
  1686. }
  1687. if (!Name.equals_insensitive("offset"))
  1688. End = consumeToken();
  1689. return true;
  1690. }
  1691. bool X86AsmParser::ParseMasmNamedOperator(StringRef Name,
  1692. IntelExprStateMachine &SM,
  1693. bool &ParseError, SMLoc &End) {
  1694. if (Name.equals_insensitive("eq")) {
  1695. SM.onEq();
  1696. } else if (Name.equals_insensitive("ne")) {
  1697. SM.onNE();
  1698. } else if (Name.equals_insensitive("lt")) {
  1699. SM.onLT();
  1700. } else if (Name.equals_insensitive("le")) {
  1701. SM.onLE();
  1702. } else if (Name.equals_insensitive("gt")) {
  1703. SM.onGT();
  1704. } else if (Name.equals_insensitive("ge")) {
  1705. SM.onGE();
  1706. } else {
  1707. return false;
  1708. }
  1709. End = consumeToken();
  1710. return true;
  1711. }
  1712. bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
  1713. MCAsmParser &Parser = getParser();
  1714. StringRef ErrMsg;
  1715. AsmToken::TokenKind PrevTK = AsmToken::Error;
  1716. bool Done = false;
  1717. while (!Done) {
  1718. // Get a fresh reference on each loop iteration in case the previous
  1719. // iteration moved the token storage during UnLex().
  1720. const AsmToken &Tok = Parser.getTok();
  1721. bool UpdateLocLex = true;
  1722. AsmToken::TokenKind TK = getLexer().getKind();
  1723. switch (TK) {
  1724. default:
  1725. if ((Done = SM.isValidEndState()))
  1726. break;
  1727. return Error(Tok.getLoc(), "unknown token in expression");
  1728. case AsmToken::Error:
  1729. return Error(getLexer().getErrLoc(), getLexer().getErr());
  1730. break;
  1731. case AsmToken::EndOfStatement:
  1732. Done = true;
  1733. break;
  1734. case AsmToken::Real:
  1735. // DotOperator: [ebx].0
  1736. UpdateLocLex = false;
  1737. if (ParseIntelDotOperator(SM, End))
  1738. return true;
  1739. break;
  1740. case AsmToken::Dot:
  1741. if (!Parser.isParsingMasm()) {
  1742. if ((Done = SM.isValidEndState()))
  1743. break;
  1744. return Error(Tok.getLoc(), "unknown token in expression");
  1745. }
  1746. // MASM allows spaces around the dot operator (e.g., "var . x")
  1747. Lex();
  1748. UpdateLocLex = false;
  1749. if (ParseIntelDotOperator(SM, End))
  1750. return true;
  1751. break;
  1752. case AsmToken::Dollar:
  1753. if (!Parser.isParsingMasm()) {
  1754. if ((Done = SM.isValidEndState()))
  1755. break;
  1756. return Error(Tok.getLoc(), "unknown token in expression");
  1757. }
  1758. LLVM_FALLTHROUGH;
  1759. case AsmToken::String: {
  1760. if (Parser.isParsingMasm()) {
  1761. // MASM parsers handle strings in expressions as constants.
  1762. SMLoc ValueLoc = Tok.getLoc();
  1763. int64_t Res;
  1764. const MCExpr *Val;
  1765. if (Parser.parsePrimaryExpr(Val, End, nullptr))
  1766. return true;
  1767. UpdateLocLex = false;
  1768. if (!Val->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr()))
  1769. return Error(ValueLoc, "expected absolute value");
  1770. if (SM.onInteger(Res, ErrMsg))
  1771. return Error(ValueLoc, ErrMsg);
  1772. break;
  1773. }
  1774. LLVM_FALLTHROUGH;
  1775. }
  1776. case AsmToken::At:
  1777. case AsmToken::Identifier: {
  1778. SMLoc IdentLoc = Tok.getLoc();
  1779. StringRef Identifier = Tok.getString();
  1780. UpdateLocLex = false;
  1781. if (Parser.isParsingMasm()) {
  1782. size_t DotOffset = Identifier.find_first_of('.');
  1783. if (DotOffset != StringRef::npos) {
  1784. consumeToken();
  1785. StringRef LHS = Identifier.slice(0, DotOffset);
  1786. StringRef Dot = Identifier.slice(DotOffset, DotOffset + 1);
  1787. StringRef RHS = Identifier.slice(DotOffset + 1, StringRef::npos);
  1788. if (!RHS.empty()) {
  1789. getLexer().UnLex(AsmToken(AsmToken::Identifier, RHS));
  1790. }
  1791. getLexer().UnLex(AsmToken(AsmToken::Dot, Dot));
  1792. if (!LHS.empty()) {
  1793. getLexer().UnLex(AsmToken(AsmToken::Identifier, LHS));
  1794. }
  1795. break;
  1796. }
  1797. }
  1798. // (MASM only) <TYPE> PTR operator
  1799. if (Parser.isParsingMasm()) {
  1800. const AsmToken &NextTok = getLexer().peekTok();
  1801. if (NextTok.is(AsmToken::Identifier) &&
  1802. NextTok.getIdentifier().equals_insensitive("ptr")) {
  1803. AsmTypeInfo Info;
  1804. if (Parser.lookUpType(Identifier, Info))
  1805. return Error(Tok.getLoc(), "unknown type");
  1806. SM.onCast(Info);
  1807. // Eat type and PTR.
  1808. consumeToken();
  1809. End = consumeToken();
  1810. break;
  1811. }
  1812. }
  1813. // Register, or (MASM only) <register>.<field>
  1814. unsigned Reg;
  1815. if (Tok.is(AsmToken::Identifier)) {
  1816. if (!ParseRegister(Reg, IdentLoc, End, /*RestoreOnFailure=*/true)) {
  1817. if (SM.onRegister(Reg, ErrMsg))
  1818. return Error(IdentLoc, ErrMsg);
  1819. break;
  1820. }
  1821. if (Parser.isParsingMasm()) {
  1822. const std::pair<StringRef, StringRef> IDField =
  1823. Tok.getString().split('.');
  1824. const StringRef ID = IDField.first, Field = IDField.second;
  1825. SMLoc IDEndLoc = SMLoc::getFromPointer(ID.data() + ID.size());
  1826. if (!Field.empty() &&
  1827. !MatchRegisterByName(Reg, ID, IdentLoc, IDEndLoc)) {
  1828. if (SM.onRegister(Reg, ErrMsg))
  1829. return Error(IdentLoc, ErrMsg);
  1830. AsmFieldInfo Info;
  1831. SMLoc FieldStartLoc = SMLoc::getFromPointer(Field.data());
  1832. if (Parser.lookUpField(Field, Info))
  1833. return Error(FieldStartLoc, "unknown offset");
  1834. else if (SM.onPlus(ErrMsg))
  1835. return Error(getTok().getLoc(), ErrMsg);
  1836. else if (SM.onInteger(Info.Offset, ErrMsg))
  1837. return Error(IdentLoc, ErrMsg);
  1838. SM.setTypeInfo(Info.Type);
  1839. End = consumeToken();
  1840. break;
  1841. }
  1842. }
  1843. }
  1844. // Operator synonymous ("not", "or" etc.)
  1845. bool ParseError = false;
  1846. if (ParseIntelNamedOperator(Identifier, SM, ParseError, End)) {
  1847. if (ParseError)
  1848. return true;
  1849. break;
  1850. }
  1851. if (Parser.isParsingMasm() &&
  1852. ParseMasmNamedOperator(Identifier, SM, ParseError, End)) {
  1853. if (ParseError)
  1854. return true;
  1855. break;
  1856. }
  1857. // Symbol reference, when parsing assembly content
  1858. InlineAsmIdentifierInfo Info;
  1859. AsmFieldInfo FieldInfo;
  1860. const MCExpr *Val;
  1861. if (isParsingMSInlineAsm() || Parser.isParsingMasm()) {
  1862. // MS Dot Operator expression
  1863. if (Identifier.count('.') &&
  1864. (PrevTK == AsmToken::RBrac || PrevTK == AsmToken::RParen)) {
  1865. if (ParseIntelDotOperator(SM, End))
  1866. return true;
  1867. break;
  1868. }
  1869. }
  1870. if (isParsingMSInlineAsm()) {
  1871. // MS InlineAsm operators (TYPE/LENGTH/SIZE)
  1872. if (unsigned OpKind = IdentifyIntelInlineAsmOperator(Identifier)) {
  1873. if (int64_t Val = ParseIntelInlineAsmOperator(OpKind)) {
  1874. if (SM.onInteger(Val, ErrMsg))
  1875. return Error(IdentLoc, ErrMsg);
  1876. } else {
  1877. return true;
  1878. }
  1879. break;
  1880. }
  1881. // MS InlineAsm identifier
  1882. // Call parseIdentifier() to combine @ with the identifier behind it.
  1883. if (TK == AsmToken::At && Parser.parseIdentifier(Identifier))
  1884. return Error(IdentLoc, "expected identifier");
  1885. if (ParseIntelInlineAsmIdentifier(Val, Identifier, Info, false, End))
  1886. return true;
  1887. else if (SM.onIdentifierExpr(Val, Identifier, Info, FieldInfo.Type,
  1888. true, ErrMsg))
  1889. return Error(IdentLoc, ErrMsg);
  1890. break;
  1891. }
  1892. if (Parser.isParsingMasm()) {
  1893. if (unsigned OpKind = IdentifyMasmOperator(Identifier)) {
  1894. int64_t Val;
  1895. if (ParseMasmOperator(OpKind, Val))
  1896. return true;
  1897. if (SM.onInteger(Val, ErrMsg))
  1898. return Error(IdentLoc, ErrMsg);
  1899. break;
  1900. }
  1901. if (!getParser().lookUpType(Identifier, FieldInfo.Type)) {
  1902. // Field offset immediate; <TYPE>.<field specification>
  1903. Lex(); // eat type
  1904. bool EndDot = parseOptionalToken(AsmToken::Dot);
  1905. while (EndDot || (getTok().is(AsmToken::Identifier) &&
  1906. getTok().getString().startswith("."))) {
  1907. getParser().parseIdentifier(Identifier);
  1908. if (!EndDot)
  1909. Identifier.consume_front(".");
  1910. EndDot = Identifier.consume_back(".");
  1911. if (getParser().lookUpField(FieldInfo.Type.Name, Identifier,
  1912. FieldInfo)) {
  1913. SMLoc IDEnd =
  1914. SMLoc::getFromPointer(Identifier.data() + Identifier.size());
  1915. return Error(IdentLoc, "Unable to lookup field reference!",
  1916. SMRange(IdentLoc, IDEnd));
  1917. }
  1918. if (!EndDot)
  1919. EndDot = parseOptionalToken(AsmToken::Dot);
  1920. }
  1921. if (SM.onInteger(FieldInfo.Offset, ErrMsg))
  1922. return Error(IdentLoc, ErrMsg);
  1923. break;
  1924. }
  1925. }
  1926. if (getParser().parsePrimaryExpr(Val, End, &FieldInfo.Type)) {
  1927. return Error(Tok.getLoc(), "Unexpected identifier!");
  1928. } else if (SM.onIdentifierExpr(Val, Identifier, Info, FieldInfo.Type,
  1929. false, ErrMsg)) {
  1930. return Error(IdentLoc, ErrMsg);
  1931. }
  1932. break;
  1933. }
  1934. case AsmToken::Integer: {
  1935. // Look for 'b' or 'f' following an Integer as a directional label
  1936. SMLoc Loc = getTok().getLoc();
  1937. int64_t IntVal = getTok().getIntVal();
  1938. End = consumeToken();
  1939. UpdateLocLex = false;
  1940. if (getLexer().getKind() == AsmToken::Identifier) {
  1941. StringRef IDVal = getTok().getString();
  1942. if (IDVal == "f" || IDVal == "b") {
  1943. MCSymbol *Sym =
  1944. getContext().getDirectionalLocalSymbol(IntVal, IDVal == "b");
  1945. MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
  1946. const MCExpr *Val =
  1947. MCSymbolRefExpr::create(Sym, Variant, getContext());
  1948. if (IDVal == "b" && Sym->isUndefined())
  1949. return Error(Loc, "invalid reference to undefined symbol");
  1950. StringRef Identifier = Sym->getName();
  1951. InlineAsmIdentifierInfo Info;
  1952. AsmTypeInfo Type;
  1953. if (SM.onIdentifierExpr(Val, Identifier, Info, Type,
  1954. isParsingMSInlineAsm(), ErrMsg))
  1955. return Error(Loc, ErrMsg);
  1956. End = consumeToken();
  1957. } else {
  1958. if (SM.onInteger(IntVal, ErrMsg))
  1959. return Error(Loc, ErrMsg);
  1960. }
  1961. } else {
  1962. if (SM.onInteger(IntVal, ErrMsg))
  1963. return Error(Loc, ErrMsg);
  1964. }
  1965. break;
  1966. }
  1967. case AsmToken::Plus:
  1968. if (SM.onPlus(ErrMsg))
  1969. return Error(getTok().getLoc(), ErrMsg);
  1970. break;
  1971. case AsmToken::Minus:
  1972. if (SM.onMinus(ErrMsg))
  1973. return Error(getTok().getLoc(), ErrMsg);
  1974. break;
  1975. case AsmToken::Tilde: SM.onNot(); break;
  1976. case AsmToken::Star: SM.onStar(); break;
  1977. case AsmToken::Slash: SM.onDivide(); break;
  1978. case AsmToken::Percent: SM.onMod(); break;
  1979. case AsmToken::Pipe: SM.onOr(); break;
  1980. case AsmToken::Caret: SM.onXor(); break;
  1981. case AsmToken::Amp: SM.onAnd(); break;
  1982. case AsmToken::LessLess:
  1983. SM.onLShift(); break;
  1984. case AsmToken::GreaterGreater:
  1985. SM.onRShift(); break;
  1986. case AsmToken::LBrac:
  1987. if (SM.onLBrac())
  1988. return Error(Tok.getLoc(), "unexpected bracket encountered");
  1989. break;
  1990. case AsmToken::RBrac:
  1991. if (SM.onRBrac())
  1992. return Error(Tok.getLoc(), "unexpected bracket encountered");
  1993. break;
  1994. case AsmToken::LParen: SM.onLParen(); break;
  1995. case AsmToken::RParen: SM.onRParen(); break;
  1996. }
  1997. if (SM.hadError())
  1998. return Error(Tok.getLoc(), "unknown token in expression");
  1999. if (!Done && UpdateLocLex)
  2000. End = consumeToken();
  2001. PrevTK = TK;
  2002. }
  2003. return false;
  2004. }
  2005. void X86AsmParser::RewriteIntelExpression(IntelExprStateMachine &SM,
  2006. SMLoc Start, SMLoc End) {
  2007. SMLoc Loc = Start;
  2008. unsigned ExprLen = End.getPointer() - Start.getPointer();
  2009. // Skip everything before a symbol displacement (if we have one)
  2010. if (SM.getSym() && !SM.isOffsetOperator()) {
  2011. StringRef SymName = SM.getSymName();
  2012. if (unsigned Len = SymName.data() - Start.getPointer())
  2013. InstInfo->AsmRewrites->emplace_back(AOK_Skip, Start, Len);
  2014. Loc = SMLoc::getFromPointer(SymName.data() + SymName.size());
  2015. ExprLen = End.getPointer() - (SymName.data() + SymName.size());
  2016. // If we have only a symbol than there's no need for complex rewrite,
  2017. // simply skip everything after it
  2018. if (!(SM.getBaseReg() || SM.getIndexReg() || SM.getImm())) {
  2019. if (ExprLen)
  2020. InstInfo->AsmRewrites->emplace_back(AOK_Skip, Loc, ExprLen);
  2021. return;
  2022. }
  2023. }
  2024. // Build an Intel Expression rewrite
  2025. StringRef BaseRegStr;
  2026. StringRef IndexRegStr;
  2027. StringRef OffsetNameStr;
  2028. if (SM.getBaseReg())
  2029. BaseRegStr = X86IntelInstPrinter::getRegisterName(SM.getBaseReg());
  2030. if (SM.getIndexReg())
  2031. IndexRegStr = X86IntelInstPrinter::getRegisterName(SM.getIndexReg());
  2032. if (SM.isOffsetOperator())
  2033. OffsetNameStr = SM.getSymName();
  2034. // Emit it
  2035. IntelExpr Expr(BaseRegStr, IndexRegStr, SM.getScale(), OffsetNameStr,
  2036. SM.getImm(), SM.isMemExpr());
  2037. InstInfo->AsmRewrites->emplace_back(Loc, ExprLen, Expr);
  2038. }
  2039. // Inline assembly may use variable names with namespace alias qualifiers.
  2040. bool X86AsmParser::ParseIntelInlineAsmIdentifier(
  2041. const MCExpr *&Val, StringRef &Identifier, InlineAsmIdentifierInfo &Info,
  2042. bool IsUnevaluatedOperand, SMLoc &End, bool IsParsingOffsetOperator) {
  2043. MCAsmParser &Parser = getParser();
  2044. assert(isParsingMSInlineAsm() && "Expected to be parsing inline assembly.");
  2045. Val = nullptr;
  2046. StringRef LineBuf(Identifier.data());
  2047. SemaCallback->LookupInlineAsmIdentifier(LineBuf, Info, IsUnevaluatedOperand);
  2048. const AsmToken &Tok = Parser.getTok();
  2049. SMLoc Loc = Tok.getLoc();
  2050. // Advance the token stream until the end of the current token is
  2051. // after the end of what the frontend claimed.
  2052. const char *EndPtr = Tok.getLoc().getPointer() + LineBuf.size();
  2053. do {
  2054. End = Tok.getEndLoc();
  2055. getLexer().Lex();
  2056. } while (End.getPointer() < EndPtr);
  2057. Identifier = LineBuf;
  2058. // The frontend should end parsing on an assembler token boundary, unless it
  2059. // failed parsing.
  2060. assert((End.getPointer() == EndPtr ||
  2061. Info.isKind(InlineAsmIdentifierInfo::IK_Invalid)) &&
  2062. "frontend claimed part of a token?");
  2063. // If the identifier lookup was unsuccessful, assume that we are dealing with
  2064. // a label.
  2065. if (Info.isKind(InlineAsmIdentifierInfo::IK_Invalid)) {
  2066. StringRef InternalName =
  2067. SemaCallback->LookupInlineAsmLabel(Identifier, getSourceManager(),
  2068. Loc, false);
  2069. assert(InternalName.size() && "We should have an internal name here.");
  2070. // Push a rewrite for replacing the identifier name with the internal name,
  2071. // unless we are parsing the operand of an offset operator
  2072. if (!IsParsingOffsetOperator)
  2073. InstInfo->AsmRewrites->emplace_back(AOK_Label, Loc, Identifier.size(),
  2074. InternalName);
  2075. else
  2076. Identifier = InternalName;
  2077. } else if (Info.isKind(InlineAsmIdentifierInfo::IK_EnumVal))
  2078. return false;
  2079. // Create the symbol reference.
  2080. MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
  2081. MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
  2082. Val = MCSymbolRefExpr::create(Sym, Variant, getParser().getContext());
  2083. return false;
  2084. }
  2085. //ParseRoundingModeOp - Parse AVX-512 rounding mode operand
  2086. bool X86AsmParser::ParseRoundingModeOp(SMLoc Start, OperandVector &Operands) {
  2087. MCAsmParser &Parser = getParser();
  2088. const AsmToken &Tok = Parser.getTok();
  2089. // Eat "{" and mark the current place.
  2090. const SMLoc consumedToken = consumeToken();
  2091. if (Tok.isNot(AsmToken::Identifier))
  2092. return Error(Tok.getLoc(), "Expected an identifier after {");
  2093. if (Tok.getIdentifier().startswith("r")){
  2094. int rndMode = StringSwitch<int>(Tok.getIdentifier())
  2095. .Case("rn", X86::STATIC_ROUNDING::TO_NEAREST_INT)
  2096. .Case("rd", X86::STATIC_ROUNDING::TO_NEG_INF)
  2097. .Case("ru", X86::STATIC_ROUNDING::TO_POS_INF)
  2098. .Case("rz", X86::STATIC_ROUNDING::TO_ZERO)
  2099. .Default(-1);
  2100. if (-1 == rndMode)
  2101. return Error(Tok.getLoc(), "Invalid rounding mode.");
  2102. Parser.Lex(); // Eat "r*" of r*-sae
  2103. if (!getLexer().is(AsmToken::Minus))
  2104. return Error(Tok.getLoc(), "Expected - at this point");
  2105. Parser.Lex(); // Eat "-"
  2106. Parser.Lex(); // Eat the sae
  2107. if (!getLexer().is(AsmToken::RCurly))
  2108. return Error(Tok.getLoc(), "Expected } at this point");
  2109. SMLoc End = Tok.getEndLoc();
  2110. Parser.Lex(); // Eat "}"
  2111. const MCExpr *RndModeOp =
  2112. MCConstantExpr::create(rndMode, Parser.getContext());
  2113. Operands.push_back(X86Operand::CreateImm(RndModeOp, Start, End));
  2114. return false;
  2115. }
  2116. if(Tok.getIdentifier().equals("sae")){
  2117. Parser.Lex(); // Eat the sae
  2118. if (!getLexer().is(AsmToken::RCurly))
  2119. return Error(Tok.getLoc(), "Expected } at this point");
  2120. Parser.Lex(); // Eat "}"
  2121. Operands.push_back(X86Operand::CreateToken("{sae}", consumedToken));
  2122. return false;
  2123. }
  2124. return Error(Tok.getLoc(), "unknown token in expression");
  2125. }
  2126. /// Parse the '.' operator.
  2127. bool X86AsmParser::ParseIntelDotOperator(IntelExprStateMachine &SM,
  2128. SMLoc &End) {
  2129. const AsmToken &Tok = getTok();
  2130. AsmFieldInfo Info;
  2131. // Drop the optional '.'.
  2132. StringRef DotDispStr = Tok.getString();
  2133. if (DotDispStr.startswith("."))
  2134. DotDispStr = DotDispStr.drop_front(1);
  2135. StringRef TrailingDot;
  2136. // .Imm gets lexed as a real.
  2137. if (Tok.is(AsmToken::Real)) {
  2138. APInt DotDisp;
  2139. DotDispStr.getAsInteger(10, DotDisp);
  2140. Info.Offset = DotDisp.getZExtValue();
  2141. } else if ((isParsingMSInlineAsm() || getParser().isParsingMasm()) &&
  2142. Tok.is(AsmToken::Identifier)) {
  2143. if (DotDispStr.endswith(".")) {
  2144. TrailingDot = DotDispStr.substr(DotDispStr.size() - 1);
  2145. DotDispStr = DotDispStr.drop_back(1);
  2146. }
  2147. const std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
  2148. const StringRef Base = BaseMember.first, Member = BaseMember.second;
  2149. if (getParser().lookUpField(SM.getType(), DotDispStr, Info) &&
  2150. getParser().lookUpField(SM.getSymName(), DotDispStr, Info) &&
  2151. getParser().lookUpField(DotDispStr, Info) &&
  2152. (!SemaCallback ||
  2153. SemaCallback->LookupInlineAsmField(Base, Member, Info.Offset)))
  2154. return Error(Tok.getLoc(), "Unable to lookup field reference!");
  2155. } else {
  2156. return Error(Tok.getLoc(), "Unexpected token type!");
  2157. }
  2158. // Eat the DotExpression and update End
  2159. End = SMLoc::getFromPointer(DotDispStr.data());
  2160. const char *DotExprEndLoc = DotDispStr.data() + DotDispStr.size();
  2161. while (Tok.getLoc().getPointer() < DotExprEndLoc)
  2162. Lex();
  2163. if (!TrailingDot.empty())
  2164. getLexer().UnLex(AsmToken(AsmToken::Dot, TrailingDot));
  2165. SM.addImm(Info.Offset);
  2166. SM.setTypeInfo(Info.Type);
  2167. return false;
  2168. }
  2169. /// Parse the 'offset' operator.
  2170. /// This operator is used to specify the location of a given operand
  2171. bool X86AsmParser::ParseIntelOffsetOperator(const MCExpr *&Val, StringRef &ID,
  2172. InlineAsmIdentifierInfo &Info,
  2173. SMLoc &End) {
  2174. // Eat offset, mark start of identifier.
  2175. SMLoc Start = Lex().getLoc();
  2176. ID = getTok().getString();
  2177. if (!isParsingMSInlineAsm()) {
  2178. if ((getTok().isNot(AsmToken::Identifier) &&
  2179. getTok().isNot(AsmToken::String)) ||
  2180. getParser().parsePrimaryExpr(Val, End, nullptr))
  2181. return Error(Start, "unexpected token!");
  2182. } else if (ParseIntelInlineAsmIdentifier(Val, ID, Info, false, End, true)) {
  2183. return Error(Start, "unable to lookup expression");
  2184. } else if (Info.isKind(InlineAsmIdentifierInfo::IK_EnumVal)) {
  2185. return Error(Start, "offset operator cannot yet handle constants");
  2186. }
  2187. return false;
  2188. }
  2189. // Query a candidate string for being an Intel assembly operator
  2190. // Report back its kind, or IOK_INVALID if does not evaluated as a known one
  2191. unsigned X86AsmParser::IdentifyIntelInlineAsmOperator(StringRef Name) {
  2192. return StringSwitch<unsigned>(Name)
  2193. .Cases("TYPE","type",IOK_TYPE)
  2194. .Cases("SIZE","size",IOK_SIZE)
  2195. .Cases("LENGTH","length",IOK_LENGTH)
  2196. .Default(IOK_INVALID);
  2197. }
  2198. /// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
  2199. /// returns the number of elements in an array. It returns the value 1 for
  2200. /// non-array variables. The SIZE operator returns the size of a C or C++
  2201. /// variable. A variable's size is the product of its LENGTH and TYPE. The
  2202. /// TYPE operator returns the size of a C or C++ type or variable. If the
  2203. /// variable is an array, TYPE returns the size of a single element.
  2204. unsigned X86AsmParser::ParseIntelInlineAsmOperator(unsigned OpKind) {
  2205. MCAsmParser &Parser = getParser();
  2206. const AsmToken &Tok = Parser.getTok();
  2207. Parser.Lex(); // Eat operator.
  2208. const MCExpr *Val = nullptr;
  2209. InlineAsmIdentifierInfo Info;
  2210. SMLoc Start = Tok.getLoc(), End;
  2211. StringRef Identifier = Tok.getString();
  2212. if (ParseIntelInlineAsmIdentifier(Val, Identifier, Info,
  2213. /*IsUnevaluatedOperand=*/true, End))
  2214. return 0;
  2215. if (!Info.isKind(InlineAsmIdentifierInfo::IK_Var)) {
  2216. Error(Start, "unable to lookup expression");
  2217. return 0;
  2218. }
  2219. unsigned CVal = 0;
  2220. switch(OpKind) {
  2221. default: llvm_unreachable("Unexpected operand kind!");
  2222. case IOK_LENGTH: CVal = Info.Var.Length; break;
  2223. case IOK_SIZE: CVal = Info.Var.Size; break;
  2224. case IOK_TYPE: CVal = Info.Var.Type; break;
  2225. }
  2226. return CVal;
  2227. }
  2228. // Query a candidate string for being an Intel assembly operator
  2229. // Report back its kind, or IOK_INVALID if does not evaluated as a known one
  2230. unsigned X86AsmParser::IdentifyMasmOperator(StringRef Name) {
  2231. return StringSwitch<unsigned>(Name.lower())
  2232. .Case("type", MOK_TYPE)
  2233. .Cases("size", "sizeof", MOK_SIZEOF)
  2234. .Cases("length", "lengthof", MOK_LENGTHOF)
  2235. .Default(MOK_INVALID);
  2236. }
  2237. /// Parse the 'LENGTHOF', 'SIZEOF', and 'TYPE' operators. The LENGTHOF operator
  2238. /// returns the number of elements in an array. It returns the value 1 for
  2239. /// non-array variables. The SIZEOF operator returns the size of a type or
  2240. /// variable in bytes. A variable's size is the product of its LENGTH and TYPE.
  2241. /// The TYPE operator returns the size of a variable. If the variable is an
  2242. /// array, TYPE returns the size of a single element.
  2243. bool X86AsmParser::ParseMasmOperator(unsigned OpKind, int64_t &Val) {
  2244. MCAsmParser &Parser = getParser();
  2245. SMLoc OpLoc = Parser.getTok().getLoc();
  2246. Parser.Lex(); // Eat operator.
  2247. Val = 0;
  2248. if (OpKind == MOK_SIZEOF || OpKind == MOK_TYPE) {
  2249. // Check for SIZEOF(<type>) and TYPE(<type>).
  2250. bool InParens = Parser.getTok().is(AsmToken::LParen);
  2251. const AsmToken &IDTok = InParens ? getLexer().peekTok() : Parser.getTok();
  2252. AsmTypeInfo Type;
  2253. if (IDTok.is(AsmToken::Identifier) &&
  2254. !Parser.lookUpType(IDTok.getIdentifier(), Type)) {
  2255. Val = Type.Size;
  2256. // Eat tokens.
  2257. if (InParens)
  2258. parseToken(AsmToken::LParen);
  2259. parseToken(AsmToken::Identifier);
  2260. if (InParens)
  2261. parseToken(AsmToken::RParen);
  2262. }
  2263. }
  2264. if (!Val) {
  2265. IntelExprStateMachine SM;
  2266. SMLoc End, Start = Parser.getTok().getLoc();
  2267. if (ParseIntelExpression(SM, End))
  2268. return true;
  2269. switch (OpKind) {
  2270. default:
  2271. llvm_unreachable("Unexpected operand kind!");
  2272. case MOK_SIZEOF:
  2273. Val = SM.getSize();
  2274. break;
  2275. case MOK_LENGTHOF:
  2276. Val = SM.getLength();
  2277. break;
  2278. case MOK_TYPE:
  2279. Val = SM.getElementSize();
  2280. break;
  2281. }
  2282. if (!Val)
  2283. return Error(OpLoc, "expression has unknown type", SMRange(Start, End));
  2284. }
  2285. return false;
  2286. }
  2287. bool X86AsmParser::ParseIntelMemoryOperandSize(unsigned &Size) {
  2288. Size = StringSwitch<unsigned>(getTok().getString())
  2289. .Cases("BYTE", "byte", 8)
  2290. .Cases("WORD", "word", 16)
  2291. .Cases("DWORD", "dword", 32)
  2292. .Cases("FLOAT", "float", 32)
  2293. .Cases("LONG", "long", 32)
  2294. .Cases("FWORD", "fword", 48)
  2295. .Cases("DOUBLE", "double", 64)
  2296. .Cases("QWORD", "qword", 64)
  2297. .Cases("MMWORD","mmword", 64)
  2298. .Cases("XWORD", "xword", 80)
  2299. .Cases("TBYTE", "tbyte", 80)
  2300. .Cases("XMMWORD", "xmmword", 128)
  2301. .Cases("YMMWORD", "ymmword", 256)
  2302. .Cases("ZMMWORD", "zmmword", 512)
  2303. .Default(0);
  2304. if (Size) {
  2305. const AsmToken &Tok = Lex(); // Eat operand size (e.g., byte, word).
  2306. if (!(Tok.getString().equals("PTR") || Tok.getString().equals("ptr")))
  2307. return Error(Tok.getLoc(), "Expected 'PTR' or 'ptr' token!");
  2308. Lex(); // Eat ptr.
  2309. }
  2310. return false;
  2311. }
  2312. bool X86AsmParser::ParseIntelOperand(OperandVector &Operands) {
  2313. MCAsmParser &Parser = getParser();
  2314. const AsmToken &Tok = Parser.getTok();
  2315. SMLoc Start, End;
  2316. // Parse optional Size directive.
  2317. unsigned Size;
  2318. if (ParseIntelMemoryOperandSize(Size))
  2319. return true;
  2320. bool PtrInOperand = bool(Size);
  2321. Start = Tok.getLoc();
  2322. // Rounding mode operand.
  2323. if (getLexer().is(AsmToken::LCurly))
  2324. return ParseRoundingModeOp(Start, Operands);
  2325. // Register operand.
  2326. unsigned RegNo = 0;
  2327. if (Tok.is(AsmToken::Identifier) && !ParseRegister(RegNo, Start, End)) {
  2328. if (RegNo == X86::RIP)
  2329. return Error(Start, "rip can only be used as a base register");
  2330. // A Register followed by ':' is considered a segment override
  2331. if (Tok.isNot(AsmToken::Colon)) {
  2332. if (PtrInOperand)
  2333. return Error(Start, "expected memory operand after 'ptr', "
  2334. "found register operand instead");
  2335. Operands.push_back(X86Operand::CreateReg(RegNo, Start, End));
  2336. return false;
  2337. }
  2338. // An alleged segment override. check if we have a valid segment register
  2339. if (!X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo))
  2340. return Error(Start, "invalid segment register");
  2341. // Eat ':' and update Start location
  2342. Start = Lex().getLoc();
  2343. }
  2344. // Immediates and Memory
  2345. IntelExprStateMachine SM;
  2346. if (ParseIntelExpression(SM, End))
  2347. return true;
  2348. if (isParsingMSInlineAsm())
  2349. RewriteIntelExpression(SM, Start, Tok.getLoc());
  2350. int64_t Imm = SM.getImm();
  2351. const MCExpr *Disp = SM.getSym();
  2352. const MCExpr *ImmDisp = MCConstantExpr::create(Imm, getContext());
  2353. if (Disp && Imm)
  2354. Disp = MCBinaryExpr::createAdd(Disp, ImmDisp, getContext());
  2355. if (!Disp)
  2356. Disp = ImmDisp;
  2357. // RegNo != 0 specifies a valid segment register,
  2358. // and we are parsing a segment override
  2359. if (!SM.isMemExpr() && !RegNo) {
  2360. if (isParsingMSInlineAsm() && SM.isOffsetOperator()) {
  2361. const InlineAsmIdentifierInfo &Info = SM.getIdentifierInfo();
  2362. if (Info.isKind(InlineAsmIdentifierInfo::IK_Var)) {
  2363. // Disp includes the address of a variable; make sure this is recorded
  2364. // for later handling.
  2365. Operands.push_back(X86Operand::CreateImm(Disp, Start, End,
  2366. SM.getSymName(), Info.Var.Decl,
  2367. Info.Var.IsGlobalLV));
  2368. return false;
  2369. }
  2370. }
  2371. Operands.push_back(X86Operand::CreateImm(Disp, Start, End));
  2372. return false;
  2373. }
  2374. StringRef ErrMsg;
  2375. unsigned BaseReg = SM.getBaseReg();
  2376. unsigned IndexReg = SM.getIndexReg();
  2377. unsigned Scale = SM.getScale();
  2378. if (!PtrInOperand)
  2379. Size = SM.getElementSize() << 3;
  2380. if (Scale == 0 && BaseReg != X86::ESP && BaseReg != X86::RSP &&
  2381. (IndexReg == X86::ESP || IndexReg == X86::RSP))
  2382. std::swap(BaseReg, IndexReg);
  2383. // If BaseReg is a vector register and IndexReg is not, swap them unless
  2384. // Scale was specified in which case it would be an error.
  2385. if (Scale == 0 &&
  2386. !(X86MCRegisterClasses[X86::VR128XRegClassID].contains(IndexReg) ||
  2387. X86MCRegisterClasses[X86::VR256XRegClassID].contains(IndexReg) ||
  2388. X86MCRegisterClasses[X86::VR512RegClassID].contains(IndexReg)) &&
  2389. (X86MCRegisterClasses[X86::VR128XRegClassID].contains(BaseReg) ||
  2390. X86MCRegisterClasses[X86::VR256XRegClassID].contains(BaseReg) ||
  2391. X86MCRegisterClasses[X86::VR512RegClassID].contains(BaseReg)))
  2392. std::swap(BaseReg, IndexReg);
  2393. if (Scale != 0 &&
  2394. X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg))
  2395. return Error(Start, "16-bit addresses cannot have a scale");
  2396. // If there was no explicit scale specified, change it to 1.
  2397. if (Scale == 0)
  2398. Scale = 1;
  2399. // If this is a 16-bit addressing mode with the base and index in the wrong
  2400. // order, swap them so CheckBaseRegAndIndexRegAndScale doesn't fail. It is
  2401. // shared with att syntax where order matters.
  2402. if ((BaseReg == X86::SI || BaseReg == X86::DI) &&
  2403. (IndexReg == X86::BX || IndexReg == X86::BP))
  2404. std::swap(BaseReg, IndexReg);
  2405. if ((BaseReg || IndexReg) &&
  2406. CheckBaseRegAndIndexRegAndScale(BaseReg, IndexReg, Scale, is64BitMode(),
  2407. ErrMsg))
  2408. return Error(Start, ErrMsg);
  2409. if (isParsingMSInlineAsm())
  2410. return CreateMemForMSInlineAsm(RegNo, Disp, BaseReg, IndexReg, Scale, Start,
  2411. End, Size, SM.getSymName(),
  2412. SM.getIdentifierInfo(), Operands);
  2413. // When parsing x64 MS-style assembly, all non-absolute references to a named
  2414. // variable default to RIP-relative.
  2415. if (Parser.isParsingMasm() && is64BitMode() && SM.getElementSize() > 0) {
  2416. Operands.push_back(X86Operand::CreateMem(getPointerWidth(), RegNo, Disp,
  2417. BaseReg, IndexReg, Scale, Start,
  2418. End, Size,
  2419. /*DefaultBaseReg=*/X86::RIP));
  2420. return false;
  2421. }
  2422. if ((BaseReg || IndexReg || RegNo))
  2423. Operands.push_back(X86Operand::CreateMem(getPointerWidth(), RegNo, Disp,
  2424. BaseReg, IndexReg, Scale, Start,
  2425. End, Size));
  2426. else
  2427. Operands.push_back(
  2428. X86Operand::CreateMem(getPointerWidth(), Disp, Start, End, Size));
  2429. return false;
  2430. }
  2431. bool X86AsmParser::ParseATTOperand(OperandVector &Operands) {
  2432. MCAsmParser &Parser = getParser();
  2433. switch (getLexer().getKind()) {
  2434. case AsmToken::Dollar: {
  2435. // $42 or $ID -> immediate.
  2436. SMLoc Start = Parser.getTok().getLoc(), End;
  2437. Parser.Lex();
  2438. const MCExpr *Val;
  2439. // This is an immediate, so we should not parse a register. Do a precheck
  2440. // for '%' to supercede intra-register parse errors.
  2441. SMLoc L = Parser.getTok().getLoc();
  2442. if (check(getLexer().is(AsmToken::Percent), L,
  2443. "expected immediate expression") ||
  2444. getParser().parseExpression(Val, End) ||
  2445. check(isa<X86MCExpr>(Val), L, "expected immediate expression"))
  2446. return true;
  2447. Operands.push_back(X86Operand::CreateImm(Val, Start, End));
  2448. return false;
  2449. }
  2450. case AsmToken::LCurly: {
  2451. SMLoc Start = Parser.getTok().getLoc();
  2452. return ParseRoundingModeOp(Start, Operands);
  2453. }
  2454. default: {
  2455. // This a memory operand or a register. We have some parsing complications
  2456. // as a '(' may be part of an immediate expression or the addressing mode
  2457. // block. This is complicated by the fact that an assembler-level variable
  2458. // may refer either to a register or an immediate expression.
  2459. SMLoc Loc = Parser.getTok().getLoc(), EndLoc;
  2460. const MCExpr *Expr = nullptr;
  2461. unsigned Reg = 0;
  2462. if (getLexer().isNot(AsmToken::LParen)) {
  2463. // No '(' so this is either a displacement expression or a register.
  2464. if (Parser.parseExpression(Expr, EndLoc))
  2465. return true;
  2466. if (auto *RE = dyn_cast<X86MCExpr>(Expr)) {
  2467. // Segment Register. Reset Expr and copy value to register.
  2468. Expr = nullptr;
  2469. Reg = RE->getRegNo();
  2470. // Check the register.
  2471. if (Reg == X86::EIZ || Reg == X86::RIZ)
  2472. return Error(
  2473. Loc, "%eiz and %riz can only be used as index registers",
  2474. SMRange(Loc, EndLoc));
  2475. if (Reg == X86::RIP)
  2476. return Error(Loc, "%rip can only be used as a base register",
  2477. SMRange(Loc, EndLoc));
  2478. // Return register that are not segment prefixes immediately.
  2479. if (!Parser.parseOptionalToken(AsmToken::Colon)) {
  2480. Operands.push_back(X86Operand::CreateReg(Reg, Loc, EndLoc));
  2481. return false;
  2482. }
  2483. if (!X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(Reg))
  2484. return Error(Loc, "invalid segment register");
  2485. // Accept a '*' absolute memory reference after the segment. Place it
  2486. // before the full memory operand.
  2487. if (getLexer().is(AsmToken::Star))
  2488. Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
  2489. }
  2490. }
  2491. // This is a Memory operand.
  2492. return ParseMemOperand(Reg, Expr, Loc, EndLoc, Operands);
  2493. }
  2494. }
  2495. }
  2496. // X86::COND_INVALID if not a recognized condition code or alternate mnemonic,
  2497. // otherwise the EFLAGS Condition Code enumerator.
  2498. X86::CondCode X86AsmParser::ParseConditionCode(StringRef CC) {
  2499. return StringSwitch<X86::CondCode>(CC)
  2500. .Case("o", X86::COND_O) // Overflow
  2501. .Case("no", X86::COND_NO) // No Overflow
  2502. .Cases("b", "nae", X86::COND_B) // Below/Neither Above nor Equal
  2503. .Cases("ae", "nb", X86::COND_AE) // Above or Equal/Not Below
  2504. .Cases("e", "z", X86::COND_E) // Equal/Zero
  2505. .Cases("ne", "nz", X86::COND_NE) // Not Equal/Not Zero
  2506. .Cases("be", "na", X86::COND_BE) // Below or Equal/Not Above
  2507. .Cases("a", "nbe", X86::COND_A) // Above/Neither Below nor Equal
  2508. .Case("s", X86::COND_S) // Sign
  2509. .Case("ns", X86::COND_NS) // No Sign
  2510. .Cases("p", "pe", X86::COND_P) // Parity/Parity Even
  2511. .Cases("np", "po", X86::COND_NP) // No Parity/Parity Odd
  2512. .Cases("l", "nge", X86::COND_L) // Less/Neither Greater nor Equal
  2513. .Cases("ge", "nl", X86::COND_GE) // Greater or Equal/Not Less
  2514. .Cases("le", "ng", X86::COND_LE) // Less or Equal/Not Greater
  2515. .Cases("g", "nle", X86::COND_G) // Greater/Neither Less nor Equal
  2516. .Default(X86::COND_INVALID);
  2517. }
  2518. // true on failure, false otherwise
  2519. // If no {z} mark was found - Parser doesn't advance
  2520. bool X86AsmParser::ParseZ(std::unique_ptr<X86Operand> &Z,
  2521. const SMLoc &StartLoc) {
  2522. MCAsmParser &Parser = getParser();
  2523. // Assuming we are just pass the '{' mark, quering the next token
  2524. // Searched for {z}, but none was found. Return false, as no parsing error was
  2525. // encountered
  2526. if (!(getLexer().is(AsmToken::Identifier) &&
  2527. (getLexer().getTok().getIdentifier() == "z")))
  2528. return false;
  2529. Parser.Lex(); // Eat z
  2530. // Query and eat the '}' mark
  2531. if (!getLexer().is(AsmToken::RCurly))
  2532. return Error(getLexer().getLoc(), "Expected } at this point");
  2533. Parser.Lex(); // Eat '}'
  2534. // Assign Z with the {z} mark opernad
  2535. Z = X86Operand::CreateToken("{z}", StartLoc);
  2536. return false;
  2537. }
  2538. // true on failure, false otherwise
  2539. bool X86AsmParser::HandleAVX512Operand(OperandVector &Operands) {
  2540. MCAsmParser &Parser = getParser();
  2541. if (getLexer().is(AsmToken::LCurly)) {
  2542. // Eat "{" and mark the current place.
  2543. const SMLoc consumedToken = consumeToken();
  2544. // Distinguish {1to<NUM>} from {%k<NUM>}.
  2545. if(getLexer().is(AsmToken::Integer)) {
  2546. // Parse memory broadcasting ({1to<NUM>}).
  2547. if (getLexer().getTok().getIntVal() != 1)
  2548. return TokError("Expected 1to<NUM> at this point");
  2549. StringRef Prefix = getLexer().getTok().getString();
  2550. Parser.Lex(); // Eat first token of 1to8
  2551. if (!getLexer().is(AsmToken::Identifier))
  2552. return TokError("Expected 1to<NUM> at this point");
  2553. // Recognize only reasonable suffixes.
  2554. SmallVector<char, 5> BroadcastVector;
  2555. StringRef BroadcastString = (Prefix + getLexer().getTok().getIdentifier())
  2556. .toStringRef(BroadcastVector);
  2557. if (!BroadcastString.startswith("1to"))
  2558. return TokError("Expected 1to<NUM> at this point");
  2559. const char *BroadcastPrimitive =
  2560. StringSwitch<const char *>(BroadcastString)
  2561. .Case("1to2", "{1to2}")
  2562. .Case("1to4", "{1to4}")
  2563. .Case("1to8", "{1to8}")
  2564. .Case("1to16", "{1to16}")
  2565. .Case("1to32", "{1to32}")
  2566. .Default(nullptr);
  2567. if (!BroadcastPrimitive)
  2568. return TokError("Invalid memory broadcast primitive.");
  2569. Parser.Lex(); // Eat trailing token of 1toN
  2570. if (!getLexer().is(AsmToken::RCurly))
  2571. return TokError("Expected } at this point");
  2572. Parser.Lex(); // Eat "}"
  2573. Operands.push_back(X86Operand::CreateToken(BroadcastPrimitive,
  2574. consumedToken));
  2575. // No AVX512 specific primitives can pass
  2576. // after memory broadcasting, so return.
  2577. return false;
  2578. } else {
  2579. // Parse either {k}{z}, {z}{k}, {k} or {z}
  2580. // last one have no meaning, but GCC accepts it
  2581. // Currently, we're just pass a '{' mark
  2582. std::unique_ptr<X86Operand> Z;
  2583. if (ParseZ(Z, consumedToken))
  2584. return true;
  2585. // Reaching here means that parsing of the allegadly '{z}' mark yielded
  2586. // no errors.
  2587. // Query for the need of further parsing for a {%k<NUM>} mark
  2588. if (!Z || getLexer().is(AsmToken::LCurly)) {
  2589. SMLoc StartLoc = Z ? consumeToken() : consumedToken;
  2590. // Parse an op-mask register mark ({%k<NUM>}), which is now to be
  2591. // expected
  2592. unsigned RegNo;
  2593. SMLoc RegLoc;
  2594. if (!ParseRegister(RegNo, RegLoc, StartLoc) &&
  2595. X86MCRegisterClasses[X86::VK1RegClassID].contains(RegNo)) {
  2596. if (RegNo == X86::K0)
  2597. return Error(RegLoc, "Register k0 can't be used as write mask");
  2598. if (!getLexer().is(AsmToken::RCurly))
  2599. return Error(getLexer().getLoc(), "Expected } at this point");
  2600. Operands.push_back(X86Operand::CreateToken("{", StartLoc));
  2601. Operands.push_back(
  2602. X86Operand::CreateReg(RegNo, StartLoc, StartLoc));
  2603. Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
  2604. } else
  2605. return Error(getLexer().getLoc(),
  2606. "Expected an op-mask register at this point");
  2607. // {%k<NUM>} mark is found, inquire for {z}
  2608. if (getLexer().is(AsmToken::LCurly) && !Z) {
  2609. // Have we've found a parsing error, or found no (expected) {z} mark
  2610. // - report an error
  2611. if (ParseZ(Z, consumeToken()) || !Z)
  2612. return Error(getLexer().getLoc(),
  2613. "Expected a {z} mark at this point");
  2614. }
  2615. // '{z}' on its own is meaningless, hence should be ignored.
  2616. // on the contrary - have it been accompanied by a K register,
  2617. // allow it.
  2618. if (Z)
  2619. Operands.push_back(std::move(Z));
  2620. }
  2621. }
  2622. }
  2623. return false;
  2624. }
  2625. /// ParseMemOperand: 'seg : disp(basereg, indexreg, scale)'. The '%ds:' prefix
  2626. /// has already been parsed if present. disp may be provided as well.
  2627. bool X86AsmParser::ParseMemOperand(unsigned SegReg, const MCExpr *Disp,
  2628. SMLoc StartLoc, SMLoc EndLoc,
  2629. OperandVector &Operands) {
  2630. MCAsmParser &Parser = getParser();
  2631. SMLoc Loc;
  2632. // Based on the initial passed values, we may be in any of these cases, we are
  2633. // in one of these cases (with current position (*)):
  2634. // 1. seg : * disp (base-index-scale-expr)
  2635. // 2. seg : *(disp) (base-index-scale-expr)
  2636. // 3. seg : *(base-index-scale-expr)
  2637. // 4. disp *(base-index-scale-expr)
  2638. // 5. *(disp) (base-index-scale-expr)
  2639. // 6. *(base-index-scale-expr)
  2640. // 7. disp *
  2641. // 8. *(disp)
  2642. // If we do not have an displacement yet, check if we're in cases 4 or 6 by
  2643. // checking if the first object after the parenthesis is a register (or an
  2644. // identifier referring to a register) and parse the displacement or default
  2645. // to 0 as appropriate.
  2646. auto isAtMemOperand = [this]() {
  2647. if (this->getLexer().isNot(AsmToken::LParen))
  2648. return false;
  2649. AsmToken Buf[2];
  2650. StringRef Id;
  2651. auto TokCount = this->getLexer().peekTokens(Buf, true);
  2652. if (TokCount == 0)
  2653. return false;
  2654. switch (Buf[0].getKind()) {
  2655. case AsmToken::Percent:
  2656. case AsmToken::Comma:
  2657. return true;
  2658. // These lower cases are doing a peekIdentifier.
  2659. case AsmToken::At:
  2660. case AsmToken::Dollar:
  2661. if ((TokCount > 1) &&
  2662. (Buf[1].is(AsmToken::Identifier) || Buf[1].is(AsmToken::String)) &&
  2663. (Buf[0].getLoc().getPointer() + 1 == Buf[1].getLoc().getPointer()))
  2664. Id = StringRef(Buf[0].getLoc().getPointer(),
  2665. Buf[1].getIdentifier().size() + 1);
  2666. break;
  2667. case AsmToken::Identifier:
  2668. case AsmToken::String:
  2669. Id = Buf[0].getIdentifier();
  2670. break;
  2671. default:
  2672. return false;
  2673. }
  2674. // We have an ID. Check if it is bound to a register.
  2675. if (!Id.empty()) {
  2676. MCSymbol *Sym = this->getContext().getOrCreateSymbol(Id);
  2677. if (Sym->isVariable()) {
  2678. auto V = Sym->getVariableValue(/*SetUsed*/ false);
  2679. return isa<X86MCExpr>(V);
  2680. }
  2681. }
  2682. return false;
  2683. };
  2684. if (!Disp) {
  2685. // Parse immediate if we're not at a mem operand yet.
  2686. if (!isAtMemOperand()) {
  2687. if (Parser.parseTokenLoc(Loc) || Parser.parseExpression(Disp, EndLoc))
  2688. return true;
  2689. assert(!isa<X86MCExpr>(Disp) && "Expected non-register here.");
  2690. } else {
  2691. // Disp is implicitly zero if we haven't parsed it yet.
  2692. Disp = MCConstantExpr::create(0, Parser.getContext());
  2693. }
  2694. }
  2695. // We are now either at the end of the operand or at the '(' at the start of a
  2696. // base-index-scale-expr.
  2697. if (!parseOptionalToken(AsmToken::LParen)) {
  2698. if (SegReg == 0)
  2699. Operands.push_back(
  2700. X86Operand::CreateMem(getPointerWidth(), Disp, StartLoc, EndLoc));
  2701. else
  2702. Operands.push_back(X86Operand::CreateMem(getPointerWidth(), SegReg, Disp,
  2703. 0, 0, 1, StartLoc, EndLoc));
  2704. return false;
  2705. }
  2706. // If we reached here, then eat the '(' and Process
  2707. // the rest of the memory operand.
  2708. unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
  2709. SMLoc BaseLoc = getLexer().getLoc();
  2710. const MCExpr *E;
  2711. StringRef ErrMsg;
  2712. // Parse BaseReg if one is provided.
  2713. if (getLexer().isNot(AsmToken::Comma) && getLexer().isNot(AsmToken::RParen)) {
  2714. if (Parser.parseExpression(E, EndLoc) ||
  2715. check(!isa<X86MCExpr>(E), BaseLoc, "expected register here"))
  2716. return true;
  2717. // Check the register.
  2718. BaseReg = cast<X86MCExpr>(E)->getRegNo();
  2719. if (BaseReg == X86::EIZ || BaseReg == X86::RIZ)
  2720. return Error(BaseLoc, "eiz and riz can only be used as index registers",
  2721. SMRange(BaseLoc, EndLoc));
  2722. }
  2723. if (parseOptionalToken(AsmToken::Comma)) {
  2724. // Following the comma we should have either an index register, or a scale
  2725. // value. We don't support the later form, but we want to parse it
  2726. // correctly.
  2727. //
  2728. // Even though it would be completely consistent to support syntax like
  2729. // "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
  2730. if (getLexer().isNot(AsmToken::RParen)) {
  2731. if (Parser.parseTokenLoc(Loc) || Parser.parseExpression(E, EndLoc))
  2732. return true;
  2733. if (!isa<X86MCExpr>(E)) {
  2734. // We've parsed an unexpected Scale Value instead of an index
  2735. // register. Interpret it as an absolute.
  2736. int64_t ScaleVal;
  2737. if (!E->evaluateAsAbsolute(ScaleVal, getStreamer().getAssemblerPtr()))
  2738. return Error(Loc, "expected absolute expression");
  2739. if (ScaleVal != 1)
  2740. Warning(Loc, "scale factor without index register is ignored");
  2741. Scale = 1;
  2742. } else { // IndexReg Found.
  2743. IndexReg = cast<X86MCExpr>(E)->getRegNo();
  2744. if (BaseReg == X86::RIP)
  2745. return Error(Loc,
  2746. "%rip as base register can not have an index register");
  2747. if (IndexReg == X86::RIP)
  2748. return Error(Loc, "%rip is not allowed as an index register");
  2749. if (parseOptionalToken(AsmToken::Comma)) {
  2750. // Parse the scale amount:
  2751. // ::= ',' [scale-expression]
  2752. // A scale amount without an index is ignored.
  2753. if (getLexer().isNot(AsmToken::RParen)) {
  2754. int64_t ScaleVal;
  2755. if (Parser.parseTokenLoc(Loc) ||
  2756. Parser.parseAbsoluteExpression(ScaleVal))
  2757. return Error(Loc, "expected scale expression");
  2758. Scale = (unsigned)ScaleVal;
  2759. // Validate the scale amount.
  2760. if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
  2761. Scale != 1)
  2762. return Error(Loc, "scale factor in 16-bit address must be 1");
  2763. if (checkScale(Scale, ErrMsg))
  2764. return Error(Loc, ErrMsg);
  2765. }
  2766. }
  2767. }
  2768. }
  2769. }
  2770. // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
  2771. if (parseToken(AsmToken::RParen, "unexpected token in memory operand"))
  2772. return true;
  2773. // This is to support otherwise illegal operand (%dx) found in various
  2774. // unofficial manuals examples (e.g. "out[s]?[bwl]? %al, (%dx)") and must now
  2775. // be supported. Mark such DX variants separately fix only in special cases.
  2776. if (BaseReg == X86::DX && IndexReg == 0 && Scale == 1 && SegReg == 0 &&
  2777. isa<MCConstantExpr>(Disp) &&
  2778. cast<MCConstantExpr>(Disp)->getValue() == 0) {
  2779. Operands.push_back(X86Operand::CreateDXReg(BaseLoc, BaseLoc));
  2780. return false;
  2781. }
  2782. if (CheckBaseRegAndIndexRegAndScale(BaseReg, IndexReg, Scale, is64BitMode(),
  2783. ErrMsg))
  2784. return Error(BaseLoc, ErrMsg);
  2785. if (SegReg || BaseReg || IndexReg)
  2786. Operands.push_back(X86Operand::CreateMem(getPointerWidth(), SegReg, Disp,
  2787. BaseReg, IndexReg, Scale, StartLoc,
  2788. EndLoc));
  2789. else
  2790. Operands.push_back(
  2791. X86Operand::CreateMem(getPointerWidth(), Disp, StartLoc, EndLoc));
  2792. return false;
  2793. }
  2794. // Parse either a standard primary expression or a register.
  2795. bool X86AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
  2796. MCAsmParser &Parser = getParser();
  2797. // See if this is a register first.
  2798. if (getTok().is(AsmToken::Percent) ||
  2799. (isParsingIntelSyntax() && getTok().is(AsmToken::Identifier) &&
  2800. MatchRegisterName(Parser.getTok().getString()))) {
  2801. SMLoc StartLoc = Parser.getTok().getLoc();
  2802. unsigned RegNo;
  2803. if (ParseRegister(RegNo, StartLoc, EndLoc))
  2804. return true;
  2805. Res = X86MCExpr::create(RegNo, Parser.getContext());
  2806. return false;
  2807. }
  2808. return Parser.parsePrimaryExpr(Res, EndLoc, nullptr);
  2809. }
  2810. bool X86AsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
  2811. SMLoc NameLoc, OperandVector &Operands) {
  2812. MCAsmParser &Parser = getParser();
  2813. InstInfo = &Info;
  2814. // Reset the forced VEX encoding.
  2815. ForcedVEXEncoding = VEXEncoding_Default;
  2816. ForcedDispEncoding = DispEncoding_Default;
  2817. // Parse pseudo prefixes.
  2818. while (true) {
  2819. if (Name == "{") {
  2820. if (getLexer().isNot(AsmToken::Identifier))
  2821. return Error(Parser.getTok().getLoc(), "Unexpected token after '{'");
  2822. std::string Prefix = Parser.getTok().getString().lower();
  2823. Parser.Lex(); // Eat identifier.
  2824. if (getLexer().isNot(AsmToken::RCurly))
  2825. return Error(Parser.getTok().getLoc(), "Expected '}'");
  2826. Parser.Lex(); // Eat curly.
  2827. if (Prefix == "vex")
  2828. ForcedVEXEncoding = VEXEncoding_VEX;
  2829. else if (Prefix == "vex2")
  2830. ForcedVEXEncoding = VEXEncoding_VEX2;
  2831. else if (Prefix == "vex3")
  2832. ForcedVEXEncoding = VEXEncoding_VEX3;
  2833. else if (Prefix == "evex")
  2834. ForcedVEXEncoding = VEXEncoding_EVEX;
  2835. else if (Prefix == "disp8")
  2836. ForcedDispEncoding = DispEncoding_Disp8;
  2837. else if (Prefix == "disp32")
  2838. ForcedDispEncoding = DispEncoding_Disp32;
  2839. else
  2840. return Error(NameLoc, "unknown prefix");
  2841. NameLoc = Parser.getTok().getLoc();
  2842. if (getLexer().is(AsmToken::LCurly)) {
  2843. Parser.Lex();
  2844. Name = "{";
  2845. } else {
  2846. if (getLexer().isNot(AsmToken::Identifier))
  2847. return Error(Parser.getTok().getLoc(), "Expected identifier");
  2848. // FIXME: The mnemonic won't match correctly if its not in lower case.
  2849. Name = Parser.getTok().getString();
  2850. Parser.Lex();
  2851. }
  2852. continue;
  2853. }
  2854. // Parse MASM style pseudo prefixes.
  2855. if (isParsingMSInlineAsm()) {
  2856. if (Name.equals_insensitive("vex"))
  2857. ForcedVEXEncoding = VEXEncoding_VEX;
  2858. else if (Name.equals_insensitive("vex2"))
  2859. ForcedVEXEncoding = VEXEncoding_VEX2;
  2860. else if (Name.equals_insensitive("vex3"))
  2861. ForcedVEXEncoding = VEXEncoding_VEX3;
  2862. else if (Name.equals_insensitive("evex"))
  2863. ForcedVEXEncoding = VEXEncoding_EVEX;
  2864. if (ForcedVEXEncoding != VEXEncoding_Default) {
  2865. if (getLexer().isNot(AsmToken::Identifier))
  2866. return Error(Parser.getTok().getLoc(), "Expected identifier");
  2867. // FIXME: The mnemonic won't match correctly if its not in lower case.
  2868. Name = Parser.getTok().getString();
  2869. NameLoc = Parser.getTok().getLoc();
  2870. Parser.Lex();
  2871. }
  2872. }
  2873. break;
  2874. }
  2875. // Support the suffix syntax for overriding displacement size as well.
  2876. if (Name.consume_back(".d32")) {
  2877. ForcedDispEncoding = DispEncoding_Disp32;
  2878. } else if (Name.consume_back(".d8")) {
  2879. ForcedDispEncoding = DispEncoding_Disp8;
  2880. }
  2881. StringRef PatchedName = Name;
  2882. // Hack to skip "short" following Jcc.
  2883. if (isParsingIntelSyntax() &&
  2884. (PatchedName == "jmp" || PatchedName == "jc" || PatchedName == "jnc" ||
  2885. PatchedName == "jcxz" || PatchedName == "jecxz" ||
  2886. (PatchedName.startswith("j") &&
  2887. ParseConditionCode(PatchedName.substr(1)) != X86::COND_INVALID))) {
  2888. StringRef NextTok = Parser.getTok().getString();
  2889. if (Parser.isParsingMasm() ? NextTok.equals_insensitive("short")
  2890. : NextTok == "short") {
  2891. SMLoc NameEndLoc =
  2892. NameLoc.getFromPointer(NameLoc.getPointer() + Name.size());
  2893. // Eat the short keyword.
  2894. Parser.Lex();
  2895. // MS and GAS ignore the short keyword; they both determine the jmp type
  2896. // based on the distance of the label. (NASM does emit different code with
  2897. // and without "short," though.)
  2898. InstInfo->AsmRewrites->emplace_back(AOK_Skip, NameEndLoc,
  2899. NextTok.size() + 1);
  2900. }
  2901. }
  2902. // FIXME: Hack to recognize setneb as setne.
  2903. if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
  2904. PatchedName != "setb" && PatchedName != "setnb")
  2905. PatchedName = PatchedName.substr(0, Name.size()-1);
  2906. unsigned ComparisonPredicate = ~0U;
  2907. // FIXME: Hack to recognize cmp<comparison code>{sh,ss,sd,ph,ps,pd}.
  2908. if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
  2909. (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
  2910. PatchedName.endswith("sh") || PatchedName.endswith("ph") ||
  2911. PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
  2912. bool IsVCMP = PatchedName[0] == 'v';
  2913. unsigned CCIdx = IsVCMP ? 4 : 3;
  2914. unsigned CC = StringSwitch<unsigned>(
  2915. PatchedName.slice(CCIdx, PatchedName.size() - 2))
  2916. .Case("eq", 0x00)
  2917. .Case("eq_oq", 0x00)
  2918. .Case("lt", 0x01)
  2919. .Case("lt_os", 0x01)
  2920. .Case("le", 0x02)
  2921. .Case("le_os", 0x02)
  2922. .Case("unord", 0x03)
  2923. .Case("unord_q", 0x03)
  2924. .Case("neq", 0x04)
  2925. .Case("neq_uq", 0x04)
  2926. .Case("nlt", 0x05)
  2927. .Case("nlt_us", 0x05)
  2928. .Case("nle", 0x06)
  2929. .Case("nle_us", 0x06)
  2930. .Case("ord", 0x07)
  2931. .Case("ord_q", 0x07)
  2932. /* AVX only from here */
  2933. .Case("eq_uq", 0x08)
  2934. .Case("nge", 0x09)
  2935. .Case("nge_us", 0x09)
  2936. .Case("ngt", 0x0A)
  2937. .Case("ngt_us", 0x0A)
  2938. .Case("false", 0x0B)
  2939. .Case("false_oq", 0x0B)
  2940. .Case("neq_oq", 0x0C)
  2941. .Case("ge", 0x0D)
  2942. .Case("ge_os", 0x0D)
  2943. .Case("gt", 0x0E)
  2944. .Case("gt_os", 0x0E)
  2945. .Case("true", 0x0F)
  2946. .Case("true_uq", 0x0F)
  2947. .Case("eq_os", 0x10)
  2948. .Case("lt_oq", 0x11)
  2949. .Case("le_oq", 0x12)
  2950. .Case("unord_s", 0x13)
  2951. .Case("neq_us", 0x14)
  2952. .Case("nlt_uq", 0x15)
  2953. .Case("nle_uq", 0x16)
  2954. .Case("ord_s", 0x17)
  2955. .Case("eq_us", 0x18)
  2956. .Case("nge_uq", 0x19)
  2957. .Case("ngt_uq", 0x1A)
  2958. .Case("false_os", 0x1B)
  2959. .Case("neq_os", 0x1C)
  2960. .Case("ge_oq", 0x1D)
  2961. .Case("gt_oq", 0x1E)
  2962. .Case("true_us", 0x1F)
  2963. .Default(~0U);
  2964. if (CC != ~0U && (IsVCMP || CC < 8) &&
  2965. (IsVCMP || PatchedName.back() != 'h')) {
  2966. if (PatchedName.endswith("ss"))
  2967. PatchedName = IsVCMP ? "vcmpss" : "cmpss";
  2968. else if (PatchedName.endswith("sd"))
  2969. PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
  2970. else if (PatchedName.endswith("ps"))
  2971. PatchedName = IsVCMP ? "vcmpps" : "cmpps";
  2972. else if (PatchedName.endswith("pd"))
  2973. PatchedName = IsVCMP ? "vcmppd" : "cmppd";
  2974. else if (PatchedName.endswith("sh"))
  2975. PatchedName = "vcmpsh";
  2976. else if (PatchedName.endswith("ph"))
  2977. PatchedName = "vcmpph";
  2978. else
  2979. llvm_unreachable("Unexpected suffix!");
  2980. ComparisonPredicate = CC;
  2981. }
  2982. }
  2983. // FIXME: Hack to recognize vpcmp<comparison code>{ub,uw,ud,uq,b,w,d,q}.
  2984. if (PatchedName.startswith("vpcmp") &&
  2985. (PatchedName.back() == 'b' || PatchedName.back() == 'w' ||
  2986. PatchedName.back() == 'd' || PatchedName.back() == 'q')) {
  2987. unsigned SuffixSize = PatchedName.drop_back().back() == 'u' ? 2 : 1;
  2988. unsigned CC = StringSwitch<unsigned>(
  2989. PatchedName.slice(5, PatchedName.size() - SuffixSize))
  2990. .Case("eq", 0x0) // Only allowed on unsigned. Checked below.
  2991. .Case("lt", 0x1)
  2992. .Case("le", 0x2)
  2993. //.Case("false", 0x3) // Not a documented alias.
  2994. .Case("neq", 0x4)
  2995. .Case("nlt", 0x5)
  2996. .Case("nle", 0x6)
  2997. //.Case("true", 0x7) // Not a documented alias.
  2998. .Default(~0U);
  2999. if (CC != ~0U && (CC != 0 || SuffixSize == 2)) {
  3000. switch (PatchedName.back()) {
  3001. default: llvm_unreachable("Unexpected character!");
  3002. case 'b': PatchedName = SuffixSize == 2 ? "vpcmpub" : "vpcmpb"; break;
  3003. case 'w': PatchedName = SuffixSize == 2 ? "vpcmpuw" : "vpcmpw"; break;
  3004. case 'd': PatchedName = SuffixSize == 2 ? "vpcmpud" : "vpcmpd"; break;
  3005. case 'q': PatchedName = SuffixSize == 2 ? "vpcmpuq" : "vpcmpq"; break;
  3006. }
  3007. // Set up the immediate to push into the operands later.
  3008. ComparisonPredicate = CC;
  3009. }
  3010. }
  3011. // FIXME: Hack to recognize vpcom<comparison code>{ub,uw,ud,uq,b,w,d,q}.
  3012. if (PatchedName.startswith("vpcom") &&
  3013. (PatchedName.back() == 'b' || PatchedName.back() == 'w' ||
  3014. PatchedName.back() == 'd' || PatchedName.back() == 'q')) {
  3015. unsigned SuffixSize = PatchedName.drop_back().back() == 'u' ? 2 : 1;
  3016. unsigned CC = StringSwitch<unsigned>(
  3017. PatchedName.slice(5, PatchedName.size() - SuffixSize))
  3018. .Case("lt", 0x0)
  3019. .Case("le", 0x1)
  3020. .Case("gt", 0x2)
  3021. .Case("ge", 0x3)
  3022. .Case("eq", 0x4)
  3023. .Case("neq", 0x5)
  3024. .Case("false", 0x6)
  3025. .Case("true", 0x7)
  3026. .Default(~0U);
  3027. if (CC != ~0U) {
  3028. switch (PatchedName.back()) {
  3029. default: llvm_unreachable("Unexpected character!");
  3030. case 'b': PatchedName = SuffixSize == 2 ? "vpcomub" : "vpcomb"; break;
  3031. case 'w': PatchedName = SuffixSize == 2 ? "vpcomuw" : "vpcomw"; break;
  3032. case 'd': PatchedName = SuffixSize == 2 ? "vpcomud" : "vpcomd"; break;
  3033. case 'q': PatchedName = SuffixSize == 2 ? "vpcomuq" : "vpcomq"; break;
  3034. }
  3035. // Set up the immediate to push into the operands later.
  3036. ComparisonPredicate = CC;
  3037. }
  3038. }
  3039. // Determine whether this is an instruction prefix.
  3040. // FIXME:
  3041. // Enhance prefixes integrity robustness. for example, following forms
  3042. // are currently tolerated:
  3043. // repz repnz <insn> ; GAS errors for the use of two similar prefixes
  3044. // lock addq %rax, %rbx ; Destination operand must be of memory type
  3045. // xacquire <insn> ; xacquire must be accompanied by 'lock'
  3046. bool IsPrefix =
  3047. StringSwitch<bool>(Name)
  3048. .Cases("cs", "ds", "es", "fs", "gs", "ss", true)
  3049. .Cases("rex64", "data32", "data16", "addr32", "addr16", true)
  3050. .Cases("xacquire", "xrelease", true)
  3051. .Cases("acquire", "release", isParsingIntelSyntax())
  3052. .Default(false);
  3053. auto isLockRepeatNtPrefix = [](StringRef N) {
  3054. return StringSwitch<bool>(N)
  3055. .Cases("lock", "rep", "repe", "repz", "repne", "repnz", "notrack", true)
  3056. .Default(false);
  3057. };
  3058. bool CurlyAsEndOfStatement = false;
  3059. unsigned Flags = X86::IP_NO_PREFIX;
  3060. while (isLockRepeatNtPrefix(Name.lower())) {
  3061. unsigned Prefix =
  3062. StringSwitch<unsigned>(Name)
  3063. .Cases("lock", "lock", X86::IP_HAS_LOCK)
  3064. .Cases("rep", "repe", "repz", X86::IP_HAS_REPEAT)
  3065. .Cases("repne", "repnz", X86::IP_HAS_REPEAT_NE)
  3066. .Cases("notrack", "notrack", X86::IP_HAS_NOTRACK)
  3067. .Default(X86::IP_NO_PREFIX); // Invalid prefix (impossible)
  3068. Flags |= Prefix;
  3069. if (getLexer().is(AsmToken::EndOfStatement)) {
  3070. // We don't have real instr with the given prefix
  3071. // let's use the prefix as the instr.
  3072. // TODO: there could be several prefixes one after another
  3073. Flags = X86::IP_NO_PREFIX;
  3074. break;
  3075. }
  3076. // FIXME: The mnemonic won't match correctly if its not in lower case.
  3077. Name = Parser.getTok().getString();
  3078. Parser.Lex(); // eat the prefix
  3079. // Hack: we could have something like "rep # some comment" or
  3080. // "lock; cmpxchg16b $1" or "lock\0A\09incl" or "lock/incl"
  3081. while (Name.startswith(";") || Name.startswith("\n") ||
  3082. Name.startswith("#") || Name.startswith("\t") ||
  3083. Name.startswith("/")) {
  3084. // FIXME: The mnemonic won't match correctly if its not in lower case.
  3085. Name = Parser.getTok().getString();
  3086. Parser.Lex(); // go to next prefix or instr
  3087. }
  3088. }
  3089. if (Flags)
  3090. PatchedName = Name;
  3091. // Hacks to handle 'data16' and 'data32'
  3092. if (PatchedName == "data16" && is16BitMode()) {
  3093. return Error(NameLoc, "redundant data16 prefix");
  3094. }
  3095. if (PatchedName == "data32") {
  3096. if (is32BitMode())
  3097. return Error(NameLoc, "redundant data32 prefix");
  3098. if (is64BitMode())
  3099. return Error(NameLoc, "'data32' is not supported in 64-bit mode");
  3100. // Hack to 'data16' for the table lookup.
  3101. PatchedName = "data16";
  3102. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  3103. StringRef Next = Parser.getTok().getString();
  3104. getLexer().Lex();
  3105. // data32 effectively changes the instruction suffix.
  3106. // TODO Generalize.
  3107. if (Next == "callw")
  3108. Next = "calll";
  3109. if (Next == "ljmpw")
  3110. Next = "ljmpl";
  3111. Name = Next;
  3112. PatchedName = Name;
  3113. ForcedDataPrefix = X86::Mode32Bit;
  3114. IsPrefix = false;
  3115. }
  3116. }
  3117. Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
  3118. // Push the immediate if we extracted one from the mnemonic.
  3119. if (ComparisonPredicate != ~0U && !isParsingIntelSyntax()) {
  3120. const MCExpr *ImmOp = MCConstantExpr::create(ComparisonPredicate,
  3121. getParser().getContext());
  3122. Operands.push_back(X86Operand::CreateImm(ImmOp, NameLoc, NameLoc));
  3123. }
  3124. // This does the actual operand parsing. Don't parse any more if we have a
  3125. // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
  3126. // just want to parse the "lock" as the first instruction and the "incl" as
  3127. // the next one.
  3128. if (getLexer().isNot(AsmToken::EndOfStatement) && !IsPrefix) {
  3129. // Parse '*' modifier.
  3130. if (getLexer().is(AsmToken::Star))
  3131. Operands.push_back(X86Operand::CreateToken("*", consumeToken()));
  3132. // Read the operands.
  3133. while (true) {
  3134. if (ParseOperand(Operands))
  3135. return true;
  3136. if (HandleAVX512Operand(Operands))
  3137. return true;
  3138. // check for comma and eat it
  3139. if (getLexer().is(AsmToken::Comma))
  3140. Parser.Lex();
  3141. else
  3142. break;
  3143. }
  3144. // In MS inline asm curly braces mark the beginning/end of a block,
  3145. // therefore they should be interepreted as end of statement
  3146. CurlyAsEndOfStatement =
  3147. isParsingIntelSyntax() && isParsingMSInlineAsm() &&
  3148. (getLexer().is(AsmToken::LCurly) || getLexer().is(AsmToken::RCurly));
  3149. if (getLexer().isNot(AsmToken::EndOfStatement) && !CurlyAsEndOfStatement)
  3150. return TokError("unexpected token in argument list");
  3151. }
  3152. // Push the immediate if we extracted one from the mnemonic.
  3153. if (ComparisonPredicate != ~0U && isParsingIntelSyntax()) {
  3154. const MCExpr *ImmOp = MCConstantExpr::create(ComparisonPredicate,
  3155. getParser().getContext());
  3156. Operands.push_back(X86Operand::CreateImm(ImmOp, NameLoc, NameLoc));
  3157. }
  3158. // Consume the EndOfStatement or the prefix separator Slash
  3159. if (getLexer().is(AsmToken::EndOfStatement) ||
  3160. (IsPrefix && getLexer().is(AsmToken::Slash)))
  3161. Parser.Lex();
  3162. else if (CurlyAsEndOfStatement)
  3163. // Add an actual EndOfStatement before the curly brace
  3164. Info.AsmRewrites->emplace_back(AOK_EndOfStatement,
  3165. getLexer().getTok().getLoc(), 0);
  3166. // This is for gas compatibility and cannot be done in td.
  3167. // Adding "p" for some floating point with no argument.
  3168. // For example: fsub --> fsubp
  3169. bool IsFp =
  3170. Name == "fsub" || Name == "fdiv" || Name == "fsubr" || Name == "fdivr";
  3171. if (IsFp && Operands.size() == 1) {
  3172. const char *Repl = StringSwitch<const char *>(Name)
  3173. .Case("fsub", "fsubp")
  3174. .Case("fdiv", "fdivp")
  3175. .Case("fsubr", "fsubrp")
  3176. .Case("fdivr", "fdivrp");
  3177. static_cast<X86Operand &>(*Operands[0]).setTokenValue(Repl);
  3178. }
  3179. if ((Name == "mov" || Name == "movw" || Name == "movl") &&
  3180. (Operands.size() == 3)) {
  3181. X86Operand &Op1 = (X86Operand &)*Operands[1];
  3182. X86Operand &Op2 = (X86Operand &)*Operands[2];
  3183. SMLoc Loc = Op1.getEndLoc();
  3184. // Moving a 32 or 16 bit value into a segment register has the same
  3185. // behavior. Modify such instructions to always take shorter form.
  3186. if (Op1.isReg() && Op2.isReg() &&
  3187. X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(
  3188. Op2.getReg()) &&
  3189. (X86MCRegisterClasses[X86::GR16RegClassID].contains(Op1.getReg()) ||
  3190. X86MCRegisterClasses[X86::GR32RegClassID].contains(Op1.getReg()))) {
  3191. // Change instruction name to match new instruction.
  3192. if (Name != "mov" && Name[3] == (is16BitMode() ? 'l' : 'w')) {
  3193. Name = is16BitMode() ? "movw" : "movl";
  3194. Operands[0] = X86Operand::CreateToken(Name, NameLoc);
  3195. }
  3196. // Select the correct equivalent 16-/32-bit source register.
  3197. unsigned Reg =
  3198. getX86SubSuperRegisterOrZero(Op1.getReg(), is16BitMode() ? 16 : 32);
  3199. Operands[1] = X86Operand::CreateReg(Reg, Loc, Loc);
  3200. }
  3201. }
  3202. // This is a terrible hack to handle "out[s]?[bwl]? %al, (%dx)" ->
  3203. // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
  3204. // documented form in various unofficial manuals, so a lot of code uses it.
  3205. if ((Name == "outb" || Name == "outsb" || Name == "outw" || Name == "outsw" ||
  3206. Name == "outl" || Name == "outsl" || Name == "out" || Name == "outs") &&
  3207. Operands.size() == 3) {
  3208. X86Operand &Op = (X86Operand &)*Operands.back();
  3209. if (Op.isDXReg())
  3210. Operands.back() = X86Operand::CreateReg(X86::DX, Op.getStartLoc(),
  3211. Op.getEndLoc());
  3212. }
  3213. // Same hack for "in[s]?[bwl]? (%dx), %al" -> "inb %dx, %al".
  3214. if ((Name == "inb" || Name == "insb" || Name == "inw" || Name == "insw" ||
  3215. Name == "inl" || Name == "insl" || Name == "in" || Name == "ins") &&
  3216. Operands.size() == 3) {
  3217. X86Operand &Op = (X86Operand &)*Operands[1];
  3218. if (Op.isDXReg())
  3219. Operands[1] = X86Operand::CreateReg(X86::DX, Op.getStartLoc(),
  3220. Op.getEndLoc());
  3221. }
  3222. SmallVector<std::unique_ptr<MCParsedAsmOperand>, 2> TmpOperands;
  3223. bool HadVerifyError = false;
  3224. // Append default arguments to "ins[bwld]"
  3225. if (Name.startswith("ins") &&
  3226. (Operands.size() == 1 || Operands.size() == 3) &&
  3227. (Name == "insb" || Name == "insw" || Name == "insl" || Name == "insd" ||
  3228. Name == "ins")) {
  3229. AddDefaultSrcDestOperands(TmpOperands,
  3230. X86Operand::CreateReg(X86::DX, NameLoc, NameLoc),
  3231. DefaultMemDIOperand(NameLoc));
  3232. HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
  3233. }
  3234. // Append default arguments to "outs[bwld]"
  3235. if (Name.startswith("outs") &&
  3236. (Operands.size() == 1 || Operands.size() == 3) &&
  3237. (Name == "outsb" || Name == "outsw" || Name == "outsl" ||
  3238. Name == "outsd" || Name == "outs")) {
  3239. AddDefaultSrcDestOperands(TmpOperands, DefaultMemSIOperand(NameLoc),
  3240. X86Operand::CreateReg(X86::DX, NameLoc, NameLoc));
  3241. HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
  3242. }
  3243. // Transform "lods[bwlq]" into "lods[bwlq] ($SIREG)" for appropriate
  3244. // values of $SIREG according to the mode. It would be nice if this
  3245. // could be achieved with InstAlias in the tables.
  3246. if (Name.startswith("lods") &&
  3247. (Operands.size() == 1 || Operands.size() == 2) &&
  3248. (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
  3249. Name == "lodsl" || Name == "lodsd" || Name == "lodsq")) {
  3250. TmpOperands.push_back(DefaultMemSIOperand(NameLoc));
  3251. HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
  3252. }
  3253. // Transform "stos[bwlq]" into "stos[bwlq] ($DIREG)" for appropriate
  3254. // values of $DIREG according to the mode. It would be nice if this
  3255. // could be achieved with InstAlias in the tables.
  3256. if (Name.startswith("stos") &&
  3257. (Operands.size() == 1 || Operands.size() == 2) &&
  3258. (Name == "stos" || Name == "stosb" || Name == "stosw" ||
  3259. Name == "stosl" || Name == "stosd" || Name == "stosq")) {
  3260. TmpOperands.push_back(DefaultMemDIOperand(NameLoc));
  3261. HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
  3262. }
  3263. // Transform "scas[bwlq]" into "scas[bwlq] ($DIREG)" for appropriate
  3264. // values of $DIREG according to the mode. It would be nice if this
  3265. // could be achieved with InstAlias in the tables.
  3266. if (Name.startswith("scas") &&
  3267. (Operands.size() == 1 || Operands.size() == 2) &&
  3268. (Name == "scas" || Name == "scasb" || Name == "scasw" ||
  3269. Name == "scasl" || Name == "scasd" || Name == "scasq")) {
  3270. TmpOperands.push_back(DefaultMemDIOperand(NameLoc));
  3271. HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
  3272. }
  3273. // Add default SI and DI operands to "cmps[bwlq]".
  3274. if (Name.startswith("cmps") &&
  3275. (Operands.size() == 1 || Operands.size() == 3) &&
  3276. (Name == "cmps" || Name == "cmpsb" || Name == "cmpsw" ||
  3277. Name == "cmpsl" || Name == "cmpsd" || Name == "cmpsq")) {
  3278. AddDefaultSrcDestOperands(TmpOperands, DefaultMemDIOperand(NameLoc),
  3279. DefaultMemSIOperand(NameLoc));
  3280. HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
  3281. }
  3282. // Add default SI and DI operands to "movs[bwlq]".
  3283. if (((Name.startswith("movs") &&
  3284. (Name == "movs" || Name == "movsb" || Name == "movsw" ||
  3285. Name == "movsl" || Name == "movsd" || Name == "movsq")) ||
  3286. (Name.startswith("smov") &&
  3287. (Name == "smov" || Name == "smovb" || Name == "smovw" ||
  3288. Name == "smovl" || Name == "smovd" || Name == "smovq"))) &&
  3289. (Operands.size() == 1 || Operands.size() == 3)) {
  3290. if (Name == "movsd" && Operands.size() == 1 && !isParsingIntelSyntax())
  3291. Operands.back() = X86Operand::CreateToken("movsl", NameLoc);
  3292. AddDefaultSrcDestOperands(TmpOperands, DefaultMemSIOperand(NameLoc),
  3293. DefaultMemDIOperand(NameLoc));
  3294. HadVerifyError = VerifyAndAdjustOperands(Operands, TmpOperands);
  3295. }
  3296. // Check if we encountered an error for one the string insturctions
  3297. if (HadVerifyError) {
  3298. return HadVerifyError;
  3299. }
  3300. // Transforms "xlat mem8" into "xlatb"
  3301. if ((Name == "xlat" || Name == "xlatb") && Operands.size() == 2) {
  3302. X86Operand &Op1 = static_cast<X86Operand &>(*Operands[1]);
  3303. if (Op1.isMem8()) {
  3304. Warning(Op1.getStartLoc(), "memory operand is only for determining the "
  3305. "size, (R|E)BX will be used for the location");
  3306. Operands.pop_back();
  3307. static_cast<X86Operand &>(*Operands[0]).setTokenValue("xlatb");
  3308. }
  3309. }
  3310. if (Flags)
  3311. Operands.push_back(X86Operand::CreatePrefix(Flags, NameLoc, NameLoc));
  3312. return false;
  3313. }
  3314. bool X86AsmParser::processInstruction(MCInst &Inst, const OperandVector &Ops) {
  3315. const MCRegisterInfo *MRI = getContext().getRegisterInfo();
  3316. switch (Inst.getOpcode()) {
  3317. default: return false;
  3318. case X86::JMP_1:
  3319. // {disp32} forces a larger displacement as if the instruction was relaxed.
  3320. // NOTE: 16-bit mode uses 16-bit displacement even though it says {disp32}.
  3321. // This matches GNU assembler.
  3322. if (ForcedDispEncoding == DispEncoding_Disp32) {
  3323. Inst.setOpcode(is16BitMode() ? X86::JMP_2 : X86::JMP_4);
  3324. return true;
  3325. }
  3326. return false;
  3327. case X86::JCC_1:
  3328. // {disp32} forces a larger displacement as if the instruction was relaxed.
  3329. // NOTE: 16-bit mode uses 16-bit displacement even though it says {disp32}.
  3330. // This matches GNU assembler.
  3331. if (ForcedDispEncoding == DispEncoding_Disp32) {
  3332. Inst.setOpcode(is16BitMode() ? X86::JCC_2 : X86::JCC_4);
  3333. return true;
  3334. }
  3335. return false;
  3336. case X86::VMOVZPQILo2PQIrr:
  3337. case X86::VMOVAPDrr:
  3338. case X86::VMOVAPDYrr:
  3339. case X86::VMOVAPSrr:
  3340. case X86::VMOVAPSYrr:
  3341. case X86::VMOVDQArr:
  3342. case X86::VMOVDQAYrr:
  3343. case X86::VMOVDQUrr:
  3344. case X86::VMOVDQUYrr:
  3345. case X86::VMOVUPDrr:
  3346. case X86::VMOVUPDYrr:
  3347. case X86::VMOVUPSrr:
  3348. case X86::VMOVUPSYrr: {
  3349. // We can get a smaller encoding by using VEX.R instead of VEX.B if one of
  3350. // the registers is extended, but other isn't.
  3351. if (ForcedVEXEncoding == VEXEncoding_VEX3 ||
  3352. MRI->getEncodingValue(Inst.getOperand(0).getReg()) >= 8 ||
  3353. MRI->getEncodingValue(Inst.getOperand(1).getReg()) < 8)
  3354. return false;
  3355. unsigned NewOpc;
  3356. switch (Inst.getOpcode()) {
  3357. default: llvm_unreachable("Invalid opcode");
  3358. case X86::VMOVZPQILo2PQIrr: NewOpc = X86::VMOVPQI2QIrr; break;
  3359. case X86::VMOVAPDrr: NewOpc = X86::VMOVAPDrr_REV; break;
  3360. case X86::VMOVAPDYrr: NewOpc = X86::VMOVAPDYrr_REV; break;
  3361. case X86::VMOVAPSrr: NewOpc = X86::VMOVAPSrr_REV; break;
  3362. case X86::VMOVAPSYrr: NewOpc = X86::VMOVAPSYrr_REV; break;
  3363. case X86::VMOVDQArr: NewOpc = X86::VMOVDQArr_REV; break;
  3364. case X86::VMOVDQAYrr: NewOpc = X86::VMOVDQAYrr_REV; break;
  3365. case X86::VMOVDQUrr: NewOpc = X86::VMOVDQUrr_REV; break;
  3366. case X86::VMOVDQUYrr: NewOpc = X86::VMOVDQUYrr_REV; break;
  3367. case X86::VMOVUPDrr: NewOpc = X86::VMOVUPDrr_REV; break;
  3368. case X86::VMOVUPDYrr: NewOpc = X86::VMOVUPDYrr_REV; break;
  3369. case X86::VMOVUPSrr: NewOpc = X86::VMOVUPSrr_REV; break;
  3370. case X86::VMOVUPSYrr: NewOpc = X86::VMOVUPSYrr_REV; break;
  3371. }
  3372. Inst.setOpcode(NewOpc);
  3373. return true;
  3374. }
  3375. case X86::VMOVSDrr:
  3376. case X86::VMOVSSrr: {
  3377. // We can get a smaller encoding by using VEX.R instead of VEX.B if one of
  3378. // the registers is extended, but other isn't.
  3379. if (ForcedVEXEncoding == VEXEncoding_VEX3 ||
  3380. MRI->getEncodingValue(Inst.getOperand(0).getReg()) >= 8 ||
  3381. MRI->getEncodingValue(Inst.getOperand(2).getReg()) < 8)
  3382. return false;
  3383. unsigned NewOpc;
  3384. switch (Inst.getOpcode()) {
  3385. default: llvm_unreachable("Invalid opcode");
  3386. case X86::VMOVSDrr: NewOpc = X86::VMOVSDrr_REV; break;
  3387. case X86::VMOVSSrr: NewOpc = X86::VMOVSSrr_REV; break;
  3388. }
  3389. Inst.setOpcode(NewOpc);
  3390. return true;
  3391. }
  3392. case X86::RCR8ri: case X86::RCR16ri: case X86::RCR32ri: case X86::RCR64ri:
  3393. case X86::RCL8ri: case X86::RCL16ri: case X86::RCL32ri: case X86::RCL64ri:
  3394. case X86::ROR8ri: case X86::ROR16ri: case X86::ROR32ri: case X86::ROR64ri:
  3395. case X86::ROL8ri: case X86::ROL16ri: case X86::ROL32ri: case X86::ROL64ri:
  3396. case X86::SAR8ri: case X86::SAR16ri: case X86::SAR32ri: case X86::SAR64ri:
  3397. case X86::SHR8ri: case X86::SHR16ri: case X86::SHR32ri: case X86::SHR64ri:
  3398. case X86::SHL8ri: case X86::SHL16ri: case X86::SHL32ri: case X86::SHL64ri: {
  3399. // Optimize s{hr,ar,hl} $1, <op> to "shift <op>". Similar for rotate.
  3400. // FIXME: It would be great if we could just do this with an InstAlias.
  3401. if (!Inst.getOperand(2).isImm() || Inst.getOperand(2).getImm() != 1)
  3402. return false;
  3403. unsigned NewOpc;
  3404. switch (Inst.getOpcode()) {
  3405. default: llvm_unreachable("Invalid opcode");
  3406. case X86::RCR8ri: NewOpc = X86::RCR8r1; break;
  3407. case X86::RCR16ri: NewOpc = X86::RCR16r1; break;
  3408. case X86::RCR32ri: NewOpc = X86::RCR32r1; break;
  3409. case X86::RCR64ri: NewOpc = X86::RCR64r1; break;
  3410. case X86::RCL8ri: NewOpc = X86::RCL8r1; break;
  3411. case X86::RCL16ri: NewOpc = X86::RCL16r1; break;
  3412. case X86::RCL32ri: NewOpc = X86::RCL32r1; break;
  3413. case X86::RCL64ri: NewOpc = X86::RCL64r1; break;
  3414. case X86::ROR8ri: NewOpc = X86::ROR8r1; break;
  3415. case X86::ROR16ri: NewOpc = X86::ROR16r1; break;
  3416. case X86::ROR32ri: NewOpc = X86::ROR32r1; break;
  3417. case X86::ROR64ri: NewOpc = X86::ROR64r1; break;
  3418. case X86::ROL8ri: NewOpc = X86::ROL8r1; break;
  3419. case X86::ROL16ri: NewOpc = X86::ROL16r1; break;
  3420. case X86::ROL32ri: NewOpc = X86::ROL32r1; break;
  3421. case X86::ROL64ri: NewOpc = X86::ROL64r1; break;
  3422. case X86::SAR8ri: NewOpc = X86::SAR8r1; break;
  3423. case X86::SAR16ri: NewOpc = X86::SAR16r1; break;
  3424. case X86::SAR32ri: NewOpc = X86::SAR32r1; break;
  3425. case X86::SAR64ri: NewOpc = X86::SAR64r1; break;
  3426. case X86::SHR8ri: NewOpc = X86::SHR8r1; break;
  3427. case X86::SHR16ri: NewOpc = X86::SHR16r1; break;
  3428. case X86::SHR32ri: NewOpc = X86::SHR32r1; break;
  3429. case X86::SHR64ri: NewOpc = X86::SHR64r1; break;
  3430. case X86::SHL8ri: NewOpc = X86::SHL8r1; break;
  3431. case X86::SHL16ri: NewOpc = X86::SHL16r1; break;
  3432. case X86::SHL32ri: NewOpc = X86::SHL32r1; break;
  3433. case X86::SHL64ri: NewOpc = X86::SHL64r1; break;
  3434. }
  3435. MCInst TmpInst;
  3436. TmpInst.setOpcode(NewOpc);
  3437. TmpInst.addOperand(Inst.getOperand(0));
  3438. TmpInst.addOperand(Inst.getOperand(1));
  3439. Inst = TmpInst;
  3440. return true;
  3441. }
  3442. case X86::RCR8mi: case X86::RCR16mi: case X86::RCR32mi: case X86::RCR64mi:
  3443. case X86::RCL8mi: case X86::RCL16mi: case X86::RCL32mi: case X86::RCL64mi:
  3444. case X86::ROR8mi: case X86::ROR16mi: case X86::ROR32mi: case X86::ROR64mi:
  3445. case X86::ROL8mi: case X86::ROL16mi: case X86::ROL32mi: case X86::ROL64mi:
  3446. case X86::SAR8mi: case X86::SAR16mi: case X86::SAR32mi: case X86::SAR64mi:
  3447. case X86::SHR8mi: case X86::SHR16mi: case X86::SHR32mi: case X86::SHR64mi:
  3448. case X86::SHL8mi: case X86::SHL16mi: case X86::SHL32mi: case X86::SHL64mi: {
  3449. // Optimize s{hr,ar,hl} $1, <op> to "shift <op>". Similar for rotate.
  3450. // FIXME: It would be great if we could just do this with an InstAlias.
  3451. if (!Inst.getOperand(X86::AddrNumOperands).isImm() ||
  3452. Inst.getOperand(X86::AddrNumOperands).getImm() != 1)
  3453. return false;
  3454. unsigned NewOpc;
  3455. switch (Inst.getOpcode()) {
  3456. default: llvm_unreachable("Invalid opcode");
  3457. case X86::RCR8mi: NewOpc = X86::RCR8m1; break;
  3458. case X86::RCR16mi: NewOpc = X86::RCR16m1; break;
  3459. case X86::RCR32mi: NewOpc = X86::RCR32m1; break;
  3460. case X86::RCR64mi: NewOpc = X86::RCR64m1; break;
  3461. case X86::RCL8mi: NewOpc = X86::RCL8m1; break;
  3462. case X86::RCL16mi: NewOpc = X86::RCL16m1; break;
  3463. case X86::RCL32mi: NewOpc = X86::RCL32m1; break;
  3464. case X86::RCL64mi: NewOpc = X86::RCL64m1; break;
  3465. case X86::ROR8mi: NewOpc = X86::ROR8m1; break;
  3466. case X86::ROR16mi: NewOpc = X86::ROR16m1; break;
  3467. case X86::ROR32mi: NewOpc = X86::ROR32m1; break;
  3468. case X86::ROR64mi: NewOpc = X86::ROR64m1; break;
  3469. case X86::ROL8mi: NewOpc = X86::ROL8m1; break;
  3470. case X86::ROL16mi: NewOpc = X86::ROL16m1; break;
  3471. case X86::ROL32mi: NewOpc = X86::ROL32m1; break;
  3472. case X86::ROL64mi: NewOpc = X86::ROL64m1; break;
  3473. case X86::SAR8mi: NewOpc = X86::SAR8m1; break;
  3474. case X86::SAR16mi: NewOpc = X86::SAR16m1; break;
  3475. case X86::SAR32mi: NewOpc = X86::SAR32m1; break;
  3476. case X86::SAR64mi: NewOpc = X86::SAR64m1; break;
  3477. case X86::SHR8mi: NewOpc = X86::SHR8m1; break;
  3478. case X86::SHR16mi: NewOpc = X86::SHR16m1; break;
  3479. case X86::SHR32mi: NewOpc = X86::SHR32m1; break;
  3480. case X86::SHR64mi: NewOpc = X86::SHR64m1; break;
  3481. case X86::SHL8mi: NewOpc = X86::SHL8m1; break;
  3482. case X86::SHL16mi: NewOpc = X86::SHL16m1; break;
  3483. case X86::SHL32mi: NewOpc = X86::SHL32m1; break;
  3484. case X86::SHL64mi: NewOpc = X86::SHL64m1; break;
  3485. }
  3486. MCInst TmpInst;
  3487. TmpInst.setOpcode(NewOpc);
  3488. for (int i = 0; i != X86::AddrNumOperands; ++i)
  3489. TmpInst.addOperand(Inst.getOperand(i));
  3490. Inst = TmpInst;
  3491. return true;
  3492. }
  3493. case X86::INT: {
  3494. // Transforms "int $3" into "int3" as a size optimization. We can't write an
  3495. // instalias with an immediate operand yet.
  3496. if (!Inst.getOperand(0).isImm() || Inst.getOperand(0).getImm() != 3)
  3497. return false;
  3498. MCInst TmpInst;
  3499. TmpInst.setOpcode(X86::INT3);
  3500. Inst = TmpInst;
  3501. return true;
  3502. }
  3503. }
  3504. }
  3505. bool X86AsmParser::validateInstruction(MCInst &Inst, const OperandVector &Ops) {
  3506. const MCRegisterInfo *MRI = getContext().getRegisterInfo();
  3507. switch (Inst.getOpcode()) {
  3508. case X86::VGATHERDPDYrm:
  3509. case X86::VGATHERDPDrm:
  3510. case X86::VGATHERDPSYrm:
  3511. case X86::VGATHERDPSrm:
  3512. case X86::VGATHERQPDYrm:
  3513. case X86::VGATHERQPDrm:
  3514. case X86::VGATHERQPSYrm:
  3515. case X86::VGATHERQPSrm:
  3516. case X86::VPGATHERDDYrm:
  3517. case X86::VPGATHERDDrm:
  3518. case X86::VPGATHERDQYrm:
  3519. case X86::VPGATHERDQrm:
  3520. case X86::VPGATHERQDYrm:
  3521. case X86::VPGATHERQDrm:
  3522. case X86::VPGATHERQQYrm:
  3523. case X86::VPGATHERQQrm: {
  3524. unsigned Dest = MRI->getEncodingValue(Inst.getOperand(0).getReg());
  3525. unsigned Mask = MRI->getEncodingValue(Inst.getOperand(1).getReg());
  3526. unsigned Index =
  3527. MRI->getEncodingValue(Inst.getOperand(3 + X86::AddrIndexReg).getReg());
  3528. if (Dest == Mask || Dest == Index || Mask == Index)
  3529. return Warning(Ops[0]->getStartLoc(), "mask, index, and destination "
  3530. "registers should be distinct");
  3531. break;
  3532. }
  3533. case X86::VGATHERDPDZ128rm:
  3534. case X86::VGATHERDPDZ256rm:
  3535. case X86::VGATHERDPDZrm:
  3536. case X86::VGATHERDPSZ128rm:
  3537. case X86::VGATHERDPSZ256rm:
  3538. case X86::VGATHERDPSZrm:
  3539. case X86::VGATHERQPDZ128rm:
  3540. case X86::VGATHERQPDZ256rm:
  3541. case X86::VGATHERQPDZrm:
  3542. case X86::VGATHERQPSZ128rm:
  3543. case X86::VGATHERQPSZ256rm:
  3544. case X86::VGATHERQPSZrm:
  3545. case X86::VPGATHERDDZ128rm:
  3546. case X86::VPGATHERDDZ256rm:
  3547. case X86::VPGATHERDDZrm:
  3548. case X86::VPGATHERDQZ128rm:
  3549. case X86::VPGATHERDQZ256rm:
  3550. case X86::VPGATHERDQZrm:
  3551. case X86::VPGATHERQDZ128rm:
  3552. case X86::VPGATHERQDZ256rm:
  3553. case X86::VPGATHERQDZrm:
  3554. case X86::VPGATHERQQZ128rm:
  3555. case X86::VPGATHERQQZ256rm:
  3556. case X86::VPGATHERQQZrm: {
  3557. unsigned Dest = MRI->getEncodingValue(Inst.getOperand(0).getReg());
  3558. unsigned Index =
  3559. MRI->getEncodingValue(Inst.getOperand(4 + X86::AddrIndexReg).getReg());
  3560. if (Dest == Index)
  3561. return Warning(Ops[0]->getStartLoc(), "index and destination registers "
  3562. "should be distinct");
  3563. break;
  3564. }
  3565. case X86::V4FMADDPSrm:
  3566. case X86::V4FMADDPSrmk:
  3567. case X86::V4FMADDPSrmkz:
  3568. case X86::V4FMADDSSrm:
  3569. case X86::V4FMADDSSrmk:
  3570. case X86::V4FMADDSSrmkz:
  3571. case X86::V4FNMADDPSrm:
  3572. case X86::V4FNMADDPSrmk:
  3573. case X86::V4FNMADDPSrmkz:
  3574. case X86::V4FNMADDSSrm:
  3575. case X86::V4FNMADDSSrmk:
  3576. case X86::V4FNMADDSSrmkz:
  3577. case X86::VP4DPWSSDSrm:
  3578. case X86::VP4DPWSSDSrmk:
  3579. case X86::VP4DPWSSDSrmkz:
  3580. case X86::VP4DPWSSDrm:
  3581. case X86::VP4DPWSSDrmk:
  3582. case X86::VP4DPWSSDrmkz: {
  3583. unsigned Src2 = Inst.getOperand(Inst.getNumOperands() -
  3584. X86::AddrNumOperands - 1).getReg();
  3585. unsigned Src2Enc = MRI->getEncodingValue(Src2);
  3586. if (Src2Enc % 4 != 0) {
  3587. StringRef RegName = X86IntelInstPrinter::getRegisterName(Src2);
  3588. unsigned GroupStart = (Src2Enc / 4) * 4;
  3589. unsigned GroupEnd = GroupStart + 3;
  3590. return Warning(Ops[0]->getStartLoc(),
  3591. "source register '" + RegName + "' implicitly denotes '" +
  3592. RegName.take_front(3) + Twine(GroupStart) + "' to '" +
  3593. RegName.take_front(3) + Twine(GroupEnd) +
  3594. "' source group");
  3595. }
  3596. break;
  3597. }
  3598. case X86::VFCMADDCPHZ128m:
  3599. case X86::VFCMADDCPHZ256m:
  3600. case X86::VFCMADDCPHZm:
  3601. case X86::VFCMADDCPHZ128mb:
  3602. case X86::VFCMADDCPHZ256mb:
  3603. case X86::VFCMADDCPHZmb:
  3604. case X86::VFCMADDCPHZ128mbk:
  3605. case X86::VFCMADDCPHZ256mbk:
  3606. case X86::VFCMADDCPHZmbk:
  3607. case X86::VFCMADDCPHZ128mbkz:
  3608. case X86::VFCMADDCPHZ256mbkz:
  3609. case X86::VFCMADDCPHZmbkz:
  3610. case X86::VFCMADDCPHZ128mk:
  3611. case X86::VFCMADDCPHZ256mk:
  3612. case X86::VFCMADDCPHZmk:
  3613. case X86::VFCMADDCPHZ128mkz:
  3614. case X86::VFCMADDCPHZ256mkz:
  3615. case X86::VFCMADDCPHZmkz:
  3616. case X86::VFCMADDCPHZ128r:
  3617. case X86::VFCMADDCPHZ256r:
  3618. case X86::VFCMADDCPHZr:
  3619. case X86::VFCMADDCPHZ128rk:
  3620. case X86::VFCMADDCPHZ256rk:
  3621. case X86::VFCMADDCPHZrk:
  3622. case X86::VFCMADDCPHZ128rkz:
  3623. case X86::VFCMADDCPHZ256rkz:
  3624. case X86::VFCMADDCPHZrkz:
  3625. case X86::VFCMADDCPHZrb:
  3626. case X86::VFCMADDCPHZrbk:
  3627. case X86::VFCMADDCPHZrbkz:
  3628. case X86::VFCMADDCSHZm:
  3629. case X86::VFCMADDCSHZmk:
  3630. case X86::VFCMADDCSHZmkz:
  3631. case X86::VFCMADDCSHZr:
  3632. case X86::VFCMADDCSHZrb:
  3633. case X86::VFCMADDCSHZrbk:
  3634. case X86::VFCMADDCSHZrbkz:
  3635. case X86::VFCMADDCSHZrk:
  3636. case X86::VFCMADDCSHZrkz:
  3637. case X86::VFMADDCPHZ128m:
  3638. case X86::VFMADDCPHZ256m:
  3639. case X86::VFMADDCPHZm:
  3640. case X86::VFMADDCPHZ128mb:
  3641. case X86::VFMADDCPHZ256mb:
  3642. case X86::VFMADDCPHZmb:
  3643. case X86::VFMADDCPHZ128mbk:
  3644. case X86::VFMADDCPHZ256mbk:
  3645. case X86::VFMADDCPHZmbk:
  3646. case X86::VFMADDCPHZ128mbkz:
  3647. case X86::VFMADDCPHZ256mbkz:
  3648. case X86::VFMADDCPHZmbkz:
  3649. case X86::VFMADDCPHZ128mk:
  3650. case X86::VFMADDCPHZ256mk:
  3651. case X86::VFMADDCPHZmk:
  3652. case X86::VFMADDCPHZ128mkz:
  3653. case X86::VFMADDCPHZ256mkz:
  3654. case X86::VFMADDCPHZmkz:
  3655. case X86::VFMADDCPHZ128r:
  3656. case X86::VFMADDCPHZ256r:
  3657. case X86::VFMADDCPHZr:
  3658. case X86::VFMADDCPHZ128rk:
  3659. case X86::VFMADDCPHZ256rk:
  3660. case X86::VFMADDCPHZrk:
  3661. case X86::VFMADDCPHZ128rkz:
  3662. case X86::VFMADDCPHZ256rkz:
  3663. case X86::VFMADDCPHZrkz:
  3664. case X86::VFMADDCPHZrb:
  3665. case X86::VFMADDCPHZrbk:
  3666. case X86::VFMADDCPHZrbkz:
  3667. case X86::VFMADDCSHZm:
  3668. case X86::VFMADDCSHZmk:
  3669. case X86::VFMADDCSHZmkz:
  3670. case X86::VFMADDCSHZr:
  3671. case X86::VFMADDCSHZrb:
  3672. case X86::VFMADDCSHZrbk:
  3673. case X86::VFMADDCSHZrbkz:
  3674. case X86::VFMADDCSHZrk:
  3675. case X86::VFMADDCSHZrkz: {
  3676. unsigned Dest = Inst.getOperand(0).getReg();
  3677. for (unsigned i = 2; i < Inst.getNumOperands(); i++)
  3678. if (Inst.getOperand(i).isReg() && Dest == Inst.getOperand(i).getReg())
  3679. return Warning(Ops[0]->getStartLoc(), "Destination register should be "
  3680. "distinct from source registers");
  3681. break;
  3682. }
  3683. case X86::VFCMULCPHZ128rm:
  3684. case X86::VFCMULCPHZ256rm:
  3685. case X86::VFCMULCPHZrm:
  3686. case X86::VFCMULCPHZ128rmb:
  3687. case X86::VFCMULCPHZ256rmb:
  3688. case X86::VFCMULCPHZrmb:
  3689. case X86::VFCMULCPHZ128rmbk:
  3690. case X86::VFCMULCPHZ256rmbk:
  3691. case X86::VFCMULCPHZrmbk:
  3692. case X86::VFCMULCPHZ128rmbkz:
  3693. case X86::VFCMULCPHZ256rmbkz:
  3694. case X86::VFCMULCPHZrmbkz:
  3695. case X86::VFCMULCPHZ128rmk:
  3696. case X86::VFCMULCPHZ256rmk:
  3697. case X86::VFCMULCPHZrmk:
  3698. case X86::VFCMULCPHZ128rmkz:
  3699. case X86::VFCMULCPHZ256rmkz:
  3700. case X86::VFCMULCPHZrmkz:
  3701. case X86::VFCMULCPHZ128rr:
  3702. case X86::VFCMULCPHZ256rr:
  3703. case X86::VFCMULCPHZrr:
  3704. case X86::VFCMULCPHZ128rrk:
  3705. case X86::VFCMULCPHZ256rrk:
  3706. case X86::VFCMULCPHZrrk:
  3707. case X86::VFCMULCPHZ128rrkz:
  3708. case X86::VFCMULCPHZ256rrkz:
  3709. case X86::VFCMULCPHZrrkz:
  3710. case X86::VFCMULCPHZrrb:
  3711. case X86::VFCMULCPHZrrbk:
  3712. case X86::VFCMULCPHZrrbkz:
  3713. case X86::VFCMULCSHZrm:
  3714. case X86::VFCMULCSHZrmk:
  3715. case X86::VFCMULCSHZrmkz:
  3716. case X86::VFCMULCSHZrr:
  3717. case X86::VFCMULCSHZrrb:
  3718. case X86::VFCMULCSHZrrbk:
  3719. case X86::VFCMULCSHZrrbkz:
  3720. case X86::VFCMULCSHZrrk:
  3721. case X86::VFCMULCSHZrrkz:
  3722. case X86::VFMULCPHZ128rm:
  3723. case X86::VFMULCPHZ256rm:
  3724. case X86::VFMULCPHZrm:
  3725. case X86::VFMULCPHZ128rmb:
  3726. case X86::VFMULCPHZ256rmb:
  3727. case X86::VFMULCPHZrmb:
  3728. case X86::VFMULCPHZ128rmbk:
  3729. case X86::VFMULCPHZ256rmbk:
  3730. case X86::VFMULCPHZrmbk:
  3731. case X86::VFMULCPHZ128rmbkz:
  3732. case X86::VFMULCPHZ256rmbkz:
  3733. case X86::VFMULCPHZrmbkz:
  3734. case X86::VFMULCPHZ128rmk:
  3735. case X86::VFMULCPHZ256rmk:
  3736. case X86::VFMULCPHZrmk:
  3737. case X86::VFMULCPHZ128rmkz:
  3738. case X86::VFMULCPHZ256rmkz:
  3739. case X86::VFMULCPHZrmkz:
  3740. case X86::VFMULCPHZ128rr:
  3741. case X86::VFMULCPHZ256rr:
  3742. case X86::VFMULCPHZrr:
  3743. case X86::VFMULCPHZ128rrk:
  3744. case X86::VFMULCPHZ256rrk:
  3745. case X86::VFMULCPHZrrk:
  3746. case X86::VFMULCPHZ128rrkz:
  3747. case X86::VFMULCPHZ256rrkz:
  3748. case X86::VFMULCPHZrrkz:
  3749. case X86::VFMULCPHZrrb:
  3750. case X86::VFMULCPHZrrbk:
  3751. case X86::VFMULCPHZrrbkz:
  3752. case X86::VFMULCSHZrm:
  3753. case X86::VFMULCSHZrmk:
  3754. case X86::VFMULCSHZrmkz:
  3755. case X86::VFMULCSHZrr:
  3756. case X86::VFMULCSHZrrb:
  3757. case X86::VFMULCSHZrrbk:
  3758. case X86::VFMULCSHZrrbkz:
  3759. case X86::VFMULCSHZrrk:
  3760. case X86::VFMULCSHZrrkz: {
  3761. unsigned Dest = Inst.getOperand(0).getReg();
  3762. for (unsigned i = 1; i < Inst.getNumOperands(); i++)
  3763. if (Inst.getOperand(i).isReg() && Dest == Inst.getOperand(i).getReg())
  3764. return Warning(Ops[0]->getStartLoc(), "Destination register should be "
  3765. "distinct from source registers");
  3766. break;
  3767. }
  3768. }
  3769. const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
  3770. // Check that we aren't mixing AH/BH/CH/DH with REX prefix. We only need to
  3771. // check this with the legacy encoding, VEX/EVEX/XOP don't use REX.
  3772. if ((MCID.TSFlags & X86II::EncodingMask) == 0) {
  3773. MCPhysReg HReg = X86::NoRegister;
  3774. bool UsesRex = MCID.TSFlags & X86II::REX_W;
  3775. unsigned NumOps = Inst.getNumOperands();
  3776. for (unsigned i = 0; i != NumOps; ++i) {
  3777. const MCOperand &MO = Inst.getOperand(i);
  3778. if (!MO.isReg())
  3779. continue;
  3780. unsigned Reg = MO.getReg();
  3781. if (Reg == X86::AH || Reg == X86::BH || Reg == X86::CH || Reg == X86::DH)
  3782. HReg = Reg;
  3783. if (X86II::isX86_64NonExtLowByteReg(Reg) ||
  3784. X86II::isX86_64ExtendedReg(Reg))
  3785. UsesRex = true;
  3786. }
  3787. if (UsesRex && HReg != X86::NoRegister) {
  3788. StringRef RegName = X86IntelInstPrinter::getRegisterName(HReg);
  3789. return Error(Ops[0]->getStartLoc(),
  3790. "can't encode '" + RegName + "' in an instruction requiring "
  3791. "REX prefix");
  3792. }
  3793. }
  3794. return false;
  3795. }
  3796. static const char *getSubtargetFeatureName(uint64_t Val);
  3797. void X86AsmParser::emitWarningForSpecialLVIInstruction(SMLoc Loc) {
  3798. Warning(Loc, "Instruction may be vulnerable to LVI and "
  3799. "requires manual mitigation");
  3800. Note(SMLoc(), "See https://software.intel.com/"
  3801. "security-software-guidance/insights/"
  3802. "deep-dive-load-value-injection#specialinstructions"
  3803. " for more information");
  3804. }
  3805. /// RET instructions and also instructions that indirect calls/jumps from memory
  3806. /// combine a load and a branch within a single instruction. To mitigate these
  3807. /// instructions against LVI, they must be decomposed into separate load and
  3808. /// branch instructions, with an LFENCE in between. For more details, see:
  3809. /// - X86LoadValueInjectionRetHardening.cpp
  3810. /// - X86LoadValueInjectionIndirectThunks.cpp
  3811. /// - https://software.intel.com/security-software-guidance/insights/deep-dive-load-value-injection
  3812. ///
  3813. /// Returns `true` if a mitigation was applied or warning was emitted.
  3814. void X86AsmParser::applyLVICFIMitigation(MCInst &Inst, MCStreamer &Out) {
  3815. // Information on control-flow instructions that require manual mitigation can
  3816. // be found here:
  3817. // https://software.intel.com/security-software-guidance/insights/deep-dive-load-value-injection#specialinstructions
  3818. switch (Inst.getOpcode()) {
  3819. case X86::RET16:
  3820. case X86::RET32:
  3821. case X86::RET64:
  3822. case X86::RETI16:
  3823. case X86::RETI32:
  3824. case X86::RETI64: {
  3825. MCInst ShlInst, FenceInst;
  3826. bool Parse32 = is32BitMode() || Code16GCC;
  3827. unsigned Basereg =
  3828. is64BitMode() ? X86::RSP : (Parse32 ? X86::ESP : X86::SP);
  3829. const MCExpr *Disp = MCConstantExpr::create(0, getContext());
  3830. auto ShlMemOp = X86Operand::CreateMem(getPointerWidth(), /*SegReg=*/0, Disp,
  3831. /*BaseReg=*/Basereg, /*IndexReg=*/0,
  3832. /*Scale=*/1, SMLoc{}, SMLoc{}, 0);
  3833. ShlInst.setOpcode(X86::SHL64mi);
  3834. ShlMemOp->addMemOperands(ShlInst, 5);
  3835. ShlInst.addOperand(MCOperand::createImm(0));
  3836. FenceInst.setOpcode(X86::LFENCE);
  3837. Out.emitInstruction(ShlInst, getSTI());
  3838. Out.emitInstruction(FenceInst, getSTI());
  3839. return;
  3840. }
  3841. case X86::JMP16m:
  3842. case X86::JMP32m:
  3843. case X86::JMP64m:
  3844. case X86::CALL16m:
  3845. case X86::CALL32m:
  3846. case X86::CALL64m:
  3847. emitWarningForSpecialLVIInstruction(Inst.getLoc());
  3848. return;
  3849. }
  3850. }
  3851. /// To mitigate LVI, every instruction that performs a load can be followed by
  3852. /// an LFENCE instruction to squash any potential mis-speculation. There are
  3853. /// some instructions that require additional considerations, and may requre
  3854. /// manual mitigation. For more details, see:
  3855. /// https://software.intel.com/security-software-guidance/insights/deep-dive-load-value-injection
  3856. ///
  3857. /// Returns `true` if a mitigation was applied or warning was emitted.
  3858. void X86AsmParser::applyLVILoadHardeningMitigation(MCInst &Inst,
  3859. MCStreamer &Out) {
  3860. auto Opcode = Inst.getOpcode();
  3861. auto Flags = Inst.getFlags();
  3862. if ((Flags & X86::IP_HAS_REPEAT) || (Flags & X86::IP_HAS_REPEAT_NE)) {
  3863. // Information on REP string instructions that require manual mitigation can
  3864. // be found here:
  3865. // https://software.intel.com/security-software-guidance/insights/deep-dive-load-value-injection#specialinstructions
  3866. switch (Opcode) {
  3867. case X86::CMPSB:
  3868. case X86::CMPSW:
  3869. case X86::CMPSL:
  3870. case X86::CMPSQ:
  3871. case X86::SCASB:
  3872. case X86::SCASW:
  3873. case X86::SCASL:
  3874. case X86::SCASQ:
  3875. emitWarningForSpecialLVIInstruction(Inst.getLoc());
  3876. return;
  3877. }
  3878. } else if (Opcode == X86::REP_PREFIX || Opcode == X86::REPNE_PREFIX) {
  3879. // If a REP instruction is found on its own line, it may or may not be
  3880. // followed by a vulnerable instruction. Emit a warning just in case.
  3881. emitWarningForSpecialLVIInstruction(Inst.getLoc());
  3882. return;
  3883. }
  3884. const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
  3885. // Can't mitigate after terminators or calls. A control flow change may have
  3886. // already occurred.
  3887. if (MCID.isTerminator() || MCID.isCall())
  3888. return;
  3889. // LFENCE has the mayLoad property, don't double fence.
  3890. if (MCID.mayLoad() && Inst.getOpcode() != X86::LFENCE) {
  3891. MCInst FenceInst;
  3892. FenceInst.setOpcode(X86::LFENCE);
  3893. Out.emitInstruction(FenceInst, getSTI());
  3894. }
  3895. }
  3896. void X86AsmParser::emitInstruction(MCInst &Inst, OperandVector &Operands,
  3897. MCStreamer &Out) {
  3898. if (LVIInlineAsmHardening &&
  3899. getSTI().getFeatureBits()[X86::FeatureLVIControlFlowIntegrity])
  3900. applyLVICFIMitigation(Inst, Out);
  3901. Out.emitInstruction(Inst, getSTI());
  3902. if (LVIInlineAsmHardening &&
  3903. getSTI().getFeatureBits()[X86::FeatureLVILoadHardening])
  3904. applyLVILoadHardeningMitigation(Inst, Out);
  3905. }
  3906. bool X86AsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
  3907. OperandVector &Operands,
  3908. MCStreamer &Out, uint64_t &ErrorInfo,
  3909. bool MatchingInlineAsm) {
  3910. if (isParsingIntelSyntax())
  3911. return MatchAndEmitIntelInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo,
  3912. MatchingInlineAsm);
  3913. return MatchAndEmitATTInstruction(IDLoc, Opcode, Operands, Out, ErrorInfo,
  3914. MatchingInlineAsm);
  3915. }
  3916. void X86AsmParser::MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op,
  3917. OperandVector &Operands, MCStreamer &Out,
  3918. bool MatchingInlineAsm) {
  3919. // FIXME: This should be replaced with a real .td file alias mechanism.
  3920. // Also, MatchInstructionImpl should actually *do* the EmitInstruction
  3921. // call.
  3922. const char *Repl = StringSwitch<const char *>(Op.getToken())
  3923. .Case("finit", "fninit")
  3924. .Case("fsave", "fnsave")
  3925. .Case("fstcw", "fnstcw")
  3926. .Case("fstcww", "fnstcw")
  3927. .Case("fstenv", "fnstenv")
  3928. .Case("fstsw", "fnstsw")
  3929. .Case("fstsww", "fnstsw")
  3930. .Case("fclex", "fnclex")
  3931. .Default(nullptr);
  3932. if (Repl) {
  3933. MCInst Inst;
  3934. Inst.setOpcode(X86::WAIT);
  3935. Inst.setLoc(IDLoc);
  3936. if (!MatchingInlineAsm)
  3937. emitInstruction(Inst, Operands, Out);
  3938. Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
  3939. }
  3940. }
  3941. bool X86AsmParser::ErrorMissingFeature(SMLoc IDLoc,
  3942. const FeatureBitset &MissingFeatures,
  3943. bool MatchingInlineAsm) {
  3944. assert(MissingFeatures.any() && "Unknown missing feature!");
  3945. SmallString<126> Msg;
  3946. raw_svector_ostream OS(Msg);
  3947. OS << "instruction requires:";
  3948. for (unsigned i = 0, e = MissingFeatures.size(); i != e; ++i) {
  3949. if (MissingFeatures[i])
  3950. OS << ' ' << getSubtargetFeatureName(i);
  3951. }
  3952. return Error(IDLoc, OS.str(), SMRange(), MatchingInlineAsm);
  3953. }
  3954. static unsigned getPrefixes(OperandVector &Operands) {
  3955. unsigned Result = 0;
  3956. X86Operand &Prefix = static_cast<X86Operand &>(*Operands.back());
  3957. if (Prefix.isPrefix()) {
  3958. Result = Prefix.getPrefix();
  3959. Operands.pop_back();
  3960. }
  3961. return Result;
  3962. }
  3963. unsigned X86AsmParser::checkTargetMatchPredicate(MCInst &Inst) {
  3964. unsigned Opc = Inst.getOpcode();
  3965. const MCInstrDesc &MCID = MII.get(Opc);
  3966. if (ForcedVEXEncoding == VEXEncoding_EVEX &&
  3967. (MCID.TSFlags & X86II::EncodingMask) != X86II::EVEX)
  3968. return Match_Unsupported;
  3969. if ((ForcedVEXEncoding == VEXEncoding_VEX ||
  3970. ForcedVEXEncoding == VEXEncoding_VEX2 ||
  3971. ForcedVEXEncoding == VEXEncoding_VEX3) &&
  3972. (MCID.TSFlags & X86II::EncodingMask) != X86II::VEX)
  3973. return Match_Unsupported;
  3974. // These instructions are only available with {vex}, {vex2} or {vex3} prefix
  3975. if (MCID.TSFlags & X86II::ExplicitVEXPrefix &&
  3976. (ForcedVEXEncoding != VEXEncoding_VEX &&
  3977. ForcedVEXEncoding != VEXEncoding_VEX2 &&
  3978. ForcedVEXEncoding != VEXEncoding_VEX3))
  3979. return Match_Unsupported;
  3980. return Match_Success;
  3981. }
  3982. bool X86AsmParser::MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode,
  3983. OperandVector &Operands,
  3984. MCStreamer &Out,
  3985. uint64_t &ErrorInfo,
  3986. bool MatchingInlineAsm) {
  3987. assert(!Operands.empty() && "Unexpect empty operand list!");
  3988. assert((*Operands[0]).isToken() && "Leading operand should always be a mnemonic!");
  3989. SMRange EmptyRange = None;
  3990. // First, handle aliases that expand to multiple instructions.
  3991. MatchFPUWaitAlias(IDLoc, static_cast<X86Operand &>(*Operands[0]), Operands,
  3992. Out, MatchingInlineAsm);
  3993. X86Operand &Op = static_cast<X86Operand &>(*Operands[0]);
  3994. unsigned Prefixes = getPrefixes(Operands);
  3995. MCInst Inst;
  3996. // If VEX/EVEX encoding is forced, we need to pass the USE_* flag to the
  3997. // encoder and printer.
  3998. if (ForcedVEXEncoding == VEXEncoding_VEX)
  3999. Prefixes |= X86::IP_USE_VEX;
  4000. else if (ForcedVEXEncoding == VEXEncoding_VEX2)
  4001. Prefixes |= X86::IP_USE_VEX2;
  4002. else if (ForcedVEXEncoding == VEXEncoding_VEX3)
  4003. Prefixes |= X86::IP_USE_VEX3;
  4004. else if (ForcedVEXEncoding == VEXEncoding_EVEX)
  4005. Prefixes |= X86::IP_USE_EVEX;
  4006. // Set encoded flags for {disp8} and {disp32}.
  4007. if (ForcedDispEncoding == DispEncoding_Disp8)
  4008. Prefixes |= X86::IP_USE_DISP8;
  4009. else if (ForcedDispEncoding == DispEncoding_Disp32)
  4010. Prefixes |= X86::IP_USE_DISP32;
  4011. if (Prefixes)
  4012. Inst.setFlags(Prefixes);
  4013. // In 16-bit mode, if data32 is specified, temporarily switch to 32-bit mode
  4014. // when matching the instruction.
  4015. if (ForcedDataPrefix == X86::Mode32Bit)
  4016. SwitchMode(X86::Mode32Bit);
  4017. // First, try a direct match.
  4018. FeatureBitset MissingFeatures;
  4019. unsigned OriginalError = MatchInstruction(Operands, Inst, ErrorInfo,
  4020. MissingFeatures, MatchingInlineAsm,
  4021. isParsingIntelSyntax());
  4022. if (ForcedDataPrefix == X86::Mode32Bit) {
  4023. SwitchMode(X86::Mode16Bit);
  4024. ForcedDataPrefix = 0;
  4025. }
  4026. switch (OriginalError) {
  4027. default: llvm_unreachable("Unexpected match result!");
  4028. case Match_Success:
  4029. if (!MatchingInlineAsm && validateInstruction(Inst, Operands))
  4030. return true;
  4031. // Some instructions need post-processing to, for example, tweak which
  4032. // encoding is selected. Loop on it while changes happen so the
  4033. // individual transformations can chain off each other.
  4034. if (!MatchingInlineAsm)
  4035. while (processInstruction(Inst, Operands))
  4036. ;
  4037. Inst.setLoc(IDLoc);
  4038. if (!MatchingInlineAsm)
  4039. emitInstruction(Inst, Operands, Out);
  4040. Opcode = Inst.getOpcode();
  4041. return false;
  4042. case Match_InvalidImmUnsignedi4: {
  4043. SMLoc ErrorLoc = ((X86Operand &)*Operands[ErrorInfo]).getStartLoc();
  4044. if (ErrorLoc == SMLoc())
  4045. ErrorLoc = IDLoc;
  4046. return Error(ErrorLoc, "immediate must be an integer in range [0, 15]",
  4047. EmptyRange, MatchingInlineAsm);
  4048. }
  4049. case Match_MissingFeature:
  4050. return ErrorMissingFeature(IDLoc, MissingFeatures, MatchingInlineAsm);
  4051. case Match_InvalidOperand:
  4052. case Match_MnemonicFail:
  4053. case Match_Unsupported:
  4054. break;
  4055. }
  4056. if (Op.getToken().empty()) {
  4057. Error(IDLoc, "instruction must have size higher than 0", EmptyRange,
  4058. MatchingInlineAsm);
  4059. return true;
  4060. }
  4061. // FIXME: Ideally, we would only attempt suffix matches for things which are
  4062. // valid prefixes, and we could just infer the right unambiguous
  4063. // type. However, that requires substantially more matcher support than the
  4064. // following hack.
  4065. // Change the operand to point to a temporary token.
  4066. StringRef Base = Op.getToken();
  4067. SmallString<16> Tmp;
  4068. Tmp += Base;
  4069. Tmp += ' ';
  4070. Op.setTokenValue(Tmp);
  4071. // If this instruction starts with an 'f', then it is a floating point stack
  4072. // instruction. These come in up to three forms for 32-bit, 64-bit, and
  4073. // 80-bit floating point, which use the suffixes s,l,t respectively.
  4074. //
  4075. // Otherwise, we assume that this may be an integer instruction, which comes
  4076. // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
  4077. const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
  4078. // MemSize corresponding to Suffixes. { 8, 16, 32, 64 } { 32, 64, 80, 0 }
  4079. const char *MemSize = Base[0] != 'f' ? "\x08\x10\x20\x40" : "\x20\x40\x50\0";
  4080. // Check for the various suffix matches.
  4081. uint64_t ErrorInfoIgnore;
  4082. FeatureBitset ErrorInfoMissingFeatures; // Init suppresses compiler warnings.
  4083. unsigned Match[4];
  4084. // Some instruction like VPMULDQ is NOT the variant of VPMULD but a new one.
  4085. // So we should make sure the suffix matcher only works for memory variant
  4086. // that has the same size with the suffix.
  4087. // FIXME: This flag is a workaround for legacy instructions that didn't
  4088. // declare non suffix variant assembly.
  4089. bool HasVectorReg = false;
  4090. X86Operand *MemOp = nullptr;
  4091. for (const auto &Op : Operands) {
  4092. X86Operand *X86Op = static_cast<X86Operand *>(Op.get());
  4093. if (X86Op->isVectorReg())
  4094. HasVectorReg = true;
  4095. else if (X86Op->isMem()) {
  4096. MemOp = X86Op;
  4097. assert(MemOp->Mem.Size == 0 && "Memory size always 0 under ATT syntax");
  4098. // Have we found an unqualified memory operand,
  4099. // break. IA allows only one memory operand.
  4100. break;
  4101. }
  4102. }
  4103. for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I) {
  4104. Tmp.back() = Suffixes[I];
  4105. if (MemOp && HasVectorReg)
  4106. MemOp->Mem.Size = MemSize[I];
  4107. Match[I] = Match_MnemonicFail;
  4108. if (MemOp || !HasVectorReg) {
  4109. Match[I] =
  4110. MatchInstruction(Operands, Inst, ErrorInfoIgnore, MissingFeatures,
  4111. MatchingInlineAsm, isParsingIntelSyntax());
  4112. // If this returned as a missing feature failure, remember that.
  4113. if (Match[I] == Match_MissingFeature)
  4114. ErrorInfoMissingFeatures = MissingFeatures;
  4115. }
  4116. }
  4117. // Restore the old token.
  4118. Op.setTokenValue(Base);
  4119. // If exactly one matched, then we treat that as a successful match (and the
  4120. // instruction will already have been filled in correctly, since the failing
  4121. // matches won't have modified it).
  4122. unsigned NumSuccessfulMatches = llvm::count(Match, Match_Success);
  4123. if (NumSuccessfulMatches == 1) {
  4124. if (!MatchingInlineAsm && validateInstruction(Inst, Operands))
  4125. return true;
  4126. // Some instructions need post-processing to, for example, tweak which
  4127. // encoding is selected. Loop on it while changes happen so the
  4128. // individual transformations can chain off each other.
  4129. if (!MatchingInlineAsm)
  4130. while (processInstruction(Inst, Operands))
  4131. ;
  4132. Inst.setLoc(IDLoc);
  4133. if (!MatchingInlineAsm)
  4134. emitInstruction(Inst, Operands, Out);
  4135. Opcode = Inst.getOpcode();
  4136. return false;
  4137. }
  4138. // Otherwise, the match failed, try to produce a decent error message.
  4139. // If we had multiple suffix matches, then identify this as an ambiguous
  4140. // match.
  4141. if (NumSuccessfulMatches > 1) {
  4142. char MatchChars[4];
  4143. unsigned NumMatches = 0;
  4144. for (unsigned I = 0, E = array_lengthof(Match); I != E; ++I)
  4145. if (Match[I] == Match_Success)
  4146. MatchChars[NumMatches++] = Suffixes[I];
  4147. SmallString<126> Msg;
  4148. raw_svector_ostream OS(Msg);
  4149. OS << "ambiguous instructions require an explicit suffix (could be ";
  4150. for (unsigned i = 0; i != NumMatches; ++i) {
  4151. if (i != 0)
  4152. OS << ", ";
  4153. if (i + 1 == NumMatches)
  4154. OS << "or ";
  4155. OS << "'" << Base << MatchChars[i] << "'";
  4156. }
  4157. OS << ")";
  4158. Error(IDLoc, OS.str(), EmptyRange, MatchingInlineAsm);
  4159. return true;
  4160. }
  4161. // Okay, we know that none of the variants matched successfully.
  4162. // If all of the instructions reported an invalid mnemonic, then the original
  4163. // mnemonic was invalid.
  4164. if (llvm::count(Match, Match_MnemonicFail) == 4) {
  4165. if (OriginalError == Match_MnemonicFail)
  4166. return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
  4167. Op.getLocRange(), MatchingInlineAsm);
  4168. if (OriginalError == Match_Unsupported)
  4169. return Error(IDLoc, "unsupported instruction", EmptyRange,
  4170. MatchingInlineAsm);
  4171. assert(OriginalError == Match_InvalidOperand && "Unexpected error");
  4172. // Recover location info for the operand if we know which was the problem.
  4173. if (ErrorInfo != ~0ULL) {
  4174. if (ErrorInfo >= Operands.size())
  4175. return Error(IDLoc, "too few operands for instruction", EmptyRange,
  4176. MatchingInlineAsm);
  4177. X86Operand &Operand = (X86Operand &)*Operands[ErrorInfo];
  4178. if (Operand.getStartLoc().isValid()) {
  4179. SMRange OperandRange = Operand.getLocRange();
  4180. return Error(Operand.getStartLoc(), "invalid operand for instruction",
  4181. OperandRange, MatchingInlineAsm);
  4182. }
  4183. }
  4184. return Error(IDLoc, "invalid operand for instruction", EmptyRange,
  4185. MatchingInlineAsm);
  4186. }
  4187. // If one instruction matched as unsupported, report this as unsupported.
  4188. if (llvm::count(Match, Match_Unsupported) == 1) {
  4189. return Error(IDLoc, "unsupported instruction", EmptyRange,
  4190. MatchingInlineAsm);
  4191. }
  4192. // If one instruction matched with a missing feature, report this as a
  4193. // missing feature.
  4194. if (llvm::count(Match, Match_MissingFeature) == 1) {
  4195. ErrorInfo = Match_MissingFeature;
  4196. return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeatures,
  4197. MatchingInlineAsm);
  4198. }
  4199. // If one instruction matched with an invalid operand, report this as an
  4200. // operand failure.
  4201. if (llvm::count(Match, Match_InvalidOperand) == 1) {
  4202. return Error(IDLoc, "invalid operand for instruction", EmptyRange,
  4203. MatchingInlineAsm);
  4204. }
  4205. // If all of these were an outright failure, report it in a useless way.
  4206. Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
  4207. EmptyRange, MatchingInlineAsm);
  4208. return true;
  4209. }
  4210. bool X86AsmParser::MatchAndEmitIntelInstruction(SMLoc IDLoc, unsigned &Opcode,
  4211. OperandVector &Operands,
  4212. MCStreamer &Out,
  4213. uint64_t &ErrorInfo,
  4214. bool MatchingInlineAsm) {
  4215. assert(!Operands.empty() && "Unexpect empty operand list!");
  4216. assert((*Operands[0]).isToken() && "Leading operand should always be a mnemonic!");
  4217. StringRef Mnemonic = (static_cast<X86Operand &>(*Operands[0])).getToken();
  4218. SMRange EmptyRange = None;
  4219. StringRef Base = (static_cast<X86Operand &>(*Operands[0])).getToken();
  4220. unsigned Prefixes = getPrefixes(Operands);
  4221. // First, handle aliases that expand to multiple instructions.
  4222. MatchFPUWaitAlias(IDLoc, static_cast<X86Operand &>(*Operands[0]), Operands, Out, MatchingInlineAsm);
  4223. X86Operand &Op = static_cast<X86Operand &>(*Operands[0]);
  4224. MCInst Inst;
  4225. // If VEX/EVEX encoding is forced, we need to pass the USE_* flag to the
  4226. // encoder and printer.
  4227. if (ForcedVEXEncoding == VEXEncoding_VEX)
  4228. Prefixes |= X86::IP_USE_VEX;
  4229. else if (ForcedVEXEncoding == VEXEncoding_VEX2)
  4230. Prefixes |= X86::IP_USE_VEX2;
  4231. else if (ForcedVEXEncoding == VEXEncoding_VEX3)
  4232. Prefixes |= X86::IP_USE_VEX3;
  4233. else if (ForcedVEXEncoding == VEXEncoding_EVEX)
  4234. Prefixes |= X86::IP_USE_EVEX;
  4235. // Set encoded flags for {disp8} and {disp32}.
  4236. if (ForcedDispEncoding == DispEncoding_Disp8)
  4237. Prefixes |= X86::IP_USE_DISP8;
  4238. else if (ForcedDispEncoding == DispEncoding_Disp32)
  4239. Prefixes |= X86::IP_USE_DISP32;
  4240. if (Prefixes)
  4241. Inst.setFlags(Prefixes);
  4242. // Find one unsized memory operand, if present.
  4243. X86Operand *UnsizedMemOp = nullptr;
  4244. for (const auto &Op : Operands) {
  4245. X86Operand *X86Op = static_cast<X86Operand *>(Op.get());
  4246. if (X86Op->isMemUnsized()) {
  4247. UnsizedMemOp = X86Op;
  4248. // Have we found an unqualified memory operand,
  4249. // break. IA allows only one memory operand.
  4250. break;
  4251. }
  4252. }
  4253. // Allow some instructions to have implicitly pointer-sized operands. This is
  4254. // compatible with gas.
  4255. if (UnsizedMemOp) {
  4256. static const char *const PtrSizedInstrs[] = {"call", "jmp", "push"};
  4257. for (const char *Instr : PtrSizedInstrs) {
  4258. if (Mnemonic == Instr) {
  4259. UnsizedMemOp->Mem.Size = getPointerWidth();
  4260. break;
  4261. }
  4262. }
  4263. }
  4264. SmallVector<unsigned, 8> Match;
  4265. FeatureBitset ErrorInfoMissingFeatures;
  4266. FeatureBitset MissingFeatures;
  4267. // If unsized push has immediate operand we should default the default pointer
  4268. // size for the size.
  4269. if (Mnemonic == "push" && Operands.size() == 2) {
  4270. auto *X86Op = static_cast<X86Operand *>(Operands[1].get());
  4271. if (X86Op->isImm()) {
  4272. // If it's not a constant fall through and let remainder take care of it.
  4273. const auto *CE = dyn_cast<MCConstantExpr>(X86Op->getImm());
  4274. unsigned Size = getPointerWidth();
  4275. if (CE &&
  4276. (isIntN(Size, CE->getValue()) || isUIntN(Size, CE->getValue()))) {
  4277. SmallString<16> Tmp;
  4278. Tmp += Base;
  4279. Tmp += (is64BitMode())
  4280. ? "q"
  4281. : (is32BitMode()) ? "l" : (is16BitMode()) ? "w" : " ";
  4282. Op.setTokenValue(Tmp);
  4283. // Do match in ATT mode to allow explicit suffix usage.
  4284. Match.push_back(MatchInstruction(Operands, Inst, ErrorInfo,
  4285. MissingFeatures, MatchingInlineAsm,
  4286. false /*isParsingIntelSyntax()*/));
  4287. Op.setTokenValue(Base);
  4288. }
  4289. }
  4290. }
  4291. // If an unsized memory operand is present, try to match with each memory
  4292. // operand size. In Intel assembly, the size is not part of the instruction
  4293. // mnemonic.
  4294. if (UnsizedMemOp && UnsizedMemOp->isMemUnsized()) {
  4295. static const unsigned MopSizes[] = {8, 16, 32, 64, 80, 128, 256, 512};
  4296. for (unsigned Size : MopSizes) {
  4297. UnsizedMemOp->Mem.Size = Size;
  4298. uint64_t ErrorInfoIgnore;
  4299. unsigned LastOpcode = Inst.getOpcode();
  4300. unsigned M = MatchInstruction(Operands, Inst, ErrorInfoIgnore,
  4301. MissingFeatures, MatchingInlineAsm,
  4302. isParsingIntelSyntax());
  4303. if (Match.empty() || LastOpcode != Inst.getOpcode())
  4304. Match.push_back(M);
  4305. // If this returned as a missing feature failure, remember that.
  4306. if (Match.back() == Match_MissingFeature)
  4307. ErrorInfoMissingFeatures = MissingFeatures;
  4308. }
  4309. // Restore the size of the unsized memory operand if we modified it.
  4310. UnsizedMemOp->Mem.Size = 0;
  4311. }
  4312. // If we haven't matched anything yet, this is not a basic integer or FPU
  4313. // operation. There shouldn't be any ambiguity in our mnemonic table, so try
  4314. // matching with the unsized operand.
  4315. if (Match.empty()) {
  4316. Match.push_back(MatchInstruction(
  4317. Operands, Inst, ErrorInfo, MissingFeatures, MatchingInlineAsm,
  4318. isParsingIntelSyntax()));
  4319. // If this returned as a missing feature failure, remember that.
  4320. if (Match.back() == Match_MissingFeature)
  4321. ErrorInfoMissingFeatures = MissingFeatures;
  4322. }
  4323. // Restore the size of the unsized memory operand if we modified it.
  4324. if (UnsizedMemOp)
  4325. UnsizedMemOp->Mem.Size = 0;
  4326. // If it's a bad mnemonic, all results will be the same.
  4327. if (Match.back() == Match_MnemonicFail) {
  4328. return Error(IDLoc, "invalid instruction mnemonic '" + Mnemonic + "'",
  4329. Op.getLocRange(), MatchingInlineAsm);
  4330. }
  4331. unsigned NumSuccessfulMatches = llvm::count(Match, Match_Success);
  4332. // If matching was ambiguous and we had size information from the frontend,
  4333. // try again with that. This handles cases like "movxz eax, m8/m16".
  4334. if (UnsizedMemOp && NumSuccessfulMatches > 1 &&
  4335. UnsizedMemOp->getMemFrontendSize()) {
  4336. UnsizedMemOp->Mem.Size = UnsizedMemOp->getMemFrontendSize();
  4337. unsigned M = MatchInstruction(
  4338. Operands, Inst, ErrorInfo, MissingFeatures, MatchingInlineAsm,
  4339. isParsingIntelSyntax());
  4340. if (M == Match_Success)
  4341. NumSuccessfulMatches = 1;
  4342. // Add a rewrite that encodes the size information we used from the
  4343. // frontend.
  4344. InstInfo->AsmRewrites->emplace_back(
  4345. AOK_SizeDirective, UnsizedMemOp->getStartLoc(),
  4346. /*Len=*/0, UnsizedMemOp->getMemFrontendSize());
  4347. }
  4348. // If exactly one matched, then we treat that as a successful match (and the
  4349. // instruction will already have been filled in correctly, since the failing
  4350. // matches won't have modified it).
  4351. if (NumSuccessfulMatches == 1) {
  4352. if (!MatchingInlineAsm && validateInstruction(Inst, Operands))
  4353. return true;
  4354. // Some instructions need post-processing to, for example, tweak which
  4355. // encoding is selected. Loop on it while changes happen so the individual
  4356. // transformations can chain off each other.
  4357. if (!MatchingInlineAsm)
  4358. while (processInstruction(Inst, Operands))
  4359. ;
  4360. Inst.setLoc(IDLoc);
  4361. if (!MatchingInlineAsm)
  4362. emitInstruction(Inst, Operands, Out);
  4363. Opcode = Inst.getOpcode();
  4364. return false;
  4365. } else if (NumSuccessfulMatches > 1) {
  4366. assert(UnsizedMemOp &&
  4367. "multiple matches only possible with unsized memory operands");
  4368. return Error(UnsizedMemOp->getStartLoc(),
  4369. "ambiguous operand size for instruction '" + Mnemonic + "\'",
  4370. UnsizedMemOp->getLocRange());
  4371. }
  4372. // If one instruction matched as unsupported, report this as unsupported.
  4373. if (llvm::count(Match, Match_Unsupported) == 1) {
  4374. return Error(IDLoc, "unsupported instruction", EmptyRange,
  4375. MatchingInlineAsm);
  4376. }
  4377. // If one instruction matched with a missing feature, report this as a
  4378. // missing feature.
  4379. if (llvm::count(Match, Match_MissingFeature) == 1) {
  4380. ErrorInfo = Match_MissingFeature;
  4381. return ErrorMissingFeature(IDLoc, ErrorInfoMissingFeatures,
  4382. MatchingInlineAsm);
  4383. }
  4384. // If one instruction matched with an invalid operand, report this as an
  4385. // operand failure.
  4386. if (llvm::count(Match, Match_InvalidOperand) == 1) {
  4387. return Error(IDLoc, "invalid operand for instruction", EmptyRange,
  4388. MatchingInlineAsm);
  4389. }
  4390. if (llvm::count(Match, Match_InvalidImmUnsignedi4) == 1) {
  4391. SMLoc ErrorLoc = ((X86Operand &)*Operands[ErrorInfo]).getStartLoc();
  4392. if (ErrorLoc == SMLoc())
  4393. ErrorLoc = IDLoc;
  4394. return Error(ErrorLoc, "immediate must be an integer in range [0, 15]",
  4395. EmptyRange, MatchingInlineAsm);
  4396. }
  4397. // If all of these were an outright failure, report it in a useless way.
  4398. return Error(IDLoc, "unknown instruction mnemonic", EmptyRange,
  4399. MatchingInlineAsm);
  4400. }
  4401. bool X86AsmParser::OmitRegisterFromClobberLists(unsigned RegNo) {
  4402. return X86MCRegisterClasses[X86::SEGMENT_REGRegClassID].contains(RegNo);
  4403. }
  4404. bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
  4405. MCAsmParser &Parser = getParser();
  4406. StringRef IDVal = DirectiveID.getIdentifier();
  4407. if (IDVal.startswith(".arch"))
  4408. return parseDirectiveArch();
  4409. if (IDVal.startswith(".code"))
  4410. return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
  4411. else if (IDVal.startswith(".att_syntax")) {
  4412. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  4413. if (Parser.getTok().getString() == "prefix")
  4414. Parser.Lex();
  4415. else if (Parser.getTok().getString() == "noprefix")
  4416. return Error(DirectiveID.getLoc(), "'.att_syntax noprefix' is not "
  4417. "supported: registers must have a "
  4418. "'%' prefix in .att_syntax");
  4419. }
  4420. getParser().setAssemblerDialect(0);
  4421. return false;
  4422. } else if (IDVal.startswith(".intel_syntax")) {
  4423. getParser().setAssemblerDialect(1);
  4424. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  4425. if (Parser.getTok().getString() == "noprefix")
  4426. Parser.Lex();
  4427. else if (Parser.getTok().getString() == "prefix")
  4428. return Error(DirectiveID.getLoc(), "'.intel_syntax prefix' is not "
  4429. "supported: registers must not have "
  4430. "a '%' prefix in .intel_syntax");
  4431. }
  4432. return false;
  4433. } else if (IDVal == ".nops")
  4434. return parseDirectiveNops(DirectiveID.getLoc());
  4435. else if (IDVal == ".even")
  4436. return parseDirectiveEven(DirectiveID.getLoc());
  4437. else if (IDVal == ".cv_fpo_proc")
  4438. return parseDirectiveFPOProc(DirectiveID.getLoc());
  4439. else if (IDVal == ".cv_fpo_setframe")
  4440. return parseDirectiveFPOSetFrame(DirectiveID.getLoc());
  4441. else if (IDVal == ".cv_fpo_pushreg")
  4442. return parseDirectiveFPOPushReg(DirectiveID.getLoc());
  4443. else if (IDVal == ".cv_fpo_stackalloc")
  4444. return parseDirectiveFPOStackAlloc(DirectiveID.getLoc());
  4445. else if (IDVal == ".cv_fpo_stackalign")
  4446. return parseDirectiveFPOStackAlign(DirectiveID.getLoc());
  4447. else if (IDVal == ".cv_fpo_endprologue")
  4448. return parseDirectiveFPOEndPrologue(DirectiveID.getLoc());
  4449. else if (IDVal == ".cv_fpo_endproc")
  4450. return parseDirectiveFPOEndProc(DirectiveID.getLoc());
  4451. else if (IDVal == ".seh_pushreg" ||
  4452. (Parser.isParsingMasm() && IDVal.equals_insensitive(".pushreg")))
  4453. return parseDirectiveSEHPushReg(DirectiveID.getLoc());
  4454. else if (IDVal == ".seh_setframe" ||
  4455. (Parser.isParsingMasm() && IDVal.equals_insensitive(".setframe")))
  4456. return parseDirectiveSEHSetFrame(DirectiveID.getLoc());
  4457. else if (IDVal == ".seh_savereg" ||
  4458. (Parser.isParsingMasm() && IDVal.equals_insensitive(".savereg")))
  4459. return parseDirectiveSEHSaveReg(DirectiveID.getLoc());
  4460. else if (IDVal == ".seh_savexmm" ||
  4461. (Parser.isParsingMasm() && IDVal.equals_insensitive(".savexmm128")))
  4462. return parseDirectiveSEHSaveXMM(DirectiveID.getLoc());
  4463. else if (IDVal == ".seh_pushframe" ||
  4464. (Parser.isParsingMasm() && IDVal.equals_insensitive(".pushframe")))
  4465. return parseDirectiveSEHPushFrame(DirectiveID.getLoc());
  4466. return true;
  4467. }
  4468. bool X86AsmParser::parseDirectiveArch() {
  4469. // Ignore .arch for now.
  4470. getParser().parseStringToEndOfStatement();
  4471. return false;
  4472. }
  4473. /// parseDirectiveNops
  4474. /// ::= .nops size[, control]
  4475. bool X86AsmParser::parseDirectiveNops(SMLoc L) {
  4476. int64_t NumBytes = 0, Control = 0;
  4477. SMLoc NumBytesLoc, ControlLoc;
  4478. const MCSubtargetInfo& STI = getSTI();
  4479. NumBytesLoc = getTok().getLoc();
  4480. if (getParser().checkForValidSection() ||
  4481. getParser().parseAbsoluteExpression(NumBytes))
  4482. return true;
  4483. if (parseOptionalToken(AsmToken::Comma)) {
  4484. ControlLoc = getTok().getLoc();
  4485. if (getParser().parseAbsoluteExpression(Control))
  4486. return true;
  4487. }
  4488. if (getParser().parseToken(AsmToken::EndOfStatement,
  4489. "unexpected token in '.nops' directive"))
  4490. return true;
  4491. if (NumBytes <= 0) {
  4492. Error(NumBytesLoc, "'.nops' directive with non-positive size");
  4493. return false;
  4494. }
  4495. if (Control < 0) {
  4496. Error(ControlLoc, "'.nops' directive with negative NOP size");
  4497. return false;
  4498. }
  4499. /// Emit nops
  4500. getParser().getStreamer().emitNops(NumBytes, Control, L, STI);
  4501. return false;
  4502. }
  4503. /// parseDirectiveEven
  4504. /// ::= .even
  4505. bool X86AsmParser::parseDirectiveEven(SMLoc L) {
  4506. if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive"))
  4507. return false;
  4508. const MCSection *Section = getStreamer().getCurrentSectionOnly();
  4509. if (!Section) {
  4510. getStreamer().initSections(false, getSTI());
  4511. Section = getStreamer().getCurrentSectionOnly();
  4512. }
  4513. if (Section->UseCodeAlign())
  4514. getStreamer().emitCodeAlignment(2, &getSTI(), 0);
  4515. else
  4516. getStreamer().emitValueToAlignment(2, 0, 1, 0);
  4517. return false;
  4518. }
  4519. /// ParseDirectiveCode
  4520. /// ::= .code16 | .code32 | .code64
  4521. bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
  4522. MCAsmParser &Parser = getParser();
  4523. Code16GCC = false;
  4524. if (IDVal == ".code16") {
  4525. Parser.Lex();
  4526. if (!is16BitMode()) {
  4527. SwitchMode(X86::Mode16Bit);
  4528. getParser().getStreamer().emitAssemblerFlag(MCAF_Code16);
  4529. }
  4530. } else if (IDVal == ".code16gcc") {
  4531. // .code16gcc parses as if in 32-bit mode, but emits code in 16-bit mode.
  4532. Parser.Lex();
  4533. Code16GCC = true;
  4534. if (!is16BitMode()) {
  4535. SwitchMode(X86::Mode16Bit);
  4536. getParser().getStreamer().emitAssemblerFlag(MCAF_Code16);
  4537. }
  4538. } else if (IDVal == ".code32") {
  4539. Parser.Lex();
  4540. if (!is32BitMode()) {
  4541. SwitchMode(X86::Mode32Bit);
  4542. getParser().getStreamer().emitAssemblerFlag(MCAF_Code32);
  4543. }
  4544. } else if (IDVal == ".code64") {
  4545. Parser.Lex();
  4546. if (!is64BitMode()) {
  4547. SwitchMode(X86::Mode64Bit);
  4548. getParser().getStreamer().emitAssemblerFlag(MCAF_Code64);
  4549. }
  4550. } else {
  4551. Error(L, "unknown directive " + IDVal);
  4552. return false;
  4553. }
  4554. return false;
  4555. }
  4556. // .cv_fpo_proc foo
  4557. bool X86AsmParser::parseDirectiveFPOProc(SMLoc L) {
  4558. MCAsmParser &Parser = getParser();
  4559. StringRef ProcName;
  4560. int64_t ParamsSize;
  4561. if (Parser.parseIdentifier(ProcName))
  4562. return Parser.TokError("expected symbol name");
  4563. if (Parser.parseIntToken(ParamsSize, "expected parameter byte count"))
  4564. return true;
  4565. if (!isUIntN(32, ParamsSize))
  4566. return Parser.TokError("parameters size out of range");
  4567. if (parseEOL())
  4568. return true;
  4569. MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName);
  4570. return getTargetStreamer().emitFPOProc(ProcSym, ParamsSize, L);
  4571. }
  4572. // .cv_fpo_setframe ebp
  4573. bool X86AsmParser::parseDirectiveFPOSetFrame(SMLoc L) {
  4574. unsigned Reg;
  4575. SMLoc DummyLoc;
  4576. if (ParseRegister(Reg, DummyLoc, DummyLoc) || parseEOL())
  4577. return true;
  4578. return getTargetStreamer().emitFPOSetFrame(Reg, L);
  4579. }
  4580. // .cv_fpo_pushreg ebx
  4581. bool X86AsmParser::parseDirectiveFPOPushReg(SMLoc L) {
  4582. unsigned Reg;
  4583. SMLoc DummyLoc;
  4584. if (ParseRegister(Reg, DummyLoc, DummyLoc) || parseEOL())
  4585. return true;
  4586. return getTargetStreamer().emitFPOPushReg(Reg, L);
  4587. }
  4588. // .cv_fpo_stackalloc 20
  4589. bool X86AsmParser::parseDirectiveFPOStackAlloc(SMLoc L) {
  4590. MCAsmParser &Parser = getParser();
  4591. int64_t Offset;
  4592. if (Parser.parseIntToken(Offset, "expected offset") || parseEOL())
  4593. return true;
  4594. return getTargetStreamer().emitFPOStackAlloc(Offset, L);
  4595. }
  4596. // .cv_fpo_stackalign 8
  4597. bool X86AsmParser::parseDirectiveFPOStackAlign(SMLoc L) {
  4598. MCAsmParser &Parser = getParser();
  4599. int64_t Offset;
  4600. if (Parser.parseIntToken(Offset, "expected offset") || parseEOL())
  4601. return true;
  4602. return getTargetStreamer().emitFPOStackAlign(Offset, L);
  4603. }
  4604. // .cv_fpo_endprologue
  4605. bool X86AsmParser::parseDirectiveFPOEndPrologue(SMLoc L) {
  4606. MCAsmParser &Parser = getParser();
  4607. if (Parser.parseEOL())
  4608. return true;
  4609. return getTargetStreamer().emitFPOEndPrologue(L);
  4610. }
  4611. // .cv_fpo_endproc
  4612. bool X86AsmParser::parseDirectiveFPOEndProc(SMLoc L) {
  4613. MCAsmParser &Parser = getParser();
  4614. if (Parser.parseEOL())
  4615. return true;
  4616. return getTargetStreamer().emitFPOEndProc(L);
  4617. }
  4618. bool X86AsmParser::parseSEHRegisterNumber(unsigned RegClassID,
  4619. unsigned &RegNo) {
  4620. SMLoc startLoc = getLexer().getLoc();
  4621. const MCRegisterInfo *MRI = getContext().getRegisterInfo();
  4622. // Try parsing the argument as a register first.
  4623. if (getLexer().getTok().isNot(AsmToken::Integer)) {
  4624. SMLoc endLoc;
  4625. if (ParseRegister(RegNo, startLoc, endLoc))
  4626. return true;
  4627. if (!X86MCRegisterClasses[RegClassID].contains(RegNo)) {
  4628. return Error(startLoc,
  4629. "register is not supported for use with this directive");
  4630. }
  4631. } else {
  4632. // Otherwise, an integer number matching the encoding of the desired
  4633. // register may appear.
  4634. int64_t EncodedReg;
  4635. if (getParser().parseAbsoluteExpression(EncodedReg))
  4636. return true;
  4637. // The SEH register number is the same as the encoding register number. Map
  4638. // from the encoding back to the LLVM register number.
  4639. RegNo = 0;
  4640. for (MCPhysReg Reg : X86MCRegisterClasses[RegClassID]) {
  4641. if (MRI->getEncodingValue(Reg) == EncodedReg) {
  4642. RegNo = Reg;
  4643. break;
  4644. }
  4645. }
  4646. if (RegNo == 0) {
  4647. return Error(startLoc,
  4648. "incorrect register number for use with this directive");
  4649. }
  4650. }
  4651. return false;
  4652. }
  4653. bool X86AsmParser::parseDirectiveSEHPushReg(SMLoc Loc) {
  4654. unsigned Reg = 0;
  4655. if (parseSEHRegisterNumber(X86::GR64RegClassID, Reg))
  4656. return true;
  4657. if (getLexer().isNot(AsmToken::EndOfStatement))
  4658. return TokError("unexpected token in directive");
  4659. getParser().Lex();
  4660. getStreamer().EmitWinCFIPushReg(Reg, Loc);
  4661. return false;
  4662. }
  4663. bool X86AsmParser::parseDirectiveSEHSetFrame(SMLoc Loc) {
  4664. unsigned Reg = 0;
  4665. int64_t Off;
  4666. if (parseSEHRegisterNumber(X86::GR64RegClassID, Reg))
  4667. return true;
  4668. if (getLexer().isNot(AsmToken::Comma))
  4669. return TokError("you must specify a stack pointer offset");
  4670. getParser().Lex();
  4671. if (getParser().parseAbsoluteExpression(Off))
  4672. return true;
  4673. if (getLexer().isNot(AsmToken::EndOfStatement))
  4674. return TokError("unexpected token in directive");
  4675. getParser().Lex();
  4676. getStreamer().EmitWinCFISetFrame(Reg, Off, Loc);
  4677. return false;
  4678. }
  4679. bool X86AsmParser::parseDirectiveSEHSaveReg(SMLoc Loc) {
  4680. unsigned Reg = 0;
  4681. int64_t Off;
  4682. if (parseSEHRegisterNumber(X86::GR64RegClassID, Reg))
  4683. return true;
  4684. if (getLexer().isNot(AsmToken::Comma))
  4685. return TokError("you must specify an offset on the stack");
  4686. getParser().Lex();
  4687. if (getParser().parseAbsoluteExpression(Off))
  4688. return true;
  4689. if (getLexer().isNot(AsmToken::EndOfStatement))
  4690. return TokError("unexpected token in directive");
  4691. getParser().Lex();
  4692. getStreamer().EmitWinCFISaveReg(Reg, Off, Loc);
  4693. return false;
  4694. }
  4695. bool X86AsmParser::parseDirectiveSEHSaveXMM(SMLoc Loc) {
  4696. unsigned Reg = 0;
  4697. int64_t Off;
  4698. if (parseSEHRegisterNumber(X86::VR128XRegClassID, Reg))
  4699. return true;
  4700. if (getLexer().isNot(AsmToken::Comma))
  4701. return TokError("you must specify an offset on the stack");
  4702. getParser().Lex();
  4703. if (getParser().parseAbsoluteExpression(Off))
  4704. return true;
  4705. if (getLexer().isNot(AsmToken::EndOfStatement))
  4706. return TokError("unexpected token in directive");
  4707. getParser().Lex();
  4708. getStreamer().EmitWinCFISaveXMM(Reg, Off, Loc);
  4709. return false;
  4710. }
  4711. bool X86AsmParser::parseDirectiveSEHPushFrame(SMLoc Loc) {
  4712. bool Code = false;
  4713. StringRef CodeID;
  4714. if (getLexer().is(AsmToken::At)) {
  4715. SMLoc startLoc = getLexer().getLoc();
  4716. getParser().Lex();
  4717. if (!getParser().parseIdentifier(CodeID)) {
  4718. if (CodeID != "code")
  4719. return Error(startLoc, "expected @code");
  4720. Code = true;
  4721. }
  4722. }
  4723. if (getLexer().isNot(AsmToken::EndOfStatement))
  4724. return TokError("unexpected token in directive");
  4725. getParser().Lex();
  4726. getStreamer().EmitWinCFIPushFrame(Code, Loc);
  4727. return false;
  4728. }
  4729. // Force static initialization.
  4730. extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeX86AsmParser() {
  4731. RegisterMCAsmParser<X86AsmParser> X(getTheX86_32Target());
  4732. RegisterMCAsmParser<X86AsmParser> Y(getTheX86_64Target());
  4733. }
  4734. #define GET_REGISTER_MATCHER
  4735. #define GET_MATCHER_IMPLEMENTATION
  4736. #define GET_SUBTARGET_FEATURE_NAME
  4737. #include "X86GenAsmMatcher.inc"