blob: d13234cafdba0576c9dab7ef04213c9f390c1683 [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"
11#include "llvm/MC/MCExpr.h"
12#include "llvm/MC/MCInst.h"
13#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
14#include "llvm/MC/MCStreamer.h"
15#include "llvm/MC/MCSubtargetInfo.h"
16#include "llvm/MC/MCTargetAsmParser.h"
17#include "llvm/Support/TargetRegistry.h"
18
19using namespace llvm;
20
21// Return true if Expr is in the range [MinValue, MaxValue].
22static bool inRange(const MCExpr *Expr, int64_t MinValue, int64_t MaxValue) {
23 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) {
24 int64_t Value = CE->getValue();
25 return Value >= MinValue && Value <= MaxValue;
26 }
27 return false;
28}
29
30namespace {
31class SystemZOperand : public MCParsedAsmOperand {
32public:
33 enum RegisterKind {
34 GR32Reg,
35 GR64Reg,
36 GR128Reg,
37 ADDR32Reg,
38 ADDR64Reg,
39 FP32Reg,
40 FP64Reg,
41 FP128Reg
42 };
43
44private:
45 enum OperandKind {
46 KindToken,
47 KindReg,
48 KindAccessReg,
49 KindImm,
50 KindMem
51 };
52
53 OperandKind Kind;
54 SMLoc StartLoc, EndLoc;
55
56 // A string of length Length, starting at Data.
57 struct TokenOp {
58 const char *Data;
59 unsigned Length;
60 };
61
62 // LLVM register Num, which has kind Kind.
63 struct RegOp {
64 RegisterKind Kind;
65 unsigned Num;
66 };
67
68 // Base + Disp + Index, where Base and Index are LLVM registers or 0.
69 // RegKind says what type the registers have (ADDR32Reg or ADDR64Reg).
70 struct MemOp {
71 unsigned Base : 8;
72 unsigned Index : 8;
73 unsigned RegKind : 8;
74 unsigned Unused : 8;
75 const MCExpr *Disp;
76 };
77
78 union {
79 TokenOp Token;
80 RegOp Reg;
81 unsigned AccessReg;
82 const MCExpr *Imm;
83 MemOp Mem;
84 };
85
86 SystemZOperand(OperandKind kind, SMLoc startLoc, SMLoc endLoc)
87 : Kind(kind), StartLoc(startLoc), EndLoc(endLoc)
88 {}
89
90 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
91 // Add as immediates when possible. Null MCExpr = 0.
92 if (Expr == 0)
93 Inst.addOperand(MCOperand::CreateImm(0));
94 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
95 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
96 else
97 Inst.addOperand(MCOperand::CreateExpr(Expr));
98 }
99
100public:
101 // Create particular kinds of operand.
102 static SystemZOperand *createToken(StringRef Str, SMLoc Loc) {
103 SystemZOperand *Op = new SystemZOperand(KindToken, Loc, Loc);
104 Op->Token.Data = Str.data();
105 Op->Token.Length = Str.size();
106 return Op;
107 }
108 static SystemZOperand *createReg(RegisterKind Kind, unsigned Num,
109 SMLoc StartLoc, SMLoc EndLoc) {
110 SystemZOperand *Op = new SystemZOperand(KindReg, StartLoc, EndLoc);
111 Op->Reg.Kind = Kind;
112 Op->Reg.Num = Num;
113 return Op;
114 }
115 static SystemZOperand *createAccessReg(unsigned Num, SMLoc StartLoc,
116 SMLoc EndLoc) {
117 SystemZOperand *Op = new SystemZOperand(KindAccessReg, StartLoc, EndLoc);
118 Op->AccessReg = Num;
119 return Op;
120 }
121 static SystemZOperand *createImm(const MCExpr *Expr, SMLoc StartLoc,
122 SMLoc EndLoc) {
123 SystemZOperand *Op = new SystemZOperand(KindImm, StartLoc, EndLoc);
124 Op->Imm = Expr;
125 return Op;
126 }
127 static SystemZOperand *createMem(RegisterKind RegKind, unsigned Base,
128 const MCExpr *Disp, unsigned Index,
129 SMLoc StartLoc, SMLoc EndLoc) {
130 SystemZOperand *Op = new SystemZOperand(KindMem, StartLoc, EndLoc);
131 Op->Mem.RegKind = RegKind;
132 Op->Mem.Base = Base;
133 Op->Mem.Index = Index;
134 Op->Mem.Disp = Disp;
135 return Op;
136 }
137
138 // Token operands
139 virtual bool isToken() const LLVM_OVERRIDE {
140 return Kind == KindToken;
141 }
142 StringRef getToken() const {
143 assert(Kind == KindToken && "Not a token");
144 return StringRef(Token.Data, Token.Length);
145 }
146
147 // Register operands.
148 virtual bool isReg() const LLVM_OVERRIDE {
149 return Kind == KindReg;
150 }
151 bool isReg(RegisterKind RegKind) const {
152 return Kind == KindReg && Reg.Kind == RegKind;
153 }
154 virtual unsigned getReg() const LLVM_OVERRIDE {
155 assert(Kind == KindReg && "Not a register");
156 return Reg.Num;
157 }
158
159 // Access register operands. Access registers aren't exposed to LLVM
160 // as registers.
161 bool isAccessReg() const {
162 return Kind == KindAccessReg;
163 }
164
165 // Immediate operands.
166 virtual bool isImm() const LLVM_OVERRIDE {
167 return Kind == KindImm;
168 }
169 bool isImm(int64_t MinValue, int64_t MaxValue) const {
170 return Kind == KindImm && inRange(Imm, MinValue, MaxValue);
171 }
172 const MCExpr *getImm() const {
173 assert(Kind == KindImm && "Not an immediate");
174 return Imm;
175 }
176
177 // Memory operands.
178 virtual bool isMem() const LLVM_OVERRIDE {
179 return Kind == KindMem;
180 }
181 bool isMem(RegisterKind RegKind, bool HasIndex) const {
182 return (Kind == KindMem &&
183 Mem.RegKind == RegKind &&
184 (HasIndex || !Mem.Index));
185 }
186 bool isMemDisp12(RegisterKind RegKind, bool HasIndex) const {
187 return isMem(RegKind, HasIndex) && inRange(Mem.Disp, 0, 0xfff);
188 }
189 bool isMemDisp20(RegisterKind RegKind, bool HasIndex) const {
190 return isMem(RegKind, HasIndex) && inRange(Mem.Disp, -524288, 524287);
191 }
192
193 // Override MCParsedAsmOperand.
194 virtual SMLoc getStartLoc() const LLVM_OVERRIDE { return StartLoc; }
195 virtual SMLoc getEndLoc() const LLVM_OVERRIDE { return EndLoc; }
196 virtual void print(raw_ostream &OS) const LLVM_OVERRIDE;
197
198 // Used by the TableGen code to add particular types of operand
199 // to an instruction.
200 void addRegOperands(MCInst &Inst, unsigned N) const {
201 assert(N == 1 && "Invalid number of operands");
202 Inst.addOperand(MCOperand::CreateReg(getReg()));
203 }
204 void addAccessRegOperands(MCInst &Inst, unsigned N) const {
205 assert(N == 1 && "Invalid number of operands");
206 assert(Kind == KindAccessReg && "Invalid operand type");
207 Inst.addOperand(MCOperand::CreateImm(AccessReg));
208 }
209 void addImmOperands(MCInst &Inst, unsigned N) const {
210 assert(N == 1 && "Invalid number of operands");
211 addExpr(Inst, getImm());
212 }
213 void addBDAddrOperands(MCInst &Inst, unsigned N) const {
214 assert(N == 2 && "Invalid number of operands");
215 assert(Kind == KindMem && Mem.Index == 0 && "Invalid operand type");
216 Inst.addOperand(MCOperand::CreateReg(Mem.Base));
217 addExpr(Inst, Mem.Disp);
218 }
219 void addBDXAddrOperands(MCInst &Inst, unsigned N) const {
220 assert(N == 3 && "Invalid number of operands");
221 assert(Kind == KindMem && "Invalid operand type");
222 Inst.addOperand(MCOperand::CreateReg(Mem.Base));
223 addExpr(Inst, Mem.Disp);
224 Inst.addOperand(MCOperand::CreateReg(Mem.Index));
225 }
226
227 // Used by the TableGen code to check for particular operand types.
228 bool isGR32() const { return isReg(GR32Reg); }
229 bool isGR64() const { return isReg(GR64Reg); }
230 bool isGR128() const { return isReg(GR128Reg); }
231 bool isADDR32() const { return isReg(ADDR32Reg); }
232 bool isADDR64() const { return isReg(ADDR64Reg); }
233 bool isADDR128() const { return false; }
234 bool isFP32() const { return isReg(FP32Reg); }
235 bool isFP64() const { return isReg(FP64Reg); }
236 bool isFP128() const { return isReg(FP128Reg); }
237 bool isBDAddr32Disp12() const { return isMemDisp12(ADDR32Reg, false); }
238 bool isBDAddr32Disp20() const { return isMemDisp20(ADDR32Reg, false); }
239 bool isBDAddr64Disp12() const { return isMemDisp12(ADDR64Reg, false); }
240 bool isBDAddr64Disp20() const { return isMemDisp20(ADDR64Reg, false); }
241 bool isBDXAddr64Disp12() const { return isMemDisp12(ADDR64Reg, true); }
242 bool isBDXAddr64Disp20() const { return isMemDisp20(ADDR64Reg, true); }
243 bool isU4Imm() const { return isImm(0, 15); }
244 bool isU6Imm() const { return isImm(0, 63); }
245 bool isU8Imm() const { return isImm(0, 255); }
246 bool isS8Imm() const { return isImm(-128, 127); }
247 bool isU16Imm() const { return isImm(0, 65535); }
248 bool isS16Imm() const { return isImm(-32768, 32767); }
249 bool isU32Imm() const { return isImm(0, (1LL << 32) - 1); }
250 bool isS32Imm() const { return isImm(-(1LL << 31), (1LL << 31) - 1); }
251};
252
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000253class SystemZAsmParser : public MCTargetAsmParser {
254#define GET_ASSEMBLER_HEADER
255#include "SystemZGenAsmMatcher.inc"
256
257private:
258 MCSubtargetInfo &STI;
259 MCAsmParser &Parser;
260 struct Register {
261 char Prefix;
262 unsigned Number;
263 SMLoc StartLoc, EndLoc;
264 };
265
266 bool parseRegister(Register &Reg);
267
268 OperandMatchResultTy
269 parseRegister(Register &Reg, char Prefix, const unsigned *Regs,
270 bool IsAddress = false);
271
272 OperandMatchResultTy
273 parseRegister(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
274 char Prefix, const unsigned *Regs,
275 SystemZOperand::RegisterKind Kind,
276 bool IsAddress = false);
277
278 OperandMatchResultTy
279 parseAddress(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
280 const unsigned *Regs, SystemZOperand::RegisterKind RegKind,
281 bool HasIndex);
282
283 bool parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
284 StringRef Mnemonic);
285
286public:
287 SystemZAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
288 : MCTargetAsmParser(), STI(sti), Parser(parser) {
289 MCAsmParserExtension::Initialize(Parser);
290
291 // Initialize the set of available features.
292 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
293 }
294
295 // Override MCTargetAsmParser.
296 virtual bool ParseDirective(AsmToken DirectiveID) LLVM_OVERRIDE;
297 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
298 SMLoc &EndLoc) LLVM_OVERRIDE;
299 virtual bool ParseInstruction(ParseInstructionInfo &Info,
300 StringRef Name, SMLoc NameLoc,
301 SmallVectorImpl<MCParsedAsmOperand*> &Operands)
302 LLVM_OVERRIDE;
303 virtual bool
304 MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
305 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
306 MCStreamer &Out, unsigned &ErrorInfo,
307 bool MatchingInlineAsm) LLVM_OVERRIDE;
308
309 // Used by the TableGen code to parse particular operand types.
310 OperandMatchResultTy
311 parseGR32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford7d37cd22013-05-14 09:36:44 +0000312 return parseRegister(Operands, 'r', SystemZMC::GR32Regs,
313 SystemZOperand::GR32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000314 }
315 OperandMatchResultTy
316 parseGR64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford7d37cd22013-05-14 09:36:44 +0000317 return parseRegister(Operands, 'r', SystemZMC::GR64Regs,
318 SystemZOperand::GR64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000319 }
320 OperandMatchResultTy
321 parseGR128(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford7d37cd22013-05-14 09:36:44 +0000322 return parseRegister(Operands, 'r', SystemZMC::GR128Regs,
323 SystemZOperand::GR128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000324 }
325 OperandMatchResultTy
326 parseADDR32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford7d37cd22013-05-14 09:36:44 +0000327 return parseRegister(Operands, 'r', SystemZMC::GR32Regs,
328 SystemZOperand::ADDR32Reg, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000329 }
330 OperandMatchResultTy
331 parseADDR64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford7d37cd22013-05-14 09:36:44 +0000332 return parseRegister(Operands, 'r', SystemZMC::GR64Regs,
333 SystemZOperand::ADDR64Reg, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000334 }
335 OperandMatchResultTy
336 parseADDR128(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
337 llvm_unreachable("Shouldn't be used as an operand");
338 }
339 OperandMatchResultTy
340 parseFP32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford7d37cd22013-05-14 09:36:44 +0000341 return parseRegister(Operands, 'f', SystemZMC::FP32Regs,
342 SystemZOperand::FP32Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000343 }
344 OperandMatchResultTy
345 parseFP64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford7d37cd22013-05-14 09:36:44 +0000346 return parseRegister(Operands, 'f', SystemZMC::FP64Regs,
347 SystemZOperand::FP64Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000348 }
349 OperandMatchResultTy
350 parseFP128(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford7d37cd22013-05-14 09:36:44 +0000351 return parseRegister(Operands, 'f', SystemZMC::FP128Regs,
352 SystemZOperand::FP128Reg);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000353 }
354 OperandMatchResultTy
355 parseBDAddr32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford7d37cd22013-05-14 09:36:44 +0000356 return parseAddress(Operands, SystemZMC::GR32Regs,
357 SystemZOperand::ADDR32Reg, false);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000358 }
359 OperandMatchResultTy
360 parseBDAddr64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford7d37cd22013-05-14 09:36:44 +0000361 return parseAddress(Operands, SystemZMC::GR64Regs,
362 SystemZOperand::ADDR64Reg, false);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000363 }
364 OperandMatchResultTy
365 parseBDXAddr64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Richard Sandiford7d37cd22013-05-14 09:36:44 +0000366 return parseAddress(Operands, SystemZMC::GR64Regs,
367 SystemZOperand::ADDR64Reg, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000368 }
369 OperandMatchResultTy
370 parseAccessReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
371};
372}
373
374#define GET_REGISTER_MATCHER
375#define GET_SUBTARGET_FEATURE_NAME
376#define GET_MATCHER_IMPLEMENTATION
377#include "SystemZGenAsmMatcher.inc"
378
379void SystemZOperand::print(raw_ostream &OS) const {
380 llvm_unreachable("Not implemented");
381}
382
383// Parse one register of the form %<prefix><number>.
384bool SystemZAsmParser::parseRegister(Register &Reg) {
385 Reg.StartLoc = Parser.getTok().getLoc();
386
387 // Eat the % prefix.
388 if (Parser.getTok().isNot(AsmToken::Percent))
389 return true;
390 Parser.Lex();
391
392 // Expect a register name.
393 if (Parser.getTok().isNot(AsmToken::Identifier))
394 return true;
395
396 // Check the prefix.
397 StringRef Name = Parser.getTok().getString();
398 if (Name.size() < 2)
399 return true;
400 Reg.Prefix = Name[0];
401
402 // Treat the rest of the register name as a register number.
403 if (Name.substr(1).getAsInteger(10, Reg.Number))
404 return true;
405
406 Reg.EndLoc = Parser.getTok().getLoc();
407 Parser.Lex();
408 return false;
409}
410
411// Parse a register with prefix Prefix and convert it to LLVM numbering.
412// Regs maps asm register numbers to LLVM register numbers, with zero
413// entries indicating an invalid register. IsAddress says whether the
414// register appears in an address context.
415SystemZAsmParser::OperandMatchResultTy
416SystemZAsmParser::parseRegister(Register &Reg, char Prefix,
417 const unsigned *Regs, bool IsAddress) {
418 if (parseRegister(Reg))
419 return MatchOperand_NoMatch;
420 if (Reg.Prefix != Prefix || Reg.Number > 15 || Regs[Reg.Number] == 0) {
421 Error(Reg.StartLoc, "invalid register");
422 return MatchOperand_ParseFail;
423 }
424 if (Reg.Number == 0 && IsAddress) {
425 Error(Reg.StartLoc, "%r0 used in an address");
426 return MatchOperand_ParseFail;
427 }
428 Reg.Number = Regs[Reg.Number];
429 return MatchOperand_Success;
430}
431
432// Parse a register and add it to Operands. Prefix is 'r' for GPRs,
433// 'f' for FPRs, etc. Regs maps asm register numbers to LLVM register numbers,
434// with zero entries indicating an invalid register. Kind is the type of
435// register represented by Regs and IsAddress says whether the register is
436// being parsed in an address context, meaning that %r0 evaluates as 0.
437SystemZAsmParser::OperandMatchResultTy
438SystemZAsmParser::parseRegister(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
439 char Prefix, const unsigned *Regs,
440 SystemZOperand::RegisterKind Kind,
441 bool IsAddress) {
442 Register Reg;
443 OperandMatchResultTy Result = parseRegister(Reg, Prefix, Regs, IsAddress);
444 if (Result == MatchOperand_Success)
445 Operands.push_back(SystemZOperand::createReg(Kind, Reg.Number,
446 Reg.StartLoc, Reg.EndLoc));
447 return Result;
448}
449
450// Parse a memory operand and add it to Operands. Regs maps asm register
451// numbers to LLVM address registers and RegKind says what kind of address
452// register we're using (ADDR32Reg or ADDR64Reg). HasIndex says whether
453// the address allows index registers.
454SystemZAsmParser::OperandMatchResultTy
455SystemZAsmParser::parseAddress(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
456 const unsigned *Regs,
457 SystemZOperand::RegisterKind RegKind,
458 bool HasIndex) {
459 SMLoc StartLoc = Parser.getTok().getLoc();
460
461 // Parse the displacement, which must always be present.
462 const MCExpr *Disp;
463 if (getParser().parseExpression(Disp))
464 return MatchOperand_NoMatch;
465
466 // Parse the optional base and index.
467 unsigned Index = 0;
468 unsigned Base = 0;
469 if (getLexer().is(AsmToken::LParen)) {
470 Parser.Lex();
471
472 // Parse the first register.
473 Register Reg;
Richard Sandiford7d37cd22013-05-14 09:36:44 +0000474 OperandMatchResultTy Result = parseRegister(Reg, 'r', SystemZMC::GR64Regs,
475 true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000476 if (Result != MatchOperand_Success)
477 return Result;
478
479 // Check whether there's a second register. If so, the one that we
480 // just parsed was the index.
481 if (getLexer().is(AsmToken::Comma)) {
482 Parser.Lex();
483
484 if (!HasIndex) {
485 Error(Reg.StartLoc, "invalid use of indexed addressing");
486 return MatchOperand_ParseFail;
487 }
488
489 Index = Reg.Number;
Richard Sandiford7d37cd22013-05-14 09:36:44 +0000490 Result = parseRegister(Reg, 'r', SystemZMC::GR64Regs, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000491 if (Result != MatchOperand_Success)
492 return Result;
493 }
494 Base = Reg.Number;
495
496 // Consume the closing bracket.
497 if (getLexer().isNot(AsmToken::RParen))
498 return MatchOperand_NoMatch;
499 Parser.Lex();
500 }
501
502 SMLoc EndLoc =
503 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
504 Operands.push_back(SystemZOperand::createMem(RegKind, Base, Disp, Index,
505 StartLoc, EndLoc));
506 return MatchOperand_Success;
507}
508
509bool SystemZAsmParser::ParseDirective(AsmToken DirectiveID) {
510 return true;
511}
512
513bool SystemZAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
514 SMLoc &EndLoc) {
515 Register Reg;
516 if (parseRegister(Reg))
517 return Error(Reg.StartLoc, "register expected");
518 if (Reg.Prefix == 'r' && Reg.Number < 16)
Richard Sandiford7d37cd22013-05-14 09:36:44 +0000519 RegNo = SystemZMC::GR64Regs[Reg.Number];
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000520 else if (Reg.Prefix == 'f' && Reg.Number < 16)
Richard Sandiford7d37cd22013-05-14 09:36:44 +0000521 RegNo = SystemZMC::FP64Regs[Reg.Number];
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000522 else
523 return Error(Reg.StartLoc, "invalid register");
524 StartLoc = Reg.StartLoc;
525 EndLoc = Reg.EndLoc;
526 return false;
527}
528
529bool SystemZAsmParser::
530ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
531 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
532 Operands.push_back(SystemZOperand::createToken(Name, NameLoc));
533
534 // Read the remaining operands.
535 if (getLexer().isNot(AsmToken::EndOfStatement)) {
536 // Read the first operand.
537 if (parseOperand(Operands, Name)) {
538 Parser.eatToEndOfStatement();
539 return true;
540 }
541
542 // Read any subsequent operands.
543 while (getLexer().is(AsmToken::Comma)) {
544 Parser.Lex();
545 if (parseOperand(Operands, Name)) {
546 Parser.eatToEndOfStatement();
547 return true;
548 }
549 }
550 if (getLexer().isNot(AsmToken::EndOfStatement)) {
551 SMLoc Loc = getLexer().getLoc();
552 Parser.eatToEndOfStatement();
553 return Error(Loc, "unexpected token in argument list");
554 }
555 }
556
557 // Consume the EndOfStatement.
558 Parser.Lex();
559 return false;
560}
561
562bool SystemZAsmParser::
563parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
564 StringRef Mnemonic) {
565 // Check if the current operand has a custom associated parser, if so, try to
566 // custom parse the operand, or fallback to the general approach.
567 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
568 if (ResTy == MatchOperand_Success)
569 return false;
570
571 // If there wasn't a custom match, try the generic matcher below. Otherwise,
572 // there was a match, but an error occurred, in which case, just return that
573 // the operand parsing failed.
574 if (ResTy == MatchOperand_ParseFail)
575 return true;
576
577 // The only other type of operand is an immediate.
578 const MCExpr *Expr;
579 SMLoc StartLoc = Parser.getTok().getLoc();
580 if (getParser().parseExpression(Expr))
581 return true;
582
583 SMLoc EndLoc =
584 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
585 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
586 return false;
587}
588
589bool SystemZAsmParser::
590MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
591 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
592 MCStreamer &Out, unsigned &ErrorInfo,
593 bool MatchingInlineAsm) {
594 MCInst Inst;
595 unsigned MatchResult;
596
597 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
598 MatchingInlineAsm);
599 switch (MatchResult) {
600 default: break;
601 case Match_Success:
602 Inst.setLoc(IDLoc);
603 Out.EmitInstruction(Inst);
604 return false;
605
606 case Match_MissingFeature: {
607 assert(ErrorInfo && "Unknown missing feature!");
608 // Special case the error message for the very common case where only
609 // a single subtarget feature is missing
610 std::string Msg = "instruction requires:";
611 unsigned Mask = 1;
612 for (unsigned I = 0; I < sizeof(ErrorInfo) * 8 - 1; ++I) {
613 if (ErrorInfo & Mask) {
614 Msg += " ";
615 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
616 }
617 Mask <<= 1;
618 }
619 return Error(IDLoc, Msg);
620 }
621
622 case Match_InvalidOperand: {
623 SMLoc ErrorLoc = IDLoc;
624 if (ErrorInfo != ~0U) {
625 if (ErrorInfo >= Operands.size())
626 return Error(IDLoc, "too few operands for instruction");
627
628 ErrorLoc = ((SystemZOperand*)Operands[ErrorInfo])->getStartLoc();
629 if (ErrorLoc == SMLoc())
630 ErrorLoc = IDLoc;
631 }
632 return Error(ErrorLoc, "invalid operand for instruction");
633 }
634
635 case Match_MnemonicFail:
636 return Error(IDLoc, "invalid instruction");
637 }
638
639 llvm_unreachable("Unexpected match type");
640}
641
642SystemZAsmParser::OperandMatchResultTy SystemZAsmParser::
643parseAccessReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
644 Register Reg;
645 if (parseRegister(Reg))
646 return MatchOperand_NoMatch;
647 if (Reg.Prefix != 'a' || Reg.Number > 15) {
648 Error(Reg.StartLoc, "invalid register");
649 return MatchOperand_ParseFail;
650 }
651 Operands.push_back(SystemZOperand::createAccessReg(Reg.Number,
652 Reg.StartLoc, Reg.EndLoc));
653 return MatchOperand_Success;
654}
655
656// Force static initialization.
657extern "C" void LLVMInitializeSystemZAsmParser() {
658 RegisterMCAsmParser<SystemZAsmParser> X(TheSystemZTarget);
659}