blob: 1d36ff4e1458d8ef4f001ae8a3867b9cadb83cd8 [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"
Matthias Braun89401142017-05-05 21:09:30 +000015
Alex Lorenz91370c52015-06-22 20:37:46 +000016#include "MILexer.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000017#include "llvm/ADT/StringMap.h"
Reid Klecknera5b1eef2016-08-26 17:58:37 +000018#include "llvm/ADT/StringSwitch.h"
Alex Lorenzad156fb2015-07-31 20:49:21 +000019#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000020#include "llvm/AsmParser/SlotMapping.h"
Matthias Braun89401142017-05-05 21:09:30 +000021#include "llvm/CodeGen/MIRPrinter.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000024#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000025#include "llvm/CodeGen/MachineInstr.h"
Alex Lorenzcb268d42015-07-06 23:07:26 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000027#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000028#include "llvm/CodeGen/MachineModuleInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000030#include "llvm/IR/Constants.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000031#include "llvm/IR/Instructions.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000032#include "llvm/IR/Intrinsics.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000033#include "llvm/IR/Module.h"
Alex Lorenz8a1915b2015-07-27 22:42:41 +000034#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000035#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000036#include "llvm/Support/SourceMgr.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000037#include "llvm/Support/raw_ostream.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000038#include "llvm/Target/TargetInstrInfo.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000039#include "llvm/Target/TargetIntrinsicInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000040#include "llvm/Target/TargetSubtargetInfo.h"
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +000041#include <cctype>
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000042
43using namespace llvm;
44
Matthias Braune35861d2016-07-13 23:27:50 +000045PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
Matthias Braunde5fea22017-01-18 00:59:19 +000046 SourceMgr &SM, const SlotMapping &IRSlots,
47 const Name2RegClassMap &Names2RegClasses,
48 const Name2RegBankMap &Names2RegBanks)
49 : MF(MF), SM(&SM), IRSlots(IRSlots), Names2RegClasses(Names2RegClasses),
50 Names2RegBanks(Names2RegBanks) {
Matthias Braun83947862016-07-13 22:23:23 +000051}
52
Matthias Braun74ad41c2016-10-11 03:13:01 +000053VRegInfo &PerFunctionMIParsingState::getVRegInfo(unsigned Num) {
54 auto I = VRegInfos.insert(std::make_pair(Num, nullptr));
55 if (I.second) {
56 MachineRegisterInfo &MRI = MF.getRegInfo();
57 VRegInfo *Info = new (Allocator) VRegInfo;
58 Info->VReg = MRI.createIncompleteVirtualRegister();
59 I.first->second = Info;
60 }
61 return *I.first->second;
62}
63
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000064namespace {
65
Alex Lorenz36962cd2015-07-07 02:08:46 +000066/// A wrapper struct around the 'MachineOperand' struct that includes a source
Alex Lorenzfeb6b432015-08-19 19:19:16 +000067/// range and other attributes.
68struct ParsedMachineOperand {
Alex Lorenz36962cd2015-07-07 02:08:46 +000069 MachineOperand Operand;
70 StringRef::iterator Begin;
71 StringRef::iterator End;
Alex Lorenz5ef93b02015-08-19 19:05:34 +000072 Optional<unsigned> TiedDefIdx;
Alex Lorenz36962cd2015-07-07 02:08:46 +000073
Alex Lorenzfeb6b432015-08-19 19:19:16 +000074 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
75 StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
Alex Lorenz5ef93b02015-08-19 19:05:34 +000076 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
77 if (TiedDefIdx)
78 assert(Operand.isReg() && Operand.isUse() &&
79 "Only used register operands can be tied");
80 }
Alex Lorenz36962cd2015-07-07 02:08:46 +000081};
82
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000083class MIParser {
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000084 MachineFunction &MF;
85 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000086 StringRef Source, CurrentSource;
87 MIToken Token;
Matthias Braun74ad41c2016-10-11 03:13:01 +000088 PerFunctionMIParsingState &PFS;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000089 /// Maps from instruction names to op codes.
90 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000091 /// Maps from register names to registers.
92 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000093 /// Maps from register mask names to register masks.
94 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000095 /// Maps from subregister names to subregister indices.
96 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000097 /// Maps from slot numbers to function's unnamed basic blocks.
98 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzdd13be02015-08-19 23:31:05 +000099 /// Maps from slot numbers to function's unnamed values.
100 DenseMap<unsigned, const Value *> Slots2Values;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000101 /// Maps from target index names to target indices.
102 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +0000103 /// Maps from direct target flag names to the direct target flag values.
104 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenzf3630112015-08-18 22:52:15 +0000105 /// Maps from direct target flag names to the bitmask target flag values.
106 StringMap<unsigned> Names2BitmaskTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000107
108public:
Matthias Braun74ad41c2016-10-11 03:13:01 +0000109 MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
Matthias Braune35861d2016-07-13 23:27:50 +0000110 StringRef Source);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000111
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000112 /// \p SkipChar gives the number of characters to skip before looking
113 /// for the next token.
114 void lex(unsigned SkipChar = 0);
Alex Lorenz91370c52015-06-22 20:37:46 +0000115
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000116 /// Report an error at the current location with the given message.
117 ///
118 /// This function always return true.
119 bool error(const Twine &Msg);
120
Alex Lorenz91370c52015-06-22 20:37:46 +0000121 /// Report an error at the given location with the given message.
122 ///
123 /// This function always return true.
124 bool error(StringRef::iterator Loc, const Twine &Msg);
125
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000126 bool
127 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
128 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +0000129 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +0000130 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
131 bool parseStandaloneNamedRegister(unsigned &Reg);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000132 bool parseStandaloneVirtualRegister(VRegInfo *&Info);
Tom Stellard9c884e42016-11-15 00:03:14 +0000133 bool parseStandaloneRegister(unsigned &Reg);
Alex Lorenza314d812015-08-18 22:26:26 +0000134 bool parseStandaloneStackObject(int &FI);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000135 bool parseStandaloneMDNode(MDNode *&Node);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000136
137 bool
138 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
Matthias Braun89401142017-05-05 21:09:30 +0000139 bool parseBasicBlock(MachineBasicBlock &MBB,
140 MachineBasicBlock *&AddFalthroughFrom);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000141 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
142 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000143
Matthias Braun74ad41c2016-10-11 03:13:01 +0000144 bool parseNamedRegister(unsigned &Reg);
145 bool parseVirtualRegister(VRegInfo *&Info);
146 bool parseRegister(unsigned &Reg, VRegInfo *&VRegInfo);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000147 bool parseRegisterFlag(unsigned &Flags);
Matthias Braunde5fea22017-01-18 00:59:19 +0000148 bool parseRegisterClassOrBank(VRegInfo &RegInfo);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000149 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000150 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
151 bool parseRegisterOperand(MachineOperand &Dest,
152 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000153 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000154 bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
155 const Constant *&C);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000156 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Ahmed Bougachad760de02016-07-28 17:15:12 +0000157 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
Alex Lorenz05e38822015-08-05 18:52:21 +0000158 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000159 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000160 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000161 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000162 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000163 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000164 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000165 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000166 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000167 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000168 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +0000169 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000170 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000171 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000172 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000173 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000174 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000175 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000176 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000177 bool parseIRBlock(BasicBlock *&BB, const Function &F);
178 bool parseBlockAddressOperand(MachineOperand &Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +0000179 bool parseIntrinsicOperand(MachineOperand &Dest);
Tim Northoverde3aea0412016-08-17 20:25:25 +0000180 bool parsePredicateOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000181 bool parseTargetIndexOperand(MachineOperand &Dest);
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000182 bool parseCustomRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000183 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000184 bool parseMachineOperand(MachineOperand &Dest,
185 Optional<unsigned> &TiedDefIdx);
186 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
187 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000188 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000189 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000190 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000191 bool parseIRValue(const Value *&V);
Justin Lebar0af80cd2016-07-15 18:26:59 +0000192 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000193 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
194 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Tim Northoverb73e3092017-02-13 22:14:08 +0000195 bool parseOptionalAtomicOrdering(AtomicOrdering &Order);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000196 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000197
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000198private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000199 /// Convert the integer literal in the current token into an unsigned integer.
200 ///
201 /// Return true if an error occurred.
202 bool getUnsigned(unsigned &Result);
203
Alex Lorenz4af7e612015-08-03 23:08:19 +0000204 /// Convert the integer literal in the current token into an uint64.
205 ///
206 /// Return true if an error occurred.
207 bool getUint64(uint64_t &Result);
208
Krzysztof Parzyszek36ef5dc2016-12-16 13:58:01 +0000209 /// Convert the hexadecimal literal in the current token into an unsigned
210 /// APInt with a minimum bitwidth required to represent the value.
211 ///
212 /// Return true if the literal does not represent an integer value.
213 bool getHexUint(APInt &Result);
214
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000215 /// If the current token is of the given kind, consume it and return false.
216 /// Otherwise report an error and return true.
217 bool expectAndConsume(MIToken::TokenKind TokenKind);
218
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000219 /// If the current token is of the given kind, consume it and return true.
220 /// Otherwise return false.
221 bool consumeIfPresent(MIToken::TokenKind TokenKind);
222
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000223 void initNames2InstrOpCodes();
224
225 /// Try to convert an instruction name to an opcode. Return true if the
226 /// instruction name is invalid.
227 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000228
Alex Lorenze5a44662015-07-17 00:24:15 +0000229 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000230
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000231 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000232 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000233
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000234 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000235 const MCInstrDesc &MCID);
236
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000237 void initNames2Regs();
238
239 /// Try to convert a register name to a register number. Return true if the
240 /// register name is invalid.
241 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000242
243 void initNames2RegMasks();
244
245 /// Check if the given identifier is a name of a register mask.
246 ///
247 /// Return null if the identifier isn't a register mask.
248 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000249
250 void initNames2SubRegIndices();
251
252 /// Check if the given identifier is a name of a subregister index.
253 ///
254 /// Return 0 if the name isn't a subregister index class.
255 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000256
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000257 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000258 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000259
Alex Lorenzdd13be02015-08-19 23:31:05 +0000260 const Value *getIRValue(unsigned Slot);
261
Alex Lorenzef5c1962015-07-28 23:02:45 +0000262 void initNames2TargetIndices();
263
264 /// Try to convert a name of target index to the corresponding target index.
265 ///
266 /// Return true if the name isn't a name of a target index.
267 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000268
269 void initNames2DirectTargetFlags();
270
271 /// Try to convert a name of a direct target flag to the corresponding
272 /// target flag.
273 ///
274 /// Return true if the name isn't a name of a direct flag.
275 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000276
277 void initNames2BitmaskTargetFlags();
278
279 /// Try to convert a name of a bitmask target flag to the corresponding
280 /// target flag.
281 ///
282 /// Return true if the name isn't a name of a bitmask target flag.
283 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000284};
285
286} // end anonymous namespace
287
Matthias Braun74ad41c2016-10-11 03:13:01 +0000288MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
Matthias Braune35861d2016-07-13 23:27:50 +0000289 StringRef Source)
290 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
291{}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000292
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000293void MIParser::lex(unsigned SkipChar) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000294 CurrentSource = lexMIToken(
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000295 CurrentSource.data() + SkipChar, Token,
Alex Lorenz91370c52015-06-22 20:37:46 +0000296 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
297}
298
299bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
300
301bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Matthias Braune35861d2016-07-13 23:27:50 +0000302 const SourceMgr &SM = *PFS.SM;
Alex Lorenz91370c52015-06-22 20:37:46 +0000303 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000304 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
305 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
306 // Create an ordinary diagnostic when the source manager's buffer is the
307 // source string.
308 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
309 return true;
310 }
311 // Create a diagnostic for a YAML string literal.
312 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
313 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
314 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000315 return true;
316}
317
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000318static const char *toString(MIToken::TokenKind TokenKind) {
319 switch (TokenKind) {
320 case MIToken::comma:
321 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000322 case MIToken::equal:
323 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000324 case MIToken::colon:
325 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000326 case MIToken::lparen:
327 return "'('";
328 case MIToken::rparen:
329 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000330 default:
331 return "<unknown token>";
332 }
333}
334
335bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
336 if (Token.isNot(TokenKind))
337 return error(Twine("expected ") + toString(TokenKind));
338 lex();
339 return false;
340}
341
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000342bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
343 if (Token.isNot(TokenKind))
344 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000345 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000346 return true;
347}
Alex Lorenz91370c52015-06-22 20:37:46 +0000348
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000349bool MIParser::parseBasicBlockDefinition(
350 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
351 assert(Token.is(MIToken::MachineBasicBlockLabel));
352 unsigned ID = 0;
353 if (getUnsigned(ID))
354 return true;
355 auto Loc = Token.location();
356 auto Name = Token.stringValue();
357 lex();
358 bool HasAddressTaken = false;
359 bool IsLandingPad = false;
360 unsigned Alignment = 0;
361 BasicBlock *BB = nullptr;
362 if (consumeIfPresent(MIToken::lparen)) {
363 do {
364 // TODO: Report an error when multiple same attributes are specified.
365 switch (Token.kind()) {
366 case MIToken::kw_address_taken:
367 HasAddressTaken = true;
368 lex();
369 break;
370 case MIToken::kw_landing_pad:
371 IsLandingPad = true;
372 lex();
373 break;
374 case MIToken::kw_align:
375 if (parseAlignment(Alignment))
376 return true;
377 break;
378 case MIToken::IRBlock:
379 // TODO: Report an error when both name and ir block are specified.
380 if (parseIRBlock(BB, *MF.getFunction()))
381 return true;
382 lex();
383 break;
384 default:
385 break;
386 }
387 } while (consumeIfPresent(MIToken::comma));
388 if (expectAndConsume(MIToken::rparen))
389 return true;
390 }
391 if (expectAndConsume(MIToken::colon))
392 return true;
393
394 if (!Name.empty()) {
395 BB = dyn_cast_or_null<BasicBlock>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000396 MF.getFunction()->getValueSymbolTable()->lookup(Name));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000397 if (!BB)
398 return error(Loc, Twine("basic block '") + Name +
399 "' is not defined in the function '" +
400 MF.getName() + "'");
401 }
402 auto *MBB = MF.CreateMachineBasicBlock(BB);
403 MF.insert(MF.end(), MBB);
404 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
405 if (!WasInserted)
406 return error(Loc, Twine("redefinition of machine basic block with id #") +
407 Twine(ID));
408 if (Alignment)
409 MBB->setAlignment(Alignment);
410 if (HasAddressTaken)
411 MBB->setHasAddressTaken();
Reid Kleckner0e288232015-08-27 23:27:47 +0000412 MBB->setIsEHPad(IsLandingPad);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000413 return false;
414}
415
416bool MIParser::parseBasicBlockDefinitions(
417 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
418 lex();
419 // Skip until the first machine basic block.
420 while (Token.is(MIToken::Newline))
421 lex();
422 if (Token.isErrorOrEOF())
423 return Token.isError();
424 if (Token.isNot(MIToken::MachineBasicBlockLabel))
425 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000426 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000427 do {
428 if (parseBasicBlockDefinition(MBBSlots))
429 return true;
430 bool IsAfterNewline = false;
431 // Skip until the next machine basic block.
432 while (true) {
433 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
434 Token.isErrorOrEOF())
435 break;
436 else if (Token.is(MIToken::MachineBasicBlockLabel))
437 return error("basic block definition should be located at the start of "
438 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000439 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000440 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000441 continue;
442 }
443 IsAfterNewline = false;
444 if (Token.is(MIToken::lbrace))
445 ++BraceDepth;
446 if (Token.is(MIToken::rbrace)) {
447 if (!BraceDepth)
448 return error("extraneous closing brace ('}')");
449 --BraceDepth;
450 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000451 lex();
452 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000453 // Verify that we closed all of the '{' at the end of a file or a block.
454 if (!Token.isError() && BraceDepth)
455 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000456 } while (!Token.isErrorOrEOF());
457 return Token.isError();
458}
459
460bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
461 assert(Token.is(MIToken::kw_liveins));
462 lex();
463 if (expectAndConsume(MIToken::colon))
464 return true;
465 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
466 return false;
467 do {
468 if (Token.isNot(MIToken::NamedRegister))
469 return error("expected a named register");
470 unsigned Reg = 0;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000471 if (parseNamedRegister(Reg))
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000472 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000473 lex();
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000474 LaneBitmask Mask = LaneBitmask::getAll();
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000475 if (consumeIfPresent(MIToken::colon)) {
476 // Parse lane mask.
477 if (Token.isNot(MIToken::IntegerLiteral) &&
478 Token.isNot(MIToken::HexLiteral))
479 return error("expected a lane mask");
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000480 static_assert(sizeof(LaneBitmask::Type) == sizeof(unsigned),
481 "Use correct get-function for lane mask");
482 LaneBitmask::Type V;
483 if (getUnsigned(V))
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000484 return error("invalid lane mask value");
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000485 Mask = LaneBitmask(V);
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000486 lex();
487 }
488 MBB.addLiveIn(Reg, Mask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000489 } while (consumeIfPresent(MIToken::comma));
490 return false;
491}
492
493bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
494 assert(Token.is(MIToken::kw_successors));
495 lex();
496 if (expectAndConsume(MIToken::colon))
497 return true;
498 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
499 return false;
500 do {
501 if (Token.isNot(MIToken::MachineBasicBlock))
502 return error("expected a machine basic block reference");
503 MachineBasicBlock *SuccMBB = nullptr;
504 if (parseMBBReference(SuccMBB))
505 return true;
506 lex();
507 unsigned Weight = 0;
508 if (consumeIfPresent(MIToken::lparen)) {
Geoff Berryb51774a2016-11-18 19:37:24 +0000509 if (Token.isNot(MIToken::IntegerLiteral) &&
510 Token.isNot(MIToken::HexLiteral))
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000511 return error("expected an integer literal after '('");
512 if (getUnsigned(Weight))
513 return true;
514 lex();
515 if (expectAndConsume(MIToken::rparen))
516 return true;
517 }
Cong Houd97c1002015-12-01 05:29:22 +0000518 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000519 } while (consumeIfPresent(MIToken::comma));
Cong Houd97c1002015-12-01 05:29:22 +0000520 MBB.normalizeSuccProbs();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000521 return false;
522}
523
Matthias Braun89401142017-05-05 21:09:30 +0000524bool MIParser::parseBasicBlock(MachineBasicBlock &MBB,
525 MachineBasicBlock *&AddFalthroughFrom) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000526 // Skip the definition.
527 assert(Token.is(MIToken::MachineBasicBlockLabel));
528 lex();
529 if (consumeIfPresent(MIToken::lparen)) {
530 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
531 lex();
532 consumeIfPresent(MIToken::rparen);
533 }
534 consumeIfPresent(MIToken::colon);
535
536 // Parse the liveins and successors.
537 // N.B: Multiple lists of successors and liveins are allowed and they're
538 // merged into one.
539 // Example:
540 // liveins: %edi
541 // liveins: %esi
542 //
543 // is equivalent to
544 // liveins: %edi, %esi
Matthias Braun89401142017-05-05 21:09:30 +0000545 bool ExplicitSuccesors = false;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000546 while (true) {
547 if (Token.is(MIToken::kw_successors)) {
548 if (parseBasicBlockSuccessors(MBB))
549 return true;
Matthias Braun89401142017-05-05 21:09:30 +0000550 ExplicitSuccesors = true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000551 } else if (Token.is(MIToken::kw_liveins)) {
552 if (parseBasicBlockLiveins(MBB))
553 return true;
554 } else if (consumeIfPresent(MIToken::Newline)) {
555 continue;
556 } else
557 break;
558 if (!Token.isNewlineOrEOF())
559 return error("expected line break at the end of a list");
560 lex();
561 }
562
563 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000564 bool IsInBundle = false;
565 MachineInstr *PrevMI = nullptr;
Matthias Braun89401142017-05-05 21:09:30 +0000566 while (!Token.is(MIToken::MachineBasicBlockLabel) &&
567 !Token.is(MIToken::Eof)) {
568 if (consumeIfPresent(MIToken::Newline))
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000569 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000570 if (consumeIfPresent(MIToken::rbrace)) {
571 // The first parsing pass should verify that all closing '}' have an
572 // opening '{'.
573 assert(IsInBundle);
574 IsInBundle = false;
575 continue;
576 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000577 MachineInstr *MI = nullptr;
578 if (parse(MI))
579 return true;
580 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000581 if (IsInBundle) {
582 PrevMI->setFlag(MachineInstr::BundledSucc);
583 MI->setFlag(MachineInstr::BundledPred);
584 }
585 PrevMI = MI;
586 if (Token.is(MIToken::lbrace)) {
587 if (IsInBundle)
588 return error("nested instruction bundles are not allowed");
589 lex();
590 // This instruction is the start of the bundle.
591 MI->setFlag(MachineInstr::BundledSucc);
592 IsInBundle = true;
593 if (!Token.is(MIToken::Newline))
594 // The next instruction can be on the same line.
595 continue;
596 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000597 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
598 lex();
599 }
Matthias Braun89401142017-05-05 21:09:30 +0000600
601 // Construct successor list by searching for basic block machine operands.
602 if (!ExplicitSuccesors) {
603 SmallVector<MachineBasicBlock*,4> Successors;
604 bool IsFallthrough;
605 guessSuccessors(MBB, Successors, IsFallthrough);
606 for (MachineBasicBlock *Succ : Successors)
607 MBB.addSuccessor(Succ);
608
609 if (IsFallthrough) {
610 AddFalthroughFrom = &MBB;
611 } else {
612 MBB.normalizeSuccProbs();
613 }
614 }
615
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000616 return false;
617}
618
619bool MIParser::parseBasicBlocks() {
620 lex();
621 // Skip until the first machine basic block.
622 while (Token.is(MIToken::Newline))
623 lex();
624 if (Token.isErrorOrEOF())
625 return Token.isError();
626 // The first parsing pass should have verified that this token is a MBB label
627 // in the 'parseBasicBlockDefinitions' method.
628 assert(Token.is(MIToken::MachineBasicBlockLabel));
Matthias Braun89401142017-05-05 21:09:30 +0000629 MachineBasicBlock *AddFalthroughFrom = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000630 do {
631 MachineBasicBlock *MBB = nullptr;
632 if (parseMBBReference(MBB))
633 return true;
Matthias Braun89401142017-05-05 21:09:30 +0000634 if (AddFalthroughFrom) {
635 if (!AddFalthroughFrom->isSuccessor(MBB))
636 AddFalthroughFrom->addSuccessor(MBB);
637 AddFalthroughFrom->normalizeSuccProbs();
638 AddFalthroughFrom = nullptr;
639 }
640 if (parseBasicBlock(*MBB, AddFalthroughFrom))
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000641 return true;
642 // The method 'parseBasicBlock' should parse the whole block until the next
643 // block or the end of file.
644 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
645 } while (Token.isNot(MIToken::Eof));
646 return false;
647}
648
649bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000650 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000651 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000652 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000653 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000654 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000655 Optional<unsigned> TiedDefIdx;
656 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000657 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000658 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000659 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000660 if (Token.isNot(MIToken::comma))
661 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000662 lex();
663 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000664 if (!Operands.empty() && expectAndConsume(MIToken::equal))
665 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000666
Alex Lorenze5a44662015-07-17 00:24:15 +0000667 unsigned OpCode, Flags = 0;
668 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000669 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000670
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000671 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000672 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000673 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000674 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000675 Optional<unsigned> TiedDefIdx;
676 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000677 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000678 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000679 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000680 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
681 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000682 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000683 if (Token.isNot(MIToken::comma))
684 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000685 lex();
686 }
687
Alex Lorenz46d760d2015-07-22 21:15:11 +0000688 DebugLoc DebugLocation;
689 if (Token.is(MIToken::kw_debug_location)) {
690 lex();
691 if (Token.isNot(MIToken::exclaim))
692 return error("expected a metadata node after 'debug-location'");
693 MDNode *Node = nullptr;
694 if (parseMDNode(Node))
695 return true;
696 DebugLocation = DebugLoc(Node);
697 }
698
Alex Lorenz4af7e612015-08-03 23:08:19 +0000699 // Parse the machine memory operands.
700 SmallVector<MachineMemOperand *, 2> MemOperands;
701 if (Token.is(MIToken::coloncolon)) {
702 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000703 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000704 MachineMemOperand *MemOp = nullptr;
705 if (parseMachineMemoryOperand(MemOp))
706 return true;
707 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000708 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000709 break;
710 if (Token.isNot(MIToken::comma))
711 return error("expected ',' before the next machine memory operand");
712 lex();
713 }
714 }
715
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000716 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000717 if (!MCID.isVariadic()) {
718 // FIXME: Move the implicit operand verification to the machine verifier.
719 if (verifyImplicitOperands(Operands, MCID))
720 return true;
721 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000722
Alex Lorenzcb268d42015-07-06 23:07:26 +0000723 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000724 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000725 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000726 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000727 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000728 if (assignRegisterTies(*MI, Operands))
729 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000730 if (MemOperands.empty())
731 return false;
732 MachineInstr::mmo_iterator MemRefs =
733 MF.allocateMemRefsArray(MemOperands.size());
734 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
735 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000736 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000737}
738
Alex Lorenz1ea60892015-07-27 20:29:27 +0000739bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000740 lex();
741 if (Token.isNot(MIToken::MachineBasicBlock))
742 return error("expected a machine basic block reference");
743 if (parseMBBReference(MBB))
744 return true;
745 lex();
746 if (Token.isNot(MIToken::Eof))
747 return error(
748 "expected end of string after the machine basic block reference");
749 return false;
750}
751
Alex Lorenz1ea60892015-07-27 20:29:27 +0000752bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000753 lex();
754 if (Token.isNot(MIToken::NamedRegister))
755 return error("expected a named register");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000756 if (parseNamedRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000757 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000758 lex();
759 if (Token.isNot(MIToken::Eof))
760 return error("expected end of string after the register reference");
761 return false;
762}
763
Matthias Braun74ad41c2016-10-11 03:13:01 +0000764bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
Alex Lorenz12045a42015-07-27 17:42:45 +0000765 lex();
766 if (Token.isNot(MIToken::VirtualRegister))
767 return error("expected a virtual register");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000768 if (parseVirtualRegister(Info))
Alex Lorenz607efb62015-08-18 22:57:36 +0000769 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000770 lex();
771 if (Token.isNot(MIToken::Eof))
772 return error("expected end of string after the register reference");
773 return false;
774}
775
Tom Stellard9c884e42016-11-15 00:03:14 +0000776bool MIParser::parseStandaloneRegister(unsigned &Reg) {
777 lex();
778 if (Token.isNot(MIToken::NamedRegister) &&
779 Token.isNot(MIToken::VirtualRegister))
780 return error("expected either a named or virtual register");
781
782 VRegInfo *Info;
783 if (parseRegister(Reg, Info))
784 return true;
785
786 lex();
787 if (Token.isNot(MIToken::Eof))
788 return error("expected end of string after the register reference");
789 return false;
790}
791
Alex Lorenza314d812015-08-18 22:26:26 +0000792bool MIParser::parseStandaloneStackObject(int &FI) {
793 lex();
794 if (Token.isNot(MIToken::StackObject))
795 return error("expected a stack object");
796 if (parseStackFrameIndex(FI))
797 return true;
798 if (Token.isNot(MIToken::Eof))
799 return error("expected end of string after the stack object reference");
800 return false;
801}
802
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000803bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
804 lex();
805 if (Token.isNot(MIToken::exclaim))
806 return error("expected a metadata node");
807 if (parseMDNode(Node))
808 return true;
809 if (Token.isNot(MIToken::Eof))
810 return error("expected end of string after the metadata node");
811 return false;
812}
813
Alex Lorenz36962cd2015-07-07 02:08:46 +0000814static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
815 assert(MO.isImplicit());
816 return MO.isDef() ? "implicit-def" : "implicit";
817}
818
819static std::string getRegisterName(const TargetRegisterInfo *TRI,
820 unsigned Reg) {
821 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
822 return StringRef(TRI->getName(Reg)).lower();
823}
824
Alex Lorenz0153e592015-09-10 14:04:34 +0000825/// Return true if the parsed machine operands contain a given machine operand.
826static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
827 ArrayRef<ParsedMachineOperand> Operands) {
828 for (const auto &I : Operands) {
829 if (ImplicitOperand.isIdenticalTo(I.Operand))
830 return true;
831 }
832 return false;
833}
834
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000835bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
836 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000837 if (MCID.isCall())
838 // We can't verify call instructions as they can contain arbitrary implicit
839 // register and register mask operands.
840 return false;
841
842 // Gather all the expected implicit operands.
843 SmallVector<MachineOperand, 4> ImplicitOperands;
844 if (MCID.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000845 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000846 ImplicitOperands.push_back(
847 MachineOperand::CreateReg(*ImpDefs, true, true));
848 if (MCID.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000849 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000850 ImplicitOperands.push_back(
851 MachineOperand::CreateReg(*ImpUses, false, true));
852
853 const auto *TRI = MF.getSubtarget().getRegisterInfo();
854 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000855 for (const auto &I : ImplicitOperands) {
856 if (isImplicitOperandIn(I, Operands))
857 continue;
858 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000859 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000860 printImplicitRegisterFlag(I) + " %" +
861 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000862 }
863 return false;
864}
865
Alex Lorenze5a44662015-07-17 00:24:15 +0000866bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
867 if (Token.is(MIToken::kw_frame_setup)) {
868 Flags |= MachineInstr::FrameSetup;
869 lex();
870 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000871 if (Token.isNot(MIToken::Identifier))
872 return error("expected a machine instruction");
873 StringRef InstrName = Token.stringValue();
874 if (parseInstrName(InstrName, OpCode))
875 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000876 lex();
877 return false;
878}
879
Matthias Braun74ad41c2016-10-11 03:13:01 +0000880bool MIParser::parseNamedRegister(unsigned &Reg) {
881 assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
882 StringRef Name = Token.stringValue();
883 if (getRegisterByName(Name, Reg))
884 return error(Twine("unknown register name '") + Name + "'");
885 return false;
886}
887
888bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
889 assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
890 unsigned ID;
891 if (getUnsigned(ID))
892 return true;
893 Info = &PFS.getVRegInfo(ID);
894 return false;
895}
896
897bool MIParser::parseRegister(unsigned &Reg, VRegInfo *&Info) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000898 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000899 case MIToken::underscore:
900 Reg = 0;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000901 return false;
902 case MIToken::NamedRegister:
903 return parseNamedRegister(Reg);
904 case MIToken::VirtualRegister:
905 if (parseVirtualRegister(Info))
Alex Lorenz53464512015-07-10 22:51:20 +0000906 return true;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000907 Reg = Info->VReg;
908 return false;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000909 // TODO: Parse other register kinds.
910 default:
911 llvm_unreachable("The current token should be a register");
912 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000913}
914
Matthias Braunde5fea22017-01-18 00:59:19 +0000915bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) {
Ahmed Bougachabf480552017-01-20 00:29:59 +0000916 if (Token.isNot(MIToken::Identifier) && Token.isNot(MIToken::underscore))
917 return error("expected '_', register class, or register bank name");
Matthias Braunde5fea22017-01-18 00:59:19 +0000918 StringRef::iterator Loc = Token.location();
919 StringRef Name = Token.stringValue();
920
921 // Was it a register class?
922 auto RCNameI = PFS.Names2RegClasses.find(Name);
923 if (RCNameI != PFS.Names2RegClasses.end()) {
924 lex();
925 const TargetRegisterClass &RC = *RCNameI->getValue();
926
927 switch (RegInfo.Kind) {
928 case VRegInfo::UNKNOWN:
929 case VRegInfo::NORMAL:
930 RegInfo.Kind = VRegInfo::NORMAL;
931 if (RegInfo.Explicit && RegInfo.D.RC != &RC) {
932 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
933 return error(Loc, Twine("conflicting register classes, previously: ") +
934 Twine(TRI.getRegClassName(RegInfo.D.RC)));
935 }
936 RegInfo.D.RC = &RC;
937 RegInfo.Explicit = true;
938 return false;
939
940 case VRegInfo::GENERIC:
941 case VRegInfo::REGBANK:
942 return error(Loc, "register class specification on generic register");
943 }
944 llvm_unreachable("Unexpected register kind");
945 }
946
Ahmed Bougachabf480552017-01-20 00:29:59 +0000947 // Should be a register bank or a generic register.
948 const RegisterBank *RegBank = nullptr;
949 if (Name != "_") {
950 auto RBNameI = PFS.Names2RegBanks.find(Name);
951 if (RBNameI == PFS.Names2RegBanks.end())
952 return error(Loc, "expected '_', register class, or register bank name");
953 RegBank = RBNameI->getValue();
954 }
Matthias Braunde5fea22017-01-18 00:59:19 +0000955
Ahmed Bougachabf480552017-01-20 00:29:59 +0000956 lex();
957
Matthias Braunde5fea22017-01-18 00:59:19 +0000958 switch (RegInfo.Kind) {
959 case VRegInfo::UNKNOWN:
960 case VRegInfo::GENERIC:
961 case VRegInfo::REGBANK:
Ahmed Bougachabf480552017-01-20 00:29:59 +0000962 RegInfo.Kind = RegBank ? VRegInfo::REGBANK : VRegInfo::GENERIC;
963 if (RegInfo.Explicit && RegInfo.D.RegBank != RegBank)
964 return error(Loc, "conflicting generic register banks");
965 RegInfo.D.RegBank = RegBank;
Matthias Braunde5fea22017-01-18 00:59:19 +0000966 RegInfo.Explicit = true;
967 return false;
968
969 case VRegInfo::NORMAL:
Ahmed Bougachabf480552017-01-20 00:29:59 +0000970 return error(Loc, "register bank specification on normal register");
Matthias Braunde5fea22017-01-18 00:59:19 +0000971 }
972 llvm_unreachable("Unexpected register kind");
973}
974
Alex Lorenzcb268d42015-07-06 23:07:26 +0000975bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000976 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000977 switch (Token.kind()) {
978 case MIToken::kw_implicit:
979 Flags |= RegState::Implicit;
980 break;
981 case MIToken::kw_implicit_define:
982 Flags |= RegState::ImplicitDefine;
983 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000984 case MIToken::kw_def:
985 Flags |= RegState::Define;
986 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000987 case MIToken::kw_dead:
988 Flags |= RegState::Dead;
989 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000990 case MIToken::kw_killed:
991 Flags |= RegState::Kill;
992 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000993 case MIToken::kw_undef:
994 Flags |= RegState::Undef;
995 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000996 case MIToken::kw_internal:
997 Flags |= RegState::InternalRead;
998 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000999 case MIToken::kw_early_clobber:
1000 Flags |= RegState::EarlyClobber;
1001 break;
Alex Lorenz90752582015-08-05 17:41:17 +00001002 case MIToken::kw_debug_use:
1003 Flags |= RegState::Debug;
1004 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +00001005 default:
1006 llvm_unreachable("The current token should be a register flag");
1007 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +00001008 if (OldFlags == Flags)
1009 // We know that the same flag is specified more than once when the flags
1010 // weren't modified.
1011 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +00001012 lex();
1013 return false;
1014}
1015
Alex Lorenz2eacca82015-07-13 23:24:34 +00001016bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
Matthias Braun333e4682016-07-26 21:49:34 +00001017 assert(Token.is(MIToken::dot));
Alex Lorenz2eacca82015-07-13 23:24:34 +00001018 lex();
1019 if (Token.isNot(MIToken::Identifier))
Matthias Braun333e4682016-07-26 21:49:34 +00001020 return error("expected a subregister index after '.'");
Alex Lorenz2eacca82015-07-13 23:24:34 +00001021 auto Name = Token.stringValue();
1022 SubReg = getSubRegIndex(Name);
1023 if (!SubReg)
1024 return error(Twine("use of unknown subregister index '") + Name + "'");
1025 lex();
1026 return false;
1027}
1028
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001029bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
1030 if (!consumeIfPresent(MIToken::kw_tied_def))
Tim Northoverd28d3cc2016-09-12 11:20:10 +00001031 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001032 if (Token.isNot(MIToken::IntegerLiteral))
1033 return error("expected an integer literal after 'tied-def'");
1034 if (getUnsigned(TiedDefIdx))
1035 return true;
1036 lex();
1037 if (expectAndConsume(MIToken::rparen))
1038 return true;
1039 return false;
1040}
1041
Alex Lorenzfeb6b432015-08-19 19:19:16 +00001042bool MIParser::assignRegisterTies(MachineInstr &MI,
1043 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001044 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
1045 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
1046 if (!Operands[I].TiedDefIdx)
1047 continue;
1048 // The parser ensures that this operand is a register use, so we just have
1049 // to check the tied-def operand.
1050 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
1051 if (DefIdx >= E)
1052 return error(Operands[I].Begin,
1053 Twine("use of invalid tied-def operand index '" +
1054 Twine(DefIdx) + "'; instruction has only ") +
1055 Twine(E) + " operands");
1056 const auto &DefOperand = Operands[DefIdx].Operand;
1057 if (!DefOperand.isReg() || !DefOperand.isDef())
1058 // FIXME: add note with the def operand.
1059 return error(Operands[I].Begin,
1060 Twine("use of invalid tied-def operand index '") +
1061 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
1062 " isn't a defined register");
1063 // Check that the tied-def operand wasn't tied elsewhere.
1064 for (const auto &TiedPair : TiedRegisterPairs) {
1065 if (TiedPair.first == DefIdx)
1066 return error(Operands[I].Begin,
1067 Twine("the tied-def operand #") + Twine(DefIdx) +
1068 " is already tied with another register operand");
1069 }
1070 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
1071 }
1072 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1073 // indices must be less than tied max.
1074 for (const auto &TiedPair : TiedRegisterPairs)
1075 MI.tieOperands(TiedPair.first, TiedPair.second);
1076 return false;
1077}
1078
1079bool MIParser::parseRegisterOperand(MachineOperand &Dest,
1080 Optional<unsigned> &TiedDefIdx,
1081 bool IsDef) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001082 unsigned Flags = IsDef ? RegState::Define : 0;
1083 while (Token.isRegisterFlag()) {
1084 if (parseRegisterFlag(Flags))
1085 return true;
1086 }
1087 if (!Token.isRegister())
1088 return error("expected a register after register flags");
Matthias Braun74ad41c2016-10-11 03:13:01 +00001089 unsigned Reg;
1090 VRegInfo *RegInfo;
1091 if (parseRegister(Reg, RegInfo))
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001092 return true;
1093 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +00001094 unsigned SubReg = 0;
Matthias Braun333e4682016-07-26 21:49:34 +00001095 if (Token.is(MIToken::dot)) {
Alex Lorenz2eacca82015-07-13 23:24:34 +00001096 if (parseSubRegisterIndex(SubReg))
1097 return true;
Matthias Braun5d00b322016-07-16 01:36:18 +00001098 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1099 return error("subregister index expects a virtual register");
Alex Lorenz2eacca82015-07-13 23:24:34 +00001100 }
Matthias Braunde5fea22017-01-18 00:59:19 +00001101 if (Token.is(MIToken::colon)) {
1102 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1103 return error("register class specification expects a virtual register");
1104 lex();
1105 if (parseRegisterClassOrBank(*RegInfo))
1106 return true;
1107 }
Tim Northoverd28d3cc2016-09-12 11:20:10 +00001108 MachineRegisterInfo &MRI = MF.getRegInfo();
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001109 if ((Flags & RegState::Define) == 0) {
1110 if (consumeIfPresent(MIToken::lparen)) {
1111 unsigned Idx;
Tim Northoverd28d3cc2016-09-12 11:20:10 +00001112 if (!parseRegisterTiedDefIndex(Idx))
1113 TiedDefIdx = Idx;
1114 else {
1115 // Try a redundant low-level type.
1116 LLT Ty;
1117 if (parseLowLevelType(Token.location(), Ty))
1118 return error("expected tied-def or low-level type after '('");
1119
1120 if (expectAndConsume(MIToken::rparen))
1121 return true;
1122
1123 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1124 return error("inconsistent type for generic virtual register");
1125
1126 MRI.setType(Reg, Ty);
1127 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001128 }
1129 } else if (consumeIfPresent(MIToken::lparen)) {
Quentin Colombete08cc592016-12-22 21:56:35 +00001130 // Virtual registers may have a tpe with GlobalISel.
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001131 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Quentin Colombete08cc592016-12-22 21:56:35 +00001132 return error("unexpected type on physical register");
Ahmed Bougacha5a59b242016-07-19 19:48:36 +00001133
Tim Northover0f140c72016-09-09 11:46:34 +00001134 LLT Ty;
1135 if (parseLowLevelType(Token.location(), Ty))
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001136 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001137
Tim Northover0f140c72016-09-09 11:46:34 +00001138 if (expectAndConsume(MIToken::rparen))
1139 return true;
1140
Tim Northoverd28d3cc2016-09-12 11:20:10 +00001141 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1142 return error("inconsistent type for generic virtual register");
1143
Tim Northover0f140c72016-09-09 11:46:34 +00001144 MRI.setType(Reg, Ty);
Matthias Braun74ad41c2016-10-11 03:13:01 +00001145 } else if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Quentin Colombet3749f332016-12-22 22:50:34 +00001146 // Generic virtual registers must have a type.
1147 // If we end up here this means the type hasn't been specified and
Quentin Colombet2c646962016-06-08 23:27:46 +00001148 // this is bad!
Matthias Braun74ad41c2016-10-11 03:13:01 +00001149 if (RegInfo->Kind == VRegInfo::GENERIC ||
1150 RegInfo->Kind == VRegInfo::REGBANK)
Quentin Colombet3749f332016-12-22 22:50:34 +00001151 return error("generic virtual registers must have a type");
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001152 }
Alex Lorenz495ad872015-07-08 21:23:34 +00001153 Dest = MachineOperand::CreateReg(
1154 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +00001155 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +00001156 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1157 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001158 return false;
1159}
1160
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001161bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1162 assert(Token.is(MIToken::IntegerLiteral));
1163 const APSInt &Int = Token.integerValue();
1164 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +00001165 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001166 Dest = MachineOperand::CreateImm(Int.getExtValue());
1167 lex();
1168 return false;
1169}
1170
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001171bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1172 const Constant *&C) {
1173 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001174 SMDiagnostic Err;
Malcolm Parsons06ac79c2016-11-02 16:43:50 +00001175 C = parseConstantValue(Source, Err, *MF.getFunction()->getParent(),
Matthias Braune35861d2016-07-13 23:27:50 +00001176 &PFS.IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001177 if (!C)
1178 return error(Loc + Err.getColumnNo(), Err.getMessage());
1179 return false;
1180}
1181
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001182bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1183 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1184 return true;
1185 lex();
1186 return false;
1187}
1188
Ahmed Bougachad760de02016-07-28 17:15:12 +00001189bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
Tim Northover32a078a2016-09-15 10:09:59 +00001190 if (Token.is(MIToken::ScalarType)) {
Tim Northover62ae5682016-07-20 19:09:30 +00001191 Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1192 lex();
1193 return false;
Tim Northoverbd505462016-07-22 16:59:52 +00001194 } else if (Token.is(MIToken::PointerType)) {
Tim Northover5ae83502016-09-15 09:20:34 +00001195 const DataLayout &DL = MF.getFunction()->getParent()->getDataLayout();
1196 unsigned AS = APSInt(Token.range().drop_front()).getZExtValue();
1197 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
Tim Northoverbd505462016-07-22 16:59:52 +00001198 lex();
1199 return false;
Tim Northover62ae5682016-07-20 19:09:30 +00001200 }
Quentin Colombet85199672016-03-08 00:20:48 +00001201
Tim Northover62ae5682016-07-20 19:09:30 +00001202 // Now we're looking for a vector.
1203 if (Token.isNot(MIToken::less))
Tim Northoverbd505462016-07-22 16:59:52 +00001204 return error(Loc,
1205 "expected unsized, pN, sN or <N x sM> for GlobalISel type");
1206
Tim Northover62ae5682016-07-20 19:09:30 +00001207 lex();
1208
1209 if (Token.isNot(MIToken::IntegerLiteral))
1210 return error(Loc, "expected <N x sM> for vctor type");
1211 uint64_t NumElements = Token.integerValue().getZExtValue();
1212 lex();
1213
1214 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1215 return error(Loc, "expected '<N x sM>' for vector type");
1216 lex();
1217
1218 if (Token.isNot(MIToken::ScalarType))
1219 return error(Loc, "expected '<N x sM>' for vector type");
1220 uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1221 lex();
1222
1223 if (Token.isNot(MIToken::greater))
1224 return error(Loc, "expected '<N x sM>' for vector type");
1225 lex();
1226
1227 Ty = LLT::vector(NumElements, ScalarSize);
Quentin Colombet85199672016-03-08 00:20:48 +00001228 return false;
1229}
1230
Alex Lorenz05e38822015-08-05 18:52:21 +00001231bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1232 assert(Token.is(MIToken::IntegerType));
1233 auto Loc = Token.location();
1234 lex();
1235 if (Token.isNot(MIToken::IntegerLiteral))
1236 return error("expected an integer literal");
1237 const Constant *C = nullptr;
1238 if (parseIRConstant(Loc, C))
1239 return true;
1240 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1241 return false;
1242}
1243
Alex Lorenzad156fb2015-07-31 20:49:21 +00001244bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1245 auto Loc = Token.location();
1246 lex();
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +00001247 if (Token.isNot(MIToken::FloatingPointLiteral) &&
1248 Token.isNot(MIToken::HexLiteral))
Alex Lorenzad156fb2015-07-31 20:49:21 +00001249 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001250 const Constant *C = nullptr;
1251 if (parseIRConstant(Loc, C))
1252 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001253 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1254 return false;
1255}
1256
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001257bool MIParser::getUnsigned(unsigned &Result) {
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +00001258 if (Token.hasIntegerValue()) {
1259 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1260 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1261 if (Val64 == Limit)
1262 return error("expected 32-bit integer (too large)");
1263 Result = Val64;
1264 return false;
1265 }
1266 if (Token.is(MIToken::HexLiteral)) {
Krzysztof Parzyszek36ef5dc2016-12-16 13:58:01 +00001267 APInt A;
1268 if (getHexUint(A))
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +00001269 return true;
Krzysztof Parzyszek36ef5dc2016-12-16 13:58:01 +00001270 if (A.getBitWidth() > 32)
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +00001271 return error("expected 32-bit integer (too large)");
1272 Result = A.getZExtValue();
1273 return false;
1274 }
1275 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001276}
1277
Alex Lorenzf09df002015-06-30 18:16:42 +00001278bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001279 assert(Token.is(MIToken::MachineBasicBlock) ||
1280 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001281 unsigned Number;
1282 if (getUnsigned(Number))
1283 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001284 auto MBBInfo = PFS.MBBSlots.find(Number);
1285 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001286 return error(Twine("use of undefined machine basic block #") +
1287 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001288 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001289 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1290 return error(Twine("the name of machine basic block #") + Twine(Number) +
1291 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001292 return false;
1293}
1294
1295bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1296 MachineBasicBlock *MBB;
1297 if (parseMBBReference(MBB))
1298 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001299 Dest = MachineOperand::CreateMBB(MBB);
1300 lex();
1301 return false;
1302}
1303
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001304bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001305 assert(Token.is(MIToken::StackObject));
1306 unsigned ID;
1307 if (getUnsigned(ID))
1308 return true;
1309 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1310 if (ObjectInfo == PFS.StackObjectSlots.end())
1311 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1312 "'");
1313 StringRef Name;
1314 if (const auto *Alloca =
Matthias Braun941a7052016-07-28 18:40:00 +00001315 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001316 Name = Alloca->getName();
1317 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1318 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1319 "' isn't '" + Token.stringValue() + "'");
1320 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001321 FI = ObjectInfo->second;
1322 return false;
1323}
1324
1325bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1326 int FI;
1327 if (parseStackFrameIndex(FI))
1328 return true;
1329 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001330 return false;
1331}
1332
Alex Lorenzea882122015-08-12 21:17:02 +00001333bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001334 assert(Token.is(MIToken::FixedStackObject));
1335 unsigned ID;
1336 if (getUnsigned(ID))
1337 return true;
1338 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1339 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1340 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1341 Twine(ID) + "'");
1342 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001343 FI = ObjectInfo->second;
1344 return false;
1345}
1346
1347bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1348 int FI;
1349 if (parseFixedStackFrameIndex(FI))
1350 return true;
1351 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001352 return false;
1353}
1354
Alex Lorenz41df7d32015-07-28 17:09:52 +00001355bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001356 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001357 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001358 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001359 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001360 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001361 return error(Twine("use of undefined global value '") + Token.range() +
1362 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001363 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001364 }
1365 case MIToken::GlobalValue: {
1366 unsigned GVIdx;
1367 if (getUnsigned(GVIdx))
1368 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001369 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001370 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1371 "'");
Matthias Braune35861d2016-07-13 23:27:50 +00001372 GV = PFS.IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001373 break;
1374 }
1375 default:
1376 llvm_unreachable("The current token should be a global value");
1377 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001378 return false;
1379}
1380
1381bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1382 GlobalValue *GV = nullptr;
1383 if (parseGlobalValue(GV))
1384 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001385 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001386 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001387 if (parseOperandsOffset(Dest))
1388 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001389 return false;
1390}
1391
Alex Lorenzab980492015-07-20 20:51:18 +00001392bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1393 assert(Token.is(MIToken::ConstantPoolItem));
1394 unsigned ID;
1395 if (getUnsigned(ID))
1396 return true;
1397 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1398 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1399 return error("use of undefined constant '%const." + Twine(ID) + "'");
1400 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001401 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001402 if (parseOperandsOffset(Dest))
1403 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001404 return false;
1405}
1406
Alex Lorenz31d70682015-07-15 23:38:35 +00001407bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1408 assert(Token.is(MIToken::JumpTableIndex));
1409 unsigned ID;
1410 if (getUnsigned(ID))
1411 return true;
1412 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1413 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1414 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1415 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001416 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1417 return false;
1418}
1419
Alex Lorenz6ede3742015-07-21 16:59:53 +00001420bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001421 assert(Token.is(MIToken::ExternalSymbol));
1422 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001423 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001424 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001425 if (parseOperandsOffset(Dest))
1426 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001427 return false;
1428}
1429
Matthias Braunb74eb412016-03-28 18:18:46 +00001430bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1431 assert(Token.is(MIToken::SubRegisterIndex));
1432 StringRef Name = Token.stringValue();
1433 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1434 if (SubRegIndex == 0)
1435 return error(Twine("unknown subregister index '") + Name + "'");
1436 lex();
1437 Dest = MachineOperand::CreateImm(SubRegIndex);
1438 return false;
1439}
1440
Alex Lorenz44f29252015-07-22 21:07:04 +00001441bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001442 assert(Token.is(MIToken::exclaim));
1443 auto Loc = Token.location();
1444 lex();
1445 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1446 return error("expected metadata id after '!'");
1447 unsigned ID;
1448 if (getUnsigned(ID))
1449 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001450 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1451 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
Alex Lorenz35e44462015-07-22 17:58:46 +00001452 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1453 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001454 Node = NodeInfo->second.get();
1455 return false;
1456}
1457
1458bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1459 MDNode *Node = nullptr;
1460 if (parseMDNode(Node))
1461 return true;
1462 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001463 return false;
1464}
1465
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001466bool MIParser::parseCFIOffset(int &Offset) {
1467 if (Token.isNot(MIToken::IntegerLiteral))
1468 return error("expected a cfi offset");
1469 if (Token.integerValue().getMinSignedBits() > 32)
1470 return error("expected a 32 bit integer (the cfi offset is too large)");
1471 Offset = (int)Token.integerValue().getExtValue();
1472 lex();
1473 return false;
1474}
1475
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001476bool MIParser::parseCFIRegister(unsigned &Reg) {
1477 if (Token.isNot(MIToken::NamedRegister))
1478 return error("expected a cfi register");
1479 unsigned LLVMReg;
Matthias Braun74ad41c2016-10-11 03:13:01 +00001480 if (parseNamedRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001481 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001482 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1483 assert(TRI && "Expected target register info");
1484 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1485 if (DwarfReg < 0)
1486 return error("invalid DWARF register");
1487 Reg = (unsigned)DwarfReg;
1488 lex();
1489 return false;
1490}
1491
1492bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1493 auto Kind = Token.kind();
1494 lex();
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001495 int Offset;
1496 unsigned Reg;
1497 unsigned CFIIndex;
1498 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001499 case MIToken::kw_cfi_same_value:
1500 if (parseCFIRegister(Reg))
1501 return true;
Matthias Braunf23ef432016-11-30 23:48:42 +00001502 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
Alex Lorenz577d2712015-08-14 21:55:58 +00001503 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001504 case MIToken::kw_cfi_offset:
1505 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1506 parseCFIOffset(Offset))
1507 return true;
1508 CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +00001509 MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001510 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001511 case MIToken::kw_cfi_def_cfa_register:
1512 if (parseCFIRegister(Reg))
1513 return true;
1514 CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +00001515 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001516 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001517 case MIToken::kw_cfi_def_cfa_offset:
1518 if (parseCFIOffset(Offset))
1519 return true;
1520 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
Matthias Braunf23ef432016-11-30 23:48:42 +00001521 CFIIndex = MF.addFrameInst(
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001522 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1523 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001524 case MIToken::kw_cfi_def_cfa:
1525 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1526 parseCFIOffset(Offset))
1527 return true;
1528 // NB: MCCFIInstruction::createDefCfa negates the offset.
1529 CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +00001530 MF.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
Alex Lorenzb1393232015-07-29 18:57:23 +00001531 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001532 default:
1533 // TODO: Parse the other CFI operands.
1534 llvm_unreachable("The current token should be a cfi operand");
1535 }
1536 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001537 return false;
1538}
1539
Alex Lorenzdeb53492015-07-28 17:28:03 +00001540bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1541 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001542 case MIToken::NamedIRBlock: {
1543 BB = dyn_cast_or_null<BasicBlock>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +00001544 F.getValueSymbolTable()->lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001545 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001546 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001547 break;
1548 }
1549 case MIToken::IRBlock: {
1550 unsigned SlotNumber = 0;
1551 if (getUnsigned(SlotNumber))
1552 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001553 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001554 if (!BB)
1555 return error(Twine("use of undefined IR block '%ir-block.") +
1556 Twine(SlotNumber) + "'");
1557 break;
1558 }
1559 default:
1560 llvm_unreachable("The current token should be an IR block reference");
1561 }
1562 return false;
1563}
1564
1565bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1566 assert(Token.is(MIToken::kw_blockaddress));
1567 lex();
1568 if (expectAndConsume(MIToken::lparen))
1569 return true;
1570 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001571 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001572 return error("expected a global value");
1573 GlobalValue *GV = nullptr;
1574 if (parseGlobalValue(GV))
1575 return true;
1576 auto *F = dyn_cast<Function>(GV);
1577 if (!F)
1578 return error("expected an IR function reference");
1579 lex();
1580 if (expectAndConsume(MIToken::comma))
1581 return true;
1582 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001583 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001584 return error("expected an IR block reference");
1585 if (parseIRBlock(BB, *F))
1586 return true;
1587 lex();
1588 if (expectAndConsume(MIToken::rparen))
1589 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001590 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001591 if (parseOperandsOffset(Dest))
1592 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001593 return false;
1594}
1595
Tim Northover6b3bd612016-07-29 20:32:59 +00001596bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
1597 assert(Token.is(MIToken::kw_intrinsic));
1598 lex();
1599 if (expectAndConsume(MIToken::lparen))
1600 return error("expected syntax intrinsic(@llvm.whatever)");
1601
1602 if (Token.isNot(MIToken::NamedGlobalValue))
1603 return error("expected syntax intrinsic(@llvm.whatever)");
1604
1605 std::string Name = Token.stringValue();
1606 lex();
1607
1608 if (expectAndConsume(MIToken::rparen))
1609 return error("expected ')' to terminate intrinsic name");
1610
1611 // Find out what intrinsic we're dealing with, first try the global namespace
1612 // and then the target's private intrinsics if that fails.
1613 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1614 Intrinsic::ID ID = Function::lookupIntrinsicID(Name);
1615 if (ID == Intrinsic::not_intrinsic && TII)
1616 ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
1617
1618 if (ID == Intrinsic::not_intrinsic)
1619 return error("unknown intrinsic name");
1620 Dest = MachineOperand::CreateIntrinsicID(ID);
1621
1622 return false;
1623}
1624
Tim Northoverde3aea0412016-08-17 20:25:25 +00001625bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
1626 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
1627 bool IsFloat = Token.is(MIToken::kw_floatpred);
1628 lex();
1629
1630 if (expectAndConsume(MIToken::lparen))
1631 return error("expected syntax intpred(whatever) or floatpred(whatever");
1632
1633 if (Token.isNot(MIToken::Identifier))
1634 return error("whatever");
1635
1636 CmpInst::Predicate Pred;
1637 if (IsFloat) {
1638 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1639 .Case("false", CmpInst::FCMP_FALSE)
1640 .Case("oeq", CmpInst::FCMP_OEQ)
1641 .Case("ogt", CmpInst::FCMP_OGT)
1642 .Case("oge", CmpInst::FCMP_OGE)
1643 .Case("olt", CmpInst::FCMP_OLT)
1644 .Case("ole", CmpInst::FCMP_OLE)
1645 .Case("one", CmpInst::FCMP_ONE)
1646 .Case("ord", CmpInst::FCMP_ORD)
1647 .Case("uno", CmpInst::FCMP_UNO)
1648 .Case("ueq", CmpInst::FCMP_UEQ)
1649 .Case("ugt", CmpInst::FCMP_UGT)
1650 .Case("uge", CmpInst::FCMP_UGE)
1651 .Case("ult", CmpInst::FCMP_ULT)
1652 .Case("ule", CmpInst::FCMP_ULE)
1653 .Case("une", CmpInst::FCMP_UNE)
1654 .Case("true", CmpInst::FCMP_TRUE)
1655 .Default(CmpInst::BAD_FCMP_PREDICATE);
1656 if (!CmpInst::isFPPredicate(Pred))
1657 return error("invalid floating-point predicate");
1658 } else {
1659 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1660 .Case("eq", CmpInst::ICMP_EQ)
1661 .Case("ne", CmpInst::ICMP_NE)
1662 .Case("sgt", CmpInst::ICMP_SGT)
1663 .Case("sge", CmpInst::ICMP_SGE)
1664 .Case("slt", CmpInst::ICMP_SLT)
1665 .Case("sle", CmpInst::ICMP_SLE)
1666 .Case("ugt", CmpInst::ICMP_UGT)
1667 .Case("uge", CmpInst::ICMP_UGE)
1668 .Case("ult", CmpInst::ICMP_ULT)
1669 .Case("ule", CmpInst::ICMP_ULE)
1670 .Default(CmpInst::BAD_ICMP_PREDICATE);
1671 if (!CmpInst::isIntPredicate(Pred))
1672 return error("invalid integer predicate");
1673 }
1674
1675 lex();
1676 Dest = MachineOperand::CreatePredicate(Pred);
Tim Northover6cd4b232016-08-23 21:01:26 +00001677 if (expectAndConsume(MIToken::rparen))
Tim Northoverde3aea0412016-08-17 20:25:25 +00001678 return error("predicate should be terminated by ')'.");
1679
1680 return false;
1681}
1682
Alex Lorenzef5c1962015-07-28 23:02:45 +00001683bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1684 assert(Token.is(MIToken::kw_target_index));
1685 lex();
1686 if (expectAndConsume(MIToken::lparen))
1687 return true;
1688 if (Token.isNot(MIToken::Identifier))
1689 return error("expected the name of the target index");
1690 int Index = 0;
1691 if (getTargetIndex(Token.stringValue(), Index))
1692 return error("use of undefined target index '" + Token.stringValue() + "'");
1693 lex();
1694 if (expectAndConsume(MIToken::rparen))
1695 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001696 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001697 if (parseOperandsOffset(Dest))
1698 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001699 return false;
1700}
1701
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +00001702bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
1703 assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
1704 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1705 assert(TRI && "Expected target register info");
1706 lex();
1707 if (expectAndConsume(MIToken::lparen))
1708 return true;
1709
1710 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1711 while (true) {
1712 if (Token.isNot(MIToken::NamedRegister))
1713 return error("expected a named register");
1714 unsigned Reg;
1715 if (parseNamedRegister(Reg))
1716 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
1725 if (expectAndConsume(MIToken::rparen))
1726 return true;
1727 Dest = MachineOperand::CreateRegMask(Mask);
1728 return false;
1729}
1730
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001731bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1732 assert(Token.is(MIToken::kw_liveout));
1733 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1734 assert(TRI && "Expected target register info");
1735 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1736 lex();
1737 if (expectAndConsume(MIToken::lparen))
1738 return true;
1739 while (true) {
1740 if (Token.isNot(MIToken::NamedRegister))
1741 return error("expected a named register");
Matthias Braun74ad41c2016-10-11 03:13:01 +00001742 unsigned Reg;
1743 if (parseNamedRegister(Reg))
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001744 return true;
1745 lex();
1746 Mask[Reg / 32] |= 1U << (Reg % 32);
1747 // TODO: Report an error if the same register is used more than once.
1748 if (Token.isNot(MIToken::comma))
1749 break;
1750 lex();
1751 }
1752 if (expectAndConsume(MIToken::rparen))
1753 return true;
1754 Dest = MachineOperand::CreateRegLiveOut(Mask);
1755 return false;
1756}
1757
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001758bool MIParser::parseMachineOperand(MachineOperand &Dest,
1759 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001760 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001761 case MIToken::kw_implicit:
1762 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001763 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001764 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001765 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001766 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001767 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001768 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001769 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001770 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001771 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001772 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001773 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001774 case MIToken::IntegerLiteral:
1775 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001776 case MIToken::IntegerType:
1777 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001778 case MIToken::kw_half:
1779 case MIToken::kw_float:
1780 case MIToken::kw_double:
1781 case MIToken::kw_x86_fp80:
1782 case MIToken::kw_fp128:
1783 case MIToken::kw_ppc_fp128:
1784 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001785 case MIToken::MachineBasicBlock:
1786 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001787 case MIToken::StackObject:
1788 return parseStackObjectOperand(Dest);
1789 case MIToken::FixedStackObject:
1790 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001791 case MIToken::GlobalValue:
1792 case MIToken::NamedGlobalValue:
1793 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001794 case MIToken::ConstantPoolItem:
1795 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001796 case MIToken::JumpTableIndex:
1797 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001798 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001799 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00001800 case MIToken::SubRegisterIndex:
1801 return parseSubRegisterIndexOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001802 case MIToken::exclaim:
1803 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001804 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001805 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001806 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001807 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001808 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001809 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001810 case MIToken::kw_blockaddress:
1811 return parseBlockAddressOperand(Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +00001812 case MIToken::kw_intrinsic:
1813 return parseIntrinsicOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001814 case MIToken::kw_target_index:
1815 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001816 case MIToken::kw_liveout:
1817 return parseLiveoutRegisterMaskOperand(Dest);
Tim Northoverde3aea0412016-08-17 20:25:25 +00001818 case MIToken::kw_floatpred:
1819 case MIToken::kw_intpred:
1820 return parsePredicateOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001821 case MIToken::Error:
1822 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001823 case MIToken::Identifier:
1824 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1825 Dest = MachineOperand::CreateRegMask(RegMask);
1826 lex();
1827 break;
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +00001828 } else
1829 return parseCustomRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001830 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001831 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001832 return error("expected a machine operand");
1833 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001834 return false;
1835}
1836
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001837bool MIParser::parseMachineOperandAndTargetFlags(
1838 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001839 unsigned TF = 0;
1840 bool HasTargetFlags = false;
1841 if (Token.is(MIToken::kw_target_flags)) {
1842 HasTargetFlags = true;
1843 lex();
1844 if (expectAndConsume(MIToken::lparen))
1845 return true;
1846 if (Token.isNot(MIToken::Identifier))
1847 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001848 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1849 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1850 return error("use of undefined target flag '" + Token.stringValue() +
1851 "'");
1852 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001853 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001854 while (Token.is(MIToken::comma)) {
1855 lex();
1856 if (Token.isNot(MIToken::Identifier))
1857 return error("expected the name of the target flag");
1858 unsigned BitFlag = 0;
1859 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1860 return error("use of undefined target flag '" + Token.stringValue() +
1861 "'");
1862 // TODO: Report an error when using a duplicate bit target flag.
1863 TF |= BitFlag;
1864 lex();
1865 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001866 if (expectAndConsume(MIToken::rparen))
1867 return true;
1868 }
1869 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001870 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001871 return true;
1872 if (!HasTargetFlags)
1873 return false;
1874 if (Dest.isReg())
1875 return error(Loc, "register operands can't have target flags");
1876 Dest.setTargetFlags(TF);
1877 return false;
1878}
1879
Alex Lorenzdc24c172015-08-07 20:21:00 +00001880bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001881 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1882 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001883 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001884 bool IsNegative = Token.is(MIToken::minus);
1885 lex();
1886 if (Token.isNot(MIToken::IntegerLiteral))
1887 return error("expected an integer literal after '" + Sign + "'");
1888 if (Token.integerValue().getMinSignedBits() > 64)
1889 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001890 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001891 if (IsNegative)
1892 Offset = -Offset;
1893 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001894 return false;
1895}
1896
Alex Lorenz620f8912015-08-13 20:33:33 +00001897bool MIParser::parseAlignment(unsigned &Alignment) {
1898 assert(Token.is(MIToken::kw_align));
1899 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001900 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001901 return error("expected an integer literal after 'align'");
1902 if (getUnsigned(Alignment))
1903 return true;
1904 lex();
1905 return false;
1906}
1907
Alex Lorenzdc24c172015-08-07 20:21:00 +00001908bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1909 int64_t Offset = 0;
1910 if (parseOffset(Offset))
1911 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001912 Op.setOffset(Offset);
1913 return false;
1914}
1915
Alex Lorenz36593ac2015-08-19 23:27:07 +00001916bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001917 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001918 case MIToken::NamedIRValue: {
Mehdi Aminia53d49e2016-09-17 06:00:02 +00001919 V = MF.getFunction()->getValueSymbolTable()->lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001920 break;
1921 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001922 case MIToken::IRValue: {
1923 unsigned SlotNumber = 0;
1924 if (getUnsigned(SlotNumber))
1925 return true;
1926 V = getIRValue(SlotNumber);
1927 break;
1928 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001929 case MIToken::NamedGlobalValue:
1930 case MIToken::GlobalValue: {
1931 GlobalValue *GV = nullptr;
1932 if (parseGlobalValue(GV))
1933 return true;
1934 V = GV;
1935 break;
1936 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001937 case MIToken::QuotedIRValue: {
1938 const Constant *C = nullptr;
1939 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1940 return true;
1941 V = C;
1942 break;
1943 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001944 default:
1945 llvm_unreachable("The current token should be an IR block reference");
1946 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001947 if (!V)
1948 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001949 return false;
1950}
1951
1952bool MIParser::getUint64(uint64_t &Result) {
Krzysztof Parzyszek36ef5dc2016-12-16 13:58:01 +00001953 if (Token.hasIntegerValue()) {
1954 if (Token.integerValue().getActiveBits() > 64)
1955 return error("expected 64-bit integer (too large)");
1956 Result = Token.integerValue().getZExtValue();
1957 return false;
1958 }
1959 if (Token.is(MIToken::HexLiteral)) {
1960 APInt A;
1961 if (getHexUint(A))
1962 return true;
1963 if (A.getBitWidth() > 64)
1964 return error("expected 64-bit integer (too large)");
1965 Result = A.getZExtValue();
1966 return false;
1967 }
1968 return true;
1969}
1970
1971bool MIParser::getHexUint(APInt &Result) {
1972 assert(Token.is(MIToken::HexLiteral));
1973 StringRef S = Token.range();
1974 assert(S[0] == '0' && tolower(S[1]) == 'x');
1975 // This could be a floating point literal with a special prefix.
1976 if (!isxdigit(S[2]))
1977 return true;
1978 StringRef V = S.substr(2);
1979 APInt A(V.size()*4, V, 16);
Renato Golin4abfb3d2017-04-23 12:15:30 +00001980 Result = APInt(A.getActiveBits(),
1981 ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
Alex Lorenz4af7e612015-08-03 23:08:19 +00001982 return false;
1983}
1984
Justin Lebar0af80cd2016-07-15 18:26:59 +00001985bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
1986 const auto OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001987 switch (Token.kind()) {
1988 case MIToken::kw_volatile:
1989 Flags |= MachineMemOperand::MOVolatile;
1990 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001991 case MIToken::kw_non_temporal:
1992 Flags |= MachineMemOperand::MONonTemporal;
1993 break;
Justin Lebaradbf09e2016-09-11 01:38:58 +00001994 case MIToken::kw_dereferenceable:
1995 Flags |= MachineMemOperand::MODereferenceable;
1996 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001997 case MIToken::kw_invariant:
1998 Flags |= MachineMemOperand::MOInvariant;
1999 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00002000 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00002001 default:
2002 llvm_unreachable("The current token should be a memory operand flag");
2003 }
Alex Lorenze86d5152015-08-06 18:26:36 +00002004 if (OldFlags == Flags)
2005 // We know that the same flag is specified more than once when the flags
2006 // weren't modified.
2007 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00002008 lex();
2009 return false;
2010}
2011
Alex Lorenz91097a32015-08-12 20:33:26 +00002012bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
2013 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00002014 case MIToken::kw_stack:
2015 PSV = MF.getPSVManager().getStack();
2016 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00002017 case MIToken::kw_got:
2018 PSV = MF.getPSVManager().getGOT();
2019 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00002020 case MIToken::kw_jump_table:
2021 PSV = MF.getPSVManager().getJumpTable();
2022 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00002023 case MIToken::kw_constant_pool:
2024 PSV = MF.getPSVManager().getConstantPool();
2025 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00002026 case MIToken::FixedStackObject: {
2027 int FI;
2028 if (parseFixedStackFrameIndex(FI))
2029 return true;
2030 PSV = MF.getPSVManager().getFixedStack(FI);
2031 // The token was already consumed, so use return here instead of break.
2032 return false;
2033 }
Matthias Braun3ef7df92016-06-08 00:47:07 +00002034 case MIToken::StackObject: {
2035 int FI;
2036 if (parseStackFrameIndex(FI))
2037 return true;
2038 PSV = MF.getPSVManager().getFixedStack(FI);
2039 // The token was already consumed, so use return here instead of break.
2040 return false;
2041 }
Alex Lorenz0d009642015-08-20 00:12:57 +00002042 case MIToken::kw_call_entry: {
2043 lex();
2044 switch (Token.kind()) {
2045 case MIToken::GlobalValue:
2046 case MIToken::NamedGlobalValue: {
2047 GlobalValue *GV = nullptr;
2048 if (parseGlobalValue(GV))
2049 return true;
2050 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
2051 break;
2052 }
2053 case MIToken::ExternalSymbol:
2054 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
2055 MF.createExternalSymbolName(Token.stringValue()));
2056 break;
2057 default:
2058 return error(
2059 "expected a global value or an external symbol after 'call-entry'");
2060 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00002061 break;
2062 }
Alex Lorenz91097a32015-08-12 20:33:26 +00002063 default:
2064 llvm_unreachable("The current token should be pseudo source value");
2065 }
2066 lex();
2067 return false;
2068}
2069
2070bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00002071 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00002072 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Matthias Braun3ef7df92016-06-08 00:47:07 +00002073 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
2074 Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00002075 const PseudoSourceValue *PSV = nullptr;
2076 if (parseMemoryPseudoSourceValue(PSV))
2077 return true;
2078 int64_t Offset = 0;
2079 if (parseOffset(Offset))
2080 return true;
2081 Dest = MachinePointerInfo(PSV, Offset);
2082 return false;
2083 }
Alex Lorenz36efd382015-08-20 00:20:03 +00002084 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
2085 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00002086 Token.isNot(MIToken::NamedGlobalValue) &&
2087 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00002088 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00002089 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00002090 if (parseIRValue(V))
2091 return true;
2092 if (!V->getType()->isPointerTy())
2093 return error("expected a pointer IR value");
2094 lex();
2095 int64_t Offset = 0;
2096 if (parseOffset(Offset))
2097 return true;
2098 Dest = MachinePointerInfo(V, Offset);
2099 return false;
2100}
2101
Tim Northoverb73e3092017-02-13 22:14:08 +00002102bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
2103 Order = AtomicOrdering::NotAtomic;
2104 if (Token.isNot(MIToken::Identifier))
2105 return false;
2106
2107 Order = StringSwitch<AtomicOrdering>(Token.stringValue())
2108 .Case("unordered", AtomicOrdering::Unordered)
2109 .Case("monotonic", AtomicOrdering::Monotonic)
2110 .Case("acquire", AtomicOrdering::Acquire)
2111 .Case("release", AtomicOrdering::Release)
2112 .Case("acq_rel", AtomicOrdering::AcquireRelease)
2113 .Case("seq_cst", AtomicOrdering::SequentiallyConsistent)
2114 .Default(AtomicOrdering::NotAtomic);
2115
2116 if (Order != AtomicOrdering::NotAtomic) {
2117 lex();
2118 return false;
2119 }
2120
2121 return error("expected an atomic scope, ordering or a size integer literal");
2122}
2123
Alex Lorenz4af7e612015-08-03 23:08:19 +00002124bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
2125 if (expectAndConsume(MIToken::lparen))
2126 return true;
Justin Lebar0af80cd2016-07-15 18:26:59 +00002127 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
Alex Lorenza518b792015-08-04 00:24:45 +00002128 while (Token.isMemoryOperandFlag()) {
2129 if (parseMemoryOperandFlag(Flags))
2130 return true;
2131 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00002132 if (Token.isNot(MIToken::Identifier) ||
2133 (Token.stringValue() != "load" && Token.stringValue() != "store"))
2134 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00002135 if (Token.stringValue() == "load")
2136 Flags |= MachineMemOperand::MOLoad;
2137 else
2138 Flags |= MachineMemOperand::MOStore;
2139 lex();
2140
Tim Northoverb73e3092017-02-13 22:14:08 +00002141 // Optional "singlethread" scope.
2142 SynchronizationScope Scope = SynchronizationScope::CrossThread;
2143 if (Token.is(MIToken::Identifier) && Token.stringValue() == "singlethread") {
2144 Scope = SynchronizationScope::SingleThread;
2145 lex();
2146 }
2147
2148 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
2149 AtomicOrdering Order, FailureOrder;
2150 if (parseOptionalAtomicOrdering(Order))
2151 return true;
2152
2153 if (parseOptionalAtomicOrdering(FailureOrder))
2154 return true;
2155
Alex Lorenz4af7e612015-08-03 23:08:19 +00002156 if (Token.isNot(MIToken::IntegerLiteral))
2157 return error("expected the size integer literal after memory operation");
2158 uint64_t Size;
2159 if (getUint64(Size))
2160 return true;
2161 lex();
2162
Alex Lorenz91097a32015-08-12 20:33:26 +00002163 MachinePointerInfo Ptr = MachinePointerInfo();
Matthias Braunc25c9cc2016-06-04 00:06:31 +00002164 if (Token.is(MIToken::Identifier)) {
2165 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
2166 if (Token.stringValue() != Word)
2167 return error(Twine("expected '") + Word + "'");
2168 lex();
2169
2170 if (parseMachinePointerInfo(Ptr))
2171 return true;
2172 }
Alex Lorenz61420f72015-08-07 20:48:30 +00002173 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00002174 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00002175 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00002176 while (consumeIfPresent(MIToken::comma)) {
2177 switch (Token.kind()) {
2178 case MIToken::kw_align:
2179 if (parseAlignment(BaseAlignment))
2180 return true;
2181 break;
2182 case MIToken::md_tbaa:
2183 lex();
2184 if (parseMDNode(AAInfo.TBAA))
2185 return true;
2186 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00002187 case MIToken::md_alias_scope:
2188 lex();
2189 if (parseMDNode(AAInfo.Scope))
2190 return true;
2191 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00002192 case MIToken::md_noalias:
2193 lex();
2194 if (parseMDNode(AAInfo.NoAlias))
2195 return true;
2196 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00002197 case MIToken::md_range:
2198 lex();
2199 if (parseMDNode(Range))
2200 return true;
2201 break;
Alex Lorenza617c912015-08-17 22:05:15 +00002202 // TODO: Report an error on duplicate metadata nodes.
2203 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00002204 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
2205 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00002206 }
Alex Lorenz61420f72015-08-07 20:48:30 +00002207 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00002208 if (expectAndConsume(MIToken::rparen))
2209 return true;
Tim Northoverb73e3092017-02-13 22:14:08 +00002210 Dest = MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range,
2211 Scope, Order, FailureOrder);
Alex Lorenz4af7e612015-08-03 23:08:19 +00002212 return false;
2213}
2214
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002215void MIParser::initNames2InstrOpCodes() {
2216 if (!Names2InstrOpCodes.empty())
2217 return;
2218 const auto *TII = MF.getSubtarget().getInstrInfo();
2219 assert(TII && "Expected target instruction info");
2220 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
2221 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
2222}
2223
2224bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
2225 initNames2InstrOpCodes();
2226 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
2227 if (InstrInfo == Names2InstrOpCodes.end())
2228 return true;
2229 OpCode = InstrInfo->getValue();
2230 return false;
2231}
2232
Alex Lorenzf3db51de2015-06-23 16:35:26 +00002233void MIParser::initNames2Regs() {
2234 if (!Names2Regs.empty())
2235 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00002236 // The '%noreg' register is the register 0.
2237 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00002238 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2239 assert(TRI && "Expected target register info");
2240 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
2241 bool WasInserted =
2242 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
2243 .second;
2244 (void)WasInserted;
2245 assert(WasInserted && "Expected registers to be unique case-insensitively");
2246 }
2247}
2248
2249bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
2250 initNames2Regs();
2251 auto RegInfo = Names2Regs.find(RegName);
2252 if (RegInfo == Names2Regs.end())
2253 return true;
2254 Reg = RegInfo->getValue();
2255 return false;
2256}
2257
Alex Lorenz8f6f4282015-06-29 16:57:06 +00002258void MIParser::initNames2RegMasks() {
2259 if (!Names2RegMasks.empty())
2260 return;
2261 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2262 assert(TRI && "Expected target register info");
2263 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
2264 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
2265 assert(RegMasks.size() == RegMaskNames.size());
2266 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
2267 Names2RegMasks.insert(
2268 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
2269}
2270
2271const uint32_t *MIParser::getRegMask(StringRef Identifier) {
2272 initNames2RegMasks();
2273 auto RegMaskInfo = Names2RegMasks.find(Identifier);
2274 if (RegMaskInfo == Names2RegMasks.end())
2275 return nullptr;
2276 return RegMaskInfo->getValue();
2277}
2278
Alex Lorenz2eacca82015-07-13 23:24:34 +00002279void MIParser::initNames2SubRegIndices() {
2280 if (!Names2SubRegIndices.empty())
2281 return;
2282 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2283 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
2284 Names2SubRegIndices.insert(
2285 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
2286}
2287
2288unsigned MIParser::getSubRegIndex(StringRef Name) {
2289 initNames2SubRegIndices();
2290 auto SubRegInfo = Names2SubRegIndices.find(Name);
2291 if (SubRegInfo == Names2SubRegIndices.end())
2292 return 0;
2293 return SubRegInfo->getValue();
2294}
2295
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002296static void initSlots2BasicBlocks(
2297 const Function &F,
2298 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2299 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002300 MST.incorporateFunction(F);
2301 for (auto &BB : F) {
2302 if (BB.hasName())
2303 continue;
2304 int Slot = MST.getLocalSlot(&BB);
2305 if (Slot == -1)
2306 continue;
2307 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2308 }
2309}
2310
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002311static const BasicBlock *getIRBlockFromSlot(
2312 unsigned Slot,
2313 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002314 auto BlockInfo = Slots2BasicBlocks.find(Slot);
2315 if (BlockInfo == Slots2BasicBlocks.end())
2316 return nullptr;
2317 return BlockInfo->second;
2318}
2319
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002320const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2321 if (Slots2BasicBlocks.empty())
2322 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
2323 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2324}
2325
2326const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2327 if (&F == MF.getFunction())
2328 return getIRBlock(Slot);
2329 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2330 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2331 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2332}
2333
Alex Lorenzdd13be02015-08-19 23:31:05 +00002334static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2335 DenseMap<unsigned, const Value *> &Slots2Values) {
2336 int Slot = MST.getLocalSlot(V);
2337 if (Slot == -1)
2338 return;
2339 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2340}
2341
2342/// Creates the mapping from slot numbers to function's unnamed IR values.
2343static void initSlots2Values(const Function &F,
2344 DenseMap<unsigned, const Value *> &Slots2Values) {
2345 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2346 MST.incorporateFunction(F);
2347 for (const auto &Arg : F.args())
2348 mapValueToSlot(&Arg, MST, Slots2Values);
2349 for (const auto &BB : F) {
2350 mapValueToSlot(&BB, MST, Slots2Values);
2351 for (const auto &I : BB)
2352 mapValueToSlot(&I, MST, Slots2Values);
2353 }
2354}
2355
2356const Value *MIParser::getIRValue(unsigned Slot) {
2357 if (Slots2Values.empty())
2358 initSlots2Values(*MF.getFunction(), Slots2Values);
2359 auto ValueInfo = Slots2Values.find(Slot);
2360 if (ValueInfo == Slots2Values.end())
2361 return nullptr;
2362 return ValueInfo->second;
2363}
2364
Alex Lorenzef5c1962015-07-28 23:02:45 +00002365void MIParser::initNames2TargetIndices() {
2366 if (!Names2TargetIndices.empty())
2367 return;
2368 const auto *TII = MF.getSubtarget().getInstrInfo();
2369 assert(TII && "Expected target instruction info");
2370 auto Indices = TII->getSerializableTargetIndices();
2371 for (const auto &I : Indices)
2372 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2373}
2374
2375bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2376 initNames2TargetIndices();
2377 auto IndexInfo = Names2TargetIndices.find(Name);
2378 if (IndexInfo == Names2TargetIndices.end())
2379 return true;
2380 Index = IndexInfo->second;
2381 return false;
2382}
2383
Alex Lorenz49873a82015-08-06 00:44:07 +00002384void MIParser::initNames2DirectTargetFlags() {
2385 if (!Names2DirectTargetFlags.empty())
2386 return;
2387 const auto *TII = MF.getSubtarget().getInstrInfo();
2388 assert(TII && "Expected target instruction info");
2389 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2390 for (const auto &I : Flags)
2391 Names2DirectTargetFlags.insert(
2392 std::make_pair(StringRef(I.second), I.first));
2393}
2394
2395bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2396 initNames2DirectTargetFlags();
2397 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2398 if (FlagInfo == Names2DirectTargetFlags.end())
2399 return true;
2400 Flag = FlagInfo->second;
2401 return false;
2402}
2403
Alex Lorenzf3630112015-08-18 22:52:15 +00002404void MIParser::initNames2BitmaskTargetFlags() {
2405 if (!Names2BitmaskTargetFlags.empty())
2406 return;
2407 const auto *TII = MF.getSubtarget().getInstrInfo();
2408 assert(TII && "Expected target instruction info");
2409 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2410 for (const auto &I : Flags)
2411 Names2BitmaskTargetFlags.insert(
2412 std::make_pair(StringRef(I.second), I.first));
2413}
2414
2415bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2416 initNames2BitmaskTargetFlags();
2417 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2418 if (FlagInfo == Names2BitmaskTargetFlags.end())
2419 return true;
2420 Flag = FlagInfo->second;
2421 return false;
2422}
2423
Matthias Braun83947862016-07-13 22:23:23 +00002424bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2425 StringRef Src,
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002426 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002427 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002428}
2429
Matthias Braun74ad41c2016-10-11 03:13:01 +00002430bool llvm::parseMachineInstructions(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002431 StringRef Src, SMDiagnostic &Error) {
2432 return MIParser(PFS, Error, Src).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002433}
Alex Lorenzf09df002015-06-30 18:16:42 +00002434
Matthias Braun74ad41c2016-10-11 03:13:01 +00002435bool llvm::parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002436 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +00002437 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002438 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002439}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002440
Tom Stellard9c884e42016-11-15 00:03:14 +00002441bool llvm::parseRegisterReference(PerFunctionMIParsingState &PFS,
2442 unsigned &Reg, StringRef Src,
2443 SMDiagnostic &Error) {
2444 return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
2445}
2446
Matthias Braun74ad41c2016-10-11 03:13:01 +00002447bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002448 unsigned &Reg, StringRef Src,
Alex Lorenz9fab3702015-07-14 21:24:41 +00002449 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002450 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002451}
Alex Lorenz12045a42015-07-27 17:42:45 +00002452
Matthias Braun74ad41c2016-10-11 03:13:01 +00002453bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState &PFS,
2454 VRegInfo *&Info, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +00002455 SMDiagnostic &Error) {
Matthias Braun74ad41c2016-10-11 03:13:01 +00002456 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
Alex Lorenz12045a42015-07-27 17:42:45 +00002457}
Alex Lorenza314d812015-08-18 22:26:26 +00002458
Matthias Braun74ad41c2016-10-11 03:13:01 +00002459bool llvm::parseStackObjectReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002460 int &FI, StringRef Src,
Alex Lorenza314d812015-08-18 22:26:26 +00002461 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002462 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
Alex Lorenza314d812015-08-18 22:26:26 +00002463}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002464
Matthias Braun74ad41c2016-10-11 03:13:01 +00002465bool llvm::parseMDNode(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002466 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2467 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002468}