blob: b2931189578370be35c4483846923663f9f181c2 [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"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000021#include "llvm/CodeGen/MachineInstr.h"
Alex Lorenzcb268d42015-07-06 23:07:26 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000023#include "llvm/IR/Instructions.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000024#include "llvm/IR/Module.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000025#include "llvm/Support/raw_ostream.h"
26#include "llvm/Support/SourceMgr.h"
27#include "llvm/Target/TargetSubtargetInfo.h"
28#include "llvm/Target/TargetInstrInfo.h"
29
30using namespace llvm;
31
32namespace {
33
Alex Lorenz36962cd2015-07-07 02:08:46 +000034/// A wrapper struct around the 'MachineOperand' struct that includes a source
35/// range.
36struct MachineOperandWithLocation {
37 MachineOperand Operand;
38 StringRef::iterator Begin;
39 StringRef::iterator End;
40
41 MachineOperandWithLocation(const MachineOperand &Operand,
42 StringRef::iterator Begin, StringRef::iterator End)
43 : Operand(Operand), Begin(Begin), End(End) {}
44};
45
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000046class MIParser {
47 SourceMgr &SM;
48 MachineFunction &MF;
49 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000050 StringRef Source, CurrentSource;
51 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000052 const PerFunctionMIParsingState &PFS;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000053 /// Maps from indices to unnamed global values and metadata nodes.
54 const SlotMapping &IRSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000055 /// Maps from instruction names to op codes.
56 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000057 /// Maps from register names to registers.
58 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000059 /// Maps from register mask names to register masks.
60 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000061 /// Maps from subregister names to subregister indices.
62 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000063
64public:
65 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000066 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000067 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000068
Alex Lorenz91370c52015-06-22 20:37:46 +000069 void lex();
70
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000071 /// Report an error at the current location with the given message.
72 ///
73 /// This function always return true.
74 bool error(const Twine &Msg);
75
Alex Lorenz91370c52015-06-22 20:37:46 +000076 /// Report an error at the given location with the given message.
77 ///
78 /// This function always return true.
79 bool error(StringRef::iterator Loc, const Twine &Msg);
80
Alex Lorenz3708a642015-06-30 17:47:50 +000081 bool parse(MachineInstr *&MI);
Alex Lorenzf09df002015-06-30 18:16:42 +000082 bool parseMBB(MachineBasicBlock *&MBB);
Alex Lorenz9fab3702015-07-14 21:24:41 +000083 bool parseNamedRegister(unsigned &Reg);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000084
Alex Lorenzf3db51de2015-06-23 16:35:26 +000085 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +000086 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +000087 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000088 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +000089 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +000090 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +000091 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000092 bool parseStackObjectOperand(MachineOperand &Dest);
93 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +000094 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +000095 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000096 bool parseMachineOperand(MachineOperand &Dest);
97
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000098private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +000099 /// Convert the integer literal in the current token into an unsigned integer.
100 ///
101 /// Return true if an error occurred.
102 bool getUnsigned(unsigned &Result);
103
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000104 void initNames2InstrOpCodes();
105
106 /// Try to convert an instruction name to an opcode. Return true if the
107 /// instruction name is invalid.
108 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000109
110 bool parseInstruction(unsigned &OpCode);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000111
Alex Lorenz36962cd2015-07-07 02:08:46 +0000112 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
113 const MCInstrDesc &MCID);
114
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000115 void initNames2Regs();
116
117 /// Try to convert a register name to a register number. Return true if the
118 /// register name is invalid.
119 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000120
121 void initNames2RegMasks();
122
123 /// Check if the given identifier is a name of a register mask.
124 ///
125 /// Return null if the identifier isn't a register mask.
126 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000127
128 void initNames2SubRegIndices();
129
130 /// Check if the given identifier is a name of a subregister index.
131 ///
132 /// Return 0 if the name isn't a subregister index class.
133 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000134};
135
136} // end anonymous namespace
137
138MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000139 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000140 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000141 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000142 Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000143
Alex Lorenz91370c52015-06-22 20:37:46 +0000144void MIParser::lex() {
145 CurrentSource = lexMIToken(
146 CurrentSource, Token,
147 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
148}
149
150bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
151
152bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000153 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
154 Error = SMDiagnostic(
155 SM, SMLoc(),
156 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
157 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000158 return true;
159}
160
Alex Lorenz3708a642015-06-30 17:47:50 +0000161bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000162 lex();
163
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000164 // Parse any register operands before '='
165 // TODO: Allow parsing of multiple operands before '='
166 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000167 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000168 if (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000169 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000170 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000171 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000172 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenz3708a642015-06-30 17:47:50 +0000173 if (Token.isNot(MIToken::equal))
174 return error("expected '='");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000175 lex();
176 }
177
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000178 unsigned OpCode;
Alex Lorenz91370c52015-06-22 20:37:46 +0000179 if (Token.isError() || parseInstruction(OpCode))
Alex Lorenz3708a642015-06-30 17:47:50 +0000180 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000181
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000182 // TODO: Parse the instruction flags and memory operands.
183
184 // Parse the remaining machine operands.
185 while (Token.isNot(MIToken::Eof)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000186 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000187 if (parseMachineOperand(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000188 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000189 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000190 if (Token.is(MIToken::Eof))
191 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000192 if (Token.isNot(MIToken::comma))
193 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000194 lex();
195 }
196
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000197 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000198 if (!MCID.isVariadic()) {
199 // FIXME: Move the implicit operand verification to the machine verifier.
200 if (verifyImplicitOperands(Operands, MCID))
201 return true;
202 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000203
Alex Lorenzcb268d42015-07-06 23:07:26 +0000204 // TODO: Check for extraneous machine operands.
Alex Lorenz3708a642015-06-30 17:47:50 +0000205 MI = MF.CreateMachineInstr(MCID, DebugLoc(), /*NoImplicit=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000206 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000207 MI->addOperand(MF, Operand.Operand);
Alex Lorenz3708a642015-06-30 17:47:50 +0000208 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000209}
210
Alex Lorenzf09df002015-06-30 18:16:42 +0000211bool MIParser::parseMBB(MachineBasicBlock *&MBB) {
212 lex();
213 if (Token.isNot(MIToken::MachineBasicBlock))
214 return error("expected a machine basic block reference");
215 if (parseMBBReference(MBB))
216 return true;
217 lex();
218 if (Token.isNot(MIToken::Eof))
219 return error(
220 "expected end of string after the machine basic block reference");
221 return false;
222}
223
Alex Lorenz9fab3702015-07-14 21:24:41 +0000224bool MIParser::parseNamedRegister(unsigned &Reg) {
225 lex();
226 if (Token.isNot(MIToken::NamedRegister))
227 return error("expected a named register");
228 if (parseRegister(Reg))
229 return 0;
230 lex();
231 if (Token.isNot(MIToken::Eof))
232 return error("expected end of string after the register reference");
233 return false;
234}
235
Alex Lorenz36962cd2015-07-07 02:08:46 +0000236static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
237 assert(MO.isImplicit());
238 return MO.isDef() ? "implicit-def" : "implicit";
239}
240
241static std::string getRegisterName(const TargetRegisterInfo *TRI,
242 unsigned Reg) {
243 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
244 return StringRef(TRI->getName(Reg)).lower();
245}
246
247bool MIParser::verifyImplicitOperands(
248 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
249 if (MCID.isCall())
250 // We can't verify call instructions as they can contain arbitrary implicit
251 // register and register mask operands.
252 return false;
253
254 // Gather all the expected implicit operands.
255 SmallVector<MachineOperand, 4> ImplicitOperands;
256 if (MCID.ImplicitDefs)
257 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
258 ImplicitOperands.push_back(
259 MachineOperand::CreateReg(*ImpDefs, true, true));
260 if (MCID.ImplicitUses)
261 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
262 ImplicitOperands.push_back(
263 MachineOperand::CreateReg(*ImpUses, false, true));
264
265 const auto *TRI = MF.getSubtarget().getRegisterInfo();
266 assert(TRI && "Expected target register info");
267 size_t I = ImplicitOperands.size(), J = Operands.size();
268 while (I) {
269 --I;
270 if (J) {
271 --J;
272 const auto &ImplicitOperand = ImplicitOperands[I];
273 const auto &Operand = Operands[J].Operand;
274 if (ImplicitOperand.isIdenticalTo(Operand))
275 continue;
276 if (Operand.isReg() && Operand.isImplicit()) {
277 return error(Operands[J].Begin,
278 Twine("expected an implicit register operand '") +
279 printImplicitRegisterFlag(ImplicitOperand) + " %" +
280 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
281 }
282 }
283 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
284 // insead of reporting an error at this location:
285 // %eax = MOV32r0
286 // ^
287 // report the error at the following location:
288 // %eax = MOV32r0
289 // ^
290 return error(J < Operands.size() ? Operands[J].End : Token.location(),
291 Twine("missing implicit register operand '") +
292 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
293 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
294 }
295 return false;
296}
297
Alex Lorenz91370c52015-06-22 20:37:46 +0000298bool MIParser::parseInstruction(unsigned &OpCode) {
299 if (Token.isNot(MIToken::Identifier))
300 return error("expected a machine instruction");
301 StringRef InstrName = Token.stringValue();
302 if (parseInstrName(InstrName, OpCode))
303 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000304 lex();
305 return false;
306}
307
308bool MIParser::parseRegister(unsigned &Reg) {
309 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000310 case MIToken::underscore:
311 Reg = 0;
312 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000313 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000314 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000315 if (getRegisterByName(Name, Reg))
316 return error(Twine("unknown register name '") + Name + "'");
317 break;
318 }
Alex Lorenz53464512015-07-10 22:51:20 +0000319 case MIToken::VirtualRegister: {
320 unsigned ID;
321 if (getUnsigned(ID))
322 return true;
323 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
324 if (RegInfo == PFS.VirtualRegisterSlots.end())
325 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
326 "'");
327 Reg = RegInfo->second;
328 break;
329 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000330 // TODO: Parse other register kinds.
331 default:
332 llvm_unreachable("The current token should be a register");
333 }
334 return false;
335}
336
Alex Lorenzcb268d42015-07-06 23:07:26 +0000337bool MIParser::parseRegisterFlag(unsigned &Flags) {
338 switch (Token.kind()) {
339 case MIToken::kw_implicit:
340 Flags |= RegState::Implicit;
341 break;
342 case MIToken::kw_implicit_define:
343 Flags |= RegState::ImplicitDefine;
344 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000345 case MIToken::kw_dead:
346 Flags |= RegState::Dead;
347 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000348 case MIToken::kw_killed:
349 Flags |= RegState::Kill;
350 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000351 case MIToken::kw_undef:
352 Flags |= RegState::Undef;
353 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000354 // TODO: report an error when we specify the same flag more than once.
355 // TODO: parse the other register flags.
356 default:
357 llvm_unreachable("The current token should be a register flag");
358 }
359 lex();
360 return false;
361}
362
Alex Lorenz2eacca82015-07-13 23:24:34 +0000363bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
364 assert(Token.is(MIToken::colon));
365 lex();
366 if (Token.isNot(MIToken::Identifier))
367 return error("expected a subregister index after ':'");
368 auto Name = Token.stringValue();
369 SubReg = getSubRegIndex(Name);
370 if (!SubReg)
371 return error(Twine("use of unknown subregister index '") + Name + "'");
372 lex();
373 return false;
374}
375
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000376bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
377 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000378 unsigned Flags = IsDef ? RegState::Define : 0;
379 while (Token.isRegisterFlag()) {
380 if (parseRegisterFlag(Flags))
381 return true;
382 }
383 if (!Token.isRegister())
384 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000385 if (parseRegister(Reg))
386 return true;
387 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000388 unsigned SubReg = 0;
389 if (Token.is(MIToken::colon)) {
390 if (parseSubRegisterIndex(SubReg))
391 return true;
392 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000393 Dest = MachineOperand::CreateReg(
394 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000395 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
396 /*isEarlyClobber=*/false, SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000397 return false;
398}
399
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000400bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
401 assert(Token.is(MIToken::IntegerLiteral));
402 const APSInt &Int = Token.integerValue();
403 if (Int.getMinSignedBits() > 64)
404 // TODO: Replace this with an error when we can parse CIMM Machine Operands.
405 llvm_unreachable("Can't parse large integer literals yet!");
406 Dest = MachineOperand::CreateImm(Int.getExtValue());
407 lex();
408 return false;
409}
410
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000411bool MIParser::getUnsigned(unsigned &Result) {
412 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
413 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
414 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
415 if (Val64 == Limit)
416 return error("expected 32-bit integer (too large)");
417 Result = Val64;
418 return false;
419}
420
Alex Lorenzf09df002015-06-30 18:16:42 +0000421bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000422 assert(Token.is(MIToken::MachineBasicBlock));
423 unsigned Number;
424 if (getUnsigned(Number))
425 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000426 auto MBBInfo = PFS.MBBSlots.find(Number);
427 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000428 return error(Twine("use of undefined machine basic block #") +
429 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000430 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000431 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
432 return error(Twine("the name of machine basic block #") + Twine(Number) +
433 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000434 return false;
435}
436
437bool MIParser::parseMBBOperand(MachineOperand &Dest) {
438 MachineBasicBlock *MBB;
439 if (parseMBBReference(MBB))
440 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000441 Dest = MachineOperand::CreateMBB(MBB);
442 lex();
443 return false;
444}
445
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000446bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
447 assert(Token.is(MIToken::StackObject));
448 unsigned ID;
449 if (getUnsigned(ID))
450 return true;
451 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
452 if (ObjectInfo == PFS.StackObjectSlots.end())
453 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
454 "'");
455 StringRef Name;
456 if (const auto *Alloca =
457 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
458 Name = Alloca->getName();
459 if (!Token.stringValue().empty() && Token.stringValue() != Name)
460 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
461 "' isn't '" + Token.stringValue() + "'");
462 lex();
463 Dest = MachineOperand::CreateFI(ObjectInfo->second);
464 return false;
465}
466
467bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
468 assert(Token.is(MIToken::FixedStackObject));
469 unsigned ID;
470 if (getUnsigned(ID))
471 return true;
472 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
473 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
474 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
475 Twine(ID) + "'");
476 lex();
477 Dest = MachineOperand::CreateFI(ObjectInfo->second);
478 return false;
479}
480
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000481bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
482 switch (Token.kind()) {
483 case MIToken::NamedGlobalValue: {
484 auto Name = Token.stringValue();
485 const Module *M = MF.getFunction()->getParent();
486 if (const auto *GV = M->getNamedValue(Name)) {
487 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
488 break;
489 }
490 return error(Twine("use of undefined global value '@") + Name + "'");
491 }
492 case MIToken::GlobalValue: {
493 unsigned GVIdx;
494 if (getUnsigned(GVIdx))
495 return true;
496 if (GVIdx >= IRSlots.GlobalValues.size())
497 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
498 "'");
499 Dest = MachineOperand::CreateGA(IRSlots.GlobalValues[GVIdx],
500 /*Offset=*/0);
501 break;
502 }
503 default:
504 llvm_unreachable("The current token should be a global value");
505 }
506 // TODO: Parse offset and target flags.
507 lex();
508 return false;
509}
510
Alex Lorenz31d70682015-07-15 23:38:35 +0000511bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
512 assert(Token.is(MIToken::JumpTableIndex));
513 unsigned ID;
514 if (getUnsigned(ID))
515 return true;
516 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
517 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
518 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
519 lex();
520 // TODO: Parse target flags.
521 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
522 return false;
523}
524
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000525bool MIParser::parseMachineOperand(MachineOperand &Dest) {
526 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000527 case MIToken::kw_implicit:
528 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000529 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000530 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000531 case MIToken::kw_undef:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000532 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000533 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000534 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000535 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000536 case MIToken::IntegerLiteral:
537 return parseImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000538 case MIToken::MachineBasicBlock:
539 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000540 case MIToken::StackObject:
541 return parseStackObjectOperand(Dest);
542 case MIToken::FixedStackObject:
543 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000544 case MIToken::GlobalValue:
545 case MIToken::NamedGlobalValue:
546 return parseGlobalAddressOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000547 case MIToken::JumpTableIndex:
548 return parseJumpTableIndexOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000549 case MIToken::Error:
550 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000551 case MIToken::Identifier:
552 if (const auto *RegMask = getRegMask(Token.stringValue())) {
553 Dest = MachineOperand::CreateRegMask(RegMask);
554 lex();
555 break;
556 }
557 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000558 default:
559 // TODO: parse the other machine operands.
560 return error("expected a machine operand");
561 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000562 return false;
563}
564
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000565void MIParser::initNames2InstrOpCodes() {
566 if (!Names2InstrOpCodes.empty())
567 return;
568 const auto *TII = MF.getSubtarget().getInstrInfo();
569 assert(TII && "Expected target instruction info");
570 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
571 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
572}
573
574bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
575 initNames2InstrOpCodes();
576 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
577 if (InstrInfo == Names2InstrOpCodes.end())
578 return true;
579 OpCode = InstrInfo->getValue();
580 return false;
581}
582
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000583void MIParser::initNames2Regs() {
584 if (!Names2Regs.empty())
585 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +0000586 // The '%noreg' register is the register 0.
587 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000588 const auto *TRI = MF.getSubtarget().getRegisterInfo();
589 assert(TRI && "Expected target register info");
590 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
591 bool WasInserted =
592 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
593 .second;
594 (void)WasInserted;
595 assert(WasInserted && "Expected registers to be unique case-insensitively");
596 }
597}
598
599bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
600 initNames2Regs();
601 auto RegInfo = Names2Regs.find(RegName);
602 if (RegInfo == Names2Regs.end())
603 return true;
604 Reg = RegInfo->getValue();
605 return false;
606}
607
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000608void MIParser::initNames2RegMasks() {
609 if (!Names2RegMasks.empty())
610 return;
611 const auto *TRI = MF.getSubtarget().getRegisterInfo();
612 assert(TRI && "Expected target register info");
613 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
614 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
615 assert(RegMasks.size() == RegMaskNames.size());
616 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
617 Names2RegMasks.insert(
618 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
619}
620
621const uint32_t *MIParser::getRegMask(StringRef Identifier) {
622 initNames2RegMasks();
623 auto RegMaskInfo = Names2RegMasks.find(Identifier);
624 if (RegMaskInfo == Names2RegMasks.end())
625 return nullptr;
626 return RegMaskInfo->getValue();
627}
628
Alex Lorenz2eacca82015-07-13 23:24:34 +0000629void MIParser::initNames2SubRegIndices() {
630 if (!Names2SubRegIndices.empty())
631 return;
632 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
633 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
634 Names2SubRegIndices.insert(
635 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
636}
637
638unsigned MIParser::getSubRegIndex(StringRef Name) {
639 initNames2SubRegIndices();
640 auto SubRegInfo = Names2SubRegIndices.find(Name);
641 if (SubRegInfo == Names2SubRegIndices.end())
642 return 0;
643 return SubRegInfo->getValue();
644}
645
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000646bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
647 MachineFunction &MF, StringRef Src,
648 const PerFunctionMIParsingState &PFS,
649 const SlotMapping &IRSlots, SMDiagnostic &Error) {
650 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000651}
Alex Lorenzf09df002015-06-30 18:16:42 +0000652
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000653bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
654 MachineFunction &MF, StringRef Src,
655 const PerFunctionMIParsingState &PFS,
656 const SlotMapping &IRSlots, SMDiagnostic &Error) {
657 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000658}
Alex Lorenz9fab3702015-07-14 21:24:41 +0000659
660bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
661 MachineFunction &MF, StringRef Src,
662 const PerFunctionMIParsingState &PFS,
663 const SlotMapping &IRSlots,
664 SMDiagnostic &Error) {
665 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseNamedRegister(Reg);
666}