blob: 34d3e1e3c53a36983361c253dbd4c2606b834592 [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 Lorenzf3db51de2015-06-23 16:35:26 +0000113 bool parseMachineOperand(MachineOperand &Dest);
114
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000115private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000116 /// Convert the integer literal in the current token into an unsigned integer.
117 ///
118 /// Return true if an error occurred.
119 bool getUnsigned(unsigned &Result);
120
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000121 void initNames2InstrOpCodes();
122
123 /// Try to convert an instruction name to an opcode. Return true if the
124 /// instruction name is invalid.
125 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000126
Alex Lorenze5a44662015-07-17 00:24:15 +0000127 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000128
Alex Lorenz36962cd2015-07-07 02:08:46 +0000129 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
130 const MCInstrDesc &MCID);
131
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000132 void initNames2Regs();
133
134 /// Try to convert a register name to a register number. Return true if the
135 /// register name is invalid.
136 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000137
138 void initNames2RegMasks();
139
140 /// Check if the given identifier is a name of a register mask.
141 ///
142 /// Return null if the identifier isn't a register mask.
143 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000144
145 void initNames2SubRegIndices();
146
147 /// Check if the given identifier is a name of a subregister index.
148 ///
149 /// Return 0 if the name isn't a subregister index class.
150 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000151};
152
153} // end anonymous namespace
154
155MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000156 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000157 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000158 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000159 Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000160
Alex Lorenz91370c52015-06-22 20:37:46 +0000161void MIParser::lex() {
162 CurrentSource = lexMIToken(
163 CurrentSource, Token,
164 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
165}
166
167bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
168
169bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000170 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
171 Error = SMDiagnostic(
172 SM, SMLoc(),
173 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
174 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000175 return true;
176}
177
Alex Lorenz3708a642015-06-30 17:47:50 +0000178bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000179 lex();
180
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000181 // Parse any register operands before '='
182 // TODO: Allow parsing of multiple operands before '='
183 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000184 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000185 if (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000186 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000187 if (parseRegisterOperand(MO, /*IsDef=*/true))
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 Lorenz3708a642015-06-30 17:47:50 +0000190 if (Token.isNot(MIToken::equal))
191 return error("expected '='");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000192 lex();
193 }
194
Alex Lorenze5a44662015-07-17 00:24:15 +0000195 unsigned OpCode, Flags = 0;
196 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000197 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000198
Alex Lorenze5a44662015-07-17 00:24:15 +0000199 // TODO: Parse the bundle instruction flags and memory operands.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000200
201 // Parse the remaining machine operands.
202 while (Token.isNot(MIToken::Eof)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000203 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000204 if (parseMachineOperand(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000205 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000206 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000207 if (Token.is(MIToken::Eof))
208 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000209 if (Token.isNot(MIToken::comma))
210 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000211 lex();
212 }
213
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000214 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000215 if (!MCID.isVariadic()) {
216 // FIXME: Move the implicit operand verification to the machine verifier.
217 if (verifyImplicitOperands(Operands, MCID))
218 return true;
219 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000220
Alex Lorenzcb268d42015-07-06 23:07:26 +0000221 // TODO: Check for extraneous machine operands.
Alex Lorenz3708a642015-06-30 17:47:50 +0000222 MI = MF.CreateMachineInstr(MCID, DebugLoc(), /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000223 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000224 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000225 MI->addOperand(MF, Operand.Operand);
Alex Lorenz3708a642015-06-30 17:47:50 +0000226 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000227}
228
Alex Lorenzf09df002015-06-30 18:16:42 +0000229bool MIParser::parseMBB(MachineBasicBlock *&MBB) {
230 lex();
231 if (Token.isNot(MIToken::MachineBasicBlock))
232 return error("expected a machine basic block reference");
233 if (parseMBBReference(MBB))
234 return true;
235 lex();
236 if (Token.isNot(MIToken::Eof))
237 return error(
238 "expected end of string after the machine basic block reference");
239 return false;
240}
241
Alex Lorenz9fab3702015-07-14 21:24:41 +0000242bool MIParser::parseNamedRegister(unsigned &Reg) {
243 lex();
244 if (Token.isNot(MIToken::NamedRegister))
245 return error("expected a named register");
246 if (parseRegister(Reg))
247 return 0;
248 lex();
249 if (Token.isNot(MIToken::Eof))
250 return error("expected end of string after the register reference");
251 return false;
252}
253
Alex Lorenz36962cd2015-07-07 02:08:46 +0000254static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
255 assert(MO.isImplicit());
256 return MO.isDef() ? "implicit-def" : "implicit";
257}
258
259static std::string getRegisterName(const TargetRegisterInfo *TRI,
260 unsigned Reg) {
261 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
262 return StringRef(TRI->getName(Reg)).lower();
263}
264
265bool MIParser::verifyImplicitOperands(
266 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
267 if (MCID.isCall())
268 // We can't verify call instructions as they can contain arbitrary implicit
269 // register and register mask operands.
270 return false;
271
272 // Gather all the expected implicit operands.
273 SmallVector<MachineOperand, 4> ImplicitOperands;
274 if (MCID.ImplicitDefs)
275 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
276 ImplicitOperands.push_back(
277 MachineOperand::CreateReg(*ImpDefs, true, true));
278 if (MCID.ImplicitUses)
279 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
280 ImplicitOperands.push_back(
281 MachineOperand::CreateReg(*ImpUses, false, true));
282
283 const auto *TRI = MF.getSubtarget().getRegisterInfo();
284 assert(TRI && "Expected target register info");
285 size_t I = ImplicitOperands.size(), J = Operands.size();
286 while (I) {
287 --I;
288 if (J) {
289 --J;
290 const auto &ImplicitOperand = ImplicitOperands[I];
291 const auto &Operand = Operands[J].Operand;
292 if (ImplicitOperand.isIdenticalTo(Operand))
293 continue;
294 if (Operand.isReg() && Operand.isImplicit()) {
295 return error(Operands[J].Begin,
296 Twine("expected an implicit register operand '") +
297 printImplicitRegisterFlag(ImplicitOperand) + " %" +
298 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
299 }
300 }
301 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
302 // insead of reporting an error at this location:
303 // %eax = MOV32r0
304 // ^
305 // report the error at the following location:
306 // %eax = MOV32r0
307 // ^
308 return error(J < Operands.size() ? Operands[J].End : Token.location(),
309 Twine("missing implicit register operand '") +
310 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
311 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
312 }
313 return false;
314}
315
Alex Lorenze5a44662015-07-17 00:24:15 +0000316bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
317 if (Token.is(MIToken::kw_frame_setup)) {
318 Flags |= MachineInstr::FrameSetup;
319 lex();
320 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000321 if (Token.isNot(MIToken::Identifier))
322 return error("expected a machine instruction");
323 StringRef InstrName = Token.stringValue();
324 if (parseInstrName(InstrName, OpCode))
325 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000326 lex();
327 return false;
328}
329
330bool MIParser::parseRegister(unsigned &Reg) {
331 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000332 case MIToken::underscore:
333 Reg = 0;
334 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000335 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000336 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000337 if (getRegisterByName(Name, Reg))
338 return error(Twine("unknown register name '") + Name + "'");
339 break;
340 }
Alex Lorenz53464512015-07-10 22:51:20 +0000341 case MIToken::VirtualRegister: {
342 unsigned ID;
343 if (getUnsigned(ID))
344 return true;
345 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
346 if (RegInfo == PFS.VirtualRegisterSlots.end())
347 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
348 "'");
349 Reg = RegInfo->second;
350 break;
351 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000352 // TODO: Parse other register kinds.
353 default:
354 llvm_unreachable("The current token should be a register");
355 }
356 return false;
357}
358
Alex Lorenzcb268d42015-07-06 23:07:26 +0000359bool MIParser::parseRegisterFlag(unsigned &Flags) {
360 switch (Token.kind()) {
361 case MIToken::kw_implicit:
362 Flags |= RegState::Implicit;
363 break;
364 case MIToken::kw_implicit_define:
365 Flags |= RegState::ImplicitDefine;
366 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000367 case MIToken::kw_dead:
368 Flags |= RegState::Dead;
369 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000370 case MIToken::kw_killed:
371 Flags |= RegState::Kill;
372 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000373 case MIToken::kw_undef:
374 Flags |= RegState::Undef;
375 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000376 // TODO: report an error when we specify the same flag more than once.
377 // TODO: parse the other register flags.
378 default:
379 llvm_unreachable("The current token should be a register flag");
380 }
381 lex();
382 return false;
383}
384
Alex Lorenz2eacca82015-07-13 23:24:34 +0000385bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
386 assert(Token.is(MIToken::colon));
387 lex();
388 if (Token.isNot(MIToken::Identifier))
389 return error("expected a subregister index after ':'");
390 auto Name = Token.stringValue();
391 SubReg = getSubRegIndex(Name);
392 if (!SubReg)
393 return error(Twine("use of unknown subregister index '") + Name + "'");
394 lex();
395 return false;
396}
397
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000398bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
399 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000400 unsigned Flags = IsDef ? RegState::Define : 0;
401 while (Token.isRegisterFlag()) {
402 if (parseRegisterFlag(Flags))
403 return true;
404 }
405 if (!Token.isRegister())
406 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000407 if (parseRegister(Reg))
408 return true;
409 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000410 unsigned SubReg = 0;
411 if (Token.is(MIToken::colon)) {
412 if (parseSubRegisterIndex(SubReg))
413 return true;
414 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000415 Dest = MachineOperand::CreateReg(
416 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000417 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
418 /*isEarlyClobber=*/false, SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000419 return false;
420}
421
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000422bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
423 assert(Token.is(MIToken::IntegerLiteral));
424 const APSInt &Int = Token.integerValue();
425 if (Int.getMinSignedBits() > 64)
426 // TODO: Replace this with an error when we can parse CIMM Machine Operands.
427 llvm_unreachable("Can't parse large integer literals yet!");
428 Dest = MachineOperand::CreateImm(Int.getExtValue());
429 lex();
430 return false;
431}
432
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000433bool MIParser::getUnsigned(unsigned &Result) {
434 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
435 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
436 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
437 if (Val64 == Limit)
438 return error("expected 32-bit integer (too large)");
439 Result = Val64;
440 return false;
441}
442
Alex Lorenzf09df002015-06-30 18:16:42 +0000443bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000444 assert(Token.is(MIToken::MachineBasicBlock));
445 unsigned Number;
446 if (getUnsigned(Number))
447 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000448 auto MBBInfo = PFS.MBBSlots.find(Number);
449 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000450 return error(Twine("use of undefined machine basic block #") +
451 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000452 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000453 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
454 return error(Twine("the name of machine basic block #") + Twine(Number) +
455 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000456 return false;
457}
458
459bool MIParser::parseMBBOperand(MachineOperand &Dest) {
460 MachineBasicBlock *MBB;
461 if (parseMBBReference(MBB))
462 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000463 Dest = MachineOperand::CreateMBB(MBB);
464 lex();
465 return false;
466}
467
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000468bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
469 assert(Token.is(MIToken::StackObject));
470 unsigned ID;
471 if (getUnsigned(ID))
472 return true;
473 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
474 if (ObjectInfo == PFS.StackObjectSlots.end())
475 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
476 "'");
477 StringRef Name;
478 if (const auto *Alloca =
479 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
480 Name = Alloca->getName();
481 if (!Token.stringValue().empty() && Token.stringValue() != Name)
482 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
483 "' isn't '" + Token.stringValue() + "'");
484 lex();
485 Dest = MachineOperand::CreateFI(ObjectInfo->second);
486 return false;
487}
488
489bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
490 assert(Token.is(MIToken::FixedStackObject));
491 unsigned ID;
492 if (getUnsigned(ID))
493 return true;
494 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
495 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
496 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
497 Twine(ID) + "'");
498 lex();
499 Dest = MachineOperand::CreateFI(ObjectInfo->second);
500 return false;
501}
502
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000503bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
504 switch (Token.kind()) {
Alex Lorenzb29554d2015-07-20 20:31:01 +0000505 case MIToken::NamedGlobalValue:
506 case MIToken::QuotedNamedGlobalValue: {
507 StringValueUtility Name(Token);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000508 const Module *M = MF.getFunction()->getParent();
509 if (const auto *GV = M->getNamedValue(Name)) {
510 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
511 break;
512 }
Alex Lorenzb29554d2015-07-20 20:31:01 +0000513 return error(Twine("use of undefined global value '@") +
514 Token.rawStringValue() + "'");
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000515 }
516 case MIToken::GlobalValue: {
517 unsigned GVIdx;
518 if (getUnsigned(GVIdx))
519 return true;
520 if (GVIdx >= IRSlots.GlobalValues.size())
521 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
522 "'");
523 Dest = MachineOperand::CreateGA(IRSlots.GlobalValues[GVIdx],
524 /*Offset=*/0);
525 break;
526 }
527 default:
528 llvm_unreachable("The current token should be a global value");
529 }
530 // TODO: Parse offset and target flags.
531 lex();
532 return false;
533}
534
Alex Lorenzab980492015-07-20 20:51:18 +0000535bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
536 assert(Token.is(MIToken::ConstantPoolItem));
537 unsigned ID;
538 if (getUnsigned(ID))
539 return true;
540 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
541 if (ConstantInfo == PFS.ConstantPoolSlots.end())
542 return error("use of undefined constant '%const." + Twine(ID) + "'");
543 lex();
544 // TODO: Parse offset and target flags.
545 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
546 return false;
547}
548
Alex Lorenz31d70682015-07-15 23:38:35 +0000549bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
550 assert(Token.is(MIToken::JumpTableIndex));
551 unsigned ID;
552 if (getUnsigned(ID))
553 return true;
554 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
555 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
556 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
557 lex();
558 // TODO: Parse target flags.
559 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
560 return false;
561}
562
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000563bool MIParser::parseMachineOperand(MachineOperand &Dest) {
564 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000565 case MIToken::kw_implicit:
566 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000567 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000568 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000569 case MIToken::kw_undef:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000570 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000571 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000572 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000573 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000574 case MIToken::IntegerLiteral:
575 return parseImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000576 case MIToken::MachineBasicBlock:
577 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000578 case MIToken::StackObject:
579 return parseStackObjectOperand(Dest);
580 case MIToken::FixedStackObject:
581 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000582 case MIToken::GlobalValue:
583 case MIToken::NamedGlobalValue:
Alex Lorenzb29554d2015-07-20 20:31:01 +0000584 case MIToken::QuotedNamedGlobalValue:
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000585 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000586 case MIToken::ConstantPoolItem:
587 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000588 case MIToken::JumpTableIndex:
589 return parseJumpTableIndexOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000590 case MIToken::Error:
591 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000592 case MIToken::Identifier:
593 if (const auto *RegMask = getRegMask(Token.stringValue())) {
594 Dest = MachineOperand::CreateRegMask(RegMask);
595 lex();
596 break;
597 }
598 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000599 default:
600 // TODO: parse the other machine operands.
601 return error("expected a machine operand");
602 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000603 return false;
604}
605
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000606void MIParser::initNames2InstrOpCodes() {
607 if (!Names2InstrOpCodes.empty())
608 return;
609 const auto *TII = MF.getSubtarget().getInstrInfo();
610 assert(TII && "Expected target instruction info");
611 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
612 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
613}
614
615bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
616 initNames2InstrOpCodes();
617 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
618 if (InstrInfo == Names2InstrOpCodes.end())
619 return true;
620 OpCode = InstrInfo->getValue();
621 return false;
622}
623
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000624void MIParser::initNames2Regs() {
625 if (!Names2Regs.empty())
626 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +0000627 // The '%noreg' register is the register 0.
628 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000629 const auto *TRI = MF.getSubtarget().getRegisterInfo();
630 assert(TRI && "Expected target register info");
631 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
632 bool WasInserted =
633 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
634 .second;
635 (void)WasInserted;
636 assert(WasInserted && "Expected registers to be unique case-insensitively");
637 }
638}
639
640bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
641 initNames2Regs();
642 auto RegInfo = Names2Regs.find(RegName);
643 if (RegInfo == Names2Regs.end())
644 return true;
645 Reg = RegInfo->getValue();
646 return false;
647}
648
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000649void MIParser::initNames2RegMasks() {
650 if (!Names2RegMasks.empty())
651 return;
652 const auto *TRI = MF.getSubtarget().getRegisterInfo();
653 assert(TRI && "Expected target register info");
654 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
655 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
656 assert(RegMasks.size() == RegMaskNames.size());
657 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
658 Names2RegMasks.insert(
659 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
660}
661
662const uint32_t *MIParser::getRegMask(StringRef Identifier) {
663 initNames2RegMasks();
664 auto RegMaskInfo = Names2RegMasks.find(Identifier);
665 if (RegMaskInfo == Names2RegMasks.end())
666 return nullptr;
667 return RegMaskInfo->getValue();
668}
669
Alex Lorenz2eacca82015-07-13 23:24:34 +0000670void MIParser::initNames2SubRegIndices() {
671 if (!Names2SubRegIndices.empty())
672 return;
673 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
674 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
675 Names2SubRegIndices.insert(
676 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
677}
678
679unsigned MIParser::getSubRegIndex(StringRef Name) {
680 initNames2SubRegIndices();
681 auto SubRegInfo = Names2SubRegIndices.find(Name);
682 if (SubRegInfo == Names2SubRegIndices.end())
683 return 0;
684 return SubRegInfo->getValue();
685}
686
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000687bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
688 MachineFunction &MF, StringRef Src,
689 const PerFunctionMIParsingState &PFS,
690 const SlotMapping &IRSlots, SMDiagnostic &Error) {
691 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000692}
Alex Lorenzf09df002015-06-30 18:16:42 +0000693
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000694bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
695 MachineFunction &MF, StringRef Src,
696 const PerFunctionMIParsingState &PFS,
697 const SlotMapping &IRSlots, SMDiagnostic &Error) {
698 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000699}
Alex Lorenz9fab3702015-07-14 21:24:41 +0000700
701bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
702 MachineFunction &MF, StringRef Src,
703 const PerFunctionMIParsingState &PFS,
704 const SlotMapping &IRSlots,
705 SMDiagnostic &Error) {
706 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseNamedRegister(Reg);
707}