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/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 // The generated AsmMatcher SparcGenAsmMatcher uses "Sparc" as the target
24 // namespace. But SPARC backend uses "SP" as its namespace.
33 class SparcAsmParser : public MCTargetAsmParser {
38 /// @name Auto-generated Match Functions
41 #define GET_ASSEMBLER_HEADER
42 #include "SparcGenAsmMatcher.inc"
46 // public interface of the MCTargetAsmParser.
47 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
48 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
49 MCStreamer &Out, unsigned &ErrorInfo,
50 bool MatchingInlineAsm);
51 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
52 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
54 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
55 bool ParseDirective(AsmToken DirectiveID);
58 // Custom parse functions for Sparc specific operands.
60 parseMEMOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
63 parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
67 parseSparcAsmOperand(SparcOperand *&Operand);
69 // returns true if Tok is matched to a register and returns register in RegNo.
70 bool matchRegisterName(const AsmToken &Tok, unsigned &RegNo, bool isDFP,
72 bool matchSparcAsmModifiers(const MCExpr *&EVal, SMLoc &EndLoc);
75 SparcAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
76 const MCInstrInfo &MII)
77 : MCTargetAsmParser(), STI(sti), Parser(parser) {
78 // Initialize the set of available features.
79 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
84 static unsigned IntRegs[32] = {
85 Sparc::G0, Sparc::G1, Sparc::G2, Sparc::G3,
86 Sparc::G4, Sparc::G5, Sparc::G6, Sparc::G7,
87 Sparc::O0, Sparc::O1, Sparc::O2, Sparc::O3,
88 Sparc::O4, Sparc::O5, Sparc::O6, Sparc::O7,
89 Sparc::L0, Sparc::L1, Sparc::L2, Sparc::L3,
90 Sparc::L4, Sparc::L5, Sparc::L6, Sparc::L7,
91 Sparc::I0, Sparc::I1, Sparc::I2, Sparc::I3,
92 Sparc::I4, Sparc::I5, Sparc::I6, Sparc::I7 };
94 static unsigned FloatRegs[32] = {
95 Sparc::F0, Sparc::F1, Sparc::F2, Sparc::F3,
96 Sparc::F4, Sparc::F5, Sparc::F6, Sparc::F7,
97 Sparc::F8, Sparc::F9, Sparc::F10, Sparc::F11,
98 Sparc::F12, Sparc::F13, Sparc::F14, Sparc::F15,
99 Sparc::F16, Sparc::F17, Sparc::F18, Sparc::F19,
100 Sparc::F20, Sparc::F21, Sparc::F22, Sparc::F23,
101 Sparc::F24, Sparc::F25, Sparc::F26, Sparc::F27,
102 Sparc::F28, Sparc::F29, Sparc::F30, Sparc::F31 };
104 static unsigned DoubleRegs[32] = {
105 Sparc::D0, Sparc::D1, Sparc::D2, Sparc::D3,
106 Sparc::D4, Sparc::D5, Sparc::D6, Sparc::D7,
107 Sparc::D8, Sparc::D7, Sparc::D8, Sparc::D9,
108 Sparc::D12, Sparc::D13, Sparc::D14, Sparc::D15,
109 Sparc::D16, Sparc::D17, Sparc::D18, Sparc::D19,
110 Sparc::D20, Sparc::D21, Sparc::D22, Sparc::D23,
111 Sparc::D24, Sparc::D25, Sparc::D26, Sparc::D27,
112 Sparc::D28, Sparc::D29, Sparc::D30, Sparc::D31 };
114 static unsigned QuadFPRegs[32] = {
115 Sparc::Q0, Sparc::Q1, Sparc::Q2, Sparc::Q3,
116 Sparc::Q4, Sparc::Q5, Sparc::Q6, Sparc::Q7,
117 Sparc::Q8, Sparc::Q7, Sparc::Q8, Sparc::Q9,
118 Sparc::Q12, Sparc::Q13, Sparc::Q14, Sparc::Q15 };
121 /// SparcOperand - Instances of this class represent a parsed Sparc machine
123 class SparcOperand : public MCParsedAsmOperand {
143 SMLoc StartLoc, EndLoc;
145 SparcOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
174 bool isToken() const { return Kind == k_Token; }
175 bool isReg() const { return Kind == k_Register; }
176 bool isImm() const { return Kind == k_Immediate; }
177 bool isMem() const { return isMEMrr() || isMEMri(); }
178 bool isMEMrr() const { return Kind == k_MemoryReg; }
179 bool isMEMri() const { return Kind == k_MemoryImm; }
181 StringRef getToken() const {
182 assert(Kind == k_Token && "Invalid access!");
183 return StringRef(Tok.Data, Tok.Length);
186 unsigned getReg() const {
187 assert((Kind == k_Register) && "Invalid access!");
191 const MCExpr *getImm() const {
192 assert((Kind == k_Immediate) && "Invalid access!");
196 unsigned getMemBase() const {
197 assert((Kind == k_MemoryReg || Kind == k_MemoryImm) && "Invalid access!");
201 unsigned getMemOffsetReg() const {
202 assert((Kind == k_MemoryReg) && "Invalid access!");
203 return Mem.OffsetReg;
206 const MCExpr *getMemOff() const {
207 assert((Kind == k_MemoryImm) && "Invalid access!");
211 /// getStartLoc - Get the location of the first token of this operand.
212 SMLoc getStartLoc() const {
215 /// getEndLoc - Get the location of the last token of this operand.
216 SMLoc getEndLoc() const {
220 virtual void print(raw_ostream &OS) const {
222 case k_Token: OS << "Token: " << getToken() << "\n"; break;
223 case k_Register: OS << "Reg: #" << getReg() << "\n"; break;
224 case k_Immediate: OS << "Imm: " << getImm() << "\n"; break;
225 case k_MemoryReg: OS << "Mem: " << getMemBase() << "+"
226 << getMemOffsetReg() << "\n"; break;
227 case k_MemoryImm: assert(getMemOff() != 0);
228 OS << "Mem: " << getMemBase()
229 << "+" << *getMemOff()
234 void addRegOperands(MCInst &Inst, unsigned N) const {
235 assert(N == 1 && "Invalid number of operands!");
236 Inst.addOperand(MCOperand::CreateReg(getReg()));
239 void addImmOperands(MCInst &Inst, unsigned N) const {
240 assert(N == 1 && "Invalid number of operands!");
241 const MCExpr *Expr = getImm();
245 void addExpr(MCInst &Inst, const MCExpr *Expr) const{
246 // Add as immediate when possible. Null MCExpr = 0.
248 Inst.addOperand(MCOperand::CreateImm(0));
249 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
250 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
252 Inst.addOperand(MCOperand::CreateExpr(Expr));
255 void addMEMrrOperands(MCInst &Inst, unsigned N) const {
256 assert(N == 2 && "Invalid number of operands!");
258 Inst.addOperand(MCOperand::CreateReg(getMemBase()));
260 assert(getMemOffsetReg() != 0 && "Invalid offset");
261 Inst.addOperand(MCOperand::CreateReg(getMemOffsetReg()));
264 void addMEMriOperands(MCInst &Inst, unsigned N) const {
265 assert(N == 2 && "Invalid number of operands!");
267 Inst.addOperand(MCOperand::CreateReg(getMemBase()));
269 const MCExpr *Expr = getMemOff();
273 static SparcOperand *CreateToken(StringRef Str, SMLoc S) {
274 SparcOperand *Op = new SparcOperand(k_Token);
275 Op->Tok.Data = Str.data();
276 Op->Tok.Length = Str.size();
282 static SparcOperand *CreateReg(unsigned RegNum,
283 SparcOperand::RegisterKind Kind,
285 SparcOperand *Op = new SparcOperand(k_Register);
286 Op->Reg.RegNum = RegNum;
293 static SparcOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
294 SparcOperand *Op = new SparcOperand(k_Immediate);
301 static SparcOperand *MorphToMEMrr(unsigned Base, SparcOperand *Op) {
302 unsigned offsetReg = Op->getReg();
303 Op->Kind = k_MemoryReg;
305 Op->Mem.OffsetReg = offsetReg;
310 static SparcOperand *CreateMEMri(unsigned Base,
313 SparcOperand *Op = new SparcOperand(k_MemoryImm);
315 Op->Mem.OffsetReg = 0;
322 static SparcOperand *MorphToMEMri(unsigned Base, SparcOperand *Op) {
323 const MCExpr *Imm = Op->getImm();
324 Op->Kind = k_MemoryImm;
326 Op->Mem.OffsetReg = 0;
334 bool SparcAsmParser::
335 MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
336 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
337 MCStreamer &Out, unsigned &ErrorInfo,
338 bool MatchingInlineAsm) {
340 SmallVector<MCInst, 8> Instructions;
341 unsigned MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
343 switch (MatchResult) {
347 case Match_Success: {
349 Out.EmitInstruction(Inst);
353 case Match_MissingFeature:
355 "instruction requires a CPU feature not currently enabled");
357 case Match_InvalidOperand: {
358 SMLoc ErrorLoc = IDLoc;
359 if (ErrorInfo != ~0U) {
360 if (ErrorInfo >= Operands.size())
361 return Error(IDLoc, "too few operands for instruction");
363 ErrorLoc = ((SparcOperand*) Operands[ErrorInfo])->getStartLoc();
364 if (ErrorLoc == SMLoc())
368 return Error(ErrorLoc, "invalid operand for instruction");
370 case Match_MnemonicFail:
371 return Error(IDLoc, "invalid instruction");
376 bool SparcAsmParser::
377 ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc)
379 const AsmToken &Tok = Parser.getTok();
380 StartLoc = Tok.getLoc();
381 EndLoc = Tok.getEndLoc();
383 if (getLexer().getKind() != AsmToken::Percent)
386 if (matchRegisterName(Tok, RegNo, false, false)) {
391 return Error(StartLoc, "invalid register name");
394 bool SparcAsmParser::
395 ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
397 SmallVectorImpl<MCParsedAsmOperand*> &Operands)
399 // Check if we have valid mnemonic.
400 if (!mnemonicIsValid(Name, 0)) {
401 Parser.eatToEndOfStatement();
402 return Error(NameLoc, "Unknown instruction");
404 // First operand in MCInst is instruction mnemonic.
405 Operands.push_back(SparcOperand::CreateToken(Name, NameLoc));
407 if (getLexer().isNot(AsmToken::EndOfStatement)) {
408 // Read the first operand.
409 if (parseOperand(Operands, Name) != MatchOperand_Success) {
410 SMLoc Loc = getLexer().getLoc();
411 Parser.eatToEndOfStatement();
412 return Error(Loc, "unexpected token");
415 while (getLexer().is(AsmToken::Comma)) {
416 Parser.Lex(); // Eat the comma.
417 // Parse and remember the operand.
418 if (parseOperand(Operands, Name) != MatchOperand_Success) {
419 SMLoc Loc = getLexer().getLoc();
420 Parser.eatToEndOfStatement();
421 return Error(Loc, "unexpected token");
425 if (getLexer().isNot(AsmToken::EndOfStatement)) {
426 SMLoc Loc = getLexer().getLoc();
427 Parser.eatToEndOfStatement();
428 return Error(Loc, "unexpected token");
430 Parser.Lex(); // Consume the EndOfStatement.
434 bool SparcAsmParser::
435 ParseDirective(AsmToken DirectiveID)
437 // Ignore all directives for now.
438 Parser.eatToEndOfStatement();
442 SparcAsmParser::OperandMatchResultTy SparcAsmParser::
443 parseMEMOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands)
447 unsigned BaseReg = 0;
449 if (ParseRegister(BaseReg, S, E)) {
450 return MatchOperand_NoMatch;
453 switch (getLexer().getKind()) {
454 default: return MatchOperand_NoMatch;
456 case AsmToken::RBrac:
457 case AsmToken::EndOfStatement:
458 Operands.push_back(SparcOperand::CreateMEMri(BaseReg, 0, S, E));
459 return MatchOperand_Success;
461 case AsmToken:: Plus:
462 Parser.Lex(); // Eat the '+'
464 case AsmToken::Minus:
468 SparcOperand *Offset = 0;
469 OperandMatchResultTy ResTy = parseSparcAsmOperand(Offset);
470 if (ResTy != MatchOperand_Success || !Offset)
471 return MatchOperand_NoMatch;
473 Offset = (Offset->isImm()
474 ? SparcOperand::MorphToMEMri(BaseReg, Offset)
475 : SparcOperand::MorphToMEMrr(BaseReg, Offset));
477 Operands.push_back(Offset);
478 return MatchOperand_Success;
481 SparcAsmParser::OperandMatchResultTy SparcAsmParser::
482 parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
486 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
488 // If there wasn't a custom match, try the generic matcher below. Otherwise,
489 // there was a match, but an error occurred, in which case, just return that
490 // the operand parsing failed.
491 if (ResTy == MatchOperand_Success || ResTy == MatchOperand_ParseFail)
494 if (getLexer().is(AsmToken::LBrac)) {
496 Operands.push_back(SparcOperand::CreateToken("[",
497 Parser.getTok().getLoc()));
498 Parser.Lex(); // Eat the [
500 ResTy = parseMEMOperand(Operands);
501 if (ResTy != MatchOperand_Success)
504 if (!getLexer().is(AsmToken::RBrac))
505 return MatchOperand_ParseFail;
507 Operands.push_back(SparcOperand::CreateToken("]",
508 Parser.getTok().getLoc()));
509 Parser.Lex(); // Eat the ]
510 return MatchOperand_Success;
513 SparcOperand *Op = 0;
514 ResTy = parseSparcAsmOperand(Op);
515 if (ResTy != MatchOperand_Success || !Op)
516 return MatchOperand_ParseFail;
518 // Push the parsed operand into the list of operands
519 Operands.push_back(Op);
521 return MatchOperand_Success;
524 SparcAsmParser::OperandMatchResultTy
525 SparcAsmParser::parseSparcAsmOperand(SparcOperand *&Op)
528 SMLoc S = Parser.getTok().getLoc();
529 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
533 switch (getLexer().getKind()) {
536 case AsmToken::Percent:
537 Parser.Lex(); // Eat the '%'.
539 if (matchRegisterName(Parser.getTok(), RegNo, false, false)) {
540 Parser.Lex(); // Eat the identifier token.
541 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
542 Op = SparcOperand::CreateReg(RegNo, SparcOperand::rk_None, S, E);
545 if (matchSparcAsmModifiers(EVal, E)) {
546 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
547 Op = SparcOperand::CreateImm(EVal, S, E);
551 case AsmToken::Minus:
552 case AsmToken::Integer:
553 if (!getParser().parseExpression(EVal, E))
554 Op = SparcOperand::CreateImm(EVal, S, E);
557 case AsmToken::Identifier: {
558 StringRef Identifier;
559 if (!getParser().parseIdentifier(Identifier)) {
560 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
561 MCSymbol *Sym = getContext().GetOrCreateSymbol(Identifier);
563 const MCExpr *Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
565 Op = SparcOperand::CreateImm(Res, S, E);
570 return (Op) ? MatchOperand_Success : MatchOperand_ParseFail;
573 bool SparcAsmParser::matchRegisterName(const AsmToken &Tok,
580 if (Tok.is(AsmToken::Identifier)) {
581 StringRef name = Tok.getString();
584 if (name.equals("fp")) {
589 if (name.equals("sp")) {
594 if (name.equals("y")) {
599 if (name.equals("icc")) {
604 if (name.equals("xcc")) {
605 // FIXME:: check 64bit.
611 if (name.substr(0, 3).equals_lower("fcc")
612 && !name.substr(3).getAsInteger(10, intVal)
614 // FIXME: check 64bit and handle %fcc1 - %fcc3
620 if (name.substr(0, 1).equals_lower("g")
621 && !name.substr(1).getAsInteger(10, intVal)
623 RegNo = IntRegs[intVal];
627 if (name.substr(0, 1).equals_lower("o")
628 && !name.substr(1).getAsInteger(10, intVal)
630 RegNo = IntRegs[8 + intVal];
633 if (name.substr(0, 1).equals_lower("l")
634 && !name.substr(1).getAsInteger(10, intVal)
636 RegNo = IntRegs[16 + intVal];
639 if (name.substr(0, 1).equals_lower("i")
640 && !name.substr(1).getAsInteger(10, intVal)
642 RegNo = IntRegs[24 + intVal];
646 if (name.substr(0, 1).equals_lower("f")
647 && !name.substr(1, 2).getAsInteger(10, intVal) && intVal < 32) {
648 if (isDFP && (intVal%2 == 0)) {
649 RegNo = DoubleRegs[intVal/2];
650 } else if (isQFP && (intVal%4 == 0)) {
651 RegNo = QuadFPRegs[intVal/4];
653 RegNo = FloatRegs[intVal];
658 if (name.substr(0, 1).equals_lower("f")
659 && !name.substr(1, 2).getAsInteger(10, intVal)
660 && intVal >= 32 && intVal <= 62 && (intVal % 2 == 0)) {
662 RegNo = DoubleRegs[16 + intVal/2];
663 } else if (isQFP && (intVal % 4 == 0)) {
664 RegNo = QuadFPRegs[8 + intVal/4];
672 if (name.substr(0, 1).equals_lower("r")
673 && !name.substr(1, 2).getAsInteger(10, intVal) && intVal < 31) {
674 RegNo = IntRegs[intVal];
682 bool SparcAsmParser::matchSparcAsmModifiers(const MCExpr *&EVal,
685 AsmToken Tok = Parser.getTok();
686 if (!Tok.is(AsmToken::Identifier))
689 StringRef name = Tok.getString();
691 SparcMCExpr::VariantKind VK = SparcMCExpr::parseVariantKind(name);
693 if (VK == SparcMCExpr::VK_Sparc_None)
696 Parser.Lex(); // Eat the identifier.
697 if (Parser.getTok().getKind() != AsmToken::LParen)
700 Parser.Lex(); // Eat the LParen token.
701 const MCExpr *subExpr;
702 if (Parser.parseParenExpression(subExpr, EndLoc))
704 EVal = SparcMCExpr::Create(VK, subExpr, getContext());
709 extern "C" void LLVMInitializeSparcAsmParser() {
710 RegisterMCAsmParser<SparcAsmParser> A(TheSparcTarget);
711 RegisterMCAsmParser<SparcAsmParser> B(TheSparcV9Target);
714 #define GET_REGISTER_MATCHER
715 #define GET_MATCHER_IMPLEMENTATION
716 #include "SparcGenAsmMatcher.inc"