| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1 | //===-- SystemZAsmParser.cpp - Parse SystemZ assembly instructions --------===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
|  | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
|  | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 |  | 
|  | 10 | #include "MCTargetDesc/SystemZMCTargetDesc.h" | 
| Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" | 
| Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCContext.h" | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCExpr.h" | 
|  | 14 | #include "llvm/MC/MCInst.h" | 
| Zhan Jun Liau | 4fbc3f4 | 2016-08-08 15:13:08 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCInstBuilder.h" | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" | 
| Benjamin Kramer | b3e8a6d | 2016-01-27 10:01:28 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCParser/MCTargetAsmParser.h" | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCStreamer.h" | 
|  | 19 | #include "llvm/MC/MCSubtargetInfo.h" | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 20 | #include "llvm/Support/TargetRegistry.h" | 
|  | 21 |  | 
|  | 22 | using namespace llvm; | 
|  | 23 |  | 
|  | 24 | // Return true if Expr is in the range [MinValue, MaxValue]. | 
|  | 25 | static bool inRange(const MCExpr *Expr, int64_t MinValue, int64_t MaxValue) { | 
| Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 26 | if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 27 | int64_t Value = CE->getValue(); | 
|  | 28 | return Value >= MinValue && Value <= MaxValue; | 
|  | 29 | } | 
|  | 30 | return false; | 
|  | 31 | } | 
|  | 32 |  | 
|  | 33 | namespace { | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 34 | enum RegisterKind { | 
|  | 35 | GR32Reg, | 
| Richard Sandiford | f949606 | 2013-09-30 10:45:16 +0000 | [diff] [blame] | 36 | GRH32Reg, | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 37 | GR64Reg, | 
|  | 38 | GR128Reg, | 
|  | 39 | ADDR32Reg, | 
|  | 40 | ADDR64Reg, | 
|  | 41 | FP32Reg, | 
|  | 42 | FP64Reg, | 
| Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame] | 43 | FP128Reg, | 
|  | 44 | VR32Reg, | 
|  | 45 | VR64Reg, | 
| Ulrich Weigand | fffc711 | 2016-11-08 20:15:26 +0000 | [diff] [blame] | 46 | VR128Reg, | 
|  | 47 | AR32Reg, | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 48 | }; | 
|  | 49 |  | 
|  | 50 | enum MemoryKind { | 
|  | 51 | BDMem, | 
|  | 52 | BDXMem, | 
| Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame] | 53 | BDLMem, | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 54 | BDRMem, | 
| Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame] | 55 | BDVMem | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 56 | }; | 
|  | 57 |  | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 58 | class SystemZOperand : public MCParsedAsmOperand { | 
|  | 59 | public: | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 60 | private: | 
|  | 61 | enum OperandKind { | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 62 | KindInvalid, | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 63 | KindToken, | 
|  | 64 | KindReg, | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 65 | KindImm, | 
| Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 66 | KindImmTLS, | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 67 | KindMem | 
|  | 68 | }; | 
|  | 69 |  | 
|  | 70 | OperandKind Kind; | 
|  | 71 | SMLoc StartLoc, EndLoc; | 
|  | 72 |  | 
|  | 73 | // A string of length Length, starting at Data. | 
|  | 74 | struct TokenOp { | 
|  | 75 | const char *Data; | 
|  | 76 | unsigned Length; | 
|  | 77 | }; | 
|  | 78 |  | 
| Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 79 | // LLVM register Num, which has kind Kind.  In some ways it might be | 
|  | 80 | // easier for this class to have a register bank (general, floating-point | 
|  | 81 | // or access) and a raw register number (0-15).  This would postpone the | 
|  | 82 | // interpretation of the operand to the add*() methods and avoid the need | 
|  | 83 | // for context-dependent parsing.  However, we do things the current way | 
|  | 84 | // because of the virtual getReg() method, which needs to distinguish | 
|  | 85 | // between (say) %r0 used as a single register and %r0 used as a pair. | 
|  | 86 | // Context-dependent parsing can also give us slightly better error | 
|  | 87 | // messages when invalid pairs like %r1 are used. | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 88 | struct RegOp { | 
|  | 89 | RegisterKind Kind; | 
|  | 90 | unsigned Num; | 
|  | 91 | }; | 
|  | 92 |  | 
|  | 93 | // Base + Disp + Index, where Base and Index are LLVM registers or 0. | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 94 | // MemKind says what type of memory this is and RegKind says what type | 
|  | 95 | // the base register has (ADDR32Reg or ADDR64Reg).  Length is the operand | 
|  | 96 | // length for D(L,B)-style operands, otherwise it is null. | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 97 | struct MemOp { | 
| Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame] | 98 | unsigned Base : 12; | 
|  | 99 | unsigned Index : 12; | 
|  | 100 | unsigned MemKind : 4; | 
|  | 101 | unsigned RegKind : 4; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 102 | const MCExpr *Disp; | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 103 | union { | 
|  | 104 | const MCExpr *Imm; | 
|  | 105 | unsigned Reg; | 
|  | 106 | } Length; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 107 | }; | 
|  | 108 |  | 
| Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 109 | // Imm is an immediate operand, and Sym is an optional TLS symbol | 
|  | 110 | // for use with a __tls_get_offset marker relocation. | 
|  | 111 | struct ImmTLSOp { | 
|  | 112 | const MCExpr *Imm; | 
|  | 113 | const MCExpr *Sym; | 
|  | 114 | }; | 
|  | 115 |  | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 116 | union { | 
|  | 117 | TokenOp Token; | 
|  | 118 | RegOp Reg; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 119 | const MCExpr *Imm; | 
| Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 120 | ImmTLSOp ImmTLS; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 121 | MemOp Mem; | 
|  | 122 | }; | 
|  | 123 |  | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 124 | void addExpr(MCInst &Inst, const MCExpr *Expr) const { | 
|  | 125 | // Add as immediates when possible.  Null MCExpr = 0. | 
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 126 | if (!Expr) | 
| Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 127 | Inst.addOperand(MCOperand::createImm(0)); | 
| Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 128 | else if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) | 
| Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 129 | Inst.addOperand(MCOperand::createImm(CE->getValue())); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 130 | else | 
| Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 131 | Inst.addOperand(MCOperand::createExpr(Expr)); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
|  | 134 | public: | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 135 | SystemZOperand(OperandKind kind, SMLoc startLoc, SMLoc endLoc) | 
|  | 136 | : Kind(kind), StartLoc(startLoc), EndLoc(endLoc) {} | 
|  | 137 |  | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 138 | // Create particular kinds of operand. | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 139 | static std::unique_ptr<SystemZOperand> createInvalid(SMLoc StartLoc, | 
|  | 140 | SMLoc EndLoc) { | 
|  | 141 | return make_unique<SystemZOperand>(KindInvalid, StartLoc, EndLoc); | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 142 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 143 | static std::unique_ptr<SystemZOperand> createToken(StringRef Str, SMLoc Loc) { | 
|  | 144 | auto Op = make_unique<SystemZOperand>(KindToken, Loc, Loc); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 145 | Op->Token.Data = Str.data(); | 
|  | 146 | Op->Token.Length = Str.size(); | 
|  | 147 | return Op; | 
|  | 148 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 149 | static std::unique_ptr<SystemZOperand> | 
|  | 150 | createReg(RegisterKind Kind, unsigned Num, SMLoc StartLoc, SMLoc EndLoc) { | 
|  | 151 | auto Op = make_unique<SystemZOperand>(KindReg, StartLoc, EndLoc); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 152 | Op->Reg.Kind = Kind; | 
|  | 153 | Op->Reg.Num = Num; | 
|  | 154 | return Op; | 
|  | 155 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 156 | static std::unique_ptr<SystemZOperand> | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 157 | createImm(const MCExpr *Expr, SMLoc StartLoc, SMLoc EndLoc) { | 
|  | 158 | auto Op = make_unique<SystemZOperand>(KindImm, StartLoc, EndLoc); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 159 | Op->Imm = Expr; | 
|  | 160 | return Op; | 
|  | 161 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 162 | static std::unique_ptr<SystemZOperand> | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 163 | createMem(MemoryKind MemKind, RegisterKind RegKind, unsigned Base, | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 164 | const MCExpr *Disp, unsigned Index, const MCExpr *LengthImm, | 
|  | 165 | unsigned LengthReg, SMLoc StartLoc, SMLoc EndLoc) { | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 166 | auto Op = make_unique<SystemZOperand>(KindMem, StartLoc, EndLoc); | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 167 | Op->Mem.MemKind = MemKind; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 168 | Op->Mem.RegKind = RegKind; | 
|  | 169 | Op->Mem.Base = Base; | 
|  | 170 | Op->Mem.Index = Index; | 
|  | 171 | Op->Mem.Disp = Disp; | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 172 | if (MemKind == BDLMem) | 
|  | 173 | Op->Mem.Length.Imm = LengthImm; | 
|  | 174 | if (MemKind == BDRMem) | 
|  | 175 | Op->Mem.Length.Reg = LengthReg; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 176 | return Op; | 
|  | 177 | } | 
| Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 178 | static std::unique_ptr<SystemZOperand> | 
|  | 179 | createImmTLS(const MCExpr *Imm, const MCExpr *Sym, | 
|  | 180 | SMLoc StartLoc, SMLoc EndLoc) { | 
|  | 181 | auto Op = make_unique<SystemZOperand>(KindImmTLS, StartLoc, EndLoc); | 
|  | 182 | Op->ImmTLS.Imm = Imm; | 
|  | 183 | Op->ImmTLS.Sym = Sym; | 
|  | 184 | return Op; | 
|  | 185 | } | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 186 |  | 
|  | 187 | // Token operands | 
| Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 188 | bool isToken() const override { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 189 | return Kind == KindToken; | 
|  | 190 | } | 
|  | 191 | StringRef getToken() const { | 
|  | 192 | assert(Kind == KindToken && "Not a token"); | 
|  | 193 | return StringRef(Token.Data, Token.Length); | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | // Register operands. | 
| Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 197 | bool isReg() const override { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 198 | return Kind == KindReg; | 
|  | 199 | } | 
|  | 200 | bool isReg(RegisterKind RegKind) const { | 
|  | 201 | return Kind == KindReg && Reg.Kind == RegKind; | 
|  | 202 | } | 
| Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 203 | unsigned getReg() const override { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 204 | assert(Kind == KindReg && "Not a register"); | 
|  | 205 | return Reg.Num; | 
|  | 206 | } | 
|  | 207 |  | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 208 | // Immediate operands. | 
| Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 209 | bool isImm() const override { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 210 | return Kind == KindImm; | 
|  | 211 | } | 
|  | 212 | bool isImm(int64_t MinValue, int64_t MaxValue) const { | 
|  | 213 | return Kind == KindImm && inRange(Imm, MinValue, MaxValue); | 
|  | 214 | } | 
|  | 215 | const MCExpr *getImm() const { | 
|  | 216 | assert(Kind == KindImm && "Not an immediate"); | 
|  | 217 | return Imm; | 
|  | 218 | } | 
|  | 219 |  | 
| Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 220 | // Immediate operands with optional TLS symbol. | 
|  | 221 | bool isImmTLS() const { | 
|  | 222 | return Kind == KindImmTLS; | 
|  | 223 | } | 
|  | 224 |  | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 225 | // Memory operands. | 
| Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 226 | bool isMem() const override { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 227 | return Kind == KindMem; | 
|  | 228 | } | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 229 | bool isMem(MemoryKind MemKind) const { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 230 | return (Kind == KindMem && | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 231 | (Mem.MemKind == MemKind || | 
|  | 232 | // A BDMem can be treated as a BDXMem in which the index | 
|  | 233 | // register field is 0. | 
|  | 234 | (Mem.MemKind == BDMem && MemKind == BDXMem))); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 235 | } | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 236 | bool isMem(MemoryKind MemKind, RegisterKind RegKind) const { | 
|  | 237 | return isMem(MemKind) && Mem.RegKind == RegKind; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 238 | } | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 239 | bool isMemDisp12(MemoryKind MemKind, RegisterKind RegKind) const { | 
|  | 240 | return isMem(MemKind, RegKind) && inRange(Mem.Disp, 0, 0xfff); | 
|  | 241 | } | 
|  | 242 | bool isMemDisp20(MemoryKind MemKind, RegisterKind RegKind) const { | 
|  | 243 | return isMem(MemKind, RegKind) && inRange(Mem.Disp, -524288, 524287); | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 244 | } | 
|  | 245 | bool isMemDisp12Len8(RegisterKind RegKind) const { | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 246 | return isMemDisp12(BDLMem, RegKind) && inRange(Mem.Length.Imm, 1, 0x100); | 
| Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame] | 247 | } | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 248 |  | 
|  | 249 | // Override MCParsedAsmOperand. | 
| Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 250 | SMLoc getStartLoc() const override { return StartLoc; } | 
| Peter Collingbourne | 0da8630 | 2016-10-10 22:49:37 +0000 | [diff] [blame] | 251 | SMLoc getEndLoc() const override { return EndLoc; } | 
| Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 252 | void print(raw_ostream &OS) const override; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 253 |  | 
|  | 254 | // Used by the TableGen code to add particular types of operand | 
|  | 255 | // to an instruction. | 
|  | 256 | void addRegOperands(MCInst &Inst, unsigned N) const { | 
|  | 257 | assert(N == 1 && "Invalid number of operands"); | 
| Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 258 | Inst.addOperand(MCOperand::createReg(getReg())); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 259 | } | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 260 | void addImmOperands(MCInst &Inst, unsigned N) const { | 
|  | 261 | assert(N == 1 && "Invalid number of operands"); | 
|  | 262 | addExpr(Inst, getImm()); | 
|  | 263 | } | 
|  | 264 | void addBDAddrOperands(MCInst &Inst, unsigned N) const { | 
|  | 265 | assert(N == 2 && "Invalid number of operands"); | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 266 | assert(isMem(BDMem) && "Invalid operand type"); | 
| Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 267 | Inst.addOperand(MCOperand::createReg(Mem.Base)); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 268 | addExpr(Inst, Mem.Disp); | 
|  | 269 | } | 
|  | 270 | void addBDXAddrOperands(MCInst &Inst, unsigned N) const { | 
|  | 271 | assert(N == 3 && "Invalid number of operands"); | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 272 | assert(isMem(BDXMem) && "Invalid operand type"); | 
| Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 273 | Inst.addOperand(MCOperand::createReg(Mem.Base)); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 274 | addExpr(Inst, Mem.Disp); | 
| Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 275 | Inst.addOperand(MCOperand::createReg(Mem.Index)); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 276 | } | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 277 | void addBDLAddrOperands(MCInst &Inst, unsigned N) const { | 
|  | 278 | assert(N == 3 && "Invalid number of operands"); | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 279 | assert(isMem(BDLMem) && "Invalid operand type"); | 
| Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 280 | Inst.addOperand(MCOperand::createReg(Mem.Base)); | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 281 | addExpr(Inst, Mem.Disp); | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 282 | addExpr(Inst, Mem.Length.Imm); | 
|  | 283 | } | 
|  | 284 | void addBDRAddrOperands(MCInst &Inst, unsigned N) const { | 
|  | 285 | assert(N == 3 && "Invalid number of operands"); | 
|  | 286 | assert(isMem(BDRMem) && "Invalid operand type"); | 
|  | 287 | Inst.addOperand(MCOperand::createReg(Mem.Base)); | 
|  | 288 | addExpr(Inst, Mem.Disp); | 
|  | 289 | Inst.addOperand(MCOperand::createReg(Mem.Length.Reg)); | 
|  | 290 | } | 
|  | 291 | void addBDVAddrOperands(MCInst &Inst, unsigned N) const { | 
|  | 292 | assert(N == 3 && "Invalid number of operands"); | 
|  | 293 | assert(isMem(BDVMem) && "Invalid operand type"); | 
|  | 294 | Inst.addOperand(MCOperand::createReg(Mem.Base)); | 
|  | 295 | addExpr(Inst, Mem.Disp); | 
|  | 296 | Inst.addOperand(MCOperand::createReg(Mem.Index)); | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 297 | } | 
| Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 298 | void addImmTLSOperands(MCInst &Inst, unsigned N) const { | 
|  | 299 | assert(N == 2 && "Invalid number of operands"); | 
|  | 300 | assert(Kind == KindImmTLS && "Invalid operand type"); | 
|  | 301 | addExpr(Inst, ImmTLS.Imm); | 
|  | 302 | if (ImmTLS.Sym) | 
|  | 303 | addExpr(Inst, ImmTLS.Sym); | 
|  | 304 | } | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 305 |  | 
|  | 306 | // Used by the TableGen code to check for particular operand types. | 
|  | 307 | bool isGR32() const { return isReg(GR32Reg); } | 
| Richard Sandiford | f949606 | 2013-09-30 10:45:16 +0000 | [diff] [blame] | 308 | bool isGRH32() const { return isReg(GRH32Reg); } | 
| Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 309 | bool isGRX32() const { return false; } | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 310 | bool isGR64() const { return isReg(GR64Reg); } | 
|  | 311 | bool isGR128() const { return isReg(GR128Reg); } | 
|  | 312 | bool isADDR32() const { return isReg(ADDR32Reg); } | 
|  | 313 | bool isADDR64() const { return isReg(ADDR64Reg); } | 
|  | 314 | bool isADDR128() const { return false; } | 
|  | 315 | bool isFP32() const { return isReg(FP32Reg); } | 
|  | 316 | bool isFP64() const { return isReg(FP64Reg); } | 
|  | 317 | bool isFP128() const { return isReg(FP128Reg); } | 
| Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame] | 318 | bool isVR32() const { return isReg(VR32Reg); } | 
|  | 319 | bool isVR64() const { return isReg(VR64Reg); } | 
|  | 320 | bool isVF128() const { return false; } | 
|  | 321 | bool isVR128() const { return isReg(VR128Reg); } | 
| Ulrich Weigand | fffc711 | 2016-11-08 20:15:26 +0000 | [diff] [blame] | 322 | bool isAR32() const { return isReg(AR32Reg); } | 
| Zhan Jun Liau | 4fbc3f4 | 2016-08-08 15:13:08 +0000 | [diff] [blame] | 323 | bool isAnyReg() const { return (isReg() || isImm(0, 15)); } | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 324 | bool isBDAddr32Disp12() const { return isMemDisp12(BDMem, ADDR32Reg); } | 
|  | 325 | bool isBDAddr32Disp20() const { return isMemDisp20(BDMem, ADDR32Reg); } | 
|  | 326 | bool isBDAddr64Disp12() const { return isMemDisp12(BDMem, ADDR64Reg); } | 
|  | 327 | bool isBDAddr64Disp20() const { return isMemDisp20(BDMem, ADDR64Reg); } | 
|  | 328 | bool isBDXAddr64Disp12() const { return isMemDisp12(BDXMem, ADDR64Reg); } | 
|  | 329 | bool isBDXAddr64Disp20() const { return isMemDisp20(BDXMem, ADDR64Reg); } | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 330 | bool isBDLAddr64Disp12Len8() const { return isMemDisp12Len8(ADDR64Reg); } | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 331 | bool isBDRAddr64Disp12() const { return isMemDisp12(BDRMem, ADDR64Reg); } | 
| Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame] | 332 | bool isBDVAddr64Disp12() const { return isMemDisp12(BDVMem, ADDR64Reg); } | 
|  | 333 | bool isU1Imm() const { return isImm(0, 1); } | 
|  | 334 | bool isU2Imm() const { return isImm(0, 3); } | 
|  | 335 | bool isU3Imm() const { return isImm(0, 7); } | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 336 | bool isU4Imm() const { return isImm(0, 15); } | 
|  | 337 | bool isU6Imm() const { return isImm(0, 63); } | 
|  | 338 | bool isU8Imm() const { return isImm(0, 255); } | 
|  | 339 | bool isS8Imm() const { return isImm(-128, 127); } | 
| Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame] | 340 | bool isU12Imm() const { return isImm(0, 4095); } | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 341 | bool isU16Imm() const { return isImm(0, 65535); } | 
|  | 342 | bool isS16Imm() const { return isImm(-32768, 32767); } | 
|  | 343 | bool isU32Imm() const { return isImm(0, (1LL << 32) - 1); } | 
|  | 344 | bool isS32Imm() const { return isImm(-(1LL << 31), (1LL << 31) - 1); } | 
| Zhan Jun Liau | 4fbc3f4 | 2016-08-08 15:13:08 +0000 | [diff] [blame] | 345 | bool isU48Imm() const { return isImm(0, (1LL << 48) - 1); } | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 346 | }; | 
|  | 347 |  | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 348 | class SystemZAsmParser : public MCTargetAsmParser { | 
|  | 349 | #define GET_ASSEMBLER_HEADER | 
|  | 350 | #include "SystemZGenAsmMatcher.inc" | 
|  | 351 |  | 
|  | 352 | private: | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 353 | MCAsmParser &Parser; | 
| Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 354 | enum RegisterGroup { | 
|  | 355 | RegGR, | 
|  | 356 | RegFP, | 
| Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame] | 357 | RegV, | 
| Ulrich Weigand | fffc711 | 2016-11-08 20:15:26 +0000 | [diff] [blame] | 358 | RegAR | 
| Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 359 | }; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 360 | struct Register { | 
| Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 361 | RegisterGroup Group; | 
|  | 362 | unsigned Num; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 363 | SMLoc StartLoc, EndLoc; | 
|  | 364 | }; | 
|  | 365 |  | 
|  | 366 | bool parseRegister(Register &Reg); | 
|  | 367 |  | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 368 | bool parseRegister(Register &Reg, RegisterGroup Group, const unsigned *Regs, | 
|  | 369 | bool IsAddress = false); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 370 |  | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 371 | OperandMatchResultTy parseRegister(OperandVector &Operands, | 
|  | 372 | RegisterGroup Group, const unsigned *Regs, | 
|  | 373 | RegisterKind Kind); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 374 |  | 
| Zhan Jun Liau | 4fbc3f4 | 2016-08-08 15:13:08 +0000 | [diff] [blame] | 375 | OperandMatchResultTy parseAnyRegister(OperandVector &Operands); | 
|  | 376 |  | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 377 | bool parseAddress(bool &HaveReg1, Register &Reg1, | 
|  | 378 | bool &HaveReg2, Register &Reg2, | 
|  | 379 | const MCExpr *&Disp, const MCExpr *&Length); | 
|  | 380 | bool parseAddressRegister(Register &Reg); | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 381 |  | 
| Zhan Jun Liau | 4fbc3f4 | 2016-08-08 15:13:08 +0000 | [diff] [blame] | 382 | bool ParseDirectiveInsn(SMLoc L); | 
|  | 383 |  | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 384 | OperandMatchResultTy parseAddress(OperandVector &Operands, | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 385 | MemoryKind MemKind, const unsigned *Regs, | 
|  | 386 | RegisterKind RegKind); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 387 |  | 
| Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 388 | OperandMatchResultTy parsePCRel(OperandVector &Operands, int64_t MinVal, | 
|  | 389 | int64_t MaxVal, bool AllowTLS); | 
|  | 390 |  | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 391 | bool parseOperand(OperandVector &Operands, StringRef Mnemonic); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 392 |  | 
|  | 393 | public: | 
| Akira Hatanaka | b11ef08 | 2015-11-14 06:35:56 +0000 | [diff] [blame] | 394 | SystemZAsmParser(const MCSubtargetInfo &sti, MCAsmParser &parser, | 
| Evgeniy Stepanov | 0a951b7 | 2014-04-23 11:16:03 +0000 | [diff] [blame] | 395 | const MCInstrInfo &MII, | 
|  | 396 | const MCTargetOptions &Options) | 
| Akira Hatanaka | bd9fc28 | 2015-11-14 05:20:05 +0000 | [diff] [blame] | 397 | : MCTargetAsmParser(Options, sti), Parser(parser) { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 398 | MCAsmParserExtension::Initialize(Parser); | 
|  | 399 |  | 
| Zhan Jun Liau | 7d4d436 | 2016-07-08 16:50:02 +0000 | [diff] [blame] | 400 | // Alias the .word directive to .short. | 
|  | 401 | parser.addAliasForDirective(".word", ".short"); | 
|  | 402 |  | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 403 | // Initialize the set of available features. | 
| Akira Hatanaka | bd9fc28 | 2015-11-14 05:20:05 +0000 | [diff] [blame] | 404 | setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits())); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 405 | } | 
|  | 406 |  | 
|  | 407 | // Override MCTargetAsmParser. | 
| Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 408 | bool ParseDirective(AsmToken DirectiveID) override; | 
|  | 409 | bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override; | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 410 | bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name, | 
|  | 411 | SMLoc NameLoc, OperandVector &Operands) override; | 
| Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 412 | bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 413 | OperandVector &Operands, MCStreamer &Out, | 
| Tim Northover | 26bb14e | 2014-08-18 11:49:42 +0000 | [diff] [blame] | 414 | uint64_t &ErrorInfo, | 
| Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 415 | bool MatchingInlineAsm) override; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 416 |  | 
|  | 417 | // Used by the TableGen code to parse particular operand types. | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 418 | OperandMatchResultTy parseGR32(OperandVector &Operands) { | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 419 | return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, GR32Reg); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 420 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 421 | OperandMatchResultTy parseGRH32(OperandVector &Operands) { | 
| Richard Sandiford | f949606 | 2013-09-30 10:45:16 +0000 | [diff] [blame] | 422 | return parseRegister(Operands, RegGR, SystemZMC::GRH32Regs, GRH32Reg); | 
|  | 423 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 424 | OperandMatchResultTy parseGRX32(OperandVector &Operands) { | 
| Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 425 | llvm_unreachable("GRX32 should only be used for pseudo instructions"); | 
|  | 426 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 427 | OperandMatchResultTy parseGR64(OperandVector &Operands) { | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 428 | return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, GR64Reg); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 429 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 430 | OperandMatchResultTy parseGR128(OperandVector &Operands) { | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 431 | return parseRegister(Operands, RegGR, SystemZMC::GR128Regs, GR128Reg); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 432 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 433 | OperandMatchResultTy parseADDR32(OperandVector &Operands) { | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 434 | return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, ADDR32Reg); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 435 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 436 | OperandMatchResultTy parseADDR64(OperandVector &Operands) { | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 437 | return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, ADDR64Reg); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 438 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 439 | OperandMatchResultTy parseADDR128(OperandVector &Operands) { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 440 | llvm_unreachable("Shouldn't be used as an operand"); | 
|  | 441 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 442 | OperandMatchResultTy parseFP32(OperandVector &Operands) { | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 443 | return parseRegister(Operands, RegFP, SystemZMC::FP32Regs, FP32Reg); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 444 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 445 | OperandMatchResultTy parseFP64(OperandVector &Operands) { | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 446 | return parseRegister(Operands, RegFP, SystemZMC::FP64Regs, FP64Reg); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 447 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 448 | OperandMatchResultTy parseFP128(OperandVector &Operands) { | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 449 | return parseRegister(Operands, RegFP, SystemZMC::FP128Regs, FP128Reg); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 450 | } | 
| Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame] | 451 | OperandMatchResultTy parseVR32(OperandVector &Operands) { | 
|  | 452 | return parseRegister(Operands, RegV, SystemZMC::VR32Regs, VR32Reg); | 
|  | 453 | } | 
|  | 454 | OperandMatchResultTy parseVR64(OperandVector &Operands) { | 
|  | 455 | return parseRegister(Operands, RegV, SystemZMC::VR64Regs, VR64Reg); | 
|  | 456 | } | 
|  | 457 | OperandMatchResultTy parseVF128(OperandVector &Operands) { | 
|  | 458 | llvm_unreachable("Shouldn't be used as an operand"); | 
|  | 459 | } | 
|  | 460 | OperandMatchResultTy parseVR128(OperandVector &Operands) { | 
|  | 461 | return parseRegister(Operands, RegV, SystemZMC::VR128Regs, VR128Reg); | 
|  | 462 | } | 
| Ulrich Weigand | fffc711 | 2016-11-08 20:15:26 +0000 | [diff] [blame] | 463 | OperandMatchResultTy parseAR32(OperandVector &Operands) { | 
|  | 464 | return parseRegister(Operands, RegAR, SystemZMC::AR32Regs, AR32Reg); | 
|  | 465 | } | 
| Zhan Jun Liau | 4fbc3f4 | 2016-08-08 15:13:08 +0000 | [diff] [blame] | 466 | OperandMatchResultTy parseAnyReg(OperandVector &Operands) { | 
|  | 467 | return parseAnyRegister(Operands); | 
|  | 468 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 469 | OperandMatchResultTy parseBDAddr32(OperandVector &Operands) { | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 470 | return parseAddress(Operands, BDMem, SystemZMC::GR32Regs, ADDR32Reg); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 471 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 472 | OperandMatchResultTy parseBDAddr64(OperandVector &Operands) { | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 473 | return parseAddress(Operands, BDMem, SystemZMC::GR64Regs, ADDR64Reg); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 474 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 475 | OperandMatchResultTy parseBDXAddr64(OperandVector &Operands) { | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 476 | return parseAddress(Operands, BDXMem, SystemZMC::GR64Regs, ADDR64Reg); | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 477 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 478 | OperandMatchResultTy parseBDLAddr64(OperandVector &Operands) { | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 479 | return parseAddress(Operands, BDLMem, SystemZMC::GR64Regs, ADDR64Reg); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 480 | } | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 481 | OperandMatchResultTy parseBDRAddr64(OperandVector &Operands) { | 
|  | 482 | return parseAddress(Operands, BDRMem, SystemZMC::GR64Regs, ADDR64Reg); | 
|  | 483 | } | 
| Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame] | 484 | OperandMatchResultTy parseBDVAddr64(OperandVector &Operands) { | 
|  | 485 | return parseAddress(Operands, BDVMem, SystemZMC::GR64Regs, ADDR64Reg); | 
|  | 486 | } | 
| Ulrich Weigand | 84404f3 | 2016-11-28 14:01:51 +0000 | [diff] [blame] | 487 | OperandMatchResultTy parsePCRel12(OperandVector &Operands) { | 
|  | 488 | return parsePCRel(Operands, -(1LL << 12), (1LL << 12) - 1, false); | 
|  | 489 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 490 | OperandMatchResultTy parsePCRel16(OperandVector &Operands) { | 
| Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 491 | return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1, false); | 
| Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 492 | } | 
| Ulrich Weigand | 84404f3 | 2016-11-28 14:01:51 +0000 | [diff] [blame] | 493 | OperandMatchResultTy parsePCRel24(OperandVector &Operands) { | 
|  | 494 | return parsePCRel(Operands, -(1LL << 24), (1LL << 24) - 1, false); | 
|  | 495 | } | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 496 | OperandMatchResultTy parsePCRel32(OperandVector &Operands) { | 
| Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 497 | return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1, false); | 
|  | 498 | } | 
|  | 499 | OperandMatchResultTy parsePCRelTLS16(OperandVector &Operands) { | 
|  | 500 | return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1, true); | 
|  | 501 | } | 
|  | 502 | OperandMatchResultTy parsePCRelTLS32(OperandVector &Operands) { | 
|  | 503 | return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1, true); | 
| Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 504 | } | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 505 | }; | 
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 506 | } // end anonymous namespace | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 507 |  | 
|  | 508 | #define GET_REGISTER_MATCHER | 
|  | 509 | #define GET_SUBTARGET_FEATURE_NAME | 
|  | 510 | #define GET_MATCHER_IMPLEMENTATION | 
|  | 511 | #include "SystemZGenAsmMatcher.inc" | 
|  | 512 |  | 
| Zhan Jun Liau | 4fbc3f4 | 2016-08-08 15:13:08 +0000 | [diff] [blame] | 513 | // Used for the .insn directives; contains information needed to parse the | 
|  | 514 | // operands in the directive. | 
|  | 515 | struct InsnMatchEntry { | 
|  | 516 | StringRef Format; | 
|  | 517 | uint64_t Opcode; | 
|  | 518 | int32_t NumOperands; | 
|  | 519 | MatchClassKind OperandKinds[5]; | 
|  | 520 | }; | 
|  | 521 |  | 
|  | 522 | // For equal_range comparison. | 
|  | 523 | struct CompareInsn { | 
|  | 524 | bool operator() (const InsnMatchEntry &LHS, StringRef RHS) { | 
|  | 525 | return LHS.Format < RHS; | 
|  | 526 | } | 
|  | 527 | bool operator() (StringRef LHS, const InsnMatchEntry &RHS) { | 
|  | 528 | return LHS < RHS.Format; | 
|  | 529 | } | 
| Roger Ferrer Ibanez | 1758658 | 2016-08-10 16:39:58 +0000 | [diff] [blame] | 530 | bool operator() (const InsnMatchEntry &LHS, const InsnMatchEntry &RHS) { | 
|  | 531 | return LHS.Format < RHS.Format; | 
|  | 532 | } | 
| Zhan Jun Liau | 4fbc3f4 | 2016-08-08 15:13:08 +0000 | [diff] [blame] | 533 | }; | 
|  | 534 |  | 
|  | 535 | // Table initializing information for parsing the .insn directive. | 
|  | 536 | static struct InsnMatchEntry InsnMatchTable[] = { | 
|  | 537 | /* Format, Opcode, NumOperands, OperandKinds */ | 
|  | 538 | { "e", SystemZ::InsnE, 1, | 
|  | 539 | { MCK_U16Imm } }, | 
|  | 540 | { "ri", SystemZ::InsnRI, 3, | 
|  | 541 | { MCK_U32Imm, MCK_AnyReg, MCK_S16Imm } }, | 
|  | 542 | { "rie", SystemZ::InsnRIE, 4, | 
|  | 543 | { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_PCRel16 } }, | 
|  | 544 | { "ril", SystemZ::InsnRIL, 3, | 
|  | 545 | { MCK_U48Imm, MCK_AnyReg, MCK_PCRel32 } }, | 
|  | 546 | { "rilu", SystemZ::InsnRILU, 3, | 
|  | 547 | { MCK_U48Imm, MCK_AnyReg, MCK_U32Imm } }, | 
|  | 548 | { "ris", SystemZ::InsnRIS, 5, | 
|  | 549 | { MCK_U48Imm, MCK_AnyReg, MCK_S8Imm, MCK_U4Imm, MCK_BDAddr64Disp12 } }, | 
|  | 550 | { "rr", SystemZ::InsnRR, 3, | 
|  | 551 | { MCK_U16Imm, MCK_AnyReg, MCK_AnyReg } }, | 
|  | 552 | { "rre", SystemZ::InsnRRE, 3, | 
|  | 553 | { MCK_U32Imm, MCK_AnyReg, MCK_AnyReg } }, | 
|  | 554 | { "rrf", SystemZ::InsnRRF, 5, | 
|  | 555 | { MCK_U32Imm, MCK_AnyReg, MCK_AnyReg, MCK_AnyReg, MCK_U4Imm } }, | 
|  | 556 | { "rrs", SystemZ::InsnRRS, 5, | 
|  | 557 | { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_U4Imm, MCK_BDAddr64Disp12 } }, | 
|  | 558 | { "rs", SystemZ::InsnRS, 4, | 
|  | 559 | { MCK_U32Imm, MCK_AnyReg, MCK_AnyReg, MCK_BDAddr64Disp12 } }, | 
|  | 560 | { "rse", SystemZ::InsnRSE, 4, | 
|  | 561 | { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_BDAddr64Disp12 } }, | 
|  | 562 | { "rsi", SystemZ::InsnRSI, 4, | 
|  | 563 | { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_PCRel16 } }, | 
|  | 564 | { "rsy", SystemZ::InsnRSY, 4, | 
|  | 565 | { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_BDAddr64Disp20 } }, | 
|  | 566 | { "rx", SystemZ::InsnRX, 3, | 
|  | 567 | { MCK_U32Imm, MCK_AnyReg, MCK_BDXAddr64Disp12 } }, | 
|  | 568 | { "rxe", SystemZ::InsnRXE, 3, | 
|  | 569 | { MCK_U48Imm, MCK_AnyReg, MCK_BDXAddr64Disp12 } }, | 
|  | 570 | { "rxf", SystemZ::InsnRXF, 4, | 
|  | 571 | { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_BDXAddr64Disp12 } }, | 
|  | 572 | { "rxy", SystemZ::InsnRXY, 3, | 
|  | 573 | { MCK_U48Imm, MCK_AnyReg, MCK_BDXAddr64Disp20 } }, | 
|  | 574 | { "s", SystemZ::InsnS, 2, | 
|  | 575 | { MCK_U32Imm, MCK_BDAddr64Disp12 } }, | 
|  | 576 | { "si", SystemZ::InsnSI, 3, | 
|  | 577 | { MCK_U32Imm, MCK_BDAddr64Disp12, MCK_S8Imm } }, | 
|  | 578 | { "sil", SystemZ::InsnSIL, 3, | 
|  | 579 | { MCK_U48Imm, MCK_BDAddr64Disp12, MCK_U16Imm } }, | 
|  | 580 | { "siy", SystemZ::InsnSIY, 3, | 
|  | 581 | { MCK_U48Imm, MCK_BDAddr64Disp20, MCK_U8Imm } }, | 
|  | 582 | { "ss", SystemZ::InsnSS, 4, | 
|  | 583 | { MCK_U48Imm, MCK_BDXAddr64Disp12, MCK_BDAddr64Disp12, MCK_AnyReg } }, | 
|  | 584 | { "sse", SystemZ::InsnSSE, 3, | 
|  | 585 | { MCK_U48Imm, MCK_BDAddr64Disp12, MCK_BDAddr64Disp12 } }, | 
|  | 586 | { "ssf", SystemZ::InsnSSF, 4, | 
|  | 587 | { MCK_U48Imm, MCK_BDAddr64Disp12, MCK_BDAddr64Disp12, MCK_AnyReg } } | 
|  | 588 | }; | 
|  | 589 |  | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 590 | void SystemZOperand::print(raw_ostream &OS) const { | 
|  | 591 | llvm_unreachable("Not implemented"); | 
|  | 592 | } | 
|  | 593 |  | 
|  | 594 | // Parse one register of the form %<prefix><number>. | 
|  | 595 | bool SystemZAsmParser::parseRegister(Register &Reg) { | 
|  | 596 | Reg.StartLoc = Parser.getTok().getLoc(); | 
|  | 597 |  | 
|  | 598 | // Eat the % prefix. | 
|  | 599 | if (Parser.getTok().isNot(AsmToken::Percent)) | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 600 | return Error(Parser.getTok().getLoc(), "register expected"); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 601 | Parser.Lex(); | 
|  | 602 |  | 
|  | 603 | // Expect a register name. | 
|  | 604 | if (Parser.getTok().isNot(AsmToken::Identifier)) | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 605 | return Error(Reg.StartLoc, "invalid register"); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 606 |  | 
| Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 607 | // Check that there's a prefix. | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 608 | StringRef Name = Parser.getTok().getString(); | 
|  | 609 | if (Name.size() < 2) | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 610 | return Error(Reg.StartLoc, "invalid register"); | 
| Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 611 | char Prefix = Name[0]; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 612 |  | 
|  | 613 | // Treat the rest of the register name as a register number. | 
| Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 614 | if (Name.substr(1).getAsInteger(10, Reg.Num)) | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 615 | return Error(Reg.StartLoc, "invalid register"); | 
| Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 616 |  | 
|  | 617 | // Look for valid combinations of prefix and number. | 
|  | 618 | if (Prefix == 'r' && Reg.Num < 16) | 
|  | 619 | Reg.Group = RegGR; | 
|  | 620 | else if (Prefix == 'f' && Reg.Num < 16) | 
|  | 621 | Reg.Group = RegFP; | 
| Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame] | 622 | else if (Prefix == 'v' && Reg.Num < 32) | 
|  | 623 | Reg.Group = RegV; | 
| Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 624 | else if (Prefix == 'a' && Reg.Num < 16) | 
| Ulrich Weigand | fffc711 | 2016-11-08 20:15:26 +0000 | [diff] [blame] | 625 | Reg.Group = RegAR; | 
| Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 626 | else | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 627 | return Error(Reg.StartLoc, "invalid register"); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 628 |  | 
|  | 629 | Reg.EndLoc = Parser.getTok().getLoc(); | 
|  | 630 | Parser.Lex(); | 
|  | 631 | return false; | 
|  | 632 | } | 
|  | 633 |  | 
| Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 634 | // Parse a register of group Group.  If Regs is nonnull, use it to map | 
| Jonas Paulsson | 0a9049b | 2015-10-09 07:19:12 +0000 | [diff] [blame] | 635 | // the raw register number to LLVM numbering, with zero entries | 
|  | 636 | // indicating an invalid register.  IsAddress says whether the | 
|  | 637 | // register appears in an address context. Allow FP Group if expecting | 
|  | 638 | // RegV Group, since the f-prefix yields the FP group even while used | 
|  | 639 | // with vector instructions. | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 640 | bool SystemZAsmParser::parseRegister(Register &Reg, RegisterGroup Group, | 
|  | 641 | const unsigned *Regs, bool IsAddress) { | 
|  | 642 | if (parseRegister(Reg)) | 
|  | 643 | return true; | 
| Jonas Paulsson | 0a9049b | 2015-10-09 07:19:12 +0000 | [diff] [blame] | 644 | if (Reg.Group != Group && !(Reg.Group == RegFP && Group == RegV)) | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 645 | return Error(Reg.StartLoc, "invalid operand for instruction"); | 
|  | 646 | if (Regs && Regs[Reg.Num] == 0) | 
|  | 647 | return Error(Reg.StartLoc, "invalid register pair"); | 
|  | 648 | if (Reg.Num == 0 && IsAddress) | 
|  | 649 | return Error(Reg.StartLoc, "%r0 used in an address"); | 
| Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 650 | if (Regs) | 
|  | 651 | Reg.Num = Regs[Reg.Num]; | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 652 | return false; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 653 | } | 
|  | 654 |  | 
| Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 655 | // Parse a register and add it to Operands.  The other arguments are as above. | 
| Alex Bradbury | 58eba09 | 2016-11-01 16:32:05 +0000 | [diff] [blame] | 656 | OperandMatchResultTy | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 657 | SystemZAsmParser::parseRegister(OperandVector &Operands, RegisterGroup Group, | 
|  | 658 | const unsigned *Regs, RegisterKind Kind) { | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 659 | if (Parser.getTok().isNot(AsmToken::Percent)) | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 660 | return MatchOperand_NoMatch; | 
|  | 661 |  | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 662 | Register Reg; | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 663 | bool IsAddress = (Kind == ADDR32Reg || Kind == ADDR64Reg); | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 664 | if (parseRegister(Reg, Group, Regs, IsAddress)) | 
|  | 665 | return MatchOperand_ParseFail; | 
|  | 666 |  | 
|  | 667 | Operands.push_back(SystemZOperand::createReg(Kind, Reg.Num, | 
|  | 668 | Reg.StartLoc, Reg.EndLoc)); | 
|  | 669 | return MatchOperand_Success; | 
|  | 670 | } | 
|  | 671 |  | 
| Zhan Jun Liau | 4fbc3f4 | 2016-08-08 15:13:08 +0000 | [diff] [blame] | 672 | // Parse any type of register (including integers) and add it to Operands. | 
| Alex Bradbury | 58eba09 | 2016-11-01 16:32:05 +0000 | [diff] [blame] | 673 | OperandMatchResultTy | 
| Zhan Jun Liau | 4fbc3f4 | 2016-08-08 15:13:08 +0000 | [diff] [blame] | 674 | SystemZAsmParser::parseAnyRegister(OperandVector &Operands) { | 
|  | 675 | // Handle integer values. | 
|  | 676 | if (Parser.getTok().is(AsmToken::Integer)) { | 
|  | 677 | const MCExpr *Register; | 
|  | 678 | SMLoc StartLoc = Parser.getTok().getLoc(); | 
|  | 679 | if (Parser.parseExpression(Register)) | 
|  | 680 | return MatchOperand_ParseFail; | 
|  | 681 |  | 
|  | 682 | if (auto *CE = dyn_cast<MCConstantExpr>(Register)) { | 
|  | 683 | int64_t Value = CE->getValue(); | 
|  | 684 | if (Value < 0 || Value > 15) { | 
|  | 685 | Error(StartLoc, "invalid register"); | 
|  | 686 | return MatchOperand_ParseFail; | 
|  | 687 | } | 
|  | 688 | } | 
|  | 689 |  | 
|  | 690 | SMLoc EndLoc = | 
|  | 691 | SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); | 
|  | 692 |  | 
|  | 693 | Operands.push_back(SystemZOperand::createImm(Register, StartLoc, EndLoc)); | 
|  | 694 | } | 
|  | 695 | else { | 
|  | 696 | Register Reg; | 
|  | 697 | if (parseRegister(Reg)) | 
|  | 698 | return MatchOperand_ParseFail; | 
|  | 699 |  | 
|  | 700 | // Map to the correct register kind. | 
|  | 701 | RegisterKind Kind; | 
|  | 702 | unsigned RegNo; | 
|  | 703 | if (Reg.Group == RegGR) { | 
|  | 704 | Kind = GR64Reg; | 
|  | 705 | RegNo = SystemZMC::GR64Regs[Reg.Num]; | 
|  | 706 | } | 
|  | 707 | else if (Reg.Group == RegFP) { | 
|  | 708 | Kind = FP64Reg; | 
|  | 709 | RegNo = SystemZMC::FP64Regs[Reg.Num]; | 
|  | 710 | } | 
|  | 711 | else if (Reg.Group == RegV) { | 
|  | 712 | Kind = VR128Reg; | 
|  | 713 | RegNo = SystemZMC::VR128Regs[Reg.Num]; | 
|  | 714 | } | 
| Ulrich Weigand | fffc711 | 2016-11-08 20:15:26 +0000 | [diff] [blame] | 715 | else if (Reg.Group == RegAR) { | 
|  | 716 | Kind = AR32Reg; | 
|  | 717 | RegNo = SystemZMC::AR32Regs[Reg.Num]; | 
|  | 718 | } | 
| Zhan Jun Liau | 4fbc3f4 | 2016-08-08 15:13:08 +0000 | [diff] [blame] | 719 | else { | 
|  | 720 | return MatchOperand_ParseFail; | 
|  | 721 | } | 
|  | 722 |  | 
|  | 723 | Operands.push_back(SystemZOperand::createReg(Kind, RegNo, | 
|  | 724 | Reg.StartLoc, Reg.EndLoc)); | 
|  | 725 | } | 
|  | 726 | return MatchOperand_Success; | 
|  | 727 | } | 
|  | 728 |  | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 729 | // Parse a memory operand into Reg1, Reg2, Disp, and Length. | 
|  | 730 | bool SystemZAsmParser::parseAddress(bool &HaveReg1, Register &Reg1, | 
|  | 731 | bool &HaveReg2, Register &Reg2, | 
|  | 732 | const MCExpr *&Disp, | 
|  | 733 | const MCExpr *&Length) { | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 734 | // Parse the displacement, which must always be present. | 
|  | 735 | if (getParser().parseExpression(Disp)) | 
|  | 736 | return true; | 
|  | 737 |  | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 738 | // Parse the optional base and index. | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 739 | HaveReg1 = false; | 
|  | 740 | HaveReg2 = false; | 
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 741 | Length = nullptr; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 742 | if (getLexer().is(AsmToken::LParen)) { | 
|  | 743 | Parser.Lex(); | 
|  | 744 |  | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 745 | if (getLexer().is(AsmToken::Percent)) { | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 746 | // Parse the first register. | 
|  | 747 | HaveReg1 = true; | 
|  | 748 | if (parseRegister(Reg1)) | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 749 | return true; | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 750 | } else { | 
|  | 751 | // Parse the length. | 
|  | 752 | if (getParser().parseExpression(Length)) | 
|  | 753 | return true; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 754 | } | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 755 |  | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 756 | // Check whether there's a second register. | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 757 | if (getLexer().is(AsmToken::Comma)) { | 
|  | 758 | Parser.Lex(); | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 759 | HaveReg2 = true; | 
|  | 760 | if (parseRegister(Reg2)) | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 761 | return true; | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 762 | } | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 763 |  | 
|  | 764 | // Consume the closing bracket. | 
|  | 765 | if (getLexer().isNot(AsmToken::RParen)) | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 766 | return Error(Parser.getTok().getLoc(), "unexpected token in address"); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 767 | Parser.Lex(); | 
|  | 768 | } | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 769 | return false; | 
|  | 770 | } | 
|  | 771 |  | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 772 | // Verify that Reg is a valid address register (base or index). | 
|  | 773 | bool | 
|  | 774 | SystemZAsmParser::parseAddressRegister(Register &Reg) { | 
|  | 775 | if (Reg.Group == RegV) { | 
|  | 776 | Error(Reg.StartLoc, "invalid use of vector addressing"); | 
|  | 777 | return true; | 
|  | 778 | } else if (Reg.Group != RegGR) { | 
|  | 779 | Error(Reg.StartLoc, "invalid address register"); | 
|  | 780 | return true; | 
|  | 781 | } else if (Reg.Num == 0) { | 
|  | 782 | Error(Reg.StartLoc, "%r0 used in an address"); | 
|  | 783 | return true; | 
|  | 784 | } | 
|  | 785 | return false; | 
|  | 786 | } | 
|  | 787 |  | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 788 | // Parse a memory operand and add it to Operands.  The other arguments | 
|  | 789 | // are as above. | 
| Alex Bradbury | 58eba09 | 2016-11-01 16:32:05 +0000 | [diff] [blame] | 790 | OperandMatchResultTy | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 791 | SystemZAsmParser::parseAddress(OperandVector &Operands, MemoryKind MemKind, | 
|  | 792 | const unsigned *Regs, RegisterKind RegKind) { | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 793 | SMLoc StartLoc = Parser.getTok().getLoc(); | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 794 | unsigned Base = 0, Index = 0, LengthReg = 0; | 
|  | 795 | Register Reg1, Reg2; | 
|  | 796 | bool HaveReg1, HaveReg2; | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 797 | const MCExpr *Disp; | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 798 | const MCExpr *Length; | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 799 | if (parseAddress(HaveReg1, Reg1, HaveReg2, Reg2, Disp, Length)) | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 800 | return MatchOperand_ParseFail; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 801 |  | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 802 | switch (MemKind) { | 
|  | 803 | case BDMem: | 
|  | 804 | // If we have Reg1, it must be an address register. | 
|  | 805 | if (HaveReg1) { | 
|  | 806 | if (parseAddressRegister(Reg1)) | 
|  | 807 | return MatchOperand_ParseFail; | 
|  | 808 | Base = Regs[Reg1.Num]; | 
|  | 809 | } | 
|  | 810 | // There must be no Reg2 or length. | 
|  | 811 | if (Length) { | 
|  | 812 | Error(StartLoc, "invalid use of length addressing"); | 
|  | 813 | return MatchOperand_ParseFail; | 
|  | 814 | } | 
|  | 815 | if (HaveReg2) { | 
|  | 816 | Error(StartLoc, "invalid use of indexed addressing"); | 
|  | 817 | return MatchOperand_ParseFail; | 
|  | 818 | } | 
|  | 819 | break; | 
|  | 820 | case BDXMem: | 
|  | 821 | // If we have Reg1, it must be an address register. | 
|  | 822 | if (HaveReg1) { | 
|  | 823 | if (parseAddressRegister(Reg1)) | 
|  | 824 | return MatchOperand_ParseFail; | 
|  | 825 | // If the are two registers, the first one is the index and the | 
|  | 826 | // second is the base. | 
|  | 827 | if (HaveReg2) | 
|  | 828 | Index = Regs[Reg1.Num]; | 
|  | 829 | else | 
|  | 830 | Base = Regs[Reg1.Num]; | 
|  | 831 | } | 
|  | 832 | // If we have Reg2, it must be an address register. | 
|  | 833 | if (HaveReg2) { | 
|  | 834 | if (parseAddressRegister(Reg2)) | 
|  | 835 | return MatchOperand_ParseFail; | 
|  | 836 | Base = Regs[Reg2.Num]; | 
|  | 837 | } | 
|  | 838 | // There must be no length. | 
|  | 839 | if (Length) { | 
|  | 840 | Error(StartLoc, "invalid use of length addressing"); | 
|  | 841 | return MatchOperand_ParseFail; | 
|  | 842 | } | 
|  | 843 | break; | 
|  | 844 | case BDLMem: | 
|  | 845 | // If we have Reg2, it must be an address register. | 
|  | 846 | if (HaveReg2) { | 
|  | 847 | if (parseAddressRegister(Reg2)) | 
|  | 848 | return MatchOperand_ParseFail; | 
|  | 849 | Base = Regs[Reg2.Num]; | 
|  | 850 | } | 
|  | 851 | // We cannot support base+index addressing. | 
|  | 852 | if (HaveReg1 && HaveReg2) { | 
|  | 853 | Error(StartLoc, "invalid use of indexed addressing"); | 
|  | 854 | return MatchOperand_ParseFail; | 
|  | 855 | } | 
|  | 856 | // We must have a length. | 
|  | 857 | if (!Length) { | 
|  | 858 | Error(StartLoc, "missing length in address"); | 
|  | 859 | return MatchOperand_ParseFail; | 
|  | 860 | } | 
|  | 861 | break; | 
|  | 862 | case BDRMem: | 
|  | 863 | // We must have Reg1, and it must be a GPR. | 
|  | 864 | if (!HaveReg1 || Reg1.Group != RegGR) { | 
|  | 865 | Error(StartLoc, "invalid operand for instruction"); | 
|  | 866 | return MatchOperand_ParseFail; | 
|  | 867 | } | 
|  | 868 | LengthReg = SystemZMC::GR64Regs[Reg1.Num]; | 
|  | 869 | // If we have Reg2, it must be an address register. | 
|  | 870 | if (HaveReg2) { | 
|  | 871 | if (parseAddressRegister(Reg2)) | 
|  | 872 | return MatchOperand_ParseFail; | 
|  | 873 | Base = Regs[Reg2.Num]; | 
|  | 874 | } | 
|  | 875 | // There must be no length. | 
|  | 876 | if (Length) { | 
|  | 877 | Error(StartLoc, "invalid use of length addressing"); | 
|  | 878 | return MatchOperand_ParseFail; | 
|  | 879 | } | 
|  | 880 | break; | 
|  | 881 | case BDVMem: | 
|  | 882 | // We must have Reg1, and it must be a vector register. | 
|  | 883 | if (!HaveReg1 || Reg1.Group != RegV) { | 
|  | 884 | Error(StartLoc, "vector index required in address"); | 
|  | 885 | return MatchOperand_ParseFail; | 
|  | 886 | } | 
|  | 887 | Index = SystemZMC::VR128Regs[Reg1.Num]; | 
|  | 888 | // If we have Reg2, it must be an address register. | 
|  | 889 | if (HaveReg2) { | 
|  | 890 | if (parseAddressRegister(Reg2)) | 
|  | 891 | return MatchOperand_ParseFail; | 
|  | 892 | Base = Regs[Reg2.Num]; | 
|  | 893 | } | 
|  | 894 | // There must be no length. | 
|  | 895 | if (Length) { | 
|  | 896 | Error(StartLoc, "invalid use of length addressing"); | 
|  | 897 | return MatchOperand_ParseFail; | 
|  | 898 | } | 
|  | 899 | break; | 
| Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame] | 900 | } | 
| Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 901 |  | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 902 | SMLoc EndLoc = | 
|  | 903 | SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); | 
| Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 904 | Operands.push_back(SystemZOperand::createMem(MemKind, RegKind, Base, Disp, | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 905 | Index, Length, LengthReg, | 
|  | 906 | StartLoc, EndLoc)); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 907 | return MatchOperand_Success; | 
|  | 908 | } | 
|  | 909 |  | 
|  | 910 | bool SystemZAsmParser::ParseDirective(AsmToken DirectiveID) { | 
| Zhan Jun Liau | 4fbc3f4 | 2016-08-08 15:13:08 +0000 | [diff] [blame] | 911 | StringRef IDVal = DirectiveID.getIdentifier(); | 
|  | 912 |  | 
|  | 913 | if (IDVal == ".insn") | 
|  | 914 | return ParseDirectiveInsn(DirectiveID.getLoc()); | 
|  | 915 |  | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 916 | return true; | 
|  | 917 | } | 
|  | 918 |  | 
| Zhan Jun Liau | 4fbc3f4 | 2016-08-08 15:13:08 +0000 | [diff] [blame] | 919 | /// ParseDirectiveInsn | 
|  | 920 | /// ::= .insn [ format, encoding, (operands (, operands)*) ] | 
|  | 921 | bool SystemZAsmParser::ParseDirectiveInsn(SMLoc L) { | 
|  | 922 | MCAsmParser &Parser = getParser(); | 
|  | 923 |  | 
|  | 924 | // Expect instruction format as identifier. | 
|  | 925 | StringRef Format; | 
|  | 926 | SMLoc ErrorLoc = Parser.getTok().getLoc(); | 
|  | 927 | if (Parser.parseIdentifier(Format)) | 
|  | 928 | return Error(ErrorLoc, "expected instruction format"); | 
|  | 929 |  | 
|  | 930 | SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> Operands; | 
|  | 931 |  | 
|  | 932 | // Find entry for this format in InsnMatchTable. | 
|  | 933 | auto EntryRange = | 
|  | 934 | std::equal_range(std::begin(InsnMatchTable), std::end(InsnMatchTable), | 
|  | 935 | Format, CompareInsn()); | 
|  | 936 |  | 
|  | 937 | // If first == second, couldn't find a match in the table. | 
|  | 938 | if (EntryRange.first == EntryRange.second) | 
|  | 939 | return Error(ErrorLoc, "unrecognized format"); | 
|  | 940 |  | 
|  | 941 | struct InsnMatchEntry *Entry = EntryRange.first; | 
|  | 942 |  | 
|  | 943 | // Format should match from equal_range. | 
|  | 944 | assert(Entry->Format == Format); | 
|  | 945 |  | 
|  | 946 | // Parse the following operands using the table's information. | 
|  | 947 | for (int i = 0; i < Entry->NumOperands; i++) { | 
|  | 948 | MatchClassKind Kind = Entry->OperandKinds[i]; | 
|  | 949 |  | 
|  | 950 | SMLoc StartLoc = Parser.getTok().getLoc(); | 
|  | 951 |  | 
|  | 952 | // Always expect commas as separators for operands. | 
|  | 953 | if (getLexer().isNot(AsmToken::Comma)) | 
|  | 954 | return Error(StartLoc, "unexpected token in directive"); | 
|  | 955 | Lex(); | 
|  | 956 |  | 
|  | 957 | // Parse operands. | 
|  | 958 | OperandMatchResultTy ResTy; | 
|  | 959 | if (Kind == MCK_AnyReg) | 
|  | 960 | ResTy = parseAnyReg(Operands); | 
|  | 961 | else if (Kind == MCK_BDXAddr64Disp12 || Kind == MCK_BDXAddr64Disp20) | 
|  | 962 | ResTy = parseBDXAddr64(Operands); | 
|  | 963 | else if (Kind == MCK_BDAddr64Disp12 || Kind == MCK_BDAddr64Disp20) | 
|  | 964 | ResTy = parseBDAddr64(Operands); | 
|  | 965 | else if (Kind == MCK_PCRel32) | 
|  | 966 | ResTy = parsePCRel32(Operands); | 
|  | 967 | else if (Kind == MCK_PCRel16) | 
|  | 968 | ResTy = parsePCRel16(Operands); | 
|  | 969 | else { | 
|  | 970 | // Only remaining operand kind is an immediate. | 
|  | 971 | const MCExpr *Expr; | 
|  | 972 | SMLoc StartLoc = Parser.getTok().getLoc(); | 
|  | 973 |  | 
|  | 974 | // Expect immediate expression. | 
|  | 975 | if (Parser.parseExpression(Expr)) | 
|  | 976 | return Error(StartLoc, "unexpected token in directive"); | 
|  | 977 |  | 
|  | 978 | SMLoc EndLoc = | 
|  | 979 | SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); | 
|  | 980 |  | 
|  | 981 | Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc)); | 
|  | 982 | ResTy = MatchOperand_Success; | 
|  | 983 | } | 
|  | 984 |  | 
|  | 985 | if (ResTy != MatchOperand_Success) | 
|  | 986 | return true; | 
|  | 987 | } | 
|  | 988 |  | 
|  | 989 | // Build the instruction with the parsed operands. | 
|  | 990 | MCInst Inst = MCInstBuilder(Entry->Opcode); | 
|  | 991 |  | 
|  | 992 | for (size_t i = 0; i < Operands.size(); i++) { | 
|  | 993 | MCParsedAsmOperand &Operand = *Operands[i]; | 
|  | 994 | MatchClassKind Kind = Entry->OperandKinds[i]; | 
|  | 995 |  | 
|  | 996 | // Verify operand. | 
|  | 997 | unsigned Res = validateOperandClass(Operand, Kind); | 
|  | 998 | if (Res != Match_Success) | 
|  | 999 | return Error(Operand.getStartLoc(), "unexpected operand type"); | 
|  | 1000 |  | 
|  | 1001 | // Add operands to instruction. | 
|  | 1002 | SystemZOperand &ZOperand = static_cast<SystemZOperand &>(Operand); | 
|  | 1003 | if (ZOperand.isReg()) | 
|  | 1004 | ZOperand.addRegOperands(Inst, 1); | 
|  | 1005 | else if (ZOperand.isMem(BDMem)) | 
|  | 1006 | ZOperand.addBDAddrOperands(Inst, 2); | 
|  | 1007 | else if (ZOperand.isMem(BDXMem)) | 
|  | 1008 | ZOperand.addBDXAddrOperands(Inst, 3); | 
|  | 1009 | else if (ZOperand.isImm()) | 
|  | 1010 | ZOperand.addImmOperands(Inst, 1); | 
|  | 1011 | else | 
|  | 1012 | llvm_unreachable("unexpected operand type"); | 
|  | 1013 | } | 
|  | 1014 |  | 
|  | 1015 | // Emit as a regular instruction. | 
|  | 1016 | Parser.getStreamer().EmitInstruction(Inst, getSTI()); | 
|  | 1017 |  | 
|  | 1018 | return false; | 
|  | 1019 | } | 
|  | 1020 |  | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1021 | bool SystemZAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, | 
|  | 1022 | SMLoc &EndLoc) { | 
|  | 1023 | Register Reg; | 
|  | 1024 | if (parseRegister(Reg)) | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 1025 | return true; | 
| Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 1026 | if (Reg.Group == RegGR) | 
|  | 1027 | RegNo = SystemZMC::GR64Regs[Reg.Num]; | 
|  | 1028 | else if (Reg.Group == RegFP) | 
|  | 1029 | RegNo = SystemZMC::FP64Regs[Reg.Num]; | 
| Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame] | 1030 | else if (Reg.Group == RegV) | 
|  | 1031 | RegNo = SystemZMC::VR128Regs[Reg.Num]; | 
| Ulrich Weigand | fffc711 | 2016-11-08 20:15:26 +0000 | [diff] [blame] | 1032 | else if (Reg.Group == RegAR) | 
|  | 1033 | RegNo = SystemZMC::AR32Regs[Reg.Num]; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1034 | StartLoc = Reg.StartLoc; | 
|  | 1035 | EndLoc = Reg.EndLoc; | 
|  | 1036 | return false; | 
|  | 1037 | } | 
|  | 1038 |  | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 1039 | bool SystemZAsmParser::ParseInstruction(ParseInstructionInfo &Info, | 
|  | 1040 | StringRef Name, SMLoc NameLoc, | 
|  | 1041 | OperandVector &Operands) { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1042 | Operands.push_back(SystemZOperand::createToken(Name, NameLoc)); | 
|  | 1043 |  | 
|  | 1044 | // Read the remaining operands. | 
|  | 1045 | if (getLexer().isNot(AsmToken::EndOfStatement)) { | 
|  | 1046 | // Read the first operand. | 
|  | 1047 | if (parseOperand(Operands, Name)) { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1048 | return true; | 
|  | 1049 | } | 
|  | 1050 |  | 
|  | 1051 | // Read any subsequent operands. | 
|  | 1052 | while (getLexer().is(AsmToken::Comma)) { | 
|  | 1053 | Parser.Lex(); | 
|  | 1054 | if (parseOperand(Operands, Name)) { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1055 | return true; | 
|  | 1056 | } | 
|  | 1057 | } | 
|  | 1058 | if (getLexer().isNot(AsmToken::EndOfStatement)) { | 
|  | 1059 | SMLoc Loc = getLexer().getLoc(); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1060 | return Error(Loc, "unexpected token in argument list"); | 
|  | 1061 | } | 
|  | 1062 | } | 
|  | 1063 |  | 
|  | 1064 | // Consume the EndOfStatement. | 
|  | 1065 | Parser.Lex(); | 
|  | 1066 | return false; | 
|  | 1067 | } | 
|  | 1068 |  | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 1069 | bool SystemZAsmParser::parseOperand(OperandVector &Operands, | 
|  | 1070 | StringRef Mnemonic) { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1071 | // Check if the current operand has a custom associated parser, if so, try to | 
| Ulrich Weigand | d900130 | 2016-10-31 14:25:05 +0000 | [diff] [blame] | 1072 | // custom parse the operand, or fallback to the general approach.  Force all | 
|  | 1073 | // features to be available during the operand check, or else we will fail to | 
|  | 1074 | // find the custom parser, and then we will later get an InvalidOperand error | 
|  | 1075 | // instead of a MissingFeature errror. | 
|  | 1076 | uint64_t AvailableFeatures = getAvailableFeatures(); | 
|  | 1077 | setAvailableFeatures(~(uint64_t)0); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1078 | OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic); | 
| Ulrich Weigand | d900130 | 2016-10-31 14:25:05 +0000 | [diff] [blame] | 1079 | setAvailableFeatures(AvailableFeatures); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1080 | if (ResTy == MatchOperand_Success) | 
|  | 1081 | return false; | 
|  | 1082 |  | 
|  | 1083 | // If there wasn't a custom match, try the generic matcher below. Otherwise, | 
|  | 1084 | // there was a match, but an error occurred, in which case, just return that | 
|  | 1085 | // the operand parsing failed. | 
|  | 1086 | if (ResTy == MatchOperand_ParseFail) | 
|  | 1087 | return true; | 
|  | 1088 |  | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 1089 | // Check for a register.  All real register operands should have used | 
|  | 1090 | // a context-dependent parse routine, which gives the required register | 
|  | 1091 | // class.  The code is here to mop up other cases, like those where | 
|  | 1092 | // the instruction isn't recognized. | 
|  | 1093 | if (Parser.getTok().is(AsmToken::Percent)) { | 
|  | 1094 | Register Reg; | 
|  | 1095 | if (parseRegister(Reg)) | 
|  | 1096 | return true; | 
|  | 1097 | Operands.push_back(SystemZOperand::createInvalid(Reg.StartLoc, Reg.EndLoc)); | 
|  | 1098 | return false; | 
|  | 1099 | } | 
|  | 1100 |  | 
|  | 1101 | // The only other type of operand is an immediate or address.  As above, | 
|  | 1102 | // real address operands should have used a context-dependent parse routine, | 
|  | 1103 | // so we treat any plain expression as an immediate. | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1104 | SMLoc StartLoc = Parser.getTok().getLoc(); | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 1105 | Register Reg1, Reg2; | 
|  | 1106 | bool HaveReg1, HaveReg2; | 
|  | 1107 | const MCExpr *Expr; | 
|  | 1108 | const MCExpr *Length; | 
|  | 1109 | if (parseAddress(HaveReg1, Reg1, HaveReg2, Reg2, Expr, Length)) | 
| Ulrich Weigand | 75d2f1b | 2016-11-02 11:32:28 +0000 | [diff] [blame] | 1110 | return true; | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 1111 | // If the register combination is not valid for any instruction, reject it. | 
|  | 1112 | // Otherwise, fall back to reporting an unrecognized instruction. | 
|  | 1113 | if (HaveReg1 && Reg1.Group != RegGR && Reg1.Group != RegV | 
|  | 1114 | && parseAddressRegister(Reg1)) | 
| Ulrich Weigand | 75d2f1b | 2016-11-02 11:32:28 +0000 | [diff] [blame] | 1115 | return true; | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 1116 | if (HaveReg2 && parseAddressRegister(Reg2)) | 
| Ulrich Weigand | 75d2f1b | 2016-11-02 11:32:28 +0000 | [diff] [blame] | 1117 | return true; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1118 |  | 
|  | 1119 | SMLoc EndLoc = | 
|  | 1120 | SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); | 
| Ulrich Weigand | ec5d779 | 2016-10-31 14:21:36 +0000 | [diff] [blame] | 1121 | if (HaveReg1 || HaveReg2 || Length) | 
| Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 1122 | Operands.push_back(SystemZOperand::createInvalid(StartLoc, EndLoc)); | 
|  | 1123 | else | 
|  | 1124 | Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc)); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1125 | return false; | 
|  | 1126 | } | 
|  | 1127 |  | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 1128 | bool SystemZAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, | 
|  | 1129 | OperandVector &Operands, | 
|  | 1130 | MCStreamer &Out, | 
| Tim Northover | 26bb14e | 2014-08-18 11:49:42 +0000 | [diff] [blame] | 1131 | uint64_t &ErrorInfo, | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 1132 | bool MatchingInlineAsm) { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1133 | MCInst Inst; | 
|  | 1134 | unsigned MatchResult; | 
|  | 1135 |  | 
|  | 1136 | MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo, | 
| Ranjeet Singh | 86ecbb7 | 2015-06-30 12:32:53 +0000 | [diff] [blame] | 1137 | MatchingInlineAsm); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1138 | switch (MatchResult) { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1139 | case Match_Success: | 
|  | 1140 | Inst.setLoc(IDLoc); | 
| Akira Hatanaka | bd9fc28 | 2015-11-14 05:20:05 +0000 | [diff] [blame] | 1141 | Out.EmitInstruction(Inst, getSTI()); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1142 | return false; | 
|  | 1143 |  | 
|  | 1144 | case Match_MissingFeature: { | 
| Ranjeet Singh | 86ecbb7 | 2015-06-30 12:32:53 +0000 | [diff] [blame] | 1145 | assert(ErrorInfo && "Unknown missing feature!"); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1146 | // Special case the error message for the very common case where only | 
|  | 1147 | // a single subtarget feature is missing | 
|  | 1148 | std::string Msg = "instruction requires:"; | 
| Ranjeet Singh | 86ecbb7 | 2015-06-30 12:32:53 +0000 | [diff] [blame] | 1149 | uint64_t Mask = 1; | 
|  | 1150 | for (unsigned I = 0; I < sizeof(ErrorInfo) * 8 - 1; ++I) { | 
|  | 1151 | if (ErrorInfo & Mask) { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1152 | Msg += " "; | 
| Ranjeet Singh | 86ecbb7 | 2015-06-30 12:32:53 +0000 | [diff] [blame] | 1153 | Msg += getSubtargetFeatureName(ErrorInfo & Mask); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1154 | } | 
| Ranjeet Singh | 86ecbb7 | 2015-06-30 12:32:53 +0000 | [diff] [blame] | 1155 | Mask <<= 1; | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1156 | } | 
|  | 1157 | return Error(IDLoc, Msg); | 
|  | 1158 | } | 
|  | 1159 |  | 
|  | 1160 | case Match_InvalidOperand: { | 
|  | 1161 | SMLoc ErrorLoc = IDLoc; | 
| Tim Northover | 26bb14e | 2014-08-18 11:49:42 +0000 | [diff] [blame] | 1162 | if (ErrorInfo != ~0ULL) { | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1163 | if (ErrorInfo >= Operands.size()) | 
|  | 1164 | return Error(IDLoc, "too few operands for instruction"); | 
|  | 1165 |  | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 1166 | ErrorLoc = ((SystemZOperand &)*Operands[ErrorInfo]).getStartLoc(); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1167 | if (ErrorLoc == SMLoc()) | 
|  | 1168 | ErrorLoc = IDLoc; | 
|  | 1169 | } | 
|  | 1170 | return Error(ErrorLoc, "invalid operand for instruction"); | 
|  | 1171 | } | 
|  | 1172 |  | 
|  | 1173 | case Match_MnemonicFail: | 
|  | 1174 | return Error(IDLoc, "invalid instruction"); | 
|  | 1175 | } | 
|  | 1176 |  | 
|  | 1177 | llvm_unreachable("Unexpected match type"); | 
|  | 1178 | } | 
|  | 1179 |  | 
| Alex Bradbury | 58eba09 | 2016-11-01 16:32:05 +0000 | [diff] [blame] | 1180 | OperandMatchResultTy | 
| David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 1181 | SystemZAsmParser::parsePCRel(OperandVector &Operands, int64_t MinVal, | 
| Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 1182 | int64_t MaxVal, bool AllowTLS) { | 
| Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 1183 | MCContext &Ctx = getContext(); | 
|  | 1184 | MCStreamer &Out = getStreamer(); | 
|  | 1185 | const MCExpr *Expr; | 
|  | 1186 | SMLoc StartLoc = Parser.getTok().getLoc(); | 
|  | 1187 | if (getParser().parseExpression(Expr)) | 
|  | 1188 | return MatchOperand_NoMatch; | 
|  | 1189 |  | 
|  | 1190 | // For consistency with the GNU assembler, treat immediates as offsets | 
|  | 1191 | // from ".". | 
| Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1192 | if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) { | 
| Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 1193 | int64_t Value = CE->getValue(); | 
|  | 1194 | if ((Value & 1) || Value < MinVal || Value > MaxVal) { | 
|  | 1195 | Error(StartLoc, "offset out of range"); | 
|  | 1196 | return MatchOperand_ParseFail; | 
|  | 1197 | } | 
| Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1198 | MCSymbol *Sym = Ctx.createTempSymbol(); | 
| Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 1199 | Out.EmitLabel(Sym); | 
| Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1200 | const MCExpr *Base = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, | 
| Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 1201 | Ctx); | 
| Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1202 | Expr = Value == 0 ? Base : MCBinaryExpr::createAdd(Base, Expr, Ctx); | 
| Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 1203 | } | 
|  | 1204 |  | 
| Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 1205 | // Optionally match :tls_gdcall: or :tls_ldcall: followed by a TLS symbol. | 
|  | 1206 | const MCExpr *Sym = nullptr; | 
|  | 1207 | if (AllowTLS && getLexer().is(AsmToken::Colon)) { | 
|  | 1208 | Parser.Lex(); | 
|  | 1209 |  | 
|  | 1210 | if (Parser.getTok().isNot(AsmToken::Identifier)) { | 
|  | 1211 | Error(Parser.getTok().getLoc(), "unexpected token"); | 
|  | 1212 | return MatchOperand_ParseFail; | 
|  | 1213 | } | 
|  | 1214 |  | 
|  | 1215 | MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None; | 
|  | 1216 | StringRef Name = Parser.getTok().getString(); | 
|  | 1217 | if (Name == "tls_gdcall") | 
|  | 1218 | Kind = MCSymbolRefExpr::VK_TLSGD; | 
|  | 1219 | else if (Name == "tls_ldcall") | 
|  | 1220 | Kind = MCSymbolRefExpr::VK_TLSLDM; | 
|  | 1221 | else { | 
|  | 1222 | Error(Parser.getTok().getLoc(), "unknown TLS tag"); | 
|  | 1223 | return MatchOperand_ParseFail; | 
|  | 1224 | } | 
|  | 1225 | Parser.Lex(); | 
|  | 1226 |  | 
|  | 1227 | if (Parser.getTok().isNot(AsmToken::Colon)) { | 
|  | 1228 | Error(Parser.getTok().getLoc(), "unexpected token"); | 
|  | 1229 | return MatchOperand_ParseFail; | 
|  | 1230 | } | 
|  | 1231 | Parser.Lex(); | 
|  | 1232 |  | 
|  | 1233 | if (Parser.getTok().isNot(AsmToken::Identifier)) { | 
|  | 1234 | Error(Parser.getTok().getLoc(), "unexpected token"); | 
|  | 1235 | return MatchOperand_ParseFail; | 
|  | 1236 | } | 
|  | 1237 |  | 
|  | 1238 | StringRef Identifier = Parser.getTok().getString(); | 
| Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1239 | Sym = MCSymbolRefExpr::create(Ctx.getOrCreateSymbol(Identifier), | 
| Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 1240 | Kind, Ctx); | 
|  | 1241 | Parser.Lex(); | 
|  | 1242 | } | 
|  | 1243 |  | 
| Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 1244 | SMLoc EndLoc = | 
|  | 1245 | SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); | 
| Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 1246 |  | 
|  | 1247 | if (AllowTLS) | 
|  | 1248 | Operands.push_back(SystemZOperand::createImmTLS(Expr, Sym, | 
|  | 1249 | StartLoc, EndLoc)); | 
|  | 1250 | else | 
|  | 1251 | Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc)); | 
|  | 1252 |  | 
| Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 1253 | return MatchOperand_Success; | 
|  | 1254 | } | 
|  | 1255 |  | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1256 | // Force static initialization. | 
|  | 1257 | extern "C" void LLVMInitializeSystemZAsmParser() { | 
| Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 1258 | RegisterMCAsmParser<SystemZAsmParser> X(getTheSystemZTarget()); | 
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1259 | } |