1 //===- MIParser.cpp - Machine instructions parser implementation ----------===//
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 // This file implements the parsing of machine instructions.
12 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "llvm/Target/TargetSubtargetInfo.h"
23 #include "llvm/Target/TargetInstrInfo.h"
33 StringRef Source, CurrentSource;
35 /// Maps from instruction names to op codes.
36 StringMap<unsigned> Names2InstrOpCodes;
39 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
44 /// Report an error at the current location with the given message.
46 /// This function always return true.
47 bool error(const Twine &Msg);
49 /// Report an error at the given location with the given message.
51 /// This function always return true.
52 bool error(StringRef::iterator Loc, const Twine &Msg);
54 MachineInstr *parse();
57 void initNames2InstrOpCodes();
59 /// Try to convert an instruction name to an opcode. Return true if the
60 /// instruction name is invalid.
61 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
63 bool parseInstruction(unsigned &OpCode);
66 } // end anonymous namespace
68 MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
70 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
71 Token(MIToken::Error, StringRef()) {}
73 void MIParser::lex() {
74 CurrentSource = lexMIToken(
76 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
79 bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
81 bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
82 // TODO: Get the proper location in the MIR file, not just a location inside
84 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
87 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
88 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
92 MachineInstr *MIParser::parse() {
96 if (Token.isError() || parseInstruction(OpCode))
99 // TODO: Parse the rest of instruction - machine operands, etc.
100 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
101 auto *MI = MF.CreateMachineInstr(MCID, DebugLoc());
105 bool MIParser::parseInstruction(unsigned &OpCode) {
106 if (Token.isNot(MIToken::Identifier))
107 return error("expected a machine instruction");
108 StringRef InstrName = Token.stringValue();
109 if (parseInstrName(InstrName, OpCode))
110 return error(Twine("unknown machine instruction name '") + InstrName + "'");
114 void MIParser::initNames2InstrOpCodes() {
115 if (!Names2InstrOpCodes.empty())
117 const auto *TII = MF.getSubtarget().getInstrInfo();
118 assert(TII && "Expected target instruction info");
119 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
120 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
123 bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
124 initNames2InstrOpCodes();
125 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
126 if (InstrInfo == Names2InstrOpCodes.end())
128 OpCode = InstrInfo->getValue();
132 MachineInstr *llvm::parseMachineInstr(SourceMgr &SM, MachineFunction &MF,
133 StringRef Src, SMDiagnostic &Error) {
134 return MIParser(SM, MF, Error, Src).parse();