blob: ca98ae3c97f6aad55f4737ee32a4e8d5dc4bcb1c [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 Lorenzb29554d2015-07-20 20:31:01 +000034struct StringValueUtility {
35 StringRef String;
36 std::string UnescapedString;
37
38 StringValueUtility(const MIToken &Token) {
39 if (Token.isStringValueQuoted()) {
40 Token.unescapeQuotedStringValue(UnescapedString);
41 String = UnescapedString;
42 return;
43 }
44 String = Token.stringValue();
45 }
46
47 operator StringRef() const { return String; }
48};
49
Alex Lorenz36962cd2015-07-07 02:08:46 +000050/// A wrapper struct around the 'MachineOperand' struct that includes a source
51/// range.
52struct MachineOperandWithLocation {
53 MachineOperand Operand;
54 StringRef::iterator Begin;
55 StringRef::iterator End;
56
57 MachineOperandWithLocation(const MachineOperand &Operand,
58 StringRef::iterator Begin, StringRef::iterator End)
59 : Operand(Operand), Begin(Begin), End(End) {}
60};
61
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000062class MIParser {
63 SourceMgr &SM;
64 MachineFunction &MF;
65 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000066 StringRef Source, CurrentSource;
67 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000068 const PerFunctionMIParsingState &PFS;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000069 /// Maps from indices to unnamed global values and metadata nodes.
70 const SlotMapping &IRSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000071 /// Maps from instruction names to op codes.
72 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000073 /// Maps from register names to registers.
74 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000075 /// Maps from register mask names to register masks.
76 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000077 /// Maps from subregister names to subregister indices.
78 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000079
80public:
81 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000082 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000083 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000084
Alex Lorenz91370c52015-06-22 20:37:46 +000085 void lex();
86
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000087 /// Report an error at the current location with the given message.
88 ///
89 /// This function always return true.
90 bool error(const Twine &Msg);
91
Alex Lorenz91370c52015-06-22 20:37:46 +000092 /// Report an error at the given location with the given message.
93 ///
94 /// This function always return true.
95 bool error(StringRef::iterator Loc, const Twine &Msg);
96
Alex Lorenz3708a642015-06-30 17:47:50 +000097 bool parse(MachineInstr *&MI);
Alex Lorenzf09df002015-06-30 18:16:42 +000098 bool parseMBB(MachineBasicBlock *&MBB);
Alex Lorenz9fab3702015-07-14 21:24:41 +000099 bool parseNamedRegister(unsigned &Reg);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000100
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000101 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000102 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000103 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000104 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000105 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000106 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000107 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000108 bool parseStackObjectOperand(MachineOperand &Dest);
109 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000110 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000111 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000112 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000113 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000114 bool parseMachineOperand(MachineOperand &Dest);
115
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000116private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000117 /// Convert the integer literal in the current token into an unsigned integer.
118 ///
119 /// Return true if an error occurred.
120 bool getUnsigned(unsigned &Result);
121
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000122 void initNames2InstrOpCodes();
123
124 /// Try to convert an instruction name to an opcode. Return true if the
125 /// instruction name is invalid.
126 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000127
Alex Lorenze5a44662015-07-17 00:24:15 +0000128 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000129
Alex Lorenz36962cd2015-07-07 02:08:46 +0000130 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
131 const MCInstrDesc &MCID);
132
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000133 void initNames2Regs();
134
135 /// Try to convert a register name to a register number. Return true if the
136 /// register name is invalid.
137 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000138
139 void initNames2RegMasks();
140
141 /// Check if the given identifier is a name of a register mask.
142 ///
143 /// Return null if the identifier isn't a register mask.
144 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000145
146 void initNames2SubRegIndices();
147
148 /// Check if the given identifier is a name of a subregister index.
149 ///
150 /// Return 0 if the name isn't a subregister index class.
151 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000152};
153
154} // end anonymous namespace
155
156MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000157 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000158 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000159 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000160 Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000161
Alex Lorenz91370c52015-06-22 20:37:46 +0000162void MIParser::lex() {
163 CurrentSource = lexMIToken(
164 CurrentSource, Token,
165 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
166}
167
168bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
169
170bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000171 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
172 Error = SMDiagnostic(
173 SM, SMLoc(),
174 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
175 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000176 return true;
177}
178
Alex Lorenz3708a642015-06-30 17:47:50 +0000179bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000180 lex();
181
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000182 // Parse any register operands before '='
183 // TODO: Allow parsing of multiple operands before '='
184 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000185 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000186 if (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000187 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000188 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000189 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000190 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenz3708a642015-06-30 17:47:50 +0000191 if (Token.isNot(MIToken::equal))
192 return error("expected '='");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000193 lex();
194 }
195
Alex Lorenze5a44662015-07-17 00:24:15 +0000196 unsigned OpCode, Flags = 0;
197 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000198 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000199
Alex Lorenze5a44662015-07-17 00:24:15 +0000200 // TODO: Parse the bundle instruction flags and memory operands.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000201
202 // Parse the remaining machine operands.
203 while (Token.isNot(MIToken::Eof)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000204 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000205 if (parseMachineOperand(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000206 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000207 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000208 if (Token.is(MIToken::Eof))
209 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000210 if (Token.isNot(MIToken::comma))
211 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000212 lex();
213 }
214
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000215 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000216 if (!MCID.isVariadic()) {
217 // FIXME: Move the implicit operand verification to the machine verifier.
218 if (verifyImplicitOperands(Operands, MCID))
219 return true;
220 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000221
Alex Lorenzcb268d42015-07-06 23:07:26 +0000222 // TODO: Check for extraneous machine operands.
Alex Lorenz3708a642015-06-30 17:47:50 +0000223 MI = MF.CreateMachineInstr(MCID, DebugLoc(), /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000224 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000225 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000226 MI->addOperand(MF, Operand.Operand);
Alex Lorenz3708a642015-06-30 17:47:50 +0000227 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000228}
229
Alex Lorenzf09df002015-06-30 18:16:42 +0000230bool MIParser::parseMBB(MachineBasicBlock *&MBB) {
231 lex();
232 if (Token.isNot(MIToken::MachineBasicBlock))
233 return error("expected a machine basic block reference");
234 if (parseMBBReference(MBB))
235 return true;
236 lex();
237 if (Token.isNot(MIToken::Eof))
238 return error(
239 "expected end of string after the machine basic block reference");
240 return false;
241}
242
Alex Lorenz9fab3702015-07-14 21:24:41 +0000243bool MIParser::parseNamedRegister(unsigned &Reg) {
244 lex();
245 if (Token.isNot(MIToken::NamedRegister))
246 return error("expected a named register");
247 if (parseRegister(Reg))
248 return 0;
249 lex();
250 if (Token.isNot(MIToken::Eof))
251 return error("expected end of string after the register reference");
252 return false;
253}
254
Alex Lorenz36962cd2015-07-07 02:08:46 +0000255static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
256 assert(MO.isImplicit());
257 return MO.isDef() ? "implicit-def" : "implicit";
258}
259
260static std::string getRegisterName(const TargetRegisterInfo *TRI,
261 unsigned Reg) {
262 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
263 return StringRef(TRI->getName(Reg)).lower();
264}
265
266bool MIParser::verifyImplicitOperands(
267 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
268 if (MCID.isCall())
269 // We can't verify call instructions as they can contain arbitrary implicit
270 // register and register mask operands.
271 return false;
272
273 // Gather all the expected implicit operands.
274 SmallVector<MachineOperand, 4> ImplicitOperands;
275 if (MCID.ImplicitDefs)
276 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
277 ImplicitOperands.push_back(
278 MachineOperand::CreateReg(*ImpDefs, true, true));
279 if (MCID.ImplicitUses)
280 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
281 ImplicitOperands.push_back(
282 MachineOperand::CreateReg(*ImpUses, false, true));
283
284 const auto *TRI = MF.getSubtarget().getRegisterInfo();
285 assert(TRI && "Expected target register info");
286 size_t I = ImplicitOperands.size(), J = Operands.size();
287 while (I) {
288 --I;
289 if (J) {
290 --J;
291 const auto &ImplicitOperand = ImplicitOperands[I];
292 const auto &Operand = Operands[J].Operand;
293 if (ImplicitOperand.isIdenticalTo(Operand))
294 continue;
295 if (Operand.isReg() && Operand.isImplicit()) {
296 return error(Operands[J].Begin,
297 Twine("expected an implicit register operand '") +
298 printImplicitRegisterFlag(ImplicitOperand) + " %" +
299 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
300 }
301 }
302 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
303 // insead of reporting an error at this location:
304 // %eax = MOV32r0
305 // ^
306 // report the error at the following location:
307 // %eax = MOV32r0
308 // ^
309 return error(J < Operands.size() ? Operands[J].End : Token.location(),
310 Twine("missing implicit register operand '") +
311 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
312 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
313 }
314 return false;
315}
316
Alex Lorenze5a44662015-07-17 00:24:15 +0000317bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
318 if (Token.is(MIToken::kw_frame_setup)) {
319 Flags |= MachineInstr::FrameSetup;
320 lex();
321 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000322 if (Token.isNot(MIToken::Identifier))
323 return error("expected a machine instruction");
324 StringRef InstrName = Token.stringValue();
325 if (parseInstrName(InstrName, OpCode))
326 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000327 lex();
328 return false;
329}
330
331bool MIParser::parseRegister(unsigned &Reg) {
332 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000333 case MIToken::underscore:
334 Reg = 0;
335 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000336 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000337 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000338 if (getRegisterByName(Name, Reg))
339 return error(Twine("unknown register name '") + Name + "'");
340 break;
341 }
Alex Lorenz53464512015-07-10 22:51:20 +0000342 case MIToken::VirtualRegister: {
343 unsigned ID;
344 if (getUnsigned(ID))
345 return true;
346 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
347 if (RegInfo == PFS.VirtualRegisterSlots.end())
348 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
349 "'");
350 Reg = RegInfo->second;
351 break;
352 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000353 // TODO: Parse other register kinds.
354 default:
355 llvm_unreachable("The current token should be a register");
356 }
357 return false;
358}
359
Alex Lorenzcb268d42015-07-06 23:07:26 +0000360bool MIParser::parseRegisterFlag(unsigned &Flags) {
361 switch (Token.kind()) {
362 case MIToken::kw_implicit:
363 Flags |= RegState::Implicit;
364 break;
365 case MIToken::kw_implicit_define:
366 Flags |= RegState::ImplicitDefine;
367 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000368 case MIToken::kw_dead:
369 Flags |= RegState::Dead;
370 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000371 case MIToken::kw_killed:
372 Flags |= RegState::Kill;
373 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000374 case MIToken::kw_undef:
375 Flags |= RegState::Undef;
376 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000377 // TODO: report an error when we specify the same flag more than once.
378 // TODO: parse the other register flags.
379 default:
380 llvm_unreachable("The current token should be a register flag");
381 }
382 lex();
383 return false;
384}
385
Alex Lorenz2eacca82015-07-13 23:24:34 +0000386bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
387 assert(Token.is(MIToken::colon));
388 lex();
389 if (Token.isNot(MIToken::Identifier))
390 return error("expected a subregister index after ':'");
391 auto Name = Token.stringValue();
392 SubReg = getSubRegIndex(Name);
393 if (!SubReg)
394 return error(Twine("use of unknown subregister index '") + Name + "'");
395 lex();
396 return false;
397}
398
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000399bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
400 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000401 unsigned Flags = IsDef ? RegState::Define : 0;
402 while (Token.isRegisterFlag()) {
403 if (parseRegisterFlag(Flags))
404 return true;
405 }
406 if (!Token.isRegister())
407 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000408 if (parseRegister(Reg))
409 return true;
410 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000411 unsigned SubReg = 0;
412 if (Token.is(MIToken::colon)) {
413 if (parseSubRegisterIndex(SubReg))
414 return true;
415 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000416 Dest = MachineOperand::CreateReg(
417 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000418 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
419 /*isEarlyClobber=*/false, SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000420 return false;
421}
422
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000423bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
424 assert(Token.is(MIToken::IntegerLiteral));
425 const APSInt &Int = Token.integerValue();
426 if (Int.getMinSignedBits() > 64)
427 // TODO: Replace this with an error when we can parse CIMM Machine Operands.
428 llvm_unreachable("Can't parse large integer literals yet!");
429 Dest = MachineOperand::CreateImm(Int.getExtValue());
430 lex();
431 return false;
432}
433
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000434bool MIParser::getUnsigned(unsigned &Result) {
435 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
436 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
437 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
438 if (Val64 == Limit)
439 return error("expected 32-bit integer (too large)");
440 Result = Val64;
441 return false;
442}
443
Alex Lorenzf09df002015-06-30 18:16:42 +0000444bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000445 assert(Token.is(MIToken::MachineBasicBlock));
446 unsigned Number;
447 if (getUnsigned(Number))
448 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000449 auto MBBInfo = PFS.MBBSlots.find(Number);
450 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000451 return error(Twine("use of undefined machine basic block #") +
452 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000453 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000454 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
455 return error(Twine("the name of machine basic block #") + Twine(Number) +
456 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000457 return false;
458}
459
460bool MIParser::parseMBBOperand(MachineOperand &Dest) {
461 MachineBasicBlock *MBB;
462 if (parseMBBReference(MBB))
463 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000464 Dest = MachineOperand::CreateMBB(MBB);
465 lex();
466 return false;
467}
468
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000469bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
470 assert(Token.is(MIToken::StackObject));
471 unsigned ID;
472 if (getUnsigned(ID))
473 return true;
474 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
475 if (ObjectInfo == PFS.StackObjectSlots.end())
476 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
477 "'");
478 StringRef Name;
479 if (const auto *Alloca =
480 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
481 Name = Alloca->getName();
482 if (!Token.stringValue().empty() && Token.stringValue() != Name)
483 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
484 "' isn't '" + Token.stringValue() + "'");
485 lex();
486 Dest = MachineOperand::CreateFI(ObjectInfo->second);
487 return false;
488}
489
490bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
491 assert(Token.is(MIToken::FixedStackObject));
492 unsigned ID;
493 if (getUnsigned(ID))
494 return true;
495 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
496 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
497 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
498 Twine(ID) + "'");
499 lex();
500 Dest = MachineOperand::CreateFI(ObjectInfo->second);
501 return false;
502}
503
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000504bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
505 switch (Token.kind()) {
Alex Lorenzb29554d2015-07-20 20:31:01 +0000506 case MIToken::NamedGlobalValue:
507 case MIToken::QuotedNamedGlobalValue: {
508 StringValueUtility Name(Token);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000509 const Module *M = MF.getFunction()->getParent();
510 if (const auto *GV = M->getNamedValue(Name)) {
511 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
512 break;
513 }
Alex Lorenzb29554d2015-07-20 20:31:01 +0000514 return error(Twine("use of undefined global value '@") +
515 Token.rawStringValue() + "'");
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000516 }
517 case MIToken::GlobalValue: {
518 unsigned GVIdx;
519 if (getUnsigned(GVIdx))
520 return true;
521 if (GVIdx >= IRSlots.GlobalValues.size())
522 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
523 "'");
524 Dest = MachineOperand::CreateGA(IRSlots.GlobalValues[GVIdx],
525 /*Offset=*/0);
526 break;
527 }
528 default:
529 llvm_unreachable("The current token should be a global value");
530 }
531 // TODO: Parse offset and target flags.
532 lex();
533 return false;
534}
535
Alex Lorenzab980492015-07-20 20:51:18 +0000536bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
537 assert(Token.is(MIToken::ConstantPoolItem));
538 unsigned ID;
539 if (getUnsigned(ID))
540 return true;
541 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
542 if (ConstantInfo == PFS.ConstantPoolSlots.end())
543 return error("use of undefined constant '%const." + Twine(ID) + "'");
544 lex();
545 // TODO: Parse offset and target flags.
546 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
547 return false;
548}
549
Alex Lorenz31d70682015-07-15 23:38:35 +0000550bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
551 assert(Token.is(MIToken::JumpTableIndex));
552 unsigned ID;
553 if (getUnsigned(ID))
554 return true;
555 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
556 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
557 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
558 lex();
559 // TODO: Parse target flags.
560 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
561 return false;
562}
563
Alex Lorenz6ede3742015-07-21 16:59:53 +0000564bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
565 assert(Token.is(MIToken::ExternalSymbol) ||
566 Token.is(MIToken::QuotedExternalSymbol));
567 StringValueUtility Name(Token);
568 const char *Symbol = MF.createExternalSymbolName(Name);
569 lex();
570 // TODO: Parse the target flags.
571 Dest = MachineOperand::CreateES(Symbol);
572 return false;
573}
574
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000575bool MIParser::parseMachineOperand(MachineOperand &Dest) {
576 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000577 case MIToken::kw_implicit:
578 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000579 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000580 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000581 case MIToken::kw_undef:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000582 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000583 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000584 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000585 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000586 case MIToken::IntegerLiteral:
587 return parseImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000588 case MIToken::MachineBasicBlock:
589 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000590 case MIToken::StackObject:
591 return parseStackObjectOperand(Dest);
592 case MIToken::FixedStackObject:
593 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000594 case MIToken::GlobalValue:
595 case MIToken::NamedGlobalValue:
Alex Lorenzb29554d2015-07-20 20:31:01 +0000596 case MIToken::QuotedNamedGlobalValue:
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000597 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000598 case MIToken::ConstantPoolItem:
599 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000600 case MIToken::JumpTableIndex:
601 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000602 case MIToken::ExternalSymbol:
603 case MIToken::QuotedExternalSymbol:
604 return parseExternalSymbolOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000605 case MIToken::Error:
606 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000607 case MIToken::Identifier:
608 if (const auto *RegMask = getRegMask(Token.stringValue())) {
609 Dest = MachineOperand::CreateRegMask(RegMask);
610 lex();
611 break;
612 }
613 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000614 default:
615 // TODO: parse the other machine operands.
616 return error("expected a machine operand");
617 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000618 return false;
619}
620
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000621void MIParser::initNames2InstrOpCodes() {
622 if (!Names2InstrOpCodes.empty())
623 return;
624 const auto *TII = MF.getSubtarget().getInstrInfo();
625 assert(TII && "Expected target instruction info");
626 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
627 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
628}
629
630bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
631 initNames2InstrOpCodes();
632 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
633 if (InstrInfo == Names2InstrOpCodes.end())
634 return true;
635 OpCode = InstrInfo->getValue();
636 return false;
637}
638
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000639void MIParser::initNames2Regs() {
640 if (!Names2Regs.empty())
641 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +0000642 // The '%noreg' register is the register 0.
643 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000644 const auto *TRI = MF.getSubtarget().getRegisterInfo();
645 assert(TRI && "Expected target register info");
646 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
647 bool WasInserted =
648 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
649 .second;
650 (void)WasInserted;
651 assert(WasInserted && "Expected registers to be unique case-insensitively");
652 }
653}
654
655bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
656 initNames2Regs();
657 auto RegInfo = Names2Regs.find(RegName);
658 if (RegInfo == Names2Regs.end())
659 return true;
660 Reg = RegInfo->getValue();
661 return false;
662}
663
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000664void MIParser::initNames2RegMasks() {
665 if (!Names2RegMasks.empty())
666 return;
667 const auto *TRI = MF.getSubtarget().getRegisterInfo();
668 assert(TRI && "Expected target register info");
669 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
670 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
671 assert(RegMasks.size() == RegMaskNames.size());
672 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
673 Names2RegMasks.insert(
674 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
675}
676
677const uint32_t *MIParser::getRegMask(StringRef Identifier) {
678 initNames2RegMasks();
679 auto RegMaskInfo = Names2RegMasks.find(Identifier);
680 if (RegMaskInfo == Names2RegMasks.end())
681 return nullptr;
682 return RegMaskInfo->getValue();
683}
684
Alex Lorenz2eacca82015-07-13 23:24:34 +0000685void MIParser::initNames2SubRegIndices() {
686 if (!Names2SubRegIndices.empty())
687 return;
688 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
689 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
690 Names2SubRegIndices.insert(
691 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
692}
693
694unsigned MIParser::getSubRegIndex(StringRef Name) {
695 initNames2SubRegIndices();
696 auto SubRegInfo = Names2SubRegIndices.find(Name);
697 if (SubRegInfo == Names2SubRegIndices.end())
698 return 0;
699 return SubRegInfo->getValue();
700}
701
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000702bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
703 MachineFunction &MF, StringRef Src,
704 const PerFunctionMIParsingState &PFS,
705 const SlotMapping &IRSlots, SMDiagnostic &Error) {
706 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000707}
Alex Lorenzf09df002015-06-30 18:16:42 +0000708
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000709bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
710 MachineFunction &MF, StringRef Src,
711 const PerFunctionMIParsingState &PFS,
712 const SlotMapping &IRSlots, SMDiagnostic &Error) {
713 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000714}
Alex Lorenz9fab3702015-07-14 21:24:41 +0000715
716bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
717 MachineFunction &MF, StringRef Src,
718 const PerFunctionMIParsingState &PFS,
719 const SlotMapping &IRSlots,
720 SMDiagnostic &Error) {
721 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseNamedRegister(Reg);
722}