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