blob: 656f23143151c1d3ff75959879dbdc047adb5179 [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 Lorenz44f29252015-07-22 21:07:04 +0000115 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000116 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000117 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000118 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000119 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000120 bool parseMachineOperand(MachineOperand &Dest);
121
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000122private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000123 /// Convert the integer literal in the current token into an unsigned integer.
124 ///
125 /// Return true if an error occurred.
126 bool getUnsigned(unsigned &Result);
127
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000128 /// If the current token is of the given kind, consume it and return false.
129 /// Otherwise report an error and return true.
130 bool expectAndConsume(MIToken::TokenKind TokenKind);
131
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000132 void initNames2InstrOpCodes();
133
134 /// Try to convert an instruction name to an opcode. Return true if the
135 /// instruction name is invalid.
136 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000137
Alex Lorenze5a44662015-07-17 00:24:15 +0000138 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000139
Alex Lorenz36962cd2015-07-07 02:08:46 +0000140 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
141 const MCInstrDesc &MCID);
142
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000143 void initNames2Regs();
144
145 /// Try to convert a register name to a register number. Return true if the
146 /// register name is invalid.
147 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000148
149 void initNames2RegMasks();
150
151 /// Check if the given identifier is a name of a register mask.
152 ///
153 /// Return null if the identifier isn't a register mask.
154 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000155
156 void initNames2SubRegIndices();
157
158 /// Check if the given identifier is a name of a subregister index.
159 ///
160 /// Return 0 if the name isn't a subregister index class.
161 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000162};
163
164} // end anonymous namespace
165
166MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000167 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000168 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000169 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000170 Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000171
Alex Lorenz91370c52015-06-22 20:37:46 +0000172void MIParser::lex() {
173 CurrentSource = lexMIToken(
174 CurrentSource, Token,
175 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
176}
177
178bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
179
180bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000181 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
182 Error = SMDiagnostic(
183 SM, SMLoc(),
184 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
185 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000186 return true;
187}
188
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000189static const char *toString(MIToken::TokenKind TokenKind) {
190 switch (TokenKind) {
191 case MIToken::comma:
192 return "','";
193 default:
194 return "<unknown token>";
195 }
196}
197
198bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
199 if (Token.isNot(TokenKind))
200 return error(Twine("expected ") + toString(TokenKind));
201 lex();
202 return false;
203}
204
Alex Lorenz3708a642015-06-30 17:47:50 +0000205bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000206 lex();
207
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000208 // Parse any register operands before '='
209 // TODO: Allow parsing of multiple operands before '='
210 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000211 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000212 if (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000213 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000214 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000215 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000216 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenz3708a642015-06-30 17:47:50 +0000217 if (Token.isNot(MIToken::equal))
218 return error("expected '='");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000219 lex();
220 }
221
Alex Lorenze5a44662015-07-17 00:24:15 +0000222 unsigned OpCode, Flags = 0;
223 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000224 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000225
Alex Lorenze5a44662015-07-17 00:24:15 +0000226 // TODO: Parse the bundle instruction flags and memory operands.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000227
228 // Parse the remaining machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000229 while (Token.isNot(MIToken::Eof) && Token.isNot(MIToken::kw_debug_location)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000230 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000231 if (parseMachineOperand(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000232 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000233 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000234 if (Token.is(MIToken::Eof))
235 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000236 if (Token.isNot(MIToken::comma))
237 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000238 lex();
239 }
240
Alex Lorenz46d760d2015-07-22 21:15:11 +0000241 DebugLoc DebugLocation;
242 if (Token.is(MIToken::kw_debug_location)) {
243 lex();
244 if (Token.isNot(MIToken::exclaim))
245 return error("expected a metadata node after 'debug-location'");
246 MDNode *Node = nullptr;
247 if (parseMDNode(Node))
248 return true;
249 DebugLocation = DebugLoc(Node);
250 }
251
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000252 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000253 if (!MCID.isVariadic()) {
254 // FIXME: Move the implicit operand verification to the machine verifier.
255 if (verifyImplicitOperands(Operands, MCID))
256 return true;
257 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000258
Alex Lorenzcb268d42015-07-06 23:07:26 +0000259 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000260 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000261 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000262 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000263 MI->addOperand(MF, Operand.Operand);
Alex Lorenz3708a642015-06-30 17:47:50 +0000264 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000265}
266
Alex Lorenzf09df002015-06-30 18:16:42 +0000267bool MIParser::parseMBB(MachineBasicBlock *&MBB) {
268 lex();
269 if (Token.isNot(MIToken::MachineBasicBlock))
270 return error("expected a machine basic block reference");
271 if (parseMBBReference(MBB))
272 return true;
273 lex();
274 if (Token.isNot(MIToken::Eof))
275 return error(
276 "expected end of string after the machine basic block reference");
277 return false;
278}
279
Alex Lorenz9fab3702015-07-14 21:24:41 +0000280bool MIParser::parseNamedRegister(unsigned &Reg) {
281 lex();
282 if (Token.isNot(MIToken::NamedRegister))
283 return error("expected a named register");
284 if (parseRegister(Reg))
285 return 0;
286 lex();
287 if (Token.isNot(MIToken::Eof))
288 return error("expected end of string after the register reference");
289 return false;
290}
291
Alex Lorenz36962cd2015-07-07 02:08:46 +0000292static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
293 assert(MO.isImplicit());
294 return MO.isDef() ? "implicit-def" : "implicit";
295}
296
297static std::string getRegisterName(const TargetRegisterInfo *TRI,
298 unsigned Reg) {
299 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
300 return StringRef(TRI->getName(Reg)).lower();
301}
302
303bool MIParser::verifyImplicitOperands(
304 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
305 if (MCID.isCall())
306 // We can't verify call instructions as they can contain arbitrary implicit
307 // register and register mask operands.
308 return false;
309
310 // Gather all the expected implicit operands.
311 SmallVector<MachineOperand, 4> ImplicitOperands;
312 if (MCID.ImplicitDefs)
313 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
314 ImplicitOperands.push_back(
315 MachineOperand::CreateReg(*ImpDefs, true, true));
316 if (MCID.ImplicitUses)
317 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
318 ImplicitOperands.push_back(
319 MachineOperand::CreateReg(*ImpUses, false, true));
320
321 const auto *TRI = MF.getSubtarget().getRegisterInfo();
322 assert(TRI && "Expected target register info");
323 size_t I = ImplicitOperands.size(), J = Operands.size();
324 while (I) {
325 --I;
326 if (J) {
327 --J;
328 const auto &ImplicitOperand = ImplicitOperands[I];
329 const auto &Operand = Operands[J].Operand;
330 if (ImplicitOperand.isIdenticalTo(Operand))
331 continue;
332 if (Operand.isReg() && Operand.isImplicit()) {
333 return error(Operands[J].Begin,
334 Twine("expected an implicit register operand '") +
335 printImplicitRegisterFlag(ImplicitOperand) + " %" +
336 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
337 }
338 }
339 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
340 // insead of reporting an error at this location:
341 // %eax = MOV32r0
342 // ^
343 // report the error at the following location:
344 // %eax = MOV32r0
345 // ^
346 return error(J < Operands.size() ? Operands[J].End : Token.location(),
347 Twine("missing implicit register operand '") +
348 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
349 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
350 }
351 return false;
352}
353
Alex Lorenze5a44662015-07-17 00:24:15 +0000354bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
355 if (Token.is(MIToken::kw_frame_setup)) {
356 Flags |= MachineInstr::FrameSetup;
357 lex();
358 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000359 if (Token.isNot(MIToken::Identifier))
360 return error("expected a machine instruction");
361 StringRef InstrName = Token.stringValue();
362 if (parseInstrName(InstrName, OpCode))
363 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000364 lex();
365 return false;
366}
367
368bool MIParser::parseRegister(unsigned &Reg) {
369 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000370 case MIToken::underscore:
371 Reg = 0;
372 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000373 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000374 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000375 if (getRegisterByName(Name, Reg))
376 return error(Twine("unknown register name '") + Name + "'");
377 break;
378 }
Alex Lorenz53464512015-07-10 22:51:20 +0000379 case MIToken::VirtualRegister: {
380 unsigned ID;
381 if (getUnsigned(ID))
382 return true;
383 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
384 if (RegInfo == PFS.VirtualRegisterSlots.end())
385 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
386 "'");
387 Reg = RegInfo->second;
388 break;
389 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000390 // TODO: Parse other register kinds.
391 default:
392 llvm_unreachable("The current token should be a register");
393 }
394 return false;
395}
396
Alex Lorenzcb268d42015-07-06 23:07:26 +0000397bool MIParser::parseRegisterFlag(unsigned &Flags) {
398 switch (Token.kind()) {
399 case MIToken::kw_implicit:
400 Flags |= RegState::Implicit;
401 break;
402 case MIToken::kw_implicit_define:
403 Flags |= RegState::ImplicitDefine;
404 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000405 case MIToken::kw_dead:
406 Flags |= RegState::Dead;
407 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000408 case MIToken::kw_killed:
409 Flags |= RegState::Kill;
410 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000411 case MIToken::kw_undef:
412 Flags |= RegState::Undef;
413 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000414 // TODO: report an error when we specify the same flag more than once.
415 // TODO: parse the other register flags.
416 default:
417 llvm_unreachable("The current token should be a register flag");
418 }
419 lex();
420 return false;
421}
422
Alex Lorenz2eacca82015-07-13 23:24:34 +0000423bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
424 assert(Token.is(MIToken::colon));
425 lex();
426 if (Token.isNot(MIToken::Identifier))
427 return error("expected a subregister index after ':'");
428 auto Name = Token.stringValue();
429 SubReg = getSubRegIndex(Name);
430 if (!SubReg)
431 return error(Twine("use of unknown subregister index '") + Name + "'");
432 lex();
433 return false;
434}
435
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000436bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
437 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000438 unsigned Flags = IsDef ? RegState::Define : 0;
439 while (Token.isRegisterFlag()) {
440 if (parseRegisterFlag(Flags))
441 return true;
442 }
443 if (!Token.isRegister())
444 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000445 if (parseRegister(Reg))
446 return true;
447 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000448 unsigned SubReg = 0;
449 if (Token.is(MIToken::colon)) {
450 if (parseSubRegisterIndex(SubReg))
451 return true;
452 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000453 Dest = MachineOperand::CreateReg(
454 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000455 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
456 /*isEarlyClobber=*/false, SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000457 return false;
458}
459
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000460bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
461 assert(Token.is(MIToken::IntegerLiteral));
462 const APSInt &Int = Token.integerValue();
463 if (Int.getMinSignedBits() > 64)
464 // TODO: Replace this with an error when we can parse CIMM Machine Operands.
465 llvm_unreachable("Can't parse large integer literals yet!");
466 Dest = MachineOperand::CreateImm(Int.getExtValue());
467 lex();
468 return false;
469}
470
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000471bool MIParser::getUnsigned(unsigned &Result) {
472 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
473 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
474 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
475 if (Val64 == Limit)
476 return error("expected 32-bit integer (too large)");
477 Result = Val64;
478 return false;
479}
480
Alex Lorenzf09df002015-06-30 18:16:42 +0000481bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000482 assert(Token.is(MIToken::MachineBasicBlock));
483 unsigned Number;
484 if (getUnsigned(Number))
485 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000486 auto MBBInfo = PFS.MBBSlots.find(Number);
487 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000488 return error(Twine("use of undefined machine basic block #") +
489 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000490 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000491 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
492 return error(Twine("the name of machine basic block #") + Twine(Number) +
493 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000494 return false;
495}
496
497bool MIParser::parseMBBOperand(MachineOperand &Dest) {
498 MachineBasicBlock *MBB;
499 if (parseMBBReference(MBB))
500 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000501 Dest = MachineOperand::CreateMBB(MBB);
502 lex();
503 return false;
504}
505
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000506bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
507 assert(Token.is(MIToken::StackObject));
508 unsigned ID;
509 if (getUnsigned(ID))
510 return true;
511 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
512 if (ObjectInfo == PFS.StackObjectSlots.end())
513 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
514 "'");
515 StringRef Name;
516 if (const auto *Alloca =
517 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
518 Name = Alloca->getName();
519 if (!Token.stringValue().empty() && Token.stringValue() != Name)
520 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
521 "' isn't '" + Token.stringValue() + "'");
522 lex();
523 Dest = MachineOperand::CreateFI(ObjectInfo->second);
524 return false;
525}
526
527bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
528 assert(Token.is(MIToken::FixedStackObject));
529 unsigned ID;
530 if (getUnsigned(ID))
531 return true;
532 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
533 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
534 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
535 Twine(ID) + "'");
536 lex();
537 Dest = MachineOperand::CreateFI(ObjectInfo->second);
538 return false;
539}
540
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000541bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
542 switch (Token.kind()) {
Alex Lorenzb29554d2015-07-20 20:31:01 +0000543 case MIToken::NamedGlobalValue:
544 case MIToken::QuotedNamedGlobalValue: {
545 StringValueUtility Name(Token);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000546 const Module *M = MF.getFunction()->getParent();
547 if (const auto *GV = M->getNamedValue(Name)) {
548 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
549 break;
550 }
Alex Lorenzb29554d2015-07-20 20:31:01 +0000551 return error(Twine("use of undefined global value '@") +
552 Token.rawStringValue() + "'");
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000553 }
554 case MIToken::GlobalValue: {
555 unsigned GVIdx;
556 if (getUnsigned(GVIdx))
557 return true;
558 if (GVIdx >= IRSlots.GlobalValues.size())
559 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
560 "'");
561 Dest = MachineOperand::CreateGA(IRSlots.GlobalValues[GVIdx],
562 /*Offset=*/0);
563 break;
564 }
565 default:
566 llvm_unreachable("The current token should be a global value");
567 }
568 // TODO: Parse offset and target flags.
569 lex();
570 return false;
571}
572
Alex Lorenzab980492015-07-20 20:51:18 +0000573bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
574 assert(Token.is(MIToken::ConstantPoolItem));
575 unsigned ID;
576 if (getUnsigned(ID))
577 return true;
578 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
579 if (ConstantInfo == PFS.ConstantPoolSlots.end())
580 return error("use of undefined constant '%const." + Twine(ID) + "'");
581 lex();
582 // TODO: Parse offset and target flags.
583 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
584 return false;
585}
586
Alex Lorenz31d70682015-07-15 23:38:35 +0000587bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
588 assert(Token.is(MIToken::JumpTableIndex));
589 unsigned ID;
590 if (getUnsigned(ID))
591 return true;
592 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
593 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
594 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
595 lex();
596 // TODO: Parse target flags.
597 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
598 return false;
599}
600
Alex Lorenz6ede3742015-07-21 16:59:53 +0000601bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
602 assert(Token.is(MIToken::ExternalSymbol) ||
603 Token.is(MIToken::QuotedExternalSymbol));
604 StringValueUtility Name(Token);
605 const char *Symbol = MF.createExternalSymbolName(Name);
606 lex();
607 // TODO: Parse the target flags.
608 Dest = MachineOperand::CreateES(Symbol);
609 return false;
610}
611
Alex Lorenz44f29252015-07-22 21:07:04 +0000612bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +0000613 assert(Token.is(MIToken::exclaim));
614 auto Loc = Token.location();
615 lex();
616 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
617 return error("expected metadata id after '!'");
618 unsigned ID;
619 if (getUnsigned(ID))
620 return true;
621 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
622 if (NodeInfo == IRSlots.MetadataNodes.end())
623 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
624 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +0000625 Node = NodeInfo->second.get();
626 return false;
627}
628
629bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
630 MDNode *Node = nullptr;
631 if (parseMDNode(Node))
632 return true;
633 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000634 return false;
635}
636
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000637bool MIParser::parseCFIOffset(int &Offset) {
638 if (Token.isNot(MIToken::IntegerLiteral))
639 return error("expected a cfi offset");
640 if (Token.integerValue().getMinSignedBits() > 32)
641 return error("expected a 32 bit integer (the cfi offset is too large)");
642 Offset = (int)Token.integerValue().getExtValue();
643 lex();
644 return false;
645}
646
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000647bool MIParser::parseCFIRegister(unsigned &Reg) {
648 if (Token.isNot(MIToken::NamedRegister))
649 return error("expected a cfi register");
650 unsigned LLVMReg;
651 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000652 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000653 const auto *TRI = MF.getSubtarget().getRegisterInfo();
654 assert(TRI && "Expected target register info");
655 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
656 if (DwarfReg < 0)
657 return error("invalid DWARF register");
658 Reg = (unsigned)DwarfReg;
659 lex();
660 return false;
661}
662
663bool MIParser::parseCFIOperand(MachineOperand &Dest) {
664 auto Kind = Token.kind();
665 lex();
666 auto &MMI = MF.getMMI();
667 int Offset;
668 unsigned Reg;
669 unsigned CFIIndex;
670 switch (Kind) {
671 case MIToken::kw_cfi_offset:
672 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
673 parseCFIOffset(Offset))
674 return true;
675 CFIIndex =
676 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
677 break;
678 case MIToken::kw_cfi_def_cfa_offset:
679 if (parseCFIOffset(Offset))
680 return true;
681 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
682 CFIIndex = MMI.addFrameInst(
683 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
684 break;
685 default:
686 // TODO: Parse the other CFI operands.
687 llvm_unreachable("The current token should be a cfi operand");
688 }
689 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000690 return false;
691}
692
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000693bool MIParser::parseMachineOperand(MachineOperand &Dest) {
694 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000695 case MIToken::kw_implicit:
696 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000697 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000698 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000699 case MIToken::kw_undef:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000700 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000701 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000702 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000703 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000704 case MIToken::IntegerLiteral:
705 return parseImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000706 case MIToken::MachineBasicBlock:
707 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000708 case MIToken::StackObject:
709 return parseStackObjectOperand(Dest);
710 case MIToken::FixedStackObject:
711 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000712 case MIToken::GlobalValue:
713 case MIToken::NamedGlobalValue:
Alex Lorenzb29554d2015-07-20 20:31:01 +0000714 case MIToken::QuotedNamedGlobalValue:
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000715 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000716 case MIToken::ConstantPoolItem:
717 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000718 case MIToken::JumpTableIndex:
719 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000720 case MIToken::ExternalSymbol:
721 case MIToken::QuotedExternalSymbol:
722 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +0000723 case MIToken::exclaim:
724 return parseMetadataOperand(Dest);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000725 case MIToken::kw_cfi_offset:
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000726 case MIToken::kw_cfi_def_cfa_offset:
727 return parseCFIOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000728 case MIToken::Error:
729 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000730 case MIToken::Identifier:
731 if (const auto *RegMask = getRegMask(Token.stringValue())) {
732 Dest = MachineOperand::CreateRegMask(RegMask);
733 lex();
734 break;
735 }
736 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000737 default:
738 // TODO: parse the other machine operands.
739 return error("expected a machine operand");
740 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000741 return false;
742}
743
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000744void MIParser::initNames2InstrOpCodes() {
745 if (!Names2InstrOpCodes.empty())
746 return;
747 const auto *TII = MF.getSubtarget().getInstrInfo();
748 assert(TII && "Expected target instruction info");
749 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
750 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
751}
752
753bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
754 initNames2InstrOpCodes();
755 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
756 if (InstrInfo == Names2InstrOpCodes.end())
757 return true;
758 OpCode = InstrInfo->getValue();
759 return false;
760}
761
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000762void MIParser::initNames2Regs() {
763 if (!Names2Regs.empty())
764 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +0000765 // The '%noreg' register is the register 0.
766 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000767 const auto *TRI = MF.getSubtarget().getRegisterInfo();
768 assert(TRI && "Expected target register info");
769 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
770 bool WasInserted =
771 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
772 .second;
773 (void)WasInserted;
774 assert(WasInserted && "Expected registers to be unique case-insensitively");
775 }
776}
777
778bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
779 initNames2Regs();
780 auto RegInfo = Names2Regs.find(RegName);
781 if (RegInfo == Names2Regs.end())
782 return true;
783 Reg = RegInfo->getValue();
784 return false;
785}
786
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000787void MIParser::initNames2RegMasks() {
788 if (!Names2RegMasks.empty())
789 return;
790 const auto *TRI = MF.getSubtarget().getRegisterInfo();
791 assert(TRI && "Expected target register info");
792 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
793 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
794 assert(RegMasks.size() == RegMaskNames.size());
795 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
796 Names2RegMasks.insert(
797 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
798}
799
800const uint32_t *MIParser::getRegMask(StringRef Identifier) {
801 initNames2RegMasks();
802 auto RegMaskInfo = Names2RegMasks.find(Identifier);
803 if (RegMaskInfo == Names2RegMasks.end())
804 return nullptr;
805 return RegMaskInfo->getValue();
806}
807
Alex Lorenz2eacca82015-07-13 23:24:34 +0000808void MIParser::initNames2SubRegIndices() {
809 if (!Names2SubRegIndices.empty())
810 return;
811 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
812 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
813 Names2SubRegIndices.insert(
814 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
815}
816
817unsigned MIParser::getSubRegIndex(StringRef Name) {
818 initNames2SubRegIndices();
819 auto SubRegInfo = Names2SubRegIndices.find(Name);
820 if (SubRegInfo == Names2SubRegIndices.end())
821 return 0;
822 return SubRegInfo->getValue();
823}
824
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000825bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
826 MachineFunction &MF, StringRef Src,
827 const PerFunctionMIParsingState &PFS,
828 const SlotMapping &IRSlots, SMDiagnostic &Error) {
829 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000830}
Alex Lorenzf09df002015-06-30 18:16:42 +0000831
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000832bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
833 MachineFunction &MF, StringRef Src,
834 const PerFunctionMIParsingState &PFS,
835 const SlotMapping &IRSlots, SMDiagnostic &Error) {
836 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000837}
Alex Lorenz9fab3702015-07-14 21:24:41 +0000838
839bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
840 MachineFunction &MF, StringRef Src,
841 const PerFunctionMIParsingState &PFS,
842 const SlotMapping &IRSlots,
843 SMDiagnostic &Error) {
844 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseNamedRegister(Reg);
845}