blob: cac22af32956ebe44b9d0a23bcc06ba6a015879f [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"
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +000039#include <cctype>
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000040
41using namespace llvm;
42
Matthias Braune35861d2016-07-13 23:27:50 +000043PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
Matthias Braunde5fea22017-01-18 00:59:19 +000044 SourceMgr &SM, const SlotMapping &IRSlots,
45 const Name2RegClassMap &Names2RegClasses,
46 const Name2RegBankMap &Names2RegBanks)
47 : MF(MF), SM(&SM), IRSlots(IRSlots), Names2RegClasses(Names2RegClasses),
48 Names2RegBanks(Names2RegBanks) {
Matthias Braun83947862016-07-13 22:23:23 +000049}
50
Matthias Braun74ad41c2016-10-11 03:13:01 +000051VRegInfo &PerFunctionMIParsingState::getVRegInfo(unsigned Num) {
52 auto I = VRegInfos.insert(std::make_pair(Num, nullptr));
53 if (I.second) {
54 MachineRegisterInfo &MRI = MF.getRegInfo();
55 VRegInfo *Info = new (Allocator) VRegInfo;
56 Info->VReg = MRI.createIncompleteVirtualRegister();
57 I.first->second = Info;
58 }
59 return *I.first->second;
60}
61
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000062namespace {
63
Alex Lorenz36962cd2015-07-07 02:08:46 +000064/// A wrapper struct around the 'MachineOperand' struct that includes a source
Alex Lorenzfeb6b432015-08-19 19:19:16 +000065/// range and other attributes.
66struct ParsedMachineOperand {
Alex Lorenz36962cd2015-07-07 02:08:46 +000067 MachineOperand Operand;
68 StringRef::iterator Begin;
69 StringRef::iterator End;
Alex Lorenz5ef93b02015-08-19 19:05:34 +000070 Optional<unsigned> TiedDefIdx;
Alex Lorenz36962cd2015-07-07 02:08:46 +000071
Alex Lorenzfeb6b432015-08-19 19:19:16 +000072 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
73 StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
Alex Lorenz5ef93b02015-08-19 19:05:34 +000074 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
75 if (TiedDefIdx)
76 assert(Operand.isReg() && Operand.isUse() &&
77 "Only used register operands can be tied");
78 }
Alex Lorenz36962cd2015-07-07 02:08:46 +000079};
80
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000081class MIParser {
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000082 MachineFunction &MF;
83 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000084 StringRef Source, CurrentSource;
85 MIToken Token;
Matthias Braun74ad41c2016-10-11 03:13:01 +000086 PerFunctionMIParsingState &PFS;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000087 /// Maps from instruction names to op codes.
88 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000089 /// Maps from register names to registers.
90 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000091 /// Maps from register mask names to register masks.
92 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000093 /// Maps from subregister names to subregister indices.
94 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000095 /// Maps from slot numbers to function's unnamed basic blocks.
96 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzdd13be02015-08-19 23:31:05 +000097 /// Maps from slot numbers to function's unnamed values.
98 DenseMap<unsigned, const Value *> Slots2Values;
Alex Lorenzef5c1962015-07-28 23:02:45 +000099 /// Maps from target index names to target indices.
100 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +0000101 /// Maps from direct target flag names to the direct target flag values.
102 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenzf3630112015-08-18 22:52:15 +0000103 /// Maps from direct target flag names to the bitmask target flag values.
104 StringMap<unsigned> Names2BitmaskTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000105
106public:
Matthias Braun74ad41c2016-10-11 03:13:01 +0000107 MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
Matthias Braune35861d2016-07-13 23:27:50 +0000108 StringRef Source);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000109
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000110 /// \p SkipChar gives the number of characters to skip before looking
111 /// for the next token.
112 void lex(unsigned SkipChar = 0);
Alex Lorenz91370c52015-06-22 20:37:46 +0000113
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000114 /// Report an error at the current location with the given message.
115 ///
116 /// This function always return true.
117 bool error(const Twine &Msg);
118
Alex Lorenz91370c52015-06-22 20:37:46 +0000119 /// Report an error at the given location with the given message.
120 ///
121 /// This function always return true.
122 bool error(StringRef::iterator Loc, const Twine &Msg);
123
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000124 bool
125 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
126 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +0000127 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +0000128 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
129 bool parseStandaloneNamedRegister(unsigned &Reg);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000130 bool parseStandaloneVirtualRegister(VRegInfo *&Info);
Tom Stellard9c884e42016-11-15 00:03:14 +0000131 bool parseStandaloneRegister(unsigned &Reg);
Alex Lorenza314d812015-08-18 22:26:26 +0000132 bool parseStandaloneStackObject(int &FI);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000133 bool parseStandaloneMDNode(MDNode *&Node);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000134
135 bool
136 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
137 bool parseBasicBlock(MachineBasicBlock &MBB);
138 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
139 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000140
Matthias Braun74ad41c2016-10-11 03:13:01 +0000141 bool parseNamedRegister(unsigned &Reg);
142 bool parseVirtualRegister(VRegInfo *&Info);
143 bool parseRegister(unsigned &Reg, VRegInfo *&VRegInfo);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000144 bool parseRegisterFlag(unsigned &Flags);
Matthias Braunde5fea22017-01-18 00:59:19 +0000145 bool parseRegisterClassOrBank(VRegInfo &RegInfo);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000146 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000147 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
148 bool parseRegisterOperand(MachineOperand &Dest,
149 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000150 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000151 bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
152 const Constant *&C);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000153 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Ahmed Bougachad760de02016-07-28 17:15:12 +0000154 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
Alex Lorenz05e38822015-08-05 18:52:21 +0000155 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000156 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000157 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000158 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000159 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000160 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000161 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000162 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000163 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000164 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000165 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +0000166 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000167 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000168 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000169 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000170 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000171 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000172 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000173 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000174 bool parseIRBlock(BasicBlock *&BB, const Function &F);
175 bool parseBlockAddressOperand(MachineOperand &Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +0000176 bool parseIntrinsicOperand(MachineOperand &Dest);
Tim Northoverde3aea0412016-08-17 20:25:25 +0000177 bool parsePredicateOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000178 bool parseTargetIndexOperand(MachineOperand &Dest);
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000179 bool parseCustomRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000180 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000181 bool parseMachineOperand(MachineOperand &Dest,
182 Optional<unsigned> &TiedDefIdx);
183 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
184 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000185 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000186 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000187 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000188 bool parseIRValue(const Value *&V);
Justin Lebar0af80cd2016-07-15 18:26:59 +0000189 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000190 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
191 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Tim Northoverb73e3092017-02-13 22:14:08 +0000192 bool parseOptionalAtomicOrdering(AtomicOrdering &Order);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000193 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000194
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000195private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000196 /// Convert the integer literal in the current token into an unsigned integer.
197 ///
198 /// Return true if an error occurred.
199 bool getUnsigned(unsigned &Result);
200
Alex Lorenz4af7e612015-08-03 23:08:19 +0000201 /// Convert the integer literal in the current token into an uint64.
202 ///
203 /// Return true if an error occurred.
204 bool getUint64(uint64_t &Result);
205
Krzysztof Parzyszek36ef5dc2016-12-16 13:58:01 +0000206 /// Convert the hexadecimal literal in the current token into an unsigned
207 /// APInt with a minimum bitwidth required to represent the value.
208 ///
209 /// Return true if the literal does not represent an integer value.
210 bool getHexUint(APInt &Result);
211
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000212 /// If the current token is of the given kind, consume it and return false.
213 /// Otherwise report an error and return true.
214 bool expectAndConsume(MIToken::TokenKind TokenKind);
215
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000216 /// If the current token is of the given kind, consume it and return true.
217 /// Otherwise return false.
218 bool consumeIfPresent(MIToken::TokenKind TokenKind);
219
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000220 void initNames2InstrOpCodes();
221
222 /// Try to convert an instruction name to an opcode. Return true if the
223 /// instruction name is invalid.
224 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000225
Alex Lorenze5a44662015-07-17 00:24:15 +0000226 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000227
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000228 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000229 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000230
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000231 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000232 const MCInstrDesc &MCID);
233
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000234 void initNames2Regs();
235
236 /// Try to convert a register name to a register number. Return true if the
237 /// register name is invalid.
238 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000239
240 void initNames2RegMasks();
241
242 /// Check if the given identifier is a name of a register mask.
243 ///
244 /// Return null if the identifier isn't a register mask.
245 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000246
247 void initNames2SubRegIndices();
248
249 /// Check if the given identifier is a name of a subregister index.
250 ///
251 /// Return 0 if the name isn't a subregister index class.
252 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000253
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000254 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000255 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000256
Alex Lorenzdd13be02015-08-19 23:31:05 +0000257 const Value *getIRValue(unsigned Slot);
258
Alex Lorenzef5c1962015-07-28 23:02:45 +0000259 void initNames2TargetIndices();
260
261 /// Try to convert a name of target index to the corresponding target index.
262 ///
263 /// Return true if the name isn't a name of a target index.
264 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000265
266 void initNames2DirectTargetFlags();
267
268 /// Try to convert a name of a direct target flag to the corresponding
269 /// target flag.
270 ///
271 /// Return true if the name isn't a name of a direct flag.
272 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000273
274 void initNames2BitmaskTargetFlags();
275
276 /// Try to convert a name of a bitmask target flag to the corresponding
277 /// target flag.
278 ///
279 /// Return true if the name isn't a name of a bitmask target flag.
280 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000281};
282
283} // end anonymous namespace
284
Matthias Braun74ad41c2016-10-11 03:13:01 +0000285MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
Matthias Braune35861d2016-07-13 23:27:50 +0000286 StringRef Source)
287 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
288{}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000289
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000290void MIParser::lex(unsigned SkipChar) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000291 CurrentSource = lexMIToken(
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000292 CurrentSource.data() + SkipChar, Token,
Alex Lorenz91370c52015-06-22 20:37:46 +0000293 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
294}
295
296bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
297
298bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Matthias Braune35861d2016-07-13 23:27:50 +0000299 const SourceMgr &SM = *PFS.SM;
Alex Lorenz91370c52015-06-22 20:37:46 +0000300 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000301 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
302 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
303 // Create an ordinary diagnostic when the source manager's buffer is the
304 // source string.
305 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
306 return true;
307 }
308 // Create a diagnostic for a YAML string literal.
309 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
310 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
311 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000312 return true;
313}
314
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000315static const char *toString(MIToken::TokenKind TokenKind) {
316 switch (TokenKind) {
317 case MIToken::comma:
318 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000319 case MIToken::equal:
320 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000321 case MIToken::colon:
322 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000323 case MIToken::lparen:
324 return "'('";
325 case MIToken::rparen:
326 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000327 default:
328 return "<unknown token>";
329 }
330}
331
332bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
333 if (Token.isNot(TokenKind))
334 return error(Twine("expected ") + toString(TokenKind));
335 lex();
336 return false;
337}
338
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000339bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
340 if (Token.isNot(TokenKind))
341 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000342 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000343 return true;
344}
Alex Lorenz91370c52015-06-22 20:37:46 +0000345
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000346bool MIParser::parseBasicBlockDefinition(
347 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
348 assert(Token.is(MIToken::MachineBasicBlockLabel));
349 unsigned ID = 0;
350 if (getUnsigned(ID))
351 return true;
352 auto Loc = Token.location();
353 auto Name = Token.stringValue();
354 lex();
355 bool HasAddressTaken = false;
356 bool IsLandingPad = false;
357 unsigned Alignment = 0;
358 BasicBlock *BB = nullptr;
359 if (consumeIfPresent(MIToken::lparen)) {
360 do {
361 // TODO: Report an error when multiple same attributes are specified.
362 switch (Token.kind()) {
363 case MIToken::kw_address_taken:
364 HasAddressTaken = true;
365 lex();
366 break;
367 case MIToken::kw_landing_pad:
368 IsLandingPad = true;
369 lex();
370 break;
371 case MIToken::kw_align:
372 if (parseAlignment(Alignment))
373 return true;
374 break;
375 case MIToken::IRBlock:
376 // TODO: Report an error when both name and ir block are specified.
377 if (parseIRBlock(BB, *MF.getFunction()))
378 return true;
379 lex();
380 break;
381 default:
382 break;
383 }
384 } while (consumeIfPresent(MIToken::comma));
385 if (expectAndConsume(MIToken::rparen))
386 return true;
387 }
388 if (expectAndConsume(MIToken::colon))
389 return true;
390
391 if (!Name.empty()) {
392 BB = dyn_cast_or_null<BasicBlock>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000393 MF.getFunction()->getValueSymbolTable()->lookup(Name));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000394 if (!BB)
395 return error(Loc, Twine("basic block '") + Name +
396 "' is not defined in the function '" +
397 MF.getName() + "'");
398 }
399 auto *MBB = MF.CreateMachineBasicBlock(BB);
400 MF.insert(MF.end(), MBB);
401 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
402 if (!WasInserted)
403 return error(Loc, Twine("redefinition of machine basic block with id #") +
404 Twine(ID));
405 if (Alignment)
406 MBB->setAlignment(Alignment);
407 if (HasAddressTaken)
408 MBB->setHasAddressTaken();
Reid Kleckner0e288232015-08-27 23:27:47 +0000409 MBB->setIsEHPad(IsLandingPad);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000410 return false;
411}
412
413bool MIParser::parseBasicBlockDefinitions(
414 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
415 lex();
416 // Skip until the first machine basic block.
417 while (Token.is(MIToken::Newline))
418 lex();
419 if (Token.isErrorOrEOF())
420 return Token.isError();
421 if (Token.isNot(MIToken::MachineBasicBlockLabel))
422 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000423 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000424 do {
425 if (parseBasicBlockDefinition(MBBSlots))
426 return true;
427 bool IsAfterNewline = false;
428 // Skip until the next machine basic block.
429 while (true) {
430 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
431 Token.isErrorOrEOF())
432 break;
433 else if (Token.is(MIToken::MachineBasicBlockLabel))
434 return error("basic block definition should be located at the start of "
435 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000436 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000437 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000438 continue;
439 }
440 IsAfterNewline = false;
441 if (Token.is(MIToken::lbrace))
442 ++BraceDepth;
443 if (Token.is(MIToken::rbrace)) {
444 if (!BraceDepth)
445 return error("extraneous closing brace ('}')");
446 --BraceDepth;
447 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000448 lex();
449 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000450 // Verify that we closed all of the '{' at the end of a file or a block.
451 if (!Token.isError() && BraceDepth)
452 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000453 } while (!Token.isErrorOrEOF());
454 return Token.isError();
455}
456
457bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
458 assert(Token.is(MIToken::kw_liveins));
459 lex();
460 if (expectAndConsume(MIToken::colon))
461 return true;
462 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
463 return false;
464 do {
465 if (Token.isNot(MIToken::NamedRegister))
466 return error("expected a named register");
467 unsigned Reg = 0;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000468 if (parseNamedRegister(Reg))
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000469 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000470 lex();
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000471 LaneBitmask Mask = LaneBitmask::getAll();
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000472 if (consumeIfPresent(MIToken::colon)) {
473 // Parse lane mask.
474 if (Token.isNot(MIToken::IntegerLiteral) &&
475 Token.isNot(MIToken::HexLiteral))
476 return error("expected a lane mask");
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000477 static_assert(sizeof(LaneBitmask::Type) == sizeof(unsigned),
478 "Use correct get-function for lane mask");
479 LaneBitmask::Type V;
480 if (getUnsigned(V))
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000481 return error("invalid lane mask value");
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000482 Mask = LaneBitmask(V);
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000483 lex();
484 }
485 MBB.addLiveIn(Reg, Mask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000486 } while (consumeIfPresent(MIToken::comma));
487 return false;
488}
489
490bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
491 assert(Token.is(MIToken::kw_successors));
492 lex();
493 if (expectAndConsume(MIToken::colon))
494 return true;
495 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
496 return false;
497 do {
498 if (Token.isNot(MIToken::MachineBasicBlock))
499 return error("expected a machine basic block reference");
500 MachineBasicBlock *SuccMBB = nullptr;
501 if (parseMBBReference(SuccMBB))
502 return true;
503 lex();
504 unsigned Weight = 0;
505 if (consumeIfPresent(MIToken::lparen)) {
Geoff Berryb51774a2016-11-18 19:37:24 +0000506 if (Token.isNot(MIToken::IntegerLiteral) &&
507 Token.isNot(MIToken::HexLiteral))
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000508 return error("expected an integer literal after '('");
509 if (getUnsigned(Weight))
510 return true;
511 lex();
512 if (expectAndConsume(MIToken::rparen))
513 return true;
514 }
Cong Houd97c1002015-12-01 05:29:22 +0000515 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000516 } while (consumeIfPresent(MIToken::comma));
Cong Houd97c1002015-12-01 05:29:22 +0000517 MBB.normalizeSuccProbs();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000518 return false;
519}
520
521bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
522 // Skip the definition.
523 assert(Token.is(MIToken::MachineBasicBlockLabel));
524 lex();
525 if (consumeIfPresent(MIToken::lparen)) {
526 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
527 lex();
528 consumeIfPresent(MIToken::rparen);
529 }
530 consumeIfPresent(MIToken::colon);
531
532 // Parse the liveins and successors.
533 // N.B: Multiple lists of successors and liveins are allowed and they're
534 // merged into one.
535 // Example:
536 // liveins: %edi
537 // liveins: %esi
538 //
539 // is equivalent to
540 // liveins: %edi, %esi
541 while (true) {
542 if (Token.is(MIToken::kw_successors)) {
543 if (parseBasicBlockSuccessors(MBB))
544 return true;
545 } else if (Token.is(MIToken::kw_liveins)) {
546 if (parseBasicBlockLiveins(MBB))
547 return true;
548 } else if (consumeIfPresent(MIToken::Newline)) {
549 continue;
550 } else
551 break;
552 if (!Token.isNewlineOrEOF())
553 return error("expected line break at the end of a list");
554 lex();
555 }
556
557 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000558 bool IsInBundle = false;
559 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000560 while (true) {
561 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
562 return false;
563 else if (consumeIfPresent(MIToken::Newline))
564 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000565 if (consumeIfPresent(MIToken::rbrace)) {
566 // The first parsing pass should verify that all closing '}' have an
567 // opening '{'.
568 assert(IsInBundle);
569 IsInBundle = false;
570 continue;
571 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000572 MachineInstr *MI = nullptr;
573 if (parse(MI))
574 return true;
575 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000576 if (IsInBundle) {
577 PrevMI->setFlag(MachineInstr::BundledSucc);
578 MI->setFlag(MachineInstr::BundledPred);
579 }
580 PrevMI = MI;
581 if (Token.is(MIToken::lbrace)) {
582 if (IsInBundle)
583 return error("nested instruction bundles are not allowed");
584 lex();
585 // This instruction is the start of the bundle.
586 MI->setFlag(MachineInstr::BundledSucc);
587 IsInBundle = true;
588 if (!Token.is(MIToken::Newline))
589 // The next instruction can be on the same line.
590 continue;
591 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000592 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
593 lex();
594 }
595 return false;
596}
597
598bool MIParser::parseBasicBlocks() {
599 lex();
600 // Skip until the first machine basic block.
601 while (Token.is(MIToken::Newline))
602 lex();
603 if (Token.isErrorOrEOF())
604 return Token.isError();
605 // The first parsing pass should have verified that this token is a MBB label
606 // in the 'parseBasicBlockDefinitions' method.
607 assert(Token.is(MIToken::MachineBasicBlockLabel));
608 do {
609 MachineBasicBlock *MBB = nullptr;
610 if (parseMBBReference(MBB))
611 return true;
612 if (parseBasicBlock(*MBB))
613 return true;
614 // The method 'parseBasicBlock' should parse the whole block until the next
615 // block or the end of file.
616 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
617 } while (Token.isNot(MIToken::Eof));
618 return false;
619}
620
621bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000622 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000623 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000624 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000625 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000626 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000627 Optional<unsigned> TiedDefIdx;
628 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000629 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000630 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000631 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000632 if (Token.isNot(MIToken::comma))
633 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000634 lex();
635 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000636 if (!Operands.empty() && expectAndConsume(MIToken::equal))
637 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000638
Alex Lorenze5a44662015-07-17 00:24:15 +0000639 unsigned OpCode, Flags = 0;
640 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000641 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000642
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000643 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000644 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000645 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000646 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000647 Optional<unsigned> TiedDefIdx;
648 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000649 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000650 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000651 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000652 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
653 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000654 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000655 if (Token.isNot(MIToken::comma))
656 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000657 lex();
658 }
659
Alex Lorenz46d760d2015-07-22 21:15:11 +0000660 DebugLoc DebugLocation;
661 if (Token.is(MIToken::kw_debug_location)) {
662 lex();
663 if (Token.isNot(MIToken::exclaim))
664 return error("expected a metadata node after 'debug-location'");
665 MDNode *Node = nullptr;
666 if (parseMDNode(Node))
667 return true;
668 DebugLocation = DebugLoc(Node);
669 }
670
Alex Lorenz4af7e612015-08-03 23:08:19 +0000671 // Parse the machine memory operands.
672 SmallVector<MachineMemOperand *, 2> MemOperands;
673 if (Token.is(MIToken::coloncolon)) {
674 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000675 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000676 MachineMemOperand *MemOp = nullptr;
677 if (parseMachineMemoryOperand(MemOp))
678 return true;
679 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000680 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000681 break;
682 if (Token.isNot(MIToken::comma))
683 return error("expected ',' before the next machine memory operand");
684 lex();
685 }
686 }
687
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000688 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000689 if (!MCID.isVariadic()) {
690 // FIXME: Move the implicit operand verification to the machine verifier.
691 if (verifyImplicitOperands(Operands, MCID))
692 return true;
693 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000694
Alex Lorenzcb268d42015-07-06 23:07:26 +0000695 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000696 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000697 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000698 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000699 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000700 if (assignRegisterTies(*MI, Operands))
701 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000702 if (MemOperands.empty())
703 return false;
704 MachineInstr::mmo_iterator MemRefs =
705 MF.allocateMemRefsArray(MemOperands.size());
706 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
707 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000708 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000709}
710
Alex Lorenz1ea60892015-07-27 20:29:27 +0000711bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000712 lex();
713 if (Token.isNot(MIToken::MachineBasicBlock))
714 return error("expected a machine basic block reference");
715 if (parseMBBReference(MBB))
716 return true;
717 lex();
718 if (Token.isNot(MIToken::Eof))
719 return error(
720 "expected end of string after the machine basic block reference");
721 return false;
722}
723
Alex Lorenz1ea60892015-07-27 20:29:27 +0000724bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000725 lex();
726 if (Token.isNot(MIToken::NamedRegister))
727 return error("expected a named register");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000728 if (parseNamedRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000729 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000730 lex();
731 if (Token.isNot(MIToken::Eof))
732 return error("expected end of string after the register reference");
733 return false;
734}
735
Matthias Braun74ad41c2016-10-11 03:13:01 +0000736bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
Alex Lorenz12045a42015-07-27 17:42:45 +0000737 lex();
738 if (Token.isNot(MIToken::VirtualRegister))
739 return error("expected a virtual register");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000740 if (parseVirtualRegister(Info))
Alex Lorenz607efb62015-08-18 22:57:36 +0000741 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000742 lex();
743 if (Token.isNot(MIToken::Eof))
744 return error("expected end of string after the register reference");
745 return false;
746}
747
Tom Stellard9c884e42016-11-15 00:03:14 +0000748bool MIParser::parseStandaloneRegister(unsigned &Reg) {
749 lex();
750 if (Token.isNot(MIToken::NamedRegister) &&
751 Token.isNot(MIToken::VirtualRegister))
752 return error("expected either a named or virtual register");
753
754 VRegInfo *Info;
755 if (parseRegister(Reg, Info))
756 return true;
757
758 lex();
759 if (Token.isNot(MIToken::Eof))
760 return error("expected end of string after the register reference");
761 return false;
762}
763
Alex Lorenza314d812015-08-18 22:26:26 +0000764bool MIParser::parseStandaloneStackObject(int &FI) {
765 lex();
766 if (Token.isNot(MIToken::StackObject))
767 return error("expected a stack object");
768 if (parseStackFrameIndex(FI))
769 return true;
770 if (Token.isNot(MIToken::Eof))
771 return error("expected end of string after the stack object reference");
772 return false;
773}
774
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000775bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
776 lex();
777 if (Token.isNot(MIToken::exclaim))
778 return error("expected a metadata node");
779 if (parseMDNode(Node))
780 return true;
781 if (Token.isNot(MIToken::Eof))
782 return error("expected end of string after the metadata node");
783 return false;
784}
785
Alex Lorenz36962cd2015-07-07 02:08:46 +0000786static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
787 assert(MO.isImplicit());
788 return MO.isDef() ? "implicit-def" : "implicit";
789}
790
791static std::string getRegisterName(const TargetRegisterInfo *TRI,
792 unsigned Reg) {
793 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
794 return StringRef(TRI->getName(Reg)).lower();
795}
796
Alex Lorenz0153e592015-09-10 14:04:34 +0000797/// Return true if the parsed machine operands contain a given machine operand.
798static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
799 ArrayRef<ParsedMachineOperand> Operands) {
800 for (const auto &I : Operands) {
801 if (ImplicitOperand.isIdenticalTo(I.Operand))
802 return true;
803 }
804 return false;
805}
806
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000807bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
808 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000809 if (MCID.isCall())
810 // We can't verify call instructions as they can contain arbitrary implicit
811 // register and register mask operands.
812 return false;
813
814 // Gather all the expected implicit operands.
815 SmallVector<MachineOperand, 4> ImplicitOperands;
816 if (MCID.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000817 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000818 ImplicitOperands.push_back(
819 MachineOperand::CreateReg(*ImpDefs, true, true));
820 if (MCID.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000821 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000822 ImplicitOperands.push_back(
823 MachineOperand::CreateReg(*ImpUses, false, true));
824
825 const auto *TRI = MF.getSubtarget().getRegisterInfo();
826 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000827 for (const auto &I : ImplicitOperands) {
828 if (isImplicitOperandIn(I, Operands))
829 continue;
830 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000831 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000832 printImplicitRegisterFlag(I) + " %" +
833 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000834 }
835 return false;
836}
837
Alex Lorenze5a44662015-07-17 00:24:15 +0000838bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
839 if (Token.is(MIToken::kw_frame_setup)) {
840 Flags |= MachineInstr::FrameSetup;
841 lex();
842 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000843 if (Token.isNot(MIToken::Identifier))
844 return error("expected a machine instruction");
845 StringRef InstrName = Token.stringValue();
846 if (parseInstrName(InstrName, OpCode))
847 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000848 lex();
849 return false;
850}
851
Matthias Braun74ad41c2016-10-11 03:13:01 +0000852bool MIParser::parseNamedRegister(unsigned &Reg) {
853 assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
854 StringRef Name = Token.stringValue();
855 if (getRegisterByName(Name, Reg))
856 return error(Twine("unknown register name '") + Name + "'");
857 return false;
858}
859
860bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
861 assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
862 unsigned ID;
863 if (getUnsigned(ID))
864 return true;
865 Info = &PFS.getVRegInfo(ID);
866 return false;
867}
868
869bool MIParser::parseRegister(unsigned &Reg, VRegInfo *&Info) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000870 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000871 case MIToken::underscore:
872 Reg = 0;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000873 return false;
874 case MIToken::NamedRegister:
875 return parseNamedRegister(Reg);
876 case MIToken::VirtualRegister:
877 if (parseVirtualRegister(Info))
Alex Lorenz53464512015-07-10 22:51:20 +0000878 return true;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000879 Reg = Info->VReg;
880 return false;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000881 // TODO: Parse other register kinds.
882 default:
883 llvm_unreachable("The current token should be a register");
884 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000885}
886
Matthias Braunde5fea22017-01-18 00:59:19 +0000887bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) {
Ahmed Bougachabf480552017-01-20 00:29:59 +0000888 if (Token.isNot(MIToken::Identifier) && Token.isNot(MIToken::underscore))
889 return error("expected '_', register class, or register bank name");
Matthias Braunde5fea22017-01-18 00:59:19 +0000890 StringRef::iterator Loc = Token.location();
891 StringRef Name = Token.stringValue();
892
893 // Was it a register class?
894 auto RCNameI = PFS.Names2RegClasses.find(Name);
895 if (RCNameI != PFS.Names2RegClasses.end()) {
896 lex();
897 const TargetRegisterClass &RC = *RCNameI->getValue();
898
899 switch (RegInfo.Kind) {
900 case VRegInfo::UNKNOWN:
901 case VRegInfo::NORMAL:
902 RegInfo.Kind = VRegInfo::NORMAL;
903 if (RegInfo.Explicit && RegInfo.D.RC != &RC) {
904 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
905 return error(Loc, Twine("conflicting register classes, previously: ") +
906 Twine(TRI.getRegClassName(RegInfo.D.RC)));
907 }
908 RegInfo.D.RC = &RC;
909 RegInfo.Explicit = true;
910 return false;
911
912 case VRegInfo::GENERIC:
913 case VRegInfo::REGBANK:
914 return error(Loc, "register class specification on generic register");
915 }
916 llvm_unreachable("Unexpected register kind");
917 }
918
Ahmed Bougachabf480552017-01-20 00:29:59 +0000919 // Should be a register bank or a generic register.
920 const RegisterBank *RegBank = nullptr;
921 if (Name != "_") {
922 auto RBNameI = PFS.Names2RegBanks.find(Name);
923 if (RBNameI == PFS.Names2RegBanks.end())
924 return error(Loc, "expected '_', register class, or register bank name");
925 RegBank = RBNameI->getValue();
926 }
Matthias Braunde5fea22017-01-18 00:59:19 +0000927
Ahmed Bougachabf480552017-01-20 00:29:59 +0000928 lex();
929
Matthias Braunde5fea22017-01-18 00:59:19 +0000930 switch (RegInfo.Kind) {
931 case VRegInfo::UNKNOWN:
932 case VRegInfo::GENERIC:
933 case VRegInfo::REGBANK:
Ahmed Bougachabf480552017-01-20 00:29:59 +0000934 RegInfo.Kind = RegBank ? VRegInfo::REGBANK : VRegInfo::GENERIC;
935 if (RegInfo.Explicit && RegInfo.D.RegBank != RegBank)
936 return error(Loc, "conflicting generic register banks");
937 RegInfo.D.RegBank = RegBank;
Matthias Braunde5fea22017-01-18 00:59:19 +0000938 RegInfo.Explicit = true;
939 return false;
940
941 case VRegInfo::NORMAL:
Ahmed Bougachabf480552017-01-20 00:29:59 +0000942 return error(Loc, "register bank specification on normal register");
Matthias Braunde5fea22017-01-18 00:59:19 +0000943 }
944 llvm_unreachable("Unexpected register kind");
945}
946
Alex Lorenzcb268d42015-07-06 23:07:26 +0000947bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000948 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000949 switch (Token.kind()) {
950 case MIToken::kw_implicit:
951 Flags |= RegState::Implicit;
952 break;
953 case MIToken::kw_implicit_define:
954 Flags |= RegState::ImplicitDefine;
955 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000956 case MIToken::kw_def:
957 Flags |= RegState::Define;
958 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000959 case MIToken::kw_dead:
960 Flags |= RegState::Dead;
961 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000962 case MIToken::kw_killed:
963 Flags |= RegState::Kill;
964 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000965 case MIToken::kw_undef:
966 Flags |= RegState::Undef;
967 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000968 case MIToken::kw_internal:
969 Flags |= RegState::InternalRead;
970 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000971 case MIToken::kw_early_clobber:
972 Flags |= RegState::EarlyClobber;
973 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000974 case MIToken::kw_debug_use:
975 Flags |= RegState::Debug;
976 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000977 default:
978 llvm_unreachable("The current token should be a register flag");
979 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000980 if (OldFlags == Flags)
981 // We know that the same flag is specified more than once when the flags
982 // weren't modified.
983 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000984 lex();
985 return false;
986}
987
Alex Lorenz2eacca82015-07-13 23:24:34 +0000988bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
Matthias Braun333e4682016-07-26 21:49:34 +0000989 assert(Token.is(MIToken::dot));
Alex Lorenz2eacca82015-07-13 23:24:34 +0000990 lex();
991 if (Token.isNot(MIToken::Identifier))
Matthias Braun333e4682016-07-26 21:49:34 +0000992 return error("expected a subregister index after '.'");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000993 auto Name = Token.stringValue();
994 SubReg = getSubRegIndex(Name);
995 if (!SubReg)
996 return error(Twine("use of unknown subregister index '") + Name + "'");
997 lex();
998 return false;
999}
1000
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001001bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
1002 if (!consumeIfPresent(MIToken::kw_tied_def))
Tim Northoverd28d3cc2016-09-12 11:20:10 +00001003 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001004 if (Token.isNot(MIToken::IntegerLiteral))
1005 return error("expected an integer literal after 'tied-def'");
1006 if (getUnsigned(TiedDefIdx))
1007 return true;
1008 lex();
1009 if (expectAndConsume(MIToken::rparen))
1010 return true;
1011 return false;
1012}
1013
Alex Lorenzfeb6b432015-08-19 19:19:16 +00001014bool MIParser::assignRegisterTies(MachineInstr &MI,
1015 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001016 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
1017 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
1018 if (!Operands[I].TiedDefIdx)
1019 continue;
1020 // The parser ensures that this operand is a register use, so we just have
1021 // to check the tied-def operand.
1022 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
1023 if (DefIdx >= E)
1024 return error(Operands[I].Begin,
1025 Twine("use of invalid tied-def operand index '" +
1026 Twine(DefIdx) + "'; instruction has only ") +
1027 Twine(E) + " operands");
1028 const auto &DefOperand = Operands[DefIdx].Operand;
1029 if (!DefOperand.isReg() || !DefOperand.isDef())
1030 // FIXME: add note with the def operand.
1031 return error(Operands[I].Begin,
1032 Twine("use of invalid tied-def operand index '") +
1033 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
1034 " isn't a defined register");
1035 // Check that the tied-def operand wasn't tied elsewhere.
1036 for (const auto &TiedPair : TiedRegisterPairs) {
1037 if (TiedPair.first == DefIdx)
1038 return error(Operands[I].Begin,
1039 Twine("the tied-def operand #") + Twine(DefIdx) +
1040 " is already tied with another register operand");
1041 }
1042 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
1043 }
1044 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1045 // indices must be less than tied max.
1046 for (const auto &TiedPair : TiedRegisterPairs)
1047 MI.tieOperands(TiedPair.first, TiedPair.second);
1048 return false;
1049}
1050
1051bool MIParser::parseRegisterOperand(MachineOperand &Dest,
1052 Optional<unsigned> &TiedDefIdx,
1053 bool IsDef) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001054 unsigned Flags = IsDef ? RegState::Define : 0;
1055 while (Token.isRegisterFlag()) {
1056 if (parseRegisterFlag(Flags))
1057 return true;
1058 }
1059 if (!Token.isRegister())
1060 return error("expected a register after register flags");
Matthias Braun74ad41c2016-10-11 03:13:01 +00001061 unsigned Reg;
1062 VRegInfo *RegInfo;
1063 if (parseRegister(Reg, RegInfo))
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001064 return true;
1065 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +00001066 unsigned SubReg = 0;
Matthias Braun333e4682016-07-26 21:49:34 +00001067 if (Token.is(MIToken::dot)) {
Alex Lorenz2eacca82015-07-13 23:24:34 +00001068 if (parseSubRegisterIndex(SubReg))
1069 return true;
Matthias Braun5d00b322016-07-16 01:36:18 +00001070 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1071 return error("subregister index expects a virtual register");
Alex Lorenz2eacca82015-07-13 23:24:34 +00001072 }
Matthias Braunde5fea22017-01-18 00:59:19 +00001073 if (Token.is(MIToken::colon)) {
1074 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1075 return error("register class specification expects a virtual register");
1076 lex();
1077 if (parseRegisterClassOrBank(*RegInfo))
1078 return true;
1079 }
Tim Northoverd28d3cc2016-09-12 11:20:10 +00001080 MachineRegisterInfo &MRI = MF.getRegInfo();
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001081 if ((Flags & RegState::Define) == 0) {
1082 if (consumeIfPresent(MIToken::lparen)) {
1083 unsigned Idx;
Tim Northoverd28d3cc2016-09-12 11:20:10 +00001084 if (!parseRegisterTiedDefIndex(Idx))
1085 TiedDefIdx = Idx;
1086 else {
1087 // Try a redundant low-level type.
1088 LLT Ty;
1089 if (parseLowLevelType(Token.location(), Ty))
1090 return error("expected tied-def or low-level type after '('");
1091
1092 if (expectAndConsume(MIToken::rparen))
1093 return true;
1094
1095 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1096 return error("inconsistent type for generic virtual register");
1097
1098 MRI.setType(Reg, Ty);
1099 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001100 }
1101 } else if (consumeIfPresent(MIToken::lparen)) {
Quentin Colombete08cc592016-12-22 21:56:35 +00001102 // Virtual registers may have a tpe with GlobalISel.
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001103 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Quentin Colombete08cc592016-12-22 21:56:35 +00001104 return error("unexpected type on physical register");
Ahmed Bougacha5a59b242016-07-19 19:48:36 +00001105
Tim Northover0f140c72016-09-09 11:46:34 +00001106 LLT Ty;
1107 if (parseLowLevelType(Token.location(), Ty))
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001108 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001109
Tim Northover0f140c72016-09-09 11:46:34 +00001110 if (expectAndConsume(MIToken::rparen))
1111 return true;
1112
Tim Northoverd28d3cc2016-09-12 11:20:10 +00001113 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1114 return error("inconsistent type for generic virtual register");
1115
Tim Northover0f140c72016-09-09 11:46:34 +00001116 MRI.setType(Reg, Ty);
Matthias Braun74ad41c2016-10-11 03:13:01 +00001117 } else if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Quentin Colombet3749f332016-12-22 22:50:34 +00001118 // Generic virtual registers must have a type.
1119 // If we end up here this means the type hasn't been specified and
Quentin Colombet2c646962016-06-08 23:27:46 +00001120 // this is bad!
Matthias Braun74ad41c2016-10-11 03:13:01 +00001121 if (RegInfo->Kind == VRegInfo::GENERIC ||
1122 RegInfo->Kind == VRegInfo::REGBANK)
Quentin Colombet3749f332016-12-22 22:50:34 +00001123 return error("generic virtual registers must have a type");
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001124 }
Alex Lorenz495ad872015-07-08 21:23:34 +00001125 Dest = MachineOperand::CreateReg(
1126 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +00001127 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +00001128 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1129 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001130 return false;
1131}
1132
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001133bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1134 assert(Token.is(MIToken::IntegerLiteral));
1135 const APSInt &Int = Token.integerValue();
1136 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +00001137 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001138 Dest = MachineOperand::CreateImm(Int.getExtValue());
1139 lex();
1140 return false;
1141}
1142
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001143bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1144 const Constant *&C) {
1145 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001146 SMDiagnostic Err;
Malcolm Parsons06ac79c2016-11-02 16:43:50 +00001147 C = parseConstantValue(Source, Err, *MF.getFunction()->getParent(),
Matthias Braune35861d2016-07-13 23:27:50 +00001148 &PFS.IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001149 if (!C)
1150 return error(Loc + Err.getColumnNo(), Err.getMessage());
1151 return false;
1152}
1153
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001154bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1155 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1156 return true;
1157 lex();
1158 return false;
1159}
1160
Ahmed Bougachad760de02016-07-28 17:15:12 +00001161bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
Tim Northover32a078a2016-09-15 10:09:59 +00001162 if (Token.is(MIToken::ScalarType)) {
Tim Northover62ae5682016-07-20 19:09:30 +00001163 Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1164 lex();
1165 return false;
Tim Northoverbd505462016-07-22 16:59:52 +00001166 } else if (Token.is(MIToken::PointerType)) {
Tim Northover5ae83502016-09-15 09:20:34 +00001167 const DataLayout &DL = MF.getFunction()->getParent()->getDataLayout();
1168 unsigned AS = APSInt(Token.range().drop_front()).getZExtValue();
1169 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
Tim Northoverbd505462016-07-22 16:59:52 +00001170 lex();
1171 return false;
Tim Northover62ae5682016-07-20 19:09:30 +00001172 }
Quentin Colombet85199672016-03-08 00:20:48 +00001173
Tim Northover62ae5682016-07-20 19:09:30 +00001174 // Now we're looking for a vector.
1175 if (Token.isNot(MIToken::less))
Tim Northoverbd505462016-07-22 16:59:52 +00001176 return error(Loc,
1177 "expected unsized, pN, sN or <N x sM> for GlobalISel type");
1178
Tim Northover62ae5682016-07-20 19:09:30 +00001179 lex();
1180
1181 if (Token.isNot(MIToken::IntegerLiteral))
1182 return error(Loc, "expected <N x sM> for vctor type");
1183 uint64_t NumElements = Token.integerValue().getZExtValue();
1184 lex();
1185
1186 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1187 return error(Loc, "expected '<N x sM>' for vector type");
1188 lex();
1189
1190 if (Token.isNot(MIToken::ScalarType))
1191 return error(Loc, "expected '<N x sM>' for vector type");
1192 uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1193 lex();
1194
1195 if (Token.isNot(MIToken::greater))
1196 return error(Loc, "expected '<N x sM>' for vector type");
1197 lex();
1198
1199 Ty = LLT::vector(NumElements, ScalarSize);
Quentin Colombet85199672016-03-08 00:20:48 +00001200 return false;
1201}
1202
Alex Lorenz05e38822015-08-05 18:52:21 +00001203bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1204 assert(Token.is(MIToken::IntegerType));
1205 auto Loc = Token.location();
1206 lex();
1207 if (Token.isNot(MIToken::IntegerLiteral))
1208 return error("expected an integer literal");
1209 const Constant *C = nullptr;
1210 if (parseIRConstant(Loc, C))
1211 return true;
1212 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1213 return false;
1214}
1215
Alex Lorenzad156fb2015-07-31 20:49:21 +00001216bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1217 auto Loc = Token.location();
1218 lex();
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +00001219 if (Token.isNot(MIToken::FloatingPointLiteral) &&
1220 Token.isNot(MIToken::HexLiteral))
Alex Lorenzad156fb2015-07-31 20:49:21 +00001221 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001222 const Constant *C = nullptr;
1223 if (parseIRConstant(Loc, C))
1224 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001225 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1226 return false;
1227}
1228
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001229bool MIParser::getUnsigned(unsigned &Result) {
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +00001230 if (Token.hasIntegerValue()) {
1231 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1232 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1233 if (Val64 == Limit)
1234 return error("expected 32-bit integer (too large)");
1235 Result = Val64;
1236 return false;
1237 }
1238 if (Token.is(MIToken::HexLiteral)) {
Krzysztof Parzyszek36ef5dc2016-12-16 13:58:01 +00001239 APInt A;
1240 if (getHexUint(A))
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +00001241 return true;
Krzysztof Parzyszek36ef5dc2016-12-16 13:58:01 +00001242 if (A.getBitWidth() > 32)
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +00001243 return error("expected 32-bit integer (too large)");
1244 Result = A.getZExtValue();
1245 return false;
1246 }
1247 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001248}
1249
Alex Lorenzf09df002015-06-30 18:16:42 +00001250bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001251 assert(Token.is(MIToken::MachineBasicBlock) ||
1252 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001253 unsigned Number;
1254 if (getUnsigned(Number))
1255 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001256 auto MBBInfo = PFS.MBBSlots.find(Number);
1257 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001258 return error(Twine("use of undefined machine basic block #") +
1259 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001260 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001261 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1262 return error(Twine("the name of machine basic block #") + Twine(Number) +
1263 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001264 return false;
1265}
1266
1267bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1268 MachineBasicBlock *MBB;
1269 if (parseMBBReference(MBB))
1270 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001271 Dest = MachineOperand::CreateMBB(MBB);
1272 lex();
1273 return false;
1274}
1275
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001276bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001277 assert(Token.is(MIToken::StackObject));
1278 unsigned ID;
1279 if (getUnsigned(ID))
1280 return true;
1281 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1282 if (ObjectInfo == PFS.StackObjectSlots.end())
1283 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1284 "'");
1285 StringRef Name;
1286 if (const auto *Alloca =
Matthias Braun941a7052016-07-28 18:40:00 +00001287 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001288 Name = Alloca->getName();
1289 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1290 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1291 "' isn't '" + Token.stringValue() + "'");
1292 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001293 FI = ObjectInfo->second;
1294 return false;
1295}
1296
1297bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1298 int FI;
1299 if (parseStackFrameIndex(FI))
1300 return true;
1301 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001302 return false;
1303}
1304
Alex Lorenzea882122015-08-12 21:17:02 +00001305bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001306 assert(Token.is(MIToken::FixedStackObject));
1307 unsigned ID;
1308 if (getUnsigned(ID))
1309 return true;
1310 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1311 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1312 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1313 Twine(ID) + "'");
1314 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001315 FI = ObjectInfo->second;
1316 return false;
1317}
1318
1319bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1320 int FI;
1321 if (parseFixedStackFrameIndex(FI))
1322 return true;
1323 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001324 return false;
1325}
1326
Alex Lorenz41df7d32015-07-28 17:09:52 +00001327bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001328 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001329 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001330 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001331 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001332 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001333 return error(Twine("use of undefined global value '") + Token.range() +
1334 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001335 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001336 }
1337 case MIToken::GlobalValue: {
1338 unsigned GVIdx;
1339 if (getUnsigned(GVIdx))
1340 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001341 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001342 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1343 "'");
Matthias Braune35861d2016-07-13 23:27:50 +00001344 GV = PFS.IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001345 break;
1346 }
1347 default:
1348 llvm_unreachable("The current token should be a global value");
1349 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001350 return false;
1351}
1352
1353bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1354 GlobalValue *GV = nullptr;
1355 if (parseGlobalValue(GV))
1356 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001357 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001358 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001359 if (parseOperandsOffset(Dest))
1360 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001361 return false;
1362}
1363
Alex Lorenzab980492015-07-20 20:51:18 +00001364bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1365 assert(Token.is(MIToken::ConstantPoolItem));
1366 unsigned ID;
1367 if (getUnsigned(ID))
1368 return true;
1369 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1370 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1371 return error("use of undefined constant '%const." + Twine(ID) + "'");
1372 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001373 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001374 if (parseOperandsOffset(Dest))
1375 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001376 return false;
1377}
1378
Alex Lorenz31d70682015-07-15 23:38:35 +00001379bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1380 assert(Token.is(MIToken::JumpTableIndex));
1381 unsigned ID;
1382 if (getUnsigned(ID))
1383 return true;
1384 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1385 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1386 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1387 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001388 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1389 return false;
1390}
1391
Alex Lorenz6ede3742015-07-21 16:59:53 +00001392bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001393 assert(Token.is(MIToken::ExternalSymbol));
1394 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001395 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001396 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001397 if (parseOperandsOffset(Dest))
1398 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001399 return false;
1400}
1401
Matthias Braunb74eb412016-03-28 18:18:46 +00001402bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1403 assert(Token.is(MIToken::SubRegisterIndex));
1404 StringRef Name = Token.stringValue();
1405 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1406 if (SubRegIndex == 0)
1407 return error(Twine("unknown subregister index '") + Name + "'");
1408 lex();
1409 Dest = MachineOperand::CreateImm(SubRegIndex);
1410 return false;
1411}
1412
Alex Lorenz44f29252015-07-22 21:07:04 +00001413bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001414 assert(Token.is(MIToken::exclaim));
1415 auto Loc = Token.location();
1416 lex();
1417 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1418 return error("expected metadata id after '!'");
1419 unsigned ID;
1420 if (getUnsigned(ID))
1421 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001422 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1423 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
Alex Lorenz35e44462015-07-22 17:58:46 +00001424 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1425 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001426 Node = NodeInfo->second.get();
1427 return false;
1428}
1429
1430bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1431 MDNode *Node = nullptr;
1432 if (parseMDNode(Node))
1433 return true;
1434 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001435 return false;
1436}
1437
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001438bool MIParser::parseCFIOffset(int &Offset) {
1439 if (Token.isNot(MIToken::IntegerLiteral))
1440 return error("expected a cfi offset");
1441 if (Token.integerValue().getMinSignedBits() > 32)
1442 return error("expected a 32 bit integer (the cfi offset is too large)");
1443 Offset = (int)Token.integerValue().getExtValue();
1444 lex();
1445 return false;
1446}
1447
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001448bool MIParser::parseCFIRegister(unsigned &Reg) {
1449 if (Token.isNot(MIToken::NamedRegister))
1450 return error("expected a cfi register");
1451 unsigned LLVMReg;
Matthias Braun74ad41c2016-10-11 03:13:01 +00001452 if (parseNamedRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001453 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001454 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1455 assert(TRI && "Expected target register info");
1456 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1457 if (DwarfReg < 0)
1458 return error("invalid DWARF register");
1459 Reg = (unsigned)DwarfReg;
1460 lex();
1461 return false;
1462}
1463
1464bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1465 auto Kind = Token.kind();
1466 lex();
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001467 int Offset;
1468 unsigned Reg;
1469 unsigned CFIIndex;
1470 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001471 case MIToken::kw_cfi_same_value:
1472 if (parseCFIRegister(Reg))
1473 return true;
Matthias Braunf23ef432016-11-30 23:48:42 +00001474 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
Alex Lorenz577d2712015-08-14 21:55:58 +00001475 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001476 case MIToken::kw_cfi_offset:
1477 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1478 parseCFIOffset(Offset))
1479 return true;
1480 CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +00001481 MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001482 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001483 case MIToken::kw_cfi_def_cfa_register:
1484 if (parseCFIRegister(Reg))
1485 return true;
1486 CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +00001487 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001488 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001489 case MIToken::kw_cfi_def_cfa_offset:
1490 if (parseCFIOffset(Offset))
1491 return true;
1492 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
Matthias Braunf23ef432016-11-30 23:48:42 +00001493 CFIIndex = MF.addFrameInst(
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001494 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1495 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001496 case MIToken::kw_cfi_def_cfa:
1497 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1498 parseCFIOffset(Offset))
1499 return true;
1500 // NB: MCCFIInstruction::createDefCfa negates the offset.
1501 CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +00001502 MF.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
Alex Lorenzb1393232015-07-29 18:57:23 +00001503 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001504 default:
1505 // TODO: Parse the other CFI operands.
1506 llvm_unreachable("The current token should be a cfi operand");
1507 }
1508 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001509 return false;
1510}
1511
Alex Lorenzdeb53492015-07-28 17:28:03 +00001512bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1513 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001514 case MIToken::NamedIRBlock: {
1515 BB = dyn_cast_or_null<BasicBlock>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +00001516 F.getValueSymbolTable()->lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001517 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001518 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001519 break;
1520 }
1521 case MIToken::IRBlock: {
1522 unsigned SlotNumber = 0;
1523 if (getUnsigned(SlotNumber))
1524 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001525 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001526 if (!BB)
1527 return error(Twine("use of undefined IR block '%ir-block.") +
1528 Twine(SlotNumber) + "'");
1529 break;
1530 }
1531 default:
1532 llvm_unreachable("The current token should be an IR block reference");
1533 }
1534 return false;
1535}
1536
1537bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1538 assert(Token.is(MIToken::kw_blockaddress));
1539 lex();
1540 if (expectAndConsume(MIToken::lparen))
1541 return true;
1542 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001543 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001544 return error("expected a global value");
1545 GlobalValue *GV = nullptr;
1546 if (parseGlobalValue(GV))
1547 return true;
1548 auto *F = dyn_cast<Function>(GV);
1549 if (!F)
1550 return error("expected an IR function reference");
1551 lex();
1552 if (expectAndConsume(MIToken::comma))
1553 return true;
1554 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001555 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001556 return error("expected an IR block reference");
1557 if (parseIRBlock(BB, *F))
1558 return true;
1559 lex();
1560 if (expectAndConsume(MIToken::rparen))
1561 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001562 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001563 if (parseOperandsOffset(Dest))
1564 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001565 return false;
1566}
1567
Tim Northover6b3bd612016-07-29 20:32:59 +00001568bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
1569 assert(Token.is(MIToken::kw_intrinsic));
1570 lex();
1571 if (expectAndConsume(MIToken::lparen))
1572 return error("expected syntax intrinsic(@llvm.whatever)");
1573
1574 if (Token.isNot(MIToken::NamedGlobalValue))
1575 return error("expected syntax intrinsic(@llvm.whatever)");
1576
1577 std::string Name = Token.stringValue();
1578 lex();
1579
1580 if (expectAndConsume(MIToken::rparen))
1581 return error("expected ')' to terminate intrinsic name");
1582
1583 // Find out what intrinsic we're dealing with, first try the global namespace
1584 // and then the target's private intrinsics if that fails.
1585 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1586 Intrinsic::ID ID = Function::lookupIntrinsicID(Name);
1587 if (ID == Intrinsic::not_intrinsic && TII)
1588 ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
1589
1590 if (ID == Intrinsic::not_intrinsic)
1591 return error("unknown intrinsic name");
1592 Dest = MachineOperand::CreateIntrinsicID(ID);
1593
1594 return false;
1595}
1596
Tim Northoverde3aea0412016-08-17 20:25:25 +00001597bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
1598 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
1599 bool IsFloat = Token.is(MIToken::kw_floatpred);
1600 lex();
1601
1602 if (expectAndConsume(MIToken::lparen))
1603 return error("expected syntax intpred(whatever) or floatpred(whatever");
1604
1605 if (Token.isNot(MIToken::Identifier))
1606 return error("whatever");
1607
1608 CmpInst::Predicate Pred;
1609 if (IsFloat) {
1610 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1611 .Case("false", CmpInst::FCMP_FALSE)
1612 .Case("oeq", CmpInst::FCMP_OEQ)
1613 .Case("ogt", CmpInst::FCMP_OGT)
1614 .Case("oge", CmpInst::FCMP_OGE)
1615 .Case("olt", CmpInst::FCMP_OLT)
1616 .Case("ole", CmpInst::FCMP_OLE)
1617 .Case("one", CmpInst::FCMP_ONE)
1618 .Case("ord", CmpInst::FCMP_ORD)
1619 .Case("uno", CmpInst::FCMP_UNO)
1620 .Case("ueq", CmpInst::FCMP_UEQ)
1621 .Case("ugt", CmpInst::FCMP_UGT)
1622 .Case("uge", CmpInst::FCMP_UGE)
1623 .Case("ult", CmpInst::FCMP_ULT)
1624 .Case("ule", CmpInst::FCMP_ULE)
1625 .Case("une", CmpInst::FCMP_UNE)
1626 .Case("true", CmpInst::FCMP_TRUE)
1627 .Default(CmpInst::BAD_FCMP_PREDICATE);
1628 if (!CmpInst::isFPPredicate(Pred))
1629 return error("invalid floating-point predicate");
1630 } else {
1631 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1632 .Case("eq", CmpInst::ICMP_EQ)
1633 .Case("ne", CmpInst::ICMP_NE)
1634 .Case("sgt", CmpInst::ICMP_SGT)
1635 .Case("sge", CmpInst::ICMP_SGE)
1636 .Case("slt", CmpInst::ICMP_SLT)
1637 .Case("sle", CmpInst::ICMP_SLE)
1638 .Case("ugt", CmpInst::ICMP_UGT)
1639 .Case("uge", CmpInst::ICMP_UGE)
1640 .Case("ult", CmpInst::ICMP_ULT)
1641 .Case("ule", CmpInst::ICMP_ULE)
1642 .Default(CmpInst::BAD_ICMP_PREDICATE);
1643 if (!CmpInst::isIntPredicate(Pred))
1644 return error("invalid integer predicate");
1645 }
1646
1647 lex();
1648 Dest = MachineOperand::CreatePredicate(Pred);
Tim Northover6cd4b232016-08-23 21:01:26 +00001649 if (expectAndConsume(MIToken::rparen))
Tim Northoverde3aea0412016-08-17 20:25:25 +00001650 return error("predicate should be terminated by ')'.");
1651
1652 return false;
1653}
1654
Alex Lorenzef5c1962015-07-28 23:02:45 +00001655bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1656 assert(Token.is(MIToken::kw_target_index));
1657 lex();
1658 if (expectAndConsume(MIToken::lparen))
1659 return true;
1660 if (Token.isNot(MIToken::Identifier))
1661 return error("expected the name of the target index");
1662 int Index = 0;
1663 if (getTargetIndex(Token.stringValue(), Index))
1664 return error("use of undefined target index '" + Token.stringValue() + "'");
1665 lex();
1666 if (expectAndConsume(MIToken::rparen))
1667 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001668 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001669 if (parseOperandsOffset(Dest))
1670 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001671 return false;
1672}
1673
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +00001674bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
1675 assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
1676 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1677 assert(TRI && "Expected target register info");
1678 lex();
1679 if (expectAndConsume(MIToken::lparen))
1680 return true;
1681
1682 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1683 while (true) {
1684 if (Token.isNot(MIToken::NamedRegister))
1685 return error("expected a named register");
1686 unsigned Reg;
1687 if (parseNamedRegister(Reg))
1688 return true;
1689 lex();
1690 Mask[Reg / 32] |= 1U << (Reg % 32);
1691 // TODO: Report an error if the same register is used more than once.
1692 if (Token.isNot(MIToken::comma))
1693 break;
1694 lex();
1695 }
1696
1697 if (expectAndConsume(MIToken::rparen))
1698 return true;
1699 Dest = MachineOperand::CreateRegMask(Mask);
1700 return false;
1701}
1702
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001703bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1704 assert(Token.is(MIToken::kw_liveout));
1705 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1706 assert(TRI && "Expected target register info");
1707 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1708 lex();
1709 if (expectAndConsume(MIToken::lparen))
1710 return true;
1711 while (true) {
1712 if (Token.isNot(MIToken::NamedRegister))
1713 return error("expected a named register");
Matthias Braun74ad41c2016-10-11 03:13:01 +00001714 unsigned Reg;
1715 if (parseNamedRegister(Reg))
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001716 return true;
1717 lex();
1718 Mask[Reg / 32] |= 1U << (Reg % 32);
1719 // TODO: Report an error if the same register is used more than once.
1720 if (Token.isNot(MIToken::comma))
1721 break;
1722 lex();
1723 }
1724 if (expectAndConsume(MIToken::rparen))
1725 return true;
1726 Dest = MachineOperand::CreateRegLiveOut(Mask);
1727 return false;
1728}
1729
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001730bool MIParser::parseMachineOperand(MachineOperand &Dest,
1731 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001732 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001733 case MIToken::kw_implicit:
1734 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001735 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001736 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001737 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001738 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001739 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001740 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001741 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001742 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001743 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001744 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001745 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001746 case MIToken::IntegerLiteral:
1747 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001748 case MIToken::IntegerType:
1749 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001750 case MIToken::kw_half:
1751 case MIToken::kw_float:
1752 case MIToken::kw_double:
1753 case MIToken::kw_x86_fp80:
1754 case MIToken::kw_fp128:
1755 case MIToken::kw_ppc_fp128:
1756 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001757 case MIToken::MachineBasicBlock:
1758 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001759 case MIToken::StackObject:
1760 return parseStackObjectOperand(Dest);
1761 case MIToken::FixedStackObject:
1762 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001763 case MIToken::GlobalValue:
1764 case MIToken::NamedGlobalValue:
1765 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001766 case MIToken::ConstantPoolItem:
1767 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001768 case MIToken::JumpTableIndex:
1769 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001770 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001771 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00001772 case MIToken::SubRegisterIndex:
1773 return parseSubRegisterIndexOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001774 case MIToken::exclaim:
1775 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001776 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001777 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001778 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001779 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001780 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001781 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001782 case MIToken::kw_blockaddress:
1783 return parseBlockAddressOperand(Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +00001784 case MIToken::kw_intrinsic:
1785 return parseIntrinsicOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001786 case MIToken::kw_target_index:
1787 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001788 case MIToken::kw_liveout:
1789 return parseLiveoutRegisterMaskOperand(Dest);
Tim Northoverde3aea0412016-08-17 20:25:25 +00001790 case MIToken::kw_floatpred:
1791 case MIToken::kw_intpred:
1792 return parsePredicateOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001793 case MIToken::Error:
1794 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001795 case MIToken::Identifier:
1796 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1797 Dest = MachineOperand::CreateRegMask(RegMask);
1798 lex();
1799 break;
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +00001800 } else
1801 return parseCustomRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001802 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001803 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001804 return error("expected a machine operand");
1805 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001806 return false;
1807}
1808
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001809bool MIParser::parseMachineOperandAndTargetFlags(
1810 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001811 unsigned TF = 0;
1812 bool HasTargetFlags = false;
1813 if (Token.is(MIToken::kw_target_flags)) {
1814 HasTargetFlags = true;
1815 lex();
1816 if (expectAndConsume(MIToken::lparen))
1817 return true;
1818 if (Token.isNot(MIToken::Identifier))
1819 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001820 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1821 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1822 return error("use of undefined target flag '" + Token.stringValue() +
1823 "'");
1824 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001825 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001826 while (Token.is(MIToken::comma)) {
1827 lex();
1828 if (Token.isNot(MIToken::Identifier))
1829 return error("expected the name of the target flag");
1830 unsigned BitFlag = 0;
1831 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1832 return error("use of undefined target flag '" + Token.stringValue() +
1833 "'");
1834 // TODO: Report an error when using a duplicate bit target flag.
1835 TF |= BitFlag;
1836 lex();
1837 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001838 if (expectAndConsume(MIToken::rparen))
1839 return true;
1840 }
1841 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001842 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001843 return true;
1844 if (!HasTargetFlags)
1845 return false;
1846 if (Dest.isReg())
1847 return error(Loc, "register operands can't have target flags");
1848 Dest.setTargetFlags(TF);
1849 return false;
1850}
1851
Alex Lorenzdc24c172015-08-07 20:21:00 +00001852bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001853 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1854 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001855 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001856 bool IsNegative = Token.is(MIToken::minus);
1857 lex();
1858 if (Token.isNot(MIToken::IntegerLiteral))
1859 return error("expected an integer literal after '" + Sign + "'");
1860 if (Token.integerValue().getMinSignedBits() > 64)
1861 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001862 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001863 if (IsNegative)
1864 Offset = -Offset;
1865 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001866 return false;
1867}
1868
Alex Lorenz620f8912015-08-13 20:33:33 +00001869bool MIParser::parseAlignment(unsigned &Alignment) {
1870 assert(Token.is(MIToken::kw_align));
1871 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001872 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001873 return error("expected an integer literal after 'align'");
1874 if (getUnsigned(Alignment))
1875 return true;
1876 lex();
1877 return false;
1878}
1879
Alex Lorenzdc24c172015-08-07 20:21:00 +00001880bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1881 int64_t Offset = 0;
1882 if (parseOffset(Offset))
1883 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001884 Op.setOffset(Offset);
1885 return false;
1886}
1887
Alex Lorenz36593ac2015-08-19 23:27:07 +00001888bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001889 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001890 case MIToken::NamedIRValue: {
Mehdi Aminia53d49e2016-09-17 06:00:02 +00001891 V = MF.getFunction()->getValueSymbolTable()->lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001892 break;
1893 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001894 case MIToken::IRValue: {
1895 unsigned SlotNumber = 0;
1896 if (getUnsigned(SlotNumber))
1897 return true;
1898 V = getIRValue(SlotNumber);
1899 break;
1900 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001901 case MIToken::NamedGlobalValue:
1902 case MIToken::GlobalValue: {
1903 GlobalValue *GV = nullptr;
1904 if (parseGlobalValue(GV))
1905 return true;
1906 V = GV;
1907 break;
1908 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001909 case MIToken::QuotedIRValue: {
1910 const Constant *C = nullptr;
1911 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1912 return true;
1913 V = C;
1914 break;
1915 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001916 default:
1917 llvm_unreachable("The current token should be an IR block reference");
1918 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001919 if (!V)
1920 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001921 return false;
1922}
1923
1924bool MIParser::getUint64(uint64_t &Result) {
Krzysztof Parzyszek36ef5dc2016-12-16 13:58:01 +00001925 if (Token.hasIntegerValue()) {
1926 if (Token.integerValue().getActiveBits() > 64)
1927 return error("expected 64-bit integer (too large)");
1928 Result = Token.integerValue().getZExtValue();
1929 return false;
1930 }
1931 if (Token.is(MIToken::HexLiteral)) {
1932 APInt A;
1933 if (getHexUint(A))
1934 return true;
1935 if (A.getBitWidth() > 64)
1936 return error("expected 64-bit integer (too large)");
1937 Result = A.getZExtValue();
1938 return false;
1939 }
1940 return true;
1941}
1942
1943bool MIParser::getHexUint(APInt &Result) {
1944 assert(Token.is(MIToken::HexLiteral));
1945 StringRef S = Token.range();
1946 assert(S[0] == '0' && tolower(S[1]) == 'x');
1947 // This could be a floating point literal with a special prefix.
1948 if (!isxdigit(S[2]))
1949 return true;
1950 StringRef V = S.substr(2);
1951 APInt A(V.size()*4, V, 16);
Renato Golin4abfb3d2017-04-23 12:15:30 +00001952 Result = APInt(A.getActiveBits(),
1953 ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
Alex Lorenz4af7e612015-08-03 23:08:19 +00001954 return false;
1955}
1956
Justin Lebar0af80cd2016-07-15 18:26:59 +00001957bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
1958 const auto OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001959 switch (Token.kind()) {
1960 case MIToken::kw_volatile:
1961 Flags |= MachineMemOperand::MOVolatile;
1962 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001963 case MIToken::kw_non_temporal:
1964 Flags |= MachineMemOperand::MONonTemporal;
1965 break;
Justin Lebaradbf09e2016-09-11 01:38:58 +00001966 case MIToken::kw_dereferenceable:
1967 Flags |= MachineMemOperand::MODereferenceable;
1968 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001969 case MIToken::kw_invariant:
1970 Flags |= MachineMemOperand::MOInvariant;
1971 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001972 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001973 default:
1974 llvm_unreachable("The current token should be a memory operand flag");
1975 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001976 if (OldFlags == Flags)
1977 // We know that the same flag is specified more than once when the flags
1978 // weren't modified.
1979 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001980 lex();
1981 return false;
1982}
1983
Alex Lorenz91097a32015-08-12 20:33:26 +00001984bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1985 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001986 case MIToken::kw_stack:
1987 PSV = MF.getPSVManager().getStack();
1988 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001989 case MIToken::kw_got:
1990 PSV = MF.getPSVManager().getGOT();
1991 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001992 case MIToken::kw_jump_table:
1993 PSV = MF.getPSVManager().getJumpTable();
1994 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001995 case MIToken::kw_constant_pool:
1996 PSV = MF.getPSVManager().getConstantPool();
1997 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001998 case MIToken::FixedStackObject: {
1999 int FI;
2000 if (parseFixedStackFrameIndex(FI))
2001 return true;
2002 PSV = MF.getPSVManager().getFixedStack(FI);
2003 // The token was already consumed, so use return here instead of break.
2004 return false;
2005 }
Matthias Braun3ef7df92016-06-08 00:47:07 +00002006 case MIToken::StackObject: {
2007 int FI;
2008 if (parseStackFrameIndex(FI))
2009 return true;
2010 PSV = MF.getPSVManager().getFixedStack(FI);
2011 // The token was already consumed, so use return here instead of break.
2012 return false;
2013 }
Alex Lorenz0d009642015-08-20 00:12:57 +00002014 case MIToken::kw_call_entry: {
2015 lex();
2016 switch (Token.kind()) {
2017 case MIToken::GlobalValue:
2018 case MIToken::NamedGlobalValue: {
2019 GlobalValue *GV = nullptr;
2020 if (parseGlobalValue(GV))
2021 return true;
2022 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
2023 break;
2024 }
2025 case MIToken::ExternalSymbol:
2026 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
2027 MF.createExternalSymbolName(Token.stringValue()));
2028 break;
2029 default:
2030 return error(
2031 "expected a global value or an external symbol after 'call-entry'");
2032 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00002033 break;
2034 }
Alex Lorenz91097a32015-08-12 20:33:26 +00002035 default:
2036 llvm_unreachable("The current token should be pseudo source value");
2037 }
2038 lex();
2039 return false;
2040}
2041
2042bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00002043 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00002044 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Matthias Braun3ef7df92016-06-08 00:47:07 +00002045 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
2046 Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00002047 const PseudoSourceValue *PSV = nullptr;
2048 if (parseMemoryPseudoSourceValue(PSV))
2049 return true;
2050 int64_t Offset = 0;
2051 if (parseOffset(Offset))
2052 return true;
2053 Dest = MachinePointerInfo(PSV, Offset);
2054 return false;
2055 }
Alex Lorenz36efd382015-08-20 00:20:03 +00002056 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
2057 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00002058 Token.isNot(MIToken::NamedGlobalValue) &&
2059 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00002060 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00002061 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00002062 if (parseIRValue(V))
2063 return true;
2064 if (!V->getType()->isPointerTy())
2065 return error("expected a pointer IR value");
2066 lex();
2067 int64_t Offset = 0;
2068 if (parseOffset(Offset))
2069 return true;
2070 Dest = MachinePointerInfo(V, Offset);
2071 return false;
2072}
2073
Tim Northoverb73e3092017-02-13 22:14:08 +00002074bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
2075 Order = AtomicOrdering::NotAtomic;
2076 if (Token.isNot(MIToken::Identifier))
2077 return false;
2078
2079 Order = StringSwitch<AtomicOrdering>(Token.stringValue())
2080 .Case("unordered", AtomicOrdering::Unordered)
2081 .Case("monotonic", AtomicOrdering::Monotonic)
2082 .Case("acquire", AtomicOrdering::Acquire)
2083 .Case("release", AtomicOrdering::Release)
2084 .Case("acq_rel", AtomicOrdering::AcquireRelease)
2085 .Case("seq_cst", AtomicOrdering::SequentiallyConsistent)
2086 .Default(AtomicOrdering::NotAtomic);
2087
2088 if (Order != AtomicOrdering::NotAtomic) {
2089 lex();
2090 return false;
2091 }
2092
2093 return error("expected an atomic scope, ordering or a size integer literal");
2094}
2095
Alex Lorenz4af7e612015-08-03 23:08:19 +00002096bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
2097 if (expectAndConsume(MIToken::lparen))
2098 return true;
Justin Lebar0af80cd2016-07-15 18:26:59 +00002099 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
Alex Lorenza518b792015-08-04 00:24:45 +00002100 while (Token.isMemoryOperandFlag()) {
2101 if (parseMemoryOperandFlag(Flags))
2102 return true;
2103 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00002104 if (Token.isNot(MIToken::Identifier) ||
2105 (Token.stringValue() != "load" && Token.stringValue() != "store"))
2106 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00002107 if (Token.stringValue() == "load")
2108 Flags |= MachineMemOperand::MOLoad;
2109 else
2110 Flags |= MachineMemOperand::MOStore;
2111 lex();
2112
Tim Northoverb73e3092017-02-13 22:14:08 +00002113 // Optional "singlethread" scope.
2114 SynchronizationScope Scope = SynchronizationScope::CrossThread;
2115 if (Token.is(MIToken::Identifier) && Token.stringValue() == "singlethread") {
2116 Scope = SynchronizationScope::SingleThread;
2117 lex();
2118 }
2119
2120 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
2121 AtomicOrdering Order, FailureOrder;
2122 if (parseOptionalAtomicOrdering(Order))
2123 return true;
2124
2125 if (parseOptionalAtomicOrdering(FailureOrder))
2126 return true;
2127
Alex Lorenz4af7e612015-08-03 23:08:19 +00002128 if (Token.isNot(MIToken::IntegerLiteral))
2129 return error("expected the size integer literal after memory operation");
2130 uint64_t Size;
2131 if (getUint64(Size))
2132 return true;
2133 lex();
2134
Alex Lorenz91097a32015-08-12 20:33:26 +00002135 MachinePointerInfo Ptr = MachinePointerInfo();
Matthias Braunc25c9cc2016-06-04 00:06:31 +00002136 if (Token.is(MIToken::Identifier)) {
2137 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
2138 if (Token.stringValue() != Word)
2139 return error(Twine("expected '") + Word + "'");
2140 lex();
2141
2142 if (parseMachinePointerInfo(Ptr))
2143 return true;
2144 }
Alex Lorenz61420f72015-08-07 20:48:30 +00002145 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00002146 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00002147 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00002148 while (consumeIfPresent(MIToken::comma)) {
2149 switch (Token.kind()) {
2150 case MIToken::kw_align:
2151 if (parseAlignment(BaseAlignment))
2152 return true;
2153 break;
2154 case MIToken::md_tbaa:
2155 lex();
2156 if (parseMDNode(AAInfo.TBAA))
2157 return true;
2158 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00002159 case MIToken::md_alias_scope:
2160 lex();
2161 if (parseMDNode(AAInfo.Scope))
2162 return true;
2163 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00002164 case MIToken::md_noalias:
2165 lex();
2166 if (parseMDNode(AAInfo.NoAlias))
2167 return true;
2168 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00002169 case MIToken::md_range:
2170 lex();
2171 if (parseMDNode(Range))
2172 return true;
2173 break;
Alex Lorenza617c912015-08-17 22:05:15 +00002174 // TODO: Report an error on duplicate metadata nodes.
2175 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00002176 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
2177 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00002178 }
Alex Lorenz61420f72015-08-07 20:48:30 +00002179 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00002180 if (expectAndConsume(MIToken::rparen))
2181 return true;
Tim Northoverb73e3092017-02-13 22:14:08 +00002182 Dest = MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range,
2183 Scope, Order, FailureOrder);
Alex Lorenz4af7e612015-08-03 23:08:19 +00002184 return false;
2185}
2186
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002187void MIParser::initNames2InstrOpCodes() {
2188 if (!Names2InstrOpCodes.empty())
2189 return;
2190 const auto *TII = MF.getSubtarget().getInstrInfo();
2191 assert(TII && "Expected target instruction info");
2192 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
2193 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
2194}
2195
2196bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
2197 initNames2InstrOpCodes();
2198 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
2199 if (InstrInfo == Names2InstrOpCodes.end())
2200 return true;
2201 OpCode = InstrInfo->getValue();
2202 return false;
2203}
2204
Alex Lorenzf3db51de2015-06-23 16:35:26 +00002205void MIParser::initNames2Regs() {
2206 if (!Names2Regs.empty())
2207 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00002208 // The '%noreg' register is the register 0.
2209 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00002210 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2211 assert(TRI && "Expected target register info");
2212 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
2213 bool WasInserted =
2214 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
2215 .second;
2216 (void)WasInserted;
2217 assert(WasInserted && "Expected registers to be unique case-insensitively");
2218 }
2219}
2220
2221bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
2222 initNames2Regs();
2223 auto RegInfo = Names2Regs.find(RegName);
2224 if (RegInfo == Names2Regs.end())
2225 return true;
2226 Reg = RegInfo->getValue();
2227 return false;
2228}
2229
Alex Lorenz8f6f4282015-06-29 16:57:06 +00002230void MIParser::initNames2RegMasks() {
2231 if (!Names2RegMasks.empty())
2232 return;
2233 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2234 assert(TRI && "Expected target register info");
2235 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
2236 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
2237 assert(RegMasks.size() == RegMaskNames.size());
2238 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
2239 Names2RegMasks.insert(
2240 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
2241}
2242
2243const uint32_t *MIParser::getRegMask(StringRef Identifier) {
2244 initNames2RegMasks();
2245 auto RegMaskInfo = Names2RegMasks.find(Identifier);
2246 if (RegMaskInfo == Names2RegMasks.end())
2247 return nullptr;
2248 return RegMaskInfo->getValue();
2249}
2250
Alex Lorenz2eacca82015-07-13 23:24:34 +00002251void MIParser::initNames2SubRegIndices() {
2252 if (!Names2SubRegIndices.empty())
2253 return;
2254 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2255 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
2256 Names2SubRegIndices.insert(
2257 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
2258}
2259
2260unsigned MIParser::getSubRegIndex(StringRef Name) {
2261 initNames2SubRegIndices();
2262 auto SubRegInfo = Names2SubRegIndices.find(Name);
2263 if (SubRegInfo == Names2SubRegIndices.end())
2264 return 0;
2265 return SubRegInfo->getValue();
2266}
2267
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002268static void initSlots2BasicBlocks(
2269 const Function &F,
2270 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2271 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002272 MST.incorporateFunction(F);
2273 for (auto &BB : F) {
2274 if (BB.hasName())
2275 continue;
2276 int Slot = MST.getLocalSlot(&BB);
2277 if (Slot == -1)
2278 continue;
2279 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2280 }
2281}
2282
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002283static const BasicBlock *getIRBlockFromSlot(
2284 unsigned Slot,
2285 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002286 auto BlockInfo = Slots2BasicBlocks.find(Slot);
2287 if (BlockInfo == Slots2BasicBlocks.end())
2288 return nullptr;
2289 return BlockInfo->second;
2290}
2291
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002292const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2293 if (Slots2BasicBlocks.empty())
2294 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
2295 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2296}
2297
2298const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2299 if (&F == MF.getFunction())
2300 return getIRBlock(Slot);
2301 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2302 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2303 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2304}
2305
Alex Lorenzdd13be02015-08-19 23:31:05 +00002306static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2307 DenseMap<unsigned, const Value *> &Slots2Values) {
2308 int Slot = MST.getLocalSlot(V);
2309 if (Slot == -1)
2310 return;
2311 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2312}
2313
2314/// Creates the mapping from slot numbers to function's unnamed IR values.
2315static void initSlots2Values(const Function &F,
2316 DenseMap<unsigned, const Value *> &Slots2Values) {
2317 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2318 MST.incorporateFunction(F);
2319 for (const auto &Arg : F.args())
2320 mapValueToSlot(&Arg, MST, Slots2Values);
2321 for (const auto &BB : F) {
2322 mapValueToSlot(&BB, MST, Slots2Values);
2323 for (const auto &I : BB)
2324 mapValueToSlot(&I, MST, Slots2Values);
2325 }
2326}
2327
2328const Value *MIParser::getIRValue(unsigned Slot) {
2329 if (Slots2Values.empty())
2330 initSlots2Values(*MF.getFunction(), Slots2Values);
2331 auto ValueInfo = Slots2Values.find(Slot);
2332 if (ValueInfo == Slots2Values.end())
2333 return nullptr;
2334 return ValueInfo->second;
2335}
2336
Alex Lorenzef5c1962015-07-28 23:02:45 +00002337void MIParser::initNames2TargetIndices() {
2338 if (!Names2TargetIndices.empty())
2339 return;
2340 const auto *TII = MF.getSubtarget().getInstrInfo();
2341 assert(TII && "Expected target instruction info");
2342 auto Indices = TII->getSerializableTargetIndices();
2343 for (const auto &I : Indices)
2344 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2345}
2346
2347bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2348 initNames2TargetIndices();
2349 auto IndexInfo = Names2TargetIndices.find(Name);
2350 if (IndexInfo == Names2TargetIndices.end())
2351 return true;
2352 Index = IndexInfo->second;
2353 return false;
2354}
2355
Alex Lorenz49873a82015-08-06 00:44:07 +00002356void MIParser::initNames2DirectTargetFlags() {
2357 if (!Names2DirectTargetFlags.empty())
2358 return;
2359 const auto *TII = MF.getSubtarget().getInstrInfo();
2360 assert(TII && "Expected target instruction info");
2361 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2362 for (const auto &I : Flags)
2363 Names2DirectTargetFlags.insert(
2364 std::make_pair(StringRef(I.second), I.first));
2365}
2366
2367bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2368 initNames2DirectTargetFlags();
2369 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2370 if (FlagInfo == Names2DirectTargetFlags.end())
2371 return true;
2372 Flag = FlagInfo->second;
2373 return false;
2374}
2375
Alex Lorenzf3630112015-08-18 22:52:15 +00002376void MIParser::initNames2BitmaskTargetFlags() {
2377 if (!Names2BitmaskTargetFlags.empty())
2378 return;
2379 const auto *TII = MF.getSubtarget().getInstrInfo();
2380 assert(TII && "Expected target instruction info");
2381 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2382 for (const auto &I : Flags)
2383 Names2BitmaskTargetFlags.insert(
2384 std::make_pair(StringRef(I.second), I.first));
2385}
2386
2387bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2388 initNames2BitmaskTargetFlags();
2389 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2390 if (FlagInfo == Names2BitmaskTargetFlags.end())
2391 return true;
2392 Flag = FlagInfo->second;
2393 return false;
2394}
2395
Matthias Braun83947862016-07-13 22:23:23 +00002396bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2397 StringRef Src,
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002398 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002399 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002400}
2401
Matthias Braun74ad41c2016-10-11 03:13:01 +00002402bool llvm::parseMachineInstructions(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002403 StringRef Src, SMDiagnostic &Error) {
2404 return MIParser(PFS, Error, Src).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002405}
Alex Lorenzf09df002015-06-30 18:16:42 +00002406
Matthias Braun74ad41c2016-10-11 03:13:01 +00002407bool llvm::parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002408 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +00002409 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002410 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002411}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002412
Tom Stellard9c884e42016-11-15 00:03:14 +00002413bool llvm::parseRegisterReference(PerFunctionMIParsingState &PFS,
2414 unsigned &Reg, StringRef Src,
2415 SMDiagnostic &Error) {
2416 return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
2417}
2418
Matthias Braun74ad41c2016-10-11 03:13:01 +00002419bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002420 unsigned &Reg, StringRef Src,
Alex Lorenz9fab3702015-07-14 21:24:41 +00002421 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002422 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002423}
Alex Lorenz12045a42015-07-27 17:42:45 +00002424
Matthias Braun74ad41c2016-10-11 03:13:01 +00002425bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState &PFS,
2426 VRegInfo *&Info, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +00002427 SMDiagnostic &Error) {
Matthias Braun74ad41c2016-10-11 03:13:01 +00002428 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
Alex Lorenz12045a42015-07-27 17:42:45 +00002429}
Alex Lorenza314d812015-08-18 22:26:26 +00002430
Matthias Braun74ad41c2016-10-11 03:13:01 +00002431bool llvm::parseStackObjectReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002432 int &FI, StringRef Src,
Alex Lorenza314d812015-08-18 22:26:26 +00002433 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002434 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
Alex Lorenza314d812015-08-18 22:26:26 +00002435}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002436
Matthias Braun74ad41c2016-10-11 03:13:01 +00002437bool llvm::parseMDNode(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002438 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2439 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002440}