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" |
| 15 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
| 16 | #include "llvm/MC/MCStreamer.h" |
| 17 | #include "llvm/MC/MCSubtargetInfo.h" |
| 18 | #include "llvm/MC/MCTargetAsmParser.h" |
| 19 | #include "llvm/Support/TargetRegistry.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | // Return true if Expr is in the range [MinValue, MaxValue]. |
| 24 | static bool inRange(const MCExpr *Expr, int64_t MinValue, int64_t MaxValue) { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 25 | if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 26 | int64_t Value = CE->getValue(); |
| 27 | return Value >= MinValue && Value <= MaxValue; |
| 28 | } |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | namespace { |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 33 | enum RegisterKind { |
| 34 | GR32Reg, |
Richard Sandiford | f949606 | 2013-09-30 10:45:16 +0000 | [diff] [blame] | 35 | GRH32Reg, |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 36 | GR64Reg, |
| 37 | GR128Reg, |
| 38 | ADDR32Reg, |
| 39 | ADDR64Reg, |
| 40 | FP32Reg, |
| 41 | FP64Reg, |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 42 | FP128Reg, |
| 43 | VR32Reg, |
| 44 | VR64Reg, |
| 45 | VR128Reg |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | enum MemoryKind { |
| 49 | BDMem, |
| 50 | BDXMem, |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 51 | BDLMem, |
| 52 | BDVMem |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 55 | class SystemZOperand : public MCParsedAsmOperand { |
| 56 | public: |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 57 | private: |
| 58 | enum OperandKind { |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 59 | KindInvalid, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 60 | KindToken, |
| 61 | KindReg, |
| 62 | KindAccessReg, |
| 63 | KindImm, |
Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 64 | KindImmTLS, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 65 | KindMem |
| 66 | }; |
| 67 | |
| 68 | OperandKind Kind; |
| 69 | SMLoc StartLoc, EndLoc; |
| 70 | |
| 71 | // A string of length Length, starting at Data. |
| 72 | struct TokenOp { |
| 73 | const char *Data; |
| 74 | unsigned Length; |
| 75 | }; |
| 76 | |
Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 77 | // LLVM register Num, which has kind Kind. In some ways it might be |
| 78 | // easier for this class to have a register bank (general, floating-point |
| 79 | // or access) and a raw register number (0-15). This would postpone the |
| 80 | // interpretation of the operand to the add*() methods and avoid the need |
| 81 | // for context-dependent parsing. However, we do things the current way |
| 82 | // because of the virtual getReg() method, which needs to distinguish |
| 83 | // between (say) %r0 used as a single register and %r0 used as a pair. |
| 84 | // Context-dependent parsing can also give us slightly better error |
| 85 | // messages when invalid pairs like %r1 are used. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 86 | struct RegOp { |
| 87 | RegisterKind Kind; |
| 88 | unsigned Num; |
| 89 | }; |
| 90 | |
| 91 | // Base + Disp + Index, where Base and Index are LLVM registers or 0. |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 92 | // MemKind says what type of memory this is and RegKind says what type |
| 93 | // the base register has (ADDR32Reg or ADDR64Reg). Length is the operand |
| 94 | // length for D(L,B)-style operands, otherwise it is null. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 95 | struct MemOp { |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 96 | unsigned Base : 12; |
| 97 | unsigned Index : 12; |
| 98 | unsigned MemKind : 4; |
| 99 | unsigned RegKind : 4; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 100 | const MCExpr *Disp; |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 101 | const MCExpr *Length; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 104 | // Imm is an immediate operand, and Sym is an optional TLS symbol |
| 105 | // for use with a __tls_get_offset marker relocation. |
| 106 | struct ImmTLSOp { |
| 107 | const MCExpr *Imm; |
| 108 | const MCExpr *Sym; |
| 109 | }; |
| 110 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 111 | union { |
| 112 | TokenOp Token; |
| 113 | RegOp Reg; |
| 114 | unsigned AccessReg; |
| 115 | const MCExpr *Imm; |
Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 116 | ImmTLSOp ImmTLS; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 117 | MemOp Mem; |
| 118 | }; |
| 119 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 120 | void addExpr(MCInst &Inst, const MCExpr *Expr) const { |
| 121 | // Add as immediates when possible. Null MCExpr = 0. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 122 | if (!Expr) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 123 | Inst.addOperand(MCOperand::CreateImm(0)); |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 124 | else if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 125 | Inst.addOperand(MCOperand::CreateImm(CE->getValue())); |
| 126 | else |
| 127 | Inst.addOperand(MCOperand::CreateExpr(Expr)); |
| 128 | } |
| 129 | |
| 130 | public: |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 131 | SystemZOperand(OperandKind kind, SMLoc startLoc, SMLoc endLoc) |
| 132 | : Kind(kind), StartLoc(startLoc), EndLoc(endLoc) {} |
| 133 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 134 | // Create particular kinds of operand. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 135 | static std::unique_ptr<SystemZOperand> createInvalid(SMLoc StartLoc, |
| 136 | SMLoc EndLoc) { |
| 137 | return make_unique<SystemZOperand>(KindInvalid, StartLoc, EndLoc); |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 138 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 139 | static std::unique_ptr<SystemZOperand> createToken(StringRef Str, SMLoc Loc) { |
| 140 | auto Op = make_unique<SystemZOperand>(KindToken, Loc, Loc); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 141 | Op->Token.Data = Str.data(); |
| 142 | Op->Token.Length = Str.size(); |
| 143 | return Op; |
| 144 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 145 | static std::unique_ptr<SystemZOperand> |
| 146 | createReg(RegisterKind Kind, unsigned Num, SMLoc StartLoc, SMLoc EndLoc) { |
| 147 | auto Op = make_unique<SystemZOperand>(KindReg, StartLoc, EndLoc); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 148 | Op->Reg.Kind = Kind; |
| 149 | Op->Reg.Num = Num; |
| 150 | return Op; |
| 151 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 152 | static std::unique_ptr<SystemZOperand> |
| 153 | createAccessReg(unsigned Num, SMLoc StartLoc, SMLoc EndLoc) { |
| 154 | auto Op = make_unique<SystemZOperand>(KindAccessReg, StartLoc, EndLoc); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 155 | Op->AccessReg = Num; |
| 156 | return Op; |
| 157 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 158 | static std::unique_ptr<SystemZOperand> |
| 159 | createImm(const MCExpr *Expr, SMLoc StartLoc, SMLoc EndLoc) { |
| 160 | auto Op = make_unique<SystemZOperand>(KindImm, StartLoc, EndLoc); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 161 | Op->Imm = Expr; |
| 162 | return Op; |
| 163 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 164 | static std::unique_ptr<SystemZOperand> |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 165 | createMem(MemoryKind MemKind, RegisterKind RegKind, unsigned Base, |
| 166 | const MCExpr *Disp, unsigned Index, const MCExpr *Length, |
| 167 | SMLoc StartLoc, SMLoc EndLoc) { |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 168 | auto Op = make_unique<SystemZOperand>(KindMem, StartLoc, EndLoc); |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 169 | Op->Mem.MemKind = MemKind; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 170 | Op->Mem.RegKind = RegKind; |
| 171 | Op->Mem.Base = Base; |
| 172 | Op->Mem.Index = Index; |
| 173 | Op->Mem.Disp = Disp; |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 174 | Op->Mem.Length = Length; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 175 | return Op; |
| 176 | } |
Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 177 | static std::unique_ptr<SystemZOperand> |
| 178 | createImmTLS(const MCExpr *Imm, const MCExpr *Sym, |
| 179 | SMLoc StartLoc, SMLoc EndLoc) { |
| 180 | auto Op = make_unique<SystemZOperand>(KindImmTLS, StartLoc, EndLoc); |
| 181 | Op->ImmTLS.Imm = Imm; |
| 182 | Op->ImmTLS.Sym = Sym; |
| 183 | return Op; |
| 184 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 185 | |
| 186 | // Token operands |
Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 187 | bool isToken() const override { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 188 | return Kind == KindToken; |
| 189 | } |
| 190 | StringRef getToken() const { |
| 191 | assert(Kind == KindToken && "Not a token"); |
| 192 | return StringRef(Token.Data, Token.Length); |
| 193 | } |
| 194 | |
| 195 | // Register operands. |
Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 196 | bool isReg() const override { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 197 | return Kind == KindReg; |
| 198 | } |
| 199 | bool isReg(RegisterKind RegKind) const { |
| 200 | return Kind == KindReg && Reg.Kind == RegKind; |
| 201 | } |
Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 202 | unsigned getReg() const override { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 203 | assert(Kind == KindReg && "Not a register"); |
| 204 | return Reg.Num; |
| 205 | } |
| 206 | |
| 207 | // Access register operands. Access registers aren't exposed to LLVM |
| 208 | // as registers. |
| 209 | bool isAccessReg() const { |
| 210 | return Kind == KindAccessReg; |
| 211 | } |
| 212 | |
| 213 | // Immediate operands. |
Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 214 | bool isImm() const override { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 215 | return Kind == KindImm; |
| 216 | } |
| 217 | bool isImm(int64_t MinValue, int64_t MaxValue) const { |
| 218 | return Kind == KindImm && inRange(Imm, MinValue, MaxValue); |
| 219 | } |
| 220 | const MCExpr *getImm() const { |
| 221 | assert(Kind == KindImm && "Not an immediate"); |
| 222 | return Imm; |
| 223 | } |
| 224 | |
Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 225 | // Immediate operands with optional TLS symbol. |
| 226 | bool isImmTLS() const { |
| 227 | return Kind == KindImmTLS; |
| 228 | } |
| 229 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 230 | // Memory operands. |
Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 231 | bool isMem() const override { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 232 | return Kind == KindMem; |
| 233 | } |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 234 | bool isMem(MemoryKind MemKind) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 235 | return (Kind == KindMem && |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 236 | (Mem.MemKind == MemKind || |
| 237 | // A BDMem can be treated as a BDXMem in which the index |
| 238 | // register field is 0. |
| 239 | (Mem.MemKind == BDMem && MemKind == BDXMem))); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 240 | } |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 241 | bool isMem(MemoryKind MemKind, RegisterKind RegKind) const { |
| 242 | return isMem(MemKind) && Mem.RegKind == RegKind; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 243 | } |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 244 | bool isMemDisp12(MemoryKind MemKind, RegisterKind RegKind) const { |
| 245 | return isMem(MemKind, RegKind) && inRange(Mem.Disp, 0, 0xfff); |
| 246 | } |
| 247 | bool isMemDisp20(MemoryKind MemKind, RegisterKind RegKind) const { |
| 248 | return isMem(MemKind, RegKind) && inRange(Mem.Disp, -524288, 524287); |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 249 | } |
| 250 | bool isMemDisp12Len8(RegisterKind RegKind) const { |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 251 | return isMemDisp12(BDLMem, RegKind) && inRange(Mem.Length, 1, 0x100); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 252 | } |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 253 | void addBDVAddrOperands(MCInst &Inst, unsigned N) const { |
| 254 | assert(N == 3 && "Invalid number of operands"); |
| 255 | assert(isMem(BDVMem) && "Invalid operand type"); |
| 256 | Inst.addOperand(MCOperand::CreateReg(Mem.Base)); |
| 257 | addExpr(Inst, Mem.Disp); |
| 258 | Inst.addOperand(MCOperand::CreateReg(Mem.Index)); |
| 259 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 260 | |
| 261 | // Override MCParsedAsmOperand. |
Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 262 | SMLoc getStartLoc() const override { return StartLoc; } |
| 263 | SMLoc getEndLoc() const override { return EndLoc; } |
| 264 | void print(raw_ostream &OS) const override; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 265 | |
| 266 | // Used by the TableGen code to add particular types of operand |
| 267 | // to an instruction. |
| 268 | void addRegOperands(MCInst &Inst, unsigned N) const { |
| 269 | assert(N == 1 && "Invalid number of operands"); |
| 270 | Inst.addOperand(MCOperand::CreateReg(getReg())); |
| 271 | } |
| 272 | void addAccessRegOperands(MCInst &Inst, unsigned N) const { |
| 273 | assert(N == 1 && "Invalid number of operands"); |
| 274 | assert(Kind == KindAccessReg && "Invalid operand type"); |
| 275 | Inst.addOperand(MCOperand::CreateImm(AccessReg)); |
| 276 | } |
| 277 | void addImmOperands(MCInst &Inst, unsigned N) const { |
| 278 | assert(N == 1 && "Invalid number of operands"); |
| 279 | addExpr(Inst, getImm()); |
| 280 | } |
| 281 | void addBDAddrOperands(MCInst &Inst, unsigned N) const { |
| 282 | assert(N == 2 && "Invalid number of operands"); |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 283 | assert(isMem(BDMem) && "Invalid operand type"); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 284 | Inst.addOperand(MCOperand::CreateReg(Mem.Base)); |
| 285 | addExpr(Inst, Mem.Disp); |
| 286 | } |
| 287 | void addBDXAddrOperands(MCInst &Inst, unsigned N) const { |
| 288 | assert(N == 3 && "Invalid number of operands"); |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 289 | assert(isMem(BDXMem) && "Invalid operand type"); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 290 | Inst.addOperand(MCOperand::CreateReg(Mem.Base)); |
| 291 | addExpr(Inst, Mem.Disp); |
| 292 | Inst.addOperand(MCOperand::CreateReg(Mem.Index)); |
| 293 | } |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 294 | void addBDLAddrOperands(MCInst &Inst, unsigned N) const { |
| 295 | assert(N == 3 && "Invalid number of operands"); |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 296 | assert(isMem(BDLMem) && "Invalid operand type"); |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 297 | Inst.addOperand(MCOperand::CreateReg(Mem.Base)); |
| 298 | addExpr(Inst, Mem.Disp); |
| 299 | addExpr(Inst, Mem.Length); |
| 300 | } |
Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 301 | void addImmTLSOperands(MCInst &Inst, unsigned N) const { |
| 302 | assert(N == 2 && "Invalid number of operands"); |
| 303 | assert(Kind == KindImmTLS && "Invalid operand type"); |
| 304 | addExpr(Inst, ImmTLS.Imm); |
| 305 | if (ImmTLS.Sym) |
| 306 | addExpr(Inst, ImmTLS.Sym); |
| 307 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 308 | |
| 309 | // Used by the TableGen code to check for particular operand types. |
| 310 | bool isGR32() const { return isReg(GR32Reg); } |
Richard Sandiford | f949606 | 2013-09-30 10:45:16 +0000 | [diff] [blame] | 311 | bool isGRH32() const { return isReg(GRH32Reg); } |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 312 | bool isGRX32() const { return false; } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 313 | bool isGR64() const { return isReg(GR64Reg); } |
| 314 | bool isGR128() const { return isReg(GR128Reg); } |
| 315 | bool isADDR32() const { return isReg(ADDR32Reg); } |
| 316 | bool isADDR64() const { return isReg(ADDR64Reg); } |
| 317 | bool isADDR128() const { return false; } |
| 318 | bool isFP32() const { return isReg(FP32Reg); } |
| 319 | bool isFP64() const { return isReg(FP64Reg); } |
| 320 | bool isFP128() const { return isReg(FP128Reg); } |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 321 | bool isVR32() const { return isReg(VR32Reg); } |
| 322 | bool isVR64() const { return isReg(VR64Reg); } |
| 323 | bool isVF128() const { return false; } |
| 324 | bool isVR128() const { return isReg(VR128Reg); } |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 325 | bool isBDAddr32Disp12() const { return isMemDisp12(BDMem, ADDR32Reg); } |
| 326 | bool isBDAddr32Disp20() const { return isMemDisp20(BDMem, ADDR32Reg); } |
| 327 | bool isBDAddr64Disp12() const { return isMemDisp12(BDMem, ADDR64Reg); } |
| 328 | bool isBDAddr64Disp20() const { return isMemDisp20(BDMem, ADDR64Reg); } |
| 329 | bool isBDXAddr64Disp12() const { return isMemDisp12(BDXMem, ADDR64Reg); } |
| 330 | bool isBDXAddr64Disp20() const { return isMemDisp20(BDXMem, ADDR64Reg); } |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 331 | bool isBDLAddr64Disp12Len8() const { return isMemDisp12Len8(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); } |
| 345 | }; |
| 346 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 347 | class SystemZAsmParser : public MCTargetAsmParser { |
| 348 | #define GET_ASSEMBLER_HEADER |
| 349 | #include "SystemZGenAsmMatcher.inc" |
| 350 | |
| 351 | private: |
| 352 | MCSubtargetInfo &STI; |
| 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, |
Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 358 | RegAccess |
| 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 | |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 375 | bool parseAddress(unsigned &Base, const MCExpr *&Disp, |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 376 | unsigned &Index, bool &IsVector, const MCExpr *&Length, |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 377 | const unsigned *Regs, RegisterKind RegKind); |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 378 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 379 | OperandMatchResultTy parseAddress(OperandVector &Operands, |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 380 | MemoryKind MemKind, const unsigned *Regs, |
| 381 | RegisterKind RegKind); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 382 | |
Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 383 | OperandMatchResultTy parsePCRel(OperandVector &Operands, int64_t MinVal, |
| 384 | int64_t MaxVal, bool AllowTLS); |
| 385 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 386 | bool parseOperand(OperandVector &Operands, StringRef Mnemonic); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 387 | |
| 388 | public: |
Joey Gouly | 0e76fa7 | 2013-09-12 10:28:05 +0000 | [diff] [blame] | 389 | SystemZAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser, |
Evgeniy Stepanov | 0a951b7 | 2014-04-23 11:16:03 +0000 | [diff] [blame] | 390 | const MCInstrInfo &MII, |
| 391 | const MCTargetOptions &Options) |
Joey Gouly | 0e76fa7 | 2013-09-12 10:28:05 +0000 | [diff] [blame] | 392 | : MCTargetAsmParser(), STI(sti), Parser(parser) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 393 | MCAsmParserExtension::Initialize(Parser); |
| 394 | |
| 395 | // Initialize the set of available features. |
| 396 | setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits())); |
| 397 | } |
| 398 | |
| 399 | // Override MCTargetAsmParser. |
Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 400 | bool ParseDirective(AsmToken DirectiveID) override; |
| 401 | bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override; |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 402 | bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name, |
| 403 | SMLoc NameLoc, OperandVector &Operands) override; |
Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 404 | bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 405 | OperandVector &Operands, MCStreamer &Out, |
Tim Northover | 26bb14e | 2014-08-18 11:49:42 +0000 | [diff] [blame] | 406 | uint64_t &ErrorInfo, |
Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 407 | bool MatchingInlineAsm) override; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 408 | |
| 409 | // Used by the TableGen code to parse particular operand types. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 410 | OperandMatchResultTy parseGR32(OperandVector &Operands) { |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 411 | return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, GR32Reg); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 412 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 413 | OperandMatchResultTy parseGRH32(OperandVector &Operands) { |
Richard Sandiford | f949606 | 2013-09-30 10:45:16 +0000 | [diff] [blame] | 414 | return parseRegister(Operands, RegGR, SystemZMC::GRH32Regs, GRH32Reg); |
| 415 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 416 | OperandMatchResultTy parseGRX32(OperandVector &Operands) { |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 417 | llvm_unreachable("GRX32 should only be used for pseudo instructions"); |
| 418 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 419 | OperandMatchResultTy parseGR64(OperandVector &Operands) { |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 420 | return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, GR64Reg); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 421 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 422 | OperandMatchResultTy parseGR128(OperandVector &Operands) { |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 423 | return parseRegister(Operands, RegGR, SystemZMC::GR128Regs, GR128Reg); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 424 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 425 | OperandMatchResultTy parseADDR32(OperandVector &Operands) { |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 426 | return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, ADDR32Reg); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 427 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 428 | OperandMatchResultTy parseADDR64(OperandVector &Operands) { |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 429 | return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, ADDR64Reg); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 430 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 431 | OperandMatchResultTy parseADDR128(OperandVector &Operands) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 432 | llvm_unreachable("Shouldn't be used as an operand"); |
| 433 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 434 | OperandMatchResultTy parseFP32(OperandVector &Operands) { |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 435 | return parseRegister(Operands, RegFP, SystemZMC::FP32Regs, FP32Reg); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 436 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 437 | OperandMatchResultTy parseFP64(OperandVector &Operands) { |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 438 | return parseRegister(Operands, RegFP, SystemZMC::FP64Regs, FP64Reg); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 439 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 440 | OperandMatchResultTy parseFP128(OperandVector &Operands) { |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 441 | return parseRegister(Operands, RegFP, SystemZMC::FP128Regs, FP128Reg); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 442 | } |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 443 | OperandMatchResultTy parseVR32(OperandVector &Operands) { |
| 444 | return parseRegister(Operands, RegV, SystemZMC::VR32Regs, VR32Reg); |
| 445 | } |
| 446 | OperandMatchResultTy parseVR64(OperandVector &Operands) { |
| 447 | return parseRegister(Operands, RegV, SystemZMC::VR64Regs, VR64Reg); |
| 448 | } |
| 449 | OperandMatchResultTy parseVF128(OperandVector &Operands) { |
| 450 | llvm_unreachable("Shouldn't be used as an operand"); |
| 451 | } |
| 452 | OperandMatchResultTy parseVR128(OperandVector &Operands) { |
| 453 | return parseRegister(Operands, RegV, SystemZMC::VR128Regs, VR128Reg); |
| 454 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 455 | OperandMatchResultTy parseBDAddr32(OperandVector &Operands) { |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 456 | return parseAddress(Operands, BDMem, SystemZMC::GR32Regs, ADDR32Reg); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 457 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 458 | OperandMatchResultTy parseBDAddr64(OperandVector &Operands) { |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 459 | return parseAddress(Operands, BDMem, SystemZMC::GR64Regs, ADDR64Reg); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 460 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 461 | OperandMatchResultTy parseBDXAddr64(OperandVector &Operands) { |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 462 | return parseAddress(Operands, BDXMem, SystemZMC::GR64Regs, ADDR64Reg); |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 463 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 464 | OperandMatchResultTy parseBDLAddr64(OperandVector &Operands) { |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 465 | return parseAddress(Operands, BDLMem, SystemZMC::GR64Regs, ADDR64Reg); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 466 | } |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 467 | OperandMatchResultTy parseBDVAddr64(OperandVector &Operands) { |
| 468 | return parseAddress(Operands, BDVMem, SystemZMC::GR64Regs, ADDR64Reg); |
| 469 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 470 | OperandMatchResultTy parseAccessReg(OperandVector &Operands); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 471 | OperandMatchResultTy parsePCRel16(OperandVector &Operands) { |
Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 472 | return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1, false); |
Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 473 | } |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 474 | OperandMatchResultTy parsePCRel32(OperandVector &Operands) { |
Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 475 | return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1, false); |
| 476 | } |
| 477 | OperandMatchResultTy parsePCRelTLS16(OperandVector &Operands) { |
| 478 | return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1, true); |
| 479 | } |
| 480 | OperandMatchResultTy parsePCRelTLS32(OperandVector &Operands) { |
| 481 | return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1, true); |
Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 482 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 483 | }; |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 484 | } // end anonymous namespace |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 485 | |
| 486 | #define GET_REGISTER_MATCHER |
| 487 | #define GET_SUBTARGET_FEATURE_NAME |
| 488 | #define GET_MATCHER_IMPLEMENTATION |
| 489 | #include "SystemZGenAsmMatcher.inc" |
| 490 | |
| 491 | void SystemZOperand::print(raw_ostream &OS) const { |
| 492 | llvm_unreachable("Not implemented"); |
| 493 | } |
| 494 | |
| 495 | // Parse one register of the form %<prefix><number>. |
| 496 | bool SystemZAsmParser::parseRegister(Register &Reg) { |
| 497 | Reg.StartLoc = Parser.getTok().getLoc(); |
| 498 | |
| 499 | // Eat the % prefix. |
| 500 | if (Parser.getTok().isNot(AsmToken::Percent)) |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 501 | return Error(Parser.getTok().getLoc(), "register expected"); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 502 | Parser.Lex(); |
| 503 | |
| 504 | // Expect a register name. |
| 505 | if (Parser.getTok().isNot(AsmToken::Identifier)) |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 506 | return Error(Reg.StartLoc, "invalid register"); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 507 | |
Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 508 | // Check that there's a prefix. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 509 | StringRef Name = Parser.getTok().getString(); |
| 510 | if (Name.size() < 2) |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 511 | return Error(Reg.StartLoc, "invalid register"); |
Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 512 | char Prefix = Name[0]; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 513 | |
| 514 | // Treat the rest of the register name as a register number. |
Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 515 | if (Name.substr(1).getAsInteger(10, Reg.Num)) |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 516 | return Error(Reg.StartLoc, "invalid register"); |
Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 517 | |
| 518 | // Look for valid combinations of prefix and number. |
| 519 | if (Prefix == 'r' && Reg.Num < 16) |
| 520 | Reg.Group = RegGR; |
| 521 | else if (Prefix == 'f' && Reg.Num < 16) |
| 522 | Reg.Group = RegFP; |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 523 | else if (Prefix == 'v' && Reg.Num < 32) |
| 524 | Reg.Group = RegV; |
Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 525 | else if (Prefix == 'a' && Reg.Num < 16) |
| 526 | Reg.Group = RegAccess; |
| 527 | else |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 528 | return Error(Reg.StartLoc, "invalid register"); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 529 | |
| 530 | Reg.EndLoc = Parser.getTok().getLoc(); |
| 531 | Parser.Lex(); |
| 532 | return false; |
| 533 | } |
| 534 | |
Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 535 | // Parse a register of group Group. If Regs is nonnull, use it to map |
| 536 | // the raw register number to LLVM numbering, with zero entries indicating |
| 537 | // an invalid register. IsAddress says whether the register appears in an |
| 538 | // address context. |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 539 | bool SystemZAsmParser::parseRegister(Register &Reg, RegisterGroup Group, |
| 540 | const unsigned *Regs, bool IsAddress) { |
| 541 | if (parseRegister(Reg)) |
| 542 | return true; |
| 543 | if (Reg.Group != Group) |
| 544 | return Error(Reg.StartLoc, "invalid operand for instruction"); |
| 545 | if (Regs && Regs[Reg.Num] == 0) |
| 546 | return Error(Reg.StartLoc, "invalid register pair"); |
| 547 | if (Reg.Num == 0 && IsAddress) |
| 548 | return Error(Reg.StartLoc, "%r0 used in an address"); |
Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 549 | if (Regs) |
| 550 | Reg.Num = Regs[Reg.Num]; |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 551 | return false; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 554 | // 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] | 555 | SystemZAsmParser::OperandMatchResultTy |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 556 | SystemZAsmParser::parseRegister(OperandVector &Operands, RegisterGroup Group, |
| 557 | const unsigned *Regs, RegisterKind Kind) { |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 558 | if (Parser.getTok().isNot(AsmToken::Percent)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 559 | return MatchOperand_NoMatch; |
| 560 | |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 561 | Register Reg; |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 562 | bool IsAddress = (Kind == ADDR32Reg || Kind == ADDR64Reg); |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 563 | if (parseRegister(Reg, Group, Regs, IsAddress)) |
| 564 | return MatchOperand_ParseFail; |
| 565 | |
| 566 | Operands.push_back(SystemZOperand::createReg(Kind, Reg.Num, |
| 567 | Reg.StartLoc, Reg.EndLoc)); |
| 568 | return MatchOperand_Success; |
| 569 | } |
| 570 | |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 571 | // Parse a memory operand into Base, Disp, Index and Length. |
| 572 | // Regs maps asm register numbers to LLVM register numbers and RegKind |
| 573 | // says what kind of address register we're using (ADDR32Reg or ADDR64Reg). |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 574 | bool SystemZAsmParser::parseAddress(unsigned &Base, const MCExpr *&Disp, |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 575 | unsigned &Index, bool &IsVector, |
| 576 | const MCExpr *&Length, const unsigned *Regs, |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 577 | RegisterKind RegKind) { |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 578 | // Parse the displacement, which must always be present. |
| 579 | if (getParser().parseExpression(Disp)) |
| 580 | return true; |
| 581 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 582 | // Parse the optional base and index. |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 583 | Index = 0; |
| 584 | Base = 0; |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 585 | IsVector = false; |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 586 | Length = nullptr; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 587 | if (getLexer().is(AsmToken::LParen)) { |
| 588 | Parser.Lex(); |
| 589 | |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 590 | if (getLexer().is(AsmToken::Percent)) { |
| 591 | // Parse the first register and decide whether it's a base or an index. |
| 592 | Register Reg; |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 593 | if (parseRegister(Reg)) |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 594 | return true; |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 595 | if (Reg.Group == RegV) { |
| 596 | // A vector index register. The base register is optional. |
| 597 | IsVector = true; |
| 598 | Index = SystemZMC::VR128Regs[Reg.Num]; |
| 599 | } else if (Reg.Group == RegGR) { |
| 600 | if (Reg.Num == 0) |
| 601 | return Error(Reg.StartLoc, "%r0 used in an address"); |
| 602 | // If the are two registers, the first one is the index and the |
| 603 | // second is the base. |
| 604 | if (getLexer().is(AsmToken::Comma)) |
| 605 | Index = Regs[Reg.Num]; |
| 606 | else |
| 607 | Base = Regs[Reg.Num]; |
| 608 | } else |
| 609 | return Error(Reg.StartLoc, "invalid address register"); |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 610 | } else { |
| 611 | // Parse the length. |
| 612 | if (getParser().parseExpression(Length)) |
| 613 | return true; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 614 | } |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 615 | |
| 616 | // Check whether there's a second register. It's the base if so. |
| 617 | if (getLexer().is(AsmToken::Comma)) { |
| 618 | Parser.Lex(); |
| 619 | Register Reg; |
| 620 | if (parseRegister(Reg, RegGR, Regs, RegKind)) |
| 621 | return true; |
| 622 | Base = Reg.Num; |
| 623 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 624 | |
| 625 | // Consume the closing bracket. |
| 626 | if (getLexer().isNot(AsmToken::RParen)) |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 627 | return Error(Parser.getTok().getLoc(), "unexpected token in address"); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 628 | Parser.Lex(); |
| 629 | } |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 630 | return false; |
| 631 | } |
| 632 | |
| 633 | // Parse a memory operand and add it to Operands. The other arguments |
| 634 | // are as above. |
| 635 | SystemZAsmParser::OperandMatchResultTy |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 636 | SystemZAsmParser::parseAddress(OperandVector &Operands, MemoryKind MemKind, |
| 637 | const unsigned *Regs, RegisterKind RegKind) { |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 638 | SMLoc StartLoc = Parser.getTok().getLoc(); |
| 639 | unsigned Base, Index; |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 640 | bool IsVector; |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 641 | const MCExpr *Disp; |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 642 | const MCExpr *Length; |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 643 | if (parseAddress(Base, Disp, Index, IsVector, Length, Regs, RegKind)) |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 644 | return MatchOperand_ParseFail; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 645 | |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 646 | if (IsVector && MemKind != BDVMem) { |
| 647 | Error(StartLoc, "invalid use of vector addressing"); |
| 648 | return MatchOperand_ParseFail; |
| 649 | } |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 650 | |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 651 | if (!IsVector && MemKind == BDVMem) { |
| 652 | Error(StartLoc, "vector index required in address"); |
| 653 | return MatchOperand_ParseFail; |
| 654 | } |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 655 | |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 656 | if (Index && MemKind != BDXMem && MemKind != BDVMem) { |
| 657 | Error(StartLoc, "invalid use of indexed addressing"); |
| 658 | return MatchOperand_ParseFail; |
| 659 | } |
| 660 | |
| 661 | if (Length && MemKind != BDLMem) { |
| 662 | Error(StartLoc, "invalid use of length addressing"); |
| 663 | return MatchOperand_ParseFail; |
| 664 | } |
| 665 | |
| 666 | if (!Length && MemKind == BDLMem) { |
| 667 | Error(StartLoc, "missing length in address"); |
| 668 | return MatchOperand_ParseFail; |
| 669 | } |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 670 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 671 | SMLoc EndLoc = |
| 672 | SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); |
Ulrich Weigand | 1f698b0 | 2015-05-04 17:40:53 +0000 | [diff] [blame] | 673 | Operands.push_back(SystemZOperand::createMem(MemKind, RegKind, Base, Disp, |
| 674 | Index, Length, StartLoc, |
| 675 | EndLoc)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 676 | return MatchOperand_Success; |
| 677 | } |
| 678 | |
| 679 | bool SystemZAsmParser::ParseDirective(AsmToken DirectiveID) { |
| 680 | return true; |
| 681 | } |
| 682 | |
| 683 | bool SystemZAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, |
| 684 | SMLoc &EndLoc) { |
| 685 | Register Reg; |
| 686 | if (parseRegister(Reg)) |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 687 | return true; |
Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 688 | if (Reg.Group == RegGR) |
| 689 | RegNo = SystemZMC::GR64Regs[Reg.Num]; |
| 690 | else if (Reg.Group == RegFP) |
| 691 | RegNo = SystemZMC::FP64Regs[Reg.Num]; |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 692 | else if (Reg.Group == RegV) |
| 693 | RegNo = SystemZMC::VR128Regs[Reg.Num]; |
Richard Sandiford | 675f869 | 2013-05-24 14:14:38 +0000 | [diff] [blame] | 694 | else |
| 695 | // FIXME: Access registers aren't modelled as LLVM registers yet. |
| 696 | return Error(Reg.StartLoc, "invalid operand for instruction"); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 697 | StartLoc = Reg.StartLoc; |
| 698 | EndLoc = Reg.EndLoc; |
| 699 | return false; |
| 700 | } |
| 701 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 702 | bool SystemZAsmParser::ParseInstruction(ParseInstructionInfo &Info, |
| 703 | StringRef Name, SMLoc NameLoc, |
| 704 | OperandVector &Operands) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 705 | Operands.push_back(SystemZOperand::createToken(Name, NameLoc)); |
| 706 | |
| 707 | // Read the remaining operands. |
| 708 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 709 | // Read the first operand. |
| 710 | if (parseOperand(Operands, Name)) { |
| 711 | Parser.eatToEndOfStatement(); |
| 712 | return true; |
| 713 | } |
| 714 | |
| 715 | // Read any subsequent operands. |
| 716 | while (getLexer().is(AsmToken::Comma)) { |
| 717 | Parser.Lex(); |
| 718 | if (parseOperand(Operands, Name)) { |
| 719 | Parser.eatToEndOfStatement(); |
| 720 | return true; |
| 721 | } |
| 722 | } |
| 723 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 724 | SMLoc Loc = getLexer().getLoc(); |
| 725 | Parser.eatToEndOfStatement(); |
| 726 | return Error(Loc, "unexpected token in argument list"); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | // Consume the EndOfStatement. |
| 731 | Parser.Lex(); |
| 732 | return false; |
| 733 | } |
| 734 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 735 | bool SystemZAsmParser::parseOperand(OperandVector &Operands, |
| 736 | StringRef Mnemonic) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 737 | // Check if the current operand has a custom associated parser, if so, try to |
| 738 | // custom parse the operand, or fallback to the general approach. |
| 739 | OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic); |
| 740 | if (ResTy == MatchOperand_Success) |
| 741 | return false; |
| 742 | |
| 743 | // If there wasn't a custom match, try the generic matcher below. Otherwise, |
| 744 | // there was a match, but an error occurred, in which case, just return that |
| 745 | // the operand parsing failed. |
| 746 | if (ResTy == MatchOperand_ParseFail) |
| 747 | return true; |
| 748 | |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 749 | // Check for a register. All real register operands should have used |
| 750 | // a context-dependent parse routine, which gives the required register |
| 751 | // class. The code is here to mop up other cases, like those where |
| 752 | // the instruction isn't recognized. |
| 753 | if (Parser.getTok().is(AsmToken::Percent)) { |
| 754 | Register Reg; |
| 755 | if (parseRegister(Reg)) |
| 756 | return true; |
| 757 | Operands.push_back(SystemZOperand::createInvalid(Reg.StartLoc, Reg.EndLoc)); |
| 758 | return false; |
| 759 | } |
| 760 | |
| 761 | // The only other type of operand is an immediate or address. As above, |
| 762 | // real address operands should have used a context-dependent parse routine, |
| 763 | // so we treat any plain expression as an immediate. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 764 | SMLoc StartLoc = Parser.getTok().getLoc(); |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 765 | unsigned Base, Index; |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 766 | bool IsVector; |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 767 | const MCExpr *Expr, *Length; |
Ulrich Weigand | a8b04e1 | 2015-05-05 19:23:40 +0000 | [diff] [blame^] | 768 | if (parseAddress(Base, Expr, Index, IsVector, Length, SystemZMC::GR64Regs, |
| 769 | ADDR64Reg)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 770 | return true; |
| 771 | |
| 772 | SMLoc EndLoc = |
| 773 | SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); |
Richard Sandiford | 1d95900 | 2013-07-02 14:56:45 +0000 | [diff] [blame] | 774 | if (Base || Index || Length) |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 775 | Operands.push_back(SystemZOperand::createInvalid(StartLoc, EndLoc)); |
| 776 | else |
| 777 | Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 778 | return false; |
| 779 | } |
| 780 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 781 | bool SystemZAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, |
| 782 | OperandVector &Operands, |
| 783 | MCStreamer &Out, |
Tim Northover | 26bb14e | 2014-08-18 11:49:42 +0000 | [diff] [blame] | 784 | uint64_t &ErrorInfo, |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 785 | bool MatchingInlineAsm) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 786 | MCInst Inst; |
| 787 | unsigned MatchResult; |
| 788 | |
| 789 | MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo, |
| 790 | MatchingInlineAsm); |
| 791 | switch (MatchResult) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 792 | case Match_Success: |
| 793 | Inst.setLoc(IDLoc); |
David Woodhouse | e6c13e4 | 2014-01-28 23:12:42 +0000 | [diff] [blame] | 794 | Out.EmitInstruction(Inst, STI); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 795 | return false; |
| 796 | |
| 797 | case Match_MissingFeature: { |
| 798 | assert(ErrorInfo && "Unknown missing feature!"); |
| 799 | // Special case the error message for the very common case where only |
| 800 | // a single subtarget feature is missing |
| 801 | std::string Msg = "instruction requires:"; |
Tim Northover | 26bb14e | 2014-08-18 11:49:42 +0000 | [diff] [blame] | 802 | uint64_t Mask = 1; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 803 | for (unsigned I = 0; I < sizeof(ErrorInfo) * 8 - 1; ++I) { |
| 804 | if (ErrorInfo & Mask) { |
| 805 | Msg += " "; |
| 806 | Msg += getSubtargetFeatureName(ErrorInfo & Mask); |
| 807 | } |
| 808 | Mask <<= 1; |
| 809 | } |
| 810 | return Error(IDLoc, Msg); |
| 811 | } |
| 812 | |
| 813 | case Match_InvalidOperand: { |
| 814 | SMLoc ErrorLoc = IDLoc; |
Tim Northover | 26bb14e | 2014-08-18 11:49:42 +0000 | [diff] [blame] | 815 | if (ErrorInfo != ~0ULL) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 816 | if (ErrorInfo >= Operands.size()) |
| 817 | return Error(IDLoc, "too few operands for instruction"); |
| 818 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 819 | ErrorLoc = ((SystemZOperand &)*Operands[ErrorInfo]).getStartLoc(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 820 | if (ErrorLoc == SMLoc()) |
| 821 | ErrorLoc = IDLoc; |
| 822 | } |
| 823 | return Error(ErrorLoc, "invalid operand for instruction"); |
| 824 | } |
| 825 | |
| 826 | case Match_MnemonicFail: |
| 827 | return Error(IDLoc, "invalid instruction"); |
| 828 | } |
| 829 | |
| 830 | llvm_unreachable("Unexpected match type"); |
| 831 | } |
| 832 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 833 | SystemZAsmParser::OperandMatchResultTy |
| 834 | SystemZAsmParser::parseAccessReg(OperandVector &Operands) { |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 835 | if (Parser.getTok().isNot(AsmToken::Percent)) |
| 836 | return MatchOperand_NoMatch; |
| 837 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 838 | Register Reg; |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 839 | if (parseRegister(Reg, RegAccess, nullptr)) |
Richard Sandiford | dc5ed71 | 2013-05-24 14:26:46 +0000 | [diff] [blame] | 840 | return MatchOperand_ParseFail; |
| 841 | |
| 842 | Operands.push_back(SystemZOperand::createAccessReg(Reg.Num, |
| 843 | Reg.StartLoc, |
| 844 | Reg.EndLoc)); |
| 845 | return MatchOperand_Success; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 846 | } |
| 847 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 848 | SystemZAsmParser::OperandMatchResultTy |
| 849 | SystemZAsmParser::parsePCRel(OperandVector &Operands, int64_t MinVal, |
Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 850 | int64_t MaxVal, bool AllowTLS) { |
Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 851 | MCContext &Ctx = getContext(); |
| 852 | MCStreamer &Out = getStreamer(); |
| 853 | const MCExpr *Expr; |
| 854 | SMLoc StartLoc = Parser.getTok().getLoc(); |
| 855 | if (getParser().parseExpression(Expr)) |
| 856 | return MatchOperand_NoMatch; |
| 857 | |
| 858 | // For consistency with the GNU assembler, treat immediates as offsets |
| 859 | // from ".". |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 860 | if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) { |
Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 861 | int64_t Value = CE->getValue(); |
| 862 | if ((Value & 1) || Value < MinVal || Value > MaxVal) { |
| 863 | Error(StartLoc, "offset out of range"); |
| 864 | return MatchOperand_ParseFail; |
| 865 | } |
| 866 | MCSymbol *Sym = Ctx.CreateTempSymbol(); |
| 867 | Out.EmitLabel(Sym); |
| 868 | const MCExpr *Base = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, |
| 869 | Ctx); |
| 870 | Expr = Value == 0 ? Base : MCBinaryExpr::CreateAdd(Base, Expr, Ctx); |
| 871 | } |
| 872 | |
Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 873 | // Optionally match :tls_gdcall: or :tls_ldcall: followed by a TLS symbol. |
| 874 | const MCExpr *Sym = nullptr; |
| 875 | if (AllowTLS && getLexer().is(AsmToken::Colon)) { |
| 876 | Parser.Lex(); |
| 877 | |
| 878 | if (Parser.getTok().isNot(AsmToken::Identifier)) { |
| 879 | Error(Parser.getTok().getLoc(), "unexpected token"); |
| 880 | return MatchOperand_ParseFail; |
| 881 | } |
| 882 | |
| 883 | MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None; |
| 884 | StringRef Name = Parser.getTok().getString(); |
| 885 | if (Name == "tls_gdcall") |
| 886 | Kind = MCSymbolRefExpr::VK_TLSGD; |
| 887 | else if (Name == "tls_ldcall") |
| 888 | Kind = MCSymbolRefExpr::VK_TLSLDM; |
| 889 | else { |
| 890 | Error(Parser.getTok().getLoc(), "unknown TLS tag"); |
| 891 | return MatchOperand_ParseFail; |
| 892 | } |
| 893 | Parser.Lex(); |
| 894 | |
| 895 | if (Parser.getTok().isNot(AsmToken::Colon)) { |
| 896 | Error(Parser.getTok().getLoc(), "unexpected token"); |
| 897 | return MatchOperand_ParseFail; |
| 898 | } |
| 899 | Parser.Lex(); |
| 900 | |
| 901 | if (Parser.getTok().isNot(AsmToken::Identifier)) { |
| 902 | Error(Parser.getTok().getLoc(), "unexpected token"); |
| 903 | return MatchOperand_ParseFail; |
| 904 | } |
| 905 | |
| 906 | StringRef Identifier = Parser.getTok().getString(); |
| 907 | Sym = MCSymbolRefExpr::Create(Ctx.GetOrCreateSymbol(Identifier), |
| 908 | Kind, Ctx); |
| 909 | Parser.Lex(); |
| 910 | } |
| 911 | |
Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 912 | SMLoc EndLoc = |
| 913 | SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); |
Ulrich Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 914 | |
| 915 | if (AllowTLS) |
| 916 | Operands.push_back(SystemZOperand::createImmTLS(Expr, Sym, |
| 917 | StartLoc, EndLoc)); |
| 918 | else |
| 919 | Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc)); |
| 920 | |
Richard Sandiford | 1fb5883 | 2013-05-14 09:47:26 +0000 | [diff] [blame] | 921 | return MatchOperand_Success; |
| 922 | } |
| 923 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 924 | // Force static initialization. |
| 925 | extern "C" void LLVMInitializeSystemZAsmParser() { |
| 926 | RegisterMCAsmParser<SystemZAsmParser> X(TheSystemZTarget); |
| 927 | } |