LLVM API Documentation

SystemZAsmParser.cpp
Go to the documentation of this file.
00001 //===-- SystemZAsmParser.cpp - Parse SystemZ assembly instructions --------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "MCTargetDesc/SystemZMCTargetDesc.h"
00011 #include "llvm/ADT/STLExtras.h"
00012 #include "llvm/MC/MCContext.h"
00013 #include "llvm/MC/MCExpr.h"
00014 #include "llvm/MC/MCInst.h"
00015 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
00016 #include "llvm/MC/MCStreamer.h"
00017 #include "llvm/MC/MCSubtargetInfo.h"
00018 #include "llvm/MC/MCTargetAsmParser.h"
00019 #include "llvm/Support/TargetRegistry.h"
00020 
00021 using namespace llvm;
00022 
00023 // Return true if Expr is in the range [MinValue, MaxValue].
00024 static bool inRange(const MCExpr *Expr, int64_t MinValue, int64_t MaxValue) {
00025   if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
00026     int64_t Value = CE->getValue();
00027     return Value >= MinValue && Value <= MaxValue;
00028   }
00029   return false;
00030 }
00031 
00032 namespace {
00033 enum RegisterKind {
00034   GR32Reg,
00035   GRH32Reg,
00036   GR64Reg,
00037   GR128Reg,
00038   ADDR32Reg,
00039   ADDR64Reg,
00040   FP32Reg,
00041   FP64Reg,
00042   FP128Reg
00043 };
00044 
00045 enum MemoryKind {
00046   BDMem,
00047   BDXMem,
00048   BDLMem
00049 };
00050 
00051 class SystemZOperand : public MCParsedAsmOperand {
00052 public:
00053 private:
00054   enum OperandKind {
00055     KindInvalid,
00056     KindToken,
00057     KindReg,
00058     KindAccessReg,
00059     KindImm,
00060     KindMem
00061   };
00062 
00063   OperandKind Kind;
00064   SMLoc StartLoc, EndLoc;
00065 
00066   // A string of length Length, starting at Data.
00067   struct TokenOp {
00068     const char *Data;
00069     unsigned Length;
00070   };
00071 
00072   // LLVM register Num, which has kind Kind.  In some ways it might be
00073   // easier for this class to have a register bank (general, floating-point
00074   // or access) and a raw register number (0-15).  This would postpone the
00075   // interpretation of the operand to the add*() methods and avoid the need
00076   // for context-dependent parsing.  However, we do things the current way
00077   // because of the virtual getReg() method, which needs to distinguish
00078   // between (say) %r0 used as a single register and %r0 used as a pair.
00079   // Context-dependent parsing can also give us slightly better error
00080   // messages when invalid pairs like %r1 are used.
00081   struct RegOp {
00082     RegisterKind Kind;
00083     unsigned Num;
00084   };
00085 
00086   // Base + Disp + Index, where Base and Index are LLVM registers or 0.
00087   // RegKind says what type the registers have (ADDR32Reg or ADDR64Reg).
00088   // Length is the operand length for D(L,B)-style operands, otherwise
00089   // it is null.
00090   struct MemOp {
00091     unsigned Base : 8;
00092     unsigned Index : 8;
00093     unsigned RegKind : 8;
00094     unsigned Unused : 8;
00095     const MCExpr *Disp;
00096     const MCExpr *Length;
00097   };
00098 
00099   union {
00100     TokenOp Token;
00101     RegOp Reg;
00102     unsigned AccessReg;
00103     const MCExpr *Imm;
00104     MemOp Mem;
00105   };
00106 
00107   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
00108     // Add as immediates when possible.  Null MCExpr = 0.
00109     if (!Expr)
00110       Inst.addOperand(MCOperand::CreateImm(0));
00111     else if (auto *CE = dyn_cast<MCConstantExpr>(Expr))
00112       Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
00113     else
00114       Inst.addOperand(MCOperand::CreateExpr(Expr));
00115   }
00116 
00117 public:
00118   SystemZOperand(OperandKind kind, SMLoc startLoc, SMLoc endLoc)
00119       : Kind(kind), StartLoc(startLoc), EndLoc(endLoc) {}
00120 
00121   // Create particular kinds of operand.
00122   static std::unique_ptr<SystemZOperand> createInvalid(SMLoc StartLoc,
00123                                                        SMLoc EndLoc) {
00124     return make_unique<SystemZOperand>(KindInvalid, StartLoc, EndLoc);
00125   }
00126   static std::unique_ptr<SystemZOperand> createToken(StringRef Str, SMLoc Loc) {
00127     auto Op = make_unique<SystemZOperand>(KindToken, Loc, Loc);
00128     Op->Token.Data = Str.data();
00129     Op->Token.Length = Str.size();
00130     return Op;
00131   }
00132   static std::unique_ptr<SystemZOperand>
00133   createReg(RegisterKind Kind, unsigned Num, SMLoc StartLoc, SMLoc EndLoc) {
00134     auto Op = make_unique<SystemZOperand>(KindReg, StartLoc, EndLoc);
00135     Op->Reg.Kind = Kind;
00136     Op->Reg.Num = Num;
00137     return Op;
00138   }
00139   static std::unique_ptr<SystemZOperand>
00140   createAccessReg(unsigned Num, SMLoc StartLoc, SMLoc EndLoc) {
00141     auto Op = make_unique<SystemZOperand>(KindAccessReg, StartLoc, EndLoc);
00142     Op->AccessReg = Num;
00143     return Op;
00144   }
00145   static std::unique_ptr<SystemZOperand>
00146   createImm(const MCExpr *Expr, SMLoc StartLoc, SMLoc EndLoc) {
00147     auto Op = make_unique<SystemZOperand>(KindImm, StartLoc, EndLoc);
00148     Op->Imm = Expr;
00149     return Op;
00150   }
00151   static std::unique_ptr<SystemZOperand>
00152   createMem(RegisterKind RegKind, unsigned Base, const MCExpr *Disp,
00153             unsigned Index, const MCExpr *Length, SMLoc StartLoc,
00154             SMLoc EndLoc) {
00155     auto Op = make_unique<SystemZOperand>(KindMem, StartLoc, EndLoc);
00156     Op->Mem.RegKind = RegKind;
00157     Op->Mem.Base = Base;
00158     Op->Mem.Index = Index;
00159     Op->Mem.Disp = Disp;
00160     Op->Mem.Length = Length;
00161     return Op;
00162   }
00163 
00164   // Token operands
00165   bool isToken() const override {
00166     return Kind == KindToken;
00167   }
00168   StringRef getToken() const {
00169     assert(Kind == KindToken && "Not a token");
00170     return StringRef(Token.Data, Token.Length);
00171   }
00172 
00173   // Register operands.
00174   bool isReg() const override {
00175     return Kind == KindReg;
00176   }
00177   bool isReg(RegisterKind RegKind) const {
00178     return Kind == KindReg && Reg.Kind == RegKind;
00179   }
00180   unsigned getReg() const override {
00181     assert(Kind == KindReg && "Not a register");
00182     return Reg.Num;
00183   }
00184 
00185   // Access register operands.  Access registers aren't exposed to LLVM
00186   // as registers.
00187   bool isAccessReg() const {
00188     return Kind == KindAccessReg;
00189   }
00190 
00191   // Immediate operands.
00192   bool isImm() const override {
00193     return Kind == KindImm;
00194   }
00195   bool isImm(int64_t MinValue, int64_t MaxValue) const {
00196     return Kind == KindImm && inRange(Imm, MinValue, MaxValue);
00197   }
00198   const MCExpr *getImm() const {
00199     assert(Kind == KindImm && "Not an immediate");
00200     return Imm;
00201   }
00202 
00203   // Memory operands.
00204   bool isMem() const override {
00205     return Kind == KindMem;
00206   }
00207   bool isMem(RegisterKind RegKind, MemoryKind MemKind) const {
00208     return (Kind == KindMem &&
00209             Mem.RegKind == RegKind &&
00210             (MemKind == BDXMem || !Mem.Index) &&
00211             (MemKind == BDLMem) == (Mem.Length != nullptr));
00212   }
00213   bool isMemDisp12(RegisterKind RegKind, MemoryKind MemKind) const {
00214     return isMem(RegKind, MemKind) && inRange(Mem.Disp, 0, 0xfff);
00215   }
00216   bool isMemDisp20(RegisterKind RegKind, MemoryKind MemKind) const {
00217     return isMem(RegKind, MemKind) && inRange(Mem.Disp, -524288, 524287);
00218   }
00219   bool isMemDisp12Len8(RegisterKind RegKind) const {
00220     return isMemDisp12(RegKind, BDLMem) && inRange(Mem.Length, 1, 0x100);
00221   }
00222 
00223   // Override MCParsedAsmOperand.
00224   SMLoc getStartLoc() const override { return StartLoc; }
00225   SMLoc getEndLoc() const override { return EndLoc; }
00226   void print(raw_ostream &OS) const override;
00227 
00228   // Used by the TableGen code to add particular types of operand
00229   // to an instruction.
00230   void addRegOperands(MCInst &Inst, unsigned N) const {
00231     assert(N == 1 && "Invalid number of operands");
00232     Inst.addOperand(MCOperand::CreateReg(getReg()));
00233   }
00234   void addAccessRegOperands(MCInst &Inst, unsigned N) const {
00235     assert(N == 1 && "Invalid number of operands");
00236     assert(Kind == KindAccessReg && "Invalid operand type");
00237     Inst.addOperand(MCOperand::CreateImm(AccessReg));
00238   }
00239   void addImmOperands(MCInst &Inst, unsigned N) const {
00240     assert(N == 1 && "Invalid number of operands");
00241     addExpr(Inst, getImm());
00242   }
00243   void addBDAddrOperands(MCInst &Inst, unsigned N) const {
00244     assert(N == 2 && "Invalid number of operands");
00245     assert(Kind == KindMem && Mem.Index == 0 && "Invalid operand type");
00246     Inst.addOperand(MCOperand::CreateReg(Mem.Base));
00247     addExpr(Inst, Mem.Disp);
00248   }
00249   void addBDXAddrOperands(MCInst &Inst, unsigned N) const {
00250     assert(N == 3 && "Invalid number of operands");
00251     assert(Kind == KindMem && "Invalid operand type");
00252     Inst.addOperand(MCOperand::CreateReg(Mem.Base));
00253     addExpr(Inst, Mem.Disp);
00254     Inst.addOperand(MCOperand::CreateReg(Mem.Index));
00255   }
00256   void addBDLAddrOperands(MCInst &Inst, unsigned N) const {
00257     assert(N == 3 && "Invalid number of operands");
00258     assert(Kind == KindMem && "Invalid operand type");
00259     Inst.addOperand(MCOperand::CreateReg(Mem.Base));
00260     addExpr(Inst, Mem.Disp);
00261     addExpr(Inst, Mem.Length);
00262   }
00263 
00264   // Used by the TableGen code to check for particular operand types.
00265   bool isGR32() const { return isReg(GR32Reg); }
00266   bool isGRH32() const { return isReg(GRH32Reg); }
00267   bool isGRX32() const { return false; }
00268   bool isGR64() const { return isReg(GR64Reg); }
00269   bool isGR128() const { return isReg(GR128Reg); }
00270   bool isADDR32() const { return isReg(ADDR32Reg); }
00271   bool isADDR64() const { return isReg(ADDR64Reg); }
00272   bool isADDR128() const { return false; }
00273   bool isFP32() const { return isReg(FP32Reg); }
00274   bool isFP64() const { return isReg(FP64Reg); }
00275   bool isFP128() const { return isReg(FP128Reg); }
00276   bool isBDAddr32Disp12() const { return isMemDisp12(ADDR32Reg, BDMem); }
00277   bool isBDAddr32Disp20() const { return isMemDisp20(ADDR32Reg, BDMem); }
00278   bool isBDAddr64Disp12() const { return isMemDisp12(ADDR64Reg, BDMem); }
00279   bool isBDAddr64Disp20() const { return isMemDisp20(ADDR64Reg, BDMem); }
00280   bool isBDXAddr64Disp12() const { return isMemDisp12(ADDR64Reg, BDXMem); }
00281   bool isBDXAddr64Disp20() const { return isMemDisp20(ADDR64Reg, BDXMem); }
00282   bool isBDLAddr64Disp12Len8() const { return isMemDisp12Len8(ADDR64Reg); }
00283   bool isU4Imm() const { return isImm(0, 15); }
00284   bool isU6Imm() const { return isImm(0, 63); }
00285   bool isU8Imm() const { return isImm(0, 255); }
00286   bool isS8Imm() const { return isImm(-128, 127); }
00287   bool isU16Imm() const { return isImm(0, 65535); }
00288   bool isS16Imm() const { return isImm(-32768, 32767); }
00289   bool isU32Imm() const { return isImm(0, (1LL << 32) - 1); }
00290   bool isS32Imm() const { return isImm(-(1LL << 31), (1LL << 31) - 1); }
00291 };
00292 
00293 class SystemZAsmParser : public MCTargetAsmParser {
00294 #define GET_ASSEMBLER_HEADER
00295 #include "SystemZGenAsmMatcher.inc"
00296 
00297 private:
00298   MCSubtargetInfo &STI;
00299   MCAsmParser &Parser;
00300   enum RegisterGroup {
00301     RegGR,
00302     RegFP,
00303     RegAccess
00304   };
00305   struct Register {
00306     RegisterGroup Group;
00307     unsigned Num;
00308     SMLoc StartLoc, EndLoc;
00309   };
00310 
00311   bool parseRegister(Register &Reg);
00312 
00313   bool parseRegister(Register &Reg, RegisterGroup Group, const unsigned *Regs,
00314                      bool IsAddress = false);
00315 
00316   OperandMatchResultTy parseRegister(OperandVector &Operands,
00317                                      RegisterGroup Group, const unsigned *Regs,
00318                                      RegisterKind Kind);
00319 
00320   bool parseAddress(unsigned &Base, const MCExpr *&Disp,
00321                     unsigned &Index, const MCExpr *&Length,
00322                     const unsigned *Regs, RegisterKind RegKind);
00323 
00324   OperandMatchResultTy parseAddress(OperandVector &Operands,
00325                                     const unsigned *Regs, RegisterKind RegKind,
00326                                     MemoryKind MemKind);
00327 
00328   bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
00329 
00330 public:
00331   SystemZAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
00332                    const MCInstrInfo &MII,
00333                    const MCTargetOptions &Options)
00334       : MCTargetAsmParser(), STI(sti), Parser(parser) {
00335     MCAsmParserExtension::Initialize(Parser);
00336 
00337     // Initialize the set of available features.
00338     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
00339   }
00340 
00341   // Override MCTargetAsmParser.
00342   bool ParseDirective(AsmToken DirectiveID) override;
00343   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
00344   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
00345                         SMLoc NameLoc, OperandVector &Operands) override;
00346   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
00347                                OperandVector &Operands, MCStreamer &Out,
00348                                uint64_t &ErrorInfo,
00349                                bool MatchingInlineAsm) override;
00350 
00351   // Used by the TableGen code to parse particular operand types.
00352   OperandMatchResultTy parseGR32(OperandVector &Operands) {
00353     return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, GR32Reg);
00354   }
00355   OperandMatchResultTy parseGRH32(OperandVector &Operands) {
00356     return parseRegister(Operands, RegGR, SystemZMC::GRH32Regs, GRH32Reg);
00357   }
00358   OperandMatchResultTy parseGRX32(OperandVector &Operands) {
00359     llvm_unreachable("GRX32 should only be used for pseudo instructions");
00360   }
00361   OperandMatchResultTy parseGR64(OperandVector &Operands) {
00362     return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, GR64Reg);
00363   }
00364   OperandMatchResultTy parseGR128(OperandVector &Operands) {
00365     return parseRegister(Operands, RegGR, SystemZMC::GR128Regs, GR128Reg);
00366   }
00367   OperandMatchResultTy parseADDR32(OperandVector &Operands) {
00368     return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, ADDR32Reg);
00369   }
00370   OperandMatchResultTy parseADDR64(OperandVector &Operands) {
00371     return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, ADDR64Reg);
00372   }
00373   OperandMatchResultTy parseADDR128(OperandVector &Operands) {
00374     llvm_unreachable("Shouldn't be used as an operand");
00375   }
00376   OperandMatchResultTy parseFP32(OperandVector &Operands) {
00377     return parseRegister(Operands, RegFP, SystemZMC::FP32Regs, FP32Reg);
00378   }
00379   OperandMatchResultTy parseFP64(OperandVector &Operands) {
00380     return parseRegister(Operands, RegFP, SystemZMC::FP64Regs, FP64Reg);
00381   }
00382   OperandMatchResultTy parseFP128(OperandVector &Operands) {
00383     return parseRegister(Operands, RegFP, SystemZMC::FP128Regs, FP128Reg);
00384   }
00385   OperandMatchResultTy parseBDAddr32(OperandVector &Operands) {
00386     return parseAddress(Operands, SystemZMC::GR32Regs, ADDR32Reg, BDMem);
00387   }
00388   OperandMatchResultTy parseBDAddr64(OperandVector &Operands) {
00389     return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDMem);
00390   }
00391   OperandMatchResultTy parseBDXAddr64(OperandVector &Operands) {
00392     return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDXMem);
00393   }
00394   OperandMatchResultTy parseBDLAddr64(OperandVector &Operands) {
00395     return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDLMem);
00396   }
00397   OperandMatchResultTy parseAccessReg(OperandVector &Operands);
00398   OperandMatchResultTy parsePCRel(OperandVector &Operands, int64_t MinVal,
00399                                   int64_t MaxVal);
00400   OperandMatchResultTy parsePCRel16(OperandVector &Operands) {
00401     return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1);
00402   }
00403   OperandMatchResultTy parsePCRel32(OperandVector &Operands) {
00404     return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1);
00405   }
00406 };
00407 } // end anonymous namespace
00408 
00409 #define GET_REGISTER_MATCHER
00410 #define GET_SUBTARGET_FEATURE_NAME
00411 #define GET_MATCHER_IMPLEMENTATION
00412 #include "SystemZGenAsmMatcher.inc"
00413 
00414 void SystemZOperand::print(raw_ostream &OS) const {
00415   llvm_unreachable("Not implemented");
00416 }
00417 
00418 // Parse one register of the form %<prefix><number>.
00419 bool SystemZAsmParser::parseRegister(Register &Reg) {
00420   Reg.StartLoc = Parser.getTok().getLoc();
00421 
00422   // Eat the % prefix.
00423   if (Parser.getTok().isNot(AsmToken::Percent))
00424     return Error(Parser.getTok().getLoc(), "register expected");
00425   Parser.Lex();
00426 
00427   // Expect a register name.
00428   if (Parser.getTok().isNot(AsmToken::Identifier))
00429     return Error(Reg.StartLoc, "invalid register");
00430 
00431   // Check that there's a prefix.
00432   StringRef Name = Parser.getTok().getString();
00433   if (Name.size() < 2)
00434     return Error(Reg.StartLoc, "invalid register");
00435   char Prefix = Name[0];
00436 
00437   // Treat the rest of the register name as a register number.
00438   if (Name.substr(1).getAsInteger(10, Reg.Num))
00439     return Error(Reg.StartLoc, "invalid register");
00440 
00441   // Look for valid combinations of prefix and number.
00442   if (Prefix == 'r' && Reg.Num < 16)
00443     Reg.Group = RegGR;
00444   else if (Prefix == 'f' && Reg.Num < 16)
00445     Reg.Group = RegFP;
00446   else if (Prefix == 'a' && Reg.Num < 16)
00447     Reg.Group = RegAccess;
00448   else
00449     return Error(Reg.StartLoc, "invalid register");
00450 
00451   Reg.EndLoc = Parser.getTok().getLoc();
00452   Parser.Lex();
00453   return false;
00454 }
00455 
00456 // Parse a register of group Group.  If Regs is nonnull, use it to map
00457 // the raw register number to LLVM numbering, with zero entries indicating
00458 // an invalid register.  IsAddress says whether the register appears in an
00459 // address context.
00460 bool SystemZAsmParser::parseRegister(Register &Reg, RegisterGroup Group,
00461                                      const unsigned *Regs, bool IsAddress) {
00462   if (parseRegister(Reg))
00463     return true;
00464   if (Reg.Group != Group)
00465     return Error(Reg.StartLoc, "invalid operand for instruction");
00466   if (Regs && Regs[Reg.Num] == 0)
00467     return Error(Reg.StartLoc, "invalid register pair");
00468   if (Reg.Num == 0 && IsAddress)
00469     return Error(Reg.StartLoc, "%r0 used in an address");
00470   if (Regs)
00471     Reg.Num = Regs[Reg.Num];
00472   return false;
00473 }
00474 
00475 // Parse a register and add it to Operands.  The other arguments are as above.
00476 SystemZAsmParser::OperandMatchResultTy
00477 SystemZAsmParser::parseRegister(OperandVector &Operands, RegisterGroup Group,
00478                                 const unsigned *Regs, RegisterKind Kind) {
00479   if (Parser.getTok().isNot(AsmToken::Percent))
00480     return MatchOperand_NoMatch;
00481 
00482   Register Reg;
00483   bool IsAddress = (Kind == ADDR32Reg || Kind == ADDR64Reg);
00484   if (parseRegister(Reg, Group, Regs, IsAddress))
00485     return MatchOperand_ParseFail;
00486 
00487   Operands.push_back(SystemZOperand::createReg(Kind, Reg.Num,
00488                                                Reg.StartLoc, Reg.EndLoc));
00489   return MatchOperand_Success;
00490 }
00491 
00492 // Parse a memory operand into Base, Disp, Index and Length.
00493 // Regs maps asm register numbers to LLVM register numbers and RegKind
00494 // says what kind of address register we're using (ADDR32Reg or ADDR64Reg).
00495 bool SystemZAsmParser::parseAddress(unsigned &Base, const MCExpr *&Disp,
00496                                     unsigned &Index, const MCExpr *&Length,
00497                                     const unsigned *Regs,
00498                                     RegisterKind RegKind) {
00499   // Parse the displacement, which must always be present.
00500   if (getParser().parseExpression(Disp))
00501     return true;
00502 
00503   // Parse the optional base and index.
00504   Index = 0;
00505   Base = 0;
00506   Length = nullptr;
00507   if (getLexer().is(AsmToken::LParen)) {
00508     Parser.Lex();
00509 
00510     if (getLexer().is(AsmToken::Percent)) {
00511       // Parse the first register and decide whether it's a base or an index.
00512       Register Reg;
00513       if (parseRegister(Reg, RegGR, Regs, RegKind))
00514         return true;
00515       if (getLexer().is(AsmToken::Comma))
00516         Index = Reg.Num;
00517       else
00518         Base = Reg.Num;
00519     } else {
00520       // Parse the length.
00521       if (getParser().parseExpression(Length))
00522         return true;
00523     }
00524 
00525     // Check whether there's a second register.  It's the base if so.
00526     if (getLexer().is(AsmToken::Comma)) {
00527       Parser.Lex();
00528       Register Reg;
00529       if (parseRegister(Reg, RegGR, Regs, RegKind))
00530         return true;
00531       Base = Reg.Num;
00532     }
00533 
00534     // Consume the closing bracket.
00535     if (getLexer().isNot(AsmToken::RParen))
00536       return Error(Parser.getTok().getLoc(), "unexpected token in address");
00537     Parser.Lex();
00538   }
00539   return false;
00540 }
00541 
00542 // Parse a memory operand and add it to Operands.  The other arguments
00543 // are as above.
00544 SystemZAsmParser::OperandMatchResultTy
00545 SystemZAsmParser::parseAddress(OperandVector &Operands, const unsigned *Regs,
00546                                RegisterKind RegKind, MemoryKind MemKind) {
00547   SMLoc StartLoc = Parser.getTok().getLoc();
00548   unsigned Base, Index;
00549   const MCExpr *Disp;
00550   const MCExpr *Length;
00551   if (parseAddress(Base, Disp, Index, Length, Regs, RegKind))
00552     return MatchOperand_ParseFail;
00553 
00554   if (Index && MemKind != BDXMem)
00555     {
00556       Error(StartLoc, "invalid use of indexed addressing");
00557       return MatchOperand_ParseFail;
00558     }
00559 
00560   if (Length && MemKind != BDLMem)
00561     {
00562       Error(StartLoc, "invalid use of length addressing");
00563       return MatchOperand_ParseFail;
00564     }
00565 
00566   if (!Length && MemKind == BDLMem)
00567     {
00568       Error(StartLoc, "missing length in address");
00569       return MatchOperand_ParseFail;
00570     }
00571 
00572   SMLoc EndLoc =
00573     SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
00574   Operands.push_back(SystemZOperand::createMem(RegKind, Base, Disp, Index,
00575                                                Length, StartLoc, EndLoc));
00576   return MatchOperand_Success;
00577 }
00578 
00579 bool SystemZAsmParser::ParseDirective(AsmToken DirectiveID) {
00580   return true;
00581 }
00582 
00583 bool SystemZAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
00584                                      SMLoc &EndLoc) {
00585   Register Reg;
00586   if (parseRegister(Reg))
00587     return true;
00588   if (Reg.Group == RegGR)
00589     RegNo = SystemZMC::GR64Regs[Reg.Num];
00590   else if (Reg.Group == RegFP)
00591     RegNo = SystemZMC::FP64Regs[Reg.Num];
00592   else
00593     // FIXME: Access registers aren't modelled as LLVM registers yet.
00594     return Error(Reg.StartLoc, "invalid operand for instruction");
00595   StartLoc = Reg.StartLoc;
00596   EndLoc = Reg.EndLoc;
00597   return false;
00598 }
00599 
00600 bool SystemZAsmParser::ParseInstruction(ParseInstructionInfo &Info,
00601                                         StringRef Name, SMLoc NameLoc,
00602                                         OperandVector &Operands) {
00603   Operands.push_back(SystemZOperand::createToken(Name, NameLoc));
00604 
00605   // Read the remaining operands.
00606   if (getLexer().isNot(AsmToken::EndOfStatement)) {
00607     // Read the first operand.
00608     if (parseOperand(Operands, Name)) {
00609       Parser.eatToEndOfStatement();
00610       return true;
00611     }
00612 
00613     // Read any subsequent operands.
00614     while (getLexer().is(AsmToken::Comma)) {
00615       Parser.Lex();
00616       if (parseOperand(Operands, Name)) {
00617         Parser.eatToEndOfStatement();
00618         return true;
00619       }
00620     }
00621     if (getLexer().isNot(AsmToken::EndOfStatement)) {
00622       SMLoc Loc = getLexer().getLoc();
00623       Parser.eatToEndOfStatement();
00624       return Error(Loc, "unexpected token in argument list");
00625     }
00626   }
00627 
00628   // Consume the EndOfStatement.
00629   Parser.Lex();
00630   return false;
00631 }
00632 
00633 bool SystemZAsmParser::parseOperand(OperandVector &Operands,
00634                                     StringRef Mnemonic) {
00635   // Check if the current operand has a custom associated parser, if so, try to
00636   // custom parse the operand, or fallback to the general approach.
00637   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
00638   if (ResTy == MatchOperand_Success)
00639     return false;
00640 
00641   // If there wasn't a custom match, try the generic matcher below. Otherwise,
00642   // there was a match, but an error occurred, in which case, just return that
00643   // the operand parsing failed.
00644   if (ResTy == MatchOperand_ParseFail)
00645     return true;
00646 
00647   // Check for a register.  All real register operands should have used
00648   // a context-dependent parse routine, which gives the required register
00649   // class.  The code is here to mop up other cases, like those where
00650   // the instruction isn't recognized.
00651   if (Parser.getTok().is(AsmToken::Percent)) {
00652     Register Reg;
00653     if (parseRegister(Reg))
00654       return true;
00655     Operands.push_back(SystemZOperand::createInvalid(Reg.StartLoc, Reg.EndLoc));
00656     return false;
00657   }
00658 
00659   // The only other type of operand is an immediate or address.  As above,
00660   // real address operands should have used a context-dependent parse routine,
00661   // so we treat any plain expression as an immediate.
00662   SMLoc StartLoc = Parser.getTok().getLoc();
00663   unsigned Base, Index;
00664   const MCExpr *Expr, *Length;
00665   if (parseAddress(Base, Expr, Index, Length, SystemZMC::GR64Regs, ADDR64Reg))
00666     return true;
00667 
00668   SMLoc EndLoc =
00669     SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
00670   if (Base || Index || Length)
00671     Operands.push_back(SystemZOperand::createInvalid(StartLoc, EndLoc));
00672   else
00673     Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
00674   return false;
00675 }
00676 
00677 bool SystemZAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
00678                                                OperandVector &Operands,
00679                                                MCStreamer &Out,
00680                                                uint64_t &ErrorInfo,
00681                                                bool MatchingInlineAsm) {
00682   MCInst Inst;
00683   unsigned MatchResult;
00684 
00685   MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
00686                                      MatchingInlineAsm);
00687   switch (MatchResult) {
00688   default: break;
00689   case Match_Success:
00690     Inst.setLoc(IDLoc);
00691     Out.EmitInstruction(Inst, STI);
00692     return false;
00693 
00694   case Match_MissingFeature: {
00695     assert(ErrorInfo && "Unknown missing feature!");
00696     // Special case the error message for the very common case where only
00697     // a single subtarget feature is missing
00698     std::string Msg = "instruction requires:";
00699     uint64_t Mask = 1;
00700     for (unsigned I = 0; I < sizeof(ErrorInfo) * 8 - 1; ++I) {
00701       if (ErrorInfo & Mask) {
00702         Msg += " ";
00703         Msg += getSubtargetFeatureName(ErrorInfo & Mask);
00704       }
00705       Mask <<= 1;
00706     }
00707     return Error(IDLoc, Msg);
00708   }
00709 
00710   case Match_InvalidOperand: {
00711     SMLoc ErrorLoc = IDLoc;
00712     if (ErrorInfo != ~0ULL) {
00713       if (ErrorInfo >= Operands.size())
00714         return Error(IDLoc, "too few operands for instruction");
00715 
00716       ErrorLoc = ((SystemZOperand &)*Operands[ErrorInfo]).getStartLoc();
00717       if (ErrorLoc == SMLoc())
00718         ErrorLoc = IDLoc;
00719     }
00720     return Error(ErrorLoc, "invalid operand for instruction");
00721   }
00722 
00723   case Match_MnemonicFail:
00724     return Error(IDLoc, "invalid instruction");
00725   }
00726 
00727   llvm_unreachable("Unexpected match type");
00728 }
00729 
00730 SystemZAsmParser::OperandMatchResultTy
00731 SystemZAsmParser::parseAccessReg(OperandVector &Operands) {
00732   if (Parser.getTok().isNot(AsmToken::Percent))
00733     return MatchOperand_NoMatch;
00734 
00735   Register Reg;
00736   if (parseRegister(Reg, RegAccess, nullptr))
00737     return MatchOperand_ParseFail;
00738 
00739   Operands.push_back(SystemZOperand::createAccessReg(Reg.Num,
00740                                                      Reg.StartLoc,
00741                                                      Reg.EndLoc));
00742   return MatchOperand_Success;
00743 }
00744 
00745 SystemZAsmParser::OperandMatchResultTy
00746 SystemZAsmParser::parsePCRel(OperandVector &Operands, int64_t MinVal,
00747                              int64_t MaxVal) {
00748   MCContext &Ctx = getContext();
00749   MCStreamer &Out = getStreamer();
00750   const MCExpr *Expr;
00751   SMLoc StartLoc = Parser.getTok().getLoc();
00752   if (getParser().parseExpression(Expr))
00753     return MatchOperand_NoMatch;
00754 
00755   // For consistency with the GNU assembler, treat immediates as offsets
00756   // from ".".
00757   if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
00758     int64_t Value = CE->getValue();
00759     if ((Value & 1) || Value < MinVal || Value > MaxVal) {
00760       Error(StartLoc, "offset out of range");
00761       return MatchOperand_ParseFail;
00762     }
00763     MCSymbol *Sym = Ctx.CreateTempSymbol();
00764     Out.EmitLabel(Sym);
00765     const MCExpr *Base = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
00766                                                  Ctx);
00767     Expr = Value == 0 ? Base : MCBinaryExpr::CreateAdd(Base, Expr, Ctx);
00768   }
00769 
00770   SMLoc EndLoc =
00771     SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
00772   Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
00773   return MatchOperand_Success;
00774 }
00775 
00776 // Force static initialization.
00777 extern "C" void LLVMInitializeSystemZAsmParser() {
00778   RegisterMCAsmParser<SystemZAsmParser> X(TheSystemZTarget);
00779 }