blob: b333f0b52b1b84f8df0cf8970a4022bd779bdf05 [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);
Alex Lorenzea882122015-08-12 21:17:02 +0000110 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000111 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000112 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000113 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000114 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000115 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000116 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000117 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000118 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000119 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000120 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000121 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000122 bool parseIRBlock(BasicBlock *&BB, const Function &F);
123 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000124 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000125 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000126 bool parseMachineOperand(MachineOperand &Dest);
Alex Lorenz49873a82015-08-06 00:44:07 +0000127 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000128 bool parseOffset(int64_t &Offset);
Alex Lorenz5672a892015-08-05 22:26:15 +0000129 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000130 bool parseIRValue(Value *&V);
Alex Lorenza518b792015-08-04 00:24:45 +0000131 bool parseMemoryOperandFlag(unsigned &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000132 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
133 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000134 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000135
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000136private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000137 /// Convert the integer literal in the current token into an unsigned integer.
138 ///
139 /// Return true if an error occurred.
140 bool getUnsigned(unsigned &Result);
141
Alex Lorenz4af7e612015-08-03 23:08:19 +0000142 /// Convert the integer literal in the current token into an uint64.
143 ///
144 /// Return true if an error occurred.
145 bool getUint64(uint64_t &Result);
146
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000147 /// If the current token is of the given kind, consume it and return false.
148 /// Otherwise report an error and return true.
149 bool expectAndConsume(MIToken::TokenKind TokenKind);
150
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000151 void initNames2InstrOpCodes();
152
153 /// Try to convert an instruction name to an opcode. Return true if the
154 /// instruction name is invalid.
155 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000156
Alex Lorenze5a44662015-07-17 00:24:15 +0000157 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000158
Alex Lorenz36962cd2015-07-07 02:08:46 +0000159 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
160 const MCInstrDesc &MCID);
161
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000162 void initNames2Regs();
163
164 /// Try to convert a register name to a register number. Return true if the
165 /// register name is invalid.
166 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000167
168 void initNames2RegMasks();
169
170 /// Check if the given identifier is a name of a register mask.
171 ///
172 /// Return null if the identifier isn't a register mask.
173 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000174
175 void initNames2SubRegIndices();
176
177 /// Check if the given identifier is a name of a subregister index.
178 ///
179 /// Return 0 if the name isn't a subregister index class.
180 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000181
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000182 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000183 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000184
185 void initNames2TargetIndices();
186
187 /// Try to convert a name of target index to the corresponding target index.
188 ///
189 /// Return true if the name isn't a name of a target index.
190 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000191
192 void initNames2DirectTargetFlags();
193
194 /// Try to convert a name of a direct target flag to the corresponding
195 /// target flag.
196 ///
197 /// Return true if the name isn't a name of a direct flag.
198 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000199};
200
201} // end anonymous namespace
202
203MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000204 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000205 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000206 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz3fb77682015-08-06 23:17:42 +0000207 PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000208
Alex Lorenz91370c52015-06-22 20:37:46 +0000209void MIParser::lex() {
210 CurrentSource = lexMIToken(
211 CurrentSource, Token,
212 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
213}
214
215bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
216
217bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000218 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
219 Error = SMDiagnostic(
220 SM, SMLoc(),
221 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
222 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000223 return true;
224}
225
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000226static const char *toString(MIToken::TokenKind TokenKind) {
227 switch (TokenKind) {
228 case MIToken::comma:
229 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000230 case MIToken::equal:
231 return "'='";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000232 case MIToken::lparen:
233 return "'('";
234 case MIToken::rparen:
235 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000236 default:
237 return "<unknown token>";
238 }
239}
240
241bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
242 if (Token.isNot(TokenKind))
243 return error(Twine("expected ") + toString(TokenKind));
244 lex();
245 return false;
246}
247
Alex Lorenz3708a642015-06-30 17:47:50 +0000248bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000249 lex();
250
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000251 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000252 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000253 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000254 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000255 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000256 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000257 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000258 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000259 if (Token.isNot(MIToken::comma))
260 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000261 lex();
262 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000263 if (!Operands.empty() && expectAndConsume(MIToken::equal))
264 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000265
Alex Lorenze5a44662015-07-17 00:24:15 +0000266 unsigned OpCode, Flags = 0;
267 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000268 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000269
Alex Lorenz4af7e612015-08-03 23:08:19 +0000270 // TODO: Parse the bundle instruction flags.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000271
272 // Parse the remaining machine operands.
Alex Lorenz4af7e612015-08-03 23:08:19 +0000273 while (Token.isNot(MIToken::Eof) && Token.isNot(MIToken::kw_debug_location) &&
274 Token.isNot(MIToken::coloncolon)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000275 auto Loc = Token.location();
Alex Lorenz49873a82015-08-06 00:44:07 +0000276 if (parseMachineOperandAndTargetFlags(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000277 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000278 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000279 if (Token.is(MIToken::Eof) || Token.is(MIToken::coloncolon))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000280 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000281 if (Token.isNot(MIToken::comma))
282 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000283 lex();
284 }
285
Alex Lorenz46d760d2015-07-22 21:15:11 +0000286 DebugLoc DebugLocation;
287 if (Token.is(MIToken::kw_debug_location)) {
288 lex();
289 if (Token.isNot(MIToken::exclaim))
290 return error("expected a metadata node after 'debug-location'");
291 MDNode *Node = nullptr;
292 if (parseMDNode(Node))
293 return true;
294 DebugLocation = DebugLoc(Node);
295 }
296
Alex Lorenz4af7e612015-08-03 23:08:19 +0000297 // Parse the machine memory operands.
298 SmallVector<MachineMemOperand *, 2> MemOperands;
299 if (Token.is(MIToken::coloncolon)) {
300 lex();
301 while (Token.isNot(MIToken::Eof)) {
302 MachineMemOperand *MemOp = nullptr;
303 if (parseMachineMemoryOperand(MemOp))
304 return true;
305 MemOperands.push_back(MemOp);
306 if (Token.is(MIToken::Eof))
307 break;
308 if (Token.isNot(MIToken::comma))
309 return error("expected ',' before the next machine memory operand");
310 lex();
311 }
312 }
313
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000314 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000315 if (!MCID.isVariadic()) {
316 // FIXME: Move the implicit operand verification to the machine verifier.
317 if (verifyImplicitOperands(Operands, MCID))
318 return true;
319 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000320
Alex Lorenzcb268d42015-07-06 23:07:26 +0000321 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000322 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000323 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000324 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000325 MI->addOperand(MF, Operand.Operand);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000326 if (MemOperands.empty())
327 return false;
328 MachineInstr::mmo_iterator MemRefs =
329 MF.allocateMemRefsArray(MemOperands.size());
330 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
331 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000332 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000333}
334
Alex Lorenz1ea60892015-07-27 20:29:27 +0000335bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000336 lex();
337 if (Token.isNot(MIToken::MachineBasicBlock))
338 return error("expected a machine basic block reference");
339 if (parseMBBReference(MBB))
340 return true;
341 lex();
342 if (Token.isNot(MIToken::Eof))
343 return error(
344 "expected end of string after the machine basic block reference");
345 return false;
346}
347
Alex Lorenz1ea60892015-07-27 20:29:27 +0000348bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000349 lex();
350 if (Token.isNot(MIToken::NamedRegister))
351 return error("expected a named register");
352 if (parseRegister(Reg))
353 return 0;
354 lex();
355 if (Token.isNot(MIToken::Eof))
356 return error("expected end of string after the register reference");
357 return false;
358}
359
Alex Lorenz12045a42015-07-27 17:42:45 +0000360bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
361 lex();
362 if (Token.isNot(MIToken::VirtualRegister))
363 return error("expected a virtual register");
364 if (parseRegister(Reg))
365 return 0;
366 lex();
367 if (Token.isNot(MIToken::Eof))
368 return error("expected end of string after the register reference");
369 return false;
370}
371
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000372bool MIParser::parseStandaloneIRBlockReference(const BasicBlock *&BB) {
373 lex();
374 if (Token.isNot(MIToken::IRBlock))
375 return error("expected an IR block reference");
376 unsigned SlotNumber = 0;
377 if (getUnsigned(SlotNumber))
378 return true;
379 BB = getIRBlock(SlotNumber);
380 if (!BB)
381 return error(Twine("use of undefined IR block '%ir-block.") +
382 Twine(SlotNumber) + "'");
383 lex();
384 if (Token.isNot(MIToken::Eof))
385 return error("expected end of string after the IR block reference");
386 return false;
387}
388
Alex Lorenz36962cd2015-07-07 02:08:46 +0000389static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
390 assert(MO.isImplicit());
391 return MO.isDef() ? "implicit-def" : "implicit";
392}
393
394static std::string getRegisterName(const TargetRegisterInfo *TRI,
395 unsigned Reg) {
396 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
397 return StringRef(TRI->getName(Reg)).lower();
398}
399
400bool MIParser::verifyImplicitOperands(
401 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
402 if (MCID.isCall())
403 // We can't verify call instructions as they can contain arbitrary implicit
404 // register and register mask operands.
405 return false;
406
407 // Gather all the expected implicit operands.
408 SmallVector<MachineOperand, 4> ImplicitOperands;
409 if (MCID.ImplicitDefs)
410 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
411 ImplicitOperands.push_back(
412 MachineOperand::CreateReg(*ImpDefs, true, true));
413 if (MCID.ImplicitUses)
414 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
415 ImplicitOperands.push_back(
416 MachineOperand::CreateReg(*ImpUses, false, true));
417
418 const auto *TRI = MF.getSubtarget().getRegisterInfo();
419 assert(TRI && "Expected target register info");
420 size_t I = ImplicitOperands.size(), J = Operands.size();
421 while (I) {
422 --I;
423 if (J) {
424 --J;
425 const auto &ImplicitOperand = ImplicitOperands[I];
426 const auto &Operand = Operands[J].Operand;
427 if (ImplicitOperand.isIdenticalTo(Operand))
428 continue;
429 if (Operand.isReg() && Operand.isImplicit()) {
430 return error(Operands[J].Begin,
431 Twine("expected an implicit register operand '") +
432 printImplicitRegisterFlag(ImplicitOperand) + " %" +
433 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
434 }
435 }
436 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
437 // insead of reporting an error at this location:
438 // %eax = MOV32r0
439 // ^
440 // report the error at the following location:
441 // %eax = MOV32r0
442 // ^
443 return error(J < Operands.size() ? Operands[J].End : Token.location(),
444 Twine("missing implicit register operand '") +
445 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
446 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
447 }
448 return false;
449}
450
Alex Lorenze5a44662015-07-17 00:24:15 +0000451bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
452 if (Token.is(MIToken::kw_frame_setup)) {
453 Flags |= MachineInstr::FrameSetup;
454 lex();
455 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000456 if (Token.isNot(MIToken::Identifier))
457 return error("expected a machine instruction");
458 StringRef InstrName = Token.stringValue();
459 if (parseInstrName(InstrName, OpCode))
460 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000461 lex();
462 return false;
463}
464
465bool MIParser::parseRegister(unsigned &Reg) {
466 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000467 case MIToken::underscore:
468 Reg = 0;
469 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000470 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000471 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000472 if (getRegisterByName(Name, Reg))
473 return error(Twine("unknown register name '") + Name + "'");
474 break;
475 }
Alex Lorenz53464512015-07-10 22:51:20 +0000476 case MIToken::VirtualRegister: {
477 unsigned ID;
478 if (getUnsigned(ID))
479 return true;
480 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
481 if (RegInfo == PFS.VirtualRegisterSlots.end())
482 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
483 "'");
484 Reg = RegInfo->second;
485 break;
486 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000487 // TODO: Parse other register kinds.
488 default:
489 llvm_unreachable("The current token should be a register");
490 }
491 return false;
492}
493
Alex Lorenzcb268d42015-07-06 23:07:26 +0000494bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000495 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000496 switch (Token.kind()) {
497 case MIToken::kw_implicit:
498 Flags |= RegState::Implicit;
499 break;
500 case MIToken::kw_implicit_define:
501 Flags |= RegState::ImplicitDefine;
502 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000503 case MIToken::kw_dead:
504 Flags |= RegState::Dead;
505 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000506 case MIToken::kw_killed:
507 Flags |= RegState::Kill;
508 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000509 case MIToken::kw_undef:
510 Flags |= RegState::Undef;
511 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000512 case MIToken::kw_early_clobber:
513 Flags |= RegState::EarlyClobber;
514 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000515 case MIToken::kw_debug_use:
516 Flags |= RegState::Debug;
517 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000518 // TODO: parse the other register flags.
519 default:
520 llvm_unreachable("The current token should be a register flag");
521 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000522 if (OldFlags == Flags)
523 // We know that the same flag is specified more than once when the flags
524 // weren't modified.
525 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000526 lex();
527 return false;
528}
529
Alex Lorenz2eacca82015-07-13 23:24:34 +0000530bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
531 assert(Token.is(MIToken::colon));
532 lex();
533 if (Token.isNot(MIToken::Identifier))
534 return error("expected a subregister index after ':'");
535 auto Name = Token.stringValue();
536 SubReg = getSubRegIndex(Name);
537 if (!SubReg)
538 return error(Twine("use of unknown subregister index '") + Name + "'");
539 lex();
540 return false;
541}
542
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000543bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
544 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000545 unsigned Flags = IsDef ? RegState::Define : 0;
546 while (Token.isRegisterFlag()) {
547 if (parseRegisterFlag(Flags))
548 return true;
549 }
550 if (!Token.isRegister())
551 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000552 if (parseRegister(Reg))
553 return true;
554 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000555 unsigned SubReg = 0;
556 if (Token.is(MIToken::colon)) {
557 if (parseSubRegisterIndex(SubReg))
558 return true;
559 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000560 Dest = MachineOperand::CreateReg(
561 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000562 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000563 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000564 return false;
565}
566
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000567bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
568 assert(Token.is(MIToken::IntegerLiteral));
569 const APSInt &Int = Token.integerValue();
570 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +0000571 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000572 Dest = MachineOperand::CreateImm(Int.getExtValue());
573 lex();
574 return false;
575}
576
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000577bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
Alex Lorenz3fb77682015-08-06 23:17:42 +0000578 auto Source = StringRef(Loc, Token.range().end() - Loc).str();
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000579 lex();
580 SMDiagnostic Err;
581 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
582 if (!C)
583 return error(Loc + Err.getColumnNo(), Err.getMessage());
584 return false;
585}
586
Alex Lorenz05e38822015-08-05 18:52:21 +0000587bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
588 assert(Token.is(MIToken::IntegerType));
589 auto Loc = Token.location();
590 lex();
591 if (Token.isNot(MIToken::IntegerLiteral))
592 return error("expected an integer literal");
593 const Constant *C = nullptr;
594 if (parseIRConstant(Loc, C))
595 return true;
596 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
597 return false;
598}
599
Alex Lorenzad156fb2015-07-31 20:49:21 +0000600bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
601 auto Loc = Token.location();
602 lex();
603 if (Token.isNot(MIToken::FloatingPointLiteral))
604 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000605 const Constant *C = nullptr;
606 if (parseIRConstant(Loc, C))
607 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000608 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
609 return false;
610}
611
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000612bool MIParser::getUnsigned(unsigned &Result) {
613 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
614 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
615 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
616 if (Val64 == Limit)
617 return error("expected 32-bit integer (too large)");
618 Result = Val64;
619 return false;
620}
621
Alex Lorenzf09df002015-06-30 18:16:42 +0000622bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000623 assert(Token.is(MIToken::MachineBasicBlock));
624 unsigned Number;
625 if (getUnsigned(Number))
626 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000627 auto MBBInfo = PFS.MBBSlots.find(Number);
628 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000629 return error(Twine("use of undefined machine basic block #") +
630 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000631 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000632 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
633 return error(Twine("the name of machine basic block #") + Twine(Number) +
634 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000635 return false;
636}
637
638bool MIParser::parseMBBOperand(MachineOperand &Dest) {
639 MachineBasicBlock *MBB;
640 if (parseMBBReference(MBB))
641 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000642 Dest = MachineOperand::CreateMBB(MBB);
643 lex();
644 return false;
645}
646
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000647bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
648 assert(Token.is(MIToken::StackObject));
649 unsigned ID;
650 if (getUnsigned(ID))
651 return true;
652 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
653 if (ObjectInfo == PFS.StackObjectSlots.end())
654 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
655 "'");
656 StringRef Name;
657 if (const auto *Alloca =
658 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
659 Name = Alloca->getName();
660 if (!Token.stringValue().empty() && Token.stringValue() != Name)
661 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
662 "' isn't '" + Token.stringValue() + "'");
663 lex();
664 Dest = MachineOperand::CreateFI(ObjectInfo->second);
665 return false;
666}
667
Alex Lorenzea882122015-08-12 21:17:02 +0000668bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000669 assert(Token.is(MIToken::FixedStackObject));
670 unsigned ID;
671 if (getUnsigned(ID))
672 return true;
673 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
674 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
675 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
676 Twine(ID) + "'");
677 lex();
Alex Lorenzea882122015-08-12 21:17:02 +0000678 FI = ObjectInfo->second;
679 return false;
680}
681
682bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
683 int FI;
684 if (parseFixedStackFrameIndex(FI))
685 return true;
686 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000687 return false;
688}
689
Alex Lorenz41df7d32015-07-28 17:09:52 +0000690bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000691 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000692 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000693 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +0000694 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +0000695 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +0000696 return error(Twine("use of undefined global value '") + Token.range() +
697 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000698 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000699 }
700 case MIToken::GlobalValue: {
701 unsigned GVIdx;
702 if (getUnsigned(GVIdx))
703 return true;
704 if (GVIdx >= IRSlots.GlobalValues.size())
705 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
706 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000707 GV = IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000708 break;
709 }
710 default:
711 llvm_unreachable("The current token should be a global value");
712 }
Alex Lorenz41df7d32015-07-28 17:09:52 +0000713 return false;
714}
715
716bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
717 GlobalValue *GV = nullptr;
718 if (parseGlobalValue(GV))
719 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000720 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000721 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000722 if (parseOperandsOffset(Dest))
723 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000724 return false;
725}
726
Alex Lorenzab980492015-07-20 20:51:18 +0000727bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
728 assert(Token.is(MIToken::ConstantPoolItem));
729 unsigned ID;
730 if (getUnsigned(ID))
731 return true;
732 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
733 if (ConstantInfo == PFS.ConstantPoolSlots.end())
734 return error("use of undefined constant '%const." + Twine(ID) + "'");
735 lex();
Alex Lorenzab980492015-07-20 20:51:18 +0000736 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000737 if (parseOperandsOffset(Dest))
738 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000739 return false;
740}
741
Alex Lorenz31d70682015-07-15 23:38:35 +0000742bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
743 assert(Token.is(MIToken::JumpTableIndex));
744 unsigned ID;
745 if (getUnsigned(ID))
746 return true;
747 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
748 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
749 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
750 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000751 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
752 return false;
753}
754
Alex Lorenz6ede3742015-07-21 16:59:53 +0000755bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000756 assert(Token.is(MIToken::ExternalSymbol));
757 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000758 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +0000759 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +0000760 if (parseOperandsOffset(Dest))
761 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000762 return false;
763}
764
Alex Lorenz44f29252015-07-22 21:07:04 +0000765bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +0000766 assert(Token.is(MIToken::exclaim));
767 auto Loc = Token.location();
768 lex();
769 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
770 return error("expected metadata id after '!'");
771 unsigned ID;
772 if (getUnsigned(ID))
773 return true;
774 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
775 if (NodeInfo == IRSlots.MetadataNodes.end())
776 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
777 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +0000778 Node = NodeInfo->second.get();
779 return false;
780}
781
782bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
783 MDNode *Node = nullptr;
784 if (parseMDNode(Node))
785 return true;
786 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000787 return false;
788}
789
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000790bool MIParser::parseCFIOffset(int &Offset) {
791 if (Token.isNot(MIToken::IntegerLiteral))
792 return error("expected a cfi offset");
793 if (Token.integerValue().getMinSignedBits() > 32)
794 return error("expected a 32 bit integer (the cfi offset is too large)");
795 Offset = (int)Token.integerValue().getExtValue();
796 lex();
797 return false;
798}
799
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000800bool MIParser::parseCFIRegister(unsigned &Reg) {
801 if (Token.isNot(MIToken::NamedRegister))
802 return error("expected a cfi register");
803 unsigned LLVMReg;
804 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000805 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000806 const auto *TRI = MF.getSubtarget().getRegisterInfo();
807 assert(TRI && "Expected target register info");
808 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
809 if (DwarfReg < 0)
810 return error("invalid DWARF register");
811 Reg = (unsigned)DwarfReg;
812 lex();
813 return false;
814}
815
816bool MIParser::parseCFIOperand(MachineOperand &Dest) {
817 auto Kind = Token.kind();
818 lex();
819 auto &MMI = MF.getMMI();
820 int Offset;
821 unsigned Reg;
822 unsigned CFIIndex;
823 switch (Kind) {
824 case MIToken::kw_cfi_offset:
825 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
826 parseCFIOffset(Offset))
827 return true;
828 CFIIndex =
829 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
830 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000831 case MIToken::kw_cfi_def_cfa_register:
832 if (parseCFIRegister(Reg))
833 return true;
834 CFIIndex =
835 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
836 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000837 case MIToken::kw_cfi_def_cfa_offset:
838 if (parseCFIOffset(Offset))
839 return true;
840 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
841 CFIIndex = MMI.addFrameInst(
842 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
843 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000844 case MIToken::kw_cfi_def_cfa:
845 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
846 parseCFIOffset(Offset))
847 return true;
848 // NB: MCCFIInstruction::createDefCfa negates the offset.
849 CFIIndex =
850 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
851 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000852 default:
853 // TODO: Parse the other CFI operands.
854 llvm_unreachable("The current token should be a cfi operand");
855 }
856 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000857 return false;
858}
859
Alex Lorenzdeb53492015-07-28 17:28:03 +0000860bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
861 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000862 case MIToken::NamedIRBlock: {
863 BB = dyn_cast_or_null<BasicBlock>(
864 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +0000865 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +0000866 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +0000867 break;
868 }
869 case MIToken::IRBlock: {
870 unsigned SlotNumber = 0;
871 if (getUnsigned(SlotNumber))
872 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000873 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +0000874 if (!BB)
875 return error(Twine("use of undefined IR block '%ir-block.") +
876 Twine(SlotNumber) + "'");
877 break;
878 }
879 default:
880 llvm_unreachable("The current token should be an IR block reference");
881 }
882 return false;
883}
884
885bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
886 assert(Token.is(MIToken::kw_blockaddress));
887 lex();
888 if (expectAndConsume(MIToken::lparen))
889 return true;
890 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +0000891 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +0000892 return error("expected a global value");
893 GlobalValue *GV = nullptr;
894 if (parseGlobalValue(GV))
895 return true;
896 auto *F = dyn_cast<Function>(GV);
897 if (!F)
898 return error("expected an IR function reference");
899 lex();
900 if (expectAndConsume(MIToken::comma))
901 return true;
902 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +0000903 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +0000904 return error("expected an IR block reference");
905 if (parseIRBlock(BB, *F))
906 return true;
907 lex();
908 if (expectAndConsume(MIToken::rparen))
909 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000910 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000911 if (parseOperandsOffset(Dest))
912 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000913 return false;
914}
915
Alex Lorenzef5c1962015-07-28 23:02:45 +0000916bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
917 assert(Token.is(MIToken::kw_target_index));
918 lex();
919 if (expectAndConsume(MIToken::lparen))
920 return true;
921 if (Token.isNot(MIToken::Identifier))
922 return error("expected the name of the target index");
923 int Index = 0;
924 if (getTargetIndex(Token.stringValue(), Index))
925 return error("use of undefined target index '" + Token.stringValue() + "'");
926 lex();
927 if (expectAndConsume(MIToken::rparen))
928 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000929 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000930 if (parseOperandsOffset(Dest))
931 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000932 return false;
933}
934
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000935bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
936 assert(Token.is(MIToken::kw_liveout));
937 const auto *TRI = MF.getSubtarget().getRegisterInfo();
938 assert(TRI && "Expected target register info");
939 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
940 lex();
941 if (expectAndConsume(MIToken::lparen))
942 return true;
943 while (true) {
944 if (Token.isNot(MIToken::NamedRegister))
945 return error("expected a named register");
946 unsigned Reg = 0;
947 if (parseRegister(Reg))
948 return true;
949 lex();
950 Mask[Reg / 32] |= 1U << (Reg % 32);
951 // TODO: Report an error if the same register is used more than once.
952 if (Token.isNot(MIToken::comma))
953 break;
954 lex();
955 }
956 if (expectAndConsume(MIToken::rparen))
957 return true;
958 Dest = MachineOperand::CreateRegLiveOut(Mask);
959 return false;
960}
961
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000962bool MIParser::parseMachineOperand(MachineOperand &Dest) {
963 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000964 case MIToken::kw_implicit:
965 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000966 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000967 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000968 case MIToken::kw_undef:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000969 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +0000970 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000971 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000972 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000973 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000974 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000975 case MIToken::IntegerLiteral:
976 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +0000977 case MIToken::IntegerType:
978 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000979 case MIToken::kw_half:
980 case MIToken::kw_float:
981 case MIToken::kw_double:
982 case MIToken::kw_x86_fp80:
983 case MIToken::kw_fp128:
984 case MIToken::kw_ppc_fp128:
985 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000986 case MIToken::MachineBasicBlock:
987 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000988 case MIToken::StackObject:
989 return parseStackObjectOperand(Dest);
990 case MIToken::FixedStackObject:
991 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000992 case MIToken::GlobalValue:
993 case MIToken::NamedGlobalValue:
994 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000995 case MIToken::ConstantPoolItem:
996 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000997 case MIToken::JumpTableIndex:
998 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000999 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001000 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001001 case MIToken::exclaim:
1002 return parseMetadataOperand(Dest);
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001003 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001004 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001005 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001006 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001007 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001008 case MIToken::kw_blockaddress:
1009 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001010 case MIToken::kw_target_index:
1011 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001012 case MIToken::kw_liveout:
1013 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001014 case MIToken::Error:
1015 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001016 case MIToken::Identifier:
1017 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1018 Dest = MachineOperand::CreateRegMask(RegMask);
1019 lex();
1020 break;
1021 }
1022 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001023 default:
1024 // TODO: parse the other machine operands.
1025 return error("expected a machine operand");
1026 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001027 return false;
1028}
1029
Alex Lorenz49873a82015-08-06 00:44:07 +00001030bool MIParser::parseMachineOperandAndTargetFlags(MachineOperand &Dest) {
1031 unsigned TF = 0;
1032 bool HasTargetFlags = false;
1033 if (Token.is(MIToken::kw_target_flags)) {
1034 HasTargetFlags = true;
1035 lex();
1036 if (expectAndConsume(MIToken::lparen))
1037 return true;
1038 if (Token.isNot(MIToken::Identifier))
1039 return error("expected the name of the target flag");
1040 if (getDirectTargetFlag(Token.stringValue(), TF))
1041 return error("use of undefined target flag '" + Token.stringValue() +
1042 "'");
1043 lex();
1044 // TODO: Parse target's bit target flags.
1045 if (expectAndConsume(MIToken::rparen))
1046 return true;
1047 }
1048 auto Loc = Token.location();
1049 if (parseMachineOperand(Dest))
1050 return true;
1051 if (!HasTargetFlags)
1052 return false;
1053 if (Dest.isReg())
1054 return error(Loc, "register operands can't have target flags");
1055 Dest.setTargetFlags(TF);
1056 return false;
1057}
1058
Alex Lorenzdc24c172015-08-07 20:21:00 +00001059bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001060 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1061 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001062 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001063 bool IsNegative = Token.is(MIToken::minus);
1064 lex();
1065 if (Token.isNot(MIToken::IntegerLiteral))
1066 return error("expected an integer literal after '" + Sign + "'");
1067 if (Token.integerValue().getMinSignedBits() > 64)
1068 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001069 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001070 if (IsNegative)
1071 Offset = -Offset;
1072 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001073 return false;
1074}
1075
1076bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1077 int64_t Offset = 0;
1078 if (parseOffset(Offset))
1079 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001080 Op.setOffset(Offset);
1081 return false;
1082}
1083
Alex Lorenz4af7e612015-08-03 23:08:19 +00001084bool MIParser::parseIRValue(Value *&V) {
1085 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001086 case MIToken::NamedIRValue: {
1087 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001088 if (!V)
Alex Lorenz2791dcc2015-08-12 21:27:16 +00001089 V = MF.getFunction()->getParent()->getValueSymbolTable().lookup(
1090 Token.stringValue());
1091 if (!V)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001092 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001093 break;
1094 }
1095 // TODO: Parse unnamed IR value references.
1096 default:
1097 llvm_unreachable("The current token should be an IR block reference");
1098 }
1099 return false;
1100}
1101
1102bool MIParser::getUint64(uint64_t &Result) {
1103 assert(Token.hasIntegerValue());
1104 if (Token.integerValue().getActiveBits() > 64)
1105 return error("expected 64-bit integer (too large)");
1106 Result = Token.integerValue().getZExtValue();
1107 return false;
1108}
1109
Alex Lorenza518b792015-08-04 00:24:45 +00001110bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
Alex Lorenze86d5152015-08-06 18:26:36 +00001111 const unsigned OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001112 switch (Token.kind()) {
1113 case MIToken::kw_volatile:
1114 Flags |= MachineMemOperand::MOVolatile;
1115 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001116 case MIToken::kw_non_temporal:
1117 Flags |= MachineMemOperand::MONonTemporal;
1118 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001119 case MIToken::kw_invariant:
1120 Flags |= MachineMemOperand::MOInvariant;
1121 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001122 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001123 default:
1124 llvm_unreachable("The current token should be a memory operand flag");
1125 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001126 if (OldFlags == Flags)
1127 // We know that the same flag is specified more than once when the flags
1128 // weren't modified.
1129 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001130 lex();
1131 return false;
1132}
1133
Alex Lorenz91097a32015-08-12 20:33:26 +00001134bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1135 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001136 case MIToken::kw_stack:
1137 PSV = MF.getPSVManager().getStack();
1138 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001139 case MIToken::kw_got:
1140 PSV = MF.getPSVManager().getGOT();
1141 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001142 case MIToken::kw_jump_table:
1143 PSV = MF.getPSVManager().getJumpTable();
1144 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001145 case MIToken::kw_constant_pool:
1146 PSV = MF.getPSVManager().getConstantPool();
1147 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001148 case MIToken::FixedStackObject: {
1149 int FI;
1150 if (parseFixedStackFrameIndex(FI))
1151 return true;
1152 PSV = MF.getPSVManager().getFixedStack(FI);
1153 // The token was already consumed, so use return here instead of break.
1154 return false;
1155 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001156 // TODO: Parse the other pseudo source values.
1157 default:
1158 llvm_unreachable("The current token should be pseudo source value");
1159 }
1160 lex();
1161 return false;
1162}
1163
1164bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001165 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001166 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
1167 Token.is(MIToken::FixedStackObject)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001168 const PseudoSourceValue *PSV = nullptr;
1169 if (parseMemoryPseudoSourceValue(PSV))
1170 return true;
1171 int64_t Offset = 0;
1172 if (parseOffset(Offset))
1173 return true;
1174 Dest = MachinePointerInfo(PSV, Offset);
1175 return false;
1176 }
1177 if (Token.isNot(MIToken::NamedIRValue))
1178 return error("expected an IR value reference");
1179 Value *V = nullptr;
1180 if (parseIRValue(V))
1181 return true;
1182 if (!V->getType()->isPointerTy())
1183 return error("expected a pointer IR value");
1184 lex();
1185 int64_t Offset = 0;
1186 if (parseOffset(Offset))
1187 return true;
1188 Dest = MachinePointerInfo(V, Offset);
1189 return false;
1190}
1191
Alex Lorenz4af7e612015-08-03 23:08:19 +00001192bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1193 if (expectAndConsume(MIToken::lparen))
1194 return true;
Alex Lorenza518b792015-08-04 00:24:45 +00001195 unsigned Flags = 0;
1196 while (Token.isMemoryOperandFlag()) {
1197 if (parseMemoryOperandFlag(Flags))
1198 return true;
1199 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001200 if (Token.isNot(MIToken::Identifier) ||
1201 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1202 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001203 if (Token.stringValue() == "load")
1204 Flags |= MachineMemOperand::MOLoad;
1205 else
1206 Flags |= MachineMemOperand::MOStore;
1207 lex();
1208
1209 if (Token.isNot(MIToken::IntegerLiteral))
1210 return error("expected the size integer literal after memory operation");
1211 uint64_t Size;
1212 if (getUint64(Size))
1213 return true;
1214 lex();
1215
1216 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1217 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1218 return error(Twine("expected '") + Word + "'");
1219 lex();
1220
Alex Lorenz91097a32015-08-12 20:33:26 +00001221 MachinePointerInfo Ptr = MachinePointerInfo();
1222 if (parseMachinePointerInfo(Ptr))
Alex Lorenz83127732015-08-07 20:26:52 +00001223 return true;
Alex Lorenz61420f72015-08-07 20:48:30 +00001224 unsigned BaseAlignment = Size;
1225 if (Token.is(MIToken::comma)) {
1226 lex();
1227 if (Token.isNot(MIToken::kw_align))
1228 return error("expected 'align'");
1229 lex();
1230 if (Token.isNot(MIToken::IntegerLiteral))
1231 return error("expected an integer literal after 'align'");
1232 if (getUnsigned(BaseAlignment))
1233 return true;
1234 lex();
1235 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001236 // TODO: Parse the attached metadata nodes.
1237 if (expectAndConsume(MIToken::rparen))
1238 return true;
Alex Lorenz91097a32015-08-12 20:33:26 +00001239 Dest = MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001240 return false;
1241}
1242
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001243void MIParser::initNames2InstrOpCodes() {
1244 if (!Names2InstrOpCodes.empty())
1245 return;
1246 const auto *TII = MF.getSubtarget().getInstrInfo();
1247 assert(TII && "Expected target instruction info");
1248 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1249 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1250}
1251
1252bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1253 initNames2InstrOpCodes();
1254 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1255 if (InstrInfo == Names2InstrOpCodes.end())
1256 return true;
1257 OpCode = InstrInfo->getValue();
1258 return false;
1259}
1260
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001261void MIParser::initNames2Regs() {
1262 if (!Names2Regs.empty())
1263 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001264 // The '%noreg' register is the register 0.
1265 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001266 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1267 assert(TRI && "Expected target register info");
1268 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1269 bool WasInserted =
1270 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1271 .second;
1272 (void)WasInserted;
1273 assert(WasInserted && "Expected registers to be unique case-insensitively");
1274 }
1275}
1276
1277bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1278 initNames2Regs();
1279 auto RegInfo = Names2Regs.find(RegName);
1280 if (RegInfo == Names2Regs.end())
1281 return true;
1282 Reg = RegInfo->getValue();
1283 return false;
1284}
1285
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001286void MIParser::initNames2RegMasks() {
1287 if (!Names2RegMasks.empty())
1288 return;
1289 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1290 assert(TRI && "Expected target register info");
1291 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1292 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1293 assert(RegMasks.size() == RegMaskNames.size());
1294 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1295 Names2RegMasks.insert(
1296 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1297}
1298
1299const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1300 initNames2RegMasks();
1301 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1302 if (RegMaskInfo == Names2RegMasks.end())
1303 return nullptr;
1304 return RegMaskInfo->getValue();
1305}
1306
Alex Lorenz2eacca82015-07-13 23:24:34 +00001307void MIParser::initNames2SubRegIndices() {
1308 if (!Names2SubRegIndices.empty())
1309 return;
1310 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1311 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1312 Names2SubRegIndices.insert(
1313 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1314}
1315
1316unsigned MIParser::getSubRegIndex(StringRef Name) {
1317 initNames2SubRegIndices();
1318 auto SubRegInfo = Names2SubRegIndices.find(Name);
1319 if (SubRegInfo == Names2SubRegIndices.end())
1320 return 0;
1321 return SubRegInfo->getValue();
1322}
1323
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001324static void initSlots2BasicBlocks(
1325 const Function &F,
1326 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1327 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001328 MST.incorporateFunction(F);
1329 for (auto &BB : F) {
1330 if (BB.hasName())
1331 continue;
1332 int Slot = MST.getLocalSlot(&BB);
1333 if (Slot == -1)
1334 continue;
1335 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1336 }
1337}
1338
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001339static const BasicBlock *getIRBlockFromSlot(
1340 unsigned Slot,
1341 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001342 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1343 if (BlockInfo == Slots2BasicBlocks.end())
1344 return nullptr;
1345 return BlockInfo->second;
1346}
1347
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001348const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1349 if (Slots2BasicBlocks.empty())
1350 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1351 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1352}
1353
1354const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1355 if (&F == MF.getFunction())
1356 return getIRBlock(Slot);
1357 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1358 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1359 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1360}
1361
Alex Lorenzef5c1962015-07-28 23:02:45 +00001362void MIParser::initNames2TargetIndices() {
1363 if (!Names2TargetIndices.empty())
1364 return;
1365 const auto *TII = MF.getSubtarget().getInstrInfo();
1366 assert(TII && "Expected target instruction info");
1367 auto Indices = TII->getSerializableTargetIndices();
1368 for (const auto &I : Indices)
1369 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1370}
1371
1372bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1373 initNames2TargetIndices();
1374 auto IndexInfo = Names2TargetIndices.find(Name);
1375 if (IndexInfo == Names2TargetIndices.end())
1376 return true;
1377 Index = IndexInfo->second;
1378 return false;
1379}
1380
Alex Lorenz49873a82015-08-06 00:44:07 +00001381void MIParser::initNames2DirectTargetFlags() {
1382 if (!Names2DirectTargetFlags.empty())
1383 return;
1384 const auto *TII = MF.getSubtarget().getInstrInfo();
1385 assert(TII && "Expected target instruction info");
1386 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1387 for (const auto &I : Flags)
1388 Names2DirectTargetFlags.insert(
1389 std::make_pair(StringRef(I.second), I.first));
1390}
1391
1392bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1393 initNames2DirectTargetFlags();
1394 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1395 if (FlagInfo == Names2DirectTargetFlags.end())
1396 return true;
1397 Flag = FlagInfo->second;
1398 return false;
1399}
1400
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001401bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
1402 MachineFunction &MF, StringRef Src,
1403 const PerFunctionMIParsingState &PFS,
1404 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1405 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001406}
Alex Lorenzf09df002015-06-30 18:16:42 +00001407
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001408bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1409 MachineFunction &MF, StringRef Src,
1410 const PerFunctionMIParsingState &PFS,
1411 const SlotMapping &IRSlots, SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001412 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00001413}
Alex Lorenz9fab3702015-07-14 21:24:41 +00001414
1415bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1416 MachineFunction &MF, StringRef Src,
1417 const PerFunctionMIParsingState &PFS,
1418 const SlotMapping &IRSlots,
1419 SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001420 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1421 .parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00001422}
Alex Lorenz12045a42015-07-27 17:42:45 +00001423
1424bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1425 MachineFunction &MF, StringRef Src,
1426 const PerFunctionMIParsingState &PFS,
1427 const SlotMapping &IRSlots,
1428 SMDiagnostic &Error) {
1429 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1430 .parseStandaloneVirtualRegister(Reg);
1431}
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001432
1433bool llvm::parseIRBlockReference(const BasicBlock *&BB, SourceMgr &SM,
1434 MachineFunction &MF, StringRef Src,
1435 const PerFunctionMIParsingState &PFS,
1436 const SlotMapping &IRSlots,
1437 SMDiagnostic &Error) {
1438 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1439 .parseStandaloneIRBlockReference(BB);
1440}