30#define DEBUG_TYPE "msp430-asm-parser"
40 bool matchAndEmitInstruction(
SMLoc IDLoc,
unsigned &Opcode,
43 bool MatchingInlineAsm)
override;
47 SMLoc &EndLoc)
override;
53 bool ParseDirectiveRefSym(
AsmToken DirectiveID);
60 bool ParseLiteralValues(
unsigned Size,
SMLoc L);
68#define GET_ASSEMBLER_HEADER
69#include "MSP430GenAsmMatcher.inc"
79 setAvailableFeatures(ComputeAvailableFeatures(STI.
getFeatureBits()));
85 typedef MCParsedAsmOperand Base;
110 MSP430Operand(StringRef Tok, SMLoc S)
111 : Kind(k_Tok), Tok(Tok), Start(S), End(S) {}
112 MSP430Operand(KindTy Kind, MCRegister
Reg, SMLoc S, SMLoc
E)
113 : Kind(Kind),
Reg(
Reg), Start(S), End(
E) {}
114 MSP430Operand(MCExpr
const *
Imm, SMLoc S, SMLoc
E)
115 : Kind(k_Imm),
Imm(
Imm), Start(S), End(
E) {}
116 MSP430Operand(MCRegister
Reg, MCExpr
const *Expr, SMLoc S, SMLoc
E)
117 : Kind(k_Mem), Mem({
Reg, Expr}), Start(S),
End(
E) {}
119 void addRegOperands(MCInst &Inst,
unsigned N)
const {
120 assert((Kind == k_Reg || Kind == k_IndReg || Kind == k_PostIndReg) &&
121 "Unexpected operand kind");
122 assert(
N == 1 &&
"Invalid number of operands!");
127 void addExprOperand(MCInst &Inst,
const MCExpr *Expr)
const {
137 void addImmOperands(MCInst &Inst,
unsigned N)
const {
138 assert(Kind == k_Imm &&
"Unexpected operand kind");
139 assert(
N == 1 &&
"Invalid number of operands!");
141 addExprOperand(Inst,
Imm);
144 void addMemOperands(MCInst &Inst,
unsigned N)
const {
145 assert(Kind == k_Mem &&
"Unexpected operand kind");
146 assert(
N == 2 &&
"Invalid number of operands");
149 addExprOperand(Inst, Mem.Offset);
152 bool isReg()
const override {
return Kind == k_Reg; }
153 bool isImm()
const override {
return Kind == k_Imm; }
154 bool isToken()
const override {
return Kind == k_Tok; }
155 bool isMem()
const override {
return Kind == k_Mem; }
156 bool isIndReg()
const {
return Kind == k_IndReg; }
157 bool isPostIndReg()
const {
return Kind == k_PostIndReg; }
159 bool isCGImm()
const {
164 if (!
Imm->evaluateAsAbsolute(Val))
167 if (Val == 0 || Val == 1 || Val == 2 || Val == 4 || Val == 8 || Val == -1)
174 assert(Kind == k_Tok &&
"Invalid access!");
178 MCRegister
getReg()
const override {
179 assert(Kind == k_Reg &&
"Invalid access!");
183 static std::unique_ptr<MSP430Operand> CreateToken(StringRef Str, SMLoc S) {
184 return std::make_unique<MSP430Operand>(Str, S);
187 static std::unique_ptr<MSP430Operand> CreateReg(MCRegister
Reg, SMLoc S,
189 return std::make_unique<MSP430Operand>(k_Reg,
Reg, S,
E);
192 static std::unique_ptr<MSP430Operand> CreateImm(
const MCExpr *Val, SMLoc S,
194 return std::make_unique<MSP430Operand>(Val, S,
E);
197 static std::unique_ptr<MSP430Operand>
198 CreateMem(MCRegister
Reg,
const MCExpr *Val, SMLoc S, SMLoc
E) {
199 return std::make_unique<MSP430Operand>(
Reg, Val, S,
E);
202 static std::unique_ptr<MSP430Operand> CreateIndReg(MCRegister
Reg, SMLoc S,
204 return std::make_unique<MSP430Operand>(k_IndReg,
Reg, S,
E);
207 static std::unique_ptr<MSP430Operand> CreatePostIndReg(MCRegister
Reg,
209 return std::make_unique<MSP430Operand>(k_PostIndReg,
Reg, S,
E);
212 SMLoc getStartLoc()
const override {
return Start; }
213 SMLoc getEndLoc()
const override {
return End; }
215 void print(raw_ostream &O,
const MCAsmInfo &MAI)
const override {
218 O <<
"Token " << Tok;
221 O <<
"Register " <<
Reg.
id();
232 O <<
"RegInd " <<
Reg.
id();
235 O <<
"PostInc " <<
Reg.
id();
242bool MSP430AsmParser::matchAndEmitInstruction(
SMLoc Loc,
unsigned &Opcode,
246 bool MatchingInlineAsm) {
248 unsigned MatchResult =
249 MatchInstructionImpl(
Operands, Inst, ErrorInfo, MatchingInlineAsm);
251 switch (MatchResult) {
254 Out.emitInstruction(Inst, *STI);
256 case Match_MnemonicFail:
257 return Error(Loc,
"invalid instruction mnemonic");
258 case Match_InvalidOperand: {
259 SMLoc ErrorLoc = Loc;
260 if (ErrorInfo != ~0U) {
262 return Error(ErrorLoc,
"too few operands for instruction");
264 ErrorLoc = ((MSP430Operand &)*
Operands[ErrorInfo]).getStartLoc();
265 if (ErrorLoc == SMLoc())
268 return Error(ErrorLoc,
"invalid operand for instruction");
283 return Error(StartLoc,
"invalid register name");
292ParseStatus MSP430AsmParser::tryParseRegister(MCRegister &
Reg, SMLoc &StartLoc,
295 auto Name = getLexer().getTok().getIdentifier().lower();
297 if (
Reg == MSP430::NoRegister) {
299 if (
Reg == MSP430::NoRegister)
303 AsmToken
const &
T = getParser().getTok();
304 StartLoc =
T.getLoc();
305 EndLoc =
T.getEndLoc();
314bool MSP430AsmParser::parseJccInstruction(ParseInstructionInfo &Info,
315 StringRef Name, SMLoc NameLoc,
317 if (!
Name.starts_with_insensitive(
"j"))
320 auto CC =
Name.drop_front().lower();
322 if (
CC ==
"ne" ||
CC ==
"nz")
324 else if (
CC ==
"eq" ||
CC ==
"z")
326 else if (
CC ==
"lo" ||
CC ==
"nc")
328 else if (
CC ==
"hs" ||
CC ==
"c")
339 return Error(NameLoc,
"unknown instruction");
342 Operands.push_back(MSP430Operand::CreateToken(
"jmp", NameLoc));
344 Operands.push_back(MSP430Operand::CreateToken(
"j", NameLoc));
346 Operands.push_back(MSP430Operand::CreateImm(CCode, SMLoc(), SMLoc()));
353 SMLoc ExprLoc = getLexer().getLoc();
354 if (getParser().parseExpression(Val))
355 return Error(ExprLoc,
"expected expression operand");
358 if (Val->evaluateAsAbsolute(Res))
359 if (Res < -512 || Res > 511)
360 return Error(ExprLoc,
"invalid jump offset");
362 Operands.push_back(MSP430Operand::CreateImm(Val, ExprLoc,
363 getLexer().getLoc()));
366 SMLoc Loc = getLexer().getLoc();
367 getParser().eatToEndOfStatement();
368 return Error(Loc,
"unexpected token");
375bool MSP430AsmParser::parseInstruction(ParseInstructionInfo &Info,
376 StringRef Name, SMLoc NameLoc,
379 if (
Name.ends_with_insensitive(
".w"))
382 if (!parseJccInstruction(Info, Name, NameLoc,
Operands))
386 Operands.push_back(MSP430Operand::CreateToken(Name, NameLoc));
401 SMLoc Loc = getLexer().getLoc();
402 getParser().eatToEndOfStatement();
403 return Error(Loc,
"unexpected token");
410bool MSP430AsmParser::ParseDirectiveRefSym(AsmToken DirectiveID) {
412 if (getParser().parseIdentifier(Name))
413 return TokError(
"expected identifier in directive");
416 getStreamer().emitSymbolAttribute(Sym,
MCSA_Global);
420ParseStatus MSP430AsmParser::parseDirective(AsmToken DirectiveID) {
422 if (IDVal.
lower() ==
".long")
423 return ParseLiteralValues(4, DirectiveID.
getLoc());
424 if (IDVal.
lower() ==
".word" || IDVal.
lower() ==
".short")
425 return ParseLiteralValues(2, DirectiveID.
getLoc());
426 if (IDVal.
lower() ==
".byte")
427 return ParseLiteralValues(1, DirectiveID.
getLoc());
428 if (IDVal.
lower() ==
".refsym")
429 return ParseDirectiveRefSym(DirectiveID);
434 switch (getLexer().getKind()) {
435 default:
return true;
439 SMLoc StartLoc, EndLoc;
440 if (!parseRegister(RegNo, StartLoc, EndLoc)) {
441 Operands.push_back(MSP430Operand::CreateReg(RegNo, StartLoc, EndLoc));
449 SMLoc StartLoc = getParser().getTok().getLoc();
452 if (!getParser().parseExpression(Val)) {
453 MCRegister RegNo = MSP430::PC;
454 SMLoc EndLoc = getParser().getTok().getLoc();
458 if (parseRegister(RegNo, RegStartLoc, EndLoc))
460 EndLoc = getParser().getTok().getEndLoc();
464 Operands.push_back(MSP430Operand::CreateMem(RegNo, Val, StartLoc,
472 SMLoc StartLoc = getParser().getTok().getLoc();
475 if (!getParser().parseExpression(Val)) {
476 SMLoc EndLoc = getParser().getTok().getLoc();
477 Operands.push_back(MSP430Operand::CreateMem(MSP430::SR, Val, StartLoc,
485 SMLoc StartLoc = getParser().getTok().getLoc();
488 SMLoc RegStartLoc, EndLoc;
489 if (parseRegister(RegNo, RegStartLoc, EndLoc))
492 Operands.push_back(MSP430Operand::CreatePostIndReg(RegNo, StartLoc, EndLoc));
496 Operands.push_back(MSP430Operand::CreateMem(RegNo,
499 Operands.push_back(MSP430Operand::CreateIndReg(RegNo, StartLoc, EndLoc));
504 SMLoc StartLoc = getParser().getTok().getLoc();
507 if (!getParser().parseExpression(Val)) {
508 SMLoc EndLoc = getParser().getTok().getLoc();
509 Operands.push_back(MSP430Operand::CreateImm(Val, StartLoc, EndLoc));
516bool MSP430AsmParser::ParseLiteralValues(
unsigned Size, SMLoc L) {
517 auto parseOne = [&]() ->
bool {
519 if (getParser().parseExpression(
Value))
521 getParser().getStreamer().emitValue(
Value,
Size, L);
524 return (parseMany(parseOne));
532#define GET_REGISTER_MATCHER
533#define GET_MATCHER_IMPLEMENTATION
534#include "MSP430GenAsmMatcher.inc"
static MCRegister MatchRegisterName(StringRef Name)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isNot(const MachineRegisterInfo &MRI, const MachineInstr &MI)
This file implements a class to represent arbitrary precision integral constant values and operations...
static MCRegister MatchRegisterAltName(StringRef Name)
Maps from the set of all alternative registernames to a register number.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_EXTERNAL_VISIBILITY
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeMSP430AsmParser()
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static bool isReg(const MCInst &MI, unsigned OpNo)
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
Target independent representation for an assembler token.
LLVM_ABI SMLoc getLoc() const
StringRef getIdentifier() const
Get the identifier string for the current token, which should be an identifier or a string.
Base class for user error types.
void printExpr(raw_ostream &, const MCExpr &) const
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
Generic assembler parser interface, for use by target specific assembly parsers.
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
void addOperand(const MCOperand Op)
Interface to description of machine instruction set.
static MCOperand createExpr(const MCExpr *Val)
static MCOperand createReg(MCRegister Reg)
static MCOperand createImm(int64_t Val)
MCParsedAsmOperand - This abstract class represents a source-level assembly instruction operand.
Wrapper class representing physical registers. Should be passed by value.
Streaming machine code generation interface.
Generic base class for all target subtargets.
const FeatureBitset & getFeatureBits() const
MCTargetAsmParser - Generic interface to target specific assembly parsers.
Ternary parse status returned by various parse* methods.
constexpr bool isFailure() const
static constexpr StatusTy Failure
constexpr bool isSuccess() const
static constexpr StatusTy Success
static constexpr StatusTy NoMatch
constexpr bool isNoMatch() const
constexpr unsigned id() const
Represents a location in source code.
Represent a constant reference to a string, i.e.
LLVM_ABI std::string lower() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
@ CE
Windows NT (Windows on ARM)
This is an optimization pass for GlobalISel generic memory operations.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
static bool isMem(const MachineInstr &MI, unsigned Op)
LLVM_ABI std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \t\n\v\f\r")
getToken - This function extracts one token from source, ignoring any leading characters that appear ...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Target & getTheMSP430Target()
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
SmallVectorImpl< std::unique_ptr< MCParsedAsmOperand > > OperandVector
@ End
Run the machine verifier once, at the end of the pipeline.
@ MCSA_Global
.type _foo, @gnu_unique_object
RegisterMCAsmParser - Helper template for registering a target specific assembly parser,...