blob: a894bea91b6cf7a4fcedd12a7177eb4589d37329 [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- 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 Topper690d8ea2013-07-24 07:33:14 +000011#include "llvm/ADT/STLExtras.h"
Richard Sandiford1fb58832013-05-14 09:47:26 +000012#include "llvm/MC/MCContext.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000013#include "llvm/MC/MCExpr.h"
14#include "llvm/MC/MCInst.h"
15#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000016#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000017#include "llvm/MC/MCStreamer.h"
18#include "llvm/MC/MCSubtargetInfo.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000019#include "llvm/Support/TargetRegistry.h"
20
21using namespace llvm;
22
23// Return true if Expr is in the range [MinValue, MaxValue].
24static bool inRange(const MCExpr *Expr, int64_t MinValue, int64_t MaxValue) {
Richard Sandiford21f5d682014-03-06 11:22:58 +000025 if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000026 int64_t Value = CE->getValue();
27 return Value >= MinValue && Value <= MaxValue;
28 }
29 return false;
30}
31
32namespace {
Richard Sandiford1d959002013-07-02 14:56:45 +000033enum RegisterKind {
34 GR32Reg,
Richard Sandifordf9496062013-09-30 10:45:16 +000035 GRH32Reg,
Richard Sandiford1d959002013-07-02 14:56:45 +000036 GR64Reg,
37 GR128Reg,
38 ADDR32Reg,
39 ADDR64Reg,
40 FP32Reg,
41 FP64Reg,
Ulrich Weiganda8b04e12015-05-05 19:23:40 +000042 FP128Reg,
43 VR32Reg,
44 VR64Reg,
45 VR128Reg
Richard Sandiford1d959002013-07-02 14:56:45 +000046};
47
48enum MemoryKind {
49 BDMem,
50 BDXMem,
Ulrich Weiganda8b04e12015-05-05 19:23:40 +000051 BDLMem,
52 BDVMem
Richard Sandiford1d959002013-07-02 14:56:45 +000053};
54
Ulrich Weigand5f613df2013-05-06 16:15:19 +000055class SystemZOperand : public MCParsedAsmOperand {
56public:
Ulrich Weigand5f613df2013-05-06 16:15:19 +000057private:
58 enum OperandKind {
Richard Sandiforddc5ed712013-05-24 14:26:46 +000059 KindInvalid,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000060 KindToken,
61 KindReg,
62 KindAccessReg,
63 KindImm,
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +000064 KindImmTLS,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000065 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 Sandiford675f8692013-05-24 14:14:38 +000077 // 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 Weigand5f613df2013-05-06 16:15:19 +000086 struct RegOp {
87 RegisterKind Kind;
88 unsigned Num;
89 };
90
91 // Base + Disp + Index, where Base and Index are LLVM registers or 0.
Ulrich Weigand1f698b02015-05-04 17:40:53 +000092 // 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 Weigand5f613df2013-05-06 16:15:19 +000095 struct MemOp {
Ulrich Weiganda8b04e12015-05-05 19:23:40 +000096 unsigned Base : 12;
97 unsigned Index : 12;
98 unsigned MemKind : 4;
99 unsigned RegKind : 4;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000100 const MCExpr *Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +0000101 const MCExpr *Length;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000102 };
103
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000104 // 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 Weigand5f613df2013-05-06 16:15:19 +0000111 union {
112 TokenOp Token;
113 RegOp Reg;
114 unsigned AccessReg;
115 const MCExpr *Imm;
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000116 ImmTLSOp ImmTLS;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000117 MemOp Mem;
118 };
119
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000120 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
121 // Add as immediates when possible. Null MCExpr = 0.
Craig Topper062a2ba2014-04-25 05:30:21 +0000122 if (!Expr)
Jim Grosbache9119e42015-05-13 18:37:00 +0000123 Inst.addOperand(MCOperand::createImm(0));
Richard Sandiford21f5d682014-03-06 11:22:58 +0000124 else if (auto *CE = dyn_cast<MCConstantExpr>(Expr))
Jim Grosbache9119e42015-05-13 18:37:00 +0000125 Inst.addOperand(MCOperand::createImm(CE->getValue()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000126 else
Jim Grosbache9119e42015-05-13 18:37:00 +0000127 Inst.addOperand(MCOperand::createExpr(Expr));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000128 }
129
130public:
David Blaikie960ea3f2014-06-08 16:18:35 +0000131 SystemZOperand(OperandKind kind, SMLoc startLoc, SMLoc endLoc)
132 : Kind(kind), StartLoc(startLoc), EndLoc(endLoc) {}
133
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000134 // Create particular kinds of operand.
David Blaikie960ea3f2014-06-08 16:18:35 +0000135 static std::unique_ptr<SystemZOperand> createInvalid(SMLoc StartLoc,
136 SMLoc EndLoc) {
137 return make_unique<SystemZOperand>(KindInvalid, StartLoc, EndLoc);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000138 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000139 static std::unique_ptr<SystemZOperand> createToken(StringRef Str, SMLoc Loc) {
140 auto Op = make_unique<SystemZOperand>(KindToken, Loc, Loc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000141 Op->Token.Data = Str.data();
142 Op->Token.Length = Str.size();
143 return Op;
144 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000145 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 Weigand5f613df2013-05-06 16:15:19 +0000148 Op->Reg.Kind = Kind;
149 Op->Reg.Num = Num;
150 return Op;
151 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000152 static std::unique_ptr<SystemZOperand>
153 createAccessReg(unsigned Num, SMLoc StartLoc, SMLoc EndLoc) {
154 auto Op = make_unique<SystemZOperand>(KindAccessReg, StartLoc, EndLoc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000155 Op->AccessReg = Num;
156 return Op;
157 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000158 static std::unique_ptr<SystemZOperand>
159 createImm(const MCExpr *Expr, SMLoc StartLoc, SMLoc EndLoc) {
160 auto Op = make_unique<SystemZOperand>(KindImm, StartLoc, EndLoc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000161 Op->Imm = Expr;
162 return Op;
163 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000164 static std::unique_ptr<SystemZOperand>
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000165 createMem(MemoryKind MemKind, RegisterKind RegKind, unsigned Base,
166 const MCExpr *Disp, unsigned Index, const MCExpr *Length,
167 SMLoc StartLoc, SMLoc EndLoc) {
David Blaikie960ea3f2014-06-08 16:18:35 +0000168 auto Op = make_unique<SystemZOperand>(KindMem, StartLoc, EndLoc);
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000169 Op->Mem.MemKind = MemKind;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000170 Op->Mem.RegKind = RegKind;
171 Op->Mem.Base = Base;
172 Op->Mem.Index = Index;
173 Op->Mem.Disp = Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +0000174 Op->Mem.Length = Length;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000175 return Op;
176 }
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000177 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 Weigand5f613df2013-05-06 16:15:19 +0000185
186 // Token operands
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000187 bool isToken() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000188 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 Sandifordb4d67b52014-03-06 12:03:36 +0000196 bool isReg() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000197 return Kind == KindReg;
198 }
199 bool isReg(RegisterKind RegKind) const {
200 return Kind == KindReg && Reg.Kind == RegKind;
201 }
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000202 unsigned getReg() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000203 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 Sandifordb4d67b52014-03-06 12:03:36 +0000214 bool isImm() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000215 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 Weigand7bdd7c22015-02-18 09:11:36 +0000225 // Immediate operands with optional TLS symbol.
226 bool isImmTLS() const {
227 return Kind == KindImmTLS;
228 }
229
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000230 // Memory operands.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000231 bool isMem() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000232 return Kind == KindMem;
233 }
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000234 bool isMem(MemoryKind MemKind) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000235 return (Kind == KindMem &&
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000236 (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 Weigand5f613df2013-05-06 16:15:19 +0000240 }
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000241 bool isMem(MemoryKind MemKind, RegisterKind RegKind) const {
242 return isMem(MemKind) && Mem.RegKind == RegKind;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000243 }
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000244 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 Sandiford1d959002013-07-02 14:56:45 +0000249 }
250 bool isMemDisp12Len8(RegisterKind RegKind) const {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000251 return isMemDisp12(BDLMem, RegKind) && inRange(Mem.Length, 1, 0x100);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000252 }
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000253 void addBDVAddrOperands(MCInst &Inst, unsigned N) const {
254 assert(N == 3 && "Invalid number of operands");
255 assert(isMem(BDVMem) && "Invalid operand type");
Jim Grosbache9119e42015-05-13 18:37:00 +0000256 Inst.addOperand(MCOperand::createReg(Mem.Base));
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000257 addExpr(Inst, Mem.Disp);
Jim Grosbache9119e42015-05-13 18:37:00 +0000258 Inst.addOperand(MCOperand::createReg(Mem.Index));
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000259 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000260
261 // Override MCParsedAsmOperand.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000262 SMLoc getStartLoc() const override { return StartLoc; }
263 SMLoc getEndLoc() const override { return EndLoc; }
264 void print(raw_ostream &OS) const override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000265
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");
Jim Grosbache9119e42015-05-13 18:37:00 +0000270 Inst.addOperand(MCOperand::createReg(getReg()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000271 }
272 void addAccessRegOperands(MCInst &Inst, unsigned N) const {
273 assert(N == 1 && "Invalid number of operands");
274 assert(Kind == KindAccessReg && "Invalid operand type");
Jim Grosbache9119e42015-05-13 18:37:00 +0000275 Inst.addOperand(MCOperand::createImm(AccessReg));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000276 }
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 Weigand1f698b02015-05-04 17:40:53 +0000283 assert(isMem(BDMem) && "Invalid operand type");
Jim Grosbache9119e42015-05-13 18:37:00 +0000284 Inst.addOperand(MCOperand::createReg(Mem.Base));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000285 addExpr(Inst, Mem.Disp);
286 }
287 void addBDXAddrOperands(MCInst &Inst, unsigned N) const {
288 assert(N == 3 && "Invalid number of operands");
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000289 assert(isMem(BDXMem) && "Invalid operand type");
Jim Grosbache9119e42015-05-13 18:37:00 +0000290 Inst.addOperand(MCOperand::createReg(Mem.Base));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000291 addExpr(Inst, Mem.Disp);
Jim Grosbache9119e42015-05-13 18:37:00 +0000292 Inst.addOperand(MCOperand::createReg(Mem.Index));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000293 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000294 void addBDLAddrOperands(MCInst &Inst, unsigned N) const {
295 assert(N == 3 && "Invalid number of operands");
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000296 assert(isMem(BDLMem) && "Invalid operand type");
Jim Grosbache9119e42015-05-13 18:37:00 +0000297 Inst.addOperand(MCOperand::createReg(Mem.Base));
Richard Sandiford1d959002013-07-02 14:56:45 +0000298 addExpr(Inst, Mem.Disp);
299 addExpr(Inst, Mem.Length);
300 }
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000301 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 Weigand5f613df2013-05-06 16:15:19 +0000308
309 // Used by the TableGen code to check for particular operand types.
310 bool isGR32() const { return isReg(GR32Reg); }
Richard Sandifordf9496062013-09-30 10:45:16 +0000311 bool isGRH32() const { return isReg(GRH32Reg); }
Richard Sandiford0755c932013-10-01 11:26:28 +0000312 bool isGRX32() const { return false; }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000313 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 Weiganda8b04e12015-05-05 19:23:40 +0000321 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 Weigand1f698b02015-05-04 17:40:53 +0000325 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 Sandiford1d959002013-07-02 14:56:45 +0000331 bool isBDLAddr64Disp12Len8() const { return isMemDisp12Len8(ADDR64Reg); }
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000332 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 Weigand5f613df2013-05-06 16:15:19 +0000336 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 Weiganda8b04e12015-05-05 19:23:40 +0000340 bool isU12Imm() const { return isImm(0, 4095); }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000341 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 Weigand5f613df2013-05-06 16:15:19 +0000347class SystemZAsmParser : public MCTargetAsmParser {
348#define GET_ASSEMBLER_HEADER
349#include "SystemZGenAsmMatcher.inc"
350
351private:
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000352 MCAsmParser &Parser;
Richard Sandiford675f8692013-05-24 14:14:38 +0000353 enum RegisterGroup {
354 RegGR,
355 RegFP,
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000356 RegV,
Richard Sandiford675f8692013-05-24 14:14:38 +0000357 RegAccess
358 };
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000359 struct Register {
Richard Sandiford675f8692013-05-24 14:14:38 +0000360 RegisterGroup Group;
361 unsigned Num;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000362 SMLoc StartLoc, EndLoc;
363 };
364
365 bool parseRegister(Register &Reg);
366
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000367 bool parseRegister(Register &Reg, RegisterGroup Group, const unsigned *Regs,
368 bool IsAddress = false);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000369
David Blaikie960ea3f2014-06-08 16:18:35 +0000370 OperandMatchResultTy parseRegister(OperandVector &Operands,
371 RegisterGroup Group, const unsigned *Regs,
372 RegisterKind Kind);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000373
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000374 bool parseAddress(unsigned &Base, const MCExpr *&Disp,
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000375 unsigned &Index, bool &IsVector, const MCExpr *&Length,
Richard Sandiford1d959002013-07-02 14:56:45 +0000376 const unsigned *Regs, RegisterKind RegKind);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000377
David Blaikie960ea3f2014-06-08 16:18:35 +0000378 OperandMatchResultTy parseAddress(OperandVector &Operands,
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000379 MemoryKind MemKind, const unsigned *Regs,
380 RegisterKind RegKind);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000381
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000382 OperandMatchResultTy parsePCRel(OperandVector &Operands, int64_t MinVal,
383 int64_t MaxVal, bool AllowTLS);
384
David Blaikie960ea3f2014-06-08 16:18:35 +0000385 bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000386
387public:
Akira Hatanakab11ef082015-11-14 06:35:56 +0000388 SystemZAsmParser(const MCSubtargetInfo &sti, MCAsmParser &parser,
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000389 const MCInstrInfo &MII,
390 const MCTargetOptions &Options)
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000391 : MCTargetAsmParser(Options, sti), Parser(parser) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000392 MCAsmParserExtension::Initialize(Parser);
393
394 // Initialize the set of available features.
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000395 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000396 }
397
398 // Override MCTargetAsmParser.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000399 bool ParseDirective(AsmToken DirectiveID) override;
400 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
David Blaikie960ea3f2014-06-08 16:18:35 +0000401 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
402 SMLoc NameLoc, OperandVector &Operands) override;
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000403 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
David Blaikie960ea3f2014-06-08 16:18:35 +0000404 OperandVector &Operands, MCStreamer &Out,
Tim Northover26bb14e2014-08-18 11:49:42 +0000405 uint64_t &ErrorInfo,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000406 bool MatchingInlineAsm) override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000407
408 // Used by the TableGen code to parse particular operand types.
David Blaikie960ea3f2014-06-08 16:18:35 +0000409 OperandMatchResultTy parseGR32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000410 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, GR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000411 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000412 OperandMatchResultTy parseGRH32(OperandVector &Operands) {
Richard Sandifordf9496062013-09-30 10:45:16 +0000413 return parseRegister(Operands, RegGR, SystemZMC::GRH32Regs, GRH32Reg);
414 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000415 OperandMatchResultTy parseGRX32(OperandVector &Operands) {
Richard Sandiford0755c932013-10-01 11:26:28 +0000416 llvm_unreachable("GRX32 should only be used for pseudo instructions");
417 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000418 OperandMatchResultTy parseGR64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000419 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, GR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000420 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000421 OperandMatchResultTy parseGR128(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000422 return parseRegister(Operands, RegGR, SystemZMC::GR128Regs, GR128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000423 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000424 OperandMatchResultTy parseADDR32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000425 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, ADDR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000426 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000427 OperandMatchResultTy parseADDR64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000428 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000429 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000430 OperandMatchResultTy parseADDR128(OperandVector &Operands) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000431 llvm_unreachable("Shouldn't be used as an operand");
432 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000433 OperandMatchResultTy parseFP32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000434 return parseRegister(Operands, RegFP, SystemZMC::FP32Regs, FP32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000435 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000436 OperandMatchResultTy parseFP64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000437 return parseRegister(Operands, RegFP, SystemZMC::FP64Regs, FP64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000438 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000439 OperandMatchResultTy parseFP128(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000440 return parseRegister(Operands, RegFP, SystemZMC::FP128Regs, FP128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000441 }
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000442 OperandMatchResultTy parseVR32(OperandVector &Operands) {
443 return parseRegister(Operands, RegV, SystemZMC::VR32Regs, VR32Reg);
444 }
445 OperandMatchResultTy parseVR64(OperandVector &Operands) {
446 return parseRegister(Operands, RegV, SystemZMC::VR64Regs, VR64Reg);
447 }
448 OperandMatchResultTy parseVF128(OperandVector &Operands) {
449 llvm_unreachable("Shouldn't be used as an operand");
450 }
451 OperandMatchResultTy parseVR128(OperandVector &Operands) {
452 return parseRegister(Operands, RegV, SystemZMC::VR128Regs, VR128Reg);
453 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000454 OperandMatchResultTy parseBDAddr32(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000455 return parseAddress(Operands, BDMem, SystemZMC::GR32Regs, ADDR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000456 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000457 OperandMatchResultTy parseBDAddr64(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000458 return parseAddress(Operands, BDMem, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000459 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000460 OperandMatchResultTy parseBDXAddr64(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000461 return parseAddress(Operands, BDXMem, SystemZMC::GR64Regs, ADDR64Reg);
Richard Sandiford1d959002013-07-02 14:56:45 +0000462 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000463 OperandMatchResultTy parseBDLAddr64(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000464 return parseAddress(Operands, BDLMem, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000465 }
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000466 OperandMatchResultTy parseBDVAddr64(OperandVector &Operands) {
467 return parseAddress(Operands, BDVMem, SystemZMC::GR64Regs, ADDR64Reg);
468 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000469 OperandMatchResultTy parseAccessReg(OperandVector &Operands);
David Blaikie960ea3f2014-06-08 16:18:35 +0000470 OperandMatchResultTy parsePCRel16(OperandVector &Operands) {
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000471 return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1, false);
Richard Sandiford1fb58832013-05-14 09:47:26 +0000472 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000473 OperandMatchResultTy parsePCRel32(OperandVector &Operands) {
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000474 return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1, false);
475 }
476 OperandMatchResultTy parsePCRelTLS16(OperandVector &Operands) {
477 return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1, true);
478 }
479 OperandMatchResultTy parsePCRelTLS32(OperandVector &Operands) {
480 return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1, true);
Richard Sandiford1fb58832013-05-14 09:47:26 +0000481 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000482};
Richard Sandifordc2312692014-03-06 10:38:30 +0000483} // end anonymous namespace
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000484
485#define GET_REGISTER_MATCHER
486#define GET_SUBTARGET_FEATURE_NAME
487#define GET_MATCHER_IMPLEMENTATION
488#include "SystemZGenAsmMatcher.inc"
489
490void SystemZOperand::print(raw_ostream &OS) const {
491 llvm_unreachable("Not implemented");
492}
493
494// Parse one register of the form %<prefix><number>.
495bool SystemZAsmParser::parseRegister(Register &Reg) {
496 Reg.StartLoc = Parser.getTok().getLoc();
497
498 // Eat the % prefix.
499 if (Parser.getTok().isNot(AsmToken::Percent))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000500 return Error(Parser.getTok().getLoc(), "register expected");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000501 Parser.Lex();
502
503 // Expect a register name.
504 if (Parser.getTok().isNot(AsmToken::Identifier))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000505 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000506
Richard Sandiford675f8692013-05-24 14:14:38 +0000507 // Check that there's a prefix.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000508 StringRef Name = Parser.getTok().getString();
509 if (Name.size() < 2)
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000510 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000511 char Prefix = Name[0];
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000512
513 // Treat the rest of the register name as a register number.
Richard Sandiford675f8692013-05-24 14:14:38 +0000514 if (Name.substr(1).getAsInteger(10, Reg.Num))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000515 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000516
517 // Look for valid combinations of prefix and number.
518 if (Prefix == 'r' && Reg.Num < 16)
519 Reg.Group = RegGR;
520 else if (Prefix == 'f' && Reg.Num < 16)
521 Reg.Group = RegFP;
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000522 else if (Prefix == 'v' && Reg.Num < 32)
523 Reg.Group = RegV;
Richard Sandiford675f8692013-05-24 14:14:38 +0000524 else if (Prefix == 'a' && Reg.Num < 16)
525 Reg.Group = RegAccess;
526 else
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000527 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000528
529 Reg.EndLoc = Parser.getTok().getLoc();
530 Parser.Lex();
531 return false;
532}
533
Richard Sandiford675f8692013-05-24 14:14:38 +0000534// Parse a register of group Group. If Regs is nonnull, use it to map
Jonas Paulsson0a9049b2015-10-09 07:19:12 +0000535// the raw register number to LLVM numbering, with zero entries
536// indicating an invalid register. IsAddress says whether the
537// register appears in an address context. Allow FP Group if expecting
538// RegV Group, since the f-prefix yields the FP group even while used
539// with vector instructions.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000540bool SystemZAsmParser::parseRegister(Register &Reg, RegisterGroup Group,
541 const unsigned *Regs, bool IsAddress) {
542 if (parseRegister(Reg))
543 return true;
Jonas Paulsson0a9049b2015-10-09 07:19:12 +0000544 if (Reg.Group != Group && !(Reg.Group == RegFP && Group == RegV))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000545 return Error(Reg.StartLoc, "invalid operand for instruction");
546 if (Regs && Regs[Reg.Num] == 0)
547 return Error(Reg.StartLoc, "invalid register pair");
548 if (Reg.Num == 0 && IsAddress)
549 return Error(Reg.StartLoc, "%r0 used in an address");
Richard Sandiford675f8692013-05-24 14:14:38 +0000550 if (Regs)
551 Reg.Num = Regs[Reg.Num];
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000552 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000553}
554
Richard Sandiford675f8692013-05-24 14:14:38 +0000555// Parse a register and add it to Operands. The other arguments are as above.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000556SystemZAsmParser::OperandMatchResultTy
David Blaikie960ea3f2014-06-08 16:18:35 +0000557SystemZAsmParser::parseRegister(OperandVector &Operands, RegisterGroup Group,
558 const unsigned *Regs, RegisterKind Kind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000559 if (Parser.getTok().isNot(AsmToken::Percent))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000560 return MatchOperand_NoMatch;
561
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000562 Register Reg;
Richard Sandiford1d959002013-07-02 14:56:45 +0000563 bool IsAddress = (Kind == ADDR32Reg || Kind == ADDR64Reg);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000564 if (parseRegister(Reg, Group, Regs, IsAddress))
565 return MatchOperand_ParseFail;
566
567 Operands.push_back(SystemZOperand::createReg(Kind, Reg.Num,
568 Reg.StartLoc, Reg.EndLoc));
569 return MatchOperand_Success;
570}
571
Richard Sandiford1d959002013-07-02 14:56:45 +0000572// Parse a memory operand into Base, Disp, Index and Length.
573// Regs maps asm register numbers to LLVM register numbers and RegKind
574// says what kind of address register we're using (ADDR32Reg or ADDR64Reg).
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000575bool SystemZAsmParser::parseAddress(unsigned &Base, const MCExpr *&Disp,
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000576 unsigned &Index, bool &IsVector,
577 const MCExpr *&Length, const unsigned *Regs,
Richard Sandiford1d959002013-07-02 14:56:45 +0000578 RegisterKind RegKind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000579 // Parse the displacement, which must always be present.
580 if (getParser().parseExpression(Disp))
581 return true;
582
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000583 // Parse the optional base and index.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000584 Index = 0;
585 Base = 0;
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000586 IsVector = false;
Craig Topper062a2ba2014-04-25 05:30:21 +0000587 Length = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000588 if (getLexer().is(AsmToken::LParen)) {
589 Parser.Lex();
590
Richard Sandiford1d959002013-07-02 14:56:45 +0000591 if (getLexer().is(AsmToken::Percent)) {
592 // Parse the first register and decide whether it's a base or an index.
593 Register Reg;
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000594 if (parseRegister(Reg))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000595 return true;
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000596 if (Reg.Group == RegV) {
597 // A vector index register. The base register is optional.
598 IsVector = true;
599 Index = SystemZMC::VR128Regs[Reg.Num];
600 } else if (Reg.Group == RegGR) {
601 if (Reg.Num == 0)
602 return Error(Reg.StartLoc, "%r0 used in an address");
603 // If the are two registers, the first one is the index and the
604 // second is the base.
605 if (getLexer().is(AsmToken::Comma))
606 Index = Regs[Reg.Num];
607 else
608 Base = Regs[Reg.Num];
609 } else
610 return Error(Reg.StartLoc, "invalid address register");
Richard Sandiford1d959002013-07-02 14:56:45 +0000611 } else {
612 // Parse the length.
613 if (getParser().parseExpression(Length))
614 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000615 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000616
617 // Check whether there's a second register. It's the base if so.
618 if (getLexer().is(AsmToken::Comma)) {
619 Parser.Lex();
620 Register Reg;
621 if (parseRegister(Reg, RegGR, Regs, RegKind))
622 return true;
623 Base = Reg.Num;
624 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000625
626 // Consume the closing bracket.
627 if (getLexer().isNot(AsmToken::RParen))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000628 return Error(Parser.getTok().getLoc(), "unexpected token in address");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000629 Parser.Lex();
630 }
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000631 return false;
632}
633
634// Parse a memory operand and add it to Operands. The other arguments
635// are as above.
636SystemZAsmParser::OperandMatchResultTy
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000637SystemZAsmParser::parseAddress(OperandVector &Operands, MemoryKind MemKind,
638 const unsigned *Regs, RegisterKind RegKind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000639 SMLoc StartLoc = Parser.getTok().getLoc();
640 unsigned Base, Index;
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000641 bool IsVector;
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000642 const MCExpr *Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +0000643 const MCExpr *Length;
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000644 if (parseAddress(Base, Disp, Index, IsVector, Length, Regs, RegKind))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000645 return MatchOperand_ParseFail;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000646
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000647 if (IsVector && MemKind != BDVMem) {
648 Error(StartLoc, "invalid use of vector addressing");
649 return MatchOperand_ParseFail;
650 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000651
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000652 if (!IsVector && MemKind == BDVMem) {
653 Error(StartLoc, "vector index required in address");
654 return MatchOperand_ParseFail;
655 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000656
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000657 if (Index && MemKind != BDXMem && MemKind != BDVMem) {
658 Error(StartLoc, "invalid use of indexed addressing");
659 return MatchOperand_ParseFail;
660 }
661
662 if (Length && MemKind != BDLMem) {
663 Error(StartLoc, "invalid use of length addressing");
664 return MatchOperand_ParseFail;
665 }
666
667 if (!Length && MemKind == BDLMem) {
668 Error(StartLoc, "missing length in address");
669 return MatchOperand_ParseFail;
670 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000671
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000672 SMLoc EndLoc =
673 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000674 Operands.push_back(SystemZOperand::createMem(MemKind, RegKind, Base, Disp,
675 Index, Length, StartLoc,
676 EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000677 return MatchOperand_Success;
678}
679
680bool SystemZAsmParser::ParseDirective(AsmToken DirectiveID) {
681 return true;
682}
683
684bool SystemZAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
685 SMLoc &EndLoc) {
686 Register Reg;
687 if (parseRegister(Reg))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000688 return true;
Richard Sandiford675f8692013-05-24 14:14:38 +0000689 if (Reg.Group == RegGR)
690 RegNo = SystemZMC::GR64Regs[Reg.Num];
691 else if (Reg.Group == RegFP)
692 RegNo = SystemZMC::FP64Regs[Reg.Num];
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000693 else if (Reg.Group == RegV)
694 RegNo = SystemZMC::VR128Regs[Reg.Num];
Richard Sandiford675f8692013-05-24 14:14:38 +0000695 else
696 // FIXME: Access registers aren't modelled as LLVM registers yet.
697 return Error(Reg.StartLoc, "invalid operand for instruction");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000698 StartLoc = Reg.StartLoc;
699 EndLoc = Reg.EndLoc;
700 return false;
701}
702
David Blaikie960ea3f2014-06-08 16:18:35 +0000703bool SystemZAsmParser::ParseInstruction(ParseInstructionInfo &Info,
704 StringRef Name, SMLoc NameLoc,
705 OperandVector &Operands) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000706 Operands.push_back(SystemZOperand::createToken(Name, NameLoc));
707
708 // Read the remaining operands.
709 if (getLexer().isNot(AsmToken::EndOfStatement)) {
710 // Read the first operand.
711 if (parseOperand(Operands, Name)) {
712 Parser.eatToEndOfStatement();
713 return true;
714 }
715
716 // Read any subsequent operands.
717 while (getLexer().is(AsmToken::Comma)) {
718 Parser.Lex();
719 if (parseOperand(Operands, Name)) {
720 Parser.eatToEndOfStatement();
721 return true;
722 }
723 }
724 if (getLexer().isNot(AsmToken::EndOfStatement)) {
725 SMLoc Loc = getLexer().getLoc();
726 Parser.eatToEndOfStatement();
727 return Error(Loc, "unexpected token in argument list");
728 }
729 }
730
731 // Consume the EndOfStatement.
732 Parser.Lex();
733 return false;
734}
735
David Blaikie960ea3f2014-06-08 16:18:35 +0000736bool SystemZAsmParser::parseOperand(OperandVector &Operands,
737 StringRef Mnemonic) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000738 // Check if the current operand has a custom associated parser, if so, try to
739 // custom parse the operand, or fallback to the general approach.
740 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
741 if (ResTy == MatchOperand_Success)
742 return false;
743
744 // If there wasn't a custom match, try the generic matcher below. Otherwise,
745 // there was a match, but an error occurred, in which case, just return that
746 // the operand parsing failed.
747 if (ResTy == MatchOperand_ParseFail)
748 return true;
749
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000750 // Check for a register. All real register operands should have used
751 // a context-dependent parse routine, which gives the required register
752 // class. The code is here to mop up other cases, like those where
753 // the instruction isn't recognized.
754 if (Parser.getTok().is(AsmToken::Percent)) {
755 Register Reg;
756 if (parseRegister(Reg))
757 return true;
758 Operands.push_back(SystemZOperand::createInvalid(Reg.StartLoc, Reg.EndLoc));
759 return false;
760 }
761
762 // The only other type of operand is an immediate or address. As above,
763 // real address operands should have used a context-dependent parse routine,
764 // so we treat any plain expression as an immediate.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000765 SMLoc StartLoc = Parser.getTok().getLoc();
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000766 unsigned Base, Index;
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000767 bool IsVector;
Richard Sandiford1d959002013-07-02 14:56:45 +0000768 const MCExpr *Expr, *Length;
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000769 if (parseAddress(Base, Expr, Index, IsVector, Length, SystemZMC::GR64Regs,
770 ADDR64Reg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000771 return true;
772
773 SMLoc EndLoc =
774 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Richard Sandiford1d959002013-07-02 14:56:45 +0000775 if (Base || Index || Length)
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000776 Operands.push_back(SystemZOperand::createInvalid(StartLoc, EndLoc));
777 else
778 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000779 return false;
780}
781
David Blaikie960ea3f2014-06-08 16:18:35 +0000782bool SystemZAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
783 OperandVector &Operands,
784 MCStreamer &Out,
Tim Northover26bb14e2014-08-18 11:49:42 +0000785 uint64_t &ErrorInfo,
David Blaikie960ea3f2014-06-08 16:18:35 +0000786 bool MatchingInlineAsm) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000787 MCInst Inst;
788 unsigned MatchResult;
789
790 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +0000791 MatchingInlineAsm);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000792 switch (MatchResult) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000793 case Match_Success:
794 Inst.setLoc(IDLoc);
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000795 Out.EmitInstruction(Inst, getSTI());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000796 return false;
797
798 case Match_MissingFeature: {
Ranjeet Singh86ecbb72015-06-30 12:32:53 +0000799 assert(ErrorInfo && "Unknown missing feature!");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000800 // Special case the error message for the very common case where only
801 // a single subtarget feature is missing
802 std::string Msg = "instruction requires:";
Ranjeet Singh86ecbb72015-06-30 12:32:53 +0000803 uint64_t Mask = 1;
804 for (unsigned I = 0; I < sizeof(ErrorInfo) * 8 - 1; ++I) {
805 if (ErrorInfo & Mask) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000806 Msg += " ";
Ranjeet Singh86ecbb72015-06-30 12:32:53 +0000807 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000808 }
Ranjeet Singh86ecbb72015-06-30 12:32:53 +0000809 Mask <<= 1;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000810 }
811 return Error(IDLoc, Msg);
812 }
813
814 case Match_InvalidOperand: {
815 SMLoc ErrorLoc = IDLoc;
Tim Northover26bb14e2014-08-18 11:49:42 +0000816 if (ErrorInfo != ~0ULL) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000817 if (ErrorInfo >= Operands.size())
818 return Error(IDLoc, "too few operands for instruction");
819
David Blaikie960ea3f2014-06-08 16:18:35 +0000820 ErrorLoc = ((SystemZOperand &)*Operands[ErrorInfo]).getStartLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000821 if (ErrorLoc == SMLoc())
822 ErrorLoc = IDLoc;
823 }
824 return Error(ErrorLoc, "invalid operand for instruction");
825 }
826
827 case Match_MnemonicFail:
828 return Error(IDLoc, "invalid instruction");
829 }
830
831 llvm_unreachable("Unexpected match type");
832}
833
David Blaikie960ea3f2014-06-08 16:18:35 +0000834SystemZAsmParser::OperandMatchResultTy
835SystemZAsmParser::parseAccessReg(OperandVector &Operands) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000836 if (Parser.getTok().isNot(AsmToken::Percent))
837 return MatchOperand_NoMatch;
838
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000839 Register Reg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000840 if (parseRegister(Reg, RegAccess, nullptr))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000841 return MatchOperand_ParseFail;
842
843 Operands.push_back(SystemZOperand::createAccessReg(Reg.Num,
844 Reg.StartLoc,
845 Reg.EndLoc));
846 return MatchOperand_Success;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000847}
848
David Blaikie960ea3f2014-06-08 16:18:35 +0000849SystemZAsmParser::OperandMatchResultTy
850SystemZAsmParser::parsePCRel(OperandVector &Operands, int64_t MinVal,
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000851 int64_t MaxVal, bool AllowTLS) {
Richard Sandiford1fb58832013-05-14 09:47:26 +0000852 MCContext &Ctx = getContext();
853 MCStreamer &Out = getStreamer();
854 const MCExpr *Expr;
855 SMLoc StartLoc = Parser.getTok().getLoc();
856 if (getParser().parseExpression(Expr))
857 return MatchOperand_NoMatch;
858
859 // For consistency with the GNU assembler, treat immediates as offsets
860 // from ".".
Richard Sandiford21f5d682014-03-06 11:22:58 +0000861 if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
Richard Sandiford1fb58832013-05-14 09:47:26 +0000862 int64_t Value = CE->getValue();
863 if ((Value & 1) || Value < MinVal || Value > MaxVal) {
864 Error(StartLoc, "offset out of range");
865 return MatchOperand_ParseFail;
866 }
Jim Grosbach6f482002015-05-18 18:43:14 +0000867 MCSymbol *Sym = Ctx.createTempSymbol();
Richard Sandiford1fb58832013-05-14 09:47:26 +0000868 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000869 const MCExpr *Base = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
Richard Sandiford1fb58832013-05-14 09:47:26 +0000870 Ctx);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000871 Expr = Value == 0 ? Base : MCBinaryExpr::createAdd(Base, Expr, Ctx);
Richard Sandiford1fb58832013-05-14 09:47:26 +0000872 }
873
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000874 // Optionally match :tls_gdcall: or :tls_ldcall: followed by a TLS symbol.
875 const MCExpr *Sym = nullptr;
876 if (AllowTLS && getLexer().is(AsmToken::Colon)) {
877 Parser.Lex();
878
879 if (Parser.getTok().isNot(AsmToken::Identifier)) {
880 Error(Parser.getTok().getLoc(), "unexpected token");
881 return MatchOperand_ParseFail;
882 }
883
884 MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
885 StringRef Name = Parser.getTok().getString();
886 if (Name == "tls_gdcall")
887 Kind = MCSymbolRefExpr::VK_TLSGD;
888 else if (Name == "tls_ldcall")
889 Kind = MCSymbolRefExpr::VK_TLSLDM;
890 else {
891 Error(Parser.getTok().getLoc(), "unknown TLS tag");
892 return MatchOperand_ParseFail;
893 }
894 Parser.Lex();
895
896 if (Parser.getTok().isNot(AsmToken::Colon)) {
897 Error(Parser.getTok().getLoc(), "unexpected token");
898 return MatchOperand_ParseFail;
899 }
900 Parser.Lex();
901
902 if (Parser.getTok().isNot(AsmToken::Identifier)) {
903 Error(Parser.getTok().getLoc(), "unexpected token");
904 return MatchOperand_ParseFail;
905 }
906
907 StringRef Identifier = Parser.getTok().getString();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000908 Sym = MCSymbolRefExpr::create(Ctx.getOrCreateSymbol(Identifier),
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000909 Kind, Ctx);
910 Parser.Lex();
911 }
912
Richard Sandiford1fb58832013-05-14 09:47:26 +0000913 SMLoc EndLoc =
914 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000915
916 if (AllowTLS)
917 Operands.push_back(SystemZOperand::createImmTLS(Expr, Sym,
918 StartLoc, EndLoc));
919 else
920 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
921
Richard Sandiford1fb58832013-05-14 09:47:26 +0000922 return MatchOperand_Success;
923}
924
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000925// Force static initialization.
926extern "C" void LLVMInitializeSystemZAsmParser() {
927 RegisterMCAsmParser<SystemZAsmParser> X(TheSystemZTarget);
928}