1 //===-- SystemZAsmParser.cpp - Parse SystemZ assembly instructions --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/SystemZMCTargetDesc.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/MC/MCContext.h"
13 #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"
23 // Return true if Expr is in the range [MinValue, MaxValue].
24 static 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;
51 class SystemZOperand : public MCParsedAsmOperand {
64 SMLoc StartLoc, EndLoc;
66 // A string of length Length, starting at Data.
72 // 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.
86 // Base + Disp + Index, where Base and Index are LLVM registers or 0.
87 // RegKind says what type the registers have (ADDR32Reg or ADDR64Reg).
88 // Length is the operand length for D(L,B)-style operands, otherwise
107 SystemZOperand(OperandKind kind, SMLoc startLoc, SMLoc endLoc)
108 : Kind(kind), StartLoc(startLoc), EndLoc(endLoc)
111 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
112 // Add as immediates when possible. Null MCExpr = 0.
114 Inst.addOperand(MCOperand::CreateImm(0));
115 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
116 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
118 Inst.addOperand(MCOperand::CreateExpr(Expr));
122 // Create particular kinds of operand.
123 static SystemZOperand *createInvalid(SMLoc StartLoc, SMLoc EndLoc) {
124 return new SystemZOperand(KindInvalid, StartLoc, EndLoc);
126 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();
132 static SystemZOperand *createReg(RegisterKind Kind, unsigned Num,
133 SMLoc StartLoc, SMLoc EndLoc) {
134 SystemZOperand *Op = new SystemZOperand(KindReg, StartLoc, EndLoc);
139 static SystemZOperand *createAccessReg(unsigned Num, SMLoc StartLoc,
141 SystemZOperand *Op = new SystemZOperand(KindAccessReg, StartLoc, EndLoc);
145 static SystemZOperand *createImm(const MCExpr *Expr, SMLoc StartLoc,
147 SystemZOperand *Op = new SystemZOperand(KindImm, StartLoc, EndLoc);
151 static SystemZOperand *createMem(RegisterKind RegKind, unsigned Base,
152 const MCExpr *Disp, unsigned Index,
153 const MCExpr *Length, SMLoc StartLoc,
155 SystemZOperand *Op = new SystemZOperand(KindMem, StartLoc, EndLoc);
156 Op->Mem.RegKind = RegKind;
158 Op->Mem.Index = Index;
160 Op->Mem.Length = Length;
165 virtual bool isToken() const LLVM_OVERRIDE {
166 return Kind == KindToken;
168 StringRef getToken() const {
169 assert(Kind == KindToken && "Not a token");
170 return StringRef(Token.Data, Token.Length);
173 // Register operands.
174 virtual bool isReg() const LLVM_OVERRIDE {
175 return Kind == KindReg;
177 bool isReg(RegisterKind RegKind) const {
178 return Kind == KindReg && Reg.Kind == RegKind;
180 virtual unsigned getReg() const LLVM_OVERRIDE {
181 assert(Kind == KindReg && "Not a register");
185 // Access register operands. Access registers aren't exposed to LLVM
187 bool isAccessReg() const {
188 return Kind == KindAccessReg;
191 // Immediate operands.
192 virtual bool isImm() const LLVM_OVERRIDE {
193 return Kind == KindImm;
195 bool isImm(int64_t MinValue, int64_t MaxValue) const {
196 return Kind == KindImm && inRange(Imm, MinValue, MaxValue);
198 const MCExpr *getImm() const {
199 assert(Kind == KindImm && "Not an immediate");
204 virtual bool isMem() const LLVM_OVERRIDE {
205 return Kind == KindMem;
207 bool isMem(RegisterKind RegKind, MemoryKind MemKind) const {
208 return (Kind == KindMem &&
209 Mem.RegKind == RegKind &&
210 (MemKind == BDXMem || !Mem.Index) &&
211 (MemKind == BDLMem) == (Mem.Length != 0));
213 bool isMemDisp12(RegisterKind RegKind, MemoryKind MemKind) const {
214 return isMem(RegKind, MemKind) && inRange(Mem.Disp, 0, 0xfff);
216 bool isMemDisp20(RegisterKind RegKind, MemoryKind MemKind) const {
217 return isMem(RegKind, MemKind) && inRange(Mem.Disp, -524288, 524287);
219 bool isMemDisp12Len8(RegisterKind RegKind) const {
220 return isMemDisp12(RegKind, BDLMem) && inRange(Mem.Length, 1, 0x100);
223 // Override MCParsedAsmOperand.
224 virtual SMLoc getStartLoc() const LLVM_OVERRIDE { return StartLoc; }
225 virtual SMLoc getEndLoc() const LLVM_OVERRIDE { return EndLoc; }
226 virtual void print(raw_ostream &OS) const LLVM_OVERRIDE;
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()));
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));
239 void addImmOperands(MCInst &Inst, unsigned N) const {
240 assert(N == 1 && "Invalid number of operands");
241 addExpr(Inst, getImm());
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);
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));
256 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);
264 // Used by the TableGen code to check for particular operand types.
265 bool isGR32() const { return isReg(GR32Reg); }
266 bool isGRH32() const { return isReg(GRH32Reg); }
267 bool isGRX32() const { return false; }
268 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); }
276 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); }
283 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); }
293 class SystemZAsmParser : public MCTargetAsmParser {
294 #define GET_ASSEMBLER_HEADER
295 #include "SystemZGenAsmMatcher.inc"
298 MCSubtargetInfo &STI;
308 SMLoc StartLoc, EndLoc;
311 bool parseRegister(Register &Reg);
313 bool parseRegister(Register &Reg, RegisterGroup Group, const unsigned *Regs,
314 bool IsAddress = false);
317 parseRegister(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
318 RegisterGroup Group, const unsigned *Regs, RegisterKind Kind);
320 bool parseAddress(unsigned &Base, const MCExpr *&Disp,
321 unsigned &Index, const MCExpr *&Length,
322 const unsigned *Regs, RegisterKind RegKind);
325 parseAddress(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
326 const unsigned *Regs, RegisterKind RegKind,
329 bool parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
333 SystemZAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
334 const MCInstrInfo &MII)
335 : MCTargetAsmParser(), STI(sti), Parser(parser) {
336 MCAsmParserExtension::Initialize(Parser);
338 // Initialize the set of available features.
339 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
342 // Override MCTargetAsmParser.
343 virtual bool ParseDirective(AsmToken DirectiveID) LLVM_OVERRIDE;
344 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
345 SMLoc &EndLoc) LLVM_OVERRIDE;
346 virtual bool ParseInstruction(ParseInstructionInfo &Info,
347 StringRef Name, SMLoc NameLoc,
348 SmallVectorImpl<MCParsedAsmOperand*> &Operands)
351 MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
352 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
353 MCStreamer &Out, unsigned &ErrorInfo,
354 bool MatchingInlineAsm) LLVM_OVERRIDE;
356 // Used by the TableGen code to parse particular operand types.
358 parseGR32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
359 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, GR32Reg);
362 parseGRH32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
363 return parseRegister(Operands, RegGR, SystemZMC::GRH32Regs, GRH32Reg);
366 parseGRX32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
367 llvm_unreachable("GRX32 should only be used for pseudo instructions");
370 parseGR64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
371 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, GR64Reg);
374 parseGR128(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
375 return parseRegister(Operands, RegGR, SystemZMC::GR128Regs, GR128Reg);
378 parseADDR32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
379 return parseRegister(Operands, RegGR, SystemZMC::GR32Regs, ADDR32Reg);
382 parseADDR64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
383 return parseRegister(Operands, RegGR, SystemZMC::GR64Regs, ADDR64Reg);
386 parseADDR128(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
387 llvm_unreachable("Shouldn't be used as an operand");
390 parseFP32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
391 return parseRegister(Operands, RegFP, SystemZMC::FP32Regs, FP32Reg);
394 parseFP64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
395 return parseRegister(Operands, RegFP, SystemZMC::FP64Regs, FP64Reg);
398 parseFP128(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
399 return parseRegister(Operands, RegFP, SystemZMC::FP128Regs, FP128Reg);
402 parseBDAddr32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
403 return parseAddress(Operands, SystemZMC::GR32Regs, ADDR32Reg, BDMem);
406 parseBDAddr64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
407 return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDMem);
410 parseBDXAddr64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
411 return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDXMem);
414 parseBDLAddr64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
415 return parseAddress(Operands, SystemZMC::GR64Regs, ADDR64Reg, BDLMem);
418 parseAccessReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
420 parsePCRel(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
421 int64_t MinVal, int64_t MaxVal);
423 parsePCRel16(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
424 return parsePCRel(Operands, -(1LL << 16), (1LL << 16) - 1);
427 parsePCRel32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
428 return parsePCRel(Operands, -(1LL << 32), (1LL << 32) - 1);
433 #define GET_REGISTER_MATCHER
434 #define GET_SUBTARGET_FEATURE_NAME
435 #define GET_MATCHER_IMPLEMENTATION
436 #include "SystemZGenAsmMatcher.inc"
438 void SystemZOperand::print(raw_ostream &OS) const {
439 llvm_unreachable("Not implemented");
442 // Parse one register of the form %<prefix><number>.
443 bool SystemZAsmParser::parseRegister(Register &Reg) {
444 Reg.StartLoc = Parser.getTok().getLoc();
447 if (Parser.getTok().isNot(AsmToken::Percent))
448 return Error(Parser.getTok().getLoc(), "register expected");
451 // Expect a register name.
452 if (Parser.getTok().isNot(AsmToken::Identifier))
453 return Error(Reg.StartLoc, "invalid register");
455 // Check that there's a prefix.
456 StringRef Name = Parser.getTok().getString();
458 return Error(Reg.StartLoc, "invalid register");
459 char Prefix = Name[0];
461 // Treat the rest of the register name as a register number.
462 if (Name.substr(1).getAsInteger(10, Reg.Num))
463 return Error(Reg.StartLoc, "invalid register");
465 // Look for valid combinations of prefix and number.
466 if (Prefix == 'r' && Reg.Num < 16)
468 else if (Prefix == 'f' && Reg.Num < 16)
470 else if (Prefix == 'a' && Reg.Num < 16)
471 Reg.Group = RegAccess;
473 return Error(Reg.StartLoc, "invalid register");
475 Reg.EndLoc = Parser.getTok().getLoc();
480 // 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
484 bool SystemZAsmParser::parseRegister(Register &Reg, RegisterGroup Group,
485 const unsigned *Regs, bool IsAddress) {
486 if (parseRegister(Reg))
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");
495 Reg.Num = Regs[Reg.Num];
499 // Parse a register and add it to Operands. The other arguments are as above.
500 SystemZAsmParser::OperandMatchResultTy
501 SystemZAsmParser::parseRegister(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
502 RegisterGroup Group, const unsigned *Regs,
504 if (Parser.getTok().isNot(AsmToken::Percent))
505 return MatchOperand_NoMatch;
508 bool IsAddress = (Kind == ADDR32Reg || Kind == ADDR64Reg);
509 if (parseRegister(Reg, Group, Regs, IsAddress))
510 return MatchOperand_ParseFail;
512 Operands.push_back(SystemZOperand::createReg(Kind, Reg.Num,
513 Reg.StartLoc, Reg.EndLoc));
514 return MatchOperand_Success;
517 // 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).
520 bool SystemZAsmParser::parseAddress(unsigned &Base, const MCExpr *&Disp,
521 unsigned &Index, const MCExpr *&Length,
522 const unsigned *Regs,
523 RegisterKind RegKind) {
524 // Parse the displacement, which must always be present.
525 if (getParser().parseExpression(Disp))
528 // Parse the optional base and index.
532 if (getLexer().is(AsmToken::LParen)) {
535 if (getLexer().is(AsmToken::Percent)) {
536 // Parse the first register and decide whether it's a base or an index.
538 if (parseRegister(Reg, RegGR, Regs, RegKind))
540 if (getLexer().is(AsmToken::Comma))
546 if (getParser().parseExpression(Length))
550 // Check whether there's a second register. It's the base if so.
551 if (getLexer().is(AsmToken::Comma)) {
554 if (parseRegister(Reg, RegGR, Regs, RegKind))
559 // Consume the closing bracket.
560 if (getLexer().isNot(AsmToken::RParen))
561 return Error(Parser.getTok().getLoc(), "unexpected token in address");
567 // Parse a memory operand and add it to Operands. The other arguments
569 SystemZAsmParser::OperandMatchResultTy
570 SystemZAsmParser::parseAddress(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
571 const unsigned *Regs, RegisterKind RegKind,
572 MemoryKind MemKind) {
573 SMLoc StartLoc = Parser.getTok().getLoc();
574 unsigned Base, Index;
576 const MCExpr *Length;
577 if (parseAddress(Base, Disp, Index, Length, Regs, RegKind))
578 return MatchOperand_ParseFail;
580 if (Index && MemKind != BDXMem)
582 Error(StartLoc, "invalid use of indexed addressing");
583 return MatchOperand_ParseFail;
586 if (Length && MemKind != BDLMem)
588 Error(StartLoc, "invalid use of length addressing");
589 return MatchOperand_ParseFail;
592 if (!Length && MemKind == BDLMem)
594 Error(StartLoc, "missing length in address");
595 return MatchOperand_ParseFail;
599 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
600 Operands.push_back(SystemZOperand::createMem(RegKind, Base, Disp, Index,
601 Length, StartLoc, EndLoc));
602 return MatchOperand_Success;
605 bool SystemZAsmParser::ParseDirective(AsmToken DirectiveID) {
609 bool SystemZAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
612 if (parseRegister(Reg))
614 if (Reg.Group == RegGR)
615 RegNo = SystemZMC::GR64Regs[Reg.Num];
616 else if (Reg.Group == RegFP)
617 RegNo = SystemZMC::FP64Regs[Reg.Num];
619 // FIXME: Access registers aren't modelled as LLVM registers yet.
620 return Error(Reg.StartLoc, "invalid operand for instruction");
621 StartLoc = Reg.StartLoc;
626 bool SystemZAsmParser::
627 ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
628 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
629 Operands.push_back(SystemZOperand::createToken(Name, NameLoc));
631 // Read the remaining operands.
632 if (getLexer().isNot(AsmToken::EndOfStatement)) {
633 // Read the first operand.
634 if (parseOperand(Operands, Name)) {
635 Parser.eatToEndOfStatement();
639 // Read any subsequent operands.
640 while (getLexer().is(AsmToken::Comma)) {
642 if (parseOperand(Operands, Name)) {
643 Parser.eatToEndOfStatement();
647 if (getLexer().isNot(AsmToken::EndOfStatement)) {
648 SMLoc Loc = getLexer().getLoc();
649 Parser.eatToEndOfStatement();
650 return Error(Loc, "unexpected token in argument list");
654 // Consume the EndOfStatement.
659 bool SystemZAsmParser::
660 parseOperand(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)
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)
674 // 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)) {
680 if (parseRegister(Reg))
682 Operands.push_back(SystemZOperand::createInvalid(Reg.StartLoc, Reg.EndLoc));
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.
689 SMLoc StartLoc = Parser.getTok().getLoc();
690 unsigned Base, Index;
691 const MCExpr *Expr, *Length;
692 if (parseAddress(Base, Expr, Index, Length, SystemZMC::GR64Regs, ADDR64Reg))
696 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
697 if (Base || Index || Length)
698 Operands.push_back(SystemZOperand::createInvalid(StartLoc, EndLoc));
700 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
704 bool SystemZAsmParser::
705 MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
706 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
707 MCStreamer &Out, unsigned &ErrorInfo,
708 bool MatchingInlineAsm) {
710 unsigned MatchResult;
712 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
714 switch (MatchResult) {
718 Out.EmitInstruction(Inst);
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:";
727 for (unsigned I = 0; I < sizeof(ErrorInfo) * 8 - 1; ++I) {
728 if (ErrorInfo & Mask) {
730 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
734 return Error(IDLoc, Msg);
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");
743 ErrorLoc = ((SystemZOperand*)Operands[ErrorInfo])->getStartLoc();
744 if (ErrorLoc == SMLoc())
747 return Error(ErrorLoc, "invalid operand for instruction");
750 case Match_MnemonicFail:
751 return Error(IDLoc, "invalid instruction");
754 llvm_unreachable("Unexpected match type");
757 SystemZAsmParser::OperandMatchResultTy SystemZAsmParser::
758 parseAccessReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
759 if (Parser.getTok().isNot(AsmToken::Percent))
760 return MatchOperand_NoMatch;
763 if (parseRegister(Reg, RegAccess, 0))
764 return MatchOperand_ParseFail;
766 Operands.push_back(SystemZOperand::createAccessReg(Reg.Num,
769 return MatchOperand_Success;
772 SystemZAsmParser::OperandMatchResultTy SystemZAsmParser::
773 parsePCRel(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
774 int64_t MinVal, int64_t MaxVal) {
775 MCContext &Ctx = getContext();
776 MCStreamer &Out = getStreamer();
778 SMLoc StartLoc = Parser.getTok().getLoc();
779 if (getParser().parseExpression(Expr))
780 return MatchOperand_NoMatch;
782 // For consistency with the GNU assembler, treat immediates as offsets
784 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) {
785 int64_t Value = CE->getValue();
786 if ((Value & 1) || Value < MinVal || Value > MaxVal) {
787 Error(StartLoc, "offset out of range");
788 return MatchOperand_ParseFail;
790 MCSymbol *Sym = Ctx.CreateTempSymbol();
792 const MCExpr *Base = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
794 Expr = Value == 0 ? Base : MCBinaryExpr::CreateAdd(Base, Expr, Ctx);
798 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
799 Operands.push_back(SystemZOperand::createImm(Expr, StartLoc, EndLoc));
800 return MatchOperand_Success;
803 // Force static initialization.
804 extern "C" void LLVMInitializeSystemZAsmParser() {
805 RegisterMCAsmParser<SystemZAsmParser> X(TheSystemZTarget);