blob: a94717c934563a917cb947139b3341c49e717128 [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,
Ulrich Weigandfffc7112016-11-08 20:15:26 +000046 VR128Reg,
47 AR32Reg,
Richard Sandiford1d959002013-07-02 14:56:45 +000048};
49
50enum MemoryKind {
51 BDMem,
52 BDXMem,
Ulrich Weiganda8b04e12015-05-05 19:23:40 +000053 BDLMem,
Ulrich Weigandec5d7792016-10-31 14:21:36 +000054 BDRMem,
Ulrich Weiganda8b04e12015-05-05 19:23:40 +000055 BDVMem
Richard Sandiford1d959002013-07-02 14:56:45 +000056};
57
Ulrich Weigand5f613df2013-05-06 16:15:19 +000058class SystemZOperand : public MCParsedAsmOperand {
59public:
Ulrich Weigand5f613df2013-05-06 16:15:19 +000060private:
61 enum OperandKind {
Richard Sandiforddc5ed712013-05-24 14:26:46 +000062 KindInvalid,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000063 KindToken,
64 KindReg,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000065 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;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000119 const MCExpr *Imm;
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000120 ImmTLSOp ImmTLS;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000121 MemOp Mem;
122 };
123
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000124 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
125 // Add as immediates when possible. Null MCExpr = 0.
Craig Topper062a2ba2014-04-25 05:30:21 +0000126 if (!Expr)
Jim Grosbache9119e42015-05-13 18:37:00 +0000127 Inst.addOperand(MCOperand::createImm(0));
Richard Sandiford21f5d682014-03-06 11:22:58 +0000128 else if (auto *CE = dyn_cast<MCConstantExpr>(Expr))
Jim Grosbache9119e42015-05-13 18:37:00 +0000129 Inst.addOperand(MCOperand::createImm(CE->getValue()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000130 else
Jim Grosbache9119e42015-05-13 18:37:00 +0000131 Inst.addOperand(MCOperand::createExpr(Expr));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000132 }
133
134public:
David Blaikie960ea3f2014-06-08 16:18:35 +0000135 SystemZOperand(OperandKind kind, SMLoc startLoc, SMLoc endLoc)
136 : Kind(kind), StartLoc(startLoc), EndLoc(endLoc) {}
137
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000138 // Create particular kinds of operand.
David Blaikie960ea3f2014-06-08 16:18:35 +0000139 static std::unique_ptr<SystemZOperand> createInvalid(SMLoc StartLoc,
140 SMLoc EndLoc) {
141 return make_unique<SystemZOperand>(KindInvalid, StartLoc, EndLoc);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000142 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000143 static std::unique_ptr<SystemZOperand> createToken(StringRef Str, SMLoc Loc) {
144 auto Op = make_unique<SystemZOperand>(KindToken, Loc, Loc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000145 Op->Token.Data = Str.data();
146 Op->Token.Length = Str.size();
147 return Op;
148 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000149 static std::unique_ptr<SystemZOperand>
150 createReg(RegisterKind Kind, unsigned Num, SMLoc StartLoc, SMLoc EndLoc) {
151 auto Op = make_unique<SystemZOperand>(KindReg, StartLoc, EndLoc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000152 Op->Reg.Kind = Kind;
153 Op->Reg.Num = Num;
154 return Op;
155 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000156 static std::unique_ptr<SystemZOperand>
David Blaikie960ea3f2014-06-08 16:18:35 +0000157 createImm(const MCExpr *Expr, SMLoc StartLoc, SMLoc EndLoc) {
158 auto Op = make_unique<SystemZOperand>(KindImm, StartLoc, EndLoc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000159 Op->Imm = Expr;
160 return Op;
161 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000162 static std::unique_ptr<SystemZOperand>
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000163 createMem(MemoryKind MemKind, RegisterKind RegKind, unsigned Base,
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000164 const MCExpr *Disp, unsigned Index, const MCExpr *LengthImm,
165 unsigned LengthReg, SMLoc StartLoc, SMLoc EndLoc) {
David Blaikie960ea3f2014-06-08 16:18:35 +0000166 auto Op = make_unique<SystemZOperand>(KindMem, StartLoc, EndLoc);
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000167 Op->Mem.MemKind = MemKind;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000168 Op->Mem.RegKind = RegKind;
169 Op->Mem.Base = Base;
170 Op->Mem.Index = Index;
171 Op->Mem.Disp = Disp;
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000172 if (MemKind == BDLMem)
173 Op->Mem.Length.Imm = LengthImm;
174 if (MemKind == BDRMem)
175 Op->Mem.Length.Reg = LengthReg;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000176 return Op;
177 }
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000178 static std::unique_ptr<SystemZOperand>
179 createImmTLS(const MCExpr *Imm, const MCExpr *Sym,
180 SMLoc StartLoc, SMLoc EndLoc) {
181 auto Op = make_unique<SystemZOperand>(KindImmTLS, StartLoc, EndLoc);
182 Op->ImmTLS.Imm = Imm;
183 Op->ImmTLS.Sym = Sym;
184 return Op;
185 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000186
187 // Token operands
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000188 bool isToken() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000189 return Kind == KindToken;
190 }
191 StringRef getToken() const {
192 assert(Kind == KindToken && "Not a token");
193 return StringRef(Token.Data, Token.Length);
194 }
195
196 // Register operands.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000197 bool isReg() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000198 return Kind == KindReg;
199 }
200 bool isReg(RegisterKind RegKind) const {
201 return Kind == KindReg && Reg.Kind == RegKind;
202 }
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000203 unsigned getReg() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000204 assert(Kind == KindReg && "Not a register");
205 return Reg.Num;
206 }
207
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000208 // Immediate operands.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000209 bool isImm() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000210 return Kind == KindImm;
211 }
212 bool isImm(int64_t MinValue, int64_t MaxValue) const {
213 return Kind == KindImm && inRange(Imm, MinValue, MaxValue);
214 }
215 const MCExpr *getImm() const {
216 assert(Kind == KindImm && "Not an immediate");
217 return Imm;
218 }
219
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000220 // Immediate operands with optional TLS symbol.
221 bool isImmTLS() const {
222 return Kind == KindImmTLS;
223 }
224
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000225 // Memory operands.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000226 bool isMem() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000227 return Kind == KindMem;
228 }
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000229 bool isMem(MemoryKind MemKind) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000230 return (Kind == KindMem &&
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000231 (Mem.MemKind == MemKind ||
232 // A BDMem can be treated as a BDXMem in which the index
233 // register field is 0.
234 (Mem.MemKind == BDMem && MemKind == BDXMem)));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000235 }
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000236 bool isMem(MemoryKind MemKind, RegisterKind RegKind) const {
237 return isMem(MemKind) && Mem.RegKind == RegKind;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000238 }
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000239 bool isMemDisp12(MemoryKind MemKind, RegisterKind RegKind) const {
240 return isMem(MemKind, RegKind) && inRange(Mem.Disp, 0, 0xfff);
241 }
242 bool isMemDisp20(MemoryKind MemKind, RegisterKind RegKind) const {
243 return isMem(MemKind, RegKind) && inRange(Mem.Disp, -524288, 524287);
Richard Sandiford1d959002013-07-02 14:56:45 +0000244 }
245 bool isMemDisp12Len8(RegisterKind RegKind) const {
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000246 return isMemDisp12(BDLMem, RegKind) && inRange(Mem.Length.Imm, 1, 0x100);
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000247 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000248
249 // Override MCParsedAsmOperand.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000250 SMLoc getStartLoc() const override { return StartLoc; }
Peter Collingbourne0da86302016-10-10 22:49:37 +0000251 SMLoc getEndLoc() const override { return EndLoc; }
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000252 void print(raw_ostream &OS) const override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000253
254 // Used by the TableGen code to add particular types of operand
255 // to an instruction.
256 void addRegOperands(MCInst &Inst, unsigned N) const {
257 assert(N == 1 && "Invalid number of operands");
Jim Grosbache9119e42015-05-13 18:37:00 +0000258 Inst.addOperand(MCOperand::createReg(getReg()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000259 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000260 void addImmOperands(MCInst &Inst, unsigned N) const {
261 assert(N == 1 && "Invalid number of operands");
262 addExpr(Inst, getImm());
263 }
264 void addBDAddrOperands(MCInst &Inst, unsigned N) const {
265 assert(N == 2 && "Invalid number of operands");
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000266 assert(isMem(BDMem) && "Invalid operand type");
Jim Grosbache9119e42015-05-13 18:37:00 +0000267 Inst.addOperand(MCOperand::createReg(Mem.Base));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000268 addExpr(Inst, Mem.Disp);
269 }
270 void addBDXAddrOperands(MCInst &Inst, unsigned N) const {
271 assert(N == 3 && "Invalid number of operands");
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000272 assert(isMem(BDXMem) && "Invalid operand type");
Jim Grosbache9119e42015-05-13 18:37:00 +0000273 Inst.addOperand(MCOperand::createReg(Mem.Base));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000274 addExpr(Inst, Mem.Disp);
Jim Grosbache9119e42015-05-13 18:37:00 +0000275 Inst.addOperand(MCOperand::createReg(Mem.Index));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000276 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000277 void addBDLAddrOperands(MCInst &Inst, unsigned N) const {
278 assert(N == 3 && "Invalid number of operands");
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000279 assert(isMem(BDLMem) && "Invalid operand type");
Jim Grosbache9119e42015-05-13 18:37:00 +0000280 Inst.addOperand(MCOperand::createReg(Mem.Base));
Richard Sandiford1d959002013-07-02 14:56:45 +0000281 addExpr(Inst, Mem.Disp);
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000282 addExpr(Inst, Mem.Length.Imm);
283 }
284 void addBDRAddrOperands(MCInst &Inst, unsigned N) const {
285 assert(N == 3 && "Invalid number of operands");
286 assert(isMem(BDRMem) && "Invalid operand type");
287 Inst.addOperand(MCOperand::createReg(Mem.Base));
288 addExpr(Inst, Mem.Disp);
289 Inst.addOperand(MCOperand::createReg(Mem.Length.Reg));
290 }
291 void addBDVAddrOperands(MCInst &Inst, unsigned N) const {
292 assert(N == 3 && "Invalid number of operands");
293 assert(isMem(BDVMem) && "Invalid operand type");
294 Inst.addOperand(MCOperand::createReg(Mem.Base));
295 addExpr(Inst, Mem.Disp);
296 Inst.addOperand(MCOperand::createReg(Mem.Index));
Richard Sandiford1d959002013-07-02 14:56:45 +0000297 }
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000298 void addImmTLSOperands(MCInst &Inst, unsigned N) const {
299 assert(N == 2 && "Invalid number of operands");
300 assert(Kind == KindImmTLS && "Invalid operand type");
301 addExpr(Inst, ImmTLS.Imm);
302 if (ImmTLS.Sym)
303 addExpr(Inst, ImmTLS.Sym);
304 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000305
306 // Used by the TableGen code to check for particular operand types.
307 bool isGR32() const { return isReg(GR32Reg); }
Richard Sandifordf9496062013-09-30 10:45:16 +0000308 bool isGRH32() const { return isReg(GRH32Reg); }
Richard Sandiford0755c932013-10-01 11:26:28 +0000309 bool isGRX32() const { return false; }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000310 bool isGR64() const { return isReg(GR64Reg); }
311 bool isGR128() const { return isReg(GR128Reg); }
312 bool isADDR32() const { return isReg(ADDR32Reg); }
313 bool isADDR64() const { return isReg(ADDR64Reg); }
314 bool isADDR128() const { return false; }
315 bool isFP32() const { return isReg(FP32Reg); }
316 bool isFP64() const { return isReg(FP64Reg); }
317 bool isFP128() const { return isReg(FP128Reg); }
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000318 bool isVR32() const { return isReg(VR32Reg); }
319 bool isVR64() const { return isReg(VR64Reg); }
320 bool isVF128() const { return false; }
321 bool isVR128() const { return isReg(VR128Reg); }
Ulrich Weigandfffc7112016-11-08 20:15:26 +0000322 bool isAR32() const { return isReg(AR32Reg); }
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000323 bool isAnyReg() const { return (isReg() || isImm(0, 15)); }
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000324 bool isBDAddr32Disp12() const { return isMemDisp12(BDMem, ADDR32Reg); }
325 bool isBDAddr32Disp20() const { return isMemDisp20(BDMem, ADDR32Reg); }
326 bool isBDAddr64Disp12() const { return isMemDisp12(BDMem, ADDR64Reg); }
327 bool isBDAddr64Disp20() const { return isMemDisp20(BDMem, ADDR64Reg); }
328 bool isBDXAddr64Disp12() const { return isMemDisp12(BDXMem, ADDR64Reg); }
329 bool isBDXAddr64Disp20() const { return isMemDisp20(BDXMem, ADDR64Reg); }
Richard Sandiford1d959002013-07-02 14:56:45 +0000330 bool isBDLAddr64Disp12Len8() const { return isMemDisp12Len8(ADDR64Reg); }
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000331 bool isBDRAddr64Disp12() const { return isMemDisp12(BDRMem, 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); }
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000345 bool isU48Imm() const { return isImm(0, (1LL << 48) - 1); }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000346};
347
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000348class SystemZAsmParser : public MCTargetAsmParser {
349#define GET_ASSEMBLER_HEADER
350#include "SystemZGenAsmMatcher.inc"
351
352private:
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000353 MCAsmParser &Parser;
Richard Sandiford675f8692013-05-24 14:14:38 +0000354 enum RegisterGroup {
355 RegGR,
356 RegFP,
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000357 RegV,
Ulrich Weigandfffc7112016-11-08 20:15:26 +0000358 RegAR
Richard Sandiford675f8692013-05-24 14:14:38 +0000359 };
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000360 struct Register {
Richard Sandiford675f8692013-05-24 14:14:38 +0000361 RegisterGroup Group;
362 unsigned Num;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000363 SMLoc StartLoc, EndLoc;
364 };
365
366 bool parseRegister(Register &Reg);
367
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000368 bool parseRegister(Register &Reg, RegisterGroup Group, const unsigned *Regs,
369 bool IsAddress = false);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000370
David Blaikie960ea3f2014-06-08 16:18:35 +0000371 OperandMatchResultTy parseRegister(OperandVector &Operands,
372 RegisterGroup Group, const unsigned *Regs,
373 RegisterKind Kind);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000374
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000375 OperandMatchResultTy parseAnyRegister(OperandVector &Operands);
376
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000377 bool parseAddress(bool &HaveReg1, Register &Reg1,
378 bool &HaveReg2, Register &Reg2,
379 const MCExpr *&Disp, const MCExpr *&Length);
380 bool parseAddressRegister(Register &Reg);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000381
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000382 bool ParseDirectiveInsn(SMLoc L);
383
David Blaikie960ea3f2014-06-08 16:18:35 +0000384 OperandMatchResultTy parseAddress(OperandVector &Operands,
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000385 MemoryKind MemKind, const unsigned *Regs,
386 RegisterKind RegKind);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000387
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000388 OperandMatchResultTy parsePCRel(OperandVector &Operands, int64_t MinVal,
389 int64_t MaxVal, bool AllowTLS);
390
David Blaikie960ea3f2014-06-08 16:18:35 +0000391 bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000392
393public:
Akira Hatanakab11ef082015-11-14 06:35:56 +0000394 SystemZAsmParser(const MCSubtargetInfo &sti, MCAsmParser &parser,
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000395 const MCInstrInfo &MII,
396 const MCTargetOptions &Options)
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000397 : MCTargetAsmParser(Options, sti), Parser(parser) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000398 MCAsmParserExtension::Initialize(Parser);
399
Zhan Jun Liau7d4d4362016-07-08 16:50:02 +0000400 // Alias the .word directive to .short.
401 parser.addAliasForDirective(".word", ".short");
402
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000403 // Initialize the set of available features.
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000404 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000405 }
406
407 // Override MCTargetAsmParser.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000408 bool ParseDirective(AsmToken DirectiveID) override;
409 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
David Blaikie960ea3f2014-06-08 16:18:35 +0000410 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
411 SMLoc NameLoc, OperandVector &Operands) override;
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000412 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
David Blaikie960ea3f2014-06-08 16:18:35 +0000413 OperandVector &Operands, MCStreamer &Out,
Tim Northover26bb14e2014-08-18 11:49:42 +0000414 uint64_t &ErrorInfo,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000415 bool MatchingInlineAsm) override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000416
417 // Used by the TableGen code to parse particular operand types.
David Blaikie960ea3f2014-06-08 16:18:35 +0000418 OperandMatchResultTy parseGR32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000419 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, GR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000420 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000421 OperandMatchResultTy parseGRH32(OperandVector &Operands) {
Richard Sandifordf9496062013-09-30 10:45:16 +0000422 return parseRegister(Operands, RegGR, SystemZMC::GRH32Regs, GRH32Reg);
423 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000424 OperandMatchResultTy parseGRX32(OperandVector &Operands) {
Richard Sandiford0755c932013-10-01 11:26:28 +0000425 llvm_unreachable("GRX32 should only be used for pseudo instructions");
426 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000427 OperandMatchResultTy parseGR64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000428 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, GR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000429 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000430 OperandMatchResultTy parseGR128(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000431 return parseRegister(Operands, RegGR, SystemZMC::GR128Regs, GR128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000432 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000433 OperandMatchResultTy parseADDR32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000434 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, ADDR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000435 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000436 OperandMatchResultTy parseADDR64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000437 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000438 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000439 OperandMatchResultTy parseADDR128(OperandVector &Operands) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000440 llvm_unreachable("Shouldn't be used as an operand");
441 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000442 OperandMatchResultTy parseFP32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000443 return parseRegister(Operands, RegFP, SystemZMC::FP32Regs, FP32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000444 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000445 OperandMatchResultTy parseFP64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000446 return parseRegister(Operands, RegFP, SystemZMC::FP64Regs, FP64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000447 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000448 OperandMatchResultTy parseFP128(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000449 return parseRegister(Operands, RegFP, SystemZMC::FP128Regs, FP128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000450 }
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000451 OperandMatchResultTy parseVR32(OperandVector &Operands) {
452 return parseRegister(Operands, RegV, SystemZMC::VR32Regs, VR32Reg);
453 }
454 OperandMatchResultTy parseVR64(OperandVector &Operands) {
455 return parseRegister(Operands, RegV, SystemZMC::VR64Regs, VR64Reg);
456 }
457 OperandMatchResultTy parseVF128(OperandVector &Operands) {
458 llvm_unreachable("Shouldn't be used as an operand");
459 }
460 OperandMatchResultTy parseVR128(OperandVector &Operands) {
461 return parseRegister(Operands, RegV, SystemZMC::VR128Regs, VR128Reg);
462 }
Ulrich Weigandfffc7112016-11-08 20:15:26 +0000463 OperandMatchResultTy parseAR32(OperandVector &Operands) {
464 return parseRegister(Operands, RegAR, SystemZMC::AR32Regs, AR32Reg);
465 }
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000466 OperandMatchResultTy parseAnyReg(OperandVector &Operands) {
467 return parseAnyRegister(Operands);
468 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000469 OperandMatchResultTy parseBDAddr32(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000470 return parseAddress(Operands, BDMem, SystemZMC::GR32Regs, ADDR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000471 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000472 OperandMatchResultTy parseBDAddr64(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000473 return parseAddress(Operands, BDMem, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000474 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000475 OperandMatchResultTy parseBDXAddr64(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000476 return parseAddress(Operands, BDXMem, SystemZMC::GR64Regs, ADDR64Reg);
Richard Sandiford1d959002013-07-02 14:56:45 +0000477 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000478 OperandMatchResultTy parseBDLAddr64(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000479 return parseAddress(Operands, BDLMem, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000480 }
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000481 OperandMatchResultTy parseBDRAddr64(OperandVector &Operands) {
482 return parseAddress(Operands, BDRMem, SystemZMC::GR64Regs, ADDR64Reg);
483 }
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000484 OperandMatchResultTy parseBDVAddr64(OperandVector &Operands) {
485 return parseAddress(Operands, BDVMem, SystemZMC::GR64Regs, ADDR64Reg);
486 }
Ulrich Weigand84404f32016-11-28 14:01:51 +0000487 OperandMatchResultTy parsePCRel12(OperandVector &Operands) {
488 return parsePCRel(Operands, -(1LL << 12), (1LL << 12) - 1, false);
489 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000490 OperandMatchResultTy parsePCRel16(OperandVector &Operands) {
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000491 return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1, false);
Richard Sandiford1fb58832013-05-14 09:47:26 +0000492 }
Ulrich Weigand84404f32016-11-28 14:01:51 +0000493 OperandMatchResultTy parsePCRel24(OperandVector &Operands) {
494 return parsePCRel(Operands, -(1LL << 24), (1LL << 24) - 1, false);
495 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000496 OperandMatchResultTy parsePCRel32(OperandVector &Operands) {
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000497 return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1, false);
498 }
499 OperandMatchResultTy parsePCRelTLS16(OperandVector &Operands) {
500 return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1, true);
501 }
502 OperandMatchResultTy parsePCRelTLS32(OperandVector &Operands) {
503 return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1, true);
Richard Sandiford1fb58832013-05-14 09:47:26 +0000504 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000505};
Richard Sandifordc2312692014-03-06 10:38:30 +0000506} // end anonymous namespace
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000507
508#define GET_REGISTER_MATCHER
509#define GET_SUBTARGET_FEATURE_NAME
510#define GET_MATCHER_IMPLEMENTATION
511#include "SystemZGenAsmMatcher.inc"
512
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000513// Used for the .insn directives; contains information needed to parse the
514// operands in the directive.
515struct InsnMatchEntry {
516 StringRef Format;
517 uint64_t Opcode;
518 int32_t NumOperands;
519 MatchClassKind OperandKinds[5];
520};
521
522// For equal_range comparison.
523struct CompareInsn {
524 bool operator() (const InsnMatchEntry &LHS, StringRef RHS) {
525 return LHS.Format < RHS;
526 }
527 bool operator() (StringRef LHS, const InsnMatchEntry &RHS) {
528 return LHS < RHS.Format;
529 }
Roger Ferrer Ibanez17586582016-08-10 16:39:58 +0000530 bool operator() (const InsnMatchEntry &LHS, const InsnMatchEntry &RHS) {
531 return LHS.Format < RHS.Format;
532 }
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000533};
534
535// Table initializing information for parsing the .insn directive.
536static struct InsnMatchEntry InsnMatchTable[] = {
537 /* Format, Opcode, NumOperands, OperandKinds */
538 { "e", SystemZ::InsnE, 1,
539 { MCK_U16Imm } },
540 { "ri", SystemZ::InsnRI, 3,
541 { MCK_U32Imm, MCK_AnyReg, MCK_S16Imm } },
542 { "rie", SystemZ::InsnRIE, 4,
543 { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_PCRel16 } },
544 { "ril", SystemZ::InsnRIL, 3,
545 { MCK_U48Imm, MCK_AnyReg, MCK_PCRel32 } },
546 { "rilu", SystemZ::InsnRILU, 3,
547 { MCK_U48Imm, MCK_AnyReg, MCK_U32Imm } },
548 { "ris", SystemZ::InsnRIS, 5,
549 { MCK_U48Imm, MCK_AnyReg, MCK_S8Imm, MCK_U4Imm, MCK_BDAddr64Disp12 } },
550 { "rr", SystemZ::InsnRR, 3,
551 { MCK_U16Imm, MCK_AnyReg, MCK_AnyReg } },
552 { "rre", SystemZ::InsnRRE, 3,
553 { MCK_U32Imm, MCK_AnyReg, MCK_AnyReg } },
554 { "rrf", SystemZ::InsnRRF, 5,
555 { MCK_U32Imm, MCK_AnyReg, MCK_AnyReg, MCK_AnyReg, MCK_U4Imm } },
556 { "rrs", SystemZ::InsnRRS, 5,
557 { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_U4Imm, MCK_BDAddr64Disp12 } },
558 { "rs", SystemZ::InsnRS, 4,
559 { MCK_U32Imm, MCK_AnyReg, MCK_AnyReg, MCK_BDAddr64Disp12 } },
560 { "rse", SystemZ::InsnRSE, 4,
561 { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_BDAddr64Disp12 } },
562 { "rsi", SystemZ::InsnRSI, 4,
563 { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_PCRel16 } },
564 { "rsy", SystemZ::InsnRSY, 4,
565 { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_BDAddr64Disp20 } },
566 { "rx", SystemZ::InsnRX, 3,
567 { MCK_U32Imm, MCK_AnyReg, MCK_BDXAddr64Disp12 } },
568 { "rxe", SystemZ::InsnRXE, 3,
569 { MCK_U48Imm, MCK_AnyReg, MCK_BDXAddr64Disp12 } },
570 { "rxf", SystemZ::InsnRXF, 4,
571 { MCK_U48Imm, MCK_AnyReg, MCK_AnyReg, MCK_BDXAddr64Disp12 } },
572 { "rxy", SystemZ::InsnRXY, 3,
573 { MCK_U48Imm, MCK_AnyReg, MCK_BDXAddr64Disp20 } },
574 { "s", SystemZ::InsnS, 2,
575 { MCK_U32Imm, MCK_BDAddr64Disp12 } },
576 { "si", SystemZ::InsnSI, 3,
577 { MCK_U32Imm, MCK_BDAddr64Disp12, MCK_S8Imm } },
578 { "sil", SystemZ::InsnSIL, 3,
579 { MCK_U48Imm, MCK_BDAddr64Disp12, MCK_U16Imm } },
580 { "siy", SystemZ::InsnSIY, 3,
581 { MCK_U48Imm, MCK_BDAddr64Disp20, MCK_U8Imm } },
582 { "ss", SystemZ::InsnSS, 4,
583 { MCK_U48Imm, MCK_BDXAddr64Disp12, MCK_BDAddr64Disp12, MCK_AnyReg } },
584 { "sse", SystemZ::InsnSSE, 3,
585 { MCK_U48Imm, MCK_BDAddr64Disp12, MCK_BDAddr64Disp12 } },
586 { "ssf", SystemZ::InsnSSF, 4,
587 { MCK_U48Imm, MCK_BDAddr64Disp12, MCK_BDAddr64Disp12, MCK_AnyReg } }
588};
589
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000590void SystemZOperand::print(raw_ostream &OS) const {
591 llvm_unreachable("Not implemented");
592}
593
594// Parse one register of the form %<prefix><number>.
595bool SystemZAsmParser::parseRegister(Register &Reg) {
596 Reg.StartLoc = Parser.getTok().getLoc();
597
598 // Eat the % prefix.
599 if (Parser.getTok().isNot(AsmToken::Percent))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000600 return Error(Parser.getTok().getLoc(), "register expected");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000601 Parser.Lex();
602
603 // Expect a register name.
604 if (Parser.getTok().isNot(AsmToken::Identifier))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000605 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000606
Richard Sandiford675f8692013-05-24 14:14:38 +0000607 // Check that there's a prefix.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000608 StringRef Name = Parser.getTok().getString();
609 if (Name.size() < 2)
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000610 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000611 char Prefix = Name[0];
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000612
613 // Treat the rest of the register name as a register number.
Richard Sandiford675f8692013-05-24 14:14:38 +0000614 if (Name.substr(1).getAsInteger(10, Reg.Num))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000615 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000616
617 // Look for valid combinations of prefix and number.
618 if (Prefix == 'r' && Reg.Num < 16)
619 Reg.Group = RegGR;
620 else if (Prefix == 'f' && Reg.Num < 16)
621 Reg.Group = RegFP;
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000622 else if (Prefix == 'v' && Reg.Num < 32)
623 Reg.Group = RegV;
Richard Sandiford675f8692013-05-24 14:14:38 +0000624 else if (Prefix == 'a' && Reg.Num < 16)
Ulrich Weigandfffc7112016-11-08 20:15:26 +0000625 Reg.Group = RegAR;
Richard Sandiford675f8692013-05-24 14:14:38 +0000626 else
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000627 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000628
629 Reg.EndLoc = Parser.getTok().getLoc();
630 Parser.Lex();
631 return false;
632}
633
Richard Sandiford675f8692013-05-24 14:14:38 +0000634// Parse a register of group Group. If Regs is nonnull, use it to map
Jonas Paulsson0a9049b2015-10-09 07:19:12 +0000635// the raw register number to LLVM numbering, with zero entries
636// indicating an invalid register. IsAddress says whether the
637// register appears in an address context. Allow FP Group if expecting
638// RegV Group, since the f-prefix yields the FP group even while used
639// with vector instructions.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000640bool SystemZAsmParser::parseRegister(Register &Reg, RegisterGroup Group,
641 const unsigned *Regs, bool IsAddress) {
642 if (parseRegister(Reg))
643 return true;
Jonas Paulsson0a9049b2015-10-09 07:19:12 +0000644 if (Reg.Group != Group && !(Reg.Group == RegFP && Group == RegV))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000645 return Error(Reg.StartLoc, "invalid operand for instruction");
646 if (Regs && Regs[Reg.Num] == 0)
647 return Error(Reg.StartLoc, "invalid register pair");
648 if (Reg.Num == 0 && IsAddress)
649 return Error(Reg.StartLoc, "%r0 used in an address");
Richard Sandiford675f8692013-05-24 14:14:38 +0000650 if (Regs)
651 Reg.Num = Regs[Reg.Num];
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000652 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000653}
654
Richard Sandiford675f8692013-05-24 14:14:38 +0000655// Parse a register and add it to Operands. The other arguments are as above.
Alex Bradbury58eba092016-11-01 16:32:05 +0000656OperandMatchResultTy
David Blaikie960ea3f2014-06-08 16:18:35 +0000657SystemZAsmParser::parseRegister(OperandVector &Operands, RegisterGroup Group,
658 const unsigned *Regs, RegisterKind Kind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000659 if (Parser.getTok().isNot(AsmToken::Percent))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000660 return MatchOperand_NoMatch;
661
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000662 Register Reg;
Richard Sandiford1d959002013-07-02 14:56:45 +0000663 bool IsAddress = (Kind == ADDR32Reg || Kind == ADDR64Reg);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000664 if (parseRegister(Reg, Group, Regs, IsAddress))
665 return MatchOperand_ParseFail;
666
667 Operands.push_back(SystemZOperand::createReg(Kind, Reg.Num,
668 Reg.StartLoc, Reg.EndLoc));
669 return MatchOperand_Success;
670}
671
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000672// Parse any type of register (including integers) and add it to Operands.
Alex Bradbury58eba092016-11-01 16:32:05 +0000673OperandMatchResultTy
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000674SystemZAsmParser::parseAnyRegister(OperandVector &Operands) {
675 // Handle integer values.
676 if (Parser.getTok().is(AsmToken::Integer)) {
677 const MCExpr *Register;
678 SMLoc StartLoc = Parser.getTok().getLoc();
679 if (Parser.parseExpression(Register))
680 return MatchOperand_ParseFail;
681
682 if (auto *CE = dyn_cast<MCConstantExpr>(Register)) {
683 int64_t Value = CE->getValue();
684 if (Value < 0 || Value > 15) {
685 Error(StartLoc, "invalid register");
686 return MatchOperand_ParseFail;
687 }
688 }
689
690 SMLoc EndLoc =
691 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
692
693 Operands.push_back(SystemZOperand::createImm(Register, StartLoc, EndLoc));
694 }
695 else {
696 Register Reg;
697 if (parseRegister(Reg))
698 return MatchOperand_ParseFail;
699
700 // Map to the correct register kind.
701 RegisterKind Kind;
702 unsigned RegNo;
703 if (Reg.Group == RegGR) {
704 Kind = GR64Reg;
705 RegNo = SystemZMC::GR64Regs[Reg.Num];
706 }
707 else if (Reg.Group == RegFP) {
708 Kind = FP64Reg;
709 RegNo = SystemZMC::FP64Regs[Reg.Num];
710 }
711 else if (Reg.Group == RegV) {
712 Kind = VR128Reg;
713 RegNo = SystemZMC::VR128Regs[Reg.Num];
714 }
Ulrich Weigandfffc7112016-11-08 20:15:26 +0000715 else if (Reg.Group == RegAR) {
716 Kind = AR32Reg;
717 RegNo = SystemZMC::AR32Regs[Reg.Num];
718 }
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000719 else {
720 return MatchOperand_ParseFail;
721 }
722
723 Operands.push_back(SystemZOperand::createReg(Kind, RegNo,
724 Reg.StartLoc, Reg.EndLoc));
725 }
726 return MatchOperand_Success;
727}
728
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000729// Parse a memory operand into Reg1, Reg2, Disp, and Length.
730bool SystemZAsmParser::parseAddress(bool &HaveReg1, Register &Reg1,
731 bool &HaveReg2, Register &Reg2,
732 const MCExpr *&Disp,
733 const MCExpr *&Length) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000734 // Parse the displacement, which must always be present.
735 if (getParser().parseExpression(Disp))
736 return true;
737
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000738 // Parse the optional base and index.
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000739 HaveReg1 = false;
740 HaveReg2 = false;
Craig Topper062a2ba2014-04-25 05:30:21 +0000741 Length = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000742 if (getLexer().is(AsmToken::LParen)) {
743 Parser.Lex();
744
Richard Sandiford1d959002013-07-02 14:56:45 +0000745 if (getLexer().is(AsmToken::Percent)) {
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000746 // Parse the first register.
747 HaveReg1 = true;
748 if (parseRegister(Reg1))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000749 return true;
Richard Sandiford1d959002013-07-02 14:56:45 +0000750 } else {
751 // Parse the length.
752 if (getParser().parseExpression(Length))
753 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000754 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000755
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000756 // Check whether there's a second register.
Richard Sandiford1d959002013-07-02 14:56:45 +0000757 if (getLexer().is(AsmToken::Comma)) {
758 Parser.Lex();
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000759 HaveReg2 = true;
760 if (parseRegister(Reg2))
Richard Sandiford1d959002013-07-02 14:56:45 +0000761 return true;
Richard Sandiford1d959002013-07-02 14:56:45 +0000762 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000763
764 // Consume the closing bracket.
765 if (getLexer().isNot(AsmToken::RParen))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000766 return Error(Parser.getTok().getLoc(), "unexpected token in address");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000767 Parser.Lex();
768 }
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000769 return false;
770}
771
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000772// Verify that Reg is a valid address register (base or index).
773bool
774SystemZAsmParser::parseAddressRegister(Register &Reg) {
775 if (Reg.Group == RegV) {
776 Error(Reg.StartLoc, "invalid use of vector addressing");
777 return true;
778 } else if (Reg.Group != RegGR) {
779 Error(Reg.StartLoc, "invalid address register");
780 return true;
781 } else if (Reg.Num == 0) {
782 Error(Reg.StartLoc, "%r0 used in an address");
783 return true;
784 }
785 return false;
786}
787
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000788// Parse a memory operand and add it to Operands. The other arguments
789// are as above.
Alex Bradbury58eba092016-11-01 16:32:05 +0000790OperandMatchResultTy
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000791SystemZAsmParser::parseAddress(OperandVector &Operands, MemoryKind MemKind,
792 const unsigned *Regs, RegisterKind RegKind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000793 SMLoc StartLoc = Parser.getTok().getLoc();
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000794 unsigned Base = 0, Index = 0, LengthReg = 0;
795 Register Reg1, Reg2;
796 bool HaveReg1, HaveReg2;
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000797 const MCExpr *Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +0000798 const MCExpr *Length;
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000799 if (parseAddress(HaveReg1, Reg1, HaveReg2, Reg2, Disp, Length))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000800 return MatchOperand_ParseFail;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000801
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000802 switch (MemKind) {
803 case BDMem:
804 // If we have Reg1, it must be an address register.
805 if (HaveReg1) {
806 if (parseAddressRegister(Reg1))
807 return MatchOperand_ParseFail;
808 Base = Regs[Reg1.Num];
809 }
810 // There must be no Reg2 or length.
811 if (Length) {
812 Error(StartLoc, "invalid use of length addressing");
813 return MatchOperand_ParseFail;
814 }
815 if (HaveReg2) {
816 Error(StartLoc, "invalid use of indexed addressing");
817 return MatchOperand_ParseFail;
818 }
819 break;
820 case BDXMem:
821 // If we have Reg1, it must be an address register.
822 if (HaveReg1) {
823 if (parseAddressRegister(Reg1))
824 return MatchOperand_ParseFail;
825 // If the are two registers, the first one is the index and the
826 // second is the base.
827 if (HaveReg2)
828 Index = Regs[Reg1.Num];
829 else
830 Base = Regs[Reg1.Num];
831 }
832 // If we have Reg2, it must be an address register.
833 if (HaveReg2) {
834 if (parseAddressRegister(Reg2))
835 return MatchOperand_ParseFail;
836 Base = Regs[Reg2.Num];
837 }
838 // There must be no length.
839 if (Length) {
840 Error(StartLoc, "invalid use of length addressing");
841 return MatchOperand_ParseFail;
842 }
843 break;
844 case BDLMem:
845 // If we have Reg2, it must be an address register.
846 if (HaveReg2) {
847 if (parseAddressRegister(Reg2))
848 return MatchOperand_ParseFail;
849 Base = Regs[Reg2.Num];
850 }
851 // We cannot support base+index addressing.
852 if (HaveReg1 && HaveReg2) {
853 Error(StartLoc, "invalid use of indexed addressing");
854 return MatchOperand_ParseFail;
855 }
856 // We must have a length.
857 if (!Length) {
858 Error(StartLoc, "missing length in address");
859 return MatchOperand_ParseFail;
860 }
861 break;
862 case BDRMem:
863 // We must have Reg1, and it must be a GPR.
864 if (!HaveReg1 || Reg1.Group != RegGR) {
865 Error(StartLoc, "invalid operand for instruction");
866 return MatchOperand_ParseFail;
867 }
868 LengthReg = SystemZMC::GR64Regs[Reg1.Num];
869 // If we have Reg2, it must be an address register.
870 if (HaveReg2) {
871 if (parseAddressRegister(Reg2))
872 return MatchOperand_ParseFail;
873 Base = Regs[Reg2.Num];
874 }
875 // There must be no length.
876 if (Length) {
877 Error(StartLoc, "invalid use of length addressing");
878 return MatchOperand_ParseFail;
879 }
880 break;
881 case BDVMem:
882 // We must have Reg1, and it must be a vector register.
883 if (!HaveReg1 || Reg1.Group != RegV) {
884 Error(StartLoc, "vector index required in address");
885 return MatchOperand_ParseFail;
886 }
887 Index = SystemZMC::VR128Regs[Reg1.Num];
888 // If we have Reg2, it must be an address register.
889 if (HaveReg2) {
890 if (parseAddressRegister(Reg2))
891 return MatchOperand_ParseFail;
892 Base = Regs[Reg2.Num];
893 }
894 // There must be no length.
895 if (Length) {
896 Error(StartLoc, "invalid use of length addressing");
897 return MatchOperand_ParseFail;
898 }
899 break;
Ulrich Weiganda8b04e12015-05-05 19:23:40 +0000900 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000901
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000902 SMLoc EndLoc =
903 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000904 Operands.push_back(SystemZOperand::createMem(MemKind, RegKind, Base, Disp,
Ulrich Weigandec5d7792016-10-31 14:21:36 +0000905 Index, Length, LengthReg,
906 StartLoc, EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000907 return MatchOperand_Success;
908}
909
910bool SystemZAsmParser::ParseDirective(AsmToken DirectiveID) {
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000911 StringRef IDVal = DirectiveID.getIdentifier();
912
913 if (IDVal == ".insn")
914 return ParseDirectiveInsn(DirectiveID.getLoc());
915
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000916 return true;
917}
918
Zhan Jun Liau4fbc3f42016-08-08 15:13:08 +0000919/// ParseDirectiveInsn
920/// ::= .insn [ format, encoding, (operands (, operands)*) ]
921bool SystemZAsmParser::ParseDirectiveInsn(SMLoc L) {
922 MCAsmParser &Parser = getParser();
923
924 // Expect instruction format as identifier.
925 StringRef Format;
926 SMLoc ErrorLoc = Parser.getTok().getLoc();
927 if (Parser.parseIdentifier(Format))
928 return Error(ErrorLoc, "expected instruction format");
929
930 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> Operands;
931
932 // Find entry for this format in InsnMatchTable.
933 auto EntryRange =
934 std::equal_range(std::begin(InsnMatchTable), std::end(InsnMatchTable),
935 Format, CompareInsn());
936
937 // If first == second, couldn't find a match in the table.
938 if (EntryRange.first == EntryRange.second)
939 return Error(ErrorLoc, "unrecognized format");
940
941 struct InsnMatchEntry *Entry = EntryRange.first;
942
943 // Format should match from equal_range.
944 assert(Entry->Format == Format);
945
946 // Parse the following operands using the table's information.
947 for (int i = 0; i < Entry->NumOperands; i++) {
948 MatchClassKind Kind = Entry->OperandKinds[i];
949
950 SMLoc StartLoc = Parser.getTok().getLoc();
951
952 // Always expect commas as separators for operands.
953 if (getLexer().isNot(AsmToken::Comma))
954 return Error(StartLoc, "unexpected token in directive");
955 Lex();
956
957 // Parse operands.
958 OperandMatchResultTy ResTy;
959 if (Kind == MCK_AnyReg)
960 ResTy = parseAnyReg(Operands);
961 else if (Kind == MCK_BDXAddr64Disp12 || Kind == MCK_BDXAddr64Disp20)
962 ResTy = parseBDXAddr64(Operands);
963 else if (Kind == MCK_BDAddr64Disp12 || Kind == MCK_BDAddr64Disp20)
964 ResTy = parseBDAddr64(Operands);
965 else if (Kind == MCK_PCRel32)
966 ResTy = parsePCRel32(Operands);
967 else if (Kind == MCK_PCRel16)
968 ResTy = parsePCRel16(Operands);
969 else {
970 // Only remaining operand kind is an immediate.
971 const MCExpr *Expr;
972 SMLoc StartLoc = Parser.getTok().getLoc();
973
974 // Expect immediate expression.
975 if (Parser.parseExpression(Expr))
976 return Error(StartLoc, "unexpected token in directive");
977
978 SMLoc EndLoc =
979 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
980
981 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
982 ResTy = MatchOperand_Success;
983 }
984
985 if (ResTy != MatchOperand_Success)
986 return true;
987 }
988
989 // Build the instruction with the parsed operands.
990 MCInst Inst = MCInstBuilder(Entry->Opcode);
991
992 for (size_t i = 0; i < Operands.size(); i++) {
993 MCParsedAsmOperand &Operand = *Operands[i];
994 MatchClassKind Kind = Entry->OperandKinds[i];
995
996 // Verify operand.
997 unsigned Res = validateOperandClass(Operand, Kind);
998 if (Res != Match_Success)
999 return Error(Operand.getStartLoc(), "unexpected operand type");
1000
1001 // Add operands to instruction.
1002 SystemZOperand &ZOperand = static_cast<SystemZOperand &>(Operand);
1003 if (ZOperand.isReg())
1004 ZOperand.addRegOperands(Inst, 1);
1005 else if (ZOperand.isMem(BDMem))
1006 ZOperand.addBDAddrOperands(Inst, 2);
1007 else if (ZOperand.isMem(BDXMem))
1008 ZOperand.addBDXAddrOperands(Inst, 3);
1009 else if (ZOperand.isImm())
1010 ZOperand.addImmOperands(Inst, 1);
1011 else
1012 llvm_unreachable("unexpected operand type");
1013 }
1014
1015 // Emit as a regular instruction.
1016 Parser.getStreamer().EmitInstruction(Inst, getSTI());
1017
1018 return false;
1019}
1020
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001021bool SystemZAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
1022 SMLoc &EndLoc) {
1023 Register Reg;
1024 if (parseRegister(Reg))
Richard Sandiforddc5ed712013-05-24 14:26:46 +00001025 return true;
Richard Sandiford675f8692013-05-24 14:14:38 +00001026 if (Reg.Group == RegGR)
1027 RegNo = SystemZMC::GR64Regs[Reg.Num];
1028 else if (Reg.Group == RegFP)
1029 RegNo = SystemZMC::FP64Regs[Reg.Num];
Ulrich Weiganda8b04e12015-05-05 19:23:40 +00001030 else if (Reg.Group == RegV)
1031 RegNo = SystemZMC::VR128Regs[Reg.Num];
Ulrich Weigandfffc7112016-11-08 20:15:26 +00001032 else if (Reg.Group == RegAR)
1033 RegNo = SystemZMC::AR32Regs[Reg.Num];
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001034 StartLoc = Reg.StartLoc;
1035 EndLoc = Reg.EndLoc;
1036 return false;
1037}
1038
David Blaikie960ea3f2014-06-08 16:18:35 +00001039bool SystemZAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1040 StringRef Name, SMLoc NameLoc,
1041 OperandVector &Operands) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001042 Operands.push_back(SystemZOperand::createToken(Name, NameLoc));
1043
1044 // Read the remaining operands.
1045 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1046 // Read the first operand.
1047 if (parseOperand(Operands, Name)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001048 return true;
1049 }
1050
1051 // Read any subsequent operands.
1052 while (getLexer().is(AsmToken::Comma)) {
1053 Parser.Lex();
1054 if (parseOperand(Operands, Name)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001055 return true;
1056 }
1057 }
1058 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1059 SMLoc Loc = getLexer().getLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001060 return Error(Loc, "unexpected token in argument list");
1061 }
1062 }
1063
1064 // Consume the EndOfStatement.
1065 Parser.Lex();
1066 return false;
1067}
1068
David Blaikie960ea3f2014-06-08 16:18:35 +00001069bool SystemZAsmParser::parseOperand(OperandVector &Operands,
1070 StringRef Mnemonic) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001071 // Check if the current operand has a custom associated parser, if so, try to
Ulrich Weigandd9001302016-10-31 14:25:05 +00001072 // custom parse the operand, or fallback to the general approach. Force all
1073 // features to be available during the operand check, or else we will fail to
1074 // find the custom parser, and then we will later get an InvalidOperand error
1075 // instead of a MissingFeature errror.
1076 uint64_t AvailableFeatures = getAvailableFeatures();
1077 setAvailableFeatures(~(uint64_t)0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001078 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
Ulrich Weigandd9001302016-10-31 14:25:05 +00001079 setAvailableFeatures(AvailableFeatures);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001080 if (ResTy == MatchOperand_Success)
1081 return false;
1082
1083 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1084 // there was a match, but an error occurred, in which case, just return that
1085 // the operand parsing failed.
1086 if (ResTy == MatchOperand_ParseFail)
1087 return true;
1088
Richard Sandiforddc5ed712013-05-24 14:26:46 +00001089 // Check for a register. All real register operands should have used
1090 // a context-dependent parse routine, which gives the required register
1091 // class. The code is here to mop up other cases, like those where
1092 // the instruction isn't recognized.
1093 if (Parser.getTok().is(AsmToken::Percent)) {
1094 Register Reg;
1095 if (parseRegister(Reg))
1096 return true;
1097 Operands.push_back(SystemZOperand::createInvalid(Reg.StartLoc, Reg.EndLoc));
1098 return false;
1099 }
1100
1101 // The only other type of operand is an immediate or address. As above,
1102 // real address operands should have used a context-dependent parse routine,
1103 // so we treat any plain expression as an immediate.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001104 SMLoc StartLoc = Parser.getTok().getLoc();
Ulrich Weigandec5d7792016-10-31 14:21:36 +00001105 Register Reg1, Reg2;
1106 bool HaveReg1, HaveReg2;
1107 const MCExpr *Expr;
1108 const MCExpr *Length;
1109 if (parseAddress(HaveReg1, Reg1, HaveReg2, Reg2, Expr, Length))
Ulrich Weigand75d2f1b2016-11-02 11:32:28 +00001110 return true;
Ulrich Weigandec5d7792016-10-31 14:21:36 +00001111 // If the register combination is not valid for any instruction, reject it.
1112 // Otherwise, fall back to reporting an unrecognized instruction.
1113 if (HaveReg1 && Reg1.Group != RegGR && Reg1.Group != RegV
1114 && parseAddressRegister(Reg1))
Ulrich Weigand75d2f1b2016-11-02 11:32:28 +00001115 return true;
Ulrich Weigandec5d7792016-10-31 14:21:36 +00001116 if (HaveReg2 && parseAddressRegister(Reg2))
Ulrich Weigand75d2f1b2016-11-02 11:32:28 +00001117 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001118
1119 SMLoc EndLoc =
1120 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Ulrich Weigandec5d7792016-10-31 14:21:36 +00001121 if (HaveReg1 || HaveReg2 || Length)
Richard Sandiforddc5ed712013-05-24 14:26:46 +00001122 Operands.push_back(SystemZOperand::createInvalid(StartLoc, EndLoc));
1123 else
1124 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001125 return false;
1126}
1127
David Blaikie960ea3f2014-06-08 16:18:35 +00001128bool SystemZAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
1129 OperandVector &Operands,
1130 MCStreamer &Out,
Tim Northover26bb14e2014-08-18 11:49:42 +00001131 uint64_t &ErrorInfo,
David Blaikie960ea3f2014-06-08 16:18:35 +00001132 bool MatchingInlineAsm) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001133 MCInst Inst;
1134 unsigned MatchResult;
1135
1136 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001137 MatchingInlineAsm);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001138 switch (MatchResult) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001139 case Match_Success:
1140 Inst.setLoc(IDLoc);
Akira Hatanakabd9fc282015-11-14 05:20:05 +00001141 Out.EmitInstruction(Inst, getSTI());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001142 return false;
1143
1144 case Match_MissingFeature: {
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001145 assert(ErrorInfo && "Unknown missing feature!");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001146 // Special case the error message for the very common case where only
1147 // a single subtarget feature is missing
1148 std::string Msg = "instruction requires:";
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001149 uint64_t Mask = 1;
1150 for (unsigned I = 0; I < sizeof(ErrorInfo) * 8 - 1; ++I) {
1151 if (ErrorInfo & Mask) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001152 Msg += " ";
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001153 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001154 }
Ranjeet Singh86ecbb72015-06-30 12:32:53 +00001155 Mask <<= 1;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001156 }
1157 return Error(IDLoc, Msg);
1158 }
1159
1160 case Match_InvalidOperand: {
1161 SMLoc ErrorLoc = IDLoc;
Tim Northover26bb14e2014-08-18 11:49:42 +00001162 if (ErrorInfo != ~0ULL) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001163 if (ErrorInfo >= Operands.size())
1164 return Error(IDLoc, "too few operands for instruction");
1165
David Blaikie960ea3f2014-06-08 16:18:35 +00001166 ErrorLoc = ((SystemZOperand &)*Operands[ErrorInfo]).getStartLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001167 if (ErrorLoc == SMLoc())
1168 ErrorLoc = IDLoc;
1169 }
1170 return Error(ErrorLoc, "invalid operand for instruction");
1171 }
1172
1173 case Match_MnemonicFail:
1174 return Error(IDLoc, "invalid instruction");
1175 }
1176
1177 llvm_unreachable("Unexpected match type");
1178}
1179
Alex Bradbury58eba092016-11-01 16:32:05 +00001180OperandMatchResultTy
David Blaikie960ea3f2014-06-08 16:18:35 +00001181SystemZAsmParser::parsePCRel(OperandVector &Operands, int64_t MinVal,
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +00001182 int64_t MaxVal, bool AllowTLS) {
Richard Sandiford1fb58832013-05-14 09:47:26 +00001183 MCContext &Ctx = getContext();
1184 MCStreamer &Out = getStreamer();
1185 const MCExpr *Expr;
1186 SMLoc StartLoc = Parser.getTok().getLoc();
1187 if (getParser().parseExpression(Expr))
1188 return MatchOperand_NoMatch;
1189
1190 // For consistency with the GNU assembler, treat immediates as offsets
1191 // from ".".
Richard Sandiford21f5d682014-03-06 11:22:58 +00001192 if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
Richard Sandiford1fb58832013-05-14 09:47:26 +00001193 int64_t Value = CE->getValue();
1194 if ((Value & 1) || Value < MinVal || Value > MaxVal) {
1195 Error(StartLoc, "offset out of range");
1196 return MatchOperand_ParseFail;
1197 }
Jim Grosbach6f482002015-05-18 18:43:14 +00001198 MCSymbol *Sym = Ctx.createTempSymbol();
Richard Sandiford1fb58832013-05-14 09:47:26 +00001199 Out.EmitLabel(Sym);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001200 const MCExpr *Base = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
Richard Sandiford1fb58832013-05-14 09:47:26 +00001201 Ctx);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001202 Expr = Value == 0 ? Base : MCBinaryExpr::createAdd(Base, Expr, Ctx);
Richard Sandiford1fb58832013-05-14 09:47:26 +00001203 }
1204
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +00001205 // Optionally match :tls_gdcall: or :tls_ldcall: followed by a TLS symbol.
1206 const MCExpr *Sym = nullptr;
1207 if (AllowTLS && getLexer().is(AsmToken::Colon)) {
1208 Parser.Lex();
1209
1210 if (Parser.getTok().isNot(AsmToken::Identifier)) {
1211 Error(Parser.getTok().getLoc(), "unexpected token");
1212 return MatchOperand_ParseFail;
1213 }
1214
1215 MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
1216 StringRef Name = Parser.getTok().getString();
1217 if (Name == "tls_gdcall")
1218 Kind = MCSymbolRefExpr::VK_TLSGD;
1219 else if (Name == "tls_ldcall")
1220 Kind = MCSymbolRefExpr::VK_TLSLDM;
1221 else {
1222 Error(Parser.getTok().getLoc(), "unknown TLS tag");
1223 return MatchOperand_ParseFail;
1224 }
1225 Parser.Lex();
1226
1227 if (Parser.getTok().isNot(AsmToken::Colon)) {
1228 Error(Parser.getTok().getLoc(), "unexpected token");
1229 return MatchOperand_ParseFail;
1230 }
1231 Parser.Lex();
1232
1233 if (Parser.getTok().isNot(AsmToken::Identifier)) {
1234 Error(Parser.getTok().getLoc(), "unexpected token");
1235 return MatchOperand_ParseFail;
1236 }
1237
1238 StringRef Identifier = Parser.getTok().getString();
Jim Grosbach13760bd2015-05-30 01:25:56 +00001239 Sym = MCSymbolRefExpr::create(Ctx.getOrCreateSymbol(Identifier),
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +00001240 Kind, Ctx);
1241 Parser.Lex();
1242 }
1243
Richard Sandiford1fb58832013-05-14 09:47:26 +00001244 SMLoc EndLoc =
1245 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +00001246
1247 if (AllowTLS)
1248 Operands.push_back(SystemZOperand::createImmTLS(Expr, Sym,
1249 StartLoc, EndLoc));
1250 else
1251 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
1252
Richard Sandiford1fb58832013-05-14 09:47:26 +00001253 return MatchOperand_Success;
1254}
1255
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001256// Force static initialization.
1257extern "C" void LLVMInitializeSystemZAsmParser() {
Mehdi Aminif42454b2016-10-09 23:00:34 +00001258 RegisterMCAsmParser<SystemZAsmParser> X(getTheSystemZTarget());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001259}