blob: c71eeb99fca5f0ae9c7909010a0d204a851a2d9a [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 Lorenz5d6108e2015-06-26 22:56:48 +000017#include "llvm/AsmParser/SlotMapping.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000018#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000021#include "llvm/CodeGen/MachineInstr.h"
Alex Lorenzcb268d42015-07-06 23:07:26 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000024#include "llvm/IR/Instructions.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000025#include "llvm/IR/Constants.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000026#include "llvm/IR/Module.h"
Alex Lorenz8a1915b2015-07-27 22:42:41 +000027#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000028#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000029#include "llvm/Support/raw_ostream.h"
30#include "llvm/Support/SourceMgr.h"
31#include "llvm/Target/TargetSubtargetInfo.h"
32#include "llvm/Target/TargetInstrInfo.h"
33
34using namespace llvm;
35
36namespace {
37
Alex Lorenzb29554d2015-07-20 20:31:01 +000038struct StringValueUtility {
39 StringRef String;
40 std::string UnescapedString;
41
42 StringValueUtility(const MIToken &Token) {
43 if (Token.isStringValueQuoted()) {
44 Token.unescapeQuotedStringValue(UnescapedString);
45 String = UnescapedString;
46 return;
47 }
48 String = Token.stringValue();
49 }
50
51 operator StringRef() const { return String; }
52};
53
Alex Lorenz36962cd2015-07-07 02:08:46 +000054/// A wrapper struct around the 'MachineOperand' struct that includes a source
55/// range.
56struct MachineOperandWithLocation {
57 MachineOperand Operand;
58 StringRef::iterator Begin;
59 StringRef::iterator End;
60
61 MachineOperandWithLocation(const MachineOperand &Operand,
62 StringRef::iterator Begin, StringRef::iterator End)
63 : Operand(Operand), Begin(Begin), End(End) {}
64};
65
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000066class MIParser {
67 SourceMgr &SM;
68 MachineFunction &MF;
69 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000070 StringRef Source, CurrentSource;
71 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000072 const PerFunctionMIParsingState &PFS;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000073 /// Maps from indices to unnamed global values and metadata nodes.
74 const SlotMapping &IRSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000075 /// Maps from instruction names to op codes.
76 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000077 /// Maps from register names to registers.
78 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000079 /// Maps from register mask names to register masks.
80 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000081 /// Maps from subregister names to subregister indices.
82 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000083 /// Maps from slot numbers to function's unnamed basic blocks.
84 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzef5c1962015-07-28 23:02:45 +000085 /// Maps from target index names to target indices.
86 StringMap<int> Names2TargetIndices;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000087
88public:
89 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000090 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000091 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000092
Alex Lorenz91370c52015-06-22 20:37:46 +000093 void lex();
94
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000095 /// Report an error at the current location with the given message.
96 ///
97 /// This function always return true.
98 bool error(const Twine &Msg);
99
Alex Lorenz91370c52015-06-22 20:37:46 +0000100 /// Report an error at the given location with the given message.
101 ///
102 /// This function always return true.
103 bool error(StringRef::iterator Loc, const Twine &Msg);
104
Alex Lorenz3708a642015-06-30 17:47:50 +0000105 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +0000106 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
107 bool parseStandaloneNamedRegister(unsigned &Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +0000108 bool parseStandaloneVirtualRegister(unsigned &Reg);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000109 bool parseStandaloneIRBlockReference(const BasicBlock *&BB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000110
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000111 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000112 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000113 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000114 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000115 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000116 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000117 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000118 bool parseStackObjectOperand(MachineOperand &Dest);
119 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000120 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000121 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000122 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000123 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000124 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000125 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000126 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000127 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000128 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000129 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000130 bool parseIRBlock(BasicBlock *&BB, const Function &F);
131 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000132 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000133 bool parseMachineOperand(MachineOperand &Dest);
134
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000135private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000136 /// Convert the integer literal in the current token into an unsigned integer.
137 ///
138 /// Return true if an error occurred.
139 bool getUnsigned(unsigned &Result);
140
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000141 /// If the current token is of the given kind, consume it and return false.
142 /// Otherwise report an error and return true.
143 bool expectAndConsume(MIToken::TokenKind TokenKind);
144
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000145 void initNames2InstrOpCodes();
146
147 /// Try to convert an instruction name to an opcode. Return true if the
148 /// instruction name is invalid.
149 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000150
Alex Lorenze5a44662015-07-17 00:24:15 +0000151 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000152
Alex Lorenz36962cd2015-07-07 02:08:46 +0000153 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
154 const MCInstrDesc &MCID);
155
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000156 void initNames2Regs();
157
158 /// Try to convert a register name to a register number. Return true if the
159 /// register name is invalid.
160 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000161
162 void initNames2RegMasks();
163
164 /// Check if the given identifier is a name of a register mask.
165 ///
166 /// Return null if the identifier isn't a register mask.
167 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000168
169 void initNames2SubRegIndices();
170
171 /// Check if the given identifier is a name of a subregister index.
172 ///
173 /// Return 0 if the name isn't a subregister index class.
174 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000175
176 void initSlots2BasicBlocks();
177
178 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000179
180 void initNames2TargetIndices();
181
182 /// Try to convert a name of target index to the corresponding target index.
183 ///
184 /// Return true if the name isn't a name of a target index.
185 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000186};
187
188} // end anonymous namespace
189
190MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000191 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000192 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000193 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000194 Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000195
Alex Lorenz91370c52015-06-22 20:37:46 +0000196void MIParser::lex() {
197 CurrentSource = lexMIToken(
198 CurrentSource, Token,
199 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
200}
201
202bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
203
204bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000205 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
206 Error = SMDiagnostic(
207 SM, SMLoc(),
208 SM.getMemoryBuffer(SM.getMainFileID())->getBufferIdentifier(), 1,
209 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000210 return true;
211}
212
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000213static const char *toString(MIToken::TokenKind TokenKind) {
214 switch (TokenKind) {
215 case MIToken::comma:
216 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000217 case MIToken::equal:
218 return "'='";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000219 case MIToken::lparen:
220 return "'('";
221 case MIToken::rparen:
222 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000223 default:
224 return "<unknown token>";
225 }
226}
227
228bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
229 if (Token.isNot(TokenKind))
230 return error(Twine("expected ") + toString(TokenKind));
231 lex();
232 return false;
233}
234
Alex Lorenz3708a642015-06-30 17:47:50 +0000235bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000236 lex();
237
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000238 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000239 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000240 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000241 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000242 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000243 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000244 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000245 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000246 if (Token.isNot(MIToken::comma))
247 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000248 lex();
249 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000250 if (!Operands.empty() && expectAndConsume(MIToken::equal))
251 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000252
Alex Lorenze5a44662015-07-17 00:24:15 +0000253 unsigned OpCode, Flags = 0;
254 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000255 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000256
Alex Lorenze5a44662015-07-17 00:24:15 +0000257 // TODO: Parse the bundle instruction flags and memory operands.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000258
259 // Parse the remaining machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000260 while (Token.isNot(MIToken::Eof) && Token.isNot(MIToken::kw_debug_location)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000261 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000262 if (parseMachineOperand(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000263 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000264 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000265 if (Token.is(MIToken::Eof))
266 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000267 if (Token.isNot(MIToken::comma))
268 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000269 lex();
270 }
271
Alex Lorenz46d760d2015-07-22 21:15:11 +0000272 DebugLoc DebugLocation;
273 if (Token.is(MIToken::kw_debug_location)) {
274 lex();
275 if (Token.isNot(MIToken::exclaim))
276 return error("expected a metadata node after 'debug-location'");
277 MDNode *Node = nullptr;
278 if (parseMDNode(Node))
279 return true;
280 DebugLocation = DebugLoc(Node);
281 }
282
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000283 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000284 if (!MCID.isVariadic()) {
285 // FIXME: Move the implicit operand verification to the machine verifier.
286 if (verifyImplicitOperands(Operands, MCID))
287 return true;
288 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000289
Alex Lorenzcb268d42015-07-06 23:07:26 +0000290 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000291 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000292 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000293 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000294 MI->addOperand(MF, Operand.Operand);
Alex Lorenz3708a642015-06-30 17:47:50 +0000295 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000296}
297
Alex Lorenz1ea60892015-07-27 20:29:27 +0000298bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000299 lex();
300 if (Token.isNot(MIToken::MachineBasicBlock))
301 return error("expected a machine basic block reference");
302 if (parseMBBReference(MBB))
303 return true;
304 lex();
305 if (Token.isNot(MIToken::Eof))
306 return error(
307 "expected end of string after the machine basic block reference");
308 return false;
309}
310
Alex Lorenz1ea60892015-07-27 20:29:27 +0000311bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000312 lex();
313 if (Token.isNot(MIToken::NamedRegister))
314 return error("expected a named register");
315 if (parseRegister(Reg))
316 return 0;
317 lex();
318 if (Token.isNot(MIToken::Eof))
319 return error("expected end of string after the register reference");
320 return false;
321}
322
Alex Lorenz12045a42015-07-27 17:42:45 +0000323bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
324 lex();
325 if (Token.isNot(MIToken::VirtualRegister))
326 return error("expected a virtual register");
327 if (parseRegister(Reg))
328 return 0;
329 lex();
330 if (Token.isNot(MIToken::Eof))
331 return error("expected end of string after the register reference");
332 return false;
333}
334
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000335bool MIParser::parseStandaloneIRBlockReference(const BasicBlock *&BB) {
336 lex();
337 if (Token.isNot(MIToken::IRBlock))
338 return error("expected an IR block reference");
339 unsigned SlotNumber = 0;
340 if (getUnsigned(SlotNumber))
341 return true;
342 BB = getIRBlock(SlotNumber);
343 if (!BB)
344 return error(Twine("use of undefined IR block '%ir-block.") +
345 Twine(SlotNumber) + "'");
346 lex();
347 if (Token.isNot(MIToken::Eof))
348 return error("expected end of string after the IR block reference");
349 return false;
350}
351
Alex Lorenz36962cd2015-07-07 02:08:46 +0000352static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
353 assert(MO.isImplicit());
354 return MO.isDef() ? "implicit-def" : "implicit";
355}
356
357static std::string getRegisterName(const TargetRegisterInfo *TRI,
358 unsigned Reg) {
359 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
360 return StringRef(TRI->getName(Reg)).lower();
361}
362
363bool MIParser::verifyImplicitOperands(
364 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
365 if (MCID.isCall())
366 // We can't verify call instructions as they can contain arbitrary implicit
367 // register and register mask operands.
368 return false;
369
370 // Gather all the expected implicit operands.
371 SmallVector<MachineOperand, 4> ImplicitOperands;
372 if (MCID.ImplicitDefs)
373 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
374 ImplicitOperands.push_back(
375 MachineOperand::CreateReg(*ImpDefs, true, true));
376 if (MCID.ImplicitUses)
377 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
378 ImplicitOperands.push_back(
379 MachineOperand::CreateReg(*ImpUses, false, true));
380
381 const auto *TRI = MF.getSubtarget().getRegisterInfo();
382 assert(TRI && "Expected target register info");
383 size_t I = ImplicitOperands.size(), J = Operands.size();
384 while (I) {
385 --I;
386 if (J) {
387 --J;
388 const auto &ImplicitOperand = ImplicitOperands[I];
389 const auto &Operand = Operands[J].Operand;
390 if (ImplicitOperand.isIdenticalTo(Operand))
391 continue;
392 if (Operand.isReg() && Operand.isImplicit()) {
393 return error(Operands[J].Begin,
394 Twine("expected an implicit register operand '") +
395 printImplicitRegisterFlag(ImplicitOperand) + " %" +
396 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
397 }
398 }
399 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
400 // insead of reporting an error at this location:
401 // %eax = MOV32r0
402 // ^
403 // report the error at the following location:
404 // %eax = MOV32r0
405 // ^
406 return error(J < Operands.size() ? Operands[J].End : Token.location(),
407 Twine("missing implicit register operand '") +
408 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
409 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
410 }
411 return false;
412}
413
Alex Lorenze5a44662015-07-17 00:24:15 +0000414bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
415 if (Token.is(MIToken::kw_frame_setup)) {
416 Flags |= MachineInstr::FrameSetup;
417 lex();
418 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000419 if (Token.isNot(MIToken::Identifier))
420 return error("expected a machine instruction");
421 StringRef InstrName = Token.stringValue();
422 if (parseInstrName(InstrName, OpCode))
423 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000424 lex();
425 return false;
426}
427
428bool MIParser::parseRegister(unsigned &Reg) {
429 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000430 case MIToken::underscore:
431 Reg = 0;
432 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000433 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000434 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000435 if (getRegisterByName(Name, Reg))
436 return error(Twine("unknown register name '") + Name + "'");
437 break;
438 }
Alex Lorenz53464512015-07-10 22:51:20 +0000439 case MIToken::VirtualRegister: {
440 unsigned ID;
441 if (getUnsigned(ID))
442 return true;
443 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
444 if (RegInfo == PFS.VirtualRegisterSlots.end())
445 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
446 "'");
447 Reg = RegInfo->second;
448 break;
449 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000450 // TODO: Parse other register kinds.
451 default:
452 llvm_unreachable("The current token should be a register");
453 }
454 return false;
455}
456
Alex Lorenzcb268d42015-07-06 23:07:26 +0000457bool MIParser::parseRegisterFlag(unsigned &Flags) {
458 switch (Token.kind()) {
459 case MIToken::kw_implicit:
460 Flags |= RegState::Implicit;
461 break;
462 case MIToken::kw_implicit_define:
463 Flags |= RegState::ImplicitDefine;
464 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000465 case MIToken::kw_dead:
466 Flags |= RegState::Dead;
467 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000468 case MIToken::kw_killed:
469 Flags |= RegState::Kill;
470 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000471 case MIToken::kw_undef:
472 Flags |= RegState::Undef;
473 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000474 // TODO: report an error when we specify the same flag more than once.
475 // TODO: parse the other register flags.
476 default:
477 llvm_unreachable("The current token should be a register flag");
478 }
479 lex();
480 return false;
481}
482
Alex Lorenz2eacca82015-07-13 23:24:34 +0000483bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
484 assert(Token.is(MIToken::colon));
485 lex();
486 if (Token.isNot(MIToken::Identifier))
487 return error("expected a subregister index after ':'");
488 auto Name = Token.stringValue();
489 SubReg = getSubRegIndex(Name);
490 if (!SubReg)
491 return error(Twine("use of unknown subregister index '") + Name + "'");
492 lex();
493 return false;
494}
495
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000496bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
497 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000498 unsigned Flags = IsDef ? RegState::Define : 0;
499 while (Token.isRegisterFlag()) {
500 if (parseRegisterFlag(Flags))
501 return true;
502 }
503 if (!Token.isRegister())
504 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000505 if (parseRegister(Reg))
506 return true;
507 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000508 unsigned SubReg = 0;
509 if (Token.is(MIToken::colon)) {
510 if (parseSubRegisterIndex(SubReg))
511 return true;
512 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000513 Dest = MachineOperand::CreateReg(
514 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000515 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
516 /*isEarlyClobber=*/false, SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000517 return false;
518}
519
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000520bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
521 assert(Token.is(MIToken::IntegerLiteral));
522 const APSInt &Int = Token.integerValue();
523 if (Int.getMinSignedBits() > 64)
524 // TODO: Replace this with an error when we can parse CIMM Machine Operands.
525 llvm_unreachable("Can't parse large integer literals yet!");
526 Dest = MachineOperand::CreateImm(Int.getExtValue());
527 lex();
528 return false;
529}
530
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000531bool MIParser::getUnsigned(unsigned &Result) {
532 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
533 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
534 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
535 if (Val64 == Limit)
536 return error("expected 32-bit integer (too large)");
537 Result = Val64;
538 return false;
539}
540
Alex Lorenzf09df002015-06-30 18:16:42 +0000541bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000542 assert(Token.is(MIToken::MachineBasicBlock));
543 unsigned Number;
544 if (getUnsigned(Number))
545 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000546 auto MBBInfo = PFS.MBBSlots.find(Number);
547 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000548 return error(Twine("use of undefined machine basic block #") +
549 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000550 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000551 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
552 return error(Twine("the name of machine basic block #") + Twine(Number) +
553 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000554 return false;
555}
556
557bool MIParser::parseMBBOperand(MachineOperand &Dest) {
558 MachineBasicBlock *MBB;
559 if (parseMBBReference(MBB))
560 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000561 Dest = MachineOperand::CreateMBB(MBB);
562 lex();
563 return false;
564}
565
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000566bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
567 assert(Token.is(MIToken::StackObject));
568 unsigned ID;
569 if (getUnsigned(ID))
570 return true;
571 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
572 if (ObjectInfo == PFS.StackObjectSlots.end())
573 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
574 "'");
575 StringRef Name;
576 if (const auto *Alloca =
577 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
578 Name = Alloca->getName();
579 if (!Token.stringValue().empty() && Token.stringValue() != Name)
580 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
581 "' isn't '" + Token.stringValue() + "'");
582 lex();
583 Dest = MachineOperand::CreateFI(ObjectInfo->second);
584 return false;
585}
586
587bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
588 assert(Token.is(MIToken::FixedStackObject));
589 unsigned ID;
590 if (getUnsigned(ID))
591 return true;
592 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
593 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
594 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
595 Twine(ID) + "'");
596 lex();
597 Dest = MachineOperand::CreateFI(ObjectInfo->second);
598 return false;
599}
600
Alex Lorenz41df7d32015-07-28 17:09:52 +0000601bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000602 switch (Token.kind()) {
Alex Lorenzb29554d2015-07-20 20:31:01 +0000603 case MIToken::NamedGlobalValue:
604 case MIToken::QuotedNamedGlobalValue: {
605 StringValueUtility Name(Token);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000606 const Module *M = MF.getFunction()->getParent();
Alex Lorenz41df7d32015-07-28 17:09:52 +0000607 GV = M->getNamedValue(Name);
608 if (!GV)
609 return error(Twine("use of undefined global value '@") +
610 Token.rawStringValue() + "'");
611 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000612 }
613 case MIToken::GlobalValue: {
614 unsigned GVIdx;
615 if (getUnsigned(GVIdx))
616 return true;
617 if (GVIdx >= IRSlots.GlobalValues.size())
618 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
619 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000620 GV = IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000621 break;
622 }
623 default:
624 llvm_unreachable("The current token should be a global value");
625 }
Alex Lorenz41df7d32015-07-28 17:09:52 +0000626 return false;
627}
628
629bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
630 GlobalValue *GV = nullptr;
631 if (parseGlobalValue(GV))
632 return true;
633 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000634 // TODO: Parse offset and target flags.
635 lex();
636 return false;
637}
638
Alex Lorenzab980492015-07-20 20:51:18 +0000639bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
640 assert(Token.is(MIToken::ConstantPoolItem));
641 unsigned ID;
642 if (getUnsigned(ID))
643 return true;
644 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
645 if (ConstantInfo == PFS.ConstantPoolSlots.end())
646 return error("use of undefined constant '%const." + Twine(ID) + "'");
647 lex();
648 // TODO: Parse offset and target flags.
649 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
650 return false;
651}
652
Alex Lorenz31d70682015-07-15 23:38:35 +0000653bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
654 assert(Token.is(MIToken::JumpTableIndex));
655 unsigned ID;
656 if (getUnsigned(ID))
657 return true;
658 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
659 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
660 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
661 lex();
662 // TODO: Parse target flags.
663 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
664 return false;
665}
666
Alex Lorenz6ede3742015-07-21 16:59:53 +0000667bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
668 assert(Token.is(MIToken::ExternalSymbol) ||
669 Token.is(MIToken::QuotedExternalSymbol));
670 StringValueUtility Name(Token);
671 const char *Symbol = MF.createExternalSymbolName(Name);
672 lex();
673 // TODO: Parse the target flags.
674 Dest = MachineOperand::CreateES(Symbol);
675 return false;
676}
677
Alex Lorenz44f29252015-07-22 21:07:04 +0000678bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +0000679 assert(Token.is(MIToken::exclaim));
680 auto Loc = Token.location();
681 lex();
682 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
683 return error("expected metadata id after '!'");
684 unsigned ID;
685 if (getUnsigned(ID))
686 return true;
687 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
688 if (NodeInfo == IRSlots.MetadataNodes.end())
689 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
690 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +0000691 Node = NodeInfo->second.get();
692 return false;
693}
694
695bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
696 MDNode *Node = nullptr;
697 if (parseMDNode(Node))
698 return true;
699 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000700 return false;
701}
702
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000703bool MIParser::parseCFIOffset(int &Offset) {
704 if (Token.isNot(MIToken::IntegerLiteral))
705 return error("expected a cfi offset");
706 if (Token.integerValue().getMinSignedBits() > 32)
707 return error("expected a 32 bit integer (the cfi offset is too large)");
708 Offset = (int)Token.integerValue().getExtValue();
709 lex();
710 return false;
711}
712
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000713bool MIParser::parseCFIRegister(unsigned &Reg) {
714 if (Token.isNot(MIToken::NamedRegister))
715 return error("expected a cfi register");
716 unsigned LLVMReg;
717 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000718 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000719 const auto *TRI = MF.getSubtarget().getRegisterInfo();
720 assert(TRI && "Expected target register info");
721 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
722 if (DwarfReg < 0)
723 return error("invalid DWARF register");
724 Reg = (unsigned)DwarfReg;
725 lex();
726 return false;
727}
728
729bool MIParser::parseCFIOperand(MachineOperand &Dest) {
730 auto Kind = Token.kind();
731 lex();
732 auto &MMI = MF.getMMI();
733 int Offset;
734 unsigned Reg;
735 unsigned CFIIndex;
736 switch (Kind) {
737 case MIToken::kw_cfi_offset:
738 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
739 parseCFIOffset(Offset))
740 return true;
741 CFIIndex =
742 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
743 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000744 case MIToken::kw_cfi_def_cfa_register:
745 if (parseCFIRegister(Reg))
746 return true;
747 CFIIndex =
748 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
749 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000750 case MIToken::kw_cfi_def_cfa_offset:
751 if (parseCFIOffset(Offset))
752 return true;
753 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
754 CFIIndex = MMI.addFrameInst(
755 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
756 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000757 case MIToken::kw_cfi_def_cfa:
758 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
759 parseCFIOffset(Offset))
760 return true;
761 // NB: MCCFIInstruction::createDefCfa negates the offset.
762 CFIIndex =
763 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
764 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000765 default:
766 // TODO: Parse the other CFI operands.
767 llvm_unreachable("The current token should be a cfi operand");
768 }
769 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000770 return false;
771}
772
Alex Lorenzdeb53492015-07-28 17:28:03 +0000773bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
774 switch (Token.kind()) {
775 case MIToken::NamedIRBlock:
776 case MIToken::QuotedNamedIRBlock: {
777 StringValueUtility Name(Token);
778 BB = dyn_cast_or_null<BasicBlock>(F.getValueSymbolTable().lookup(Name));
779 if (!BB)
780 return error(Twine("use of undefined IR block '%ir-block.") +
781 Token.rawStringValue() + "'");
782 break;
783 }
784 case MIToken::IRBlock: {
785 unsigned SlotNumber = 0;
786 if (getUnsigned(SlotNumber))
787 return true;
788 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber));
789 if (!BB)
790 return error(Twine("use of undefined IR block '%ir-block.") +
791 Twine(SlotNumber) + "'");
792 break;
793 }
794 default:
795 llvm_unreachable("The current token should be an IR block reference");
796 }
797 return false;
798}
799
800bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
801 assert(Token.is(MIToken::kw_blockaddress));
802 lex();
803 if (expectAndConsume(MIToken::lparen))
804 return true;
805 if (Token.isNot(MIToken::GlobalValue) &&
806 Token.isNot(MIToken::NamedGlobalValue) &&
807 Token.isNot(MIToken::QuotedNamedGlobalValue))
808 return error("expected a global value");
809 GlobalValue *GV = nullptr;
810 if (parseGlobalValue(GV))
811 return true;
812 auto *F = dyn_cast<Function>(GV);
813 if (!F)
814 return error("expected an IR function reference");
815 lex();
816 if (expectAndConsume(MIToken::comma))
817 return true;
818 BasicBlock *BB = nullptr;
819 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock) &&
820 Token.isNot(MIToken::QuotedNamedIRBlock))
821 return error("expected an IR block reference");
822 if (parseIRBlock(BB, *F))
823 return true;
824 lex();
825 if (expectAndConsume(MIToken::rparen))
826 return true;
827 // TODO: parse offset and target flags.
828 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
829 return false;
830}
831
Alex Lorenzef5c1962015-07-28 23:02:45 +0000832bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
833 assert(Token.is(MIToken::kw_target_index));
834 lex();
835 if (expectAndConsume(MIToken::lparen))
836 return true;
837 if (Token.isNot(MIToken::Identifier))
838 return error("expected the name of the target index");
839 int Index = 0;
840 if (getTargetIndex(Token.stringValue(), Index))
841 return error("use of undefined target index '" + Token.stringValue() + "'");
842 lex();
843 if (expectAndConsume(MIToken::rparen))
844 return true;
845 // TODO: Parse the offset and target flags.
846 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
847 return false;
848}
849
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000850bool MIParser::parseMachineOperand(MachineOperand &Dest) {
851 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000852 case MIToken::kw_implicit:
853 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000854 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +0000855 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +0000856 case MIToken::kw_undef:
Alex Lorenz12b554e2015-06-24 17:34:58 +0000857 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000858 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +0000859 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000860 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000861 case MIToken::IntegerLiteral:
862 return parseImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000863 case MIToken::MachineBasicBlock:
864 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000865 case MIToken::StackObject:
866 return parseStackObjectOperand(Dest);
867 case MIToken::FixedStackObject:
868 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000869 case MIToken::GlobalValue:
870 case MIToken::NamedGlobalValue:
Alex Lorenzb29554d2015-07-20 20:31:01 +0000871 case MIToken::QuotedNamedGlobalValue:
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000872 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000873 case MIToken::ConstantPoolItem:
874 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000875 case MIToken::JumpTableIndex:
876 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000877 case MIToken::ExternalSymbol:
878 case MIToken::QuotedExternalSymbol:
879 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +0000880 case MIToken::exclaim:
881 return parseMetadataOperand(Dest);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000882 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000883 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000884 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +0000885 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000886 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000887 case MIToken::kw_blockaddress:
888 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000889 case MIToken::kw_target_index:
890 return parseTargetIndexOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000891 case MIToken::Error:
892 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000893 case MIToken::Identifier:
894 if (const auto *RegMask = getRegMask(Token.stringValue())) {
895 Dest = MachineOperand::CreateRegMask(RegMask);
896 lex();
897 break;
898 }
899 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000900 default:
901 // TODO: parse the other machine operands.
902 return error("expected a machine operand");
903 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000904 return false;
905}
906
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000907void MIParser::initNames2InstrOpCodes() {
908 if (!Names2InstrOpCodes.empty())
909 return;
910 const auto *TII = MF.getSubtarget().getInstrInfo();
911 assert(TII && "Expected target instruction info");
912 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
913 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
914}
915
916bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
917 initNames2InstrOpCodes();
918 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
919 if (InstrInfo == Names2InstrOpCodes.end())
920 return true;
921 OpCode = InstrInfo->getValue();
922 return false;
923}
924
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000925void MIParser::initNames2Regs() {
926 if (!Names2Regs.empty())
927 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +0000928 // The '%noreg' register is the register 0.
929 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000930 const auto *TRI = MF.getSubtarget().getRegisterInfo();
931 assert(TRI && "Expected target register info");
932 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
933 bool WasInserted =
934 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
935 .second;
936 (void)WasInserted;
937 assert(WasInserted && "Expected registers to be unique case-insensitively");
938 }
939}
940
941bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
942 initNames2Regs();
943 auto RegInfo = Names2Regs.find(RegName);
944 if (RegInfo == Names2Regs.end())
945 return true;
946 Reg = RegInfo->getValue();
947 return false;
948}
949
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000950void MIParser::initNames2RegMasks() {
951 if (!Names2RegMasks.empty())
952 return;
953 const auto *TRI = MF.getSubtarget().getRegisterInfo();
954 assert(TRI && "Expected target register info");
955 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
956 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
957 assert(RegMasks.size() == RegMaskNames.size());
958 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
959 Names2RegMasks.insert(
960 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
961}
962
963const uint32_t *MIParser::getRegMask(StringRef Identifier) {
964 initNames2RegMasks();
965 auto RegMaskInfo = Names2RegMasks.find(Identifier);
966 if (RegMaskInfo == Names2RegMasks.end())
967 return nullptr;
968 return RegMaskInfo->getValue();
969}
970
Alex Lorenz2eacca82015-07-13 23:24:34 +0000971void MIParser::initNames2SubRegIndices() {
972 if (!Names2SubRegIndices.empty())
973 return;
974 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
975 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
976 Names2SubRegIndices.insert(
977 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
978}
979
980unsigned MIParser::getSubRegIndex(StringRef Name) {
981 initNames2SubRegIndices();
982 auto SubRegInfo = Names2SubRegIndices.find(Name);
983 if (SubRegInfo == Names2SubRegIndices.end())
984 return 0;
985 return SubRegInfo->getValue();
986}
987
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000988void MIParser::initSlots2BasicBlocks() {
989 if (!Slots2BasicBlocks.empty())
990 return;
991 const auto &F = *MF.getFunction();
992 ModuleSlotTracker MST(F.getParent());
993 MST.incorporateFunction(F);
994 for (auto &BB : F) {
995 if (BB.hasName())
996 continue;
997 int Slot = MST.getLocalSlot(&BB);
998 if (Slot == -1)
999 continue;
1000 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1001 }
1002}
1003
1004const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1005 initSlots2BasicBlocks();
1006 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1007 if (BlockInfo == Slots2BasicBlocks.end())
1008 return nullptr;
1009 return BlockInfo->second;
1010}
1011
Alex Lorenzef5c1962015-07-28 23:02:45 +00001012void MIParser::initNames2TargetIndices() {
1013 if (!Names2TargetIndices.empty())
1014 return;
1015 const auto *TII = MF.getSubtarget().getInstrInfo();
1016 assert(TII && "Expected target instruction info");
1017 auto Indices = TII->getSerializableTargetIndices();
1018 for (const auto &I : Indices)
1019 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1020}
1021
1022bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1023 initNames2TargetIndices();
1024 auto IndexInfo = Names2TargetIndices.find(Name);
1025 if (IndexInfo == Names2TargetIndices.end())
1026 return true;
1027 Index = IndexInfo->second;
1028 return false;
1029}
1030
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001031bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
1032 MachineFunction &MF, StringRef Src,
1033 const PerFunctionMIParsingState &PFS,
1034 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1035 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parse(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001036}
Alex Lorenzf09df002015-06-30 18:16:42 +00001037
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001038bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1039 MachineFunction &MF, StringRef Src,
1040 const PerFunctionMIParsingState &PFS,
1041 const SlotMapping &IRSlots, SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001042 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00001043}
Alex Lorenz9fab3702015-07-14 21:24:41 +00001044
1045bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1046 MachineFunction &MF, StringRef Src,
1047 const PerFunctionMIParsingState &PFS,
1048 const SlotMapping &IRSlots,
1049 SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001050 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1051 .parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00001052}
Alex Lorenz12045a42015-07-27 17:42:45 +00001053
1054bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1055 MachineFunction &MF, StringRef Src,
1056 const PerFunctionMIParsingState &PFS,
1057 const SlotMapping &IRSlots,
1058 SMDiagnostic &Error) {
1059 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1060 .parseStandaloneVirtualRegister(Reg);
1061}
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001062
1063bool llvm::parseIRBlockReference(const BasicBlock *&BB, SourceMgr &SM,
1064 MachineFunction &MF, StringRef Src,
1065 const PerFunctionMIParsingState &PFS,
1066 const SlotMapping &IRSlots,
1067 SMDiagnostic &Error) {
1068 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1069 .parseStandaloneIRBlockReference(BB);
1070}