blob: a677b7c47f700ce999837a25479a76c07769961d [file] [log] [blame]
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001//===- MIParser.cpp - Machine instructions parser implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the parsing of machine instructions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MIParser.h"
Alex Lorenz91370c52015-06-22 20:37:46 +000015#include "MILexer.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000016#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"
24
25using namespace llvm;
26
27namespace {
28
29class MIParser {
30 SourceMgr &SM;
31 MachineFunction &MF;
32 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000033 StringRef Source, CurrentSource;
34 MIToken Token;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000035 /// Maps from instruction names to op codes.
36 StringMap<unsigned> Names2InstrOpCodes;
37
38public:
39 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
40 StringRef Source);
41
Alex Lorenz91370c52015-06-22 20:37:46 +000042 void lex();
43
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000044 /// Report an error at the current location with the given message.
45 ///
46 /// This function always return true.
47 bool error(const Twine &Msg);
48
Alex Lorenz91370c52015-06-22 20:37:46 +000049 /// Report an error at the given location with the given message.
50 ///
51 /// This function always return true.
52 bool error(StringRef::iterator Loc, const Twine &Msg);
53
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000054 MachineInstr *parse();
55
56private:
57 void initNames2InstrOpCodes();
58
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);
Alex Lorenz91370c52015-06-22 20:37:46 +000062
63 bool parseInstruction(unsigned &OpCode);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000064};
65
66} // end anonymous namespace
67
68MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
69 StringRef Source)
Alex Lorenz91370c52015-06-22 20:37:46 +000070 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
71 Token(MIToken::Error, StringRef()) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000072
Alex Lorenz91370c52015-06-22 20:37:46 +000073void MIParser::lex() {
74 CurrentSource = lexMIToken(
75 CurrentSource, Token,
76 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
77}
78
79bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
80
81bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000082 // TODO: Get the proper location in the MIR file, not just a location inside
83 // the string.
Alex Lorenz91370c52015-06-22 20:37:46 +000084 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
85 Error = SMDiagnostic(
86 SM, SMLoc(),
87 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
88 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000089 return true;
90}
91
92MachineInstr *MIParser::parse() {
Alex Lorenz91370c52015-06-22 20:37:46 +000093 lex();
94
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000095 unsigned OpCode;
Alex Lorenz91370c52015-06-22 20:37:46 +000096 if (Token.isError() || parseInstruction(OpCode))
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000097 return nullptr;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000098
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());
102 return MI;
103}
104
Alex Lorenz91370c52015-06-22 20:37:46 +0000105bool 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 + "'");
111 return false;
112}
113
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000114void MIParser::initNames2InstrOpCodes() {
115 if (!Names2InstrOpCodes.empty())
116 return;
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));
121}
122
123bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
124 initNames2InstrOpCodes();
125 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
126 if (InstrInfo == Names2InstrOpCodes.end())
127 return true;
128 OpCode = InstrInfo->getValue();
129 return false;
130}
131
132MachineInstr *llvm::parseMachineInstr(SourceMgr &SM, MachineFunction &MF,
133 StringRef Src, SMDiagnostic &Error) {
134 return MIParser(SM, MF, Error, Src).parse();
135}