blob: 6dae29a58e3b2853a4aab3d4b25103c568b5bf4d [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 Lorenzb97c9ef2015-08-10 23:24:42 +0000124 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000125 bool parseMachineOperand(MachineOperand &Dest);
Alex Lorenz49873a82015-08-06 00:44:07 +0000126 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000127 bool parseOffset(int64_t &Offset);
Alex Lorenz5672a892015-08-05 22:26:15 +0000128 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000129 bool parseIRValue(Value *&V);
Alex Lorenza518b792015-08-04 00:24:45 +0000130 bool parseMemoryOperandFlag(unsigned &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000131 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
132 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000133 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000134
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000135private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000136 /// Convert the integer literal in the current token into an unsigned integer.
137 ///
138 /// Return true if an error occurred.
139 bool getUnsigned(unsigned &Result);
140
Alex Lorenz4af7e612015-08-03 23:08:19 +0000141 /// Convert the integer literal in the current token into an uint64.
142 ///
143 /// Return true if an error occurred.
144 bool getUint64(uint64_t &Result);
145
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000146 /// If the current token is of the given kind, consume it and return false.
147 /// Otherwise report an error and return true.
148 bool expectAndConsume(MIToken::TokenKind TokenKind);
149
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000150 void initNames2InstrOpCodes();
151
152 /// Try to convert an instruction name to an opcode. Return true if the
153 /// instruction name is invalid.
154 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000155
Alex Lorenze5a44662015-07-17 00:24:15 +0000156 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000157
Alex Lorenz36962cd2015-07-07 02:08:46 +0000158 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
159 const MCInstrDesc &MCID);
160
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000161 void initNames2Regs();
162
163 /// Try to convert a register name to a register number. Return true if the
164 /// register name is invalid.
165 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000166
167 void initNames2RegMasks();
168
169 /// Check if the given identifier is a name of a register mask.
170 ///
171 /// Return null if the identifier isn't a register mask.
172 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000173
174 void initNames2SubRegIndices();
175
176 /// Check if the given identifier is a name of a subregister index.
177 ///
178 /// Return 0 if the name isn't a subregister index class.
179 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000180
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000181 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000182 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000183
184 void initNames2TargetIndices();
185
186 /// Try to convert a name of target index to the corresponding target index.
187 ///
188 /// Return true if the name isn't a name of a target index.
189 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000190
191 void initNames2DirectTargetFlags();
192
193 /// Try to convert a name of a direct target flag to the corresponding
194 /// target flag.
195 ///
196 /// Return true if the name isn't a name of a direct flag.
197 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000198};
199
200} // end anonymous namespace
201
202MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000203 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000204 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000205 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz3fb77682015-08-06 23:17:42 +0000206 PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000207
Alex Lorenz91370c52015-06-22 20:37:46 +0000208void MIParser::lex() {
209 CurrentSource = lexMIToken(
210 CurrentSource, Token,
211 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
212}
213
214bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
215
216bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000217 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
218 Error = SMDiagnostic(
219 SM, SMLoc(),
220 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
221 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000222 return true;
223}
224
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000225static const char *toString(MIToken::TokenKind TokenKind) {
226 switch (TokenKind) {
227 case MIToken::comma:
228 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000229 case MIToken::equal:
230 return "'='";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000231 case MIToken::lparen:
232 return "'('";
233 case MIToken::rparen:
234 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000235 default:
236 return "<unknown token>";
237 }
238}
239
240bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
241 if (Token.isNot(TokenKind))
242 return error(Twine("expected ") + toString(TokenKind));
243 lex();
244 return false;
245}
246
Alex Lorenz3708a642015-06-30 17:47:50 +0000247bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000248 lex();
249
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000250 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000251 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000252 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000253 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000254 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000255 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000256 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000257 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000258 if (Token.isNot(MIToken::comma))
259 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000260 lex();
261 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000262 if (!Operands.empty() && expectAndConsume(MIToken::equal))
263 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000264
Alex Lorenze5a44662015-07-17 00:24:15 +0000265 unsigned OpCode, Flags = 0;
266 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000267 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000268
Alex Lorenz4af7e612015-08-03 23:08:19 +0000269 // TODO: Parse the bundle instruction flags.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000270
271 // Parse the remaining machine operands.
Alex Lorenz4af7e612015-08-03 23:08:19 +0000272 while (Token.isNot(MIToken::Eof) && Token.isNot(MIToken::kw_debug_location) &&
273 Token.isNot(MIToken::coloncolon)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000274 auto Loc = Token.location();
Alex Lorenz49873a82015-08-06 00:44:07 +0000275 if (parseMachineOperandAndTargetFlags(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000276 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000277 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000278 if (Token.is(MIToken::Eof) || Token.is(MIToken::coloncolon))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000279 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000280 if (Token.isNot(MIToken::comma))
281 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000282 lex();
283 }
284
Alex Lorenz46d760d2015-07-22 21:15:11 +0000285 DebugLoc DebugLocation;
286 if (Token.is(MIToken::kw_debug_location)) {
287 lex();
288 if (Token.isNot(MIToken::exclaim))
289 return error("expected a metadata node after 'debug-location'");
290 MDNode *Node = nullptr;
291 if (parseMDNode(Node))
292 return true;
293 DebugLocation = DebugLoc(Node);
294 }
295
Alex Lorenz4af7e612015-08-03 23:08:19 +0000296 // Parse the machine memory operands.
297 SmallVector<MachineMemOperand *, 2> MemOperands;
298 if (Token.is(MIToken::coloncolon)) {
299 lex();
300 while (Token.isNot(MIToken::Eof)) {
301 MachineMemOperand *MemOp = nullptr;
302 if (parseMachineMemoryOperand(MemOp))
303 return true;
304 MemOperands.push_back(MemOp);
305 if (Token.is(MIToken::Eof))
306 break;
307 if (Token.isNot(MIToken::comma))
308 return error("expected ',' before the next machine memory operand");
309 lex();
310 }
311 }
312
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000313 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000314 if (!MCID.isVariadic()) {
315 // FIXME: Move the implicit operand verification to the machine verifier.
316 if (verifyImplicitOperands(Operands, MCID))
317 return true;
318 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000319
Alex Lorenzcb268d42015-07-06 23:07:26 +0000320 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000321 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000322 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000323 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000324 MI->addOperand(MF, Operand.Operand);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000325 if (MemOperands.empty())
326 return false;
327 MachineInstr::mmo_iterator MemRefs =
328 MF.allocateMemRefsArray(MemOperands.size());
329 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
330 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000331 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000332}
333
Alex Lorenz1ea60892015-07-27 20:29:27 +0000334bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000335 lex();
336 if (Token.isNot(MIToken::MachineBasicBlock))
337 return error("expected a machine basic block reference");
338 if (parseMBBReference(MBB))
339 return true;
340 lex();
341 if (Token.isNot(MIToken::Eof))
342 return error(
343 "expected end of string after the machine basic block reference");
344 return false;
345}
346
Alex Lorenz1ea60892015-07-27 20:29:27 +0000347bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000348 lex();
349 if (Token.isNot(MIToken::NamedRegister))
350 return error("expected a named register");
351 if (parseRegister(Reg))
352 return 0;
353 lex();
354 if (Token.isNot(MIToken::Eof))
355 return error("expected end of string after the register reference");
356 return false;
357}
358
Alex Lorenz12045a42015-07-27 17:42:45 +0000359bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
360 lex();
361 if (Token.isNot(MIToken::VirtualRegister))
362 return error("expected a virtual register");
363 if (parseRegister(Reg))
364 return 0;
365 lex();
366 if (Token.isNot(MIToken::Eof))
367 return error("expected end of string after the register reference");
368 return false;
369}
370
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000371bool MIParser::parseStandaloneIRBlockReference(const BasicBlock *&BB) {
372 lex();
373 if (Token.isNot(MIToken::IRBlock))
374 return error("expected an IR block reference");
375 unsigned SlotNumber = 0;
376 if (getUnsigned(SlotNumber))
377 return true;
378 BB = getIRBlock(SlotNumber);
379 if (!BB)
380 return error(Twine("use of undefined IR block '%ir-block.") +
381 Twine(SlotNumber) + "'");
382 lex();
383 if (Token.isNot(MIToken::Eof))
384 return error("expected end of string after the IR block reference");
385 return false;
386}
387
Alex Lorenz36962cd2015-07-07 02:08:46 +0000388static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
389 assert(MO.isImplicit());
390 return MO.isDef() ? "implicit-def" : "implicit";
391}
392
393static std::string getRegisterName(const TargetRegisterInfo *TRI,
394 unsigned Reg) {
395 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
396 return StringRef(TRI->getName(Reg)).lower();
397}
398
399bool MIParser::verifyImplicitOperands(
400 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
401 if (MCID.isCall())
402 // We can't verify call instructions as they can contain arbitrary implicit
403 // register and register mask operands.
404 return false;
405
406 // Gather all the expected implicit operands.
407 SmallVector<MachineOperand, 4> ImplicitOperands;
408 if (MCID.ImplicitDefs)
409 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
410 ImplicitOperands.push_back(
411 MachineOperand::CreateReg(*ImpDefs, true, true));
412 if (MCID.ImplicitUses)
413 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
414 ImplicitOperands.push_back(
415 MachineOperand::CreateReg(*ImpUses, false, true));
416
417 const auto *TRI = MF.getSubtarget().getRegisterInfo();
418 assert(TRI && "Expected target register info");
419 size_t I = ImplicitOperands.size(), J = Operands.size();
420 while (I) {
421 --I;
422 if (J) {
423 --J;
424 const auto &ImplicitOperand = ImplicitOperands[I];
425 const auto &Operand = Operands[J].Operand;
426 if (ImplicitOperand.isIdenticalTo(Operand))
427 continue;
428 if (Operand.isReg() && Operand.isImplicit()) {
429 return error(Operands[J].Begin,
430 Twine("expected an implicit register operand '") +
431 printImplicitRegisterFlag(ImplicitOperand) + " %" +
432 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
433 }
434 }
435 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
436 // insead of reporting an error at this location:
437 // %eax = MOV32r0
438 // ^
439 // report the error at the following location:
440 // %eax = MOV32r0
441 // ^
442 return error(J < Operands.size() ? Operands[J].End : Token.location(),
443 Twine("missing implicit register operand '") +
444 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
445 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
446 }
447 return false;
448}
449
Alex Lorenze5a44662015-07-17 00:24:15 +0000450bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
451 if (Token.is(MIToken::kw_frame_setup)) {
452 Flags |= MachineInstr::FrameSetup;
453 lex();
454 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000455 if (Token.isNot(MIToken::Identifier))
456 return error("expected a machine instruction");
457 StringRef InstrName = Token.stringValue();
458 if (parseInstrName(InstrName, OpCode))
459 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000460 lex();
461 return false;
462}
463
464bool MIParser::parseRegister(unsigned &Reg) {
465 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000466 case MIToken::underscore:
467 Reg = 0;
468 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000469 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000470 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000471 if (getRegisterByName(Name, Reg))
472 return error(Twine("unknown register name '") + Name + "'");
473 break;
474 }
Alex Lorenz53464512015-07-10 22:51:20 +0000475 case MIToken::VirtualRegister: {
476 unsigned ID;
477 if (getUnsigned(ID))
478 return true;
479 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
480 if (RegInfo == PFS.VirtualRegisterSlots.end())
481 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
482 "'");
483 Reg = RegInfo->second;
484 break;
485 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000486 // TODO: Parse other register kinds.
487 default:
488 llvm_unreachable("The current token should be a register");
489 }
490 return false;
491}
492
Alex Lorenzcb268d42015-07-06 23:07:26 +0000493bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000494 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000495 switch (Token.kind()) {
496 case MIToken::kw_implicit:
497 Flags |= RegState::Implicit;
498 break;
499 case MIToken::kw_implicit_define:
500 Flags |= RegState::ImplicitDefine;
501 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000502 case MIToken::kw_dead:
503 Flags |= RegState::Dead;
504 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000505 case MIToken::kw_killed:
506 Flags |= RegState::Kill;
507 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000508 case MIToken::kw_undef:
509 Flags |= RegState::Undef;
510 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000511 case MIToken::kw_early_clobber:
512 Flags |= RegState::EarlyClobber;
513 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000514 case MIToken::kw_debug_use:
515 Flags |= RegState::Debug;
516 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000517 // TODO: parse the other register flags.
518 default:
519 llvm_unreachable("The current token should be a register flag");
520 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000521 if (OldFlags == Flags)
522 // We know that the same flag is specified more than once when the flags
523 // weren't modified.
524 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000525 lex();
526 return false;
527}
528
Alex Lorenz2eacca82015-07-13 23:24:34 +0000529bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
530 assert(Token.is(MIToken::colon));
531 lex();
532 if (Token.isNot(MIToken::Identifier))
533 return error("expected a subregister index after ':'");
534 auto Name = Token.stringValue();
535 SubReg = getSubRegIndex(Name);
536 if (!SubReg)
537 return error(Twine("use of unknown subregister index '") + Name + "'");
538 lex();
539 return false;
540}
541
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000542bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
543 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000544 unsigned Flags = IsDef ? RegState::Define : 0;
545 while (Token.isRegisterFlag()) {
546 if (parseRegisterFlag(Flags))
547 return true;
548 }
549 if (!Token.isRegister())
550 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000551 if (parseRegister(Reg))
552 return true;
553 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000554 unsigned SubReg = 0;
555 if (Token.is(MIToken::colon)) {
556 if (parseSubRegisterIndex(SubReg))
557 return true;
558 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000559 Dest = MachineOperand::CreateReg(
560 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000561 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000562 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000563 return false;
564}
565
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000566bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
567 assert(Token.is(MIToken::IntegerLiteral));
568 const APSInt &Int = Token.integerValue();
569 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +0000570 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000571 Dest = MachineOperand::CreateImm(Int.getExtValue());
572 lex();
573 return false;
574}
575
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000576bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
Alex Lorenz3fb77682015-08-06 23:17:42 +0000577 auto Source = StringRef(Loc, Token.range().end() - Loc).str();
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000578 lex();
579 SMDiagnostic Err;
580 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
581 if (!C)
582 return error(Loc + Err.getColumnNo(), Err.getMessage());
583 return false;
584}
585
Alex Lorenz05e38822015-08-05 18:52:21 +0000586bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
587 assert(Token.is(MIToken::IntegerType));
588 auto Loc = Token.location();
589 lex();
590 if (Token.isNot(MIToken::IntegerLiteral))
591 return error("expected an integer literal");
592 const Constant *C = nullptr;
593 if (parseIRConstant(Loc, C))
594 return true;
595 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
596 return false;
597}
598
Alex Lorenzad156fb2015-07-31 20:49:21 +0000599bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
600 auto Loc = Token.location();
601 lex();
602 if (Token.isNot(MIToken::FloatingPointLiteral))
603 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000604 const Constant *C = nullptr;
605 if (parseIRConstant(Loc, C))
606 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000607 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
608 return false;
609}
610
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000611bool MIParser::getUnsigned(unsigned &Result) {
612 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
613 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
614 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
615 if (Val64 == Limit)
616 return error("expected 32-bit integer (too large)");
617 Result = Val64;
618 return false;
619}
620
Alex Lorenzf09df002015-06-30 18:16:42 +0000621bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000622 assert(Token.is(MIToken::MachineBasicBlock));
623 unsigned Number;
624 if (getUnsigned(Number))
625 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000626 auto MBBInfo = PFS.MBBSlots.find(Number);
627 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000628 return error(Twine("use of undefined machine basic block #") +
629 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000630 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000631 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
632 return error(Twine("the name of machine basic block #") + Twine(Number) +
633 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000634 return false;
635}
636
637bool MIParser::parseMBBOperand(MachineOperand &Dest) {
638 MachineBasicBlock *MBB;
639 if (parseMBBReference(MBB))
640 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000641 Dest = MachineOperand::CreateMBB(MBB);
642 lex();
643 return false;
644}
645
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000646bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
647 assert(Token.is(MIToken::StackObject));
648 unsigned ID;
649 if (getUnsigned(ID))
650 return true;
651 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
652 if (ObjectInfo == PFS.StackObjectSlots.end())
653 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
654 "'");
655 StringRef Name;
656 if (const auto *Alloca =
657 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
658 Name = Alloca->getName();
659 if (!Token.stringValue().empty() && Token.stringValue() != Name)
660 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
661 "' isn't '" + Token.stringValue() + "'");
662 lex();
663 Dest = MachineOperand::CreateFI(ObjectInfo->second);
664 return false;
665}
666
667bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
668 assert(Token.is(MIToken::FixedStackObject));
669 unsigned ID;
670 if (getUnsigned(ID))
671 return true;
672 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
673 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
674 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
675 Twine(ID) + "'");
676 lex();
677 Dest = MachineOperand::CreateFI(ObjectInfo->second);
678 return false;
679}
680
Alex Lorenz41df7d32015-07-28 17:09:52 +0000681bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000682 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000683 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000684 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +0000685 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +0000686 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +0000687 return error(Twine("use of undefined global value '") + Token.range() +
688 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000689 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000690 }
691 case MIToken::GlobalValue: {
692 unsigned GVIdx;
693 if (getUnsigned(GVIdx))
694 return true;
695 if (GVIdx >= IRSlots.GlobalValues.size())
696 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
697 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000698 GV = IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000699 break;
700 }
701 default:
702 llvm_unreachable("The current token should be a global value");
703 }
Alex Lorenz41df7d32015-07-28 17:09:52 +0000704 return false;
705}
706
707bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
708 GlobalValue *GV = nullptr;
709 if (parseGlobalValue(GV))
710 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000711 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000712 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000713 if (parseOperandsOffset(Dest))
714 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000715 return false;
716}
717
Alex Lorenzab980492015-07-20 20:51:18 +0000718bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
719 assert(Token.is(MIToken::ConstantPoolItem));
720 unsigned ID;
721 if (getUnsigned(ID))
722 return true;
723 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
724 if (ConstantInfo == PFS.ConstantPoolSlots.end())
725 return error("use of undefined constant '%const." + Twine(ID) + "'");
726 lex();
Alex Lorenzab980492015-07-20 20:51:18 +0000727 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000728 if (parseOperandsOffset(Dest))
729 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000730 return false;
731}
732
Alex Lorenz31d70682015-07-15 23:38:35 +0000733bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
734 assert(Token.is(MIToken::JumpTableIndex));
735 unsigned ID;
736 if (getUnsigned(ID))
737 return true;
738 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
739 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
740 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
741 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000742 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
743 return false;
744}
745
Alex Lorenz6ede3742015-07-21 16:59:53 +0000746bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000747 assert(Token.is(MIToken::ExternalSymbol));
748 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000749 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +0000750 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +0000751 if (parseOperandsOffset(Dest))
752 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000753 return false;
754}
755
Alex Lorenz44f29252015-07-22 21:07:04 +0000756bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +0000757 assert(Token.is(MIToken::exclaim));
758 auto Loc = Token.location();
759 lex();
760 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
761 return error("expected metadata id after '!'");
762 unsigned ID;
763 if (getUnsigned(ID))
764 return true;
765 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
766 if (NodeInfo == IRSlots.MetadataNodes.end())
767 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
768 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +0000769 Node = NodeInfo->second.get();
770 return false;
771}
772
773bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
774 MDNode *Node = nullptr;
775 if (parseMDNode(Node))
776 return true;
777 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000778 return false;
779}
780
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000781bool MIParser::parseCFIOffset(int &Offset) {
782 if (Token.isNot(MIToken::IntegerLiteral))
783 return error("expected a cfi offset");
784 if (Token.integerValue().getMinSignedBits() > 32)
785 return error("expected a 32 bit integer (the cfi offset is too large)");
786 Offset = (int)Token.integerValue().getExtValue();
787 lex();
788 return false;
789}
790
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000791bool MIParser::parseCFIRegister(unsigned &Reg) {
792 if (Token.isNot(MIToken::NamedRegister))
793 return error("expected a cfi register");
794 unsigned LLVMReg;
795 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000796 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000797 const auto *TRI = MF.getSubtarget().getRegisterInfo();
798 assert(TRI && "Expected target register info");
799 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
800 if (DwarfReg < 0)
801 return error("invalid DWARF register");
802 Reg = (unsigned)DwarfReg;
803 lex();
804 return false;
805}
806
807bool MIParser::parseCFIOperand(MachineOperand &Dest) {
808 auto Kind = Token.kind();
809 lex();
810 auto &MMI = MF.getMMI();
811 int Offset;
812 unsigned Reg;
813 unsigned CFIIndex;
814 switch (Kind) {
815 case MIToken::kw_cfi_offset:
816 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
817 parseCFIOffset(Offset))
818 return true;
819 CFIIndex =
820 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
821 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000822 case MIToken::kw_cfi_def_cfa_register:
823 if (parseCFIRegister(Reg))
824 return true;
825 CFIIndex =
826 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
827 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000828 case MIToken::kw_cfi_def_cfa_offset:
829 if (parseCFIOffset(Offset))
830 return true;
831 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
832 CFIIndex = MMI.addFrameInst(
833 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
834 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000835 case MIToken::kw_cfi_def_cfa:
836 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
837 parseCFIOffset(Offset))
838 return true;
839 // NB: MCCFIInstruction::createDefCfa negates the offset.
840 CFIIndex =
841 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
842 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000843 default:
844 // TODO: Parse the other CFI operands.
845 llvm_unreachable("The current token should be a cfi operand");
846 }
847 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000848 return false;
849}
850
Alex Lorenzdeb53492015-07-28 17:28:03 +0000851bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
852 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000853 case MIToken::NamedIRBlock: {
854 BB = dyn_cast_or_null<BasicBlock>(
855 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +0000856 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +0000857 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +0000858 break;
859 }
860 case MIToken::IRBlock: {
861 unsigned SlotNumber = 0;
862 if (getUnsigned(SlotNumber))
863 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000864 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +0000865 if (!BB)
866 return error(Twine("use of undefined IR block '%ir-block.") +
867 Twine(SlotNumber) + "'");
868 break;
869 }
870 default:
871 llvm_unreachable("The current token should be an IR block reference");
872 }
873 return false;
874}
875
876bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
877 assert(Token.is(MIToken::kw_blockaddress));
878 lex();
879 if (expectAndConsume(MIToken::lparen))
880 return true;
881 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +0000882 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +0000883 return error("expected a global value");
884 GlobalValue *GV = nullptr;
885 if (parseGlobalValue(GV))
886 return true;
887 auto *F = dyn_cast<Function>(GV);
888 if (!F)
889 return error("expected an IR function reference");
890 lex();
891 if (expectAndConsume(MIToken::comma))
892 return true;
893 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +0000894 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +0000895 return error("expected an IR block reference");
896 if (parseIRBlock(BB, *F))
897 return true;
898 lex();
899 if (expectAndConsume(MIToken::rparen))
900 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000901 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000902 if (parseOperandsOffset(Dest))
903 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000904 return false;
905}
906
Alex Lorenzef5c1962015-07-28 23:02:45 +0000907bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
908 assert(Token.is(MIToken::kw_target_index));
909 lex();
910 if (expectAndConsume(MIToken::lparen))
911 return true;
912 if (Token.isNot(MIToken::Identifier))
913 return error("expected the name of the target index");
914 int Index = 0;
915 if (getTargetIndex(Token.stringValue(), Index))
916 return error("use of undefined target index '" + Token.stringValue() + "'");
917 lex();
918 if (expectAndConsume(MIToken::rparen))
919 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000920 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000921 if (parseOperandsOffset(Dest))
922 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000923 return false;
924}
925
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000926bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
927 assert(Token.is(MIToken::kw_liveout));
928 const auto *TRI = MF.getSubtarget().getRegisterInfo();
929 assert(TRI && "Expected target register info");
930 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
931 lex();
932 if (expectAndConsume(MIToken::lparen))
933 return true;
934 while (true) {
935 if (Token.isNot(MIToken::NamedRegister))
936 return error("expected a named register");
937 unsigned Reg = 0;
938 if (parseRegister(Reg))
939 return true;
940 lex();
941 Mask[Reg / 32] |= 1U << (Reg % 32);
942 // TODO: Report an error if the same register is used more than once.
943 if (Token.isNot(MIToken::comma))
944 break;
945 lex();
946 }
947 if (expectAndConsume(MIToken::rparen))
948 return true;
949 Dest = MachineOperand::CreateRegLiveOut(Mask);
950 return false;
951}
952
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000953bool MIParser::parseMachineOperand(MachineOperand &Dest) {
954 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000955 case MIToken::kw_implicit:
956 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000957 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000958 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000959 case MIToken::kw_undef:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000960 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +0000961 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000962 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000963 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000964 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000965 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000966 case MIToken::IntegerLiteral:
967 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +0000968 case MIToken::IntegerType:
969 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000970 case MIToken::kw_half:
971 case MIToken::kw_float:
972 case MIToken::kw_double:
973 case MIToken::kw_x86_fp80:
974 case MIToken::kw_fp128:
975 case MIToken::kw_ppc_fp128:
976 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000977 case MIToken::MachineBasicBlock:
978 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000979 case MIToken::StackObject:
980 return parseStackObjectOperand(Dest);
981 case MIToken::FixedStackObject:
982 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000983 case MIToken::GlobalValue:
984 case MIToken::NamedGlobalValue:
985 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000986 case MIToken::ConstantPoolItem:
987 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000988 case MIToken::JumpTableIndex:
989 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000990 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +0000991 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +0000992 case MIToken::exclaim:
993 return parseMetadataOperand(Dest);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000994 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000995 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000996 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +0000997 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000998 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000999 case MIToken::kw_blockaddress:
1000 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001001 case MIToken::kw_target_index:
1002 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001003 case MIToken::kw_liveout:
1004 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001005 case MIToken::Error:
1006 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001007 case MIToken::Identifier:
1008 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1009 Dest = MachineOperand::CreateRegMask(RegMask);
1010 lex();
1011 break;
1012 }
1013 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001014 default:
1015 // TODO: parse the other machine operands.
1016 return error("expected a machine operand");
1017 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001018 return false;
1019}
1020
Alex Lorenz49873a82015-08-06 00:44:07 +00001021bool MIParser::parseMachineOperandAndTargetFlags(MachineOperand &Dest) {
1022 unsigned TF = 0;
1023 bool HasTargetFlags = false;
1024 if (Token.is(MIToken::kw_target_flags)) {
1025 HasTargetFlags = true;
1026 lex();
1027 if (expectAndConsume(MIToken::lparen))
1028 return true;
1029 if (Token.isNot(MIToken::Identifier))
1030 return error("expected the name of the target flag");
1031 if (getDirectTargetFlag(Token.stringValue(), TF))
1032 return error("use of undefined target flag '" + Token.stringValue() +
1033 "'");
1034 lex();
1035 // TODO: Parse target's bit target flags.
1036 if (expectAndConsume(MIToken::rparen))
1037 return true;
1038 }
1039 auto Loc = Token.location();
1040 if (parseMachineOperand(Dest))
1041 return true;
1042 if (!HasTargetFlags)
1043 return false;
1044 if (Dest.isReg())
1045 return error(Loc, "register operands can't have target flags");
1046 Dest.setTargetFlags(TF);
1047 return false;
1048}
1049
Alex Lorenzdc24c172015-08-07 20:21:00 +00001050bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001051 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1052 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001053 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001054 bool IsNegative = Token.is(MIToken::minus);
1055 lex();
1056 if (Token.isNot(MIToken::IntegerLiteral))
1057 return error("expected an integer literal after '" + Sign + "'");
1058 if (Token.integerValue().getMinSignedBits() > 64)
1059 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001060 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001061 if (IsNegative)
1062 Offset = -Offset;
1063 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001064 return false;
1065}
1066
1067bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1068 int64_t Offset = 0;
1069 if (parseOffset(Offset))
1070 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001071 Op.setOffset(Offset);
1072 return false;
1073}
1074
Alex Lorenz4af7e612015-08-03 23:08:19 +00001075bool MIParser::parseIRValue(Value *&V) {
1076 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001077 case MIToken::NamedIRValue: {
1078 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001079 if (!V)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001080 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001081 break;
1082 }
1083 // TODO: Parse unnamed IR value references.
1084 default:
1085 llvm_unreachable("The current token should be an IR block reference");
1086 }
1087 return false;
1088}
1089
1090bool MIParser::getUint64(uint64_t &Result) {
1091 assert(Token.hasIntegerValue());
1092 if (Token.integerValue().getActiveBits() > 64)
1093 return error("expected 64-bit integer (too large)");
1094 Result = Token.integerValue().getZExtValue();
1095 return false;
1096}
1097
Alex Lorenza518b792015-08-04 00:24:45 +00001098bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
Alex Lorenze86d5152015-08-06 18:26:36 +00001099 const unsigned OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001100 switch (Token.kind()) {
1101 case MIToken::kw_volatile:
1102 Flags |= MachineMemOperand::MOVolatile;
1103 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001104 case MIToken::kw_non_temporal:
1105 Flags |= MachineMemOperand::MONonTemporal;
1106 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001107 case MIToken::kw_invariant:
1108 Flags |= MachineMemOperand::MOInvariant;
1109 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001110 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001111 default:
1112 llvm_unreachable("The current token should be a memory operand flag");
1113 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001114 if (OldFlags == Flags)
1115 // We know that the same flag is specified more than once when the flags
1116 // weren't modified.
1117 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001118 lex();
1119 return false;
1120}
1121
Alex Lorenz91097a32015-08-12 20:33:26 +00001122bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1123 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001124 case MIToken::kw_stack:
1125 PSV = MF.getPSVManager().getStack();
1126 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001127 case MIToken::kw_got:
1128 PSV = MF.getPSVManager().getGOT();
1129 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001130 case MIToken::kw_constant_pool:
1131 PSV = MF.getPSVManager().getConstantPool();
1132 break;
1133 // TODO: Parse the other pseudo source values.
1134 default:
1135 llvm_unreachable("The current token should be pseudo source value");
1136 }
1137 lex();
1138 return false;
1139}
1140
1141bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001142 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
1143 Token.is(MIToken::kw_got)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001144 const PseudoSourceValue *PSV = nullptr;
1145 if (parseMemoryPseudoSourceValue(PSV))
1146 return true;
1147 int64_t Offset = 0;
1148 if (parseOffset(Offset))
1149 return true;
1150 Dest = MachinePointerInfo(PSV, Offset);
1151 return false;
1152 }
1153 if (Token.isNot(MIToken::NamedIRValue))
1154 return error("expected an IR value reference");
1155 Value *V = nullptr;
1156 if (parseIRValue(V))
1157 return true;
1158 if (!V->getType()->isPointerTy())
1159 return error("expected a pointer IR value");
1160 lex();
1161 int64_t Offset = 0;
1162 if (parseOffset(Offset))
1163 return true;
1164 Dest = MachinePointerInfo(V, Offset);
1165 return false;
1166}
1167
Alex Lorenz4af7e612015-08-03 23:08:19 +00001168bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1169 if (expectAndConsume(MIToken::lparen))
1170 return true;
Alex Lorenza518b792015-08-04 00:24:45 +00001171 unsigned Flags = 0;
1172 while (Token.isMemoryOperandFlag()) {
1173 if (parseMemoryOperandFlag(Flags))
1174 return true;
1175 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001176 if (Token.isNot(MIToken::Identifier) ||
1177 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1178 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001179 if (Token.stringValue() == "load")
1180 Flags |= MachineMemOperand::MOLoad;
1181 else
1182 Flags |= MachineMemOperand::MOStore;
1183 lex();
1184
1185 if (Token.isNot(MIToken::IntegerLiteral))
1186 return error("expected the size integer literal after memory operation");
1187 uint64_t Size;
1188 if (getUint64(Size))
1189 return true;
1190 lex();
1191
1192 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1193 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1194 return error(Twine("expected '") + Word + "'");
1195 lex();
1196
Alex Lorenz91097a32015-08-12 20:33:26 +00001197 MachinePointerInfo Ptr = MachinePointerInfo();
1198 if (parseMachinePointerInfo(Ptr))
Alex Lorenz83127732015-08-07 20:26:52 +00001199 return true;
Alex Lorenz61420f72015-08-07 20:48:30 +00001200 unsigned BaseAlignment = Size;
1201 if (Token.is(MIToken::comma)) {
1202 lex();
1203 if (Token.isNot(MIToken::kw_align))
1204 return error("expected 'align'");
1205 lex();
1206 if (Token.isNot(MIToken::IntegerLiteral))
1207 return error("expected an integer literal after 'align'");
1208 if (getUnsigned(BaseAlignment))
1209 return true;
1210 lex();
1211 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001212 // TODO: Parse the attached metadata nodes.
1213 if (expectAndConsume(MIToken::rparen))
1214 return true;
Alex Lorenz91097a32015-08-12 20:33:26 +00001215 Dest = MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001216 return false;
1217}
1218
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001219void MIParser::initNames2InstrOpCodes() {
1220 if (!Names2InstrOpCodes.empty())
1221 return;
1222 const auto *TII = MF.getSubtarget().getInstrInfo();
1223 assert(TII && "Expected target instruction info");
1224 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1225 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1226}
1227
1228bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1229 initNames2InstrOpCodes();
1230 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1231 if (InstrInfo == Names2InstrOpCodes.end())
1232 return true;
1233 OpCode = InstrInfo->getValue();
1234 return false;
1235}
1236
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001237void MIParser::initNames2Regs() {
1238 if (!Names2Regs.empty())
1239 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001240 // The '%noreg' register is the register 0.
1241 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001242 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1243 assert(TRI && "Expected target register info");
1244 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1245 bool WasInserted =
1246 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1247 .second;
1248 (void)WasInserted;
1249 assert(WasInserted && "Expected registers to be unique case-insensitively");
1250 }
1251}
1252
1253bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1254 initNames2Regs();
1255 auto RegInfo = Names2Regs.find(RegName);
1256 if (RegInfo == Names2Regs.end())
1257 return true;
1258 Reg = RegInfo->getValue();
1259 return false;
1260}
1261
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001262void MIParser::initNames2RegMasks() {
1263 if (!Names2RegMasks.empty())
1264 return;
1265 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1266 assert(TRI && "Expected target register info");
1267 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1268 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1269 assert(RegMasks.size() == RegMaskNames.size());
1270 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1271 Names2RegMasks.insert(
1272 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1273}
1274
1275const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1276 initNames2RegMasks();
1277 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1278 if (RegMaskInfo == Names2RegMasks.end())
1279 return nullptr;
1280 return RegMaskInfo->getValue();
1281}
1282
Alex Lorenz2eacca82015-07-13 23:24:34 +00001283void MIParser::initNames2SubRegIndices() {
1284 if (!Names2SubRegIndices.empty())
1285 return;
1286 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1287 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1288 Names2SubRegIndices.insert(
1289 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1290}
1291
1292unsigned MIParser::getSubRegIndex(StringRef Name) {
1293 initNames2SubRegIndices();
1294 auto SubRegInfo = Names2SubRegIndices.find(Name);
1295 if (SubRegInfo == Names2SubRegIndices.end())
1296 return 0;
1297 return SubRegInfo->getValue();
1298}
1299
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001300static void initSlots2BasicBlocks(
1301 const Function &F,
1302 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1303 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001304 MST.incorporateFunction(F);
1305 for (auto &BB : F) {
1306 if (BB.hasName())
1307 continue;
1308 int Slot = MST.getLocalSlot(&BB);
1309 if (Slot == -1)
1310 continue;
1311 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1312 }
1313}
1314
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001315static const BasicBlock *getIRBlockFromSlot(
1316 unsigned Slot,
1317 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001318 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1319 if (BlockInfo == Slots2BasicBlocks.end())
1320 return nullptr;
1321 return BlockInfo->second;
1322}
1323
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001324const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1325 if (Slots2BasicBlocks.empty())
1326 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1327 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1328}
1329
1330const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1331 if (&F == MF.getFunction())
1332 return getIRBlock(Slot);
1333 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1334 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1335 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1336}
1337
Alex Lorenzef5c1962015-07-28 23:02:45 +00001338void MIParser::initNames2TargetIndices() {
1339 if (!Names2TargetIndices.empty())
1340 return;
1341 const auto *TII = MF.getSubtarget().getInstrInfo();
1342 assert(TII && "Expected target instruction info");
1343 auto Indices = TII->getSerializableTargetIndices();
1344 for (const auto &I : Indices)
1345 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1346}
1347
1348bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1349 initNames2TargetIndices();
1350 auto IndexInfo = Names2TargetIndices.find(Name);
1351 if (IndexInfo == Names2TargetIndices.end())
1352 return true;
1353 Index = IndexInfo->second;
1354 return false;
1355}
1356
Alex Lorenz49873a82015-08-06 00:44:07 +00001357void MIParser::initNames2DirectTargetFlags() {
1358 if (!Names2DirectTargetFlags.empty())
1359 return;
1360 const auto *TII = MF.getSubtarget().getInstrInfo();
1361 assert(TII && "Expected target instruction info");
1362 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1363 for (const auto &I : Flags)
1364 Names2DirectTargetFlags.insert(
1365 std::make_pair(StringRef(I.second), I.first));
1366}
1367
1368bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1369 initNames2DirectTargetFlags();
1370 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1371 if (FlagInfo == Names2DirectTargetFlags.end())
1372 return true;
1373 Flag = FlagInfo->second;
1374 return false;
1375}
1376
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001377bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
1378 MachineFunction &MF, StringRef Src,
1379 const PerFunctionMIParsingState &PFS,
1380 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1381 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001382}
Alex Lorenzf09df002015-06-30 18:16:42 +00001383
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001384bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1385 MachineFunction &MF, StringRef Src,
1386 const PerFunctionMIParsingState &PFS,
1387 const SlotMapping &IRSlots, SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001388 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00001389}
Alex Lorenz9fab3702015-07-14 21:24:41 +00001390
1391bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1392 MachineFunction &MF, StringRef Src,
1393 const PerFunctionMIParsingState &PFS,
1394 const SlotMapping &IRSlots,
1395 SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001396 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1397 .parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00001398}
Alex Lorenz12045a42015-07-27 17:42:45 +00001399
1400bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1401 MachineFunction &MF, StringRef Src,
1402 const PerFunctionMIParsingState &PFS,
1403 const SlotMapping &IRSlots,
1404 SMDiagnostic &Error) {
1405 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1406 .parseStandaloneVirtualRegister(Reg);
1407}
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001408
1409bool llvm::parseIRBlockReference(const BasicBlock *&BB, SourceMgr &SM,
1410 MachineFunction &MF, StringRef Src,
1411 const PerFunctionMIParsingState &PFS,
1412 const SlotMapping &IRSlots,
1413 SMDiagnostic &Error) {
1414 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1415 .parseStandaloneIRBlockReference(BB);
1416}