blob: 61a7dfc68b5e6c353c4275fe2ee9426a43407aef [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 Lorenz33f0aef2015-06-26 16:46:11 +000035 /// Maps from basic block numbers to MBBs.
36 const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000037 /// Maps from instruction names to op codes.
38 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000039 /// Maps from register names to registers.
40 StringMap<unsigned> Names2Regs;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000041
42public:
43 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz33f0aef2015-06-26 16:46:11 +000044 StringRef Source,
45 const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000046
Alex Lorenz91370c52015-06-22 20:37:46 +000047 void lex();
48
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000049 /// Report an error at the current location with the given message.
50 ///
51 /// This function always return true.
52 bool error(const Twine &Msg);
53
Alex Lorenz91370c52015-06-22 20:37:46 +000054 /// Report an error at the given location with the given message.
55 ///
56 /// This function always return true.
57 bool error(StringRef::iterator Loc, const Twine &Msg);
58
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000059 MachineInstr *parse();
60
Alex Lorenzf3db51de2015-06-23 16:35:26 +000061 bool parseRegister(unsigned &Reg);
62 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +000063 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +000064 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000065 bool parseMachineOperand(MachineOperand &Dest);
66
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000067private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +000068 /// Convert the integer literal in the current token into an unsigned integer.
69 ///
70 /// Return true if an error occurred.
71 bool getUnsigned(unsigned &Result);
72
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000073 void initNames2InstrOpCodes();
74
75 /// Try to convert an instruction name to an opcode. Return true if the
76 /// instruction name is invalid.
77 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +000078
79 bool parseInstruction(unsigned &OpCode);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000080
81 void initNames2Regs();
82
83 /// Try to convert a register name to a register number. Return true if the
84 /// register name is invalid.
85 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000086};
87
88} // end anonymous namespace
89
90MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz33f0aef2015-06-26 16:46:11 +000091 StringRef Source,
92 const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +000093 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz33f0aef2015-06-26 16:46:11 +000094 Token(MIToken::Error, StringRef()), MBBSlots(MBBSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000095
Alex Lorenz91370c52015-06-22 20:37:46 +000096void MIParser::lex() {
97 CurrentSource = lexMIToken(
98 CurrentSource, Token,
99 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
100}
101
102bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
103
104bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000105 // TODO: Get the proper location in the MIR file, not just a location inside
106 // the string.
Alex Lorenz91370c52015-06-22 20:37:46 +0000107 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
108 Error = SMDiagnostic(
109 SM, SMLoc(),
110 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
111 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000112 return true;
113}
114
115MachineInstr *MIParser::parse() {
Alex Lorenz91370c52015-06-22 20:37:46 +0000116 lex();
117
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000118 // Parse any register operands before '='
119 // TODO: Allow parsing of multiple operands before '='
120 MachineOperand MO = MachineOperand::CreateImm(0);
121 SmallVector<MachineOperand, 8> Operands;
122 if (Token.isRegister()) {
123 if (parseRegisterOperand(MO, /*IsDef=*/true))
124 return nullptr;
125 Operands.push_back(MO);
126 if (Token.isNot(MIToken::equal)) {
127 error("expected '='");
128 return nullptr;
129 }
130 lex();
131 }
132
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000133 unsigned OpCode;
Alex Lorenz91370c52015-06-22 20:37:46 +0000134 if (Token.isError() || parseInstruction(OpCode))
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000135 return nullptr;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000136
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000137 // TODO: Parse the instruction flags and memory operands.
138
139 // Parse the remaining machine operands.
140 while (Token.isNot(MIToken::Eof)) {
141 if (parseMachineOperand(MO))
142 return nullptr;
143 Operands.push_back(MO);
144 if (Token.is(MIToken::Eof))
145 break;
146 if (Token.isNot(MIToken::comma)) {
147 error("expected ',' before the next machine operand");
148 return nullptr;
149 }
150 lex();
151 }
152
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000153 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000154
155 // Verify machine operands.
156 if (!MCID.isVariadic()) {
157 for (size_t I = 0, E = Operands.size(); I < E; ++I) {
158 if (I < MCID.getNumOperands())
159 continue;
160 // Mark this register as implicit to prevent an assertion when it's added
161 // to an instruction. This is a temporary workaround until the implicit
162 // register flag can be parsed.
163 Operands[I].setImplicit();
164 }
165 }
166
167 // TODO: Determine the implicit behaviour when implicit register flags are
168 // parsed.
169 auto *MI = MF.CreateMachineInstr(MCID, DebugLoc(), /*NoImplicit=*/true);
170 for (const auto &Operand : Operands)
171 MI->addOperand(MF, Operand);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000172 return MI;
173}
174
Alex Lorenz91370c52015-06-22 20:37:46 +0000175bool MIParser::parseInstruction(unsigned &OpCode) {
176 if (Token.isNot(MIToken::Identifier))
177 return error("expected a machine instruction");
178 StringRef InstrName = Token.stringValue();
179 if (parseInstrName(InstrName, OpCode))
180 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000181 lex();
182 return false;
183}
184
185bool MIParser::parseRegister(unsigned &Reg) {
186 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000187 case MIToken::underscore:
188 Reg = 0;
189 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000190 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000191 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000192 if (getRegisterByName(Name, Reg))
193 return error(Twine("unknown register name '") + Name + "'");
194 break;
195 }
196 // TODO: Parse other register kinds.
197 default:
198 llvm_unreachable("The current token should be a register");
199 }
200 return false;
201}
202
203bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
204 unsigned Reg;
205 // TODO: Parse register flags.
206 if (parseRegister(Reg))
207 return true;
208 lex();
209 // TODO: Parse subregister.
210 Dest = MachineOperand::CreateReg(Reg, IsDef);
211 return false;
212}
213
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000214bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
215 assert(Token.is(MIToken::IntegerLiteral));
216 const APSInt &Int = Token.integerValue();
217 if (Int.getMinSignedBits() > 64)
218 // TODO: Replace this with an error when we can parse CIMM Machine Operands.
219 llvm_unreachable("Can't parse large integer literals yet!");
220 Dest = MachineOperand::CreateImm(Int.getExtValue());
221 lex();
222 return false;
223}
224
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000225bool MIParser::getUnsigned(unsigned &Result) {
226 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
227 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
228 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
229 if (Val64 == Limit)
230 return error("expected 32-bit integer (too large)");
231 Result = Val64;
232 return false;
233}
234
235bool MIParser::parseMBBOperand(MachineOperand &Dest) {
236 assert(Token.is(MIToken::MachineBasicBlock));
237 unsigned Number;
238 if (getUnsigned(Number))
239 return true;
240 auto MBBInfo = MBBSlots.find(Number);
241 if (MBBInfo == MBBSlots.end())
242 return error(Twine("use of undefined machine basic block #") +
243 Twine(Number));
244 MachineBasicBlock *MBB = MBBInfo->second;
245 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
246 return error(Twine("the name of machine basic block #") + Twine(Number) +
247 " isn't '" + Token.stringValue() + "'");
248 Dest = MachineOperand::CreateMBB(MBB);
249 lex();
250 return false;
251}
252
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000253bool MIParser::parseMachineOperand(MachineOperand &Dest) {
254 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000255 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000256 case MIToken::NamedRegister:
257 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000258 case MIToken::IntegerLiteral:
259 return parseImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000260 case MIToken::MachineBasicBlock:
261 return parseMBBOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000262 case MIToken::Error:
263 return true;
264 default:
265 // TODO: parse the other machine operands.
266 return error("expected a machine operand");
267 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000268 return false;
269}
270
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000271void MIParser::initNames2InstrOpCodes() {
272 if (!Names2InstrOpCodes.empty())
273 return;
274 const auto *TII = MF.getSubtarget().getInstrInfo();
275 assert(TII && "Expected target instruction info");
276 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
277 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
278}
279
280bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
281 initNames2InstrOpCodes();
282 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
283 if (InstrInfo == Names2InstrOpCodes.end())
284 return true;
285 OpCode = InstrInfo->getValue();
286 return false;
287}
288
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000289void MIParser::initNames2Regs() {
290 if (!Names2Regs.empty())
291 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +0000292 // The '%noreg' register is the register 0.
293 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000294 const auto *TRI = MF.getSubtarget().getRegisterInfo();
295 assert(TRI && "Expected target register info");
296 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
297 bool WasInserted =
298 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
299 .second;
300 (void)WasInserted;
301 assert(WasInserted && "Expected registers to be unique case-insensitively");
302 }
303}
304
305bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
306 initNames2Regs();
307 auto RegInfo = Names2Regs.find(RegName);
308 if (RegInfo == Names2Regs.end())
309 return true;
310 Reg = RegInfo->getValue();
311 return false;
312}
313
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000314MachineInstr *
315llvm::parseMachineInstr(SourceMgr &SM, MachineFunction &MF, StringRef Src,
316 const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots,
317 SMDiagnostic &Error) {
318 return MIParser(SM, MF, Error, Src, MBBSlots).parse();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000319}