blob: 498ca0615d5a6b9cf41d9b0847964ecdb11f6111 [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 Lorenzf4baeb52015-07-21 22:28:27 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000024#include "llvm/IR/Instructions.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000025#include "llvm/IR/Module.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000026#include "llvm/Support/raw_ostream.h"
27#include "llvm/Support/SourceMgr.h"
28#include "llvm/Target/TargetSubtargetInfo.h"
29#include "llvm/Target/TargetInstrInfo.h"
30
31using namespace llvm;
32
33namespace {
34
Alex Lorenzb29554d2015-07-20 20:31:01 +000035struct StringValueUtility {
36 StringRef String;
37 std::string UnescapedString;
38
39 StringValueUtility(const MIToken &Token) {
40 if (Token.isStringValueQuoted()) {
41 Token.unescapeQuotedStringValue(UnescapedString);
42 String = UnescapedString;
43 return;
44 }
45 String = Token.stringValue();
46 }
47
48 operator StringRef() const { return String; }
49};
50
Alex Lorenz36962cd2015-07-07 02:08:46 +000051/// A wrapper struct around the 'MachineOperand' struct that includes a source
52/// range.
53struct MachineOperandWithLocation {
54 MachineOperand Operand;
55 StringRef::iterator Begin;
56 StringRef::iterator End;
57
58 MachineOperandWithLocation(const MachineOperand &Operand,
59 StringRef::iterator Begin, StringRef::iterator End)
60 : Operand(Operand), Begin(Begin), End(End) {}
61};
62
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000063class MIParser {
64 SourceMgr &SM;
65 MachineFunction &MF;
66 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000067 StringRef Source, CurrentSource;
68 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000069 const PerFunctionMIParsingState &PFS;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000070 /// Maps from indices to unnamed global values and metadata nodes.
71 const SlotMapping &IRSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000072 /// Maps from instruction names to op codes.
73 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000074 /// Maps from register names to registers.
75 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000076 /// Maps from register mask names to register masks.
77 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000078 /// Maps from subregister names to subregister indices.
79 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000080
81public:
82 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000083 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000084 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000085
Alex Lorenz91370c52015-06-22 20:37:46 +000086 void lex();
87
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000088 /// Report an error at the current location with the given message.
89 ///
90 /// This function always return true.
91 bool error(const Twine &Msg);
92
Alex Lorenz91370c52015-06-22 20:37:46 +000093 /// Report an error at the given location with the given message.
94 ///
95 /// This function always return true.
96 bool error(StringRef::iterator Loc, const Twine &Msg);
97
Alex Lorenz3708a642015-06-30 17:47:50 +000098 bool parse(MachineInstr *&MI);
Alex Lorenzf09df002015-06-30 18:16:42 +000099 bool parseMBB(MachineBasicBlock *&MBB);
Alex Lorenz9fab3702015-07-14 21:24:41 +0000100 bool parseNamedRegister(unsigned &Reg);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000101
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000102 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000103 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000104 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000105 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000106 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000107 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000108 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000109 bool parseStackObjectOperand(MachineOperand &Dest);
110 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000111 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000112 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000113 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000114 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000115 bool parseCFIOffset(int &Offset);
116 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000117 bool parseMachineOperand(MachineOperand &Dest);
118
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000119private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000120 /// Convert the integer literal in the current token into an unsigned integer.
121 ///
122 /// Return true if an error occurred.
123 bool getUnsigned(unsigned &Result);
124
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000125 void initNames2InstrOpCodes();
126
127 /// Try to convert an instruction name to an opcode. Return true if the
128 /// instruction name is invalid.
129 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000130
Alex Lorenze5a44662015-07-17 00:24:15 +0000131 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000132
Alex Lorenz36962cd2015-07-07 02:08:46 +0000133 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
134 const MCInstrDesc &MCID);
135
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000136 void initNames2Regs();
137
138 /// Try to convert a register name to a register number. Return true if the
139 /// register name is invalid.
140 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000141
142 void initNames2RegMasks();
143
144 /// Check if the given identifier is a name of a register mask.
145 ///
146 /// Return null if the identifier isn't a register mask.
147 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000148
149 void initNames2SubRegIndices();
150
151 /// Check if the given identifier is a name of a subregister index.
152 ///
153 /// Return 0 if the name isn't a subregister index class.
154 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000155};
156
157} // end anonymous namespace
158
159MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000160 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000161 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000162 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000163 Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000164
Alex Lorenz91370c52015-06-22 20:37:46 +0000165void MIParser::lex() {
166 CurrentSource = lexMIToken(
167 CurrentSource, Token,
168 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
169}
170
171bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
172
173bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000174 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
175 Error = SMDiagnostic(
176 SM, SMLoc(),
177 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
178 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000179 return true;
180}
181
Alex Lorenz3708a642015-06-30 17:47:50 +0000182bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000183 lex();
184
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000185 // Parse any register operands before '='
186 // TODO: Allow parsing of multiple operands before '='
187 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000188 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000189 if (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000190 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000191 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000192 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000193 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenz3708a642015-06-30 17:47:50 +0000194 if (Token.isNot(MIToken::equal))
195 return error("expected '='");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000196 lex();
197 }
198
Alex Lorenze5a44662015-07-17 00:24:15 +0000199 unsigned OpCode, Flags = 0;
200 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000201 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000202
Alex Lorenze5a44662015-07-17 00:24:15 +0000203 // TODO: Parse the bundle instruction flags and memory operands.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000204
205 // Parse the remaining machine operands.
206 while (Token.isNot(MIToken::Eof)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000207 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000208 if (parseMachineOperand(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000209 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000210 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000211 if (Token.is(MIToken::Eof))
212 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000213 if (Token.isNot(MIToken::comma))
214 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000215 lex();
216 }
217
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000218 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000219 if (!MCID.isVariadic()) {
220 // FIXME: Move the implicit operand verification to the machine verifier.
221 if (verifyImplicitOperands(Operands, MCID))
222 return true;
223 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000224
Alex Lorenzcb268d42015-07-06 23:07:26 +0000225 // TODO: Check for extraneous machine operands.
Alex Lorenz3708a642015-06-30 17:47:50 +0000226 MI = MF.CreateMachineInstr(MCID, DebugLoc(), /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000227 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000228 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000229 MI->addOperand(MF, Operand.Operand);
Alex Lorenz3708a642015-06-30 17:47:50 +0000230 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000231}
232
Alex Lorenzf09df002015-06-30 18:16:42 +0000233bool MIParser::parseMBB(MachineBasicBlock *&MBB) {
234 lex();
235 if (Token.isNot(MIToken::MachineBasicBlock))
236 return error("expected a machine basic block reference");
237 if (parseMBBReference(MBB))
238 return true;
239 lex();
240 if (Token.isNot(MIToken::Eof))
241 return error(
242 "expected end of string after the machine basic block reference");
243 return false;
244}
245
Alex Lorenz9fab3702015-07-14 21:24:41 +0000246bool MIParser::parseNamedRegister(unsigned &Reg) {
247 lex();
248 if (Token.isNot(MIToken::NamedRegister))
249 return error("expected a named register");
250 if (parseRegister(Reg))
251 return 0;
252 lex();
253 if (Token.isNot(MIToken::Eof))
254 return error("expected end of string after the register reference");
255 return false;
256}
257
Alex Lorenz36962cd2015-07-07 02:08:46 +0000258static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
259 assert(MO.isImplicit());
260 return MO.isDef() ? "implicit-def" : "implicit";
261}
262
263static std::string getRegisterName(const TargetRegisterInfo *TRI,
264 unsigned Reg) {
265 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
266 return StringRef(TRI->getName(Reg)).lower();
267}
268
269bool MIParser::verifyImplicitOperands(
270 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
271 if (MCID.isCall())
272 // We can't verify call instructions as they can contain arbitrary implicit
273 // register and register mask operands.
274 return false;
275
276 // Gather all the expected implicit operands.
277 SmallVector<MachineOperand, 4> ImplicitOperands;
278 if (MCID.ImplicitDefs)
279 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
280 ImplicitOperands.push_back(
281 MachineOperand::CreateReg(*ImpDefs, true, true));
282 if (MCID.ImplicitUses)
283 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
284 ImplicitOperands.push_back(
285 MachineOperand::CreateReg(*ImpUses, false, true));
286
287 const auto *TRI = MF.getSubtarget().getRegisterInfo();
288 assert(TRI && "Expected target register info");
289 size_t I = ImplicitOperands.size(), J = Operands.size();
290 while (I) {
291 --I;
292 if (J) {
293 --J;
294 const auto &ImplicitOperand = ImplicitOperands[I];
295 const auto &Operand = Operands[J].Operand;
296 if (ImplicitOperand.isIdenticalTo(Operand))
297 continue;
298 if (Operand.isReg() && Operand.isImplicit()) {
299 return error(Operands[J].Begin,
300 Twine("expected an implicit register operand '") +
301 printImplicitRegisterFlag(ImplicitOperand) + " %" +
302 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
303 }
304 }
305 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
306 // insead of reporting an error at this location:
307 // %eax = MOV32r0
308 // ^
309 // report the error at the following location:
310 // %eax = MOV32r0
311 // ^
312 return error(J < Operands.size() ? Operands[J].End : Token.location(),
313 Twine("missing implicit register operand '") +
314 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
315 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
316 }
317 return false;
318}
319
Alex Lorenze5a44662015-07-17 00:24:15 +0000320bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
321 if (Token.is(MIToken::kw_frame_setup)) {
322 Flags |= MachineInstr::FrameSetup;
323 lex();
324 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000325 if (Token.isNot(MIToken::Identifier))
326 return error("expected a machine instruction");
327 StringRef InstrName = Token.stringValue();
328 if (parseInstrName(InstrName, OpCode))
329 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000330 lex();
331 return false;
332}
333
334bool MIParser::parseRegister(unsigned &Reg) {
335 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000336 case MIToken::underscore:
337 Reg = 0;
338 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000339 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000340 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000341 if (getRegisterByName(Name, Reg))
342 return error(Twine("unknown register name '") + Name + "'");
343 break;
344 }
Alex Lorenz53464512015-07-10 22:51:20 +0000345 case MIToken::VirtualRegister: {
346 unsigned ID;
347 if (getUnsigned(ID))
348 return true;
349 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
350 if (RegInfo == PFS.VirtualRegisterSlots.end())
351 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
352 "'");
353 Reg = RegInfo->second;
354 break;
355 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000356 // TODO: Parse other register kinds.
357 default:
358 llvm_unreachable("The current token should be a register");
359 }
360 return false;
361}
362
Alex Lorenzcb268d42015-07-06 23:07:26 +0000363bool MIParser::parseRegisterFlag(unsigned &Flags) {
364 switch (Token.kind()) {
365 case MIToken::kw_implicit:
366 Flags |= RegState::Implicit;
367 break;
368 case MIToken::kw_implicit_define:
369 Flags |= RegState::ImplicitDefine;
370 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000371 case MIToken::kw_dead:
372 Flags |= RegState::Dead;
373 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000374 case MIToken::kw_killed:
375 Flags |= RegState::Kill;
376 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000377 case MIToken::kw_undef:
378 Flags |= RegState::Undef;
379 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000380 // TODO: report an error when we specify the same flag more than once.
381 // TODO: parse the other register flags.
382 default:
383 llvm_unreachable("The current token should be a register flag");
384 }
385 lex();
386 return false;
387}
388
Alex Lorenz2eacca82015-07-13 23:24:34 +0000389bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
390 assert(Token.is(MIToken::colon));
391 lex();
392 if (Token.isNot(MIToken::Identifier))
393 return error("expected a subregister index after ':'");
394 auto Name = Token.stringValue();
395 SubReg = getSubRegIndex(Name);
396 if (!SubReg)
397 return error(Twine("use of unknown subregister index '") + Name + "'");
398 lex();
399 return false;
400}
401
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000402bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
403 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000404 unsigned Flags = IsDef ? RegState::Define : 0;
405 while (Token.isRegisterFlag()) {
406 if (parseRegisterFlag(Flags))
407 return true;
408 }
409 if (!Token.isRegister())
410 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000411 if (parseRegister(Reg))
412 return true;
413 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000414 unsigned SubReg = 0;
415 if (Token.is(MIToken::colon)) {
416 if (parseSubRegisterIndex(SubReg))
417 return true;
418 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000419 Dest = MachineOperand::CreateReg(
420 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000421 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
422 /*isEarlyClobber=*/false, SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000423 return false;
424}
425
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000426bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
427 assert(Token.is(MIToken::IntegerLiteral));
428 const APSInt &Int = Token.integerValue();
429 if (Int.getMinSignedBits() > 64)
430 // TODO: Replace this with an error when we can parse CIMM Machine Operands.
431 llvm_unreachable("Can't parse large integer literals yet!");
432 Dest = MachineOperand::CreateImm(Int.getExtValue());
433 lex();
434 return false;
435}
436
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000437bool MIParser::getUnsigned(unsigned &Result) {
438 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
439 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
440 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
441 if (Val64 == Limit)
442 return error("expected 32-bit integer (too large)");
443 Result = Val64;
444 return false;
445}
446
Alex Lorenzf09df002015-06-30 18:16:42 +0000447bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000448 assert(Token.is(MIToken::MachineBasicBlock));
449 unsigned Number;
450 if (getUnsigned(Number))
451 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000452 auto MBBInfo = PFS.MBBSlots.find(Number);
453 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000454 return error(Twine("use of undefined machine basic block #") +
455 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000456 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000457 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
458 return error(Twine("the name of machine basic block #") + Twine(Number) +
459 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000460 return false;
461}
462
463bool MIParser::parseMBBOperand(MachineOperand &Dest) {
464 MachineBasicBlock *MBB;
465 if (parseMBBReference(MBB))
466 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000467 Dest = MachineOperand::CreateMBB(MBB);
468 lex();
469 return false;
470}
471
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000472bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
473 assert(Token.is(MIToken::StackObject));
474 unsigned ID;
475 if (getUnsigned(ID))
476 return true;
477 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
478 if (ObjectInfo == PFS.StackObjectSlots.end())
479 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
480 "'");
481 StringRef Name;
482 if (const auto *Alloca =
483 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
484 Name = Alloca->getName();
485 if (!Token.stringValue().empty() && Token.stringValue() != Name)
486 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
487 "' isn't '" + Token.stringValue() + "'");
488 lex();
489 Dest = MachineOperand::CreateFI(ObjectInfo->second);
490 return false;
491}
492
493bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
494 assert(Token.is(MIToken::FixedStackObject));
495 unsigned ID;
496 if (getUnsigned(ID))
497 return true;
498 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
499 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
500 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
501 Twine(ID) + "'");
502 lex();
503 Dest = MachineOperand::CreateFI(ObjectInfo->second);
504 return false;
505}
506
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000507bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
508 switch (Token.kind()) {
Alex Lorenzb29554d2015-07-20 20:31:01 +0000509 case MIToken::NamedGlobalValue:
510 case MIToken::QuotedNamedGlobalValue: {
511 StringValueUtility Name(Token);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000512 const Module *M = MF.getFunction()->getParent();
513 if (const auto *GV = M->getNamedValue(Name)) {
514 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
515 break;
516 }
Alex Lorenzb29554d2015-07-20 20:31:01 +0000517 return error(Twine("use of undefined global value '@") +
518 Token.rawStringValue() + "'");
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000519 }
520 case MIToken::GlobalValue: {
521 unsigned GVIdx;
522 if (getUnsigned(GVIdx))
523 return true;
524 if (GVIdx >= IRSlots.GlobalValues.size())
525 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
526 "'");
527 Dest = MachineOperand::CreateGA(IRSlots.GlobalValues[GVIdx],
528 /*Offset=*/0);
529 break;
530 }
531 default:
532 llvm_unreachable("The current token should be a global value");
533 }
534 // TODO: Parse offset and target flags.
535 lex();
536 return false;
537}
538
Alex Lorenzab980492015-07-20 20:51:18 +0000539bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
540 assert(Token.is(MIToken::ConstantPoolItem));
541 unsigned ID;
542 if (getUnsigned(ID))
543 return true;
544 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
545 if (ConstantInfo == PFS.ConstantPoolSlots.end())
546 return error("use of undefined constant '%const." + Twine(ID) + "'");
547 lex();
548 // TODO: Parse offset and target flags.
549 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
550 return false;
551}
552
Alex Lorenz31d70682015-07-15 23:38:35 +0000553bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
554 assert(Token.is(MIToken::JumpTableIndex));
555 unsigned ID;
556 if (getUnsigned(ID))
557 return true;
558 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
559 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
560 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
561 lex();
562 // TODO: Parse target flags.
563 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
564 return false;
565}
566
Alex Lorenz6ede3742015-07-21 16:59:53 +0000567bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
568 assert(Token.is(MIToken::ExternalSymbol) ||
569 Token.is(MIToken::QuotedExternalSymbol));
570 StringValueUtility Name(Token);
571 const char *Symbol = MF.createExternalSymbolName(Name);
572 lex();
573 // TODO: Parse the target flags.
574 Dest = MachineOperand::CreateES(Symbol);
575 return false;
576}
577
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000578bool MIParser::parseCFIOffset(int &Offset) {
579 if (Token.isNot(MIToken::IntegerLiteral))
580 return error("expected a cfi offset");
581 if (Token.integerValue().getMinSignedBits() > 32)
582 return error("expected a 32 bit integer (the cfi offset is too large)");
583 Offset = (int)Token.integerValue().getExtValue();
584 lex();
585 return false;
586}
587
588bool MIParser::parseCFIOperand(MachineOperand &Dest) {
589 // TODO: Parse the other CFI operands.
590 assert(Token.is(MIToken::kw_cfi_def_cfa_offset));
591 lex();
592 int Offset;
593 if (parseCFIOffset(Offset))
594 return true;
595 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
596 Dest = MachineOperand::CreateCFIIndex(MF.getMMI().addFrameInst(
597 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset)));
598 return false;
599}
600
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000601bool MIParser::parseMachineOperand(MachineOperand &Dest) {
602 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000603 case MIToken::kw_implicit:
604 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000605 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000606 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000607 case MIToken::kw_undef:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000608 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000609 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000610 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000611 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000612 case MIToken::IntegerLiteral:
613 return parseImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000614 case MIToken::MachineBasicBlock:
615 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000616 case MIToken::StackObject:
617 return parseStackObjectOperand(Dest);
618 case MIToken::FixedStackObject:
619 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000620 case MIToken::GlobalValue:
621 case MIToken::NamedGlobalValue:
Alex Lorenzb29554d2015-07-20 20:31:01 +0000622 case MIToken::QuotedNamedGlobalValue:
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000623 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000624 case MIToken::ConstantPoolItem:
625 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000626 case MIToken::JumpTableIndex:
627 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000628 case MIToken::ExternalSymbol:
629 case MIToken::QuotedExternalSymbol:
630 return parseExternalSymbolOperand(Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000631 case MIToken::kw_cfi_def_cfa_offset:
632 return parseCFIOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000633 case MIToken::Error:
634 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000635 case MIToken::Identifier:
636 if (const auto *RegMask = getRegMask(Token.stringValue())) {
637 Dest = MachineOperand::CreateRegMask(RegMask);
638 lex();
639 break;
640 }
641 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000642 default:
643 // TODO: parse the other machine operands.
644 return error("expected a machine operand");
645 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000646 return false;
647}
648
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000649void MIParser::initNames2InstrOpCodes() {
650 if (!Names2InstrOpCodes.empty())
651 return;
652 const auto *TII = MF.getSubtarget().getInstrInfo();
653 assert(TII && "Expected target instruction info");
654 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
655 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
656}
657
658bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
659 initNames2InstrOpCodes();
660 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
661 if (InstrInfo == Names2InstrOpCodes.end())
662 return true;
663 OpCode = InstrInfo->getValue();
664 return false;
665}
666
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000667void MIParser::initNames2Regs() {
668 if (!Names2Regs.empty())
669 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +0000670 // The '%noreg' register is the register 0.
671 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000672 const auto *TRI = MF.getSubtarget().getRegisterInfo();
673 assert(TRI && "Expected target register info");
674 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
675 bool WasInserted =
676 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
677 .second;
678 (void)WasInserted;
679 assert(WasInserted && "Expected registers to be unique case-insensitively");
680 }
681}
682
683bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
684 initNames2Regs();
685 auto RegInfo = Names2Regs.find(RegName);
686 if (RegInfo == Names2Regs.end())
687 return true;
688 Reg = RegInfo->getValue();
689 return false;
690}
691
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000692void MIParser::initNames2RegMasks() {
693 if (!Names2RegMasks.empty())
694 return;
695 const auto *TRI = MF.getSubtarget().getRegisterInfo();
696 assert(TRI && "Expected target register info");
697 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
698 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
699 assert(RegMasks.size() == RegMaskNames.size());
700 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
701 Names2RegMasks.insert(
702 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
703}
704
705const uint32_t *MIParser::getRegMask(StringRef Identifier) {
706 initNames2RegMasks();
707 auto RegMaskInfo = Names2RegMasks.find(Identifier);
708 if (RegMaskInfo == Names2RegMasks.end())
709 return nullptr;
710 return RegMaskInfo->getValue();
711}
712
Alex Lorenz2eacca82015-07-13 23:24:34 +0000713void MIParser::initNames2SubRegIndices() {
714 if (!Names2SubRegIndices.empty())
715 return;
716 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
717 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
718 Names2SubRegIndices.insert(
719 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
720}
721
722unsigned MIParser::getSubRegIndex(StringRef Name) {
723 initNames2SubRegIndices();
724 auto SubRegInfo = Names2SubRegIndices.find(Name);
725 if (SubRegInfo == Names2SubRegIndices.end())
726 return 0;
727 return SubRegInfo->getValue();
728}
729
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000730bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
731 MachineFunction &MF, StringRef Src,
732 const PerFunctionMIParsingState &PFS,
733 const SlotMapping &IRSlots, SMDiagnostic &Error) {
734 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000735}
Alex Lorenzf09df002015-06-30 18:16:42 +0000736
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000737bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
738 MachineFunction &MF, StringRef Src,
739 const PerFunctionMIParsingState &PFS,
740 const SlotMapping &IRSlots, SMDiagnostic &Error) {
741 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000742}
Alex Lorenz9fab3702015-07-14 21:24:41 +0000743
744bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
745 MachineFunction &MF, StringRef Src,
746 const PerFunctionMIParsingState &PFS,
747 const SlotMapping &IRSlots,
748 SMDiagnostic &Error) {
749 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseNamedRegister(Reg);
750}