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"
59 unsigned GetMCInstOperandNum(unsigned Kind, MCInst &Inst,
60 const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
61 unsigned OperandNum, unsigned &NumMCOperands) {
62 return GetMCInstOperandNumImpl(Kind, Inst, Operands, OperandNum,
67 MBlazeAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
68 : MCTargetAsmParser(), Parser(_Parser) {}
70 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
71 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
73 virtual bool ParseDirective(AsmToken DirectiveID);
76 /// MBlazeOperand - Instances of this class represent a parsed MBlaze machine
78 struct MBlazeOperand : public MCParsedAsmOperand {
87 SMLoc StartLoc, EndLoc;
114 MBlazeOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
116 MBlazeOperand(const MBlazeOperand &o) : MCParsedAsmOperand() {
118 StartLoc = o.StartLoc;
139 /// getStartLoc - Get the location of the first token of this operand.
140 SMLoc getStartLoc() const { return StartLoc; }
142 /// getEndLoc - Get the location of the last token of this operand.
143 SMLoc getEndLoc() const { return EndLoc; }
145 unsigned getReg() const {
146 assert(Kind == Register && "Invalid access!");
150 const MCExpr *getImm() const {
151 assert(Kind == Immediate && "Invalid access!");
155 const MCExpr *getFslImm() const {
156 assert(Kind == Fsl && "Invalid access!");
160 unsigned getMemBase() const {
161 assert(Kind == Memory && "Invalid access!");
165 const MCExpr* getMemOff() const {
166 assert(Kind == Memory && "Invalid access!");
170 unsigned getMemOffReg() const {
171 assert(Kind == Memory && "Invalid access!");
175 bool isToken() const { return Kind == Token; }
176 bool isImm() const { return Kind == Immediate; }
177 bool isMem() const { return Kind == Memory; }
178 bool isFsl() const { return Kind == Fsl; }
179 bool isReg() const { return Kind == Register; }
181 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
182 // Add as immediates when possible. Null MCExpr = 0.
184 Inst.addOperand(MCOperand::CreateImm(0));
185 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
186 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
188 Inst.addOperand(MCOperand::CreateExpr(Expr));
191 void addRegOperands(MCInst &Inst, unsigned N) const {
192 assert(N == 1 && "Invalid number of operands!");
193 Inst.addOperand(MCOperand::CreateReg(getReg()));
196 void addImmOperands(MCInst &Inst, unsigned N) const {
197 assert(N == 1 && "Invalid number of operands!");
198 addExpr(Inst, getImm());
201 void addFslOperands(MCInst &Inst, unsigned N) const {
202 assert(N == 1 && "Invalid number of operands!");
203 addExpr(Inst, getFslImm());
206 void addMemOperands(MCInst &Inst, unsigned N) const {
207 assert(N == 2 && "Invalid number of operands!");
209 Inst.addOperand(MCOperand::CreateReg(getMemBase()));
211 unsigned RegOff = getMemOffReg();
213 Inst.addOperand(MCOperand::CreateReg(RegOff));
215 addExpr(Inst, getMemOff());
218 StringRef getToken() const {
219 assert(Kind == Token && "Invalid access!");
220 return StringRef(Tok.Data, Tok.Length);
223 virtual void print(raw_ostream &OS) const;
225 static MBlazeOperand *CreateToken(StringRef Str, SMLoc S) {
226 MBlazeOperand *Op = new MBlazeOperand(Token);
227 Op->Tok.Data = Str.data();
228 Op->Tok.Length = Str.size();
234 static MBlazeOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
235 MBlazeOperand *Op = new MBlazeOperand(Register);
236 Op->Reg.RegNum = RegNum;
242 static MBlazeOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
243 MBlazeOperand *Op = new MBlazeOperand(Immediate);
250 static MBlazeOperand *CreateFslImm(const MCExpr *Val, SMLoc S, SMLoc E) {
251 MBlazeOperand *Op = new MBlazeOperand(Fsl);
258 static MBlazeOperand *CreateMem(unsigned Base, const MCExpr *Off, SMLoc S,
260 MBlazeOperand *Op = new MBlazeOperand(Memory);
269 static MBlazeOperand *CreateMem(unsigned Base, unsigned Off, SMLoc S,
271 MBlazeOperand *Op = new MBlazeOperand(Memory);
273 Op->Mem.OffReg = Off;
281 } // end anonymous namespace.
283 void MBlazeOperand::print(raw_ostream &OS) const {
290 OS << getMBlazeRegisterNumbering(getReg()) << ">";
293 OS << "'" << getToken() << "'";
297 OS << getMBlazeRegisterNumbering(getMemBase());
300 unsigned RegOff = getMemOffReg();
302 OS << "R" << getMBlazeRegisterNumbering(RegOff);
309 getFslImm()->print(OS);
314 /// @name Auto-generated Match Functions
317 static unsigned MatchRegisterName(StringRef Name);
321 bool MBlazeAsmParser::
322 MatchAndEmitInstruction(SMLoc IDLoc,
323 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
329 switch (MatchInstructionImpl(Operands, Kind, Inst, ErrorInfo)) {
332 Out.EmitInstruction(Inst);
334 case Match_MissingFeature:
335 return Error(IDLoc, "instruction use requires an option to be enabled");
336 case Match_MnemonicFail:
337 return Error(IDLoc, "unrecognized instruction mnemonic");
338 case Match_InvalidOperand: {
339 SMLoc ErrorLoc = IDLoc;
340 if (ErrorInfo != ~0U) {
341 if (ErrorInfo >= Operands.size())
342 return Error(IDLoc, "too few operands for instruction");
344 ErrorLoc = ((MBlazeOperand*)Operands[ErrorInfo])->getStartLoc();
345 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
348 return Error(ErrorLoc, "invalid operand for instruction");
352 llvm_unreachable("Implement any new match types added!");
355 MBlazeOperand *MBlazeAsmParser::
356 ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
357 if (Operands.size() != 4)
360 MBlazeOperand &Base = *(MBlazeOperand*)Operands[2];
361 MBlazeOperand &Offset = *(MBlazeOperand*)Operands[3];
363 SMLoc S = Base.getStartLoc();
364 SMLoc O = Offset.getStartLoc();
365 SMLoc E = Offset.getEndLoc();
368 Error(S, "base address must be a register");
372 if (!Offset.isReg() && !Offset.isImm()) {
373 Error(O, "offset must be a register or immediate");
379 Op = MBlazeOperand::CreateMem(Base.getReg(), Offset.getReg(), S, E);
381 Op = MBlazeOperand::CreateMem(Base.getReg(), Offset.getImm(), S, E);
383 delete Operands.pop_back_val();
384 delete Operands.pop_back_val();
385 Operands.push_back(Op);
390 bool MBlazeAsmParser::ParseRegister(unsigned &RegNo,
391 SMLoc &StartLoc, SMLoc &EndLoc) {
392 return (ParseRegister(RegNo) == 0);
395 MBlazeOperand *MBlazeAsmParser::ParseRegister(unsigned &RegNo) {
396 SMLoc S = Parser.getTok().getLoc();
397 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
399 switch (getLexer().getKind()) {
401 case AsmToken::Identifier:
402 RegNo = MatchRegisterName(getLexer().getTok().getIdentifier());
407 return MBlazeOperand::CreateReg(RegNo, S, E);
411 static unsigned MatchFslRegister(StringRef String) {
412 if (!String.startswith("rfsl"))
416 if (String.substr(4).getAsInteger(10,regNum))
422 MBlazeOperand *MBlazeAsmParser::ParseFsl() {
423 SMLoc S = Parser.getTok().getLoc();
424 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
426 switch (getLexer().getKind()) {
428 case AsmToken::Identifier:
429 unsigned reg = MatchFslRegister(getLexer().getTok().getIdentifier());
434 const MCExpr *EVal = MCConstantExpr::Create(reg,getContext());
435 return MBlazeOperand::CreateFslImm(EVal,S,E);
439 MBlazeOperand *MBlazeAsmParser::ParseImmediate() {
440 SMLoc S = Parser.getTok().getLoc();
441 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
444 switch (getLexer().getKind()) {
446 case AsmToken::LParen:
448 case AsmToken::Minus:
449 case AsmToken::Integer:
450 case AsmToken::Identifier:
451 if (getParser().ParseExpression(EVal))
454 return MBlazeOperand::CreateImm(EVal, S, E);
458 MBlazeOperand *MBlazeAsmParser::
459 ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
462 // Attempt to parse the next token as a register name
464 Op = ParseRegister(RegNo);
466 // Attempt to parse the next token as an FSL immediate
470 // Attempt to parse the next token as an immediate
472 Op = ParseImmediate();
474 // If the token could not be parsed then fail
476 Error(Parser.getTok().getLoc(), "unknown operand");
480 // Push the parsed operand into the list of operands
481 Operands.push_back(Op);
485 /// Parse an mblaze instruction mnemonic followed by its operands.
486 bool MBlazeAsmParser::
487 ParseInstruction(StringRef Name, SMLoc NameLoc,
488 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
489 // The first operands is the token for the instruction name
490 size_t dotLoc = Name.find('.');
491 Operands.push_back(MBlazeOperand::CreateToken(Name.substr(0,dotLoc),NameLoc));
492 if (dotLoc < Name.size())
493 Operands.push_back(MBlazeOperand::CreateToken(Name.substr(dotLoc),NameLoc));
495 // If there are no more operands then finish
496 if (getLexer().is(AsmToken::EndOfStatement))
499 // Parse the first operand
500 if (!ParseOperand(Operands))
503 while (getLexer().isNot(AsmToken::EndOfStatement) &&
504 getLexer().is(AsmToken::Comma)) {
505 // Consume the comma token
508 // Parse the next operand
509 if (!ParseOperand(Operands))
513 // If the instruction requires a memory operand then we need to
514 // replace the last two operands (base+offset) with a single
516 if (Name.startswith("lw") || Name.startswith("sw") ||
517 Name.startswith("lh") || Name.startswith("sh") ||
518 Name.startswith("lb") || Name.startswith("sb"))
519 return (ParseMemory(Operands) == NULL);
524 /// ParseDirective parses the MBlaze specific directives
525 bool MBlazeAsmParser::ParseDirective(AsmToken DirectiveID) {
526 StringRef IDVal = DirectiveID.getIdentifier();
527 if (IDVal == ".word")
528 return ParseDirectiveWord(2, DirectiveID.getLoc());
532 /// ParseDirectiveWord
533 /// ::= .word [ expression (, expression)* ]
534 bool MBlazeAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
535 if (getLexer().isNot(AsmToken::EndOfStatement)) {
538 if (getParser().ParseExpression(Value))
541 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
543 if (getLexer().is(AsmToken::EndOfStatement))
546 // FIXME: Improve diagnostic.
547 if (getLexer().isNot(AsmToken::Comma))
548 return Error(L, "unexpected token in directive");
557 extern "C" void LLVMInitializeMBlazeAsmLexer();
559 /// Force static initialization.
560 extern "C" void LLVMInitializeMBlazeAsmParser() {
561 RegisterMCAsmParser<MBlazeAsmParser> X(TheMBlazeTarget);
562 LLVMInitializeMBlazeAsmLexer();
565 #define GET_REGISTER_MATCHER
566 #define GET_MATCHER_IMPLEMENTATION
567 #include "MBlazeGenAsmMatcher.inc"