blob: 2f4410ba7cd5d82f2836f8abf0ff030ab56973bd [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"
Reid Klecknera5b1eef2016-08-26 17:58:37 +000017#include "llvm/ADT/StringSwitch.h"
Alex Lorenzad156fb2015-07-31 20:49:21 +000018#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000019#include "llvm/AsmParser/SlotMapping.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000020#include "llvm/CodeGen/MachineBasicBlock.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000022#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000023#include "llvm/CodeGen/MachineInstr.h"
Alex Lorenzcb268d42015-07-06 23:07:26 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000025#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000028#include "llvm/IR/Constants.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000029#include "llvm/IR/Instructions.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000030#include "llvm/IR/Intrinsics.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000031#include "llvm/IR/Module.h"
Alex Lorenz8a1915b2015-07-27 22:42:41 +000032#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000033#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000034#include "llvm/Support/SourceMgr.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000035#include "llvm/Support/raw_ostream.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000036#include "llvm/Target/TargetInstrInfo.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000037#include "llvm/Target/TargetIntrinsicInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000038#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000039
40using namespace llvm;
41
Matthias Braune35861d2016-07-13 23:27:50 +000042PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
43 SourceMgr &SM, const SlotMapping &IRSlots)
44 : MF(MF), SM(&SM), IRSlots(IRSlots) {
Matthias Braun83947862016-07-13 22:23:23 +000045}
46
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000047namespace {
48
Alex Lorenz36962cd2015-07-07 02:08:46 +000049/// A wrapper struct around the 'MachineOperand' struct that includes a source
Alex Lorenzfeb6b432015-08-19 19:19:16 +000050/// range and other attributes.
51struct ParsedMachineOperand {
Alex Lorenz36962cd2015-07-07 02:08:46 +000052 MachineOperand Operand;
53 StringRef::iterator Begin;
54 StringRef::iterator End;
Alex Lorenz5ef93b02015-08-19 19:05:34 +000055 Optional<unsigned> TiedDefIdx;
Alex Lorenz36962cd2015-07-07 02:08:46 +000056
Alex Lorenzfeb6b432015-08-19 19:19:16 +000057 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
58 StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
Alex Lorenz5ef93b02015-08-19 19:05:34 +000059 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
60 if (TiedDefIdx)
61 assert(Operand.isReg() && Operand.isUse() &&
62 "Only used register operands can be tied");
63 }
Alex Lorenz36962cd2015-07-07 02:08:46 +000064};
65
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000066class MIParser {
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000067 MachineFunction &MF;
68 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000069 StringRef Source, CurrentSource;
70 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000071 const PerFunctionMIParsingState &PFS;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000072 /// Maps from instruction names to op codes.
73 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000074 /// Maps from register names to registers.
75 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000076 /// Maps from register mask names to register masks.
77 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000078 /// Maps from subregister names to subregister indices.
79 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000080 /// Maps from slot numbers to function's unnamed basic blocks.
81 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzdd13be02015-08-19 23:31:05 +000082 /// Maps from slot numbers to function's unnamed values.
83 DenseMap<unsigned, const Value *> Slots2Values;
Alex Lorenzef5c1962015-07-28 23:02:45 +000084 /// Maps from target index names to target indices.
85 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +000086 /// Maps from direct target flag names to the direct target flag values.
87 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenzf3630112015-08-18 22:52:15 +000088 /// Maps from direct target flag names to the bitmask target flag values.
89 StringMap<unsigned> Names2BitmaskTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000090
91public:
Matthias Braune35861d2016-07-13 23:27:50 +000092 MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
93 StringRef Source);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000094
Quentin Colombet287c6bb2016-03-08 00:57:31 +000095 /// \p SkipChar gives the number of characters to skip before looking
96 /// for the next token.
97 void lex(unsigned SkipChar = 0);
Alex Lorenz91370c52015-06-22 20:37:46 +000098
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000099 /// Report an error at the current location with the given message.
100 ///
101 /// This function always return true.
102 bool error(const Twine &Msg);
103
Alex Lorenz91370c52015-06-22 20:37:46 +0000104 /// Report an error at the given location with the given message.
105 ///
106 /// This function always return true.
107 bool error(StringRef::iterator Loc, const Twine &Msg);
108
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000109 bool
110 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
111 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +0000112 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +0000113 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
114 bool parseStandaloneNamedRegister(unsigned &Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +0000115 bool parseStandaloneVirtualRegister(unsigned &Reg);
Alex Lorenza314d812015-08-18 22:26:26 +0000116 bool parseStandaloneStackObject(int &FI);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000117 bool parseStandaloneMDNode(MDNode *&Node);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000118
119 bool
120 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
121 bool parseBasicBlock(MachineBasicBlock &MBB);
122 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
123 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000124
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000125 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000126 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000127 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000128 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000129 bool parseSize(unsigned &Size);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000130 bool parseRegisterOperand(MachineOperand &Dest,
131 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000132 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000133 bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
134 const Constant *&C);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000135 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Ahmed Bougachad760de02016-07-28 17:15:12 +0000136 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
Alex Lorenz05e38822015-08-05 18:52:21 +0000137 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000138 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000139 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000140 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000141 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000142 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000143 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000144 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000145 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000146 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000147 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +0000148 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000149 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000150 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000151 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000152 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000153 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000154 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000155 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000156 bool parseIRBlock(BasicBlock *&BB, const Function &F);
157 bool parseBlockAddressOperand(MachineOperand &Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +0000158 bool parseIntrinsicOperand(MachineOperand &Dest);
Tim Northoverde3aea0412016-08-17 20:25:25 +0000159 bool parsePredicateOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000160 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000161 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000162 bool parseMachineOperand(MachineOperand &Dest,
163 Optional<unsigned> &TiedDefIdx);
164 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
165 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000166 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000167 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000168 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000169 bool parseIRValue(const Value *&V);
Justin Lebar0af80cd2016-07-15 18:26:59 +0000170 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000171 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
172 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000173 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000174
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000175private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000176 /// Convert the integer literal in the current token into an unsigned integer.
177 ///
178 /// Return true if an error occurred.
179 bool getUnsigned(unsigned &Result);
180
Alex Lorenz4af7e612015-08-03 23:08:19 +0000181 /// Convert the integer literal in the current token into an uint64.
182 ///
183 /// Return true if an error occurred.
184 bool getUint64(uint64_t &Result);
185
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000186 /// If the current token is of the given kind, consume it and return false.
187 /// Otherwise report an error and return true.
188 bool expectAndConsume(MIToken::TokenKind TokenKind);
189
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000190 /// If the current token is of the given kind, consume it and return true.
191 /// Otherwise return false.
192 bool consumeIfPresent(MIToken::TokenKind TokenKind);
193
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000194 void initNames2InstrOpCodes();
195
196 /// Try to convert an instruction name to an opcode. Return true if the
197 /// instruction name is invalid.
198 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000199
Alex Lorenze5a44662015-07-17 00:24:15 +0000200 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000201
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000202 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000203 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000204
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000205 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000206 const MCInstrDesc &MCID);
207
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000208 void initNames2Regs();
209
210 /// Try to convert a register name to a register number. Return true if the
211 /// register name is invalid.
212 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000213
214 void initNames2RegMasks();
215
216 /// Check if the given identifier is a name of a register mask.
217 ///
218 /// Return null if the identifier isn't a register mask.
219 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000220
221 void initNames2SubRegIndices();
222
223 /// Check if the given identifier is a name of a subregister index.
224 ///
225 /// Return 0 if the name isn't a subregister index class.
226 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000227
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000228 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000229 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000230
Alex Lorenzdd13be02015-08-19 23:31:05 +0000231 const Value *getIRValue(unsigned Slot);
232
Alex Lorenzef5c1962015-07-28 23:02:45 +0000233 void initNames2TargetIndices();
234
235 /// Try to convert a name of target index to the corresponding target index.
236 ///
237 /// Return true if the name isn't a name of a target index.
238 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000239
240 void initNames2DirectTargetFlags();
241
242 /// Try to convert a name of a direct target flag to the corresponding
243 /// target flag.
244 ///
245 /// Return true if the name isn't a name of a direct flag.
246 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000247
248 void initNames2BitmaskTargetFlags();
249
250 /// Try to convert a name of a bitmask target flag to the corresponding
251 /// target flag.
252 ///
253 /// Return true if the name isn't a name of a bitmask target flag.
254 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000255};
256
257} // end anonymous namespace
258
Matthias Braune35861d2016-07-13 23:27:50 +0000259MIParser::MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
260 StringRef Source)
261 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
262{}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000263
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000264void MIParser::lex(unsigned SkipChar) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000265 CurrentSource = lexMIToken(
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000266 CurrentSource.data() + SkipChar, Token,
Alex Lorenz91370c52015-06-22 20:37:46 +0000267 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
268}
269
270bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
271
272bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Matthias Braune35861d2016-07-13 23:27:50 +0000273 const SourceMgr &SM = *PFS.SM;
Alex Lorenz91370c52015-06-22 20:37:46 +0000274 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000275 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
276 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
277 // Create an ordinary diagnostic when the source manager's buffer is the
278 // source string.
279 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
280 return true;
281 }
282 // Create a diagnostic for a YAML string literal.
283 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
284 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
285 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000286 return true;
287}
288
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000289static const char *toString(MIToken::TokenKind TokenKind) {
290 switch (TokenKind) {
291 case MIToken::comma:
292 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000293 case MIToken::equal:
294 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000295 case MIToken::colon:
296 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000297 case MIToken::lparen:
298 return "'('";
299 case MIToken::rparen:
300 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000301 default:
302 return "<unknown token>";
303 }
304}
305
306bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
307 if (Token.isNot(TokenKind))
308 return error(Twine("expected ") + toString(TokenKind));
309 lex();
310 return false;
311}
312
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000313bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
314 if (Token.isNot(TokenKind))
315 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000316 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000317 return true;
318}
Alex Lorenz91370c52015-06-22 20:37:46 +0000319
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000320bool MIParser::parseBasicBlockDefinition(
321 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
322 assert(Token.is(MIToken::MachineBasicBlockLabel));
323 unsigned ID = 0;
324 if (getUnsigned(ID))
325 return true;
326 auto Loc = Token.location();
327 auto Name = Token.stringValue();
328 lex();
329 bool HasAddressTaken = false;
330 bool IsLandingPad = false;
331 unsigned Alignment = 0;
332 BasicBlock *BB = nullptr;
333 if (consumeIfPresent(MIToken::lparen)) {
334 do {
335 // TODO: Report an error when multiple same attributes are specified.
336 switch (Token.kind()) {
337 case MIToken::kw_address_taken:
338 HasAddressTaken = true;
339 lex();
340 break;
341 case MIToken::kw_landing_pad:
342 IsLandingPad = true;
343 lex();
344 break;
345 case MIToken::kw_align:
346 if (parseAlignment(Alignment))
347 return true;
348 break;
349 case MIToken::IRBlock:
350 // TODO: Report an error when both name and ir block are specified.
351 if (parseIRBlock(BB, *MF.getFunction()))
352 return true;
353 lex();
354 break;
355 default:
356 break;
357 }
358 } while (consumeIfPresent(MIToken::comma));
359 if (expectAndConsume(MIToken::rparen))
360 return true;
361 }
362 if (expectAndConsume(MIToken::colon))
363 return true;
364
365 if (!Name.empty()) {
366 BB = dyn_cast_or_null<BasicBlock>(
367 MF.getFunction()->getValueSymbolTable().lookup(Name));
368 if (!BB)
369 return error(Loc, Twine("basic block '") + Name +
370 "' is not defined in the function '" +
371 MF.getName() + "'");
372 }
373 auto *MBB = MF.CreateMachineBasicBlock(BB);
374 MF.insert(MF.end(), MBB);
375 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
376 if (!WasInserted)
377 return error(Loc, Twine("redefinition of machine basic block with id #") +
378 Twine(ID));
379 if (Alignment)
380 MBB->setAlignment(Alignment);
381 if (HasAddressTaken)
382 MBB->setHasAddressTaken();
Reid Kleckner0e288232015-08-27 23:27:47 +0000383 MBB->setIsEHPad(IsLandingPad);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000384 return false;
385}
386
387bool MIParser::parseBasicBlockDefinitions(
388 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
389 lex();
390 // Skip until the first machine basic block.
391 while (Token.is(MIToken::Newline))
392 lex();
393 if (Token.isErrorOrEOF())
394 return Token.isError();
395 if (Token.isNot(MIToken::MachineBasicBlockLabel))
396 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000397 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000398 do {
399 if (parseBasicBlockDefinition(MBBSlots))
400 return true;
401 bool IsAfterNewline = false;
402 // Skip until the next machine basic block.
403 while (true) {
404 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
405 Token.isErrorOrEOF())
406 break;
407 else if (Token.is(MIToken::MachineBasicBlockLabel))
408 return error("basic block definition should be located at the start of "
409 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000410 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000411 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000412 continue;
413 }
414 IsAfterNewline = false;
415 if (Token.is(MIToken::lbrace))
416 ++BraceDepth;
417 if (Token.is(MIToken::rbrace)) {
418 if (!BraceDepth)
419 return error("extraneous closing brace ('}')");
420 --BraceDepth;
421 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000422 lex();
423 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000424 // Verify that we closed all of the '{' at the end of a file or a block.
425 if (!Token.isError() && BraceDepth)
426 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000427 } while (!Token.isErrorOrEOF());
428 return Token.isError();
429}
430
431bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
432 assert(Token.is(MIToken::kw_liveins));
433 lex();
434 if (expectAndConsume(MIToken::colon))
435 return true;
436 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
437 return false;
438 do {
439 if (Token.isNot(MIToken::NamedRegister))
440 return error("expected a named register");
441 unsigned Reg = 0;
442 if (parseRegister(Reg))
443 return true;
444 MBB.addLiveIn(Reg);
445 lex();
446 } while (consumeIfPresent(MIToken::comma));
447 return false;
448}
449
450bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
451 assert(Token.is(MIToken::kw_successors));
452 lex();
453 if (expectAndConsume(MIToken::colon))
454 return true;
455 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
456 return false;
457 do {
458 if (Token.isNot(MIToken::MachineBasicBlock))
459 return error("expected a machine basic block reference");
460 MachineBasicBlock *SuccMBB = nullptr;
461 if (parseMBBReference(SuccMBB))
462 return true;
463 lex();
464 unsigned Weight = 0;
465 if (consumeIfPresent(MIToken::lparen)) {
466 if (Token.isNot(MIToken::IntegerLiteral))
467 return error("expected an integer literal after '('");
468 if (getUnsigned(Weight))
469 return true;
470 lex();
471 if (expectAndConsume(MIToken::rparen))
472 return true;
473 }
Cong Houd97c1002015-12-01 05:29:22 +0000474 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000475 } while (consumeIfPresent(MIToken::comma));
Cong Houd97c1002015-12-01 05:29:22 +0000476 MBB.normalizeSuccProbs();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000477 return false;
478}
479
480bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
481 // Skip the definition.
482 assert(Token.is(MIToken::MachineBasicBlockLabel));
483 lex();
484 if (consumeIfPresent(MIToken::lparen)) {
485 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
486 lex();
487 consumeIfPresent(MIToken::rparen);
488 }
489 consumeIfPresent(MIToken::colon);
490
491 // Parse the liveins and successors.
492 // N.B: Multiple lists of successors and liveins are allowed and they're
493 // merged into one.
494 // Example:
495 // liveins: %edi
496 // liveins: %esi
497 //
498 // is equivalent to
499 // liveins: %edi, %esi
500 while (true) {
501 if (Token.is(MIToken::kw_successors)) {
502 if (parseBasicBlockSuccessors(MBB))
503 return true;
504 } else if (Token.is(MIToken::kw_liveins)) {
505 if (parseBasicBlockLiveins(MBB))
506 return true;
507 } else if (consumeIfPresent(MIToken::Newline)) {
508 continue;
509 } else
510 break;
511 if (!Token.isNewlineOrEOF())
512 return error("expected line break at the end of a list");
513 lex();
514 }
515
516 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000517 bool IsInBundle = false;
518 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000519 while (true) {
520 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
521 return false;
522 else if (consumeIfPresent(MIToken::Newline))
523 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000524 if (consumeIfPresent(MIToken::rbrace)) {
525 // The first parsing pass should verify that all closing '}' have an
526 // opening '{'.
527 assert(IsInBundle);
528 IsInBundle = false;
529 continue;
530 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000531 MachineInstr *MI = nullptr;
532 if (parse(MI))
533 return true;
534 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000535 if (IsInBundle) {
536 PrevMI->setFlag(MachineInstr::BundledSucc);
537 MI->setFlag(MachineInstr::BundledPred);
538 }
539 PrevMI = MI;
540 if (Token.is(MIToken::lbrace)) {
541 if (IsInBundle)
542 return error("nested instruction bundles are not allowed");
543 lex();
544 // This instruction is the start of the bundle.
545 MI->setFlag(MachineInstr::BundledSucc);
546 IsInBundle = true;
547 if (!Token.is(MIToken::Newline))
548 // The next instruction can be on the same line.
549 continue;
550 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000551 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
552 lex();
553 }
554 return false;
555}
556
557bool MIParser::parseBasicBlocks() {
558 lex();
559 // Skip until the first machine basic block.
560 while (Token.is(MIToken::Newline))
561 lex();
562 if (Token.isErrorOrEOF())
563 return Token.isError();
564 // The first parsing pass should have verified that this token is a MBB label
565 // in the 'parseBasicBlockDefinitions' method.
566 assert(Token.is(MIToken::MachineBasicBlockLabel));
567 do {
568 MachineBasicBlock *MBB = nullptr;
569 if (parseMBBReference(MBB))
570 return true;
571 if (parseBasicBlock(*MBB))
572 return true;
573 // The method 'parseBasicBlock' should parse the whole block until the next
574 // block or the end of file.
575 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
576 } while (Token.isNot(MIToken::Eof));
577 return false;
578}
579
580bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000581 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000582 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000583 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000584 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000585 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000586 Optional<unsigned> TiedDefIdx;
587 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000588 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000589 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000590 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000591 if (Token.isNot(MIToken::comma))
592 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000593 lex();
594 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000595 if (!Operands.empty() && expectAndConsume(MIToken::equal))
596 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000597
Alex Lorenze5a44662015-07-17 00:24:15 +0000598 unsigned OpCode, Flags = 0;
599 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000600 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000601
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000602 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000603 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000604 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000605 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000606 Optional<unsigned> TiedDefIdx;
607 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000608 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000609 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000610 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000611 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
612 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000613 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000614 if (Token.isNot(MIToken::comma))
615 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000616 lex();
617 }
618
Alex Lorenz46d760d2015-07-22 21:15:11 +0000619 DebugLoc DebugLocation;
620 if (Token.is(MIToken::kw_debug_location)) {
621 lex();
622 if (Token.isNot(MIToken::exclaim))
623 return error("expected a metadata node after 'debug-location'");
624 MDNode *Node = nullptr;
625 if (parseMDNode(Node))
626 return true;
627 DebugLocation = DebugLoc(Node);
628 }
629
Alex Lorenz4af7e612015-08-03 23:08:19 +0000630 // Parse the machine memory operands.
631 SmallVector<MachineMemOperand *, 2> MemOperands;
632 if (Token.is(MIToken::coloncolon)) {
633 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000634 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000635 MachineMemOperand *MemOp = nullptr;
636 if (parseMachineMemoryOperand(MemOp))
637 return true;
638 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000639 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000640 break;
641 if (Token.isNot(MIToken::comma))
642 return error("expected ',' before the next machine memory operand");
643 lex();
644 }
645 }
646
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000647 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000648 if (!MCID.isVariadic()) {
649 // FIXME: Move the implicit operand verification to the machine verifier.
650 if (verifyImplicitOperands(Operands, MCID))
651 return true;
652 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000653
Alex Lorenzcb268d42015-07-06 23:07:26 +0000654 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000655 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000656 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000657 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000658 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000659 if (assignRegisterTies(*MI, Operands))
660 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000661 if (MemOperands.empty())
662 return false;
663 MachineInstr::mmo_iterator MemRefs =
664 MF.allocateMemRefsArray(MemOperands.size());
665 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
666 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000667 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000668}
669
Alex Lorenz1ea60892015-07-27 20:29:27 +0000670bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000671 lex();
672 if (Token.isNot(MIToken::MachineBasicBlock))
673 return error("expected a machine basic block reference");
674 if (parseMBBReference(MBB))
675 return true;
676 lex();
677 if (Token.isNot(MIToken::Eof))
678 return error(
679 "expected end of string after the machine basic block reference");
680 return false;
681}
682
Alex Lorenz1ea60892015-07-27 20:29:27 +0000683bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000684 lex();
685 if (Token.isNot(MIToken::NamedRegister))
686 return error("expected a named register");
687 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000688 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000689 lex();
690 if (Token.isNot(MIToken::Eof))
691 return error("expected end of string after the register reference");
692 return false;
693}
694
Alex Lorenz12045a42015-07-27 17:42:45 +0000695bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
696 lex();
697 if (Token.isNot(MIToken::VirtualRegister))
698 return error("expected a virtual register");
699 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000700 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000701 lex();
702 if (Token.isNot(MIToken::Eof))
703 return error("expected end of string after the register reference");
704 return false;
705}
706
Alex Lorenza314d812015-08-18 22:26:26 +0000707bool MIParser::parseStandaloneStackObject(int &FI) {
708 lex();
709 if (Token.isNot(MIToken::StackObject))
710 return error("expected a stack object");
711 if (parseStackFrameIndex(FI))
712 return true;
713 if (Token.isNot(MIToken::Eof))
714 return error("expected end of string after the stack object reference");
715 return false;
716}
717
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000718bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
719 lex();
720 if (Token.isNot(MIToken::exclaim))
721 return error("expected a metadata node");
722 if (parseMDNode(Node))
723 return true;
724 if (Token.isNot(MIToken::Eof))
725 return error("expected end of string after the metadata node");
726 return false;
727}
728
Alex Lorenz36962cd2015-07-07 02:08:46 +0000729static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
730 assert(MO.isImplicit());
731 return MO.isDef() ? "implicit-def" : "implicit";
732}
733
734static std::string getRegisterName(const TargetRegisterInfo *TRI,
735 unsigned Reg) {
736 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
737 return StringRef(TRI->getName(Reg)).lower();
738}
739
Alex Lorenz0153e592015-09-10 14:04:34 +0000740/// Return true if the parsed machine operands contain a given machine operand.
741static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
742 ArrayRef<ParsedMachineOperand> Operands) {
743 for (const auto &I : Operands) {
744 if (ImplicitOperand.isIdenticalTo(I.Operand))
745 return true;
746 }
747 return false;
748}
749
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000750bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
751 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000752 if (MCID.isCall())
753 // We can't verify call instructions as they can contain arbitrary implicit
754 // register and register mask operands.
755 return false;
756
757 // Gather all the expected implicit operands.
758 SmallVector<MachineOperand, 4> ImplicitOperands;
759 if (MCID.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000760 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000761 ImplicitOperands.push_back(
762 MachineOperand::CreateReg(*ImpDefs, true, true));
763 if (MCID.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000764 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000765 ImplicitOperands.push_back(
766 MachineOperand::CreateReg(*ImpUses, false, true));
767
768 const auto *TRI = MF.getSubtarget().getRegisterInfo();
769 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000770 for (const auto &I : ImplicitOperands) {
771 if (isImplicitOperandIn(I, Operands))
772 continue;
773 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000774 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000775 printImplicitRegisterFlag(I) + " %" +
776 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000777 }
778 return false;
779}
780
Alex Lorenze5a44662015-07-17 00:24:15 +0000781bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
782 if (Token.is(MIToken::kw_frame_setup)) {
783 Flags |= MachineInstr::FrameSetup;
784 lex();
785 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000786 if (Token.isNot(MIToken::Identifier))
787 return error("expected a machine instruction");
788 StringRef InstrName = Token.stringValue();
789 if (parseInstrName(InstrName, OpCode))
790 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000791 lex();
792 return false;
793}
794
795bool MIParser::parseRegister(unsigned &Reg) {
796 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000797 case MIToken::underscore:
798 Reg = 0;
799 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000800 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000801 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000802 if (getRegisterByName(Name, Reg))
803 return error(Twine("unknown register name '") + Name + "'");
804 break;
805 }
Alex Lorenz53464512015-07-10 22:51:20 +0000806 case MIToken::VirtualRegister: {
807 unsigned ID;
808 if (getUnsigned(ID))
809 return true;
810 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
811 if (RegInfo == PFS.VirtualRegisterSlots.end())
812 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
813 "'");
814 Reg = RegInfo->second;
815 break;
816 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000817 // TODO: Parse other register kinds.
818 default:
819 llvm_unreachable("The current token should be a register");
820 }
821 return false;
822}
823
Alex Lorenzcb268d42015-07-06 23:07:26 +0000824bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000825 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000826 switch (Token.kind()) {
827 case MIToken::kw_implicit:
828 Flags |= RegState::Implicit;
829 break;
830 case MIToken::kw_implicit_define:
831 Flags |= RegState::ImplicitDefine;
832 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000833 case MIToken::kw_def:
834 Flags |= RegState::Define;
835 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000836 case MIToken::kw_dead:
837 Flags |= RegState::Dead;
838 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000839 case MIToken::kw_killed:
840 Flags |= RegState::Kill;
841 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000842 case MIToken::kw_undef:
843 Flags |= RegState::Undef;
844 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000845 case MIToken::kw_internal:
846 Flags |= RegState::InternalRead;
847 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000848 case MIToken::kw_early_clobber:
849 Flags |= RegState::EarlyClobber;
850 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000851 case MIToken::kw_debug_use:
852 Flags |= RegState::Debug;
853 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000854 default:
855 llvm_unreachable("The current token should be a register flag");
856 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000857 if (OldFlags == Flags)
858 // We know that the same flag is specified more than once when the flags
859 // weren't modified.
860 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000861 lex();
862 return false;
863}
864
Alex Lorenz2eacca82015-07-13 23:24:34 +0000865bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
Matthias Braun333e4682016-07-26 21:49:34 +0000866 assert(Token.is(MIToken::dot));
Alex Lorenz2eacca82015-07-13 23:24:34 +0000867 lex();
868 if (Token.isNot(MIToken::Identifier))
Matthias Braun333e4682016-07-26 21:49:34 +0000869 return error("expected a subregister index after '.'");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000870 auto Name = Token.stringValue();
871 SubReg = getSubRegIndex(Name);
872 if (!SubReg)
873 return error(Twine("use of unknown subregister index '") + Name + "'");
874 lex();
875 return false;
876}
877
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000878bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
879 if (!consumeIfPresent(MIToken::kw_tied_def))
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000880 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000881 if (Token.isNot(MIToken::IntegerLiteral))
882 return error("expected an integer literal after 'tied-def'");
883 if (getUnsigned(TiedDefIdx))
884 return true;
885 lex();
886 if (expectAndConsume(MIToken::rparen))
887 return true;
888 return false;
889}
890
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000891bool MIParser::parseSize(unsigned &Size) {
892 if (Token.isNot(MIToken::IntegerLiteral))
893 return error("expected an integer literal for the size");
894 if (getUnsigned(Size))
895 return true;
896 lex();
897 if (expectAndConsume(MIToken::rparen))
898 return true;
899 return false;
900}
901
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000902bool MIParser::assignRegisterTies(MachineInstr &MI,
903 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000904 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
905 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
906 if (!Operands[I].TiedDefIdx)
907 continue;
908 // The parser ensures that this operand is a register use, so we just have
909 // to check the tied-def operand.
910 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
911 if (DefIdx >= E)
912 return error(Operands[I].Begin,
913 Twine("use of invalid tied-def operand index '" +
914 Twine(DefIdx) + "'; instruction has only ") +
915 Twine(E) + " operands");
916 const auto &DefOperand = Operands[DefIdx].Operand;
917 if (!DefOperand.isReg() || !DefOperand.isDef())
918 // FIXME: add note with the def operand.
919 return error(Operands[I].Begin,
920 Twine("use of invalid tied-def operand index '") +
921 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
922 " isn't a defined register");
923 // Check that the tied-def operand wasn't tied elsewhere.
924 for (const auto &TiedPair : TiedRegisterPairs) {
925 if (TiedPair.first == DefIdx)
926 return error(Operands[I].Begin,
927 Twine("the tied-def operand #") + Twine(DefIdx) +
928 " is already tied with another register operand");
929 }
930 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
931 }
932 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
933 // indices must be less than tied max.
934 for (const auto &TiedPair : TiedRegisterPairs)
935 MI.tieOperands(TiedPair.first, TiedPair.second);
936 return false;
937}
938
939bool MIParser::parseRegisterOperand(MachineOperand &Dest,
940 Optional<unsigned> &TiedDefIdx,
941 bool IsDef) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000942 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000943 unsigned Flags = IsDef ? RegState::Define : 0;
944 while (Token.isRegisterFlag()) {
945 if (parseRegisterFlag(Flags))
946 return true;
947 }
948 if (!Token.isRegister())
949 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000950 if (parseRegister(Reg))
951 return true;
952 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000953 unsigned SubReg = 0;
Matthias Braun333e4682016-07-26 21:49:34 +0000954 if (Token.is(MIToken::dot)) {
Alex Lorenz2eacca82015-07-13 23:24:34 +0000955 if (parseSubRegisterIndex(SubReg))
956 return true;
Matthias Braun5d00b322016-07-16 01:36:18 +0000957 if (!TargetRegisterInfo::isVirtualRegister(Reg))
958 return error("subregister index expects a virtual register");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000959 }
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000960 MachineRegisterInfo &MRI = MF.getRegInfo();
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000961 if ((Flags & RegState::Define) == 0) {
962 if (consumeIfPresent(MIToken::lparen)) {
963 unsigned Idx;
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000964 if (!parseRegisterTiedDefIndex(Idx))
965 TiedDefIdx = Idx;
966 else {
967 // Try a redundant low-level type.
968 LLT Ty;
969 if (parseLowLevelType(Token.location(), Ty))
970 return error("expected tied-def or low-level type after '('");
971
972 if (expectAndConsume(MIToken::rparen))
973 return true;
974
975 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
976 return error("inconsistent type for generic virtual register");
977
978 MRI.setType(Reg, Ty);
979 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000980 }
981 } else if (consumeIfPresent(MIToken::lparen)) {
Quentin Colombet2c646962016-06-08 23:27:46 +0000982 // Virtual registers may have a size with GlobalISel.
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000983 if (!TargetRegisterInfo::isVirtualRegister(Reg))
984 return error("unexpected size on physical register");
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000985 if (MRI.getRegClassOrRegBank(Reg).is<const TargetRegisterClass *>())
986 return error("unexpected size on non-generic virtual register");
987
Tim Northover0f140c72016-09-09 11:46:34 +0000988 LLT Ty;
989 if (parseLowLevelType(Token.location(), Ty))
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000990 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000991
Tim Northover0f140c72016-09-09 11:46:34 +0000992 if (expectAndConsume(MIToken::rparen))
993 return true;
994
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000995 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
996 return error("inconsistent type for generic virtual register");
997
Tim Northover0f140c72016-09-09 11:46:34 +0000998 MRI.setType(Reg, Ty);
Quentin Colombet2c646962016-06-08 23:27:46 +0000999 } else if (PFS.GenericVRegs.count(Reg)) {
1000 // Generic virtual registers must have a size.
1001 // If we end up here this means the size hasn't been specified and
1002 // this is bad!
1003 return error("generic virtual registers must have a size");
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001004 }
Alex Lorenz495ad872015-07-08 21:23:34 +00001005 Dest = MachineOperand::CreateReg(
1006 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +00001007 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +00001008 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1009 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001010 return false;
1011}
1012
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001013bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1014 assert(Token.is(MIToken::IntegerLiteral));
1015 const APSInt &Int = Token.integerValue();
1016 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +00001017 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001018 Dest = MachineOperand::CreateImm(Int.getExtValue());
1019 lex();
1020 return false;
1021}
1022
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001023bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1024 const Constant *&C) {
1025 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001026 SMDiagnostic Err;
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001027 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
Matthias Braune35861d2016-07-13 23:27:50 +00001028 &PFS.IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001029 if (!C)
1030 return error(Loc + Err.getColumnNo(), Err.getMessage());
1031 return false;
1032}
1033
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001034bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1035 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1036 return true;
1037 lex();
1038 return false;
1039}
1040
Ahmed Bougachad760de02016-07-28 17:15:12 +00001041bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
Tim Northover32a078a2016-09-15 10:09:59 +00001042 if (Token.is(MIToken::ScalarType)) {
Tim Northover62ae5682016-07-20 19:09:30 +00001043 Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1044 lex();
1045 return false;
Tim Northoverbd505462016-07-22 16:59:52 +00001046 } else if (Token.is(MIToken::PointerType)) {
Tim Northover5ae83502016-09-15 09:20:34 +00001047 const DataLayout &DL = MF.getFunction()->getParent()->getDataLayout();
1048 unsigned AS = APSInt(Token.range().drop_front()).getZExtValue();
1049 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
Tim Northoverbd505462016-07-22 16:59:52 +00001050 lex();
1051 return false;
Tim Northover62ae5682016-07-20 19:09:30 +00001052 }
Quentin Colombet85199672016-03-08 00:20:48 +00001053
Tim Northover62ae5682016-07-20 19:09:30 +00001054 // Now we're looking for a vector.
1055 if (Token.isNot(MIToken::less))
Tim Northoverbd505462016-07-22 16:59:52 +00001056 return error(Loc,
1057 "expected unsized, pN, sN or <N x sM> for GlobalISel type");
1058
Tim Northover62ae5682016-07-20 19:09:30 +00001059 lex();
1060
1061 if (Token.isNot(MIToken::IntegerLiteral))
1062 return error(Loc, "expected <N x sM> for vctor type");
1063 uint64_t NumElements = Token.integerValue().getZExtValue();
1064 lex();
1065
1066 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1067 return error(Loc, "expected '<N x sM>' for vector type");
1068 lex();
1069
1070 if (Token.isNot(MIToken::ScalarType))
1071 return error(Loc, "expected '<N x sM>' for vector type");
1072 uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1073 lex();
1074
1075 if (Token.isNot(MIToken::greater))
1076 return error(Loc, "expected '<N x sM>' for vector type");
1077 lex();
1078
1079 Ty = LLT::vector(NumElements, ScalarSize);
Quentin Colombet85199672016-03-08 00:20:48 +00001080 return false;
1081}
1082
Alex Lorenz05e38822015-08-05 18:52:21 +00001083bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1084 assert(Token.is(MIToken::IntegerType));
1085 auto Loc = Token.location();
1086 lex();
1087 if (Token.isNot(MIToken::IntegerLiteral))
1088 return error("expected an integer literal");
1089 const Constant *C = nullptr;
1090 if (parseIRConstant(Loc, C))
1091 return true;
1092 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1093 return false;
1094}
1095
Alex Lorenzad156fb2015-07-31 20:49:21 +00001096bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1097 auto Loc = Token.location();
1098 lex();
1099 if (Token.isNot(MIToken::FloatingPointLiteral))
1100 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001101 const Constant *C = nullptr;
1102 if (parseIRConstant(Loc, C))
1103 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001104 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1105 return false;
1106}
1107
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001108bool MIParser::getUnsigned(unsigned &Result) {
1109 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1110 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1111 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1112 if (Val64 == Limit)
1113 return error("expected 32-bit integer (too large)");
1114 Result = Val64;
1115 return false;
1116}
1117
Alex Lorenzf09df002015-06-30 18:16:42 +00001118bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001119 assert(Token.is(MIToken::MachineBasicBlock) ||
1120 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001121 unsigned Number;
1122 if (getUnsigned(Number))
1123 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001124 auto MBBInfo = PFS.MBBSlots.find(Number);
1125 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001126 return error(Twine("use of undefined machine basic block #") +
1127 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001128 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001129 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1130 return error(Twine("the name of machine basic block #") + Twine(Number) +
1131 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001132 return false;
1133}
1134
1135bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1136 MachineBasicBlock *MBB;
1137 if (parseMBBReference(MBB))
1138 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001139 Dest = MachineOperand::CreateMBB(MBB);
1140 lex();
1141 return false;
1142}
1143
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001144bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001145 assert(Token.is(MIToken::StackObject));
1146 unsigned ID;
1147 if (getUnsigned(ID))
1148 return true;
1149 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1150 if (ObjectInfo == PFS.StackObjectSlots.end())
1151 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1152 "'");
1153 StringRef Name;
1154 if (const auto *Alloca =
Matthias Braun941a7052016-07-28 18:40:00 +00001155 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001156 Name = Alloca->getName();
1157 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1158 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1159 "' isn't '" + Token.stringValue() + "'");
1160 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001161 FI = ObjectInfo->second;
1162 return false;
1163}
1164
1165bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1166 int FI;
1167 if (parseStackFrameIndex(FI))
1168 return true;
1169 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001170 return false;
1171}
1172
Alex Lorenzea882122015-08-12 21:17:02 +00001173bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001174 assert(Token.is(MIToken::FixedStackObject));
1175 unsigned ID;
1176 if (getUnsigned(ID))
1177 return true;
1178 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1179 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1180 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1181 Twine(ID) + "'");
1182 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001183 FI = ObjectInfo->second;
1184 return false;
1185}
1186
1187bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1188 int FI;
1189 if (parseFixedStackFrameIndex(FI))
1190 return true;
1191 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001192 return false;
1193}
1194
Alex Lorenz41df7d32015-07-28 17:09:52 +00001195bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001196 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001197 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001198 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001199 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001200 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001201 return error(Twine("use of undefined global value '") + Token.range() +
1202 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001203 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001204 }
1205 case MIToken::GlobalValue: {
1206 unsigned GVIdx;
1207 if (getUnsigned(GVIdx))
1208 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001209 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001210 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1211 "'");
Matthias Braune35861d2016-07-13 23:27:50 +00001212 GV = PFS.IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001213 break;
1214 }
1215 default:
1216 llvm_unreachable("The current token should be a global value");
1217 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001218 return false;
1219}
1220
1221bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1222 GlobalValue *GV = nullptr;
1223 if (parseGlobalValue(GV))
1224 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001225 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001226 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001227 if (parseOperandsOffset(Dest))
1228 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001229 return false;
1230}
1231
Alex Lorenzab980492015-07-20 20:51:18 +00001232bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1233 assert(Token.is(MIToken::ConstantPoolItem));
1234 unsigned ID;
1235 if (getUnsigned(ID))
1236 return true;
1237 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1238 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1239 return error("use of undefined constant '%const." + Twine(ID) + "'");
1240 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001241 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001242 if (parseOperandsOffset(Dest))
1243 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001244 return false;
1245}
1246
Alex Lorenz31d70682015-07-15 23:38:35 +00001247bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1248 assert(Token.is(MIToken::JumpTableIndex));
1249 unsigned ID;
1250 if (getUnsigned(ID))
1251 return true;
1252 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1253 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1254 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1255 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001256 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1257 return false;
1258}
1259
Alex Lorenz6ede3742015-07-21 16:59:53 +00001260bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001261 assert(Token.is(MIToken::ExternalSymbol));
1262 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001263 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001264 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001265 if (parseOperandsOffset(Dest))
1266 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001267 return false;
1268}
1269
Matthias Braunb74eb412016-03-28 18:18:46 +00001270bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1271 assert(Token.is(MIToken::SubRegisterIndex));
1272 StringRef Name = Token.stringValue();
1273 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1274 if (SubRegIndex == 0)
1275 return error(Twine("unknown subregister index '") + Name + "'");
1276 lex();
1277 Dest = MachineOperand::CreateImm(SubRegIndex);
1278 return false;
1279}
1280
Alex Lorenz44f29252015-07-22 21:07:04 +00001281bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001282 assert(Token.is(MIToken::exclaim));
1283 auto Loc = Token.location();
1284 lex();
1285 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1286 return error("expected metadata id after '!'");
1287 unsigned ID;
1288 if (getUnsigned(ID))
1289 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001290 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1291 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
Alex Lorenz35e44462015-07-22 17:58:46 +00001292 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1293 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001294 Node = NodeInfo->second.get();
1295 return false;
1296}
1297
1298bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1299 MDNode *Node = nullptr;
1300 if (parseMDNode(Node))
1301 return true;
1302 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001303 return false;
1304}
1305
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001306bool MIParser::parseCFIOffset(int &Offset) {
1307 if (Token.isNot(MIToken::IntegerLiteral))
1308 return error("expected a cfi offset");
1309 if (Token.integerValue().getMinSignedBits() > 32)
1310 return error("expected a 32 bit integer (the cfi offset is too large)");
1311 Offset = (int)Token.integerValue().getExtValue();
1312 lex();
1313 return false;
1314}
1315
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001316bool MIParser::parseCFIRegister(unsigned &Reg) {
1317 if (Token.isNot(MIToken::NamedRegister))
1318 return error("expected a cfi register");
1319 unsigned LLVMReg;
1320 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001321 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001322 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1323 assert(TRI && "Expected target register info");
1324 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1325 if (DwarfReg < 0)
1326 return error("invalid DWARF register");
1327 Reg = (unsigned)DwarfReg;
1328 lex();
1329 return false;
1330}
1331
1332bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1333 auto Kind = Token.kind();
1334 lex();
1335 auto &MMI = MF.getMMI();
1336 int Offset;
1337 unsigned Reg;
1338 unsigned CFIIndex;
1339 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001340 case MIToken::kw_cfi_same_value:
1341 if (parseCFIRegister(Reg))
1342 return true;
1343 CFIIndex =
1344 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1345 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001346 case MIToken::kw_cfi_offset:
1347 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1348 parseCFIOffset(Offset))
1349 return true;
1350 CFIIndex =
1351 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1352 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001353 case MIToken::kw_cfi_def_cfa_register:
1354 if (parseCFIRegister(Reg))
1355 return true;
1356 CFIIndex =
1357 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1358 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001359 case MIToken::kw_cfi_def_cfa_offset:
1360 if (parseCFIOffset(Offset))
1361 return true;
1362 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1363 CFIIndex = MMI.addFrameInst(
1364 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1365 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001366 case MIToken::kw_cfi_def_cfa:
1367 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1368 parseCFIOffset(Offset))
1369 return true;
1370 // NB: MCCFIInstruction::createDefCfa negates the offset.
1371 CFIIndex =
1372 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1373 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001374 default:
1375 // TODO: Parse the other CFI operands.
1376 llvm_unreachable("The current token should be a cfi operand");
1377 }
1378 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001379 return false;
1380}
1381
Alex Lorenzdeb53492015-07-28 17:28:03 +00001382bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1383 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001384 case MIToken::NamedIRBlock: {
1385 BB = dyn_cast_or_null<BasicBlock>(
1386 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001387 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001388 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001389 break;
1390 }
1391 case MIToken::IRBlock: {
1392 unsigned SlotNumber = 0;
1393 if (getUnsigned(SlotNumber))
1394 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001395 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001396 if (!BB)
1397 return error(Twine("use of undefined IR block '%ir-block.") +
1398 Twine(SlotNumber) + "'");
1399 break;
1400 }
1401 default:
1402 llvm_unreachable("The current token should be an IR block reference");
1403 }
1404 return false;
1405}
1406
1407bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1408 assert(Token.is(MIToken::kw_blockaddress));
1409 lex();
1410 if (expectAndConsume(MIToken::lparen))
1411 return true;
1412 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001413 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001414 return error("expected a global value");
1415 GlobalValue *GV = nullptr;
1416 if (parseGlobalValue(GV))
1417 return true;
1418 auto *F = dyn_cast<Function>(GV);
1419 if (!F)
1420 return error("expected an IR function reference");
1421 lex();
1422 if (expectAndConsume(MIToken::comma))
1423 return true;
1424 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001425 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001426 return error("expected an IR block reference");
1427 if (parseIRBlock(BB, *F))
1428 return true;
1429 lex();
1430 if (expectAndConsume(MIToken::rparen))
1431 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001432 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001433 if (parseOperandsOffset(Dest))
1434 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001435 return false;
1436}
1437
Tim Northover6b3bd612016-07-29 20:32:59 +00001438bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
1439 assert(Token.is(MIToken::kw_intrinsic));
1440 lex();
1441 if (expectAndConsume(MIToken::lparen))
1442 return error("expected syntax intrinsic(@llvm.whatever)");
1443
1444 if (Token.isNot(MIToken::NamedGlobalValue))
1445 return error("expected syntax intrinsic(@llvm.whatever)");
1446
1447 std::string Name = Token.stringValue();
1448 lex();
1449
1450 if (expectAndConsume(MIToken::rparen))
1451 return error("expected ')' to terminate intrinsic name");
1452
1453 // Find out what intrinsic we're dealing with, first try the global namespace
1454 // and then the target's private intrinsics if that fails.
1455 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1456 Intrinsic::ID ID = Function::lookupIntrinsicID(Name);
1457 if (ID == Intrinsic::not_intrinsic && TII)
1458 ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
1459
1460 if (ID == Intrinsic::not_intrinsic)
1461 return error("unknown intrinsic name");
1462 Dest = MachineOperand::CreateIntrinsicID(ID);
1463
1464 return false;
1465}
1466
Tim Northoverde3aea0412016-08-17 20:25:25 +00001467bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
1468 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
1469 bool IsFloat = Token.is(MIToken::kw_floatpred);
1470 lex();
1471
1472 if (expectAndConsume(MIToken::lparen))
1473 return error("expected syntax intpred(whatever) or floatpred(whatever");
1474
1475 if (Token.isNot(MIToken::Identifier))
1476 return error("whatever");
1477
1478 CmpInst::Predicate Pred;
1479 if (IsFloat) {
1480 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1481 .Case("false", CmpInst::FCMP_FALSE)
1482 .Case("oeq", CmpInst::FCMP_OEQ)
1483 .Case("ogt", CmpInst::FCMP_OGT)
1484 .Case("oge", CmpInst::FCMP_OGE)
1485 .Case("olt", CmpInst::FCMP_OLT)
1486 .Case("ole", CmpInst::FCMP_OLE)
1487 .Case("one", CmpInst::FCMP_ONE)
1488 .Case("ord", CmpInst::FCMP_ORD)
1489 .Case("uno", CmpInst::FCMP_UNO)
1490 .Case("ueq", CmpInst::FCMP_UEQ)
1491 .Case("ugt", CmpInst::FCMP_UGT)
1492 .Case("uge", CmpInst::FCMP_UGE)
1493 .Case("ult", CmpInst::FCMP_ULT)
1494 .Case("ule", CmpInst::FCMP_ULE)
1495 .Case("une", CmpInst::FCMP_UNE)
1496 .Case("true", CmpInst::FCMP_TRUE)
1497 .Default(CmpInst::BAD_FCMP_PREDICATE);
1498 if (!CmpInst::isFPPredicate(Pred))
1499 return error("invalid floating-point predicate");
1500 } else {
1501 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1502 .Case("eq", CmpInst::ICMP_EQ)
1503 .Case("ne", CmpInst::ICMP_NE)
1504 .Case("sgt", CmpInst::ICMP_SGT)
1505 .Case("sge", CmpInst::ICMP_SGE)
1506 .Case("slt", CmpInst::ICMP_SLT)
1507 .Case("sle", CmpInst::ICMP_SLE)
1508 .Case("ugt", CmpInst::ICMP_UGT)
1509 .Case("uge", CmpInst::ICMP_UGE)
1510 .Case("ult", CmpInst::ICMP_ULT)
1511 .Case("ule", CmpInst::ICMP_ULE)
1512 .Default(CmpInst::BAD_ICMP_PREDICATE);
1513 if (!CmpInst::isIntPredicate(Pred))
1514 return error("invalid integer predicate");
1515 }
1516
1517 lex();
1518 Dest = MachineOperand::CreatePredicate(Pred);
Tim Northover6cd4b232016-08-23 21:01:26 +00001519 if (expectAndConsume(MIToken::rparen))
Tim Northoverde3aea0412016-08-17 20:25:25 +00001520 return error("predicate should be terminated by ')'.");
1521
1522 return false;
1523}
1524
Alex Lorenzef5c1962015-07-28 23:02:45 +00001525bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1526 assert(Token.is(MIToken::kw_target_index));
1527 lex();
1528 if (expectAndConsume(MIToken::lparen))
1529 return true;
1530 if (Token.isNot(MIToken::Identifier))
1531 return error("expected the name of the target index");
1532 int Index = 0;
1533 if (getTargetIndex(Token.stringValue(), Index))
1534 return error("use of undefined target index '" + Token.stringValue() + "'");
1535 lex();
1536 if (expectAndConsume(MIToken::rparen))
1537 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001538 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001539 if (parseOperandsOffset(Dest))
1540 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001541 return false;
1542}
1543
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001544bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1545 assert(Token.is(MIToken::kw_liveout));
1546 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1547 assert(TRI && "Expected target register info");
1548 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1549 lex();
1550 if (expectAndConsume(MIToken::lparen))
1551 return true;
1552 while (true) {
1553 if (Token.isNot(MIToken::NamedRegister))
1554 return error("expected a named register");
1555 unsigned Reg = 0;
1556 if (parseRegister(Reg))
1557 return true;
1558 lex();
1559 Mask[Reg / 32] |= 1U << (Reg % 32);
1560 // TODO: Report an error if the same register is used more than once.
1561 if (Token.isNot(MIToken::comma))
1562 break;
1563 lex();
1564 }
1565 if (expectAndConsume(MIToken::rparen))
1566 return true;
1567 Dest = MachineOperand::CreateRegLiveOut(Mask);
1568 return false;
1569}
1570
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001571bool MIParser::parseMachineOperand(MachineOperand &Dest,
1572 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001573 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001574 case MIToken::kw_implicit:
1575 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001576 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001577 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001578 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001579 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001580 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001581 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001582 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001583 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001584 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001585 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001586 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001587 case MIToken::IntegerLiteral:
1588 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001589 case MIToken::IntegerType:
1590 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001591 case MIToken::kw_half:
1592 case MIToken::kw_float:
1593 case MIToken::kw_double:
1594 case MIToken::kw_x86_fp80:
1595 case MIToken::kw_fp128:
1596 case MIToken::kw_ppc_fp128:
1597 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001598 case MIToken::MachineBasicBlock:
1599 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001600 case MIToken::StackObject:
1601 return parseStackObjectOperand(Dest);
1602 case MIToken::FixedStackObject:
1603 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001604 case MIToken::GlobalValue:
1605 case MIToken::NamedGlobalValue:
1606 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001607 case MIToken::ConstantPoolItem:
1608 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001609 case MIToken::JumpTableIndex:
1610 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001611 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001612 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00001613 case MIToken::SubRegisterIndex:
1614 return parseSubRegisterIndexOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001615 case MIToken::exclaim:
1616 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001617 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001618 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001619 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001620 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001621 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001622 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001623 case MIToken::kw_blockaddress:
1624 return parseBlockAddressOperand(Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +00001625 case MIToken::kw_intrinsic:
1626 return parseIntrinsicOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001627 case MIToken::kw_target_index:
1628 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001629 case MIToken::kw_liveout:
1630 return parseLiveoutRegisterMaskOperand(Dest);
Tim Northoverde3aea0412016-08-17 20:25:25 +00001631 case MIToken::kw_floatpred:
1632 case MIToken::kw_intpred:
1633 return parsePredicateOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001634 case MIToken::Error:
1635 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001636 case MIToken::Identifier:
1637 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1638 Dest = MachineOperand::CreateRegMask(RegMask);
1639 lex();
1640 break;
1641 }
Justin Bognerb03fd122016-08-17 05:10:15 +00001642 LLVM_FALLTHROUGH;
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001643 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001644 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001645 return error("expected a machine operand");
1646 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001647 return false;
1648}
1649
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001650bool MIParser::parseMachineOperandAndTargetFlags(
1651 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001652 unsigned TF = 0;
1653 bool HasTargetFlags = false;
1654 if (Token.is(MIToken::kw_target_flags)) {
1655 HasTargetFlags = true;
1656 lex();
1657 if (expectAndConsume(MIToken::lparen))
1658 return true;
1659 if (Token.isNot(MIToken::Identifier))
1660 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001661 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1662 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1663 return error("use of undefined target flag '" + Token.stringValue() +
1664 "'");
1665 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001666 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001667 while (Token.is(MIToken::comma)) {
1668 lex();
1669 if (Token.isNot(MIToken::Identifier))
1670 return error("expected the name of the target flag");
1671 unsigned BitFlag = 0;
1672 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1673 return error("use of undefined target flag '" + Token.stringValue() +
1674 "'");
1675 // TODO: Report an error when using a duplicate bit target flag.
1676 TF |= BitFlag;
1677 lex();
1678 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001679 if (expectAndConsume(MIToken::rparen))
1680 return true;
1681 }
1682 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001683 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001684 return true;
1685 if (!HasTargetFlags)
1686 return false;
1687 if (Dest.isReg())
1688 return error(Loc, "register operands can't have target flags");
1689 Dest.setTargetFlags(TF);
1690 return false;
1691}
1692
Alex Lorenzdc24c172015-08-07 20:21:00 +00001693bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001694 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1695 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001696 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001697 bool IsNegative = Token.is(MIToken::minus);
1698 lex();
1699 if (Token.isNot(MIToken::IntegerLiteral))
1700 return error("expected an integer literal after '" + Sign + "'");
1701 if (Token.integerValue().getMinSignedBits() > 64)
1702 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001703 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001704 if (IsNegative)
1705 Offset = -Offset;
1706 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001707 return false;
1708}
1709
Alex Lorenz620f8912015-08-13 20:33:33 +00001710bool MIParser::parseAlignment(unsigned &Alignment) {
1711 assert(Token.is(MIToken::kw_align));
1712 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001713 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001714 return error("expected an integer literal after 'align'");
1715 if (getUnsigned(Alignment))
1716 return true;
1717 lex();
1718 return false;
1719}
1720
Alex Lorenzdc24c172015-08-07 20:21:00 +00001721bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1722 int64_t Offset = 0;
1723 if (parseOffset(Offset))
1724 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001725 Op.setOffset(Offset);
1726 return false;
1727}
1728
Alex Lorenz36593ac2015-08-19 23:27:07 +00001729bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001730 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001731 case MIToken::NamedIRValue: {
1732 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001733 break;
1734 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001735 case MIToken::IRValue: {
1736 unsigned SlotNumber = 0;
1737 if (getUnsigned(SlotNumber))
1738 return true;
1739 V = getIRValue(SlotNumber);
1740 break;
1741 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001742 case MIToken::NamedGlobalValue:
1743 case MIToken::GlobalValue: {
1744 GlobalValue *GV = nullptr;
1745 if (parseGlobalValue(GV))
1746 return true;
1747 V = GV;
1748 break;
1749 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001750 case MIToken::QuotedIRValue: {
1751 const Constant *C = nullptr;
1752 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1753 return true;
1754 V = C;
1755 break;
1756 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001757 default:
1758 llvm_unreachable("The current token should be an IR block reference");
1759 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001760 if (!V)
1761 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001762 return false;
1763}
1764
1765bool MIParser::getUint64(uint64_t &Result) {
1766 assert(Token.hasIntegerValue());
1767 if (Token.integerValue().getActiveBits() > 64)
1768 return error("expected 64-bit integer (too large)");
1769 Result = Token.integerValue().getZExtValue();
1770 return false;
1771}
1772
Justin Lebar0af80cd2016-07-15 18:26:59 +00001773bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
1774 const auto OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001775 switch (Token.kind()) {
1776 case MIToken::kw_volatile:
1777 Flags |= MachineMemOperand::MOVolatile;
1778 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001779 case MIToken::kw_non_temporal:
1780 Flags |= MachineMemOperand::MONonTemporal;
1781 break;
Justin Lebaradbf09e2016-09-11 01:38:58 +00001782 case MIToken::kw_dereferenceable:
1783 Flags |= MachineMemOperand::MODereferenceable;
1784 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001785 case MIToken::kw_invariant:
1786 Flags |= MachineMemOperand::MOInvariant;
1787 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001788 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001789 default:
1790 llvm_unreachable("The current token should be a memory operand flag");
1791 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001792 if (OldFlags == Flags)
1793 // We know that the same flag is specified more than once when the flags
1794 // weren't modified.
1795 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001796 lex();
1797 return false;
1798}
1799
Alex Lorenz91097a32015-08-12 20:33:26 +00001800bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1801 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001802 case MIToken::kw_stack:
1803 PSV = MF.getPSVManager().getStack();
1804 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001805 case MIToken::kw_got:
1806 PSV = MF.getPSVManager().getGOT();
1807 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001808 case MIToken::kw_jump_table:
1809 PSV = MF.getPSVManager().getJumpTable();
1810 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001811 case MIToken::kw_constant_pool:
1812 PSV = MF.getPSVManager().getConstantPool();
1813 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001814 case MIToken::FixedStackObject: {
1815 int FI;
1816 if (parseFixedStackFrameIndex(FI))
1817 return true;
1818 PSV = MF.getPSVManager().getFixedStack(FI);
1819 // The token was already consumed, so use return here instead of break.
1820 return false;
1821 }
Matthias Braun3ef7df92016-06-08 00:47:07 +00001822 case MIToken::StackObject: {
1823 int FI;
1824 if (parseStackFrameIndex(FI))
1825 return true;
1826 PSV = MF.getPSVManager().getFixedStack(FI);
1827 // The token was already consumed, so use return here instead of break.
1828 return false;
1829 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001830 case MIToken::kw_call_entry: {
1831 lex();
1832 switch (Token.kind()) {
1833 case MIToken::GlobalValue:
1834 case MIToken::NamedGlobalValue: {
1835 GlobalValue *GV = nullptr;
1836 if (parseGlobalValue(GV))
1837 return true;
1838 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1839 break;
1840 }
1841 case MIToken::ExternalSymbol:
1842 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1843 MF.createExternalSymbolName(Token.stringValue()));
1844 break;
1845 default:
1846 return error(
1847 "expected a global value or an external symbol after 'call-entry'");
1848 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001849 break;
1850 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001851 default:
1852 llvm_unreachable("The current token should be pseudo source value");
1853 }
1854 lex();
1855 return false;
1856}
1857
1858bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001859 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001860 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Matthias Braun3ef7df92016-06-08 00:47:07 +00001861 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
1862 Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001863 const PseudoSourceValue *PSV = nullptr;
1864 if (parseMemoryPseudoSourceValue(PSV))
1865 return true;
1866 int64_t Offset = 0;
1867 if (parseOffset(Offset))
1868 return true;
1869 Dest = MachinePointerInfo(PSV, Offset);
1870 return false;
1871 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001872 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1873 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001874 Token.isNot(MIToken::NamedGlobalValue) &&
1875 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001876 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001877 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001878 if (parseIRValue(V))
1879 return true;
1880 if (!V->getType()->isPointerTy())
1881 return error("expected a pointer IR value");
1882 lex();
1883 int64_t Offset = 0;
1884 if (parseOffset(Offset))
1885 return true;
1886 Dest = MachinePointerInfo(V, Offset);
1887 return false;
1888}
1889
Alex Lorenz4af7e612015-08-03 23:08:19 +00001890bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1891 if (expectAndConsume(MIToken::lparen))
1892 return true;
Justin Lebar0af80cd2016-07-15 18:26:59 +00001893 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
Alex Lorenza518b792015-08-04 00:24:45 +00001894 while (Token.isMemoryOperandFlag()) {
1895 if (parseMemoryOperandFlag(Flags))
1896 return true;
1897 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001898 if (Token.isNot(MIToken::Identifier) ||
1899 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1900 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001901 if (Token.stringValue() == "load")
1902 Flags |= MachineMemOperand::MOLoad;
1903 else
1904 Flags |= MachineMemOperand::MOStore;
1905 lex();
1906
1907 if (Token.isNot(MIToken::IntegerLiteral))
1908 return error("expected the size integer literal after memory operation");
1909 uint64_t Size;
1910 if (getUint64(Size))
1911 return true;
1912 lex();
1913
Alex Lorenz91097a32015-08-12 20:33:26 +00001914 MachinePointerInfo Ptr = MachinePointerInfo();
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001915 if (Token.is(MIToken::Identifier)) {
1916 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1917 if (Token.stringValue() != Word)
1918 return error(Twine("expected '") + Word + "'");
1919 lex();
1920
1921 if (parseMachinePointerInfo(Ptr))
1922 return true;
1923 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001924 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001925 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001926 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001927 while (consumeIfPresent(MIToken::comma)) {
1928 switch (Token.kind()) {
1929 case MIToken::kw_align:
1930 if (parseAlignment(BaseAlignment))
1931 return true;
1932 break;
1933 case MIToken::md_tbaa:
1934 lex();
1935 if (parseMDNode(AAInfo.TBAA))
1936 return true;
1937 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001938 case MIToken::md_alias_scope:
1939 lex();
1940 if (parseMDNode(AAInfo.Scope))
1941 return true;
1942 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001943 case MIToken::md_noalias:
1944 lex();
1945 if (parseMDNode(AAInfo.NoAlias))
1946 return true;
1947 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001948 case MIToken::md_range:
1949 lex();
1950 if (parseMDNode(Range))
1951 return true;
1952 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001953 // TODO: Report an error on duplicate metadata nodes.
1954 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001955 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1956 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001957 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001958 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001959 if (expectAndConsume(MIToken::rparen))
1960 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001961 Dest =
1962 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001963 return false;
1964}
1965
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001966void MIParser::initNames2InstrOpCodes() {
1967 if (!Names2InstrOpCodes.empty())
1968 return;
1969 const auto *TII = MF.getSubtarget().getInstrInfo();
1970 assert(TII && "Expected target instruction info");
1971 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1972 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1973}
1974
1975bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1976 initNames2InstrOpCodes();
1977 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1978 if (InstrInfo == Names2InstrOpCodes.end())
1979 return true;
1980 OpCode = InstrInfo->getValue();
1981 return false;
1982}
1983
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001984void MIParser::initNames2Regs() {
1985 if (!Names2Regs.empty())
1986 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001987 // The '%noreg' register is the register 0.
1988 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001989 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1990 assert(TRI && "Expected target register info");
1991 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1992 bool WasInserted =
1993 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1994 .second;
1995 (void)WasInserted;
1996 assert(WasInserted && "Expected registers to be unique case-insensitively");
1997 }
1998}
1999
2000bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
2001 initNames2Regs();
2002 auto RegInfo = Names2Regs.find(RegName);
2003 if (RegInfo == Names2Regs.end())
2004 return true;
2005 Reg = RegInfo->getValue();
2006 return false;
2007}
2008
Alex Lorenz8f6f4282015-06-29 16:57:06 +00002009void MIParser::initNames2RegMasks() {
2010 if (!Names2RegMasks.empty())
2011 return;
2012 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2013 assert(TRI && "Expected target register info");
2014 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
2015 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
2016 assert(RegMasks.size() == RegMaskNames.size());
2017 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
2018 Names2RegMasks.insert(
2019 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
2020}
2021
2022const uint32_t *MIParser::getRegMask(StringRef Identifier) {
2023 initNames2RegMasks();
2024 auto RegMaskInfo = Names2RegMasks.find(Identifier);
2025 if (RegMaskInfo == Names2RegMasks.end())
2026 return nullptr;
2027 return RegMaskInfo->getValue();
2028}
2029
Alex Lorenz2eacca82015-07-13 23:24:34 +00002030void MIParser::initNames2SubRegIndices() {
2031 if (!Names2SubRegIndices.empty())
2032 return;
2033 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2034 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
2035 Names2SubRegIndices.insert(
2036 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
2037}
2038
2039unsigned MIParser::getSubRegIndex(StringRef Name) {
2040 initNames2SubRegIndices();
2041 auto SubRegInfo = Names2SubRegIndices.find(Name);
2042 if (SubRegInfo == Names2SubRegIndices.end())
2043 return 0;
2044 return SubRegInfo->getValue();
2045}
2046
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002047static void initSlots2BasicBlocks(
2048 const Function &F,
2049 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2050 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002051 MST.incorporateFunction(F);
2052 for (auto &BB : F) {
2053 if (BB.hasName())
2054 continue;
2055 int Slot = MST.getLocalSlot(&BB);
2056 if (Slot == -1)
2057 continue;
2058 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2059 }
2060}
2061
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002062static const BasicBlock *getIRBlockFromSlot(
2063 unsigned Slot,
2064 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002065 auto BlockInfo = Slots2BasicBlocks.find(Slot);
2066 if (BlockInfo == Slots2BasicBlocks.end())
2067 return nullptr;
2068 return BlockInfo->second;
2069}
2070
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002071const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2072 if (Slots2BasicBlocks.empty())
2073 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
2074 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2075}
2076
2077const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2078 if (&F == MF.getFunction())
2079 return getIRBlock(Slot);
2080 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2081 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2082 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2083}
2084
Alex Lorenzdd13be02015-08-19 23:31:05 +00002085static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2086 DenseMap<unsigned, const Value *> &Slots2Values) {
2087 int Slot = MST.getLocalSlot(V);
2088 if (Slot == -1)
2089 return;
2090 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2091}
2092
2093/// Creates the mapping from slot numbers to function's unnamed IR values.
2094static void initSlots2Values(const Function &F,
2095 DenseMap<unsigned, const Value *> &Slots2Values) {
2096 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2097 MST.incorporateFunction(F);
2098 for (const auto &Arg : F.args())
2099 mapValueToSlot(&Arg, MST, Slots2Values);
2100 for (const auto &BB : F) {
2101 mapValueToSlot(&BB, MST, Slots2Values);
2102 for (const auto &I : BB)
2103 mapValueToSlot(&I, MST, Slots2Values);
2104 }
2105}
2106
2107const Value *MIParser::getIRValue(unsigned Slot) {
2108 if (Slots2Values.empty())
2109 initSlots2Values(*MF.getFunction(), Slots2Values);
2110 auto ValueInfo = Slots2Values.find(Slot);
2111 if (ValueInfo == Slots2Values.end())
2112 return nullptr;
2113 return ValueInfo->second;
2114}
2115
Alex Lorenzef5c1962015-07-28 23:02:45 +00002116void MIParser::initNames2TargetIndices() {
2117 if (!Names2TargetIndices.empty())
2118 return;
2119 const auto *TII = MF.getSubtarget().getInstrInfo();
2120 assert(TII && "Expected target instruction info");
2121 auto Indices = TII->getSerializableTargetIndices();
2122 for (const auto &I : Indices)
2123 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2124}
2125
2126bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2127 initNames2TargetIndices();
2128 auto IndexInfo = Names2TargetIndices.find(Name);
2129 if (IndexInfo == Names2TargetIndices.end())
2130 return true;
2131 Index = IndexInfo->second;
2132 return false;
2133}
2134
Alex Lorenz49873a82015-08-06 00:44:07 +00002135void MIParser::initNames2DirectTargetFlags() {
2136 if (!Names2DirectTargetFlags.empty())
2137 return;
2138 const auto *TII = MF.getSubtarget().getInstrInfo();
2139 assert(TII && "Expected target instruction info");
2140 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2141 for (const auto &I : Flags)
2142 Names2DirectTargetFlags.insert(
2143 std::make_pair(StringRef(I.second), I.first));
2144}
2145
2146bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2147 initNames2DirectTargetFlags();
2148 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2149 if (FlagInfo == Names2DirectTargetFlags.end())
2150 return true;
2151 Flag = FlagInfo->second;
2152 return false;
2153}
2154
Alex Lorenzf3630112015-08-18 22:52:15 +00002155void MIParser::initNames2BitmaskTargetFlags() {
2156 if (!Names2BitmaskTargetFlags.empty())
2157 return;
2158 const auto *TII = MF.getSubtarget().getInstrInfo();
2159 assert(TII && "Expected target instruction info");
2160 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2161 for (const auto &I : Flags)
2162 Names2BitmaskTargetFlags.insert(
2163 std::make_pair(StringRef(I.second), I.first));
2164}
2165
2166bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2167 initNames2BitmaskTargetFlags();
2168 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2169 if (FlagInfo == Names2BitmaskTargetFlags.end())
2170 return true;
2171 Flag = FlagInfo->second;
2172 return false;
2173}
2174
Matthias Braun83947862016-07-13 22:23:23 +00002175bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2176 StringRef Src,
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002177 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002178 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002179}
2180
Matthias Braun83947862016-07-13 22:23:23 +00002181bool llvm::parseMachineInstructions(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002182 StringRef Src, SMDiagnostic &Error) {
2183 return MIParser(PFS, Error, Src).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002184}
Alex Lorenzf09df002015-06-30 18:16:42 +00002185
Matthias Braun83947862016-07-13 22:23:23 +00002186bool llvm::parseMBBReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002187 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +00002188 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002189 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002190}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002191
Matthias Braun83947862016-07-13 22:23:23 +00002192bool llvm::parseNamedRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002193 unsigned &Reg, StringRef Src,
Alex Lorenz9fab3702015-07-14 21:24:41 +00002194 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002195 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002196}
Alex Lorenz12045a42015-07-27 17:42:45 +00002197
Matthias Braun83947862016-07-13 22:23:23 +00002198bool llvm::parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002199 unsigned &Reg, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +00002200 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002201 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +00002202}
Alex Lorenza314d812015-08-18 22:26:26 +00002203
Matthias Braun83947862016-07-13 22:23:23 +00002204bool llvm::parseStackObjectReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002205 int &FI, StringRef Src,
Alex Lorenza314d812015-08-18 22:26:26 +00002206 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002207 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
Alex Lorenza314d812015-08-18 22:26:26 +00002208}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002209
Matthias Braun83947862016-07-13 22:23:23 +00002210bool llvm::parseMDNode(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002211 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2212 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002213}