blob: 3551b2dfb41a8ab97738bdb253acd88a83e72724 [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) {
25 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) {
26 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,
35 GR64Reg,
36 GR128Reg,
37 ADDR32Reg,
38 ADDR64Reg,
39 FP32Reg,
40 FP64Reg,
41 FP128Reg
42};
43
44enum MemoryKind {
45 BDMem,
46 BDXMem,
47 BDLMem
48};
49
Ulrich Weigand5f613df2013-05-06 16:15:19 +000050class SystemZOperand : public MCParsedAsmOperand {
51public:
Ulrich Weigand5f613df2013-05-06 16:15:19 +000052private:
53 enum OperandKind {
Richard Sandiforddc5ed712013-05-24 14:26:46 +000054 KindInvalid,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000055 KindToken,
56 KindReg,
57 KindAccessReg,
58 KindImm,
59 KindMem
60 };
61
62 OperandKind Kind;
63 SMLoc StartLoc, EndLoc;
64
65 // A string of length Length, starting at Data.
66 struct TokenOp {
67 const char *Data;
68 unsigned Length;
69 };
70
Richard Sandiford675f8692013-05-24 14:14:38 +000071 // LLVM register Num, which has kind Kind. In some ways it might be
72 // easier for this class to have a register bank (general, floating-point
73 // or access) and a raw register number (0-15). This would postpone the
74 // interpretation of the operand to the add*() methods and avoid the need
75 // for context-dependent parsing. However, we do things the current way
76 // because of the virtual getReg() method, which needs to distinguish
77 // between (say) %r0 used as a single register and %r0 used as a pair.
78 // Context-dependent parsing can also give us slightly better error
79 // messages when invalid pairs like %r1 are used.
Ulrich Weigand5f613df2013-05-06 16:15:19 +000080 struct RegOp {
81 RegisterKind Kind;
82 unsigned Num;
83 };
84
85 // Base + Disp + Index, where Base and Index are LLVM registers or 0.
86 // RegKind says what type the registers have (ADDR32Reg or ADDR64Reg).
Richard Sandiford1d959002013-07-02 14:56:45 +000087 // Length is the operand length for D(L,B)-style operands, otherwise
88 // it is null.
Ulrich Weigand5f613df2013-05-06 16:15:19 +000089 struct MemOp {
90 unsigned Base : 8;
91 unsigned Index : 8;
92 unsigned RegKind : 8;
93 unsigned Unused : 8;
94 const MCExpr *Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +000095 const MCExpr *Length;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000096 };
97
98 union {
99 TokenOp Token;
100 RegOp Reg;
101 unsigned AccessReg;
102 const MCExpr *Imm;
103 MemOp Mem;
104 };
105
106 SystemZOperand(OperandKind kind, SMLoc startLoc, SMLoc endLoc)
107 : Kind(kind), StartLoc(startLoc), EndLoc(endLoc)
108 {}
109
110 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
111 // Add as immediates when possible. Null MCExpr = 0.
112 if (Expr == 0)
113 Inst.addOperand(MCOperand::CreateImm(0));
114 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
115 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
116 else
117 Inst.addOperand(MCOperand::CreateExpr(Expr));
118 }
119
120public:
121 // Create particular kinds of operand.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000122 static SystemZOperand *createInvalid(SMLoc StartLoc, SMLoc EndLoc) {
123 return new SystemZOperand(KindInvalid, StartLoc, EndLoc);
124 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000125 static SystemZOperand *createToken(StringRef Str, SMLoc Loc) {
126 SystemZOperand *Op = new SystemZOperand(KindToken, Loc, Loc);
127 Op->Token.Data = Str.data();
128 Op->Token.Length = Str.size();
129 return Op;
130 }
131 static SystemZOperand *createReg(RegisterKind Kind, unsigned Num,
132 SMLoc StartLoc, SMLoc EndLoc) {
133 SystemZOperand *Op = new SystemZOperand(KindReg, StartLoc, EndLoc);
134 Op->Reg.Kind = Kind;
135 Op->Reg.Num = Num;
136 return Op;
137 }
138 static SystemZOperand *createAccessReg(unsigned Num, SMLoc StartLoc,
139 SMLoc EndLoc) {
140 SystemZOperand *Op = new SystemZOperand(KindAccessReg, StartLoc, EndLoc);
141 Op->AccessReg = Num;
142 return Op;
143 }
144 static SystemZOperand *createImm(const MCExpr *Expr, SMLoc StartLoc,
145 SMLoc EndLoc) {
146 SystemZOperand *Op = new SystemZOperand(KindImm, StartLoc, EndLoc);
147 Op->Imm = Expr;
148 return Op;
149 }
150 static SystemZOperand *createMem(RegisterKind RegKind, unsigned Base,
151 const MCExpr *Disp, unsigned Index,
Richard Sandiford1d959002013-07-02 14:56:45 +0000152 const MCExpr *Length, SMLoc StartLoc,
153 SMLoc EndLoc) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000154 SystemZOperand *Op = new SystemZOperand(KindMem, StartLoc, EndLoc);
155 Op->Mem.RegKind = RegKind;
156 Op->Mem.Base = Base;
157 Op->Mem.Index = Index;
158 Op->Mem.Disp = Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +0000159 Op->Mem.Length = Length;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000160 return Op;
161 }
162
163 // Token operands
164 virtual bool isToken() const LLVM_OVERRIDE {
165 return Kind == KindToken;
166 }
167 StringRef getToken() const {
168 assert(Kind == KindToken && "Not a token");
169 return StringRef(Token.Data, Token.Length);
170 }
171
172 // Register operands.
173 virtual bool isReg() const LLVM_OVERRIDE {
174 return Kind == KindReg;
175 }
176 bool isReg(RegisterKind RegKind) const {
177 return Kind == KindReg && Reg.Kind == RegKind;
178 }
179 virtual unsigned getReg() const LLVM_OVERRIDE {
180 assert(Kind == KindReg && "Not a register");
181 return Reg.Num;
182 }
183
184 // Access register operands. Access registers aren't exposed to LLVM
185 // as registers.
186 bool isAccessReg() const {
187 return Kind == KindAccessReg;
188 }
189
190 // Immediate operands.
191 virtual bool isImm() const LLVM_OVERRIDE {
192 return Kind == KindImm;
193 }
194 bool isImm(int64_t MinValue, int64_t MaxValue) const {
195 return Kind == KindImm && inRange(Imm, MinValue, MaxValue);
196 }
197 const MCExpr *getImm() const {
198 assert(Kind == KindImm && "Not an immediate");
199 return Imm;
200 }
201
202 // Memory operands.
203 virtual bool isMem() const LLVM_OVERRIDE {
204 return Kind == KindMem;
205 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000206 bool isMem(RegisterKind RegKind, MemoryKind MemKind) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000207 return (Kind == KindMem &&
208 Mem.RegKind == RegKind &&
Richard Sandiford1d959002013-07-02 14:56:45 +0000209 (MemKind == BDXMem || !Mem.Index) &&
210 (MemKind == BDLMem) == (Mem.Length != 0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000211 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000212 bool isMemDisp12(RegisterKind RegKind, MemoryKind MemKind) const {
213 return isMem(RegKind, MemKind) && inRange(Mem.Disp, 0, 0xfff);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000214 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000215 bool isMemDisp20(RegisterKind RegKind, MemoryKind MemKind) const {
216 return isMem(RegKind, MemKind) && inRange(Mem.Disp, -524288, 524287);
217 }
218 bool isMemDisp12Len8(RegisterKind RegKind) const {
219 return isMemDisp12(RegKind, BDLMem) && inRange(Mem.Length, 1, 0x100);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000220 }
221
222 // Override MCParsedAsmOperand.
223 virtual SMLoc getStartLoc() const LLVM_OVERRIDE { return StartLoc; }
224 virtual SMLoc getEndLoc() const LLVM_OVERRIDE { return EndLoc; }
225 virtual void print(raw_ostream &OS) const LLVM_OVERRIDE;
226
227 // Used by the TableGen code to add particular types of operand
228 // to an instruction.
229 void addRegOperands(MCInst &Inst, unsigned N) const {
230 assert(N == 1 && "Invalid number of operands");
231 Inst.addOperand(MCOperand::CreateReg(getReg()));
232 }
233 void addAccessRegOperands(MCInst &Inst, unsigned N) const {
234 assert(N == 1 && "Invalid number of operands");
235 assert(Kind == KindAccessReg && "Invalid operand type");
236 Inst.addOperand(MCOperand::CreateImm(AccessReg));
237 }
238 void addImmOperands(MCInst &Inst, unsigned N) const {
239 assert(N == 1 && "Invalid number of operands");
240 addExpr(Inst, getImm());
241 }
242 void addBDAddrOperands(MCInst &Inst, unsigned N) const {
243 assert(N == 2 && "Invalid number of operands");
244 assert(Kind == KindMem && Mem.Index == 0 && "Invalid operand type");
245 Inst.addOperand(MCOperand::CreateReg(Mem.Base));
246 addExpr(Inst, Mem.Disp);
247 }
248 void addBDXAddrOperands(MCInst &Inst, unsigned N) const {
249 assert(N == 3 && "Invalid number of operands");
250 assert(Kind == KindMem && "Invalid operand type");
251 Inst.addOperand(MCOperand::CreateReg(Mem.Base));
252 addExpr(Inst, Mem.Disp);
253 Inst.addOperand(MCOperand::CreateReg(Mem.Index));
254 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000255 void addBDLAddrOperands(MCInst &Inst, unsigned N) const {
256 assert(N == 3 && "Invalid number of operands");
257 assert(Kind == KindMem && "Invalid operand type");
258 Inst.addOperand(MCOperand::CreateReg(Mem.Base));
259 addExpr(Inst, Mem.Disp);
260 addExpr(Inst, Mem.Length);
261 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000262
263 // Used by the TableGen code to check for particular operand types.
264 bool isGR32() const { return isReg(GR32Reg); }
265 bool isGR64() const { return isReg(GR64Reg); }
266 bool isGR128() const { return isReg(GR128Reg); }
267 bool isADDR32() const { return isReg(ADDR32Reg); }
268 bool isADDR64() const { return isReg(ADDR64Reg); }
269 bool isADDR128() const { return false; }
270 bool isFP32() const { return isReg(FP32Reg); }
271 bool isFP64() const { return isReg(FP64Reg); }
272 bool isFP128() const { return isReg(FP128Reg); }
Richard Sandiford1d959002013-07-02 14:56:45 +0000273 bool isBDAddr32Disp12() const { return isMemDisp12(ADDR32Reg, BDMem); }
274 bool isBDAddr32Disp20() const { return isMemDisp20(ADDR32Reg, BDMem); }
275 bool isBDAddr64Disp12() const { return isMemDisp12(ADDR64Reg, BDMem); }
276 bool isBDAddr64Disp20() const { return isMemDisp20(ADDR64Reg, BDMem); }
277 bool isBDXAddr64Disp12() const { return isMemDisp12(ADDR64Reg, BDXMem); }
278 bool isBDXAddr64Disp20() const { return isMemDisp20(ADDR64Reg, BDXMem); }
279 bool isBDLAddr64Disp12Len8() const { return isMemDisp12Len8(ADDR64Reg); }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000280 bool isU4Imm() const { return isImm(0, 15); }
281 bool isU6Imm() const { return isImm(0, 63); }
282 bool isU8Imm() const { return isImm(0, 255); }
283 bool isS8Imm() const { return isImm(-128, 127); }
284 bool isU16Imm() const { return isImm(0, 65535); }
285 bool isS16Imm() const { return isImm(-32768, 32767); }
286 bool isU32Imm() const { return isImm(0, (1LL << 32) - 1); }
287 bool isS32Imm() const { return isImm(-(1LL << 31), (1LL << 31) - 1); }
288};
289
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000290class SystemZAsmParser : public MCTargetAsmParser {
291#define GET_ASSEMBLER_HEADER
292#include "SystemZGenAsmMatcher.inc"
293
294private:
295 MCSubtargetInfo &STI;
296 MCAsmParser &Parser;
Richard Sandiford675f8692013-05-24 14:14:38 +0000297 enum RegisterGroup {
298 RegGR,
299 RegFP,
300 RegAccess
301 };
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000302 struct Register {
Richard Sandiford675f8692013-05-24 14:14:38 +0000303 RegisterGroup Group;
304 unsigned Num;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000305 SMLoc StartLoc, EndLoc;
306 };
307
308 bool parseRegister(Register &Reg);
309
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000310 bool parseRegister(Register &Reg, RegisterGroup Group, const unsigned *Regs,
311 bool IsAddress = false);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000312
313 OperandMatchResultTy
314 parseRegister(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Richard Sandiford1d959002013-07-02 14:56:45 +0000315 RegisterGroup Group, const unsigned *Regs, RegisterKind Kind);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000316
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000317 bool parseAddress(unsigned &Base, const MCExpr *&Disp,
Richard Sandiford1d959002013-07-02 14:56:45 +0000318 unsigned &Index, const MCExpr *&Length,
319 const unsigned *Regs, RegisterKind RegKind);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000320
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000321 OperandMatchResultTy
322 parseAddress(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Richard Sandiford1d959002013-07-02 14:56:45 +0000323 const unsigned *Regs, RegisterKind RegKind,
324 MemoryKind MemKind);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000325
326 bool parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
327 StringRef Mnemonic);
328
329public:
Joey Gouly0e76fa72013-09-12 10:28:05 +0000330 SystemZAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
331 const MCInstrInfo &MII)
332 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000333 MCAsmParserExtension::Initialize(Parser);
334
335 // Initialize the set of available features.
336 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
337 }
338
339 // Override MCTargetAsmParser.
340 virtual bool ParseDirective(AsmToken DirectiveID) LLVM_OVERRIDE;
341 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
342 SMLoc &EndLoc) LLVM_OVERRIDE;
343 virtual bool ParseInstruction(ParseInstructionInfo &Info,
344 StringRef Name, SMLoc NameLoc,
345 SmallVectorImpl<MCParsedAsmOperand*> &Operands)
346 LLVM_OVERRIDE;
347 virtual bool
348 MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
349 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
350 MCStreamer &Out, unsigned &ErrorInfo,
351 bool MatchingInlineAsm) LLVM_OVERRIDE;
352
353 // Used by the TableGen code to parse particular operand types.
354 OperandMatchResultTy
355 parseGR32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000356 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, GR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000357 }
358 OperandMatchResultTy
359 parseGR64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000360 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, GR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000361 }
362 OperandMatchResultTy
363 parseGR128(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000364 return parseRegister(Operands, RegGR, SystemZMC::GR128Regs, GR128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000365 }
366 OperandMatchResultTy
367 parseADDR32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000368 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, ADDR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000369 }
370 OperandMatchResultTy
371 parseADDR64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000372 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, ADDR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000373 }
374 OperandMatchResultTy
375 parseADDR128(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
376 llvm_unreachable("Shouldn't be used as an operand");
377 }
378 OperandMatchResultTy
379 parseFP32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000380 return parseRegister(Operands, RegFP, SystemZMC::FP32Regs, FP32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000381 }
382 OperandMatchResultTy
383 parseFP64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000384 return parseRegister(Operands, RegFP, SystemZMC::FP64Regs, FP64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000385 }
386 OperandMatchResultTy
387 parseFP128(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000388 return parseRegister(Operands, RegFP, SystemZMC::FP128Regs, FP128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000389 }
390 OperandMatchResultTy
391 parseBDAddr32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000392 return parseAddress(Operands, SystemZMC::GR32Regs, ADDR32Reg, BDMem);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000393 }
394 OperandMatchResultTy
395 parseBDAddr64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000396 return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDMem);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000397 }
398 OperandMatchResultTy
399 parseBDXAddr64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford1d959002013-07-02 14:56:45 +0000400 return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDXMem);
401 }
402 OperandMatchResultTy
403 parseBDLAddr64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
404 return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDLMem);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000405 }
406 OperandMatchResultTy
407 parseAccessReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Richard Sandiford1fb58832013-05-14 09:47:26 +0000408 OperandMatchResultTy
409 parsePCRel(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
410 int64_t MinVal, int64_t MaxVal);
411 OperandMatchResultTy
412 parsePCRel16(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
413 return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1);
414 }
415 OperandMatchResultTy
416 parsePCRel32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
417 return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1);
418 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000419};
420}
421
422#define GET_REGISTER_MATCHER
423#define GET_SUBTARGET_FEATURE_NAME
424#define GET_MATCHER_IMPLEMENTATION
425#include "SystemZGenAsmMatcher.inc"
426
427void SystemZOperand::print(raw_ostream &OS) const {
428 llvm_unreachable("Not implemented");
429}
430
431// Parse one register of the form %<prefix><number>.
432bool SystemZAsmParser::parseRegister(Register &Reg) {
433 Reg.StartLoc = Parser.getTok().getLoc();
434
435 // Eat the % prefix.
436 if (Parser.getTok().isNot(AsmToken::Percent))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000437 return Error(Parser.getTok().getLoc(), "register expected");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000438 Parser.Lex();
439
440 // Expect a register name.
441 if (Parser.getTok().isNot(AsmToken::Identifier))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000442 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000443
Richard Sandiford675f8692013-05-24 14:14:38 +0000444 // Check that there's a prefix.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000445 StringRef Name = Parser.getTok().getString();
446 if (Name.size() < 2)
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000447 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000448 char Prefix = Name[0];
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000449
450 // Treat the rest of the register name as a register number.
Richard Sandiford675f8692013-05-24 14:14:38 +0000451 if (Name.substr(1).getAsInteger(10, Reg.Num))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000452 return Error(Reg.StartLoc, "invalid register");
Richard Sandiford675f8692013-05-24 14:14:38 +0000453
454 // Look for valid combinations of prefix and number.
455 if (Prefix == 'r' && Reg.Num < 16)
456 Reg.Group = RegGR;
457 else if (Prefix == 'f' && Reg.Num < 16)
458 Reg.Group = RegFP;
459 else if (Prefix == 'a' && Reg.Num < 16)
460 Reg.Group = RegAccess;
461 else
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000462 return Error(Reg.StartLoc, "invalid register");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000463
464 Reg.EndLoc = Parser.getTok().getLoc();
465 Parser.Lex();
466 return false;
467}
468
Richard Sandiford675f8692013-05-24 14:14:38 +0000469// Parse a register of group Group. If Regs is nonnull, use it to map
470// the raw register number to LLVM numbering, with zero entries indicating
471// an invalid register. IsAddress says whether the register appears in an
472// address context.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000473bool SystemZAsmParser::parseRegister(Register &Reg, RegisterGroup Group,
474 const unsigned *Regs, bool IsAddress) {
475 if (parseRegister(Reg))
476 return true;
477 if (Reg.Group != Group)
478 return Error(Reg.StartLoc, "invalid operand for instruction");
479 if (Regs && Regs[Reg.Num] == 0)
480 return Error(Reg.StartLoc, "invalid register pair");
481 if (Reg.Num == 0 && IsAddress)
482 return Error(Reg.StartLoc, "%r0 used in an address");
Richard Sandiford675f8692013-05-24 14:14:38 +0000483 if (Regs)
484 Reg.Num = Regs[Reg.Num];
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000485 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000486}
487
Richard Sandiford675f8692013-05-24 14:14:38 +0000488// Parse a register and add it to Operands. The other arguments are as above.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000489SystemZAsmParser::OperandMatchResultTy
490SystemZAsmParser::parseRegister(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Richard Sandiford675f8692013-05-24 14:14:38 +0000491 RegisterGroup Group, const unsigned *Regs,
Richard Sandiford1d959002013-07-02 14:56:45 +0000492 RegisterKind Kind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000493 if (Parser.getTok().isNot(AsmToken::Percent))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000494 return MatchOperand_NoMatch;
495
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000496 Register Reg;
Richard Sandiford1d959002013-07-02 14:56:45 +0000497 bool IsAddress = (Kind == ADDR32Reg || Kind == ADDR64Reg);
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000498 if (parseRegister(Reg, Group, Regs, IsAddress))
499 return MatchOperand_ParseFail;
500
501 Operands.push_back(SystemZOperand::createReg(Kind, Reg.Num,
502 Reg.StartLoc, Reg.EndLoc));
503 return MatchOperand_Success;
504}
505
Richard Sandiford1d959002013-07-02 14:56:45 +0000506// Parse a memory operand into Base, Disp, Index and Length.
507// Regs maps asm register numbers to LLVM register numbers and RegKind
508// says what kind of address register we're using (ADDR32Reg or ADDR64Reg).
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000509bool SystemZAsmParser::parseAddress(unsigned &Base, const MCExpr *&Disp,
Richard Sandiford1d959002013-07-02 14:56:45 +0000510 unsigned &Index, const MCExpr *&Length,
511 const unsigned *Regs,
512 RegisterKind RegKind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000513 // Parse the displacement, which must always be present.
514 if (getParser().parseExpression(Disp))
515 return true;
516
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000517 // Parse the optional base and index.
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000518 Index = 0;
519 Base = 0;
Richard Sandiford1d959002013-07-02 14:56:45 +0000520 Length = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000521 if (getLexer().is(AsmToken::LParen)) {
522 Parser.Lex();
523
Richard Sandiford1d959002013-07-02 14:56:45 +0000524 if (getLexer().is(AsmToken::Percent)) {
525 // Parse the first register and decide whether it's a base or an index.
526 Register Reg;
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000527 if (parseRegister(Reg, RegGR, Regs, RegKind))
528 return true;
Richard Sandiford1d959002013-07-02 14:56:45 +0000529 if (getLexer().is(AsmToken::Comma))
530 Index = Reg.Num;
531 else
532 Base = Reg.Num;
533 } else {
534 // Parse the length.
535 if (getParser().parseExpression(Length))
536 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000537 }
Richard Sandiford1d959002013-07-02 14:56:45 +0000538
539 // Check whether there's a second register. It's the base if so.
540 if (getLexer().is(AsmToken::Comma)) {
541 Parser.Lex();
542 Register Reg;
543 if (parseRegister(Reg, RegGR, Regs, RegKind))
544 return true;
545 Base = Reg.Num;
546 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000547
548 // Consume the closing bracket.
549 if (getLexer().isNot(AsmToken::RParen))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000550 return Error(Parser.getTok().getLoc(), "unexpected token in address");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000551 Parser.Lex();
552 }
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000553 return false;
554}
555
556// Parse a memory operand and add it to Operands. The other arguments
557// are as above.
558SystemZAsmParser::OperandMatchResultTy
559SystemZAsmParser::parseAddress(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Richard Sandiford1d959002013-07-02 14:56:45 +0000560 const unsigned *Regs, RegisterKind RegKind,
561 MemoryKind MemKind) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000562 SMLoc StartLoc = Parser.getTok().getLoc();
563 unsigned Base, Index;
564 const MCExpr *Disp;
Richard Sandiford1d959002013-07-02 14:56:45 +0000565 const MCExpr *Length;
566 if (parseAddress(Base, Disp, Index, Length, Regs, RegKind))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000567 return MatchOperand_ParseFail;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000568
Richard Sandiford1d959002013-07-02 14:56:45 +0000569 if (Index && MemKind != BDXMem)
570 {
571 Error(StartLoc, "invalid use of indexed addressing");
572 return MatchOperand_ParseFail;
573 }
574
575 if (Length && MemKind != BDLMem)
576 {
577 Error(StartLoc, "invalid use of length addressing");
578 return MatchOperand_ParseFail;
579 }
580
581 if (!Length && MemKind == BDLMem)
582 {
583 Error(StartLoc, "missing length in address");
584 return MatchOperand_ParseFail;
585 }
586
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000587 SMLoc EndLoc =
588 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
589 Operands.push_back(SystemZOperand::createMem(RegKind, Base, Disp, Index,
Richard Sandiford1d959002013-07-02 14:56:45 +0000590 Length, StartLoc, EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000591 return MatchOperand_Success;
592}
593
594bool SystemZAsmParser::ParseDirective(AsmToken DirectiveID) {
595 return true;
596}
597
598bool SystemZAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
599 SMLoc &EndLoc) {
600 Register Reg;
601 if (parseRegister(Reg))
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000602 return true;
Richard Sandiford675f8692013-05-24 14:14:38 +0000603 if (Reg.Group == RegGR)
604 RegNo = SystemZMC::GR64Regs[Reg.Num];
605 else if (Reg.Group == RegFP)
606 RegNo = SystemZMC::FP64Regs[Reg.Num];
607 else
608 // FIXME: Access registers aren't modelled as LLVM registers yet.
609 return Error(Reg.StartLoc, "invalid operand for instruction");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000610 StartLoc = Reg.StartLoc;
611 EndLoc = Reg.EndLoc;
612 return false;
613}
614
615bool SystemZAsmParser::
616ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
617 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
618 Operands.push_back(SystemZOperand::createToken(Name, NameLoc));
619
620 // Read the remaining operands.
621 if (getLexer().isNot(AsmToken::EndOfStatement)) {
622 // Read the first operand.
623 if (parseOperand(Operands, Name)) {
624 Parser.eatToEndOfStatement();
625 return true;
626 }
627
628 // Read any subsequent operands.
629 while (getLexer().is(AsmToken::Comma)) {
630 Parser.Lex();
631 if (parseOperand(Operands, Name)) {
632 Parser.eatToEndOfStatement();
633 return true;
634 }
635 }
636 if (getLexer().isNot(AsmToken::EndOfStatement)) {
637 SMLoc Loc = getLexer().getLoc();
638 Parser.eatToEndOfStatement();
639 return Error(Loc, "unexpected token in argument list");
640 }
641 }
642
643 // Consume the EndOfStatement.
644 Parser.Lex();
645 return false;
646}
647
648bool SystemZAsmParser::
649parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
650 StringRef Mnemonic) {
651 // Check if the current operand has a custom associated parser, if so, try to
652 // custom parse the operand, or fallback to the general approach.
653 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
654 if (ResTy == MatchOperand_Success)
655 return false;
656
657 // If there wasn't a custom match, try the generic matcher below. Otherwise,
658 // there was a match, but an error occurred, in which case, just return that
659 // the operand parsing failed.
660 if (ResTy == MatchOperand_ParseFail)
661 return true;
662
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000663 // Check for a register. All real register operands should have used
664 // a context-dependent parse routine, which gives the required register
665 // class. The code is here to mop up other cases, like those where
666 // the instruction isn't recognized.
667 if (Parser.getTok().is(AsmToken::Percent)) {
668 Register Reg;
669 if (parseRegister(Reg))
670 return true;
671 Operands.push_back(SystemZOperand::createInvalid(Reg.StartLoc, Reg.EndLoc));
672 return false;
673 }
674
675 // The only other type of operand is an immediate or address. As above,
676 // real address operands should have used a context-dependent parse routine,
677 // so we treat any plain expression as an immediate.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000678 SMLoc StartLoc = Parser.getTok().getLoc();
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000679 unsigned Base, Index;
Richard Sandiford1d959002013-07-02 14:56:45 +0000680 const MCExpr *Expr, *Length;
681 if (parseAddress(Base, Expr, Index, Length, SystemZMC::GR64Regs, ADDR64Reg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000682 return true;
683
684 SMLoc EndLoc =
685 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Richard Sandiford1d959002013-07-02 14:56:45 +0000686 if (Base || Index || Length)
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000687 Operands.push_back(SystemZOperand::createInvalid(StartLoc, EndLoc));
688 else
689 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000690 return false;
691}
692
693bool SystemZAsmParser::
694MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
695 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
696 MCStreamer &Out, unsigned &ErrorInfo,
697 bool MatchingInlineAsm) {
698 MCInst Inst;
699 unsigned MatchResult;
700
701 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
702 MatchingInlineAsm);
703 switch (MatchResult) {
704 default: break;
705 case Match_Success:
706 Inst.setLoc(IDLoc);
707 Out.EmitInstruction(Inst);
708 return false;
709
710 case Match_MissingFeature: {
711 assert(ErrorInfo && "Unknown missing feature!");
712 // Special case the error message for the very common case where only
713 // a single subtarget feature is missing
714 std::string Msg = "instruction requires:";
715 unsigned Mask = 1;
716 for (unsigned I = 0; I < sizeof(ErrorInfo) * 8 - 1; ++I) {
717 if (ErrorInfo & Mask) {
718 Msg += " ";
719 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
720 }
721 Mask <<= 1;
722 }
723 return Error(IDLoc, Msg);
724 }
725
726 case Match_InvalidOperand: {
727 SMLoc ErrorLoc = IDLoc;
728 if (ErrorInfo != ~0U) {
729 if (ErrorInfo >= Operands.size())
730 return Error(IDLoc, "too few operands for instruction");
731
732 ErrorLoc = ((SystemZOperand*)Operands[ErrorInfo])->getStartLoc();
733 if (ErrorLoc == SMLoc())
734 ErrorLoc = IDLoc;
735 }
736 return Error(ErrorLoc, "invalid operand for instruction");
737 }
738
739 case Match_MnemonicFail:
740 return Error(IDLoc, "invalid instruction");
741 }
742
743 llvm_unreachable("Unexpected match type");
744}
745
746SystemZAsmParser::OperandMatchResultTy SystemZAsmParser::
747parseAccessReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000748 if (Parser.getTok().isNot(AsmToken::Percent))
749 return MatchOperand_NoMatch;
750
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000751 Register Reg;
Richard Sandiforddc5ed712013-05-24 14:26:46 +0000752 if (parseRegister(Reg, RegAccess, 0))
753 return MatchOperand_ParseFail;
754
755 Operands.push_back(SystemZOperand::createAccessReg(Reg.Num,
756 Reg.StartLoc,
757 Reg.EndLoc));
758 return MatchOperand_Success;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000759}
760
Richard Sandiford1fb58832013-05-14 09:47:26 +0000761SystemZAsmParser::OperandMatchResultTy SystemZAsmParser::
762parsePCRel(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
763 int64_t MinVal, int64_t MaxVal) {
764 MCContext &Ctx = getContext();
765 MCStreamer &Out = getStreamer();
766 const MCExpr *Expr;
767 SMLoc StartLoc = Parser.getTok().getLoc();
768 if (getParser().parseExpression(Expr))
769 return MatchOperand_NoMatch;
770
771 // For consistency with the GNU assembler, treat immediates as offsets
772 // from ".".
773 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) {
774 int64_t Value = CE->getValue();
775 if ((Value & 1) || Value < MinVal || Value > MaxVal) {
776 Error(StartLoc, "offset out of range");
777 return MatchOperand_ParseFail;
778 }
779 MCSymbol *Sym = Ctx.CreateTempSymbol();
780 Out.EmitLabel(Sym);
781 const MCExpr *Base = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
782 Ctx);
783 Expr = Value == 0 ? Base : MCBinaryExpr::CreateAdd(Base, Expr, Ctx);
784 }
785
786 SMLoc EndLoc =
787 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
788 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
789 return MatchOperand_Success;
790}
791
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000792// Force static initialization.
793extern "C" void LLVMInitializeSystemZAsmParser() {
794 RegisterMCAsmParser<SystemZAsmParser> X(TheSystemZTarget);
795}