blob: 3caff5352117f508cd4b8398e75aaf20eb6ffd2a [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"
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +000015#include "llvm/MC/MCInstBuilder.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000016#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000017#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000018#include "llvm/MC/MCStreamer.h"
19#include "llvm/MC/MCSubtargetInfo.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000020#include "llvm/Support/TargetRegistry.h"
21
22using namespace llvm;
23
24// Return true if Expr is in the range [MinValue, MaxValue].
25static bool inRange(const MCExpr *Expr, int64_t MinValue, int64_t MaxValue) {
Richard Sandiford21f5d682014-03-06 11:22:58 +000026 if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000027 int64_t Value = CE->getValue();
28 return Value >= MinValue && Value <= MaxValue;
29 }
30 return false;
31}
32
33namespace {
Richard Sandiford1d959002013-07-02 14:56:45 +000034enum RegisterKind {
35 GR32Reg,
Richard Sandifordf9496062013-09-30 10:45:16 +000036 GRH32Reg,
Richard Sandiford1d959002013-07-02 14:56:45 +000037 GR64Reg,
38 GR128Reg,
39 ADDR32Reg,
40 ADDR64Reg,
41 FP32Reg,
42 FP64Reg,
Ulrich Weiganda8b04e12015-05-05 19:23:40 +000043 FP128Reg,
44 VR32Reg,
45 VR64Reg,
46 VR128Reg
Richard Sandiford1d959002013-07-02 14:56:45 +000047};
48
49enum MemoryKind {
50 BDMem,
51 BDXMem,
Ulrich Weiganda8b04e12015-05-05 19:23:40 +000052 BDLMem,
Ulrich Weigandec5d7792016-10-31 14:21:36 +000053 BDRMem,
Ulrich Weiganda8b04e12015-05-05 19:23:40 +000054 BDVMem
Richard Sandiford1d959002013-07-02 14:56:45 +000055};
56
Ulrich Weigand5f613df2013-05-06 16:15:19 +000057class SystemZOperand : public MCParsedAsmOperand {
58public:
Ulrich Weigand5f613df2013-05-06 16:15:19 +000059private:
60 enum OperandKind {
Richard Sandiforddc5ed712013-05-24 14:26:46 +000061 KindInvalid,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000062 KindToken,
63 KindReg,
64 KindAccessReg,
65 KindImm,
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +000066 KindImmTLS,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000067 KindMem
68 };
69
70 OperandKind Kind;
71 SMLoc StartLoc, EndLoc;
72
73 // A string of length Length, starting at Data.
74 struct TokenOp {
75 const char *Data;
76 unsigned Length;
77 };
78
Richard Sandiford675f8692013-05-24 14:14:38 +000079 // LLVM register Num, which has kind Kind. In some ways it might be
80 // easier for this class to have a register bank (general, floating-point
81 // or access) and a raw register number (0-15). This would postpone the
82 // interpretation of the operand to the add*() methods and avoid the need
83 // for context-dependent parsing. However, we do things the current way
84 // because of the virtual getReg() method, which needs to distinguish
85 // between (say) %r0 used as a single register and %r0 used as a pair.
86 // Context-dependent parsing can also give us slightly better error
87 // messages when invalid pairs like %r1 are used.
Ulrich Weigand5f613df2013-05-06 16:15:19 +000088 struct RegOp {
89 RegisterKind Kind;
90 unsigned Num;
91 };
92
93 // Base + Disp + Index, where Base and Index are LLVM registers or 0.
Ulrich Weigand1f698b02015-05-04 17:40:53 +000094 // MemKind says what type of memory this is and RegKind says what type
95 // the base register has (ADDR32Reg or ADDR64Reg). Length is the operand
96 // length for D(L,B)-style operands, otherwise it is null.
Ulrich Weigand5f613df2013-05-06 16:15:19 +000097 struct MemOp {
Ulrich Weiganda8b04e12015-05-05 19:23:40 +000098 unsigned Base : 12;
99 unsigned Index : 12;
100 unsigned MemKind : 4;
101 unsigned RegKind : 4;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000102 const MCExpr *Disp;
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000103 union {
104 const MCExpr *Imm;
105 unsigned Reg;
106 } Length;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000107 };
108
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000109 // Imm is an immediate operand, and Sym is an optional TLS symbol
110 // for use with a __tls_get_offset marker relocation.
111 struct ImmTLSOp {
112 const MCExpr *Imm;
113 const MCExpr *Sym;
114 };
115
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000116 union {
117 TokenOp Token;
118 RegOp Reg;
119 unsigned AccessReg;
120 const MCExpr *Imm;
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000121 ImmTLSOp ImmTLS;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000122 MemOp Mem;
123 };
124
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000125 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
126 // Add as immediates when possible. Null MCExpr = 0.
Craig Topper062a2ba2014-04-25 05:30:21 +0000127 if (!Expr)
Jim Grosbache9119e42015-05-13 18:37:00 +0000128 Inst.addOperand(MCOperand::createImm(0));
Richard Sandiford21f5d682014-03-06 11:22:58 +0000129 else if (auto *CE = dyn_cast<MCConstantExpr>(Expr))
Jim Grosbache9119e42015-05-13 18:37:00 +0000130 Inst.addOperand(MCOperand::createImm(CE->getValue()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000131 else
Jim Grosbache9119e42015-05-13 18:37:00 +0000132 Inst.addOperand(MCOperand::createExpr(Expr));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000133 }
134
135public:
David Blaikie960ea3f2014-06-08 16:18:35 +0000136 SystemZOperand(OperandKind kind, SMLoc startLoc, SMLoc endLoc)
137 : Kind(kind), StartLoc(startLoc), EndLoc(endLoc) {}
138
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000139 // Create particular kinds of operand.
David Blaikie960ea3f2014-06-08 16:18:35 +0000140 static std::unique_ptr<SystemZOperand> createInvalid(SMLoc StartLoc,
141 SMLoc EndLoc) {
142 return make_unique<SystemZOperand>(KindInvalid, StartLoc, EndLoc);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000143 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000144 static std::unique_ptr<SystemZOperand> createToken(StringRef Str, SMLoc Loc) {
145 auto Op = make_unique<SystemZOperand>(KindToken, Loc, Loc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000146 Op->Token.Data = Str.data();
147 Op->Token.Length = Str.size();
148 return Op;
149 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000150 static std::unique_ptr<SystemZOperand>
151 createReg(RegisterKind Kind, unsigned Num, SMLoc StartLoc, SMLoc EndLoc) {
152 auto Op = make_unique<SystemZOperand>(KindReg, StartLoc, EndLoc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000153 Op->Reg.Kind = Kind;
154 Op->Reg.Num = Num;
155 return Op;
156 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000157 static std::unique_ptr<SystemZOperand>
158 createAccessReg(unsigned Num, SMLoc StartLoc, SMLoc EndLoc) {
159 auto Op = make_unique<SystemZOperand>(KindAccessReg, StartLoc, EndLoc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000160 Op->AccessReg = Num;
161 return Op;
162 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000163 static std::unique_ptr<SystemZOperand>
164 createImm(const MCExpr *Expr, SMLoc StartLoc, SMLoc EndLoc) {
165 auto Op = make_unique<SystemZOperand>(KindImm, StartLoc, EndLoc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000166 Op->Imm = Expr;
167 return Op;
168 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000169 static std::unique_ptr<SystemZOperand>
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000170 createMem(MemoryKind MemKind, RegisterKind RegKind, unsigned Base,
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000171 const MCExpr *Disp, unsigned Index, const MCExpr *LengthImm,
172 unsigned LengthReg, SMLoc StartLoc, SMLoc EndLoc) {
David Blaikie960ea3f2014-06-08 16:18:35 +0000173 auto Op = make_unique<SystemZOperand>(KindMem, StartLoc, EndLoc);
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000174 Op->Mem.MemKind = MemKind;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000175 Op->Mem.RegKind = RegKind;
176 Op->Mem.Base = Base;
177 Op->Mem.Index = Index;
178 Op->Mem.Disp = Disp;
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000179 if (MemKind == BDLMem)
180 Op->Mem.Length.Imm = LengthImm;
181 if (MemKind == BDRMem)
182 Op->Mem.Length.Reg = LengthReg;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000183 return Op;
184 }
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000185 static std::unique_ptr<SystemZOperand>
186 createImmTLS(const MCExpr *Imm, const MCExpr *Sym,
187 SMLoc StartLoc, SMLoc EndLoc) {
188 auto Op = make_unique<SystemZOperand>(KindImmTLS, StartLoc, EndLoc);
189 Op->ImmTLS.Imm = Imm;
190 Op->ImmTLS.Sym = Sym;
191 return Op;
192 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000193
194 // Token operands
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000195 bool isToken() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000196 return Kind == KindToken;
197 }
198 StringRef getToken() const {
199 assert(Kind == KindToken && "Not a token");
200 return StringRef(Token.Data, Token.Length);
201 }
202
203 // Register operands.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000204 bool isReg() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000205 return Kind == KindReg;
206 }
207 bool isReg(RegisterKind RegKind) const {
208 return Kind == KindReg && Reg.Kind == RegKind;
209 }
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000210 unsigned getReg() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000211 assert(Kind == KindReg && "Not a register");
212 return Reg.Num;
213 }
214
215 // Access register operands. Access registers aren't exposed to LLVM
216 // as registers.
217 bool isAccessReg() const {
218 return Kind == KindAccessReg;
219 }
220
221 // Immediate operands.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000222 bool isImm() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000223 return Kind == KindImm;
224 }
225 bool isImm(int64_t MinValue, int64_t MaxValue) const {
226 return Kind == KindImm && inRange(Imm, MinValue, MaxValue);
227 }
228 const MCExpr *getImm() const {
229 assert(Kind == KindImm && "Not an immediate");
230 return Imm;
231 }
232
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000233 // Immediate operands with optional TLS symbol.
234 bool isImmTLS() const {
235 return Kind == KindImmTLS;
236 }
237
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000238 // Memory operands.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000239 bool isMem() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000240 return Kind == KindMem;
241 }
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000242 bool isMem(MemoryKind MemKind) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000243 return (Kind == KindMem &&
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000244 (Mem.MemKind == MemKind ||
245 // A BDMem can be treated as a BDXMem in which the index
246 // register field is 0.
247 (Mem.MemKind == BDMem && MemKind == BDXMem)));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000248 }
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000249 bool isMem(MemoryKind MemKind, RegisterKind RegKind) const {
250 return isMem(MemKind) && Mem.RegKind == RegKind;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000251 }
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000252 bool isMemDisp12(MemoryKind MemKind, RegisterKind RegKind) const {
253 return isMem(MemKind, RegKind) && inRange(Mem.Disp, 0, 0xfff);
254 }
255 bool isMemDisp20(MemoryKind MemKind, RegisterKind RegKind) const {
256 return isMem(MemKind, RegKind) && inRange(Mem.Disp, -524288, 524287);
Richard Sandiford1d959002013-07-02 14:56:45 +0000257 }
258 bool isMemDisp12Len8(RegisterKind RegKind) const {
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000259 return isMemDisp12(BDLMem, RegKind) && inRange(Mem.Length.Imm, 1, 0x100);
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000260 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000261
262 // Override MCParsedAsmOperand.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000263 SMLoc getStartLoc() const override { return StartLoc; }
Peter Collingbourne0da86302016-10-10 22:49:37 +0000264 SMLoc getEndLoc() const override { return EndLoc; }
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000265 void print(raw_ostream &OS) const override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000266
267 // Used by the TableGen code to add particular types of operand
268 // to an instruction.
269 void addRegOperands(MCInst &Inst, unsigned N) const {
270 assert(N == 1 && "Invalid number of operands");
Jim Grosbache9119e42015-05-13 18:37:00 +0000271 Inst.addOperand(MCOperand::createReg(getReg()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000272 }
273 void addAccessRegOperands(MCInst &Inst, unsigned N) const {
274 assert(N == 1 && "Invalid number of operands");
275 assert(Kind == KindAccessReg && "Invalid operand type");
Jim Grosbache9119e42015-05-13 18:37:00 +0000276 Inst.addOperand(MCOperand::createImm(AccessReg));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000277 }
278 void addImmOperands(MCInst &Inst, unsigned N) const {
279 assert(N == 1 && "Invalid number of operands");
280 addExpr(Inst, getImm());
281 }
282 void addBDAddrOperands(MCInst &Inst, unsigned N) const {
283 assert(N == 2 && "Invalid number of operands");
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000284 assert(isMem(BDMem) && "Invalid operand type");
Jim Grosbache9119e42015-05-13 18:37:00 +0000285 Inst.addOperand(MCOperand::createReg(Mem.Base));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000286 addExpr(Inst, Mem.Disp);
287 }
288 void addBDXAddrOperands(MCInst &Inst, unsigned N) const {
289 assert(N == 3 && "Invalid number of operands");
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000290 assert(isMem(BDXMem) && "Invalid operand type");
Jim Grosbache9119e42015-05-13 18:37:00 +0000291 Inst.addOperand(MCOperand::createReg(Mem.Base));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000292 addExpr(Inst, Mem.Disp);
Jim Grosbache9119e42015-05-13 18:37:00 +0000293 Inst.addOperand(MCOperand::createReg(Mem.Index));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000294 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000295 void addBDLAddrOperands(MCInst &Inst, unsigned N) const {
296 assert(N == 3 && "Invalid number of operands");
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000297 assert(isMem(BDLMem) && "Invalid operand type");
Jim Grosbache9119e42015-05-13 18:37:00 +0000298 Inst.addOperand(MCOperand::createReg(Mem.Base));
Richard Sandiford1d959002013-07-02 14:56:45 +0000299 addExpr(Inst, Mem.Disp);
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000300 addExpr(Inst, Mem.Length.Imm);
301 }
302 void addBDRAddrOperands(MCInst &Inst, unsigned N) const {
303 assert(N == 3 && "Invalid number of operands");
304 assert(isMem(BDRMem) && "Invalid operand type");
305 Inst.addOperand(MCOperand::createReg(Mem.Base));
306 addExpr(Inst, Mem.Disp);
307 Inst.addOperand(MCOperand::createReg(Mem.Length.Reg));
308 }
309 void addBDVAddrOperands(MCInst &Inst, unsigned N) const {
310 assert(N == 3 && "Invalid number of operands");
311 assert(isMem(BDVMem) && "Invalid operand type");
312 Inst.addOperand(MCOperand::createReg(Mem.Base));
313 addExpr(Inst, Mem.Disp);
314 Inst.addOperand(MCOperand::createReg(Mem.Index));
Richard Sandiford1d959002013-07-02 14:56:45 +0000315 }
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000316 void addImmTLSOperands(MCInst &Inst, unsigned N) const {
317 assert(N == 2 && "Invalid number of operands");
318 assert(Kind == KindImmTLS && "Invalid operand type");
319 addExpr(Inst, ImmTLS.Imm);
320 if (ImmTLS.Sym)
321 addExpr(Inst, ImmTLS.Sym);
322 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000323
324 // Used by the TableGen code to check for particular operand types.
325 bool isGR32() const { return isReg(GR32Reg); }
Richard Sandifordf9496062013-09-30 10:45:16 +0000326 bool isGRH32() const { return isReg(GRH32Reg); }
Richard Sandiford0755c932013-10-01 11:26:28 +0000327 bool isGRX32() const { return false; }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000328 bool isGR64() const { return isReg(GR64Reg); }
329 bool isGR128() const { return isReg(GR128Reg); }
330 bool isADDR32() const { return isReg(ADDR32Reg); }
331 bool isADDR64() const { return isReg(ADDR64Reg); }
332 bool isADDR128() const { return false; }
333 bool isFP32() const { return isReg(FP32Reg); }
334 bool isFP64() const { return isReg(FP64Reg); }
335 bool isFP128() const { return isReg(FP128Reg); }
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000336 bool isVR32() const { return isReg(VR32Reg); }
337 bool isVR64() const { return isReg(VR64Reg); }
338 bool isVF128() const { return false; }
339 bool isVR128() const { return isReg(VR128Reg); }
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000340 bool isAnyReg() const { return (isReg() || isImm(0, 15)); }
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000341 bool isBDAddr32Disp12() const { return isMemDisp12(BDMem, ADDR32Reg); }
342 bool isBDAddr32Disp20() const { return isMemDisp20(BDMem, ADDR32Reg); }
343 bool isBDAddr64Disp12() const { return isMemDisp12(BDMem, ADDR64Reg); }
344 bool isBDAddr64Disp20() const { return isMemDisp20(BDMem, ADDR64Reg); }
345 bool isBDXAddr64Disp12() const { return isMemDisp12(BDXMem, ADDR64Reg); }
346 bool isBDXAddr64Disp20() const { return isMemDisp20(BDXMem, ADDR64Reg); }
Richard Sandiford1d959002013-07-02 14:56:45 +0000347 bool isBDLAddr64Disp12Len8() const { return isMemDisp12Len8(ADDR64Reg); }
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000348 bool isBDRAddr64Disp12() const { return isMemDisp12(BDRMem, ADDR64Reg); }
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000349 bool isBDVAddr64Disp12() const { return isMemDisp12(BDVMem, ADDR64Reg); }
350 bool isU1Imm() const { return isImm(0, 1); }
351 bool isU2Imm() const { return isImm(0, 3); }
352 bool isU3Imm() const { return isImm(0, 7); }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000353 bool isU4Imm() const { return isImm(0, 15); }
354 bool isU6Imm() const { return isImm(0, 63); }
355 bool isU8Imm() const { return isImm(0, 255); }
356 bool isS8Imm() const { return isImm(-128, 127); }
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000357 bool isU12Imm() const { return isImm(0, 4095); }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000358 bool isU16Imm() const { return isImm(0, 65535); }
359 bool isS16Imm() const { return isImm(-32768, 32767); }
360 bool isU32Imm() const { return isImm(0, (1LL << 32) - 1); }
361 bool isS32Imm() const { return isImm(-(1LL << 31), (1LL << 31) - 1); }
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000362 bool isU48Imm() const { return isImm(0, (1LL << 48) - 1); }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000363};
364
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000365class SystemZAsmParser : public MCTargetAsmParser {
366#define GET_ASSEMBLER_HEADER
367#include "SystemZGenAsmMatcher.inc"
368
369private:
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000370 MCAsmParser &Parser;
Richard Sandiford675f8692013-05-24 14:14:38 +0000371 enum RegisterGroup {
372 RegGR,
373 RegFP,
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000374 RegV,
Richard Sandiford675f8692013-05-24 14:14:38 +0000375 RegAccess
376 };
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000377 struct Register {
Richard Sandiford675f8692013-05-24 14:14:38 +0000378 RegisterGroup Group;
379 unsigned Num;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000380 SMLoc StartLoc, EndLoc;
381 };
382
383 bool parseRegister(Register &Reg);
384
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000385 bool parseRegister(Register &Reg, RegisterGroup Group, const unsigned *Regs,
386 bool IsAddress = false);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000387
David Blaikie960ea3f2014-06-08 16:18:35 +0000388 OperandMatchResultTy parseRegister(OperandVector &Operands,
389 RegisterGroup Group, const unsigned *Regs,
390 RegisterKind Kind);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000391
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000392 OperandMatchResultTy parseAnyRegister(OperandVector &Operands);
393
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000394 bool parseAddress(bool &HaveReg1, Register &Reg1,
395 bool &HaveReg2, Register &Reg2,
396 const MCExpr *&Disp, const MCExpr *&Length);
397 bool parseAddressRegister(Register &Reg);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000398
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000399 bool ParseDirectiveInsn(SMLoc L);
400
David Blaikie960ea3f2014-06-08 16:18:35 +0000401 OperandMatchResultTy parseAddress(OperandVector &Operands,
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000402 MemoryKind MemKind, const unsigned *Regs,
403 RegisterKind RegKind);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000404
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000405 OperandMatchResultTy parsePCRel(OperandVector &Operands, int64_t MinVal,
406 int64_t MaxVal, bool AllowTLS);
407
David Blaikie960ea3f2014-06-08 16:18:35 +0000408 bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000409
410public:
Akira Hatanakab11ef082015-11-14 06:35:56 +0000411 SystemZAsmParser(const MCSubtargetInfo &sti, MCAsmParser &parser,
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000412 const MCInstrInfo &MII,
413 const MCTargetOptions &Options)
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000414 : MCTargetAsmParser(Options, sti), Parser(parser) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000415 MCAsmParserExtension::Initialize(Parser);
416
Zhan Jun Liau7d4d4362016-07-08 16:50:02 +0000417 // Alias the .word directive to .short.
418 parser.addAliasForDirective(".word", ".short");
419
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000420 // Initialize the set of available features.
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000421 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000422 }
423
424 // Override MCTargetAsmParser.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000425 bool ParseDirective(AsmToken DirectiveID) override;
426 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
David Blaikie960ea3f2014-06-08 16:18:35 +0000427 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
428 SMLoc NameLoc, OperandVector &Operands) override;
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000429 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
David Blaikie960ea3f2014-06-08 16:18:35 +0000430 OperandVector &Operands, MCStreamer &Out,
Tim Northover26bb14e2014-08-18 11:49:42 +0000431 uint64_t &ErrorInfo,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000432 bool MatchingInlineAsm) override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000433
434 // Used by the TableGen code to parse particular operand types.
David Blaikie960ea3f2014-06-08 16:18:35 +0000435 OperandMatchResultTy parseGR32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000436 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, GR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000437 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000438 OperandMatchResultTy parseGRH32(OperandVector &Operands) {
Richard Sandifordf9496062013-09-30 10:45:16 +0000439 return parseRegister(Operands, RegGR, SystemZMC::GRH32Regs, GRH32Reg);
440 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000441 OperandMatchResultTy parseGRX32(OperandVector &Operands) {
Richard Sandiford0755c932013-10-01 11:26:28 +0000442 llvm_unreachable("GRX32 should only be used for pseudo instructions");
443 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000444 OperandMatchResultTy parseGR64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000445 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, GR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000446 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000447 OperandMatchResultTy parseGR128(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000448 return parseRegister(Operands, RegGR, SystemZMC::GR128Regs, GR128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000449 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000450 OperandMatchResultTy parseADDR32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000451 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, ADDR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000452 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000453 OperandMatchResultTy parseADDR64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000454 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000455 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000456 OperandMatchResultTy parseADDR128(OperandVector &Operands) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000457 llvm_unreachable("Shouldn't be used as an operand");
458 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000459 OperandMatchResultTy parseFP32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000460 return parseRegister(Operands, RegFP, SystemZMC::FP32Regs, FP32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000461 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000462 OperandMatchResultTy parseFP64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000463 return parseRegister(Operands, RegFP, SystemZMC::FP64Regs, FP64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000464 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000465 OperandMatchResultTy parseFP128(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000466 return parseRegister(Operands, RegFP, SystemZMC::FP128Regs, FP128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000467 }
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000468 OperandMatchResultTy parseVR32(OperandVector &Operands) {
469 return parseRegister(Operands, RegV, SystemZMC::VR32Regs, VR32Reg);
470 }
471 OperandMatchResultTy parseVR64(OperandVector &Operands) {
472 return parseRegister(Operands, RegV, SystemZMC::VR64Regs, VR64Reg);
473 }
474 OperandMatchResultTy parseVF128(OperandVector &Operands) {
475 llvm_unreachable("Shouldn't be used as an operand");
476 }
477 OperandMatchResultTy parseVR128(OperandVector &Operands) {
478 return parseRegister(Operands, RegV, SystemZMC::VR128Regs, VR128Reg);
479 }
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000480 OperandMatchResultTy parseAnyReg(OperandVector &Operands) {
481 return parseAnyRegister(Operands);
482 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000483 OperandMatchResultTy parseBDAddr32(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000484 return parseAddress(Operands, BDMem, SystemZMC::GR32Regs, ADDR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000485 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000486 OperandMatchResultTy parseBDAddr64(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000487 return parseAddress(Operands, BDMem, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000488 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000489 OperandMatchResultTy parseBDXAddr64(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000490 return parseAddress(Operands, BDXMem, SystemZMC::GR64Regs, ADDR64Reg);
Richard Sandiford1d959002013-07-02 14:56:45 +0000491 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000492 OperandMatchResultTy parseBDLAddr64(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000493 return parseAddress(Operands, BDLMem, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000494 }
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000495 OperandMatchResultTy parseBDRAddr64(OperandVector &Operands) {
496 return parseAddress(Operands, BDRMem, SystemZMC::GR64Regs, ADDR64Reg);
497 }
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000498 OperandMatchResultTy parseBDVAddr64(OperandVector &Operands) {
499 return parseAddress(Operands, BDVMem, SystemZMC::GR64Regs, ADDR64Reg);
500 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000501 OperandMatchResultTy parseAccessReg(OperandVector &Operands);
David Blaikie960ea3f2014-06-08 16:18:35 +0000502 OperandMatchResultTy parsePCRel16(OperandVector &Operands) {
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000503 return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1, false);
Richard Sandiford1fb58832013-05-14 09:47:26 +0000504 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000505 OperandMatchResultTy parsePCRel32(OperandVector &Operands) {
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000506 return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1, false);
507 }
508 OperandMatchResultTy parsePCRelTLS16(OperandVector &Operands) {
509 return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1, true);
510 }
511 OperandMatchResultTy parsePCRelTLS32(OperandVector &Operands) {
512 return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1, true);
Richard Sandiford1fb58832013-05-14 09:47:26 +0000513 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000514};
Richard Sandifordc2312692014-03-06 10:38:30 +0000515} // end anonymous namespace
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000516
517#define GET_REGISTER_MATCHER
518#define GET_SUBTARGET_FEATURE_NAME
519#define GET_MATCHER_IMPLEMENTATION
520#include "SystemZGenAsmMatcher.inc"
521
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000522// Used for the .insn directives; contains information needed to parse the
523// operands in the directive.
524struct InsnMatchEntry {
525 StringRef Format;
526 uint64_t Opcode;
527 int32_t NumOperands;
528 MatchClassKind OperandKinds[5];
529};
530
531// For equal_range comparison.
532struct CompareInsn {
533 bool operator() (const InsnMatchEntry &LHS, StringRef RHS) {
534 return LHS.Format < RHS;
535 }
536 bool operator() (StringRef LHS, const InsnMatchEntry &RHS) {
537 return LHS < RHS.Format;
538 }
Roger Ferrer Ibanez17586582016-08-10 16:39:58 +0000539 bool operator() (const InsnMatchEntry &LHS, const InsnMatchEntry &RHS) {
540 return LHS.Format < RHS.Format;
541 }
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000542};
543
544// Table initializing information for parsing the .insn directive.
545static struct InsnMatchEntry InsnMatchTable[] = {
546 /* Format, Opcode, NumOperands, OperandKinds */
547 { "e", SystemZ::InsnE, 1,
548 { MCK_U16Imm } },
549 { "ri", SystemZ::InsnRI, 3,
550 { MCK_U32Imm, MCK_AnyReg, MCK_S16Imm } },
551 { "rie", SystemZ::InsnRIE, 4,
552 { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_PCRel16 } },
553 { "ril", SystemZ::InsnRIL, 3,
554 { MCK_U48Imm, MCK_AnyReg, MCK_PCRel32 } },
555 { "rilu", SystemZ::InsnRILU, 3,
556 { MCK_U48Imm, MCK_AnyReg, MCK_U32Imm } },
557 { "ris", SystemZ::InsnRIS, 5,
558 { MCK_U48Imm, MCK_AnyReg, MCK_S8Imm, MCK_U4Imm, MCK_BDAddr64Disp12 } },
559 { "rr", SystemZ::InsnRR, 3,
560 { MCK_U16Imm, MCK_AnyReg, MCK_AnyReg } },
561 { "rre", SystemZ::InsnRRE, 3,
562 { MCK_U32Imm, MCK_AnyReg, MCK_AnyReg } },
563 { "rrf", SystemZ::InsnRRF, 5,
564 { MCK_U32Imm, MCK_AnyReg, MCK_AnyReg, MCK_AnyReg, MCK_U4Imm } },
565 { "rrs", SystemZ::InsnRRS, 5,
566 { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_U4Imm, MCK_BDAddr64Disp12 } },
567 { "rs", SystemZ::InsnRS, 4,
568 { MCK_U32Imm, MCK_AnyReg, MCK_AnyReg, MCK_BDAddr64Disp12 } },
569 { "rse", SystemZ::InsnRSE, 4,
570 { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_BDAddr64Disp12 } },
571 { "rsi", SystemZ::InsnRSI, 4,
572 { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_PCRel16 } },
573 { "rsy", SystemZ::InsnRSY, 4,
574 { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_BDAddr64Disp20 } },
575 { "rx", SystemZ::InsnRX, 3,
576 { MCK_U32Imm, MCK_AnyReg, MCK_BDXAddr64Disp12 } },
577 { "rxe", SystemZ::InsnRXE, 3,
578 { MCK_U48Imm, MCK_AnyReg, MCK_BDXAddr64Disp12 } },
579 { "rxf", SystemZ::InsnRXF, 4,
580 { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_BDXAddr64Disp12 } },
581 { "rxy", SystemZ::InsnRXY, 3,
582 { MCK_U48Imm, MCK_AnyReg, MCK_BDXAddr64Disp20 } },
583 { "s", SystemZ::InsnS, 2,
584 { MCK_U32Imm, MCK_BDAddr64Disp12 } },
585 { "si", SystemZ::InsnSI, 3,
586 { MCK_U32Imm, MCK_BDAddr64Disp12, MCK_S8Imm } },
587 { "sil", SystemZ::InsnSIL, 3,
588 { MCK_U48Imm, MCK_BDAddr64Disp12, MCK_U16Imm } },
589 { "siy", SystemZ::InsnSIY, 3,
590 { MCK_U48Imm, MCK_BDAddr64Disp20, MCK_U8Imm } },
591 { "ss", SystemZ::InsnSS, 4,
592 { MCK_U48Imm, MCK_BDXAddr64Disp12, MCK_BDAddr64Disp12, MCK_AnyReg } },
593 { "sse", SystemZ::InsnSSE, 3,
594 { MCK_U48Imm, MCK_BDAddr64Disp12, MCK_BDAddr64Disp12 } },
595 { "ssf", SystemZ::InsnSSF, 4,
596 { MCK_U48Imm, MCK_BDAddr64Disp12, MCK_BDAddr64Disp12, MCK_AnyReg } }
597};
598
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000599void SystemZOperand::print(raw_ostream &OS) const {
600 llvm_unreachable("Not implemented");
601}
602
603// Parse one register of the form %<prefix><number>.
604bool SystemZAsmParser::parseRegister(Register &Reg) {
605 Reg.StartLoc = Parser.getTok().getLoc();
606
607 // Eat the % prefix.
608 if (Parser.getTok().isNot(AsmToken::Percent))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000609 return Error(Parser.getTok().getLoc(), "register expected");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000610 Parser.Lex();
611
612 // Expect a register name.
613 if (Parser.getTok().isNot(AsmToken::Identifier))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000614 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000615
Richard Sandiford675f8692013-05-24 14:14:38 +0000616 // Check that there's a prefix.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000617 StringRef Name = Parser.getTok().getString();
618 if (Name.size() < 2)
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000619 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000620 char Prefix = Name[0];
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000621
622 // Treat the rest of the register name as a register number.
Richard Sandiford675f8692013-05-24 14:14:38 +0000623 if (Name.substr(1).getAsInteger(10, Reg.Num))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000624 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000625
626 // Look for valid combinations of prefix and number.
627 if (Prefix == 'r' && Reg.Num < 16)
628 Reg.Group = RegGR;
629 else if (Prefix == 'f' && Reg.Num < 16)
630 Reg.Group = RegFP;
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000631 else if (Prefix == 'v' && Reg.Num < 32)
632 Reg.Group = RegV;
Richard Sandiford675f8692013-05-24 14:14:38 +0000633 else if (Prefix == 'a' && Reg.Num < 16)
634 Reg.Group = RegAccess;
635 else
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000636 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000637
638 Reg.EndLoc = Parser.getTok().getLoc();
639 Parser.Lex();
640 return false;
641}
642
Richard Sandiford675f8692013-05-24 14:14:38 +0000643// Parse a register of group Group. If Regs is nonnull, use it to map
Jonas Paulsson0a9049b2015-10-09 07:19:12 +0000644// the raw register number to LLVM numbering, with zero entries
645// indicating an invalid register. IsAddress says whether the
646// register appears in an address context. Allow FP Group if expecting
647// RegV Group, since the f-prefix yields the FP group even while used
648// with vector instructions.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000649bool SystemZAsmParser::parseRegister(Register &Reg, RegisterGroup Group,
650 const unsigned *Regs, bool IsAddress) {
651 if (parseRegister(Reg))
652 return true;
Jonas Paulsson0a9049b2015-10-09 07:19:12 +0000653 if (Reg.Group != Group && !(Reg.Group == RegFP && Group == RegV))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000654 return Error(Reg.StartLoc, "invalid operand for instruction");
655 if (Regs && Regs[Reg.Num] == 0)
656 return Error(Reg.StartLoc, "invalid register pair");
657 if (Reg.Num == 0 && IsAddress)
658 return Error(Reg.StartLoc, "%r0 used in an address");
Richard Sandiford675f8692013-05-24 14:14:38 +0000659 if (Regs)
660 Reg.Num = Regs[Reg.Num];
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000661 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000662}
663
Richard Sandiford675f8692013-05-24 14:14:38 +0000664// Parse a register and add it to Operands. The other arguments are as above.
Alex Bradbury58eba092016-11-01 16:32:05 +0000665OperandMatchResultTy
David Blaikie960ea3f2014-06-08 16:18:35 +0000666SystemZAsmParser::parseRegister(OperandVector &Operands, RegisterGroup Group,
667 const unsigned *Regs, RegisterKind Kind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000668 if (Parser.getTok().isNot(AsmToken::Percent))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000669 return MatchOperand_NoMatch;
670
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000671 Register Reg;
Richard Sandiford1d959002013-07-02 14:56:45 +0000672 bool IsAddress = (Kind == ADDR32Reg || Kind == ADDR64Reg);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000673 if (parseRegister(Reg, Group, Regs, IsAddress))
674 return MatchOperand_ParseFail;
675
676 Operands.push_back(SystemZOperand::createReg(Kind, Reg.Num,
677 Reg.StartLoc, Reg.EndLoc));
678 return MatchOperand_Success;
679}
680
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000681// Parse any type of register (including integers) and add it to Operands.
Alex Bradbury58eba092016-11-01 16:32:05 +0000682OperandMatchResultTy
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000683SystemZAsmParser::parseAnyRegister(OperandVector &Operands) {
684 // Handle integer values.
685 if (Parser.getTok().is(AsmToken::Integer)) {
686 const MCExpr *Register;
687 SMLoc StartLoc = Parser.getTok().getLoc();
688 if (Parser.parseExpression(Register))
689 return MatchOperand_ParseFail;
690
691 if (auto *CE = dyn_cast<MCConstantExpr>(Register)) {
692 int64_t Value = CE->getValue();
693 if (Value < 0 || Value > 15) {
694 Error(StartLoc, "invalid register");
695 return MatchOperand_ParseFail;
696 }
697 }
698
699 SMLoc EndLoc =
700 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
701
702 Operands.push_back(SystemZOperand::createImm(Register, StartLoc, EndLoc));
703 }
704 else {
705 Register Reg;
706 if (parseRegister(Reg))
707 return MatchOperand_ParseFail;
708
709 // Map to the correct register kind.
710 RegisterKind Kind;
711 unsigned RegNo;
712 if (Reg.Group == RegGR) {
713 Kind = GR64Reg;
714 RegNo = SystemZMC::GR64Regs[Reg.Num];
715 }
716 else if (Reg.Group == RegFP) {
717 Kind = FP64Reg;
718 RegNo = SystemZMC::FP64Regs[Reg.Num];
719 }
720 else if (Reg.Group == RegV) {
721 Kind = VR128Reg;
722 RegNo = SystemZMC::VR128Regs[Reg.Num];
723 }
724 else {
725 return MatchOperand_ParseFail;
726 }
727
728 Operands.push_back(SystemZOperand::createReg(Kind, RegNo,
729 Reg.StartLoc, Reg.EndLoc));
730 }
731 return MatchOperand_Success;
732}
733
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000734// Parse a memory operand into Reg1, Reg2, Disp, and Length.
735bool SystemZAsmParser::parseAddress(bool &HaveReg1, Register &Reg1,
736 bool &HaveReg2, Register &Reg2,
737 const MCExpr *&Disp,
738 const MCExpr *&Length) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000739 // Parse the displacement, which must always be present.
740 if (getParser().parseExpression(Disp))
741 return true;
742
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000743 // Parse the optional base and index.
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000744 HaveReg1 = false;
745 HaveReg2 = false;
Craig Topper062a2ba2014-04-25 05:30:21 +0000746 Length = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000747 if (getLexer().is(AsmToken::LParen)) {
748 Parser.Lex();
749
Richard Sandiford1d959002013-07-02 14:56:45 +0000750 if (getLexer().is(AsmToken::Percent)) {
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000751 // Parse the first register.
752 HaveReg1 = true;
753 if (parseRegister(Reg1))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000754 return true;
Richard Sandiford1d959002013-07-02 14:56:45 +0000755 } else {
756 // Parse the length.
757 if (getParser().parseExpression(Length))
758 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000759 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000760
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000761 // Check whether there's a second register.
Richard Sandiford1d959002013-07-02 14:56:45 +0000762 if (getLexer().is(AsmToken::Comma)) {
763 Parser.Lex();
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000764 HaveReg2 = true;
765 if (parseRegister(Reg2))
Richard Sandiford1d959002013-07-02 14:56:45 +0000766 return true;
Richard Sandiford1d959002013-07-02 14:56:45 +0000767 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000768
769 // Consume the closing bracket.
770 if (getLexer().isNot(AsmToken::RParen))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000771 return Error(Parser.getTok().getLoc(), "unexpected token in address");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000772 Parser.Lex();
773 }
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000774 return false;
775}
776
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000777// Verify that Reg is a valid address register (base or index).
778bool
779SystemZAsmParser::parseAddressRegister(Register &Reg) {
780 if (Reg.Group == RegV) {
781 Error(Reg.StartLoc, "invalid use of vector addressing");
782 return true;
783 } else if (Reg.Group != RegGR) {
784 Error(Reg.StartLoc, "invalid address register");
785 return true;
786 } else if (Reg.Num == 0) {
787 Error(Reg.StartLoc, "%r0 used in an address");
788 return true;
789 }
790 return false;
791}
792
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000793// Parse a memory operand and add it to Operands. The other arguments
794// are as above.
Alex Bradbury58eba092016-11-01 16:32:05 +0000795OperandMatchResultTy
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000796SystemZAsmParser::parseAddress(OperandVector &Operands, MemoryKind MemKind,
797 const unsigned *Regs, RegisterKind RegKind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000798 SMLoc StartLoc = Parser.getTok().getLoc();
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000799 unsigned Base = 0, Index = 0, LengthReg = 0;
800 Register Reg1, Reg2;
801 bool HaveReg1, HaveReg2;
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000802 const MCExpr *Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +0000803 const MCExpr *Length;
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000804 if (parseAddress(HaveReg1, Reg1, HaveReg2, Reg2, Disp, Length))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000805 return MatchOperand_ParseFail;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000806
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000807 switch (MemKind) {
808 case BDMem:
809 // If we have Reg1, it must be an address register.
810 if (HaveReg1) {
811 if (parseAddressRegister(Reg1))
812 return MatchOperand_ParseFail;
813 Base = Regs[Reg1.Num];
814 }
815 // There must be no Reg2 or length.
816 if (Length) {
817 Error(StartLoc, "invalid use of length addressing");
818 return MatchOperand_ParseFail;
819 }
820 if (HaveReg2) {
821 Error(StartLoc, "invalid use of indexed addressing");
822 return MatchOperand_ParseFail;
823 }
824 break;
825 case BDXMem:
826 // If we have Reg1, it must be an address register.
827 if (HaveReg1) {
828 if (parseAddressRegister(Reg1))
829 return MatchOperand_ParseFail;
830 // If the are two registers, the first one is the index and the
831 // second is the base.
832 if (HaveReg2)
833 Index = Regs[Reg1.Num];
834 else
835 Base = Regs[Reg1.Num];
836 }
837 // If we have Reg2, it must be an address register.
838 if (HaveReg2) {
839 if (parseAddressRegister(Reg2))
840 return MatchOperand_ParseFail;
841 Base = Regs[Reg2.Num];
842 }
843 // There must be no length.
844 if (Length) {
845 Error(StartLoc, "invalid use of length addressing");
846 return MatchOperand_ParseFail;
847 }
848 break;
849 case BDLMem:
850 // If we have Reg2, it must be an address register.
851 if (HaveReg2) {
852 if (parseAddressRegister(Reg2))
853 return MatchOperand_ParseFail;
854 Base = Regs[Reg2.Num];
855 }
856 // We cannot support base+index addressing.
857 if (HaveReg1 && HaveReg2) {
858 Error(StartLoc, "invalid use of indexed addressing");
859 return MatchOperand_ParseFail;
860 }
861 // We must have a length.
862 if (!Length) {
863 Error(StartLoc, "missing length in address");
864 return MatchOperand_ParseFail;
865 }
866 break;
867 case BDRMem:
868 // We must have Reg1, and it must be a GPR.
869 if (!HaveReg1 || Reg1.Group != RegGR) {
870 Error(StartLoc, "invalid operand for instruction");
871 return MatchOperand_ParseFail;
872 }
873 LengthReg = SystemZMC::GR64Regs[Reg1.Num];
874 // If we have Reg2, it must be an address register.
875 if (HaveReg2) {
876 if (parseAddressRegister(Reg2))
877 return MatchOperand_ParseFail;
878 Base = Regs[Reg2.Num];
879 }
880 // There must be no length.
881 if (Length) {
882 Error(StartLoc, "invalid use of length addressing");
883 return MatchOperand_ParseFail;
884 }
885 break;
886 case BDVMem:
887 // We must have Reg1, and it must be a vector register.
888 if (!HaveReg1 || Reg1.Group != RegV) {
889 Error(StartLoc, "vector index required in address");
890 return MatchOperand_ParseFail;
891 }
892 Index = SystemZMC::VR128Regs[Reg1.Num];
893 // If we have Reg2, it must be an address register.
894 if (HaveReg2) {
895 if (parseAddressRegister(Reg2))
896 return MatchOperand_ParseFail;
897 Base = Regs[Reg2.Num];
898 }
899 // There must be no length.
900 if (Length) {
901 Error(StartLoc, "invalid use of length addressing");
902 return MatchOperand_ParseFail;
903 }
904 break;
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000905 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000906
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000907 SMLoc EndLoc =
908 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000909 Operands.push_back(SystemZOperand::createMem(MemKind, RegKind, Base, Disp,
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000910 Index, Length, LengthReg,
911 StartLoc, EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000912 return MatchOperand_Success;
913}
914
915bool SystemZAsmParser::ParseDirective(AsmToken DirectiveID) {
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000916 StringRef IDVal = DirectiveID.getIdentifier();
917
918 if (IDVal == ".insn")
919 return ParseDirectiveInsn(DirectiveID.getLoc());
920
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000921 return true;
922}
923
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000924/// ParseDirectiveInsn
925/// ::= .insn [ format, encoding, (operands (, operands)*) ]
926bool SystemZAsmParser::ParseDirectiveInsn(SMLoc L) {
927 MCAsmParser &Parser = getParser();
928
929 // Expect instruction format as identifier.
930 StringRef Format;
931 SMLoc ErrorLoc = Parser.getTok().getLoc();
932 if (Parser.parseIdentifier(Format))
933 return Error(ErrorLoc, "expected instruction format");
934
935 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> Operands;
936
937 // Find entry for this format in InsnMatchTable.
938 auto EntryRange =
939 std::equal_range(std::begin(InsnMatchTable), std::end(InsnMatchTable),
940 Format, CompareInsn());
941
942 // If first == second, couldn't find a match in the table.
943 if (EntryRange.first == EntryRange.second)
944 return Error(ErrorLoc, "unrecognized format");
945
946 struct InsnMatchEntry *Entry = EntryRange.first;
947
948 // Format should match from equal_range.
949 assert(Entry->Format == Format);
950
951 // Parse the following operands using the table's information.
952 for (int i = 0; i < Entry->NumOperands; i++) {
953 MatchClassKind Kind = Entry->OperandKinds[i];
954
955 SMLoc StartLoc = Parser.getTok().getLoc();
956
957 // Always expect commas as separators for operands.
958 if (getLexer().isNot(AsmToken::Comma))
959 return Error(StartLoc, "unexpected token in directive");
960 Lex();
961
962 // Parse operands.
963 OperandMatchResultTy ResTy;
964 if (Kind == MCK_AnyReg)
965 ResTy = parseAnyReg(Operands);
966 else if (Kind == MCK_BDXAddr64Disp12 || Kind == MCK_BDXAddr64Disp20)
967 ResTy = parseBDXAddr64(Operands);
968 else if (Kind == MCK_BDAddr64Disp12 || Kind == MCK_BDAddr64Disp20)
969 ResTy = parseBDAddr64(Operands);
970 else if (Kind == MCK_PCRel32)
971 ResTy = parsePCRel32(Operands);
972 else if (Kind == MCK_PCRel16)
973 ResTy = parsePCRel16(Operands);
974 else {
975 // Only remaining operand kind is an immediate.
976 const MCExpr *Expr;
977 SMLoc StartLoc = Parser.getTok().getLoc();
978
979 // Expect immediate expression.
980 if (Parser.parseExpression(Expr))
981 return Error(StartLoc, "unexpected token in directive");
982
983 SMLoc EndLoc =
984 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
985
986 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
987 ResTy = MatchOperand_Success;
988 }
989
990 if (ResTy != MatchOperand_Success)
991 return true;
992 }
993
994 // Build the instruction with the parsed operands.
995 MCInst Inst = MCInstBuilder(Entry->Opcode);
996
997 for (size_t i = 0; i < Operands.size(); i++) {
998 MCParsedAsmOperand &Operand = *Operands[i];
999 MatchClassKind Kind = Entry->OperandKinds[i];
1000
1001 // Verify operand.
1002 unsigned Res = validateOperandClass(Operand, Kind);
1003 if (Res != Match_Success)
1004 return Error(Operand.getStartLoc(), "unexpected operand type");
1005
1006 // Add operands to instruction.
1007 SystemZOperand &ZOperand = static_cast<SystemZOperand &>(Operand);
1008 if (ZOperand.isReg())
1009 ZOperand.addRegOperands(Inst, 1);
1010 else if (ZOperand.isMem(BDMem))
1011 ZOperand.addBDAddrOperands(Inst, 2);
1012 else if (ZOperand.isMem(BDXMem))
1013 ZOperand.addBDXAddrOperands(Inst, 3);
1014 else if (ZOperand.isImm())
1015 ZOperand.addImmOperands(Inst, 1);
1016 else
1017 llvm_unreachable("unexpected operand type");
1018 }
1019
1020 // Emit as a regular instruction.
1021 Parser.getStreamer().EmitInstruction(Inst, getSTI());
1022
1023 return false;
1024}
1025
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001026bool SystemZAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
1027 SMLoc &EndLoc) {
1028 Register Reg;
1029 if (parseRegister(Reg))
Richard Sandiforddc5ed712013-05-24 14:26:46 +00001030 return true;
Richard Sandiford675f8692013-05-24 14:14:38 +00001031 if (Reg.Group == RegGR)
1032 RegNo = SystemZMC::GR64Regs[Reg.Num];
1033 else if (Reg.Group == RegFP)
1034 RegNo = SystemZMC::FP64Regs[Reg.Num];
Ulrich Weiganda8b04e12015-05-05 19:23:40 +00001035 else if (Reg.Group == RegV)
1036 RegNo = SystemZMC::VR128Regs[Reg.Num];
Richard Sandiford675f8692013-05-24 14:14:38 +00001037 else
1038 // FIXME: Access registers aren't modelled as LLVM registers yet.
1039 return Error(Reg.StartLoc, "invalid operand for instruction");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001040 StartLoc = Reg.StartLoc;
1041 EndLoc = Reg.EndLoc;
1042 return false;
1043}
1044
David Blaikie960ea3f2014-06-08 16:18:35 +00001045bool SystemZAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1046 StringRef Name, SMLoc NameLoc,
1047 OperandVector &Operands) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001048 Operands.push_back(SystemZOperand::createToken(Name, NameLoc));
1049
1050 // Read the remaining operands.
1051 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1052 // Read the first operand.
1053 if (parseOperand(Operands, Name)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001054 return true;
1055 }
1056
1057 // Read any subsequent operands.
1058 while (getLexer().is(AsmToken::Comma)) {
1059 Parser.Lex();
1060 if (parseOperand(Operands, Name)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001061 return true;
1062 }
1063 }
1064 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1065 SMLoc Loc = getLexer().getLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001066 return Error(Loc, "unexpected token in argument list");
1067 }
1068 }
1069
1070 // Consume the EndOfStatement.
1071 Parser.Lex();
1072 return false;
1073}
1074
David Blaikie960ea3f2014-06-08 16:18:35 +00001075bool SystemZAsmParser::parseOperand(OperandVector &Operands,
1076 StringRef Mnemonic) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001077 // Check if the current operand has a custom associated parser, if so, try to
Ulrich Weigandd9001302016-10-31 14:25:05 +00001078 // custom parse the operand, or fallback to the general approach. Force all
1079 // features to be available during the operand check, or else we will fail to
1080 // find the custom parser, and then we will later get an InvalidOperand error
1081 // instead of a MissingFeature errror.
1082 uint64_t AvailableFeatures = getAvailableFeatures();
1083 setAvailableFeatures(~(uint64_t)0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001084 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
Ulrich Weigandd9001302016-10-31 14:25:05 +00001085 setAvailableFeatures(AvailableFeatures);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001086 if (ResTy == MatchOperand_Success)
1087 return false;
1088
1089 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1090 // there was a match, but an error occurred, in which case, just return that
1091 // the operand parsing failed.
1092 if (ResTy == MatchOperand_ParseFail)
1093 return true;
1094
Richard Sandiforddc5ed712013-05-24 14:26:46 +00001095 // Check for a register. All real register operands should have used
1096 // a context-dependent parse routine, which gives the required register
1097 // class. The code is here to mop up other cases, like those where
1098 // the instruction isn't recognized.
1099 if (Parser.getTok().is(AsmToken::Percent)) {
1100 Register Reg;
1101 if (parseRegister(Reg))
1102 return true;
1103 Operands.push_back(SystemZOperand::createInvalid(Reg.StartLoc, Reg.EndLoc));
1104 return false;
1105 }
1106
1107 // The only other type of operand is an immediate or address. As above,
1108 // real address operands should have used a context-dependent parse routine,
1109 // so we treat any plain expression as an immediate.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001110 SMLoc StartLoc = Parser.getTok().getLoc();
Ulrich Weigandec5d7792016-10-31 14:21:36 +00001111 Register Reg1, Reg2;
1112 bool HaveReg1, HaveReg2;
1113 const MCExpr *Expr;
1114 const MCExpr *Length;
1115 if (parseAddress(HaveReg1, Reg1, HaveReg2, Reg2, Expr, Length))
Ulrich Weigand75d2f1b2016-11-02 11:32:28 +00001116 return true;
Ulrich Weigandec5d7792016-10-31 14:21:36 +00001117 // If the register combination is not valid for any instruction, reject it.
1118 // Otherwise, fall back to reporting an unrecognized instruction.
1119 if (HaveReg1 && Reg1.Group != RegGR && Reg1.Group != RegV
1120 && parseAddressRegister(Reg1))
Ulrich Weigand75d2f1b2016-11-02 11:32:28 +00001121 return true;
Ulrich Weigandec5d7792016-10-31 14:21:36 +00001122 if (HaveReg2 && parseAddressRegister(Reg2))
Ulrich Weigand75d2f1b2016-11-02 11:32:28 +00001123 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001124
1125 SMLoc EndLoc =
1126 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Ulrich Weigandec5d7792016-10-31 14:21:36 +00001127 if (HaveReg1 || HaveReg2 || Length)
Richard Sandiforddc5ed712013-05-24 14:26:46 +00001128 Operands.push_back(SystemZOperand::createInvalid(StartLoc, EndLoc));
1129 else
1130 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001131 return false;
1132}
1133
David Blaikie960ea3f2014-06-08 16:18:35 +00001134bool SystemZAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
1135 OperandVector &Operands,
1136 MCStreamer &Out,
Tim Northover26bb14e2014-08-18 11:49:42 +00001137 uint64_t &ErrorInfo,
David Blaikie960ea3f2014-06-08 16:18:35 +00001138 bool MatchingInlineAsm) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001139 MCInst Inst;
1140 unsigned MatchResult;
1141
1142 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001143 MatchingInlineAsm);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001144 switch (MatchResult) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001145 case Match_Success:
1146 Inst.setLoc(IDLoc);
Akira Hatanakabd9fc282015-11-14 05:20:05 +00001147 Out.EmitInstruction(Inst, getSTI());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001148 return false;
1149
1150 case Match_MissingFeature: {
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001151 assert(ErrorInfo && "Unknown missing feature!");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001152 // Special case the error message for the very common case where only
1153 // a single subtarget feature is missing
1154 std::string Msg = "instruction requires:";
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001155 uint64_t Mask = 1;
1156 for (unsigned I = 0; I < sizeof(ErrorInfo) * 8 - 1; ++I) {
1157 if (ErrorInfo & Mask) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001158 Msg += " ";
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001159 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001160 }
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001161 Mask <<= 1;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001162 }
1163 return Error(IDLoc, Msg);
1164 }
1165
1166 case Match_InvalidOperand: {
1167 SMLoc ErrorLoc = IDLoc;
Tim Northover26bb14e2014-08-18 11:49:42 +00001168 if (ErrorInfo != ~0ULL) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001169 if (ErrorInfo >= Operands.size())
1170 return Error(IDLoc, "too few operands for instruction");
1171
David Blaikie960ea3f2014-06-08 16:18:35 +00001172 ErrorLoc = ((SystemZOperand &)*Operands[ErrorInfo]).getStartLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001173 if (ErrorLoc == SMLoc())
1174 ErrorLoc = IDLoc;
1175 }
1176 return Error(ErrorLoc, "invalid operand for instruction");
1177 }
1178
1179 case Match_MnemonicFail:
1180 return Error(IDLoc, "invalid instruction");
1181 }
1182
1183 llvm_unreachable("Unexpected match type");
1184}
1185
Alex Bradbury58eba092016-11-01 16:32:05 +00001186OperandMatchResultTy
David Blaikie960ea3f2014-06-08 16:18:35 +00001187SystemZAsmParser::parseAccessReg(OperandVector &Operands) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +00001188 if (Parser.getTok().isNot(AsmToken::Percent))
1189 return MatchOperand_NoMatch;
1190
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001191 Register Reg;
Craig Topper062a2ba2014-04-25 05:30:21 +00001192 if (parseRegister(Reg, RegAccess, nullptr))
Richard Sandiforddc5ed712013-05-24 14:26:46 +00001193 return MatchOperand_ParseFail;
1194
1195 Operands.push_back(SystemZOperand::createAccessReg(Reg.Num,
1196 Reg.StartLoc,
1197 Reg.EndLoc));
1198 return MatchOperand_Success;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001199}
1200
Alex Bradbury58eba092016-11-01 16:32:05 +00001201OperandMatchResultTy
David Blaikie960ea3f2014-06-08 16:18:35 +00001202SystemZAsmParser::parsePCRel(OperandVector &Operands, int64_t MinVal,
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +00001203 int64_t MaxVal, bool AllowTLS) {
Richard Sandiford1fb58832013-05-14 09:47:26 +00001204 MCContext &Ctx = getContext();
1205 MCStreamer &Out = getStreamer();
1206 const MCExpr *Expr;
1207 SMLoc StartLoc = Parser.getTok().getLoc();
1208 if (getParser().parseExpression(Expr))
1209 return MatchOperand_NoMatch;
1210
1211 // For consistency with the GNU assembler, treat immediates as offsets
1212 // from ".".
Richard Sandiford21f5d682014-03-06 11:22:58 +00001213 if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
Richard Sandiford1fb58832013-05-14 09:47:26 +00001214 int64_t Value = CE->getValue();
1215 if ((Value & 1) || Value < MinVal || Value > MaxVal) {
1216 Error(StartLoc, "offset out of range");
1217 return MatchOperand_ParseFail;
1218 }
Jim Grosbach6f482002015-05-18 18:43:14 +00001219 MCSymbol *Sym = Ctx.createTempSymbol();
Richard Sandiford1fb58832013-05-14 09:47:26 +00001220 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001221 const MCExpr *Base = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
Richard Sandiford1fb58832013-05-14 09:47:26 +00001222 Ctx);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001223 Expr = Value == 0 ? Base : MCBinaryExpr::createAdd(Base, Expr, Ctx);
Richard Sandiford1fb58832013-05-14 09:47:26 +00001224 }
1225
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +00001226 // Optionally match :tls_gdcall: or :tls_ldcall: followed by a TLS symbol.
1227 const MCExpr *Sym = nullptr;
1228 if (AllowTLS && getLexer().is(AsmToken::Colon)) {
1229 Parser.Lex();
1230
1231 if (Parser.getTok().isNot(AsmToken::Identifier)) {
1232 Error(Parser.getTok().getLoc(), "unexpected token");
1233 return MatchOperand_ParseFail;
1234 }
1235
1236 MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
1237 StringRef Name = Parser.getTok().getString();
1238 if (Name == "tls_gdcall")
1239 Kind = MCSymbolRefExpr::VK_TLSGD;
1240 else if (Name == "tls_ldcall")
1241 Kind = MCSymbolRefExpr::VK_TLSLDM;
1242 else {
1243 Error(Parser.getTok().getLoc(), "unknown TLS tag");
1244 return MatchOperand_ParseFail;
1245 }
1246 Parser.Lex();
1247
1248 if (Parser.getTok().isNot(AsmToken::Colon)) {
1249 Error(Parser.getTok().getLoc(), "unexpected token");
1250 return MatchOperand_ParseFail;
1251 }
1252 Parser.Lex();
1253
1254 if (Parser.getTok().isNot(AsmToken::Identifier)) {
1255 Error(Parser.getTok().getLoc(), "unexpected token");
1256 return MatchOperand_ParseFail;
1257 }
1258
1259 StringRef Identifier = Parser.getTok().getString();
Jim Grosbach13760bd2015-05-30 01:25:56 +00001260 Sym = MCSymbolRefExpr::create(Ctx.getOrCreateSymbol(Identifier),
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +00001261 Kind, Ctx);
1262 Parser.Lex();
1263 }
1264
Richard Sandiford1fb58832013-05-14 09:47:26 +00001265 SMLoc EndLoc =
1266 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +00001267
1268 if (AllowTLS)
1269 Operands.push_back(SystemZOperand::createImmTLS(Expr, Sym,
1270 StartLoc, EndLoc));
1271 else
1272 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
1273
Richard Sandiford1fb58832013-05-14 09:47:26 +00001274 return MatchOperand_Success;
1275}
1276
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001277// Force static initialization.
1278extern "C" void LLVMInitializeSystemZAsmParser() {
Mehdi Aminif42454b2016-10-09 23:00:34 +00001279 RegisterMCAsmParser<SystemZAsmParser> X(getTheSystemZTarget());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001280}