PPCAsmParser.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  1. //===-- PPCAsmParser.cpp - Parse PowerPC asm 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/PPCMCExpr.h"
  9. #include "MCTargetDesc/PPCMCTargetDesc.h"
  10. #include "PPCTargetStreamer.h"
  11. #include "TargetInfo/PowerPCTargetInfo.h"
  12. #include "llvm/ADT/STLExtras.h"
  13. #include "llvm/ADT/Twine.h"
  14. #include "llvm/MC/MCContext.h"
  15. #include "llvm/MC/MCExpr.h"
  16. #include "llvm/MC/MCInst.h"
  17. #include "llvm/MC/MCInstrInfo.h"
  18. #include "llvm/MC/MCParser/MCAsmLexer.h"
  19. #include "llvm/MC/MCParser/MCAsmParser.h"
  20. #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
  21. #include "llvm/MC/MCParser/MCTargetAsmParser.h"
  22. #include "llvm/MC/MCStreamer.h"
  23. #include "llvm/MC/MCSubtargetInfo.h"
  24. #include "llvm/MC/MCSymbolELF.h"
  25. #include "llvm/MC/TargetRegistry.h"
  26. #include "llvm/Support/SourceMgr.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. using namespace llvm;
  29. DEFINE_PPC_REGCLASSES;
  30. // Evaluate an expression containing condition register
  31. // or condition register field symbols. Returns positive
  32. // value on success, or -1 on error.
  33. static int64_t
  34. EvaluateCRExpr(const MCExpr *E) {
  35. switch (E->getKind()) {
  36. case MCExpr::Target:
  37. return -1;
  38. case MCExpr::Constant: {
  39. int64_t Res = cast<MCConstantExpr>(E)->getValue();
  40. return Res < 0 ? -1 : Res;
  41. }
  42. case MCExpr::SymbolRef: {
  43. const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
  44. StringRef Name = SRE->getSymbol().getName();
  45. if (Name == "lt") return 0;
  46. if (Name == "gt") return 1;
  47. if (Name == "eq") return 2;
  48. if (Name == "so") return 3;
  49. if (Name == "un") return 3;
  50. if (Name == "cr0") return 0;
  51. if (Name == "cr1") return 1;
  52. if (Name == "cr2") return 2;
  53. if (Name == "cr3") return 3;
  54. if (Name == "cr4") return 4;
  55. if (Name == "cr5") return 5;
  56. if (Name == "cr6") return 6;
  57. if (Name == "cr7") return 7;
  58. return -1;
  59. }
  60. case MCExpr::Unary:
  61. return -1;
  62. case MCExpr::Binary: {
  63. const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
  64. int64_t LHSVal = EvaluateCRExpr(BE->getLHS());
  65. int64_t RHSVal = EvaluateCRExpr(BE->getRHS());
  66. int64_t Res;
  67. if (LHSVal < 0 || RHSVal < 0)
  68. return -1;
  69. switch (BE->getOpcode()) {
  70. default: return -1;
  71. case MCBinaryExpr::Add: Res = LHSVal + RHSVal; break;
  72. case MCBinaryExpr::Mul: Res = LHSVal * RHSVal; break;
  73. }
  74. return Res < 0 ? -1 : Res;
  75. }
  76. }
  77. llvm_unreachable("Invalid expression kind!");
  78. }
  79. namespace {
  80. struct PPCOperand;
  81. class PPCAsmParser : public MCTargetAsmParser {
  82. bool IsPPC64;
  83. void Warning(SMLoc L, const Twine &Msg) { getParser().Warning(L, Msg); }
  84. bool isPPC64() const { return IsPPC64; }
  85. bool MatchRegisterName(unsigned &RegNo, int64_t &IntVal);
  86. bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
  87. OperandMatchResultTy tryParseRegister(unsigned &RegNo, SMLoc &StartLoc,
  88. SMLoc &EndLoc) override;
  89. const MCExpr *ExtractModifierFromExpr(const MCExpr *E,
  90. PPCMCExpr::VariantKind &Variant);
  91. const MCExpr *FixupVariantKind(const MCExpr *E);
  92. bool ParseExpression(const MCExpr *&EVal);
  93. bool ParseOperand(OperandVector &Operands);
  94. bool ParseDirectiveWord(unsigned Size, AsmToken ID);
  95. bool ParseDirectiveTC(unsigned Size, AsmToken ID);
  96. bool ParseDirectiveMachine(SMLoc L);
  97. bool ParseDirectiveAbiVersion(SMLoc L);
  98. bool ParseDirectiveLocalEntry(SMLoc L);
  99. bool ParseGNUAttribute(SMLoc L);
  100. bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
  101. OperandVector &Operands, MCStreamer &Out,
  102. uint64_t &ErrorInfo,
  103. bool MatchingInlineAsm) override;
  104. void ProcessInstruction(MCInst &Inst, const OperandVector &Ops);
  105. /// @name Auto-generated Match Functions
  106. /// {
  107. #define GET_ASSEMBLER_HEADER
  108. #include "PPCGenAsmMatcher.inc"
  109. /// }
  110. public:
  111. PPCAsmParser(const MCSubtargetInfo &STI, MCAsmParser &,
  112. const MCInstrInfo &MII, const MCTargetOptions &Options)
  113. : MCTargetAsmParser(Options, STI, MII) {
  114. // Check for 64-bit vs. 32-bit pointer mode.
  115. const Triple &TheTriple = STI.getTargetTriple();
  116. IsPPC64 = TheTriple.isPPC64();
  117. // Initialize the set of available features.
  118. setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
  119. }
  120. bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
  121. SMLoc NameLoc, OperandVector &Operands) override;
  122. bool ParseDirective(AsmToken DirectiveID) override;
  123. unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
  124. unsigned Kind) override;
  125. const MCExpr *applyModifierToExpr(const MCExpr *E,
  126. MCSymbolRefExpr::VariantKind,
  127. MCContext &Ctx) override;
  128. };
  129. /// PPCOperand - Instances of this class represent a parsed PowerPC machine
  130. /// instruction.
  131. struct PPCOperand : public MCParsedAsmOperand {
  132. enum KindTy {
  133. Token,
  134. Immediate,
  135. ContextImmediate,
  136. Expression,
  137. TLSRegister
  138. } Kind;
  139. SMLoc StartLoc, EndLoc;
  140. bool IsPPC64;
  141. struct TokOp {
  142. const char *Data;
  143. unsigned Length;
  144. };
  145. struct ImmOp {
  146. int64_t Val;
  147. };
  148. struct ExprOp {
  149. const MCExpr *Val;
  150. int64_t CRVal; // Cached result of EvaluateCRExpr(Val)
  151. };
  152. struct TLSRegOp {
  153. const MCSymbolRefExpr *Sym;
  154. };
  155. union {
  156. struct TokOp Tok;
  157. struct ImmOp Imm;
  158. struct ExprOp Expr;
  159. struct TLSRegOp TLSReg;
  160. };
  161. PPCOperand(KindTy K) : Kind(K) {}
  162. public:
  163. PPCOperand(const PPCOperand &o) : MCParsedAsmOperand() {
  164. Kind = o.Kind;
  165. StartLoc = o.StartLoc;
  166. EndLoc = o.EndLoc;
  167. IsPPC64 = o.IsPPC64;
  168. switch (Kind) {
  169. case Token:
  170. Tok = o.Tok;
  171. break;
  172. case Immediate:
  173. case ContextImmediate:
  174. Imm = o.Imm;
  175. break;
  176. case Expression:
  177. Expr = o.Expr;
  178. break;
  179. case TLSRegister:
  180. TLSReg = o.TLSReg;
  181. break;
  182. }
  183. }
  184. // Disable use of sized deallocation due to overallocation of PPCOperand
  185. // objects in CreateTokenWithStringCopy.
  186. void operator delete(void *p) { ::operator delete(p); }
  187. /// getStartLoc - Get the location of the first token of this operand.
  188. SMLoc getStartLoc() const override { return StartLoc; }
  189. /// getEndLoc - Get the location of the last token of this operand.
  190. SMLoc getEndLoc() const override { return EndLoc; }
  191. /// getLocRange - Get the range between the first and last token of this
  192. /// operand.
  193. SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
  194. /// isPPC64 - True if this operand is for an instruction in 64-bit mode.
  195. bool isPPC64() const { return IsPPC64; }
  196. int64_t getImm() const {
  197. assert(Kind == Immediate && "Invalid access!");
  198. return Imm.Val;
  199. }
  200. int64_t getImmS16Context() const {
  201. assert((Kind == Immediate || Kind == ContextImmediate) &&
  202. "Invalid access!");
  203. if (Kind == Immediate)
  204. return Imm.Val;
  205. return static_cast<int16_t>(Imm.Val);
  206. }
  207. int64_t getImmU16Context() const {
  208. assert((Kind == Immediate || Kind == ContextImmediate) &&
  209. "Invalid access!");
  210. return Imm.Val;
  211. }
  212. const MCExpr *getExpr() const {
  213. assert(Kind == Expression && "Invalid access!");
  214. return Expr.Val;
  215. }
  216. int64_t getExprCRVal() const {
  217. assert(Kind == Expression && "Invalid access!");
  218. return Expr.CRVal;
  219. }
  220. const MCExpr *getTLSReg() const {
  221. assert(Kind == TLSRegister && "Invalid access!");
  222. return TLSReg.Sym;
  223. }
  224. unsigned getReg() const override {
  225. assert(isRegNumber() && "Invalid access!");
  226. return (unsigned) Imm.Val;
  227. }
  228. unsigned getVSReg() const {
  229. assert(isVSRegNumber() && "Invalid access!");
  230. return (unsigned) Imm.Val;
  231. }
  232. unsigned getACCReg() const {
  233. assert(isACCRegNumber() && "Invalid access!");
  234. return (unsigned) Imm.Val;
  235. }
  236. unsigned getVSRpEvenReg() const {
  237. assert(isVSRpEvenRegNumber() && "Invalid access!");
  238. return (unsigned) Imm.Val >> 1;
  239. }
  240. unsigned getG8pReg() const {
  241. assert(isEvenRegNumber() && "Invalid access!");
  242. return (unsigned)Imm.Val;
  243. }
  244. unsigned getCCReg() const {
  245. assert(isCCRegNumber() && "Invalid access!");
  246. return (unsigned) (Kind == Immediate ? Imm.Val : Expr.CRVal);
  247. }
  248. unsigned getCRBit() const {
  249. assert(isCRBitNumber() && "Invalid access!");
  250. return (unsigned) (Kind == Immediate ? Imm.Val : Expr.CRVal);
  251. }
  252. unsigned getCRBitMask() const {
  253. assert(isCRBitMask() && "Invalid access!");
  254. return 7 - countTrailingZeros<uint64_t>(Imm.Val);
  255. }
  256. bool isToken() const override { return Kind == Token; }
  257. bool isImm() const override {
  258. return Kind == Immediate || Kind == Expression;
  259. }
  260. bool isU1Imm() const { return Kind == Immediate && isUInt<1>(getImm()); }
  261. bool isU2Imm() const { return Kind == Immediate && isUInt<2>(getImm()); }
  262. bool isU3Imm() const { return Kind == Immediate && isUInt<3>(getImm()); }
  263. bool isU4Imm() const { return Kind == Immediate && isUInt<4>(getImm()); }
  264. bool isU5Imm() const { return Kind == Immediate && isUInt<5>(getImm()); }
  265. bool isS5Imm() const { return Kind == Immediate && isInt<5>(getImm()); }
  266. bool isU6Imm() const { return Kind == Immediate && isUInt<6>(getImm()); }
  267. bool isU6ImmX2() const { return Kind == Immediate &&
  268. isUInt<6>(getImm()) &&
  269. (getImm() & 1) == 0; }
  270. bool isU7Imm() const { return Kind == Immediate && isUInt<7>(getImm()); }
  271. bool isU7ImmX4() const { return Kind == Immediate &&
  272. isUInt<7>(getImm()) &&
  273. (getImm() & 3) == 0; }
  274. bool isU8Imm() const { return Kind == Immediate && isUInt<8>(getImm()); }
  275. bool isU8ImmX8() const { return Kind == Immediate &&
  276. isUInt<8>(getImm()) &&
  277. (getImm() & 7) == 0; }
  278. bool isU10Imm() const { return Kind == Immediate && isUInt<10>(getImm()); }
  279. bool isU12Imm() const { return Kind == Immediate && isUInt<12>(getImm()); }
  280. bool isU16Imm() const { return isExtImm<16>(/*Signed*/ false, 1); }
  281. bool isS16Imm() const { return isExtImm<16>(/*Signed*/ true, 1); }
  282. bool isS16ImmX4() const { return isExtImm<16>(/*Signed*/ true, 4); }
  283. bool isS16ImmX16() const { return isExtImm<16>(/*Signed*/ true, 16); }
  284. bool isS17Imm() const { return isExtImm<17>(/*Signed*/ true, 1); }
  285. bool isHashImmX8() const {
  286. // The Hash Imm form is used for instructions that check or store a hash.
  287. // These instructions have a small immediate range that spans between
  288. // -8 and -512.
  289. return (Kind == Immediate && getImm() <= -8 && getImm() >= -512 &&
  290. (getImm() & 7) == 0);
  291. }
  292. bool isS34ImmX16() const {
  293. return Kind == Expression ||
  294. (Kind == Immediate && isInt<34>(getImm()) && (getImm() & 15) == 0);
  295. }
  296. bool isS34Imm() const {
  297. // Once the PC-Rel ABI is finalized, evaluate whether a 34-bit
  298. // ContextImmediate is needed.
  299. return Kind == Expression || (Kind == Immediate && isInt<34>(getImm()));
  300. }
  301. bool isTLSReg() const { return Kind == TLSRegister; }
  302. bool isDirectBr() const {
  303. if (Kind == Expression)
  304. return true;
  305. if (Kind != Immediate)
  306. return false;
  307. // Operand must be 64-bit aligned, signed 27-bit immediate.
  308. if ((getImm() & 3) != 0)
  309. return false;
  310. if (isInt<26>(getImm()))
  311. return true;
  312. if (!IsPPC64) {
  313. // In 32-bit mode, large 32-bit quantities wrap around.
  314. if (isUInt<32>(getImm()) && isInt<26>(static_cast<int32_t>(getImm())))
  315. return true;
  316. }
  317. return false;
  318. }
  319. bool isCondBr() const { return Kind == Expression ||
  320. (Kind == Immediate && isInt<16>(getImm()) &&
  321. (getImm() & 3) == 0); }
  322. bool isImmZero() const { return Kind == Immediate && getImm() == 0; }
  323. bool isRegNumber() const { return Kind == Immediate && isUInt<5>(getImm()); }
  324. bool isACCRegNumber() const {
  325. return Kind == Immediate && isUInt<3>(getImm());
  326. }
  327. bool isVSRpEvenRegNumber() const {
  328. return Kind == Immediate && isUInt<6>(getImm()) && ((getImm() & 1) == 0);
  329. }
  330. bool isVSRegNumber() const {
  331. return Kind == Immediate && isUInt<6>(getImm());
  332. }
  333. bool isCCRegNumber() const { return (Kind == Expression
  334. && isUInt<3>(getExprCRVal())) ||
  335. (Kind == Immediate
  336. && isUInt<3>(getImm())); }
  337. bool isCRBitNumber() const { return (Kind == Expression
  338. && isUInt<5>(getExprCRVal())) ||
  339. (Kind == Immediate
  340. && isUInt<5>(getImm())); }
  341. bool isEvenRegNumber() const { return isRegNumber() && (getImm() & 1) == 0; }
  342. bool isCRBitMask() const { return Kind == Immediate && isUInt<8>(getImm()) &&
  343. isPowerOf2_32(getImm()); }
  344. bool isATBitsAsHint() const { return false; }
  345. bool isMem() const override { return false; }
  346. bool isReg() const override { return false; }
  347. void addRegOperands(MCInst &Inst, unsigned N) const {
  348. llvm_unreachable("addRegOperands");
  349. }
  350. void addRegGPRCOperands(MCInst &Inst, unsigned N) const {
  351. assert(N == 1 && "Invalid number of operands!");
  352. Inst.addOperand(MCOperand::createReg(RRegs[getReg()]));
  353. }
  354. void addRegGPRCNoR0Operands(MCInst &Inst, unsigned N) const {
  355. assert(N == 1 && "Invalid number of operands!");
  356. Inst.addOperand(MCOperand::createReg(RRegsNoR0[getReg()]));
  357. }
  358. void addRegG8RCOperands(MCInst &Inst, unsigned N) const {
  359. assert(N == 1 && "Invalid number of operands!");
  360. Inst.addOperand(MCOperand::createReg(XRegs[getReg()]));
  361. }
  362. void addRegG8RCNoX0Operands(MCInst &Inst, unsigned N) const {
  363. assert(N == 1 && "Invalid number of operands!");
  364. Inst.addOperand(MCOperand::createReg(XRegsNoX0[getReg()]));
  365. }
  366. void addRegG8pRCOperands(MCInst &Inst, unsigned N) const {
  367. assert(N == 1 && "Invalid number of operands!");
  368. Inst.addOperand(MCOperand::createReg(XRegs[getG8pReg()]));
  369. }
  370. void addRegGxRCOperands(MCInst &Inst, unsigned N) const {
  371. if (isPPC64())
  372. addRegG8RCOperands(Inst, N);
  373. else
  374. addRegGPRCOperands(Inst, N);
  375. }
  376. void addRegGxRCNoR0Operands(MCInst &Inst, unsigned N) const {
  377. if (isPPC64())
  378. addRegG8RCNoX0Operands(Inst, N);
  379. else
  380. addRegGPRCNoR0Operands(Inst, N);
  381. }
  382. void addRegF4RCOperands(MCInst &Inst, unsigned N) const {
  383. assert(N == 1 && "Invalid number of operands!");
  384. Inst.addOperand(MCOperand::createReg(FRegs[getReg()]));
  385. }
  386. void addRegF8RCOperands(MCInst &Inst, unsigned N) const {
  387. assert(N == 1 && "Invalid number of operands!");
  388. Inst.addOperand(MCOperand::createReg(FRegs[getReg()]));
  389. }
  390. void addRegVFRCOperands(MCInst &Inst, unsigned N) const {
  391. assert(N == 1 && "Invalid number of operands!");
  392. Inst.addOperand(MCOperand::createReg(VFRegs[getReg()]));
  393. }
  394. void addRegVRRCOperands(MCInst &Inst, unsigned N) const {
  395. assert(N == 1 && "Invalid number of operands!");
  396. Inst.addOperand(MCOperand::createReg(VRegs[getReg()]));
  397. }
  398. void addRegVSRCOperands(MCInst &Inst, unsigned N) const {
  399. assert(N == 1 && "Invalid number of operands!");
  400. Inst.addOperand(MCOperand::createReg(VSRegs[getVSReg()]));
  401. }
  402. void addRegVSFRCOperands(MCInst &Inst, unsigned N) const {
  403. assert(N == 1 && "Invalid number of operands!");
  404. Inst.addOperand(MCOperand::createReg(VSFRegs[getVSReg()]));
  405. }
  406. void addRegVSSRCOperands(MCInst &Inst, unsigned N) const {
  407. assert(N == 1 && "Invalid number of operands!");
  408. Inst.addOperand(MCOperand::createReg(VSSRegs[getVSReg()]));
  409. }
  410. void addRegSPE4RCOperands(MCInst &Inst, unsigned N) const {
  411. assert(N == 1 && "Invalid number of operands!");
  412. Inst.addOperand(MCOperand::createReg(RRegs[getReg()]));
  413. }
  414. void addRegSPERCOperands(MCInst &Inst, unsigned N) const {
  415. assert(N == 1 && "Invalid number of operands!");
  416. Inst.addOperand(MCOperand::createReg(SPERegs[getReg()]));
  417. }
  418. void addRegACCRCOperands(MCInst &Inst, unsigned N) const {
  419. assert(N == 1 && "Invalid number of operands!");
  420. Inst.addOperand(MCOperand::createReg(ACCRegs[getACCReg()]));
  421. }
  422. void addRegVSRpRCOperands(MCInst &Inst, unsigned N) const {
  423. assert(N == 1 && "Invalid number of operands!");
  424. Inst.addOperand(MCOperand::createReg(VSRpRegs[getVSRpEvenReg()]));
  425. }
  426. void addRegVSRpEvenRCOperands(MCInst &Inst, unsigned N) const {
  427. assert(N == 1 && "Invalid number of operands!");
  428. Inst.addOperand(MCOperand::createReg(VSRpRegs[getVSRpEvenReg()]));
  429. }
  430. void addRegCRBITRCOperands(MCInst &Inst, unsigned N) const {
  431. assert(N == 1 && "Invalid number of operands!");
  432. Inst.addOperand(MCOperand::createReg(CRBITRegs[getCRBit()]));
  433. }
  434. void addRegCRRCOperands(MCInst &Inst, unsigned N) const {
  435. assert(N == 1 && "Invalid number of operands!");
  436. Inst.addOperand(MCOperand::createReg(CRRegs[getCCReg()]));
  437. }
  438. void addCRBitMaskOperands(MCInst &Inst, unsigned N) const {
  439. assert(N == 1 && "Invalid number of operands!");
  440. Inst.addOperand(MCOperand::createReg(CRRegs[getCRBitMask()]));
  441. }
  442. void addImmOperands(MCInst &Inst, unsigned N) const {
  443. assert(N == 1 && "Invalid number of operands!");
  444. if (Kind == Immediate)
  445. Inst.addOperand(MCOperand::createImm(getImm()));
  446. else
  447. Inst.addOperand(MCOperand::createExpr(getExpr()));
  448. }
  449. void addS16ImmOperands(MCInst &Inst, unsigned N) const {
  450. assert(N == 1 && "Invalid number of operands!");
  451. switch (Kind) {
  452. case Immediate:
  453. Inst.addOperand(MCOperand::createImm(getImm()));
  454. break;
  455. case ContextImmediate:
  456. Inst.addOperand(MCOperand::createImm(getImmS16Context()));
  457. break;
  458. default:
  459. Inst.addOperand(MCOperand::createExpr(getExpr()));
  460. break;
  461. }
  462. }
  463. void addU16ImmOperands(MCInst &Inst, unsigned N) const {
  464. assert(N == 1 && "Invalid number of operands!");
  465. switch (Kind) {
  466. case Immediate:
  467. Inst.addOperand(MCOperand::createImm(getImm()));
  468. break;
  469. case ContextImmediate:
  470. Inst.addOperand(MCOperand::createImm(getImmU16Context()));
  471. break;
  472. default:
  473. Inst.addOperand(MCOperand::createExpr(getExpr()));
  474. break;
  475. }
  476. }
  477. void addBranchTargetOperands(MCInst &Inst, unsigned N) const {
  478. assert(N == 1 && "Invalid number of operands!");
  479. if (Kind == Immediate)
  480. Inst.addOperand(MCOperand::createImm(getImm() / 4));
  481. else
  482. Inst.addOperand(MCOperand::createExpr(getExpr()));
  483. }
  484. void addTLSRegOperands(MCInst &Inst, unsigned N) const {
  485. assert(N == 1 && "Invalid number of operands!");
  486. Inst.addOperand(MCOperand::createExpr(getTLSReg()));
  487. }
  488. StringRef getToken() const {
  489. assert(Kind == Token && "Invalid access!");
  490. return StringRef(Tok.Data, Tok.Length);
  491. }
  492. void print(raw_ostream &OS) const override;
  493. static std::unique_ptr<PPCOperand> CreateToken(StringRef Str, SMLoc S,
  494. bool IsPPC64) {
  495. auto Op = std::make_unique<PPCOperand>(Token);
  496. Op->Tok.Data = Str.data();
  497. Op->Tok.Length = Str.size();
  498. Op->StartLoc = S;
  499. Op->EndLoc = S;
  500. Op->IsPPC64 = IsPPC64;
  501. return Op;
  502. }
  503. static std::unique_ptr<PPCOperand>
  504. CreateTokenWithStringCopy(StringRef Str, SMLoc S, bool IsPPC64) {
  505. // Allocate extra memory for the string and copy it.
  506. // FIXME: This is incorrect, Operands are owned by unique_ptr with a default
  507. // deleter which will destroy them by simply using "delete", not correctly
  508. // calling operator delete on this extra memory after calling the dtor
  509. // explicitly.
  510. void *Mem = ::operator new(sizeof(PPCOperand) + Str.size());
  511. std::unique_ptr<PPCOperand> Op(new (Mem) PPCOperand(Token));
  512. Op->Tok.Data = reinterpret_cast<const char *>(Op.get() + 1);
  513. Op->Tok.Length = Str.size();
  514. std::memcpy(const_cast<char *>(Op->Tok.Data), Str.data(), Str.size());
  515. Op->StartLoc = S;
  516. Op->EndLoc = S;
  517. Op->IsPPC64 = IsPPC64;
  518. return Op;
  519. }
  520. static std::unique_ptr<PPCOperand> CreateImm(int64_t Val, SMLoc S, SMLoc E,
  521. bool IsPPC64) {
  522. auto Op = std::make_unique<PPCOperand>(Immediate);
  523. Op->Imm.Val = Val;
  524. Op->StartLoc = S;
  525. Op->EndLoc = E;
  526. Op->IsPPC64 = IsPPC64;
  527. return Op;
  528. }
  529. static std::unique_ptr<PPCOperand> CreateExpr(const MCExpr *Val, SMLoc S,
  530. SMLoc E, bool IsPPC64) {
  531. auto Op = std::make_unique<PPCOperand>(Expression);
  532. Op->Expr.Val = Val;
  533. Op->Expr.CRVal = EvaluateCRExpr(Val);
  534. Op->StartLoc = S;
  535. Op->EndLoc = E;
  536. Op->IsPPC64 = IsPPC64;
  537. return Op;
  538. }
  539. static std::unique_ptr<PPCOperand>
  540. CreateTLSReg(const MCSymbolRefExpr *Sym, SMLoc S, SMLoc E, bool IsPPC64) {
  541. auto Op = std::make_unique<PPCOperand>(TLSRegister);
  542. Op->TLSReg.Sym = Sym;
  543. Op->StartLoc = S;
  544. Op->EndLoc = E;
  545. Op->IsPPC64 = IsPPC64;
  546. return Op;
  547. }
  548. static std::unique_ptr<PPCOperand>
  549. CreateContextImm(int64_t Val, SMLoc S, SMLoc E, bool IsPPC64) {
  550. auto Op = std::make_unique<PPCOperand>(ContextImmediate);
  551. Op->Imm.Val = Val;
  552. Op->StartLoc = S;
  553. Op->EndLoc = E;
  554. Op->IsPPC64 = IsPPC64;
  555. return Op;
  556. }
  557. static std::unique_ptr<PPCOperand>
  558. CreateFromMCExpr(const MCExpr *Val, SMLoc S, SMLoc E, bool IsPPC64) {
  559. if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Val))
  560. return CreateImm(CE->getValue(), S, E, IsPPC64);
  561. if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Val))
  562. if (SRE->getKind() == MCSymbolRefExpr::VK_PPC_TLS ||
  563. SRE->getKind() == MCSymbolRefExpr::VK_PPC_TLS_PCREL)
  564. return CreateTLSReg(SRE, S, E, IsPPC64);
  565. if (const PPCMCExpr *TE = dyn_cast<PPCMCExpr>(Val)) {
  566. int64_t Res;
  567. if (TE->evaluateAsConstant(Res))
  568. return CreateContextImm(Res, S, E, IsPPC64);
  569. }
  570. return CreateExpr(Val, S, E, IsPPC64);
  571. }
  572. private:
  573. template <unsigned Width>
  574. bool isExtImm(bool Signed, unsigned Multiple) const {
  575. switch (Kind) {
  576. default:
  577. return false;
  578. case Expression:
  579. return true;
  580. case Immediate:
  581. case ContextImmediate:
  582. if (Signed)
  583. return isInt<Width>(getImmS16Context()) &&
  584. (getImmS16Context() & (Multiple - 1)) == 0;
  585. else
  586. return isUInt<Width>(getImmU16Context()) &&
  587. (getImmU16Context() & (Multiple - 1)) == 0;
  588. }
  589. }
  590. };
  591. } // end anonymous namespace.
  592. void PPCOperand::print(raw_ostream &OS) const {
  593. switch (Kind) {
  594. case Token:
  595. OS << "'" << getToken() << "'";
  596. break;
  597. case Immediate:
  598. case ContextImmediate:
  599. OS << getImm();
  600. break;
  601. case Expression:
  602. OS << *getExpr();
  603. break;
  604. case TLSRegister:
  605. OS << *getTLSReg();
  606. break;
  607. }
  608. }
  609. static void
  610. addNegOperand(MCInst &Inst, MCOperand &Op, MCContext &Ctx) {
  611. if (Op.isImm()) {
  612. Inst.addOperand(MCOperand::createImm(-Op.getImm()));
  613. return;
  614. }
  615. const MCExpr *Expr = Op.getExpr();
  616. if (const MCUnaryExpr *UnExpr = dyn_cast<MCUnaryExpr>(Expr)) {
  617. if (UnExpr->getOpcode() == MCUnaryExpr::Minus) {
  618. Inst.addOperand(MCOperand::createExpr(UnExpr->getSubExpr()));
  619. return;
  620. }
  621. } else if (const MCBinaryExpr *BinExpr = dyn_cast<MCBinaryExpr>(Expr)) {
  622. if (BinExpr->getOpcode() == MCBinaryExpr::Sub) {
  623. const MCExpr *NE = MCBinaryExpr::createSub(BinExpr->getRHS(),
  624. BinExpr->getLHS(), Ctx);
  625. Inst.addOperand(MCOperand::createExpr(NE));
  626. return;
  627. }
  628. }
  629. Inst.addOperand(MCOperand::createExpr(MCUnaryExpr::createMinus(Expr, Ctx)));
  630. }
  631. void PPCAsmParser::ProcessInstruction(MCInst &Inst,
  632. const OperandVector &Operands) {
  633. int Opcode = Inst.getOpcode();
  634. switch (Opcode) {
  635. case PPC::DCBTx:
  636. case PPC::DCBTT:
  637. case PPC::DCBTSTx:
  638. case PPC::DCBTSTT: {
  639. MCInst TmpInst;
  640. TmpInst.setOpcode((Opcode == PPC::DCBTx || Opcode == PPC::DCBTT) ?
  641. PPC::DCBT : PPC::DCBTST);
  642. TmpInst.addOperand(MCOperand::createImm(
  643. (Opcode == PPC::DCBTx || Opcode == PPC::DCBTSTx) ? 0 : 16));
  644. TmpInst.addOperand(Inst.getOperand(0));
  645. TmpInst.addOperand(Inst.getOperand(1));
  646. Inst = TmpInst;
  647. break;
  648. }
  649. case PPC::DCBTCT:
  650. case PPC::DCBTDS: {
  651. MCInst TmpInst;
  652. TmpInst.setOpcode(PPC::DCBT);
  653. TmpInst.addOperand(Inst.getOperand(2));
  654. TmpInst.addOperand(Inst.getOperand(0));
  655. TmpInst.addOperand(Inst.getOperand(1));
  656. Inst = TmpInst;
  657. break;
  658. }
  659. case PPC::DCBTSTCT:
  660. case PPC::DCBTSTDS: {
  661. MCInst TmpInst;
  662. TmpInst.setOpcode(PPC::DCBTST);
  663. TmpInst.addOperand(Inst.getOperand(2));
  664. TmpInst.addOperand(Inst.getOperand(0));
  665. TmpInst.addOperand(Inst.getOperand(1));
  666. Inst = TmpInst;
  667. break;
  668. }
  669. case PPC::DCBFx:
  670. case PPC::DCBFL:
  671. case PPC::DCBFLP:
  672. case PPC::DCBFPS:
  673. case PPC::DCBSTPS: {
  674. int L = 0;
  675. if (Opcode == PPC::DCBFL)
  676. L = 1;
  677. else if (Opcode == PPC::DCBFLP)
  678. L = 3;
  679. else if (Opcode == PPC::DCBFPS)
  680. L = 4;
  681. else if (Opcode == PPC::DCBSTPS)
  682. L = 6;
  683. MCInst TmpInst;
  684. TmpInst.setOpcode(PPC::DCBF);
  685. TmpInst.addOperand(MCOperand::createImm(L));
  686. TmpInst.addOperand(Inst.getOperand(0));
  687. TmpInst.addOperand(Inst.getOperand(1));
  688. Inst = TmpInst;
  689. break;
  690. }
  691. case PPC::LAx: {
  692. MCInst TmpInst;
  693. TmpInst.setOpcode(PPC::LA);
  694. TmpInst.addOperand(Inst.getOperand(0));
  695. TmpInst.addOperand(Inst.getOperand(2));
  696. TmpInst.addOperand(Inst.getOperand(1));
  697. Inst = TmpInst;
  698. break;
  699. }
  700. case PPC::SUBI: {
  701. MCInst TmpInst;
  702. TmpInst.setOpcode(PPC::ADDI);
  703. TmpInst.addOperand(Inst.getOperand(0));
  704. TmpInst.addOperand(Inst.getOperand(1));
  705. addNegOperand(TmpInst, Inst.getOperand(2), getContext());
  706. Inst = TmpInst;
  707. break;
  708. }
  709. case PPC::SUBIS: {
  710. MCInst TmpInst;
  711. TmpInst.setOpcode(PPC::ADDIS);
  712. TmpInst.addOperand(Inst.getOperand(0));
  713. TmpInst.addOperand(Inst.getOperand(1));
  714. addNegOperand(TmpInst, Inst.getOperand(2), getContext());
  715. Inst = TmpInst;
  716. break;
  717. }
  718. case PPC::SUBIC: {
  719. MCInst TmpInst;
  720. TmpInst.setOpcode(PPC::ADDIC);
  721. TmpInst.addOperand(Inst.getOperand(0));
  722. TmpInst.addOperand(Inst.getOperand(1));
  723. addNegOperand(TmpInst, Inst.getOperand(2), getContext());
  724. Inst = TmpInst;
  725. break;
  726. }
  727. case PPC::SUBIC_rec: {
  728. MCInst TmpInst;
  729. TmpInst.setOpcode(PPC::ADDIC_rec);
  730. TmpInst.addOperand(Inst.getOperand(0));
  731. TmpInst.addOperand(Inst.getOperand(1));
  732. addNegOperand(TmpInst, Inst.getOperand(2), getContext());
  733. Inst = TmpInst;
  734. break;
  735. }
  736. case PPC::EXTLWI:
  737. case PPC::EXTLWI_rec: {
  738. MCInst TmpInst;
  739. int64_t N = Inst.getOperand(2).getImm();
  740. int64_t B = Inst.getOperand(3).getImm();
  741. TmpInst.setOpcode(Opcode == PPC::EXTLWI ? PPC::RLWINM : PPC::RLWINM_rec);
  742. TmpInst.addOperand(Inst.getOperand(0));
  743. TmpInst.addOperand(Inst.getOperand(1));
  744. TmpInst.addOperand(MCOperand::createImm(B));
  745. TmpInst.addOperand(MCOperand::createImm(0));
  746. TmpInst.addOperand(MCOperand::createImm(N - 1));
  747. Inst = TmpInst;
  748. break;
  749. }
  750. case PPC::EXTRWI:
  751. case PPC::EXTRWI_rec: {
  752. MCInst TmpInst;
  753. int64_t N = Inst.getOperand(2).getImm();
  754. int64_t B = Inst.getOperand(3).getImm();
  755. TmpInst.setOpcode(Opcode == PPC::EXTRWI ? PPC::RLWINM : PPC::RLWINM_rec);
  756. TmpInst.addOperand(Inst.getOperand(0));
  757. TmpInst.addOperand(Inst.getOperand(1));
  758. TmpInst.addOperand(MCOperand::createImm(B + N));
  759. TmpInst.addOperand(MCOperand::createImm(32 - N));
  760. TmpInst.addOperand(MCOperand::createImm(31));
  761. Inst = TmpInst;
  762. break;
  763. }
  764. case PPC::INSLWI:
  765. case PPC::INSLWI_rec: {
  766. MCInst TmpInst;
  767. int64_t N = Inst.getOperand(2).getImm();
  768. int64_t B = Inst.getOperand(3).getImm();
  769. TmpInst.setOpcode(Opcode == PPC::INSLWI ? PPC::RLWIMI : PPC::RLWIMI_rec);
  770. TmpInst.addOperand(Inst.getOperand(0));
  771. TmpInst.addOperand(Inst.getOperand(0));
  772. TmpInst.addOperand(Inst.getOperand(1));
  773. TmpInst.addOperand(MCOperand::createImm(32 - B));
  774. TmpInst.addOperand(MCOperand::createImm(B));
  775. TmpInst.addOperand(MCOperand::createImm((B + N) - 1));
  776. Inst = TmpInst;
  777. break;
  778. }
  779. case PPC::INSRWI:
  780. case PPC::INSRWI_rec: {
  781. MCInst TmpInst;
  782. int64_t N = Inst.getOperand(2).getImm();
  783. int64_t B = Inst.getOperand(3).getImm();
  784. TmpInst.setOpcode(Opcode == PPC::INSRWI ? PPC::RLWIMI : PPC::RLWIMI_rec);
  785. TmpInst.addOperand(Inst.getOperand(0));
  786. TmpInst.addOperand(Inst.getOperand(0));
  787. TmpInst.addOperand(Inst.getOperand(1));
  788. TmpInst.addOperand(MCOperand::createImm(32 - (B + N)));
  789. TmpInst.addOperand(MCOperand::createImm(B));
  790. TmpInst.addOperand(MCOperand::createImm((B + N) - 1));
  791. Inst = TmpInst;
  792. break;
  793. }
  794. case PPC::ROTRWI:
  795. case PPC::ROTRWI_rec: {
  796. MCInst TmpInst;
  797. int64_t N = Inst.getOperand(2).getImm();
  798. TmpInst.setOpcode(Opcode == PPC::ROTRWI ? PPC::RLWINM : PPC::RLWINM_rec);
  799. TmpInst.addOperand(Inst.getOperand(0));
  800. TmpInst.addOperand(Inst.getOperand(1));
  801. TmpInst.addOperand(MCOperand::createImm(32 - N));
  802. TmpInst.addOperand(MCOperand::createImm(0));
  803. TmpInst.addOperand(MCOperand::createImm(31));
  804. Inst = TmpInst;
  805. break;
  806. }
  807. case PPC::SLWI:
  808. case PPC::SLWI_rec: {
  809. MCInst TmpInst;
  810. int64_t N = Inst.getOperand(2).getImm();
  811. TmpInst.setOpcode(Opcode == PPC::SLWI ? PPC::RLWINM : PPC::RLWINM_rec);
  812. TmpInst.addOperand(Inst.getOperand(0));
  813. TmpInst.addOperand(Inst.getOperand(1));
  814. TmpInst.addOperand(MCOperand::createImm(N));
  815. TmpInst.addOperand(MCOperand::createImm(0));
  816. TmpInst.addOperand(MCOperand::createImm(31 - N));
  817. Inst = TmpInst;
  818. break;
  819. }
  820. case PPC::SRWI:
  821. case PPC::SRWI_rec: {
  822. MCInst TmpInst;
  823. int64_t N = Inst.getOperand(2).getImm();
  824. TmpInst.setOpcode(Opcode == PPC::SRWI ? PPC::RLWINM : PPC::RLWINM_rec);
  825. TmpInst.addOperand(Inst.getOperand(0));
  826. TmpInst.addOperand(Inst.getOperand(1));
  827. TmpInst.addOperand(MCOperand::createImm(32 - N));
  828. TmpInst.addOperand(MCOperand::createImm(N));
  829. TmpInst.addOperand(MCOperand::createImm(31));
  830. Inst = TmpInst;
  831. break;
  832. }
  833. case PPC::CLRRWI:
  834. case PPC::CLRRWI_rec: {
  835. MCInst TmpInst;
  836. int64_t N = Inst.getOperand(2).getImm();
  837. TmpInst.setOpcode(Opcode == PPC::CLRRWI ? PPC::RLWINM : PPC::RLWINM_rec);
  838. TmpInst.addOperand(Inst.getOperand(0));
  839. TmpInst.addOperand(Inst.getOperand(1));
  840. TmpInst.addOperand(MCOperand::createImm(0));
  841. TmpInst.addOperand(MCOperand::createImm(0));
  842. TmpInst.addOperand(MCOperand::createImm(31 - N));
  843. Inst = TmpInst;
  844. break;
  845. }
  846. case PPC::CLRLSLWI:
  847. case PPC::CLRLSLWI_rec: {
  848. MCInst TmpInst;
  849. int64_t B = Inst.getOperand(2).getImm();
  850. int64_t N = Inst.getOperand(3).getImm();
  851. TmpInst.setOpcode(Opcode == PPC::CLRLSLWI ? PPC::RLWINM : PPC::RLWINM_rec);
  852. TmpInst.addOperand(Inst.getOperand(0));
  853. TmpInst.addOperand(Inst.getOperand(1));
  854. TmpInst.addOperand(MCOperand::createImm(N));
  855. TmpInst.addOperand(MCOperand::createImm(B - N));
  856. TmpInst.addOperand(MCOperand::createImm(31 - N));
  857. Inst = TmpInst;
  858. break;
  859. }
  860. case PPC::EXTLDI:
  861. case PPC::EXTLDI_rec: {
  862. MCInst TmpInst;
  863. int64_t N = Inst.getOperand(2).getImm();
  864. int64_t B = Inst.getOperand(3).getImm();
  865. TmpInst.setOpcode(Opcode == PPC::EXTLDI ? PPC::RLDICR : PPC::RLDICR_rec);
  866. TmpInst.addOperand(Inst.getOperand(0));
  867. TmpInst.addOperand(Inst.getOperand(1));
  868. TmpInst.addOperand(MCOperand::createImm(B));
  869. TmpInst.addOperand(MCOperand::createImm(N - 1));
  870. Inst = TmpInst;
  871. break;
  872. }
  873. case PPC::EXTRDI:
  874. case PPC::EXTRDI_rec: {
  875. MCInst TmpInst;
  876. int64_t N = Inst.getOperand(2).getImm();
  877. int64_t B = Inst.getOperand(3).getImm();
  878. TmpInst.setOpcode(Opcode == PPC::EXTRDI ? PPC::RLDICL : PPC::RLDICL_rec);
  879. TmpInst.addOperand(Inst.getOperand(0));
  880. TmpInst.addOperand(Inst.getOperand(1));
  881. TmpInst.addOperand(MCOperand::createImm(B + N));
  882. TmpInst.addOperand(MCOperand::createImm(64 - N));
  883. Inst = TmpInst;
  884. break;
  885. }
  886. case PPC::INSRDI:
  887. case PPC::INSRDI_rec: {
  888. MCInst TmpInst;
  889. int64_t N = Inst.getOperand(2).getImm();
  890. int64_t B = Inst.getOperand(3).getImm();
  891. TmpInst.setOpcode(Opcode == PPC::INSRDI ? PPC::RLDIMI : PPC::RLDIMI_rec);
  892. TmpInst.addOperand(Inst.getOperand(0));
  893. TmpInst.addOperand(Inst.getOperand(0));
  894. TmpInst.addOperand(Inst.getOperand(1));
  895. TmpInst.addOperand(MCOperand::createImm(64 - (B + N)));
  896. TmpInst.addOperand(MCOperand::createImm(B));
  897. Inst = TmpInst;
  898. break;
  899. }
  900. case PPC::ROTRDI:
  901. case PPC::ROTRDI_rec: {
  902. MCInst TmpInst;
  903. int64_t N = Inst.getOperand(2).getImm();
  904. TmpInst.setOpcode(Opcode == PPC::ROTRDI ? PPC::RLDICL : PPC::RLDICL_rec);
  905. TmpInst.addOperand(Inst.getOperand(0));
  906. TmpInst.addOperand(Inst.getOperand(1));
  907. TmpInst.addOperand(MCOperand::createImm(64 - N));
  908. TmpInst.addOperand(MCOperand::createImm(0));
  909. Inst = TmpInst;
  910. break;
  911. }
  912. case PPC::SLDI:
  913. case PPC::SLDI_rec: {
  914. MCInst TmpInst;
  915. int64_t N = Inst.getOperand(2).getImm();
  916. TmpInst.setOpcode(Opcode == PPC::SLDI ? PPC::RLDICR : PPC::RLDICR_rec);
  917. TmpInst.addOperand(Inst.getOperand(0));
  918. TmpInst.addOperand(Inst.getOperand(1));
  919. TmpInst.addOperand(MCOperand::createImm(N));
  920. TmpInst.addOperand(MCOperand::createImm(63 - N));
  921. Inst = TmpInst;
  922. break;
  923. }
  924. case PPC::SUBPCIS: {
  925. MCInst TmpInst;
  926. int64_t N = Inst.getOperand(1).getImm();
  927. TmpInst.setOpcode(PPC::ADDPCIS);
  928. TmpInst.addOperand(Inst.getOperand(0));
  929. TmpInst.addOperand(MCOperand::createImm(-N));
  930. Inst = TmpInst;
  931. break;
  932. }
  933. case PPC::SRDI:
  934. case PPC::SRDI_rec: {
  935. MCInst TmpInst;
  936. int64_t N = Inst.getOperand(2).getImm();
  937. TmpInst.setOpcode(Opcode == PPC::SRDI ? PPC::RLDICL : PPC::RLDICL_rec);
  938. TmpInst.addOperand(Inst.getOperand(0));
  939. TmpInst.addOperand(Inst.getOperand(1));
  940. TmpInst.addOperand(MCOperand::createImm(64 - N));
  941. TmpInst.addOperand(MCOperand::createImm(N));
  942. Inst = TmpInst;
  943. break;
  944. }
  945. case PPC::CLRRDI:
  946. case PPC::CLRRDI_rec: {
  947. MCInst TmpInst;
  948. int64_t N = Inst.getOperand(2).getImm();
  949. TmpInst.setOpcode(Opcode == PPC::CLRRDI ? PPC::RLDICR : PPC::RLDICR_rec);
  950. TmpInst.addOperand(Inst.getOperand(0));
  951. TmpInst.addOperand(Inst.getOperand(1));
  952. TmpInst.addOperand(MCOperand::createImm(0));
  953. TmpInst.addOperand(MCOperand::createImm(63 - N));
  954. Inst = TmpInst;
  955. break;
  956. }
  957. case PPC::CLRLSLDI:
  958. case PPC::CLRLSLDI_rec: {
  959. MCInst TmpInst;
  960. int64_t B = Inst.getOperand(2).getImm();
  961. int64_t N = Inst.getOperand(3).getImm();
  962. TmpInst.setOpcode(Opcode == PPC::CLRLSLDI ? PPC::RLDIC : PPC::RLDIC_rec);
  963. TmpInst.addOperand(Inst.getOperand(0));
  964. TmpInst.addOperand(Inst.getOperand(1));
  965. TmpInst.addOperand(MCOperand::createImm(N));
  966. TmpInst.addOperand(MCOperand::createImm(B - N));
  967. Inst = TmpInst;
  968. break;
  969. }
  970. case PPC::RLWINMbm:
  971. case PPC::RLWINMbm_rec: {
  972. unsigned MB, ME;
  973. int64_t BM = Inst.getOperand(3).getImm();
  974. if (!isRunOfOnes(BM, MB, ME))
  975. break;
  976. MCInst TmpInst;
  977. TmpInst.setOpcode(Opcode == PPC::RLWINMbm ? PPC::RLWINM : PPC::RLWINM_rec);
  978. TmpInst.addOperand(Inst.getOperand(0));
  979. TmpInst.addOperand(Inst.getOperand(1));
  980. TmpInst.addOperand(Inst.getOperand(2));
  981. TmpInst.addOperand(MCOperand::createImm(MB));
  982. TmpInst.addOperand(MCOperand::createImm(ME));
  983. Inst = TmpInst;
  984. break;
  985. }
  986. case PPC::RLWIMIbm:
  987. case PPC::RLWIMIbm_rec: {
  988. unsigned MB, ME;
  989. int64_t BM = Inst.getOperand(3).getImm();
  990. if (!isRunOfOnes(BM, MB, ME))
  991. break;
  992. MCInst TmpInst;
  993. TmpInst.setOpcode(Opcode == PPC::RLWIMIbm ? PPC::RLWIMI : PPC::RLWIMI_rec);
  994. TmpInst.addOperand(Inst.getOperand(0));
  995. TmpInst.addOperand(Inst.getOperand(0)); // The tied operand.
  996. TmpInst.addOperand(Inst.getOperand(1));
  997. TmpInst.addOperand(Inst.getOperand(2));
  998. TmpInst.addOperand(MCOperand::createImm(MB));
  999. TmpInst.addOperand(MCOperand::createImm(ME));
  1000. Inst = TmpInst;
  1001. break;
  1002. }
  1003. case PPC::RLWNMbm:
  1004. case PPC::RLWNMbm_rec: {
  1005. unsigned MB, ME;
  1006. int64_t BM = Inst.getOperand(3).getImm();
  1007. if (!isRunOfOnes(BM, MB, ME))
  1008. break;
  1009. MCInst TmpInst;
  1010. TmpInst.setOpcode(Opcode == PPC::RLWNMbm ? PPC::RLWNM : PPC::RLWNM_rec);
  1011. TmpInst.addOperand(Inst.getOperand(0));
  1012. TmpInst.addOperand(Inst.getOperand(1));
  1013. TmpInst.addOperand(Inst.getOperand(2));
  1014. TmpInst.addOperand(MCOperand::createImm(MB));
  1015. TmpInst.addOperand(MCOperand::createImm(ME));
  1016. Inst = TmpInst;
  1017. break;
  1018. }
  1019. case PPC::MFTB: {
  1020. if (getSTI().getFeatureBits()[PPC::FeatureMFTB]) {
  1021. assert(Inst.getNumOperands() == 2 && "Expecting two operands");
  1022. Inst.setOpcode(PPC::MFSPR);
  1023. }
  1024. break;
  1025. }
  1026. }
  1027. }
  1028. static std::string PPCMnemonicSpellCheck(StringRef S, const FeatureBitset &FBS,
  1029. unsigned VariantID = 0);
  1030. bool PPCAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
  1031. OperandVector &Operands,
  1032. MCStreamer &Out, uint64_t &ErrorInfo,
  1033. bool MatchingInlineAsm) {
  1034. MCInst Inst;
  1035. switch (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm)) {
  1036. case Match_Success:
  1037. // Post-process instructions (typically extended mnemonics)
  1038. ProcessInstruction(Inst, Operands);
  1039. Inst.setLoc(IDLoc);
  1040. Out.emitInstruction(Inst, getSTI());
  1041. return false;
  1042. case Match_MissingFeature:
  1043. return Error(IDLoc, "instruction use requires an option to be enabled");
  1044. case Match_MnemonicFail: {
  1045. FeatureBitset FBS = ComputeAvailableFeatures(getSTI().getFeatureBits());
  1046. std::string Suggestion = PPCMnemonicSpellCheck(
  1047. ((PPCOperand &)*Operands[0]).getToken(), FBS);
  1048. return Error(IDLoc, "invalid instruction" + Suggestion,
  1049. ((PPCOperand &)*Operands[0]).getLocRange());
  1050. }
  1051. case Match_InvalidOperand: {
  1052. SMLoc ErrorLoc = IDLoc;
  1053. if (ErrorInfo != ~0ULL) {
  1054. if (ErrorInfo >= Operands.size())
  1055. return Error(IDLoc, "too few operands for instruction");
  1056. ErrorLoc = ((PPCOperand &)*Operands[ErrorInfo]).getStartLoc();
  1057. if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
  1058. }
  1059. return Error(ErrorLoc, "invalid operand for instruction");
  1060. }
  1061. }
  1062. llvm_unreachable("Implement any new match types added!");
  1063. }
  1064. bool PPCAsmParser::MatchRegisterName(unsigned &RegNo, int64_t &IntVal) {
  1065. if (getParser().getTok().is(AsmToken::Percent))
  1066. getParser().Lex(); // Eat the '%'.
  1067. if (!getParser().getTok().is(AsmToken::Identifier))
  1068. return true;
  1069. StringRef Name = getParser().getTok().getString();
  1070. if (Name.equals_insensitive("lr")) {
  1071. RegNo = isPPC64() ? PPC::LR8 : PPC::LR;
  1072. IntVal = 8;
  1073. } else if (Name.equals_insensitive("ctr")) {
  1074. RegNo = isPPC64() ? PPC::CTR8 : PPC::CTR;
  1075. IntVal = 9;
  1076. } else if (Name.equals_insensitive("vrsave")) {
  1077. RegNo = PPC::VRSAVE;
  1078. IntVal = 256;
  1079. } else if (Name.startswith_insensitive("r") &&
  1080. !Name.substr(1).getAsInteger(10, IntVal) && IntVal < 32) {
  1081. RegNo = isPPC64() ? XRegs[IntVal] : RRegs[IntVal];
  1082. } else if (Name.startswith_insensitive("f") &&
  1083. !Name.substr(1).getAsInteger(10, IntVal) && IntVal < 32) {
  1084. RegNo = FRegs[IntVal];
  1085. } else if (Name.startswith_insensitive("vs") &&
  1086. !Name.substr(2).getAsInteger(10, IntVal) && IntVal < 64) {
  1087. RegNo = VSRegs[IntVal];
  1088. } else if (Name.startswith_insensitive("v") &&
  1089. !Name.substr(1).getAsInteger(10, IntVal) && IntVal < 32) {
  1090. RegNo = VRegs[IntVal];
  1091. } else if (Name.startswith_insensitive("cr") &&
  1092. !Name.substr(2).getAsInteger(10, IntVal) && IntVal < 8) {
  1093. RegNo = CRRegs[IntVal];
  1094. } else
  1095. return true;
  1096. getParser().Lex();
  1097. return false;
  1098. }
  1099. bool PPCAsmParser::
  1100. ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
  1101. if (tryParseRegister(RegNo, StartLoc, EndLoc) != MatchOperand_Success)
  1102. return TokError("invalid register name");
  1103. return false;
  1104. }
  1105. OperandMatchResultTy PPCAsmParser::tryParseRegister(unsigned &RegNo,
  1106. SMLoc &StartLoc,
  1107. SMLoc &EndLoc) {
  1108. const AsmToken &Tok = getParser().getTok();
  1109. StartLoc = Tok.getLoc();
  1110. EndLoc = Tok.getEndLoc();
  1111. RegNo = 0;
  1112. int64_t IntVal;
  1113. if (MatchRegisterName(RegNo, IntVal))
  1114. return MatchOperand_NoMatch;
  1115. return MatchOperand_Success;
  1116. }
  1117. /// Extract \code @l/@ha \endcode modifier from expression. Recursively scan
  1118. /// the expression and check for VK_PPC_LO/HI/HA
  1119. /// symbol variants. If all symbols with modifier use the same
  1120. /// variant, return the corresponding PPCMCExpr::VariantKind,
  1121. /// and a modified expression using the default symbol variant.
  1122. /// Otherwise, return NULL.
  1123. const MCExpr *PPCAsmParser::
  1124. ExtractModifierFromExpr(const MCExpr *E,
  1125. PPCMCExpr::VariantKind &Variant) {
  1126. MCContext &Context = getParser().getContext();
  1127. Variant = PPCMCExpr::VK_PPC_None;
  1128. switch (E->getKind()) {
  1129. case MCExpr::Target:
  1130. case MCExpr::Constant:
  1131. return nullptr;
  1132. case MCExpr::SymbolRef: {
  1133. const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
  1134. switch (SRE->getKind()) {
  1135. case MCSymbolRefExpr::VK_PPC_LO:
  1136. Variant = PPCMCExpr::VK_PPC_LO;
  1137. break;
  1138. case MCSymbolRefExpr::VK_PPC_HI:
  1139. Variant = PPCMCExpr::VK_PPC_HI;
  1140. break;
  1141. case MCSymbolRefExpr::VK_PPC_HA:
  1142. Variant = PPCMCExpr::VK_PPC_HA;
  1143. break;
  1144. case MCSymbolRefExpr::VK_PPC_HIGH:
  1145. Variant = PPCMCExpr::VK_PPC_HIGH;
  1146. break;
  1147. case MCSymbolRefExpr::VK_PPC_HIGHA:
  1148. Variant = PPCMCExpr::VK_PPC_HIGHA;
  1149. break;
  1150. case MCSymbolRefExpr::VK_PPC_HIGHER:
  1151. Variant = PPCMCExpr::VK_PPC_HIGHER;
  1152. break;
  1153. case MCSymbolRefExpr::VK_PPC_HIGHERA:
  1154. Variant = PPCMCExpr::VK_PPC_HIGHERA;
  1155. break;
  1156. case MCSymbolRefExpr::VK_PPC_HIGHEST:
  1157. Variant = PPCMCExpr::VK_PPC_HIGHEST;
  1158. break;
  1159. case MCSymbolRefExpr::VK_PPC_HIGHESTA:
  1160. Variant = PPCMCExpr::VK_PPC_HIGHESTA;
  1161. break;
  1162. default:
  1163. return nullptr;
  1164. }
  1165. return MCSymbolRefExpr::create(&SRE->getSymbol(), Context);
  1166. }
  1167. case MCExpr::Unary: {
  1168. const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
  1169. const MCExpr *Sub = ExtractModifierFromExpr(UE->getSubExpr(), Variant);
  1170. if (!Sub)
  1171. return nullptr;
  1172. return MCUnaryExpr::create(UE->getOpcode(), Sub, Context);
  1173. }
  1174. case MCExpr::Binary: {
  1175. const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
  1176. PPCMCExpr::VariantKind LHSVariant, RHSVariant;
  1177. const MCExpr *LHS = ExtractModifierFromExpr(BE->getLHS(), LHSVariant);
  1178. const MCExpr *RHS = ExtractModifierFromExpr(BE->getRHS(), RHSVariant);
  1179. if (!LHS && !RHS)
  1180. return nullptr;
  1181. if (!LHS) LHS = BE->getLHS();
  1182. if (!RHS) RHS = BE->getRHS();
  1183. if (LHSVariant == PPCMCExpr::VK_PPC_None)
  1184. Variant = RHSVariant;
  1185. else if (RHSVariant == PPCMCExpr::VK_PPC_None)
  1186. Variant = LHSVariant;
  1187. else if (LHSVariant == RHSVariant)
  1188. Variant = LHSVariant;
  1189. else
  1190. return nullptr;
  1191. return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, Context);
  1192. }
  1193. }
  1194. llvm_unreachable("Invalid expression kind!");
  1195. }
  1196. /// Find all VK_TLSGD/VK_TLSLD symbol references in expression and replace
  1197. /// them by VK_PPC_TLSGD/VK_PPC_TLSLD. This is necessary to avoid having
  1198. /// _GLOBAL_OFFSET_TABLE_ created via ELFObjectWriter::RelocNeedsGOT.
  1199. /// FIXME: This is a hack.
  1200. const MCExpr *PPCAsmParser::
  1201. FixupVariantKind(const MCExpr *E) {
  1202. MCContext &Context = getParser().getContext();
  1203. switch (E->getKind()) {
  1204. case MCExpr::Target:
  1205. case MCExpr::Constant:
  1206. return E;
  1207. case MCExpr::SymbolRef: {
  1208. const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
  1209. MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
  1210. switch (SRE->getKind()) {
  1211. case MCSymbolRefExpr::VK_TLSGD:
  1212. Variant = MCSymbolRefExpr::VK_PPC_TLSGD;
  1213. break;
  1214. case MCSymbolRefExpr::VK_TLSLD:
  1215. Variant = MCSymbolRefExpr::VK_PPC_TLSLD;
  1216. break;
  1217. default:
  1218. return E;
  1219. }
  1220. return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, Context);
  1221. }
  1222. case MCExpr::Unary: {
  1223. const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
  1224. const MCExpr *Sub = FixupVariantKind(UE->getSubExpr());
  1225. if (Sub == UE->getSubExpr())
  1226. return E;
  1227. return MCUnaryExpr::create(UE->getOpcode(), Sub, Context);
  1228. }
  1229. case MCExpr::Binary: {
  1230. const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
  1231. const MCExpr *LHS = FixupVariantKind(BE->getLHS());
  1232. const MCExpr *RHS = FixupVariantKind(BE->getRHS());
  1233. if (LHS == BE->getLHS() && RHS == BE->getRHS())
  1234. return E;
  1235. return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, Context);
  1236. }
  1237. }
  1238. llvm_unreachable("Invalid expression kind!");
  1239. }
  1240. /// ParseExpression. This differs from the default "parseExpression" in that
  1241. /// it handles modifiers.
  1242. bool PPCAsmParser::
  1243. ParseExpression(const MCExpr *&EVal) {
  1244. // (ELF Platforms)
  1245. // Handle \code @l/@ha \endcode
  1246. if (getParser().parseExpression(EVal))
  1247. return true;
  1248. EVal = FixupVariantKind(EVal);
  1249. PPCMCExpr::VariantKind Variant;
  1250. const MCExpr *E = ExtractModifierFromExpr(EVal, Variant);
  1251. if (E)
  1252. EVal = PPCMCExpr::create(Variant, E, getParser().getContext());
  1253. return false;
  1254. }
  1255. /// ParseOperand
  1256. /// This handles registers in the form 'NN', '%rNN' for ELF platforms and
  1257. /// rNN for MachO.
  1258. bool PPCAsmParser::ParseOperand(OperandVector &Operands) {
  1259. MCAsmParser &Parser = getParser();
  1260. SMLoc S = Parser.getTok().getLoc();
  1261. SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
  1262. const MCExpr *EVal;
  1263. // Attempt to parse the next token as an immediate
  1264. switch (getLexer().getKind()) {
  1265. // Special handling for register names. These are interpreted
  1266. // as immediates corresponding to the register number.
  1267. case AsmToken::Percent: {
  1268. unsigned RegNo;
  1269. int64_t IntVal;
  1270. if (MatchRegisterName(RegNo, IntVal))
  1271. return Error(S, "invalid register name");
  1272. Operands.push_back(PPCOperand::CreateImm(IntVal, S, E, isPPC64()));
  1273. return false;
  1274. }
  1275. case AsmToken::Identifier:
  1276. case AsmToken::LParen:
  1277. case AsmToken::Plus:
  1278. case AsmToken::Minus:
  1279. case AsmToken::Integer:
  1280. case AsmToken::Dot:
  1281. case AsmToken::Dollar:
  1282. case AsmToken::Exclaim:
  1283. case AsmToken::Tilde:
  1284. if (!ParseExpression(EVal))
  1285. break;
  1286. // Fall-through
  1287. LLVM_FALLTHROUGH;
  1288. default:
  1289. return Error(S, "unknown operand");
  1290. }
  1291. // Push the parsed operand into the list of operands
  1292. Operands.push_back(PPCOperand::CreateFromMCExpr(EVal, S, E, isPPC64()));
  1293. // Check whether this is a TLS call expression
  1294. bool TLSCall = false;
  1295. if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(EVal))
  1296. TLSCall = Ref->getSymbol().getName() == "__tls_get_addr";
  1297. if (TLSCall && getLexer().is(AsmToken::LParen)) {
  1298. const MCExpr *TLSSym;
  1299. Parser.Lex(); // Eat the '('.
  1300. S = Parser.getTok().getLoc();
  1301. if (ParseExpression(TLSSym))
  1302. return Error(S, "invalid TLS call expression");
  1303. if (getLexer().isNot(AsmToken::RParen))
  1304. return Error(Parser.getTok().getLoc(), "missing ')'");
  1305. E = Parser.getTok().getLoc();
  1306. Parser.Lex(); // Eat the ')'.
  1307. Operands.push_back(PPCOperand::CreateFromMCExpr(TLSSym, S, E, isPPC64()));
  1308. }
  1309. // Otherwise, check for D-form memory operands
  1310. if (!TLSCall && getLexer().is(AsmToken::LParen)) {
  1311. Parser.Lex(); // Eat the '('.
  1312. S = Parser.getTok().getLoc();
  1313. int64_t IntVal;
  1314. switch (getLexer().getKind()) {
  1315. case AsmToken::Percent: {
  1316. unsigned RegNo;
  1317. if (MatchRegisterName(RegNo, IntVal))
  1318. return Error(S, "invalid register name");
  1319. break;
  1320. }
  1321. case AsmToken::Integer:
  1322. if (getParser().parseAbsoluteExpression(IntVal) || IntVal < 0 ||
  1323. IntVal > 31)
  1324. return Error(S, "invalid register number");
  1325. break;
  1326. case AsmToken::Identifier:
  1327. default:
  1328. return Error(S, "invalid memory operand");
  1329. }
  1330. E = Parser.getTok().getLoc();
  1331. if (parseToken(AsmToken::RParen, "missing ')'"))
  1332. return true;
  1333. Operands.push_back(PPCOperand::CreateImm(IntVal, S, E, isPPC64()));
  1334. }
  1335. return false;
  1336. }
  1337. /// Parse an instruction mnemonic followed by its operands.
  1338. bool PPCAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
  1339. SMLoc NameLoc, OperandVector &Operands) {
  1340. // The first operand is the token for the instruction name.
  1341. // If the next character is a '+' or '-', we need to add it to the
  1342. // instruction name, to match what TableGen is doing.
  1343. std::string NewOpcode;
  1344. if (parseOptionalToken(AsmToken::Plus)) {
  1345. NewOpcode = std::string(Name);
  1346. NewOpcode += '+';
  1347. Name = NewOpcode;
  1348. }
  1349. if (parseOptionalToken(AsmToken::Minus)) {
  1350. NewOpcode = std::string(Name);
  1351. NewOpcode += '-';
  1352. Name = NewOpcode;
  1353. }
  1354. // If the instruction ends in a '.', we need to create a separate
  1355. // token for it, to match what TableGen is doing.
  1356. size_t Dot = Name.find('.');
  1357. StringRef Mnemonic = Name.slice(0, Dot);
  1358. if (!NewOpcode.empty()) // Underlying memory for Name is volatile.
  1359. Operands.push_back(
  1360. PPCOperand::CreateTokenWithStringCopy(Mnemonic, NameLoc, isPPC64()));
  1361. else
  1362. Operands.push_back(PPCOperand::CreateToken(Mnemonic, NameLoc, isPPC64()));
  1363. if (Dot != StringRef::npos) {
  1364. SMLoc DotLoc = SMLoc::getFromPointer(NameLoc.getPointer() + Dot);
  1365. StringRef DotStr = Name.slice(Dot, StringRef::npos);
  1366. if (!NewOpcode.empty()) // Underlying memory for Name is volatile.
  1367. Operands.push_back(
  1368. PPCOperand::CreateTokenWithStringCopy(DotStr, DotLoc, isPPC64()));
  1369. else
  1370. Operands.push_back(PPCOperand::CreateToken(DotStr, DotLoc, isPPC64()));
  1371. }
  1372. // If there are no more operands then finish
  1373. if (parseOptionalToken(AsmToken::EndOfStatement))
  1374. return false;
  1375. // Parse the first operand
  1376. if (ParseOperand(Operands))
  1377. return true;
  1378. while (!parseOptionalToken(AsmToken::EndOfStatement)) {
  1379. if (parseToken(AsmToken::Comma) || ParseOperand(Operands))
  1380. return true;
  1381. }
  1382. // We'll now deal with an unfortunate special case: the syntax for the dcbt
  1383. // and dcbtst instructions differs for server vs. embedded cores.
  1384. // The syntax for dcbt is:
  1385. // dcbt ra, rb, th [server]
  1386. // dcbt th, ra, rb [embedded]
  1387. // where th can be omitted when it is 0. dcbtst is the same. We take the
  1388. // server form to be the default, so swap the operands if we're parsing for
  1389. // an embedded core (they'll be swapped again upon printing).
  1390. if (getSTI().getFeatureBits()[PPC::FeatureBookE] &&
  1391. Operands.size() == 4 &&
  1392. (Name == "dcbt" || Name == "dcbtst")) {
  1393. std::swap(Operands[1], Operands[3]);
  1394. std::swap(Operands[2], Operands[1]);
  1395. }
  1396. // Handle base mnemonic for atomic loads where the EH bit is zero.
  1397. if (Name == "lqarx" || Name == "ldarx" || Name == "lwarx" ||
  1398. Name == "lharx" || Name == "lbarx") {
  1399. if (Operands.size() != 5)
  1400. return false;
  1401. PPCOperand &EHOp = (PPCOperand &)*Operands[4];
  1402. if (EHOp.isU1Imm() && EHOp.getImm() == 0)
  1403. Operands.pop_back();
  1404. }
  1405. return false;
  1406. }
  1407. /// ParseDirective parses the PPC specific directives
  1408. bool PPCAsmParser::ParseDirective(AsmToken DirectiveID) {
  1409. StringRef IDVal = DirectiveID.getIdentifier();
  1410. if (IDVal == ".word")
  1411. ParseDirectiveWord(2, DirectiveID);
  1412. else if (IDVal == ".llong")
  1413. ParseDirectiveWord(8, DirectiveID);
  1414. else if (IDVal == ".tc")
  1415. ParseDirectiveTC(isPPC64() ? 8 : 4, DirectiveID);
  1416. else if (IDVal == ".machine")
  1417. ParseDirectiveMachine(DirectiveID.getLoc());
  1418. else if (IDVal == ".abiversion")
  1419. ParseDirectiveAbiVersion(DirectiveID.getLoc());
  1420. else if (IDVal == ".localentry")
  1421. ParseDirectiveLocalEntry(DirectiveID.getLoc());
  1422. else if (IDVal.startswith(".gnu_attribute"))
  1423. ParseGNUAttribute(DirectiveID.getLoc());
  1424. else
  1425. return true;
  1426. return false;
  1427. }
  1428. /// ParseDirectiveWord
  1429. /// ::= .word [ expression (, expression)* ]
  1430. bool PPCAsmParser::ParseDirectiveWord(unsigned Size, AsmToken ID) {
  1431. auto parseOp = [&]() -> bool {
  1432. const MCExpr *Value;
  1433. SMLoc ExprLoc = getParser().getTok().getLoc();
  1434. if (getParser().parseExpression(Value))
  1435. return true;
  1436. if (const auto *MCE = dyn_cast<MCConstantExpr>(Value)) {
  1437. assert(Size <= 8 && "Invalid size");
  1438. uint64_t IntValue = MCE->getValue();
  1439. if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
  1440. return Error(ExprLoc, "literal value out of range for '" +
  1441. ID.getIdentifier() + "' directive");
  1442. getStreamer().emitIntValue(IntValue, Size);
  1443. } else
  1444. getStreamer().emitValue(Value, Size, ExprLoc);
  1445. return false;
  1446. };
  1447. if (parseMany(parseOp))
  1448. return addErrorSuffix(" in '" + ID.getIdentifier() + "' directive");
  1449. return false;
  1450. }
  1451. /// ParseDirectiveTC
  1452. /// ::= .tc [ symbol (, expression)* ]
  1453. bool PPCAsmParser::ParseDirectiveTC(unsigned Size, AsmToken ID) {
  1454. MCAsmParser &Parser = getParser();
  1455. // Skip TC symbol, which is only used with XCOFF.
  1456. while (getLexer().isNot(AsmToken::EndOfStatement)
  1457. && getLexer().isNot(AsmToken::Comma))
  1458. Parser.Lex();
  1459. if (parseToken(AsmToken::Comma))
  1460. return addErrorSuffix(" in '.tc' directive");
  1461. // Align to word size.
  1462. getParser().getStreamer().emitValueToAlignment(Size);
  1463. // Emit expressions.
  1464. return ParseDirectiveWord(Size, ID);
  1465. }
  1466. /// ParseDirectiveMachine (ELF platforms)
  1467. /// ::= .machine [ cpu | "push" | "pop" ]
  1468. bool PPCAsmParser::ParseDirectiveMachine(SMLoc L) {
  1469. MCAsmParser &Parser = getParser();
  1470. if (Parser.getTok().isNot(AsmToken::Identifier) &&
  1471. Parser.getTok().isNot(AsmToken::String))
  1472. return Error(L, "unexpected token in '.machine' directive");
  1473. StringRef CPU = Parser.getTok().getIdentifier();
  1474. // FIXME: Right now, the parser always allows any available
  1475. // instruction, so the .machine directive is not useful.
  1476. // In the wild, any/push/pop/ppc64/altivec/power[4-9] are seen.
  1477. Parser.Lex();
  1478. if (parseToken(AsmToken::EndOfStatement))
  1479. return addErrorSuffix(" in '.machine' directive");
  1480. PPCTargetStreamer *TStreamer = static_cast<PPCTargetStreamer *>(
  1481. getParser().getStreamer().getTargetStreamer());
  1482. if (TStreamer != nullptr)
  1483. TStreamer->emitMachine(CPU);
  1484. return false;
  1485. }
  1486. /// ParseDirectiveAbiVersion
  1487. /// ::= .abiversion constant-expression
  1488. bool PPCAsmParser::ParseDirectiveAbiVersion(SMLoc L) {
  1489. int64_t AbiVersion;
  1490. if (check(getParser().parseAbsoluteExpression(AbiVersion), L,
  1491. "expected constant expression") ||
  1492. parseToken(AsmToken::EndOfStatement))
  1493. return addErrorSuffix(" in '.abiversion' directive");
  1494. PPCTargetStreamer *TStreamer = static_cast<PPCTargetStreamer *>(
  1495. getParser().getStreamer().getTargetStreamer());
  1496. if (TStreamer != nullptr)
  1497. TStreamer->emitAbiVersion(AbiVersion);
  1498. return false;
  1499. }
  1500. /// ParseDirectiveLocalEntry
  1501. /// ::= .localentry symbol, expression
  1502. bool PPCAsmParser::ParseDirectiveLocalEntry(SMLoc L) {
  1503. StringRef Name;
  1504. if (getParser().parseIdentifier(Name))
  1505. return Error(L, "expected identifier in '.localentry' directive");
  1506. MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
  1507. const MCExpr *Expr;
  1508. if (parseToken(AsmToken::Comma) ||
  1509. check(getParser().parseExpression(Expr), L, "expected expression") ||
  1510. parseToken(AsmToken::EndOfStatement))
  1511. return addErrorSuffix(" in '.localentry' directive");
  1512. PPCTargetStreamer *TStreamer = static_cast<PPCTargetStreamer *>(
  1513. getParser().getStreamer().getTargetStreamer());
  1514. if (TStreamer != nullptr)
  1515. TStreamer->emitLocalEntry(Sym, Expr);
  1516. return false;
  1517. }
  1518. bool PPCAsmParser::ParseGNUAttribute(SMLoc L) {
  1519. int64_t Tag;
  1520. int64_t IntegerValue;
  1521. if (!getParser().parseGNUAttribute(L, Tag, IntegerValue))
  1522. return false;
  1523. getParser().getStreamer().emitGNUAttribute(Tag, IntegerValue);
  1524. return true;
  1525. }
  1526. /// Force static initialization.
  1527. extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCAsmParser() {
  1528. RegisterMCAsmParser<PPCAsmParser> A(getThePPC32Target());
  1529. RegisterMCAsmParser<PPCAsmParser> B(getThePPC32LETarget());
  1530. RegisterMCAsmParser<PPCAsmParser> C(getThePPC64Target());
  1531. RegisterMCAsmParser<PPCAsmParser> D(getThePPC64LETarget());
  1532. }
  1533. #define GET_REGISTER_MATCHER
  1534. #define GET_MATCHER_IMPLEMENTATION
  1535. #define GET_MNEMONIC_SPELL_CHECKER
  1536. #include "PPCGenAsmMatcher.inc"
  1537. // Define this matcher function after the auto-generated include so we
  1538. // have the match class enum definitions.
  1539. unsigned PPCAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
  1540. unsigned Kind) {
  1541. // If the kind is a token for a literal immediate, check if our asm
  1542. // operand matches. This is for InstAliases which have a fixed-value
  1543. // immediate in the syntax.
  1544. int64_t ImmVal;
  1545. switch (Kind) {
  1546. case MCK_0: ImmVal = 0; break;
  1547. case MCK_1: ImmVal = 1; break;
  1548. case MCK_2: ImmVal = 2; break;
  1549. case MCK_3: ImmVal = 3; break;
  1550. case MCK_4: ImmVal = 4; break;
  1551. case MCK_5: ImmVal = 5; break;
  1552. case MCK_6: ImmVal = 6; break;
  1553. case MCK_7: ImmVal = 7; break;
  1554. default: return Match_InvalidOperand;
  1555. }
  1556. PPCOperand &Op = static_cast<PPCOperand &>(AsmOp);
  1557. if (Op.isU3Imm() && Op.getImm() == ImmVal)
  1558. return Match_Success;
  1559. return Match_InvalidOperand;
  1560. }
  1561. const MCExpr *
  1562. PPCAsmParser::applyModifierToExpr(const MCExpr *E,
  1563. MCSymbolRefExpr::VariantKind Variant,
  1564. MCContext &Ctx) {
  1565. switch (Variant) {
  1566. case MCSymbolRefExpr::VK_PPC_LO:
  1567. return PPCMCExpr::create(PPCMCExpr::VK_PPC_LO, E, Ctx);
  1568. case MCSymbolRefExpr::VK_PPC_HI:
  1569. return PPCMCExpr::create(PPCMCExpr::VK_PPC_HI, E, Ctx);
  1570. case MCSymbolRefExpr::VK_PPC_HA:
  1571. return PPCMCExpr::create(PPCMCExpr::VK_PPC_HA, E, Ctx);
  1572. case MCSymbolRefExpr::VK_PPC_HIGH:
  1573. return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGH, E, Ctx);
  1574. case MCSymbolRefExpr::VK_PPC_HIGHA:
  1575. return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHA, E, Ctx);
  1576. case MCSymbolRefExpr::VK_PPC_HIGHER:
  1577. return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHER, E, Ctx);
  1578. case MCSymbolRefExpr::VK_PPC_HIGHERA:
  1579. return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHERA, E, Ctx);
  1580. case MCSymbolRefExpr::VK_PPC_HIGHEST:
  1581. return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHEST, E, Ctx);
  1582. case MCSymbolRefExpr::VK_PPC_HIGHESTA:
  1583. return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHESTA, E, Ctx);
  1584. default:
  1585. return nullptr;
  1586. }
  1587. }