blob: 0f58e617b3e973d4cc6d77e668189835d65828d5 [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
Matthias Braun74ad41c2016-10-11 03:13:01 +000047VRegInfo &PerFunctionMIParsingState::getVRegInfo(unsigned Num) {
48 auto I = VRegInfos.insert(std::make_pair(Num, nullptr));
49 if (I.second) {
50 MachineRegisterInfo &MRI = MF.getRegInfo();
51 VRegInfo *Info = new (Allocator) VRegInfo;
52 Info->VReg = MRI.createIncompleteVirtualRegister();
53 I.first->second = Info;
54 }
55 return *I.first->second;
56}
57
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000058namespace {
59
Alex Lorenz36962cd2015-07-07 02:08:46 +000060/// A wrapper struct around the 'MachineOperand' struct that includes a source
Alex Lorenzfeb6b432015-08-19 19:19:16 +000061/// range and other attributes.
62struct ParsedMachineOperand {
Alex Lorenz36962cd2015-07-07 02:08:46 +000063 MachineOperand Operand;
64 StringRef::iterator Begin;
65 StringRef::iterator End;
Alex Lorenz5ef93b02015-08-19 19:05:34 +000066 Optional<unsigned> TiedDefIdx;
Alex Lorenz36962cd2015-07-07 02:08:46 +000067
Alex Lorenzfeb6b432015-08-19 19:19:16 +000068 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
69 StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
Alex Lorenz5ef93b02015-08-19 19:05:34 +000070 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
71 if (TiedDefIdx)
72 assert(Operand.isReg() && Operand.isUse() &&
73 "Only used register operands can be tied");
74 }
Alex Lorenz36962cd2015-07-07 02:08:46 +000075};
76
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000077class MIParser {
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000078 MachineFunction &MF;
79 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000080 StringRef Source, CurrentSource;
81 MIToken Token;
Matthias Braun74ad41c2016-10-11 03:13:01 +000082 PerFunctionMIParsingState &PFS;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000083 /// Maps from instruction names to op codes.
84 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000085 /// Maps from register names to registers.
86 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000087 /// Maps from register mask names to register masks.
88 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000089 /// Maps from subregister names to subregister indices.
90 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000091 /// Maps from slot numbers to function's unnamed basic blocks.
92 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzdd13be02015-08-19 23:31:05 +000093 /// Maps from slot numbers to function's unnamed values.
94 DenseMap<unsigned, const Value *> Slots2Values;
Alex Lorenzef5c1962015-07-28 23:02:45 +000095 /// Maps from target index names to target indices.
96 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +000097 /// Maps from direct target flag names to the direct target flag values.
98 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenzf3630112015-08-18 22:52:15 +000099 /// Maps from direct target flag names to the bitmask target flag values.
100 StringMap<unsigned> Names2BitmaskTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000101
102public:
Matthias Braun74ad41c2016-10-11 03:13:01 +0000103 MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
Matthias Braune35861d2016-07-13 23:27:50 +0000104 StringRef Source);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000105
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000106 /// \p SkipChar gives the number of characters to skip before looking
107 /// for the next token.
108 void lex(unsigned SkipChar = 0);
Alex Lorenz91370c52015-06-22 20:37:46 +0000109
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000110 /// Report an error at the current location with the given message.
111 ///
112 /// This function always return true.
113 bool error(const Twine &Msg);
114
Alex Lorenz91370c52015-06-22 20:37:46 +0000115 /// Report an error at the given location with the given message.
116 ///
117 /// This function always return true.
118 bool error(StringRef::iterator Loc, const Twine &Msg);
119
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000120 bool
121 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
122 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +0000123 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +0000124 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
125 bool parseStandaloneNamedRegister(unsigned &Reg);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000126 bool parseStandaloneVirtualRegister(VRegInfo *&Info);
Alex Lorenza314d812015-08-18 22:26:26 +0000127 bool parseStandaloneStackObject(int &FI);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000128 bool parseStandaloneMDNode(MDNode *&Node);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000129
130 bool
131 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
132 bool parseBasicBlock(MachineBasicBlock &MBB);
133 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
134 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000135
Matthias Braun74ad41c2016-10-11 03:13:01 +0000136 bool parseNamedRegister(unsigned &Reg);
137 bool parseVirtualRegister(VRegInfo *&Info);
138 bool parseRegister(unsigned &Reg, VRegInfo *&VRegInfo);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000139 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000140 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000141 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
142 bool parseRegisterOperand(MachineOperand &Dest,
143 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000144 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000145 bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
146 const Constant *&C);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000147 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Ahmed Bougachad760de02016-07-28 17:15:12 +0000148 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
Alex Lorenz05e38822015-08-05 18:52:21 +0000149 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000150 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000151 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000152 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000153 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000154 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000155 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000156 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000157 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000158 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000159 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +0000160 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000161 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000162 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000163 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000164 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000165 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000166 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000167 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000168 bool parseIRBlock(BasicBlock *&BB, const Function &F);
169 bool parseBlockAddressOperand(MachineOperand &Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +0000170 bool parseIntrinsicOperand(MachineOperand &Dest);
Tim Northoverde3aea0412016-08-17 20:25:25 +0000171 bool parsePredicateOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000172 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000173 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000174 bool parseMachineOperand(MachineOperand &Dest,
175 Optional<unsigned> &TiedDefIdx);
176 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
177 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000178 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000179 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000180 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000181 bool parseIRValue(const Value *&V);
Justin Lebar0af80cd2016-07-15 18:26:59 +0000182 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000183 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
184 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000185 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000186
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000187private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000188 /// Convert the integer literal in the current token into an unsigned integer.
189 ///
190 /// Return true if an error occurred.
191 bool getUnsigned(unsigned &Result);
192
Alex Lorenz4af7e612015-08-03 23:08:19 +0000193 /// Convert the integer literal in the current token into an uint64.
194 ///
195 /// Return true if an error occurred.
196 bool getUint64(uint64_t &Result);
197
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000198 /// If the current token is of the given kind, consume it and return false.
199 /// Otherwise report an error and return true.
200 bool expectAndConsume(MIToken::TokenKind TokenKind);
201
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000202 /// If the current token is of the given kind, consume it and return true.
203 /// Otherwise return false.
204 bool consumeIfPresent(MIToken::TokenKind TokenKind);
205
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000206 void initNames2InstrOpCodes();
207
208 /// Try to convert an instruction name to an opcode. Return true if the
209 /// instruction name is invalid.
210 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000211
Alex Lorenze5a44662015-07-17 00:24:15 +0000212 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000213
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000214 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000215 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000216
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000217 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000218 const MCInstrDesc &MCID);
219
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000220 void initNames2Regs();
221
222 /// Try to convert a register name to a register number. Return true if the
223 /// register name is invalid.
224 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000225
226 void initNames2RegMasks();
227
228 /// Check if the given identifier is a name of a register mask.
229 ///
230 /// Return null if the identifier isn't a register mask.
231 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000232
233 void initNames2SubRegIndices();
234
235 /// Check if the given identifier is a name of a subregister index.
236 ///
237 /// Return 0 if the name isn't a subregister index class.
238 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000239
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000240 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000241 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000242
Alex Lorenzdd13be02015-08-19 23:31:05 +0000243 const Value *getIRValue(unsigned Slot);
244
Alex Lorenzef5c1962015-07-28 23:02:45 +0000245 void initNames2TargetIndices();
246
247 /// Try to convert a name of target index to the corresponding target index.
248 ///
249 /// Return true if the name isn't a name of a target index.
250 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000251
252 void initNames2DirectTargetFlags();
253
254 /// Try to convert a name of a direct target flag to the corresponding
255 /// target flag.
256 ///
257 /// Return true if the name isn't a name of a direct flag.
258 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000259
260 void initNames2BitmaskTargetFlags();
261
262 /// Try to convert a name of a bitmask target flag to the corresponding
263 /// target flag.
264 ///
265 /// Return true if the name isn't a name of a bitmask target flag.
266 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000267};
268
269} // end anonymous namespace
270
Matthias Braun74ad41c2016-10-11 03:13:01 +0000271MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
Matthias Braune35861d2016-07-13 23:27:50 +0000272 StringRef Source)
273 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
274{}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000275
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000276void MIParser::lex(unsigned SkipChar) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000277 CurrentSource = lexMIToken(
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000278 CurrentSource.data() + SkipChar, Token,
Alex Lorenz91370c52015-06-22 20:37:46 +0000279 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
280}
281
282bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
283
284bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Matthias Braune35861d2016-07-13 23:27:50 +0000285 const SourceMgr &SM = *PFS.SM;
Alex Lorenz91370c52015-06-22 20:37:46 +0000286 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000287 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
288 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
289 // Create an ordinary diagnostic when the source manager's buffer is the
290 // source string.
291 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
292 return true;
293 }
294 // Create a diagnostic for a YAML string literal.
295 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
296 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
297 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000298 return true;
299}
300
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000301static const char *toString(MIToken::TokenKind TokenKind) {
302 switch (TokenKind) {
303 case MIToken::comma:
304 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000305 case MIToken::equal:
306 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000307 case MIToken::colon:
308 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000309 case MIToken::lparen:
310 return "'('";
311 case MIToken::rparen:
312 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000313 default:
314 return "<unknown token>";
315 }
316}
317
318bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
319 if (Token.isNot(TokenKind))
320 return error(Twine("expected ") + toString(TokenKind));
321 lex();
322 return false;
323}
324
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000325bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
326 if (Token.isNot(TokenKind))
327 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000328 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000329 return true;
330}
Alex Lorenz91370c52015-06-22 20:37:46 +0000331
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000332bool MIParser::parseBasicBlockDefinition(
333 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
334 assert(Token.is(MIToken::MachineBasicBlockLabel));
335 unsigned ID = 0;
336 if (getUnsigned(ID))
337 return true;
338 auto Loc = Token.location();
339 auto Name = Token.stringValue();
340 lex();
341 bool HasAddressTaken = false;
342 bool IsLandingPad = false;
343 unsigned Alignment = 0;
344 BasicBlock *BB = nullptr;
345 if (consumeIfPresent(MIToken::lparen)) {
346 do {
347 // TODO: Report an error when multiple same attributes are specified.
348 switch (Token.kind()) {
349 case MIToken::kw_address_taken:
350 HasAddressTaken = true;
351 lex();
352 break;
353 case MIToken::kw_landing_pad:
354 IsLandingPad = true;
355 lex();
356 break;
357 case MIToken::kw_align:
358 if (parseAlignment(Alignment))
359 return true;
360 break;
361 case MIToken::IRBlock:
362 // TODO: Report an error when both name and ir block are specified.
363 if (parseIRBlock(BB, *MF.getFunction()))
364 return true;
365 lex();
366 break;
367 default:
368 break;
369 }
370 } while (consumeIfPresent(MIToken::comma));
371 if (expectAndConsume(MIToken::rparen))
372 return true;
373 }
374 if (expectAndConsume(MIToken::colon))
375 return true;
376
377 if (!Name.empty()) {
378 BB = dyn_cast_or_null<BasicBlock>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000379 MF.getFunction()->getValueSymbolTable()->lookup(Name));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000380 if (!BB)
381 return error(Loc, Twine("basic block '") + Name +
382 "' is not defined in the function '" +
383 MF.getName() + "'");
384 }
385 auto *MBB = MF.CreateMachineBasicBlock(BB);
386 MF.insert(MF.end(), MBB);
387 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
388 if (!WasInserted)
389 return error(Loc, Twine("redefinition of machine basic block with id #") +
390 Twine(ID));
391 if (Alignment)
392 MBB->setAlignment(Alignment);
393 if (HasAddressTaken)
394 MBB->setHasAddressTaken();
Reid Kleckner0e288232015-08-27 23:27:47 +0000395 MBB->setIsEHPad(IsLandingPad);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000396 return false;
397}
398
399bool MIParser::parseBasicBlockDefinitions(
400 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
401 lex();
402 // Skip until the first machine basic block.
403 while (Token.is(MIToken::Newline))
404 lex();
405 if (Token.isErrorOrEOF())
406 return Token.isError();
407 if (Token.isNot(MIToken::MachineBasicBlockLabel))
408 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000409 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000410 do {
411 if (parseBasicBlockDefinition(MBBSlots))
412 return true;
413 bool IsAfterNewline = false;
414 // Skip until the next machine basic block.
415 while (true) {
416 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
417 Token.isErrorOrEOF())
418 break;
419 else if (Token.is(MIToken::MachineBasicBlockLabel))
420 return error("basic block definition should be located at the start of "
421 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000422 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000423 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000424 continue;
425 }
426 IsAfterNewline = false;
427 if (Token.is(MIToken::lbrace))
428 ++BraceDepth;
429 if (Token.is(MIToken::rbrace)) {
430 if (!BraceDepth)
431 return error("extraneous closing brace ('}')");
432 --BraceDepth;
433 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000434 lex();
435 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000436 // Verify that we closed all of the '{' at the end of a file or a block.
437 if (!Token.isError() && BraceDepth)
438 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000439 } while (!Token.isErrorOrEOF());
440 return Token.isError();
441}
442
443bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
444 assert(Token.is(MIToken::kw_liveins));
445 lex();
446 if (expectAndConsume(MIToken::colon))
447 return true;
448 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
449 return false;
450 do {
451 if (Token.isNot(MIToken::NamedRegister))
452 return error("expected a named register");
453 unsigned Reg = 0;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000454 if (parseNamedRegister(Reg))
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000455 return true;
456 MBB.addLiveIn(Reg);
457 lex();
458 } while (consumeIfPresent(MIToken::comma));
459 return false;
460}
461
462bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
463 assert(Token.is(MIToken::kw_successors));
464 lex();
465 if (expectAndConsume(MIToken::colon))
466 return true;
467 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
468 return false;
469 do {
470 if (Token.isNot(MIToken::MachineBasicBlock))
471 return error("expected a machine basic block reference");
472 MachineBasicBlock *SuccMBB = nullptr;
473 if (parseMBBReference(SuccMBB))
474 return true;
475 lex();
476 unsigned Weight = 0;
477 if (consumeIfPresent(MIToken::lparen)) {
478 if (Token.isNot(MIToken::IntegerLiteral))
479 return error("expected an integer literal after '('");
480 if (getUnsigned(Weight))
481 return true;
482 lex();
483 if (expectAndConsume(MIToken::rparen))
484 return true;
485 }
Cong Houd97c1002015-12-01 05:29:22 +0000486 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000487 } while (consumeIfPresent(MIToken::comma));
Cong Houd97c1002015-12-01 05:29:22 +0000488 MBB.normalizeSuccProbs();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000489 return false;
490}
491
492bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
493 // Skip the definition.
494 assert(Token.is(MIToken::MachineBasicBlockLabel));
495 lex();
496 if (consumeIfPresent(MIToken::lparen)) {
497 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
498 lex();
499 consumeIfPresent(MIToken::rparen);
500 }
501 consumeIfPresent(MIToken::colon);
502
503 // Parse the liveins and successors.
504 // N.B: Multiple lists of successors and liveins are allowed and they're
505 // merged into one.
506 // Example:
507 // liveins: %edi
508 // liveins: %esi
509 //
510 // is equivalent to
511 // liveins: %edi, %esi
512 while (true) {
513 if (Token.is(MIToken::kw_successors)) {
514 if (parseBasicBlockSuccessors(MBB))
515 return true;
516 } else if (Token.is(MIToken::kw_liveins)) {
517 if (parseBasicBlockLiveins(MBB))
518 return true;
519 } else if (consumeIfPresent(MIToken::Newline)) {
520 continue;
521 } else
522 break;
523 if (!Token.isNewlineOrEOF())
524 return error("expected line break at the end of a list");
525 lex();
526 }
527
528 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000529 bool IsInBundle = false;
530 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000531 while (true) {
532 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
533 return false;
534 else if (consumeIfPresent(MIToken::Newline))
535 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000536 if (consumeIfPresent(MIToken::rbrace)) {
537 // The first parsing pass should verify that all closing '}' have an
538 // opening '{'.
539 assert(IsInBundle);
540 IsInBundle = false;
541 continue;
542 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000543 MachineInstr *MI = nullptr;
544 if (parse(MI))
545 return true;
546 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000547 if (IsInBundle) {
548 PrevMI->setFlag(MachineInstr::BundledSucc);
549 MI->setFlag(MachineInstr::BundledPred);
550 }
551 PrevMI = MI;
552 if (Token.is(MIToken::lbrace)) {
553 if (IsInBundle)
554 return error("nested instruction bundles are not allowed");
555 lex();
556 // This instruction is the start of the bundle.
557 MI->setFlag(MachineInstr::BundledSucc);
558 IsInBundle = true;
559 if (!Token.is(MIToken::Newline))
560 // The next instruction can be on the same line.
561 continue;
562 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000563 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
564 lex();
565 }
566 return false;
567}
568
569bool MIParser::parseBasicBlocks() {
570 lex();
571 // Skip until the first machine basic block.
572 while (Token.is(MIToken::Newline))
573 lex();
574 if (Token.isErrorOrEOF())
575 return Token.isError();
576 // The first parsing pass should have verified that this token is a MBB label
577 // in the 'parseBasicBlockDefinitions' method.
578 assert(Token.is(MIToken::MachineBasicBlockLabel));
579 do {
580 MachineBasicBlock *MBB = nullptr;
581 if (parseMBBReference(MBB))
582 return true;
583 if (parseBasicBlock(*MBB))
584 return true;
585 // The method 'parseBasicBlock' should parse the whole block until the next
586 // block or the end of file.
587 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
588 } while (Token.isNot(MIToken::Eof));
589 return false;
590}
591
592bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000593 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000594 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000595 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000596 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000597 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000598 Optional<unsigned> TiedDefIdx;
599 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000600 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000601 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000602 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000603 if (Token.isNot(MIToken::comma))
604 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000605 lex();
606 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000607 if (!Operands.empty() && expectAndConsume(MIToken::equal))
608 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000609
Alex Lorenze5a44662015-07-17 00:24:15 +0000610 unsigned OpCode, Flags = 0;
611 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000612 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000613
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000614 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000615 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000616 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000617 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000618 Optional<unsigned> TiedDefIdx;
619 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000620 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000621 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000622 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000623 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
624 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000625 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000626 if (Token.isNot(MIToken::comma))
627 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000628 lex();
629 }
630
Alex Lorenz46d760d2015-07-22 21:15:11 +0000631 DebugLoc DebugLocation;
632 if (Token.is(MIToken::kw_debug_location)) {
633 lex();
634 if (Token.isNot(MIToken::exclaim))
635 return error("expected a metadata node after 'debug-location'");
636 MDNode *Node = nullptr;
637 if (parseMDNode(Node))
638 return true;
639 DebugLocation = DebugLoc(Node);
640 }
641
Alex Lorenz4af7e612015-08-03 23:08:19 +0000642 // Parse the machine memory operands.
643 SmallVector<MachineMemOperand *, 2> MemOperands;
644 if (Token.is(MIToken::coloncolon)) {
645 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000646 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000647 MachineMemOperand *MemOp = nullptr;
648 if (parseMachineMemoryOperand(MemOp))
649 return true;
650 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000651 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000652 break;
653 if (Token.isNot(MIToken::comma))
654 return error("expected ',' before the next machine memory operand");
655 lex();
656 }
657 }
658
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000659 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000660 if (!MCID.isVariadic()) {
661 // FIXME: Move the implicit operand verification to the machine verifier.
662 if (verifyImplicitOperands(Operands, MCID))
663 return true;
664 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000665
Alex Lorenzcb268d42015-07-06 23:07:26 +0000666 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000667 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000668 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000669 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000670 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000671 if (assignRegisterTies(*MI, Operands))
672 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000673 if (MemOperands.empty())
674 return false;
675 MachineInstr::mmo_iterator MemRefs =
676 MF.allocateMemRefsArray(MemOperands.size());
677 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
678 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000679 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000680}
681
Alex Lorenz1ea60892015-07-27 20:29:27 +0000682bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000683 lex();
684 if (Token.isNot(MIToken::MachineBasicBlock))
685 return error("expected a machine basic block reference");
686 if (parseMBBReference(MBB))
687 return true;
688 lex();
689 if (Token.isNot(MIToken::Eof))
690 return error(
691 "expected end of string after the machine basic block reference");
692 return false;
693}
694
Alex Lorenz1ea60892015-07-27 20:29:27 +0000695bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000696 lex();
697 if (Token.isNot(MIToken::NamedRegister))
698 return error("expected a named register");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000699 if (parseNamedRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000700 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000701 lex();
702 if (Token.isNot(MIToken::Eof))
703 return error("expected end of string after the register reference");
704 return false;
705}
706
Matthias Braun74ad41c2016-10-11 03:13:01 +0000707bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
Alex Lorenz12045a42015-07-27 17:42:45 +0000708 lex();
709 if (Token.isNot(MIToken::VirtualRegister))
710 return error("expected a virtual register");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000711 if (parseVirtualRegister(Info))
Alex Lorenz607efb62015-08-18 22:57:36 +0000712 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000713 lex();
714 if (Token.isNot(MIToken::Eof))
715 return error("expected end of string after the register reference");
716 return false;
717}
718
Alex Lorenza314d812015-08-18 22:26:26 +0000719bool MIParser::parseStandaloneStackObject(int &FI) {
720 lex();
721 if (Token.isNot(MIToken::StackObject))
722 return error("expected a stack object");
723 if (parseStackFrameIndex(FI))
724 return true;
725 if (Token.isNot(MIToken::Eof))
726 return error("expected end of string after the stack object reference");
727 return false;
728}
729
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000730bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
731 lex();
732 if (Token.isNot(MIToken::exclaim))
733 return error("expected a metadata node");
734 if (parseMDNode(Node))
735 return true;
736 if (Token.isNot(MIToken::Eof))
737 return error("expected end of string after the metadata node");
738 return false;
739}
740
Alex Lorenz36962cd2015-07-07 02:08:46 +0000741static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
742 assert(MO.isImplicit());
743 return MO.isDef() ? "implicit-def" : "implicit";
744}
745
746static std::string getRegisterName(const TargetRegisterInfo *TRI,
747 unsigned Reg) {
748 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
749 return StringRef(TRI->getName(Reg)).lower();
750}
751
Alex Lorenz0153e592015-09-10 14:04:34 +0000752/// Return true if the parsed machine operands contain a given machine operand.
753static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
754 ArrayRef<ParsedMachineOperand> Operands) {
755 for (const auto &I : Operands) {
756 if (ImplicitOperand.isIdenticalTo(I.Operand))
757 return true;
758 }
759 return false;
760}
761
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000762bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
763 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000764 if (MCID.isCall())
765 // We can't verify call instructions as they can contain arbitrary implicit
766 // register and register mask operands.
767 return false;
768
769 // Gather all the expected implicit operands.
770 SmallVector<MachineOperand, 4> ImplicitOperands;
771 if (MCID.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000772 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000773 ImplicitOperands.push_back(
774 MachineOperand::CreateReg(*ImpDefs, true, true));
775 if (MCID.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000776 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000777 ImplicitOperands.push_back(
778 MachineOperand::CreateReg(*ImpUses, false, true));
779
780 const auto *TRI = MF.getSubtarget().getRegisterInfo();
781 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000782 for (const auto &I : ImplicitOperands) {
783 if (isImplicitOperandIn(I, Operands))
784 continue;
785 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000786 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000787 printImplicitRegisterFlag(I) + " %" +
788 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000789 }
790 return false;
791}
792
Alex Lorenze5a44662015-07-17 00:24:15 +0000793bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
794 if (Token.is(MIToken::kw_frame_setup)) {
795 Flags |= MachineInstr::FrameSetup;
796 lex();
797 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000798 if (Token.isNot(MIToken::Identifier))
799 return error("expected a machine instruction");
800 StringRef InstrName = Token.stringValue();
801 if (parseInstrName(InstrName, OpCode))
802 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000803 lex();
804 return false;
805}
806
Matthias Braun74ad41c2016-10-11 03:13:01 +0000807bool MIParser::parseNamedRegister(unsigned &Reg) {
808 assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
809 StringRef Name = Token.stringValue();
810 if (getRegisterByName(Name, Reg))
811 return error(Twine("unknown register name '") + Name + "'");
812 return false;
813}
814
815bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
816 assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
817 unsigned ID;
818 if (getUnsigned(ID))
819 return true;
820 Info = &PFS.getVRegInfo(ID);
821 return false;
822}
823
824bool MIParser::parseRegister(unsigned &Reg, VRegInfo *&Info) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000825 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000826 case MIToken::underscore:
827 Reg = 0;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000828 return false;
829 case MIToken::NamedRegister:
830 return parseNamedRegister(Reg);
831 case MIToken::VirtualRegister:
832 if (parseVirtualRegister(Info))
Alex Lorenz53464512015-07-10 22:51:20 +0000833 return true;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000834 Reg = Info->VReg;
835 return false;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000836 // TODO: Parse other register kinds.
837 default:
838 llvm_unreachable("The current token should be a register");
839 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000840}
841
Alex Lorenzcb268d42015-07-06 23:07:26 +0000842bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000843 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000844 switch (Token.kind()) {
845 case MIToken::kw_implicit:
846 Flags |= RegState::Implicit;
847 break;
848 case MIToken::kw_implicit_define:
849 Flags |= RegState::ImplicitDefine;
850 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000851 case MIToken::kw_def:
852 Flags |= RegState::Define;
853 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000854 case MIToken::kw_dead:
855 Flags |= RegState::Dead;
856 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000857 case MIToken::kw_killed:
858 Flags |= RegState::Kill;
859 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000860 case MIToken::kw_undef:
861 Flags |= RegState::Undef;
862 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000863 case MIToken::kw_internal:
864 Flags |= RegState::InternalRead;
865 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000866 case MIToken::kw_early_clobber:
867 Flags |= RegState::EarlyClobber;
868 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000869 case MIToken::kw_debug_use:
870 Flags |= RegState::Debug;
871 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000872 default:
873 llvm_unreachable("The current token should be a register flag");
874 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000875 if (OldFlags == Flags)
876 // We know that the same flag is specified more than once when the flags
877 // weren't modified.
878 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000879 lex();
880 return false;
881}
882
Alex Lorenz2eacca82015-07-13 23:24:34 +0000883bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
Matthias Braun333e4682016-07-26 21:49:34 +0000884 assert(Token.is(MIToken::dot));
Alex Lorenz2eacca82015-07-13 23:24:34 +0000885 lex();
886 if (Token.isNot(MIToken::Identifier))
Matthias Braun333e4682016-07-26 21:49:34 +0000887 return error("expected a subregister index after '.'");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000888 auto Name = Token.stringValue();
889 SubReg = getSubRegIndex(Name);
890 if (!SubReg)
891 return error(Twine("use of unknown subregister index '") + Name + "'");
892 lex();
893 return false;
894}
895
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000896bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
897 if (!consumeIfPresent(MIToken::kw_tied_def))
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000898 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000899 if (Token.isNot(MIToken::IntegerLiteral))
900 return error("expected an integer literal after 'tied-def'");
901 if (getUnsigned(TiedDefIdx))
902 return true;
903 lex();
904 if (expectAndConsume(MIToken::rparen))
905 return true;
906 return false;
907}
908
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000909bool MIParser::assignRegisterTies(MachineInstr &MI,
910 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000911 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
912 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
913 if (!Operands[I].TiedDefIdx)
914 continue;
915 // The parser ensures that this operand is a register use, so we just have
916 // to check the tied-def operand.
917 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
918 if (DefIdx >= E)
919 return error(Operands[I].Begin,
920 Twine("use of invalid tied-def operand index '" +
921 Twine(DefIdx) + "'; instruction has only ") +
922 Twine(E) + " operands");
923 const auto &DefOperand = Operands[DefIdx].Operand;
924 if (!DefOperand.isReg() || !DefOperand.isDef())
925 // FIXME: add note with the def operand.
926 return error(Operands[I].Begin,
927 Twine("use of invalid tied-def operand index '") +
928 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
929 " isn't a defined register");
930 // Check that the tied-def operand wasn't tied elsewhere.
931 for (const auto &TiedPair : TiedRegisterPairs) {
932 if (TiedPair.first == DefIdx)
933 return error(Operands[I].Begin,
934 Twine("the tied-def operand #") + Twine(DefIdx) +
935 " is already tied with another register operand");
936 }
937 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
938 }
939 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
940 // indices must be less than tied max.
941 for (const auto &TiedPair : TiedRegisterPairs)
942 MI.tieOperands(TiedPair.first, TiedPair.second);
943 return false;
944}
945
946bool MIParser::parseRegisterOperand(MachineOperand &Dest,
947 Optional<unsigned> &TiedDefIdx,
948 bool IsDef) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000949 unsigned Flags = IsDef ? RegState::Define : 0;
950 while (Token.isRegisterFlag()) {
951 if (parseRegisterFlag(Flags))
952 return true;
953 }
954 if (!Token.isRegister())
955 return error("expected a register after register flags");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000956 unsigned Reg;
957 VRegInfo *RegInfo;
958 if (parseRegister(Reg, RegInfo))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000959 return true;
960 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000961 unsigned SubReg = 0;
Matthias Braun333e4682016-07-26 21:49:34 +0000962 if (Token.is(MIToken::dot)) {
Alex Lorenz2eacca82015-07-13 23:24:34 +0000963 if (parseSubRegisterIndex(SubReg))
964 return true;
Matthias Braun5d00b322016-07-16 01:36:18 +0000965 if (!TargetRegisterInfo::isVirtualRegister(Reg))
966 return error("subregister index expects a virtual register");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000967 }
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000968 MachineRegisterInfo &MRI = MF.getRegInfo();
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000969 if ((Flags & RegState::Define) == 0) {
970 if (consumeIfPresent(MIToken::lparen)) {
971 unsigned Idx;
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000972 if (!parseRegisterTiedDefIndex(Idx))
973 TiedDefIdx = Idx;
974 else {
975 // Try a redundant low-level type.
976 LLT Ty;
977 if (parseLowLevelType(Token.location(), Ty))
978 return error("expected tied-def or low-level type after '('");
979
980 if (expectAndConsume(MIToken::rparen))
981 return true;
982
983 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
984 return error("inconsistent type for generic virtual register");
985
986 MRI.setType(Reg, Ty);
987 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000988 }
989 } else if (consumeIfPresent(MIToken::lparen)) {
Quentin Colombet2c646962016-06-08 23:27:46 +0000990 // Virtual registers may have a size with GlobalISel.
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000991 if (!TargetRegisterInfo::isVirtualRegister(Reg))
992 return error("unexpected size on physical register");
Matthias Braun3d85ebe2016-10-11 04:22:29 +0000993 if (RegInfo->Kind != VRegInfo::GENERIC)
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000994 return error("unexpected size on non-generic virtual register");
995
Tim Northover0f140c72016-09-09 11:46:34 +0000996 LLT Ty;
997 if (parseLowLevelType(Token.location(), Ty))
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000998 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000999
Tim Northover0f140c72016-09-09 11:46:34 +00001000 if (expectAndConsume(MIToken::rparen))
1001 return true;
1002
Tim Northoverd28d3cc2016-09-12 11:20:10 +00001003 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1004 return error("inconsistent type for generic virtual register");
1005
Tim Northover0f140c72016-09-09 11:46:34 +00001006 MRI.setType(Reg, Ty);
Matthias Braun74ad41c2016-10-11 03:13:01 +00001007 } else if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Quentin Colombet2c646962016-06-08 23:27:46 +00001008 // Generic virtual registers must have a size.
1009 // If we end up here this means the size hasn't been specified and
1010 // this is bad!
Matthias Braun74ad41c2016-10-11 03:13:01 +00001011 if (RegInfo->Kind == VRegInfo::GENERIC ||
1012 RegInfo->Kind == VRegInfo::REGBANK)
1013 return error("generic virtual registers must have a size");
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001014 }
Alex Lorenz495ad872015-07-08 21:23:34 +00001015 Dest = MachineOperand::CreateReg(
1016 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +00001017 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +00001018 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1019 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001020 return false;
1021}
1022
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001023bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1024 assert(Token.is(MIToken::IntegerLiteral));
1025 const APSInt &Int = Token.integerValue();
1026 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +00001027 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001028 Dest = MachineOperand::CreateImm(Int.getExtValue());
1029 lex();
1030 return false;
1031}
1032
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001033bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1034 const Constant *&C) {
1035 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001036 SMDiagnostic Err;
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001037 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
Matthias Braune35861d2016-07-13 23:27:50 +00001038 &PFS.IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001039 if (!C)
1040 return error(Loc + Err.getColumnNo(), Err.getMessage());
1041 return false;
1042}
1043
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001044bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1045 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1046 return true;
1047 lex();
1048 return false;
1049}
1050
Ahmed Bougachad760de02016-07-28 17:15:12 +00001051bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
Tim Northover32a078a2016-09-15 10:09:59 +00001052 if (Token.is(MIToken::ScalarType)) {
Tim Northover62ae5682016-07-20 19:09:30 +00001053 Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1054 lex();
1055 return false;
Tim Northoverbd505462016-07-22 16:59:52 +00001056 } else if (Token.is(MIToken::PointerType)) {
Tim Northover5ae83502016-09-15 09:20:34 +00001057 const DataLayout &DL = MF.getFunction()->getParent()->getDataLayout();
1058 unsigned AS = APSInt(Token.range().drop_front()).getZExtValue();
1059 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
Tim Northoverbd505462016-07-22 16:59:52 +00001060 lex();
1061 return false;
Tim Northover62ae5682016-07-20 19:09:30 +00001062 }
Quentin Colombet85199672016-03-08 00:20:48 +00001063
Tim Northover62ae5682016-07-20 19:09:30 +00001064 // Now we're looking for a vector.
1065 if (Token.isNot(MIToken::less))
Tim Northoverbd505462016-07-22 16:59:52 +00001066 return error(Loc,
1067 "expected unsized, pN, sN or <N x sM> for GlobalISel type");
1068
Tim Northover62ae5682016-07-20 19:09:30 +00001069 lex();
1070
1071 if (Token.isNot(MIToken::IntegerLiteral))
1072 return error(Loc, "expected <N x sM> for vctor type");
1073 uint64_t NumElements = Token.integerValue().getZExtValue();
1074 lex();
1075
1076 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1077 return error(Loc, "expected '<N x sM>' for vector type");
1078 lex();
1079
1080 if (Token.isNot(MIToken::ScalarType))
1081 return error(Loc, "expected '<N x sM>' for vector type");
1082 uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1083 lex();
1084
1085 if (Token.isNot(MIToken::greater))
1086 return error(Loc, "expected '<N x sM>' for vector type");
1087 lex();
1088
1089 Ty = LLT::vector(NumElements, ScalarSize);
Quentin Colombet85199672016-03-08 00:20:48 +00001090 return false;
1091}
1092
Alex Lorenz05e38822015-08-05 18:52:21 +00001093bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1094 assert(Token.is(MIToken::IntegerType));
1095 auto Loc = Token.location();
1096 lex();
1097 if (Token.isNot(MIToken::IntegerLiteral))
1098 return error("expected an integer literal");
1099 const Constant *C = nullptr;
1100 if (parseIRConstant(Loc, C))
1101 return true;
1102 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1103 return false;
1104}
1105
Alex Lorenzad156fb2015-07-31 20:49:21 +00001106bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1107 auto Loc = Token.location();
1108 lex();
1109 if (Token.isNot(MIToken::FloatingPointLiteral))
1110 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001111 const Constant *C = nullptr;
1112 if (parseIRConstant(Loc, C))
1113 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001114 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1115 return false;
1116}
1117
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001118bool MIParser::getUnsigned(unsigned &Result) {
1119 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1120 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1121 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1122 if (Val64 == Limit)
1123 return error("expected 32-bit integer (too large)");
1124 Result = Val64;
1125 return false;
1126}
1127
Alex Lorenzf09df002015-06-30 18:16:42 +00001128bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001129 assert(Token.is(MIToken::MachineBasicBlock) ||
1130 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001131 unsigned Number;
1132 if (getUnsigned(Number))
1133 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001134 auto MBBInfo = PFS.MBBSlots.find(Number);
1135 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001136 return error(Twine("use of undefined machine basic block #") +
1137 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001138 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001139 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1140 return error(Twine("the name of machine basic block #") + Twine(Number) +
1141 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001142 return false;
1143}
1144
1145bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1146 MachineBasicBlock *MBB;
1147 if (parseMBBReference(MBB))
1148 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001149 Dest = MachineOperand::CreateMBB(MBB);
1150 lex();
1151 return false;
1152}
1153
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001154bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001155 assert(Token.is(MIToken::StackObject));
1156 unsigned ID;
1157 if (getUnsigned(ID))
1158 return true;
1159 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1160 if (ObjectInfo == PFS.StackObjectSlots.end())
1161 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1162 "'");
1163 StringRef Name;
1164 if (const auto *Alloca =
Matthias Braun941a7052016-07-28 18:40:00 +00001165 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001166 Name = Alloca->getName();
1167 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1168 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1169 "' isn't '" + Token.stringValue() + "'");
1170 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001171 FI = ObjectInfo->second;
1172 return false;
1173}
1174
1175bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1176 int FI;
1177 if (parseStackFrameIndex(FI))
1178 return true;
1179 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001180 return false;
1181}
1182
Alex Lorenzea882122015-08-12 21:17:02 +00001183bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001184 assert(Token.is(MIToken::FixedStackObject));
1185 unsigned ID;
1186 if (getUnsigned(ID))
1187 return true;
1188 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1189 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1190 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1191 Twine(ID) + "'");
1192 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001193 FI = ObjectInfo->second;
1194 return false;
1195}
1196
1197bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1198 int FI;
1199 if (parseFixedStackFrameIndex(FI))
1200 return true;
1201 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001202 return false;
1203}
1204
Alex Lorenz41df7d32015-07-28 17:09:52 +00001205bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001206 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001207 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001208 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001209 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001210 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001211 return error(Twine("use of undefined global value '") + Token.range() +
1212 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001213 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001214 }
1215 case MIToken::GlobalValue: {
1216 unsigned GVIdx;
1217 if (getUnsigned(GVIdx))
1218 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001219 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001220 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1221 "'");
Matthias Braune35861d2016-07-13 23:27:50 +00001222 GV = PFS.IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001223 break;
1224 }
1225 default:
1226 llvm_unreachable("The current token should be a global value");
1227 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001228 return false;
1229}
1230
1231bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1232 GlobalValue *GV = nullptr;
1233 if (parseGlobalValue(GV))
1234 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001235 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001236 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001237 if (parseOperandsOffset(Dest))
1238 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001239 return false;
1240}
1241
Alex Lorenzab980492015-07-20 20:51:18 +00001242bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1243 assert(Token.is(MIToken::ConstantPoolItem));
1244 unsigned ID;
1245 if (getUnsigned(ID))
1246 return true;
1247 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1248 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1249 return error("use of undefined constant '%const." + Twine(ID) + "'");
1250 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001251 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001252 if (parseOperandsOffset(Dest))
1253 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001254 return false;
1255}
1256
Alex Lorenz31d70682015-07-15 23:38:35 +00001257bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1258 assert(Token.is(MIToken::JumpTableIndex));
1259 unsigned ID;
1260 if (getUnsigned(ID))
1261 return true;
1262 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1263 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1264 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1265 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001266 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1267 return false;
1268}
1269
Alex Lorenz6ede3742015-07-21 16:59:53 +00001270bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001271 assert(Token.is(MIToken::ExternalSymbol));
1272 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001273 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001274 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001275 if (parseOperandsOffset(Dest))
1276 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001277 return false;
1278}
1279
Matthias Braunb74eb412016-03-28 18:18:46 +00001280bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1281 assert(Token.is(MIToken::SubRegisterIndex));
1282 StringRef Name = Token.stringValue();
1283 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1284 if (SubRegIndex == 0)
1285 return error(Twine("unknown subregister index '") + Name + "'");
1286 lex();
1287 Dest = MachineOperand::CreateImm(SubRegIndex);
1288 return false;
1289}
1290
Alex Lorenz44f29252015-07-22 21:07:04 +00001291bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001292 assert(Token.is(MIToken::exclaim));
1293 auto Loc = Token.location();
1294 lex();
1295 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1296 return error("expected metadata id after '!'");
1297 unsigned ID;
1298 if (getUnsigned(ID))
1299 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001300 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1301 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
Alex Lorenz35e44462015-07-22 17:58:46 +00001302 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1303 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001304 Node = NodeInfo->second.get();
1305 return false;
1306}
1307
1308bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1309 MDNode *Node = nullptr;
1310 if (parseMDNode(Node))
1311 return true;
1312 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001313 return false;
1314}
1315
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001316bool MIParser::parseCFIOffset(int &Offset) {
1317 if (Token.isNot(MIToken::IntegerLiteral))
1318 return error("expected a cfi offset");
1319 if (Token.integerValue().getMinSignedBits() > 32)
1320 return error("expected a 32 bit integer (the cfi offset is too large)");
1321 Offset = (int)Token.integerValue().getExtValue();
1322 lex();
1323 return false;
1324}
1325
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001326bool MIParser::parseCFIRegister(unsigned &Reg) {
1327 if (Token.isNot(MIToken::NamedRegister))
1328 return error("expected a cfi register");
1329 unsigned LLVMReg;
Matthias Braun74ad41c2016-10-11 03:13:01 +00001330 if (parseNamedRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001331 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001332 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1333 assert(TRI && "Expected target register info");
1334 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1335 if (DwarfReg < 0)
1336 return error("invalid DWARF register");
1337 Reg = (unsigned)DwarfReg;
1338 lex();
1339 return false;
1340}
1341
1342bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1343 auto Kind = Token.kind();
1344 lex();
1345 auto &MMI = MF.getMMI();
1346 int Offset;
1347 unsigned Reg;
1348 unsigned CFIIndex;
1349 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001350 case MIToken::kw_cfi_same_value:
1351 if (parseCFIRegister(Reg))
1352 return true;
1353 CFIIndex =
1354 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1355 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001356 case MIToken::kw_cfi_offset:
1357 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1358 parseCFIOffset(Offset))
1359 return true;
1360 CFIIndex =
1361 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1362 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001363 case MIToken::kw_cfi_def_cfa_register:
1364 if (parseCFIRegister(Reg))
1365 return true;
1366 CFIIndex =
1367 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1368 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001369 case MIToken::kw_cfi_def_cfa_offset:
1370 if (parseCFIOffset(Offset))
1371 return true;
1372 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1373 CFIIndex = MMI.addFrameInst(
1374 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1375 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001376 case MIToken::kw_cfi_def_cfa:
1377 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1378 parseCFIOffset(Offset))
1379 return true;
1380 // NB: MCCFIInstruction::createDefCfa negates the offset.
1381 CFIIndex =
1382 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1383 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001384 default:
1385 // TODO: Parse the other CFI operands.
1386 llvm_unreachable("The current token should be a cfi operand");
1387 }
1388 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001389 return false;
1390}
1391
Alex Lorenzdeb53492015-07-28 17:28:03 +00001392bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1393 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001394 case MIToken::NamedIRBlock: {
1395 BB = dyn_cast_or_null<BasicBlock>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +00001396 F.getValueSymbolTable()->lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001397 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001398 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001399 break;
1400 }
1401 case MIToken::IRBlock: {
1402 unsigned SlotNumber = 0;
1403 if (getUnsigned(SlotNumber))
1404 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001405 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001406 if (!BB)
1407 return error(Twine("use of undefined IR block '%ir-block.") +
1408 Twine(SlotNumber) + "'");
1409 break;
1410 }
1411 default:
1412 llvm_unreachable("The current token should be an IR block reference");
1413 }
1414 return false;
1415}
1416
1417bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1418 assert(Token.is(MIToken::kw_blockaddress));
1419 lex();
1420 if (expectAndConsume(MIToken::lparen))
1421 return true;
1422 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001423 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001424 return error("expected a global value");
1425 GlobalValue *GV = nullptr;
1426 if (parseGlobalValue(GV))
1427 return true;
1428 auto *F = dyn_cast<Function>(GV);
1429 if (!F)
1430 return error("expected an IR function reference");
1431 lex();
1432 if (expectAndConsume(MIToken::comma))
1433 return true;
1434 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001435 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001436 return error("expected an IR block reference");
1437 if (parseIRBlock(BB, *F))
1438 return true;
1439 lex();
1440 if (expectAndConsume(MIToken::rparen))
1441 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001442 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001443 if (parseOperandsOffset(Dest))
1444 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001445 return false;
1446}
1447
Tim Northover6b3bd612016-07-29 20:32:59 +00001448bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
1449 assert(Token.is(MIToken::kw_intrinsic));
1450 lex();
1451 if (expectAndConsume(MIToken::lparen))
1452 return error("expected syntax intrinsic(@llvm.whatever)");
1453
1454 if (Token.isNot(MIToken::NamedGlobalValue))
1455 return error("expected syntax intrinsic(@llvm.whatever)");
1456
1457 std::string Name = Token.stringValue();
1458 lex();
1459
1460 if (expectAndConsume(MIToken::rparen))
1461 return error("expected ')' to terminate intrinsic name");
1462
1463 // Find out what intrinsic we're dealing with, first try the global namespace
1464 // and then the target's private intrinsics if that fails.
1465 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1466 Intrinsic::ID ID = Function::lookupIntrinsicID(Name);
1467 if (ID == Intrinsic::not_intrinsic && TII)
1468 ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
1469
1470 if (ID == Intrinsic::not_intrinsic)
1471 return error("unknown intrinsic name");
1472 Dest = MachineOperand::CreateIntrinsicID(ID);
1473
1474 return false;
1475}
1476
Tim Northoverde3aea0412016-08-17 20:25:25 +00001477bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
1478 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
1479 bool IsFloat = Token.is(MIToken::kw_floatpred);
1480 lex();
1481
1482 if (expectAndConsume(MIToken::lparen))
1483 return error("expected syntax intpred(whatever) or floatpred(whatever");
1484
1485 if (Token.isNot(MIToken::Identifier))
1486 return error("whatever");
1487
1488 CmpInst::Predicate Pred;
1489 if (IsFloat) {
1490 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1491 .Case("false", CmpInst::FCMP_FALSE)
1492 .Case("oeq", CmpInst::FCMP_OEQ)
1493 .Case("ogt", CmpInst::FCMP_OGT)
1494 .Case("oge", CmpInst::FCMP_OGE)
1495 .Case("olt", CmpInst::FCMP_OLT)
1496 .Case("ole", CmpInst::FCMP_OLE)
1497 .Case("one", CmpInst::FCMP_ONE)
1498 .Case("ord", CmpInst::FCMP_ORD)
1499 .Case("uno", CmpInst::FCMP_UNO)
1500 .Case("ueq", CmpInst::FCMP_UEQ)
1501 .Case("ugt", CmpInst::FCMP_UGT)
1502 .Case("uge", CmpInst::FCMP_UGE)
1503 .Case("ult", CmpInst::FCMP_ULT)
1504 .Case("ule", CmpInst::FCMP_ULE)
1505 .Case("une", CmpInst::FCMP_UNE)
1506 .Case("true", CmpInst::FCMP_TRUE)
1507 .Default(CmpInst::BAD_FCMP_PREDICATE);
1508 if (!CmpInst::isFPPredicate(Pred))
1509 return error("invalid floating-point predicate");
1510 } else {
1511 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1512 .Case("eq", CmpInst::ICMP_EQ)
1513 .Case("ne", CmpInst::ICMP_NE)
1514 .Case("sgt", CmpInst::ICMP_SGT)
1515 .Case("sge", CmpInst::ICMP_SGE)
1516 .Case("slt", CmpInst::ICMP_SLT)
1517 .Case("sle", CmpInst::ICMP_SLE)
1518 .Case("ugt", CmpInst::ICMP_UGT)
1519 .Case("uge", CmpInst::ICMP_UGE)
1520 .Case("ult", CmpInst::ICMP_ULT)
1521 .Case("ule", CmpInst::ICMP_ULE)
1522 .Default(CmpInst::BAD_ICMP_PREDICATE);
1523 if (!CmpInst::isIntPredicate(Pred))
1524 return error("invalid integer predicate");
1525 }
1526
1527 lex();
1528 Dest = MachineOperand::CreatePredicate(Pred);
Tim Northover6cd4b232016-08-23 21:01:26 +00001529 if (expectAndConsume(MIToken::rparen))
Tim Northoverde3aea0412016-08-17 20:25:25 +00001530 return error("predicate should be terminated by ')'.");
1531
1532 return false;
1533}
1534
Alex Lorenzef5c1962015-07-28 23:02:45 +00001535bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1536 assert(Token.is(MIToken::kw_target_index));
1537 lex();
1538 if (expectAndConsume(MIToken::lparen))
1539 return true;
1540 if (Token.isNot(MIToken::Identifier))
1541 return error("expected the name of the target index");
1542 int Index = 0;
1543 if (getTargetIndex(Token.stringValue(), Index))
1544 return error("use of undefined target index '" + Token.stringValue() + "'");
1545 lex();
1546 if (expectAndConsume(MIToken::rparen))
1547 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001548 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001549 if (parseOperandsOffset(Dest))
1550 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001551 return false;
1552}
1553
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001554bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1555 assert(Token.is(MIToken::kw_liveout));
1556 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1557 assert(TRI && "Expected target register info");
1558 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1559 lex();
1560 if (expectAndConsume(MIToken::lparen))
1561 return true;
1562 while (true) {
1563 if (Token.isNot(MIToken::NamedRegister))
1564 return error("expected a named register");
Matthias Braun74ad41c2016-10-11 03:13:01 +00001565 unsigned Reg;
1566 if (parseNamedRegister(Reg))
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001567 return true;
1568 lex();
1569 Mask[Reg / 32] |= 1U << (Reg % 32);
1570 // TODO: Report an error if the same register is used more than once.
1571 if (Token.isNot(MIToken::comma))
1572 break;
1573 lex();
1574 }
1575 if (expectAndConsume(MIToken::rparen))
1576 return true;
1577 Dest = MachineOperand::CreateRegLiveOut(Mask);
1578 return false;
1579}
1580
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001581bool MIParser::parseMachineOperand(MachineOperand &Dest,
1582 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001583 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001584 case MIToken::kw_implicit:
1585 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001586 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001587 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001588 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001589 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001590 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001591 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001592 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001593 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001594 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001595 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001596 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001597 case MIToken::IntegerLiteral:
1598 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001599 case MIToken::IntegerType:
1600 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001601 case MIToken::kw_half:
1602 case MIToken::kw_float:
1603 case MIToken::kw_double:
1604 case MIToken::kw_x86_fp80:
1605 case MIToken::kw_fp128:
1606 case MIToken::kw_ppc_fp128:
1607 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001608 case MIToken::MachineBasicBlock:
1609 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001610 case MIToken::StackObject:
1611 return parseStackObjectOperand(Dest);
1612 case MIToken::FixedStackObject:
1613 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001614 case MIToken::GlobalValue:
1615 case MIToken::NamedGlobalValue:
1616 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001617 case MIToken::ConstantPoolItem:
1618 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001619 case MIToken::JumpTableIndex:
1620 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001621 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001622 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00001623 case MIToken::SubRegisterIndex:
1624 return parseSubRegisterIndexOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001625 case MIToken::exclaim:
1626 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001627 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001628 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001629 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001630 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001631 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001632 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001633 case MIToken::kw_blockaddress:
1634 return parseBlockAddressOperand(Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +00001635 case MIToken::kw_intrinsic:
1636 return parseIntrinsicOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001637 case MIToken::kw_target_index:
1638 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001639 case MIToken::kw_liveout:
1640 return parseLiveoutRegisterMaskOperand(Dest);
Tim Northoverde3aea0412016-08-17 20:25:25 +00001641 case MIToken::kw_floatpred:
1642 case MIToken::kw_intpred:
1643 return parsePredicateOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001644 case MIToken::Error:
1645 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001646 case MIToken::Identifier:
1647 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1648 Dest = MachineOperand::CreateRegMask(RegMask);
1649 lex();
1650 break;
1651 }
Justin Bognerb03fd122016-08-17 05:10:15 +00001652 LLVM_FALLTHROUGH;
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001653 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001654 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001655 return error("expected a machine operand");
1656 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001657 return false;
1658}
1659
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001660bool MIParser::parseMachineOperandAndTargetFlags(
1661 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001662 unsigned TF = 0;
1663 bool HasTargetFlags = false;
1664 if (Token.is(MIToken::kw_target_flags)) {
1665 HasTargetFlags = true;
1666 lex();
1667 if (expectAndConsume(MIToken::lparen))
1668 return true;
1669 if (Token.isNot(MIToken::Identifier))
1670 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001671 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1672 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1673 return error("use of undefined target flag '" + Token.stringValue() +
1674 "'");
1675 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001676 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001677 while (Token.is(MIToken::comma)) {
1678 lex();
1679 if (Token.isNot(MIToken::Identifier))
1680 return error("expected the name of the target flag");
1681 unsigned BitFlag = 0;
1682 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1683 return error("use of undefined target flag '" + Token.stringValue() +
1684 "'");
1685 // TODO: Report an error when using a duplicate bit target flag.
1686 TF |= BitFlag;
1687 lex();
1688 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001689 if (expectAndConsume(MIToken::rparen))
1690 return true;
1691 }
1692 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001693 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001694 return true;
1695 if (!HasTargetFlags)
1696 return false;
1697 if (Dest.isReg())
1698 return error(Loc, "register operands can't have target flags");
1699 Dest.setTargetFlags(TF);
1700 return false;
1701}
1702
Alex Lorenzdc24c172015-08-07 20:21:00 +00001703bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001704 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1705 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001706 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001707 bool IsNegative = Token.is(MIToken::minus);
1708 lex();
1709 if (Token.isNot(MIToken::IntegerLiteral))
1710 return error("expected an integer literal after '" + Sign + "'");
1711 if (Token.integerValue().getMinSignedBits() > 64)
1712 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001713 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001714 if (IsNegative)
1715 Offset = -Offset;
1716 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001717 return false;
1718}
1719
Alex Lorenz620f8912015-08-13 20:33:33 +00001720bool MIParser::parseAlignment(unsigned &Alignment) {
1721 assert(Token.is(MIToken::kw_align));
1722 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001723 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001724 return error("expected an integer literal after 'align'");
1725 if (getUnsigned(Alignment))
1726 return true;
1727 lex();
1728 return false;
1729}
1730
Alex Lorenzdc24c172015-08-07 20:21:00 +00001731bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1732 int64_t Offset = 0;
1733 if (parseOffset(Offset))
1734 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001735 Op.setOffset(Offset);
1736 return false;
1737}
1738
Alex Lorenz36593ac2015-08-19 23:27:07 +00001739bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001740 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001741 case MIToken::NamedIRValue: {
Mehdi Aminia53d49e2016-09-17 06:00:02 +00001742 V = MF.getFunction()->getValueSymbolTable()->lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001743 break;
1744 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001745 case MIToken::IRValue: {
1746 unsigned SlotNumber = 0;
1747 if (getUnsigned(SlotNumber))
1748 return true;
1749 V = getIRValue(SlotNumber);
1750 break;
1751 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001752 case MIToken::NamedGlobalValue:
1753 case MIToken::GlobalValue: {
1754 GlobalValue *GV = nullptr;
1755 if (parseGlobalValue(GV))
1756 return true;
1757 V = GV;
1758 break;
1759 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001760 case MIToken::QuotedIRValue: {
1761 const Constant *C = nullptr;
1762 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1763 return true;
1764 V = C;
1765 break;
1766 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001767 default:
1768 llvm_unreachable("The current token should be an IR block reference");
1769 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001770 if (!V)
1771 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001772 return false;
1773}
1774
1775bool MIParser::getUint64(uint64_t &Result) {
1776 assert(Token.hasIntegerValue());
1777 if (Token.integerValue().getActiveBits() > 64)
1778 return error("expected 64-bit integer (too large)");
1779 Result = Token.integerValue().getZExtValue();
1780 return false;
1781}
1782
Justin Lebar0af80cd2016-07-15 18:26:59 +00001783bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
1784 const auto OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001785 switch (Token.kind()) {
1786 case MIToken::kw_volatile:
1787 Flags |= MachineMemOperand::MOVolatile;
1788 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001789 case MIToken::kw_non_temporal:
1790 Flags |= MachineMemOperand::MONonTemporal;
1791 break;
Justin Lebaradbf09e2016-09-11 01:38:58 +00001792 case MIToken::kw_dereferenceable:
1793 Flags |= MachineMemOperand::MODereferenceable;
1794 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001795 case MIToken::kw_invariant:
1796 Flags |= MachineMemOperand::MOInvariant;
1797 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001798 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001799 default:
1800 llvm_unreachable("The current token should be a memory operand flag");
1801 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001802 if (OldFlags == Flags)
1803 // We know that the same flag is specified more than once when the flags
1804 // weren't modified.
1805 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001806 lex();
1807 return false;
1808}
1809
Alex Lorenz91097a32015-08-12 20:33:26 +00001810bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1811 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001812 case MIToken::kw_stack:
1813 PSV = MF.getPSVManager().getStack();
1814 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001815 case MIToken::kw_got:
1816 PSV = MF.getPSVManager().getGOT();
1817 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001818 case MIToken::kw_jump_table:
1819 PSV = MF.getPSVManager().getJumpTable();
1820 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001821 case MIToken::kw_constant_pool:
1822 PSV = MF.getPSVManager().getConstantPool();
1823 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001824 case MIToken::FixedStackObject: {
1825 int FI;
1826 if (parseFixedStackFrameIndex(FI))
1827 return true;
1828 PSV = MF.getPSVManager().getFixedStack(FI);
1829 // The token was already consumed, so use return here instead of break.
1830 return false;
1831 }
Matthias Braun3ef7df92016-06-08 00:47:07 +00001832 case MIToken::StackObject: {
1833 int FI;
1834 if (parseStackFrameIndex(FI))
1835 return true;
1836 PSV = MF.getPSVManager().getFixedStack(FI);
1837 // The token was already consumed, so use return here instead of break.
1838 return false;
1839 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001840 case MIToken::kw_call_entry: {
1841 lex();
1842 switch (Token.kind()) {
1843 case MIToken::GlobalValue:
1844 case MIToken::NamedGlobalValue: {
1845 GlobalValue *GV = nullptr;
1846 if (parseGlobalValue(GV))
1847 return true;
1848 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1849 break;
1850 }
1851 case MIToken::ExternalSymbol:
1852 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1853 MF.createExternalSymbolName(Token.stringValue()));
1854 break;
1855 default:
1856 return error(
1857 "expected a global value or an external symbol after 'call-entry'");
1858 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001859 break;
1860 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001861 default:
1862 llvm_unreachable("The current token should be pseudo source value");
1863 }
1864 lex();
1865 return false;
1866}
1867
1868bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001869 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001870 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Matthias Braun3ef7df92016-06-08 00:47:07 +00001871 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
1872 Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001873 const PseudoSourceValue *PSV = nullptr;
1874 if (parseMemoryPseudoSourceValue(PSV))
1875 return true;
1876 int64_t Offset = 0;
1877 if (parseOffset(Offset))
1878 return true;
1879 Dest = MachinePointerInfo(PSV, Offset);
1880 return false;
1881 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001882 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1883 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001884 Token.isNot(MIToken::NamedGlobalValue) &&
1885 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001886 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001887 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001888 if (parseIRValue(V))
1889 return true;
1890 if (!V->getType()->isPointerTy())
1891 return error("expected a pointer IR value");
1892 lex();
1893 int64_t Offset = 0;
1894 if (parseOffset(Offset))
1895 return true;
1896 Dest = MachinePointerInfo(V, Offset);
1897 return false;
1898}
1899
Alex Lorenz4af7e612015-08-03 23:08:19 +00001900bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1901 if (expectAndConsume(MIToken::lparen))
1902 return true;
Justin Lebar0af80cd2016-07-15 18:26:59 +00001903 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
Alex Lorenza518b792015-08-04 00:24:45 +00001904 while (Token.isMemoryOperandFlag()) {
1905 if (parseMemoryOperandFlag(Flags))
1906 return true;
1907 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001908 if (Token.isNot(MIToken::Identifier) ||
1909 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1910 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001911 if (Token.stringValue() == "load")
1912 Flags |= MachineMemOperand::MOLoad;
1913 else
1914 Flags |= MachineMemOperand::MOStore;
1915 lex();
1916
1917 if (Token.isNot(MIToken::IntegerLiteral))
1918 return error("expected the size integer literal after memory operation");
1919 uint64_t Size;
1920 if (getUint64(Size))
1921 return true;
1922 lex();
1923
Alex Lorenz91097a32015-08-12 20:33:26 +00001924 MachinePointerInfo Ptr = MachinePointerInfo();
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001925 if (Token.is(MIToken::Identifier)) {
1926 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1927 if (Token.stringValue() != Word)
1928 return error(Twine("expected '") + Word + "'");
1929 lex();
1930
1931 if (parseMachinePointerInfo(Ptr))
1932 return true;
1933 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001934 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001935 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001936 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001937 while (consumeIfPresent(MIToken::comma)) {
1938 switch (Token.kind()) {
1939 case MIToken::kw_align:
1940 if (parseAlignment(BaseAlignment))
1941 return true;
1942 break;
1943 case MIToken::md_tbaa:
1944 lex();
1945 if (parseMDNode(AAInfo.TBAA))
1946 return true;
1947 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001948 case MIToken::md_alias_scope:
1949 lex();
1950 if (parseMDNode(AAInfo.Scope))
1951 return true;
1952 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001953 case MIToken::md_noalias:
1954 lex();
1955 if (parseMDNode(AAInfo.NoAlias))
1956 return true;
1957 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001958 case MIToken::md_range:
1959 lex();
1960 if (parseMDNode(Range))
1961 return true;
1962 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001963 // TODO: Report an error on duplicate metadata nodes.
1964 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001965 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1966 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001967 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001968 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001969 if (expectAndConsume(MIToken::rparen))
1970 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001971 Dest =
1972 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001973 return false;
1974}
1975
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001976void MIParser::initNames2InstrOpCodes() {
1977 if (!Names2InstrOpCodes.empty())
1978 return;
1979 const auto *TII = MF.getSubtarget().getInstrInfo();
1980 assert(TII && "Expected target instruction info");
1981 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1982 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1983}
1984
1985bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1986 initNames2InstrOpCodes();
1987 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1988 if (InstrInfo == Names2InstrOpCodes.end())
1989 return true;
1990 OpCode = InstrInfo->getValue();
1991 return false;
1992}
1993
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001994void MIParser::initNames2Regs() {
1995 if (!Names2Regs.empty())
1996 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001997 // The '%noreg' register is the register 0.
1998 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001999 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2000 assert(TRI && "Expected target register info");
2001 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
2002 bool WasInserted =
2003 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
2004 .second;
2005 (void)WasInserted;
2006 assert(WasInserted && "Expected registers to be unique case-insensitively");
2007 }
2008}
2009
2010bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
2011 initNames2Regs();
2012 auto RegInfo = Names2Regs.find(RegName);
2013 if (RegInfo == Names2Regs.end())
2014 return true;
2015 Reg = RegInfo->getValue();
2016 return false;
2017}
2018
Alex Lorenz8f6f4282015-06-29 16:57:06 +00002019void MIParser::initNames2RegMasks() {
2020 if (!Names2RegMasks.empty())
2021 return;
2022 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2023 assert(TRI && "Expected target register info");
2024 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
2025 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
2026 assert(RegMasks.size() == RegMaskNames.size());
2027 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
2028 Names2RegMasks.insert(
2029 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
2030}
2031
2032const uint32_t *MIParser::getRegMask(StringRef Identifier) {
2033 initNames2RegMasks();
2034 auto RegMaskInfo = Names2RegMasks.find(Identifier);
2035 if (RegMaskInfo == Names2RegMasks.end())
2036 return nullptr;
2037 return RegMaskInfo->getValue();
2038}
2039
Alex Lorenz2eacca82015-07-13 23:24:34 +00002040void MIParser::initNames2SubRegIndices() {
2041 if (!Names2SubRegIndices.empty())
2042 return;
2043 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2044 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
2045 Names2SubRegIndices.insert(
2046 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
2047}
2048
2049unsigned MIParser::getSubRegIndex(StringRef Name) {
2050 initNames2SubRegIndices();
2051 auto SubRegInfo = Names2SubRegIndices.find(Name);
2052 if (SubRegInfo == Names2SubRegIndices.end())
2053 return 0;
2054 return SubRegInfo->getValue();
2055}
2056
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002057static void initSlots2BasicBlocks(
2058 const Function &F,
2059 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2060 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002061 MST.incorporateFunction(F);
2062 for (auto &BB : F) {
2063 if (BB.hasName())
2064 continue;
2065 int Slot = MST.getLocalSlot(&BB);
2066 if (Slot == -1)
2067 continue;
2068 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2069 }
2070}
2071
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002072static const BasicBlock *getIRBlockFromSlot(
2073 unsigned Slot,
2074 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002075 auto BlockInfo = Slots2BasicBlocks.find(Slot);
2076 if (BlockInfo == Slots2BasicBlocks.end())
2077 return nullptr;
2078 return BlockInfo->second;
2079}
2080
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002081const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2082 if (Slots2BasicBlocks.empty())
2083 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
2084 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2085}
2086
2087const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2088 if (&F == MF.getFunction())
2089 return getIRBlock(Slot);
2090 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2091 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2092 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2093}
2094
Alex Lorenzdd13be02015-08-19 23:31:05 +00002095static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2096 DenseMap<unsigned, const Value *> &Slots2Values) {
2097 int Slot = MST.getLocalSlot(V);
2098 if (Slot == -1)
2099 return;
2100 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2101}
2102
2103/// Creates the mapping from slot numbers to function's unnamed IR values.
2104static void initSlots2Values(const Function &F,
2105 DenseMap<unsigned, const Value *> &Slots2Values) {
2106 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2107 MST.incorporateFunction(F);
2108 for (const auto &Arg : F.args())
2109 mapValueToSlot(&Arg, MST, Slots2Values);
2110 for (const auto &BB : F) {
2111 mapValueToSlot(&BB, MST, Slots2Values);
2112 for (const auto &I : BB)
2113 mapValueToSlot(&I, MST, Slots2Values);
2114 }
2115}
2116
2117const Value *MIParser::getIRValue(unsigned Slot) {
2118 if (Slots2Values.empty())
2119 initSlots2Values(*MF.getFunction(), Slots2Values);
2120 auto ValueInfo = Slots2Values.find(Slot);
2121 if (ValueInfo == Slots2Values.end())
2122 return nullptr;
2123 return ValueInfo->second;
2124}
2125
Alex Lorenzef5c1962015-07-28 23:02:45 +00002126void MIParser::initNames2TargetIndices() {
2127 if (!Names2TargetIndices.empty())
2128 return;
2129 const auto *TII = MF.getSubtarget().getInstrInfo();
2130 assert(TII && "Expected target instruction info");
2131 auto Indices = TII->getSerializableTargetIndices();
2132 for (const auto &I : Indices)
2133 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2134}
2135
2136bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2137 initNames2TargetIndices();
2138 auto IndexInfo = Names2TargetIndices.find(Name);
2139 if (IndexInfo == Names2TargetIndices.end())
2140 return true;
2141 Index = IndexInfo->second;
2142 return false;
2143}
2144
Alex Lorenz49873a82015-08-06 00:44:07 +00002145void MIParser::initNames2DirectTargetFlags() {
2146 if (!Names2DirectTargetFlags.empty())
2147 return;
2148 const auto *TII = MF.getSubtarget().getInstrInfo();
2149 assert(TII && "Expected target instruction info");
2150 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2151 for (const auto &I : Flags)
2152 Names2DirectTargetFlags.insert(
2153 std::make_pair(StringRef(I.second), I.first));
2154}
2155
2156bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2157 initNames2DirectTargetFlags();
2158 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2159 if (FlagInfo == Names2DirectTargetFlags.end())
2160 return true;
2161 Flag = FlagInfo->second;
2162 return false;
2163}
2164
Alex Lorenzf3630112015-08-18 22:52:15 +00002165void MIParser::initNames2BitmaskTargetFlags() {
2166 if (!Names2BitmaskTargetFlags.empty())
2167 return;
2168 const auto *TII = MF.getSubtarget().getInstrInfo();
2169 assert(TII && "Expected target instruction info");
2170 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2171 for (const auto &I : Flags)
2172 Names2BitmaskTargetFlags.insert(
2173 std::make_pair(StringRef(I.second), I.first));
2174}
2175
2176bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2177 initNames2BitmaskTargetFlags();
2178 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2179 if (FlagInfo == Names2BitmaskTargetFlags.end())
2180 return true;
2181 Flag = FlagInfo->second;
2182 return false;
2183}
2184
Matthias Braun83947862016-07-13 22:23:23 +00002185bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2186 StringRef Src,
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002187 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002188 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002189}
2190
Matthias Braun74ad41c2016-10-11 03:13:01 +00002191bool llvm::parseMachineInstructions(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002192 StringRef Src, SMDiagnostic &Error) {
2193 return MIParser(PFS, Error, Src).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002194}
Alex Lorenzf09df002015-06-30 18:16:42 +00002195
Matthias Braun74ad41c2016-10-11 03:13:01 +00002196bool llvm::parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002197 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +00002198 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002199 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002200}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002201
Matthias Braun74ad41c2016-10-11 03:13:01 +00002202bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002203 unsigned &Reg, StringRef Src,
Alex Lorenz9fab3702015-07-14 21:24:41 +00002204 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002205 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002206}
Alex Lorenz12045a42015-07-27 17:42:45 +00002207
Matthias Braun74ad41c2016-10-11 03:13:01 +00002208bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState &PFS,
2209 VRegInfo *&Info, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +00002210 SMDiagnostic &Error) {
Matthias Braun74ad41c2016-10-11 03:13:01 +00002211 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
Alex Lorenz12045a42015-07-27 17:42:45 +00002212}
Alex Lorenza314d812015-08-18 22:26:26 +00002213
Matthias Braun74ad41c2016-10-11 03:13:01 +00002214bool llvm::parseStackObjectReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002215 int &FI, StringRef Src,
Alex Lorenza314d812015-08-18 22:26:26 +00002216 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002217 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
Alex Lorenza314d812015-08-18 22:26:26 +00002218}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002219
Matthias Braun74ad41c2016-10-11 03:13:01 +00002220bool llvm::parseMDNode(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002221 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2222 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002223}