blob: 799d22bc38b333af69033490ab43d209e6414d20 [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
107 SystemZOperand(OperandKind kind, SMLoc startLoc, SMLoc endLoc)
108 : Kind(kind), StartLoc(startLoc), EndLoc(endLoc)
109 {}
110
111 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
112 // Add as immediates when possible. Null MCExpr = 0.
113 if (Expr == 0)
114 Inst.addOperand(MCOperand::CreateImm(0));
Richard Sandiford21f5d682014-03-06 11:22:58 +0000115 else if (auto *CE = dyn_cast<MCConstantExpr>(Expr))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000116 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
117 else
118 Inst.addOperand(MCOperand::CreateExpr(Expr));
119 }
120
121public:
122 // Create particular kinds of operand.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000123 static SystemZOperand *createInvalid(SMLoc StartLoc, SMLoc EndLoc) {
124 return new SystemZOperand(KindInvalid, StartLoc, EndLoc);
125 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000126 static SystemZOperand *createToken(StringRef Str, SMLoc Loc) {
127 SystemZOperand *Op = new SystemZOperand(KindToken, Loc, Loc);
128 Op->Token.Data = Str.data();
129 Op->Token.Length = Str.size();
130 return Op;
131 }
132 static SystemZOperand *createReg(RegisterKind Kind, unsigned Num,
133 SMLoc StartLoc, SMLoc EndLoc) {
134 SystemZOperand *Op = new SystemZOperand(KindReg, StartLoc, EndLoc);
135 Op->Reg.Kind = Kind;
136 Op->Reg.Num = Num;
137 return Op;
138 }
139 static SystemZOperand *createAccessReg(unsigned Num, SMLoc StartLoc,
140 SMLoc EndLoc) {
141 SystemZOperand *Op = new SystemZOperand(KindAccessReg, StartLoc, EndLoc);
142 Op->AccessReg = Num;
143 return Op;
144 }
145 static SystemZOperand *createImm(const MCExpr *Expr, SMLoc StartLoc,
146 SMLoc EndLoc) {
147 SystemZOperand *Op = new SystemZOperand(KindImm, StartLoc, EndLoc);
148 Op->Imm = Expr;
149 return Op;
150 }
151 static SystemZOperand *createMem(RegisterKind RegKind, unsigned Base,
152 const MCExpr *Disp, unsigned Index,
Richard Sandiford1d959002013-07-02 14:56:45 +0000153 const MCExpr *Length, SMLoc StartLoc,
154 SMLoc EndLoc) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000155 SystemZOperand *Op = new SystemZOperand(KindMem, StartLoc, EndLoc);
156 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
Craig Topper73156022014-03-02 09:09:27 +0000165 virtual 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.
Craig Topper73156022014-03-02 09:09:27 +0000174 virtual 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 }
Craig Topper73156022014-03-02 09:09:27 +0000180 virtual 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.
Craig Topper73156022014-03-02 09:09:27 +0000192 virtual 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.
Craig Topper73156022014-03-02 09:09:27 +0000204 virtual 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) &&
211 (MemKind == BDLMem) == (Mem.Length != 0));
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.
Craig Topper73156022014-03-02 09:09:27 +0000224 virtual SMLoc getStartLoc() const override { return StartLoc; }
225 virtual SMLoc getEndLoc() const override { return EndLoc; }
226 virtual 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
316 OperandMatchResultTy
317 parseRegister(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Richard Sandiford1d959002013-07-02 14:56:45 +0000318 RegisterGroup Group, const unsigned *Regs, 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
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000324 OperandMatchResultTy
325 parseAddress(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Richard Sandiford1d959002013-07-02 14:56:45 +0000326 const unsigned *Regs, RegisterKind RegKind,
327 MemoryKind MemKind);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000328
329 bool parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
330 StringRef Mnemonic);
331
332public:
Joey Gouly0e76fa72013-09-12 10:28:05 +0000333 SystemZAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
334 const MCInstrInfo &MII)
335 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000336 MCAsmParserExtension::Initialize(Parser);
337
338 // Initialize the set of available features.
339 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
340 }
341
342 // Override MCTargetAsmParser.
Craig Topper73156022014-03-02 09:09:27 +0000343 virtual bool ParseDirective(AsmToken DirectiveID) override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000344 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
Craig Topper73156022014-03-02 09:09:27 +0000345 SMLoc &EndLoc) override;
346 virtual bool
347 ParseInstruction(ParseInstructionInfo &Info,
348 StringRef Name, SMLoc NameLoc,
349 SmallVectorImpl<MCParsedAsmOperand*> &Operands) override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000350 virtual bool
351 MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
352 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
353 MCStreamer &Out, unsigned &ErrorInfo,
Craig Topper73156022014-03-02 09:09:27 +0000354 bool MatchingInlineAsm) override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000355
356 // Used by the TableGen code to parse particular operand types.
357 OperandMatchResultTy
358 parseGR32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000359 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, GR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000360 }
361 OperandMatchResultTy
Richard Sandifordf9496062013-09-30 10:45:16 +0000362 parseGRH32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
363 return parseRegister(Operands, RegGR, SystemZMC::GRH32Regs, GRH32Reg);
364 }
365 OperandMatchResultTy
Richard Sandiford0755c932013-10-01 11:26:28 +0000366 parseGRX32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
367 llvm_unreachable("GRX32 should only be used for pseudo instructions");
368 }
369 OperandMatchResultTy
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000370 parseGR64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000371 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, GR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000372 }
373 OperandMatchResultTy
374 parseGR128(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000375 return parseRegister(Operands, RegGR, SystemZMC::GR128Regs, GR128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000376 }
377 OperandMatchResultTy
378 parseADDR32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000379 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, ADDR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000380 }
381 OperandMatchResultTy
382 parseADDR64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000383 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000384 }
385 OperandMatchResultTy
386 parseADDR128(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
387 llvm_unreachable("Shouldn't be used as an operand");
388 }
389 OperandMatchResultTy
390 parseFP32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000391 return parseRegister(Operands, RegFP, SystemZMC::FP32Regs, FP32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000392 }
393 OperandMatchResultTy
394 parseFP64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000395 return parseRegister(Operands, RegFP, SystemZMC::FP64Regs, FP64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000396 }
397 OperandMatchResultTy
398 parseFP128(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000399 return parseRegister(Operands, RegFP, SystemZMC::FP128Regs, FP128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000400 }
401 OperandMatchResultTy
402 parseBDAddr32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000403 return parseAddress(Operands, SystemZMC::GR32Regs, ADDR32Reg, BDMem);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000404 }
405 OperandMatchResultTy
406 parseBDAddr64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000407 return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDMem);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000408 }
409 OperandMatchResultTy
410 parseBDXAddr64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000411 return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDXMem);
412 }
413 OperandMatchResultTy
414 parseBDLAddr64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
415 return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDLMem);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000416 }
417 OperandMatchResultTy
418 parseAccessReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Richard Sandiford1fb58832013-05-14 09:47:26 +0000419 OperandMatchResultTy
420 parsePCRel(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
421 int64_t MinVal, int64_t MaxVal);
422 OperandMatchResultTy
423 parsePCRel16(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
424 return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1);
425 }
426 OperandMatchResultTy
427 parsePCRel32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
428 return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1);
429 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000430};
Richard Sandifordc2312692014-03-06 10:38:30 +0000431} // end anonymous namespace
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000432
433#define GET_REGISTER_MATCHER
434#define GET_SUBTARGET_FEATURE_NAME
435#define GET_MATCHER_IMPLEMENTATION
436#include "SystemZGenAsmMatcher.inc"
437
438void SystemZOperand::print(raw_ostream &OS) const {
439 llvm_unreachable("Not implemented");
440}
441
442// Parse one register of the form %<prefix><number>.
443bool SystemZAsmParser::parseRegister(Register &Reg) {
444 Reg.StartLoc = Parser.getTok().getLoc();
445
446 // Eat the % prefix.
447 if (Parser.getTok().isNot(AsmToken::Percent))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000448 return Error(Parser.getTok().getLoc(), "register expected");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000449 Parser.Lex();
450
451 // Expect a register name.
452 if (Parser.getTok().isNot(AsmToken::Identifier))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000453 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000454
Richard Sandiford675f8692013-05-24 14:14:38 +0000455 // Check that there's a prefix.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000456 StringRef Name = Parser.getTok().getString();
457 if (Name.size() < 2)
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000458 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000459 char Prefix = Name[0];
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000460
461 // Treat the rest of the register name as a register number.
Richard Sandiford675f8692013-05-24 14:14:38 +0000462 if (Name.substr(1).getAsInteger(10, Reg.Num))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000463 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000464
465 // Look for valid combinations of prefix and number.
466 if (Prefix == 'r' && Reg.Num < 16)
467 Reg.Group = RegGR;
468 else if (Prefix == 'f' && Reg.Num < 16)
469 Reg.Group = RegFP;
470 else if (Prefix == 'a' && Reg.Num < 16)
471 Reg.Group = RegAccess;
472 else
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000473 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000474
475 Reg.EndLoc = Parser.getTok().getLoc();
476 Parser.Lex();
477 return false;
478}
479
Richard Sandiford675f8692013-05-24 14:14:38 +0000480// Parse a register of group Group. If Regs is nonnull, use it to map
481// the raw register number to LLVM numbering, with zero entries indicating
482// an invalid register. IsAddress says whether the register appears in an
483// address context.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000484bool SystemZAsmParser::parseRegister(Register &Reg, RegisterGroup Group,
485 const unsigned *Regs, bool IsAddress) {
486 if (parseRegister(Reg))
487 return true;
488 if (Reg.Group != Group)
489 return Error(Reg.StartLoc, "invalid operand for instruction");
490 if (Regs && Regs[Reg.Num] == 0)
491 return Error(Reg.StartLoc, "invalid register pair");
492 if (Reg.Num == 0 && IsAddress)
493 return Error(Reg.StartLoc, "%r0 used in an address");
Richard Sandiford675f8692013-05-24 14:14:38 +0000494 if (Regs)
495 Reg.Num = Regs[Reg.Num];
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000496 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000497}
498
Richard Sandiford675f8692013-05-24 14:14:38 +0000499// Parse a register and add it to Operands. The other arguments are as above.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000500SystemZAsmParser::OperandMatchResultTy
501SystemZAsmParser::parseRegister(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Richard Sandiford675f8692013-05-24 14:14:38 +0000502 RegisterGroup Group, const unsigned *Regs,
Richard Sandiford1d959002013-07-02 14:56:45 +0000503 RegisterKind Kind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000504 if (Parser.getTok().isNot(AsmToken::Percent))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000505 return MatchOperand_NoMatch;
506
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000507 Register Reg;
Richard Sandiford1d959002013-07-02 14:56:45 +0000508 bool IsAddress = (Kind == ADDR32Reg || Kind == ADDR64Reg);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000509 if (parseRegister(Reg, Group, Regs, IsAddress))
510 return MatchOperand_ParseFail;
511
512 Operands.push_back(SystemZOperand::createReg(Kind, Reg.Num,
513 Reg.StartLoc, Reg.EndLoc));
514 return MatchOperand_Success;
515}
516
Richard Sandiford1d959002013-07-02 14:56:45 +0000517// Parse a memory operand into Base, Disp, Index and Length.
518// Regs maps asm register numbers to LLVM register numbers and RegKind
519// says what kind of address register we're using (ADDR32Reg or ADDR64Reg).
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000520bool SystemZAsmParser::parseAddress(unsigned &Base, const MCExpr *&Disp,
Richard Sandiford1d959002013-07-02 14:56:45 +0000521 unsigned &Index, const MCExpr *&Length,
522 const unsigned *Regs,
523 RegisterKind RegKind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000524 // Parse the displacement, which must always be present.
525 if (getParser().parseExpression(Disp))
526 return true;
527
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000528 // Parse the optional base and index.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000529 Index = 0;
530 Base = 0;
Richard Sandiford1d959002013-07-02 14:56:45 +0000531 Length = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000532 if (getLexer().is(AsmToken::LParen)) {
533 Parser.Lex();
534
Richard Sandiford1d959002013-07-02 14:56:45 +0000535 if (getLexer().is(AsmToken::Percent)) {
536 // Parse the first register and decide whether it's a base or an index.
537 Register Reg;
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000538 if (parseRegister(Reg, RegGR, Regs, RegKind))
539 return true;
Richard Sandiford1d959002013-07-02 14:56:45 +0000540 if (getLexer().is(AsmToken::Comma))
541 Index = Reg.Num;
542 else
543 Base = Reg.Num;
544 } else {
545 // Parse the length.
546 if (getParser().parseExpression(Length))
547 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000548 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000549
550 // Check whether there's a second register. It's the base if so.
551 if (getLexer().is(AsmToken::Comma)) {
552 Parser.Lex();
553 Register Reg;
554 if (parseRegister(Reg, RegGR, Regs, RegKind))
555 return true;
556 Base = Reg.Num;
557 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000558
559 // Consume the closing bracket.
560 if (getLexer().isNot(AsmToken::RParen))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000561 return Error(Parser.getTok().getLoc(), "unexpected token in address");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000562 Parser.Lex();
563 }
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000564 return false;
565}
566
567// Parse a memory operand and add it to Operands. The other arguments
568// are as above.
569SystemZAsmParser::OperandMatchResultTy
570SystemZAsmParser::parseAddress(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Richard Sandiford1d959002013-07-02 14:56:45 +0000571 const unsigned *Regs, RegisterKind RegKind,
572 MemoryKind MemKind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000573 SMLoc StartLoc = Parser.getTok().getLoc();
574 unsigned Base, Index;
575 const MCExpr *Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +0000576 const MCExpr *Length;
577 if (parseAddress(Base, Disp, Index, Length, Regs, RegKind))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000578 return MatchOperand_ParseFail;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000579
Richard Sandiford1d959002013-07-02 14:56:45 +0000580 if (Index && MemKind != BDXMem)
581 {
582 Error(StartLoc, "invalid use of indexed addressing");
583 return MatchOperand_ParseFail;
584 }
585
586 if (Length && MemKind != BDLMem)
587 {
588 Error(StartLoc, "invalid use of length addressing");
589 return MatchOperand_ParseFail;
590 }
591
592 if (!Length && MemKind == BDLMem)
593 {
594 Error(StartLoc, "missing length in address");
595 return MatchOperand_ParseFail;
596 }
597
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000598 SMLoc EndLoc =
599 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
600 Operands.push_back(SystemZOperand::createMem(RegKind, Base, Disp, Index,
Richard Sandiford1d959002013-07-02 14:56:45 +0000601 Length, StartLoc, EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000602 return MatchOperand_Success;
603}
604
605bool SystemZAsmParser::ParseDirective(AsmToken DirectiveID) {
606 return true;
607}
608
609bool SystemZAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
610 SMLoc &EndLoc) {
611 Register Reg;
612 if (parseRegister(Reg))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000613 return true;
Richard Sandiford675f8692013-05-24 14:14:38 +0000614 if (Reg.Group == RegGR)
615 RegNo = SystemZMC::GR64Regs[Reg.Num];
616 else if (Reg.Group == RegFP)
617 RegNo = SystemZMC::FP64Regs[Reg.Num];
618 else
619 // FIXME: Access registers aren't modelled as LLVM registers yet.
620 return Error(Reg.StartLoc, "invalid operand for instruction");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000621 StartLoc = Reg.StartLoc;
622 EndLoc = Reg.EndLoc;
623 return false;
624}
625
626bool SystemZAsmParser::
627ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
628 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
629 Operands.push_back(SystemZOperand::createToken(Name, NameLoc));
630
631 // Read the remaining operands.
632 if (getLexer().isNot(AsmToken::EndOfStatement)) {
633 // Read the first operand.
634 if (parseOperand(Operands, Name)) {
635 Parser.eatToEndOfStatement();
636 return true;
637 }
638
639 // Read any subsequent operands.
640 while (getLexer().is(AsmToken::Comma)) {
641 Parser.Lex();
642 if (parseOperand(Operands, Name)) {
643 Parser.eatToEndOfStatement();
644 return true;
645 }
646 }
647 if (getLexer().isNot(AsmToken::EndOfStatement)) {
648 SMLoc Loc = getLexer().getLoc();
649 Parser.eatToEndOfStatement();
650 return Error(Loc, "unexpected token in argument list");
651 }
652 }
653
654 // Consume the EndOfStatement.
655 Parser.Lex();
656 return false;
657}
658
659bool SystemZAsmParser::
660parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
661 StringRef Mnemonic) {
662 // Check if the current operand has a custom associated parser, if so, try to
663 // custom parse the operand, or fallback to the general approach.
664 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
665 if (ResTy == MatchOperand_Success)
666 return false;
667
668 // If there wasn't a custom match, try the generic matcher below. Otherwise,
669 // there was a match, but an error occurred, in which case, just return that
670 // the operand parsing failed.
671 if (ResTy == MatchOperand_ParseFail)
672 return true;
673
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000674 // Check for a register. All real register operands should have used
675 // a context-dependent parse routine, which gives the required register
676 // class. The code is here to mop up other cases, like those where
677 // the instruction isn't recognized.
678 if (Parser.getTok().is(AsmToken::Percent)) {
679 Register Reg;
680 if (parseRegister(Reg))
681 return true;
682 Operands.push_back(SystemZOperand::createInvalid(Reg.StartLoc, Reg.EndLoc));
683 return false;
684 }
685
686 // The only other type of operand is an immediate or address. As above,
687 // real address operands should have used a context-dependent parse routine,
688 // so we treat any plain expression as an immediate.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000689 SMLoc StartLoc = Parser.getTok().getLoc();
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000690 unsigned Base, Index;
Richard Sandiford1d959002013-07-02 14:56:45 +0000691 const MCExpr *Expr, *Length;
692 if (parseAddress(Base, Expr, Index, Length, SystemZMC::GR64Regs, ADDR64Reg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000693 return true;
694
695 SMLoc EndLoc =
696 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Richard Sandiford1d959002013-07-02 14:56:45 +0000697 if (Base || Index || Length)
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000698 Operands.push_back(SystemZOperand::createInvalid(StartLoc, EndLoc));
699 else
700 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000701 return false;
702}
703
704bool SystemZAsmParser::
705MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
706 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
707 MCStreamer &Out, unsigned &ErrorInfo,
708 bool MatchingInlineAsm) {
709 MCInst Inst;
710 unsigned MatchResult;
711
712 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
713 MatchingInlineAsm);
714 switch (MatchResult) {
715 default: break;
716 case Match_Success:
717 Inst.setLoc(IDLoc);
David Woodhousee6c13e42014-01-28 23:12:42 +0000718 Out.EmitInstruction(Inst, STI);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000719 return false;
720
721 case Match_MissingFeature: {
722 assert(ErrorInfo && "Unknown missing feature!");
723 // Special case the error message for the very common case where only
724 // a single subtarget feature is missing
725 std::string Msg = "instruction requires:";
726 unsigned Mask = 1;
727 for (unsigned I = 0; I < sizeof(ErrorInfo) * 8 - 1; ++I) {
728 if (ErrorInfo & Mask) {
729 Msg += " ";
730 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
731 }
732 Mask <<= 1;
733 }
734 return Error(IDLoc, Msg);
735 }
736
737 case Match_InvalidOperand: {
738 SMLoc ErrorLoc = IDLoc;
739 if (ErrorInfo != ~0U) {
740 if (ErrorInfo >= Operands.size())
741 return Error(IDLoc, "too few operands for instruction");
742
743 ErrorLoc = ((SystemZOperand*)Operands[ErrorInfo])->getStartLoc();
744 if (ErrorLoc == SMLoc())
745 ErrorLoc = IDLoc;
746 }
747 return Error(ErrorLoc, "invalid operand for instruction");
748 }
749
750 case Match_MnemonicFail:
751 return Error(IDLoc, "invalid instruction");
752 }
753
754 llvm_unreachable("Unexpected match type");
755}
756
757SystemZAsmParser::OperandMatchResultTy SystemZAsmParser::
758parseAccessReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000759 if (Parser.getTok().isNot(AsmToken::Percent))
760 return MatchOperand_NoMatch;
761
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000762 Register Reg;
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000763 if (parseRegister(Reg, RegAccess, 0))
764 return MatchOperand_ParseFail;
765
766 Operands.push_back(SystemZOperand::createAccessReg(Reg.Num,
767 Reg.StartLoc,
768 Reg.EndLoc));
769 return MatchOperand_Success;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000770}
771
Richard Sandiford1fb58832013-05-14 09:47:26 +0000772SystemZAsmParser::OperandMatchResultTy SystemZAsmParser::
773parsePCRel(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
774 int64_t MinVal, int64_t MaxVal) {
775 MCContext &Ctx = getContext();
776 MCStreamer &Out = getStreamer();
777 const MCExpr *Expr;
778 SMLoc StartLoc = Parser.getTok().getLoc();
779 if (getParser().parseExpression(Expr))
780 return MatchOperand_NoMatch;
781
782 // For consistency with the GNU assembler, treat immediates as offsets
783 // from ".".
Richard Sandiford21f5d682014-03-06 11:22:58 +0000784 if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
Richard Sandiford1fb58832013-05-14 09:47:26 +0000785 int64_t Value = CE->getValue();
786 if ((Value & 1) || Value < MinVal || Value > MaxVal) {
787 Error(StartLoc, "offset out of range");
788 return MatchOperand_ParseFail;
789 }
790 MCSymbol *Sym = Ctx.CreateTempSymbol();
791 Out.EmitLabel(Sym);
792 const MCExpr *Base = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
793 Ctx);
794 Expr = Value == 0 ? Base : MCBinaryExpr::CreateAdd(Base, Expr, Ctx);
795 }
796
797 SMLoc EndLoc =
798 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
799 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
800 return MatchOperand_Success;
801}
802
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000803// Force static initialization.
804extern "C" void LLVMInitializeSystemZAsmParser() {
805 RegisterMCAsmParser<SystemZAsmParser> X(TheSystemZTarget);
806}