blob: a5c049e1b43d2c2a16ad80e02b891af53a754b3f [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"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000021#include "llvm/CodeGen/MachineFunction.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"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000027#include "llvm/IR/Constants.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000028#include "llvm/IR/Instructions.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000029#include "llvm/IR/Intrinsics.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000030#include "llvm/IR/Module.h"
Alex Lorenz8a1915b2015-07-27 22:42:41 +000031#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000032#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000033#include "llvm/Support/SourceMgr.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000034#include "llvm/Support/raw_ostream.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000035#include "llvm/Target/TargetInstrInfo.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000036#include "llvm/Target/TargetIntrinsicInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000037#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000038
39using namespace llvm;
40
Matthias Braune35861d2016-07-13 23:27:50 +000041PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
42 SourceMgr &SM, const SlotMapping &IRSlots)
43 : MF(MF), SM(&SM), IRSlots(IRSlots) {
Matthias Braun83947862016-07-13 22:23:23 +000044}
45
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000046namespace {
47
Alex Lorenz36962cd2015-07-07 02:08:46 +000048/// A wrapper struct around the 'MachineOperand' struct that includes a source
Alex Lorenzfeb6b432015-08-19 19:19:16 +000049/// range and other attributes.
50struct ParsedMachineOperand {
Alex Lorenz36962cd2015-07-07 02:08:46 +000051 MachineOperand Operand;
52 StringRef::iterator Begin;
53 StringRef::iterator End;
Alex Lorenz5ef93b02015-08-19 19:05:34 +000054 Optional<unsigned> TiedDefIdx;
Alex Lorenz36962cd2015-07-07 02:08:46 +000055
Alex Lorenzfeb6b432015-08-19 19:19:16 +000056 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
57 StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
Alex Lorenz5ef93b02015-08-19 19:05:34 +000058 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
59 if (TiedDefIdx)
60 assert(Operand.isReg() && Operand.isUse() &&
61 "Only used register operands can be tied");
62 }
Alex Lorenz36962cd2015-07-07 02:08:46 +000063};
64
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000065class MIParser {
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000066 MachineFunction &MF;
67 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000068 StringRef Source, CurrentSource;
69 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000070 const PerFunctionMIParsingState &PFS;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000071 /// Maps from instruction names to op codes.
72 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000073 /// Maps from register names to registers.
74 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000075 /// Maps from register mask names to register masks.
76 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000077 /// Maps from subregister names to subregister indices.
78 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000079 /// Maps from slot numbers to function's unnamed basic blocks.
80 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzdd13be02015-08-19 23:31:05 +000081 /// Maps from slot numbers to function's unnamed values.
82 DenseMap<unsigned, const Value *> Slots2Values;
Alex Lorenzef5c1962015-07-28 23:02:45 +000083 /// Maps from target index names to target indices.
84 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +000085 /// Maps from direct target flag names to the direct target flag values.
86 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenzf3630112015-08-18 22:52:15 +000087 /// Maps from direct target flag names to the bitmask target flag values.
88 StringMap<unsigned> Names2BitmaskTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000089
90public:
Matthias Braune35861d2016-07-13 23:27:50 +000091 MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
92 StringRef Source);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000093
Quentin Colombet287c6bb2016-03-08 00:57:31 +000094 /// \p SkipChar gives the number of characters to skip before looking
95 /// for the next token.
96 void lex(unsigned SkipChar = 0);
Alex Lorenz91370c52015-06-22 20:37:46 +000097
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000098 /// Report an error at the current location with the given message.
99 ///
100 /// This function always return true.
101 bool error(const Twine &Msg);
102
Alex Lorenz91370c52015-06-22 20:37:46 +0000103 /// Report an error at the given location with the given message.
104 ///
105 /// This function always return true.
106 bool error(StringRef::iterator Loc, const Twine &Msg);
107
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000108 bool
109 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
110 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +0000111 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +0000112 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
113 bool parseStandaloneNamedRegister(unsigned &Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +0000114 bool parseStandaloneVirtualRegister(unsigned &Reg);
Alex Lorenza314d812015-08-18 22:26:26 +0000115 bool parseStandaloneStackObject(int &FI);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000116 bool parseStandaloneMDNode(MDNode *&Node);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000117
118 bool
119 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
120 bool parseBasicBlock(MachineBasicBlock &MBB);
121 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
122 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000123
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000124 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000125 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000126 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000127 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000128 bool parseSize(unsigned &Size);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000129 bool parseRegisterOperand(MachineOperand &Dest,
130 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000131 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000132 bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
133 const Constant *&C);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000134 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Ahmed Bougachad760de02016-07-28 17:15:12 +0000135 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
Alex Lorenz05e38822015-08-05 18:52:21 +0000136 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000137 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000138 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000139 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000140 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000141 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000142 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000143 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000144 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000145 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000146 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +0000147 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000148 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000149 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000150 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000151 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000152 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000153 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000154 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000155 bool parseIRBlock(BasicBlock *&BB, const Function &F);
156 bool parseBlockAddressOperand(MachineOperand &Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +0000157 bool parseIntrinsicOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000158 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000159 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000160 bool parseMachineOperand(MachineOperand &Dest,
161 Optional<unsigned> &TiedDefIdx);
162 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
163 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000164 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000165 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000166 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000167 bool parseIRValue(const Value *&V);
Justin Lebar0af80cd2016-07-15 18:26:59 +0000168 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000169 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
170 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000171 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000172
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000173private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000174 /// Convert the integer literal in the current token into an unsigned integer.
175 ///
176 /// Return true if an error occurred.
177 bool getUnsigned(unsigned &Result);
178
Alex Lorenz4af7e612015-08-03 23:08:19 +0000179 /// Convert the integer literal in the current token into an uint64.
180 ///
181 /// Return true if an error occurred.
182 bool getUint64(uint64_t &Result);
183
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000184 /// If the current token is of the given kind, consume it and return false.
185 /// Otherwise report an error and return true.
186 bool expectAndConsume(MIToken::TokenKind TokenKind);
187
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000188 /// If the current token is of the given kind, consume it and return true.
189 /// Otherwise return false.
190 bool consumeIfPresent(MIToken::TokenKind TokenKind);
191
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000192 void initNames2InstrOpCodes();
193
194 /// Try to convert an instruction name to an opcode. Return true if the
195 /// instruction name is invalid.
196 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000197
Alex Lorenze5a44662015-07-17 00:24:15 +0000198 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000199
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000200 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000201 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000202
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000203 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000204 const MCInstrDesc &MCID);
205
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000206 void initNames2Regs();
207
208 /// Try to convert a register name to a register number. Return true if the
209 /// register name is invalid.
210 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000211
212 void initNames2RegMasks();
213
214 /// Check if the given identifier is a name of a register mask.
215 ///
216 /// Return null if the identifier isn't a register mask.
217 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000218
219 void initNames2SubRegIndices();
220
221 /// Check if the given identifier is a name of a subregister index.
222 ///
223 /// Return 0 if the name isn't a subregister index class.
224 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000225
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000226 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000227 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000228
Alex Lorenzdd13be02015-08-19 23:31:05 +0000229 const Value *getIRValue(unsigned Slot);
230
Alex Lorenzef5c1962015-07-28 23:02:45 +0000231 void initNames2TargetIndices();
232
233 /// Try to convert a name of target index to the corresponding target index.
234 ///
235 /// Return true if the name isn't a name of a target index.
236 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000237
238 void initNames2DirectTargetFlags();
239
240 /// Try to convert a name of a direct target flag to the corresponding
241 /// target flag.
242 ///
243 /// Return true if the name isn't a name of a direct flag.
244 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000245
246 void initNames2BitmaskTargetFlags();
247
248 /// Try to convert a name of a bitmask target flag to the corresponding
249 /// target flag.
250 ///
251 /// Return true if the name isn't a name of a bitmask target flag.
252 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000253};
254
255} // end anonymous namespace
256
Matthias Braune35861d2016-07-13 23:27:50 +0000257MIParser::MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
258 StringRef Source)
259 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
260{}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000261
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000262void MIParser::lex(unsigned SkipChar) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000263 CurrentSource = lexMIToken(
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000264 CurrentSource.data() + SkipChar, Token,
Alex Lorenz91370c52015-06-22 20:37:46 +0000265 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
266}
267
268bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
269
270bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Matthias Braune35861d2016-07-13 23:27:50 +0000271 const SourceMgr &SM = *PFS.SM;
Alex Lorenz91370c52015-06-22 20:37:46 +0000272 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000273 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
274 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
275 // Create an ordinary diagnostic when the source manager's buffer is the
276 // source string.
277 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
278 return true;
279 }
280 // Create a diagnostic for a YAML string literal.
281 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
282 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
283 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000284 return true;
285}
286
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000287static const char *toString(MIToken::TokenKind TokenKind) {
288 switch (TokenKind) {
289 case MIToken::comma:
290 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000291 case MIToken::equal:
292 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000293 case MIToken::colon:
294 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000295 case MIToken::lparen:
296 return "'('";
297 case MIToken::rparen:
298 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000299 default:
300 return "<unknown token>";
301 }
302}
303
304bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
305 if (Token.isNot(TokenKind))
306 return error(Twine("expected ") + toString(TokenKind));
307 lex();
308 return false;
309}
310
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000311bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
312 if (Token.isNot(TokenKind))
313 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000314 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000315 return true;
316}
Alex Lorenz91370c52015-06-22 20:37:46 +0000317
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000318bool MIParser::parseBasicBlockDefinition(
319 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
320 assert(Token.is(MIToken::MachineBasicBlockLabel));
321 unsigned ID = 0;
322 if (getUnsigned(ID))
323 return true;
324 auto Loc = Token.location();
325 auto Name = Token.stringValue();
326 lex();
327 bool HasAddressTaken = false;
328 bool IsLandingPad = false;
329 unsigned Alignment = 0;
330 BasicBlock *BB = nullptr;
331 if (consumeIfPresent(MIToken::lparen)) {
332 do {
333 // TODO: Report an error when multiple same attributes are specified.
334 switch (Token.kind()) {
335 case MIToken::kw_address_taken:
336 HasAddressTaken = true;
337 lex();
338 break;
339 case MIToken::kw_landing_pad:
340 IsLandingPad = true;
341 lex();
342 break;
343 case MIToken::kw_align:
344 if (parseAlignment(Alignment))
345 return true;
346 break;
347 case MIToken::IRBlock:
348 // TODO: Report an error when both name and ir block are specified.
349 if (parseIRBlock(BB, *MF.getFunction()))
350 return true;
351 lex();
352 break;
353 default:
354 break;
355 }
356 } while (consumeIfPresent(MIToken::comma));
357 if (expectAndConsume(MIToken::rparen))
358 return true;
359 }
360 if (expectAndConsume(MIToken::colon))
361 return true;
362
363 if (!Name.empty()) {
364 BB = dyn_cast_or_null<BasicBlock>(
365 MF.getFunction()->getValueSymbolTable().lookup(Name));
366 if (!BB)
367 return error(Loc, Twine("basic block '") + Name +
368 "' is not defined in the function '" +
369 MF.getName() + "'");
370 }
371 auto *MBB = MF.CreateMachineBasicBlock(BB);
372 MF.insert(MF.end(), MBB);
373 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
374 if (!WasInserted)
375 return error(Loc, Twine("redefinition of machine basic block with id #") +
376 Twine(ID));
377 if (Alignment)
378 MBB->setAlignment(Alignment);
379 if (HasAddressTaken)
380 MBB->setHasAddressTaken();
Reid Kleckner0e288232015-08-27 23:27:47 +0000381 MBB->setIsEHPad(IsLandingPad);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000382 return false;
383}
384
385bool MIParser::parseBasicBlockDefinitions(
386 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
387 lex();
388 // Skip until the first machine basic block.
389 while (Token.is(MIToken::Newline))
390 lex();
391 if (Token.isErrorOrEOF())
392 return Token.isError();
393 if (Token.isNot(MIToken::MachineBasicBlockLabel))
394 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000395 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000396 do {
397 if (parseBasicBlockDefinition(MBBSlots))
398 return true;
399 bool IsAfterNewline = false;
400 // Skip until the next machine basic block.
401 while (true) {
402 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
403 Token.isErrorOrEOF())
404 break;
405 else if (Token.is(MIToken::MachineBasicBlockLabel))
406 return error("basic block definition should be located at the start of "
407 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000408 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000409 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000410 continue;
411 }
412 IsAfterNewline = false;
413 if (Token.is(MIToken::lbrace))
414 ++BraceDepth;
415 if (Token.is(MIToken::rbrace)) {
416 if (!BraceDepth)
417 return error("extraneous closing brace ('}')");
418 --BraceDepth;
419 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000420 lex();
421 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000422 // Verify that we closed all of the '{' at the end of a file or a block.
423 if (!Token.isError() && BraceDepth)
424 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000425 } while (!Token.isErrorOrEOF());
426 return Token.isError();
427}
428
429bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
430 assert(Token.is(MIToken::kw_liveins));
431 lex();
432 if (expectAndConsume(MIToken::colon))
433 return true;
434 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
435 return false;
436 do {
437 if (Token.isNot(MIToken::NamedRegister))
438 return error("expected a named register");
439 unsigned Reg = 0;
440 if (parseRegister(Reg))
441 return true;
442 MBB.addLiveIn(Reg);
443 lex();
444 } while (consumeIfPresent(MIToken::comma));
445 return false;
446}
447
448bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
449 assert(Token.is(MIToken::kw_successors));
450 lex();
451 if (expectAndConsume(MIToken::colon))
452 return true;
453 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
454 return false;
455 do {
456 if (Token.isNot(MIToken::MachineBasicBlock))
457 return error("expected a machine basic block reference");
458 MachineBasicBlock *SuccMBB = nullptr;
459 if (parseMBBReference(SuccMBB))
460 return true;
461 lex();
462 unsigned Weight = 0;
463 if (consumeIfPresent(MIToken::lparen)) {
464 if (Token.isNot(MIToken::IntegerLiteral))
465 return error("expected an integer literal after '('");
466 if (getUnsigned(Weight))
467 return true;
468 lex();
469 if (expectAndConsume(MIToken::rparen))
470 return true;
471 }
Cong Houd97c1002015-12-01 05:29:22 +0000472 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000473 } while (consumeIfPresent(MIToken::comma));
Cong Houd97c1002015-12-01 05:29:22 +0000474 MBB.normalizeSuccProbs();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000475 return false;
476}
477
478bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
479 // Skip the definition.
480 assert(Token.is(MIToken::MachineBasicBlockLabel));
481 lex();
482 if (consumeIfPresent(MIToken::lparen)) {
483 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
484 lex();
485 consumeIfPresent(MIToken::rparen);
486 }
487 consumeIfPresent(MIToken::colon);
488
489 // Parse the liveins and successors.
490 // N.B: Multiple lists of successors and liveins are allowed and they're
491 // merged into one.
492 // Example:
493 // liveins: %edi
494 // liveins: %esi
495 //
496 // is equivalent to
497 // liveins: %edi, %esi
498 while (true) {
499 if (Token.is(MIToken::kw_successors)) {
500 if (parseBasicBlockSuccessors(MBB))
501 return true;
502 } else if (Token.is(MIToken::kw_liveins)) {
503 if (parseBasicBlockLiveins(MBB))
504 return true;
505 } else if (consumeIfPresent(MIToken::Newline)) {
506 continue;
507 } else
508 break;
509 if (!Token.isNewlineOrEOF())
510 return error("expected line break at the end of a list");
511 lex();
512 }
513
514 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000515 bool IsInBundle = false;
516 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000517 while (true) {
518 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
519 return false;
520 else if (consumeIfPresent(MIToken::Newline))
521 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000522 if (consumeIfPresent(MIToken::rbrace)) {
523 // The first parsing pass should verify that all closing '}' have an
524 // opening '{'.
525 assert(IsInBundle);
526 IsInBundle = false;
527 continue;
528 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000529 MachineInstr *MI = nullptr;
530 if (parse(MI))
531 return true;
532 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000533 if (IsInBundle) {
534 PrevMI->setFlag(MachineInstr::BundledSucc);
535 MI->setFlag(MachineInstr::BundledPred);
536 }
537 PrevMI = MI;
538 if (Token.is(MIToken::lbrace)) {
539 if (IsInBundle)
540 return error("nested instruction bundles are not allowed");
541 lex();
542 // This instruction is the start of the bundle.
543 MI->setFlag(MachineInstr::BundledSucc);
544 IsInBundle = true;
545 if (!Token.is(MIToken::Newline))
546 // The next instruction can be on the same line.
547 continue;
548 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000549 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
550 lex();
551 }
552 return false;
553}
554
555bool MIParser::parseBasicBlocks() {
556 lex();
557 // Skip until the first machine basic block.
558 while (Token.is(MIToken::Newline))
559 lex();
560 if (Token.isErrorOrEOF())
561 return Token.isError();
562 // The first parsing pass should have verified that this token is a MBB label
563 // in the 'parseBasicBlockDefinitions' method.
564 assert(Token.is(MIToken::MachineBasicBlockLabel));
565 do {
566 MachineBasicBlock *MBB = nullptr;
567 if (parseMBBReference(MBB))
568 return true;
569 if (parseBasicBlock(*MBB))
570 return true;
571 // The method 'parseBasicBlock' should parse the whole block until the next
572 // block or the end of file.
573 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
574 } while (Token.isNot(MIToken::Eof));
575 return false;
576}
577
578bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000579 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000580 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000581 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000582 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000583 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000584 Optional<unsigned> TiedDefIdx;
585 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000586 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000587 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000588 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000589 if (Token.isNot(MIToken::comma))
590 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000591 lex();
592 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000593 if (!Operands.empty() && expectAndConsume(MIToken::equal))
594 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000595
Alex Lorenze5a44662015-07-17 00:24:15 +0000596 unsigned OpCode, Flags = 0;
597 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000598 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000599
Tim Northover98a56eb2016-07-22 22:13:36 +0000600 SmallVector<LLT, 1> Tys;
Quentin Colombet85199672016-03-08 00:20:48 +0000601 if (isPreISelGenericOpcode(OpCode)) {
Tim Northover98a56eb2016-07-22 22:13:36 +0000602 // For generic opcode, at least one type is mandatory.
Tim Northover26e40bd2016-07-26 17:28:01 +0000603 auto Loc = Token.location();
604 bool ManyTypes = Token.is(MIToken::lbrace);
605 if (ManyTypes)
606 lex();
607
608 // Now actually parse the type(s).
Tim Northover98a56eb2016-07-22 22:13:36 +0000609 do {
Tim Northover98a56eb2016-07-22 22:13:36 +0000610 Tys.resize(Tys.size() + 1);
611 if (parseLowLevelType(Loc, Tys[Tys.size() - 1]))
612 return true;
Tim Northover26e40bd2016-07-26 17:28:01 +0000613 } while (ManyTypes && consumeIfPresent(MIToken::comma));
614
615 if (ManyTypes)
616 expectAndConsume(MIToken::rbrace);
Quentin Colombet85199672016-03-08 00:20:48 +0000617 }
618
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000619 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000620 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000621 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000622 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000623 Optional<unsigned> TiedDefIdx;
624 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000625 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000626 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000627 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000628 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
629 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000630 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000631 if (Token.isNot(MIToken::comma))
632 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000633 lex();
634 }
635
Alex Lorenz46d760d2015-07-22 21:15:11 +0000636 DebugLoc DebugLocation;
637 if (Token.is(MIToken::kw_debug_location)) {
638 lex();
639 if (Token.isNot(MIToken::exclaim))
640 return error("expected a metadata node after 'debug-location'");
641 MDNode *Node = nullptr;
642 if (parseMDNode(Node))
643 return true;
644 DebugLocation = DebugLoc(Node);
645 }
646
Alex Lorenz4af7e612015-08-03 23:08:19 +0000647 // Parse the machine memory operands.
648 SmallVector<MachineMemOperand *, 2> MemOperands;
649 if (Token.is(MIToken::coloncolon)) {
650 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000651 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000652 MachineMemOperand *MemOp = nullptr;
653 if (parseMachineMemoryOperand(MemOp))
654 return true;
655 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000656 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000657 break;
658 if (Token.isNot(MIToken::comma))
659 return error("expected ',' before the next machine memory operand");
660 lex();
661 }
662 }
663
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000664 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000665 if (!MCID.isVariadic()) {
666 // FIXME: Move the implicit operand verification to the machine verifier.
667 if (verifyImplicitOperands(Operands, MCID))
668 return true;
669 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000670
Alex Lorenzcb268d42015-07-06 23:07:26 +0000671 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000672 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000673 MI->setFlags(Flags);
Tim Northover98a56eb2016-07-22 22:13:36 +0000674 if (Tys.size() > 0) {
675 for (unsigned i = 0; i < Tys.size(); ++i)
676 MI->setType(Tys[i], i);
677 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000678 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000679 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000680 if (assignRegisterTies(*MI, Operands))
681 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000682 if (MemOperands.empty())
683 return false;
684 MachineInstr::mmo_iterator MemRefs =
685 MF.allocateMemRefsArray(MemOperands.size());
686 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
687 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000688 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000689}
690
Alex Lorenz1ea60892015-07-27 20:29:27 +0000691bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000692 lex();
693 if (Token.isNot(MIToken::MachineBasicBlock))
694 return error("expected a machine basic block reference");
695 if (parseMBBReference(MBB))
696 return true;
697 lex();
698 if (Token.isNot(MIToken::Eof))
699 return error(
700 "expected end of string after the machine basic block reference");
701 return false;
702}
703
Alex Lorenz1ea60892015-07-27 20:29:27 +0000704bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000705 lex();
706 if (Token.isNot(MIToken::NamedRegister))
707 return error("expected a named register");
708 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000709 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000710 lex();
711 if (Token.isNot(MIToken::Eof))
712 return error("expected end of string after the register reference");
713 return false;
714}
715
Alex Lorenz12045a42015-07-27 17:42:45 +0000716bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
717 lex();
718 if (Token.isNot(MIToken::VirtualRegister))
719 return error("expected a virtual register");
720 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000721 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000722 lex();
723 if (Token.isNot(MIToken::Eof))
724 return error("expected end of string after the register reference");
725 return false;
726}
727
Alex Lorenza314d812015-08-18 22:26:26 +0000728bool MIParser::parseStandaloneStackObject(int &FI) {
729 lex();
730 if (Token.isNot(MIToken::StackObject))
731 return error("expected a stack object");
732 if (parseStackFrameIndex(FI))
733 return true;
734 if (Token.isNot(MIToken::Eof))
735 return error("expected end of string after the stack object reference");
736 return false;
737}
738
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000739bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
740 lex();
741 if (Token.isNot(MIToken::exclaim))
742 return error("expected a metadata node");
743 if (parseMDNode(Node))
744 return true;
745 if (Token.isNot(MIToken::Eof))
746 return error("expected end of string after the metadata node");
747 return false;
748}
749
Alex Lorenz36962cd2015-07-07 02:08:46 +0000750static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
751 assert(MO.isImplicit());
752 return MO.isDef() ? "implicit-def" : "implicit";
753}
754
755static std::string getRegisterName(const TargetRegisterInfo *TRI,
756 unsigned Reg) {
757 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
758 return StringRef(TRI->getName(Reg)).lower();
759}
760
Alex Lorenz0153e592015-09-10 14:04:34 +0000761/// Return true if the parsed machine operands contain a given machine operand.
762static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
763 ArrayRef<ParsedMachineOperand> Operands) {
764 for (const auto &I : Operands) {
765 if (ImplicitOperand.isIdenticalTo(I.Operand))
766 return true;
767 }
768 return false;
769}
770
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000771bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
772 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000773 if (MCID.isCall())
774 // We can't verify call instructions as they can contain arbitrary implicit
775 // register and register mask operands.
776 return false;
777
778 // Gather all the expected implicit operands.
779 SmallVector<MachineOperand, 4> ImplicitOperands;
780 if (MCID.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000781 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000782 ImplicitOperands.push_back(
783 MachineOperand::CreateReg(*ImpDefs, true, true));
784 if (MCID.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000785 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000786 ImplicitOperands.push_back(
787 MachineOperand::CreateReg(*ImpUses, false, true));
788
789 const auto *TRI = MF.getSubtarget().getRegisterInfo();
790 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000791 for (const auto &I : ImplicitOperands) {
792 if (isImplicitOperandIn(I, Operands))
793 continue;
794 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000795 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000796 printImplicitRegisterFlag(I) + " %" +
797 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000798 }
799 return false;
800}
801
Alex Lorenze5a44662015-07-17 00:24:15 +0000802bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
803 if (Token.is(MIToken::kw_frame_setup)) {
804 Flags |= MachineInstr::FrameSetup;
805 lex();
806 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000807 if (Token.isNot(MIToken::Identifier))
808 return error("expected a machine instruction");
809 StringRef InstrName = Token.stringValue();
810 if (parseInstrName(InstrName, OpCode))
811 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000812 lex();
813 return false;
814}
815
816bool MIParser::parseRegister(unsigned &Reg) {
817 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000818 case MIToken::underscore:
819 Reg = 0;
820 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000821 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000822 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000823 if (getRegisterByName(Name, Reg))
824 return error(Twine("unknown register name '") + Name + "'");
825 break;
826 }
Alex Lorenz53464512015-07-10 22:51:20 +0000827 case MIToken::VirtualRegister: {
828 unsigned ID;
829 if (getUnsigned(ID))
830 return true;
831 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
832 if (RegInfo == PFS.VirtualRegisterSlots.end())
833 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
834 "'");
835 Reg = RegInfo->second;
836 break;
837 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000838 // TODO: Parse other register kinds.
839 default:
840 llvm_unreachable("The current token should be a register");
841 }
842 return false;
843}
844
Alex Lorenzcb268d42015-07-06 23:07:26 +0000845bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000846 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000847 switch (Token.kind()) {
848 case MIToken::kw_implicit:
849 Flags |= RegState::Implicit;
850 break;
851 case MIToken::kw_implicit_define:
852 Flags |= RegState::ImplicitDefine;
853 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000854 case MIToken::kw_def:
855 Flags |= RegState::Define;
856 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000857 case MIToken::kw_dead:
858 Flags |= RegState::Dead;
859 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000860 case MIToken::kw_killed:
861 Flags |= RegState::Kill;
862 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000863 case MIToken::kw_undef:
864 Flags |= RegState::Undef;
865 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000866 case MIToken::kw_internal:
867 Flags |= RegState::InternalRead;
868 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000869 case MIToken::kw_early_clobber:
870 Flags |= RegState::EarlyClobber;
871 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000872 case MIToken::kw_debug_use:
873 Flags |= RegState::Debug;
874 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000875 default:
876 llvm_unreachable("The current token should be a register flag");
877 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000878 if (OldFlags == Flags)
879 // We know that the same flag is specified more than once when the flags
880 // weren't modified.
881 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000882 lex();
883 return false;
884}
885
Alex Lorenz2eacca82015-07-13 23:24:34 +0000886bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
Matthias Braun333e4682016-07-26 21:49:34 +0000887 assert(Token.is(MIToken::dot));
Alex Lorenz2eacca82015-07-13 23:24:34 +0000888 lex();
889 if (Token.isNot(MIToken::Identifier))
Matthias Braun333e4682016-07-26 21:49:34 +0000890 return error("expected a subregister index after '.'");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000891 auto Name = Token.stringValue();
892 SubReg = getSubRegIndex(Name);
893 if (!SubReg)
894 return error(Twine("use of unknown subregister index '") + Name + "'");
895 lex();
896 return false;
897}
898
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000899bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
900 if (!consumeIfPresent(MIToken::kw_tied_def))
901 return error("expected 'tied-def' after '('");
902 if (Token.isNot(MIToken::IntegerLiteral))
903 return error("expected an integer literal after 'tied-def'");
904 if (getUnsigned(TiedDefIdx))
905 return true;
906 lex();
907 if (expectAndConsume(MIToken::rparen))
908 return true;
909 return false;
910}
911
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000912bool MIParser::parseSize(unsigned &Size) {
913 if (Token.isNot(MIToken::IntegerLiteral))
914 return error("expected an integer literal for the size");
915 if (getUnsigned(Size))
916 return true;
917 lex();
918 if (expectAndConsume(MIToken::rparen))
919 return true;
920 return false;
921}
922
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000923bool MIParser::assignRegisterTies(MachineInstr &MI,
924 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000925 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
926 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
927 if (!Operands[I].TiedDefIdx)
928 continue;
929 // The parser ensures that this operand is a register use, so we just have
930 // to check the tied-def operand.
931 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
932 if (DefIdx >= E)
933 return error(Operands[I].Begin,
934 Twine("use of invalid tied-def operand index '" +
935 Twine(DefIdx) + "'; instruction has only ") +
936 Twine(E) + " operands");
937 const auto &DefOperand = Operands[DefIdx].Operand;
938 if (!DefOperand.isReg() || !DefOperand.isDef())
939 // FIXME: add note with the def operand.
940 return error(Operands[I].Begin,
941 Twine("use of invalid tied-def operand index '") +
942 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
943 " isn't a defined register");
944 // Check that the tied-def operand wasn't tied elsewhere.
945 for (const auto &TiedPair : TiedRegisterPairs) {
946 if (TiedPair.first == DefIdx)
947 return error(Operands[I].Begin,
948 Twine("the tied-def operand #") + Twine(DefIdx) +
949 " is already tied with another register operand");
950 }
951 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
952 }
953 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
954 // indices must be less than tied max.
955 for (const auto &TiedPair : TiedRegisterPairs)
956 MI.tieOperands(TiedPair.first, TiedPair.second);
957 return false;
958}
959
960bool MIParser::parseRegisterOperand(MachineOperand &Dest,
961 Optional<unsigned> &TiedDefIdx,
962 bool IsDef) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000963 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000964 unsigned Flags = IsDef ? RegState::Define : 0;
965 while (Token.isRegisterFlag()) {
966 if (parseRegisterFlag(Flags))
967 return true;
968 }
969 if (!Token.isRegister())
970 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000971 if (parseRegister(Reg))
972 return true;
973 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000974 unsigned SubReg = 0;
Matthias Braun333e4682016-07-26 21:49:34 +0000975 if (Token.is(MIToken::dot)) {
Alex Lorenz2eacca82015-07-13 23:24:34 +0000976 if (parseSubRegisterIndex(SubReg))
977 return true;
Matthias Braun5d00b322016-07-16 01:36:18 +0000978 if (!TargetRegisterInfo::isVirtualRegister(Reg))
979 return error("subregister index expects a virtual register");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000980 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000981 if ((Flags & RegState::Define) == 0) {
982 if (consumeIfPresent(MIToken::lparen)) {
983 unsigned Idx;
984 if (parseRegisterTiedDefIndex(Idx))
985 return true;
986 TiedDefIdx = Idx;
987 }
988 } else if (consumeIfPresent(MIToken::lparen)) {
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000989 MachineRegisterInfo &MRI = MF.getRegInfo();
990
Quentin Colombet2c646962016-06-08 23:27:46 +0000991 // Virtual registers may have a size with GlobalISel.
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000992 if (!TargetRegisterInfo::isVirtualRegister(Reg))
993 return error("unexpected size on physical register");
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000994 if (MRI.getRegClassOrRegBank(Reg).is<const TargetRegisterClass *>())
995 return error("unexpected size on non-generic virtual register");
996
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000997 unsigned Size;
998 if (parseSize(Size))
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000999 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001000
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001001 MRI.setSize(Reg, Size);
Quentin Colombet2c646962016-06-08 23:27:46 +00001002 } else if (PFS.GenericVRegs.count(Reg)) {
1003 // Generic virtual registers must have a size.
1004 // If we end up here this means the size hasn't been specified and
1005 // this is bad!
1006 return error("generic virtual registers must have a size");
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001007 }
Alex Lorenz495ad872015-07-08 21:23:34 +00001008 Dest = MachineOperand::CreateReg(
1009 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +00001010 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +00001011 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1012 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001013 return false;
1014}
1015
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001016bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1017 assert(Token.is(MIToken::IntegerLiteral));
1018 const APSInt &Int = Token.integerValue();
1019 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +00001020 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001021 Dest = MachineOperand::CreateImm(Int.getExtValue());
1022 lex();
1023 return false;
1024}
1025
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001026bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1027 const Constant *&C) {
1028 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001029 SMDiagnostic Err;
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001030 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
Matthias Braune35861d2016-07-13 23:27:50 +00001031 &PFS.IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001032 if (!C)
1033 return error(Loc + Err.getColumnNo(), Err.getMessage());
1034 return false;
1035}
1036
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001037bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1038 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1039 return true;
1040 lex();
1041 return false;
1042}
1043
Ahmed Bougachad760de02016-07-28 17:15:12 +00001044bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
Tim Northover62ae5682016-07-20 19:09:30 +00001045 if (Token.is(MIToken::Identifier) && Token.stringValue() == "unsized") {
Tim Northover62ae5682016-07-20 19:09:30 +00001046 lex();
1047 Ty = LLT::unsized();
1048 return false;
1049 } else if (Token.is(MIToken::ScalarType)) {
1050 Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1051 lex();
1052 return false;
Tim Northoverbd505462016-07-22 16:59:52 +00001053 } else if (Token.is(MIToken::PointerType)) {
1054 Ty = LLT::pointer(APSInt(Token.range().drop_front()).getZExtValue());
1055 lex();
1056 return false;
Tim Northover62ae5682016-07-20 19:09:30 +00001057 }
Quentin Colombet85199672016-03-08 00:20:48 +00001058
Tim Northover62ae5682016-07-20 19:09:30 +00001059 // Now we're looking for a vector.
1060 if (Token.isNot(MIToken::less))
Tim Northoverbd505462016-07-22 16:59:52 +00001061 return error(Loc,
1062 "expected unsized, pN, sN or <N x sM> for GlobalISel type");
1063
Tim Northover62ae5682016-07-20 19:09:30 +00001064 lex();
1065
1066 if (Token.isNot(MIToken::IntegerLiteral))
1067 return error(Loc, "expected <N x sM> for vctor type");
1068 uint64_t NumElements = Token.integerValue().getZExtValue();
1069 lex();
1070
1071 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1072 return error(Loc, "expected '<N x sM>' for vector type");
1073 lex();
1074
1075 if (Token.isNot(MIToken::ScalarType))
1076 return error(Loc, "expected '<N x sM>' for vector type");
1077 uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1078 lex();
1079
1080 if (Token.isNot(MIToken::greater))
1081 return error(Loc, "expected '<N x sM>' for vector type");
1082 lex();
1083
1084 Ty = LLT::vector(NumElements, ScalarSize);
Quentin Colombet85199672016-03-08 00:20:48 +00001085 return false;
1086}
1087
Alex Lorenz05e38822015-08-05 18:52:21 +00001088bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1089 assert(Token.is(MIToken::IntegerType));
1090 auto Loc = Token.location();
1091 lex();
1092 if (Token.isNot(MIToken::IntegerLiteral))
1093 return error("expected an integer literal");
1094 const Constant *C = nullptr;
1095 if (parseIRConstant(Loc, C))
1096 return true;
1097 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1098 return false;
1099}
1100
Alex Lorenzad156fb2015-07-31 20:49:21 +00001101bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1102 auto Loc = Token.location();
1103 lex();
1104 if (Token.isNot(MIToken::FloatingPointLiteral))
1105 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001106 const Constant *C = nullptr;
1107 if (parseIRConstant(Loc, C))
1108 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001109 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1110 return false;
1111}
1112
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001113bool MIParser::getUnsigned(unsigned &Result) {
1114 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1115 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1116 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1117 if (Val64 == Limit)
1118 return error("expected 32-bit integer (too large)");
1119 Result = Val64;
1120 return false;
1121}
1122
Alex Lorenzf09df002015-06-30 18:16:42 +00001123bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001124 assert(Token.is(MIToken::MachineBasicBlock) ||
1125 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001126 unsigned Number;
1127 if (getUnsigned(Number))
1128 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001129 auto MBBInfo = PFS.MBBSlots.find(Number);
1130 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001131 return error(Twine("use of undefined machine basic block #") +
1132 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001133 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001134 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1135 return error(Twine("the name of machine basic block #") + Twine(Number) +
1136 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001137 return false;
1138}
1139
1140bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1141 MachineBasicBlock *MBB;
1142 if (parseMBBReference(MBB))
1143 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001144 Dest = MachineOperand::CreateMBB(MBB);
1145 lex();
1146 return false;
1147}
1148
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001149bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001150 assert(Token.is(MIToken::StackObject));
1151 unsigned ID;
1152 if (getUnsigned(ID))
1153 return true;
1154 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1155 if (ObjectInfo == PFS.StackObjectSlots.end())
1156 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1157 "'");
1158 StringRef Name;
1159 if (const auto *Alloca =
Matthias Braun941a7052016-07-28 18:40:00 +00001160 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001161 Name = Alloca->getName();
1162 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1163 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1164 "' isn't '" + Token.stringValue() + "'");
1165 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001166 FI = ObjectInfo->second;
1167 return false;
1168}
1169
1170bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1171 int FI;
1172 if (parseStackFrameIndex(FI))
1173 return true;
1174 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001175 return false;
1176}
1177
Alex Lorenzea882122015-08-12 21:17:02 +00001178bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001179 assert(Token.is(MIToken::FixedStackObject));
1180 unsigned ID;
1181 if (getUnsigned(ID))
1182 return true;
1183 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1184 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1185 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1186 Twine(ID) + "'");
1187 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001188 FI = ObjectInfo->second;
1189 return false;
1190}
1191
1192bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1193 int FI;
1194 if (parseFixedStackFrameIndex(FI))
1195 return true;
1196 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001197 return false;
1198}
1199
Alex Lorenz41df7d32015-07-28 17:09:52 +00001200bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001201 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001202 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001203 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001204 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001205 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001206 return error(Twine("use of undefined global value '") + Token.range() +
1207 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001208 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001209 }
1210 case MIToken::GlobalValue: {
1211 unsigned GVIdx;
1212 if (getUnsigned(GVIdx))
1213 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001214 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001215 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1216 "'");
Matthias Braune35861d2016-07-13 23:27:50 +00001217 GV = PFS.IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001218 break;
1219 }
1220 default:
1221 llvm_unreachable("The current token should be a global value");
1222 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001223 return false;
1224}
1225
1226bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1227 GlobalValue *GV = nullptr;
1228 if (parseGlobalValue(GV))
1229 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001230 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001231 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001232 if (parseOperandsOffset(Dest))
1233 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001234 return false;
1235}
1236
Alex Lorenzab980492015-07-20 20:51:18 +00001237bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1238 assert(Token.is(MIToken::ConstantPoolItem));
1239 unsigned ID;
1240 if (getUnsigned(ID))
1241 return true;
1242 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1243 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1244 return error("use of undefined constant '%const." + Twine(ID) + "'");
1245 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001246 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001247 if (parseOperandsOffset(Dest))
1248 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001249 return false;
1250}
1251
Alex Lorenz31d70682015-07-15 23:38:35 +00001252bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1253 assert(Token.is(MIToken::JumpTableIndex));
1254 unsigned ID;
1255 if (getUnsigned(ID))
1256 return true;
1257 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1258 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1259 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1260 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001261 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1262 return false;
1263}
1264
Alex Lorenz6ede3742015-07-21 16:59:53 +00001265bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001266 assert(Token.is(MIToken::ExternalSymbol));
1267 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001268 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001269 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001270 if (parseOperandsOffset(Dest))
1271 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001272 return false;
1273}
1274
Matthias Braunb74eb412016-03-28 18:18:46 +00001275bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1276 assert(Token.is(MIToken::SubRegisterIndex));
1277 StringRef Name = Token.stringValue();
1278 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1279 if (SubRegIndex == 0)
1280 return error(Twine("unknown subregister index '") + Name + "'");
1281 lex();
1282 Dest = MachineOperand::CreateImm(SubRegIndex);
1283 return false;
1284}
1285
Alex Lorenz44f29252015-07-22 21:07:04 +00001286bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001287 assert(Token.is(MIToken::exclaim));
1288 auto Loc = Token.location();
1289 lex();
1290 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1291 return error("expected metadata id after '!'");
1292 unsigned ID;
1293 if (getUnsigned(ID))
1294 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001295 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1296 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
Alex Lorenz35e44462015-07-22 17:58:46 +00001297 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1298 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001299 Node = NodeInfo->second.get();
1300 return false;
1301}
1302
1303bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1304 MDNode *Node = nullptr;
1305 if (parseMDNode(Node))
1306 return true;
1307 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001308 return false;
1309}
1310
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001311bool MIParser::parseCFIOffset(int &Offset) {
1312 if (Token.isNot(MIToken::IntegerLiteral))
1313 return error("expected a cfi offset");
1314 if (Token.integerValue().getMinSignedBits() > 32)
1315 return error("expected a 32 bit integer (the cfi offset is too large)");
1316 Offset = (int)Token.integerValue().getExtValue();
1317 lex();
1318 return false;
1319}
1320
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001321bool MIParser::parseCFIRegister(unsigned &Reg) {
1322 if (Token.isNot(MIToken::NamedRegister))
1323 return error("expected a cfi register");
1324 unsigned LLVMReg;
1325 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001326 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001327 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1328 assert(TRI && "Expected target register info");
1329 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1330 if (DwarfReg < 0)
1331 return error("invalid DWARF register");
1332 Reg = (unsigned)DwarfReg;
1333 lex();
1334 return false;
1335}
1336
1337bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1338 auto Kind = Token.kind();
1339 lex();
1340 auto &MMI = MF.getMMI();
1341 int Offset;
1342 unsigned Reg;
1343 unsigned CFIIndex;
1344 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001345 case MIToken::kw_cfi_same_value:
1346 if (parseCFIRegister(Reg))
1347 return true;
1348 CFIIndex =
1349 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1350 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001351 case MIToken::kw_cfi_offset:
1352 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1353 parseCFIOffset(Offset))
1354 return true;
1355 CFIIndex =
1356 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1357 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001358 case MIToken::kw_cfi_def_cfa_register:
1359 if (parseCFIRegister(Reg))
1360 return true;
1361 CFIIndex =
1362 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1363 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001364 case MIToken::kw_cfi_def_cfa_offset:
1365 if (parseCFIOffset(Offset))
1366 return true;
1367 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1368 CFIIndex = MMI.addFrameInst(
1369 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1370 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001371 case MIToken::kw_cfi_def_cfa:
1372 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1373 parseCFIOffset(Offset))
1374 return true;
1375 // NB: MCCFIInstruction::createDefCfa negates the offset.
1376 CFIIndex =
1377 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1378 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001379 default:
1380 // TODO: Parse the other CFI operands.
1381 llvm_unreachable("The current token should be a cfi operand");
1382 }
1383 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001384 return false;
1385}
1386
Alex Lorenzdeb53492015-07-28 17:28:03 +00001387bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1388 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001389 case MIToken::NamedIRBlock: {
1390 BB = dyn_cast_or_null<BasicBlock>(
1391 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001392 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001393 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001394 break;
1395 }
1396 case MIToken::IRBlock: {
1397 unsigned SlotNumber = 0;
1398 if (getUnsigned(SlotNumber))
1399 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001400 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001401 if (!BB)
1402 return error(Twine("use of undefined IR block '%ir-block.") +
1403 Twine(SlotNumber) + "'");
1404 break;
1405 }
1406 default:
1407 llvm_unreachable("The current token should be an IR block reference");
1408 }
1409 return false;
1410}
1411
1412bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1413 assert(Token.is(MIToken::kw_blockaddress));
1414 lex();
1415 if (expectAndConsume(MIToken::lparen))
1416 return true;
1417 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001418 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001419 return error("expected a global value");
1420 GlobalValue *GV = nullptr;
1421 if (parseGlobalValue(GV))
1422 return true;
1423 auto *F = dyn_cast<Function>(GV);
1424 if (!F)
1425 return error("expected an IR function reference");
1426 lex();
1427 if (expectAndConsume(MIToken::comma))
1428 return true;
1429 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001430 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001431 return error("expected an IR block reference");
1432 if (parseIRBlock(BB, *F))
1433 return true;
1434 lex();
1435 if (expectAndConsume(MIToken::rparen))
1436 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001437 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001438 if (parseOperandsOffset(Dest))
1439 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001440 return false;
1441}
1442
Tim Northover6b3bd612016-07-29 20:32:59 +00001443bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
1444 assert(Token.is(MIToken::kw_intrinsic));
1445 lex();
1446 if (expectAndConsume(MIToken::lparen))
1447 return error("expected syntax intrinsic(@llvm.whatever)");
1448
1449 if (Token.isNot(MIToken::NamedGlobalValue))
1450 return error("expected syntax intrinsic(@llvm.whatever)");
1451
1452 std::string Name = Token.stringValue();
1453 lex();
1454
1455 if (expectAndConsume(MIToken::rparen))
1456 return error("expected ')' to terminate intrinsic name");
1457
1458 // Find out what intrinsic we're dealing with, first try the global namespace
1459 // and then the target's private intrinsics if that fails.
1460 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1461 Intrinsic::ID ID = Function::lookupIntrinsicID(Name);
1462 if (ID == Intrinsic::not_intrinsic && TII)
1463 ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
1464
1465 if (ID == Intrinsic::not_intrinsic)
1466 return error("unknown intrinsic name");
1467 Dest = MachineOperand::CreateIntrinsicID(ID);
1468
1469 return false;
1470}
1471
Alex Lorenzef5c1962015-07-28 23:02:45 +00001472bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1473 assert(Token.is(MIToken::kw_target_index));
1474 lex();
1475 if (expectAndConsume(MIToken::lparen))
1476 return true;
1477 if (Token.isNot(MIToken::Identifier))
1478 return error("expected the name of the target index");
1479 int Index = 0;
1480 if (getTargetIndex(Token.stringValue(), Index))
1481 return error("use of undefined target index '" + Token.stringValue() + "'");
1482 lex();
1483 if (expectAndConsume(MIToken::rparen))
1484 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001485 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001486 if (parseOperandsOffset(Dest))
1487 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001488 return false;
1489}
1490
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001491bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1492 assert(Token.is(MIToken::kw_liveout));
1493 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1494 assert(TRI && "Expected target register info");
1495 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1496 lex();
1497 if (expectAndConsume(MIToken::lparen))
1498 return true;
1499 while (true) {
1500 if (Token.isNot(MIToken::NamedRegister))
1501 return error("expected a named register");
1502 unsigned Reg = 0;
1503 if (parseRegister(Reg))
1504 return true;
1505 lex();
1506 Mask[Reg / 32] |= 1U << (Reg % 32);
1507 // TODO: Report an error if the same register is used more than once.
1508 if (Token.isNot(MIToken::comma))
1509 break;
1510 lex();
1511 }
1512 if (expectAndConsume(MIToken::rparen))
1513 return true;
1514 Dest = MachineOperand::CreateRegLiveOut(Mask);
1515 return false;
1516}
1517
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001518bool MIParser::parseMachineOperand(MachineOperand &Dest,
1519 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001520 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001521 case MIToken::kw_implicit:
1522 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001523 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001524 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001525 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001526 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001527 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001528 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001529 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001530 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001531 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001532 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001533 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001534 case MIToken::IntegerLiteral:
1535 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001536 case MIToken::IntegerType:
1537 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001538 case MIToken::kw_half:
1539 case MIToken::kw_float:
1540 case MIToken::kw_double:
1541 case MIToken::kw_x86_fp80:
1542 case MIToken::kw_fp128:
1543 case MIToken::kw_ppc_fp128:
1544 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001545 case MIToken::MachineBasicBlock:
1546 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001547 case MIToken::StackObject:
1548 return parseStackObjectOperand(Dest);
1549 case MIToken::FixedStackObject:
1550 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001551 case MIToken::GlobalValue:
1552 case MIToken::NamedGlobalValue:
1553 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001554 case MIToken::ConstantPoolItem:
1555 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001556 case MIToken::JumpTableIndex:
1557 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001558 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001559 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00001560 case MIToken::SubRegisterIndex:
1561 return parseSubRegisterIndexOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001562 case MIToken::exclaim:
1563 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001564 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001565 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001566 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001567 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001568 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001569 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001570 case MIToken::kw_blockaddress:
1571 return parseBlockAddressOperand(Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +00001572 case MIToken::kw_intrinsic:
1573 return parseIntrinsicOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001574 case MIToken::kw_target_index:
1575 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001576 case MIToken::kw_liveout:
1577 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001578 case MIToken::Error:
1579 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001580 case MIToken::Identifier:
1581 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1582 Dest = MachineOperand::CreateRegMask(RegMask);
1583 lex();
1584 break;
1585 }
1586 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001587 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001588 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001589 return error("expected a machine operand");
1590 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001591 return false;
1592}
1593
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001594bool MIParser::parseMachineOperandAndTargetFlags(
1595 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001596 unsigned TF = 0;
1597 bool HasTargetFlags = false;
1598 if (Token.is(MIToken::kw_target_flags)) {
1599 HasTargetFlags = true;
1600 lex();
1601 if (expectAndConsume(MIToken::lparen))
1602 return true;
1603 if (Token.isNot(MIToken::Identifier))
1604 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001605 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1606 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1607 return error("use of undefined target flag '" + Token.stringValue() +
1608 "'");
1609 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001610 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001611 while (Token.is(MIToken::comma)) {
1612 lex();
1613 if (Token.isNot(MIToken::Identifier))
1614 return error("expected the name of the target flag");
1615 unsigned BitFlag = 0;
1616 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1617 return error("use of undefined target flag '" + Token.stringValue() +
1618 "'");
1619 // TODO: Report an error when using a duplicate bit target flag.
1620 TF |= BitFlag;
1621 lex();
1622 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001623 if (expectAndConsume(MIToken::rparen))
1624 return true;
1625 }
1626 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001627 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001628 return true;
1629 if (!HasTargetFlags)
1630 return false;
1631 if (Dest.isReg())
1632 return error(Loc, "register operands can't have target flags");
1633 Dest.setTargetFlags(TF);
1634 return false;
1635}
1636
Alex Lorenzdc24c172015-08-07 20:21:00 +00001637bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001638 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1639 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001640 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001641 bool IsNegative = Token.is(MIToken::minus);
1642 lex();
1643 if (Token.isNot(MIToken::IntegerLiteral))
1644 return error("expected an integer literal after '" + Sign + "'");
1645 if (Token.integerValue().getMinSignedBits() > 64)
1646 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001647 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001648 if (IsNegative)
1649 Offset = -Offset;
1650 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001651 return false;
1652}
1653
Alex Lorenz620f8912015-08-13 20:33:33 +00001654bool MIParser::parseAlignment(unsigned &Alignment) {
1655 assert(Token.is(MIToken::kw_align));
1656 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001657 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001658 return error("expected an integer literal after 'align'");
1659 if (getUnsigned(Alignment))
1660 return true;
1661 lex();
1662 return false;
1663}
1664
Alex Lorenzdc24c172015-08-07 20:21:00 +00001665bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1666 int64_t Offset = 0;
1667 if (parseOffset(Offset))
1668 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001669 Op.setOffset(Offset);
1670 return false;
1671}
1672
Alex Lorenz36593ac2015-08-19 23:27:07 +00001673bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001674 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001675 case MIToken::NamedIRValue: {
1676 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001677 break;
1678 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001679 case MIToken::IRValue: {
1680 unsigned SlotNumber = 0;
1681 if (getUnsigned(SlotNumber))
1682 return true;
1683 V = getIRValue(SlotNumber);
1684 break;
1685 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001686 case MIToken::NamedGlobalValue:
1687 case MIToken::GlobalValue: {
1688 GlobalValue *GV = nullptr;
1689 if (parseGlobalValue(GV))
1690 return true;
1691 V = GV;
1692 break;
1693 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001694 case MIToken::QuotedIRValue: {
1695 const Constant *C = nullptr;
1696 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1697 return true;
1698 V = C;
1699 break;
1700 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001701 default:
1702 llvm_unreachable("The current token should be an IR block reference");
1703 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001704 if (!V)
1705 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001706 return false;
1707}
1708
1709bool MIParser::getUint64(uint64_t &Result) {
1710 assert(Token.hasIntegerValue());
1711 if (Token.integerValue().getActiveBits() > 64)
1712 return error("expected 64-bit integer (too large)");
1713 Result = Token.integerValue().getZExtValue();
1714 return false;
1715}
1716
Justin Lebar0af80cd2016-07-15 18:26:59 +00001717bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
1718 const auto OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001719 switch (Token.kind()) {
1720 case MIToken::kw_volatile:
1721 Flags |= MachineMemOperand::MOVolatile;
1722 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001723 case MIToken::kw_non_temporal:
1724 Flags |= MachineMemOperand::MONonTemporal;
1725 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001726 case MIToken::kw_invariant:
1727 Flags |= MachineMemOperand::MOInvariant;
1728 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001729 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001730 default:
1731 llvm_unreachable("The current token should be a memory operand flag");
1732 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001733 if (OldFlags == Flags)
1734 // We know that the same flag is specified more than once when the flags
1735 // weren't modified.
1736 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001737 lex();
1738 return false;
1739}
1740
Alex Lorenz91097a32015-08-12 20:33:26 +00001741bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1742 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001743 case MIToken::kw_stack:
1744 PSV = MF.getPSVManager().getStack();
1745 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001746 case MIToken::kw_got:
1747 PSV = MF.getPSVManager().getGOT();
1748 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001749 case MIToken::kw_jump_table:
1750 PSV = MF.getPSVManager().getJumpTable();
1751 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001752 case MIToken::kw_constant_pool:
1753 PSV = MF.getPSVManager().getConstantPool();
1754 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001755 case MIToken::FixedStackObject: {
1756 int FI;
1757 if (parseFixedStackFrameIndex(FI))
1758 return true;
1759 PSV = MF.getPSVManager().getFixedStack(FI);
1760 // The token was already consumed, so use return here instead of break.
1761 return false;
1762 }
Matthias Braun3ef7df92016-06-08 00:47:07 +00001763 case MIToken::StackObject: {
1764 int FI;
1765 if (parseStackFrameIndex(FI))
1766 return true;
1767 PSV = MF.getPSVManager().getFixedStack(FI);
1768 // The token was already consumed, so use return here instead of break.
1769 return false;
1770 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001771 case MIToken::kw_call_entry: {
1772 lex();
1773 switch (Token.kind()) {
1774 case MIToken::GlobalValue:
1775 case MIToken::NamedGlobalValue: {
1776 GlobalValue *GV = nullptr;
1777 if (parseGlobalValue(GV))
1778 return true;
1779 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1780 break;
1781 }
1782 case MIToken::ExternalSymbol:
1783 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1784 MF.createExternalSymbolName(Token.stringValue()));
1785 break;
1786 default:
1787 return error(
1788 "expected a global value or an external symbol after 'call-entry'");
1789 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001790 break;
1791 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001792 default:
1793 llvm_unreachable("The current token should be pseudo source value");
1794 }
1795 lex();
1796 return false;
1797}
1798
1799bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001800 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001801 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Matthias Braun3ef7df92016-06-08 00:47:07 +00001802 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
1803 Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001804 const PseudoSourceValue *PSV = nullptr;
1805 if (parseMemoryPseudoSourceValue(PSV))
1806 return true;
1807 int64_t Offset = 0;
1808 if (parseOffset(Offset))
1809 return true;
1810 Dest = MachinePointerInfo(PSV, Offset);
1811 return false;
1812 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001813 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1814 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001815 Token.isNot(MIToken::NamedGlobalValue) &&
1816 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001817 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001818 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001819 if (parseIRValue(V))
1820 return true;
1821 if (!V->getType()->isPointerTy())
1822 return error("expected a pointer IR value");
1823 lex();
1824 int64_t Offset = 0;
1825 if (parseOffset(Offset))
1826 return true;
1827 Dest = MachinePointerInfo(V, Offset);
1828 return false;
1829}
1830
Alex Lorenz4af7e612015-08-03 23:08:19 +00001831bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1832 if (expectAndConsume(MIToken::lparen))
1833 return true;
Justin Lebar0af80cd2016-07-15 18:26:59 +00001834 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
Alex Lorenza518b792015-08-04 00:24:45 +00001835 while (Token.isMemoryOperandFlag()) {
1836 if (parseMemoryOperandFlag(Flags))
1837 return true;
1838 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001839 if (Token.isNot(MIToken::Identifier) ||
1840 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1841 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001842 if (Token.stringValue() == "load")
1843 Flags |= MachineMemOperand::MOLoad;
1844 else
1845 Flags |= MachineMemOperand::MOStore;
1846 lex();
1847
1848 if (Token.isNot(MIToken::IntegerLiteral))
1849 return error("expected the size integer literal after memory operation");
1850 uint64_t Size;
1851 if (getUint64(Size))
1852 return true;
1853 lex();
1854
Alex Lorenz91097a32015-08-12 20:33:26 +00001855 MachinePointerInfo Ptr = MachinePointerInfo();
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001856 if (Token.is(MIToken::Identifier)) {
1857 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1858 if (Token.stringValue() != Word)
1859 return error(Twine("expected '") + Word + "'");
1860 lex();
1861
1862 if (parseMachinePointerInfo(Ptr))
1863 return true;
1864 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001865 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001866 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001867 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001868 while (consumeIfPresent(MIToken::comma)) {
1869 switch (Token.kind()) {
1870 case MIToken::kw_align:
1871 if (parseAlignment(BaseAlignment))
1872 return true;
1873 break;
1874 case MIToken::md_tbaa:
1875 lex();
1876 if (parseMDNode(AAInfo.TBAA))
1877 return true;
1878 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001879 case MIToken::md_alias_scope:
1880 lex();
1881 if (parseMDNode(AAInfo.Scope))
1882 return true;
1883 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001884 case MIToken::md_noalias:
1885 lex();
1886 if (parseMDNode(AAInfo.NoAlias))
1887 return true;
1888 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001889 case MIToken::md_range:
1890 lex();
1891 if (parseMDNode(Range))
1892 return true;
1893 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001894 // TODO: Report an error on duplicate metadata nodes.
1895 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001896 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1897 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001898 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001899 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001900 if (expectAndConsume(MIToken::rparen))
1901 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001902 Dest =
1903 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001904 return false;
1905}
1906
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001907void MIParser::initNames2InstrOpCodes() {
1908 if (!Names2InstrOpCodes.empty())
1909 return;
1910 const auto *TII = MF.getSubtarget().getInstrInfo();
1911 assert(TII && "Expected target instruction info");
1912 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1913 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1914}
1915
1916bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1917 initNames2InstrOpCodes();
1918 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1919 if (InstrInfo == Names2InstrOpCodes.end())
1920 return true;
1921 OpCode = InstrInfo->getValue();
1922 return false;
1923}
1924
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001925void MIParser::initNames2Regs() {
1926 if (!Names2Regs.empty())
1927 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001928 // The '%noreg' register is the register 0.
1929 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001930 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1931 assert(TRI && "Expected target register info");
1932 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1933 bool WasInserted =
1934 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1935 .second;
1936 (void)WasInserted;
1937 assert(WasInserted && "Expected registers to be unique case-insensitively");
1938 }
1939}
1940
1941bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1942 initNames2Regs();
1943 auto RegInfo = Names2Regs.find(RegName);
1944 if (RegInfo == Names2Regs.end())
1945 return true;
1946 Reg = RegInfo->getValue();
1947 return false;
1948}
1949
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001950void MIParser::initNames2RegMasks() {
1951 if (!Names2RegMasks.empty())
1952 return;
1953 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1954 assert(TRI && "Expected target register info");
1955 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1956 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1957 assert(RegMasks.size() == RegMaskNames.size());
1958 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1959 Names2RegMasks.insert(
1960 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1961}
1962
1963const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1964 initNames2RegMasks();
1965 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1966 if (RegMaskInfo == Names2RegMasks.end())
1967 return nullptr;
1968 return RegMaskInfo->getValue();
1969}
1970
Alex Lorenz2eacca82015-07-13 23:24:34 +00001971void MIParser::initNames2SubRegIndices() {
1972 if (!Names2SubRegIndices.empty())
1973 return;
1974 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1975 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1976 Names2SubRegIndices.insert(
1977 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1978}
1979
1980unsigned MIParser::getSubRegIndex(StringRef Name) {
1981 initNames2SubRegIndices();
1982 auto SubRegInfo = Names2SubRegIndices.find(Name);
1983 if (SubRegInfo == Names2SubRegIndices.end())
1984 return 0;
1985 return SubRegInfo->getValue();
1986}
1987
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001988static void initSlots2BasicBlocks(
1989 const Function &F,
1990 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1991 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001992 MST.incorporateFunction(F);
1993 for (auto &BB : F) {
1994 if (BB.hasName())
1995 continue;
1996 int Slot = MST.getLocalSlot(&BB);
1997 if (Slot == -1)
1998 continue;
1999 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2000 }
2001}
2002
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002003static const BasicBlock *getIRBlockFromSlot(
2004 unsigned Slot,
2005 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002006 auto BlockInfo = Slots2BasicBlocks.find(Slot);
2007 if (BlockInfo == Slots2BasicBlocks.end())
2008 return nullptr;
2009 return BlockInfo->second;
2010}
2011
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002012const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2013 if (Slots2BasicBlocks.empty())
2014 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
2015 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2016}
2017
2018const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2019 if (&F == MF.getFunction())
2020 return getIRBlock(Slot);
2021 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2022 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2023 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2024}
2025
Alex Lorenzdd13be02015-08-19 23:31:05 +00002026static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2027 DenseMap<unsigned, const Value *> &Slots2Values) {
2028 int Slot = MST.getLocalSlot(V);
2029 if (Slot == -1)
2030 return;
2031 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2032}
2033
2034/// Creates the mapping from slot numbers to function's unnamed IR values.
2035static void initSlots2Values(const Function &F,
2036 DenseMap<unsigned, const Value *> &Slots2Values) {
2037 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2038 MST.incorporateFunction(F);
2039 for (const auto &Arg : F.args())
2040 mapValueToSlot(&Arg, MST, Slots2Values);
2041 for (const auto &BB : F) {
2042 mapValueToSlot(&BB, MST, Slots2Values);
2043 for (const auto &I : BB)
2044 mapValueToSlot(&I, MST, Slots2Values);
2045 }
2046}
2047
2048const Value *MIParser::getIRValue(unsigned Slot) {
2049 if (Slots2Values.empty())
2050 initSlots2Values(*MF.getFunction(), Slots2Values);
2051 auto ValueInfo = Slots2Values.find(Slot);
2052 if (ValueInfo == Slots2Values.end())
2053 return nullptr;
2054 return ValueInfo->second;
2055}
2056
Alex Lorenzef5c1962015-07-28 23:02:45 +00002057void MIParser::initNames2TargetIndices() {
2058 if (!Names2TargetIndices.empty())
2059 return;
2060 const auto *TII = MF.getSubtarget().getInstrInfo();
2061 assert(TII && "Expected target instruction info");
2062 auto Indices = TII->getSerializableTargetIndices();
2063 for (const auto &I : Indices)
2064 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2065}
2066
2067bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2068 initNames2TargetIndices();
2069 auto IndexInfo = Names2TargetIndices.find(Name);
2070 if (IndexInfo == Names2TargetIndices.end())
2071 return true;
2072 Index = IndexInfo->second;
2073 return false;
2074}
2075
Alex Lorenz49873a82015-08-06 00:44:07 +00002076void MIParser::initNames2DirectTargetFlags() {
2077 if (!Names2DirectTargetFlags.empty())
2078 return;
2079 const auto *TII = MF.getSubtarget().getInstrInfo();
2080 assert(TII && "Expected target instruction info");
2081 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2082 for (const auto &I : Flags)
2083 Names2DirectTargetFlags.insert(
2084 std::make_pair(StringRef(I.second), I.first));
2085}
2086
2087bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2088 initNames2DirectTargetFlags();
2089 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2090 if (FlagInfo == Names2DirectTargetFlags.end())
2091 return true;
2092 Flag = FlagInfo->second;
2093 return false;
2094}
2095
Alex Lorenzf3630112015-08-18 22:52:15 +00002096void MIParser::initNames2BitmaskTargetFlags() {
2097 if (!Names2BitmaskTargetFlags.empty())
2098 return;
2099 const auto *TII = MF.getSubtarget().getInstrInfo();
2100 assert(TII && "Expected target instruction info");
2101 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2102 for (const auto &I : Flags)
2103 Names2BitmaskTargetFlags.insert(
2104 std::make_pair(StringRef(I.second), I.first));
2105}
2106
2107bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2108 initNames2BitmaskTargetFlags();
2109 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2110 if (FlagInfo == Names2BitmaskTargetFlags.end())
2111 return true;
2112 Flag = FlagInfo->second;
2113 return false;
2114}
2115
Matthias Braun83947862016-07-13 22:23:23 +00002116bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2117 StringRef Src,
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002118 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002119 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002120}
2121
Matthias Braun83947862016-07-13 22:23:23 +00002122bool llvm::parseMachineInstructions(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002123 StringRef Src, SMDiagnostic &Error) {
2124 return MIParser(PFS, Error, Src).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002125}
Alex Lorenzf09df002015-06-30 18:16:42 +00002126
Matthias Braun83947862016-07-13 22:23:23 +00002127bool llvm::parseMBBReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002128 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +00002129 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002130 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002131}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002132
Matthias Braun83947862016-07-13 22:23:23 +00002133bool llvm::parseNamedRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002134 unsigned &Reg, StringRef Src,
Alex Lorenz9fab3702015-07-14 21:24:41 +00002135 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002136 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002137}
Alex Lorenz12045a42015-07-27 17:42:45 +00002138
Matthias Braun83947862016-07-13 22:23:23 +00002139bool llvm::parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002140 unsigned &Reg, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +00002141 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002142 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +00002143}
Alex Lorenza314d812015-08-18 22:26:26 +00002144
Matthias Braun83947862016-07-13 22:23:23 +00002145bool llvm::parseStackObjectReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002146 int &FI, StringRef Src,
Alex Lorenza314d812015-08-18 22:26:26 +00002147 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002148 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
Alex Lorenza314d812015-08-18 22:26:26 +00002149}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002150
Matthias Braun83947862016-07-13 22:23:23 +00002151bool llvm::parseMDNode(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002152 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2153 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002154}