1 //===-- SparcAsmParser.cpp - Parse Sparc assembly to MCInst 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/SparcMCTargetDesc.h"
11 #include "MCTargetDesc/SparcMCExpr.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCInst.h"
15 #include "llvm/MC/MCObjectFileInfo.h"
16 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
17 #include "llvm/MC/MCStreamer.h"
18 #include "llvm/MC/MCSubtargetInfo.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/MC/MCTargetAsmParser.h"
21 #include "llvm/Support/TargetRegistry.h"
25 // The generated AsmMatcher SparcGenAsmMatcher uses "Sparc" as the target
26 // namespace. But SPARC backend uses "SP" as its namespace.
35 class SparcAsmParser : public MCTargetAsmParser {
40 /// @name Auto-generated Match Functions
43 #define GET_ASSEMBLER_HEADER
44 #include "SparcGenAsmMatcher.inc"
48 // public interface of the MCTargetAsmParser.
49 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
50 OperandVector &Operands, MCStreamer &Out,
52 bool MatchingInlineAsm) override;
53 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
54 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
55 SMLoc NameLoc, OperandVector &Operands) override;
56 bool ParseDirective(AsmToken DirectiveID) override;
58 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
59 unsigned Kind) override;
61 // Custom parse functions for Sparc specific operands.
62 OperandMatchResultTy parseMEMOperand(OperandVector &Operands);
64 OperandMatchResultTy parseOperand(OperandVector &Operands, StringRef Name);
67 parseSparcAsmOperand(std::unique_ptr<SparcOperand> &Operand,
70 OperandMatchResultTy parseBranchModifiers(OperandVector &Operands);
72 // returns true if Tok is matched to a register and returns register in RegNo.
73 bool matchRegisterName(const AsmToken &Tok, unsigned &RegNo,
76 bool matchSparcAsmModifiers(const MCExpr *&EVal, SMLoc &EndLoc);
77 bool parseDirectiveWord(unsigned Size, SMLoc L);
79 bool is64Bit() const {
80 return STI.getTargetTriple().getArchName().startswith("sparcv9");
83 void expandSET(MCInst &Inst, SMLoc IDLoc,
84 SmallVectorImpl<MCInst> &Instructions);
87 SparcAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
88 const MCInstrInfo &MII,
89 const MCTargetOptions &Options)
90 : MCTargetAsmParser(Options), STI(sti), Parser(parser) {
91 // Initialize the set of available features.
92 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
97 static unsigned IntRegs[32] = {
98 Sparc::G0, Sparc::G1, Sparc::G2, Sparc::G3,
99 Sparc::G4, Sparc::G5, Sparc::G6, Sparc::G7,
100 Sparc::O0, Sparc::O1, Sparc::O2, Sparc::O3,
101 Sparc::O4, Sparc::O5, Sparc::O6, Sparc::O7,
102 Sparc::L0, Sparc::L1, Sparc::L2, Sparc::L3,
103 Sparc::L4, Sparc::L5, Sparc::L6, Sparc::L7,
104 Sparc::I0, Sparc::I1, Sparc::I2, Sparc::I3,
105 Sparc::I4, Sparc::I5, Sparc::I6, Sparc::I7 };
107 static unsigned FloatRegs[32] = {
108 Sparc::F0, Sparc::F1, Sparc::F2, Sparc::F3,
109 Sparc::F4, Sparc::F5, Sparc::F6, Sparc::F7,
110 Sparc::F8, Sparc::F9, Sparc::F10, Sparc::F11,
111 Sparc::F12, Sparc::F13, Sparc::F14, Sparc::F15,
112 Sparc::F16, Sparc::F17, Sparc::F18, Sparc::F19,
113 Sparc::F20, Sparc::F21, Sparc::F22, Sparc::F23,
114 Sparc::F24, Sparc::F25, Sparc::F26, Sparc::F27,
115 Sparc::F28, Sparc::F29, Sparc::F30, Sparc::F31 };
117 static unsigned DoubleRegs[32] = {
118 Sparc::D0, Sparc::D1, Sparc::D2, Sparc::D3,
119 Sparc::D4, Sparc::D5, Sparc::D6, Sparc::D7,
120 Sparc::D8, Sparc::D7, Sparc::D8, Sparc::D9,
121 Sparc::D12, Sparc::D13, Sparc::D14, Sparc::D15,
122 Sparc::D16, Sparc::D17, Sparc::D18, Sparc::D19,
123 Sparc::D20, Sparc::D21, Sparc::D22, Sparc::D23,
124 Sparc::D24, Sparc::D25, Sparc::D26, Sparc::D27,
125 Sparc::D28, Sparc::D29, Sparc::D30, Sparc::D31 };
127 static unsigned QuadFPRegs[32] = {
128 Sparc::Q0, Sparc::Q1, Sparc::Q2, Sparc::Q3,
129 Sparc::Q4, Sparc::Q5, Sparc::Q6, Sparc::Q7,
130 Sparc::Q8, Sparc::Q9, Sparc::Q10, Sparc::Q11,
131 Sparc::Q12, Sparc::Q13, Sparc::Q14, Sparc::Q15 };
133 static unsigned ASRRegs[32] = {
134 SP::Y, SP::ASR1, SP::ASR2, SP::ASR3,
135 SP::ASR4, SP::ASR5, SP::ASR6, SP::ASR7,
136 SP::ASR8, SP::ASR9, SP::ASR10, SP::ASR11,
137 SP::ASR12, SP::ASR13, SP::ASR14, SP::ASR15,
138 SP::ASR16, SP::ASR17, SP::ASR18, SP::ASR19,
139 SP::ASR20, SP::ASR21, SP::ASR22, SP::ASR23,
140 SP::ASR24, SP::ASR25, SP::ASR26, SP::ASR27,
141 SP::ASR28, SP::ASR29, SP::ASR30, SP::ASR31};
143 /// SparcOperand - Instances of this class represent a parsed Sparc machine
145 class SparcOperand : public MCParsedAsmOperand {
165 SMLoc StartLoc, EndLoc;
194 SparcOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
196 bool isToken() const override { return Kind == k_Token; }
197 bool isReg() const override { return Kind == k_Register; }
198 bool isImm() const override { return Kind == k_Immediate; }
199 bool isMem() const override { return isMEMrr() || isMEMri(); }
200 bool isMEMrr() const { return Kind == k_MemoryReg; }
201 bool isMEMri() const { return Kind == k_MemoryImm; }
203 bool isFloatReg() const {
204 return (Kind == k_Register && Reg.Kind == rk_FloatReg);
207 bool isFloatOrDoubleReg() const {
208 return (Kind == k_Register && (Reg.Kind == rk_FloatReg
209 || Reg.Kind == rk_DoubleReg));
213 StringRef getToken() const {
214 assert(Kind == k_Token && "Invalid access!");
215 return StringRef(Tok.Data, Tok.Length);
218 unsigned getReg() const override {
219 assert((Kind == k_Register) && "Invalid access!");
223 const MCExpr *getImm() const {
224 assert((Kind == k_Immediate) && "Invalid access!");
228 unsigned getMemBase() const {
229 assert((Kind == k_MemoryReg || Kind == k_MemoryImm) && "Invalid access!");
233 unsigned getMemOffsetReg() const {
234 assert((Kind == k_MemoryReg) && "Invalid access!");
235 return Mem.OffsetReg;
238 const MCExpr *getMemOff() const {
239 assert((Kind == k_MemoryImm) && "Invalid access!");
243 /// getStartLoc - Get the location of the first token of this operand.
244 SMLoc getStartLoc() const override {
247 /// getEndLoc - Get the location of the last token of this operand.
248 SMLoc getEndLoc() const override {
252 void print(raw_ostream &OS) const override {
254 case k_Token: OS << "Token: " << getToken() << "\n"; break;
255 case k_Register: OS << "Reg: #" << getReg() << "\n"; break;
256 case k_Immediate: OS << "Imm: " << getImm() << "\n"; break;
257 case k_MemoryReg: OS << "Mem: " << getMemBase() << "+"
258 << getMemOffsetReg() << "\n"; break;
259 case k_MemoryImm: assert(getMemOff() != nullptr);
260 OS << "Mem: " << getMemBase()
261 << "+" << *getMemOff()
266 void addRegOperands(MCInst &Inst, unsigned N) const {
267 assert(N == 1 && "Invalid number of operands!");
268 Inst.addOperand(MCOperand::createReg(getReg()));
271 void addImmOperands(MCInst &Inst, unsigned N) const {
272 assert(N == 1 && "Invalid number of operands!");
273 const MCExpr *Expr = getImm();
277 void addExpr(MCInst &Inst, const MCExpr *Expr) const{
278 // Add as immediate when possible. Null MCExpr = 0.
280 Inst.addOperand(MCOperand::createImm(0));
281 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
282 Inst.addOperand(MCOperand::createImm(CE->getValue()));
284 Inst.addOperand(MCOperand::createExpr(Expr));
287 void addMEMrrOperands(MCInst &Inst, unsigned N) const {
288 assert(N == 2 && "Invalid number of operands!");
290 Inst.addOperand(MCOperand::createReg(getMemBase()));
292 assert(getMemOffsetReg() != 0 && "Invalid offset");
293 Inst.addOperand(MCOperand::createReg(getMemOffsetReg()));
296 void addMEMriOperands(MCInst &Inst, unsigned N) const {
297 assert(N == 2 && "Invalid number of operands!");
299 Inst.addOperand(MCOperand::createReg(getMemBase()));
301 const MCExpr *Expr = getMemOff();
305 static std::unique_ptr<SparcOperand> CreateToken(StringRef Str, SMLoc S) {
306 auto Op = make_unique<SparcOperand>(k_Token);
307 Op->Tok.Data = Str.data();
308 Op->Tok.Length = Str.size();
314 static std::unique_ptr<SparcOperand> CreateReg(unsigned RegNum, unsigned Kind,
316 auto Op = make_unique<SparcOperand>(k_Register);
317 Op->Reg.RegNum = RegNum;
318 Op->Reg.Kind = (SparcOperand::RegisterKind)Kind;
324 static std::unique_ptr<SparcOperand> CreateImm(const MCExpr *Val, SMLoc S,
326 auto Op = make_unique<SparcOperand>(k_Immediate);
333 static bool MorphToDoubleReg(SparcOperand &Op) {
334 unsigned Reg = Op.getReg();
335 assert(Op.Reg.Kind == rk_FloatReg);
336 unsigned regIdx = Reg - Sparc::F0;
337 if (regIdx % 2 || regIdx > 31)
339 Op.Reg.RegNum = DoubleRegs[regIdx / 2];
340 Op.Reg.Kind = rk_DoubleReg;
344 static bool MorphToQuadReg(SparcOperand &Op) {
345 unsigned Reg = Op.getReg();
347 switch (Op.Reg.Kind) {
348 default: llvm_unreachable("Unexpected register kind!");
350 regIdx = Reg - Sparc::F0;
351 if (regIdx % 4 || regIdx > 31)
353 Reg = QuadFPRegs[regIdx / 4];
356 regIdx = Reg - Sparc::D0;
357 if (regIdx % 2 || regIdx > 31)
359 Reg = QuadFPRegs[regIdx / 2];
363 Op.Reg.Kind = rk_QuadReg;
367 static std::unique_ptr<SparcOperand>
368 MorphToMEMrr(unsigned Base, std::unique_ptr<SparcOperand> Op) {
369 unsigned offsetReg = Op->getReg();
370 Op->Kind = k_MemoryReg;
372 Op->Mem.OffsetReg = offsetReg;
373 Op->Mem.Off = nullptr;
377 static std::unique_ptr<SparcOperand>
378 CreateMEMr(unsigned Base, SMLoc S, SMLoc E) {
379 auto Op = make_unique<SparcOperand>(k_MemoryReg);
381 Op->Mem.OffsetReg = Sparc::G0; // always 0
382 Op->Mem.Off = nullptr;
388 static std::unique_ptr<SparcOperand>
389 MorphToMEMri(unsigned Base, std::unique_ptr<SparcOperand> Op) {
390 const MCExpr *Imm = Op->getImm();
391 Op->Kind = k_MemoryImm;
393 Op->Mem.OffsetReg = 0;
401 void SparcAsmParser::expandSET(MCInst &Inst, SMLoc IDLoc,
402 SmallVectorImpl<MCInst> &Instructions) {
403 MCOperand MCRegOp = Inst.getOperand(0);
404 MCOperand MCValOp = Inst.getOperand(1);
405 assert(MCRegOp.isReg());
406 assert(MCValOp.isImm() || MCValOp.isExpr());
408 // the imm operand can be either an expression or an immediate.
409 bool IsImm = Inst.getOperand(1).isImm();
410 uint64_t ImmValue = IsImm ? MCValOp.getImm() : 0;
411 const MCExpr *ValExpr;
413 ValExpr = MCConstantExpr::create(ImmValue, getContext());
415 ValExpr = MCValOp.getExpr();
417 MCOperand PrevReg = MCOperand::createReg(Sparc::G0);
419 if (!IsImm || (ImmValue & ~0x1fff)) {
422 SparcMCExpr::create(SparcMCExpr::VK_Sparc_HI, ValExpr, getContext());
423 TmpInst.setLoc(IDLoc);
424 TmpInst.setOpcode(SP::SETHIi);
425 TmpInst.addOperand(MCRegOp);
426 TmpInst.addOperand(MCOperand::createExpr(Expr));
427 Instructions.push_back(TmpInst);
431 if (!IsImm || ((ImmValue & 0x1fff) != 0 || ImmValue == 0)) {
434 SparcMCExpr::create(SparcMCExpr::VK_Sparc_LO, ValExpr, getContext());
435 TmpInst.setLoc(IDLoc);
436 TmpInst.setOpcode(SP::ORri);
437 TmpInst.addOperand(MCRegOp);
438 TmpInst.addOperand(PrevReg);
439 TmpInst.addOperand(MCOperand::createExpr(Expr));
440 Instructions.push_back(TmpInst);
444 bool SparcAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
445 OperandVector &Operands,
448 bool MatchingInlineAsm) {
450 SmallVector<MCInst, 8> Instructions;
451 unsigned MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
453 switch (MatchResult) {
454 case Match_Success: {
455 switch (Inst.getOpcode()) {
458 Instructions.push_back(Inst);
461 expandSET(Inst, IDLoc, Instructions);
465 for (const MCInst &I : Instructions) {
466 Out.EmitInstruction(I, STI);
471 case Match_MissingFeature:
473 "instruction requires a CPU feature not currently enabled");
475 case Match_InvalidOperand: {
476 SMLoc ErrorLoc = IDLoc;
477 if (ErrorInfo != ~0ULL) {
478 if (ErrorInfo >= Operands.size())
479 return Error(IDLoc, "too few operands for instruction");
481 ErrorLoc = ((SparcOperand &)*Operands[ErrorInfo]).getStartLoc();
482 if (ErrorLoc == SMLoc())
486 return Error(ErrorLoc, "invalid operand for instruction");
488 case Match_MnemonicFail:
489 return Error(IDLoc, "invalid instruction mnemonic");
491 llvm_unreachable("Implement any new match types added!");
494 bool SparcAsmParser::
495 ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc)
497 const AsmToken &Tok = Parser.getTok();
498 StartLoc = Tok.getLoc();
499 EndLoc = Tok.getEndLoc();
501 if (getLexer().getKind() != AsmToken::Percent)
504 unsigned regKind = SparcOperand::rk_None;
505 if (matchRegisterName(Tok, RegNo, regKind)) {
510 return Error(StartLoc, "invalid register name");
513 static void applyMnemonicAliases(StringRef &Mnemonic, uint64_t Features,
516 bool SparcAsmParser::ParseInstruction(ParseInstructionInfo &Info,
517 StringRef Name, SMLoc NameLoc,
518 OperandVector &Operands) {
520 // First operand in MCInst is instruction mnemonic.
521 Operands.push_back(SparcOperand::CreateToken(Name, NameLoc));
523 // apply mnemonic aliases, if any, so that we can parse operands correctly.
524 applyMnemonicAliases(Name, getAvailableFeatures(), 0);
526 if (getLexer().isNot(AsmToken::EndOfStatement)) {
527 // Read the first operand.
528 if (getLexer().is(AsmToken::Comma)) {
529 if (parseBranchModifiers(Operands) != MatchOperand_Success) {
530 SMLoc Loc = getLexer().getLoc();
531 Parser.eatToEndOfStatement();
532 return Error(Loc, "unexpected token");
535 if (parseOperand(Operands, Name) != MatchOperand_Success) {
536 SMLoc Loc = getLexer().getLoc();
537 Parser.eatToEndOfStatement();
538 return Error(Loc, "unexpected token");
541 while (getLexer().is(AsmToken::Comma)) {
542 Parser.Lex(); // Eat the comma.
543 // Parse and remember the operand.
544 if (parseOperand(Operands, Name) != MatchOperand_Success) {
545 SMLoc Loc = getLexer().getLoc();
546 Parser.eatToEndOfStatement();
547 return Error(Loc, "unexpected token");
551 if (getLexer().isNot(AsmToken::EndOfStatement)) {
552 SMLoc Loc = getLexer().getLoc();
553 Parser.eatToEndOfStatement();
554 return Error(Loc, "unexpected token");
556 Parser.Lex(); // Consume the EndOfStatement.
560 bool SparcAsmParser::
561 ParseDirective(AsmToken DirectiveID)
563 StringRef IDVal = DirectiveID.getString();
565 if (IDVal == ".byte")
566 return parseDirectiveWord(1, DirectiveID.getLoc());
568 if (IDVal == ".half")
569 return parseDirectiveWord(2, DirectiveID.getLoc());
571 if (IDVal == ".word")
572 return parseDirectiveWord(4, DirectiveID.getLoc());
574 if (IDVal == ".nword")
575 return parseDirectiveWord(is64Bit() ? 8 : 4, DirectiveID.getLoc());
577 if (is64Bit() && IDVal == ".xword")
578 return parseDirectiveWord(8, DirectiveID.getLoc());
580 if (IDVal == ".register") {
581 // For now, ignore .register directive.
582 Parser.eatToEndOfStatement();
586 // Let the MC layer to handle other directives.
590 bool SparcAsmParser:: parseDirectiveWord(unsigned Size, SMLoc L) {
591 if (getLexer().isNot(AsmToken::EndOfStatement)) {
594 if (getParser().parseExpression(Value))
597 getParser().getStreamer().EmitValue(Value, Size);
599 if (getLexer().is(AsmToken::EndOfStatement))
602 // FIXME: Improve diagnostic.
603 if (getLexer().isNot(AsmToken::Comma))
604 return Error(L, "unexpected token in directive");
612 SparcAsmParser::OperandMatchResultTy
613 SparcAsmParser::parseMEMOperand(OperandVector &Operands) {
616 unsigned BaseReg = 0;
618 if (ParseRegister(BaseReg, S, E)) {
619 return MatchOperand_NoMatch;
622 switch (getLexer().getKind()) {
623 default: return MatchOperand_NoMatch;
625 case AsmToken::Comma:
626 case AsmToken::RBrac:
627 case AsmToken::EndOfStatement:
628 Operands.push_back(SparcOperand::CreateMEMr(BaseReg, S, E));
629 return MatchOperand_Success;
631 case AsmToken:: Plus:
632 Parser.Lex(); // Eat the '+'
634 case AsmToken::Minus:
638 std::unique_ptr<SparcOperand> Offset;
639 OperandMatchResultTy ResTy = parseSparcAsmOperand(Offset);
640 if (ResTy != MatchOperand_Success || !Offset)
641 return MatchOperand_NoMatch;
644 Offset->isImm() ? SparcOperand::MorphToMEMri(BaseReg, std::move(Offset))
645 : SparcOperand::MorphToMEMrr(BaseReg, std::move(Offset)));
647 return MatchOperand_Success;
650 SparcAsmParser::OperandMatchResultTy
651 SparcAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
653 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
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_Success || ResTy == MatchOperand_ParseFail)
661 if (getLexer().is(AsmToken::LBrac)) {
663 Operands.push_back(SparcOperand::CreateToken("[",
664 Parser.getTok().getLoc()));
665 Parser.Lex(); // Eat the [
667 if (Mnemonic == "cas" || Mnemonic == "casx") {
668 SMLoc S = Parser.getTok().getLoc();
669 if (getLexer().getKind() != AsmToken::Percent)
670 return MatchOperand_NoMatch;
671 Parser.Lex(); // eat %
673 unsigned RegNo, RegKind;
674 if (!matchRegisterName(Parser.getTok(), RegNo, RegKind))
675 return MatchOperand_NoMatch;
677 Parser.Lex(); // Eat the identifier token.
678 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer()-1);
679 Operands.push_back(SparcOperand::CreateReg(RegNo, RegKind, S, E));
680 ResTy = MatchOperand_Success;
682 ResTy = parseMEMOperand(Operands);
685 if (ResTy != MatchOperand_Success)
688 if (!getLexer().is(AsmToken::RBrac))
689 return MatchOperand_ParseFail;
691 Operands.push_back(SparcOperand::CreateToken("]",
692 Parser.getTok().getLoc()));
693 Parser.Lex(); // Eat the ]
695 // Parse an optional address-space identifier after the address.
696 if (getLexer().is(AsmToken::Integer)) {
697 std::unique_ptr<SparcOperand> Op;
698 ResTy = parseSparcAsmOperand(Op, false);
699 if (ResTy != MatchOperand_Success || !Op)
700 return MatchOperand_ParseFail;
701 Operands.push_back(std::move(Op));
703 return MatchOperand_Success;
706 std::unique_ptr<SparcOperand> Op;
708 ResTy = parseSparcAsmOperand(Op, (Mnemonic == "call"));
709 if (ResTy != MatchOperand_Success || !Op)
710 return MatchOperand_ParseFail;
712 // Push the parsed operand into the list of operands
713 Operands.push_back(std::move(Op));
715 return MatchOperand_Success;
718 SparcAsmParser::OperandMatchResultTy
719 SparcAsmParser::parseSparcAsmOperand(std::unique_ptr<SparcOperand> &Op,
722 SMLoc S = Parser.getTok().getLoc();
723 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
727 switch (getLexer().getKind()) {
730 case AsmToken::Percent:
731 Parser.Lex(); // Eat the '%'.
734 if (matchRegisterName(Parser.getTok(), RegNo, RegKind)) {
735 StringRef name = Parser.getTok().getString();
736 Parser.Lex(); // Eat the identifier token.
737 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
740 Op = SparcOperand::CreateReg(RegNo, RegKind, S, E);
743 Op = SparcOperand::CreateToken("%psr", S);
746 Op = SparcOperand::CreateToken("%wim", S);
749 Op = SparcOperand::CreateToken("%tbr", S);
753 Op = SparcOperand::CreateToken("%xcc", S);
755 Op = SparcOperand::CreateToken("%icc", S);
760 if (matchSparcAsmModifiers(EVal, E)) {
761 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
762 Op = SparcOperand::CreateImm(EVal, S, E);
766 case AsmToken::Minus:
767 case AsmToken::Integer:
768 case AsmToken::LParen:
769 if (!getParser().parseExpression(EVal, E))
770 Op = SparcOperand::CreateImm(EVal, S, E);
773 case AsmToken::Identifier: {
774 StringRef Identifier;
775 if (!getParser().parseIdentifier(Identifier)) {
776 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
777 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
779 const MCExpr *Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
782 getContext().getObjectFileInfo()->getRelocM() == Reloc::PIC_)
783 Res = SparcMCExpr::create(SparcMCExpr::VK_Sparc_WPLT30, Res,
785 Op = SparcOperand::CreateImm(Res, S, E);
790 return (Op) ? MatchOperand_Success : MatchOperand_ParseFail;
793 SparcAsmParser::OperandMatchResultTy
794 SparcAsmParser::parseBranchModifiers(OperandVector &Operands) {
796 // parse (,a|,pn|,pt)+
798 while (getLexer().is(AsmToken::Comma)) {
800 Parser.Lex(); // Eat the comma
802 if (!getLexer().is(AsmToken::Identifier))
803 return MatchOperand_ParseFail;
804 StringRef modName = Parser.getTok().getString();
805 if (modName == "a" || modName == "pn" || modName == "pt") {
806 Operands.push_back(SparcOperand::CreateToken(modName,
807 Parser.getTok().getLoc()));
808 Parser.Lex(); // eat the identifier.
811 return MatchOperand_Success;
814 bool SparcAsmParser::matchRegisterName(const AsmToken &Tok,
820 RegKind = SparcOperand::rk_None;
821 if (Tok.is(AsmToken::Identifier)) {
822 StringRef name = Tok.getString();
825 if (name.equals("fp")) {
827 RegKind = SparcOperand::rk_IntReg;
831 if (name.equals("sp")) {
833 RegKind = SparcOperand::rk_IntReg;
837 if (name.equals("y")) {
839 RegKind = SparcOperand::rk_Special;
843 if (name.substr(0, 3).equals_lower("asr")
844 && !name.substr(3).getAsInteger(10, intVal)
845 && intVal > 0 && intVal < 32) {
846 RegNo = ASRRegs[intVal];
847 RegKind = SparcOperand::rk_Special;
851 if (name.equals("icc")) {
853 RegKind = SparcOperand::rk_Special;
857 if (name.equals("psr")) {
859 RegKind = SparcOperand::rk_Special;
863 if (name.equals("wim")) {
865 RegKind = SparcOperand::rk_Special;
869 if (name.equals("tbr")) {
871 RegKind = SparcOperand::rk_Special;
875 if (name.equals("xcc")) {
876 // FIXME:: check 64bit.
878 RegKind = SparcOperand::rk_Special;
883 if (name.substr(0, 3).equals_lower("fcc")
884 && !name.substr(3).getAsInteger(10, intVal)
886 // FIXME: check 64bit and handle %fcc1 - %fcc3
887 RegNo = Sparc::FCC0 + intVal;
888 RegKind = SparcOperand::rk_Special;
893 if (name.substr(0, 1).equals_lower("g")
894 && !name.substr(1).getAsInteger(10, intVal)
896 RegNo = IntRegs[intVal];
897 RegKind = SparcOperand::rk_IntReg;
901 if (name.substr(0, 1).equals_lower("o")
902 && !name.substr(1).getAsInteger(10, intVal)
904 RegNo = IntRegs[8 + intVal];
905 RegKind = SparcOperand::rk_IntReg;
908 if (name.substr(0, 1).equals_lower("l")
909 && !name.substr(1).getAsInteger(10, intVal)
911 RegNo = IntRegs[16 + intVal];
912 RegKind = SparcOperand::rk_IntReg;
915 if (name.substr(0, 1).equals_lower("i")
916 && !name.substr(1).getAsInteger(10, intVal)
918 RegNo = IntRegs[24 + intVal];
919 RegKind = SparcOperand::rk_IntReg;
923 if (name.substr(0, 1).equals_lower("f")
924 && !name.substr(1, 2).getAsInteger(10, intVal) && intVal < 32) {
925 RegNo = FloatRegs[intVal];
926 RegKind = SparcOperand::rk_FloatReg;
930 if (name.substr(0, 1).equals_lower("f")
931 && !name.substr(1, 2).getAsInteger(10, intVal)
932 && intVal >= 32 && intVal <= 62 && (intVal % 2 == 0)) {
934 RegNo = DoubleRegs[intVal/2];
935 RegKind = SparcOperand::rk_DoubleReg;
940 if (name.substr(0, 1).equals_lower("r")
941 && !name.substr(1, 2).getAsInteger(10, intVal) && intVal < 31) {
942 RegNo = IntRegs[intVal];
943 RegKind = SparcOperand::rk_IntReg;
950 // Determine if an expression contains a reference to the symbol
951 // "_GLOBAL_OFFSET_TABLE_".
952 static bool hasGOTReference(const MCExpr *Expr) {
953 switch (Expr->getKind()) {
955 if (const SparcMCExpr *SE = dyn_cast<SparcMCExpr>(Expr))
956 return hasGOTReference(SE->getSubExpr());
959 case MCExpr::Constant:
962 case MCExpr::Binary: {
963 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
964 return hasGOTReference(BE->getLHS()) || hasGOTReference(BE->getRHS());
967 case MCExpr::SymbolRef: {
968 const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
969 return (SymRef.getSymbol().getName() == "_GLOBAL_OFFSET_TABLE_");
973 return hasGOTReference(cast<MCUnaryExpr>(Expr)->getSubExpr());
978 bool SparcAsmParser::matchSparcAsmModifiers(const MCExpr *&EVal,
981 AsmToken Tok = Parser.getTok();
982 if (!Tok.is(AsmToken::Identifier))
985 StringRef name = Tok.getString();
987 SparcMCExpr::VariantKind VK = SparcMCExpr::parseVariantKind(name);
989 if (VK == SparcMCExpr::VK_Sparc_None)
992 Parser.Lex(); // Eat the identifier.
993 if (Parser.getTok().getKind() != AsmToken::LParen)
996 Parser.Lex(); // Eat the LParen token.
997 const MCExpr *subExpr;
998 if (Parser.parseParenExpression(subExpr, EndLoc))
1001 bool isPIC = getContext().getObjectFileInfo()->getRelocM() == Reloc::PIC_;
1003 // Ugly: if a sparc assembly expression says "%hi(...)" but the
1004 // expression within contains _GLOBAL_OFFSET_TABLE_, it REALLY means
1005 // %pc22. Same with %lo -> %pc10. Worse, if it doesn't contain that,
1006 // the meaning depends on whether the assembler was invoked with
1007 // -KPIC or not: if so, it really means %got22/%got10; if not, it
1008 // actually means what it said! Sigh, historical mistakes...
1012 case SparcMCExpr::VK_Sparc_LO:
1013 VK = (hasGOTReference(subExpr)
1014 ? SparcMCExpr::VK_Sparc_PC10
1015 : (isPIC ? SparcMCExpr::VK_Sparc_GOT10 : VK));
1017 case SparcMCExpr::VK_Sparc_HI:
1018 VK = (hasGOTReference(subExpr)
1019 ? SparcMCExpr::VK_Sparc_PC22
1020 : (isPIC ? SparcMCExpr::VK_Sparc_GOT22 : VK));
1024 EVal = SparcMCExpr::create(VK, subExpr, getContext());
1028 extern "C" void LLVMInitializeSparcAsmParser() {
1029 RegisterMCAsmParser<SparcAsmParser> A(TheSparcTarget);
1030 RegisterMCAsmParser<SparcAsmParser> B(TheSparcV9Target);
1031 RegisterMCAsmParser<SparcAsmParser> C(TheSparcelTarget);
1034 #define GET_REGISTER_MATCHER
1035 #define GET_MATCHER_IMPLEMENTATION
1036 #include "SparcGenAsmMatcher.inc"
1038 unsigned SparcAsmParser::validateTargetOperandClass(MCParsedAsmOperand &GOp,
1040 SparcOperand &Op = (SparcOperand &)GOp;
1041 if (Op.isFloatOrDoubleReg()) {
1045 if (!Op.isFloatReg() || SparcOperand::MorphToDoubleReg(Op))
1046 return MCTargetAsmParser::Match_Success;
1049 if (SparcOperand::MorphToQuadReg(Op))
1050 return MCTargetAsmParser::Match_Success;
1054 return Match_InvalidOperand;