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