blob: a30a4a9a83fdabcd6214779de7f0291443dce35d [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 Lorenz7eaff4c2015-08-05 18:44:00 +0000102 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Alex Lorenz05e38822015-08-05 18:52:21 +0000103 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000104 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000105 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000106 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000107 bool parseStackObjectOperand(MachineOperand &Dest);
108 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000109 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000110 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000111 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000112 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000113 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000114 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000115 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000116 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000117 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000118 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000119 bool parseIRBlock(BasicBlock *&BB, const Function &F);
120 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000121 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000122 bool parseMachineOperand(MachineOperand &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000123 bool parseIRValue(Value *&V);
Alex Lorenza518b792015-08-04 00:24:45 +0000124 bool parseMemoryOperandFlag(unsigned &Flags);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000125 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000126
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000127private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000128 /// Convert the integer literal in the current token into an unsigned integer.
129 ///
130 /// Return true if an error occurred.
131 bool getUnsigned(unsigned &Result);
132
Alex Lorenz4af7e612015-08-03 23:08:19 +0000133 /// Convert the integer literal in the current token into an uint64.
134 ///
135 /// Return true if an error occurred.
136 bool getUint64(uint64_t &Result);
137
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000138 /// If the current token is of the given kind, consume it and return false.
139 /// Otherwise report an error and return true.
140 bool expectAndConsume(MIToken::TokenKind TokenKind);
141
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000142 void initNames2InstrOpCodes();
143
144 /// Try to convert an instruction name to an opcode. Return true if the
145 /// instruction name is invalid.
146 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000147
Alex Lorenze5a44662015-07-17 00:24:15 +0000148 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000149
Alex Lorenz36962cd2015-07-07 02:08:46 +0000150 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
151 const MCInstrDesc &MCID);
152
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000153 void initNames2Regs();
154
155 /// Try to convert a register name to a register number. Return true if the
156 /// register name is invalid.
157 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000158
159 void initNames2RegMasks();
160
161 /// Check if the given identifier is a name of a register mask.
162 ///
163 /// Return null if the identifier isn't a register mask.
164 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000165
166 void initNames2SubRegIndices();
167
168 /// Check if the given identifier is a name of a subregister index.
169 ///
170 /// Return 0 if the name isn't a subregister index class.
171 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000172
173 void initSlots2BasicBlocks();
174
175 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000176
177 void initNames2TargetIndices();
178
179 /// Try to convert a name of target index to the corresponding target index.
180 ///
181 /// Return true if the name isn't a name of a target index.
182 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000183};
184
185} // end anonymous namespace
186
187MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000188 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000189 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000190 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000191 Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000192
Alex Lorenz91370c52015-06-22 20:37:46 +0000193void MIParser::lex() {
194 CurrentSource = lexMIToken(
195 CurrentSource, Token,
196 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
197}
198
199bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
200
201bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000202 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
203 Error = SMDiagnostic(
204 SM, SMLoc(),
205 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
206 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000207 return true;
208}
209
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000210static const char *toString(MIToken::TokenKind TokenKind) {
211 switch (TokenKind) {
212 case MIToken::comma:
213 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000214 case MIToken::equal:
215 return "'='";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000216 case MIToken::lparen:
217 return "'('";
218 case MIToken::rparen:
219 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000220 default:
221 return "<unknown token>";
222 }
223}
224
225bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
226 if (Token.isNot(TokenKind))
227 return error(Twine("expected ") + toString(TokenKind));
228 lex();
229 return false;
230}
231
Alex Lorenz3708a642015-06-30 17:47:50 +0000232bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000233 lex();
234
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000235 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000236 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000237 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000238 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000239 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000240 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000241 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000242 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000243 if (Token.isNot(MIToken::comma))
244 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000245 lex();
246 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000247 if (!Operands.empty() && expectAndConsume(MIToken::equal))
248 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000249
Alex Lorenze5a44662015-07-17 00:24:15 +0000250 unsigned OpCode, Flags = 0;
251 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000252 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000253
Alex Lorenz4af7e612015-08-03 23:08:19 +0000254 // TODO: Parse the bundle instruction flags.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000255
256 // Parse the remaining machine operands.
Alex Lorenz4af7e612015-08-03 23:08:19 +0000257 while (Token.isNot(MIToken::Eof) && Token.isNot(MIToken::kw_debug_location) &&
258 Token.isNot(MIToken::coloncolon)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000259 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000260 if (parseMachineOperand(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000261 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000262 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000263 if (Token.is(MIToken::Eof) || Token.is(MIToken::coloncolon))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000264 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000265 if (Token.isNot(MIToken::comma))
266 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000267 lex();
268 }
269
Alex Lorenz46d760d2015-07-22 21:15:11 +0000270 DebugLoc DebugLocation;
271 if (Token.is(MIToken::kw_debug_location)) {
272 lex();
273 if (Token.isNot(MIToken::exclaim))
274 return error("expected a metadata node after 'debug-location'");
275 MDNode *Node = nullptr;
276 if (parseMDNode(Node))
277 return true;
278 DebugLocation = DebugLoc(Node);
279 }
280
Alex Lorenz4af7e612015-08-03 23:08:19 +0000281 // Parse the machine memory operands.
282 SmallVector<MachineMemOperand *, 2> MemOperands;
283 if (Token.is(MIToken::coloncolon)) {
284 lex();
285 while (Token.isNot(MIToken::Eof)) {
286 MachineMemOperand *MemOp = nullptr;
287 if (parseMachineMemoryOperand(MemOp))
288 return true;
289 MemOperands.push_back(MemOp);
290 if (Token.is(MIToken::Eof))
291 break;
292 if (Token.isNot(MIToken::comma))
293 return error("expected ',' before the next machine memory operand");
294 lex();
295 }
296 }
297
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000298 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000299 if (!MCID.isVariadic()) {
300 // FIXME: Move the implicit operand verification to the machine verifier.
301 if (verifyImplicitOperands(Operands, MCID))
302 return true;
303 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000304
Alex Lorenzcb268d42015-07-06 23:07:26 +0000305 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000306 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000307 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000308 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000309 MI->addOperand(MF, Operand.Operand);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000310 if (MemOperands.empty())
311 return false;
312 MachineInstr::mmo_iterator MemRefs =
313 MF.allocateMemRefsArray(MemOperands.size());
314 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
315 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000316 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000317}
318
Alex Lorenz1ea60892015-07-27 20:29:27 +0000319bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000320 lex();
321 if (Token.isNot(MIToken::MachineBasicBlock))
322 return error("expected a machine basic block reference");
323 if (parseMBBReference(MBB))
324 return true;
325 lex();
326 if (Token.isNot(MIToken::Eof))
327 return error(
328 "expected end of string after the machine basic block reference");
329 return false;
330}
331
Alex Lorenz1ea60892015-07-27 20:29:27 +0000332bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000333 lex();
334 if (Token.isNot(MIToken::NamedRegister))
335 return error("expected a named register");
336 if (parseRegister(Reg))
337 return 0;
338 lex();
339 if (Token.isNot(MIToken::Eof))
340 return error("expected end of string after the register reference");
341 return false;
342}
343
Alex Lorenz12045a42015-07-27 17:42:45 +0000344bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
345 lex();
346 if (Token.isNot(MIToken::VirtualRegister))
347 return error("expected a virtual register");
348 if (parseRegister(Reg))
349 return 0;
350 lex();
351 if (Token.isNot(MIToken::Eof))
352 return error("expected end of string after the register reference");
353 return false;
354}
355
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000356bool MIParser::parseStandaloneIRBlockReference(const BasicBlock *&BB) {
357 lex();
358 if (Token.isNot(MIToken::IRBlock))
359 return error("expected an IR block reference");
360 unsigned SlotNumber = 0;
361 if (getUnsigned(SlotNumber))
362 return true;
363 BB = getIRBlock(SlotNumber);
364 if (!BB)
365 return error(Twine("use of undefined IR block '%ir-block.") +
366 Twine(SlotNumber) + "'");
367 lex();
368 if (Token.isNot(MIToken::Eof))
369 return error("expected end of string after the IR block reference");
370 return false;
371}
372
Alex Lorenz36962cd2015-07-07 02:08:46 +0000373static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
374 assert(MO.isImplicit());
375 return MO.isDef() ? "implicit-def" : "implicit";
376}
377
378static std::string getRegisterName(const TargetRegisterInfo *TRI,
379 unsigned Reg) {
380 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
381 return StringRef(TRI->getName(Reg)).lower();
382}
383
384bool MIParser::verifyImplicitOperands(
385 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
386 if (MCID.isCall())
387 // We can't verify call instructions as they can contain arbitrary implicit
388 // register and register mask operands.
389 return false;
390
391 // Gather all the expected implicit operands.
392 SmallVector<MachineOperand, 4> ImplicitOperands;
393 if (MCID.ImplicitDefs)
394 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
395 ImplicitOperands.push_back(
396 MachineOperand::CreateReg(*ImpDefs, true, true));
397 if (MCID.ImplicitUses)
398 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
399 ImplicitOperands.push_back(
400 MachineOperand::CreateReg(*ImpUses, false, true));
401
402 const auto *TRI = MF.getSubtarget().getRegisterInfo();
403 assert(TRI && "Expected target register info");
404 size_t I = ImplicitOperands.size(), J = Operands.size();
405 while (I) {
406 --I;
407 if (J) {
408 --J;
409 const auto &ImplicitOperand = ImplicitOperands[I];
410 const auto &Operand = Operands[J].Operand;
411 if (ImplicitOperand.isIdenticalTo(Operand))
412 continue;
413 if (Operand.isReg() && Operand.isImplicit()) {
414 return error(Operands[J].Begin,
415 Twine("expected an implicit register operand '") +
416 printImplicitRegisterFlag(ImplicitOperand) + " %" +
417 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
418 }
419 }
420 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
421 // insead of reporting an error at this location:
422 // %eax = MOV32r0
423 // ^
424 // report the error at the following location:
425 // %eax = MOV32r0
426 // ^
427 return error(J < Operands.size() ? Operands[J].End : Token.location(),
428 Twine("missing implicit register operand '") +
429 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
430 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
431 }
432 return false;
433}
434
Alex Lorenze5a44662015-07-17 00:24:15 +0000435bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
436 if (Token.is(MIToken::kw_frame_setup)) {
437 Flags |= MachineInstr::FrameSetup;
438 lex();
439 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000440 if (Token.isNot(MIToken::Identifier))
441 return error("expected a machine instruction");
442 StringRef InstrName = Token.stringValue();
443 if (parseInstrName(InstrName, OpCode))
444 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000445 lex();
446 return false;
447}
448
449bool MIParser::parseRegister(unsigned &Reg) {
450 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000451 case MIToken::underscore:
452 Reg = 0;
453 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000454 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000455 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000456 if (getRegisterByName(Name, Reg))
457 return error(Twine("unknown register name '") + Name + "'");
458 break;
459 }
Alex Lorenz53464512015-07-10 22:51:20 +0000460 case MIToken::VirtualRegister: {
461 unsigned ID;
462 if (getUnsigned(ID))
463 return true;
464 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
465 if (RegInfo == PFS.VirtualRegisterSlots.end())
466 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
467 "'");
468 Reg = RegInfo->second;
469 break;
470 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000471 // TODO: Parse other register kinds.
472 default:
473 llvm_unreachable("The current token should be a register");
474 }
475 return false;
476}
477
Alex Lorenzcb268d42015-07-06 23:07:26 +0000478bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000479 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000480 switch (Token.kind()) {
481 case MIToken::kw_implicit:
482 Flags |= RegState::Implicit;
483 break;
484 case MIToken::kw_implicit_define:
485 Flags |= RegState::ImplicitDefine;
486 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000487 case MIToken::kw_dead:
488 Flags |= RegState::Dead;
489 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000490 case MIToken::kw_killed:
491 Flags |= RegState::Kill;
492 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000493 case MIToken::kw_undef:
494 Flags |= RegState::Undef;
495 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000496 case MIToken::kw_early_clobber:
497 Flags |= RegState::EarlyClobber;
498 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000499 case MIToken::kw_debug_use:
500 Flags |= RegState::Debug;
501 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000502 // TODO: parse the other register flags.
503 default:
504 llvm_unreachable("The current token should be a register flag");
505 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000506 if (OldFlags == Flags)
507 // We know that the same flag is specified more than once when the flags
508 // weren't modified.
509 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000510 lex();
511 return false;
512}
513
Alex Lorenz2eacca82015-07-13 23:24:34 +0000514bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
515 assert(Token.is(MIToken::colon));
516 lex();
517 if (Token.isNot(MIToken::Identifier))
518 return error("expected a subregister index after ':'");
519 auto Name = Token.stringValue();
520 SubReg = getSubRegIndex(Name);
521 if (!SubReg)
522 return error(Twine("use of unknown subregister index '") + Name + "'");
523 lex();
524 return false;
525}
526
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000527bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
528 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000529 unsigned Flags = IsDef ? RegState::Define : 0;
530 while (Token.isRegisterFlag()) {
531 if (parseRegisterFlag(Flags))
532 return true;
533 }
534 if (!Token.isRegister())
535 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000536 if (parseRegister(Reg))
537 return true;
538 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000539 unsigned SubReg = 0;
540 if (Token.is(MIToken::colon)) {
541 if (parseSubRegisterIndex(SubReg))
542 return true;
543 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000544 Dest = MachineOperand::CreateReg(
545 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000546 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000547 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000548 return false;
549}
550
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000551bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
552 assert(Token.is(MIToken::IntegerLiteral));
553 const APSInt &Int = Token.integerValue();
554 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +0000555 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000556 Dest = MachineOperand::CreateImm(Int.getExtValue());
557 lex();
558 return false;
559}
560
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000561bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
562 auto Source = StringRef(Loc, Token.stringValue().end() - Loc).str();
563 lex();
564 SMDiagnostic Err;
565 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
566 if (!C)
567 return error(Loc + Err.getColumnNo(), Err.getMessage());
568 return false;
569}
570
Alex Lorenz05e38822015-08-05 18:52:21 +0000571bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
572 assert(Token.is(MIToken::IntegerType));
573 auto Loc = Token.location();
574 lex();
575 if (Token.isNot(MIToken::IntegerLiteral))
576 return error("expected an integer literal");
577 const Constant *C = nullptr;
578 if (parseIRConstant(Loc, C))
579 return true;
580 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
581 return false;
582}
583
Alex Lorenzad156fb2015-07-31 20:49:21 +0000584bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
585 auto Loc = Token.location();
586 lex();
587 if (Token.isNot(MIToken::FloatingPointLiteral))
588 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000589 const Constant *C = nullptr;
590 if (parseIRConstant(Loc, C))
591 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000592 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
593 return false;
594}
595
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000596bool MIParser::getUnsigned(unsigned &Result) {
597 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
598 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
599 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
600 if (Val64 == Limit)
601 return error("expected 32-bit integer (too large)");
602 Result = Val64;
603 return false;
604}
605
Alex Lorenzf09df002015-06-30 18:16:42 +0000606bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000607 assert(Token.is(MIToken::MachineBasicBlock));
608 unsigned Number;
609 if (getUnsigned(Number))
610 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000611 auto MBBInfo = PFS.MBBSlots.find(Number);
612 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000613 return error(Twine("use of undefined machine basic block #") +
614 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000615 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000616 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
617 return error(Twine("the name of machine basic block #") + Twine(Number) +
618 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000619 return false;
620}
621
622bool MIParser::parseMBBOperand(MachineOperand &Dest) {
623 MachineBasicBlock *MBB;
624 if (parseMBBReference(MBB))
625 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000626 Dest = MachineOperand::CreateMBB(MBB);
627 lex();
628 return false;
629}
630
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000631bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
632 assert(Token.is(MIToken::StackObject));
633 unsigned ID;
634 if (getUnsigned(ID))
635 return true;
636 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
637 if (ObjectInfo == PFS.StackObjectSlots.end())
638 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
639 "'");
640 StringRef Name;
641 if (const auto *Alloca =
642 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
643 Name = Alloca->getName();
644 if (!Token.stringValue().empty() && Token.stringValue() != Name)
645 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
646 "' isn't '" + Token.stringValue() + "'");
647 lex();
648 Dest = MachineOperand::CreateFI(ObjectInfo->second);
649 return false;
650}
651
652bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
653 assert(Token.is(MIToken::FixedStackObject));
654 unsigned ID;
655 if (getUnsigned(ID))
656 return true;
657 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
658 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
659 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
660 Twine(ID) + "'");
661 lex();
662 Dest = MachineOperand::CreateFI(ObjectInfo->second);
663 return false;
664}
665
Alex Lorenz41df7d32015-07-28 17:09:52 +0000666bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000667 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000668 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000669 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +0000670 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +0000671 if (!GV)
672 return error(Twine("use of undefined global value '@") +
673 Token.rawStringValue() + "'");
674 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000675 }
676 case MIToken::GlobalValue: {
677 unsigned GVIdx;
678 if (getUnsigned(GVIdx))
679 return true;
680 if (GVIdx >= IRSlots.GlobalValues.size())
681 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
682 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000683 GV = IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000684 break;
685 }
686 default:
687 llvm_unreachable("The current token should be a global value");
688 }
Alex Lorenz41df7d32015-07-28 17:09:52 +0000689 return false;
690}
691
692bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
693 GlobalValue *GV = nullptr;
694 if (parseGlobalValue(GV))
695 return true;
696 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000697 // TODO: Parse offset and target flags.
698 lex();
699 return false;
700}
701
Alex Lorenzab980492015-07-20 20:51:18 +0000702bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
703 assert(Token.is(MIToken::ConstantPoolItem));
704 unsigned ID;
705 if (getUnsigned(ID))
706 return true;
707 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
708 if (ConstantInfo == PFS.ConstantPoolSlots.end())
709 return error("use of undefined constant '%const." + Twine(ID) + "'");
710 lex();
711 // TODO: Parse offset and target flags.
712 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
713 return false;
714}
715
Alex Lorenz31d70682015-07-15 23:38:35 +0000716bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
717 assert(Token.is(MIToken::JumpTableIndex));
718 unsigned ID;
719 if (getUnsigned(ID))
720 return true;
721 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
722 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
723 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
724 lex();
725 // TODO: Parse target flags.
726 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
727 return false;
728}
729
Alex Lorenz6ede3742015-07-21 16:59:53 +0000730bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000731 assert(Token.is(MIToken::ExternalSymbol));
732 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000733 lex();
734 // TODO: Parse the target flags.
735 Dest = MachineOperand::CreateES(Symbol);
736 return false;
737}
738
Alex Lorenz44f29252015-07-22 21:07:04 +0000739bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +0000740 assert(Token.is(MIToken::exclaim));
741 auto Loc = Token.location();
742 lex();
743 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
744 return error("expected metadata id after '!'");
745 unsigned ID;
746 if (getUnsigned(ID))
747 return true;
748 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
749 if (NodeInfo == IRSlots.MetadataNodes.end())
750 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
751 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +0000752 Node = NodeInfo->second.get();
753 return false;
754}
755
756bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
757 MDNode *Node = nullptr;
758 if (parseMDNode(Node))
759 return true;
760 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000761 return false;
762}
763
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000764bool MIParser::parseCFIOffset(int &Offset) {
765 if (Token.isNot(MIToken::IntegerLiteral))
766 return error("expected a cfi offset");
767 if (Token.integerValue().getMinSignedBits() > 32)
768 return error("expected a 32 bit integer (the cfi offset is too large)");
769 Offset = (int)Token.integerValue().getExtValue();
770 lex();
771 return false;
772}
773
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000774bool MIParser::parseCFIRegister(unsigned &Reg) {
775 if (Token.isNot(MIToken::NamedRegister))
776 return error("expected a cfi register");
777 unsigned LLVMReg;
778 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000779 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000780 const auto *TRI = MF.getSubtarget().getRegisterInfo();
781 assert(TRI && "Expected target register info");
782 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
783 if (DwarfReg < 0)
784 return error("invalid DWARF register");
785 Reg = (unsigned)DwarfReg;
786 lex();
787 return false;
788}
789
790bool MIParser::parseCFIOperand(MachineOperand &Dest) {
791 auto Kind = Token.kind();
792 lex();
793 auto &MMI = MF.getMMI();
794 int Offset;
795 unsigned Reg;
796 unsigned CFIIndex;
797 switch (Kind) {
798 case MIToken::kw_cfi_offset:
799 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
800 parseCFIOffset(Offset))
801 return true;
802 CFIIndex =
803 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
804 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000805 case MIToken::kw_cfi_def_cfa_register:
806 if (parseCFIRegister(Reg))
807 return true;
808 CFIIndex =
809 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
810 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000811 case MIToken::kw_cfi_def_cfa_offset:
812 if (parseCFIOffset(Offset))
813 return true;
814 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
815 CFIIndex = MMI.addFrameInst(
816 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
817 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000818 case MIToken::kw_cfi_def_cfa:
819 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
820 parseCFIOffset(Offset))
821 return true;
822 // NB: MCCFIInstruction::createDefCfa negates the offset.
823 CFIIndex =
824 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
825 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000826 default:
827 // TODO: Parse the other CFI operands.
828 llvm_unreachable("The current token should be a cfi operand");
829 }
830 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000831 return false;
832}
833
Alex Lorenzdeb53492015-07-28 17:28:03 +0000834bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
835 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000836 case MIToken::NamedIRBlock: {
837 BB = dyn_cast_or_null<BasicBlock>(
838 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +0000839 if (!BB)
840 return error(Twine("use of undefined IR block '%ir-block.") +
841 Token.rawStringValue() + "'");
842 break;
843 }
844 case MIToken::IRBlock: {
845 unsigned SlotNumber = 0;
846 if (getUnsigned(SlotNumber))
847 return true;
848 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber));
849 if (!BB)
850 return error(Twine("use of undefined IR block '%ir-block.") +
851 Twine(SlotNumber) + "'");
852 break;
853 }
854 default:
855 llvm_unreachable("The current token should be an IR block reference");
856 }
857 return false;
858}
859
860bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
861 assert(Token.is(MIToken::kw_blockaddress));
862 lex();
863 if (expectAndConsume(MIToken::lparen))
864 return true;
865 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +0000866 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +0000867 return error("expected a global value");
868 GlobalValue *GV = nullptr;
869 if (parseGlobalValue(GV))
870 return true;
871 auto *F = dyn_cast<Function>(GV);
872 if (!F)
873 return error("expected an IR function reference");
874 lex();
875 if (expectAndConsume(MIToken::comma))
876 return true;
877 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +0000878 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +0000879 return error("expected an IR block reference");
880 if (parseIRBlock(BB, *F))
881 return true;
882 lex();
883 if (expectAndConsume(MIToken::rparen))
884 return true;
885 // TODO: parse offset and target flags.
886 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
887 return false;
888}
889
Alex Lorenzef5c1962015-07-28 23:02:45 +0000890bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
891 assert(Token.is(MIToken::kw_target_index));
892 lex();
893 if (expectAndConsume(MIToken::lparen))
894 return true;
895 if (Token.isNot(MIToken::Identifier))
896 return error("expected the name of the target index");
897 int Index = 0;
898 if (getTargetIndex(Token.stringValue(), Index))
899 return error("use of undefined target index '" + Token.stringValue() + "'");
900 lex();
901 if (expectAndConsume(MIToken::rparen))
902 return true;
903 // TODO: Parse the offset and target flags.
904 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
905 return false;
906}
907
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000908bool MIParser::parseMachineOperand(MachineOperand &Dest) {
909 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000910 case MIToken::kw_implicit:
911 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000912 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000913 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000914 case MIToken::kw_undef:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000915 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +0000916 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000917 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000918 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000919 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000920 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000921 case MIToken::IntegerLiteral:
922 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +0000923 case MIToken::IntegerType:
924 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000925 case MIToken::kw_half:
926 case MIToken::kw_float:
927 case MIToken::kw_double:
928 case MIToken::kw_x86_fp80:
929 case MIToken::kw_fp128:
930 case MIToken::kw_ppc_fp128:
931 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000932 case MIToken::MachineBasicBlock:
933 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000934 case MIToken::StackObject:
935 return parseStackObjectOperand(Dest);
936 case MIToken::FixedStackObject:
937 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000938 case MIToken::GlobalValue:
939 case MIToken::NamedGlobalValue:
940 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000941 case MIToken::ConstantPoolItem:
942 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000943 case MIToken::JumpTableIndex:
944 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000945 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +0000946 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +0000947 case MIToken::exclaim:
948 return parseMetadataOperand(Dest);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000949 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000950 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000951 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +0000952 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000953 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000954 case MIToken::kw_blockaddress:
955 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000956 case MIToken::kw_target_index:
957 return parseTargetIndexOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000958 case MIToken::Error:
959 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000960 case MIToken::Identifier:
961 if (const auto *RegMask = getRegMask(Token.stringValue())) {
962 Dest = MachineOperand::CreateRegMask(RegMask);
963 lex();
964 break;
965 }
966 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000967 default:
968 // TODO: parse the other machine operands.
969 return error("expected a machine operand");
970 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000971 return false;
972}
973
Alex Lorenz4af7e612015-08-03 23:08:19 +0000974bool MIParser::parseIRValue(Value *&V) {
975 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000976 case MIToken::NamedIRValue: {
977 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +0000978 if (!V)
979 return error(Twine("use of undefined IR value '%ir.") +
980 Token.rawStringValue() + "'");
981 break;
982 }
983 // TODO: Parse unnamed IR value references.
984 default:
985 llvm_unreachable("The current token should be an IR block reference");
986 }
987 return false;
988}
989
990bool MIParser::getUint64(uint64_t &Result) {
991 assert(Token.hasIntegerValue());
992 if (Token.integerValue().getActiveBits() > 64)
993 return error("expected 64-bit integer (too large)");
994 Result = Token.integerValue().getZExtValue();
995 return false;
996}
997
Alex Lorenza518b792015-08-04 00:24:45 +0000998bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
999 switch (Token.kind()) {
1000 case MIToken::kw_volatile:
1001 Flags |= MachineMemOperand::MOVolatile;
1002 break;
1003 // TODO: report an error when we specify the same flag more than once.
1004 // TODO: parse the other memory operand flags.
1005 default:
1006 llvm_unreachable("The current token should be a memory operand flag");
1007 }
1008 lex();
1009 return false;
1010}
1011
Alex Lorenz4af7e612015-08-03 23:08:19 +00001012bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1013 if (expectAndConsume(MIToken::lparen))
1014 return true;
Alex Lorenza518b792015-08-04 00:24:45 +00001015 unsigned Flags = 0;
1016 while (Token.isMemoryOperandFlag()) {
1017 if (parseMemoryOperandFlag(Flags))
1018 return true;
1019 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001020 if (Token.isNot(MIToken::Identifier) ||
1021 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1022 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001023 if (Token.stringValue() == "load")
1024 Flags |= MachineMemOperand::MOLoad;
1025 else
1026 Flags |= MachineMemOperand::MOStore;
1027 lex();
1028
1029 if (Token.isNot(MIToken::IntegerLiteral))
1030 return error("expected the size integer literal after memory operation");
1031 uint64_t Size;
1032 if (getUint64(Size))
1033 return true;
1034 lex();
1035
1036 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1037 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1038 return error(Twine("expected '") + Word + "'");
1039 lex();
1040
1041 // TODO: Parse pseudo source values.
Alex Lorenz970c12e2015-08-05 17:35:55 +00001042 if (Token.isNot(MIToken::NamedIRValue))
Alex Lorenz4af7e612015-08-03 23:08:19 +00001043 return error("expected an IR value reference");
1044 Value *V = nullptr;
1045 if (parseIRValue(V))
1046 return true;
1047 if (!V->getType()->isPointerTy())
1048 return error("expected a pointer IR value");
1049 lex();
1050 // TODO: Parse the base alignment.
1051 // TODO: Parse the attached metadata nodes.
1052 if (expectAndConsume(MIToken::rparen))
1053 return true;
1054
1055 Dest = MF.getMachineMemOperand(MachinePointerInfo(V), Flags, Size, Size);
1056 return false;
1057}
1058
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001059void MIParser::initNames2InstrOpCodes() {
1060 if (!Names2InstrOpCodes.empty())
1061 return;
1062 const auto *TII = MF.getSubtarget().getInstrInfo();
1063 assert(TII && "Expected target instruction info");
1064 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1065 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1066}
1067
1068bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1069 initNames2InstrOpCodes();
1070 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1071 if (InstrInfo == Names2InstrOpCodes.end())
1072 return true;
1073 OpCode = InstrInfo->getValue();
1074 return false;
1075}
1076
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001077void MIParser::initNames2Regs() {
1078 if (!Names2Regs.empty())
1079 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001080 // The '%noreg' register is the register 0.
1081 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001082 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1083 assert(TRI && "Expected target register info");
1084 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1085 bool WasInserted =
1086 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1087 .second;
1088 (void)WasInserted;
1089 assert(WasInserted && "Expected registers to be unique case-insensitively");
1090 }
1091}
1092
1093bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1094 initNames2Regs();
1095 auto RegInfo = Names2Regs.find(RegName);
1096 if (RegInfo == Names2Regs.end())
1097 return true;
1098 Reg = RegInfo->getValue();
1099 return false;
1100}
1101
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001102void MIParser::initNames2RegMasks() {
1103 if (!Names2RegMasks.empty())
1104 return;
1105 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1106 assert(TRI && "Expected target register info");
1107 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1108 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1109 assert(RegMasks.size() == RegMaskNames.size());
1110 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1111 Names2RegMasks.insert(
1112 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1113}
1114
1115const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1116 initNames2RegMasks();
1117 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1118 if (RegMaskInfo == Names2RegMasks.end())
1119 return nullptr;
1120 return RegMaskInfo->getValue();
1121}
1122
Alex Lorenz2eacca82015-07-13 23:24:34 +00001123void MIParser::initNames2SubRegIndices() {
1124 if (!Names2SubRegIndices.empty())
1125 return;
1126 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1127 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1128 Names2SubRegIndices.insert(
1129 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1130}
1131
1132unsigned MIParser::getSubRegIndex(StringRef Name) {
1133 initNames2SubRegIndices();
1134 auto SubRegInfo = Names2SubRegIndices.find(Name);
1135 if (SubRegInfo == Names2SubRegIndices.end())
1136 return 0;
1137 return SubRegInfo->getValue();
1138}
1139
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001140void MIParser::initSlots2BasicBlocks() {
1141 if (!Slots2BasicBlocks.empty())
1142 return;
1143 const auto &F = *MF.getFunction();
1144 ModuleSlotTracker MST(F.getParent());
1145 MST.incorporateFunction(F);
1146 for (auto &BB : F) {
1147 if (BB.hasName())
1148 continue;
1149 int Slot = MST.getLocalSlot(&BB);
1150 if (Slot == -1)
1151 continue;
1152 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1153 }
1154}
1155
1156const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1157 initSlots2BasicBlocks();
1158 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1159 if (BlockInfo == Slots2BasicBlocks.end())
1160 return nullptr;
1161 return BlockInfo->second;
1162}
1163
Alex Lorenzef5c1962015-07-28 23:02:45 +00001164void MIParser::initNames2TargetIndices() {
1165 if (!Names2TargetIndices.empty())
1166 return;
1167 const auto *TII = MF.getSubtarget().getInstrInfo();
1168 assert(TII && "Expected target instruction info");
1169 auto Indices = TII->getSerializableTargetIndices();
1170 for (const auto &I : Indices)
1171 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1172}
1173
1174bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1175 initNames2TargetIndices();
1176 auto IndexInfo = Names2TargetIndices.find(Name);
1177 if (IndexInfo == Names2TargetIndices.end())
1178 return true;
1179 Index = IndexInfo->second;
1180 return false;
1181}
1182
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001183bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
1184 MachineFunction &MF, StringRef Src,
1185 const PerFunctionMIParsingState &PFS,
1186 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1187 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001188}
Alex Lorenzf09df002015-06-30 18:16:42 +00001189
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001190bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1191 MachineFunction &MF, StringRef Src,
1192 const PerFunctionMIParsingState &PFS,
1193 const SlotMapping &IRSlots, SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001194 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00001195}
Alex Lorenz9fab3702015-07-14 21:24:41 +00001196
1197bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1198 MachineFunction &MF, StringRef Src,
1199 const PerFunctionMIParsingState &PFS,
1200 const SlotMapping &IRSlots,
1201 SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001202 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1203 .parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00001204}
Alex Lorenz12045a42015-07-27 17:42:45 +00001205
1206bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1207 MachineFunction &MF, StringRef Src,
1208 const PerFunctionMIParsingState &PFS,
1209 const SlotMapping &IRSlots,
1210 SMDiagnostic &Error) {
1211 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1212 .parseStandaloneVirtualRegister(Reg);
1213}
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001214
1215bool llvm::parseIRBlockReference(const BasicBlock *&BB, SourceMgr &SM,
1216 MachineFunction &MF, StringRef Src,
1217 const PerFunctionMIParsingState &PFS,
1218 const SlotMapping &IRSlots,
1219 SMDiagnostic &Error) {
1220 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1221 .parseStandaloneIRBlockReference(BB);
1222}