blob: 7b9e45ea7f57a69ccab89b9a109444cfec38dd59 [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 Lorenz8e0a1b42015-06-22 17:02:30 +000073
74public:
75 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000076 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000077 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000078
Alex Lorenz91370c52015-06-22 20:37:46 +000079 void lex();
80
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000081 /// Report an error at the current location with the given message.
82 ///
83 /// This function always return true.
84 bool error(const Twine &Msg);
85
Alex Lorenz91370c52015-06-22 20:37:46 +000086 /// Report an error at the given location with the given message.
87 ///
88 /// This function always return true.
89 bool error(StringRef::iterator Loc, const Twine &Msg);
90
Alex Lorenz3708a642015-06-30 17:47:50 +000091 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +000092 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
93 bool parseStandaloneNamedRegister(unsigned &Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +000094 bool parseStandaloneVirtualRegister(unsigned &Reg);
Alex Lorenz8a1915b2015-07-27 22:42:41 +000095 bool parseStandaloneIRBlockReference(const BasicBlock *&BB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000096
Alex Lorenzf3db51de2015-06-23 16:35:26 +000097 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +000098 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +000099 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000100 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000101 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000102 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000103 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000104 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000105 bool parseStackObjectOperand(MachineOperand &Dest);
106 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000107 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000108 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000109 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000110 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000111 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000112 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000113 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000114 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000115 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000116 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000117 bool parseIRBlock(BasicBlock *&BB, const Function &F);
118 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000119 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000120 bool parseMachineOperand(MachineOperand &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000121 bool parseIRValue(Value *&V);
Alex Lorenza518b792015-08-04 00:24:45 +0000122 bool parseMemoryOperandFlag(unsigned &Flags);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000123 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000124
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000125private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000126 /// Convert the integer literal in the current token into an unsigned integer.
127 ///
128 /// Return true if an error occurred.
129 bool getUnsigned(unsigned &Result);
130
Alex Lorenz4af7e612015-08-03 23:08:19 +0000131 /// Convert the integer literal in the current token into an uint64.
132 ///
133 /// Return true if an error occurred.
134 bool getUint64(uint64_t &Result);
135
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000136 /// If the current token is of the given kind, consume it and return false.
137 /// Otherwise report an error and return true.
138 bool expectAndConsume(MIToken::TokenKind TokenKind);
139
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000140 void initNames2InstrOpCodes();
141
142 /// Try to convert an instruction name to an opcode. Return true if the
143 /// instruction name is invalid.
144 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000145
Alex Lorenze5a44662015-07-17 00:24:15 +0000146 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000147
Alex Lorenz36962cd2015-07-07 02:08:46 +0000148 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
149 const MCInstrDesc &MCID);
150
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000151 void initNames2Regs();
152
153 /// Try to convert a register name to a register number. Return true if the
154 /// register name is invalid.
155 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000156
157 void initNames2RegMasks();
158
159 /// Check if the given identifier is a name of a register mask.
160 ///
161 /// Return null if the identifier isn't a register mask.
162 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000163
164 void initNames2SubRegIndices();
165
166 /// Check if the given identifier is a name of a subregister index.
167 ///
168 /// Return 0 if the name isn't a subregister index class.
169 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000170
171 void initSlots2BasicBlocks();
172
173 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000174
175 void initNames2TargetIndices();
176
177 /// Try to convert a name of target index to the corresponding target index.
178 ///
179 /// Return true if the name isn't a name of a target index.
180 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000181};
182
183} // end anonymous namespace
184
185MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000186 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000187 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000188 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000189 Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000190
Alex Lorenz91370c52015-06-22 20:37:46 +0000191void MIParser::lex() {
192 CurrentSource = lexMIToken(
193 CurrentSource, Token,
194 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
195}
196
197bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
198
199bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000200 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
201 Error = SMDiagnostic(
202 SM, SMLoc(),
203 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
204 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000205 return true;
206}
207
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000208static const char *toString(MIToken::TokenKind TokenKind) {
209 switch (TokenKind) {
210 case MIToken::comma:
211 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000212 case MIToken::equal:
213 return "'='";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000214 case MIToken::lparen:
215 return "'('";
216 case MIToken::rparen:
217 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000218 default:
219 return "<unknown token>";
220 }
221}
222
223bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
224 if (Token.isNot(TokenKind))
225 return error(Twine("expected ") + toString(TokenKind));
226 lex();
227 return false;
228}
229
Alex Lorenz3708a642015-06-30 17:47:50 +0000230bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000231 lex();
232
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000233 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000234 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000235 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000236 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000237 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000238 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000239 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000240 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000241 if (Token.isNot(MIToken::comma))
242 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000243 lex();
244 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000245 if (!Operands.empty() && expectAndConsume(MIToken::equal))
246 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000247
Alex Lorenze5a44662015-07-17 00:24:15 +0000248 unsigned OpCode, Flags = 0;
249 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000250 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000251
Alex Lorenz4af7e612015-08-03 23:08:19 +0000252 // TODO: Parse the bundle instruction flags.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000253
254 // Parse the remaining machine operands.
Alex Lorenz4af7e612015-08-03 23:08:19 +0000255 while (Token.isNot(MIToken::Eof) && Token.isNot(MIToken::kw_debug_location) &&
256 Token.isNot(MIToken::coloncolon)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000257 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000258 if (parseMachineOperand(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000259 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000260 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000261 if (Token.is(MIToken::Eof) || Token.is(MIToken::coloncolon))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000262 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000263 if (Token.isNot(MIToken::comma))
264 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000265 lex();
266 }
267
Alex Lorenz46d760d2015-07-22 21:15:11 +0000268 DebugLoc DebugLocation;
269 if (Token.is(MIToken::kw_debug_location)) {
270 lex();
271 if (Token.isNot(MIToken::exclaim))
272 return error("expected a metadata node after 'debug-location'");
273 MDNode *Node = nullptr;
274 if (parseMDNode(Node))
275 return true;
276 DebugLocation = DebugLoc(Node);
277 }
278
Alex Lorenz4af7e612015-08-03 23:08:19 +0000279 // Parse the machine memory operands.
280 SmallVector<MachineMemOperand *, 2> MemOperands;
281 if (Token.is(MIToken::coloncolon)) {
282 lex();
283 while (Token.isNot(MIToken::Eof)) {
284 MachineMemOperand *MemOp = nullptr;
285 if (parseMachineMemoryOperand(MemOp))
286 return true;
287 MemOperands.push_back(MemOp);
288 if (Token.is(MIToken::Eof))
289 break;
290 if (Token.isNot(MIToken::comma))
291 return error("expected ',' before the next machine memory operand");
292 lex();
293 }
294 }
295
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000296 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000297 if (!MCID.isVariadic()) {
298 // FIXME: Move the implicit operand verification to the machine verifier.
299 if (verifyImplicitOperands(Operands, MCID))
300 return true;
301 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000302
Alex Lorenzcb268d42015-07-06 23:07:26 +0000303 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000304 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000305 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000306 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000307 MI->addOperand(MF, Operand.Operand);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000308 if (MemOperands.empty())
309 return false;
310 MachineInstr::mmo_iterator MemRefs =
311 MF.allocateMemRefsArray(MemOperands.size());
312 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
313 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000314 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000315}
316
Alex Lorenz1ea60892015-07-27 20:29:27 +0000317bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000318 lex();
319 if (Token.isNot(MIToken::MachineBasicBlock))
320 return error("expected a machine basic block reference");
321 if (parseMBBReference(MBB))
322 return true;
323 lex();
324 if (Token.isNot(MIToken::Eof))
325 return error(
326 "expected end of string after the machine basic block reference");
327 return false;
328}
329
Alex Lorenz1ea60892015-07-27 20:29:27 +0000330bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000331 lex();
332 if (Token.isNot(MIToken::NamedRegister))
333 return error("expected a named register");
334 if (parseRegister(Reg))
335 return 0;
336 lex();
337 if (Token.isNot(MIToken::Eof))
338 return error("expected end of string after the register reference");
339 return false;
340}
341
Alex Lorenz12045a42015-07-27 17:42:45 +0000342bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
343 lex();
344 if (Token.isNot(MIToken::VirtualRegister))
345 return error("expected a virtual register");
346 if (parseRegister(Reg))
347 return 0;
348 lex();
349 if (Token.isNot(MIToken::Eof))
350 return error("expected end of string after the register reference");
351 return false;
352}
353
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000354bool MIParser::parseStandaloneIRBlockReference(const BasicBlock *&BB) {
355 lex();
356 if (Token.isNot(MIToken::IRBlock))
357 return error("expected an IR block reference");
358 unsigned SlotNumber = 0;
359 if (getUnsigned(SlotNumber))
360 return true;
361 BB = getIRBlock(SlotNumber);
362 if (!BB)
363 return error(Twine("use of undefined IR block '%ir-block.") +
364 Twine(SlotNumber) + "'");
365 lex();
366 if (Token.isNot(MIToken::Eof))
367 return error("expected end of string after the IR block reference");
368 return false;
369}
370
Alex Lorenz36962cd2015-07-07 02:08:46 +0000371static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
372 assert(MO.isImplicit());
373 return MO.isDef() ? "implicit-def" : "implicit";
374}
375
376static std::string getRegisterName(const TargetRegisterInfo *TRI,
377 unsigned Reg) {
378 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
379 return StringRef(TRI->getName(Reg)).lower();
380}
381
382bool MIParser::verifyImplicitOperands(
383 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
384 if (MCID.isCall())
385 // We can't verify call instructions as they can contain arbitrary implicit
386 // register and register mask operands.
387 return false;
388
389 // Gather all the expected implicit operands.
390 SmallVector<MachineOperand, 4> ImplicitOperands;
391 if (MCID.ImplicitDefs)
392 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
393 ImplicitOperands.push_back(
394 MachineOperand::CreateReg(*ImpDefs, true, true));
395 if (MCID.ImplicitUses)
396 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
397 ImplicitOperands.push_back(
398 MachineOperand::CreateReg(*ImpUses, false, true));
399
400 const auto *TRI = MF.getSubtarget().getRegisterInfo();
401 assert(TRI && "Expected target register info");
402 size_t I = ImplicitOperands.size(), J = Operands.size();
403 while (I) {
404 --I;
405 if (J) {
406 --J;
407 const auto &ImplicitOperand = ImplicitOperands[I];
408 const auto &Operand = Operands[J].Operand;
409 if (ImplicitOperand.isIdenticalTo(Operand))
410 continue;
411 if (Operand.isReg() && Operand.isImplicit()) {
412 return error(Operands[J].Begin,
413 Twine("expected an implicit register operand '") +
414 printImplicitRegisterFlag(ImplicitOperand) + " %" +
415 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
416 }
417 }
418 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
419 // insead of reporting an error at this location:
420 // %eax = MOV32r0
421 // ^
422 // report the error at the following location:
423 // %eax = MOV32r0
424 // ^
425 return error(J < Operands.size() ? Operands[J].End : Token.location(),
426 Twine("missing implicit register operand '") +
427 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
428 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
429 }
430 return false;
431}
432
Alex Lorenze5a44662015-07-17 00:24:15 +0000433bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
434 if (Token.is(MIToken::kw_frame_setup)) {
435 Flags |= MachineInstr::FrameSetup;
436 lex();
437 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000438 if (Token.isNot(MIToken::Identifier))
439 return error("expected a machine instruction");
440 StringRef InstrName = Token.stringValue();
441 if (parseInstrName(InstrName, OpCode))
442 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000443 lex();
444 return false;
445}
446
447bool MIParser::parseRegister(unsigned &Reg) {
448 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000449 case MIToken::underscore:
450 Reg = 0;
451 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000452 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000453 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000454 if (getRegisterByName(Name, Reg))
455 return error(Twine("unknown register name '") + Name + "'");
456 break;
457 }
Alex Lorenz53464512015-07-10 22:51:20 +0000458 case MIToken::VirtualRegister: {
459 unsigned ID;
460 if (getUnsigned(ID))
461 return true;
462 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
463 if (RegInfo == PFS.VirtualRegisterSlots.end())
464 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
465 "'");
466 Reg = RegInfo->second;
467 break;
468 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000469 // TODO: Parse other register kinds.
470 default:
471 llvm_unreachable("The current token should be a register");
472 }
473 return false;
474}
475
Alex Lorenzcb268d42015-07-06 23:07:26 +0000476bool MIParser::parseRegisterFlag(unsigned &Flags) {
477 switch (Token.kind()) {
478 case MIToken::kw_implicit:
479 Flags |= RegState::Implicit;
480 break;
481 case MIToken::kw_implicit_define:
482 Flags |= RegState::ImplicitDefine;
483 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000484 case MIToken::kw_dead:
485 Flags |= RegState::Dead;
486 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000487 case MIToken::kw_killed:
488 Flags |= RegState::Kill;
489 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000490 case MIToken::kw_undef:
491 Flags |= RegState::Undef;
492 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000493 case MIToken::kw_debug_use:
494 Flags |= RegState::Debug;
495 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000496 // TODO: report an error when we specify the same flag more than once.
497 // TODO: parse the other register flags.
498 default:
499 llvm_unreachable("The current token should be a register flag");
500 }
501 lex();
502 return false;
503}
504
Alex Lorenz2eacca82015-07-13 23:24:34 +0000505bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
506 assert(Token.is(MIToken::colon));
507 lex();
508 if (Token.isNot(MIToken::Identifier))
509 return error("expected a subregister index after ':'");
510 auto Name = Token.stringValue();
511 SubReg = getSubRegIndex(Name);
512 if (!SubReg)
513 return error(Twine("use of unknown subregister index '") + Name + "'");
514 lex();
515 return false;
516}
517
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000518bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
519 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000520 unsigned Flags = IsDef ? RegState::Define : 0;
521 while (Token.isRegisterFlag()) {
522 if (parseRegisterFlag(Flags))
523 return true;
524 }
525 if (!Token.isRegister())
526 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000527 if (parseRegister(Reg))
528 return true;
529 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000530 unsigned SubReg = 0;
531 if (Token.is(MIToken::colon)) {
532 if (parseSubRegisterIndex(SubReg))
533 return true;
534 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000535 Dest = MachineOperand::CreateReg(
536 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000537 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz90752582015-08-05 17:41:17 +0000538 /*isEarlyClobber=*/false, SubReg, Flags & RegState::Debug);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000539 return false;
540}
541
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000542bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
543 assert(Token.is(MIToken::IntegerLiteral));
544 const APSInt &Int = Token.integerValue();
545 if (Int.getMinSignedBits() > 64)
546 // TODO: Replace this with an error when we can parse CIMM Machine Operands.
547 llvm_unreachable("Can't parse large integer literals yet!");
548 Dest = MachineOperand::CreateImm(Int.getExtValue());
549 lex();
550 return false;
551}
552
Alex Lorenzad156fb2015-07-31 20:49:21 +0000553bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
554 auto Loc = Token.location();
555 lex();
556 if (Token.isNot(MIToken::FloatingPointLiteral))
557 return error("expected a floating point literal");
558 auto Source = StringRef(Loc, Token.stringValue().end() - Loc).str();
559 lex();
560 SMDiagnostic Err;
561 const Constant *C =
562 parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
563 if (!C)
564 return error(Loc + Err.getColumnNo(), Err.getMessage());
565 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
566 return false;
567}
568
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000569bool MIParser::getUnsigned(unsigned &Result) {
570 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
571 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
572 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
573 if (Val64 == Limit)
574 return error("expected 32-bit integer (too large)");
575 Result = Val64;
576 return false;
577}
578
Alex Lorenzf09df002015-06-30 18:16:42 +0000579bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000580 assert(Token.is(MIToken::MachineBasicBlock));
581 unsigned Number;
582 if (getUnsigned(Number))
583 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000584 auto MBBInfo = PFS.MBBSlots.find(Number);
585 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000586 return error(Twine("use of undefined machine basic block #") +
587 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000588 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000589 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
590 return error(Twine("the name of machine basic block #") + Twine(Number) +
591 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000592 return false;
593}
594
595bool MIParser::parseMBBOperand(MachineOperand &Dest) {
596 MachineBasicBlock *MBB;
597 if (parseMBBReference(MBB))
598 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000599 Dest = MachineOperand::CreateMBB(MBB);
600 lex();
601 return false;
602}
603
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000604bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
605 assert(Token.is(MIToken::StackObject));
606 unsigned ID;
607 if (getUnsigned(ID))
608 return true;
609 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
610 if (ObjectInfo == PFS.StackObjectSlots.end())
611 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
612 "'");
613 StringRef Name;
614 if (const auto *Alloca =
615 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
616 Name = Alloca->getName();
617 if (!Token.stringValue().empty() && Token.stringValue() != Name)
618 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
619 "' isn't '" + Token.stringValue() + "'");
620 lex();
621 Dest = MachineOperand::CreateFI(ObjectInfo->second);
622 return false;
623}
624
625bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
626 assert(Token.is(MIToken::FixedStackObject));
627 unsigned ID;
628 if (getUnsigned(ID))
629 return true;
630 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
631 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
632 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
633 Twine(ID) + "'");
634 lex();
635 Dest = MachineOperand::CreateFI(ObjectInfo->second);
636 return false;
637}
638
Alex Lorenz41df7d32015-07-28 17:09:52 +0000639bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000640 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000641 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000642 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +0000643 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +0000644 if (!GV)
645 return error(Twine("use of undefined global value '@") +
646 Token.rawStringValue() + "'");
647 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000648 }
649 case MIToken::GlobalValue: {
650 unsigned GVIdx;
651 if (getUnsigned(GVIdx))
652 return true;
653 if (GVIdx >= IRSlots.GlobalValues.size())
654 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
655 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000656 GV = IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000657 break;
658 }
659 default:
660 llvm_unreachable("The current token should be a global value");
661 }
Alex Lorenz41df7d32015-07-28 17:09:52 +0000662 return false;
663}
664
665bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
666 GlobalValue *GV = nullptr;
667 if (parseGlobalValue(GV))
668 return true;
669 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000670 // TODO: Parse offset and target flags.
671 lex();
672 return false;
673}
674
Alex Lorenzab980492015-07-20 20:51:18 +0000675bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
676 assert(Token.is(MIToken::ConstantPoolItem));
677 unsigned ID;
678 if (getUnsigned(ID))
679 return true;
680 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
681 if (ConstantInfo == PFS.ConstantPoolSlots.end())
682 return error("use of undefined constant '%const." + Twine(ID) + "'");
683 lex();
684 // TODO: Parse offset and target flags.
685 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
686 return false;
687}
688
Alex Lorenz31d70682015-07-15 23:38:35 +0000689bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
690 assert(Token.is(MIToken::JumpTableIndex));
691 unsigned ID;
692 if (getUnsigned(ID))
693 return true;
694 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
695 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
696 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
697 lex();
698 // TODO: Parse target flags.
699 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
700 return false;
701}
702
Alex Lorenz6ede3742015-07-21 16:59:53 +0000703bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000704 assert(Token.is(MIToken::ExternalSymbol));
705 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000706 lex();
707 // TODO: Parse the target flags.
708 Dest = MachineOperand::CreateES(Symbol);
709 return false;
710}
711
Alex Lorenz44f29252015-07-22 21:07:04 +0000712bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +0000713 assert(Token.is(MIToken::exclaim));
714 auto Loc = Token.location();
715 lex();
716 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
717 return error("expected metadata id after '!'");
718 unsigned ID;
719 if (getUnsigned(ID))
720 return true;
721 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
722 if (NodeInfo == IRSlots.MetadataNodes.end())
723 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
724 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +0000725 Node = NodeInfo->second.get();
726 return false;
727}
728
729bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
730 MDNode *Node = nullptr;
731 if (parseMDNode(Node))
732 return true;
733 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000734 return false;
735}
736
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000737bool MIParser::parseCFIOffset(int &Offset) {
738 if (Token.isNot(MIToken::IntegerLiteral))
739 return error("expected a cfi offset");
740 if (Token.integerValue().getMinSignedBits() > 32)
741 return error("expected a 32 bit integer (the cfi offset is too large)");
742 Offset = (int)Token.integerValue().getExtValue();
743 lex();
744 return false;
745}
746
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000747bool MIParser::parseCFIRegister(unsigned &Reg) {
748 if (Token.isNot(MIToken::NamedRegister))
749 return error("expected a cfi register");
750 unsigned LLVMReg;
751 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000752 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000753 const auto *TRI = MF.getSubtarget().getRegisterInfo();
754 assert(TRI && "Expected target register info");
755 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
756 if (DwarfReg < 0)
757 return error("invalid DWARF register");
758 Reg = (unsigned)DwarfReg;
759 lex();
760 return false;
761}
762
763bool MIParser::parseCFIOperand(MachineOperand &Dest) {
764 auto Kind = Token.kind();
765 lex();
766 auto &MMI = MF.getMMI();
767 int Offset;
768 unsigned Reg;
769 unsigned CFIIndex;
770 switch (Kind) {
771 case MIToken::kw_cfi_offset:
772 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
773 parseCFIOffset(Offset))
774 return true;
775 CFIIndex =
776 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
777 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000778 case MIToken::kw_cfi_def_cfa_register:
779 if (parseCFIRegister(Reg))
780 return true;
781 CFIIndex =
782 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
783 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000784 case MIToken::kw_cfi_def_cfa_offset:
785 if (parseCFIOffset(Offset))
786 return true;
787 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
788 CFIIndex = MMI.addFrameInst(
789 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
790 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000791 case MIToken::kw_cfi_def_cfa:
792 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
793 parseCFIOffset(Offset))
794 return true;
795 // NB: MCCFIInstruction::createDefCfa negates the offset.
796 CFIIndex =
797 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
798 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000799 default:
800 // TODO: Parse the other CFI operands.
801 llvm_unreachable("The current token should be a cfi operand");
802 }
803 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000804 return false;
805}
806
Alex Lorenzdeb53492015-07-28 17:28:03 +0000807bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
808 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000809 case MIToken::NamedIRBlock: {
810 BB = dyn_cast_or_null<BasicBlock>(
811 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +0000812 if (!BB)
813 return error(Twine("use of undefined IR block '%ir-block.") +
814 Token.rawStringValue() + "'");
815 break;
816 }
817 case MIToken::IRBlock: {
818 unsigned SlotNumber = 0;
819 if (getUnsigned(SlotNumber))
820 return true;
821 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber));
822 if (!BB)
823 return error(Twine("use of undefined IR block '%ir-block.") +
824 Twine(SlotNumber) + "'");
825 break;
826 }
827 default:
828 llvm_unreachable("The current token should be an IR block reference");
829 }
830 return false;
831}
832
833bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
834 assert(Token.is(MIToken::kw_blockaddress));
835 lex();
836 if (expectAndConsume(MIToken::lparen))
837 return true;
838 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +0000839 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +0000840 return error("expected a global value");
841 GlobalValue *GV = nullptr;
842 if (parseGlobalValue(GV))
843 return true;
844 auto *F = dyn_cast<Function>(GV);
845 if (!F)
846 return error("expected an IR function reference");
847 lex();
848 if (expectAndConsume(MIToken::comma))
849 return true;
850 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +0000851 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +0000852 return error("expected an IR block reference");
853 if (parseIRBlock(BB, *F))
854 return true;
855 lex();
856 if (expectAndConsume(MIToken::rparen))
857 return true;
858 // TODO: parse offset and target flags.
859 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
860 return false;
861}
862
Alex Lorenzef5c1962015-07-28 23:02:45 +0000863bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
864 assert(Token.is(MIToken::kw_target_index));
865 lex();
866 if (expectAndConsume(MIToken::lparen))
867 return true;
868 if (Token.isNot(MIToken::Identifier))
869 return error("expected the name of the target index");
870 int Index = 0;
871 if (getTargetIndex(Token.stringValue(), Index))
872 return error("use of undefined target index '" + Token.stringValue() + "'");
873 lex();
874 if (expectAndConsume(MIToken::rparen))
875 return true;
876 // TODO: Parse the offset and target flags.
877 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
878 return false;
879}
880
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000881bool MIParser::parseMachineOperand(MachineOperand &Dest) {
882 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000883 case MIToken::kw_implicit:
884 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000885 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000886 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000887 case MIToken::kw_undef:
Alex Lorenz90752582015-08-05 17:41:17 +0000888 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000889 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000890 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000891 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000892 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000893 case MIToken::IntegerLiteral:
894 return parseImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000895 case MIToken::kw_half:
896 case MIToken::kw_float:
897 case MIToken::kw_double:
898 case MIToken::kw_x86_fp80:
899 case MIToken::kw_fp128:
900 case MIToken::kw_ppc_fp128:
901 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000902 case MIToken::MachineBasicBlock:
903 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000904 case MIToken::StackObject:
905 return parseStackObjectOperand(Dest);
906 case MIToken::FixedStackObject:
907 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000908 case MIToken::GlobalValue:
909 case MIToken::NamedGlobalValue:
910 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000911 case MIToken::ConstantPoolItem:
912 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000913 case MIToken::JumpTableIndex:
914 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000915 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +0000916 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +0000917 case MIToken::exclaim:
918 return parseMetadataOperand(Dest);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000919 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000920 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000921 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +0000922 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000923 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000924 case MIToken::kw_blockaddress:
925 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000926 case MIToken::kw_target_index:
927 return parseTargetIndexOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000928 case MIToken::Error:
929 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000930 case MIToken::Identifier:
931 if (const auto *RegMask = getRegMask(Token.stringValue())) {
932 Dest = MachineOperand::CreateRegMask(RegMask);
933 lex();
934 break;
935 }
936 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000937 default:
938 // TODO: parse the other machine operands.
939 return error("expected a machine operand");
940 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000941 return false;
942}
943
Alex Lorenz4af7e612015-08-03 23:08:19 +0000944bool MIParser::parseIRValue(Value *&V) {
945 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000946 case MIToken::NamedIRValue: {
947 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +0000948 if (!V)
949 return error(Twine("use of undefined IR value '%ir.") +
950 Token.rawStringValue() + "'");
951 break;
952 }
953 // TODO: Parse unnamed IR value references.
954 default:
955 llvm_unreachable("The current token should be an IR block reference");
956 }
957 return false;
958}
959
960bool MIParser::getUint64(uint64_t &Result) {
961 assert(Token.hasIntegerValue());
962 if (Token.integerValue().getActiveBits() > 64)
963 return error("expected 64-bit integer (too large)");
964 Result = Token.integerValue().getZExtValue();
965 return false;
966}
967
Alex Lorenza518b792015-08-04 00:24:45 +0000968bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
969 switch (Token.kind()) {
970 case MIToken::kw_volatile:
971 Flags |= MachineMemOperand::MOVolatile;
972 break;
973 // TODO: report an error when we specify the same flag more than once.
974 // TODO: parse the other memory operand flags.
975 default:
976 llvm_unreachable("The current token should be a memory operand flag");
977 }
978 lex();
979 return false;
980}
981
Alex Lorenz4af7e612015-08-03 23:08:19 +0000982bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
983 if (expectAndConsume(MIToken::lparen))
984 return true;
Alex Lorenza518b792015-08-04 00:24:45 +0000985 unsigned Flags = 0;
986 while (Token.isMemoryOperandFlag()) {
987 if (parseMemoryOperandFlag(Flags))
988 return true;
989 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000990 if (Token.isNot(MIToken::Identifier) ||
991 (Token.stringValue() != "load" && Token.stringValue() != "store"))
992 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +0000993 if (Token.stringValue() == "load")
994 Flags |= MachineMemOperand::MOLoad;
995 else
996 Flags |= MachineMemOperand::MOStore;
997 lex();
998
999 if (Token.isNot(MIToken::IntegerLiteral))
1000 return error("expected the size integer literal after memory operation");
1001 uint64_t Size;
1002 if (getUint64(Size))
1003 return true;
1004 lex();
1005
1006 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1007 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1008 return error(Twine("expected '") + Word + "'");
1009 lex();
1010
1011 // TODO: Parse pseudo source values.
Alex Lorenz970c12e2015-08-05 17:35:55 +00001012 if (Token.isNot(MIToken::NamedIRValue))
Alex Lorenz4af7e612015-08-03 23:08:19 +00001013 return error("expected an IR value reference");
1014 Value *V = nullptr;
1015 if (parseIRValue(V))
1016 return true;
1017 if (!V->getType()->isPointerTy())
1018 return error("expected a pointer IR value");
1019 lex();
1020 // TODO: Parse the base alignment.
1021 // TODO: Parse the attached metadata nodes.
1022 if (expectAndConsume(MIToken::rparen))
1023 return true;
1024
1025 Dest = MF.getMachineMemOperand(MachinePointerInfo(V), Flags, Size, Size);
1026 return false;
1027}
1028
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001029void MIParser::initNames2InstrOpCodes() {
1030 if (!Names2InstrOpCodes.empty())
1031 return;
1032 const auto *TII = MF.getSubtarget().getInstrInfo();
1033 assert(TII && "Expected target instruction info");
1034 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1035 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1036}
1037
1038bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1039 initNames2InstrOpCodes();
1040 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1041 if (InstrInfo == Names2InstrOpCodes.end())
1042 return true;
1043 OpCode = InstrInfo->getValue();
1044 return false;
1045}
1046
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001047void MIParser::initNames2Regs() {
1048 if (!Names2Regs.empty())
1049 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001050 // The '%noreg' register is the register 0.
1051 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001052 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1053 assert(TRI && "Expected target register info");
1054 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1055 bool WasInserted =
1056 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1057 .second;
1058 (void)WasInserted;
1059 assert(WasInserted && "Expected registers to be unique case-insensitively");
1060 }
1061}
1062
1063bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1064 initNames2Regs();
1065 auto RegInfo = Names2Regs.find(RegName);
1066 if (RegInfo == Names2Regs.end())
1067 return true;
1068 Reg = RegInfo->getValue();
1069 return false;
1070}
1071
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001072void MIParser::initNames2RegMasks() {
1073 if (!Names2RegMasks.empty())
1074 return;
1075 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1076 assert(TRI && "Expected target register info");
1077 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1078 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1079 assert(RegMasks.size() == RegMaskNames.size());
1080 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1081 Names2RegMasks.insert(
1082 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1083}
1084
1085const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1086 initNames2RegMasks();
1087 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1088 if (RegMaskInfo == Names2RegMasks.end())
1089 return nullptr;
1090 return RegMaskInfo->getValue();
1091}
1092
Alex Lorenz2eacca82015-07-13 23:24:34 +00001093void MIParser::initNames2SubRegIndices() {
1094 if (!Names2SubRegIndices.empty())
1095 return;
1096 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1097 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1098 Names2SubRegIndices.insert(
1099 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1100}
1101
1102unsigned MIParser::getSubRegIndex(StringRef Name) {
1103 initNames2SubRegIndices();
1104 auto SubRegInfo = Names2SubRegIndices.find(Name);
1105 if (SubRegInfo == Names2SubRegIndices.end())
1106 return 0;
1107 return SubRegInfo->getValue();
1108}
1109
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001110void MIParser::initSlots2BasicBlocks() {
1111 if (!Slots2BasicBlocks.empty())
1112 return;
1113 const auto &F = *MF.getFunction();
1114 ModuleSlotTracker MST(F.getParent());
1115 MST.incorporateFunction(F);
1116 for (auto &BB : F) {
1117 if (BB.hasName())
1118 continue;
1119 int Slot = MST.getLocalSlot(&BB);
1120 if (Slot == -1)
1121 continue;
1122 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1123 }
1124}
1125
1126const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1127 initSlots2BasicBlocks();
1128 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1129 if (BlockInfo == Slots2BasicBlocks.end())
1130 return nullptr;
1131 return BlockInfo->second;
1132}
1133
Alex Lorenzef5c1962015-07-28 23:02:45 +00001134void MIParser::initNames2TargetIndices() {
1135 if (!Names2TargetIndices.empty())
1136 return;
1137 const auto *TII = MF.getSubtarget().getInstrInfo();
1138 assert(TII && "Expected target instruction info");
1139 auto Indices = TII->getSerializableTargetIndices();
1140 for (const auto &I : Indices)
1141 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1142}
1143
1144bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1145 initNames2TargetIndices();
1146 auto IndexInfo = Names2TargetIndices.find(Name);
1147 if (IndexInfo == Names2TargetIndices.end())
1148 return true;
1149 Index = IndexInfo->second;
1150 return false;
1151}
1152
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001153bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
1154 MachineFunction &MF, StringRef Src,
1155 const PerFunctionMIParsingState &PFS,
1156 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1157 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001158}
Alex Lorenzf09df002015-06-30 18:16:42 +00001159
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001160bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1161 MachineFunction &MF, StringRef Src,
1162 const PerFunctionMIParsingState &PFS,
1163 const SlotMapping &IRSlots, SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001164 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00001165}
Alex Lorenz9fab3702015-07-14 21:24:41 +00001166
1167bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1168 MachineFunction &MF, StringRef Src,
1169 const PerFunctionMIParsingState &PFS,
1170 const SlotMapping &IRSlots,
1171 SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001172 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1173 .parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00001174}
Alex Lorenz12045a42015-07-27 17:42:45 +00001175
1176bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1177 MachineFunction &MF, StringRef Src,
1178 const PerFunctionMIParsingState &PFS,
1179 const SlotMapping &IRSlots,
1180 SMDiagnostic &Error) {
1181 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1182 .parseStandaloneVirtualRegister(Reg);
1183}
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001184
1185bool llvm::parseIRBlockReference(const BasicBlock *&BB, SourceMgr &SM,
1186 MachineFunction &MF, StringRef Src,
1187 const PerFunctionMIParsingState &PFS,
1188 const SlotMapping &IRSlots,
1189 SMDiagnostic &Error) {
1190 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1191 .parseStandaloneIRBlockReference(BB);
1192}