2010-11-08 19:40:01 +00:00
|
|
|
//===-- MBlazeAsmParser.cpp - Parse MBlaze asm to MCInst instructions -----===//
|
2010-10-21 19:48:38 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-26 00:24:13 +00:00
|
|
|
#include "MCTargetDesc/MBlazeBaseInfo.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
2010-10-21 19:48:38 +00:00
|
|
|
#include "llvm/MC/MCParser/MCAsmLexer.h"
|
|
|
|
#include "llvm/MC/MCParser/MCAsmParser.h"
|
|
|
|
#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2011-07-26 00:24:13 +00:00
|
|
|
#include "llvm/MC/MCTargetAsmParser.h"
|
2010-10-21 19:48:38 +00:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2011-08-24 18:08:43 +00:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2010-10-21 19:48:38 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct MBlazeOperand;
|
|
|
|
|
2011-07-26 00:24:13 +00:00
|
|
|
class MBlazeAsmParser : public MCTargetAsmParser {
|
2010-10-21 19:48:38 +00:00
|
|
|
MCAsmParser &Parser;
|
|
|
|
|
|
|
|
MCAsmParser &getParser() const { return Parser; }
|
|
|
|
MCAsmLexer &getLexer() const { return Parser.getLexer(); }
|
|
|
|
|
|
|
|
void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
|
|
|
|
bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
MBlazeOperand *ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
|
2013-01-07 19:00:49 +00:00
|
|
|
MBlazeOperand *ParseRegister();
|
|
|
|
MBlazeOperand *ParseRegister(SMLoc &StartLoc, SMLoc &EndLoc);
|
2010-11-08 19:40:01 +00:00
|
|
|
MBlazeOperand *ParseImmediate();
|
|
|
|
MBlazeOperand *ParseFsl();
|
|
|
|
MBlazeOperand* ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2011-01-27 17:14:22 +00:00
|
|
|
virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
|
|
|
|
|
2010-11-12 23:30:17 +00:00
|
|
|
bool ParseDirectiveWord(unsigned Size, SMLoc L);
|
|
|
|
|
2012-10-13 00:26:04 +00:00
|
|
|
bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
|
2010-10-21 19:48:38 +00:00
|
|
|
SmallVectorImpl<MCParsedAsmOperand*> &Operands,
|
2012-10-13 00:26:04 +00:00
|
|
|
MCStreamer &Out, unsigned &ErrorInfo,
|
|
|
|
bool MatchingInlineAsm);
|
2010-10-21 19:48:38 +00:00
|
|
|
|
|
|
|
/// @name Auto-generated Match Functions
|
|
|
|
/// {
|
|
|
|
|
|
|
|
#define GET_ASSEMBLER_HEADER
|
|
|
|
#include "MBlazeGenAsmMatcher.inc"
|
|
|
|
|
|
|
|
/// }
|
|
|
|
|
|
|
|
public:
|
2011-07-09 05:47:46 +00:00
|
|
|
MBlazeAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
|
2011-07-26 00:24:13 +00:00
|
|
|
: MCTargetAsmParser(), Parser(_Parser) {}
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2012-10-25 20:41:34 +00:00
|
|
|
virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
|
|
|
|
SMLoc NameLoc,
|
2010-10-21 19:48:38 +00:00
|
|
|
SmallVectorImpl<MCParsedAsmOperand*> &Operands);
|
|
|
|
|
|
|
|
virtual bool ParseDirective(AsmToken DirectiveID);
|
|
|
|
};
|
2010-11-08 19:40:01 +00:00
|
|
|
|
2010-10-21 19:48:38 +00:00
|
|
|
/// MBlazeOperand - Instances of this class represent a parsed MBlaze machine
|
|
|
|
/// instruction.
|
|
|
|
struct MBlazeOperand : public MCParsedAsmOperand {
|
|
|
|
enum KindTy {
|
2010-11-08 19:40:01 +00:00
|
|
|
Token,
|
2010-10-21 19:48:38 +00:00
|
|
|
Immediate,
|
|
|
|
Register,
|
2010-11-08 19:40:01 +00:00
|
|
|
Memory,
|
|
|
|
Fsl
|
2010-10-21 19:48:38 +00:00
|
|
|
} Kind;
|
|
|
|
|
|
|
|
SMLoc StartLoc, EndLoc;
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
const char *Data;
|
|
|
|
unsigned Length;
|
|
|
|
} Tok;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
unsigned RegNum;
|
|
|
|
} Reg;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
const MCExpr *Val;
|
|
|
|
} Imm;
|
2010-11-08 19:40:01 +00:00
|
|
|
|
2010-10-21 19:48:38 +00:00
|
|
|
struct {
|
2010-11-08 19:40:01 +00:00
|
|
|
unsigned Base;
|
|
|
|
unsigned OffReg;
|
|
|
|
const MCExpr *Off;
|
2010-10-21 19:48:38 +00:00
|
|
|
} Mem;
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
struct {
|
|
|
|
const MCExpr *Val;
|
|
|
|
} FslImm;
|
2010-10-21 19:48:38 +00:00
|
|
|
};
|
2010-11-08 19:40:01 +00:00
|
|
|
|
|
|
|
MBlazeOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
|
|
|
|
public:
|
2010-10-21 19:48:38 +00:00
|
|
|
MBlazeOperand(const MBlazeOperand &o) : MCParsedAsmOperand() {
|
|
|
|
Kind = o.Kind;
|
|
|
|
StartLoc = o.StartLoc;
|
|
|
|
EndLoc = o.EndLoc;
|
|
|
|
switch (Kind) {
|
|
|
|
case Register:
|
|
|
|
Reg = o.Reg;
|
|
|
|
break;
|
|
|
|
case Immediate:
|
|
|
|
Imm = o.Imm;
|
|
|
|
break;
|
2010-11-08 19:40:01 +00:00
|
|
|
case Token:
|
|
|
|
Tok = o.Tok;
|
|
|
|
break;
|
2010-10-21 19:48:38 +00:00
|
|
|
case Memory:
|
|
|
|
Mem = o.Mem;
|
|
|
|
break;
|
2010-11-08 19:40:01 +00:00
|
|
|
case Fsl:
|
|
|
|
FslImm = o.FslImm;
|
|
|
|
break;
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-08 19:40:01 +00:00
|
|
|
|
2010-10-21 19:48:38 +00:00
|
|
|
/// getStartLoc - Get the location of the first token of this operand.
|
|
|
|
SMLoc getStartLoc() const { return StartLoc; }
|
2010-11-08 19:40:01 +00:00
|
|
|
|
2010-10-21 19:48:38 +00:00
|
|
|
/// getEndLoc - Get the location of the last token of this operand.
|
|
|
|
SMLoc getEndLoc() const { return EndLoc; }
|
|
|
|
|
|
|
|
unsigned getReg() const {
|
|
|
|
assert(Kind == Register && "Invalid access!");
|
|
|
|
return Reg.RegNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MCExpr *getImm() const {
|
|
|
|
assert(Kind == Immediate && "Invalid access!");
|
|
|
|
return Imm.Val;
|
|
|
|
}
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
const MCExpr *getFslImm() const {
|
|
|
|
assert(Kind == Fsl && "Invalid access!");
|
|
|
|
return FslImm.Val;
|
|
|
|
}
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
unsigned getMemBase() const {
|
|
|
|
assert(Kind == Memory && "Invalid access!");
|
|
|
|
return Mem.Base;
|
|
|
|
}
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
const MCExpr* getMemOff() const {
|
|
|
|
assert(Kind == Memory && "Invalid access!");
|
|
|
|
return Mem.Off;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned getMemOffReg() const {
|
|
|
|
assert(Kind == Memory && "Invalid access!");
|
|
|
|
return Mem.OffReg;
|
|
|
|
}
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
bool isToken() const { return Kind == Token; }
|
|
|
|
bool isImm() const { return Kind == Immediate; }
|
|
|
|
bool isMem() const { return Kind == Memory; }
|
|
|
|
bool isFsl() const { return Kind == Fsl; }
|
|
|
|
bool isReg() const { return Kind == Register; }
|
2010-10-21 19:48:38 +00:00
|
|
|
|
|
|
|
void addExpr(MCInst &Inst, const MCExpr *Expr) const {
|
2010-11-08 19:40:01 +00:00
|
|
|
// Add as immediates when possible. Null MCExpr = 0.
|
|
|
|
if (Expr == 0)
|
|
|
|
Inst.addOperand(MCOperand::CreateImm(0));
|
|
|
|
else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
|
2010-10-21 19:48:38 +00:00
|
|
|
Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
|
|
|
|
else
|
|
|
|
Inst.addOperand(MCOperand::CreateExpr(Expr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void addRegOperands(MCInst &Inst, unsigned N) const {
|
|
|
|
assert(N == 1 && "Invalid number of operands!");
|
|
|
|
Inst.addOperand(MCOperand::CreateReg(getReg()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void addImmOperands(MCInst &Inst, unsigned N) const {
|
|
|
|
assert(N == 1 && "Invalid number of operands!");
|
|
|
|
addExpr(Inst, getImm());
|
|
|
|
}
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
void addFslOperands(MCInst &Inst, unsigned N) const {
|
|
|
|
assert(N == 1 && "Invalid number of operands!");
|
|
|
|
addExpr(Inst, getFslImm());
|
|
|
|
}
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
void addMemOperands(MCInst &Inst, unsigned N) const {
|
|
|
|
assert(N == 2 && "Invalid number of operands!");
|
|
|
|
|
2010-11-12 23:30:17 +00:00
|
|
|
Inst.addOperand(MCOperand::CreateReg(getMemBase()));
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
unsigned RegOff = getMemOffReg();
|
|
|
|
if (RegOff)
|
|
|
|
Inst.addOperand(MCOperand::CreateReg(RegOff));
|
|
|
|
else
|
|
|
|
addExpr(Inst, getMemOff());
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef getToken() const {
|
|
|
|
assert(Kind == Token && "Invalid access!");
|
|
|
|
return StringRef(Tok.Data, Tok.Length);
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
|
2011-07-13 15:34:57 +00:00
|
|
|
virtual void print(raw_ostream &OS) const;
|
2010-11-08 19:40:01 +00:00
|
|
|
|
|
|
|
static MBlazeOperand *CreateToken(StringRef Str, SMLoc S) {
|
|
|
|
MBlazeOperand *Op = new MBlazeOperand(Token);
|
2010-10-21 19:48:38 +00:00
|
|
|
Op->Tok.Data = Str.data();
|
|
|
|
Op->Tok.Length = Str.size();
|
|
|
|
Op->StartLoc = S;
|
|
|
|
Op->EndLoc = S;
|
2010-11-08 19:40:01 +00:00
|
|
|
return Op;
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
static MBlazeOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
|
|
|
|
MBlazeOperand *Op = new MBlazeOperand(Register);
|
2010-10-21 19:48:38 +00:00
|
|
|
Op->Reg.RegNum = RegNum;
|
|
|
|
Op->StartLoc = S;
|
|
|
|
Op->EndLoc = E;
|
2010-11-08 19:40:01 +00:00
|
|
|
return Op;
|
|
|
|
}
|
|
|
|
|
|
|
|
static MBlazeOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
|
|
|
|
MBlazeOperand *Op = new MBlazeOperand(Immediate);
|
|
|
|
Op->Imm.Val = Val;
|
|
|
|
Op->StartLoc = S;
|
|
|
|
Op->EndLoc = E;
|
|
|
|
return Op;
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
static MBlazeOperand *CreateFslImm(const MCExpr *Val, SMLoc S, SMLoc E) {
|
|
|
|
MBlazeOperand *Op = new MBlazeOperand(Fsl);
|
2010-10-21 19:48:38 +00:00
|
|
|
Op->Imm.Val = Val;
|
|
|
|
Op->StartLoc = S;
|
|
|
|
Op->EndLoc = E;
|
2010-11-08 19:40:01 +00:00
|
|
|
return Op;
|
|
|
|
}
|
|
|
|
|
|
|
|
static MBlazeOperand *CreateMem(unsigned Base, const MCExpr *Off, SMLoc S,
|
|
|
|
SMLoc E) {
|
|
|
|
MBlazeOperand *Op = new MBlazeOperand(Memory);
|
|
|
|
Op->Mem.Base = Base;
|
|
|
|
Op->Mem.Off = Off;
|
|
|
|
Op->Mem.OffReg = 0;
|
|
|
|
Op->StartLoc = S;
|
|
|
|
Op->EndLoc = E;
|
|
|
|
return Op;
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
static MBlazeOperand *CreateMem(unsigned Base, unsigned Off, SMLoc S,
|
|
|
|
SMLoc E) {
|
|
|
|
MBlazeOperand *Op = new MBlazeOperand(Memory);
|
|
|
|
Op->Mem.Base = Base;
|
|
|
|
Op->Mem.OffReg = Off;
|
|
|
|
Op->Mem.Off = 0;
|
2010-10-21 19:48:38 +00:00
|
|
|
Op->StartLoc = S;
|
|
|
|
Op->EndLoc = E;
|
2010-11-08 19:40:01 +00:00
|
|
|
return Op;
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace.
|
|
|
|
|
2011-07-13 15:34:57 +00:00
|
|
|
void MBlazeOperand::print(raw_ostream &OS) const {
|
2010-10-21 19:48:38 +00:00
|
|
|
switch (Kind) {
|
|
|
|
case Immediate:
|
|
|
|
getImm()->print(OS);
|
|
|
|
break;
|
|
|
|
case Register:
|
2010-11-12 23:30:17 +00:00
|
|
|
OS << "<register R";
|
2011-07-25 20:18:18 +00:00
|
|
|
OS << getMBlazeRegisterNumbering(getReg()) << ">";
|
2010-10-21 19:48:38 +00:00
|
|
|
break;
|
|
|
|
case Token:
|
|
|
|
OS << "'" << getToken() << "'";
|
|
|
|
break;
|
2010-11-12 23:30:17 +00:00
|
|
|
case Memory: {
|
|
|
|
OS << "<memory R";
|
2011-07-25 20:18:18 +00:00
|
|
|
OS << getMBlazeRegisterNumbering(getMemBase());
|
2010-11-12 23:30:17 +00:00
|
|
|
OS << ", ";
|
|
|
|
|
|
|
|
unsigned RegOff = getMemOffReg();
|
|
|
|
if (RegOff)
|
2011-07-25 20:18:18 +00:00
|
|
|
OS << "R" << getMBlazeRegisterNumbering(RegOff);
|
2010-11-12 23:30:17 +00:00
|
|
|
else
|
|
|
|
OS << getMemOff();
|
|
|
|
OS << ">";
|
|
|
|
}
|
2010-11-08 19:40:01 +00:00
|
|
|
break;
|
|
|
|
case Fsl:
|
|
|
|
getFslImm()->print(OS);
|
|
|
|
break;
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @name Auto-generated Match Functions
|
|
|
|
/// {
|
|
|
|
|
|
|
|
static unsigned MatchRegisterName(StringRef Name);
|
|
|
|
|
|
|
|
/// }
|
2010-11-08 19:40:01 +00:00
|
|
|
//
|
|
|
|
bool MBlazeAsmParser::
|
2012-10-13 00:26:04 +00:00
|
|
|
MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
|
2010-11-08 19:40:01 +00:00
|
|
|
SmallVectorImpl<MCParsedAsmOperand*> &Operands,
|
2012-10-13 00:26:04 +00:00
|
|
|
MCStreamer &Out, unsigned &ErrorInfo,
|
|
|
|
bool MatchingInlineAsm) {
|
2010-11-08 19:40:01 +00:00
|
|
|
MCInst Inst;
|
2012-10-12 22:53:36 +00:00
|
|
|
switch (MatchInstructionImpl(Operands, Inst, ErrorInfo,
|
2012-10-15 16:50:34 +00:00
|
|
|
MatchingInlineAsm)) {
|
2011-08-15 23:03:29 +00:00
|
|
|
default: break;
|
2010-11-08 19:40:01 +00:00
|
|
|
case Match_Success:
|
|
|
|
Out.EmitInstruction(Inst);
|
|
|
|
return false;
|
|
|
|
case Match_MissingFeature:
|
|
|
|
return Error(IDLoc, "instruction use requires an option to be enabled");
|
|
|
|
case Match_MnemonicFail:
|
|
|
|
return Error(IDLoc, "unrecognized instruction mnemonic");
|
2012-09-03 16:21:15 +00:00
|
|
|
case Match_InvalidOperand: {
|
2012-09-03 01:55:11 +00:00
|
|
|
SMLoc ErrorLoc = IDLoc;
|
2010-11-08 19:40:01 +00:00
|
|
|
if (ErrorInfo != ~0U) {
|
|
|
|
if (ErrorInfo >= Operands.size())
|
|
|
|
return Error(IDLoc, "too few operands for instruction");
|
|
|
|
|
|
|
|
ErrorLoc = ((MBlazeOperand*)Operands[ErrorInfo])->getStartLoc();
|
|
|
|
if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
return Error(ErrorLoc, "invalid operand for instruction");
|
|
|
|
}
|
2012-09-03 16:21:15 +00:00
|
|
|
}
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
llvm_unreachable("Implement any new match types added!");
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
MBlazeOperand *MBlazeAsmParser::
|
|
|
|
ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
|
|
|
|
if (Operands.size() != 4)
|
|
|
|
return 0;
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
MBlazeOperand &Base = *(MBlazeOperand*)Operands[2];
|
|
|
|
MBlazeOperand &Offset = *(MBlazeOperand*)Operands[3];
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
SMLoc S = Base.getStartLoc();
|
|
|
|
SMLoc O = Offset.getStartLoc();
|
|
|
|
SMLoc E = Offset.getEndLoc();
|
|
|
|
|
|
|
|
if (!Base.isReg()) {
|
|
|
|
Error(S, "base address must be a register");
|
|
|
|
return 0;
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
if (!Offset.isReg() && !Offset.isImm()) {
|
|
|
|
Error(O, "offset must be a register or immediate");
|
|
|
|
return 0;
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
MBlazeOperand *Op;
|
|
|
|
if (Offset.isReg())
|
|
|
|
Op = MBlazeOperand::CreateMem(Base.getReg(), Offset.getReg(), S, E);
|
|
|
|
else
|
|
|
|
Op = MBlazeOperand::CreateMem(Base.getReg(), Offset.getImm(), S, E);
|
|
|
|
|
|
|
|
delete Operands.pop_back_val();
|
|
|
|
delete Operands.pop_back_val();
|
|
|
|
Operands.push_back(Op);
|
|
|
|
|
|
|
|
return Op;
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
|
2011-02-03 21:46:10 +00:00
|
|
|
bool MBlazeAsmParser::ParseRegister(unsigned &RegNo,
|
|
|
|
SMLoc &StartLoc, SMLoc &EndLoc) {
|
2013-01-07 19:00:49 +00:00
|
|
|
MBlazeOperand *Reg = ParseRegister(StartLoc, EndLoc);
|
|
|
|
if (!Reg)
|
|
|
|
return true;
|
|
|
|
RegNo = Reg->getReg();
|
|
|
|
return false;
|
2011-01-27 17:14:22 +00:00
|
|
|
}
|
|
|
|
|
2013-01-07 19:00:49 +00:00
|
|
|
MBlazeOperand *MBlazeAsmParser::ParseRegister() {
|
|
|
|
SMLoc S, E;
|
|
|
|
return ParseRegister(S, E);
|
|
|
|
}
|
2010-11-08 19:40:01 +00:00
|
|
|
|
2013-01-07 19:00:49 +00:00
|
|
|
MBlazeOperand *MBlazeAsmParser::ParseRegister(SMLoc &StartLoc, SMLoc &EndLoc) {
|
|
|
|
StartLoc = Parser.getTok().getLoc();
|
|
|
|
EndLoc = Parser.getTok().getEndLoc();
|
2010-11-08 19:40:01 +00:00
|
|
|
|
2013-01-07 19:00:49 +00:00
|
|
|
if (getLexer().getKind() != AsmToken::Identifier)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
unsigned RegNo = MatchRegisterName(getLexer().getTok().getIdentifier());
|
|
|
|
if (RegNo == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
getLexer().Lex();
|
|
|
|
return MBlazeOperand::CreateReg(RegNo, StartLoc, EndLoc);
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 18:19:06 +00:00
|
|
|
static unsigned MatchFslRegister(StringRef String) {
|
2010-11-08 19:40:01 +00:00
|
|
|
if (!String.startswith("rfsl"))
|
|
|
|
return -1;
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
unsigned regNum;
|
|
|
|
if (String.substr(4).getAsInteger(10,regNum))
|
|
|
|
return -1;
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
return regNum;
|
|
|
|
}
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
MBlazeOperand *MBlazeAsmParser::ParseFsl() {
|
|
|
|
SMLoc S = Parser.getTok().getLoc();
|
2013-01-07 19:00:49 +00:00
|
|
|
SMLoc E = Parser.getTok().getEndLoc();
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
switch (getLexer().getKind()) {
|
|
|
|
default: return 0;
|
|
|
|
case AsmToken::Identifier:
|
|
|
|
unsigned reg = MatchFslRegister(getLexer().getTok().getIdentifier());
|
|
|
|
if (reg >= 16)
|
|
|
|
return 0;
|
|
|
|
|
2010-11-12 23:30:17 +00:00
|
|
|
getLexer().Lex();
|
2010-11-08 19:40:01 +00:00
|
|
|
const MCExpr *EVal = MCConstantExpr::Create(reg,getContext());
|
|
|
|
return MBlazeOperand::CreateFslImm(EVal,S,E);
|
|
|
|
}
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
MBlazeOperand *MBlazeAsmParser::ParseImmediate() {
|
|
|
|
SMLoc S = Parser.getTok().getLoc();
|
2013-01-07 19:00:49 +00:00
|
|
|
SMLoc E = Parser.getTok().getEndLoc();
|
2010-11-08 19:40:01 +00:00
|
|
|
|
|
|
|
const MCExpr *EVal;
|
2010-10-21 19:48:38 +00:00
|
|
|
switch (getLexer().getKind()) {
|
2010-11-08 19:40:01 +00:00
|
|
|
default: return 0;
|
|
|
|
case AsmToken::LParen:
|
|
|
|
case AsmToken::Plus:
|
|
|
|
case AsmToken::Minus:
|
|
|
|
case AsmToken::Integer:
|
2010-10-21 19:48:38 +00:00
|
|
|
case AsmToken::Identifier:
|
2013-02-20 22:21:35 +00:00
|
|
|
if (getParser().parseExpression(EVal))
|
2010-11-08 19:40:01 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return MBlazeOperand::CreateImm(EVal, S, E);
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
MBlazeOperand *MBlazeAsmParser::
|
|
|
|
ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
|
|
|
|
MBlazeOperand *Op;
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
// Attempt to parse the next token as a register name
|
2013-01-07 19:00:49 +00:00
|
|
|
Op = ParseRegister();
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
// Attempt to parse the next token as an FSL immediate
|
|
|
|
if (!Op)
|
|
|
|
Op = ParseFsl();
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
// Attempt to parse the next token as an immediate
|
|
|
|
if (!Op)
|
|
|
|
Op = ParseImmediate();
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
// If the token could not be parsed then fail
|
|
|
|
if (!Op) {
|
|
|
|
Error(Parser.getTok().getLoc(), "unknown operand");
|
|
|
|
return 0;
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
// Push the parsed operand into the list of operands
|
|
|
|
Operands.push_back(Op);
|
|
|
|
return Op;
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
/// Parse an mblaze instruction mnemonic followed by its operands.
|
|
|
|
bool MBlazeAsmParser::
|
2012-10-25 20:41:34 +00:00
|
|
|
ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
|
2010-11-08 19:40:01 +00:00
|
|
|
SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
|
2010-11-12 23:30:17 +00:00
|
|
|
// The first operands is the token for the instruction name
|
|
|
|
size_t dotLoc = Name.find('.');
|
|
|
|
Operands.push_back(MBlazeOperand::CreateToken(Name.substr(0,dotLoc),NameLoc));
|
|
|
|
if (dotLoc < Name.size())
|
|
|
|
Operands.push_back(MBlazeOperand::CreateToken(Name.substr(dotLoc),NameLoc));
|
2010-11-08 19:40:01 +00:00
|
|
|
|
|
|
|
// If there are no more operands then finish
|
|
|
|
if (getLexer().is(AsmToken::EndOfStatement))
|
|
|
|
return false;
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
// Parse the first operand
|
2010-11-11 18:41:33 +00:00
|
|
|
if (!ParseOperand(Operands))
|
2010-11-08 19:40:01 +00:00
|
|
|
return true;
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
while (getLexer().isNot(AsmToken::EndOfStatement) &&
|
|
|
|
getLexer().is(AsmToken::Comma)) {
|
|
|
|
// Consume the comma token
|
|
|
|
getLexer().Lex();
|
|
|
|
|
|
|
|
// Parse the next operand
|
2010-11-11 18:41:33 +00:00
|
|
|
if (!ParseOperand(Operands))
|
2010-11-08 19:40:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-10-21 19:48:38 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
// If the instruction requires a memory operand then we need to
|
|
|
|
// replace the last two operands (base+offset) with a single
|
|
|
|
// memory operand.
|
|
|
|
if (Name.startswith("lw") || Name.startswith("sw") ||
|
|
|
|
Name.startswith("lh") || Name.startswith("sh") ||
|
|
|
|
Name.startswith("lb") || Name.startswith("sb"))
|
2010-11-12 23:30:17 +00:00
|
|
|
return (ParseMemory(Operands) == NULL);
|
2010-10-21 19:48:38 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-21 19:23:46 +00:00
|
|
|
/// ParseDirective parses the MBlaze specific directives
|
2010-11-08 19:40:01 +00:00
|
|
|
bool MBlazeAsmParser::ParseDirective(AsmToken DirectiveID) {
|
2010-11-12 23:30:17 +00:00
|
|
|
StringRef IDVal = DirectiveID.getIdentifier();
|
|
|
|
if (IDVal == ".word")
|
|
|
|
return ParseDirectiveWord(2, DirectiveID.getLoc());
|
2010-11-08 19:40:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-12 23:30:17 +00:00
|
|
|
/// ParseDirectiveWord
|
|
|
|
/// ::= .word [ expression (, expression)* ]
|
|
|
|
bool MBlazeAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
|
|
|
|
if (getLexer().isNot(AsmToken::EndOfStatement)) {
|
|
|
|
for (;;) {
|
|
|
|
const MCExpr *Value;
|
2013-02-20 22:21:35 +00:00
|
|
|
if (getParser().parseExpression(Value))
|
2010-11-12 23:30:17 +00:00
|
|
|
return true;
|
|
|
|
|
2013-01-09 03:52:05 +00:00
|
|
|
getParser().getStreamer().EmitValue(Value, Size);
|
2010-11-12 23:30:17 +00:00
|
|
|
|
|
|
|
if (getLexer().is(AsmToken::EndOfStatement))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// FIXME: Improve diagnostic.
|
|
|
|
if (getLexer().isNot(AsmToken::Comma))
|
|
|
|
return Error(L, "unexpected token in directive");
|
|
|
|
Parser.Lex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Parser.Lex();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-21 19:48:38 +00:00
|
|
|
/// Force static initialization.
|
|
|
|
extern "C" void LLVMInitializeMBlazeAsmParser() {
|
2011-07-26 00:24:13 +00:00
|
|
|
RegisterMCAsmParser<MBlazeAsmParser> X(TheMBlazeTarget);
|
2010-10-21 19:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define GET_REGISTER_MATCHER
|
|
|
|
#define GET_MATCHER_IMPLEMENTATION
|
|
|
|
#include "MBlazeGenAsmMatcher.inc"
|