blob: 62a3fbe1e5f284db8c13e3fc796c8a8724cb890b [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"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000017#include "llvm/AsmParser/SlotMapping.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000018#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineInstr.h"
Alex Lorenzcb268d42015-07-06 23:07:26 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000022#include "llvm/IR/Module.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000023#include "llvm/Support/raw_ostream.h"
24#include "llvm/Support/SourceMgr.h"
25#include "llvm/Target/TargetSubtargetInfo.h"
26#include "llvm/Target/TargetInstrInfo.h"
27
28using namespace llvm;
29
30namespace {
31
Alex Lorenz36962cd2015-07-07 02:08:46 +000032/// A wrapper struct around the 'MachineOperand' struct that includes a source
33/// range.
34struct MachineOperandWithLocation {
35 MachineOperand Operand;
36 StringRef::iterator Begin;
37 StringRef::iterator End;
38
39 MachineOperandWithLocation(const MachineOperand &Operand,
40 StringRef::iterator Begin, StringRef::iterator End)
41 : Operand(Operand), Begin(Begin), End(End) {}
42};
43
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000044class MIParser {
45 SourceMgr &SM;
46 MachineFunction &MF;
47 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000048 StringRef Source, CurrentSource;
49 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000050 const PerFunctionMIParsingState &PFS;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000051 /// Maps from indices to unnamed global values and metadata nodes.
52 const SlotMapping &IRSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000053 /// Maps from instruction names to op codes.
54 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000055 /// Maps from register names to registers.
56 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000057 /// Maps from register mask names to register masks.
58 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000059 /// Maps from subregister names to subregister indices.
60 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000061
62public:
63 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000064 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000065 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000066
Alex Lorenz91370c52015-06-22 20:37:46 +000067 void lex();
68
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000069 /// Report an error at the current location with the given message.
70 ///
71 /// This function always return true.
72 bool error(const Twine &Msg);
73
Alex Lorenz91370c52015-06-22 20:37:46 +000074 /// Report an error at the given location with the given message.
75 ///
76 /// This function always return true.
77 bool error(StringRef::iterator Loc, const Twine &Msg);
78
Alex Lorenz3708a642015-06-30 17:47:50 +000079 bool parse(MachineInstr *&MI);
Alex Lorenzf09df002015-06-30 18:16:42 +000080 bool parseMBB(MachineBasicBlock *&MBB);
Alex Lorenz9fab3702015-07-14 21:24:41 +000081 bool parseNamedRegister(unsigned &Reg);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000082
Alex Lorenzf3db51de2015-06-23 16:35:26 +000083 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +000084 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +000085 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000086 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +000087 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +000088 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +000089 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +000090 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +000091 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000092 bool parseMachineOperand(MachineOperand &Dest);
93
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000094private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +000095 /// Convert the integer literal in the current token into an unsigned integer.
96 ///
97 /// Return true if an error occurred.
98 bool getUnsigned(unsigned &Result);
99
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000100 void initNames2InstrOpCodes();
101
102 /// Try to convert an instruction name to an opcode. Return true if the
103 /// instruction name is invalid.
104 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000105
106 bool parseInstruction(unsigned &OpCode);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000107
Alex Lorenz36962cd2015-07-07 02:08:46 +0000108 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
109 const MCInstrDesc &MCID);
110
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000111 void initNames2Regs();
112
113 /// Try to convert a register name to a register number. Return true if the
114 /// register name is invalid.
115 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000116
117 void initNames2RegMasks();
118
119 /// Check if the given identifier is a name of a register mask.
120 ///
121 /// Return null if the identifier isn't a register mask.
122 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000123
124 void initNames2SubRegIndices();
125
126 /// Check if the given identifier is a name of a subregister index.
127 ///
128 /// Return 0 if the name isn't a subregister index class.
129 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000130};
131
132} // end anonymous namespace
133
134MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000135 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000136 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000137 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000138 Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000139
Alex Lorenz91370c52015-06-22 20:37:46 +0000140void MIParser::lex() {
141 CurrentSource = lexMIToken(
142 CurrentSource, Token,
143 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
144}
145
146bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
147
148bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000149 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
150 Error = SMDiagnostic(
151 SM, SMLoc(),
152 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
153 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000154 return true;
155}
156
Alex Lorenz3708a642015-06-30 17:47:50 +0000157bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000158 lex();
159
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000160 // Parse any register operands before '='
161 // TODO: Allow parsing of multiple operands before '='
162 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000163 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000164 if (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000165 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000166 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000167 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000168 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenz3708a642015-06-30 17:47:50 +0000169 if (Token.isNot(MIToken::equal))
170 return error("expected '='");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000171 lex();
172 }
173
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000174 unsigned OpCode;
Alex Lorenz91370c52015-06-22 20:37:46 +0000175 if (Token.isError() || parseInstruction(OpCode))
Alex Lorenz3708a642015-06-30 17:47:50 +0000176 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000177
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000178 // TODO: Parse the instruction flags and memory operands.
179
180 // Parse the remaining machine operands.
181 while (Token.isNot(MIToken::Eof)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000182 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000183 if (parseMachineOperand(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000184 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000185 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000186 if (Token.is(MIToken::Eof))
187 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000188 if (Token.isNot(MIToken::comma))
189 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000190 lex();
191 }
192
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000193 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000194 if (!MCID.isVariadic()) {
195 // FIXME: Move the implicit operand verification to the machine verifier.
196 if (verifyImplicitOperands(Operands, MCID))
197 return true;
198 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000199
Alex Lorenzcb268d42015-07-06 23:07:26 +0000200 // TODO: Check for extraneous machine operands.
Alex Lorenz3708a642015-06-30 17:47:50 +0000201 MI = MF.CreateMachineInstr(MCID, DebugLoc(), /*NoImplicit=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000202 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000203 MI->addOperand(MF, Operand.Operand);
Alex Lorenz3708a642015-06-30 17:47:50 +0000204 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000205}
206
Alex Lorenzf09df002015-06-30 18:16:42 +0000207bool MIParser::parseMBB(MachineBasicBlock *&MBB) {
208 lex();
209 if (Token.isNot(MIToken::MachineBasicBlock))
210 return error("expected a machine basic block reference");
211 if (parseMBBReference(MBB))
212 return true;
213 lex();
214 if (Token.isNot(MIToken::Eof))
215 return error(
216 "expected end of string after the machine basic block reference");
217 return false;
218}
219
Alex Lorenz9fab3702015-07-14 21:24:41 +0000220bool MIParser::parseNamedRegister(unsigned &Reg) {
221 lex();
222 if (Token.isNot(MIToken::NamedRegister))
223 return error("expected a named register");
224 if (parseRegister(Reg))
225 return 0;
226 lex();
227 if (Token.isNot(MIToken::Eof))
228 return error("expected end of string after the register reference");
229 return false;
230}
231
Alex Lorenz36962cd2015-07-07 02:08:46 +0000232static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
233 assert(MO.isImplicit());
234 return MO.isDef() ? "implicit-def" : "implicit";
235}
236
237static std::string getRegisterName(const TargetRegisterInfo *TRI,
238 unsigned Reg) {
239 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
240 return StringRef(TRI->getName(Reg)).lower();
241}
242
243bool MIParser::verifyImplicitOperands(
244 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
245 if (MCID.isCall())
246 // We can't verify call instructions as they can contain arbitrary implicit
247 // register and register mask operands.
248 return false;
249
250 // Gather all the expected implicit operands.
251 SmallVector<MachineOperand, 4> ImplicitOperands;
252 if (MCID.ImplicitDefs)
253 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
254 ImplicitOperands.push_back(
255 MachineOperand::CreateReg(*ImpDefs, true, true));
256 if (MCID.ImplicitUses)
257 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
258 ImplicitOperands.push_back(
259 MachineOperand::CreateReg(*ImpUses, false, true));
260
261 const auto *TRI = MF.getSubtarget().getRegisterInfo();
262 assert(TRI && "Expected target register info");
263 size_t I = ImplicitOperands.size(), J = Operands.size();
264 while (I) {
265 --I;
266 if (J) {
267 --J;
268 const auto &ImplicitOperand = ImplicitOperands[I];
269 const auto &Operand = Operands[J].Operand;
270 if (ImplicitOperand.isIdenticalTo(Operand))
271 continue;
272 if (Operand.isReg() && Operand.isImplicit()) {
273 return error(Operands[J].Begin,
274 Twine("expected an implicit register operand '") +
275 printImplicitRegisterFlag(ImplicitOperand) + " %" +
276 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
277 }
278 }
279 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
280 // insead of reporting an error at this location:
281 // %eax = MOV32r0
282 // ^
283 // report the error at the following location:
284 // %eax = MOV32r0
285 // ^
286 return error(J < Operands.size() ? Operands[J].End : Token.location(),
287 Twine("missing implicit register operand '") +
288 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
289 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
290 }
291 return false;
292}
293
Alex Lorenz91370c52015-06-22 20:37:46 +0000294bool MIParser::parseInstruction(unsigned &OpCode) {
295 if (Token.isNot(MIToken::Identifier))
296 return error("expected a machine instruction");
297 StringRef InstrName = Token.stringValue();
298 if (parseInstrName(InstrName, OpCode))
299 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000300 lex();
301 return false;
302}
303
304bool MIParser::parseRegister(unsigned &Reg) {
305 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000306 case MIToken::underscore:
307 Reg = 0;
308 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000309 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000310 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000311 if (getRegisterByName(Name, Reg))
312 return error(Twine("unknown register name '") + Name + "'");
313 break;
314 }
Alex Lorenz53464512015-07-10 22:51:20 +0000315 case MIToken::VirtualRegister: {
316 unsigned ID;
317 if (getUnsigned(ID))
318 return true;
319 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
320 if (RegInfo == PFS.VirtualRegisterSlots.end())
321 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
322 "'");
323 Reg = RegInfo->second;
324 break;
325 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000326 // TODO: Parse other register kinds.
327 default:
328 llvm_unreachable("The current token should be a register");
329 }
330 return false;
331}
332
Alex Lorenzcb268d42015-07-06 23:07:26 +0000333bool MIParser::parseRegisterFlag(unsigned &Flags) {
334 switch (Token.kind()) {
335 case MIToken::kw_implicit:
336 Flags |= RegState::Implicit;
337 break;
338 case MIToken::kw_implicit_define:
339 Flags |= RegState::ImplicitDefine;
340 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000341 case MIToken::kw_dead:
342 Flags |= RegState::Dead;
343 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000344 case MIToken::kw_killed:
345 Flags |= RegState::Kill;
346 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000347 case MIToken::kw_undef:
348 Flags |= RegState::Undef;
349 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000350 // TODO: report an error when we specify the same flag more than once.
351 // TODO: parse the other register flags.
352 default:
353 llvm_unreachable("The current token should be a register flag");
354 }
355 lex();
356 return false;
357}
358
Alex Lorenz2eacca82015-07-13 23:24:34 +0000359bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
360 assert(Token.is(MIToken::colon));
361 lex();
362 if (Token.isNot(MIToken::Identifier))
363 return error("expected a subregister index after ':'");
364 auto Name = Token.stringValue();
365 SubReg = getSubRegIndex(Name);
366 if (!SubReg)
367 return error(Twine("use of unknown subregister index '") + Name + "'");
368 lex();
369 return false;
370}
371
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000372bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
373 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000374 unsigned Flags = IsDef ? RegState::Define : 0;
375 while (Token.isRegisterFlag()) {
376 if (parseRegisterFlag(Flags))
377 return true;
378 }
379 if (!Token.isRegister())
380 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000381 if (parseRegister(Reg))
382 return true;
383 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000384 unsigned SubReg = 0;
385 if (Token.is(MIToken::colon)) {
386 if (parseSubRegisterIndex(SubReg))
387 return true;
388 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000389 Dest = MachineOperand::CreateReg(
390 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000391 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
392 /*isEarlyClobber=*/false, SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000393 return false;
394}
395
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000396bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
397 assert(Token.is(MIToken::IntegerLiteral));
398 const APSInt &Int = Token.integerValue();
399 if (Int.getMinSignedBits() > 64)
400 // TODO: Replace this with an error when we can parse CIMM Machine Operands.
401 llvm_unreachable("Can't parse large integer literals yet!");
402 Dest = MachineOperand::CreateImm(Int.getExtValue());
403 lex();
404 return false;
405}
406
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000407bool MIParser::getUnsigned(unsigned &Result) {
408 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
409 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
410 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
411 if (Val64 == Limit)
412 return error("expected 32-bit integer (too large)");
413 Result = Val64;
414 return false;
415}
416
Alex Lorenzf09df002015-06-30 18:16:42 +0000417bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000418 assert(Token.is(MIToken::MachineBasicBlock));
419 unsigned Number;
420 if (getUnsigned(Number))
421 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000422 auto MBBInfo = PFS.MBBSlots.find(Number);
423 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000424 return error(Twine("use of undefined machine basic block #") +
425 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000426 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000427 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
428 return error(Twine("the name of machine basic block #") + Twine(Number) +
429 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000430 return false;
431}
432
433bool MIParser::parseMBBOperand(MachineOperand &Dest) {
434 MachineBasicBlock *MBB;
435 if (parseMBBReference(MBB))
436 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000437 Dest = MachineOperand::CreateMBB(MBB);
438 lex();
439 return false;
440}
441
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000442bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
443 switch (Token.kind()) {
444 case MIToken::NamedGlobalValue: {
445 auto Name = Token.stringValue();
446 const Module *M = MF.getFunction()->getParent();
447 if (const auto *GV = M->getNamedValue(Name)) {
448 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
449 break;
450 }
451 return error(Twine("use of undefined global value '@") + Name + "'");
452 }
453 case MIToken::GlobalValue: {
454 unsigned GVIdx;
455 if (getUnsigned(GVIdx))
456 return true;
457 if (GVIdx >= IRSlots.GlobalValues.size())
458 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
459 "'");
460 Dest = MachineOperand::CreateGA(IRSlots.GlobalValues[GVIdx],
461 /*Offset=*/0);
462 break;
463 }
464 default:
465 llvm_unreachable("The current token should be a global value");
466 }
467 // TODO: Parse offset and target flags.
468 lex();
469 return false;
470}
471
Alex Lorenz31d70682015-07-15 23:38:35 +0000472bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
473 assert(Token.is(MIToken::JumpTableIndex));
474 unsigned ID;
475 if (getUnsigned(ID))
476 return true;
477 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
478 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
479 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
480 lex();
481 // TODO: Parse target flags.
482 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
483 return false;
484}
485
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000486bool MIParser::parseMachineOperand(MachineOperand &Dest) {
487 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000488 case MIToken::kw_implicit:
489 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000490 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000491 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000492 case MIToken::kw_undef:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000493 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000494 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000495 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000496 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000497 case MIToken::IntegerLiteral:
498 return parseImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000499 case MIToken::MachineBasicBlock:
500 return parseMBBOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000501 case MIToken::GlobalValue:
502 case MIToken::NamedGlobalValue:
503 return parseGlobalAddressOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000504 case MIToken::JumpTableIndex:
505 return parseJumpTableIndexOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000506 case MIToken::Error:
507 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000508 case MIToken::Identifier:
509 if (const auto *RegMask = getRegMask(Token.stringValue())) {
510 Dest = MachineOperand::CreateRegMask(RegMask);
511 lex();
512 break;
513 }
514 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000515 default:
516 // TODO: parse the other machine operands.
517 return error("expected a machine operand");
518 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000519 return false;
520}
521
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000522void MIParser::initNames2InstrOpCodes() {
523 if (!Names2InstrOpCodes.empty())
524 return;
525 const auto *TII = MF.getSubtarget().getInstrInfo();
526 assert(TII && "Expected target instruction info");
527 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
528 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
529}
530
531bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
532 initNames2InstrOpCodes();
533 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
534 if (InstrInfo == Names2InstrOpCodes.end())
535 return true;
536 OpCode = InstrInfo->getValue();
537 return false;
538}
539
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000540void MIParser::initNames2Regs() {
541 if (!Names2Regs.empty())
542 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +0000543 // The '%noreg' register is the register 0.
544 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000545 const auto *TRI = MF.getSubtarget().getRegisterInfo();
546 assert(TRI && "Expected target register info");
547 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
548 bool WasInserted =
549 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
550 .second;
551 (void)WasInserted;
552 assert(WasInserted && "Expected registers to be unique case-insensitively");
553 }
554}
555
556bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
557 initNames2Regs();
558 auto RegInfo = Names2Regs.find(RegName);
559 if (RegInfo == Names2Regs.end())
560 return true;
561 Reg = RegInfo->getValue();
562 return false;
563}
564
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000565void MIParser::initNames2RegMasks() {
566 if (!Names2RegMasks.empty())
567 return;
568 const auto *TRI = MF.getSubtarget().getRegisterInfo();
569 assert(TRI && "Expected target register info");
570 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
571 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
572 assert(RegMasks.size() == RegMaskNames.size());
573 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
574 Names2RegMasks.insert(
575 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
576}
577
578const uint32_t *MIParser::getRegMask(StringRef Identifier) {
579 initNames2RegMasks();
580 auto RegMaskInfo = Names2RegMasks.find(Identifier);
581 if (RegMaskInfo == Names2RegMasks.end())
582 return nullptr;
583 return RegMaskInfo->getValue();
584}
585
Alex Lorenz2eacca82015-07-13 23:24:34 +0000586void MIParser::initNames2SubRegIndices() {
587 if (!Names2SubRegIndices.empty())
588 return;
589 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
590 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
591 Names2SubRegIndices.insert(
592 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
593}
594
595unsigned MIParser::getSubRegIndex(StringRef Name) {
596 initNames2SubRegIndices();
597 auto SubRegInfo = Names2SubRegIndices.find(Name);
598 if (SubRegInfo == Names2SubRegIndices.end())
599 return 0;
600 return SubRegInfo->getValue();
601}
602
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000603bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
604 MachineFunction &MF, StringRef Src,
605 const PerFunctionMIParsingState &PFS,
606 const SlotMapping &IRSlots, SMDiagnostic &Error) {
607 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000608}
Alex Lorenzf09df002015-06-30 18:16:42 +0000609
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000610bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
611 MachineFunction &MF, StringRef Src,
612 const PerFunctionMIParsingState &PFS,
613 const SlotMapping &IRSlots, SMDiagnostic &Error) {
614 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000615}
Alex Lorenz9fab3702015-07-14 21:24:41 +0000616
617bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
618 MachineFunction &MF, StringRef Src,
619 const PerFunctionMIParsingState &PFS,
620 const SlotMapping &IRSlots,
621 SMDiagnostic &Error) {
622 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseNamedRegister(Reg);
623}