blob: ec6a57388bc88078939748099fc1af00228bd9b4 [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- SystemZAsmParser.cpp - Parse SystemZ assembly instructions --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "MCTargetDesc/SystemZMCTargetDesc.h"
Craig Topper690d8ea2013-07-24 07:33:14 +000011#include "llvm/ADT/STLExtras.h"
Richard Sandiford1fb58832013-05-14 09:47:26 +000012#include "llvm/MC/MCContext.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000013#include "llvm/MC/MCExpr.h"
14#include "llvm/MC/MCInst.h"
15#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
16#include "llvm/MC/MCStreamer.h"
17#include "llvm/MC/MCSubtargetInfo.h"
18#include "llvm/MC/MCTargetAsmParser.h"
19#include "llvm/Support/TargetRegistry.h"
20
21using namespace llvm;
22
23// Return true if Expr is in the range [MinValue, MaxValue].
24static bool inRange(const MCExpr *Expr, int64_t MinValue, int64_t MaxValue) {
Richard Sandiford21f5d682014-03-06 11:22:58 +000025 if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000026 int64_t Value = CE->getValue();
27 return Value >= MinValue && Value <= MaxValue;
28 }
29 return false;
30}
31
32namespace {
Richard Sandiford1d959002013-07-02 14:56:45 +000033enum RegisterKind {
34 GR32Reg,
Richard Sandifordf9496062013-09-30 10:45:16 +000035 GRH32Reg,
Richard Sandiford1d959002013-07-02 14:56:45 +000036 GR64Reg,
37 GR128Reg,
38 ADDR32Reg,
39 ADDR64Reg,
40 FP32Reg,
41 FP64Reg,
42 FP128Reg
43};
44
45enum MemoryKind {
46 BDMem,
47 BDXMem,
48 BDLMem
49};
50
Ulrich Weigand5f613df2013-05-06 16:15:19 +000051class SystemZOperand : public MCParsedAsmOperand {
52public:
Ulrich Weigand5f613df2013-05-06 16:15:19 +000053private:
54 enum OperandKind {
Richard Sandiforddc5ed712013-05-24 14:26:46 +000055 KindInvalid,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000056 KindToken,
57 KindReg,
58 KindAccessReg,
59 KindImm,
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +000060 KindImmTLS,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000061 KindMem
62 };
63
64 OperandKind Kind;
65 SMLoc StartLoc, EndLoc;
66
67 // A string of length Length, starting at Data.
68 struct TokenOp {
69 const char *Data;
70 unsigned Length;
71 };
72
Richard Sandiford675f8692013-05-24 14:14:38 +000073 // LLVM register Num, which has kind Kind. In some ways it might be
74 // easier for this class to have a register bank (general, floating-point
75 // or access) and a raw register number (0-15). This would postpone the
76 // interpretation of the operand to the add*() methods and avoid the need
77 // for context-dependent parsing. However, we do things the current way
78 // because of the virtual getReg() method, which needs to distinguish
79 // between (say) %r0 used as a single register and %r0 used as a pair.
80 // Context-dependent parsing can also give us slightly better error
81 // messages when invalid pairs like %r1 are used.
Ulrich Weigand5f613df2013-05-06 16:15:19 +000082 struct RegOp {
83 RegisterKind Kind;
84 unsigned Num;
85 };
86
87 // Base + Disp + Index, where Base and Index are LLVM registers or 0.
Ulrich Weigand1f698b02015-05-04 17:40:53 +000088 // MemKind says what type of memory this is and RegKind says what type
89 // the base register has (ADDR32Reg or ADDR64Reg). Length is the operand
90 // length for D(L,B)-style operands, otherwise it is null.
Ulrich Weigand5f613df2013-05-06 16:15:19 +000091 struct MemOp {
92 unsigned Base : 8;
93 unsigned Index : 8;
Ulrich Weigand1f698b02015-05-04 17:40:53 +000094 unsigned MemKind : 8;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000095 unsigned RegKind : 8;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000096 const MCExpr *Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +000097 const MCExpr *Length;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000098 };
99
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000100 // Imm is an immediate operand, and Sym is an optional TLS symbol
101 // for use with a __tls_get_offset marker relocation.
102 struct ImmTLSOp {
103 const MCExpr *Imm;
104 const MCExpr *Sym;
105 };
106
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000107 union {
108 TokenOp Token;
109 RegOp Reg;
110 unsigned AccessReg;
111 const MCExpr *Imm;
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000112 ImmTLSOp ImmTLS;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000113 MemOp Mem;
114 };
115
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000116 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
117 // Add as immediates when possible. Null MCExpr = 0.
Craig Topper062a2ba2014-04-25 05:30:21 +0000118 if (!Expr)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000119 Inst.addOperand(MCOperand::CreateImm(0));
Richard Sandiford21f5d682014-03-06 11:22:58 +0000120 else if (auto *CE = dyn_cast<MCConstantExpr>(Expr))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000121 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
122 else
123 Inst.addOperand(MCOperand::CreateExpr(Expr));
124 }
125
126public:
David Blaikie960ea3f2014-06-08 16:18:35 +0000127 SystemZOperand(OperandKind kind, SMLoc startLoc, SMLoc endLoc)
128 : Kind(kind), StartLoc(startLoc), EndLoc(endLoc) {}
129
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000130 // Create particular kinds of operand.
David Blaikie960ea3f2014-06-08 16:18:35 +0000131 static std::unique_ptr<SystemZOperand> createInvalid(SMLoc StartLoc,
132 SMLoc EndLoc) {
133 return make_unique<SystemZOperand>(KindInvalid, StartLoc, EndLoc);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000134 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000135 static std::unique_ptr<SystemZOperand> createToken(StringRef Str, SMLoc Loc) {
136 auto Op = make_unique<SystemZOperand>(KindToken, Loc, Loc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000137 Op->Token.Data = Str.data();
138 Op->Token.Length = Str.size();
139 return Op;
140 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000141 static std::unique_ptr<SystemZOperand>
142 createReg(RegisterKind Kind, unsigned Num, SMLoc StartLoc, SMLoc EndLoc) {
143 auto Op = make_unique<SystemZOperand>(KindReg, StartLoc, EndLoc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000144 Op->Reg.Kind = Kind;
145 Op->Reg.Num = Num;
146 return Op;
147 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000148 static std::unique_ptr<SystemZOperand>
149 createAccessReg(unsigned Num, SMLoc StartLoc, SMLoc EndLoc) {
150 auto Op = make_unique<SystemZOperand>(KindAccessReg, StartLoc, EndLoc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000151 Op->AccessReg = Num;
152 return Op;
153 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000154 static std::unique_ptr<SystemZOperand>
155 createImm(const MCExpr *Expr, SMLoc StartLoc, SMLoc EndLoc) {
156 auto Op = make_unique<SystemZOperand>(KindImm, StartLoc, EndLoc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000157 Op->Imm = Expr;
158 return Op;
159 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000160 static std::unique_ptr<SystemZOperand>
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000161 createMem(MemoryKind MemKind, RegisterKind RegKind, unsigned Base,
162 const MCExpr *Disp, unsigned Index, const MCExpr *Length,
163 SMLoc StartLoc, SMLoc EndLoc) {
David Blaikie960ea3f2014-06-08 16:18:35 +0000164 auto Op = make_unique<SystemZOperand>(KindMem, StartLoc, EndLoc);
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000165 Op->Mem.MemKind = MemKind;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000166 Op->Mem.RegKind = RegKind;
167 Op->Mem.Base = Base;
168 Op->Mem.Index = Index;
169 Op->Mem.Disp = Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +0000170 Op->Mem.Length = Length;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000171 return Op;
172 }
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000173 static std::unique_ptr<SystemZOperand>
174 createImmTLS(const MCExpr *Imm, const MCExpr *Sym,
175 SMLoc StartLoc, SMLoc EndLoc) {
176 auto Op = make_unique<SystemZOperand>(KindImmTLS, StartLoc, EndLoc);
177 Op->ImmTLS.Imm = Imm;
178 Op->ImmTLS.Sym = Sym;
179 return Op;
180 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000181
182 // Token operands
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000183 bool isToken() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000184 return Kind == KindToken;
185 }
186 StringRef getToken() const {
187 assert(Kind == KindToken && "Not a token");
188 return StringRef(Token.Data, Token.Length);
189 }
190
191 // Register operands.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000192 bool isReg() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000193 return Kind == KindReg;
194 }
195 bool isReg(RegisterKind RegKind) const {
196 return Kind == KindReg && Reg.Kind == RegKind;
197 }
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000198 unsigned getReg() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000199 assert(Kind == KindReg && "Not a register");
200 return Reg.Num;
201 }
202
203 // Access register operands. Access registers aren't exposed to LLVM
204 // as registers.
205 bool isAccessReg() const {
206 return Kind == KindAccessReg;
207 }
208
209 // Immediate operands.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000210 bool isImm() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000211 return Kind == KindImm;
212 }
213 bool isImm(int64_t MinValue, int64_t MaxValue) const {
214 return Kind == KindImm && inRange(Imm, MinValue, MaxValue);
215 }
216 const MCExpr *getImm() const {
217 assert(Kind == KindImm && "Not an immediate");
218 return Imm;
219 }
220
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000221 // Immediate operands with optional TLS symbol.
222 bool isImmTLS() const {
223 return Kind == KindImmTLS;
224 }
225
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000226 // Memory operands.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000227 bool isMem() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000228 return Kind == KindMem;
229 }
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000230 bool isMem(MemoryKind MemKind) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000231 return (Kind == KindMem &&
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000232 (Mem.MemKind == MemKind ||
233 // A BDMem can be treated as a BDXMem in which the index
234 // register field is 0.
235 (Mem.MemKind == BDMem && MemKind == BDXMem)));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000236 }
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000237 bool isMem(MemoryKind MemKind, RegisterKind RegKind) const {
238 return isMem(MemKind) && Mem.RegKind == RegKind;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000239 }
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000240 bool isMemDisp12(MemoryKind MemKind, RegisterKind RegKind) const {
241 return isMem(MemKind, RegKind) && inRange(Mem.Disp, 0, 0xfff);
242 }
243 bool isMemDisp20(MemoryKind MemKind, RegisterKind RegKind) const {
244 return isMem(MemKind, RegKind) && inRange(Mem.Disp, -524288, 524287);
Richard Sandiford1d959002013-07-02 14:56:45 +0000245 }
246 bool isMemDisp12Len8(RegisterKind RegKind) const {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000247 return isMemDisp12(BDLMem, RegKind) && inRange(Mem.Length, 1, 0x100);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000248 }
249
250 // Override MCParsedAsmOperand.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000251 SMLoc getStartLoc() const override { return StartLoc; }
252 SMLoc getEndLoc() const override { return EndLoc; }
253 void print(raw_ostream &OS) const override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000254
255 // Used by the TableGen code to add particular types of operand
256 // to an instruction.
257 void addRegOperands(MCInst &Inst, unsigned N) const {
258 assert(N == 1 && "Invalid number of operands");
259 Inst.addOperand(MCOperand::CreateReg(getReg()));
260 }
261 void addAccessRegOperands(MCInst &Inst, unsigned N) const {
262 assert(N == 1 && "Invalid number of operands");
263 assert(Kind == KindAccessReg && "Invalid operand type");
264 Inst.addOperand(MCOperand::CreateImm(AccessReg));
265 }
266 void addImmOperands(MCInst &Inst, unsigned N) const {
267 assert(N == 1 && "Invalid number of operands");
268 addExpr(Inst, getImm());
269 }
270 void addBDAddrOperands(MCInst &Inst, unsigned N) const {
271 assert(N == 2 && "Invalid number of operands");
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000272 assert(isMem(BDMem) && "Invalid operand type");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000273 Inst.addOperand(MCOperand::CreateReg(Mem.Base));
274 addExpr(Inst, Mem.Disp);
275 }
276 void addBDXAddrOperands(MCInst &Inst, unsigned N) const {
277 assert(N == 3 && "Invalid number of operands");
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000278 assert(isMem(BDXMem) && "Invalid operand type");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000279 Inst.addOperand(MCOperand::CreateReg(Mem.Base));
280 addExpr(Inst, Mem.Disp);
281 Inst.addOperand(MCOperand::CreateReg(Mem.Index));
282 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000283 void addBDLAddrOperands(MCInst &Inst, unsigned N) const {
284 assert(N == 3 && "Invalid number of operands");
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000285 assert(isMem(BDLMem) && "Invalid operand type");
Richard Sandiford1d959002013-07-02 14:56:45 +0000286 Inst.addOperand(MCOperand::CreateReg(Mem.Base));
287 addExpr(Inst, Mem.Disp);
288 addExpr(Inst, Mem.Length);
289 }
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000290 void addImmTLSOperands(MCInst &Inst, unsigned N) const {
291 assert(N == 2 && "Invalid number of operands");
292 assert(Kind == KindImmTLS && "Invalid operand type");
293 addExpr(Inst, ImmTLS.Imm);
294 if (ImmTLS.Sym)
295 addExpr(Inst, ImmTLS.Sym);
296 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000297
298 // Used by the TableGen code to check for particular operand types.
299 bool isGR32() const { return isReg(GR32Reg); }
Richard Sandifordf9496062013-09-30 10:45:16 +0000300 bool isGRH32() const { return isReg(GRH32Reg); }
Richard Sandiford0755c932013-10-01 11:26:28 +0000301 bool isGRX32() const { return false; }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000302 bool isGR64() const { return isReg(GR64Reg); }
303 bool isGR128() const { return isReg(GR128Reg); }
304 bool isADDR32() const { return isReg(ADDR32Reg); }
305 bool isADDR64() const { return isReg(ADDR64Reg); }
306 bool isADDR128() const { return false; }
307 bool isFP32() const { return isReg(FP32Reg); }
308 bool isFP64() const { return isReg(FP64Reg); }
309 bool isFP128() const { return isReg(FP128Reg); }
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000310 bool isBDAddr32Disp12() const { return isMemDisp12(BDMem, ADDR32Reg); }
311 bool isBDAddr32Disp20() const { return isMemDisp20(BDMem, ADDR32Reg); }
312 bool isBDAddr64Disp12() const { return isMemDisp12(BDMem, ADDR64Reg); }
313 bool isBDAddr64Disp20() const { return isMemDisp20(BDMem, ADDR64Reg); }
314 bool isBDXAddr64Disp12() const { return isMemDisp12(BDXMem, ADDR64Reg); }
315 bool isBDXAddr64Disp20() const { return isMemDisp20(BDXMem, ADDR64Reg); }
Richard Sandiford1d959002013-07-02 14:56:45 +0000316 bool isBDLAddr64Disp12Len8() const { return isMemDisp12Len8(ADDR64Reg); }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000317 bool isU4Imm() const { return isImm(0, 15); }
318 bool isU6Imm() const { return isImm(0, 63); }
319 bool isU8Imm() const { return isImm(0, 255); }
320 bool isS8Imm() const { return isImm(-128, 127); }
321 bool isU16Imm() const { return isImm(0, 65535); }
322 bool isS16Imm() const { return isImm(-32768, 32767); }
323 bool isU32Imm() const { return isImm(0, (1LL << 32) - 1); }
324 bool isS32Imm() const { return isImm(-(1LL << 31), (1LL << 31) - 1); }
325};
326
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000327class SystemZAsmParser : public MCTargetAsmParser {
328#define GET_ASSEMBLER_HEADER
329#include "SystemZGenAsmMatcher.inc"
330
331private:
332 MCSubtargetInfo &STI;
333 MCAsmParser &Parser;
Richard Sandiford675f8692013-05-24 14:14:38 +0000334 enum RegisterGroup {
335 RegGR,
336 RegFP,
337 RegAccess
338 };
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000339 struct Register {
Richard Sandiford675f8692013-05-24 14:14:38 +0000340 RegisterGroup Group;
341 unsigned Num;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000342 SMLoc StartLoc, EndLoc;
343 };
344
345 bool parseRegister(Register &Reg);
346
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000347 bool parseRegister(Register &Reg, RegisterGroup Group, const unsigned *Regs,
348 bool IsAddress = false);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000349
David Blaikie960ea3f2014-06-08 16:18:35 +0000350 OperandMatchResultTy parseRegister(OperandVector &Operands,
351 RegisterGroup Group, const unsigned *Regs,
352 RegisterKind Kind);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000353
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000354 bool parseAddress(unsigned &Base, const MCExpr *&Disp,
Richard Sandiford1d959002013-07-02 14:56:45 +0000355 unsigned &Index, const MCExpr *&Length,
356 const unsigned *Regs, RegisterKind RegKind);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000357
David Blaikie960ea3f2014-06-08 16:18:35 +0000358 OperandMatchResultTy parseAddress(OperandVector &Operands,
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000359 MemoryKind MemKind, const unsigned *Regs,
360 RegisterKind RegKind);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000361
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000362 OperandMatchResultTy parsePCRel(OperandVector &Operands, int64_t MinVal,
363 int64_t MaxVal, bool AllowTLS);
364
David Blaikie960ea3f2014-06-08 16:18:35 +0000365 bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000366
367public:
Joey Gouly0e76fa72013-09-12 10:28:05 +0000368 SystemZAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000369 const MCInstrInfo &MII,
370 const MCTargetOptions &Options)
Joey Gouly0e76fa72013-09-12 10:28:05 +0000371 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000372 MCAsmParserExtension::Initialize(Parser);
373
374 // Initialize the set of available features.
375 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
376 }
377
378 // Override MCTargetAsmParser.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000379 bool ParseDirective(AsmToken DirectiveID) override;
380 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
David Blaikie960ea3f2014-06-08 16:18:35 +0000381 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
382 SMLoc NameLoc, OperandVector &Operands) override;
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000383 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
David Blaikie960ea3f2014-06-08 16:18:35 +0000384 OperandVector &Operands, MCStreamer &Out,
Tim Northover26bb14e2014-08-18 11:49:42 +0000385 uint64_t &ErrorInfo,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000386 bool MatchingInlineAsm) override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000387
388 // Used by the TableGen code to parse particular operand types.
David Blaikie960ea3f2014-06-08 16:18:35 +0000389 OperandMatchResultTy parseGR32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000390 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, GR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000391 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000392 OperandMatchResultTy parseGRH32(OperandVector &Operands) {
Richard Sandifordf9496062013-09-30 10:45:16 +0000393 return parseRegister(Operands, RegGR, SystemZMC::GRH32Regs, GRH32Reg);
394 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000395 OperandMatchResultTy parseGRX32(OperandVector &Operands) {
Richard Sandiford0755c932013-10-01 11:26:28 +0000396 llvm_unreachable("GRX32 should only be used for pseudo instructions");
397 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000398 OperandMatchResultTy parseGR64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000399 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, GR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000400 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000401 OperandMatchResultTy parseGR128(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000402 return parseRegister(Operands, RegGR, SystemZMC::GR128Regs, GR128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000403 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000404 OperandMatchResultTy parseADDR32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000405 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, ADDR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000406 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000407 OperandMatchResultTy parseADDR64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000408 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000409 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000410 OperandMatchResultTy parseADDR128(OperandVector &Operands) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000411 llvm_unreachable("Shouldn't be used as an operand");
412 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000413 OperandMatchResultTy parseFP32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000414 return parseRegister(Operands, RegFP, SystemZMC::FP32Regs, FP32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000415 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000416 OperandMatchResultTy parseFP64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000417 return parseRegister(Operands, RegFP, SystemZMC::FP64Regs, FP64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000418 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000419 OperandMatchResultTy parseFP128(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000420 return parseRegister(Operands, RegFP, SystemZMC::FP128Regs, FP128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000421 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000422 OperandMatchResultTy parseBDAddr32(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000423 return parseAddress(Operands, BDMem, SystemZMC::GR32Regs, ADDR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000424 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000425 OperandMatchResultTy parseBDAddr64(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000426 return parseAddress(Operands, BDMem, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000427 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000428 OperandMatchResultTy parseBDXAddr64(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000429 return parseAddress(Operands, BDXMem, SystemZMC::GR64Regs, ADDR64Reg);
Richard Sandiford1d959002013-07-02 14:56:45 +0000430 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000431 OperandMatchResultTy parseBDLAddr64(OperandVector &Operands) {
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000432 return parseAddress(Operands, BDLMem, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000433 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000434 OperandMatchResultTy parseAccessReg(OperandVector &Operands);
David Blaikie960ea3f2014-06-08 16:18:35 +0000435 OperandMatchResultTy parsePCRel16(OperandVector &Operands) {
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000436 return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1, false);
Richard Sandiford1fb58832013-05-14 09:47:26 +0000437 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000438 OperandMatchResultTy parsePCRel32(OperandVector &Operands) {
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000439 return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1, false);
440 }
441 OperandMatchResultTy parsePCRelTLS16(OperandVector &Operands) {
442 return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1, true);
443 }
444 OperandMatchResultTy parsePCRelTLS32(OperandVector &Operands) {
445 return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1, true);
Richard Sandiford1fb58832013-05-14 09:47:26 +0000446 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000447};
Richard Sandifordc2312692014-03-06 10:38:30 +0000448} // end anonymous namespace
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000449
450#define GET_REGISTER_MATCHER
451#define GET_SUBTARGET_FEATURE_NAME
452#define GET_MATCHER_IMPLEMENTATION
453#include "SystemZGenAsmMatcher.inc"
454
455void SystemZOperand::print(raw_ostream &OS) const {
456 llvm_unreachable("Not implemented");
457}
458
459// Parse one register of the form %<prefix><number>.
460bool SystemZAsmParser::parseRegister(Register &Reg) {
461 Reg.StartLoc = Parser.getTok().getLoc();
462
463 // Eat the % prefix.
464 if (Parser.getTok().isNot(AsmToken::Percent))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000465 return Error(Parser.getTok().getLoc(), "register expected");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000466 Parser.Lex();
467
468 // Expect a register name.
469 if (Parser.getTok().isNot(AsmToken::Identifier))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000470 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000471
Richard Sandiford675f8692013-05-24 14:14:38 +0000472 // Check that there's a prefix.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000473 StringRef Name = Parser.getTok().getString();
474 if (Name.size() < 2)
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000475 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000476 char Prefix = Name[0];
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000477
478 // Treat the rest of the register name as a register number.
Richard Sandiford675f8692013-05-24 14:14:38 +0000479 if (Name.substr(1).getAsInteger(10, Reg.Num))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000480 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000481
482 // Look for valid combinations of prefix and number.
483 if (Prefix == 'r' && Reg.Num < 16)
484 Reg.Group = RegGR;
485 else if (Prefix == 'f' && Reg.Num < 16)
486 Reg.Group = RegFP;
487 else if (Prefix == 'a' && Reg.Num < 16)
488 Reg.Group = RegAccess;
489 else
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000490 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000491
492 Reg.EndLoc = Parser.getTok().getLoc();
493 Parser.Lex();
494 return false;
495}
496
Richard Sandiford675f8692013-05-24 14:14:38 +0000497// Parse a register of group Group. If Regs is nonnull, use it to map
498// the raw register number to LLVM numbering, with zero entries indicating
499// an invalid register. IsAddress says whether the register appears in an
500// address context.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000501bool SystemZAsmParser::parseRegister(Register &Reg, RegisterGroup Group,
502 const unsigned *Regs, bool IsAddress) {
503 if (parseRegister(Reg))
504 return true;
505 if (Reg.Group != Group)
506 return Error(Reg.StartLoc, "invalid operand for instruction");
507 if (Regs && Regs[Reg.Num] == 0)
508 return Error(Reg.StartLoc, "invalid register pair");
509 if (Reg.Num == 0 && IsAddress)
510 return Error(Reg.StartLoc, "%r0 used in an address");
Richard Sandiford675f8692013-05-24 14:14:38 +0000511 if (Regs)
512 Reg.Num = Regs[Reg.Num];
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000513 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000514}
515
Richard Sandiford675f8692013-05-24 14:14:38 +0000516// Parse a register and add it to Operands. The other arguments are as above.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000517SystemZAsmParser::OperandMatchResultTy
David Blaikie960ea3f2014-06-08 16:18:35 +0000518SystemZAsmParser::parseRegister(OperandVector &Operands, RegisterGroup Group,
519 const unsigned *Regs, RegisterKind Kind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000520 if (Parser.getTok().isNot(AsmToken::Percent))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000521 return MatchOperand_NoMatch;
522
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000523 Register Reg;
Richard Sandiford1d959002013-07-02 14:56:45 +0000524 bool IsAddress = (Kind == ADDR32Reg || Kind == ADDR64Reg);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000525 if (parseRegister(Reg, Group, Regs, IsAddress))
526 return MatchOperand_ParseFail;
527
528 Operands.push_back(SystemZOperand::createReg(Kind, Reg.Num,
529 Reg.StartLoc, Reg.EndLoc));
530 return MatchOperand_Success;
531}
532
Richard Sandiford1d959002013-07-02 14:56:45 +0000533// Parse a memory operand into Base, Disp, Index and Length.
534// Regs maps asm register numbers to LLVM register numbers and RegKind
535// says what kind of address register we're using (ADDR32Reg or ADDR64Reg).
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000536bool SystemZAsmParser::parseAddress(unsigned &Base, const MCExpr *&Disp,
Richard Sandiford1d959002013-07-02 14:56:45 +0000537 unsigned &Index, const MCExpr *&Length,
538 const unsigned *Regs,
539 RegisterKind RegKind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000540 // Parse the displacement, which must always be present.
541 if (getParser().parseExpression(Disp))
542 return true;
543
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000544 // Parse the optional base and index.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000545 Index = 0;
546 Base = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000547 Length = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000548 if (getLexer().is(AsmToken::LParen)) {
549 Parser.Lex();
550
Richard Sandiford1d959002013-07-02 14:56:45 +0000551 if (getLexer().is(AsmToken::Percent)) {
552 // Parse the first register and decide whether it's a base or an index.
553 Register Reg;
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000554 if (parseRegister(Reg, RegGR, Regs, RegKind))
555 return true;
Richard Sandiford1d959002013-07-02 14:56:45 +0000556 if (getLexer().is(AsmToken::Comma))
557 Index = Reg.Num;
558 else
559 Base = Reg.Num;
560 } else {
561 // Parse the length.
562 if (getParser().parseExpression(Length))
563 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000564 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000565
566 // Check whether there's a second register. It's the base if so.
567 if (getLexer().is(AsmToken::Comma)) {
568 Parser.Lex();
569 Register Reg;
570 if (parseRegister(Reg, RegGR, Regs, RegKind))
571 return true;
572 Base = Reg.Num;
573 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000574
575 // Consume the closing bracket.
576 if (getLexer().isNot(AsmToken::RParen))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000577 return Error(Parser.getTok().getLoc(), "unexpected token in address");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000578 Parser.Lex();
579 }
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000580 return false;
581}
582
583// Parse a memory operand and add it to Operands. The other arguments
584// are as above.
585SystemZAsmParser::OperandMatchResultTy
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000586SystemZAsmParser::parseAddress(OperandVector &Operands, MemoryKind MemKind,
587 const unsigned *Regs, RegisterKind RegKind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000588 SMLoc StartLoc = Parser.getTok().getLoc();
589 unsigned Base, Index;
590 const MCExpr *Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +0000591 const MCExpr *Length;
592 if (parseAddress(Base, Disp, Index, Length, Regs, RegKind))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000593 return MatchOperand_ParseFail;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000594
Richard Sandiford1d959002013-07-02 14:56:45 +0000595 if (Index && MemKind != BDXMem)
596 {
597 Error(StartLoc, "invalid use of indexed addressing");
598 return MatchOperand_ParseFail;
599 }
600
601 if (Length && MemKind != BDLMem)
602 {
603 Error(StartLoc, "invalid use of length addressing");
604 return MatchOperand_ParseFail;
605 }
606
607 if (!Length && MemKind == BDLMem)
608 {
609 Error(StartLoc, "missing length in address");
610 return MatchOperand_ParseFail;
611 }
612
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000613 SMLoc EndLoc =
614 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Ulrich Weigand1f698b02015-05-04 17:40:53 +0000615 Operands.push_back(SystemZOperand::createMem(MemKind, RegKind, Base, Disp,
616 Index, Length, StartLoc,
617 EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000618 return MatchOperand_Success;
619}
620
621bool SystemZAsmParser::ParseDirective(AsmToken DirectiveID) {
622 return true;
623}
624
625bool SystemZAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
626 SMLoc &EndLoc) {
627 Register Reg;
628 if (parseRegister(Reg))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000629 return true;
Richard Sandiford675f8692013-05-24 14:14:38 +0000630 if (Reg.Group == RegGR)
631 RegNo = SystemZMC::GR64Regs[Reg.Num];
632 else if (Reg.Group == RegFP)
633 RegNo = SystemZMC::FP64Regs[Reg.Num];
634 else
635 // FIXME: Access registers aren't modelled as LLVM registers yet.
636 return Error(Reg.StartLoc, "invalid operand for instruction");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000637 StartLoc = Reg.StartLoc;
638 EndLoc = Reg.EndLoc;
639 return false;
640}
641
David Blaikie960ea3f2014-06-08 16:18:35 +0000642bool SystemZAsmParser::ParseInstruction(ParseInstructionInfo &Info,
643 StringRef Name, SMLoc NameLoc,
644 OperandVector &Operands) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000645 Operands.push_back(SystemZOperand::createToken(Name, NameLoc));
646
647 // Read the remaining operands.
648 if (getLexer().isNot(AsmToken::EndOfStatement)) {
649 // Read the first operand.
650 if (parseOperand(Operands, Name)) {
651 Parser.eatToEndOfStatement();
652 return true;
653 }
654
655 // Read any subsequent operands.
656 while (getLexer().is(AsmToken::Comma)) {
657 Parser.Lex();
658 if (parseOperand(Operands, Name)) {
659 Parser.eatToEndOfStatement();
660 return true;
661 }
662 }
663 if (getLexer().isNot(AsmToken::EndOfStatement)) {
664 SMLoc Loc = getLexer().getLoc();
665 Parser.eatToEndOfStatement();
666 return Error(Loc, "unexpected token in argument list");
667 }
668 }
669
670 // Consume the EndOfStatement.
671 Parser.Lex();
672 return false;
673}
674
David Blaikie960ea3f2014-06-08 16:18:35 +0000675bool SystemZAsmParser::parseOperand(OperandVector &Operands,
676 StringRef Mnemonic) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000677 // Check if the current operand has a custom associated parser, if so, try to
678 // custom parse the operand, or fallback to the general approach.
679 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
680 if (ResTy == MatchOperand_Success)
681 return false;
682
683 // If there wasn't a custom match, try the generic matcher below. Otherwise,
684 // there was a match, but an error occurred, in which case, just return that
685 // the operand parsing failed.
686 if (ResTy == MatchOperand_ParseFail)
687 return true;
688
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000689 // Check for a register. All real register operands should have used
690 // a context-dependent parse routine, which gives the required register
691 // class. The code is here to mop up other cases, like those where
692 // the instruction isn't recognized.
693 if (Parser.getTok().is(AsmToken::Percent)) {
694 Register Reg;
695 if (parseRegister(Reg))
696 return true;
697 Operands.push_back(SystemZOperand::createInvalid(Reg.StartLoc, Reg.EndLoc));
698 return false;
699 }
700
701 // The only other type of operand is an immediate or address. As above,
702 // real address operands should have used a context-dependent parse routine,
703 // so we treat any plain expression as an immediate.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000704 SMLoc StartLoc = Parser.getTok().getLoc();
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000705 unsigned Base, Index;
Richard Sandiford1d959002013-07-02 14:56:45 +0000706 const MCExpr *Expr, *Length;
707 if (parseAddress(Base, Expr, Index, Length, SystemZMC::GR64Regs, ADDR64Reg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000708 return true;
709
710 SMLoc EndLoc =
711 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Richard Sandiford1d959002013-07-02 14:56:45 +0000712 if (Base || Index || Length)
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000713 Operands.push_back(SystemZOperand::createInvalid(StartLoc, EndLoc));
714 else
715 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000716 return false;
717}
718
David Blaikie960ea3f2014-06-08 16:18:35 +0000719bool SystemZAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
720 OperandVector &Operands,
721 MCStreamer &Out,
Tim Northover26bb14e2014-08-18 11:49:42 +0000722 uint64_t &ErrorInfo,
David Blaikie960ea3f2014-06-08 16:18:35 +0000723 bool MatchingInlineAsm) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000724 MCInst Inst;
725 unsigned MatchResult;
726
727 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
728 MatchingInlineAsm);
729 switch (MatchResult) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000730 case Match_Success:
731 Inst.setLoc(IDLoc);
David Woodhousee6c13e42014-01-28 23:12:42 +0000732 Out.EmitInstruction(Inst, STI);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000733 return false;
734
735 case Match_MissingFeature: {
736 assert(ErrorInfo && "Unknown missing feature!");
737 // Special case the error message for the very common case where only
738 // a single subtarget feature is missing
739 std::string Msg = "instruction requires:";
Tim Northover26bb14e2014-08-18 11:49:42 +0000740 uint64_t Mask = 1;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000741 for (unsigned I = 0; I < sizeof(ErrorInfo) * 8 - 1; ++I) {
742 if (ErrorInfo & Mask) {
743 Msg += " ";
744 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
745 }
746 Mask <<= 1;
747 }
748 return Error(IDLoc, Msg);
749 }
750
751 case Match_InvalidOperand: {
752 SMLoc ErrorLoc = IDLoc;
Tim Northover26bb14e2014-08-18 11:49:42 +0000753 if (ErrorInfo != ~0ULL) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000754 if (ErrorInfo >= Operands.size())
755 return Error(IDLoc, "too few operands for instruction");
756
David Blaikie960ea3f2014-06-08 16:18:35 +0000757 ErrorLoc = ((SystemZOperand &)*Operands[ErrorInfo]).getStartLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000758 if (ErrorLoc == SMLoc())
759 ErrorLoc = IDLoc;
760 }
761 return Error(ErrorLoc, "invalid operand for instruction");
762 }
763
764 case Match_MnemonicFail:
765 return Error(IDLoc, "invalid instruction");
766 }
767
768 llvm_unreachable("Unexpected match type");
769}
770
David Blaikie960ea3f2014-06-08 16:18:35 +0000771SystemZAsmParser::OperandMatchResultTy
772SystemZAsmParser::parseAccessReg(OperandVector &Operands) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000773 if (Parser.getTok().isNot(AsmToken::Percent))
774 return MatchOperand_NoMatch;
775
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000776 Register Reg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000777 if (parseRegister(Reg, RegAccess, nullptr))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000778 return MatchOperand_ParseFail;
779
780 Operands.push_back(SystemZOperand::createAccessReg(Reg.Num,
781 Reg.StartLoc,
782 Reg.EndLoc));
783 return MatchOperand_Success;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000784}
785
David Blaikie960ea3f2014-06-08 16:18:35 +0000786SystemZAsmParser::OperandMatchResultTy
787SystemZAsmParser::parsePCRel(OperandVector &Operands, int64_t MinVal,
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000788 int64_t MaxVal, bool AllowTLS) {
Richard Sandiford1fb58832013-05-14 09:47:26 +0000789 MCContext &Ctx = getContext();
790 MCStreamer &Out = getStreamer();
791 const MCExpr *Expr;
792 SMLoc StartLoc = Parser.getTok().getLoc();
793 if (getParser().parseExpression(Expr))
794 return MatchOperand_NoMatch;
795
796 // For consistency with the GNU assembler, treat immediates as offsets
797 // from ".".
Richard Sandiford21f5d682014-03-06 11:22:58 +0000798 if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
Richard Sandiford1fb58832013-05-14 09:47:26 +0000799 int64_t Value = CE->getValue();
800 if ((Value & 1) || Value < MinVal || Value > MaxVal) {
801 Error(StartLoc, "offset out of range");
802 return MatchOperand_ParseFail;
803 }
804 MCSymbol *Sym = Ctx.CreateTempSymbol();
805 Out.EmitLabel(Sym);
806 const MCExpr *Base = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
807 Ctx);
808 Expr = Value == 0 ? Base : MCBinaryExpr::CreateAdd(Base, Expr, Ctx);
809 }
810
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000811 // Optionally match :tls_gdcall: or :tls_ldcall: followed by a TLS symbol.
812 const MCExpr *Sym = nullptr;
813 if (AllowTLS && getLexer().is(AsmToken::Colon)) {
814 Parser.Lex();
815
816 if (Parser.getTok().isNot(AsmToken::Identifier)) {
817 Error(Parser.getTok().getLoc(), "unexpected token");
818 return MatchOperand_ParseFail;
819 }
820
821 MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
822 StringRef Name = Parser.getTok().getString();
823 if (Name == "tls_gdcall")
824 Kind = MCSymbolRefExpr::VK_TLSGD;
825 else if (Name == "tls_ldcall")
826 Kind = MCSymbolRefExpr::VK_TLSLDM;
827 else {
828 Error(Parser.getTok().getLoc(), "unknown TLS tag");
829 return MatchOperand_ParseFail;
830 }
831 Parser.Lex();
832
833 if (Parser.getTok().isNot(AsmToken::Colon)) {
834 Error(Parser.getTok().getLoc(), "unexpected token");
835 return MatchOperand_ParseFail;
836 }
837 Parser.Lex();
838
839 if (Parser.getTok().isNot(AsmToken::Identifier)) {
840 Error(Parser.getTok().getLoc(), "unexpected token");
841 return MatchOperand_ParseFail;
842 }
843
844 StringRef Identifier = Parser.getTok().getString();
845 Sym = MCSymbolRefExpr::Create(Ctx.GetOrCreateSymbol(Identifier),
846 Kind, Ctx);
847 Parser.Lex();
848 }
849
Richard Sandiford1fb58832013-05-14 09:47:26 +0000850 SMLoc EndLoc =
851 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000852
853 if (AllowTLS)
854 Operands.push_back(SystemZOperand::createImmTLS(Expr, Sym,
855 StartLoc, EndLoc));
856 else
857 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
858
Richard Sandiford1fb58832013-05-14 09:47:26 +0000859 return MatchOperand_Success;
860}
861
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000862// Force static initialization.
863extern "C" void LLVMInitializeSystemZAsmParser() {
864 RegisterMCAsmParser<SystemZAsmParser> X(TheSystemZTarget);
865}