blob: fe39d157297dcf92ed2477c5e4cc00d5eb97def3 [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 Lorenzad156fb2015-07-31 20:49:21 +000017#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000018#include "llvm/AsmParser/SlotMapping.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000019#include "llvm/CodeGen/MachineBasicBlock.h"
20#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000022#include "llvm/CodeGen/MachineInstr.h"
Alex Lorenzcb268d42015-07-06 23:07:26 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000024#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000026#include "llvm/IR/Instructions.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000027#include "llvm/IR/Constants.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000028#include "llvm/IR/Module.h"
Alex Lorenz8a1915b2015-07-27 22:42:41 +000029#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000030#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000031#include "llvm/Support/raw_ostream.h"
32#include "llvm/Support/SourceMgr.h"
33#include "llvm/Target/TargetSubtargetInfo.h"
34#include "llvm/Target/TargetInstrInfo.h"
35
36using namespace llvm;
37
38namespace {
39
Alex Lorenz36962cd2015-07-07 02:08:46 +000040/// A wrapper struct around the 'MachineOperand' struct that includes a source
41/// range.
42struct MachineOperandWithLocation {
43 MachineOperand Operand;
44 StringRef::iterator Begin;
45 StringRef::iterator End;
46
47 MachineOperandWithLocation(const MachineOperand &Operand,
48 StringRef::iterator Begin, StringRef::iterator End)
49 : Operand(Operand), Begin(Begin), End(End) {}
50};
51
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000052class MIParser {
53 SourceMgr &SM;
54 MachineFunction &MF;
55 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000056 StringRef Source, CurrentSource;
57 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000058 const PerFunctionMIParsingState &PFS;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000059 /// Maps from indices to unnamed global values and metadata nodes.
60 const SlotMapping &IRSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000061 /// Maps from instruction names to op codes.
62 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000063 /// Maps from register names to registers.
64 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000065 /// Maps from register mask names to register masks.
66 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000067 /// Maps from subregister names to subregister indices.
68 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000069 /// Maps from slot numbers to function's unnamed basic blocks.
70 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzef5c1962015-07-28 23:02:45 +000071 /// Maps from target index names to target indices.
72 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +000073 /// Maps from direct target flag names to the direct target flag values.
74 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000075
76public:
77 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000078 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000079 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000080
Alex Lorenz91370c52015-06-22 20:37:46 +000081 void lex();
82
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000083 /// Report an error at the current location with the given message.
84 ///
85 /// This function always return true.
86 bool error(const Twine &Msg);
87
Alex Lorenz91370c52015-06-22 20:37:46 +000088 /// Report an error at the given location with the given message.
89 ///
90 /// This function always return true.
91 bool error(StringRef::iterator Loc, const Twine &Msg);
92
Alex Lorenz3708a642015-06-30 17:47:50 +000093 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +000094 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
95 bool parseStandaloneNamedRegister(unsigned &Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +000096 bool parseStandaloneVirtualRegister(unsigned &Reg);
Alex Lorenz8a1915b2015-07-27 22:42:41 +000097 bool parseStandaloneIRBlockReference(const BasicBlock *&BB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000098
Alex Lorenzf3db51de2015-06-23 16:35:26 +000099 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000100 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000101 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000102 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000103 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000104 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Alex Lorenz05e38822015-08-05 18:52:21 +0000105 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000106 bool parseFPImmediateOperand(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 Lorenz41df7d32015-07-28 17:09:52 +0000111 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000112 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000113 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000114 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000115 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000116 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000117 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000118 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000119 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000120 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000121 bool parseIRBlock(BasicBlock *&BB, const Function &F);
122 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000123 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000124 bool parseMachineOperand(MachineOperand &Dest);
Alex Lorenz49873a82015-08-06 00:44:07 +0000125 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000126 bool parseOffset(int64_t &Offset);
Alex Lorenz5672a892015-08-05 22:26:15 +0000127 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000128 bool parseIRValue(Value *&V);
Alex Lorenza518b792015-08-04 00:24:45 +0000129 bool parseMemoryOperandFlag(unsigned &Flags);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000130 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000131
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000132private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000133 /// Convert the integer literal in the current token into an unsigned integer.
134 ///
135 /// Return true if an error occurred.
136 bool getUnsigned(unsigned &Result);
137
Alex Lorenz4af7e612015-08-03 23:08:19 +0000138 /// Convert the integer literal in the current token into an uint64.
139 ///
140 /// Return true if an error occurred.
141 bool getUint64(uint64_t &Result);
142
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000143 /// If the current token is of the given kind, consume it and return false.
144 /// Otherwise report an error and return true.
145 bool expectAndConsume(MIToken::TokenKind TokenKind);
146
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000147 void initNames2InstrOpCodes();
148
149 /// Try to convert an instruction name to an opcode. Return true if the
150 /// instruction name is invalid.
151 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000152
Alex Lorenze5a44662015-07-17 00:24:15 +0000153 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000154
Alex Lorenz36962cd2015-07-07 02:08:46 +0000155 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
156 const MCInstrDesc &MCID);
157
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000158 void initNames2Regs();
159
160 /// Try to convert a register name to a register number. Return true if the
161 /// register name is invalid.
162 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000163
164 void initNames2RegMasks();
165
166 /// Check if the given identifier is a name of a register mask.
167 ///
168 /// Return null if the identifier isn't a register mask.
169 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000170
171 void initNames2SubRegIndices();
172
173 /// Check if the given identifier is a name of a subregister index.
174 ///
175 /// Return 0 if the name isn't a subregister index class.
176 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000177
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000178 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000179 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000180
181 void initNames2TargetIndices();
182
183 /// Try to convert a name of target index to the corresponding target index.
184 ///
185 /// Return true if the name isn't a name of a target index.
186 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000187
188 void initNames2DirectTargetFlags();
189
190 /// Try to convert a name of a direct target flag to the corresponding
191 /// target flag.
192 ///
193 /// Return true if the name isn't a name of a direct flag.
194 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000195};
196
197} // end anonymous namespace
198
199MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000200 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000201 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000202 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz3fb77682015-08-06 23:17:42 +0000203 PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000204
Alex Lorenz91370c52015-06-22 20:37:46 +0000205void MIParser::lex() {
206 CurrentSource = lexMIToken(
207 CurrentSource, Token,
208 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
209}
210
211bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
212
213bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000214 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
215 Error = SMDiagnostic(
216 SM, SMLoc(),
217 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
218 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000219 return true;
220}
221
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000222static const char *toString(MIToken::TokenKind TokenKind) {
223 switch (TokenKind) {
224 case MIToken::comma:
225 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000226 case MIToken::equal:
227 return "'='";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000228 case MIToken::lparen:
229 return "'('";
230 case MIToken::rparen:
231 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000232 default:
233 return "<unknown token>";
234 }
235}
236
237bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
238 if (Token.isNot(TokenKind))
239 return error(Twine("expected ") + toString(TokenKind));
240 lex();
241 return false;
242}
243
Alex Lorenz3708a642015-06-30 17:47:50 +0000244bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000245 lex();
246
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000247 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000248 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000249 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000250 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000251 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000252 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000253 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000254 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000255 if (Token.isNot(MIToken::comma))
256 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000257 lex();
258 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000259 if (!Operands.empty() && expectAndConsume(MIToken::equal))
260 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000261
Alex Lorenze5a44662015-07-17 00:24:15 +0000262 unsigned OpCode, Flags = 0;
263 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000264 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000265
Alex Lorenz4af7e612015-08-03 23:08:19 +0000266 // TODO: Parse the bundle instruction flags.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000267
268 // Parse the remaining machine operands.
Alex Lorenz4af7e612015-08-03 23:08:19 +0000269 while (Token.isNot(MIToken::Eof) && Token.isNot(MIToken::kw_debug_location) &&
270 Token.isNot(MIToken::coloncolon)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000271 auto Loc = Token.location();
Alex Lorenz49873a82015-08-06 00:44:07 +0000272 if (parseMachineOperandAndTargetFlags(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000273 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000274 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000275 if (Token.is(MIToken::Eof) || Token.is(MIToken::coloncolon))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000276 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000277 if (Token.isNot(MIToken::comma))
278 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000279 lex();
280 }
281
Alex Lorenz46d760d2015-07-22 21:15:11 +0000282 DebugLoc DebugLocation;
283 if (Token.is(MIToken::kw_debug_location)) {
284 lex();
285 if (Token.isNot(MIToken::exclaim))
286 return error("expected a metadata node after 'debug-location'");
287 MDNode *Node = nullptr;
288 if (parseMDNode(Node))
289 return true;
290 DebugLocation = DebugLoc(Node);
291 }
292
Alex Lorenz4af7e612015-08-03 23:08:19 +0000293 // Parse the machine memory operands.
294 SmallVector<MachineMemOperand *, 2> MemOperands;
295 if (Token.is(MIToken::coloncolon)) {
296 lex();
297 while (Token.isNot(MIToken::Eof)) {
298 MachineMemOperand *MemOp = nullptr;
299 if (parseMachineMemoryOperand(MemOp))
300 return true;
301 MemOperands.push_back(MemOp);
302 if (Token.is(MIToken::Eof))
303 break;
304 if (Token.isNot(MIToken::comma))
305 return error("expected ',' before the next machine memory operand");
306 lex();
307 }
308 }
309
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000310 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000311 if (!MCID.isVariadic()) {
312 // FIXME: Move the implicit operand verification to the machine verifier.
313 if (verifyImplicitOperands(Operands, MCID))
314 return true;
315 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000316
Alex Lorenzcb268d42015-07-06 23:07:26 +0000317 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000318 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000319 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000320 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000321 MI->addOperand(MF, Operand.Operand);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000322 if (MemOperands.empty())
323 return false;
324 MachineInstr::mmo_iterator MemRefs =
325 MF.allocateMemRefsArray(MemOperands.size());
326 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
327 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000328 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000329}
330
Alex Lorenz1ea60892015-07-27 20:29:27 +0000331bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000332 lex();
333 if (Token.isNot(MIToken::MachineBasicBlock))
334 return error("expected a machine basic block reference");
335 if (parseMBBReference(MBB))
336 return true;
337 lex();
338 if (Token.isNot(MIToken::Eof))
339 return error(
340 "expected end of string after the machine basic block reference");
341 return false;
342}
343
Alex Lorenz1ea60892015-07-27 20:29:27 +0000344bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000345 lex();
346 if (Token.isNot(MIToken::NamedRegister))
347 return error("expected a named register");
348 if (parseRegister(Reg))
349 return 0;
350 lex();
351 if (Token.isNot(MIToken::Eof))
352 return error("expected end of string after the register reference");
353 return false;
354}
355
Alex Lorenz12045a42015-07-27 17:42:45 +0000356bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
357 lex();
358 if (Token.isNot(MIToken::VirtualRegister))
359 return error("expected a virtual register");
360 if (parseRegister(Reg))
361 return 0;
362 lex();
363 if (Token.isNot(MIToken::Eof))
364 return error("expected end of string after the register reference");
365 return false;
366}
367
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000368bool MIParser::parseStandaloneIRBlockReference(const BasicBlock *&BB) {
369 lex();
370 if (Token.isNot(MIToken::IRBlock))
371 return error("expected an IR block reference");
372 unsigned SlotNumber = 0;
373 if (getUnsigned(SlotNumber))
374 return true;
375 BB = getIRBlock(SlotNumber);
376 if (!BB)
377 return error(Twine("use of undefined IR block '%ir-block.") +
378 Twine(SlotNumber) + "'");
379 lex();
380 if (Token.isNot(MIToken::Eof))
381 return error("expected end of string after the IR block reference");
382 return false;
383}
384
Alex Lorenz36962cd2015-07-07 02:08:46 +0000385static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
386 assert(MO.isImplicit());
387 return MO.isDef() ? "implicit-def" : "implicit";
388}
389
390static std::string getRegisterName(const TargetRegisterInfo *TRI,
391 unsigned Reg) {
392 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
393 return StringRef(TRI->getName(Reg)).lower();
394}
395
396bool MIParser::verifyImplicitOperands(
397 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
398 if (MCID.isCall())
399 // We can't verify call instructions as they can contain arbitrary implicit
400 // register and register mask operands.
401 return false;
402
403 // Gather all the expected implicit operands.
404 SmallVector<MachineOperand, 4> ImplicitOperands;
405 if (MCID.ImplicitDefs)
406 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
407 ImplicitOperands.push_back(
408 MachineOperand::CreateReg(*ImpDefs, true, true));
409 if (MCID.ImplicitUses)
410 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
411 ImplicitOperands.push_back(
412 MachineOperand::CreateReg(*ImpUses, false, true));
413
414 const auto *TRI = MF.getSubtarget().getRegisterInfo();
415 assert(TRI && "Expected target register info");
416 size_t I = ImplicitOperands.size(), J = Operands.size();
417 while (I) {
418 --I;
419 if (J) {
420 --J;
421 const auto &ImplicitOperand = ImplicitOperands[I];
422 const auto &Operand = Operands[J].Operand;
423 if (ImplicitOperand.isIdenticalTo(Operand))
424 continue;
425 if (Operand.isReg() && Operand.isImplicit()) {
426 return error(Operands[J].Begin,
427 Twine("expected an implicit register operand '") +
428 printImplicitRegisterFlag(ImplicitOperand) + " %" +
429 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
430 }
431 }
432 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
433 // insead of reporting an error at this location:
434 // %eax = MOV32r0
435 // ^
436 // report the error at the following location:
437 // %eax = MOV32r0
438 // ^
439 return error(J < Operands.size() ? Operands[J].End : Token.location(),
440 Twine("missing implicit register operand '") +
441 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
442 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
443 }
444 return false;
445}
446
Alex Lorenze5a44662015-07-17 00:24:15 +0000447bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
448 if (Token.is(MIToken::kw_frame_setup)) {
449 Flags |= MachineInstr::FrameSetup;
450 lex();
451 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000452 if (Token.isNot(MIToken::Identifier))
453 return error("expected a machine instruction");
454 StringRef InstrName = Token.stringValue();
455 if (parseInstrName(InstrName, OpCode))
456 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000457 lex();
458 return false;
459}
460
461bool MIParser::parseRegister(unsigned &Reg) {
462 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000463 case MIToken::underscore:
464 Reg = 0;
465 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000466 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000467 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000468 if (getRegisterByName(Name, Reg))
469 return error(Twine("unknown register name '") + Name + "'");
470 break;
471 }
Alex Lorenz53464512015-07-10 22:51:20 +0000472 case MIToken::VirtualRegister: {
473 unsigned ID;
474 if (getUnsigned(ID))
475 return true;
476 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
477 if (RegInfo == PFS.VirtualRegisterSlots.end())
478 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
479 "'");
480 Reg = RegInfo->second;
481 break;
482 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000483 // TODO: Parse other register kinds.
484 default:
485 llvm_unreachable("The current token should be a register");
486 }
487 return false;
488}
489
Alex Lorenzcb268d42015-07-06 23:07:26 +0000490bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000491 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000492 switch (Token.kind()) {
493 case MIToken::kw_implicit:
494 Flags |= RegState::Implicit;
495 break;
496 case MIToken::kw_implicit_define:
497 Flags |= RegState::ImplicitDefine;
498 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000499 case MIToken::kw_dead:
500 Flags |= RegState::Dead;
501 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000502 case MIToken::kw_killed:
503 Flags |= RegState::Kill;
504 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000505 case MIToken::kw_undef:
506 Flags |= RegState::Undef;
507 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000508 case MIToken::kw_early_clobber:
509 Flags |= RegState::EarlyClobber;
510 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000511 case MIToken::kw_debug_use:
512 Flags |= RegState::Debug;
513 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000514 // TODO: parse the other register flags.
515 default:
516 llvm_unreachable("The current token should be a register flag");
517 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000518 if (OldFlags == Flags)
519 // We know that the same flag is specified more than once when the flags
520 // weren't modified.
521 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000522 lex();
523 return false;
524}
525
Alex Lorenz2eacca82015-07-13 23:24:34 +0000526bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
527 assert(Token.is(MIToken::colon));
528 lex();
529 if (Token.isNot(MIToken::Identifier))
530 return error("expected a subregister index after ':'");
531 auto Name = Token.stringValue();
532 SubReg = getSubRegIndex(Name);
533 if (!SubReg)
534 return error(Twine("use of unknown subregister index '") + Name + "'");
535 lex();
536 return false;
537}
538
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000539bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
540 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000541 unsigned Flags = IsDef ? RegState::Define : 0;
542 while (Token.isRegisterFlag()) {
543 if (parseRegisterFlag(Flags))
544 return true;
545 }
546 if (!Token.isRegister())
547 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000548 if (parseRegister(Reg))
549 return true;
550 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000551 unsigned SubReg = 0;
552 if (Token.is(MIToken::colon)) {
553 if (parseSubRegisterIndex(SubReg))
554 return true;
555 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000556 Dest = MachineOperand::CreateReg(
557 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000558 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000559 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000560 return false;
561}
562
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000563bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
564 assert(Token.is(MIToken::IntegerLiteral));
565 const APSInt &Int = Token.integerValue();
566 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +0000567 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000568 Dest = MachineOperand::CreateImm(Int.getExtValue());
569 lex();
570 return false;
571}
572
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000573bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
Alex Lorenz3fb77682015-08-06 23:17:42 +0000574 auto Source = StringRef(Loc, Token.range().end() - Loc).str();
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000575 lex();
576 SMDiagnostic Err;
577 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
578 if (!C)
579 return error(Loc + Err.getColumnNo(), Err.getMessage());
580 return false;
581}
582
Alex Lorenz05e38822015-08-05 18:52:21 +0000583bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
584 assert(Token.is(MIToken::IntegerType));
585 auto Loc = Token.location();
586 lex();
587 if (Token.isNot(MIToken::IntegerLiteral))
588 return error("expected an integer literal");
589 const Constant *C = nullptr;
590 if (parseIRConstant(Loc, C))
591 return true;
592 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
593 return false;
594}
595
Alex Lorenzad156fb2015-07-31 20:49:21 +0000596bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
597 auto Loc = Token.location();
598 lex();
599 if (Token.isNot(MIToken::FloatingPointLiteral))
600 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000601 const Constant *C = nullptr;
602 if (parseIRConstant(Loc, C))
603 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000604 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
605 return false;
606}
607
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000608bool MIParser::getUnsigned(unsigned &Result) {
609 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
610 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
611 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
612 if (Val64 == Limit)
613 return error("expected 32-bit integer (too large)");
614 Result = Val64;
615 return false;
616}
617
Alex Lorenzf09df002015-06-30 18:16:42 +0000618bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000619 assert(Token.is(MIToken::MachineBasicBlock));
620 unsigned Number;
621 if (getUnsigned(Number))
622 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000623 auto MBBInfo = PFS.MBBSlots.find(Number);
624 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000625 return error(Twine("use of undefined machine basic block #") +
626 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000627 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000628 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
629 return error(Twine("the name of machine basic block #") + Twine(Number) +
630 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000631 return false;
632}
633
634bool MIParser::parseMBBOperand(MachineOperand &Dest) {
635 MachineBasicBlock *MBB;
636 if (parseMBBReference(MBB))
637 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000638 Dest = MachineOperand::CreateMBB(MBB);
639 lex();
640 return false;
641}
642
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000643bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
644 assert(Token.is(MIToken::StackObject));
645 unsigned ID;
646 if (getUnsigned(ID))
647 return true;
648 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
649 if (ObjectInfo == PFS.StackObjectSlots.end())
650 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
651 "'");
652 StringRef Name;
653 if (const auto *Alloca =
654 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
655 Name = Alloca->getName();
656 if (!Token.stringValue().empty() && Token.stringValue() != Name)
657 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
658 "' isn't '" + Token.stringValue() + "'");
659 lex();
660 Dest = MachineOperand::CreateFI(ObjectInfo->second);
661 return false;
662}
663
664bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
665 assert(Token.is(MIToken::FixedStackObject));
666 unsigned ID;
667 if (getUnsigned(ID))
668 return true;
669 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
670 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
671 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
672 Twine(ID) + "'");
673 lex();
674 Dest = MachineOperand::CreateFI(ObjectInfo->second);
675 return false;
676}
677
Alex Lorenz41df7d32015-07-28 17:09:52 +0000678bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000679 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000680 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000681 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +0000682 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +0000683 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +0000684 return error(Twine("use of undefined global value '") + Token.range() +
685 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000686 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000687 }
688 case MIToken::GlobalValue: {
689 unsigned GVIdx;
690 if (getUnsigned(GVIdx))
691 return true;
692 if (GVIdx >= IRSlots.GlobalValues.size())
693 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
694 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000695 GV = IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000696 break;
697 }
698 default:
699 llvm_unreachable("The current token should be a global value");
700 }
Alex Lorenz41df7d32015-07-28 17:09:52 +0000701 return false;
702}
703
704bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
705 GlobalValue *GV = nullptr;
706 if (parseGlobalValue(GV))
707 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000708 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000709 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000710 if (parseOperandsOffset(Dest))
711 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000712 return false;
713}
714
Alex Lorenzab980492015-07-20 20:51:18 +0000715bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
716 assert(Token.is(MIToken::ConstantPoolItem));
717 unsigned ID;
718 if (getUnsigned(ID))
719 return true;
720 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
721 if (ConstantInfo == PFS.ConstantPoolSlots.end())
722 return error("use of undefined constant '%const." + Twine(ID) + "'");
723 lex();
Alex Lorenzab980492015-07-20 20:51:18 +0000724 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000725 if (parseOperandsOffset(Dest))
726 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000727 return false;
728}
729
Alex Lorenz31d70682015-07-15 23:38:35 +0000730bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
731 assert(Token.is(MIToken::JumpTableIndex));
732 unsigned ID;
733 if (getUnsigned(ID))
734 return true;
735 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
736 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
737 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
738 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000739 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
740 return false;
741}
742
Alex Lorenz6ede3742015-07-21 16:59:53 +0000743bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000744 assert(Token.is(MIToken::ExternalSymbol));
745 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000746 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +0000747 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +0000748 if (parseOperandsOffset(Dest))
749 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000750 return false;
751}
752
Alex Lorenz44f29252015-07-22 21:07:04 +0000753bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +0000754 assert(Token.is(MIToken::exclaim));
755 auto Loc = Token.location();
756 lex();
757 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
758 return error("expected metadata id after '!'");
759 unsigned ID;
760 if (getUnsigned(ID))
761 return true;
762 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
763 if (NodeInfo == IRSlots.MetadataNodes.end())
764 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
765 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +0000766 Node = NodeInfo->second.get();
767 return false;
768}
769
770bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
771 MDNode *Node = nullptr;
772 if (parseMDNode(Node))
773 return true;
774 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000775 return false;
776}
777
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000778bool MIParser::parseCFIOffset(int &Offset) {
779 if (Token.isNot(MIToken::IntegerLiteral))
780 return error("expected a cfi offset");
781 if (Token.integerValue().getMinSignedBits() > 32)
782 return error("expected a 32 bit integer (the cfi offset is too large)");
783 Offset = (int)Token.integerValue().getExtValue();
784 lex();
785 return false;
786}
787
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000788bool MIParser::parseCFIRegister(unsigned &Reg) {
789 if (Token.isNot(MIToken::NamedRegister))
790 return error("expected a cfi register");
791 unsigned LLVMReg;
792 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000793 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000794 const auto *TRI = MF.getSubtarget().getRegisterInfo();
795 assert(TRI && "Expected target register info");
796 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
797 if (DwarfReg < 0)
798 return error("invalid DWARF register");
799 Reg = (unsigned)DwarfReg;
800 lex();
801 return false;
802}
803
804bool MIParser::parseCFIOperand(MachineOperand &Dest) {
805 auto Kind = Token.kind();
806 lex();
807 auto &MMI = MF.getMMI();
808 int Offset;
809 unsigned Reg;
810 unsigned CFIIndex;
811 switch (Kind) {
812 case MIToken::kw_cfi_offset:
813 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
814 parseCFIOffset(Offset))
815 return true;
816 CFIIndex =
817 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
818 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000819 case MIToken::kw_cfi_def_cfa_register:
820 if (parseCFIRegister(Reg))
821 return true;
822 CFIIndex =
823 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
824 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000825 case MIToken::kw_cfi_def_cfa_offset:
826 if (parseCFIOffset(Offset))
827 return true;
828 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
829 CFIIndex = MMI.addFrameInst(
830 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
831 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000832 case MIToken::kw_cfi_def_cfa:
833 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
834 parseCFIOffset(Offset))
835 return true;
836 // NB: MCCFIInstruction::createDefCfa negates the offset.
837 CFIIndex =
838 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
839 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000840 default:
841 // TODO: Parse the other CFI operands.
842 llvm_unreachable("The current token should be a cfi operand");
843 }
844 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000845 return false;
846}
847
Alex Lorenzdeb53492015-07-28 17:28:03 +0000848bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
849 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000850 case MIToken::NamedIRBlock: {
851 BB = dyn_cast_or_null<BasicBlock>(
852 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +0000853 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +0000854 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +0000855 break;
856 }
857 case MIToken::IRBlock: {
858 unsigned SlotNumber = 0;
859 if (getUnsigned(SlotNumber))
860 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000861 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +0000862 if (!BB)
863 return error(Twine("use of undefined IR block '%ir-block.") +
864 Twine(SlotNumber) + "'");
865 break;
866 }
867 default:
868 llvm_unreachable("The current token should be an IR block reference");
869 }
870 return false;
871}
872
873bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
874 assert(Token.is(MIToken::kw_blockaddress));
875 lex();
876 if (expectAndConsume(MIToken::lparen))
877 return true;
878 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +0000879 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +0000880 return error("expected a global value");
881 GlobalValue *GV = nullptr;
882 if (parseGlobalValue(GV))
883 return true;
884 auto *F = dyn_cast<Function>(GV);
885 if (!F)
886 return error("expected an IR function reference");
887 lex();
888 if (expectAndConsume(MIToken::comma))
889 return true;
890 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +0000891 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +0000892 return error("expected an IR block reference");
893 if (parseIRBlock(BB, *F))
894 return true;
895 lex();
896 if (expectAndConsume(MIToken::rparen))
897 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000898 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000899 if (parseOperandsOffset(Dest))
900 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000901 return false;
902}
903
Alex Lorenzef5c1962015-07-28 23:02:45 +0000904bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
905 assert(Token.is(MIToken::kw_target_index));
906 lex();
907 if (expectAndConsume(MIToken::lparen))
908 return true;
909 if (Token.isNot(MIToken::Identifier))
910 return error("expected the name of the target index");
911 int Index = 0;
912 if (getTargetIndex(Token.stringValue(), Index))
913 return error("use of undefined target index '" + Token.stringValue() + "'");
914 lex();
915 if (expectAndConsume(MIToken::rparen))
916 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000917 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000918 if (parseOperandsOffset(Dest))
919 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000920 return false;
921}
922
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000923bool MIParser::parseMachineOperand(MachineOperand &Dest) {
924 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000925 case MIToken::kw_implicit:
926 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000927 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000928 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000929 case MIToken::kw_undef:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000930 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +0000931 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000932 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000933 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000934 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000935 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000936 case MIToken::IntegerLiteral:
937 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +0000938 case MIToken::IntegerType:
939 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000940 case MIToken::kw_half:
941 case MIToken::kw_float:
942 case MIToken::kw_double:
943 case MIToken::kw_x86_fp80:
944 case MIToken::kw_fp128:
945 case MIToken::kw_ppc_fp128:
946 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000947 case MIToken::MachineBasicBlock:
948 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000949 case MIToken::StackObject:
950 return parseStackObjectOperand(Dest);
951 case MIToken::FixedStackObject:
952 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000953 case MIToken::GlobalValue:
954 case MIToken::NamedGlobalValue:
955 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000956 case MIToken::ConstantPoolItem:
957 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000958 case MIToken::JumpTableIndex:
959 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000960 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +0000961 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +0000962 case MIToken::exclaim:
963 return parseMetadataOperand(Dest);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000964 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000965 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000966 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +0000967 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000968 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000969 case MIToken::kw_blockaddress:
970 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000971 case MIToken::kw_target_index:
972 return parseTargetIndexOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000973 case MIToken::Error:
974 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000975 case MIToken::Identifier:
976 if (const auto *RegMask = getRegMask(Token.stringValue())) {
977 Dest = MachineOperand::CreateRegMask(RegMask);
978 lex();
979 break;
980 }
981 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000982 default:
983 // TODO: parse the other machine operands.
984 return error("expected a machine operand");
985 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000986 return false;
987}
988
Alex Lorenz49873a82015-08-06 00:44:07 +0000989bool MIParser::parseMachineOperandAndTargetFlags(MachineOperand &Dest) {
990 unsigned TF = 0;
991 bool HasTargetFlags = false;
992 if (Token.is(MIToken::kw_target_flags)) {
993 HasTargetFlags = true;
994 lex();
995 if (expectAndConsume(MIToken::lparen))
996 return true;
997 if (Token.isNot(MIToken::Identifier))
998 return error("expected the name of the target flag");
999 if (getDirectTargetFlag(Token.stringValue(), TF))
1000 return error("use of undefined target flag '" + Token.stringValue() +
1001 "'");
1002 lex();
1003 // TODO: Parse target's bit target flags.
1004 if (expectAndConsume(MIToken::rparen))
1005 return true;
1006 }
1007 auto Loc = Token.location();
1008 if (parseMachineOperand(Dest))
1009 return true;
1010 if (!HasTargetFlags)
1011 return false;
1012 if (Dest.isReg())
1013 return error(Loc, "register operands can't have target flags");
1014 Dest.setTargetFlags(TF);
1015 return false;
1016}
1017
Alex Lorenzdc24c172015-08-07 20:21:00 +00001018bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001019 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1020 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001021 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001022 bool IsNegative = Token.is(MIToken::minus);
1023 lex();
1024 if (Token.isNot(MIToken::IntegerLiteral))
1025 return error("expected an integer literal after '" + Sign + "'");
1026 if (Token.integerValue().getMinSignedBits() > 64)
1027 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001028 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001029 if (IsNegative)
1030 Offset = -Offset;
1031 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001032 return false;
1033}
1034
1035bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1036 int64_t Offset = 0;
1037 if (parseOffset(Offset))
1038 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001039 Op.setOffset(Offset);
1040 return false;
1041}
1042
Alex Lorenz4af7e612015-08-03 23:08:19 +00001043bool MIParser::parseIRValue(Value *&V) {
1044 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001045 case MIToken::NamedIRValue: {
1046 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001047 if (!V)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001048 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001049 break;
1050 }
1051 // TODO: Parse unnamed IR value references.
1052 default:
1053 llvm_unreachable("The current token should be an IR block reference");
1054 }
1055 return false;
1056}
1057
1058bool MIParser::getUint64(uint64_t &Result) {
1059 assert(Token.hasIntegerValue());
1060 if (Token.integerValue().getActiveBits() > 64)
1061 return error("expected 64-bit integer (too large)");
1062 Result = Token.integerValue().getZExtValue();
1063 return false;
1064}
1065
Alex Lorenza518b792015-08-04 00:24:45 +00001066bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
Alex Lorenze86d5152015-08-06 18:26:36 +00001067 const unsigned OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001068 switch (Token.kind()) {
1069 case MIToken::kw_volatile:
1070 Flags |= MachineMemOperand::MOVolatile;
1071 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001072 case MIToken::kw_non_temporal:
1073 Flags |= MachineMemOperand::MONonTemporal;
1074 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001075 case MIToken::kw_invariant:
1076 Flags |= MachineMemOperand::MOInvariant;
1077 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001078 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001079 default:
1080 llvm_unreachable("The current token should be a memory operand flag");
1081 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001082 if (OldFlags == Flags)
1083 // We know that the same flag is specified more than once when the flags
1084 // weren't modified.
1085 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001086 lex();
1087 return false;
1088}
1089
Alex Lorenz4af7e612015-08-03 23:08:19 +00001090bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1091 if (expectAndConsume(MIToken::lparen))
1092 return true;
Alex Lorenza518b792015-08-04 00:24:45 +00001093 unsigned Flags = 0;
1094 while (Token.isMemoryOperandFlag()) {
1095 if (parseMemoryOperandFlag(Flags))
1096 return true;
1097 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001098 if (Token.isNot(MIToken::Identifier) ||
1099 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1100 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001101 if (Token.stringValue() == "load")
1102 Flags |= MachineMemOperand::MOLoad;
1103 else
1104 Flags |= MachineMemOperand::MOStore;
1105 lex();
1106
1107 if (Token.isNot(MIToken::IntegerLiteral))
1108 return error("expected the size integer literal after memory operation");
1109 uint64_t Size;
1110 if (getUint64(Size))
1111 return true;
1112 lex();
1113
1114 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1115 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1116 return error(Twine("expected '") + Word + "'");
1117 lex();
1118
1119 // TODO: Parse pseudo source values.
Alex Lorenz970c12e2015-08-05 17:35:55 +00001120 if (Token.isNot(MIToken::NamedIRValue))
Alex Lorenz4af7e612015-08-03 23:08:19 +00001121 return error("expected an IR value reference");
1122 Value *V = nullptr;
1123 if (parseIRValue(V))
1124 return true;
1125 if (!V->getType()->isPointerTy())
1126 return error("expected a pointer IR value");
1127 lex();
Alex Lorenz83127732015-08-07 20:26:52 +00001128 int64_t Offset = 0;
1129 if (parseOffset(Offset))
1130 return true;
Alex Lorenz61420f72015-08-07 20:48:30 +00001131 unsigned BaseAlignment = Size;
1132 if (Token.is(MIToken::comma)) {
1133 lex();
1134 if (Token.isNot(MIToken::kw_align))
1135 return error("expected 'align'");
1136 lex();
1137 if (Token.isNot(MIToken::IntegerLiteral))
1138 return error("expected an integer literal after 'align'");
1139 if (getUnsigned(BaseAlignment))
1140 return true;
1141 lex();
1142 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001143 // TODO: Parse the attached metadata nodes.
1144 if (expectAndConsume(MIToken::rparen))
1145 return true;
1146
Alex Lorenz61420f72015-08-07 20:48:30 +00001147 Dest = MF.getMachineMemOperand(MachinePointerInfo(V, Offset), Flags, Size,
1148 BaseAlignment);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001149 return false;
1150}
1151
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001152void MIParser::initNames2InstrOpCodes() {
1153 if (!Names2InstrOpCodes.empty())
1154 return;
1155 const auto *TII = MF.getSubtarget().getInstrInfo();
1156 assert(TII && "Expected target instruction info");
1157 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1158 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1159}
1160
1161bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1162 initNames2InstrOpCodes();
1163 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1164 if (InstrInfo == Names2InstrOpCodes.end())
1165 return true;
1166 OpCode = InstrInfo->getValue();
1167 return false;
1168}
1169
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001170void MIParser::initNames2Regs() {
1171 if (!Names2Regs.empty())
1172 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001173 // The '%noreg' register is the register 0.
1174 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001175 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1176 assert(TRI && "Expected target register info");
1177 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1178 bool WasInserted =
1179 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1180 .second;
1181 (void)WasInserted;
1182 assert(WasInserted && "Expected registers to be unique case-insensitively");
1183 }
1184}
1185
1186bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1187 initNames2Regs();
1188 auto RegInfo = Names2Regs.find(RegName);
1189 if (RegInfo == Names2Regs.end())
1190 return true;
1191 Reg = RegInfo->getValue();
1192 return false;
1193}
1194
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001195void MIParser::initNames2RegMasks() {
1196 if (!Names2RegMasks.empty())
1197 return;
1198 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1199 assert(TRI && "Expected target register info");
1200 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1201 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1202 assert(RegMasks.size() == RegMaskNames.size());
1203 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1204 Names2RegMasks.insert(
1205 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1206}
1207
1208const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1209 initNames2RegMasks();
1210 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1211 if (RegMaskInfo == Names2RegMasks.end())
1212 return nullptr;
1213 return RegMaskInfo->getValue();
1214}
1215
Alex Lorenz2eacca82015-07-13 23:24:34 +00001216void MIParser::initNames2SubRegIndices() {
1217 if (!Names2SubRegIndices.empty())
1218 return;
1219 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1220 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1221 Names2SubRegIndices.insert(
1222 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1223}
1224
1225unsigned MIParser::getSubRegIndex(StringRef Name) {
1226 initNames2SubRegIndices();
1227 auto SubRegInfo = Names2SubRegIndices.find(Name);
1228 if (SubRegInfo == Names2SubRegIndices.end())
1229 return 0;
1230 return SubRegInfo->getValue();
1231}
1232
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001233static void initSlots2BasicBlocks(
1234 const Function &F,
1235 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1236 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001237 MST.incorporateFunction(F);
1238 for (auto &BB : F) {
1239 if (BB.hasName())
1240 continue;
1241 int Slot = MST.getLocalSlot(&BB);
1242 if (Slot == -1)
1243 continue;
1244 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1245 }
1246}
1247
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001248static const BasicBlock *getIRBlockFromSlot(
1249 unsigned Slot,
1250 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001251 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1252 if (BlockInfo == Slots2BasicBlocks.end())
1253 return nullptr;
1254 return BlockInfo->second;
1255}
1256
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001257const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1258 if (Slots2BasicBlocks.empty())
1259 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1260 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1261}
1262
1263const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1264 if (&F == MF.getFunction())
1265 return getIRBlock(Slot);
1266 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1267 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1268 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1269}
1270
Alex Lorenzef5c1962015-07-28 23:02:45 +00001271void MIParser::initNames2TargetIndices() {
1272 if (!Names2TargetIndices.empty())
1273 return;
1274 const auto *TII = MF.getSubtarget().getInstrInfo();
1275 assert(TII && "Expected target instruction info");
1276 auto Indices = TII->getSerializableTargetIndices();
1277 for (const auto &I : Indices)
1278 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1279}
1280
1281bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1282 initNames2TargetIndices();
1283 auto IndexInfo = Names2TargetIndices.find(Name);
1284 if (IndexInfo == Names2TargetIndices.end())
1285 return true;
1286 Index = IndexInfo->second;
1287 return false;
1288}
1289
Alex Lorenz49873a82015-08-06 00:44:07 +00001290void MIParser::initNames2DirectTargetFlags() {
1291 if (!Names2DirectTargetFlags.empty())
1292 return;
1293 const auto *TII = MF.getSubtarget().getInstrInfo();
1294 assert(TII && "Expected target instruction info");
1295 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1296 for (const auto &I : Flags)
1297 Names2DirectTargetFlags.insert(
1298 std::make_pair(StringRef(I.second), I.first));
1299}
1300
1301bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1302 initNames2DirectTargetFlags();
1303 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1304 if (FlagInfo == Names2DirectTargetFlags.end())
1305 return true;
1306 Flag = FlagInfo->second;
1307 return false;
1308}
1309
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001310bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
1311 MachineFunction &MF, StringRef Src,
1312 const PerFunctionMIParsingState &PFS,
1313 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1314 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001315}
Alex Lorenzf09df002015-06-30 18:16:42 +00001316
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001317bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1318 MachineFunction &MF, StringRef Src,
1319 const PerFunctionMIParsingState &PFS,
1320 const SlotMapping &IRSlots, SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001321 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00001322}
Alex Lorenz9fab3702015-07-14 21:24:41 +00001323
1324bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1325 MachineFunction &MF, StringRef Src,
1326 const PerFunctionMIParsingState &PFS,
1327 const SlotMapping &IRSlots,
1328 SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001329 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1330 .parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00001331}
Alex Lorenz12045a42015-07-27 17:42:45 +00001332
1333bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1334 MachineFunction &MF, StringRef Src,
1335 const PerFunctionMIParsingState &PFS,
1336 const SlotMapping &IRSlots,
1337 SMDiagnostic &Error) {
1338 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1339 .parseStandaloneVirtualRegister(Reg);
1340}
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001341
1342bool llvm::parseIRBlockReference(const BasicBlock *&BB, SourceMgr &SM,
1343 MachineFunction &MF, StringRef Src,
1344 const PerFunctionMIParsingState &PFS,
1345 const SlotMapping &IRSlots,
1346 SMDiagnostic &Error) {
1347 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1348 .parseStandaloneIRBlockReference(BB);
1349}