blob: 36460b3f3eb90da0a58552d14e73503060333384 [file] [log] [blame]
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001//===- MIParser.cpp - Machine instructions parser implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the parsing of machine instructions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MIParser.h"
Alex Lorenz91370c52015-06-22 20:37:46 +000015#include "MILexer.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000016#include "llvm/ADT/StringMap.h"
Alex Lorenzad156fb2015-07-31 20:49:21 +000017#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000018#include "llvm/AsmParser/SlotMapping.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000019#include "llvm/CodeGen/MachineBasicBlock.h"
20#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000022#include "llvm/CodeGen/MachineInstr.h"
Alex Lorenzcb268d42015-07-06 23:07:26 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000024#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000026#include "llvm/IR/Instructions.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000027#include "llvm/IR/Constants.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000028#include "llvm/IR/Module.h"
Alex Lorenz8a1915b2015-07-27 22:42:41 +000029#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000030#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000031#include "llvm/Support/raw_ostream.h"
32#include "llvm/Support/SourceMgr.h"
33#include "llvm/Target/TargetSubtargetInfo.h"
34#include "llvm/Target/TargetInstrInfo.h"
35
36using namespace llvm;
37
38namespace {
39
Alex Lorenz36962cd2015-07-07 02:08:46 +000040/// A wrapper struct around the 'MachineOperand' struct that includes a source
41/// range.
42struct MachineOperandWithLocation {
43 MachineOperand Operand;
44 StringRef::iterator Begin;
45 StringRef::iterator End;
46
47 MachineOperandWithLocation(const MachineOperand &Operand,
48 StringRef::iterator Begin, StringRef::iterator End)
49 : Operand(Operand), Begin(Begin), End(End) {}
50};
51
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000052class MIParser {
53 SourceMgr &SM;
54 MachineFunction &MF;
55 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000056 StringRef Source, CurrentSource;
57 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000058 const PerFunctionMIParsingState &PFS;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000059 /// Maps from indices to unnamed global values and metadata nodes.
60 const SlotMapping &IRSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000061 /// Maps from instruction names to op codes.
62 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000063 /// Maps from register names to registers.
64 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000065 /// Maps from register mask names to register masks.
66 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000067 /// Maps from subregister names to subregister indices.
68 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000069 /// Maps from slot numbers to function's unnamed basic blocks.
70 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzef5c1962015-07-28 23:02:45 +000071 /// Maps from target index names to target indices.
72 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +000073 /// Maps from direct target flag names to the direct target flag values.
74 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000075
76public:
77 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000078 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000079 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000080
Alex Lorenz91370c52015-06-22 20:37:46 +000081 void lex();
82
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000083 /// Report an error at the current location with the given message.
84 ///
85 /// This function always return true.
86 bool error(const Twine &Msg);
87
Alex Lorenz91370c52015-06-22 20:37:46 +000088 /// Report an error at the given location with the given message.
89 ///
90 /// This function always return true.
91 bool error(StringRef::iterator Loc, const Twine &Msg);
92
Alex Lorenz3708a642015-06-30 17:47:50 +000093 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +000094 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
95 bool parseStandaloneNamedRegister(unsigned &Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +000096 bool parseStandaloneVirtualRegister(unsigned &Reg);
Alex Lorenz8a1915b2015-07-27 22:42:41 +000097 bool parseStandaloneIRBlockReference(const BasicBlock *&BB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000098
Alex Lorenzf3db51de2015-06-23 16:35:26 +000099 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000100 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000101 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000102 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000103 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000104 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Alex Lorenz05e38822015-08-05 18:52:21 +0000105 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000106 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000107 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000108 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000109 bool parseStackObjectOperand(MachineOperand &Dest);
110 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000111 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000112 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000113 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000114 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000115 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000116 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000117 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000118 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000119 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000120 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000121 bool parseIRBlock(BasicBlock *&BB, const Function &F);
122 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000123 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000124 bool parseMachineOperand(MachineOperand &Dest);
Alex Lorenz49873a82015-08-06 00:44:07 +0000125 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest);
Alex Lorenz5672a892015-08-05 22:26:15 +0000126 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000127 bool parseIRValue(Value *&V);
Alex Lorenza518b792015-08-04 00:24:45 +0000128 bool parseMemoryOperandFlag(unsigned &Flags);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000129 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000130
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000131private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000132 /// Convert the integer literal in the current token into an unsigned integer.
133 ///
134 /// Return true if an error occurred.
135 bool getUnsigned(unsigned &Result);
136
Alex Lorenz4af7e612015-08-03 23:08:19 +0000137 /// Convert the integer literal in the current token into an uint64.
138 ///
139 /// Return true if an error occurred.
140 bool getUint64(uint64_t &Result);
141
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000142 /// If the current token is of the given kind, consume it and return false.
143 /// Otherwise report an error and return true.
144 bool expectAndConsume(MIToken::TokenKind TokenKind);
145
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000146 void initNames2InstrOpCodes();
147
148 /// Try to convert an instruction name to an opcode. Return true if the
149 /// instruction name is invalid.
150 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000151
Alex Lorenze5a44662015-07-17 00:24:15 +0000152 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000153
Alex Lorenz36962cd2015-07-07 02:08:46 +0000154 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
155 const MCInstrDesc &MCID);
156
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000157 void initNames2Regs();
158
159 /// Try to convert a register name to a register number. Return true if the
160 /// register name is invalid.
161 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000162
163 void initNames2RegMasks();
164
165 /// Check if the given identifier is a name of a register mask.
166 ///
167 /// Return null if the identifier isn't a register mask.
168 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000169
170 void initNames2SubRegIndices();
171
172 /// Check if the given identifier is a name of a subregister index.
173 ///
174 /// Return 0 if the name isn't a subregister index class.
175 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000176
177 void initSlots2BasicBlocks();
178
179 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000180
181 void initNames2TargetIndices();
182
183 /// Try to convert a name of target index to the corresponding target index.
184 ///
185 /// Return true if the name isn't a name of a target index.
186 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000187
188 void initNames2DirectTargetFlags();
189
190 /// Try to convert a name of a direct target flag to the corresponding
191 /// target flag.
192 ///
193 /// Return true if the name isn't a name of a direct flag.
194 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000195};
196
197} // end anonymous namespace
198
199MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000200 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000201 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000202 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000203 Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000204
Alex Lorenz91370c52015-06-22 20:37:46 +0000205void MIParser::lex() {
206 CurrentSource = lexMIToken(
207 CurrentSource, Token,
208 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
209}
210
211bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
212
213bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000214 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
215 Error = SMDiagnostic(
216 SM, SMLoc(),
217 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
218 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000219 return true;
220}
221
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000222static const char *toString(MIToken::TokenKind TokenKind) {
223 switch (TokenKind) {
224 case MIToken::comma:
225 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000226 case MIToken::equal:
227 return "'='";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000228 case MIToken::lparen:
229 return "'('";
230 case MIToken::rparen:
231 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000232 default:
233 return "<unknown token>";
234 }
235}
236
237bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
238 if (Token.isNot(TokenKind))
239 return error(Twine("expected ") + toString(TokenKind));
240 lex();
241 return false;
242}
243
Alex Lorenz3708a642015-06-30 17:47:50 +0000244bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000245 lex();
246
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000247 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000248 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000249 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000250 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000251 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000252 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000253 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000254 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000255 if (Token.isNot(MIToken::comma))
256 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000257 lex();
258 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000259 if (!Operands.empty() && expectAndConsume(MIToken::equal))
260 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000261
Alex Lorenze5a44662015-07-17 00:24:15 +0000262 unsigned OpCode, Flags = 0;
263 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000264 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000265
Alex Lorenz4af7e612015-08-03 23:08:19 +0000266 // TODO: Parse the bundle instruction flags.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000267
268 // Parse the remaining machine operands.
Alex Lorenz4af7e612015-08-03 23:08:19 +0000269 while (Token.isNot(MIToken::Eof) && Token.isNot(MIToken::kw_debug_location) &&
270 Token.isNot(MIToken::coloncolon)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000271 auto Loc = Token.location();
Alex Lorenz49873a82015-08-06 00:44:07 +0000272 if (parseMachineOperandAndTargetFlags(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000273 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000274 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000275 if (Token.is(MIToken::Eof) || Token.is(MIToken::coloncolon))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000276 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000277 if (Token.isNot(MIToken::comma))
278 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000279 lex();
280 }
281
Alex Lorenz46d760d2015-07-22 21:15:11 +0000282 DebugLoc DebugLocation;
283 if (Token.is(MIToken::kw_debug_location)) {
284 lex();
285 if (Token.isNot(MIToken::exclaim))
286 return error("expected a metadata node after 'debug-location'");
287 MDNode *Node = nullptr;
288 if (parseMDNode(Node))
289 return true;
290 DebugLocation = DebugLoc(Node);
291 }
292
Alex Lorenz4af7e612015-08-03 23:08:19 +0000293 // Parse the machine memory operands.
294 SmallVector<MachineMemOperand *, 2> MemOperands;
295 if (Token.is(MIToken::coloncolon)) {
296 lex();
297 while (Token.isNot(MIToken::Eof)) {
298 MachineMemOperand *MemOp = nullptr;
299 if (parseMachineMemoryOperand(MemOp))
300 return true;
301 MemOperands.push_back(MemOp);
302 if (Token.is(MIToken::Eof))
303 break;
304 if (Token.isNot(MIToken::comma))
305 return error("expected ',' before the next machine memory operand");
306 lex();
307 }
308 }
309
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000310 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000311 if (!MCID.isVariadic()) {
312 // FIXME: Move the implicit operand verification to the machine verifier.
313 if (verifyImplicitOperands(Operands, MCID))
314 return true;
315 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000316
Alex Lorenzcb268d42015-07-06 23:07:26 +0000317 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000318 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000319 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000320 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000321 MI->addOperand(MF, Operand.Operand);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000322 if (MemOperands.empty())
323 return false;
324 MachineInstr::mmo_iterator MemRefs =
325 MF.allocateMemRefsArray(MemOperands.size());
326 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
327 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000328 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000329}
330
Alex Lorenz1ea60892015-07-27 20:29:27 +0000331bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000332 lex();
333 if (Token.isNot(MIToken::MachineBasicBlock))
334 return error("expected a machine basic block reference");
335 if (parseMBBReference(MBB))
336 return true;
337 lex();
338 if (Token.isNot(MIToken::Eof))
339 return error(
340 "expected end of string after the machine basic block reference");
341 return false;
342}
343
Alex Lorenz1ea60892015-07-27 20:29:27 +0000344bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000345 lex();
346 if (Token.isNot(MIToken::NamedRegister))
347 return error("expected a named 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 Lorenz12045a42015-07-27 17:42:45 +0000356bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
357 lex();
358 if (Token.isNot(MIToken::VirtualRegister))
359 return error("expected a virtual register");
360 if (parseRegister(Reg))
361 return 0;
362 lex();
363 if (Token.isNot(MIToken::Eof))
364 return error("expected end of string after the register reference");
365 return false;
366}
367
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000368bool MIParser::parseStandaloneIRBlockReference(const BasicBlock *&BB) {
369 lex();
370 if (Token.isNot(MIToken::IRBlock))
371 return error("expected an IR block reference");
372 unsigned SlotNumber = 0;
373 if (getUnsigned(SlotNumber))
374 return true;
375 BB = getIRBlock(SlotNumber);
376 if (!BB)
377 return error(Twine("use of undefined IR block '%ir-block.") +
378 Twine(SlotNumber) + "'");
379 lex();
380 if (Token.isNot(MIToken::Eof))
381 return error("expected end of string after the IR block reference");
382 return false;
383}
384
Alex Lorenz36962cd2015-07-07 02:08:46 +0000385static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
386 assert(MO.isImplicit());
387 return MO.isDef() ? "implicit-def" : "implicit";
388}
389
390static std::string getRegisterName(const TargetRegisterInfo *TRI,
391 unsigned Reg) {
392 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
393 return StringRef(TRI->getName(Reg)).lower();
394}
395
396bool MIParser::verifyImplicitOperands(
397 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
398 if (MCID.isCall())
399 // We can't verify call instructions as they can contain arbitrary implicit
400 // register and register mask operands.
401 return false;
402
403 // Gather all the expected implicit operands.
404 SmallVector<MachineOperand, 4> ImplicitOperands;
405 if (MCID.ImplicitDefs)
406 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
407 ImplicitOperands.push_back(
408 MachineOperand::CreateReg(*ImpDefs, true, true));
409 if (MCID.ImplicitUses)
410 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
411 ImplicitOperands.push_back(
412 MachineOperand::CreateReg(*ImpUses, false, true));
413
414 const auto *TRI = MF.getSubtarget().getRegisterInfo();
415 assert(TRI && "Expected target register info");
416 size_t I = ImplicitOperands.size(), J = Operands.size();
417 while (I) {
418 --I;
419 if (J) {
420 --J;
421 const auto &ImplicitOperand = ImplicitOperands[I];
422 const auto &Operand = Operands[J].Operand;
423 if (ImplicitOperand.isIdenticalTo(Operand))
424 continue;
425 if (Operand.isReg() && Operand.isImplicit()) {
426 return error(Operands[J].Begin,
427 Twine("expected an implicit register operand '") +
428 printImplicitRegisterFlag(ImplicitOperand) + " %" +
429 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
430 }
431 }
432 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
433 // insead of reporting an error at this location:
434 // %eax = MOV32r0
435 // ^
436 // report the error at the following location:
437 // %eax = MOV32r0
438 // ^
439 return error(J < Operands.size() ? Operands[J].End : Token.location(),
440 Twine("missing implicit register operand '") +
441 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
442 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
443 }
444 return false;
445}
446
Alex Lorenze5a44662015-07-17 00:24:15 +0000447bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
448 if (Token.is(MIToken::kw_frame_setup)) {
449 Flags |= MachineInstr::FrameSetup;
450 lex();
451 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000452 if (Token.isNot(MIToken::Identifier))
453 return error("expected a machine instruction");
454 StringRef InstrName = Token.stringValue();
455 if (parseInstrName(InstrName, OpCode))
456 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000457 lex();
458 return false;
459}
460
461bool MIParser::parseRegister(unsigned &Reg) {
462 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000463 case MIToken::underscore:
464 Reg = 0;
465 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000466 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000467 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000468 if (getRegisterByName(Name, Reg))
469 return error(Twine("unknown register name '") + Name + "'");
470 break;
471 }
Alex Lorenz53464512015-07-10 22:51:20 +0000472 case MIToken::VirtualRegister: {
473 unsigned ID;
474 if (getUnsigned(ID))
475 return true;
476 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
477 if (RegInfo == PFS.VirtualRegisterSlots.end())
478 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
479 "'");
480 Reg = RegInfo->second;
481 break;
482 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000483 // TODO: Parse other register kinds.
484 default:
485 llvm_unreachable("The current token should be a register");
486 }
487 return false;
488}
489
Alex Lorenzcb268d42015-07-06 23:07:26 +0000490bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000491 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000492 switch (Token.kind()) {
493 case MIToken::kw_implicit:
494 Flags |= RegState::Implicit;
495 break;
496 case MIToken::kw_implicit_define:
497 Flags |= RegState::ImplicitDefine;
498 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000499 case MIToken::kw_dead:
500 Flags |= RegState::Dead;
501 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000502 case MIToken::kw_killed:
503 Flags |= RegState::Kill;
504 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000505 case MIToken::kw_undef:
506 Flags |= RegState::Undef;
507 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000508 case MIToken::kw_early_clobber:
509 Flags |= RegState::EarlyClobber;
510 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000511 case MIToken::kw_debug_use:
512 Flags |= RegState::Debug;
513 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000514 // TODO: parse the other register flags.
515 default:
516 llvm_unreachable("The current token should be a register flag");
517 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000518 if (OldFlags == Flags)
519 // We know that the same flag is specified more than once when the flags
520 // weren't modified.
521 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000522 lex();
523 return false;
524}
525
Alex Lorenz2eacca82015-07-13 23:24:34 +0000526bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
527 assert(Token.is(MIToken::colon));
528 lex();
529 if (Token.isNot(MIToken::Identifier))
530 return error("expected a subregister index after ':'");
531 auto Name = Token.stringValue();
532 SubReg = getSubRegIndex(Name);
533 if (!SubReg)
534 return error(Twine("use of unknown subregister index '") + Name + "'");
535 lex();
536 return false;
537}
538
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000539bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
540 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000541 unsigned Flags = IsDef ? RegState::Define : 0;
542 while (Token.isRegisterFlag()) {
543 if (parseRegisterFlag(Flags))
544 return true;
545 }
546 if (!Token.isRegister())
547 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000548 if (parseRegister(Reg))
549 return true;
550 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000551 unsigned SubReg = 0;
552 if (Token.is(MIToken::colon)) {
553 if (parseSubRegisterIndex(SubReg))
554 return true;
555 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000556 Dest = MachineOperand::CreateReg(
557 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000558 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000559 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000560 return false;
561}
562
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000563bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
564 assert(Token.is(MIToken::IntegerLiteral));
565 const APSInt &Int = Token.integerValue();
566 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +0000567 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000568 Dest = MachineOperand::CreateImm(Int.getExtValue());
569 lex();
570 return false;
571}
572
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000573bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
574 auto Source = StringRef(Loc, Token.stringValue().end() - Loc).str();
575 lex();
576 SMDiagnostic Err;
577 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
578 if (!C)
579 return error(Loc + Err.getColumnNo(), Err.getMessage());
580 return false;
581}
582
Alex Lorenz05e38822015-08-05 18:52:21 +0000583bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
584 assert(Token.is(MIToken::IntegerType));
585 auto Loc = Token.location();
586 lex();
587 if (Token.isNot(MIToken::IntegerLiteral))
588 return error("expected an integer literal");
589 const Constant *C = nullptr;
590 if (parseIRConstant(Loc, C))
591 return true;
592 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
593 return false;
594}
595
Alex Lorenzad156fb2015-07-31 20:49:21 +0000596bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
597 auto Loc = Token.location();
598 lex();
599 if (Token.isNot(MIToken::FloatingPointLiteral))
600 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000601 const Constant *C = nullptr;
602 if (parseIRConstant(Loc, C))
603 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000604 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
605 return false;
606}
607
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000608bool MIParser::getUnsigned(unsigned &Result) {
609 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
610 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
611 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
612 if (Val64 == Limit)
613 return error("expected 32-bit integer (too large)");
614 Result = Val64;
615 return false;
616}
617
Alex Lorenzf09df002015-06-30 18:16:42 +0000618bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000619 assert(Token.is(MIToken::MachineBasicBlock));
620 unsigned Number;
621 if (getUnsigned(Number))
622 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000623 auto MBBInfo = PFS.MBBSlots.find(Number);
624 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000625 return error(Twine("use of undefined machine basic block #") +
626 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000627 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000628 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
629 return error(Twine("the name of machine basic block #") + Twine(Number) +
630 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000631 return false;
632}
633
634bool MIParser::parseMBBOperand(MachineOperand &Dest) {
635 MachineBasicBlock *MBB;
636 if (parseMBBReference(MBB))
637 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000638 Dest = MachineOperand::CreateMBB(MBB);
639 lex();
640 return false;
641}
642
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000643bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
644 assert(Token.is(MIToken::StackObject));
645 unsigned ID;
646 if (getUnsigned(ID))
647 return true;
648 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
649 if (ObjectInfo == PFS.StackObjectSlots.end())
650 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
651 "'");
652 StringRef Name;
653 if (const auto *Alloca =
654 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
655 Name = Alloca->getName();
656 if (!Token.stringValue().empty() && Token.stringValue() != Name)
657 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
658 "' isn't '" + Token.stringValue() + "'");
659 lex();
660 Dest = MachineOperand::CreateFI(ObjectInfo->second);
661 return false;
662}
663
664bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
665 assert(Token.is(MIToken::FixedStackObject));
666 unsigned ID;
667 if (getUnsigned(ID))
668 return true;
669 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
670 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
671 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
672 Twine(ID) + "'");
673 lex();
674 Dest = MachineOperand::CreateFI(ObjectInfo->second);
675 return false;
676}
677
Alex Lorenz41df7d32015-07-28 17:09:52 +0000678bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000679 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000680 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000681 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +0000682 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +0000683 if (!GV)
684 return error(Twine("use of undefined global value '@") +
685 Token.rawStringValue() + "'");
686 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000687 }
688 case MIToken::GlobalValue: {
689 unsigned GVIdx;
690 if (getUnsigned(GVIdx))
691 return true;
692 if (GVIdx >= IRSlots.GlobalValues.size())
693 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
694 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000695 GV = IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000696 break;
697 }
698 default:
699 llvm_unreachable("The current token should be a global value");
700 }
Alex Lorenz41df7d32015-07-28 17:09:52 +0000701 return false;
702}
703
704bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
705 GlobalValue *GV = nullptr;
706 if (parseGlobalValue(GV))
707 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000708 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000709 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000710 if (parseOperandsOffset(Dest))
711 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000712 return false;
713}
714
Alex Lorenzab980492015-07-20 20:51:18 +0000715bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
716 assert(Token.is(MIToken::ConstantPoolItem));
717 unsigned ID;
718 if (getUnsigned(ID))
719 return true;
720 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
721 if (ConstantInfo == PFS.ConstantPoolSlots.end())
722 return error("use of undefined constant '%const." + Twine(ID) + "'");
723 lex();
Alex Lorenzab980492015-07-20 20:51:18 +0000724 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000725 if (parseOperandsOffset(Dest))
726 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000727 return false;
728}
729
Alex Lorenz31d70682015-07-15 23:38:35 +0000730bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
731 assert(Token.is(MIToken::JumpTableIndex));
732 unsigned ID;
733 if (getUnsigned(ID))
734 return true;
735 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
736 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
737 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
738 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000739 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
740 return false;
741}
742
Alex Lorenz6ede3742015-07-21 16:59:53 +0000743bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000744 assert(Token.is(MIToken::ExternalSymbol));
745 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000746 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +0000747 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +0000748 if (parseOperandsOffset(Dest))
749 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000750 return false;
751}
752
Alex Lorenz44f29252015-07-22 21:07:04 +0000753bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +0000754 assert(Token.is(MIToken::exclaim));
755 auto Loc = Token.location();
756 lex();
757 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
758 return error("expected metadata id after '!'");
759 unsigned ID;
760 if (getUnsigned(ID))
761 return true;
762 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
763 if (NodeInfo == IRSlots.MetadataNodes.end())
764 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
765 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +0000766 Node = NodeInfo->second.get();
767 return false;
768}
769
770bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
771 MDNode *Node = nullptr;
772 if (parseMDNode(Node))
773 return true;
774 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000775 return false;
776}
777
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000778bool MIParser::parseCFIOffset(int &Offset) {
779 if (Token.isNot(MIToken::IntegerLiteral))
780 return error("expected a cfi offset");
781 if (Token.integerValue().getMinSignedBits() > 32)
782 return error("expected a 32 bit integer (the cfi offset is too large)");
783 Offset = (int)Token.integerValue().getExtValue();
784 lex();
785 return false;
786}
787
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000788bool MIParser::parseCFIRegister(unsigned &Reg) {
789 if (Token.isNot(MIToken::NamedRegister))
790 return error("expected a cfi register");
791 unsigned LLVMReg;
792 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000793 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000794 const auto *TRI = MF.getSubtarget().getRegisterInfo();
795 assert(TRI && "Expected target register info");
796 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
797 if (DwarfReg < 0)
798 return error("invalid DWARF register");
799 Reg = (unsigned)DwarfReg;
800 lex();
801 return false;
802}
803
804bool MIParser::parseCFIOperand(MachineOperand &Dest) {
805 auto Kind = Token.kind();
806 lex();
807 auto &MMI = MF.getMMI();
808 int Offset;
809 unsigned Reg;
810 unsigned CFIIndex;
811 switch (Kind) {
812 case MIToken::kw_cfi_offset:
813 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
814 parseCFIOffset(Offset))
815 return true;
816 CFIIndex =
817 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
818 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000819 case MIToken::kw_cfi_def_cfa_register:
820 if (parseCFIRegister(Reg))
821 return true;
822 CFIIndex =
823 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
824 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000825 case MIToken::kw_cfi_def_cfa_offset:
826 if (parseCFIOffset(Offset))
827 return true;
828 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
829 CFIIndex = MMI.addFrameInst(
830 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
831 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000832 case MIToken::kw_cfi_def_cfa:
833 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
834 parseCFIOffset(Offset))
835 return true;
836 // NB: MCCFIInstruction::createDefCfa negates the offset.
837 CFIIndex =
838 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
839 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000840 default:
841 // TODO: Parse the other CFI operands.
842 llvm_unreachable("The current token should be a cfi operand");
843 }
844 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000845 return false;
846}
847
Alex Lorenzdeb53492015-07-28 17:28:03 +0000848bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
849 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000850 case MIToken::NamedIRBlock: {
851 BB = dyn_cast_or_null<BasicBlock>(
852 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +0000853 if (!BB)
854 return error(Twine("use of undefined IR block '%ir-block.") +
855 Token.rawStringValue() + "'");
856 break;
857 }
858 case MIToken::IRBlock: {
859 unsigned SlotNumber = 0;
860 if (getUnsigned(SlotNumber))
861 return true;
862 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber));
863 if (!BB)
864 return error(Twine("use of undefined IR block '%ir-block.") +
865 Twine(SlotNumber) + "'");
866 break;
867 }
868 default:
869 llvm_unreachable("The current token should be an IR block reference");
870 }
871 return false;
872}
873
874bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
875 assert(Token.is(MIToken::kw_blockaddress));
876 lex();
877 if (expectAndConsume(MIToken::lparen))
878 return true;
879 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +0000880 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +0000881 return error("expected a global value");
882 GlobalValue *GV = nullptr;
883 if (parseGlobalValue(GV))
884 return true;
885 auto *F = dyn_cast<Function>(GV);
886 if (!F)
887 return error("expected an IR function reference");
888 lex();
889 if (expectAndConsume(MIToken::comma))
890 return true;
891 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +0000892 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +0000893 return error("expected an IR block reference");
894 if (parseIRBlock(BB, *F))
895 return true;
896 lex();
897 if (expectAndConsume(MIToken::rparen))
898 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000899 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000900 if (parseOperandsOffset(Dest))
901 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000902 return false;
903}
904
Alex Lorenzef5c1962015-07-28 23:02:45 +0000905bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
906 assert(Token.is(MIToken::kw_target_index));
907 lex();
908 if (expectAndConsume(MIToken::lparen))
909 return true;
910 if (Token.isNot(MIToken::Identifier))
911 return error("expected the name of the target index");
912 int Index = 0;
913 if (getTargetIndex(Token.stringValue(), Index))
914 return error("use of undefined target index '" + Token.stringValue() + "'");
915 lex();
916 if (expectAndConsume(MIToken::rparen))
917 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000918 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000919 if (parseOperandsOffset(Dest))
920 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000921 return false;
922}
923
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000924bool MIParser::parseMachineOperand(MachineOperand &Dest) {
925 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000926 case MIToken::kw_implicit:
927 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000928 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000929 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000930 case MIToken::kw_undef:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000931 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +0000932 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000933 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000934 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000935 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000936 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000937 case MIToken::IntegerLiteral:
938 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +0000939 case MIToken::IntegerType:
940 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000941 case MIToken::kw_half:
942 case MIToken::kw_float:
943 case MIToken::kw_double:
944 case MIToken::kw_x86_fp80:
945 case MIToken::kw_fp128:
946 case MIToken::kw_ppc_fp128:
947 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000948 case MIToken::MachineBasicBlock:
949 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000950 case MIToken::StackObject:
951 return parseStackObjectOperand(Dest);
952 case MIToken::FixedStackObject:
953 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000954 case MIToken::GlobalValue:
955 case MIToken::NamedGlobalValue:
956 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000957 case MIToken::ConstantPoolItem:
958 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000959 case MIToken::JumpTableIndex:
960 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000961 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +0000962 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +0000963 case MIToken::exclaim:
964 return parseMetadataOperand(Dest);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000965 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000966 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000967 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +0000968 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000969 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000970 case MIToken::kw_blockaddress:
971 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000972 case MIToken::kw_target_index:
973 return parseTargetIndexOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000974 case MIToken::Error:
975 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000976 case MIToken::Identifier:
977 if (const auto *RegMask = getRegMask(Token.stringValue())) {
978 Dest = MachineOperand::CreateRegMask(RegMask);
979 lex();
980 break;
981 }
982 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000983 default:
984 // TODO: parse the other machine operands.
985 return error("expected a machine operand");
986 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000987 return false;
988}
989
Alex Lorenz49873a82015-08-06 00:44:07 +0000990bool MIParser::parseMachineOperandAndTargetFlags(MachineOperand &Dest) {
991 unsigned TF = 0;
992 bool HasTargetFlags = false;
993 if (Token.is(MIToken::kw_target_flags)) {
994 HasTargetFlags = true;
995 lex();
996 if (expectAndConsume(MIToken::lparen))
997 return true;
998 if (Token.isNot(MIToken::Identifier))
999 return error("expected the name of the target flag");
1000 if (getDirectTargetFlag(Token.stringValue(), TF))
1001 return error("use of undefined target flag '" + Token.stringValue() +
1002 "'");
1003 lex();
1004 // TODO: Parse target's bit target flags.
1005 if (expectAndConsume(MIToken::rparen))
1006 return true;
1007 }
1008 auto Loc = Token.location();
1009 if (parseMachineOperand(Dest))
1010 return true;
1011 if (!HasTargetFlags)
1012 return false;
1013 if (Dest.isReg())
1014 return error(Loc, "register operands can't have target flags");
1015 Dest.setTargetFlags(TF);
1016 return false;
1017}
1018
Alex Lorenz5672a892015-08-05 22:26:15 +00001019bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1020 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1021 return false;
1022 StringRef Sign = Token.stringValue();
1023 bool IsNegative = Token.is(MIToken::minus);
1024 lex();
1025 if (Token.isNot(MIToken::IntegerLiteral))
1026 return error("expected an integer literal after '" + Sign + "'");
1027 if (Token.integerValue().getMinSignedBits() > 64)
1028 return error("expected 64-bit integer (too large)");
1029 int64_t Offset = Token.integerValue().getExtValue();
1030 if (IsNegative)
1031 Offset = -Offset;
1032 lex();
1033 Op.setOffset(Offset);
1034 return false;
1035}
1036
Alex Lorenz4af7e612015-08-03 23:08:19 +00001037bool MIParser::parseIRValue(Value *&V) {
1038 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001039 case MIToken::NamedIRValue: {
1040 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001041 if (!V)
1042 return error(Twine("use of undefined IR value '%ir.") +
1043 Token.rawStringValue() + "'");
1044 break;
1045 }
1046 // TODO: Parse unnamed IR value references.
1047 default:
1048 llvm_unreachable("The current token should be an IR block reference");
1049 }
1050 return false;
1051}
1052
1053bool MIParser::getUint64(uint64_t &Result) {
1054 assert(Token.hasIntegerValue());
1055 if (Token.integerValue().getActiveBits() > 64)
1056 return error("expected 64-bit integer (too large)");
1057 Result = Token.integerValue().getZExtValue();
1058 return false;
1059}
1060
Alex Lorenza518b792015-08-04 00:24:45 +00001061bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
1062 switch (Token.kind()) {
1063 case MIToken::kw_volatile:
1064 Flags |= MachineMemOperand::MOVolatile;
1065 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001066 case MIToken::kw_non_temporal:
1067 Flags |= MachineMemOperand::MONonTemporal;
1068 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001069 case MIToken::kw_invariant:
1070 Flags |= MachineMemOperand::MOInvariant;
1071 break;
Alex Lorenza518b792015-08-04 00:24:45 +00001072 // TODO: report an error when we specify the same flag more than once.
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001073 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001074 default:
1075 llvm_unreachable("The current token should be a memory operand flag");
1076 }
1077 lex();
1078 return false;
1079}
1080
Alex Lorenz4af7e612015-08-03 23:08:19 +00001081bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1082 if (expectAndConsume(MIToken::lparen))
1083 return true;
Alex Lorenza518b792015-08-04 00:24:45 +00001084 unsigned Flags = 0;
1085 while (Token.isMemoryOperandFlag()) {
1086 if (parseMemoryOperandFlag(Flags))
1087 return true;
1088 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001089 if (Token.isNot(MIToken::Identifier) ||
1090 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1091 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001092 if (Token.stringValue() == "load")
1093 Flags |= MachineMemOperand::MOLoad;
1094 else
1095 Flags |= MachineMemOperand::MOStore;
1096 lex();
1097
1098 if (Token.isNot(MIToken::IntegerLiteral))
1099 return error("expected the size integer literal after memory operation");
1100 uint64_t Size;
1101 if (getUint64(Size))
1102 return true;
1103 lex();
1104
1105 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1106 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1107 return error(Twine("expected '") + Word + "'");
1108 lex();
1109
1110 // TODO: Parse pseudo source values.
Alex Lorenz970c12e2015-08-05 17:35:55 +00001111 if (Token.isNot(MIToken::NamedIRValue))
Alex Lorenz4af7e612015-08-03 23:08:19 +00001112 return error("expected an IR value reference");
1113 Value *V = nullptr;
1114 if (parseIRValue(V))
1115 return true;
1116 if (!V->getType()->isPointerTy())
1117 return error("expected a pointer IR value");
1118 lex();
1119 // TODO: Parse the base alignment.
1120 // TODO: Parse the attached metadata nodes.
1121 if (expectAndConsume(MIToken::rparen))
1122 return true;
1123
1124 Dest = MF.getMachineMemOperand(MachinePointerInfo(V), Flags, Size, Size);
1125 return false;
1126}
1127
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001128void MIParser::initNames2InstrOpCodes() {
1129 if (!Names2InstrOpCodes.empty())
1130 return;
1131 const auto *TII = MF.getSubtarget().getInstrInfo();
1132 assert(TII && "Expected target instruction info");
1133 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1134 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1135}
1136
1137bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1138 initNames2InstrOpCodes();
1139 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1140 if (InstrInfo == Names2InstrOpCodes.end())
1141 return true;
1142 OpCode = InstrInfo->getValue();
1143 return false;
1144}
1145
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001146void MIParser::initNames2Regs() {
1147 if (!Names2Regs.empty())
1148 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001149 // The '%noreg' register is the register 0.
1150 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001151 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1152 assert(TRI && "Expected target register info");
1153 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1154 bool WasInserted =
1155 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1156 .second;
1157 (void)WasInserted;
1158 assert(WasInserted && "Expected registers to be unique case-insensitively");
1159 }
1160}
1161
1162bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1163 initNames2Regs();
1164 auto RegInfo = Names2Regs.find(RegName);
1165 if (RegInfo == Names2Regs.end())
1166 return true;
1167 Reg = RegInfo->getValue();
1168 return false;
1169}
1170
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001171void MIParser::initNames2RegMasks() {
1172 if (!Names2RegMasks.empty())
1173 return;
1174 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1175 assert(TRI && "Expected target register info");
1176 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1177 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1178 assert(RegMasks.size() == RegMaskNames.size());
1179 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1180 Names2RegMasks.insert(
1181 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1182}
1183
1184const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1185 initNames2RegMasks();
1186 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1187 if (RegMaskInfo == Names2RegMasks.end())
1188 return nullptr;
1189 return RegMaskInfo->getValue();
1190}
1191
Alex Lorenz2eacca82015-07-13 23:24:34 +00001192void MIParser::initNames2SubRegIndices() {
1193 if (!Names2SubRegIndices.empty())
1194 return;
1195 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1196 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1197 Names2SubRegIndices.insert(
1198 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1199}
1200
1201unsigned MIParser::getSubRegIndex(StringRef Name) {
1202 initNames2SubRegIndices();
1203 auto SubRegInfo = Names2SubRegIndices.find(Name);
1204 if (SubRegInfo == Names2SubRegIndices.end())
1205 return 0;
1206 return SubRegInfo->getValue();
1207}
1208
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001209void MIParser::initSlots2BasicBlocks() {
1210 if (!Slots2BasicBlocks.empty())
1211 return;
1212 const auto &F = *MF.getFunction();
1213 ModuleSlotTracker MST(F.getParent());
1214 MST.incorporateFunction(F);
1215 for (auto &BB : F) {
1216 if (BB.hasName())
1217 continue;
1218 int Slot = MST.getLocalSlot(&BB);
1219 if (Slot == -1)
1220 continue;
1221 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1222 }
1223}
1224
1225const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1226 initSlots2BasicBlocks();
1227 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1228 if (BlockInfo == Slots2BasicBlocks.end())
1229 return nullptr;
1230 return BlockInfo->second;
1231}
1232
Alex Lorenzef5c1962015-07-28 23:02:45 +00001233void MIParser::initNames2TargetIndices() {
1234 if (!Names2TargetIndices.empty())
1235 return;
1236 const auto *TII = MF.getSubtarget().getInstrInfo();
1237 assert(TII && "Expected target instruction info");
1238 auto Indices = TII->getSerializableTargetIndices();
1239 for (const auto &I : Indices)
1240 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1241}
1242
1243bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1244 initNames2TargetIndices();
1245 auto IndexInfo = Names2TargetIndices.find(Name);
1246 if (IndexInfo == Names2TargetIndices.end())
1247 return true;
1248 Index = IndexInfo->second;
1249 return false;
1250}
1251
Alex Lorenz49873a82015-08-06 00:44:07 +00001252void MIParser::initNames2DirectTargetFlags() {
1253 if (!Names2DirectTargetFlags.empty())
1254 return;
1255 const auto *TII = MF.getSubtarget().getInstrInfo();
1256 assert(TII && "Expected target instruction info");
1257 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1258 for (const auto &I : Flags)
1259 Names2DirectTargetFlags.insert(
1260 std::make_pair(StringRef(I.second), I.first));
1261}
1262
1263bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1264 initNames2DirectTargetFlags();
1265 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1266 if (FlagInfo == Names2DirectTargetFlags.end())
1267 return true;
1268 Flag = FlagInfo->second;
1269 return false;
1270}
1271
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001272bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
1273 MachineFunction &MF, StringRef Src,
1274 const PerFunctionMIParsingState &PFS,
1275 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1276 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001277}
Alex Lorenzf09df002015-06-30 18:16:42 +00001278
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001279bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1280 MachineFunction &MF, StringRef Src,
1281 const PerFunctionMIParsingState &PFS,
1282 const SlotMapping &IRSlots, SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001283 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00001284}
Alex Lorenz9fab3702015-07-14 21:24:41 +00001285
1286bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1287 MachineFunction &MF, StringRef Src,
1288 const PerFunctionMIParsingState &PFS,
1289 const SlotMapping &IRSlots,
1290 SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001291 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1292 .parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00001293}
Alex Lorenz12045a42015-07-27 17:42:45 +00001294
1295bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1296 MachineFunction &MF, StringRef Src,
1297 const PerFunctionMIParsingState &PFS,
1298 const SlotMapping &IRSlots,
1299 SMDiagnostic &Error) {
1300 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1301 .parseStandaloneVirtualRegister(Reg);
1302}
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001303
1304bool llvm::parseIRBlockReference(const BasicBlock *&BB, SourceMgr &SM,
1305 MachineFunction &MF, StringRef Src,
1306 const PerFunctionMIParsingState &PFS,
1307 const SlotMapping &IRSlots,
1308 SMDiagnostic &Error) {
1309 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1310 .parseStandaloneIRBlockReference(BB);
1311}