blob: 389c5aaec8c70c3db560d278e2d692ac7a6fecb5 [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 Lorenz620f8912015-08-13 20:33:33 +0000129 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000130 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000131 bool parseIRValue(Value *&V);
Alex Lorenza518b792015-08-04 00:24:45 +0000132 bool parseMemoryOperandFlag(unsigned &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000133 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
134 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000135 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000136
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000137private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000138 /// Convert the integer literal in the current token into an unsigned integer.
139 ///
140 /// Return true if an error occurred.
141 bool getUnsigned(unsigned &Result);
142
Alex Lorenz4af7e612015-08-03 23:08:19 +0000143 /// Convert the integer literal in the current token into an uint64.
144 ///
145 /// Return true if an error occurred.
146 bool getUint64(uint64_t &Result);
147
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000148 /// If the current token is of the given kind, consume it and return false.
149 /// Otherwise report an error and return true.
150 bool expectAndConsume(MIToken::TokenKind TokenKind);
151
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000152 void initNames2InstrOpCodes();
153
154 /// Try to convert an instruction name to an opcode. Return true if the
155 /// instruction name is invalid.
156 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000157
Alex Lorenze5a44662015-07-17 00:24:15 +0000158 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000159
Alex Lorenz36962cd2015-07-07 02:08:46 +0000160 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
161 const MCInstrDesc &MCID);
162
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000163 void initNames2Regs();
164
165 /// Try to convert a register name to a register number. Return true if the
166 /// register name is invalid.
167 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000168
169 void initNames2RegMasks();
170
171 /// Check if the given identifier is a name of a register mask.
172 ///
173 /// Return null if the identifier isn't a register mask.
174 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000175
176 void initNames2SubRegIndices();
177
178 /// Check if the given identifier is a name of a subregister index.
179 ///
180 /// Return 0 if the name isn't a subregister index class.
181 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000182
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000183 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000184 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000185
186 void initNames2TargetIndices();
187
188 /// Try to convert a name of target index to the corresponding target index.
189 ///
190 /// Return true if the name isn't a name of a target index.
191 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000192
193 void initNames2DirectTargetFlags();
194
195 /// Try to convert a name of a direct target flag to the corresponding
196 /// target flag.
197 ///
198 /// Return true if the name isn't a name of a direct flag.
199 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000200};
201
202} // end anonymous namespace
203
204MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000205 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000206 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000207 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz3fb77682015-08-06 23:17:42 +0000208 PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000209
Alex Lorenz91370c52015-06-22 20:37:46 +0000210void MIParser::lex() {
211 CurrentSource = lexMIToken(
212 CurrentSource, Token,
213 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
214}
215
216bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
217
218bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000219 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
220 Error = SMDiagnostic(
221 SM, SMLoc(),
222 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
223 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000224 return true;
225}
226
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000227static const char *toString(MIToken::TokenKind TokenKind) {
228 switch (TokenKind) {
229 case MIToken::comma:
230 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000231 case MIToken::equal:
232 return "'='";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000233 case MIToken::lparen:
234 return "'('";
235 case MIToken::rparen:
236 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000237 default:
238 return "<unknown token>";
239 }
240}
241
242bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
243 if (Token.isNot(TokenKind))
244 return error(Twine("expected ") + toString(TokenKind));
245 lex();
246 return false;
247}
248
Alex Lorenz3708a642015-06-30 17:47:50 +0000249bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000250 lex();
251
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000252 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000253 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000254 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000255 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000256 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000257 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000258 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000259 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000260 if (Token.isNot(MIToken::comma))
261 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000262 lex();
263 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000264 if (!Operands.empty() && expectAndConsume(MIToken::equal))
265 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000266
Alex Lorenze5a44662015-07-17 00:24:15 +0000267 unsigned OpCode, Flags = 0;
268 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000269 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000270
Alex Lorenz4af7e612015-08-03 23:08:19 +0000271 // TODO: Parse the bundle instruction flags.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000272
273 // Parse the remaining machine operands.
Alex Lorenz4af7e612015-08-03 23:08:19 +0000274 while (Token.isNot(MIToken::Eof) && Token.isNot(MIToken::kw_debug_location) &&
275 Token.isNot(MIToken::coloncolon)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000276 auto Loc = Token.location();
Alex Lorenz49873a82015-08-06 00:44:07 +0000277 if (parseMachineOperandAndTargetFlags(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000278 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000279 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000280 if (Token.is(MIToken::Eof) || Token.is(MIToken::coloncolon))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000281 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000282 if (Token.isNot(MIToken::comma))
283 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000284 lex();
285 }
286
Alex Lorenz46d760d2015-07-22 21:15:11 +0000287 DebugLoc DebugLocation;
288 if (Token.is(MIToken::kw_debug_location)) {
289 lex();
290 if (Token.isNot(MIToken::exclaim))
291 return error("expected a metadata node after 'debug-location'");
292 MDNode *Node = nullptr;
293 if (parseMDNode(Node))
294 return true;
295 DebugLocation = DebugLoc(Node);
296 }
297
Alex Lorenz4af7e612015-08-03 23:08:19 +0000298 // Parse the machine memory operands.
299 SmallVector<MachineMemOperand *, 2> MemOperands;
300 if (Token.is(MIToken::coloncolon)) {
301 lex();
302 while (Token.isNot(MIToken::Eof)) {
303 MachineMemOperand *MemOp = nullptr;
304 if (parseMachineMemoryOperand(MemOp))
305 return true;
306 MemOperands.push_back(MemOp);
307 if (Token.is(MIToken::Eof))
308 break;
309 if (Token.isNot(MIToken::comma))
310 return error("expected ',' before the next machine memory operand");
311 lex();
312 }
313 }
314
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000315 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000316 if (!MCID.isVariadic()) {
317 // FIXME: Move the implicit operand verification to the machine verifier.
318 if (verifyImplicitOperands(Operands, MCID))
319 return true;
320 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000321
Alex Lorenzcb268d42015-07-06 23:07:26 +0000322 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000323 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000324 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000325 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000326 MI->addOperand(MF, Operand.Operand);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000327 if (MemOperands.empty())
328 return false;
329 MachineInstr::mmo_iterator MemRefs =
330 MF.allocateMemRefsArray(MemOperands.size());
331 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
332 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000333 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000334}
335
Alex Lorenz1ea60892015-07-27 20:29:27 +0000336bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000337 lex();
338 if (Token.isNot(MIToken::MachineBasicBlock))
339 return error("expected a machine basic block reference");
340 if (parseMBBReference(MBB))
341 return true;
342 lex();
343 if (Token.isNot(MIToken::Eof))
344 return error(
345 "expected end of string after the machine basic block reference");
346 return false;
347}
348
Alex Lorenz1ea60892015-07-27 20:29:27 +0000349bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000350 lex();
351 if (Token.isNot(MIToken::NamedRegister))
352 return error("expected a named register");
353 if (parseRegister(Reg))
354 return 0;
355 lex();
356 if (Token.isNot(MIToken::Eof))
357 return error("expected end of string after the register reference");
358 return false;
359}
360
Alex Lorenz12045a42015-07-27 17:42:45 +0000361bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
362 lex();
363 if (Token.isNot(MIToken::VirtualRegister))
364 return error("expected a virtual register");
365 if (parseRegister(Reg))
366 return 0;
367 lex();
368 if (Token.isNot(MIToken::Eof))
369 return error("expected end of string after the register reference");
370 return false;
371}
372
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000373bool MIParser::parseStandaloneIRBlockReference(const BasicBlock *&BB) {
374 lex();
375 if (Token.isNot(MIToken::IRBlock))
376 return error("expected an IR block reference");
377 unsigned SlotNumber = 0;
378 if (getUnsigned(SlotNumber))
379 return true;
380 BB = getIRBlock(SlotNumber);
381 if (!BB)
382 return error(Twine("use of undefined IR block '%ir-block.") +
383 Twine(SlotNumber) + "'");
384 lex();
385 if (Token.isNot(MIToken::Eof))
386 return error("expected end of string after the IR block reference");
387 return false;
388}
389
Alex Lorenz36962cd2015-07-07 02:08:46 +0000390static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
391 assert(MO.isImplicit());
392 return MO.isDef() ? "implicit-def" : "implicit";
393}
394
395static std::string getRegisterName(const TargetRegisterInfo *TRI,
396 unsigned Reg) {
397 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
398 return StringRef(TRI->getName(Reg)).lower();
399}
400
401bool MIParser::verifyImplicitOperands(
402 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
403 if (MCID.isCall())
404 // We can't verify call instructions as they can contain arbitrary implicit
405 // register and register mask operands.
406 return false;
407
408 // Gather all the expected implicit operands.
409 SmallVector<MachineOperand, 4> ImplicitOperands;
410 if (MCID.ImplicitDefs)
411 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
412 ImplicitOperands.push_back(
413 MachineOperand::CreateReg(*ImpDefs, true, true));
414 if (MCID.ImplicitUses)
415 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
416 ImplicitOperands.push_back(
417 MachineOperand::CreateReg(*ImpUses, false, true));
418
419 const auto *TRI = MF.getSubtarget().getRegisterInfo();
420 assert(TRI && "Expected target register info");
421 size_t I = ImplicitOperands.size(), J = Operands.size();
422 while (I) {
423 --I;
424 if (J) {
425 --J;
426 const auto &ImplicitOperand = ImplicitOperands[I];
427 const auto &Operand = Operands[J].Operand;
428 if (ImplicitOperand.isIdenticalTo(Operand))
429 continue;
430 if (Operand.isReg() && Operand.isImplicit()) {
431 return error(Operands[J].Begin,
432 Twine("expected an implicit register operand '") +
433 printImplicitRegisterFlag(ImplicitOperand) + " %" +
434 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
435 }
436 }
437 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
438 // insead of reporting an error at this location:
439 // %eax = MOV32r0
440 // ^
441 // report the error at the following location:
442 // %eax = MOV32r0
443 // ^
444 return error(J < Operands.size() ? Operands[J].End : Token.location(),
445 Twine("missing implicit register operand '") +
446 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
447 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
448 }
449 return false;
450}
451
Alex Lorenze5a44662015-07-17 00:24:15 +0000452bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
453 if (Token.is(MIToken::kw_frame_setup)) {
454 Flags |= MachineInstr::FrameSetup;
455 lex();
456 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000457 if (Token.isNot(MIToken::Identifier))
458 return error("expected a machine instruction");
459 StringRef InstrName = Token.stringValue();
460 if (parseInstrName(InstrName, OpCode))
461 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000462 lex();
463 return false;
464}
465
466bool MIParser::parseRegister(unsigned &Reg) {
467 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000468 case MIToken::underscore:
469 Reg = 0;
470 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000471 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000472 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000473 if (getRegisterByName(Name, Reg))
474 return error(Twine("unknown register name '") + Name + "'");
475 break;
476 }
Alex Lorenz53464512015-07-10 22:51:20 +0000477 case MIToken::VirtualRegister: {
478 unsigned ID;
479 if (getUnsigned(ID))
480 return true;
481 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
482 if (RegInfo == PFS.VirtualRegisterSlots.end())
483 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
484 "'");
485 Reg = RegInfo->second;
486 break;
487 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000488 // TODO: Parse other register kinds.
489 default:
490 llvm_unreachable("The current token should be a register");
491 }
492 return false;
493}
494
Alex Lorenzcb268d42015-07-06 23:07:26 +0000495bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000496 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000497 switch (Token.kind()) {
498 case MIToken::kw_implicit:
499 Flags |= RegState::Implicit;
500 break;
501 case MIToken::kw_implicit_define:
502 Flags |= RegState::ImplicitDefine;
503 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000504 case MIToken::kw_dead:
505 Flags |= RegState::Dead;
506 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000507 case MIToken::kw_killed:
508 Flags |= RegState::Kill;
509 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000510 case MIToken::kw_undef:
511 Flags |= RegState::Undef;
512 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000513 case MIToken::kw_early_clobber:
514 Flags |= RegState::EarlyClobber;
515 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000516 case MIToken::kw_debug_use:
517 Flags |= RegState::Debug;
518 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000519 // TODO: parse the other register flags.
520 default:
521 llvm_unreachable("The current token should be a register flag");
522 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000523 if (OldFlags == Flags)
524 // We know that the same flag is specified more than once when the flags
525 // weren't modified.
526 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000527 lex();
528 return false;
529}
530
Alex Lorenz2eacca82015-07-13 23:24:34 +0000531bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
532 assert(Token.is(MIToken::colon));
533 lex();
534 if (Token.isNot(MIToken::Identifier))
535 return error("expected a subregister index after ':'");
536 auto Name = Token.stringValue();
537 SubReg = getSubRegIndex(Name);
538 if (!SubReg)
539 return error(Twine("use of unknown subregister index '") + Name + "'");
540 lex();
541 return false;
542}
543
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000544bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
545 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000546 unsigned Flags = IsDef ? RegState::Define : 0;
547 while (Token.isRegisterFlag()) {
548 if (parseRegisterFlag(Flags))
549 return true;
550 }
551 if (!Token.isRegister())
552 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000553 if (parseRegister(Reg))
554 return true;
555 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000556 unsigned SubReg = 0;
557 if (Token.is(MIToken::colon)) {
558 if (parseSubRegisterIndex(SubReg))
559 return true;
560 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000561 Dest = MachineOperand::CreateReg(
562 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000563 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000564 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000565 return false;
566}
567
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000568bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
569 assert(Token.is(MIToken::IntegerLiteral));
570 const APSInt &Int = Token.integerValue();
571 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +0000572 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000573 Dest = MachineOperand::CreateImm(Int.getExtValue());
574 lex();
575 return false;
576}
577
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000578bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
Alex Lorenz3fb77682015-08-06 23:17:42 +0000579 auto Source = StringRef(Loc, Token.range().end() - Loc).str();
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000580 lex();
581 SMDiagnostic Err;
582 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
583 if (!C)
584 return error(Loc + Err.getColumnNo(), Err.getMessage());
585 return false;
586}
587
Alex Lorenz05e38822015-08-05 18:52:21 +0000588bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
589 assert(Token.is(MIToken::IntegerType));
590 auto Loc = Token.location();
591 lex();
592 if (Token.isNot(MIToken::IntegerLiteral))
593 return error("expected an integer literal");
594 const Constant *C = nullptr;
595 if (parseIRConstant(Loc, C))
596 return true;
597 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
598 return false;
599}
600
Alex Lorenzad156fb2015-07-31 20:49:21 +0000601bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
602 auto Loc = Token.location();
603 lex();
604 if (Token.isNot(MIToken::FloatingPointLiteral))
605 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000606 const Constant *C = nullptr;
607 if (parseIRConstant(Loc, C))
608 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000609 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
610 return false;
611}
612
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000613bool MIParser::getUnsigned(unsigned &Result) {
614 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
615 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
616 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
617 if (Val64 == Limit)
618 return error("expected 32-bit integer (too large)");
619 Result = Val64;
620 return false;
621}
622
Alex Lorenzf09df002015-06-30 18:16:42 +0000623bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000624 assert(Token.is(MIToken::MachineBasicBlock));
625 unsigned Number;
626 if (getUnsigned(Number))
627 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000628 auto MBBInfo = PFS.MBBSlots.find(Number);
629 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000630 return error(Twine("use of undefined machine basic block #") +
631 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000632 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000633 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
634 return error(Twine("the name of machine basic block #") + Twine(Number) +
635 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000636 return false;
637}
638
639bool MIParser::parseMBBOperand(MachineOperand &Dest) {
640 MachineBasicBlock *MBB;
641 if (parseMBBReference(MBB))
642 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000643 Dest = MachineOperand::CreateMBB(MBB);
644 lex();
645 return false;
646}
647
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000648bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
649 assert(Token.is(MIToken::StackObject));
650 unsigned ID;
651 if (getUnsigned(ID))
652 return true;
653 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
654 if (ObjectInfo == PFS.StackObjectSlots.end())
655 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
656 "'");
657 StringRef Name;
658 if (const auto *Alloca =
659 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
660 Name = Alloca->getName();
661 if (!Token.stringValue().empty() && Token.stringValue() != Name)
662 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
663 "' isn't '" + Token.stringValue() + "'");
664 lex();
665 Dest = MachineOperand::CreateFI(ObjectInfo->second);
666 return false;
667}
668
Alex Lorenzea882122015-08-12 21:17:02 +0000669bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000670 assert(Token.is(MIToken::FixedStackObject));
671 unsigned ID;
672 if (getUnsigned(ID))
673 return true;
674 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
675 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
676 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
677 Twine(ID) + "'");
678 lex();
Alex Lorenzea882122015-08-12 21:17:02 +0000679 FI = ObjectInfo->second;
680 return false;
681}
682
683bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
684 int FI;
685 if (parseFixedStackFrameIndex(FI))
686 return true;
687 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000688 return false;
689}
690
Alex Lorenz41df7d32015-07-28 17:09:52 +0000691bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000692 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000693 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000694 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +0000695 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +0000696 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +0000697 return error(Twine("use of undefined global value '") + Token.range() +
698 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000699 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000700 }
701 case MIToken::GlobalValue: {
702 unsigned GVIdx;
703 if (getUnsigned(GVIdx))
704 return true;
705 if (GVIdx >= IRSlots.GlobalValues.size())
706 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
707 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000708 GV = IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000709 break;
710 }
711 default:
712 llvm_unreachable("The current token should be a global value");
713 }
Alex Lorenz41df7d32015-07-28 17:09:52 +0000714 return false;
715}
716
717bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
718 GlobalValue *GV = nullptr;
719 if (parseGlobalValue(GV))
720 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000721 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000722 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000723 if (parseOperandsOffset(Dest))
724 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000725 return false;
726}
727
Alex Lorenzab980492015-07-20 20:51:18 +0000728bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
729 assert(Token.is(MIToken::ConstantPoolItem));
730 unsigned ID;
731 if (getUnsigned(ID))
732 return true;
733 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
734 if (ConstantInfo == PFS.ConstantPoolSlots.end())
735 return error("use of undefined constant '%const." + Twine(ID) + "'");
736 lex();
Alex Lorenzab980492015-07-20 20:51:18 +0000737 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000738 if (parseOperandsOffset(Dest))
739 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000740 return false;
741}
742
Alex Lorenz31d70682015-07-15 23:38:35 +0000743bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
744 assert(Token.is(MIToken::JumpTableIndex));
745 unsigned ID;
746 if (getUnsigned(ID))
747 return true;
748 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
749 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
750 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
751 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000752 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
753 return false;
754}
755
Alex Lorenz6ede3742015-07-21 16:59:53 +0000756bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000757 assert(Token.is(MIToken::ExternalSymbol));
758 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000759 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +0000760 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +0000761 if (parseOperandsOffset(Dest))
762 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000763 return false;
764}
765
Alex Lorenz44f29252015-07-22 21:07:04 +0000766bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +0000767 assert(Token.is(MIToken::exclaim));
768 auto Loc = Token.location();
769 lex();
770 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
771 return error("expected metadata id after '!'");
772 unsigned ID;
773 if (getUnsigned(ID))
774 return true;
775 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
776 if (NodeInfo == IRSlots.MetadataNodes.end())
777 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
778 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +0000779 Node = NodeInfo->second.get();
780 return false;
781}
782
783bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
784 MDNode *Node = nullptr;
785 if (parseMDNode(Node))
786 return true;
787 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000788 return false;
789}
790
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000791bool MIParser::parseCFIOffset(int &Offset) {
792 if (Token.isNot(MIToken::IntegerLiteral))
793 return error("expected a cfi offset");
794 if (Token.integerValue().getMinSignedBits() > 32)
795 return error("expected a 32 bit integer (the cfi offset is too large)");
796 Offset = (int)Token.integerValue().getExtValue();
797 lex();
798 return false;
799}
800
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000801bool MIParser::parseCFIRegister(unsigned &Reg) {
802 if (Token.isNot(MIToken::NamedRegister))
803 return error("expected a cfi register");
804 unsigned LLVMReg;
805 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000806 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000807 const auto *TRI = MF.getSubtarget().getRegisterInfo();
808 assert(TRI && "Expected target register info");
809 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
810 if (DwarfReg < 0)
811 return error("invalid DWARF register");
812 Reg = (unsigned)DwarfReg;
813 lex();
814 return false;
815}
816
817bool MIParser::parseCFIOperand(MachineOperand &Dest) {
818 auto Kind = Token.kind();
819 lex();
820 auto &MMI = MF.getMMI();
821 int Offset;
822 unsigned Reg;
823 unsigned CFIIndex;
824 switch (Kind) {
825 case MIToken::kw_cfi_offset:
826 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
827 parseCFIOffset(Offset))
828 return true;
829 CFIIndex =
830 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
831 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000832 case MIToken::kw_cfi_def_cfa_register:
833 if (parseCFIRegister(Reg))
834 return true;
835 CFIIndex =
836 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
837 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000838 case MIToken::kw_cfi_def_cfa_offset:
839 if (parseCFIOffset(Offset))
840 return true;
841 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
842 CFIIndex = MMI.addFrameInst(
843 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
844 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000845 case MIToken::kw_cfi_def_cfa:
846 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
847 parseCFIOffset(Offset))
848 return true;
849 // NB: MCCFIInstruction::createDefCfa negates the offset.
850 CFIIndex =
851 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
852 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000853 default:
854 // TODO: Parse the other CFI operands.
855 llvm_unreachable("The current token should be a cfi operand");
856 }
857 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000858 return false;
859}
860
Alex Lorenzdeb53492015-07-28 17:28:03 +0000861bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
862 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000863 case MIToken::NamedIRBlock: {
864 BB = dyn_cast_or_null<BasicBlock>(
865 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +0000866 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +0000867 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +0000868 break;
869 }
870 case MIToken::IRBlock: {
871 unsigned SlotNumber = 0;
872 if (getUnsigned(SlotNumber))
873 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000874 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +0000875 if (!BB)
876 return error(Twine("use of undefined IR block '%ir-block.") +
877 Twine(SlotNumber) + "'");
878 break;
879 }
880 default:
881 llvm_unreachable("The current token should be an IR block reference");
882 }
883 return false;
884}
885
886bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
887 assert(Token.is(MIToken::kw_blockaddress));
888 lex();
889 if (expectAndConsume(MIToken::lparen))
890 return true;
891 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +0000892 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +0000893 return error("expected a global value");
894 GlobalValue *GV = nullptr;
895 if (parseGlobalValue(GV))
896 return true;
897 auto *F = dyn_cast<Function>(GV);
898 if (!F)
899 return error("expected an IR function reference");
900 lex();
901 if (expectAndConsume(MIToken::comma))
902 return true;
903 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +0000904 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +0000905 return error("expected an IR block reference");
906 if (parseIRBlock(BB, *F))
907 return true;
908 lex();
909 if (expectAndConsume(MIToken::rparen))
910 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000911 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000912 if (parseOperandsOffset(Dest))
913 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000914 return false;
915}
916
Alex Lorenzef5c1962015-07-28 23:02:45 +0000917bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
918 assert(Token.is(MIToken::kw_target_index));
919 lex();
920 if (expectAndConsume(MIToken::lparen))
921 return true;
922 if (Token.isNot(MIToken::Identifier))
923 return error("expected the name of the target index");
924 int Index = 0;
925 if (getTargetIndex(Token.stringValue(), Index))
926 return error("use of undefined target index '" + Token.stringValue() + "'");
927 lex();
928 if (expectAndConsume(MIToken::rparen))
929 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000930 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000931 if (parseOperandsOffset(Dest))
932 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000933 return false;
934}
935
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000936bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
937 assert(Token.is(MIToken::kw_liveout));
938 const auto *TRI = MF.getSubtarget().getRegisterInfo();
939 assert(TRI && "Expected target register info");
940 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
941 lex();
942 if (expectAndConsume(MIToken::lparen))
943 return true;
944 while (true) {
945 if (Token.isNot(MIToken::NamedRegister))
946 return error("expected a named register");
947 unsigned Reg = 0;
948 if (parseRegister(Reg))
949 return true;
950 lex();
951 Mask[Reg / 32] |= 1U << (Reg % 32);
952 // TODO: Report an error if the same register is used more than once.
953 if (Token.isNot(MIToken::comma))
954 break;
955 lex();
956 }
957 if (expectAndConsume(MIToken::rparen))
958 return true;
959 Dest = MachineOperand::CreateRegLiveOut(Mask);
960 return false;
961}
962
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000963bool MIParser::parseMachineOperand(MachineOperand &Dest) {
964 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000965 case MIToken::kw_implicit:
966 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000967 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000968 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000969 case MIToken::kw_undef:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000970 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +0000971 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000972 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000973 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000974 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000975 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000976 case MIToken::IntegerLiteral:
977 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +0000978 case MIToken::IntegerType:
979 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000980 case MIToken::kw_half:
981 case MIToken::kw_float:
982 case MIToken::kw_double:
983 case MIToken::kw_x86_fp80:
984 case MIToken::kw_fp128:
985 case MIToken::kw_ppc_fp128:
986 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000987 case MIToken::MachineBasicBlock:
988 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000989 case MIToken::StackObject:
990 return parseStackObjectOperand(Dest);
991 case MIToken::FixedStackObject:
992 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000993 case MIToken::GlobalValue:
994 case MIToken::NamedGlobalValue:
995 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000996 case MIToken::ConstantPoolItem:
997 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000998 case MIToken::JumpTableIndex:
999 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001000 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001001 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001002 case MIToken::exclaim:
1003 return parseMetadataOperand(Dest);
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001004 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001005 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001006 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001007 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001008 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001009 case MIToken::kw_blockaddress:
1010 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001011 case MIToken::kw_target_index:
1012 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001013 case MIToken::kw_liveout:
1014 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001015 case MIToken::Error:
1016 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001017 case MIToken::Identifier:
1018 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1019 Dest = MachineOperand::CreateRegMask(RegMask);
1020 lex();
1021 break;
1022 }
1023 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001024 default:
1025 // TODO: parse the other machine operands.
1026 return error("expected a machine operand");
1027 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001028 return false;
1029}
1030
Alex Lorenz49873a82015-08-06 00:44:07 +00001031bool MIParser::parseMachineOperandAndTargetFlags(MachineOperand &Dest) {
1032 unsigned TF = 0;
1033 bool HasTargetFlags = false;
1034 if (Token.is(MIToken::kw_target_flags)) {
1035 HasTargetFlags = true;
1036 lex();
1037 if (expectAndConsume(MIToken::lparen))
1038 return true;
1039 if (Token.isNot(MIToken::Identifier))
1040 return error("expected the name of the target flag");
1041 if (getDirectTargetFlag(Token.stringValue(), TF))
1042 return error("use of undefined target flag '" + Token.stringValue() +
1043 "'");
1044 lex();
1045 // TODO: Parse target's bit target flags.
1046 if (expectAndConsume(MIToken::rparen))
1047 return true;
1048 }
1049 auto Loc = Token.location();
1050 if (parseMachineOperand(Dest))
1051 return true;
1052 if (!HasTargetFlags)
1053 return false;
1054 if (Dest.isReg())
1055 return error(Loc, "register operands can't have target flags");
1056 Dest.setTargetFlags(TF);
1057 return false;
1058}
1059
Alex Lorenzdc24c172015-08-07 20:21:00 +00001060bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001061 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1062 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001063 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001064 bool IsNegative = Token.is(MIToken::minus);
1065 lex();
1066 if (Token.isNot(MIToken::IntegerLiteral))
1067 return error("expected an integer literal after '" + Sign + "'");
1068 if (Token.integerValue().getMinSignedBits() > 64)
1069 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001070 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001071 if (IsNegative)
1072 Offset = -Offset;
1073 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001074 return false;
1075}
1076
Alex Lorenz620f8912015-08-13 20:33:33 +00001077bool MIParser::parseAlignment(unsigned &Alignment) {
1078 assert(Token.is(MIToken::kw_align));
1079 lex();
1080 if (Token.isNot(MIToken::IntegerLiteral))
1081 return error("expected an integer literal after 'align'");
1082 if (getUnsigned(Alignment))
1083 return true;
1084 lex();
1085 return false;
1086}
1087
Alex Lorenzdc24c172015-08-07 20:21:00 +00001088bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1089 int64_t Offset = 0;
1090 if (parseOffset(Offset))
1091 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001092 Op.setOffset(Offset);
1093 return false;
1094}
1095
Alex Lorenz4af7e612015-08-03 23:08:19 +00001096bool MIParser::parseIRValue(Value *&V) {
1097 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001098 case MIToken::NamedIRValue: {
1099 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001100 if (!V)
Alex Lorenz2791dcc2015-08-12 21:27:16 +00001101 V = MF.getFunction()->getParent()->getValueSymbolTable().lookup(
1102 Token.stringValue());
1103 if (!V)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001104 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001105 break;
1106 }
1107 // TODO: Parse unnamed IR value references.
1108 default:
1109 llvm_unreachable("The current token should be an IR block reference");
1110 }
1111 return false;
1112}
1113
1114bool MIParser::getUint64(uint64_t &Result) {
1115 assert(Token.hasIntegerValue());
1116 if (Token.integerValue().getActiveBits() > 64)
1117 return error("expected 64-bit integer (too large)");
1118 Result = Token.integerValue().getZExtValue();
1119 return false;
1120}
1121
Alex Lorenza518b792015-08-04 00:24:45 +00001122bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
Alex Lorenze86d5152015-08-06 18:26:36 +00001123 const unsigned OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001124 switch (Token.kind()) {
1125 case MIToken::kw_volatile:
1126 Flags |= MachineMemOperand::MOVolatile;
1127 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001128 case MIToken::kw_non_temporal:
1129 Flags |= MachineMemOperand::MONonTemporal;
1130 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001131 case MIToken::kw_invariant:
1132 Flags |= MachineMemOperand::MOInvariant;
1133 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001134 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001135 default:
1136 llvm_unreachable("The current token should be a memory operand flag");
1137 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001138 if (OldFlags == Flags)
1139 // We know that the same flag is specified more than once when the flags
1140 // weren't modified.
1141 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001142 lex();
1143 return false;
1144}
1145
Alex Lorenz91097a32015-08-12 20:33:26 +00001146bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1147 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001148 case MIToken::kw_stack:
1149 PSV = MF.getPSVManager().getStack();
1150 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001151 case MIToken::kw_got:
1152 PSV = MF.getPSVManager().getGOT();
1153 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001154 case MIToken::kw_jump_table:
1155 PSV = MF.getPSVManager().getJumpTable();
1156 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001157 case MIToken::kw_constant_pool:
1158 PSV = MF.getPSVManager().getConstantPool();
1159 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001160 case MIToken::FixedStackObject: {
1161 int FI;
1162 if (parseFixedStackFrameIndex(FI))
1163 return true;
1164 PSV = MF.getPSVManager().getFixedStack(FI);
1165 // The token was already consumed, so use return here instead of break.
1166 return false;
1167 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001168 // TODO: Parse the other pseudo source values.
1169 default:
1170 llvm_unreachable("The current token should be pseudo source value");
1171 }
1172 lex();
1173 return false;
1174}
1175
1176bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001177 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001178 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
1179 Token.is(MIToken::FixedStackObject)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001180 const PseudoSourceValue *PSV = nullptr;
1181 if (parseMemoryPseudoSourceValue(PSV))
1182 return true;
1183 int64_t Offset = 0;
1184 if (parseOffset(Offset))
1185 return true;
1186 Dest = MachinePointerInfo(PSV, Offset);
1187 return false;
1188 }
1189 if (Token.isNot(MIToken::NamedIRValue))
1190 return error("expected an IR value reference");
1191 Value *V = nullptr;
1192 if (parseIRValue(V))
1193 return true;
1194 if (!V->getType()->isPointerTy())
1195 return error("expected a pointer IR value");
1196 lex();
1197 int64_t Offset = 0;
1198 if (parseOffset(Offset))
1199 return true;
1200 Dest = MachinePointerInfo(V, Offset);
1201 return false;
1202}
1203
Alex Lorenz4af7e612015-08-03 23:08:19 +00001204bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1205 if (expectAndConsume(MIToken::lparen))
1206 return true;
Alex Lorenza518b792015-08-04 00:24:45 +00001207 unsigned Flags = 0;
1208 while (Token.isMemoryOperandFlag()) {
1209 if (parseMemoryOperandFlag(Flags))
1210 return true;
1211 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001212 if (Token.isNot(MIToken::Identifier) ||
1213 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1214 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001215 if (Token.stringValue() == "load")
1216 Flags |= MachineMemOperand::MOLoad;
1217 else
1218 Flags |= MachineMemOperand::MOStore;
1219 lex();
1220
1221 if (Token.isNot(MIToken::IntegerLiteral))
1222 return error("expected the size integer literal after memory operation");
1223 uint64_t Size;
1224 if (getUint64(Size))
1225 return true;
1226 lex();
1227
1228 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1229 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1230 return error(Twine("expected '") + Word + "'");
1231 lex();
1232
Alex Lorenz91097a32015-08-12 20:33:26 +00001233 MachinePointerInfo Ptr = MachinePointerInfo();
1234 if (parseMachinePointerInfo(Ptr))
Alex Lorenz83127732015-08-07 20:26:52 +00001235 return true;
Alex Lorenz61420f72015-08-07 20:48:30 +00001236 unsigned BaseAlignment = Size;
1237 if (Token.is(MIToken::comma)) {
1238 lex();
1239 if (Token.isNot(MIToken::kw_align))
1240 return error("expected 'align'");
Alex Lorenz620f8912015-08-13 20:33:33 +00001241 if (parseAlignment(BaseAlignment))
Alex Lorenz61420f72015-08-07 20:48:30 +00001242 return true;
Alex Lorenz61420f72015-08-07 20:48:30 +00001243 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001244 // TODO: Parse the attached metadata nodes.
1245 if (expectAndConsume(MIToken::rparen))
1246 return true;
Alex Lorenz91097a32015-08-12 20:33:26 +00001247 Dest = MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001248 return false;
1249}
1250
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001251void MIParser::initNames2InstrOpCodes() {
1252 if (!Names2InstrOpCodes.empty())
1253 return;
1254 const auto *TII = MF.getSubtarget().getInstrInfo();
1255 assert(TII && "Expected target instruction info");
1256 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1257 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1258}
1259
1260bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1261 initNames2InstrOpCodes();
1262 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1263 if (InstrInfo == Names2InstrOpCodes.end())
1264 return true;
1265 OpCode = InstrInfo->getValue();
1266 return false;
1267}
1268
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001269void MIParser::initNames2Regs() {
1270 if (!Names2Regs.empty())
1271 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001272 // The '%noreg' register is the register 0.
1273 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001274 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1275 assert(TRI && "Expected target register info");
1276 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1277 bool WasInserted =
1278 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1279 .second;
1280 (void)WasInserted;
1281 assert(WasInserted && "Expected registers to be unique case-insensitively");
1282 }
1283}
1284
1285bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1286 initNames2Regs();
1287 auto RegInfo = Names2Regs.find(RegName);
1288 if (RegInfo == Names2Regs.end())
1289 return true;
1290 Reg = RegInfo->getValue();
1291 return false;
1292}
1293
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001294void MIParser::initNames2RegMasks() {
1295 if (!Names2RegMasks.empty())
1296 return;
1297 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1298 assert(TRI && "Expected target register info");
1299 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1300 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1301 assert(RegMasks.size() == RegMaskNames.size());
1302 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1303 Names2RegMasks.insert(
1304 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1305}
1306
1307const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1308 initNames2RegMasks();
1309 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1310 if (RegMaskInfo == Names2RegMasks.end())
1311 return nullptr;
1312 return RegMaskInfo->getValue();
1313}
1314
Alex Lorenz2eacca82015-07-13 23:24:34 +00001315void MIParser::initNames2SubRegIndices() {
1316 if (!Names2SubRegIndices.empty())
1317 return;
1318 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1319 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1320 Names2SubRegIndices.insert(
1321 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1322}
1323
1324unsigned MIParser::getSubRegIndex(StringRef Name) {
1325 initNames2SubRegIndices();
1326 auto SubRegInfo = Names2SubRegIndices.find(Name);
1327 if (SubRegInfo == Names2SubRegIndices.end())
1328 return 0;
1329 return SubRegInfo->getValue();
1330}
1331
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001332static void initSlots2BasicBlocks(
1333 const Function &F,
1334 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1335 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001336 MST.incorporateFunction(F);
1337 for (auto &BB : F) {
1338 if (BB.hasName())
1339 continue;
1340 int Slot = MST.getLocalSlot(&BB);
1341 if (Slot == -1)
1342 continue;
1343 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1344 }
1345}
1346
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001347static const BasicBlock *getIRBlockFromSlot(
1348 unsigned Slot,
1349 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001350 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1351 if (BlockInfo == Slots2BasicBlocks.end())
1352 return nullptr;
1353 return BlockInfo->second;
1354}
1355
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001356const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1357 if (Slots2BasicBlocks.empty())
1358 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1359 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1360}
1361
1362const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1363 if (&F == MF.getFunction())
1364 return getIRBlock(Slot);
1365 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1366 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1367 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1368}
1369
Alex Lorenzef5c1962015-07-28 23:02:45 +00001370void MIParser::initNames2TargetIndices() {
1371 if (!Names2TargetIndices.empty())
1372 return;
1373 const auto *TII = MF.getSubtarget().getInstrInfo();
1374 assert(TII && "Expected target instruction info");
1375 auto Indices = TII->getSerializableTargetIndices();
1376 for (const auto &I : Indices)
1377 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1378}
1379
1380bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1381 initNames2TargetIndices();
1382 auto IndexInfo = Names2TargetIndices.find(Name);
1383 if (IndexInfo == Names2TargetIndices.end())
1384 return true;
1385 Index = IndexInfo->second;
1386 return false;
1387}
1388
Alex Lorenz49873a82015-08-06 00:44:07 +00001389void MIParser::initNames2DirectTargetFlags() {
1390 if (!Names2DirectTargetFlags.empty())
1391 return;
1392 const auto *TII = MF.getSubtarget().getInstrInfo();
1393 assert(TII && "Expected target instruction info");
1394 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1395 for (const auto &I : Flags)
1396 Names2DirectTargetFlags.insert(
1397 std::make_pair(StringRef(I.second), I.first));
1398}
1399
1400bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1401 initNames2DirectTargetFlags();
1402 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1403 if (FlagInfo == Names2DirectTargetFlags.end())
1404 return true;
1405 Flag = FlagInfo->second;
1406 return false;
1407}
1408
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001409bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
1410 MachineFunction &MF, StringRef Src,
1411 const PerFunctionMIParsingState &PFS,
1412 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1413 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001414}
Alex Lorenzf09df002015-06-30 18:16:42 +00001415
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001416bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1417 MachineFunction &MF, StringRef Src,
1418 const PerFunctionMIParsingState &PFS,
1419 const SlotMapping &IRSlots, SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001420 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00001421}
Alex Lorenz9fab3702015-07-14 21:24:41 +00001422
1423bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1424 MachineFunction &MF, StringRef Src,
1425 const PerFunctionMIParsingState &PFS,
1426 const SlotMapping &IRSlots,
1427 SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001428 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1429 .parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00001430}
Alex Lorenz12045a42015-07-27 17:42:45 +00001431
1432bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1433 MachineFunction &MF, StringRef Src,
1434 const PerFunctionMIParsingState &PFS,
1435 const SlotMapping &IRSlots,
1436 SMDiagnostic &Error) {
1437 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1438 .parseStandaloneVirtualRegister(Reg);
1439}
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001440
1441bool llvm::parseIRBlockReference(const BasicBlock *&BB, SourceMgr &SM,
1442 MachineFunction &MF, StringRef Src,
1443 const PerFunctionMIParsingState &PFS,
1444 const SlotMapping &IRSlots,
1445 SMDiagnostic &Error) {
1446 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1447 .parseStandaloneIRBlockReference(BB);
1448}