blob: 0955f4a00c7b560db51287b229d02f4fb97a983b [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,
60 KindMem
61 };
62
63 OperandKind Kind;
64 SMLoc StartLoc, EndLoc;
65
66 // A string of length Length, starting at Data.
67 struct TokenOp {
68 const char *Data;
69 unsigned Length;
70 };
71
Richard Sandiford675f8692013-05-24 14:14:38 +000072 // LLVM register Num, which has kind Kind. In some ways it might be
73 // easier for this class to have a register bank (general, floating-point
74 // or access) and a raw register number (0-15). This would postpone the
75 // interpretation of the operand to the add*() methods and avoid the need
76 // for context-dependent parsing. However, we do things the current way
77 // because of the virtual getReg() method, which needs to distinguish
78 // between (say) %r0 used as a single register and %r0 used as a pair.
79 // Context-dependent parsing can also give us slightly better error
80 // messages when invalid pairs like %r1 are used.
Ulrich Weigand5f613df2013-05-06 16:15:19 +000081 struct RegOp {
82 RegisterKind Kind;
83 unsigned Num;
84 };
85
86 // Base + Disp + Index, where Base and Index are LLVM registers or 0.
87 // RegKind says what type the registers have (ADDR32Reg or ADDR64Reg).
Richard Sandiford1d959002013-07-02 14:56:45 +000088 // Length is the operand length for D(L,B)-style operands, otherwise
89 // it is null.
Ulrich Weigand5f613df2013-05-06 16:15:19 +000090 struct MemOp {
91 unsigned Base : 8;
92 unsigned Index : 8;
93 unsigned RegKind : 8;
94 unsigned Unused : 8;
95 const MCExpr *Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +000096 const MCExpr *Length;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000097 };
98
99 union {
100 TokenOp Token;
101 RegOp Reg;
102 unsigned AccessReg;
103 const MCExpr *Imm;
104 MemOp Mem;
105 };
106
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000107 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
108 // Add as immediates when possible. Null MCExpr = 0.
Craig Topper062a2ba2014-04-25 05:30:21 +0000109 if (!Expr)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000110 Inst.addOperand(MCOperand::CreateImm(0));
Richard Sandiford21f5d682014-03-06 11:22:58 +0000111 else if (auto *CE = dyn_cast<MCConstantExpr>(Expr))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000112 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
113 else
114 Inst.addOperand(MCOperand::CreateExpr(Expr));
115 }
116
117public:
David Blaikie960ea3f2014-06-08 16:18:35 +0000118 SystemZOperand(OperandKind kind, SMLoc startLoc, SMLoc endLoc)
119 : Kind(kind), StartLoc(startLoc), EndLoc(endLoc) {}
120
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000121 // Create particular kinds of operand.
David Blaikie960ea3f2014-06-08 16:18:35 +0000122 static std::unique_ptr<SystemZOperand> createInvalid(SMLoc StartLoc,
123 SMLoc EndLoc) {
124 return make_unique<SystemZOperand>(KindInvalid, StartLoc, EndLoc);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000125 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000126 static std::unique_ptr<SystemZOperand> createToken(StringRef Str, SMLoc Loc) {
127 auto Op = make_unique<SystemZOperand>(KindToken, Loc, Loc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000128 Op->Token.Data = Str.data();
129 Op->Token.Length = Str.size();
130 return Op;
131 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000132 static std::unique_ptr<SystemZOperand>
133 createReg(RegisterKind Kind, unsigned Num, SMLoc StartLoc, SMLoc EndLoc) {
134 auto Op = make_unique<SystemZOperand>(KindReg, StartLoc, EndLoc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000135 Op->Reg.Kind = Kind;
136 Op->Reg.Num = Num;
137 return Op;
138 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000139 static std::unique_ptr<SystemZOperand>
140 createAccessReg(unsigned Num, SMLoc StartLoc, SMLoc EndLoc) {
141 auto Op = make_unique<SystemZOperand>(KindAccessReg, StartLoc, EndLoc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000142 Op->AccessReg = Num;
143 return Op;
144 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000145 static std::unique_ptr<SystemZOperand>
146 createImm(const MCExpr *Expr, SMLoc StartLoc, SMLoc EndLoc) {
147 auto Op = make_unique<SystemZOperand>(KindImm, StartLoc, EndLoc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000148 Op->Imm = Expr;
149 return Op;
150 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000151 static std::unique_ptr<SystemZOperand>
152 createMem(RegisterKind RegKind, unsigned Base, const MCExpr *Disp,
153 unsigned Index, const MCExpr *Length, SMLoc StartLoc,
154 SMLoc EndLoc) {
155 auto Op = make_unique<SystemZOperand>(KindMem, StartLoc, EndLoc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000156 Op->Mem.RegKind = RegKind;
157 Op->Mem.Base = Base;
158 Op->Mem.Index = Index;
159 Op->Mem.Disp = Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +0000160 Op->Mem.Length = Length;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000161 return Op;
162 }
163
164 // Token operands
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000165 bool isToken() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000166 return Kind == KindToken;
167 }
168 StringRef getToken() const {
169 assert(Kind == KindToken && "Not a token");
170 return StringRef(Token.Data, Token.Length);
171 }
172
173 // Register operands.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000174 bool isReg() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000175 return Kind == KindReg;
176 }
177 bool isReg(RegisterKind RegKind) const {
178 return Kind == KindReg && Reg.Kind == RegKind;
179 }
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000180 unsigned getReg() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000181 assert(Kind == KindReg && "Not a register");
182 return Reg.Num;
183 }
184
185 // Access register operands. Access registers aren't exposed to LLVM
186 // as registers.
187 bool isAccessReg() const {
188 return Kind == KindAccessReg;
189 }
190
191 // Immediate operands.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000192 bool isImm() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000193 return Kind == KindImm;
194 }
195 bool isImm(int64_t MinValue, int64_t MaxValue) const {
196 return Kind == KindImm && inRange(Imm, MinValue, MaxValue);
197 }
198 const MCExpr *getImm() const {
199 assert(Kind == KindImm && "Not an immediate");
200 return Imm;
201 }
202
203 // Memory operands.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000204 bool isMem() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000205 return Kind == KindMem;
206 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000207 bool isMem(RegisterKind RegKind, MemoryKind MemKind) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000208 return (Kind == KindMem &&
209 Mem.RegKind == RegKind &&
Richard Sandiford1d959002013-07-02 14:56:45 +0000210 (MemKind == BDXMem || !Mem.Index) &&
Craig Topper062a2ba2014-04-25 05:30:21 +0000211 (MemKind == BDLMem) == (Mem.Length != nullptr));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000212 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000213 bool isMemDisp12(RegisterKind RegKind, MemoryKind MemKind) const {
214 return isMem(RegKind, MemKind) && inRange(Mem.Disp, 0, 0xfff);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000215 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000216 bool isMemDisp20(RegisterKind RegKind, MemoryKind MemKind) const {
217 return isMem(RegKind, MemKind) && inRange(Mem.Disp, -524288, 524287);
218 }
219 bool isMemDisp12Len8(RegisterKind RegKind) const {
220 return isMemDisp12(RegKind, BDLMem) && inRange(Mem.Length, 1, 0x100);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000221 }
222
223 // Override MCParsedAsmOperand.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000224 SMLoc getStartLoc() const override { return StartLoc; }
225 SMLoc getEndLoc() const override { return EndLoc; }
226 void print(raw_ostream &OS) const override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000227
228 // Used by the TableGen code to add particular types of operand
229 // to an instruction.
230 void addRegOperands(MCInst &Inst, unsigned N) const {
231 assert(N == 1 && "Invalid number of operands");
232 Inst.addOperand(MCOperand::CreateReg(getReg()));
233 }
234 void addAccessRegOperands(MCInst &Inst, unsigned N) const {
235 assert(N == 1 && "Invalid number of operands");
236 assert(Kind == KindAccessReg && "Invalid operand type");
237 Inst.addOperand(MCOperand::CreateImm(AccessReg));
238 }
239 void addImmOperands(MCInst &Inst, unsigned N) const {
240 assert(N == 1 && "Invalid number of operands");
241 addExpr(Inst, getImm());
242 }
243 void addBDAddrOperands(MCInst &Inst, unsigned N) const {
244 assert(N == 2 && "Invalid number of operands");
245 assert(Kind == KindMem && Mem.Index == 0 && "Invalid operand type");
246 Inst.addOperand(MCOperand::CreateReg(Mem.Base));
247 addExpr(Inst, Mem.Disp);
248 }
249 void addBDXAddrOperands(MCInst &Inst, unsigned N) const {
250 assert(N == 3 && "Invalid number of operands");
251 assert(Kind == KindMem && "Invalid operand type");
252 Inst.addOperand(MCOperand::CreateReg(Mem.Base));
253 addExpr(Inst, Mem.Disp);
254 Inst.addOperand(MCOperand::CreateReg(Mem.Index));
255 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000256 void addBDLAddrOperands(MCInst &Inst, unsigned N) const {
257 assert(N == 3 && "Invalid number of operands");
258 assert(Kind == KindMem && "Invalid operand type");
259 Inst.addOperand(MCOperand::CreateReg(Mem.Base));
260 addExpr(Inst, Mem.Disp);
261 addExpr(Inst, Mem.Length);
262 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000263
264 // Used by the TableGen code to check for particular operand types.
265 bool isGR32() const { return isReg(GR32Reg); }
Richard Sandifordf9496062013-09-30 10:45:16 +0000266 bool isGRH32() const { return isReg(GRH32Reg); }
Richard Sandiford0755c932013-10-01 11:26:28 +0000267 bool isGRX32() const { return false; }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000268 bool isGR64() const { return isReg(GR64Reg); }
269 bool isGR128() const { return isReg(GR128Reg); }
270 bool isADDR32() const { return isReg(ADDR32Reg); }
271 bool isADDR64() const { return isReg(ADDR64Reg); }
272 bool isADDR128() const { return false; }
273 bool isFP32() const { return isReg(FP32Reg); }
274 bool isFP64() const { return isReg(FP64Reg); }
275 bool isFP128() const { return isReg(FP128Reg); }
Richard Sandiford1d959002013-07-02 14:56:45 +0000276 bool isBDAddr32Disp12() const { return isMemDisp12(ADDR32Reg, BDMem); }
277 bool isBDAddr32Disp20() const { return isMemDisp20(ADDR32Reg, BDMem); }
278 bool isBDAddr64Disp12() const { return isMemDisp12(ADDR64Reg, BDMem); }
279 bool isBDAddr64Disp20() const { return isMemDisp20(ADDR64Reg, BDMem); }
280 bool isBDXAddr64Disp12() const { return isMemDisp12(ADDR64Reg, BDXMem); }
281 bool isBDXAddr64Disp20() const { return isMemDisp20(ADDR64Reg, BDXMem); }
282 bool isBDLAddr64Disp12Len8() const { return isMemDisp12Len8(ADDR64Reg); }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000283 bool isU4Imm() const { return isImm(0, 15); }
284 bool isU6Imm() const { return isImm(0, 63); }
285 bool isU8Imm() const { return isImm(0, 255); }
286 bool isS8Imm() const { return isImm(-128, 127); }
287 bool isU16Imm() const { return isImm(0, 65535); }
288 bool isS16Imm() const { return isImm(-32768, 32767); }
289 bool isU32Imm() const { return isImm(0, (1LL << 32) - 1); }
290 bool isS32Imm() const { return isImm(-(1LL << 31), (1LL << 31) - 1); }
291};
292
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000293class SystemZAsmParser : public MCTargetAsmParser {
294#define GET_ASSEMBLER_HEADER
295#include "SystemZGenAsmMatcher.inc"
296
297private:
298 MCSubtargetInfo &STI;
299 MCAsmParser &Parser;
Richard Sandiford675f8692013-05-24 14:14:38 +0000300 enum RegisterGroup {
301 RegGR,
302 RegFP,
303 RegAccess
304 };
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000305 struct Register {
Richard Sandiford675f8692013-05-24 14:14:38 +0000306 RegisterGroup Group;
307 unsigned Num;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000308 SMLoc StartLoc, EndLoc;
309 };
310
311 bool parseRegister(Register &Reg);
312
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000313 bool parseRegister(Register &Reg, RegisterGroup Group, const unsigned *Regs,
314 bool IsAddress = false);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000315
David Blaikie960ea3f2014-06-08 16:18:35 +0000316 OperandMatchResultTy parseRegister(OperandVector &Operands,
317 RegisterGroup Group, const unsigned *Regs,
318 RegisterKind Kind);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000319
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000320 bool parseAddress(unsigned &Base, const MCExpr *&Disp,
Richard Sandiford1d959002013-07-02 14:56:45 +0000321 unsigned &Index, const MCExpr *&Length,
322 const unsigned *Regs, RegisterKind RegKind);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000323
David Blaikie960ea3f2014-06-08 16:18:35 +0000324 OperandMatchResultTy parseAddress(OperandVector &Operands,
325 const unsigned *Regs, RegisterKind RegKind,
326 MemoryKind MemKind);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000327
David Blaikie960ea3f2014-06-08 16:18:35 +0000328 bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000329
330public:
Joey Gouly0e76fa72013-09-12 10:28:05 +0000331 SystemZAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000332 const MCInstrInfo &MII,
333 const MCTargetOptions &Options)
Joey Gouly0e76fa72013-09-12 10:28:05 +0000334 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000335 MCAsmParserExtension::Initialize(Parser);
336
337 // Initialize the set of available features.
338 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
339 }
340
341 // Override MCTargetAsmParser.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000342 bool ParseDirective(AsmToken DirectiveID) override;
343 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
David Blaikie960ea3f2014-06-08 16:18:35 +0000344 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
345 SMLoc NameLoc, OperandVector &Operands) override;
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000346 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
David Blaikie960ea3f2014-06-08 16:18:35 +0000347 OperandVector &Operands, MCStreamer &Out,
Tim Northover26bb14e2014-08-18 11:49:42 +0000348 uint64_t &ErrorInfo,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000349 bool MatchingInlineAsm) override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000350
351 // Used by the TableGen code to parse particular operand types.
David Blaikie960ea3f2014-06-08 16:18:35 +0000352 OperandMatchResultTy parseGR32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000353 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, GR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000354 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000355 OperandMatchResultTy parseGRH32(OperandVector &Operands) {
Richard Sandifordf9496062013-09-30 10:45:16 +0000356 return parseRegister(Operands, RegGR, SystemZMC::GRH32Regs, GRH32Reg);
357 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000358 OperandMatchResultTy parseGRX32(OperandVector &Operands) {
Richard Sandiford0755c932013-10-01 11:26:28 +0000359 llvm_unreachable("GRX32 should only be used for pseudo instructions");
360 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000361 OperandMatchResultTy parseGR64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000362 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, GR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000363 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000364 OperandMatchResultTy parseGR128(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000365 return parseRegister(Operands, RegGR, SystemZMC::GR128Regs, GR128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000366 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000367 OperandMatchResultTy parseADDR32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000368 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, ADDR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000369 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000370 OperandMatchResultTy parseADDR64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000371 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000372 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000373 OperandMatchResultTy parseADDR128(OperandVector &Operands) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000374 llvm_unreachable("Shouldn't be used as an operand");
375 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000376 OperandMatchResultTy parseFP32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000377 return parseRegister(Operands, RegFP, SystemZMC::FP32Regs, FP32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000378 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000379 OperandMatchResultTy parseFP64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000380 return parseRegister(Operands, RegFP, SystemZMC::FP64Regs, FP64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000381 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000382 OperandMatchResultTy parseFP128(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000383 return parseRegister(Operands, RegFP, SystemZMC::FP128Regs, FP128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000384 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000385 OperandMatchResultTy parseBDAddr32(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000386 return parseAddress(Operands, SystemZMC::GR32Regs, ADDR32Reg, BDMem);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000387 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000388 OperandMatchResultTy parseBDAddr64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000389 return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDMem);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000390 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000391 OperandMatchResultTy parseBDXAddr64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000392 return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDXMem);
393 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000394 OperandMatchResultTy parseBDLAddr64(OperandVector &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000395 return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDLMem);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000396 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000397 OperandMatchResultTy parseAccessReg(OperandVector &Operands);
398 OperandMatchResultTy parsePCRel(OperandVector &Operands, int64_t MinVal,
399 int64_t MaxVal);
400 OperandMatchResultTy parsePCRel16(OperandVector &Operands) {
Richard Sandiford1fb58832013-05-14 09:47:26 +0000401 return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1);
402 }
David Blaikie960ea3f2014-06-08 16:18:35 +0000403 OperandMatchResultTy parsePCRel32(OperandVector &Operands) {
Richard Sandiford1fb58832013-05-14 09:47:26 +0000404 return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1);
405 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000406};
Richard Sandifordc2312692014-03-06 10:38:30 +0000407} // end anonymous namespace
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000408
409#define GET_REGISTER_MATCHER
410#define GET_SUBTARGET_FEATURE_NAME
411#define GET_MATCHER_IMPLEMENTATION
412#include "SystemZGenAsmMatcher.inc"
413
414void SystemZOperand::print(raw_ostream &OS) const {
415 llvm_unreachable("Not implemented");
416}
417
418// Parse one register of the form %<prefix><number>.
419bool SystemZAsmParser::parseRegister(Register &Reg) {
420 Reg.StartLoc = Parser.getTok().getLoc();
421
422 // Eat the % prefix.
423 if (Parser.getTok().isNot(AsmToken::Percent))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000424 return Error(Parser.getTok().getLoc(), "register expected");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000425 Parser.Lex();
426
427 // Expect a register name.
428 if (Parser.getTok().isNot(AsmToken::Identifier))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000429 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000430
Richard Sandiford675f8692013-05-24 14:14:38 +0000431 // Check that there's a prefix.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000432 StringRef Name = Parser.getTok().getString();
433 if (Name.size() < 2)
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000434 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000435 char Prefix = Name[0];
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000436
437 // Treat the rest of the register name as a register number.
Richard Sandiford675f8692013-05-24 14:14:38 +0000438 if (Name.substr(1).getAsInteger(10, Reg.Num))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000439 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000440
441 // Look for valid combinations of prefix and number.
442 if (Prefix == 'r' && Reg.Num < 16)
443 Reg.Group = RegGR;
444 else if (Prefix == 'f' && Reg.Num < 16)
445 Reg.Group = RegFP;
446 else if (Prefix == 'a' && Reg.Num < 16)
447 Reg.Group = RegAccess;
448 else
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000449 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000450
451 Reg.EndLoc = Parser.getTok().getLoc();
452 Parser.Lex();
453 return false;
454}
455
Richard Sandiford675f8692013-05-24 14:14:38 +0000456// Parse a register of group Group. If Regs is nonnull, use it to map
457// the raw register number to LLVM numbering, with zero entries indicating
458// an invalid register. IsAddress says whether the register appears in an
459// address context.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000460bool SystemZAsmParser::parseRegister(Register &Reg, RegisterGroup Group,
461 const unsigned *Regs, bool IsAddress) {
462 if (parseRegister(Reg))
463 return true;
464 if (Reg.Group != Group)
465 return Error(Reg.StartLoc, "invalid operand for instruction");
466 if (Regs && Regs[Reg.Num] == 0)
467 return Error(Reg.StartLoc, "invalid register pair");
468 if (Reg.Num == 0 && IsAddress)
469 return Error(Reg.StartLoc, "%r0 used in an address");
Richard Sandiford675f8692013-05-24 14:14:38 +0000470 if (Regs)
471 Reg.Num = Regs[Reg.Num];
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000472 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000473}
474
Richard Sandiford675f8692013-05-24 14:14:38 +0000475// Parse a register and add it to Operands. The other arguments are as above.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000476SystemZAsmParser::OperandMatchResultTy
David Blaikie960ea3f2014-06-08 16:18:35 +0000477SystemZAsmParser::parseRegister(OperandVector &Operands, RegisterGroup Group,
478 const unsigned *Regs, RegisterKind Kind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000479 if (Parser.getTok().isNot(AsmToken::Percent))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000480 return MatchOperand_NoMatch;
481
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000482 Register Reg;
Richard Sandiford1d959002013-07-02 14:56:45 +0000483 bool IsAddress = (Kind == ADDR32Reg || Kind == ADDR64Reg);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000484 if (parseRegister(Reg, Group, Regs, IsAddress))
485 return MatchOperand_ParseFail;
486
487 Operands.push_back(SystemZOperand::createReg(Kind, Reg.Num,
488 Reg.StartLoc, Reg.EndLoc));
489 return MatchOperand_Success;
490}
491
Richard Sandiford1d959002013-07-02 14:56:45 +0000492// Parse a memory operand into Base, Disp, Index and Length.
493// Regs maps asm register numbers to LLVM register numbers and RegKind
494// says what kind of address register we're using (ADDR32Reg or ADDR64Reg).
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000495bool SystemZAsmParser::parseAddress(unsigned &Base, const MCExpr *&Disp,
Richard Sandiford1d959002013-07-02 14:56:45 +0000496 unsigned &Index, const MCExpr *&Length,
497 const unsigned *Regs,
498 RegisterKind RegKind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000499 // Parse the displacement, which must always be present.
500 if (getParser().parseExpression(Disp))
501 return true;
502
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000503 // Parse the optional base and index.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000504 Index = 0;
505 Base = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000506 Length = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000507 if (getLexer().is(AsmToken::LParen)) {
508 Parser.Lex();
509
Richard Sandiford1d959002013-07-02 14:56:45 +0000510 if (getLexer().is(AsmToken::Percent)) {
511 // Parse the first register and decide whether it's a base or an index.
512 Register Reg;
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000513 if (parseRegister(Reg, RegGR, Regs, RegKind))
514 return true;
Richard Sandiford1d959002013-07-02 14:56:45 +0000515 if (getLexer().is(AsmToken::Comma))
516 Index = Reg.Num;
517 else
518 Base = Reg.Num;
519 } else {
520 // Parse the length.
521 if (getParser().parseExpression(Length))
522 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000523 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000524
525 // Check whether there's a second register. It's the base if so.
526 if (getLexer().is(AsmToken::Comma)) {
527 Parser.Lex();
528 Register Reg;
529 if (parseRegister(Reg, RegGR, Regs, RegKind))
530 return true;
531 Base = Reg.Num;
532 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000533
534 // Consume the closing bracket.
535 if (getLexer().isNot(AsmToken::RParen))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000536 return Error(Parser.getTok().getLoc(), "unexpected token in address");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000537 Parser.Lex();
538 }
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000539 return false;
540}
541
542// Parse a memory operand and add it to Operands. The other arguments
543// are as above.
544SystemZAsmParser::OperandMatchResultTy
David Blaikie960ea3f2014-06-08 16:18:35 +0000545SystemZAsmParser::parseAddress(OperandVector &Operands, const unsigned *Regs,
546 RegisterKind RegKind, MemoryKind MemKind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000547 SMLoc StartLoc = Parser.getTok().getLoc();
548 unsigned Base, Index;
549 const MCExpr *Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +0000550 const MCExpr *Length;
551 if (parseAddress(Base, Disp, Index, Length, Regs, RegKind))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000552 return MatchOperand_ParseFail;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000553
Richard Sandiford1d959002013-07-02 14:56:45 +0000554 if (Index && MemKind != BDXMem)
555 {
556 Error(StartLoc, "invalid use of indexed addressing");
557 return MatchOperand_ParseFail;
558 }
559
560 if (Length && MemKind != BDLMem)
561 {
562 Error(StartLoc, "invalid use of length addressing");
563 return MatchOperand_ParseFail;
564 }
565
566 if (!Length && MemKind == BDLMem)
567 {
568 Error(StartLoc, "missing length in address");
569 return MatchOperand_ParseFail;
570 }
571
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000572 SMLoc EndLoc =
573 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
574 Operands.push_back(SystemZOperand::createMem(RegKind, Base, Disp, Index,
Richard Sandiford1d959002013-07-02 14:56:45 +0000575 Length, StartLoc, EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000576 return MatchOperand_Success;
577}
578
579bool SystemZAsmParser::ParseDirective(AsmToken DirectiveID) {
580 return true;
581}
582
583bool SystemZAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
584 SMLoc &EndLoc) {
585 Register Reg;
586 if (parseRegister(Reg))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000587 return true;
Richard Sandiford675f8692013-05-24 14:14:38 +0000588 if (Reg.Group == RegGR)
589 RegNo = SystemZMC::GR64Regs[Reg.Num];
590 else if (Reg.Group == RegFP)
591 RegNo = SystemZMC::FP64Regs[Reg.Num];
592 else
593 // FIXME: Access registers aren't modelled as LLVM registers yet.
594 return Error(Reg.StartLoc, "invalid operand for instruction");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000595 StartLoc = Reg.StartLoc;
596 EndLoc = Reg.EndLoc;
597 return false;
598}
599
David Blaikie960ea3f2014-06-08 16:18:35 +0000600bool SystemZAsmParser::ParseInstruction(ParseInstructionInfo &Info,
601 StringRef Name, SMLoc NameLoc,
602 OperandVector &Operands) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000603 Operands.push_back(SystemZOperand::createToken(Name, NameLoc));
604
605 // Read the remaining operands.
606 if (getLexer().isNot(AsmToken::EndOfStatement)) {
607 // Read the first operand.
608 if (parseOperand(Operands, Name)) {
609 Parser.eatToEndOfStatement();
610 return true;
611 }
612
613 // Read any subsequent operands.
614 while (getLexer().is(AsmToken::Comma)) {
615 Parser.Lex();
616 if (parseOperand(Operands, Name)) {
617 Parser.eatToEndOfStatement();
618 return true;
619 }
620 }
621 if (getLexer().isNot(AsmToken::EndOfStatement)) {
622 SMLoc Loc = getLexer().getLoc();
623 Parser.eatToEndOfStatement();
624 return Error(Loc, "unexpected token in argument list");
625 }
626 }
627
628 // Consume the EndOfStatement.
629 Parser.Lex();
630 return false;
631}
632
David Blaikie960ea3f2014-06-08 16:18:35 +0000633bool SystemZAsmParser::parseOperand(OperandVector &Operands,
634 StringRef Mnemonic) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000635 // Check if the current operand has a custom associated parser, if so, try to
636 // custom parse the operand, or fallback to the general approach.
637 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
638 if (ResTy == MatchOperand_Success)
639 return false;
640
641 // If there wasn't a custom match, try the generic matcher below. Otherwise,
642 // there was a match, but an error occurred, in which case, just return that
643 // the operand parsing failed.
644 if (ResTy == MatchOperand_ParseFail)
645 return true;
646
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000647 // Check for a register. All real register operands should have used
648 // a context-dependent parse routine, which gives the required register
649 // class. The code is here to mop up other cases, like those where
650 // the instruction isn't recognized.
651 if (Parser.getTok().is(AsmToken::Percent)) {
652 Register Reg;
653 if (parseRegister(Reg))
654 return true;
655 Operands.push_back(SystemZOperand::createInvalid(Reg.StartLoc, Reg.EndLoc));
656 return false;
657 }
658
659 // The only other type of operand is an immediate or address. As above,
660 // real address operands should have used a context-dependent parse routine,
661 // so we treat any plain expression as an immediate.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000662 SMLoc StartLoc = Parser.getTok().getLoc();
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000663 unsigned Base, Index;
Richard Sandiford1d959002013-07-02 14:56:45 +0000664 const MCExpr *Expr, *Length;
665 if (parseAddress(Base, Expr, Index, Length, SystemZMC::GR64Regs, ADDR64Reg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000666 return true;
667
668 SMLoc EndLoc =
669 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Richard Sandiford1d959002013-07-02 14:56:45 +0000670 if (Base || Index || Length)
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000671 Operands.push_back(SystemZOperand::createInvalid(StartLoc, EndLoc));
672 else
673 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000674 return false;
675}
676
David Blaikie960ea3f2014-06-08 16:18:35 +0000677bool SystemZAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
678 OperandVector &Operands,
679 MCStreamer &Out,
Tim Northover26bb14e2014-08-18 11:49:42 +0000680 uint64_t &ErrorInfo,
David Blaikie960ea3f2014-06-08 16:18:35 +0000681 bool MatchingInlineAsm) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000682 MCInst Inst;
683 unsigned MatchResult;
684
685 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
686 MatchingInlineAsm);
687 switch (MatchResult) {
688 default: break;
689 case Match_Success:
690 Inst.setLoc(IDLoc);
David Woodhousee6c13e42014-01-28 23:12:42 +0000691 Out.EmitInstruction(Inst, STI);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000692 return false;
693
694 case Match_MissingFeature: {
695 assert(ErrorInfo && "Unknown missing feature!");
696 // Special case the error message for the very common case where only
697 // a single subtarget feature is missing
698 std::string Msg = "instruction requires:";
Tim Northover26bb14e2014-08-18 11:49:42 +0000699 uint64_t Mask = 1;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000700 for (unsigned I = 0; I < sizeof(ErrorInfo) * 8 - 1; ++I) {
701 if (ErrorInfo & Mask) {
702 Msg += " ";
703 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
704 }
705 Mask <<= 1;
706 }
707 return Error(IDLoc, Msg);
708 }
709
710 case Match_InvalidOperand: {
711 SMLoc ErrorLoc = IDLoc;
Tim Northover26bb14e2014-08-18 11:49:42 +0000712 if (ErrorInfo != ~0ULL) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000713 if (ErrorInfo >= Operands.size())
714 return Error(IDLoc, "too few operands for instruction");
715
David Blaikie960ea3f2014-06-08 16:18:35 +0000716 ErrorLoc = ((SystemZOperand &)*Operands[ErrorInfo]).getStartLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000717 if (ErrorLoc == SMLoc())
718 ErrorLoc = IDLoc;
719 }
720 return Error(ErrorLoc, "invalid operand for instruction");
721 }
722
723 case Match_MnemonicFail:
724 return Error(IDLoc, "invalid instruction");
725 }
726
727 llvm_unreachable("Unexpected match type");
728}
729
David Blaikie960ea3f2014-06-08 16:18:35 +0000730SystemZAsmParser::OperandMatchResultTy
731SystemZAsmParser::parseAccessReg(OperandVector &Operands) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000732 if (Parser.getTok().isNot(AsmToken::Percent))
733 return MatchOperand_NoMatch;
734
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000735 Register Reg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000736 if (parseRegister(Reg, RegAccess, nullptr))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000737 return MatchOperand_ParseFail;
738
739 Operands.push_back(SystemZOperand::createAccessReg(Reg.Num,
740 Reg.StartLoc,
741 Reg.EndLoc));
742 return MatchOperand_Success;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000743}
744
David Blaikie960ea3f2014-06-08 16:18:35 +0000745SystemZAsmParser::OperandMatchResultTy
746SystemZAsmParser::parsePCRel(OperandVector &Operands, int64_t MinVal,
747 int64_t MaxVal) {
Richard Sandiford1fb58832013-05-14 09:47:26 +0000748 MCContext &Ctx = getContext();
749 MCStreamer &Out = getStreamer();
750 const MCExpr *Expr;
751 SMLoc StartLoc = Parser.getTok().getLoc();
752 if (getParser().parseExpression(Expr))
753 return MatchOperand_NoMatch;
754
755 // For consistency with the GNU assembler, treat immediates as offsets
756 // from ".".
Richard Sandiford21f5d682014-03-06 11:22:58 +0000757 if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
Richard Sandiford1fb58832013-05-14 09:47:26 +0000758 int64_t Value = CE->getValue();
759 if ((Value & 1) || Value < MinVal || Value > MaxVal) {
760 Error(StartLoc, "offset out of range");
761 return MatchOperand_ParseFail;
762 }
763 MCSymbol *Sym = Ctx.CreateTempSymbol();
764 Out.EmitLabel(Sym);
765 const MCExpr *Base = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
766 Ctx);
767 Expr = Value == 0 ? Base : MCBinaryExpr::CreateAdd(Base, Expr, Ctx);
768 }
769
770 SMLoc EndLoc =
771 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
772 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
773 return MatchOperand_Success;
774}
775
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000776// Force static initialization.
777extern "C" void LLVMInitializeSystemZAsmParser() {
778 RegisterMCAsmParser<SystemZAsmParser> X(TheSystemZTarget);
779}