blob: 25df0ecc3eaa17565e49245f64e379f820c6d016 [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"
Richard Sandiford1fb58832013-05-14 09:47:26 +000011#include "llvm/MC/MCContext.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000012#include "llvm/MC/MCExpr.h"
13#include "llvm/MC/MCInst.h"
14#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
15#include "llvm/MC/MCStreamer.h"
16#include "llvm/MC/MCSubtargetInfo.h"
17#include "llvm/MC/MCTargetAsmParser.h"
18#include "llvm/Support/TargetRegistry.h"
19
20using namespace llvm;
21
22// Return true if Expr is in the range [MinValue, MaxValue].
23static bool inRange(const MCExpr *Expr, int64_t MinValue, int64_t MaxValue) {
24 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) {
25 int64_t Value = CE->getValue();
26 return Value >= MinValue && Value <= MaxValue;
27 }
28 return false;
29}
30
31namespace {
Richard Sandiford1d959002013-07-02 14:56:45 +000032enum RegisterKind {
33 GR32Reg,
34 GR64Reg,
35 GR128Reg,
36 ADDR32Reg,
37 ADDR64Reg,
38 FP32Reg,
39 FP64Reg,
40 FP128Reg
41};
42
43enum MemoryKind {
44 BDMem,
45 BDXMem,
46 BDLMem
47};
48
Ulrich Weigand5f613df2013-05-06 16:15:19 +000049class SystemZOperand : public MCParsedAsmOperand {
50public:
Ulrich Weigand5f613df2013-05-06 16:15:19 +000051private:
52 enum OperandKind {
Richard Sandiforddc5ed712013-05-24 14:26:46 +000053 KindInvalid,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000054 KindToken,
55 KindReg,
56 KindAccessReg,
57 KindImm,
58 KindMem
59 };
60
61 OperandKind Kind;
62 SMLoc StartLoc, EndLoc;
63
64 // A string of length Length, starting at Data.
65 struct TokenOp {
66 const char *Data;
67 unsigned Length;
68 };
69
Richard Sandiford675f8692013-05-24 14:14:38 +000070 // LLVM register Num, which has kind Kind. In some ways it might be
71 // easier for this class to have a register bank (general, floating-point
72 // or access) and a raw register number (0-15). This would postpone the
73 // interpretation of the operand to the add*() methods and avoid the need
74 // for context-dependent parsing. However, we do things the current way
75 // because of the virtual getReg() method, which needs to distinguish
76 // between (say) %r0 used as a single register and %r0 used as a pair.
77 // Context-dependent parsing can also give us slightly better error
78 // messages when invalid pairs like %r1 are used.
Ulrich Weigand5f613df2013-05-06 16:15:19 +000079 struct RegOp {
80 RegisterKind Kind;
81 unsigned Num;
82 };
83
84 // Base + Disp + Index, where Base and Index are LLVM registers or 0.
85 // RegKind says what type the registers have (ADDR32Reg or ADDR64Reg).
Richard Sandiford1d959002013-07-02 14:56:45 +000086 // Length is the operand length for D(L,B)-style operands, otherwise
87 // it is null.
Ulrich Weigand5f613df2013-05-06 16:15:19 +000088 struct MemOp {
89 unsigned Base : 8;
90 unsigned Index : 8;
91 unsigned RegKind : 8;
92 unsigned Unused : 8;
93 const MCExpr *Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +000094 const MCExpr *Length;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000095 };
96
97 union {
98 TokenOp Token;
99 RegOp Reg;
100 unsigned AccessReg;
101 const MCExpr *Imm;
102 MemOp Mem;
103 };
104
105 SystemZOperand(OperandKind kind, SMLoc startLoc, SMLoc endLoc)
106 : Kind(kind), StartLoc(startLoc), EndLoc(endLoc)
107 {}
108
109 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
110 // Add as immediates when possible. Null MCExpr = 0.
111 if (Expr == 0)
112 Inst.addOperand(MCOperand::CreateImm(0));
113 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
114 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
115 else
116 Inst.addOperand(MCOperand::CreateExpr(Expr));
117 }
118
119public:
120 // Create particular kinds of operand.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000121 static SystemZOperand *createInvalid(SMLoc StartLoc, SMLoc EndLoc) {
122 return new SystemZOperand(KindInvalid, StartLoc, EndLoc);
123 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000124 static SystemZOperand *createToken(StringRef Str, SMLoc Loc) {
125 SystemZOperand *Op = new SystemZOperand(KindToken, Loc, Loc);
126 Op->Token.Data = Str.data();
127 Op->Token.Length = Str.size();
128 return Op;
129 }
130 static SystemZOperand *createReg(RegisterKind Kind, unsigned Num,
131 SMLoc StartLoc, SMLoc EndLoc) {
132 SystemZOperand *Op = new SystemZOperand(KindReg, StartLoc, EndLoc);
133 Op->Reg.Kind = Kind;
134 Op->Reg.Num = Num;
135 return Op;
136 }
137 static SystemZOperand *createAccessReg(unsigned Num, SMLoc StartLoc,
138 SMLoc EndLoc) {
139 SystemZOperand *Op = new SystemZOperand(KindAccessReg, StartLoc, EndLoc);
140 Op->AccessReg = Num;
141 return Op;
142 }
143 static SystemZOperand *createImm(const MCExpr *Expr, SMLoc StartLoc,
144 SMLoc EndLoc) {
145 SystemZOperand *Op = new SystemZOperand(KindImm, StartLoc, EndLoc);
146 Op->Imm = Expr;
147 return Op;
148 }
149 static SystemZOperand *createMem(RegisterKind RegKind, unsigned Base,
150 const MCExpr *Disp, unsigned Index,
Richard Sandiford1d959002013-07-02 14:56:45 +0000151 const MCExpr *Length, SMLoc StartLoc,
152 SMLoc EndLoc) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000153 SystemZOperand *Op = new SystemZOperand(KindMem, StartLoc, EndLoc);
154 Op->Mem.RegKind = RegKind;
155 Op->Mem.Base = Base;
156 Op->Mem.Index = Index;
157 Op->Mem.Disp = Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +0000158 Op->Mem.Length = Length;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000159 return Op;
160 }
161
162 // Token operands
163 virtual bool isToken() const LLVM_OVERRIDE {
164 return Kind == KindToken;
165 }
166 StringRef getToken() const {
167 assert(Kind == KindToken && "Not a token");
168 return StringRef(Token.Data, Token.Length);
169 }
170
171 // Register operands.
172 virtual bool isReg() const LLVM_OVERRIDE {
173 return Kind == KindReg;
174 }
175 bool isReg(RegisterKind RegKind) const {
176 return Kind == KindReg && Reg.Kind == RegKind;
177 }
178 virtual unsigned getReg() const LLVM_OVERRIDE {
179 assert(Kind == KindReg && "Not a register");
180 return Reg.Num;
181 }
182
183 // Access register operands. Access registers aren't exposed to LLVM
184 // as registers.
185 bool isAccessReg() const {
186 return Kind == KindAccessReg;
187 }
188
189 // Immediate operands.
190 virtual bool isImm() const LLVM_OVERRIDE {
191 return Kind == KindImm;
192 }
193 bool isImm(int64_t MinValue, int64_t MaxValue) const {
194 return Kind == KindImm && inRange(Imm, MinValue, MaxValue);
195 }
196 const MCExpr *getImm() const {
197 assert(Kind == KindImm && "Not an immediate");
198 return Imm;
199 }
200
201 // Memory operands.
202 virtual bool isMem() const LLVM_OVERRIDE {
203 return Kind == KindMem;
204 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000205 bool isMem(RegisterKind RegKind, MemoryKind MemKind) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000206 return (Kind == KindMem &&
207 Mem.RegKind == RegKind &&
Richard Sandiford1d959002013-07-02 14:56:45 +0000208 (MemKind == BDXMem || !Mem.Index) &&
209 (MemKind == BDLMem) == (Mem.Length != 0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000210 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000211 bool isMemDisp12(RegisterKind RegKind, MemoryKind MemKind) const {
212 return isMem(RegKind, MemKind) && inRange(Mem.Disp, 0, 0xfff);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000213 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000214 bool isMemDisp20(RegisterKind RegKind, MemoryKind MemKind) const {
215 return isMem(RegKind, MemKind) && inRange(Mem.Disp, -524288, 524287);
216 }
217 bool isMemDisp12Len8(RegisterKind RegKind) const {
218 return isMemDisp12(RegKind, BDLMem) && inRange(Mem.Length, 1, 0x100);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000219 }
220
221 // Override MCParsedAsmOperand.
222 virtual SMLoc getStartLoc() const LLVM_OVERRIDE { return StartLoc; }
223 virtual SMLoc getEndLoc() const LLVM_OVERRIDE { return EndLoc; }
224 virtual void print(raw_ostream &OS) const LLVM_OVERRIDE;
225
226 // Used by the TableGen code to add particular types of operand
227 // to an instruction.
228 void addRegOperands(MCInst &Inst, unsigned N) const {
229 assert(N == 1 && "Invalid number of operands");
230 Inst.addOperand(MCOperand::CreateReg(getReg()));
231 }
232 void addAccessRegOperands(MCInst &Inst, unsigned N) const {
233 assert(N == 1 && "Invalid number of operands");
234 assert(Kind == KindAccessReg && "Invalid operand type");
235 Inst.addOperand(MCOperand::CreateImm(AccessReg));
236 }
237 void addImmOperands(MCInst &Inst, unsigned N) const {
238 assert(N == 1 && "Invalid number of operands");
239 addExpr(Inst, getImm());
240 }
241 void addBDAddrOperands(MCInst &Inst, unsigned N) const {
242 assert(N == 2 && "Invalid number of operands");
243 assert(Kind == KindMem && Mem.Index == 0 && "Invalid operand type");
244 Inst.addOperand(MCOperand::CreateReg(Mem.Base));
245 addExpr(Inst, Mem.Disp);
246 }
247 void addBDXAddrOperands(MCInst &Inst, unsigned N) const {
248 assert(N == 3 && "Invalid number of operands");
249 assert(Kind == KindMem && "Invalid operand type");
250 Inst.addOperand(MCOperand::CreateReg(Mem.Base));
251 addExpr(Inst, Mem.Disp);
252 Inst.addOperand(MCOperand::CreateReg(Mem.Index));
253 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000254 void addBDLAddrOperands(MCInst &Inst, unsigned N) const {
255 assert(N == 3 && "Invalid number of operands");
256 assert(Kind == KindMem && "Invalid operand type");
257 Inst.addOperand(MCOperand::CreateReg(Mem.Base));
258 addExpr(Inst, Mem.Disp);
259 addExpr(Inst, Mem.Length);
260 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000261
262 // Used by the TableGen code to check for particular operand types.
263 bool isGR32() const { return isReg(GR32Reg); }
264 bool isGR64() const { return isReg(GR64Reg); }
265 bool isGR128() const { return isReg(GR128Reg); }
266 bool isADDR32() const { return isReg(ADDR32Reg); }
267 bool isADDR64() const { return isReg(ADDR64Reg); }
268 bool isADDR128() const { return false; }
269 bool isFP32() const { return isReg(FP32Reg); }
270 bool isFP64() const { return isReg(FP64Reg); }
271 bool isFP128() const { return isReg(FP128Reg); }
Richard Sandiford1d959002013-07-02 14:56:45 +0000272 bool isBDAddr32Disp12() const { return isMemDisp12(ADDR32Reg, BDMem); }
273 bool isBDAddr32Disp20() const { return isMemDisp20(ADDR32Reg, BDMem); }
274 bool isBDAddr64Disp12() const { return isMemDisp12(ADDR64Reg, BDMem); }
275 bool isBDAddr64Disp20() const { return isMemDisp20(ADDR64Reg, BDMem); }
276 bool isBDXAddr64Disp12() const { return isMemDisp12(ADDR64Reg, BDXMem); }
277 bool isBDXAddr64Disp20() const { return isMemDisp20(ADDR64Reg, BDXMem); }
278 bool isBDLAddr64Disp12Len8() const { return isMemDisp12Len8(ADDR64Reg); }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000279 bool isU4Imm() const { return isImm(0, 15); }
280 bool isU6Imm() const { return isImm(0, 63); }
281 bool isU8Imm() const { return isImm(0, 255); }
282 bool isS8Imm() const { return isImm(-128, 127); }
283 bool isU16Imm() const { return isImm(0, 65535); }
284 bool isS16Imm() const { return isImm(-32768, 32767); }
285 bool isU32Imm() const { return isImm(0, (1LL << 32) - 1); }
286 bool isS32Imm() const { return isImm(-(1LL << 31), (1LL << 31) - 1); }
287};
288
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000289class SystemZAsmParser : public MCTargetAsmParser {
290#define GET_ASSEMBLER_HEADER
291#include "SystemZGenAsmMatcher.inc"
292
293private:
294 MCSubtargetInfo &STI;
295 MCAsmParser &Parser;
Richard Sandiford675f8692013-05-24 14:14:38 +0000296 enum RegisterGroup {
297 RegGR,
298 RegFP,
299 RegAccess
300 };
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000301 struct Register {
Richard Sandiford675f8692013-05-24 14:14:38 +0000302 RegisterGroup Group;
303 unsigned Num;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000304 SMLoc StartLoc, EndLoc;
305 };
306
307 bool parseRegister(Register &Reg);
308
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000309 bool parseRegister(Register &Reg, RegisterGroup Group, const unsigned *Regs,
310 bool IsAddress = false);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000311
312 OperandMatchResultTy
313 parseRegister(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Richard Sandiford1d959002013-07-02 14:56:45 +0000314 RegisterGroup Group, const unsigned *Regs, RegisterKind Kind);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000315
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000316 bool parseAddress(unsigned &Base, const MCExpr *&Disp,
Richard Sandiford1d959002013-07-02 14:56:45 +0000317 unsigned &Index, const MCExpr *&Length,
318 const unsigned *Regs, RegisterKind RegKind);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000319
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000320 OperandMatchResultTy
321 parseAddress(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Richard Sandiford1d959002013-07-02 14:56:45 +0000322 const unsigned *Regs, RegisterKind RegKind,
323 MemoryKind MemKind);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000324
325 bool parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
326 StringRef Mnemonic);
327
328public:
329 SystemZAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
330 : MCTargetAsmParser(), STI(sti), Parser(parser) {
331 MCAsmParserExtension::Initialize(Parser);
332
333 // Initialize the set of available features.
334 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
335 }
336
337 // Override MCTargetAsmParser.
338 virtual bool ParseDirective(AsmToken DirectiveID) LLVM_OVERRIDE;
339 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
340 SMLoc &EndLoc) LLVM_OVERRIDE;
341 virtual bool ParseInstruction(ParseInstructionInfo &Info,
342 StringRef Name, SMLoc NameLoc,
343 SmallVectorImpl<MCParsedAsmOperand*> &Operands)
344 LLVM_OVERRIDE;
345 virtual bool
346 MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
347 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
348 MCStreamer &Out, unsigned &ErrorInfo,
349 bool MatchingInlineAsm) LLVM_OVERRIDE;
350
351 // Used by the TableGen code to parse particular operand types.
352 OperandMatchResultTy
353 parseGR32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000354 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, GR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000355 }
356 OperandMatchResultTy
357 parseGR64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000358 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, GR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000359 }
360 OperandMatchResultTy
361 parseGR128(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000362 return parseRegister(Operands, RegGR, SystemZMC::GR128Regs, GR128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000363 }
364 OperandMatchResultTy
365 parseADDR32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000366 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, ADDR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000367 }
368 OperandMatchResultTy
369 parseADDR64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000370 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000371 }
372 OperandMatchResultTy
373 parseADDR128(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
374 llvm_unreachable("Shouldn't be used as an operand");
375 }
376 OperandMatchResultTy
377 parseFP32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000378 return parseRegister(Operands, RegFP, SystemZMC::FP32Regs, FP32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000379 }
380 OperandMatchResultTy
381 parseFP64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000382 return parseRegister(Operands, RegFP, SystemZMC::FP64Regs, FP64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000383 }
384 OperandMatchResultTy
385 parseFP128(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000386 return parseRegister(Operands, RegFP, SystemZMC::FP128Regs, FP128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000387 }
388 OperandMatchResultTy
389 parseBDAddr32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000390 return parseAddress(Operands, SystemZMC::GR32Regs, ADDR32Reg, BDMem);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000391 }
392 OperandMatchResultTy
393 parseBDAddr64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000394 return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDMem);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000395 }
396 OperandMatchResultTy
397 parseBDXAddr64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000398 return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDXMem);
399 }
400 OperandMatchResultTy
401 parseBDLAddr64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
402 return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDLMem);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000403 }
404 OperandMatchResultTy
405 parseAccessReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Richard Sandiford1fb58832013-05-14 09:47:26 +0000406 OperandMatchResultTy
407 parsePCRel(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
408 int64_t MinVal, int64_t MaxVal);
409 OperandMatchResultTy
410 parsePCRel16(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
411 return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1);
412 }
413 OperandMatchResultTy
414 parsePCRel32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
415 return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1);
416 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000417};
418}
419
420#define GET_REGISTER_MATCHER
421#define GET_SUBTARGET_FEATURE_NAME
422#define GET_MATCHER_IMPLEMENTATION
423#include "SystemZGenAsmMatcher.inc"
424
425void SystemZOperand::print(raw_ostream &OS) const {
426 llvm_unreachable("Not implemented");
427}
428
429// Parse one register of the form %<prefix><number>.
430bool SystemZAsmParser::parseRegister(Register &Reg) {
431 Reg.StartLoc = Parser.getTok().getLoc();
432
433 // Eat the % prefix.
434 if (Parser.getTok().isNot(AsmToken::Percent))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000435 return Error(Parser.getTok().getLoc(), "register expected");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000436 Parser.Lex();
437
438 // Expect a register name.
439 if (Parser.getTok().isNot(AsmToken::Identifier))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000440 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000441
Richard Sandiford675f8692013-05-24 14:14:38 +0000442 // Check that there's a prefix.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000443 StringRef Name = Parser.getTok().getString();
444 if (Name.size() < 2)
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000445 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000446 char Prefix = Name[0];
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000447
448 // Treat the rest of the register name as a register number.
Richard Sandiford675f8692013-05-24 14:14:38 +0000449 if (Name.substr(1).getAsInteger(10, Reg.Num))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000450 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000451
452 // Look for valid combinations of prefix and number.
453 if (Prefix == 'r' && Reg.Num < 16)
454 Reg.Group = RegGR;
455 else if (Prefix == 'f' && Reg.Num < 16)
456 Reg.Group = RegFP;
457 else if (Prefix == 'a' && Reg.Num < 16)
458 Reg.Group = RegAccess;
459 else
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000460 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000461
462 Reg.EndLoc = Parser.getTok().getLoc();
463 Parser.Lex();
464 return false;
465}
466
Richard Sandiford675f8692013-05-24 14:14:38 +0000467// Parse a register of group Group. If Regs is nonnull, use it to map
468// the raw register number to LLVM numbering, with zero entries indicating
469// an invalid register. IsAddress says whether the register appears in an
470// address context.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000471bool SystemZAsmParser::parseRegister(Register &Reg, RegisterGroup Group,
472 const unsigned *Regs, bool IsAddress) {
473 if (parseRegister(Reg))
474 return true;
475 if (Reg.Group != Group)
476 return Error(Reg.StartLoc, "invalid operand for instruction");
477 if (Regs && Regs[Reg.Num] == 0)
478 return Error(Reg.StartLoc, "invalid register pair");
479 if (Reg.Num == 0 && IsAddress)
480 return Error(Reg.StartLoc, "%r0 used in an address");
Richard Sandiford675f8692013-05-24 14:14:38 +0000481 if (Regs)
482 Reg.Num = Regs[Reg.Num];
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000483 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000484}
485
Richard Sandiford675f8692013-05-24 14:14:38 +0000486// Parse a register and add it to Operands. The other arguments are as above.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000487SystemZAsmParser::OperandMatchResultTy
488SystemZAsmParser::parseRegister(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Richard Sandiford675f8692013-05-24 14:14:38 +0000489 RegisterGroup Group, const unsigned *Regs,
Richard Sandiford1d959002013-07-02 14:56:45 +0000490 RegisterKind Kind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000491 if (Parser.getTok().isNot(AsmToken::Percent))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000492 return MatchOperand_NoMatch;
493
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000494 Register Reg;
Richard Sandiford1d959002013-07-02 14:56:45 +0000495 bool IsAddress = (Kind == ADDR32Reg || Kind == ADDR64Reg);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000496 if (parseRegister(Reg, Group, Regs, IsAddress))
497 return MatchOperand_ParseFail;
498
499 Operands.push_back(SystemZOperand::createReg(Kind, Reg.Num,
500 Reg.StartLoc, Reg.EndLoc));
501 return MatchOperand_Success;
502}
503
Richard Sandiford1d959002013-07-02 14:56:45 +0000504// Parse a memory operand into Base, Disp, Index and Length.
505// Regs maps asm register numbers to LLVM register numbers and RegKind
506// says what kind of address register we're using (ADDR32Reg or ADDR64Reg).
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000507bool SystemZAsmParser::parseAddress(unsigned &Base, const MCExpr *&Disp,
Richard Sandiford1d959002013-07-02 14:56:45 +0000508 unsigned &Index, const MCExpr *&Length,
509 const unsigned *Regs,
510 RegisterKind RegKind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000511 // Parse the displacement, which must always be present.
512 if (getParser().parseExpression(Disp))
513 return true;
514
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000515 // Parse the optional base and index.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000516 Index = 0;
517 Base = 0;
Richard Sandiford1d959002013-07-02 14:56:45 +0000518 Length = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000519 if (getLexer().is(AsmToken::LParen)) {
520 Parser.Lex();
521
Richard Sandiford1d959002013-07-02 14:56:45 +0000522 if (getLexer().is(AsmToken::Percent)) {
523 // Parse the first register and decide whether it's a base or an index.
524 Register Reg;
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000525 if (parseRegister(Reg, RegGR, Regs, RegKind))
526 return true;
Richard Sandiford1d959002013-07-02 14:56:45 +0000527 if (getLexer().is(AsmToken::Comma))
528 Index = Reg.Num;
529 else
530 Base = Reg.Num;
531 } else {
532 // Parse the length.
533 if (getParser().parseExpression(Length))
534 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000535 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000536
537 // Check whether there's a second register. It's the base if so.
538 if (getLexer().is(AsmToken::Comma)) {
539 Parser.Lex();
540 Register Reg;
541 if (parseRegister(Reg, RegGR, Regs, RegKind))
542 return true;
543 Base = Reg.Num;
544 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000545
546 // Consume the closing bracket.
547 if (getLexer().isNot(AsmToken::RParen))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000548 return Error(Parser.getTok().getLoc(), "unexpected token in address");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000549 Parser.Lex();
550 }
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000551 return false;
552}
553
554// Parse a memory operand and add it to Operands. The other arguments
555// are as above.
556SystemZAsmParser::OperandMatchResultTy
557SystemZAsmParser::parseAddress(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Richard Sandiford1d959002013-07-02 14:56:45 +0000558 const unsigned *Regs, RegisterKind RegKind,
559 MemoryKind MemKind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000560 SMLoc StartLoc = Parser.getTok().getLoc();
561 unsigned Base, Index;
562 const MCExpr *Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +0000563 const MCExpr *Length;
564 if (parseAddress(Base, Disp, Index, Length, Regs, RegKind))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000565 return MatchOperand_ParseFail;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000566
Richard Sandiford1d959002013-07-02 14:56:45 +0000567 if (Index && MemKind != BDXMem)
568 {
569 Error(StartLoc, "invalid use of indexed addressing");
570 return MatchOperand_ParseFail;
571 }
572
573 if (Length && MemKind != BDLMem)
574 {
575 Error(StartLoc, "invalid use of length addressing");
576 return MatchOperand_ParseFail;
577 }
578
579 if (!Length && MemKind == BDLMem)
580 {
581 Error(StartLoc, "missing length in address");
582 return MatchOperand_ParseFail;
583 }
584
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000585 SMLoc EndLoc =
586 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
587 Operands.push_back(SystemZOperand::createMem(RegKind, Base, Disp, Index,
Richard Sandiford1d959002013-07-02 14:56:45 +0000588 Length, StartLoc, EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000589 return MatchOperand_Success;
590}
591
592bool SystemZAsmParser::ParseDirective(AsmToken DirectiveID) {
593 return true;
594}
595
596bool SystemZAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
597 SMLoc &EndLoc) {
598 Register Reg;
599 if (parseRegister(Reg))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000600 return true;
Richard Sandiford675f8692013-05-24 14:14:38 +0000601 if (Reg.Group == RegGR)
602 RegNo = SystemZMC::GR64Regs[Reg.Num];
603 else if (Reg.Group == RegFP)
604 RegNo = SystemZMC::FP64Regs[Reg.Num];
605 else
606 // FIXME: Access registers aren't modelled as LLVM registers yet.
607 return Error(Reg.StartLoc, "invalid operand for instruction");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000608 StartLoc = Reg.StartLoc;
609 EndLoc = Reg.EndLoc;
610 return false;
611}
612
613bool SystemZAsmParser::
614ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
615 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
616 Operands.push_back(SystemZOperand::createToken(Name, NameLoc));
617
618 // Read the remaining operands.
619 if (getLexer().isNot(AsmToken::EndOfStatement)) {
620 // Read the first operand.
621 if (parseOperand(Operands, Name)) {
622 Parser.eatToEndOfStatement();
623 return true;
624 }
625
626 // Read any subsequent operands.
627 while (getLexer().is(AsmToken::Comma)) {
628 Parser.Lex();
629 if (parseOperand(Operands, Name)) {
630 Parser.eatToEndOfStatement();
631 return true;
632 }
633 }
634 if (getLexer().isNot(AsmToken::EndOfStatement)) {
635 SMLoc Loc = getLexer().getLoc();
636 Parser.eatToEndOfStatement();
637 return Error(Loc, "unexpected token in argument list");
638 }
639 }
640
641 // Consume the EndOfStatement.
642 Parser.Lex();
643 return false;
644}
645
646bool SystemZAsmParser::
647parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
648 StringRef Mnemonic) {
649 // Check if the current operand has a custom associated parser, if so, try to
650 // custom parse the operand, or fallback to the general approach.
651 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
652 if (ResTy == MatchOperand_Success)
653 return false;
654
655 // If there wasn't a custom match, try the generic matcher below. Otherwise,
656 // there was a match, but an error occurred, in which case, just return that
657 // the operand parsing failed.
658 if (ResTy == MatchOperand_ParseFail)
659 return true;
660
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000661 // Check for a register. All real register operands should have used
662 // a context-dependent parse routine, which gives the required register
663 // class. The code is here to mop up other cases, like those where
664 // the instruction isn't recognized.
665 if (Parser.getTok().is(AsmToken::Percent)) {
666 Register Reg;
667 if (parseRegister(Reg))
668 return true;
669 Operands.push_back(SystemZOperand::createInvalid(Reg.StartLoc, Reg.EndLoc));
670 return false;
671 }
672
673 // The only other type of operand is an immediate or address. As above,
674 // real address operands should have used a context-dependent parse routine,
675 // so we treat any plain expression as an immediate.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000676 SMLoc StartLoc = Parser.getTok().getLoc();
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000677 unsigned Base, Index;
Richard Sandiford1d959002013-07-02 14:56:45 +0000678 const MCExpr *Expr, *Length;
679 if (parseAddress(Base, Expr, Index, Length, SystemZMC::GR64Regs, ADDR64Reg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000680 return true;
681
682 SMLoc EndLoc =
683 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Richard Sandiford1d959002013-07-02 14:56:45 +0000684 if (Base || Index || Length)
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000685 Operands.push_back(SystemZOperand::createInvalid(StartLoc, EndLoc));
686 else
687 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000688 return false;
689}
690
691bool SystemZAsmParser::
692MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
693 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
694 MCStreamer &Out, unsigned &ErrorInfo,
695 bool MatchingInlineAsm) {
696 MCInst Inst;
697 unsigned MatchResult;
698
699 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
700 MatchingInlineAsm);
701 switch (MatchResult) {
702 default: break;
703 case Match_Success:
704 Inst.setLoc(IDLoc);
705 Out.EmitInstruction(Inst);
706 return false;
707
708 case Match_MissingFeature: {
709 assert(ErrorInfo && "Unknown missing feature!");
710 // Special case the error message for the very common case where only
711 // a single subtarget feature is missing
712 std::string Msg = "instruction requires:";
713 unsigned Mask = 1;
714 for (unsigned I = 0; I < sizeof(ErrorInfo) * 8 - 1; ++I) {
715 if (ErrorInfo & Mask) {
716 Msg += " ";
717 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
718 }
719 Mask <<= 1;
720 }
721 return Error(IDLoc, Msg);
722 }
723
724 case Match_InvalidOperand: {
725 SMLoc ErrorLoc = IDLoc;
726 if (ErrorInfo != ~0U) {
727 if (ErrorInfo >= Operands.size())
728 return Error(IDLoc, "too few operands for instruction");
729
730 ErrorLoc = ((SystemZOperand*)Operands[ErrorInfo])->getStartLoc();
731 if (ErrorLoc == SMLoc())
732 ErrorLoc = IDLoc;
733 }
734 return Error(ErrorLoc, "invalid operand for instruction");
735 }
736
737 case Match_MnemonicFail:
738 return Error(IDLoc, "invalid instruction");
739 }
740
741 llvm_unreachable("Unexpected match type");
742}
743
744SystemZAsmParser::OperandMatchResultTy SystemZAsmParser::
745parseAccessReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000746 if (Parser.getTok().isNot(AsmToken::Percent))
747 return MatchOperand_NoMatch;
748
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000749 Register Reg;
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000750 if (parseRegister(Reg, RegAccess, 0))
751 return MatchOperand_ParseFail;
752
753 Operands.push_back(SystemZOperand::createAccessReg(Reg.Num,
754 Reg.StartLoc,
755 Reg.EndLoc));
756 return MatchOperand_Success;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000757}
758
Richard Sandiford1fb58832013-05-14 09:47:26 +0000759SystemZAsmParser::OperandMatchResultTy SystemZAsmParser::
760parsePCRel(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
761 int64_t MinVal, int64_t MaxVal) {
762 MCContext &Ctx = getContext();
763 MCStreamer &Out = getStreamer();
764 const MCExpr *Expr;
765 SMLoc StartLoc = Parser.getTok().getLoc();
766 if (getParser().parseExpression(Expr))
767 return MatchOperand_NoMatch;
768
769 // For consistency with the GNU assembler, treat immediates as offsets
770 // from ".".
771 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) {
772 int64_t Value = CE->getValue();
773 if ((Value & 1) || Value < MinVal || Value > MaxVal) {
774 Error(StartLoc, "offset out of range");
775 return MatchOperand_ParseFail;
776 }
777 MCSymbol *Sym = Ctx.CreateTempSymbol();
778 Out.EmitLabel(Sym);
779 const MCExpr *Base = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
780 Ctx);
781 Expr = Value == 0 ? Base : MCBinaryExpr::CreateAdd(Base, Expr, Ctx);
782 }
783
784 SMLoc EndLoc =
785 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
786 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
787 return MatchOperand_Success;
788}
789
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000790// Force static initialization.
791extern "C" void LLVMInitializeSystemZAsmParser() {
792 RegisterMCAsmParser<SystemZAsmParser> X(TheSystemZTarget);
793}