blob: 91943ea36acb2283f3df38f441cab8a742ea0485 [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
Tim Northover98a56eb2016-07-22 22:13:36 +0000602 SmallVector<LLT, 1> Tys;
Quentin Colombet85199672016-03-08 00:20:48 +0000603 if (isPreISelGenericOpcode(OpCode)) {
Tim Northover98a56eb2016-07-22 22:13:36 +0000604 // For generic opcode, at least one type is mandatory.
Tim Northover26e40bd2016-07-26 17:28:01 +0000605 auto Loc = Token.location();
606 bool ManyTypes = Token.is(MIToken::lbrace);
607 if (ManyTypes)
608 lex();
609
610 // Now actually parse the type(s).
Tim Northover98a56eb2016-07-22 22:13:36 +0000611 do {
Tim Northover98a56eb2016-07-22 22:13:36 +0000612 Tys.resize(Tys.size() + 1);
613 if (parseLowLevelType(Loc, Tys[Tys.size() - 1]))
614 return true;
Tim Northover26e40bd2016-07-26 17:28:01 +0000615 } while (ManyTypes && consumeIfPresent(MIToken::comma));
616
617 if (ManyTypes)
618 expectAndConsume(MIToken::rbrace);
Quentin Colombet85199672016-03-08 00:20:48 +0000619 }
620
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000621 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000622 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000623 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000624 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000625 Optional<unsigned> TiedDefIdx;
626 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000627 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000628 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000629 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000630 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
631 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000632 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000633 if (Token.isNot(MIToken::comma))
634 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000635 lex();
636 }
637
Alex Lorenz46d760d2015-07-22 21:15:11 +0000638 DebugLoc DebugLocation;
639 if (Token.is(MIToken::kw_debug_location)) {
640 lex();
641 if (Token.isNot(MIToken::exclaim))
642 return error("expected a metadata node after 'debug-location'");
643 MDNode *Node = nullptr;
644 if (parseMDNode(Node))
645 return true;
646 DebugLocation = DebugLoc(Node);
647 }
648
Alex Lorenz4af7e612015-08-03 23:08:19 +0000649 // Parse the machine memory operands.
650 SmallVector<MachineMemOperand *, 2> MemOperands;
651 if (Token.is(MIToken::coloncolon)) {
652 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000653 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000654 MachineMemOperand *MemOp = nullptr;
655 if (parseMachineMemoryOperand(MemOp))
656 return true;
657 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000658 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000659 break;
660 if (Token.isNot(MIToken::comma))
661 return error("expected ',' before the next machine memory operand");
662 lex();
663 }
664 }
665
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000666 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000667 if (!MCID.isVariadic()) {
668 // FIXME: Move the implicit operand verification to the machine verifier.
669 if (verifyImplicitOperands(Operands, MCID))
670 return true;
671 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000672
Alex Lorenzcb268d42015-07-06 23:07:26 +0000673 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000674 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000675 MI->setFlags(Flags);
Tim Northover98a56eb2016-07-22 22:13:36 +0000676 if (Tys.size() > 0) {
677 for (unsigned i = 0; i < Tys.size(); ++i)
678 MI->setType(Tys[i], i);
679 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000680 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000681 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000682 if (assignRegisterTies(*MI, Operands))
683 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000684 if (MemOperands.empty())
685 return false;
686 MachineInstr::mmo_iterator MemRefs =
687 MF.allocateMemRefsArray(MemOperands.size());
688 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
689 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000690 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000691}
692
Alex Lorenz1ea60892015-07-27 20:29:27 +0000693bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000694 lex();
695 if (Token.isNot(MIToken::MachineBasicBlock))
696 return error("expected a machine basic block reference");
697 if (parseMBBReference(MBB))
698 return true;
699 lex();
700 if (Token.isNot(MIToken::Eof))
701 return error(
702 "expected end of string after the machine basic block reference");
703 return false;
704}
705
Alex Lorenz1ea60892015-07-27 20:29:27 +0000706bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000707 lex();
708 if (Token.isNot(MIToken::NamedRegister))
709 return error("expected a named register");
710 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000711 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000712 lex();
713 if (Token.isNot(MIToken::Eof))
714 return error("expected end of string after the register reference");
715 return false;
716}
717
Alex Lorenz12045a42015-07-27 17:42:45 +0000718bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
719 lex();
720 if (Token.isNot(MIToken::VirtualRegister))
721 return error("expected a virtual register");
722 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000723 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000724 lex();
725 if (Token.isNot(MIToken::Eof))
726 return error("expected end of string after the register reference");
727 return false;
728}
729
Alex Lorenza314d812015-08-18 22:26:26 +0000730bool MIParser::parseStandaloneStackObject(int &FI) {
731 lex();
732 if (Token.isNot(MIToken::StackObject))
733 return error("expected a stack object");
734 if (parseStackFrameIndex(FI))
735 return true;
736 if (Token.isNot(MIToken::Eof))
737 return error("expected end of string after the stack object reference");
738 return false;
739}
740
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000741bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
742 lex();
743 if (Token.isNot(MIToken::exclaim))
744 return error("expected a metadata node");
745 if (parseMDNode(Node))
746 return true;
747 if (Token.isNot(MIToken::Eof))
748 return error("expected end of string after the metadata node");
749 return false;
750}
751
Alex Lorenz36962cd2015-07-07 02:08:46 +0000752static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
753 assert(MO.isImplicit());
754 return MO.isDef() ? "implicit-def" : "implicit";
755}
756
757static std::string getRegisterName(const TargetRegisterInfo *TRI,
758 unsigned Reg) {
759 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
760 return StringRef(TRI->getName(Reg)).lower();
761}
762
Alex Lorenz0153e592015-09-10 14:04:34 +0000763/// Return true if the parsed machine operands contain a given machine operand.
764static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
765 ArrayRef<ParsedMachineOperand> Operands) {
766 for (const auto &I : Operands) {
767 if (ImplicitOperand.isIdenticalTo(I.Operand))
768 return true;
769 }
770 return false;
771}
772
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000773bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
774 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000775 if (MCID.isCall())
776 // We can't verify call instructions as they can contain arbitrary implicit
777 // register and register mask operands.
778 return false;
779
780 // Gather all the expected implicit operands.
781 SmallVector<MachineOperand, 4> ImplicitOperands;
782 if (MCID.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000783 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000784 ImplicitOperands.push_back(
785 MachineOperand::CreateReg(*ImpDefs, true, true));
786 if (MCID.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000787 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000788 ImplicitOperands.push_back(
789 MachineOperand::CreateReg(*ImpUses, false, true));
790
791 const auto *TRI = MF.getSubtarget().getRegisterInfo();
792 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000793 for (const auto &I : ImplicitOperands) {
794 if (isImplicitOperandIn(I, Operands))
795 continue;
796 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000797 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000798 printImplicitRegisterFlag(I) + " %" +
799 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000800 }
801 return false;
802}
803
Alex Lorenze5a44662015-07-17 00:24:15 +0000804bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
805 if (Token.is(MIToken::kw_frame_setup)) {
806 Flags |= MachineInstr::FrameSetup;
807 lex();
808 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000809 if (Token.isNot(MIToken::Identifier))
810 return error("expected a machine instruction");
811 StringRef InstrName = Token.stringValue();
812 if (parseInstrName(InstrName, OpCode))
813 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000814 lex();
815 return false;
816}
817
818bool MIParser::parseRegister(unsigned &Reg) {
819 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000820 case MIToken::underscore:
821 Reg = 0;
822 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000823 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000824 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000825 if (getRegisterByName(Name, Reg))
826 return error(Twine("unknown register name '") + Name + "'");
827 break;
828 }
Alex Lorenz53464512015-07-10 22:51:20 +0000829 case MIToken::VirtualRegister: {
830 unsigned ID;
831 if (getUnsigned(ID))
832 return true;
833 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
834 if (RegInfo == PFS.VirtualRegisterSlots.end())
835 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
836 "'");
837 Reg = RegInfo->second;
838 break;
839 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000840 // TODO: Parse other register kinds.
841 default:
842 llvm_unreachable("The current token should be a register");
843 }
844 return false;
845}
846
Alex Lorenzcb268d42015-07-06 23:07:26 +0000847bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000848 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000849 switch (Token.kind()) {
850 case MIToken::kw_implicit:
851 Flags |= RegState::Implicit;
852 break;
853 case MIToken::kw_implicit_define:
854 Flags |= RegState::ImplicitDefine;
855 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000856 case MIToken::kw_def:
857 Flags |= RegState::Define;
858 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000859 case MIToken::kw_dead:
860 Flags |= RegState::Dead;
861 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000862 case MIToken::kw_killed:
863 Flags |= RegState::Kill;
864 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000865 case MIToken::kw_undef:
866 Flags |= RegState::Undef;
867 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000868 case MIToken::kw_internal:
869 Flags |= RegState::InternalRead;
870 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000871 case MIToken::kw_early_clobber:
872 Flags |= RegState::EarlyClobber;
873 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000874 case MIToken::kw_debug_use:
875 Flags |= RegState::Debug;
876 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000877 default:
878 llvm_unreachable("The current token should be a register flag");
879 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000880 if (OldFlags == Flags)
881 // We know that the same flag is specified more than once when the flags
882 // weren't modified.
883 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000884 lex();
885 return false;
886}
887
Alex Lorenz2eacca82015-07-13 23:24:34 +0000888bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
Matthias Braun333e4682016-07-26 21:49:34 +0000889 assert(Token.is(MIToken::dot));
Alex Lorenz2eacca82015-07-13 23:24:34 +0000890 lex();
891 if (Token.isNot(MIToken::Identifier))
Matthias Braun333e4682016-07-26 21:49:34 +0000892 return error("expected a subregister index after '.'");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000893 auto Name = Token.stringValue();
894 SubReg = getSubRegIndex(Name);
895 if (!SubReg)
896 return error(Twine("use of unknown subregister index '") + Name + "'");
897 lex();
898 return false;
899}
900
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000901bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
902 if (!consumeIfPresent(MIToken::kw_tied_def))
903 return error("expected 'tied-def' after '('");
904 if (Token.isNot(MIToken::IntegerLiteral))
905 return error("expected an integer literal after 'tied-def'");
906 if (getUnsigned(TiedDefIdx))
907 return true;
908 lex();
909 if (expectAndConsume(MIToken::rparen))
910 return true;
911 return false;
912}
913
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000914bool MIParser::parseSize(unsigned &Size) {
915 if (Token.isNot(MIToken::IntegerLiteral))
916 return error("expected an integer literal for the size");
917 if (getUnsigned(Size))
918 return true;
919 lex();
920 if (expectAndConsume(MIToken::rparen))
921 return true;
922 return false;
923}
924
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000925bool MIParser::assignRegisterTies(MachineInstr &MI,
926 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000927 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
928 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
929 if (!Operands[I].TiedDefIdx)
930 continue;
931 // The parser ensures that this operand is a register use, so we just have
932 // to check the tied-def operand.
933 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
934 if (DefIdx >= E)
935 return error(Operands[I].Begin,
936 Twine("use of invalid tied-def operand index '" +
937 Twine(DefIdx) + "'; instruction has only ") +
938 Twine(E) + " operands");
939 const auto &DefOperand = Operands[DefIdx].Operand;
940 if (!DefOperand.isReg() || !DefOperand.isDef())
941 // FIXME: add note with the def operand.
942 return error(Operands[I].Begin,
943 Twine("use of invalid tied-def operand index '") +
944 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
945 " isn't a defined register");
946 // Check that the tied-def operand wasn't tied elsewhere.
947 for (const auto &TiedPair : TiedRegisterPairs) {
948 if (TiedPair.first == DefIdx)
949 return error(Operands[I].Begin,
950 Twine("the tied-def operand #") + Twine(DefIdx) +
951 " is already tied with another register operand");
952 }
953 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
954 }
955 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
956 // indices must be less than tied max.
957 for (const auto &TiedPair : TiedRegisterPairs)
958 MI.tieOperands(TiedPair.first, TiedPair.second);
959 return false;
960}
961
962bool MIParser::parseRegisterOperand(MachineOperand &Dest,
963 Optional<unsigned> &TiedDefIdx,
964 bool IsDef) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000965 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000966 unsigned Flags = IsDef ? RegState::Define : 0;
967 while (Token.isRegisterFlag()) {
968 if (parseRegisterFlag(Flags))
969 return true;
970 }
971 if (!Token.isRegister())
972 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000973 if (parseRegister(Reg))
974 return true;
975 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000976 unsigned SubReg = 0;
Matthias Braun333e4682016-07-26 21:49:34 +0000977 if (Token.is(MIToken::dot)) {
Alex Lorenz2eacca82015-07-13 23:24:34 +0000978 if (parseSubRegisterIndex(SubReg))
979 return true;
Matthias Braun5d00b322016-07-16 01:36:18 +0000980 if (!TargetRegisterInfo::isVirtualRegister(Reg))
981 return error("subregister index expects a virtual register");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000982 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000983 if ((Flags & RegState::Define) == 0) {
984 if (consumeIfPresent(MIToken::lparen)) {
985 unsigned Idx;
986 if (parseRegisterTiedDefIndex(Idx))
987 return true;
988 TiedDefIdx = Idx;
989 }
990 } else if (consumeIfPresent(MIToken::lparen)) {
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000991 MachineRegisterInfo &MRI = MF.getRegInfo();
992
Quentin Colombet2c646962016-06-08 23:27:46 +0000993 // Virtual registers may have a size with GlobalISel.
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000994 if (!TargetRegisterInfo::isVirtualRegister(Reg))
995 return error("unexpected size on physical register");
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000996 if (MRI.getRegClassOrRegBank(Reg).is<const TargetRegisterClass *>())
997 return error("unexpected size on non-generic virtual register");
998
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000999 unsigned Size;
1000 if (parseSize(Size))
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001001 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001002
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001003 MRI.setSize(Reg, Size);
Quentin Colombet2c646962016-06-08 23:27:46 +00001004 } else if (PFS.GenericVRegs.count(Reg)) {
1005 // Generic virtual registers must have a size.
1006 // If we end up here this means the size hasn't been specified and
1007 // this is bad!
1008 return error("generic virtual registers must have a size");
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001009 }
Alex Lorenz495ad872015-07-08 21:23:34 +00001010 Dest = MachineOperand::CreateReg(
1011 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +00001012 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +00001013 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1014 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001015 return false;
1016}
1017
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001018bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1019 assert(Token.is(MIToken::IntegerLiteral));
1020 const APSInt &Int = Token.integerValue();
1021 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +00001022 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001023 Dest = MachineOperand::CreateImm(Int.getExtValue());
1024 lex();
1025 return false;
1026}
1027
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001028bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1029 const Constant *&C) {
1030 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001031 SMDiagnostic Err;
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001032 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
Matthias Braune35861d2016-07-13 23:27:50 +00001033 &PFS.IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001034 if (!C)
1035 return error(Loc + Err.getColumnNo(), Err.getMessage());
1036 return false;
1037}
1038
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001039bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1040 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1041 return true;
1042 lex();
1043 return false;
1044}
1045
Ahmed Bougachad760de02016-07-28 17:15:12 +00001046bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
Tim Northover62ae5682016-07-20 19:09:30 +00001047 if (Token.is(MIToken::Identifier) && Token.stringValue() == "unsized") {
Tim Northover62ae5682016-07-20 19:09:30 +00001048 lex();
1049 Ty = LLT::unsized();
1050 return false;
1051 } else if (Token.is(MIToken::ScalarType)) {
1052 Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1053 lex();
1054 return false;
Tim Northoverbd505462016-07-22 16:59:52 +00001055 } else if (Token.is(MIToken::PointerType)) {
1056 Ty = LLT::pointer(APSInt(Token.range().drop_front()).getZExtValue());
1057 lex();
1058 return false;
Tim Northover62ae5682016-07-20 19:09:30 +00001059 }
Quentin Colombet85199672016-03-08 00:20:48 +00001060
Tim Northover62ae5682016-07-20 19:09:30 +00001061 // Now we're looking for a vector.
1062 if (Token.isNot(MIToken::less))
Tim Northoverbd505462016-07-22 16:59:52 +00001063 return error(Loc,
1064 "expected unsized, pN, sN or <N x sM> for GlobalISel type");
1065
Tim Northover62ae5682016-07-20 19:09:30 +00001066 lex();
1067
1068 if (Token.isNot(MIToken::IntegerLiteral))
1069 return error(Loc, "expected <N x sM> for vctor type");
1070 uint64_t NumElements = Token.integerValue().getZExtValue();
1071 lex();
1072
1073 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1074 return error(Loc, "expected '<N x sM>' for vector type");
1075 lex();
1076
1077 if (Token.isNot(MIToken::ScalarType))
1078 return error(Loc, "expected '<N x sM>' for vector type");
1079 uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1080 lex();
1081
1082 if (Token.isNot(MIToken::greater))
1083 return error(Loc, "expected '<N x sM>' for vector type");
1084 lex();
1085
1086 Ty = LLT::vector(NumElements, ScalarSize);
Quentin Colombet85199672016-03-08 00:20:48 +00001087 return false;
1088}
1089
Alex Lorenz05e38822015-08-05 18:52:21 +00001090bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1091 assert(Token.is(MIToken::IntegerType));
1092 auto Loc = Token.location();
1093 lex();
1094 if (Token.isNot(MIToken::IntegerLiteral))
1095 return error("expected an integer literal");
1096 const Constant *C = nullptr;
1097 if (parseIRConstant(Loc, C))
1098 return true;
1099 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1100 return false;
1101}
1102
Alex Lorenzad156fb2015-07-31 20:49:21 +00001103bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1104 auto Loc = Token.location();
1105 lex();
1106 if (Token.isNot(MIToken::FloatingPointLiteral))
1107 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001108 const Constant *C = nullptr;
1109 if (parseIRConstant(Loc, C))
1110 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001111 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1112 return false;
1113}
1114
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001115bool MIParser::getUnsigned(unsigned &Result) {
1116 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1117 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1118 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1119 if (Val64 == Limit)
1120 return error("expected 32-bit integer (too large)");
1121 Result = Val64;
1122 return false;
1123}
1124
Alex Lorenzf09df002015-06-30 18:16:42 +00001125bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001126 assert(Token.is(MIToken::MachineBasicBlock) ||
1127 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001128 unsigned Number;
1129 if (getUnsigned(Number))
1130 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001131 auto MBBInfo = PFS.MBBSlots.find(Number);
1132 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001133 return error(Twine("use of undefined machine basic block #") +
1134 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001135 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001136 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1137 return error(Twine("the name of machine basic block #") + Twine(Number) +
1138 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001139 return false;
1140}
1141
1142bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1143 MachineBasicBlock *MBB;
1144 if (parseMBBReference(MBB))
1145 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001146 Dest = MachineOperand::CreateMBB(MBB);
1147 lex();
1148 return false;
1149}
1150
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001151bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001152 assert(Token.is(MIToken::StackObject));
1153 unsigned ID;
1154 if (getUnsigned(ID))
1155 return true;
1156 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1157 if (ObjectInfo == PFS.StackObjectSlots.end())
1158 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1159 "'");
1160 StringRef Name;
1161 if (const auto *Alloca =
Matthias Braun941a7052016-07-28 18:40:00 +00001162 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001163 Name = Alloca->getName();
1164 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1165 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1166 "' isn't '" + Token.stringValue() + "'");
1167 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001168 FI = ObjectInfo->second;
1169 return false;
1170}
1171
1172bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1173 int FI;
1174 if (parseStackFrameIndex(FI))
1175 return true;
1176 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001177 return false;
1178}
1179
Alex Lorenzea882122015-08-12 21:17:02 +00001180bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001181 assert(Token.is(MIToken::FixedStackObject));
1182 unsigned ID;
1183 if (getUnsigned(ID))
1184 return true;
1185 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1186 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1187 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1188 Twine(ID) + "'");
1189 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001190 FI = ObjectInfo->second;
1191 return false;
1192}
1193
1194bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1195 int FI;
1196 if (parseFixedStackFrameIndex(FI))
1197 return true;
1198 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001199 return false;
1200}
1201
Alex Lorenz41df7d32015-07-28 17:09:52 +00001202bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001203 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001204 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001205 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001206 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001207 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001208 return error(Twine("use of undefined global value '") + Token.range() +
1209 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001210 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001211 }
1212 case MIToken::GlobalValue: {
1213 unsigned GVIdx;
1214 if (getUnsigned(GVIdx))
1215 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001216 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001217 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1218 "'");
Matthias Braune35861d2016-07-13 23:27:50 +00001219 GV = PFS.IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001220 break;
1221 }
1222 default:
1223 llvm_unreachable("The current token should be a global value");
1224 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001225 return false;
1226}
1227
1228bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1229 GlobalValue *GV = nullptr;
1230 if (parseGlobalValue(GV))
1231 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001232 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001233 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001234 if (parseOperandsOffset(Dest))
1235 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001236 return false;
1237}
1238
Alex Lorenzab980492015-07-20 20:51:18 +00001239bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1240 assert(Token.is(MIToken::ConstantPoolItem));
1241 unsigned ID;
1242 if (getUnsigned(ID))
1243 return true;
1244 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1245 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1246 return error("use of undefined constant '%const." + Twine(ID) + "'");
1247 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001248 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001249 if (parseOperandsOffset(Dest))
1250 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001251 return false;
1252}
1253
Alex Lorenz31d70682015-07-15 23:38:35 +00001254bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1255 assert(Token.is(MIToken::JumpTableIndex));
1256 unsigned ID;
1257 if (getUnsigned(ID))
1258 return true;
1259 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1260 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1261 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1262 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001263 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1264 return false;
1265}
1266
Alex Lorenz6ede3742015-07-21 16:59:53 +00001267bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001268 assert(Token.is(MIToken::ExternalSymbol));
1269 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001270 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001271 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001272 if (parseOperandsOffset(Dest))
1273 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001274 return false;
1275}
1276
Matthias Braunb74eb412016-03-28 18:18:46 +00001277bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1278 assert(Token.is(MIToken::SubRegisterIndex));
1279 StringRef Name = Token.stringValue();
1280 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1281 if (SubRegIndex == 0)
1282 return error(Twine("unknown subregister index '") + Name + "'");
1283 lex();
1284 Dest = MachineOperand::CreateImm(SubRegIndex);
1285 return false;
1286}
1287
Alex Lorenz44f29252015-07-22 21:07:04 +00001288bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001289 assert(Token.is(MIToken::exclaim));
1290 auto Loc = Token.location();
1291 lex();
1292 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1293 return error("expected metadata id after '!'");
1294 unsigned ID;
1295 if (getUnsigned(ID))
1296 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001297 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1298 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
Alex Lorenz35e44462015-07-22 17:58:46 +00001299 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1300 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001301 Node = NodeInfo->second.get();
1302 return false;
1303}
1304
1305bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1306 MDNode *Node = nullptr;
1307 if (parseMDNode(Node))
1308 return true;
1309 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001310 return false;
1311}
1312
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001313bool MIParser::parseCFIOffset(int &Offset) {
1314 if (Token.isNot(MIToken::IntegerLiteral))
1315 return error("expected a cfi offset");
1316 if (Token.integerValue().getMinSignedBits() > 32)
1317 return error("expected a 32 bit integer (the cfi offset is too large)");
1318 Offset = (int)Token.integerValue().getExtValue();
1319 lex();
1320 return false;
1321}
1322
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001323bool MIParser::parseCFIRegister(unsigned &Reg) {
1324 if (Token.isNot(MIToken::NamedRegister))
1325 return error("expected a cfi register");
1326 unsigned LLVMReg;
1327 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001328 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001329 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1330 assert(TRI && "Expected target register info");
1331 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1332 if (DwarfReg < 0)
1333 return error("invalid DWARF register");
1334 Reg = (unsigned)DwarfReg;
1335 lex();
1336 return false;
1337}
1338
1339bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1340 auto Kind = Token.kind();
1341 lex();
1342 auto &MMI = MF.getMMI();
1343 int Offset;
1344 unsigned Reg;
1345 unsigned CFIIndex;
1346 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001347 case MIToken::kw_cfi_same_value:
1348 if (parseCFIRegister(Reg))
1349 return true;
1350 CFIIndex =
1351 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1352 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001353 case MIToken::kw_cfi_offset:
1354 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1355 parseCFIOffset(Offset))
1356 return true;
1357 CFIIndex =
1358 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1359 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001360 case MIToken::kw_cfi_def_cfa_register:
1361 if (parseCFIRegister(Reg))
1362 return true;
1363 CFIIndex =
1364 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1365 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001366 case MIToken::kw_cfi_def_cfa_offset:
1367 if (parseCFIOffset(Offset))
1368 return true;
1369 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1370 CFIIndex = MMI.addFrameInst(
1371 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1372 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001373 case MIToken::kw_cfi_def_cfa:
1374 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1375 parseCFIOffset(Offset))
1376 return true;
1377 // NB: MCCFIInstruction::createDefCfa negates the offset.
1378 CFIIndex =
1379 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1380 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001381 default:
1382 // TODO: Parse the other CFI operands.
1383 llvm_unreachable("The current token should be a cfi operand");
1384 }
1385 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001386 return false;
1387}
1388
Alex Lorenzdeb53492015-07-28 17:28:03 +00001389bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1390 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001391 case MIToken::NamedIRBlock: {
1392 BB = dyn_cast_or_null<BasicBlock>(
1393 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001394 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001395 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001396 break;
1397 }
1398 case MIToken::IRBlock: {
1399 unsigned SlotNumber = 0;
1400 if (getUnsigned(SlotNumber))
1401 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001402 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001403 if (!BB)
1404 return error(Twine("use of undefined IR block '%ir-block.") +
1405 Twine(SlotNumber) + "'");
1406 break;
1407 }
1408 default:
1409 llvm_unreachable("The current token should be an IR block reference");
1410 }
1411 return false;
1412}
1413
1414bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1415 assert(Token.is(MIToken::kw_blockaddress));
1416 lex();
1417 if (expectAndConsume(MIToken::lparen))
1418 return true;
1419 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001420 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001421 return error("expected a global value");
1422 GlobalValue *GV = nullptr;
1423 if (parseGlobalValue(GV))
1424 return true;
1425 auto *F = dyn_cast<Function>(GV);
1426 if (!F)
1427 return error("expected an IR function reference");
1428 lex();
1429 if (expectAndConsume(MIToken::comma))
1430 return true;
1431 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001432 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001433 return error("expected an IR block reference");
1434 if (parseIRBlock(BB, *F))
1435 return true;
1436 lex();
1437 if (expectAndConsume(MIToken::rparen))
1438 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001439 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001440 if (parseOperandsOffset(Dest))
1441 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001442 return false;
1443}
1444
Tim Northover6b3bd612016-07-29 20:32:59 +00001445bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
1446 assert(Token.is(MIToken::kw_intrinsic));
1447 lex();
1448 if (expectAndConsume(MIToken::lparen))
1449 return error("expected syntax intrinsic(@llvm.whatever)");
1450
1451 if (Token.isNot(MIToken::NamedGlobalValue))
1452 return error("expected syntax intrinsic(@llvm.whatever)");
1453
1454 std::string Name = Token.stringValue();
1455 lex();
1456
1457 if (expectAndConsume(MIToken::rparen))
1458 return error("expected ')' to terminate intrinsic name");
1459
1460 // Find out what intrinsic we're dealing with, first try the global namespace
1461 // and then the target's private intrinsics if that fails.
1462 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1463 Intrinsic::ID ID = Function::lookupIntrinsicID(Name);
1464 if (ID == Intrinsic::not_intrinsic && TII)
1465 ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
1466
1467 if (ID == Intrinsic::not_intrinsic)
1468 return error("unknown intrinsic name");
1469 Dest = MachineOperand::CreateIntrinsicID(ID);
1470
1471 return false;
1472}
1473
Tim Northoverde3aea0412016-08-17 20:25:25 +00001474bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
1475 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
1476 bool IsFloat = Token.is(MIToken::kw_floatpred);
1477 lex();
1478
1479 if (expectAndConsume(MIToken::lparen))
1480 return error("expected syntax intpred(whatever) or floatpred(whatever");
1481
1482 if (Token.isNot(MIToken::Identifier))
1483 return error("whatever");
1484
1485 CmpInst::Predicate Pred;
1486 if (IsFloat) {
1487 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1488 .Case("false", CmpInst::FCMP_FALSE)
1489 .Case("oeq", CmpInst::FCMP_OEQ)
1490 .Case("ogt", CmpInst::FCMP_OGT)
1491 .Case("oge", CmpInst::FCMP_OGE)
1492 .Case("olt", CmpInst::FCMP_OLT)
1493 .Case("ole", CmpInst::FCMP_OLE)
1494 .Case("one", CmpInst::FCMP_ONE)
1495 .Case("ord", CmpInst::FCMP_ORD)
1496 .Case("uno", CmpInst::FCMP_UNO)
1497 .Case("ueq", CmpInst::FCMP_UEQ)
1498 .Case("ugt", CmpInst::FCMP_UGT)
1499 .Case("uge", CmpInst::FCMP_UGE)
1500 .Case("ult", CmpInst::FCMP_ULT)
1501 .Case("ule", CmpInst::FCMP_ULE)
1502 .Case("une", CmpInst::FCMP_UNE)
1503 .Case("true", CmpInst::FCMP_TRUE)
1504 .Default(CmpInst::BAD_FCMP_PREDICATE);
1505 if (!CmpInst::isFPPredicate(Pred))
1506 return error("invalid floating-point predicate");
1507 } else {
1508 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1509 .Case("eq", CmpInst::ICMP_EQ)
1510 .Case("ne", CmpInst::ICMP_NE)
1511 .Case("sgt", CmpInst::ICMP_SGT)
1512 .Case("sge", CmpInst::ICMP_SGE)
1513 .Case("slt", CmpInst::ICMP_SLT)
1514 .Case("sle", CmpInst::ICMP_SLE)
1515 .Case("ugt", CmpInst::ICMP_UGT)
1516 .Case("uge", CmpInst::ICMP_UGE)
1517 .Case("ult", CmpInst::ICMP_ULT)
1518 .Case("ule", CmpInst::ICMP_ULE)
1519 .Default(CmpInst::BAD_ICMP_PREDICATE);
1520 if (!CmpInst::isIntPredicate(Pred))
1521 return error("invalid integer predicate");
1522 }
1523
1524 lex();
1525 Dest = MachineOperand::CreatePredicate(Pred);
Tim Northover6cd4b232016-08-23 21:01:26 +00001526 if (expectAndConsume(MIToken::rparen))
Tim Northoverde3aea0412016-08-17 20:25:25 +00001527 return error("predicate should be terminated by ')'.");
1528
1529 return false;
1530}
1531
Alex Lorenzef5c1962015-07-28 23:02:45 +00001532bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1533 assert(Token.is(MIToken::kw_target_index));
1534 lex();
1535 if (expectAndConsume(MIToken::lparen))
1536 return true;
1537 if (Token.isNot(MIToken::Identifier))
1538 return error("expected the name of the target index");
1539 int Index = 0;
1540 if (getTargetIndex(Token.stringValue(), Index))
1541 return error("use of undefined target index '" + Token.stringValue() + "'");
1542 lex();
1543 if (expectAndConsume(MIToken::rparen))
1544 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001545 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001546 if (parseOperandsOffset(Dest))
1547 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001548 return false;
1549}
1550
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001551bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1552 assert(Token.is(MIToken::kw_liveout));
1553 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1554 assert(TRI && "Expected target register info");
1555 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1556 lex();
1557 if (expectAndConsume(MIToken::lparen))
1558 return true;
1559 while (true) {
1560 if (Token.isNot(MIToken::NamedRegister))
1561 return error("expected a named register");
1562 unsigned Reg = 0;
1563 if (parseRegister(Reg))
1564 return true;
1565 lex();
1566 Mask[Reg / 32] |= 1U << (Reg % 32);
1567 // TODO: Report an error if the same register is used more than once.
1568 if (Token.isNot(MIToken::comma))
1569 break;
1570 lex();
1571 }
1572 if (expectAndConsume(MIToken::rparen))
1573 return true;
1574 Dest = MachineOperand::CreateRegLiveOut(Mask);
1575 return false;
1576}
1577
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001578bool MIParser::parseMachineOperand(MachineOperand &Dest,
1579 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001580 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001581 case MIToken::kw_implicit:
1582 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001583 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001584 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001585 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001586 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001587 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001588 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001589 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001590 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001591 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001592 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001593 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001594 case MIToken::IntegerLiteral:
1595 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001596 case MIToken::IntegerType:
1597 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001598 case MIToken::kw_half:
1599 case MIToken::kw_float:
1600 case MIToken::kw_double:
1601 case MIToken::kw_x86_fp80:
1602 case MIToken::kw_fp128:
1603 case MIToken::kw_ppc_fp128:
1604 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001605 case MIToken::MachineBasicBlock:
1606 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001607 case MIToken::StackObject:
1608 return parseStackObjectOperand(Dest);
1609 case MIToken::FixedStackObject:
1610 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001611 case MIToken::GlobalValue:
1612 case MIToken::NamedGlobalValue:
1613 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001614 case MIToken::ConstantPoolItem:
1615 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001616 case MIToken::JumpTableIndex:
1617 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001618 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001619 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00001620 case MIToken::SubRegisterIndex:
1621 return parseSubRegisterIndexOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001622 case MIToken::exclaim:
1623 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001624 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001625 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001626 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001627 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001628 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001629 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001630 case MIToken::kw_blockaddress:
1631 return parseBlockAddressOperand(Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +00001632 case MIToken::kw_intrinsic:
1633 return parseIntrinsicOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001634 case MIToken::kw_target_index:
1635 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001636 case MIToken::kw_liveout:
1637 return parseLiveoutRegisterMaskOperand(Dest);
Tim Northoverde3aea0412016-08-17 20:25:25 +00001638 case MIToken::kw_floatpred:
1639 case MIToken::kw_intpred:
1640 return parsePredicateOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001641 case MIToken::Error:
1642 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001643 case MIToken::Identifier:
1644 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1645 Dest = MachineOperand::CreateRegMask(RegMask);
1646 lex();
1647 break;
1648 }
Justin Bognerb03fd122016-08-17 05:10:15 +00001649 LLVM_FALLTHROUGH;
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001650 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001651 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001652 return error("expected a machine operand");
1653 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001654 return false;
1655}
1656
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001657bool MIParser::parseMachineOperandAndTargetFlags(
1658 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001659 unsigned TF = 0;
1660 bool HasTargetFlags = false;
1661 if (Token.is(MIToken::kw_target_flags)) {
1662 HasTargetFlags = true;
1663 lex();
1664 if (expectAndConsume(MIToken::lparen))
1665 return true;
1666 if (Token.isNot(MIToken::Identifier))
1667 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001668 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1669 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1670 return error("use of undefined target flag '" + Token.stringValue() +
1671 "'");
1672 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001673 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001674 while (Token.is(MIToken::comma)) {
1675 lex();
1676 if (Token.isNot(MIToken::Identifier))
1677 return error("expected the name of the target flag");
1678 unsigned BitFlag = 0;
1679 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1680 return error("use of undefined target flag '" + Token.stringValue() +
1681 "'");
1682 // TODO: Report an error when using a duplicate bit target flag.
1683 TF |= BitFlag;
1684 lex();
1685 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001686 if (expectAndConsume(MIToken::rparen))
1687 return true;
1688 }
1689 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001690 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001691 return true;
1692 if (!HasTargetFlags)
1693 return false;
1694 if (Dest.isReg())
1695 return error(Loc, "register operands can't have target flags");
1696 Dest.setTargetFlags(TF);
1697 return false;
1698}
1699
Alex Lorenzdc24c172015-08-07 20:21:00 +00001700bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001701 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1702 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001703 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001704 bool IsNegative = Token.is(MIToken::minus);
1705 lex();
1706 if (Token.isNot(MIToken::IntegerLiteral))
1707 return error("expected an integer literal after '" + Sign + "'");
1708 if (Token.integerValue().getMinSignedBits() > 64)
1709 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001710 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001711 if (IsNegative)
1712 Offset = -Offset;
1713 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001714 return false;
1715}
1716
Alex Lorenz620f8912015-08-13 20:33:33 +00001717bool MIParser::parseAlignment(unsigned &Alignment) {
1718 assert(Token.is(MIToken::kw_align));
1719 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001720 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001721 return error("expected an integer literal after 'align'");
1722 if (getUnsigned(Alignment))
1723 return true;
1724 lex();
1725 return false;
1726}
1727
Alex Lorenzdc24c172015-08-07 20:21:00 +00001728bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1729 int64_t Offset = 0;
1730 if (parseOffset(Offset))
1731 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001732 Op.setOffset(Offset);
1733 return false;
1734}
1735
Alex Lorenz36593ac2015-08-19 23:27:07 +00001736bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001737 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001738 case MIToken::NamedIRValue: {
1739 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001740 break;
1741 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001742 case MIToken::IRValue: {
1743 unsigned SlotNumber = 0;
1744 if (getUnsigned(SlotNumber))
1745 return true;
1746 V = getIRValue(SlotNumber);
1747 break;
1748 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001749 case MIToken::NamedGlobalValue:
1750 case MIToken::GlobalValue: {
1751 GlobalValue *GV = nullptr;
1752 if (parseGlobalValue(GV))
1753 return true;
1754 V = GV;
1755 break;
1756 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001757 case MIToken::QuotedIRValue: {
1758 const Constant *C = nullptr;
1759 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1760 return true;
1761 V = C;
1762 break;
1763 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001764 default:
1765 llvm_unreachable("The current token should be an IR block reference");
1766 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001767 if (!V)
1768 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001769 return false;
1770}
1771
1772bool MIParser::getUint64(uint64_t &Result) {
1773 assert(Token.hasIntegerValue());
1774 if (Token.integerValue().getActiveBits() > 64)
1775 return error("expected 64-bit integer (too large)");
1776 Result = Token.integerValue().getZExtValue();
1777 return false;
1778}
1779
Justin Lebar0af80cd2016-07-15 18:26:59 +00001780bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
1781 const auto OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001782 switch (Token.kind()) {
1783 case MIToken::kw_volatile:
1784 Flags |= MachineMemOperand::MOVolatile;
1785 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001786 case MIToken::kw_non_temporal:
1787 Flags |= MachineMemOperand::MONonTemporal;
1788 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001789 case MIToken::kw_invariant:
1790 Flags |= MachineMemOperand::MOInvariant;
1791 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001792 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001793 default:
1794 llvm_unreachable("The current token should be a memory operand flag");
1795 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001796 if (OldFlags == Flags)
1797 // We know that the same flag is specified more than once when the flags
1798 // weren't modified.
1799 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001800 lex();
1801 return false;
1802}
1803
Alex Lorenz91097a32015-08-12 20:33:26 +00001804bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1805 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001806 case MIToken::kw_stack:
1807 PSV = MF.getPSVManager().getStack();
1808 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001809 case MIToken::kw_got:
1810 PSV = MF.getPSVManager().getGOT();
1811 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001812 case MIToken::kw_jump_table:
1813 PSV = MF.getPSVManager().getJumpTable();
1814 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001815 case MIToken::kw_constant_pool:
1816 PSV = MF.getPSVManager().getConstantPool();
1817 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001818 case MIToken::FixedStackObject: {
1819 int FI;
1820 if (parseFixedStackFrameIndex(FI))
1821 return true;
1822 PSV = MF.getPSVManager().getFixedStack(FI);
1823 // The token was already consumed, so use return here instead of break.
1824 return false;
1825 }
Matthias Braun3ef7df92016-06-08 00:47:07 +00001826 case MIToken::StackObject: {
1827 int FI;
1828 if (parseStackFrameIndex(FI))
1829 return true;
1830 PSV = MF.getPSVManager().getFixedStack(FI);
1831 // The token was already consumed, so use return here instead of break.
1832 return false;
1833 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001834 case MIToken::kw_call_entry: {
1835 lex();
1836 switch (Token.kind()) {
1837 case MIToken::GlobalValue:
1838 case MIToken::NamedGlobalValue: {
1839 GlobalValue *GV = nullptr;
1840 if (parseGlobalValue(GV))
1841 return true;
1842 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1843 break;
1844 }
1845 case MIToken::ExternalSymbol:
1846 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1847 MF.createExternalSymbolName(Token.stringValue()));
1848 break;
1849 default:
1850 return error(
1851 "expected a global value or an external symbol after 'call-entry'");
1852 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001853 break;
1854 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001855 default:
1856 llvm_unreachable("The current token should be pseudo source value");
1857 }
1858 lex();
1859 return false;
1860}
1861
1862bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001863 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001864 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Matthias Braun3ef7df92016-06-08 00:47:07 +00001865 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
1866 Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001867 const PseudoSourceValue *PSV = nullptr;
1868 if (parseMemoryPseudoSourceValue(PSV))
1869 return true;
1870 int64_t Offset = 0;
1871 if (parseOffset(Offset))
1872 return true;
1873 Dest = MachinePointerInfo(PSV, Offset);
1874 return false;
1875 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001876 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1877 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001878 Token.isNot(MIToken::NamedGlobalValue) &&
1879 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001880 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001881 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001882 if (parseIRValue(V))
1883 return true;
1884 if (!V->getType()->isPointerTy())
1885 return error("expected a pointer IR value");
1886 lex();
1887 int64_t Offset = 0;
1888 if (parseOffset(Offset))
1889 return true;
1890 Dest = MachinePointerInfo(V, Offset);
1891 return false;
1892}
1893
Alex Lorenz4af7e612015-08-03 23:08:19 +00001894bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1895 if (expectAndConsume(MIToken::lparen))
1896 return true;
Justin Lebar0af80cd2016-07-15 18:26:59 +00001897 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
Alex Lorenza518b792015-08-04 00:24:45 +00001898 while (Token.isMemoryOperandFlag()) {
1899 if (parseMemoryOperandFlag(Flags))
1900 return true;
1901 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001902 if (Token.isNot(MIToken::Identifier) ||
1903 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1904 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001905 if (Token.stringValue() == "load")
1906 Flags |= MachineMemOperand::MOLoad;
1907 else
1908 Flags |= MachineMemOperand::MOStore;
1909 lex();
1910
1911 if (Token.isNot(MIToken::IntegerLiteral))
1912 return error("expected the size integer literal after memory operation");
1913 uint64_t Size;
1914 if (getUint64(Size))
1915 return true;
1916 lex();
1917
Alex Lorenz91097a32015-08-12 20:33:26 +00001918 MachinePointerInfo Ptr = MachinePointerInfo();
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001919 if (Token.is(MIToken::Identifier)) {
1920 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1921 if (Token.stringValue() != Word)
1922 return error(Twine("expected '") + Word + "'");
1923 lex();
1924
1925 if (parseMachinePointerInfo(Ptr))
1926 return true;
1927 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001928 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001929 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001930 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001931 while (consumeIfPresent(MIToken::comma)) {
1932 switch (Token.kind()) {
1933 case MIToken::kw_align:
1934 if (parseAlignment(BaseAlignment))
1935 return true;
1936 break;
1937 case MIToken::md_tbaa:
1938 lex();
1939 if (parseMDNode(AAInfo.TBAA))
1940 return true;
1941 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001942 case MIToken::md_alias_scope:
1943 lex();
1944 if (parseMDNode(AAInfo.Scope))
1945 return true;
1946 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001947 case MIToken::md_noalias:
1948 lex();
1949 if (parseMDNode(AAInfo.NoAlias))
1950 return true;
1951 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001952 case MIToken::md_range:
1953 lex();
1954 if (parseMDNode(Range))
1955 return true;
1956 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001957 // TODO: Report an error on duplicate metadata nodes.
1958 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001959 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1960 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001961 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001962 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001963 if (expectAndConsume(MIToken::rparen))
1964 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001965 Dest =
1966 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001967 return false;
1968}
1969
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001970void MIParser::initNames2InstrOpCodes() {
1971 if (!Names2InstrOpCodes.empty())
1972 return;
1973 const auto *TII = MF.getSubtarget().getInstrInfo();
1974 assert(TII && "Expected target instruction info");
1975 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1976 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1977}
1978
1979bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1980 initNames2InstrOpCodes();
1981 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1982 if (InstrInfo == Names2InstrOpCodes.end())
1983 return true;
1984 OpCode = InstrInfo->getValue();
1985 return false;
1986}
1987
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001988void MIParser::initNames2Regs() {
1989 if (!Names2Regs.empty())
1990 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001991 // The '%noreg' register is the register 0.
1992 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001993 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1994 assert(TRI && "Expected target register info");
1995 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1996 bool WasInserted =
1997 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1998 .second;
1999 (void)WasInserted;
2000 assert(WasInserted && "Expected registers to be unique case-insensitively");
2001 }
2002}
2003
2004bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
2005 initNames2Regs();
2006 auto RegInfo = Names2Regs.find(RegName);
2007 if (RegInfo == Names2Regs.end())
2008 return true;
2009 Reg = RegInfo->getValue();
2010 return false;
2011}
2012
Alex Lorenz8f6f4282015-06-29 16:57:06 +00002013void MIParser::initNames2RegMasks() {
2014 if (!Names2RegMasks.empty())
2015 return;
2016 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2017 assert(TRI && "Expected target register info");
2018 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
2019 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
2020 assert(RegMasks.size() == RegMaskNames.size());
2021 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
2022 Names2RegMasks.insert(
2023 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
2024}
2025
2026const uint32_t *MIParser::getRegMask(StringRef Identifier) {
2027 initNames2RegMasks();
2028 auto RegMaskInfo = Names2RegMasks.find(Identifier);
2029 if (RegMaskInfo == Names2RegMasks.end())
2030 return nullptr;
2031 return RegMaskInfo->getValue();
2032}
2033
Alex Lorenz2eacca82015-07-13 23:24:34 +00002034void MIParser::initNames2SubRegIndices() {
2035 if (!Names2SubRegIndices.empty())
2036 return;
2037 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2038 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
2039 Names2SubRegIndices.insert(
2040 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
2041}
2042
2043unsigned MIParser::getSubRegIndex(StringRef Name) {
2044 initNames2SubRegIndices();
2045 auto SubRegInfo = Names2SubRegIndices.find(Name);
2046 if (SubRegInfo == Names2SubRegIndices.end())
2047 return 0;
2048 return SubRegInfo->getValue();
2049}
2050
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002051static void initSlots2BasicBlocks(
2052 const Function &F,
2053 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2054 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002055 MST.incorporateFunction(F);
2056 for (auto &BB : F) {
2057 if (BB.hasName())
2058 continue;
2059 int Slot = MST.getLocalSlot(&BB);
2060 if (Slot == -1)
2061 continue;
2062 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2063 }
2064}
2065
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002066static const BasicBlock *getIRBlockFromSlot(
2067 unsigned Slot,
2068 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002069 auto BlockInfo = Slots2BasicBlocks.find(Slot);
2070 if (BlockInfo == Slots2BasicBlocks.end())
2071 return nullptr;
2072 return BlockInfo->second;
2073}
2074
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002075const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2076 if (Slots2BasicBlocks.empty())
2077 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
2078 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2079}
2080
2081const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2082 if (&F == MF.getFunction())
2083 return getIRBlock(Slot);
2084 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2085 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2086 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2087}
2088
Alex Lorenzdd13be02015-08-19 23:31:05 +00002089static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2090 DenseMap<unsigned, const Value *> &Slots2Values) {
2091 int Slot = MST.getLocalSlot(V);
2092 if (Slot == -1)
2093 return;
2094 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2095}
2096
2097/// Creates the mapping from slot numbers to function's unnamed IR values.
2098static void initSlots2Values(const Function &F,
2099 DenseMap<unsigned, const Value *> &Slots2Values) {
2100 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2101 MST.incorporateFunction(F);
2102 for (const auto &Arg : F.args())
2103 mapValueToSlot(&Arg, MST, Slots2Values);
2104 for (const auto &BB : F) {
2105 mapValueToSlot(&BB, MST, Slots2Values);
2106 for (const auto &I : BB)
2107 mapValueToSlot(&I, MST, Slots2Values);
2108 }
2109}
2110
2111const Value *MIParser::getIRValue(unsigned Slot) {
2112 if (Slots2Values.empty())
2113 initSlots2Values(*MF.getFunction(), Slots2Values);
2114 auto ValueInfo = Slots2Values.find(Slot);
2115 if (ValueInfo == Slots2Values.end())
2116 return nullptr;
2117 return ValueInfo->second;
2118}
2119
Alex Lorenzef5c1962015-07-28 23:02:45 +00002120void MIParser::initNames2TargetIndices() {
2121 if (!Names2TargetIndices.empty())
2122 return;
2123 const auto *TII = MF.getSubtarget().getInstrInfo();
2124 assert(TII && "Expected target instruction info");
2125 auto Indices = TII->getSerializableTargetIndices();
2126 for (const auto &I : Indices)
2127 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2128}
2129
2130bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2131 initNames2TargetIndices();
2132 auto IndexInfo = Names2TargetIndices.find(Name);
2133 if (IndexInfo == Names2TargetIndices.end())
2134 return true;
2135 Index = IndexInfo->second;
2136 return false;
2137}
2138
Alex Lorenz49873a82015-08-06 00:44:07 +00002139void MIParser::initNames2DirectTargetFlags() {
2140 if (!Names2DirectTargetFlags.empty())
2141 return;
2142 const auto *TII = MF.getSubtarget().getInstrInfo();
2143 assert(TII && "Expected target instruction info");
2144 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2145 for (const auto &I : Flags)
2146 Names2DirectTargetFlags.insert(
2147 std::make_pair(StringRef(I.second), I.first));
2148}
2149
2150bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2151 initNames2DirectTargetFlags();
2152 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2153 if (FlagInfo == Names2DirectTargetFlags.end())
2154 return true;
2155 Flag = FlagInfo->second;
2156 return false;
2157}
2158
Alex Lorenzf3630112015-08-18 22:52:15 +00002159void MIParser::initNames2BitmaskTargetFlags() {
2160 if (!Names2BitmaskTargetFlags.empty())
2161 return;
2162 const auto *TII = MF.getSubtarget().getInstrInfo();
2163 assert(TII && "Expected target instruction info");
2164 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2165 for (const auto &I : Flags)
2166 Names2BitmaskTargetFlags.insert(
2167 std::make_pair(StringRef(I.second), I.first));
2168}
2169
2170bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2171 initNames2BitmaskTargetFlags();
2172 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2173 if (FlagInfo == Names2BitmaskTargetFlags.end())
2174 return true;
2175 Flag = FlagInfo->second;
2176 return false;
2177}
2178
Matthias Braun83947862016-07-13 22:23:23 +00002179bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2180 StringRef Src,
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002181 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002182 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002183}
2184
Matthias Braun83947862016-07-13 22:23:23 +00002185bool llvm::parseMachineInstructions(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002186 StringRef Src, SMDiagnostic &Error) {
2187 return MIParser(PFS, Error, Src).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002188}
Alex Lorenzf09df002015-06-30 18:16:42 +00002189
Matthias Braun83947862016-07-13 22:23:23 +00002190bool llvm::parseMBBReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002191 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +00002192 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002193 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002194}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002195
Matthias Braun83947862016-07-13 22:23:23 +00002196bool llvm::parseNamedRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002197 unsigned &Reg, StringRef Src,
Alex Lorenz9fab3702015-07-14 21:24:41 +00002198 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002199 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002200}
Alex Lorenz12045a42015-07-27 17:42:45 +00002201
Matthias Braun83947862016-07-13 22:23:23 +00002202bool llvm::parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002203 unsigned &Reg, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +00002204 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002205 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +00002206}
Alex Lorenza314d812015-08-18 22:26:26 +00002207
Matthias Braun83947862016-07-13 22:23:23 +00002208bool llvm::parseStackObjectReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002209 int &FI, StringRef Src,
Alex Lorenza314d812015-08-18 22:26:26 +00002210 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002211 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
Alex Lorenza314d812015-08-18 22:26:26 +00002212}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002213
Matthias Braun83947862016-07-13 22:23:23 +00002214bool llvm::parseMDNode(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002215 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2216 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002217}