1 //===-- MBlazeAsmParser.cpp - Parse MBlaze asm 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/MBlazeBaseInfo.h"
11 #include "llvm/MC/MCParser/MCAsmLexer.h"
12 #include "llvm/MC/MCParser/MCAsmParser.h"
13 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
14 #include "llvm/MC/MCStreamer.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCTargetAsmParser.h"
18 #include "llvm/Support/SourceMgr.h"
19 #include "llvm/Support/TargetRegistry.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Twine.h"
28 class MBlazeAsmParser : public MCTargetAsmParser {
31 MCAsmParser &getParser() const { return Parser; }
32 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
34 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
35 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
37 MBlazeOperand *ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
38 MBlazeOperand *ParseRegister(unsigned &RegNo);
39 MBlazeOperand *ParseImmediate();
40 MBlazeOperand *ParseFsl();
41 MBlazeOperand* ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
43 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
45 bool ParseDirectiveWord(unsigned Size, SMLoc L);
47 bool MatchAndEmitInstruction(SMLoc IDLoc,
48 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
51 /// @name Auto-generated Match Functions
54 #define GET_ASSEMBLER_HEADER
55 #include "MBlazeGenAsmMatcher.inc"
61 MBlazeAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
62 : MCTargetAsmParser(), Parser(_Parser) {}
64 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
65 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
67 virtual bool ParseDirective(AsmToken DirectiveID);
70 /// MBlazeOperand - Instances of this class represent a parsed MBlaze machine
72 struct MBlazeOperand : public MCParsedAsmOperand {
81 SMLoc StartLoc, EndLoc;
108 MBlazeOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
110 MBlazeOperand(const MBlazeOperand &o) : MCParsedAsmOperand() {
112 StartLoc = o.StartLoc;
133 /// getStartLoc - Get the location of the first token of this operand.
134 SMLoc getStartLoc() const { return StartLoc; }
136 /// getEndLoc - Get the location of the last token of this operand.
137 SMLoc getEndLoc() const { return EndLoc; }
139 unsigned getReg() const {
140 assert(Kind == Register && "Invalid access!");
144 const MCExpr *getImm() const {
145 assert(Kind == Immediate && "Invalid access!");
149 const MCExpr *getFslImm() const {
150 assert(Kind == Fsl && "Invalid access!");
154 unsigned getMemBase() const {
155 assert(Kind == Memory && "Invalid access!");
159 const MCExpr* getMemOff() const {
160 assert(Kind == Memory && "Invalid access!");
164 unsigned getMemOffReg() const {
165 assert(Kind == Memory && "Invalid access!");
169 bool isToken() const { return Kind == Token; }
170 bool isImm() const { return Kind == Immediate; }
171 bool isMem() const { return Kind == Memory; }
172 bool isFsl() const { return Kind == Fsl; }
173 bool isReg() const { return Kind == Register; }
175 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
176 // Add as immediates when possible. Null MCExpr = 0.
178 Inst.addOperand(MCOperand::CreateImm(0));
179 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
180 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
182 Inst.addOperand(MCOperand::CreateExpr(Expr));
185 void addRegOperands(MCInst &Inst, unsigned N) const {
186 assert(N == 1 && "Invalid number of operands!");
187 Inst.addOperand(MCOperand::CreateReg(getReg()));
190 void addImmOperands(MCInst &Inst, unsigned N) const {
191 assert(N == 1 && "Invalid number of operands!");
192 addExpr(Inst, getImm());
195 void addFslOperands(MCInst &Inst, unsigned N) const {
196 assert(N == 1 && "Invalid number of operands!");
197 addExpr(Inst, getFslImm());
200 void addMemOperands(MCInst &Inst, unsigned N) const {
201 assert(N == 2 && "Invalid number of operands!");
203 Inst.addOperand(MCOperand::CreateReg(getMemBase()));
205 unsigned RegOff = getMemOffReg();
207 Inst.addOperand(MCOperand::CreateReg(RegOff));
209 addExpr(Inst, getMemOff());
212 StringRef getToken() const {
213 assert(Kind == Token && "Invalid access!");
214 return StringRef(Tok.Data, Tok.Length);
217 virtual void print(raw_ostream &OS) const;
219 static MBlazeOperand *CreateToken(StringRef Str, SMLoc S) {
220 MBlazeOperand *Op = new MBlazeOperand(Token);
221 Op->Tok.Data = Str.data();
222 Op->Tok.Length = Str.size();
228 static MBlazeOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
229 MBlazeOperand *Op = new MBlazeOperand(Register);
230 Op->Reg.RegNum = RegNum;
236 static MBlazeOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
237 MBlazeOperand *Op = new MBlazeOperand(Immediate);
244 static MBlazeOperand *CreateFslImm(const MCExpr *Val, SMLoc S, SMLoc E) {
245 MBlazeOperand *Op = new MBlazeOperand(Fsl);
252 static MBlazeOperand *CreateMem(unsigned Base, const MCExpr *Off, SMLoc S,
254 MBlazeOperand *Op = new MBlazeOperand(Memory);
263 static MBlazeOperand *CreateMem(unsigned Base, unsigned Off, SMLoc S,
265 MBlazeOperand *Op = new MBlazeOperand(Memory);
267 Op->Mem.OffReg = Off;
275 } // end anonymous namespace.
277 void MBlazeOperand::print(raw_ostream &OS) const {
284 OS << getMBlazeRegisterNumbering(getReg()) << ">";
287 OS << "'" << getToken() << "'";
291 OS << getMBlazeRegisterNumbering(getMemBase());
294 unsigned RegOff = getMemOffReg();
296 OS << "R" << getMBlazeRegisterNumbering(RegOff);
303 getFslImm()->print(OS);
308 /// @name Auto-generated Match Functions
311 static unsigned MatchRegisterName(StringRef Name);
315 bool MBlazeAsmParser::
316 MatchAndEmitInstruction(SMLoc IDLoc,
317 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
323 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
326 Out.EmitInstruction(Inst);
328 case Match_MissingFeature:
329 return Error(IDLoc, "instruction use requires an option to be enabled");
330 case Match_MnemonicFail:
331 return Error(IDLoc, "unrecognized instruction mnemonic");
332 case Match_ConversionFail:
333 return Error(IDLoc, "unable to convert operands to instruction");
334 case Match_InvalidOperand:
336 if (ErrorInfo != ~0U) {
337 if (ErrorInfo >= Operands.size())
338 return Error(IDLoc, "too few operands for instruction");
340 ErrorLoc = ((MBlazeOperand*)Operands[ErrorInfo])->getStartLoc();
341 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
344 return Error(ErrorLoc, "invalid operand for instruction");
347 llvm_unreachable("Implement any new match types added!");
350 MBlazeOperand *MBlazeAsmParser::
351 ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
352 if (Operands.size() != 4)
355 MBlazeOperand &Base = *(MBlazeOperand*)Operands[2];
356 MBlazeOperand &Offset = *(MBlazeOperand*)Operands[3];
358 SMLoc S = Base.getStartLoc();
359 SMLoc O = Offset.getStartLoc();
360 SMLoc E = Offset.getEndLoc();
363 Error(S, "base address must be a register");
367 if (!Offset.isReg() && !Offset.isImm()) {
368 Error(O, "offset must be a register or immediate");
374 Op = MBlazeOperand::CreateMem(Base.getReg(), Offset.getReg(), S, E);
376 Op = MBlazeOperand::CreateMem(Base.getReg(), Offset.getImm(), S, E);
378 delete Operands.pop_back_val();
379 delete Operands.pop_back_val();
380 Operands.push_back(Op);
385 bool MBlazeAsmParser::ParseRegister(unsigned &RegNo,
386 SMLoc &StartLoc, SMLoc &EndLoc) {
387 return (ParseRegister(RegNo) == 0);
390 MBlazeOperand *MBlazeAsmParser::ParseRegister(unsigned &RegNo) {
391 SMLoc S = Parser.getTok().getLoc();
392 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
394 switch (getLexer().getKind()) {
396 case AsmToken::Identifier:
397 RegNo = MatchRegisterName(getLexer().getTok().getIdentifier());
402 return MBlazeOperand::CreateReg(RegNo, S, E);
406 static unsigned MatchFslRegister(StringRef String) {
407 if (!String.startswith("rfsl"))
411 if (String.substr(4).getAsInteger(10,regNum))
417 MBlazeOperand *MBlazeAsmParser::ParseFsl() {
418 SMLoc S = Parser.getTok().getLoc();
419 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
421 switch (getLexer().getKind()) {
423 case AsmToken::Identifier:
424 unsigned reg = MatchFslRegister(getLexer().getTok().getIdentifier());
429 const MCExpr *EVal = MCConstantExpr::Create(reg,getContext());
430 return MBlazeOperand::CreateFslImm(EVal,S,E);
434 MBlazeOperand *MBlazeAsmParser::ParseImmediate() {
435 SMLoc S = Parser.getTok().getLoc();
436 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
439 switch (getLexer().getKind()) {
441 case AsmToken::LParen:
443 case AsmToken::Minus:
444 case AsmToken::Integer:
445 case AsmToken::Identifier:
446 if (getParser().ParseExpression(EVal))
449 return MBlazeOperand::CreateImm(EVal, S, E);
453 MBlazeOperand *MBlazeAsmParser::
454 ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
457 // Attempt to parse the next token as a register name
459 Op = ParseRegister(RegNo);
461 // Attempt to parse the next token as an FSL immediate
465 // Attempt to parse the next token as an immediate
467 Op = ParseImmediate();
469 // If the token could not be parsed then fail
471 Error(Parser.getTok().getLoc(), "unknown operand");
475 // Push the parsed operand into the list of operands
476 Operands.push_back(Op);
480 /// Parse an mblaze instruction mnemonic followed by its operands.
481 bool MBlazeAsmParser::
482 ParseInstruction(StringRef Name, SMLoc NameLoc,
483 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
484 // The first operands is the token for the instruction name
485 size_t dotLoc = Name.find('.');
486 Operands.push_back(MBlazeOperand::CreateToken(Name.substr(0,dotLoc),NameLoc));
487 if (dotLoc < Name.size())
488 Operands.push_back(MBlazeOperand::CreateToken(Name.substr(dotLoc),NameLoc));
490 // If there are no more operands then finish
491 if (getLexer().is(AsmToken::EndOfStatement))
494 // Parse the first operand
495 if (!ParseOperand(Operands))
498 while (getLexer().isNot(AsmToken::EndOfStatement) &&
499 getLexer().is(AsmToken::Comma)) {
500 // Consume the comma token
503 // Parse the next operand
504 if (!ParseOperand(Operands))
508 // If the instruction requires a memory operand then we need to
509 // replace the last two operands (base+offset) with a single
511 if (Name.startswith("lw") || Name.startswith("sw") ||
512 Name.startswith("lh") || Name.startswith("sh") ||
513 Name.startswith("lb") || Name.startswith("sb"))
514 return (ParseMemory(Operands) == NULL);
519 /// ParseDirective parses the MBlaze specific directives
520 bool MBlazeAsmParser::ParseDirective(AsmToken DirectiveID) {
521 StringRef IDVal = DirectiveID.getIdentifier();
522 if (IDVal == ".word")
523 return ParseDirectiveWord(2, DirectiveID.getLoc());
527 /// ParseDirectiveWord
528 /// ::= .word [ expression (, expression)* ]
529 bool MBlazeAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
530 if (getLexer().isNot(AsmToken::EndOfStatement)) {
533 if (getParser().ParseExpression(Value))
536 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
538 if (getLexer().is(AsmToken::EndOfStatement))
541 // FIXME: Improve diagnostic.
542 if (getLexer().isNot(AsmToken::Comma))
543 return Error(L, "unexpected token in directive");
552 extern "C" void LLVMInitializeMBlazeAsmLexer();
554 /// Force static initialization.
555 extern "C" void LLVMInitializeMBlazeAsmParser() {
556 RegisterMCAsmParser<MBlazeAsmParser> X(TheMBlazeTarget);
557 LLVMInitializeMBlazeAsmLexer();
560 #define GET_REGISTER_MATCHER
561 #define GET_MATCHER_IMPLEMENTATION
562 #include "MBlazeGenAsmMatcher.inc"