blob: 0602256683f020f7be72c61de6072aac46a5c3bb [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);
Tim Northoverde3aea0412016-08-17 20:25:25 +0000158 bool parsePredicateOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000159 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000160 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000161 bool parseMachineOperand(MachineOperand &Dest,
162 Optional<unsigned> &TiedDefIdx);
163 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
164 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000165 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000166 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000167 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000168 bool parseIRValue(const Value *&V);
Justin Lebar0af80cd2016-07-15 18:26:59 +0000169 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000170 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
171 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000172 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000173
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000174private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000175 /// Convert the integer literal in the current token into an unsigned integer.
176 ///
177 /// Return true if an error occurred.
178 bool getUnsigned(unsigned &Result);
179
Alex Lorenz4af7e612015-08-03 23:08:19 +0000180 /// Convert the integer literal in the current token into an uint64.
181 ///
182 /// Return true if an error occurred.
183 bool getUint64(uint64_t &Result);
184
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000185 /// If the current token is of the given kind, consume it and return false.
186 /// Otherwise report an error and return true.
187 bool expectAndConsume(MIToken::TokenKind TokenKind);
188
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000189 /// If the current token is of the given kind, consume it and return true.
190 /// Otherwise return false.
191 bool consumeIfPresent(MIToken::TokenKind TokenKind);
192
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000193 void initNames2InstrOpCodes();
194
195 /// Try to convert an instruction name to an opcode. Return true if the
196 /// instruction name is invalid.
197 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000198
Alex Lorenze5a44662015-07-17 00:24:15 +0000199 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000200
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000201 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000202 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000203
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000204 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000205 const MCInstrDesc &MCID);
206
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000207 void initNames2Regs();
208
209 /// Try to convert a register name to a register number. Return true if the
210 /// register name is invalid.
211 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000212
213 void initNames2RegMasks();
214
215 /// Check if the given identifier is a name of a register mask.
216 ///
217 /// Return null if the identifier isn't a register mask.
218 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000219
220 void initNames2SubRegIndices();
221
222 /// Check if the given identifier is a name of a subregister index.
223 ///
224 /// Return 0 if the name isn't a subregister index class.
225 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000226
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000227 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000228 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000229
Alex Lorenzdd13be02015-08-19 23:31:05 +0000230 const Value *getIRValue(unsigned Slot);
231
Alex Lorenzef5c1962015-07-28 23:02:45 +0000232 void initNames2TargetIndices();
233
234 /// Try to convert a name of target index to the corresponding target index.
235 ///
236 /// Return true if the name isn't a name of a target index.
237 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000238
239 void initNames2DirectTargetFlags();
240
241 /// Try to convert a name of a direct target flag to the corresponding
242 /// target flag.
243 ///
244 /// Return true if the name isn't a name of a direct flag.
245 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000246
247 void initNames2BitmaskTargetFlags();
248
249 /// Try to convert a name of a bitmask target flag to the corresponding
250 /// target flag.
251 ///
252 /// Return true if the name isn't a name of a bitmask target flag.
253 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000254};
255
256} // end anonymous namespace
257
Matthias Braune35861d2016-07-13 23:27:50 +0000258MIParser::MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
259 StringRef Source)
260 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
261{}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000262
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000263void MIParser::lex(unsigned SkipChar) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000264 CurrentSource = lexMIToken(
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000265 CurrentSource.data() + SkipChar, Token,
Alex Lorenz91370c52015-06-22 20:37:46 +0000266 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
267}
268
269bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
270
271bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Matthias Braune35861d2016-07-13 23:27:50 +0000272 const SourceMgr &SM = *PFS.SM;
Alex Lorenz91370c52015-06-22 20:37:46 +0000273 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000274 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
275 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
276 // Create an ordinary diagnostic when the source manager's buffer is the
277 // source string.
278 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
279 return true;
280 }
281 // Create a diagnostic for a YAML string literal.
282 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
283 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
284 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000285 return true;
286}
287
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000288static const char *toString(MIToken::TokenKind TokenKind) {
289 switch (TokenKind) {
290 case MIToken::comma:
291 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000292 case MIToken::equal:
293 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000294 case MIToken::colon:
295 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000296 case MIToken::lparen:
297 return "'('";
298 case MIToken::rparen:
299 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000300 default:
301 return "<unknown token>";
302 }
303}
304
305bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
306 if (Token.isNot(TokenKind))
307 return error(Twine("expected ") + toString(TokenKind));
308 lex();
309 return false;
310}
311
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000312bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
313 if (Token.isNot(TokenKind))
314 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000315 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000316 return true;
317}
Alex Lorenz91370c52015-06-22 20:37:46 +0000318
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000319bool MIParser::parseBasicBlockDefinition(
320 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
321 assert(Token.is(MIToken::MachineBasicBlockLabel));
322 unsigned ID = 0;
323 if (getUnsigned(ID))
324 return true;
325 auto Loc = Token.location();
326 auto Name = Token.stringValue();
327 lex();
328 bool HasAddressTaken = false;
329 bool IsLandingPad = false;
330 unsigned Alignment = 0;
331 BasicBlock *BB = nullptr;
332 if (consumeIfPresent(MIToken::lparen)) {
333 do {
334 // TODO: Report an error when multiple same attributes are specified.
335 switch (Token.kind()) {
336 case MIToken::kw_address_taken:
337 HasAddressTaken = true;
338 lex();
339 break;
340 case MIToken::kw_landing_pad:
341 IsLandingPad = true;
342 lex();
343 break;
344 case MIToken::kw_align:
345 if (parseAlignment(Alignment))
346 return true;
347 break;
348 case MIToken::IRBlock:
349 // TODO: Report an error when both name and ir block are specified.
350 if (parseIRBlock(BB, *MF.getFunction()))
351 return true;
352 lex();
353 break;
354 default:
355 break;
356 }
357 } while (consumeIfPresent(MIToken::comma));
358 if (expectAndConsume(MIToken::rparen))
359 return true;
360 }
361 if (expectAndConsume(MIToken::colon))
362 return true;
363
364 if (!Name.empty()) {
365 BB = dyn_cast_or_null<BasicBlock>(
366 MF.getFunction()->getValueSymbolTable().lookup(Name));
367 if (!BB)
368 return error(Loc, Twine("basic block '") + Name +
369 "' is not defined in the function '" +
370 MF.getName() + "'");
371 }
372 auto *MBB = MF.CreateMachineBasicBlock(BB);
373 MF.insert(MF.end(), MBB);
374 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
375 if (!WasInserted)
376 return error(Loc, Twine("redefinition of machine basic block with id #") +
377 Twine(ID));
378 if (Alignment)
379 MBB->setAlignment(Alignment);
380 if (HasAddressTaken)
381 MBB->setHasAddressTaken();
Reid Kleckner0e288232015-08-27 23:27:47 +0000382 MBB->setIsEHPad(IsLandingPad);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000383 return false;
384}
385
386bool MIParser::parseBasicBlockDefinitions(
387 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
388 lex();
389 // Skip until the first machine basic block.
390 while (Token.is(MIToken::Newline))
391 lex();
392 if (Token.isErrorOrEOF())
393 return Token.isError();
394 if (Token.isNot(MIToken::MachineBasicBlockLabel))
395 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000396 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000397 do {
398 if (parseBasicBlockDefinition(MBBSlots))
399 return true;
400 bool IsAfterNewline = false;
401 // Skip until the next machine basic block.
402 while (true) {
403 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
404 Token.isErrorOrEOF())
405 break;
406 else if (Token.is(MIToken::MachineBasicBlockLabel))
407 return error("basic block definition should be located at the start of "
408 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000409 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000410 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000411 continue;
412 }
413 IsAfterNewline = false;
414 if (Token.is(MIToken::lbrace))
415 ++BraceDepth;
416 if (Token.is(MIToken::rbrace)) {
417 if (!BraceDepth)
418 return error("extraneous closing brace ('}')");
419 --BraceDepth;
420 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000421 lex();
422 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000423 // Verify that we closed all of the '{' at the end of a file or a block.
424 if (!Token.isError() && BraceDepth)
425 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000426 } while (!Token.isErrorOrEOF());
427 return Token.isError();
428}
429
430bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
431 assert(Token.is(MIToken::kw_liveins));
432 lex();
433 if (expectAndConsume(MIToken::colon))
434 return true;
435 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
436 return false;
437 do {
438 if (Token.isNot(MIToken::NamedRegister))
439 return error("expected a named register");
440 unsigned Reg = 0;
441 if (parseRegister(Reg))
442 return true;
443 MBB.addLiveIn(Reg);
444 lex();
445 } while (consumeIfPresent(MIToken::comma));
446 return false;
447}
448
449bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
450 assert(Token.is(MIToken::kw_successors));
451 lex();
452 if (expectAndConsume(MIToken::colon))
453 return true;
454 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
455 return false;
456 do {
457 if (Token.isNot(MIToken::MachineBasicBlock))
458 return error("expected a machine basic block reference");
459 MachineBasicBlock *SuccMBB = nullptr;
460 if (parseMBBReference(SuccMBB))
461 return true;
462 lex();
463 unsigned Weight = 0;
464 if (consumeIfPresent(MIToken::lparen)) {
465 if (Token.isNot(MIToken::IntegerLiteral))
466 return error("expected an integer literal after '('");
467 if (getUnsigned(Weight))
468 return true;
469 lex();
470 if (expectAndConsume(MIToken::rparen))
471 return true;
472 }
Cong Houd97c1002015-12-01 05:29:22 +0000473 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000474 } while (consumeIfPresent(MIToken::comma));
Cong Houd97c1002015-12-01 05:29:22 +0000475 MBB.normalizeSuccProbs();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000476 return false;
477}
478
479bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
480 // Skip the definition.
481 assert(Token.is(MIToken::MachineBasicBlockLabel));
482 lex();
483 if (consumeIfPresent(MIToken::lparen)) {
484 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
485 lex();
486 consumeIfPresent(MIToken::rparen);
487 }
488 consumeIfPresent(MIToken::colon);
489
490 // Parse the liveins and successors.
491 // N.B: Multiple lists of successors and liveins are allowed and they're
492 // merged into one.
493 // Example:
494 // liveins: %edi
495 // liveins: %esi
496 //
497 // is equivalent to
498 // liveins: %edi, %esi
499 while (true) {
500 if (Token.is(MIToken::kw_successors)) {
501 if (parseBasicBlockSuccessors(MBB))
502 return true;
503 } else if (Token.is(MIToken::kw_liveins)) {
504 if (parseBasicBlockLiveins(MBB))
505 return true;
506 } else if (consumeIfPresent(MIToken::Newline)) {
507 continue;
508 } else
509 break;
510 if (!Token.isNewlineOrEOF())
511 return error("expected line break at the end of a list");
512 lex();
513 }
514
515 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000516 bool IsInBundle = false;
517 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000518 while (true) {
519 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
520 return false;
521 else if (consumeIfPresent(MIToken::Newline))
522 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000523 if (consumeIfPresent(MIToken::rbrace)) {
524 // The first parsing pass should verify that all closing '}' have an
525 // opening '{'.
526 assert(IsInBundle);
527 IsInBundle = false;
528 continue;
529 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000530 MachineInstr *MI = nullptr;
531 if (parse(MI))
532 return true;
533 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000534 if (IsInBundle) {
535 PrevMI->setFlag(MachineInstr::BundledSucc);
536 MI->setFlag(MachineInstr::BundledPred);
537 }
538 PrevMI = MI;
539 if (Token.is(MIToken::lbrace)) {
540 if (IsInBundle)
541 return error("nested instruction bundles are not allowed");
542 lex();
543 // This instruction is the start of the bundle.
544 MI->setFlag(MachineInstr::BundledSucc);
545 IsInBundle = true;
546 if (!Token.is(MIToken::Newline))
547 // The next instruction can be on the same line.
548 continue;
549 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000550 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
551 lex();
552 }
553 return false;
554}
555
556bool MIParser::parseBasicBlocks() {
557 lex();
558 // Skip until the first machine basic block.
559 while (Token.is(MIToken::Newline))
560 lex();
561 if (Token.isErrorOrEOF())
562 return Token.isError();
563 // The first parsing pass should have verified that this token is a MBB label
564 // in the 'parseBasicBlockDefinitions' method.
565 assert(Token.is(MIToken::MachineBasicBlockLabel));
566 do {
567 MachineBasicBlock *MBB = nullptr;
568 if (parseMBBReference(MBB))
569 return true;
570 if (parseBasicBlock(*MBB))
571 return true;
572 // The method 'parseBasicBlock' should parse the whole block until the next
573 // block or the end of file.
574 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
575 } while (Token.isNot(MIToken::Eof));
576 return false;
577}
578
579bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000580 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000581 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000582 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000583 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000584 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000585 Optional<unsigned> TiedDefIdx;
586 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000587 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000588 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000589 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000590 if (Token.isNot(MIToken::comma))
591 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000592 lex();
593 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000594 if (!Operands.empty() && expectAndConsume(MIToken::equal))
595 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000596
Alex Lorenze5a44662015-07-17 00:24:15 +0000597 unsigned OpCode, Flags = 0;
598 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000599 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000600
Tim Northover98a56eb2016-07-22 22:13:36 +0000601 SmallVector<LLT, 1> Tys;
Quentin Colombet85199672016-03-08 00:20:48 +0000602 if (isPreISelGenericOpcode(OpCode)) {
Tim Northover98a56eb2016-07-22 22:13:36 +0000603 // For generic opcode, at least one type is mandatory.
Tim Northover26e40bd2016-07-26 17:28:01 +0000604 auto Loc = Token.location();
605 bool ManyTypes = Token.is(MIToken::lbrace);
606 if (ManyTypes)
607 lex();
608
609 // Now actually parse the type(s).
Tim Northover98a56eb2016-07-22 22:13:36 +0000610 do {
Tim Northover98a56eb2016-07-22 22:13:36 +0000611 Tys.resize(Tys.size() + 1);
612 if (parseLowLevelType(Loc, Tys[Tys.size() - 1]))
613 return true;
Tim Northover26e40bd2016-07-26 17:28:01 +0000614 } while (ManyTypes && consumeIfPresent(MIToken::comma));
615
616 if (ManyTypes)
617 expectAndConsume(MIToken::rbrace);
Quentin Colombet85199672016-03-08 00:20:48 +0000618 }
619
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000620 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000621 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000622 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000623 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000624 Optional<unsigned> TiedDefIdx;
625 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000626 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000627 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000628 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000629 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
630 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000631 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000632 if (Token.isNot(MIToken::comma))
633 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000634 lex();
635 }
636
Alex Lorenz46d760d2015-07-22 21:15:11 +0000637 DebugLoc DebugLocation;
638 if (Token.is(MIToken::kw_debug_location)) {
639 lex();
640 if (Token.isNot(MIToken::exclaim))
641 return error("expected a metadata node after 'debug-location'");
642 MDNode *Node = nullptr;
643 if (parseMDNode(Node))
644 return true;
645 DebugLocation = DebugLoc(Node);
646 }
647
Alex Lorenz4af7e612015-08-03 23:08:19 +0000648 // Parse the machine memory operands.
649 SmallVector<MachineMemOperand *, 2> MemOperands;
650 if (Token.is(MIToken::coloncolon)) {
651 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000652 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000653 MachineMemOperand *MemOp = nullptr;
654 if (parseMachineMemoryOperand(MemOp))
655 return true;
656 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000657 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000658 break;
659 if (Token.isNot(MIToken::comma))
660 return error("expected ',' before the next machine memory operand");
661 lex();
662 }
663 }
664
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000665 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000666 if (!MCID.isVariadic()) {
667 // FIXME: Move the implicit operand verification to the machine verifier.
668 if (verifyImplicitOperands(Operands, MCID))
669 return true;
670 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000671
Alex Lorenzcb268d42015-07-06 23:07:26 +0000672 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000673 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000674 MI->setFlags(Flags);
Tim Northover98a56eb2016-07-22 22:13:36 +0000675 if (Tys.size() > 0) {
676 for (unsigned i = 0; i < Tys.size(); ++i)
677 MI->setType(Tys[i], i);
678 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000679 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000680 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000681 if (assignRegisterTies(*MI, Operands))
682 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000683 if (MemOperands.empty())
684 return false;
685 MachineInstr::mmo_iterator MemRefs =
686 MF.allocateMemRefsArray(MemOperands.size());
687 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
688 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000689 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000690}
691
Alex Lorenz1ea60892015-07-27 20:29:27 +0000692bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000693 lex();
694 if (Token.isNot(MIToken::MachineBasicBlock))
695 return error("expected a machine basic block reference");
696 if (parseMBBReference(MBB))
697 return true;
698 lex();
699 if (Token.isNot(MIToken::Eof))
700 return error(
701 "expected end of string after the machine basic block reference");
702 return false;
703}
704
Alex Lorenz1ea60892015-07-27 20:29:27 +0000705bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000706 lex();
707 if (Token.isNot(MIToken::NamedRegister))
708 return error("expected a named register");
709 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000710 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000711 lex();
712 if (Token.isNot(MIToken::Eof))
713 return error("expected end of string after the register reference");
714 return false;
715}
716
Alex Lorenz12045a42015-07-27 17:42:45 +0000717bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
718 lex();
719 if (Token.isNot(MIToken::VirtualRegister))
720 return error("expected a virtual register");
721 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000722 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000723 lex();
724 if (Token.isNot(MIToken::Eof))
725 return error("expected end of string after the register reference");
726 return false;
727}
728
Alex Lorenza314d812015-08-18 22:26:26 +0000729bool MIParser::parseStandaloneStackObject(int &FI) {
730 lex();
731 if (Token.isNot(MIToken::StackObject))
732 return error("expected a stack object");
733 if (parseStackFrameIndex(FI))
734 return true;
735 if (Token.isNot(MIToken::Eof))
736 return error("expected end of string after the stack object reference");
737 return false;
738}
739
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000740bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
741 lex();
742 if (Token.isNot(MIToken::exclaim))
743 return error("expected a metadata node");
744 if (parseMDNode(Node))
745 return true;
746 if (Token.isNot(MIToken::Eof))
747 return error("expected end of string after the metadata node");
748 return false;
749}
750
Alex Lorenz36962cd2015-07-07 02:08:46 +0000751static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
752 assert(MO.isImplicit());
753 return MO.isDef() ? "implicit-def" : "implicit";
754}
755
756static std::string getRegisterName(const TargetRegisterInfo *TRI,
757 unsigned Reg) {
758 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
759 return StringRef(TRI->getName(Reg)).lower();
760}
761
Alex Lorenz0153e592015-09-10 14:04:34 +0000762/// Return true if the parsed machine operands contain a given machine operand.
763static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
764 ArrayRef<ParsedMachineOperand> Operands) {
765 for (const auto &I : Operands) {
766 if (ImplicitOperand.isIdenticalTo(I.Operand))
767 return true;
768 }
769 return false;
770}
771
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000772bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
773 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000774 if (MCID.isCall())
775 // We can't verify call instructions as they can contain arbitrary implicit
776 // register and register mask operands.
777 return false;
778
779 // Gather all the expected implicit operands.
780 SmallVector<MachineOperand, 4> ImplicitOperands;
781 if (MCID.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000782 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000783 ImplicitOperands.push_back(
784 MachineOperand::CreateReg(*ImpDefs, true, true));
785 if (MCID.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000786 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000787 ImplicitOperands.push_back(
788 MachineOperand::CreateReg(*ImpUses, false, true));
789
790 const auto *TRI = MF.getSubtarget().getRegisterInfo();
791 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000792 for (const auto &I : ImplicitOperands) {
793 if (isImplicitOperandIn(I, Operands))
794 continue;
795 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000796 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000797 printImplicitRegisterFlag(I) + " %" +
798 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000799 }
800 return false;
801}
802
Alex Lorenze5a44662015-07-17 00:24:15 +0000803bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
804 if (Token.is(MIToken::kw_frame_setup)) {
805 Flags |= MachineInstr::FrameSetup;
806 lex();
807 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000808 if (Token.isNot(MIToken::Identifier))
809 return error("expected a machine instruction");
810 StringRef InstrName = Token.stringValue();
811 if (parseInstrName(InstrName, OpCode))
812 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000813 lex();
814 return false;
815}
816
817bool MIParser::parseRegister(unsigned &Reg) {
818 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000819 case MIToken::underscore:
820 Reg = 0;
821 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000822 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000823 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000824 if (getRegisterByName(Name, Reg))
825 return error(Twine("unknown register name '") + Name + "'");
826 break;
827 }
Alex Lorenz53464512015-07-10 22:51:20 +0000828 case MIToken::VirtualRegister: {
829 unsigned ID;
830 if (getUnsigned(ID))
831 return true;
832 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
833 if (RegInfo == PFS.VirtualRegisterSlots.end())
834 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
835 "'");
836 Reg = RegInfo->second;
837 break;
838 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000839 // TODO: Parse other register kinds.
840 default:
841 llvm_unreachable("The current token should be a register");
842 }
843 return false;
844}
845
Alex Lorenzcb268d42015-07-06 23:07:26 +0000846bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000847 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000848 switch (Token.kind()) {
849 case MIToken::kw_implicit:
850 Flags |= RegState::Implicit;
851 break;
852 case MIToken::kw_implicit_define:
853 Flags |= RegState::ImplicitDefine;
854 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000855 case MIToken::kw_def:
856 Flags |= RegState::Define;
857 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000858 case MIToken::kw_dead:
859 Flags |= RegState::Dead;
860 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000861 case MIToken::kw_killed:
862 Flags |= RegState::Kill;
863 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000864 case MIToken::kw_undef:
865 Flags |= RegState::Undef;
866 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000867 case MIToken::kw_internal:
868 Flags |= RegState::InternalRead;
869 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000870 case MIToken::kw_early_clobber:
871 Flags |= RegState::EarlyClobber;
872 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000873 case MIToken::kw_debug_use:
874 Flags |= RegState::Debug;
875 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000876 default:
877 llvm_unreachable("The current token should be a register flag");
878 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000879 if (OldFlags == Flags)
880 // We know that the same flag is specified more than once when the flags
881 // weren't modified.
882 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000883 lex();
884 return false;
885}
886
Alex Lorenz2eacca82015-07-13 23:24:34 +0000887bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
Matthias Braun333e4682016-07-26 21:49:34 +0000888 assert(Token.is(MIToken::dot));
Alex Lorenz2eacca82015-07-13 23:24:34 +0000889 lex();
890 if (Token.isNot(MIToken::Identifier))
Matthias Braun333e4682016-07-26 21:49:34 +0000891 return error("expected a subregister index after '.'");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000892 auto Name = Token.stringValue();
893 SubReg = getSubRegIndex(Name);
894 if (!SubReg)
895 return error(Twine("use of unknown subregister index '") + Name + "'");
896 lex();
897 return false;
898}
899
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000900bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
901 if (!consumeIfPresent(MIToken::kw_tied_def))
902 return error("expected 'tied-def' after '('");
903 if (Token.isNot(MIToken::IntegerLiteral))
904 return error("expected an integer literal after 'tied-def'");
905 if (getUnsigned(TiedDefIdx))
906 return true;
907 lex();
908 if (expectAndConsume(MIToken::rparen))
909 return true;
910 return false;
911}
912
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000913bool MIParser::parseSize(unsigned &Size) {
914 if (Token.isNot(MIToken::IntegerLiteral))
915 return error("expected an integer literal for the size");
916 if (getUnsigned(Size))
917 return true;
918 lex();
919 if (expectAndConsume(MIToken::rparen))
920 return true;
921 return false;
922}
923
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000924bool MIParser::assignRegisterTies(MachineInstr &MI,
925 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000926 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
927 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
928 if (!Operands[I].TiedDefIdx)
929 continue;
930 // The parser ensures that this operand is a register use, so we just have
931 // to check the tied-def operand.
932 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
933 if (DefIdx >= E)
934 return error(Operands[I].Begin,
935 Twine("use of invalid tied-def operand index '" +
936 Twine(DefIdx) + "'; instruction has only ") +
937 Twine(E) + " operands");
938 const auto &DefOperand = Operands[DefIdx].Operand;
939 if (!DefOperand.isReg() || !DefOperand.isDef())
940 // FIXME: add note with the def operand.
941 return error(Operands[I].Begin,
942 Twine("use of invalid tied-def operand index '") +
943 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
944 " isn't a defined register");
945 // Check that the tied-def operand wasn't tied elsewhere.
946 for (const auto &TiedPair : TiedRegisterPairs) {
947 if (TiedPair.first == DefIdx)
948 return error(Operands[I].Begin,
949 Twine("the tied-def operand #") + Twine(DefIdx) +
950 " is already tied with another register operand");
951 }
952 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
953 }
954 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
955 // indices must be less than tied max.
956 for (const auto &TiedPair : TiedRegisterPairs)
957 MI.tieOperands(TiedPair.first, TiedPair.second);
958 return false;
959}
960
961bool MIParser::parseRegisterOperand(MachineOperand &Dest,
962 Optional<unsigned> &TiedDefIdx,
963 bool IsDef) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000964 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000965 unsigned Flags = IsDef ? RegState::Define : 0;
966 while (Token.isRegisterFlag()) {
967 if (parseRegisterFlag(Flags))
968 return true;
969 }
970 if (!Token.isRegister())
971 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000972 if (parseRegister(Reg))
973 return true;
974 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000975 unsigned SubReg = 0;
Matthias Braun333e4682016-07-26 21:49:34 +0000976 if (Token.is(MIToken::dot)) {
Alex Lorenz2eacca82015-07-13 23:24:34 +0000977 if (parseSubRegisterIndex(SubReg))
978 return true;
Matthias Braun5d00b322016-07-16 01:36:18 +0000979 if (!TargetRegisterInfo::isVirtualRegister(Reg))
980 return error("subregister index expects a virtual register");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000981 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000982 if ((Flags & RegState::Define) == 0) {
983 if (consumeIfPresent(MIToken::lparen)) {
984 unsigned Idx;
985 if (parseRegisterTiedDefIndex(Idx))
986 return true;
987 TiedDefIdx = Idx;
988 }
989 } else if (consumeIfPresent(MIToken::lparen)) {
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000990 MachineRegisterInfo &MRI = MF.getRegInfo();
991
Quentin Colombet2c646962016-06-08 23:27:46 +0000992 // Virtual registers may have a size with GlobalISel.
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000993 if (!TargetRegisterInfo::isVirtualRegister(Reg))
994 return error("unexpected size on physical register");
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000995 if (MRI.getRegClassOrRegBank(Reg).is<const TargetRegisterClass *>())
996 return error("unexpected size on non-generic virtual register");
997
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000998 unsigned Size;
999 if (parseSize(Size))
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001000 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001001
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001002 MRI.setSize(Reg, Size);
Quentin Colombet2c646962016-06-08 23:27:46 +00001003 } else if (PFS.GenericVRegs.count(Reg)) {
1004 // Generic virtual registers must have a size.
1005 // If we end up here this means the size hasn't been specified and
1006 // this is bad!
1007 return error("generic virtual registers must have a size");
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001008 }
Alex Lorenz495ad872015-07-08 21:23:34 +00001009 Dest = MachineOperand::CreateReg(
1010 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +00001011 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +00001012 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1013 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001014 return false;
1015}
1016
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001017bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1018 assert(Token.is(MIToken::IntegerLiteral));
1019 const APSInt &Int = Token.integerValue();
1020 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +00001021 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001022 Dest = MachineOperand::CreateImm(Int.getExtValue());
1023 lex();
1024 return false;
1025}
1026
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001027bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1028 const Constant *&C) {
1029 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001030 SMDiagnostic Err;
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001031 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
Matthias Braune35861d2016-07-13 23:27:50 +00001032 &PFS.IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001033 if (!C)
1034 return error(Loc + Err.getColumnNo(), Err.getMessage());
1035 return false;
1036}
1037
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001038bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1039 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1040 return true;
1041 lex();
1042 return false;
1043}
1044
Ahmed Bougachad760de02016-07-28 17:15:12 +00001045bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
Tim Northover62ae5682016-07-20 19:09:30 +00001046 if (Token.is(MIToken::Identifier) && Token.stringValue() == "unsized") {
Tim Northover62ae5682016-07-20 19:09:30 +00001047 lex();
1048 Ty = LLT::unsized();
1049 return false;
1050 } else if (Token.is(MIToken::ScalarType)) {
1051 Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1052 lex();
1053 return false;
Tim Northoverbd505462016-07-22 16:59:52 +00001054 } else if (Token.is(MIToken::PointerType)) {
1055 Ty = LLT::pointer(APSInt(Token.range().drop_front()).getZExtValue());
1056 lex();
1057 return false;
Tim Northover62ae5682016-07-20 19:09:30 +00001058 }
Quentin Colombet85199672016-03-08 00:20:48 +00001059
Tim Northover62ae5682016-07-20 19:09:30 +00001060 // Now we're looking for a vector.
1061 if (Token.isNot(MIToken::less))
Tim Northoverbd505462016-07-22 16:59:52 +00001062 return error(Loc,
1063 "expected unsized, pN, sN or <N x sM> for GlobalISel type");
1064
Tim Northover62ae5682016-07-20 19:09:30 +00001065 lex();
1066
1067 if (Token.isNot(MIToken::IntegerLiteral))
1068 return error(Loc, "expected <N x sM> for vctor type");
1069 uint64_t NumElements = Token.integerValue().getZExtValue();
1070 lex();
1071
1072 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1073 return error(Loc, "expected '<N x sM>' for vector type");
1074 lex();
1075
1076 if (Token.isNot(MIToken::ScalarType))
1077 return error(Loc, "expected '<N x sM>' for vector type");
1078 uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1079 lex();
1080
1081 if (Token.isNot(MIToken::greater))
1082 return error(Loc, "expected '<N x sM>' for vector type");
1083 lex();
1084
1085 Ty = LLT::vector(NumElements, ScalarSize);
Quentin Colombet85199672016-03-08 00:20:48 +00001086 return false;
1087}
1088
Alex Lorenz05e38822015-08-05 18:52:21 +00001089bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1090 assert(Token.is(MIToken::IntegerType));
1091 auto Loc = Token.location();
1092 lex();
1093 if (Token.isNot(MIToken::IntegerLiteral))
1094 return error("expected an integer literal");
1095 const Constant *C = nullptr;
1096 if (parseIRConstant(Loc, C))
1097 return true;
1098 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1099 return false;
1100}
1101
Alex Lorenzad156fb2015-07-31 20:49:21 +00001102bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1103 auto Loc = Token.location();
1104 lex();
1105 if (Token.isNot(MIToken::FloatingPointLiteral))
1106 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001107 const Constant *C = nullptr;
1108 if (parseIRConstant(Loc, C))
1109 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001110 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1111 return false;
1112}
1113
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001114bool MIParser::getUnsigned(unsigned &Result) {
1115 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1116 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1117 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1118 if (Val64 == Limit)
1119 return error("expected 32-bit integer (too large)");
1120 Result = Val64;
1121 return false;
1122}
1123
Alex Lorenzf09df002015-06-30 18:16:42 +00001124bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001125 assert(Token.is(MIToken::MachineBasicBlock) ||
1126 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001127 unsigned Number;
1128 if (getUnsigned(Number))
1129 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001130 auto MBBInfo = PFS.MBBSlots.find(Number);
1131 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001132 return error(Twine("use of undefined machine basic block #") +
1133 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001134 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001135 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1136 return error(Twine("the name of machine basic block #") + Twine(Number) +
1137 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001138 return false;
1139}
1140
1141bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1142 MachineBasicBlock *MBB;
1143 if (parseMBBReference(MBB))
1144 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001145 Dest = MachineOperand::CreateMBB(MBB);
1146 lex();
1147 return false;
1148}
1149
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001150bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001151 assert(Token.is(MIToken::StackObject));
1152 unsigned ID;
1153 if (getUnsigned(ID))
1154 return true;
1155 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1156 if (ObjectInfo == PFS.StackObjectSlots.end())
1157 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1158 "'");
1159 StringRef Name;
1160 if (const auto *Alloca =
Matthias Braun941a7052016-07-28 18:40:00 +00001161 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001162 Name = Alloca->getName();
1163 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1164 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1165 "' isn't '" + Token.stringValue() + "'");
1166 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001167 FI = ObjectInfo->second;
1168 return false;
1169}
1170
1171bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1172 int FI;
1173 if (parseStackFrameIndex(FI))
1174 return true;
1175 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001176 return false;
1177}
1178
Alex Lorenzea882122015-08-12 21:17:02 +00001179bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001180 assert(Token.is(MIToken::FixedStackObject));
1181 unsigned ID;
1182 if (getUnsigned(ID))
1183 return true;
1184 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1185 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1186 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1187 Twine(ID) + "'");
1188 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001189 FI = ObjectInfo->second;
1190 return false;
1191}
1192
1193bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1194 int FI;
1195 if (parseFixedStackFrameIndex(FI))
1196 return true;
1197 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001198 return false;
1199}
1200
Alex Lorenz41df7d32015-07-28 17:09:52 +00001201bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001202 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001203 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001204 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001205 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001206 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001207 return error(Twine("use of undefined global value '") + Token.range() +
1208 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001209 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001210 }
1211 case MIToken::GlobalValue: {
1212 unsigned GVIdx;
1213 if (getUnsigned(GVIdx))
1214 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001215 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001216 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1217 "'");
Matthias Braune35861d2016-07-13 23:27:50 +00001218 GV = PFS.IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001219 break;
1220 }
1221 default:
1222 llvm_unreachable("The current token should be a global value");
1223 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001224 return false;
1225}
1226
1227bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1228 GlobalValue *GV = nullptr;
1229 if (parseGlobalValue(GV))
1230 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001231 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001232 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001233 if (parseOperandsOffset(Dest))
1234 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001235 return false;
1236}
1237
Alex Lorenzab980492015-07-20 20:51:18 +00001238bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1239 assert(Token.is(MIToken::ConstantPoolItem));
1240 unsigned ID;
1241 if (getUnsigned(ID))
1242 return true;
1243 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1244 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1245 return error("use of undefined constant '%const." + Twine(ID) + "'");
1246 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001247 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001248 if (parseOperandsOffset(Dest))
1249 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001250 return false;
1251}
1252
Alex Lorenz31d70682015-07-15 23:38:35 +00001253bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1254 assert(Token.is(MIToken::JumpTableIndex));
1255 unsigned ID;
1256 if (getUnsigned(ID))
1257 return true;
1258 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1259 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1260 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1261 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001262 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1263 return false;
1264}
1265
Alex Lorenz6ede3742015-07-21 16:59:53 +00001266bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001267 assert(Token.is(MIToken::ExternalSymbol));
1268 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001269 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001270 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001271 if (parseOperandsOffset(Dest))
1272 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001273 return false;
1274}
1275
Matthias Braunb74eb412016-03-28 18:18:46 +00001276bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1277 assert(Token.is(MIToken::SubRegisterIndex));
1278 StringRef Name = Token.stringValue();
1279 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1280 if (SubRegIndex == 0)
1281 return error(Twine("unknown subregister index '") + Name + "'");
1282 lex();
1283 Dest = MachineOperand::CreateImm(SubRegIndex);
1284 return false;
1285}
1286
Alex Lorenz44f29252015-07-22 21:07:04 +00001287bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001288 assert(Token.is(MIToken::exclaim));
1289 auto Loc = Token.location();
1290 lex();
1291 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1292 return error("expected metadata id after '!'");
1293 unsigned ID;
1294 if (getUnsigned(ID))
1295 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001296 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1297 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
Alex Lorenz35e44462015-07-22 17:58:46 +00001298 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1299 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001300 Node = NodeInfo->second.get();
1301 return false;
1302}
1303
1304bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1305 MDNode *Node = nullptr;
1306 if (parseMDNode(Node))
1307 return true;
1308 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001309 return false;
1310}
1311
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001312bool MIParser::parseCFIOffset(int &Offset) {
1313 if (Token.isNot(MIToken::IntegerLiteral))
1314 return error("expected a cfi offset");
1315 if (Token.integerValue().getMinSignedBits() > 32)
1316 return error("expected a 32 bit integer (the cfi offset is too large)");
1317 Offset = (int)Token.integerValue().getExtValue();
1318 lex();
1319 return false;
1320}
1321
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001322bool MIParser::parseCFIRegister(unsigned &Reg) {
1323 if (Token.isNot(MIToken::NamedRegister))
1324 return error("expected a cfi register");
1325 unsigned LLVMReg;
1326 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001327 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001328 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1329 assert(TRI && "Expected target register info");
1330 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1331 if (DwarfReg < 0)
1332 return error("invalid DWARF register");
1333 Reg = (unsigned)DwarfReg;
1334 lex();
1335 return false;
1336}
1337
1338bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1339 auto Kind = Token.kind();
1340 lex();
1341 auto &MMI = MF.getMMI();
1342 int Offset;
1343 unsigned Reg;
1344 unsigned CFIIndex;
1345 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001346 case MIToken::kw_cfi_same_value:
1347 if (parseCFIRegister(Reg))
1348 return true;
1349 CFIIndex =
1350 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1351 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001352 case MIToken::kw_cfi_offset:
1353 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1354 parseCFIOffset(Offset))
1355 return true;
1356 CFIIndex =
1357 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1358 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001359 case MIToken::kw_cfi_def_cfa_register:
1360 if (parseCFIRegister(Reg))
1361 return true;
1362 CFIIndex =
1363 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1364 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001365 case MIToken::kw_cfi_def_cfa_offset:
1366 if (parseCFIOffset(Offset))
1367 return true;
1368 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1369 CFIIndex = MMI.addFrameInst(
1370 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1371 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001372 case MIToken::kw_cfi_def_cfa:
1373 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1374 parseCFIOffset(Offset))
1375 return true;
1376 // NB: MCCFIInstruction::createDefCfa negates the offset.
1377 CFIIndex =
1378 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1379 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001380 default:
1381 // TODO: Parse the other CFI operands.
1382 llvm_unreachable("The current token should be a cfi operand");
1383 }
1384 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001385 return false;
1386}
1387
Alex Lorenzdeb53492015-07-28 17:28:03 +00001388bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1389 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001390 case MIToken::NamedIRBlock: {
1391 BB = dyn_cast_or_null<BasicBlock>(
1392 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001393 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001394 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001395 break;
1396 }
1397 case MIToken::IRBlock: {
1398 unsigned SlotNumber = 0;
1399 if (getUnsigned(SlotNumber))
1400 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001401 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001402 if (!BB)
1403 return error(Twine("use of undefined IR block '%ir-block.") +
1404 Twine(SlotNumber) + "'");
1405 break;
1406 }
1407 default:
1408 llvm_unreachable("The current token should be an IR block reference");
1409 }
1410 return false;
1411}
1412
1413bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1414 assert(Token.is(MIToken::kw_blockaddress));
1415 lex();
1416 if (expectAndConsume(MIToken::lparen))
1417 return true;
1418 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001419 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001420 return error("expected a global value");
1421 GlobalValue *GV = nullptr;
1422 if (parseGlobalValue(GV))
1423 return true;
1424 auto *F = dyn_cast<Function>(GV);
1425 if (!F)
1426 return error("expected an IR function reference");
1427 lex();
1428 if (expectAndConsume(MIToken::comma))
1429 return true;
1430 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001431 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001432 return error("expected an IR block reference");
1433 if (parseIRBlock(BB, *F))
1434 return true;
1435 lex();
1436 if (expectAndConsume(MIToken::rparen))
1437 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001438 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001439 if (parseOperandsOffset(Dest))
1440 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001441 return false;
1442}
1443
Tim Northover6b3bd612016-07-29 20:32:59 +00001444bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
1445 assert(Token.is(MIToken::kw_intrinsic));
1446 lex();
1447 if (expectAndConsume(MIToken::lparen))
1448 return error("expected syntax intrinsic(@llvm.whatever)");
1449
1450 if (Token.isNot(MIToken::NamedGlobalValue))
1451 return error("expected syntax intrinsic(@llvm.whatever)");
1452
1453 std::string Name = Token.stringValue();
1454 lex();
1455
1456 if (expectAndConsume(MIToken::rparen))
1457 return error("expected ')' to terminate intrinsic name");
1458
1459 // Find out what intrinsic we're dealing with, first try the global namespace
1460 // and then the target's private intrinsics if that fails.
1461 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1462 Intrinsic::ID ID = Function::lookupIntrinsicID(Name);
1463 if (ID == Intrinsic::not_intrinsic && TII)
1464 ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
1465
1466 if (ID == Intrinsic::not_intrinsic)
1467 return error("unknown intrinsic name");
1468 Dest = MachineOperand::CreateIntrinsicID(ID);
1469
1470 return false;
1471}
1472
Tim Northoverde3aea0412016-08-17 20:25:25 +00001473bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
1474 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
1475 bool IsFloat = Token.is(MIToken::kw_floatpred);
1476 lex();
1477
1478 if (expectAndConsume(MIToken::lparen))
1479 return error("expected syntax intpred(whatever) or floatpred(whatever");
1480
1481 if (Token.isNot(MIToken::Identifier))
1482 return error("whatever");
1483
1484 CmpInst::Predicate Pred;
1485 if (IsFloat) {
1486 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1487 .Case("false", CmpInst::FCMP_FALSE)
1488 .Case("oeq", CmpInst::FCMP_OEQ)
1489 .Case("ogt", CmpInst::FCMP_OGT)
1490 .Case("oge", CmpInst::FCMP_OGE)
1491 .Case("olt", CmpInst::FCMP_OLT)
1492 .Case("ole", CmpInst::FCMP_OLE)
1493 .Case("one", CmpInst::FCMP_ONE)
1494 .Case("ord", CmpInst::FCMP_ORD)
1495 .Case("uno", CmpInst::FCMP_UNO)
1496 .Case("ueq", CmpInst::FCMP_UEQ)
1497 .Case("ugt", CmpInst::FCMP_UGT)
1498 .Case("uge", CmpInst::FCMP_UGE)
1499 .Case("ult", CmpInst::FCMP_ULT)
1500 .Case("ule", CmpInst::FCMP_ULE)
1501 .Case("une", CmpInst::FCMP_UNE)
1502 .Case("true", CmpInst::FCMP_TRUE)
1503 .Default(CmpInst::BAD_FCMP_PREDICATE);
1504 if (!CmpInst::isFPPredicate(Pred))
1505 return error("invalid floating-point predicate");
1506 } else {
1507 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1508 .Case("eq", CmpInst::ICMP_EQ)
1509 .Case("ne", CmpInst::ICMP_NE)
1510 .Case("sgt", CmpInst::ICMP_SGT)
1511 .Case("sge", CmpInst::ICMP_SGE)
1512 .Case("slt", CmpInst::ICMP_SLT)
1513 .Case("sle", CmpInst::ICMP_SLE)
1514 .Case("ugt", CmpInst::ICMP_UGT)
1515 .Case("uge", CmpInst::ICMP_UGE)
1516 .Case("ult", CmpInst::ICMP_ULT)
1517 .Case("ule", CmpInst::ICMP_ULE)
1518 .Default(CmpInst::BAD_ICMP_PREDICATE);
1519 if (!CmpInst::isIntPredicate(Pred))
1520 return error("invalid integer predicate");
1521 }
1522
1523 lex();
1524 Dest = MachineOperand::CreatePredicate(Pred);
1525 if (!expectAndConsume(MIToken::rparen))
1526 return error("predicate should be terminated by ')'.");
1527
1528 return false;
1529}
1530
Alex Lorenzef5c1962015-07-28 23:02:45 +00001531bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1532 assert(Token.is(MIToken::kw_target_index));
1533 lex();
1534 if (expectAndConsume(MIToken::lparen))
1535 return true;
1536 if (Token.isNot(MIToken::Identifier))
1537 return error("expected the name of the target index");
1538 int Index = 0;
1539 if (getTargetIndex(Token.stringValue(), Index))
1540 return error("use of undefined target index '" + Token.stringValue() + "'");
1541 lex();
1542 if (expectAndConsume(MIToken::rparen))
1543 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001544 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001545 if (parseOperandsOffset(Dest))
1546 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001547 return false;
1548}
1549
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001550bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1551 assert(Token.is(MIToken::kw_liveout));
1552 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1553 assert(TRI && "Expected target register info");
1554 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1555 lex();
1556 if (expectAndConsume(MIToken::lparen))
1557 return true;
1558 while (true) {
1559 if (Token.isNot(MIToken::NamedRegister))
1560 return error("expected a named register");
1561 unsigned Reg = 0;
1562 if (parseRegister(Reg))
1563 return true;
1564 lex();
1565 Mask[Reg / 32] |= 1U << (Reg % 32);
1566 // TODO: Report an error if the same register is used more than once.
1567 if (Token.isNot(MIToken::comma))
1568 break;
1569 lex();
1570 }
1571 if (expectAndConsume(MIToken::rparen))
1572 return true;
1573 Dest = MachineOperand::CreateRegLiveOut(Mask);
1574 return false;
1575}
1576
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001577bool MIParser::parseMachineOperand(MachineOperand &Dest,
1578 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001579 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001580 case MIToken::kw_implicit:
1581 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001582 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001583 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001584 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001585 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001586 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001587 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001588 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001589 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001590 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001591 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001592 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001593 case MIToken::IntegerLiteral:
1594 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001595 case MIToken::IntegerType:
1596 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001597 case MIToken::kw_half:
1598 case MIToken::kw_float:
1599 case MIToken::kw_double:
1600 case MIToken::kw_x86_fp80:
1601 case MIToken::kw_fp128:
1602 case MIToken::kw_ppc_fp128:
1603 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001604 case MIToken::MachineBasicBlock:
1605 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001606 case MIToken::StackObject:
1607 return parseStackObjectOperand(Dest);
1608 case MIToken::FixedStackObject:
1609 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001610 case MIToken::GlobalValue:
1611 case MIToken::NamedGlobalValue:
1612 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001613 case MIToken::ConstantPoolItem:
1614 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001615 case MIToken::JumpTableIndex:
1616 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001617 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001618 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00001619 case MIToken::SubRegisterIndex:
1620 return parseSubRegisterIndexOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001621 case MIToken::exclaim:
1622 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001623 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001624 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001625 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001626 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001627 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001628 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001629 case MIToken::kw_blockaddress:
1630 return parseBlockAddressOperand(Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +00001631 case MIToken::kw_intrinsic:
1632 return parseIntrinsicOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001633 case MIToken::kw_target_index:
1634 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001635 case MIToken::kw_liveout:
1636 return parseLiveoutRegisterMaskOperand(Dest);
Tim Northoverde3aea0412016-08-17 20:25:25 +00001637 case MIToken::kw_floatpred:
1638 case MIToken::kw_intpred:
1639 return parsePredicateOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001640 case MIToken::Error:
1641 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001642 case MIToken::Identifier:
1643 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1644 Dest = MachineOperand::CreateRegMask(RegMask);
1645 lex();
1646 break;
1647 }
Justin Bognerb03fd122016-08-17 05:10:15 +00001648 LLVM_FALLTHROUGH;
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001649 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001650 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001651 return error("expected a machine operand");
1652 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001653 return false;
1654}
1655
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001656bool MIParser::parseMachineOperandAndTargetFlags(
1657 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001658 unsigned TF = 0;
1659 bool HasTargetFlags = false;
1660 if (Token.is(MIToken::kw_target_flags)) {
1661 HasTargetFlags = true;
1662 lex();
1663 if (expectAndConsume(MIToken::lparen))
1664 return true;
1665 if (Token.isNot(MIToken::Identifier))
1666 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001667 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1668 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1669 return error("use of undefined target flag '" + Token.stringValue() +
1670 "'");
1671 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001672 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001673 while (Token.is(MIToken::comma)) {
1674 lex();
1675 if (Token.isNot(MIToken::Identifier))
1676 return error("expected the name of the target flag");
1677 unsigned BitFlag = 0;
1678 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1679 return error("use of undefined target flag '" + Token.stringValue() +
1680 "'");
1681 // TODO: Report an error when using a duplicate bit target flag.
1682 TF |= BitFlag;
1683 lex();
1684 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001685 if (expectAndConsume(MIToken::rparen))
1686 return true;
1687 }
1688 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001689 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001690 return true;
1691 if (!HasTargetFlags)
1692 return false;
1693 if (Dest.isReg())
1694 return error(Loc, "register operands can't have target flags");
1695 Dest.setTargetFlags(TF);
1696 return false;
1697}
1698
Alex Lorenzdc24c172015-08-07 20:21:00 +00001699bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001700 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1701 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001702 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001703 bool IsNegative = Token.is(MIToken::minus);
1704 lex();
1705 if (Token.isNot(MIToken::IntegerLiteral))
1706 return error("expected an integer literal after '" + Sign + "'");
1707 if (Token.integerValue().getMinSignedBits() > 64)
1708 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001709 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001710 if (IsNegative)
1711 Offset = -Offset;
1712 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001713 return false;
1714}
1715
Alex Lorenz620f8912015-08-13 20:33:33 +00001716bool MIParser::parseAlignment(unsigned &Alignment) {
1717 assert(Token.is(MIToken::kw_align));
1718 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001719 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001720 return error("expected an integer literal after 'align'");
1721 if (getUnsigned(Alignment))
1722 return true;
1723 lex();
1724 return false;
1725}
1726
Alex Lorenzdc24c172015-08-07 20:21:00 +00001727bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1728 int64_t Offset = 0;
1729 if (parseOffset(Offset))
1730 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001731 Op.setOffset(Offset);
1732 return false;
1733}
1734
Alex Lorenz36593ac2015-08-19 23:27:07 +00001735bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001736 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001737 case MIToken::NamedIRValue: {
1738 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001739 break;
1740 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001741 case MIToken::IRValue: {
1742 unsigned SlotNumber = 0;
1743 if (getUnsigned(SlotNumber))
1744 return true;
1745 V = getIRValue(SlotNumber);
1746 break;
1747 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001748 case MIToken::NamedGlobalValue:
1749 case MIToken::GlobalValue: {
1750 GlobalValue *GV = nullptr;
1751 if (parseGlobalValue(GV))
1752 return true;
1753 V = GV;
1754 break;
1755 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001756 case MIToken::QuotedIRValue: {
1757 const Constant *C = nullptr;
1758 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1759 return true;
1760 V = C;
1761 break;
1762 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001763 default:
1764 llvm_unreachable("The current token should be an IR block reference");
1765 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001766 if (!V)
1767 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001768 return false;
1769}
1770
1771bool MIParser::getUint64(uint64_t &Result) {
1772 assert(Token.hasIntegerValue());
1773 if (Token.integerValue().getActiveBits() > 64)
1774 return error("expected 64-bit integer (too large)");
1775 Result = Token.integerValue().getZExtValue();
1776 return false;
1777}
1778
Justin Lebar0af80cd2016-07-15 18:26:59 +00001779bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
1780 const auto OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001781 switch (Token.kind()) {
1782 case MIToken::kw_volatile:
1783 Flags |= MachineMemOperand::MOVolatile;
1784 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001785 case MIToken::kw_non_temporal:
1786 Flags |= MachineMemOperand::MONonTemporal;
1787 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001788 case MIToken::kw_invariant:
1789 Flags |= MachineMemOperand::MOInvariant;
1790 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001791 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001792 default:
1793 llvm_unreachable("The current token should be a memory operand flag");
1794 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001795 if (OldFlags == Flags)
1796 // We know that the same flag is specified more than once when the flags
1797 // weren't modified.
1798 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001799 lex();
1800 return false;
1801}
1802
Alex Lorenz91097a32015-08-12 20:33:26 +00001803bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1804 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001805 case MIToken::kw_stack:
1806 PSV = MF.getPSVManager().getStack();
1807 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001808 case MIToken::kw_got:
1809 PSV = MF.getPSVManager().getGOT();
1810 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001811 case MIToken::kw_jump_table:
1812 PSV = MF.getPSVManager().getJumpTable();
1813 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001814 case MIToken::kw_constant_pool:
1815 PSV = MF.getPSVManager().getConstantPool();
1816 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001817 case MIToken::FixedStackObject: {
1818 int FI;
1819 if (parseFixedStackFrameIndex(FI))
1820 return true;
1821 PSV = MF.getPSVManager().getFixedStack(FI);
1822 // The token was already consumed, so use return here instead of break.
1823 return false;
1824 }
Matthias Braun3ef7df92016-06-08 00:47:07 +00001825 case MIToken::StackObject: {
1826 int FI;
1827 if (parseStackFrameIndex(FI))
1828 return true;
1829 PSV = MF.getPSVManager().getFixedStack(FI);
1830 // The token was already consumed, so use return here instead of break.
1831 return false;
1832 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001833 case MIToken::kw_call_entry: {
1834 lex();
1835 switch (Token.kind()) {
1836 case MIToken::GlobalValue:
1837 case MIToken::NamedGlobalValue: {
1838 GlobalValue *GV = nullptr;
1839 if (parseGlobalValue(GV))
1840 return true;
1841 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1842 break;
1843 }
1844 case MIToken::ExternalSymbol:
1845 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1846 MF.createExternalSymbolName(Token.stringValue()));
1847 break;
1848 default:
1849 return error(
1850 "expected a global value or an external symbol after 'call-entry'");
1851 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001852 break;
1853 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001854 default:
1855 llvm_unreachable("The current token should be pseudo source value");
1856 }
1857 lex();
1858 return false;
1859}
1860
1861bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001862 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001863 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Matthias Braun3ef7df92016-06-08 00:47:07 +00001864 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
1865 Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001866 const PseudoSourceValue *PSV = nullptr;
1867 if (parseMemoryPseudoSourceValue(PSV))
1868 return true;
1869 int64_t Offset = 0;
1870 if (parseOffset(Offset))
1871 return true;
1872 Dest = MachinePointerInfo(PSV, Offset);
1873 return false;
1874 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001875 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1876 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001877 Token.isNot(MIToken::NamedGlobalValue) &&
1878 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001879 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001880 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001881 if (parseIRValue(V))
1882 return true;
1883 if (!V->getType()->isPointerTy())
1884 return error("expected a pointer IR value");
1885 lex();
1886 int64_t Offset = 0;
1887 if (parseOffset(Offset))
1888 return true;
1889 Dest = MachinePointerInfo(V, Offset);
1890 return false;
1891}
1892
Alex Lorenz4af7e612015-08-03 23:08:19 +00001893bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1894 if (expectAndConsume(MIToken::lparen))
1895 return true;
Justin Lebar0af80cd2016-07-15 18:26:59 +00001896 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
Alex Lorenza518b792015-08-04 00:24:45 +00001897 while (Token.isMemoryOperandFlag()) {
1898 if (parseMemoryOperandFlag(Flags))
1899 return true;
1900 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001901 if (Token.isNot(MIToken::Identifier) ||
1902 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1903 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001904 if (Token.stringValue() == "load")
1905 Flags |= MachineMemOperand::MOLoad;
1906 else
1907 Flags |= MachineMemOperand::MOStore;
1908 lex();
1909
1910 if (Token.isNot(MIToken::IntegerLiteral))
1911 return error("expected the size integer literal after memory operation");
1912 uint64_t Size;
1913 if (getUint64(Size))
1914 return true;
1915 lex();
1916
Alex Lorenz91097a32015-08-12 20:33:26 +00001917 MachinePointerInfo Ptr = MachinePointerInfo();
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001918 if (Token.is(MIToken::Identifier)) {
1919 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1920 if (Token.stringValue() != Word)
1921 return error(Twine("expected '") + Word + "'");
1922 lex();
1923
1924 if (parseMachinePointerInfo(Ptr))
1925 return true;
1926 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001927 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001928 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001929 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001930 while (consumeIfPresent(MIToken::comma)) {
1931 switch (Token.kind()) {
1932 case MIToken::kw_align:
1933 if (parseAlignment(BaseAlignment))
1934 return true;
1935 break;
1936 case MIToken::md_tbaa:
1937 lex();
1938 if (parseMDNode(AAInfo.TBAA))
1939 return true;
1940 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001941 case MIToken::md_alias_scope:
1942 lex();
1943 if (parseMDNode(AAInfo.Scope))
1944 return true;
1945 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001946 case MIToken::md_noalias:
1947 lex();
1948 if (parseMDNode(AAInfo.NoAlias))
1949 return true;
1950 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001951 case MIToken::md_range:
1952 lex();
1953 if (parseMDNode(Range))
1954 return true;
1955 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001956 // TODO: Report an error on duplicate metadata nodes.
1957 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001958 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1959 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001960 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001961 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001962 if (expectAndConsume(MIToken::rparen))
1963 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001964 Dest =
1965 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001966 return false;
1967}
1968
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001969void MIParser::initNames2InstrOpCodes() {
1970 if (!Names2InstrOpCodes.empty())
1971 return;
1972 const auto *TII = MF.getSubtarget().getInstrInfo();
1973 assert(TII && "Expected target instruction info");
1974 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1975 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1976}
1977
1978bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1979 initNames2InstrOpCodes();
1980 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1981 if (InstrInfo == Names2InstrOpCodes.end())
1982 return true;
1983 OpCode = InstrInfo->getValue();
1984 return false;
1985}
1986
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001987void MIParser::initNames2Regs() {
1988 if (!Names2Regs.empty())
1989 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001990 // The '%noreg' register is the register 0.
1991 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001992 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1993 assert(TRI && "Expected target register info");
1994 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1995 bool WasInserted =
1996 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1997 .second;
1998 (void)WasInserted;
1999 assert(WasInserted && "Expected registers to be unique case-insensitively");
2000 }
2001}
2002
2003bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
2004 initNames2Regs();
2005 auto RegInfo = Names2Regs.find(RegName);
2006 if (RegInfo == Names2Regs.end())
2007 return true;
2008 Reg = RegInfo->getValue();
2009 return false;
2010}
2011
Alex Lorenz8f6f4282015-06-29 16:57:06 +00002012void MIParser::initNames2RegMasks() {
2013 if (!Names2RegMasks.empty())
2014 return;
2015 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2016 assert(TRI && "Expected target register info");
2017 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
2018 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
2019 assert(RegMasks.size() == RegMaskNames.size());
2020 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
2021 Names2RegMasks.insert(
2022 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
2023}
2024
2025const uint32_t *MIParser::getRegMask(StringRef Identifier) {
2026 initNames2RegMasks();
2027 auto RegMaskInfo = Names2RegMasks.find(Identifier);
2028 if (RegMaskInfo == Names2RegMasks.end())
2029 return nullptr;
2030 return RegMaskInfo->getValue();
2031}
2032
Alex Lorenz2eacca82015-07-13 23:24:34 +00002033void MIParser::initNames2SubRegIndices() {
2034 if (!Names2SubRegIndices.empty())
2035 return;
2036 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2037 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
2038 Names2SubRegIndices.insert(
2039 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
2040}
2041
2042unsigned MIParser::getSubRegIndex(StringRef Name) {
2043 initNames2SubRegIndices();
2044 auto SubRegInfo = Names2SubRegIndices.find(Name);
2045 if (SubRegInfo == Names2SubRegIndices.end())
2046 return 0;
2047 return SubRegInfo->getValue();
2048}
2049
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002050static void initSlots2BasicBlocks(
2051 const Function &F,
2052 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2053 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002054 MST.incorporateFunction(F);
2055 for (auto &BB : F) {
2056 if (BB.hasName())
2057 continue;
2058 int Slot = MST.getLocalSlot(&BB);
2059 if (Slot == -1)
2060 continue;
2061 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2062 }
2063}
2064
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002065static const BasicBlock *getIRBlockFromSlot(
2066 unsigned Slot,
2067 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002068 auto BlockInfo = Slots2BasicBlocks.find(Slot);
2069 if (BlockInfo == Slots2BasicBlocks.end())
2070 return nullptr;
2071 return BlockInfo->second;
2072}
2073
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002074const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2075 if (Slots2BasicBlocks.empty())
2076 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
2077 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2078}
2079
2080const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2081 if (&F == MF.getFunction())
2082 return getIRBlock(Slot);
2083 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2084 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2085 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2086}
2087
Alex Lorenzdd13be02015-08-19 23:31:05 +00002088static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2089 DenseMap<unsigned, const Value *> &Slots2Values) {
2090 int Slot = MST.getLocalSlot(V);
2091 if (Slot == -1)
2092 return;
2093 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2094}
2095
2096/// Creates the mapping from slot numbers to function's unnamed IR values.
2097static void initSlots2Values(const Function &F,
2098 DenseMap<unsigned, const Value *> &Slots2Values) {
2099 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2100 MST.incorporateFunction(F);
2101 for (const auto &Arg : F.args())
2102 mapValueToSlot(&Arg, MST, Slots2Values);
2103 for (const auto &BB : F) {
2104 mapValueToSlot(&BB, MST, Slots2Values);
2105 for (const auto &I : BB)
2106 mapValueToSlot(&I, MST, Slots2Values);
2107 }
2108}
2109
2110const Value *MIParser::getIRValue(unsigned Slot) {
2111 if (Slots2Values.empty())
2112 initSlots2Values(*MF.getFunction(), Slots2Values);
2113 auto ValueInfo = Slots2Values.find(Slot);
2114 if (ValueInfo == Slots2Values.end())
2115 return nullptr;
2116 return ValueInfo->second;
2117}
2118
Alex Lorenzef5c1962015-07-28 23:02:45 +00002119void MIParser::initNames2TargetIndices() {
2120 if (!Names2TargetIndices.empty())
2121 return;
2122 const auto *TII = MF.getSubtarget().getInstrInfo();
2123 assert(TII && "Expected target instruction info");
2124 auto Indices = TII->getSerializableTargetIndices();
2125 for (const auto &I : Indices)
2126 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2127}
2128
2129bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2130 initNames2TargetIndices();
2131 auto IndexInfo = Names2TargetIndices.find(Name);
2132 if (IndexInfo == Names2TargetIndices.end())
2133 return true;
2134 Index = IndexInfo->second;
2135 return false;
2136}
2137
Alex Lorenz49873a82015-08-06 00:44:07 +00002138void MIParser::initNames2DirectTargetFlags() {
2139 if (!Names2DirectTargetFlags.empty())
2140 return;
2141 const auto *TII = MF.getSubtarget().getInstrInfo();
2142 assert(TII && "Expected target instruction info");
2143 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2144 for (const auto &I : Flags)
2145 Names2DirectTargetFlags.insert(
2146 std::make_pair(StringRef(I.second), I.first));
2147}
2148
2149bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2150 initNames2DirectTargetFlags();
2151 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2152 if (FlagInfo == Names2DirectTargetFlags.end())
2153 return true;
2154 Flag = FlagInfo->second;
2155 return false;
2156}
2157
Alex Lorenzf3630112015-08-18 22:52:15 +00002158void MIParser::initNames2BitmaskTargetFlags() {
2159 if (!Names2BitmaskTargetFlags.empty())
2160 return;
2161 const auto *TII = MF.getSubtarget().getInstrInfo();
2162 assert(TII && "Expected target instruction info");
2163 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2164 for (const auto &I : Flags)
2165 Names2BitmaskTargetFlags.insert(
2166 std::make_pair(StringRef(I.second), I.first));
2167}
2168
2169bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2170 initNames2BitmaskTargetFlags();
2171 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2172 if (FlagInfo == Names2BitmaskTargetFlags.end())
2173 return true;
2174 Flag = FlagInfo->second;
2175 return false;
2176}
2177
Matthias Braun83947862016-07-13 22:23:23 +00002178bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2179 StringRef Src,
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002180 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002181 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002182}
2183
Matthias Braun83947862016-07-13 22:23:23 +00002184bool llvm::parseMachineInstructions(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002185 StringRef Src, SMDiagnostic &Error) {
2186 return MIParser(PFS, Error, Src).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002187}
Alex Lorenzf09df002015-06-30 18:16:42 +00002188
Matthias Braun83947862016-07-13 22:23:23 +00002189bool llvm::parseMBBReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002190 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +00002191 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002192 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002193}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002194
Matthias Braun83947862016-07-13 22:23:23 +00002195bool llvm::parseNamedRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002196 unsigned &Reg, StringRef Src,
Alex Lorenz9fab3702015-07-14 21:24:41 +00002197 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002198 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002199}
Alex Lorenz12045a42015-07-27 17:42:45 +00002200
Matthias Braun83947862016-07-13 22:23:23 +00002201bool llvm::parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002202 unsigned &Reg, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +00002203 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002204 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +00002205}
Alex Lorenza314d812015-08-18 22:26:26 +00002206
Matthias Braun83947862016-07-13 22:23:23 +00002207bool llvm::parseStackObjectReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002208 int &FI, StringRef Src,
Alex Lorenza314d812015-08-18 22:26:26 +00002209 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002210 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
Alex Lorenza314d812015-08-18 22:26:26 +00002211}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002212
Matthias Braun83947862016-07-13 22:23:23 +00002213bool llvm::parseMDNode(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002214 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2215 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002216}