blob: 696ba48e97c20bc79f67c0a0192c14ee249c0f03 [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))
880 return error("expected 'tied-def' after '('");
881 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 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000960 if ((Flags & RegState::Define) == 0) {
961 if (consumeIfPresent(MIToken::lparen)) {
962 unsigned Idx;
963 if (parseRegisterTiedDefIndex(Idx))
964 return true;
965 TiedDefIdx = Idx;
966 }
967 } else if (consumeIfPresent(MIToken::lparen)) {
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000968 MachineRegisterInfo &MRI = MF.getRegInfo();
969
Quentin Colombet2c646962016-06-08 23:27:46 +0000970 // Virtual registers may have a size with GlobalISel.
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000971 if (!TargetRegisterInfo::isVirtualRegister(Reg))
972 return error("unexpected size on physical register");
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000973 if (MRI.getRegClassOrRegBank(Reg).is<const TargetRegisterClass *>())
974 return error("unexpected size on non-generic virtual register");
975
Tim Northover0f140c72016-09-09 11:46:34 +0000976 LLT Ty;
977 if (parseLowLevelType(Token.location(), Ty))
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000978 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000979
Tim Northover0f140c72016-09-09 11:46:34 +0000980 if (expectAndConsume(MIToken::rparen))
981 return true;
982
983 MRI.setType(Reg, Ty);
Quentin Colombet2c646962016-06-08 23:27:46 +0000984 } else if (PFS.GenericVRegs.count(Reg)) {
985 // Generic virtual registers must have a size.
986 // If we end up here this means the size hasn't been specified and
987 // this is bad!
988 return error("generic virtual registers must have a size");
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000989 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000990 Dest = MachineOperand::CreateReg(
991 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000992 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +0000993 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
994 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000995 return false;
996}
997
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000998bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
999 assert(Token.is(MIToken::IntegerLiteral));
1000 const APSInt &Int = Token.integerValue();
1001 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +00001002 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001003 Dest = MachineOperand::CreateImm(Int.getExtValue());
1004 lex();
1005 return false;
1006}
1007
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001008bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1009 const Constant *&C) {
1010 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001011 SMDiagnostic Err;
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001012 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
Matthias Braune35861d2016-07-13 23:27:50 +00001013 &PFS.IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001014 if (!C)
1015 return error(Loc + Err.getColumnNo(), Err.getMessage());
1016 return false;
1017}
1018
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001019bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1020 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1021 return true;
1022 lex();
1023 return false;
1024}
1025
Ahmed Bougachad760de02016-07-28 17:15:12 +00001026bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
Tim Northover62ae5682016-07-20 19:09:30 +00001027 if (Token.is(MIToken::Identifier) && Token.stringValue() == "unsized") {
Tim Northover62ae5682016-07-20 19:09:30 +00001028 lex();
1029 Ty = LLT::unsized();
1030 return false;
1031 } else if (Token.is(MIToken::ScalarType)) {
1032 Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1033 lex();
1034 return false;
Tim Northoverbd505462016-07-22 16:59:52 +00001035 } else if (Token.is(MIToken::PointerType)) {
1036 Ty = LLT::pointer(APSInt(Token.range().drop_front()).getZExtValue());
1037 lex();
1038 return false;
Tim Northover62ae5682016-07-20 19:09:30 +00001039 }
Quentin Colombet85199672016-03-08 00:20:48 +00001040
Tim Northover62ae5682016-07-20 19:09:30 +00001041 // Now we're looking for a vector.
1042 if (Token.isNot(MIToken::less))
Tim Northoverbd505462016-07-22 16:59:52 +00001043 return error(Loc,
1044 "expected unsized, pN, sN or <N x sM> for GlobalISel type");
1045
Tim Northover62ae5682016-07-20 19:09:30 +00001046 lex();
1047
1048 if (Token.isNot(MIToken::IntegerLiteral))
1049 return error(Loc, "expected <N x sM> for vctor type");
1050 uint64_t NumElements = Token.integerValue().getZExtValue();
1051 lex();
1052
1053 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1054 return error(Loc, "expected '<N x sM>' for vector type");
1055 lex();
1056
1057 if (Token.isNot(MIToken::ScalarType))
1058 return error(Loc, "expected '<N x sM>' for vector type");
1059 uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1060 lex();
1061
1062 if (Token.isNot(MIToken::greater))
1063 return error(Loc, "expected '<N x sM>' for vector type");
1064 lex();
1065
1066 Ty = LLT::vector(NumElements, ScalarSize);
Quentin Colombet85199672016-03-08 00:20:48 +00001067 return false;
1068}
1069
Alex Lorenz05e38822015-08-05 18:52:21 +00001070bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1071 assert(Token.is(MIToken::IntegerType));
1072 auto Loc = Token.location();
1073 lex();
1074 if (Token.isNot(MIToken::IntegerLiteral))
1075 return error("expected an integer literal");
1076 const Constant *C = nullptr;
1077 if (parseIRConstant(Loc, C))
1078 return true;
1079 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1080 return false;
1081}
1082
Alex Lorenzad156fb2015-07-31 20:49:21 +00001083bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1084 auto Loc = Token.location();
1085 lex();
1086 if (Token.isNot(MIToken::FloatingPointLiteral))
1087 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001088 const Constant *C = nullptr;
1089 if (parseIRConstant(Loc, C))
1090 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001091 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1092 return false;
1093}
1094
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001095bool MIParser::getUnsigned(unsigned &Result) {
1096 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1097 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1098 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1099 if (Val64 == Limit)
1100 return error("expected 32-bit integer (too large)");
1101 Result = Val64;
1102 return false;
1103}
1104
Alex Lorenzf09df002015-06-30 18:16:42 +00001105bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001106 assert(Token.is(MIToken::MachineBasicBlock) ||
1107 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001108 unsigned Number;
1109 if (getUnsigned(Number))
1110 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001111 auto MBBInfo = PFS.MBBSlots.find(Number);
1112 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001113 return error(Twine("use of undefined machine basic block #") +
1114 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001115 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001116 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1117 return error(Twine("the name of machine basic block #") + Twine(Number) +
1118 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001119 return false;
1120}
1121
1122bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1123 MachineBasicBlock *MBB;
1124 if (parseMBBReference(MBB))
1125 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001126 Dest = MachineOperand::CreateMBB(MBB);
1127 lex();
1128 return false;
1129}
1130
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001131bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001132 assert(Token.is(MIToken::StackObject));
1133 unsigned ID;
1134 if (getUnsigned(ID))
1135 return true;
1136 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1137 if (ObjectInfo == PFS.StackObjectSlots.end())
1138 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1139 "'");
1140 StringRef Name;
1141 if (const auto *Alloca =
Matthias Braun941a7052016-07-28 18:40:00 +00001142 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001143 Name = Alloca->getName();
1144 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1145 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1146 "' isn't '" + Token.stringValue() + "'");
1147 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001148 FI = ObjectInfo->second;
1149 return false;
1150}
1151
1152bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1153 int FI;
1154 if (parseStackFrameIndex(FI))
1155 return true;
1156 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001157 return false;
1158}
1159
Alex Lorenzea882122015-08-12 21:17:02 +00001160bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001161 assert(Token.is(MIToken::FixedStackObject));
1162 unsigned ID;
1163 if (getUnsigned(ID))
1164 return true;
1165 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1166 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1167 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1168 Twine(ID) + "'");
1169 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001170 FI = ObjectInfo->second;
1171 return false;
1172}
1173
1174bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1175 int FI;
1176 if (parseFixedStackFrameIndex(FI))
1177 return true;
1178 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001179 return false;
1180}
1181
Alex Lorenz41df7d32015-07-28 17:09:52 +00001182bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001183 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001184 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001185 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001186 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001187 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001188 return error(Twine("use of undefined global value '") + Token.range() +
1189 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001190 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001191 }
1192 case MIToken::GlobalValue: {
1193 unsigned GVIdx;
1194 if (getUnsigned(GVIdx))
1195 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001196 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001197 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1198 "'");
Matthias Braune35861d2016-07-13 23:27:50 +00001199 GV = PFS.IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001200 break;
1201 }
1202 default:
1203 llvm_unreachable("The current token should be a global value");
1204 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001205 return false;
1206}
1207
1208bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1209 GlobalValue *GV = nullptr;
1210 if (parseGlobalValue(GV))
1211 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001212 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001213 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001214 if (parseOperandsOffset(Dest))
1215 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001216 return false;
1217}
1218
Alex Lorenzab980492015-07-20 20:51:18 +00001219bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1220 assert(Token.is(MIToken::ConstantPoolItem));
1221 unsigned ID;
1222 if (getUnsigned(ID))
1223 return true;
1224 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1225 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1226 return error("use of undefined constant '%const." + Twine(ID) + "'");
1227 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001228 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001229 if (parseOperandsOffset(Dest))
1230 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001231 return false;
1232}
1233
Alex Lorenz31d70682015-07-15 23:38:35 +00001234bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1235 assert(Token.is(MIToken::JumpTableIndex));
1236 unsigned ID;
1237 if (getUnsigned(ID))
1238 return true;
1239 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1240 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1241 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1242 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001243 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1244 return false;
1245}
1246
Alex Lorenz6ede3742015-07-21 16:59:53 +00001247bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001248 assert(Token.is(MIToken::ExternalSymbol));
1249 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001250 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001251 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001252 if (parseOperandsOffset(Dest))
1253 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001254 return false;
1255}
1256
Matthias Braunb74eb412016-03-28 18:18:46 +00001257bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1258 assert(Token.is(MIToken::SubRegisterIndex));
1259 StringRef Name = Token.stringValue();
1260 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1261 if (SubRegIndex == 0)
1262 return error(Twine("unknown subregister index '") + Name + "'");
1263 lex();
1264 Dest = MachineOperand::CreateImm(SubRegIndex);
1265 return false;
1266}
1267
Alex Lorenz44f29252015-07-22 21:07:04 +00001268bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001269 assert(Token.is(MIToken::exclaim));
1270 auto Loc = Token.location();
1271 lex();
1272 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1273 return error("expected metadata id after '!'");
1274 unsigned ID;
1275 if (getUnsigned(ID))
1276 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001277 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1278 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
Alex Lorenz35e44462015-07-22 17:58:46 +00001279 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1280 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001281 Node = NodeInfo->second.get();
1282 return false;
1283}
1284
1285bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1286 MDNode *Node = nullptr;
1287 if (parseMDNode(Node))
1288 return true;
1289 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001290 return false;
1291}
1292
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001293bool MIParser::parseCFIOffset(int &Offset) {
1294 if (Token.isNot(MIToken::IntegerLiteral))
1295 return error("expected a cfi offset");
1296 if (Token.integerValue().getMinSignedBits() > 32)
1297 return error("expected a 32 bit integer (the cfi offset is too large)");
1298 Offset = (int)Token.integerValue().getExtValue();
1299 lex();
1300 return false;
1301}
1302
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001303bool MIParser::parseCFIRegister(unsigned &Reg) {
1304 if (Token.isNot(MIToken::NamedRegister))
1305 return error("expected a cfi register");
1306 unsigned LLVMReg;
1307 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001308 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001309 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1310 assert(TRI && "Expected target register info");
1311 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1312 if (DwarfReg < 0)
1313 return error("invalid DWARF register");
1314 Reg = (unsigned)DwarfReg;
1315 lex();
1316 return false;
1317}
1318
1319bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1320 auto Kind = Token.kind();
1321 lex();
1322 auto &MMI = MF.getMMI();
1323 int Offset;
1324 unsigned Reg;
1325 unsigned CFIIndex;
1326 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001327 case MIToken::kw_cfi_same_value:
1328 if (parseCFIRegister(Reg))
1329 return true;
1330 CFIIndex =
1331 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1332 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001333 case MIToken::kw_cfi_offset:
1334 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1335 parseCFIOffset(Offset))
1336 return true;
1337 CFIIndex =
1338 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1339 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001340 case MIToken::kw_cfi_def_cfa_register:
1341 if (parseCFIRegister(Reg))
1342 return true;
1343 CFIIndex =
1344 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1345 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001346 case MIToken::kw_cfi_def_cfa_offset:
1347 if (parseCFIOffset(Offset))
1348 return true;
1349 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1350 CFIIndex = MMI.addFrameInst(
1351 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1352 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001353 case MIToken::kw_cfi_def_cfa:
1354 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1355 parseCFIOffset(Offset))
1356 return true;
1357 // NB: MCCFIInstruction::createDefCfa negates the offset.
1358 CFIIndex =
1359 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1360 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001361 default:
1362 // TODO: Parse the other CFI operands.
1363 llvm_unreachable("The current token should be a cfi operand");
1364 }
1365 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001366 return false;
1367}
1368
Alex Lorenzdeb53492015-07-28 17:28:03 +00001369bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1370 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001371 case MIToken::NamedIRBlock: {
1372 BB = dyn_cast_or_null<BasicBlock>(
1373 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001374 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001375 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001376 break;
1377 }
1378 case MIToken::IRBlock: {
1379 unsigned SlotNumber = 0;
1380 if (getUnsigned(SlotNumber))
1381 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001382 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001383 if (!BB)
1384 return error(Twine("use of undefined IR block '%ir-block.") +
1385 Twine(SlotNumber) + "'");
1386 break;
1387 }
1388 default:
1389 llvm_unreachable("The current token should be an IR block reference");
1390 }
1391 return false;
1392}
1393
1394bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1395 assert(Token.is(MIToken::kw_blockaddress));
1396 lex();
1397 if (expectAndConsume(MIToken::lparen))
1398 return true;
1399 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001400 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001401 return error("expected a global value");
1402 GlobalValue *GV = nullptr;
1403 if (parseGlobalValue(GV))
1404 return true;
1405 auto *F = dyn_cast<Function>(GV);
1406 if (!F)
1407 return error("expected an IR function reference");
1408 lex();
1409 if (expectAndConsume(MIToken::comma))
1410 return true;
1411 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001412 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001413 return error("expected an IR block reference");
1414 if (parseIRBlock(BB, *F))
1415 return true;
1416 lex();
1417 if (expectAndConsume(MIToken::rparen))
1418 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001419 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001420 if (parseOperandsOffset(Dest))
1421 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001422 return false;
1423}
1424
Tim Northover6b3bd612016-07-29 20:32:59 +00001425bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
1426 assert(Token.is(MIToken::kw_intrinsic));
1427 lex();
1428 if (expectAndConsume(MIToken::lparen))
1429 return error("expected syntax intrinsic(@llvm.whatever)");
1430
1431 if (Token.isNot(MIToken::NamedGlobalValue))
1432 return error("expected syntax intrinsic(@llvm.whatever)");
1433
1434 std::string Name = Token.stringValue();
1435 lex();
1436
1437 if (expectAndConsume(MIToken::rparen))
1438 return error("expected ')' to terminate intrinsic name");
1439
1440 // Find out what intrinsic we're dealing with, first try the global namespace
1441 // and then the target's private intrinsics if that fails.
1442 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1443 Intrinsic::ID ID = Function::lookupIntrinsicID(Name);
1444 if (ID == Intrinsic::not_intrinsic && TII)
1445 ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
1446
1447 if (ID == Intrinsic::not_intrinsic)
1448 return error("unknown intrinsic name");
1449 Dest = MachineOperand::CreateIntrinsicID(ID);
1450
1451 return false;
1452}
1453
Tim Northoverde3aea0412016-08-17 20:25:25 +00001454bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
1455 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
1456 bool IsFloat = Token.is(MIToken::kw_floatpred);
1457 lex();
1458
1459 if (expectAndConsume(MIToken::lparen))
1460 return error("expected syntax intpred(whatever) or floatpred(whatever");
1461
1462 if (Token.isNot(MIToken::Identifier))
1463 return error("whatever");
1464
1465 CmpInst::Predicate Pred;
1466 if (IsFloat) {
1467 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1468 .Case("false", CmpInst::FCMP_FALSE)
1469 .Case("oeq", CmpInst::FCMP_OEQ)
1470 .Case("ogt", CmpInst::FCMP_OGT)
1471 .Case("oge", CmpInst::FCMP_OGE)
1472 .Case("olt", CmpInst::FCMP_OLT)
1473 .Case("ole", CmpInst::FCMP_OLE)
1474 .Case("one", CmpInst::FCMP_ONE)
1475 .Case("ord", CmpInst::FCMP_ORD)
1476 .Case("uno", CmpInst::FCMP_UNO)
1477 .Case("ueq", CmpInst::FCMP_UEQ)
1478 .Case("ugt", CmpInst::FCMP_UGT)
1479 .Case("uge", CmpInst::FCMP_UGE)
1480 .Case("ult", CmpInst::FCMP_ULT)
1481 .Case("ule", CmpInst::FCMP_ULE)
1482 .Case("une", CmpInst::FCMP_UNE)
1483 .Case("true", CmpInst::FCMP_TRUE)
1484 .Default(CmpInst::BAD_FCMP_PREDICATE);
1485 if (!CmpInst::isFPPredicate(Pred))
1486 return error("invalid floating-point predicate");
1487 } else {
1488 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1489 .Case("eq", CmpInst::ICMP_EQ)
1490 .Case("ne", CmpInst::ICMP_NE)
1491 .Case("sgt", CmpInst::ICMP_SGT)
1492 .Case("sge", CmpInst::ICMP_SGE)
1493 .Case("slt", CmpInst::ICMP_SLT)
1494 .Case("sle", CmpInst::ICMP_SLE)
1495 .Case("ugt", CmpInst::ICMP_UGT)
1496 .Case("uge", CmpInst::ICMP_UGE)
1497 .Case("ult", CmpInst::ICMP_ULT)
1498 .Case("ule", CmpInst::ICMP_ULE)
1499 .Default(CmpInst::BAD_ICMP_PREDICATE);
1500 if (!CmpInst::isIntPredicate(Pred))
1501 return error("invalid integer predicate");
1502 }
1503
1504 lex();
1505 Dest = MachineOperand::CreatePredicate(Pred);
Tim Northover6cd4b232016-08-23 21:01:26 +00001506 if (expectAndConsume(MIToken::rparen))
Tim Northoverde3aea0412016-08-17 20:25:25 +00001507 return error("predicate should be terminated by ')'.");
1508
1509 return false;
1510}
1511
Alex Lorenzef5c1962015-07-28 23:02:45 +00001512bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1513 assert(Token.is(MIToken::kw_target_index));
1514 lex();
1515 if (expectAndConsume(MIToken::lparen))
1516 return true;
1517 if (Token.isNot(MIToken::Identifier))
1518 return error("expected the name of the target index");
1519 int Index = 0;
1520 if (getTargetIndex(Token.stringValue(), Index))
1521 return error("use of undefined target index '" + Token.stringValue() + "'");
1522 lex();
1523 if (expectAndConsume(MIToken::rparen))
1524 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001525 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001526 if (parseOperandsOffset(Dest))
1527 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001528 return false;
1529}
1530
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001531bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1532 assert(Token.is(MIToken::kw_liveout));
1533 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1534 assert(TRI && "Expected target register info");
1535 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1536 lex();
1537 if (expectAndConsume(MIToken::lparen))
1538 return true;
1539 while (true) {
1540 if (Token.isNot(MIToken::NamedRegister))
1541 return error("expected a named register");
1542 unsigned Reg = 0;
1543 if (parseRegister(Reg))
1544 return true;
1545 lex();
1546 Mask[Reg / 32] |= 1U << (Reg % 32);
1547 // TODO: Report an error if the same register is used more than once.
1548 if (Token.isNot(MIToken::comma))
1549 break;
1550 lex();
1551 }
1552 if (expectAndConsume(MIToken::rparen))
1553 return true;
1554 Dest = MachineOperand::CreateRegLiveOut(Mask);
1555 return false;
1556}
1557
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001558bool MIParser::parseMachineOperand(MachineOperand &Dest,
1559 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001560 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001561 case MIToken::kw_implicit:
1562 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001563 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001564 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001565 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001566 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001567 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001568 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001569 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001570 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001571 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001572 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001573 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001574 case MIToken::IntegerLiteral:
1575 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001576 case MIToken::IntegerType:
1577 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001578 case MIToken::kw_half:
1579 case MIToken::kw_float:
1580 case MIToken::kw_double:
1581 case MIToken::kw_x86_fp80:
1582 case MIToken::kw_fp128:
1583 case MIToken::kw_ppc_fp128:
1584 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001585 case MIToken::MachineBasicBlock:
1586 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001587 case MIToken::StackObject:
1588 return parseStackObjectOperand(Dest);
1589 case MIToken::FixedStackObject:
1590 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001591 case MIToken::GlobalValue:
1592 case MIToken::NamedGlobalValue:
1593 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001594 case MIToken::ConstantPoolItem:
1595 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001596 case MIToken::JumpTableIndex:
1597 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001598 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001599 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00001600 case MIToken::SubRegisterIndex:
1601 return parseSubRegisterIndexOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001602 case MIToken::exclaim:
1603 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001604 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001605 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001606 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001607 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001608 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001609 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001610 case MIToken::kw_blockaddress:
1611 return parseBlockAddressOperand(Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +00001612 case MIToken::kw_intrinsic:
1613 return parseIntrinsicOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001614 case MIToken::kw_target_index:
1615 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001616 case MIToken::kw_liveout:
1617 return parseLiveoutRegisterMaskOperand(Dest);
Tim Northoverde3aea0412016-08-17 20:25:25 +00001618 case MIToken::kw_floatpred:
1619 case MIToken::kw_intpred:
1620 return parsePredicateOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001621 case MIToken::Error:
1622 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001623 case MIToken::Identifier:
1624 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1625 Dest = MachineOperand::CreateRegMask(RegMask);
1626 lex();
1627 break;
1628 }
Justin Bognerb03fd122016-08-17 05:10:15 +00001629 LLVM_FALLTHROUGH;
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001630 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001631 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001632 return error("expected a machine operand");
1633 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001634 return false;
1635}
1636
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001637bool MIParser::parseMachineOperandAndTargetFlags(
1638 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001639 unsigned TF = 0;
1640 bool HasTargetFlags = false;
1641 if (Token.is(MIToken::kw_target_flags)) {
1642 HasTargetFlags = true;
1643 lex();
1644 if (expectAndConsume(MIToken::lparen))
1645 return true;
1646 if (Token.isNot(MIToken::Identifier))
1647 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001648 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1649 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1650 return error("use of undefined target flag '" + Token.stringValue() +
1651 "'");
1652 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001653 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001654 while (Token.is(MIToken::comma)) {
1655 lex();
1656 if (Token.isNot(MIToken::Identifier))
1657 return error("expected the name of the target flag");
1658 unsigned BitFlag = 0;
1659 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1660 return error("use of undefined target flag '" + Token.stringValue() +
1661 "'");
1662 // TODO: Report an error when using a duplicate bit target flag.
1663 TF |= BitFlag;
1664 lex();
1665 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001666 if (expectAndConsume(MIToken::rparen))
1667 return true;
1668 }
1669 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001670 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001671 return true;
1672 if (!HasTargetFlags)
1673 return false;
1674 if (Dest.isReg())
1675 return error(Loc, "register operands can't have target flags");
1676 Dest.setTargetFlags(TF);
1677 return false;
1678}
1679
Alex Lorenzdc24c172015-08-07 20:21:00 +00001680bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001681 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1682 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001683 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001684 bool IsNegative = Token.is(MIToken::minus);
1685 lex();
1686 if (Token.isNot(MIToken::IntegerLiteral))
1687 return error("expected an integer literal after '" + Sign + "'");
1688 if (Token.integerValue().getMinSignedBits() > 64)
1689 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001690 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001691 if (IsNegative)
1692 Offset = -Offset;
1693 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001694 return false;
1695}
1696
Alex Lorenz620f8912015-08-13 20:33:33 +00001697bool MIParser::parseAlignment(unsigned &Alignment) {
1698 assert(Token.is(MIToken::kw_align));
1699 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001700 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001701 return error("expected an integer literal after 'align'");
1702 if (getUnsigned(Alignment))
1703 return true;
1704 lex();
1705 return false;
1706}
1707
Alex Lorenzdc24c172015-08-07 20:21:00 +00001708bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1709 int64_t Offset = 0;
1710 if (parseOffset(Offset))
1711 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001712 Op.setOffset(Offset);
1713 return false;
1714}
1715
Alex Lorenz36593ac2015-08-19 23:27:07 +00001716bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001717 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001718 case MIToken::NamedIRValue: {
1719 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001720 break;
1721 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001722 case MIToken::IRValue: {
1723 unsigned SlotNumber = 0;
1724 if (getUnsigned(SlotNumber))
1725 return true;
1726 V = getIRValue(SlotNumber);
1727 break;
1728 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001729 case MIToken::NamedGlobalValue:
1730 case MIToken::GlobalValue: {
1731 GlobalValue *GV = nullptr;
1732 if (parseGlobalValue(GV))
1733 return true;
1734 V = GV;
1735 break;
1736 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001737 case MIToken::QuotedIRValue: {
1738 const Constant *C = nullptr;
1739 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1740 return true;
1741 V = C;
1742 break;
1743 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001744 default:
1745 llvm_unreachable("The current token should be an IR block reference");
1746 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001747 if (!V)
1748 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001749 return false;
1750}
1751
1752bool MIParser::getUint64(uint64_t &Result) {
1753 assert(Token.hasIntegerValue());
1754 if (Token.integerValue().getActiveBits() > 64)
1755 return error("expected 64-bit integer (too large)");
1756 Result = Token.integerValue().getZExtValue();
1757 return false;
1758}
1759
Justin Lebar0af80cd2016-07-15 18:26:59 +00001760bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
1761 const auto OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001762 switch (Token.kind()) {
1763 case MIToken::kw_volatile:
1764 Flags |= MachineMemOperand::MOVolatile;
1765 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001766 case MIToken::kw_non_temporal:
1767 Flags |= MachineMemOperand::MONonTemporal;
1768 break;
Justin Lebaradbf09e2016-09-11 01:38:58 +00001769 case MIToken::kw_dereferenceable:
1770 Flags |= MachineMemOperand::MODereferenceable;
1771 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001772 case MIToken::kw_invariant:
1773 Flags |= MachineMemOperand::MOInvariant;
1774 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001775 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001776 default:
1777 llvm_unreachable("The current token should be a memory operand flag");
1778 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001779 if (OldFlags == Flags)
1780 // We know that the same flag is specified more than once when the flags
1781 // weren't modified.
1782 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001783 lex();
1784 return false;
1785}
1786
Alex Lorenz91097a32015-08-12 20:33:26 +00001787bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1788 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001789 case MIToken::kw_stack:
1790 PSV = MF.getPSVManager().getStack();
1791 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001792 case MIToken::kw_got:
1793 PSV = MF.getPSVManager().getGOT();
1794 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001795 case MIToken::kw_jump_table:
1796 PSV = MF.getPSVManager().getJumpTable();
1797 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001798 case MIToken::kw_constant_pool:
1799 PSV = MF.getPSVManager().getConstantPool();
1800 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001801 case MIToken::FixedStackObject: {
1802 int FI;
1803 if (parseFixedStackFrameIndex(FI))
1804 return true;
1805 PSV = MF.getPSVManager().getFixedStack(FI);
1806 // The token was already consumed, so use return here instead of break.
1807 return false;
1808 }
Matthias Braun3ef7df92016-06-08 00:47:07 +00001809 case MIToken::StackObject: {
1810 int FI;
1811 if (parseStackFrameIndex(FI))
1812 return true;
1813 PSV = MF.getPSVManager().getFixedStack(FI);
1814 // The token was already consumed, so use return here instead of break.
1815 return false;
1816 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001817 case MIToken::kw_call_entry: {
1818 lex();
1819 switch (Token.kind()) {
1820 case MIToken::GlobalValue:
1821 case MIToken::NamedGlobalValue: {
1822 GlobalValue *GV = nullptr;
1823 if (parseGlobalValue(GV))
1824 return true;
1825 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1826 break;
1827 }
1828 case MIToken::ExternalSymbol:
1829 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1830 MF.createExternalSymbolName(Token.stringValue()));
1831 break;
1832 default:
1833 return error(
1834 "expected a global value or an external symbol after 'call-entry'");
1835 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001836 break;
1837 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001838 default:
1839 llvm_unreachable("The current token should be pseudo source value");
1840 }
1841 lex();
1842 return false;
1843}
1844
1845bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001846 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001847 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Matthias Braun3ef7df92016-06-08 00:47:07 +00001848 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
1849 Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001850 const PseudoSourceValue *PSV = nullptr;
1851 if (parseMemoryPseudoSourceValue(PSV))
1852 return true;
1853 int64_t Offset = 0;
1854 if (parseOffset(Offset))
1855 return true;
1856 Dest = MachinePointerInfo(PSV, Offset);
1857 return false;
1858 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001859 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1860 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001861 Token.isNot(MIToken::NamedGlobalValue) &&
1862 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001863 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001864 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001865 if (parseIRValue(V))
1866 return true;
1867 if (!V->getType()->isPointerTy())
1868 return error("expected a pointer IR value");
1869 lex();
1870 int64_t Offset = 0;
1871 if (parseOffset(Offset))
1872 return true;
1873 Dest = MachinePointerInfo(V, Offset);
1874 return false;
1875}
1876
Alex Lorenz4af7e612015-08-03 23:08:19 +00001877bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1878 if (expectAndConsume(MIToken::lparen))
1879 return true;
Justin Lebar0af80cd2016-07-15 18:26:59 +00001880 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
Alex Lorenza518b792015-08-04 00:24:45 +00001881 while (Token.isMemoryOperandFlag()) {
1882 if (parseMemoryOperandFlag(Flags))
1883 return true;
1884 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001885 if (Token.isNot(MIToken::Identifier) ||
1886 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1887 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001888 if (Token.stringValue() == "load")
1889 Flags |= MachineMemOperand::MOLoad;
1890 else
1891 Flags |= MachineMemOperand::MOStore;
1892 lex();
1893
1894 if (Token.isNot(MIToken::IntegerLiteral))
1895 return error("expected the size integer literal after memory operation");
1896 uint64_t Size;
1897 if (getUint64(Size))
1898 return true;
1899 lex();
1900
Alex Lorenz91097a32015-08-12 20:33:26 +00001901 MachinePointerInfo Ptr = MachinePointerInfo();
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001902 if (Token.is(MIToken::Identifier)) {
1903 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1904 if (Token.stringValue() != Word)
1905 return error(Twine("expected '") + Word + "'");
1906 lex();
1907
1908 if (parseMachinePointerInfo(Ptr))
1909 return true;
1910 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001911 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001912 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001913 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001914 while (consumeIfPresent(MIToken::comma)) {
1915 switch (Token.kind()) {
1916 case MIToken::kw_align:
1917 if (parseAlignment(BaseAlignment))
1918 return true;
1919 break;
1920 case MIToken::md_tbaa:
1921 lex();
1922 if (parseMDNode(AAInfo.TBAA))
1923 return true;
1924 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001925 case MIToken::md_alias_scope:
1926 lex();
1927 if (parseMDNode(AAInfo.Scope))
1928 return true;
1929 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001930 case MIToken::md_noalias:
1931 lex();
1932 if (parseMDNode(AAInfo.NoAlias))
1933 return true;
1934 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001935 case MIToken::md_range:
1936 lex();
1937 if (parseMDNode(Range))
1938 return true;
1939 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001940 // TODO: Report an error on duplicate metadata nodes.
1941 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001942 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1943 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001944 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001945 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001946 if (expectAndConsume(MIToken::rparen))
1947 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001948 Dest =
1949 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001950 return false;
1951}
1952
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001953void MIParser::initNames2InstrOpCodes() {
1954 if (!Names2InstrOpCodes.empty())
1955 return;
1956 const auto *TII = MF.getSubtarget().getInstrInfo();
1957 assert(TII && "Expected target instruction info");
1958 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1959 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1960}
1961
1962bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1963 initNames2InstrOpCodes();
1964 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1965 if (InstrInfo == Names2InstrOpCodes.end())
1966 return true;
1967 OpCode = InstrInfo->getValue();
1968 return false;
1969}
1970
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001971void MIParser::initNames2Regs() {
1972 if (!Names2Regs.empty())
1973 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001974 // The '%noreg' register is the register 0.
1975 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001976 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1977 assert(TRI && "Expected target register info");
1978 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1979 bool WasInserted =
1980 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1981 .second;
1982 (void)WasInserted;
1983 assert(WasInserted && "Expected registers to be unique case-insensitively");
1984 }
1985}
1986
1987bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1988 initNames2Regs();
1989 auto RegInfo = Names2Regs.find(RegName);
1990 if (RegInfo == Names2Regs.end())
1991 return true;
1992 Reg = RegInfo->getValue();
1993 return false;
1994}
1995
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001996void MIParser::initNames2RegMasks() {
1997 if (!Names2RegMasks.empty())
1998 return;
1999 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2000 assert(TRI && "Expected target register info");
2001 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
2002 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
2003 assert(RegMasks.size() == RegMaskNames.size());
2004 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
2005 Names2RegMasks.insert(
2006 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
2007}
2008
2009const uint32_t *MIParser::getRegMask(StringRef Identifier) {
2010 initNames2RegMasks();
2011 auto RegMaskInfo = Names2RegMasks.find(Identifier);
2012 if (RegMaskInfo == Names2RegMasks.end())
2013 return nullptr;
2014 return RegMaskInfo->getValue();
2015}
2016
Alex Lorenz2eacca82015-07-13 23:24:34 +00002017void MIParser::initNames2SubRegIndices() {
2018 if (!Names2SubRegIndices.empty())
2019 return;
2020 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2021 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
2022 Names2SubRegIndices.insert(
2023 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
2024}
2025
2026unsigned MIParser::getSubRegIndex(StringRef Name) {
2027 initNames2SubRegIndices();
2028 auto SubRegInfo = Names2SubRegIndices.find(Name);
2029 if (SubRegInfo == Names2SubRegIndices.end())
2030 return 0;
2031 return SubRegInfo->getValue();
2032}
2033
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002034static void initSlots2BasicBlocks(
2035 const Function &F,
2036 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2037 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002038 MST.incorporateFunction(F);
2039 for (auto &BB : F) {
2040 if (BB.hasName())
2041 continue;
2042 int Slot = MST.getLocalSlot(&BB);
2043 if (Slot == -1)
2044 continue;
2045 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2046 }
2047}
2048
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002049static const BasicBlock *getIRBlockFromSlot(
2050 unsigned Slot,
2051 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002052 auto BlockInfo = Slots2BasicBlocks.find(Slot);
2053 if (BlockInfo == Slots2BasicBlocks.end())
2054 return nullptr;
2055 return BlockInfo->second;
2056}
2057
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002058const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2059 if (Slots2BasicBlocks.empty())
2060 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
2061 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2062}
2063
2064const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2065 if (&F == MF.getFunction())
2066 return getIRBlock(Slot);
2067 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2068 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2069 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2070}
2071
Alex Lorenzdd13be02015-08-19 23:31:05 +00002072static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2073 DenseMap<unsigned, const Value *> &Slots2Values) {
2074 int Slot = MST.getLocalSlot(V);
2075 if (Slot == -1)
2076 return;
2077 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2078}
2079
2080/// Creates the mapping from slot numbers to function's unnamed IR values.
2081static void initSlots2Values(const Function &F,
2082 DenseMap<unsigned, const Value *> &Slots2Values) {
2083 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2084 MST.incorporateFunction(F);
2085 for (const auto &Arg : F.args())
2086 mapValueToSlot(&Arg, MST, Slots2Values);
2087 for (const auto &BB : F) {
2088 mapValueToSlot(&BB, MST, Slots2Values);
2089 for (const auto &I : BB)
2090 mapValueToSlot(&I, MST, Slots2Values);
2091 }
2092}
2093
2094const Value *MIParser::getIRValue(unsigned Slot) {
2095 if (Slots2Values.empty())
2096 initSlots2Values(*MF.getFunction(), Slots2Values);
2097 auto ValueInfo = Slots2Values.find(Slot);
2098 if (ValueInfo == Slots2Values.end())
2099 return nullptr;
2100 return ValueInfo->second;
2101}
2102
Alex Lorenzef5c1962015-07-28 23:02:45 +00002103void MIParser::initNames2TargetIndices() {
2104 if (!Names2TargetIndices.empty())
2105 return;
2106 const auto *TII = MF.getSubtarget().getInstrInfo();
2107 assert(TII && "Expected target instruction info");
2108 auto Indices = TII->getSerializableTargetIndices();
2109 for (const auto &I : Indices)
2110 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2111}
2112
2113bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2114 initNames2TargetIndices();
2115 auto IndexInfo = Names2TargetIndices.find(Name);
2116 if (IndexInfo == Names2TargetIndices.end())
2117 return true;
2118 Index = IndexInfo->second;
2119 return false;
2120}
2121
Alex Lorenz49873a82015-08-06 00:44:07 +00002122void MIParser::initNames2DirectTargetFlags() {
2123 if (!Names2DirectTargetFlags.empty())
2124 return;
2125 const auto *TII = MF.getSubtarget().getInstrInfo();
2126 assert(TII && "Expected target instruction info");
2127 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2128 for (const auto &I : Flags)
2129 Names2DirectTargetFlags.insert(
2130 std::make_pair(StringRef(I.second), I.first));
2131}
2132
2133bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2134 initNames2DirectTargetFlags();
2135 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2136 if (FlagInfo == Names2DirectTargetFlags.end())
2137 return true;
2138 Flag = FlagInfo->second;
2139 return false;
2140}
2141
Alex Lorenzf3630112015-08-18 22:52:15 +00002142void MIParser::initNames2BitmaskTargetFlags() {
2143 if (!Names2BitmaskTargetFlags.empty())
2144 return;
2145 const auto *TII = MF.getSubtarget().getInstrInfo();
2146 assert(TII && "Expected target instruction info");
2147 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2148 for (const auto &I : Flags)
2149 Names2BitmaskTargetFlags.insert(
2150 std::make_pair(StringRef(I.second), I.first));
2151}
2152
2153bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2154 initNames2BitmaskTargetFlags();
2155 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2156 if (FlagInfo == Names2BitmaskTargetFlags.end())
2157 return true;
2158 Flag = FlagInfo->second;
2159 return false;
2160}
2161
Matthias Braun83947862016-07-13 22:23:23 +00002162bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2163 StringRef Src,
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002164 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002165 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002166}
2167
Matthias Braun83947862016-07-13 22:23:23 +00002168bool llvm::parseMachineInstructions(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002169 StringRef Src, SMDiagnostic &Error) {
2170 return MIParser(PFS, Error, Src).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002171}
Alex Lorenzf09df002015-06-30 18:16:42 +00002172
Matthias Braun83947862016-07-13 22:23:23 +00002173bool llvm::parseMBBReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002174 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +00002175 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002176 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002177}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002178
Matthias Braun83947862016-07-13 22:23:23 +00002179bool llvm::parseNamedRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002180 unsigned &Reg, StringRef Src,
Alex Lorenz9fab3702015-07-14 21:24:41 +00002181 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002182 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002183}
Alex Lorenz12045a42015-07-27 17:42:45 +00002184
Matthias Braun83947862016-07-13 22:23:23 +00002185bool llvm::parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002186 unsigned &Reg, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +00002187 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002188 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +00002189}
Alex Lorenza314d812015-08-18 22:26:26 +00002190
Matthias Braun83947862016-07-13 22:23:23 +00002191bool llvm::parseStackObjectReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002192 int &FI, StringRef Src,
Alex Lorenza314d812015-08-18 22:26:26 +00002193 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002194 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
Alex Lorenza314d812015-08-18 22:26:26 +00002195}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002196
Matthias Braun83947862016-07-13 22:23:23 +00002197bool llvm::parseMDNode(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002198 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2199 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002200}