blob: eb8832a92dca737743d8e2ed23510cf389011c0b [file] [log] [blame]
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001//===- MIParser.cpp - Machine instructions parser implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the parsing of machine instructions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MIParser.h"
Alex Lorenz91370c52015-06-22 20:37:46 +000015#include "MILexer.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000016#include "llvm/ADT/StringMap.h"
Reid Klecknera5b1eef2016-08-26 17:58:37 +000017#include "llvm/ADT/StringSwitch.h"
Alex Lorenzad156fb2015-07-31 20:49:21 +000018#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000019#include "llvm/AsmParser/SlotMapping.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000020#include "llvm/CodeGen/MachineBasicBlock.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000022#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000023#include "llvm/CodeGen/MachineInstr.h"
Alex Lorenzcb268d42015-07-06 23:07:26 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000025#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000028#include "llvm/IR/Constants.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000029#include "llvm/IR/Instructions.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000030#include "llvm/IR/Intrinsics.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000031#include "llvm/IR/Module.h"
Alex Lorenz8a1915b2015-07-27 22:42:41 +000032#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000033#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000034#include "llvm/Support/SourceMgr.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000035#include "llvm/Support/raw_ostream.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000036#include "llvm/Target/TargetInstrInfo.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000037#include "llvm/Target/TargetIntrinsicInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000038#include "llvm/Target/TargetSubtargetInfo.h"
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +000039#include <cctype>
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000040
41using namespace llvm;
42
Matthias Braune35861d2016-07-13 23:27:50 +000043PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
44 SourceMgr &SM, const SlotMapping &IRSlots)
45 : MF(MF), SM(&SM), IRSlots(IRSlots) {
Matthias Braun83947862016-07-13 22:23:23 +000046}
47
Matthias Braun74ad41c2016-10-11 03:13:01 +000048VRegInfo &PerFunctionMIParsingState::getVRegInfo(unsigned Num) {
49 auto I = VRegInfos.insert(std::make_pair(Num, nullptr));
50 if (I.second) {
51 MachineRegisterInfo &MRI = MF.getRegInfo();
52 VRegInfo *Info = new (Allocator) VRegInfo;
53 Info->VReg = MRI.createIncompleteVirtualRegister();
54 I.first->second = Info;
55 }
56 return *I.first->second;
57}
58
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000059namespace {
60
Alex Lorenz36962cd2015-07-07 02:08:46 +000061/// A wrapper struct around the 'MachineOperand' struct that includes a source
Alex Lorenzfeb6b432015-08-19 19:19:16 +000062/// range and other attributes.
63struct ParsedMachineOperand {
Alex Lorenz36962cd2015-07-07 02:08:46 +000064 MachineOperand Operand;
65 StringRef::iterator Begin;
66 StringRef::iterator End;
Alex Lorenz5ef93b02015-08-19 19:05:34 +000067 Optional<unsigned> TiedDefIdx;
Alex Lorenz36962cd2015-07-07 02:08:46 +000068
Alex Lorenzfeb6b432015-08-19 19:19:16 +000069 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
70 StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
Alex Lorenz5ef93b02015-08-19 19:05:34 +000071 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
72 if (TiedDefIdx)
73 assert(Operand.isReg() && Operand.isUse() &&
74 "Only used register operands can be tied");
75 }
Alex Lorenz36962cd2015-07-07 02:08:46 +000076};
77
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000078class MIParser {
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000079 MachineFunction &MF;
80 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000081 StringRef Source, CurrentSource;
82 MIToken Token;
Matthias Braun74ad41c2016-10-11 03:13:01 +000083 PerFunctionMIParsingState &PFS;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000084 /// Maps from instruction names to op codes.
85 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000086 /// Maps from register names to registers.
87 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000088 /// Maps from register mask names to register masks.
89 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000090 /// Maps from subregister names to subregister indices.
91 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000092 /// Maps from slot numbers to function's unnamed basic blocks.
93 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzdd13be02015-08-19 23:31:05 +000094 /// Maps from slot numbers to function's unnamed values.
95 DenseMap<unsigned, const Value *> Slots2Values;
Alex Lorenzef5c1962015-07-28 23:02:45 +000096 /// Maps from target index names to target indices.
97 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +000098 /// Maps from direct target flag names to the direct target flag values.
99 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenzf3630112015-08-18 22:52:15 +0000100 /// Maps from direct target flag names to the bitmask target flag values.
101 StringMap<unsigned> Names2BitmaskTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000102
103public:
Matthias Braun74ad41c2016-10-11 03:13:01 +0000104 MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
Matthias Braune35861d2016-07-13 23:27:50 +0000105 StringRef Source);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000106
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000107 /// \p SkipChar gives the number of characters to skip before looking
108 /// for the next token.
109 void lex(unsigned SkipChar = 0);
Alex Lorenz91370c52015-06-22 20:37:46 +0000110
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000111 /// Report an error at the current location with the given message.
112 ///
113 /// This function always return true.
114 bool error(const Twine &Msg);
115
Alex Lorenz91370c52015-06-22 20:37:46 +0000116 /// Report an error at the given location with the given message.
117 ///
118 /// This function always return true.
119 bool error(StringRef::iterator Loc, const Twine &Msg);
120
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000121 bool
122 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
123 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +0000124 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +0000125 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
126 bool parseStandaloneNamedRegister(unsigned &Reg);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000127 bool parseStandaloneVirtualRegister(VRegInfo *&Info);
Alex Lorenza314d812015-08-18 22:26:26 +0000128 bool parseStandaloneStackObject(int &FI);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000129 bool parseStandaloneMDNode(MDNode *&Node);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000130
131 bool
132 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
133 bool parseBasicBlock(MachineBasicBlock &MBB);
134 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
135 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000136
Matthias Braun74ad41c2016-10-11 03:13:01 +0000137 bool parseNamedRegister(unsigned &Reg);
138 bool parseVirtualRegister(VRegInfo *&Info);
139 bool parseRegister(unsigned &Reg, VRegInfo *&VRegInfo);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000140 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000141 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000142 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
143 bool parseRegisterOperand(MachineOperand &Dest,
144 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000145 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000146 bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
147 const Constant *&C);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000148 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Ahmed Bougachad760de02016-07-28 17:15:12 +0000149 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
Alex Lorenz05e38822015-08-05 18:52:21 +0000150 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000151 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000152 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000153 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000154 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000155 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000156 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000157 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000158 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000159 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000160 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +0000161 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000162 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000163 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000164 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000165 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000166 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000167 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000168 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000169 bool parseIRBlock(BasicBlock *&BB, const Function &F);
170 bool parseBlockAddressOperand(MachineOperand &Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +0000171 bool parseIntrinsicOperand(MachineOperand &Dest);
Tim Northoverde3aea0412016-08-17 20:25:25 +0000172 bool parsePredicateOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000173 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000174 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000175 bool parseMachineOperand(MachineOperand &Dest,
176 Optional<unsigned> &TiedDefIdx);
177 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
178 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000179 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000180 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000181 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000182 bool parseIRValue(const Value *&V);
Justin Lebar0af80cd2016-07-15 18:26:59 +0000183 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000184 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
185 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000186 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000187
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000188private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000189 /// Convert the integer literal in the current token into an unsigned integer.
190 ///
191 /// Return true if an error occurred.
192 bool getUnsigned(unsigned &Result);
193
Alex Lorenz4af7e612015-08-03 23:08:19 +0000194 /// Convert the integer literal in the current token into an uint64.
195 ///
196 /// Return true if an error occurred.
197 bool getUint64(uint64_t &Result);
198
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000199 /// If the current token is of the given kind, consume it and return false.
200 /// Otherwise report an error and return true.
201 bool expectAndConsume(MIToken::TokenKind TokenKind);
202
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000203 /// If the current token is of the given kind, consume it and return true.
204 /// Otherwise return false.
205 bool consumeIfPresent(MIToken::TokenKind TokenKind);
206
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000207 void initNames2InstrOpCodes();
208
209 /// Try to convert an instruction name to an opcode. Return true if the
210 /// instruction name is invalid.
211 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000212
Alex Lorenze5a44662015-07-17 00:24:15 +0000213 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000214
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000215 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000216 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000217
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000218 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000219 const MCInstrDesc &MCID);
220
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000221 void initNames2Regs();
222
223 /// Try to convert a register name to a register number. Return true if the
224 /// register name is invalid.
225 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000226
227 void initNames2RegMasks();
228
229 /// Check if the given identifier is a name of a register mask.
230 ///
231 /// Return null if the identifier isn't a register mask.
232 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000233
234 void initNames2SubRegIndices();
235
236 /// Check if the given identifier is a name of a subregister index.
237 ///
238 /// Return 0 if the name isn't a subregister index class.
239 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000240
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000241 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000242 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000243
Alex Lorenzdd13be02015-08-19 23:31:05 +0000244 const Value *getIRValue(unsigned Slot);
245
Alex Lorenzef5c1962015-07-28 23:02:45 +0000246 void initNames2TargetIndices();
247
248 /// Try to convert a name of target index to the corresponding target index.
249 ///
250 /// Return true if the name isn't a name of a target index.
251 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000252
253 void initNames2DirectTargetFlags();
254
255 /// Try to convert a name of a direct target flag to the corresponding
256 /// target flag.
257 ///
258 /// Return true if the name isn't a name of a direct flag.
259 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000260
261 void initNames2BitmaskTargetFlags();
262
263 /// Try to convert a name of a bitmask target flag to the corresponding
264 /// target flag.
265 ///
266 /// Return true if the name isn't a name of a bitmask target flag.
267 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000268};
269
270} // end anonymous namespace
271
Matthias Braun74ad41c2016-10-11 03:13:01 +0000272MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
Matthias Braune35861d2016-07-13 23:27:50 +0000273 StringRef Source)
274 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
275{}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000276
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000277void MIParser::lex(unsigned SkipChar) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000278 CurrentSource = lexMIToken(
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000279 CurrentSource.data() + SkipChar, Token,
Alex Lorenz91370c52015-06-22 20:37:46 +0000280 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
281}
282
283bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
284
285bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Matthias Braune35861d2016-07-13 23:27:50 +0000286 const SourceMgr &SM = *PFS.SM;
Alex Lorenz91370c52015-06-22 20:37:46 +0000287 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000288 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
289 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
290 // Create an ordinary diagnostic when the source manager's buffer is the
291 // source string.
292 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
293 return true;
294 }
295 // Create a diagnostic for a YAML string literal.
296 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
297 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
298 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000299 return true;
300}
301
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000302static const char *toString(MIToken::TokenKind TokenKind) {
303 switch (TokenKind) {
304 case MIToken::comma:
305 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000306 case MIToken::equal:
307 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000308 case MIToken::colon:
309 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000310 case MIToken::lparen:
311 return "'('";
312 case MIToken::rparen:
313 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000314 default:
315 return "<unknown token>";
316 }
317}
318
319bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
320 if (Token.isNot(TokenKind))
321 return error(Twine("expected ") + toString(TokenKind));
322 lex();
323 return false;
324}
325
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000326bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
327 if (Token.isNot(TokenKind))
328 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000329 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000330 return true;
331}
Alex Lorenz91370c52015-06-22 20:37:46 +0000332
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000333bool MIParser::parseBasicBlockDefinition(
334 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
335 assert(Token.is(MIToken::MachineBasicBlockLabel));
336 unsigned ID = 0;
337 if (getUnsigned(ID))
338 return true;
339 auto Loc = Token.location();
340 auto Name = Token.stringValue();
341 lex();
342 bool HasAddressTaken = false;
343 bool IsLandingPad = false;
344 unsigned Alignment = 0;
345 BasicBlock *BB = nullptr;
346 if (consumeIfPresent(MIToken::lparen)) {
347 do {
348 // TODO: Report an error when multiple same attributes are specified.
349 switch (Token.kind()) {
350 case MIToken::kw_address_taken:
351 HasAddressTaken = true;
352 lex();
353 break;
354 case MIToken::kw_landing_pad:
355 IsLandingPad = true;
356 lex();
357 break;
358 case MIToken::kw_align:
359 if (parseAlignment(Alignment))
360 return true;
361 break;
362 case MIToken::IRBlock:
363 // TODO: Report an error when both name and ir block are specified.
364 if (parseIRBlock(BB, *MF.getFunction()))
365 return true;
366 lex();
367 break;
368 default:
369 break;
370 }
371 } while (consumeIfPresent(MIToken::comma));
372 if (expectAndConsume(MIToken::rparen))
373 return true;
374 }
375 if (expectAndConsume(MIToken::colon))
376 return true;
377
378 if (!Name.empty()) {
379 BB = dyn_cast_or_null<BasicBlock>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000380 MF.getFunction()->getValueSymbolTable()->lookup(Name));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000381 if (!BB)
382 return error(Loc, Twine("basic block '") + Name +
383 "' is not defined in the function '" +
384 MF.getName() + "'");
385 }
386 auto *MBB = MF.CreateMachineBasicBlock(BB);
387 MF.insert(MF.end(), MBB);
388 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
389 if (!WasInserted)
390 return error(Loc, Twine("redefinition of machine basic block with id #") +
391 Twine(ID));
392 if (Alignment)
393 MBB->setAlignment(Alignment);
394 if (HasAddressTaken)
395 MBB->setHasAddressTaken();
Reid Kleckner0e288232015-08-27 23:27:47 +0000396 MBB->setIsEHPad(IsLandingPad);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000397 return false;
398}
399
400bool MIParser::parseBasicBlockDefinitions(
401 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
402 lex();
403 // Skip until the first machine basic block.
404 while (Token.is(MIToken::Newline))
405 lex();
406 if (Token.isErrorOrEOF())
407 return Token.isError();
408 if (Token.isNot(MIToken::MachineBasicBlockLabel))
409 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000410 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000411 do {
412 if (parseBasicBlockDefinition(MBBSlots))
413 return true;
414 bool IsAfterNewline = false;
415 // Skip until the next machine basic block.
416 while (true) {
417 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
418 Token.isErrorOrEOF())
419 break;
420 else if (Token.is(MIToken::MachineBasicBlockLabel))
421 return error("basic block definition should be located at the start of "
422 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000423 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000424 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000425 continue;
426 }
427 IsAfterNewline = false;
428 if (Token.is(MIToken::lbrace))
429 ++BraceDepth;
430 if (Token.is(MIToken::rbrace)) {
431 if (!BraceDepth)
432 return error("extraneous closing brace ('}')");
433 --BraceDepth;
434 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000435 lex();
436 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000437 // Verify that we closed all of the '{' at the end of a file or a block.
438 if (!Token.isError() && BraceDepth)
439 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000440 } while (!Token.isErrorOrEOF());
441 return Token.isError();
442}
443
444bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
445 assert(Token.is(MIToken::kw_liveins));
446 lex();
447 if (expectAndConsume(MIToken::colon))
448 return true;
449 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
450 return false;
451 do {
452 if (Token.isNot(MIToken::NamedRegister))
453 return error("expected a named register");
454 unsigned Reg = 0;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000455 if (parseNamedRegister(Reg))
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000456 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000457 lex();
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000458 LaneBitmask Mask = ~LaneBitmask(0);
459 if (consumeIfPresent(MIToken::colon)) {
460 // Parse lane mask.
461 if (Token.isNot(MIToken::IntegerLiteral) &&
462 Token.isNot(MIToken::HexLiteral))
463 return error("expected a lane mask");
464 static_assert(sizeof(LaneBitmask) == sizeof(unsigned), "");
465 if (getUnsigned(Mask))
466 return error("invalid lane mask value");
467 lex();
468 }
469 MBB.addLiveIn(Reg, Mask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000470 } while (consumeIfPresent(MIToken::comma));
471 return false;
472}
473
474bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
475 assert(Token.is(MIToken::kw_successors));
476 lex();
477 if (expectAndConsume(MIToken::colon))
478 return true;
479 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
480 return false;
481 do {
482 if (Token.isNot(MIToken::MachineBasicBlock))
483 return error("expected a machine basic block reference");
484 MachineBasicBlock *SuccMBB = nullptr;
485 if (parseMBBReference(SuccMBB))
486 return true;
487 lex();
488 unsigned Weight = 0;
489 if (consumeIfPresent(MIToken::lparen)) {
490 if (Token.isNot(MIToken::IntegerLiteral))
491 return error("expected an integer literal after '('");
492 if (getUnsigned(Weight))
493 return true;
494 lex();
495 if (expectAndConsume(MIToken::rparen))
496 return true;
497 }
Cong Houd97c1002015-12-01 05:29:22 +0000498 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000499 } while (consumeIfPresent(MIToken::comma));
Cong Houd97c1002015-12-01 05:29:22 +0000500 MBB.normalizeSuccProbs();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000501 return false;
502}
503
504bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
505 // Skip the definition.
506 assert(Token.is(MIToken::MachineBasicBlockLabel));
507 lex();
508 if (consumeIfPresent(MIToken::lparen)) {
509 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
510 lex();
511 consumeIfPresent(MIToken::rparen);
512 }
513 consumeIfPresent(MIToken::colon);
514
515 // Parse the liveins and successors.
516 // N.B: Multiple lists of successors and liveins are allowed and they're
517 // merged into one.
518 // Example:
519 // liveins: %edi
520 // liveins: %esi
521 //
522 // is equivalent to
523 // liveins: %edi, %esi
524 while (true) {
525 if (Token.is(MIToken::kw_successors)) {
526 if (parseBasicBlockSuccessors(MBB))
527 return true;
528 } else if (Token.is(MIToken::kw_liveins)) {
529 if (parseBasicBlockLiveins(MBB))
530 return true;
531 } else if (consumeIfPresent(MIToken::Newline)) {
532 continue;
533 } else
534 break;
535 if (!Token.isNewlineOrEOF())
536 return error("expected line break at the end of a list");
537 lex();
538 }
539
540 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000541 bool IsInBundle = false;
542 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000543 while (true) {
544 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
545 return false;
546 else if (consumeIfPresent(MIToken::Newline))
547 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000548 if (consumeIfPresent(MIToken::rbrace)) {
549 // The first parsing pass should verify that all closing '}' have an
550 // opening '{'.
551 assert(IsInBundle);
552 IsInBundle = false;
553 continue;
554 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000555 MachineInstr *MI = nullptr;
556 if (parse(MI))
557 return true;
558 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000559 if (IsInBundle) {
560 PrevMI->setFlag(MachineInstr::BundledSucc);
561 MI->setFlag(MachineInstr::BundledPred);
562 }
563 PrevMI = MI;
564 if (Token.is(MIToken::lbrace)) {
565 if (IsInBundle)
566 return error("nested instruction bundles are not allowed");
567 lex();
568 // This instruction is the start of the bundle.
569 MI->setFlag(MachineInstr::BundledSucc);
570 IsInBundle = true;
571 if (!Token.is(MIToken::Newline))
572 // The next instruction can be on the same line.
573 continue;
574 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000575 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
576 lex();
577 }
578 return false;
579}
580
581bool MIParser::parseBasicBlocks() {
582 lex();
583 // Skip until the first machine basic block.
584 while (Token.is(MIToken::Newline))
585 lex();
586 if (Token.isErrorOrEOF())
587 return Token.isError();
588 // The first parsing pass should have verified that this token is a MBB label
589 // in the 'parseBasicBlockDefinitions' method.
590 assert(Token.is(MIToken::MachineBasicBlockLabel));
591 do {
592 MachineBasicBlock *MBB = nullptr;
593 if (parseMBBReference(MBB))
594 return true;
595 if (parseBasicBlock(*MBB))
596 return true;
597 // The method 'parseBasicBlock' should parse the whole block until the next
598 // block or the end of file.
599 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
600 } while (Token.isNot(MIToken::Eof));
601 return false;
602}
603
604bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000605 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000606 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000607 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000608 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000609 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000610 Optional<unsigned> TiedDefIdx;
611 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000612 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000613 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000614 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000615 if (Token.isNot(MIToken::comma))
616 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000617 lex();
618 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000619 if (!Operands.empty() && expectAndConsume(MIToken::equal))
620 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000621
Alex Lorenze5a44662015-07-17 00:24:15 +0000622 unsigned OpCode, Flags = 0;
623 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000624 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000625
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000626 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000627 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000628 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000629 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000630 Optional<unsigned> TiedDefIdx;
631 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000632 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000633 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000634 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000635 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
636 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000637 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000638 if (Token.isNot(MIToken::comma))
639 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000640 lex();
641 }
642
Alex Lorenz46d760d2015-07-22 21:15:11 +0000643 DebugLoc DebugLocation;
644 if (Token.is(MIToken::kw_debug_location)) {
645 lex();
646 if (Token.isNot(MIToken::exclaim))
647 return error("expected a metadata node after 'debug-location'");
648 MDNode *Node = nullptr;
649 if (parseMDNode(Node))
650 return true;
651 DebugLocation = DebugLoc(Node);
652 }
653
Alex Lorenz4af7e612015-08-03 23:08:19 +0000654 // Parse the machine memory operands.
655 SmallVector<MachineMemOperand *, 2> MemOperands;
656 if (Token.is(MIToken::coloncolon)) {
657 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000658 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000659 MachineMemOperand *MemOp = nullptr;
660 if (parseMachineMemoryOperand(MemOp))
661 return true;
662 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000663 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000664 break;
665 if (Token.isNot(MIToken::comma))
666 return error("expected ',' before the next machine memory operand");
667 lex();
668 }
669 }
670
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000671 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000672 if (!MCID.isVariadic()) {
673 // FIXME: Move the implicit operand verification to the machine verifier.
674 if (verifyImplicitOperands(Operands, MCID))
675 return true;
676 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000677
Alex Lorenzcb268d42015-07-06 23:07:26 +0000678 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000679 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000680 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000681 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000682 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000683 if (assignRegisterTies(*MI, Operands))
684 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000685 if (MemOperands.empty())
686 return false;
687 MachineInstr::mmo_iterator MemRefs =
688 MF.allocateMemRefsArray(MemOperands.size());
689 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
690 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000691 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000692}
693
Alex Lorenz1ea60892015-07-27 20:29:27 +0000694bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000695 lex();
696 if (Token.isNot(MIToken::MachineBasicBlock))
697 return error("expected a machine basic block reference");
698 if (parseMBBReference(MBB))
699 return true;
700 lex();
701 if (Token.isNot(MIToken::Eof))
702 return error(
703 "expected end of string after the machine basic block reference");
704 return false;
705}
706
Alex Lorenz1ea60892015-07-27 20:29:27 +0000707bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000708 lex();
709 if (Token.isNot(MIToken::NamedRegister))
710 return error("expected a named register");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000711 if (parseNamedRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000712 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000713 lex();
714 if (Token.isNot(MIToken::Eof))
715 return error("expected end of string after the register reference");
716 return false;
717}
718
Matthias Braun74ad41c2016-10-11 03:13:01 +0000719bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
Alex Lorenz12045a42015-07-27 17:42:45 +0000720 lex();
721 if (Token.isNot(MIToken::VirtualRegister))
722 return error("expected a virtual register");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000723 if (parseVirtualRegister(Info))
Alex Lorenz607efb62015-08-18 22:57:36 +0000724 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000725 lex();
726 if (Token.isNot(MIToken::Eof))
727 return error("expected end of string after the register reference");
728 return false;
729}
730
Alex Lorenza314d812015-08-18 22:26:26 +0000731bool MIParser::parseStandaloneStackObject(int &FI) {
732 lex();
733 if (Token.isNot(MIToken::StackObject))
734 return error("expected a stack object");
735 if (parseStackFrameIndex(FI))
736 return true;
737 if (Token.isNot(MIToken::Eof))
738 return error("expected end of string after the stack object reference");
739 return false;
740}
741
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000742bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
743 lex();
744 if (Token.isNot(MIToken::exclaim))
745 return error("expected a metadata node");
746 if (parseMDNode(Node))
747 return true;
748 if (Token.isNot(MIToken::Eof))
749 return error("expected end of string after the metadata node");
750 return false;
751}
752
Alex Lorenz36962cd2015-07-07 02:08:46 +0000753static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
754 assert(MO.isImplicit());
755 return MO.isDef() ? "implicit-def" : "implicit";
756}
757
758static std::string getRegisterName(const TargetRegisterInfo *TRI,
759 unsigned Reg) {
760 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
761 return StringRef(TRI->getName(Reg)).lower();
762}
763
Alex Lorenz0153e592015-09-10 14:04:34 +0000764/// Return true if the parsed machine operands contain a given machine operand.
765static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
766 ArrayRef<ParsedMachineOperand> Operands) {
767 for (const auto &I : Operands) {
768 if (ImplicitOperand.isIdenticalTo(I.Operand))
769 return true;
770 }
771 return false;
772}
773
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000774bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
775 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000776 if (MCID.isCall())
777 // We can't verify call instructions as they can contain arbitrary implicit
778 // register and register mask operands.
779 return false;
780
781 // Gather all the expected implicit operands.
782 SmallVector<MachineOperand, 4> ImplicitOperands;
783 if (MCID.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000784 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000785 ImplicitOperands.push_back(
786 MachineOperand::CreateReg(*ImpDefs, true, true));
787 if (MCID.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000788 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000789 ImplicitOperands.push_back(
790 MachineOperand::CreateReg(*ImpUses, false, true));
791
792 const auto *TRI = MF.getSubtarget().getRegisterInfo();
793 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000794 for (const auto &I : ImplicitOperands) {
795 if (isImplicitOperandIn(I, Operands))
796 continue;
797 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000798 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000799 printImplicitRegisterFlag(I) + " %" +
800 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000801 }
802 return false;
803}
804
Alex Lorenze5a44662015-07-17 00:24:15 +0000805bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
806 if (Token.is(MIToken::kw_frame_setup)) {
807 Flags |= MachineInstr::FrameSetup;
808 lex();
809 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000810 if (Token.isNot(MIToken::Identifier))
811 return error("expected a machine instruction");
812 StringRef InstrName = Token.stringValue();
813 if (parseInstrName(InstrName, OpCode))
814 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000815 lex();
816 return false;
817}
818
Matthias Braun74ad41c2016-10-11 03:13:01 +0000819bool MIParser::parseNamedRegister(unsigned &Reg) {
820 assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
821 StringRef Name = Token.stringValue();
822 if (getRegisterByName(Name, Reg))
823 return error(Twine("unknown register name '") + Name + "'");
824 return false;
825}
826
827bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
828 assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
829 unsigned ID;
830 if (getUnsigned(ID))
831 return true;
832 Info = &PFS.getVRegInfo(ID);
833 return false;
834}
835
836bool MIParser::parseRegister(unsigned &Reg, VRegInfo *&Info) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000837 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000838 case MIToken::underscore:
839 Reg = 0;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000840 return false;
841 case MIToken::NamedRegister:
842 return parseNamedRegister(Reg);
843 case MIToken::VirtualRegister:
844 if (parseVirtualRegister(Info))
Alex Lorenz53464512015-07-10 22:51:20 +0000845 return true;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000846 Reg = Info->VReg;
847 return false;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000848 // TODO: Parse other register kinds.
849 default:
850 llvm_unreachable("The current token should be a register");
851 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000852}
853
Alex Lorenzcb268d42015-07-06 23:07:26 +0000854bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000855 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000856 switch (Token.kind()) {
857 case MIToken::kw_implicit:
858 Flags |= RegState::Implicit;
859 break;
860 case MIToken::kw_implicit_define:
861 Flags |= RegState::ImplicitDefine;
862 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000863 case MIToken::kw_def:
864 Flags |= RegState::Define;
865 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000866 case MIToken::kw_dead:
867 Flags |= RegState::Dead;
868 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000869 case MIToken::kw_killed:
870 Flags |= RegState::Kill;
871 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000872 case MIToken::kw_undef:
873 Flags |= RegState::Undef;
874 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000875 case MIToken::kw_internal:
876 Flags |= RegState::InternalRead;
877 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000878 case MIToken::kw_early_clobber:
879 Flags |= RegState::EarlyClobber;
880 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000881 case MIToken::kw_debug_use:
882 Flags |= RegState::Debug;
883 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000884 default:
885 llvm_unreachable("The current token should be a register flag");
886 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000887 if (OldFlags == Flags)
888 // We know that the same flag is specified more than once when the flags
889 // weren't modified.
890 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000891 lex();
892 return false;
893}
894
Alex Lorenz2eacca82015-07-13 23:24:34 +0000895bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
Matthias Braun333e4682016-07-26 21:49:34 +0000896 assert(Token.is(MIToken::dot));
Alex Lorenz2eacca82015-07-13 23:24:34 +0000897 lex();
898 if (Token.isNot(MIToken::Identifier))
Matthias Braun333e4682016-07-26 21:49:34 +0000899 return error("expected a subregister index after '.'");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000900 auto Name = Token.stringValue();
901 SubReg = getSubRegIndex(Name);
902 if (!SubReg)
903 return error(Twine("use of unknown subregister index '") + Name + "'");
904 lex();
905 return false;
906}
907
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000908bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
909 if (!consumeIfPresent(MIToken::kw_tied_def))
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000910 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000911 if (Token.isNot(MIToken::IntegerLiteral))
912 return error("expected an integer literal after 'tied-def'");
913 if (getUnsigned(TiedDefIdx))
914 return true;
915 lex();
916 if (expectAndConsume(MIToken::rparen))
917 return true;
918 return false;
919}
920
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000921bool MIParser::assignRegisterTies(MachineInstr &MI,
922 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000923 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
924 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
925 if (!Operands[I].TiedDefIdx)
926 continue;
927 // The parser ensures that this operand is a register use, so we just have
928 // to check the tied-def operand.
929 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
930 if (DefIdx >= E)
931 return error(Operands[I].Begin,
932 Twine("use of invalid tied-def operand index '" +
933 Twine(DefIdx) + "'; instruction has only ") +
934 Twine(E) + " operands");
935 const auto &DefOperand = Operands[DefIdx].Operand;
936 if (!DefOperand.isReg() || !DefOperand.isDef())
937 // FIXME: add note with the def operand.
938 return error(Operands[I].Begin,
939 Twine("use of invalid tied-def operand index '") +
940 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
941 " isn't a defined register");
942 // Check that the tied-def operand wasn't tied elsewhere.
943 for (const auto &TiedPair : TiedRegisterPairs) {
944 if (TiedPair.first == DefIdx)
945 return error(Operands[I].Begin,
946 Twine("the tied-def operand #") + Twine(DefIdx) +
947 " is already tied with another register operand");
948 }
949 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
950 }
951 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
952 // indices must be less than tied max.
953 for (const auto &TiedPair : TiedRegisterPairs)
954 MI.tieOperands(TiedPair.first, TiedPair.second);
955 return false;
956}
957
958bool MIParser::parseRegisterOperand(MachineOperand &Dest,
959 Optional<unsigned> &TiedDefIdx,
960 bool IsDef) {
Alex Lorenzcb268d42015-07-06 23:07:26 +0000961 unsigned Flags = IsDef ? RegState::Define : 0;
962 while (Token.isRegisterFlag()) {
963 if (parseRegisterFlag(Flags))
964 return true;
965 }
966 if (!Token.isRegister())
967 return error("expected a register after register flags");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000968 unsigned Reg;
969 VRegInfo *RegInfo;
970 if (parseRegister(Reg, RegInfo))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000971 return true;
972 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000973 unsigned SubReg = 0;
Matthias Braun333e4682016-07-26 21:49:34 +0000974 if (Token.is(MIToken::dot)) {
Alex Lorenz2eacca82015-07-13 23:24:34 +0000975 if (parseSubRegisterIndex(SubReg))
976 return true;
Matthias Braun5d00b322016-07-16 01:36:18 +0000977 if (!TargetRegisterInfo::isVirtualRegister(Reg))
978 return error("subregister index expects a virtual register");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000979 }
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000980 MachineRegisterInfo &MRI = MF.getRegInfo();
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000981 if ((Flags & RegState::Define) == 0) {
982 if (consumeIfPresent(MIToken::lparen)) {
983 unsigned Idx;
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000984 if (!parseRegisterTiedDefIndex(Idx))
985 TiedDefIdx = Idx;
986 else {
987 // Try a redundant low-level type.
988 LLT Ty;
989 if (parseLowLevelType(Token.location(), Ty))
990 return error("expected tied-def or low-level type after '('");
991
992 if (expectAndConsume(MIToken::rparen))
993 return true;
994
995 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
996 return error("inconsistent type for generic virtual register");
997
998 MRI.setType(Reg, Ty);
999 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001000 }
1001 } else if (consumeIfPresent(MIToken::lparen)) {
Quentin Colombet2c646962016-06-08 23:27:46 +00001002 // Virtual registers may have a size with GlobalISel.
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001003 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1004 return error("unexpected size on physical register");
Tim Northover60cf6fc2016-10-11 20:50:04 +00001005 if (RegInfo->Kind != VRegInfo::GENERIC &&
1006 RegInfo->Kind != VRegInfo::REGBANK)
Ahmed Bougacha5a59b242016-07-19 19:48:36 +00001007 return error("unexpected size on non-generic virtual register");
1008
Tim Northover0f140c72016-09-09 11:46:34 +00001009 LLT Ty;
1010 if (parseLowLevelType(Token.location(), Ty))
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001011 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001012
Tim Northover0f140c72016-09-09 11:46:34 +00001013 if (expectAndConsume(MIToken::rparen))
1014 return true;
1015
Tim Northoverd28d3cc2016-09-12 11:20:10 +00001016 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1017 return error("inconsistent type for generic virtual register");
1018
Tim Northover0f140c72016-09-09 11:46:34 +00001019 MRI.setType(Reg, Ty);
Matthias Braun74ad41c2016-10-11 03:13:01 +00001020 } else if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Quentin Colombet2c646962016-06-08 23:27:46 +00001021 // Generic virtual registers must have a size.
1022 // If we end up here this means the size hasn't been specified and
1023 // this is bad!
Matthias Braun74ad41c2016-10-11 03:13:01 +00001024 if (RegInfo->Kind == VRegInfo::GENERIC ||
1025 RegInfo->Kind == VRegInfo::REGBANK)
1026 return error("generic virtual registers must have a size");
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001027 }
Alex Lorenz495ad872015-07-08 21:23:34 +00001028 Dest = MachineOperand::CreateReg(
1029 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +00001030 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +00001031 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1032 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001033 return false;
1034}
1035
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001036bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1037 assert(Token.is(MIToken::IntegerLiteral));
1038 const APSInt &Int = Token.integerValue();
1039 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +00001040 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001041 Dest = MachineOperand::CreateImm(Int.getExtValue());
1042 lex();
1043 return false;
1044}
1045
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001046bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1047 const Constant *&C) {
1048 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001049 SMDiagnostic Err;
Malcolm Parsons06ac79c2016-11-02 16:43:50 +00001050 C = parseConstantValue(Source, Err, *MF.getFunction()->getParent(),
Matthias Braune35861d2016-07-13 23:27:50 +00001051 &PFS.IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001052 if (!C)
1053 return error(Loc + Err.getColumnNo(), Err.getMessage());
1054 return false;
1055}
1056
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001057bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1058 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1059 return true;
1060 lex();
1061 return false;
1062}
1063
Ahmed Bougachad760de02016-07-28 17:15:12 +00001064bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
Tim Northover32a078a2016-09-15 10:09:59 +00001065 if (Token.is(MIToken::ScalarType)) {
Tim Northover62ae5682016-07-20 19:09:30 +00001066 Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1067 lex();
1068 return false;
Tim Northoverbd505462016-07-22 16:59:52 +00001069 } else if (Token.is(MIToken::PointerType)) {
Tim Northover5ae83502016-09-15 09:20:34 +00001070 const DataLayout &DL = MF.getFunction()->getParent()->getDataLayout();
1071 unsigned AS = APSInt(Token.range().drop_front()).getZExtValue();
1072 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
Tim Northoverbd505462016-07-22 16:59:52 +00001073 lex();
1074 return false;
Tim Northover62ae5682016-07-20 19:09:30 +00001075 }
Quentin Colombet85199672016-03-08 00:20:48 +00001076
Tim Northover62ae5682016-07-20 19:09:30 +00001077 // Now we're looking for a vector.
1078 if (Token.isNot(MIToken::less))
Tim Northoverbd505462016-07-22 16:59:52 +00001079 return error(Loc,
1080 "expected unsized, pN, sN or <N x sM> for GlobalISel type");
1081
Tim Northover62ae5682016-07-20 19:09:30 +00001082 lex();
1083
1084 if (Token.isNot(MIToken::IntegerLiteral))
1085 return error(Loc, "expected <N x sM> for vctor type");
1086 uint64_t NumElements = Token.integerValue().getZExtValue();
1087 lex();
1088
1089 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1090 return error(Loc, "expected '<N x sM>' for vector type");
1091 lex();
1092
1093 if (Token.isNot(MIToken::ScalarType))
1094 return error(Loc, "expected '<N x sM>' for vector type");
1095 uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1096 lex();
1097
1098 if (Token.isNot(MIToken::greater))
1099 return error(Loc, "expected '<N x sM>' for vector type");
1100 lex();
1101
1102 Ty = LLT::vector(NumElements, ScalarSize);
Quentin Colombet85199672016-03-08 00:20:48 +00001103 return false;
1104}
1105
Alex Lorenz05e38822015-08-05 18:52:21 +00001106bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1107 assert(Token.is(MIToken::IntegerType));
1108 auto Loc = Token.location();
1109 lex();
1110 if (Token.isNot(MIToken::IntegerLiteral))
1111 return error("expected an integer literal");
1112 const Constant *C = nullptr;
1113 if (parseIRConstant(Loc, C))
1114 return true;
1115 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1116 return false;
1117}
1118
Alex Lorenzad156fb2015-07-31 20:49:21 +00001119bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1120 auto Loc = Token.location();
1121 lex();
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +00001122 if (Token.isNot(MIToken::FloatingPointLiteral) &&
1123 Token.isNot(MIToken::HexLiteral))
Alex Lorenzad156fb2015-07-31 20:49:21 +00001124 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001125 const Constant *C = nullptr;
1126 if (parseIRConstant(Loc, C))
1127 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001128 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1129 return false;
1130}
1131
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001132bool MIParser::getUnsigned(unsigned &Result) {
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +00001133 if (Token.hasIntegerValue()) {
1134 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1135 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1136 if (Val64 == Limit)
1137 return error("expected 32-bit integer (too large)");
1138 Result = Val64;
1139 return false;
1140 }
1141 if (Token.is(MIToken::HexLiteral)) {
1142 StringRef S = Token.range();
1143 assert(S[0] == '0' && tolower(S[1]) == 'x');
1144 // This could be a floating point literal with a special prefix.
1145 if (!isxdigit(S[2]))
1146 return true;
1147 StringRef V = S.substr(2);
1148 unsigned BW = std::min<unsigned>(V.size()*4, 32);
1149 APInt A(BW, V, 16);
1150 APInt Limit = APInt(BW, std::numeric_limits<unsigned>::max());
1151 if (A.ugt(Limit))
1152 return error("expected 32-bit integer (too large)");
1153 Result = A.getZExtValue();
1154 return false;
1155 }
1156 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001157}
1158
Alex Lorenzf09df002015-06-30 18:16:42 +00001159bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001160 assert(Token.is(MIToken::MachineBasicBlock) ||
1161 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001162 unsigned Number;
1163 if (getUnsigned(Number))
1164 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001165 auto MBBInfo = PFS.MBBSlots.find(Number);
1166 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001167 return error(Twine("use of undefined machine basic block #") +
1168 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001169 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001170 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1171 return error(Twine("the name of machine basic block #") + Twine(Number) +
1172 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001173 return false;
1174}
1175
1176bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1177 MachineBasicBlock *MBB;
1178 if (parseMBBReference(MBB))
1179 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001180 Dest = MachineOperand::CreateMBB(MBB);
1181 lex();
1182 return false;
1183}
1184
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001185bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001186 assert(Token.is(MIToken::StackObject));
1187 unsigned ID;
1188 if (getUnsigned(ID))
1189 return true;
1190 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1191 if (ObjectInfo == PFS.StackObjectSlots.end())
1192 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1193 "'");
1194 StringRef Name;
1195 if (const auto *Alloca =
Matthias Braun941a7052016-07-28 18:40:00 +00001196 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001197 Name = Alloca->getName();
1198 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1199 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1200 "' isn't '" + Token.stringValue() + "'");
1201 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001202 FI = ObjectInfo->second;
1203 return false;
1204}
1205
1206bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1207 int FI;
1208 if (parseStackFrameIndex(FI))
1209 return true;
1210 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001211 return false;
1212}
1213
Alex Lorenzea882122015-08-12 21:17:02 +00001214bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001215 assert(Token.is(MIToken::FixedStackObject));
1216 unsigned ID;
1217 if (getUnsigned(ID))
1218 return true;
1219 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1220 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1221 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1222 Twine(ID) + "'");
1223 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001224 FI = ObjectInfo->second;
1225 return false;
1226}
1227
1228bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1229 int FI;
1230 if (parseFixedStackFrameIndex(FI))
1231 return true;
1232 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001233 return false;
1234}
1235
Alex Lorenz41df7d32015-07-28 17:09:52 +00001236bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001237 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001238 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001239 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001240 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001241 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001242 return error(Twine("use of undefined global value '") + Token.range() +
1243 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001244 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001245 }
1246 case MIToken::GlobalValue: {
1247 unsigned GVIdx;
1248 if (getUnsigned(GVIdx))
1249 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001250 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001251 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1252 "'");
Matthias Braune35861d2016-07-13 23:27:50 +00001253 GV = PFS.IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001254 break;
1255 }
1256 default:
1257 llvm_unreachable("The current token should be a global value");
1258 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001259 return false;
1260}
1261
1262bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1263 GlobalValue *GV = nullptr;
1264 if (parseGlobalValue(GV))
1265 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001266 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001267 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001268 if (parseOperandsOffset(Dest))
1269 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001270 return false;
1271}
1272
Alex Lorenzab980492015-07-20 20:51:18 +00001273bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1274 assert(Token.is(MIToken::ConstantPoolItem));
1275 unsigned ID;
1276 if (getUnsigned(ID))
1277 return true;
1278 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1279 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1280 return error("use of undefined constant '%const." + Twine(ID) + "'");
1281 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001282 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001283 if (parseOperandsOffset(Dest))
1284 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001285 return false;
1286}
1287
Alex Lorenz31d70682015-07-15 23:38:35 +00001288bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1289 assert(Token.is(MIToken::JumpTableIndex));
1290 unsigned ID;
1291 if (getUnsigned(ID))
1292 return true;
1293 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1294 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1295 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1296 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001297 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1298 return false;
1299}
1300
Alex Lorenz6ede3742015-07-21 16:59:53 +00001301bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001302 assert(Token.is(MIToken::ExternalSymbol));
1303 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001304 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001305 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001306 if (parseOperandsOffset(Dest))
1307 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001308 return false;
1309}
1310
Matthias Braunb74eb412016-03-28 18:18:46 +00001311bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1312 assert(Token.is(MIToken::SubRegisterIndex));
1313 StringRef Name = Token.stringValue();
1314 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1315 if (SubRegIndex == 0)
1316 return error(Twine("unknown subregister index '") + Name + "'");
1317 lex();
1318 Dest = MachineOperand::CreateImm(SubRegIndex);
1319 return false;
1320}
1321
Alex Lorenz44f29252015-07-22 21:07:04 +00001322bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001323 assert(Token.is(MIToken::exclaim));
1324 auto Loc = Token.location();
1325 lex();
1326 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1327 return error("expected metadata id after '!'");
1328 unsigned ID;
1329 if (getUnsigned(ID))
1330 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001331 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1332 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
Alex Lorenz35e44462015-07-22 17:58:46 +00001333 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1334 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001335 Node = NodeInfo->second.get();
1336 return false;
1337}
1338
1339bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1340 MDNode *Node = nullptr;
1341 if (parseMDNode(Node))
1342 return true;
1343 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001344 return false;
1345}
1346
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001347bool MIParser::parseCFIOffset(int &Offset) {
1348 if (Token.isNot(MIToken::IntegerLiteral))
1349 return error("expected a cfi offset");
1350 if (Token.integerValue().getMinSignedBits() > 32)
1351 return error("expected a 32 bit integer (the cfi offset is too large)");
1352 Offset = (int)Token.integerValue().getExtValue();
1353 lex();
1354 return false;
1355}
1356
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001357bool MIParser::parseCFIRegister(unsigned &Reg) {
1358 if (Token.isNot(MIToken::NamedRegister))
1359 return error("expected a cfi register");
1360 unsigned LLVMReg;
Matthias Braun74ad41c2016-10-11 03:13:01 +00001361 if (parseNamedRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001362 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001363 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1364 assert(TRI && "Expected target register info");
1365 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1366 if (DwarfReg < 0)
1367 return error("invalid DWARF register");
1368 Reg = (unsigned)DwarfReg;
1369 lex();
1370 return false;
1371}
1372
1373bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1374 auto Kind = Token.kind();
1375 lex();
1376 auto &MMI = MF.getMMI();
1377 int Offset;
1378 unsigned Reg;
1379 unsigned CFIIndex;
1380 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001381 case MIToken::kw_cfi_same_value:
1382 if (parseCFIRegister(Reg))
1383 return true;
1384 CFIIndex =
1385 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1386 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001387 case MIToken::kw_cfi_offset:
1388 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1389 parseCFIOffset(Offset))
1390 return true;
1391 CFIIndex =
1392 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1393 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001394 case MIToken::kw_cfi_def_cfa_register:
1395 if (parseCFIRegister(Reg))
1396 return true;
1397 CFIIndex =
1398 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1399 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001400 case MIToken::kw_cfi_def_cfa_offset:
1401 if (parseCFIOffset(Offset))
1402 return true;
1403 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1404 CFIIndex = MMI.addFrameInst(
1405 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1406 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001407 case MIToken::kw_cfi_def_cfa:
1408 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1409 parseCFIOffset(Offset))
1410 return true;
1411 // NB: MCCFIInstruction::createDefCfa negates the offset.
1412 CFIIndex =
1413 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1414 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001415 default:
1416 // TODO: Parse the other CFI operands.
1417 llvm_unreachable("The current token should be a cfi operand");
1418 }
1419 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001420 return false;
1421}
1422
Alex Lorenzdeb53492015-07-28 17:28:03 +00001423bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1424 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001425 case MIToken::NamedIRBlock: {
1426 BB = dyn_cast_or_null<BasicBlock>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +00001427 F.getValueSymbolTable()->lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001428 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001429 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001430 break;
1431 }
1432 case MIToken::IRBlock: {
1433 unsigned SlotNumber = 0;
1434 if (getUnsigned(SlotNumber))
1435 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001436 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001437 if (!BB)
1438 return error(Twine("use of undefined IR block '%ir-block.") +
1439 Twine(SlotNumber) + "'");
1440 break;
1441 }
1442 default:
1443 llvm_unreachable("The current token should be an IR block reference");
1444 }
1445 return false;
1446}
1447
1448bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1449 assert(Token.is(MIToken::kw_blockaddress));
1450 lex();
1451 if (expectAndConsume(MIToken::lparen))
1452 return true;
1453 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001454 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001455 return error("expected a global value");
1456 GlobalValue *GV = nullptr;
1457 if (parseGlobalValue(GV))
1458 return true;
1459 auto *F = dyn_cast<Function>(GV);
1460 if (!F)
1461 return error("expected an IR function reference");
1462 lex();
1463 if (expectAndConsume(MIToken::comma))
1464 return true;
1465 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001466 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001467 return error("expected an IR block reference");
1468 if (parseIRBlock(BB, *F))
1469 return true;
1470 lex();
1471 if (expectAndConsume(MIToken::rparen))
1472 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001473 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001474 if (parseOperandsOffset(Dest))
1475 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001476 return false;
1477}
1478
Tim Northover6b3bd612016-07-29 20:32:59 +00001479bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
1480 assert(Token.is(MIToken::kw_intrinsic));
1481 lex();
1482 if (expectAndConsume(MIToken::lparen))
1483 return error("expected syntax intrinsic(@llvm.whatever)");
1484
1485 if (Token.isNot(MIToken::NamedGlobalValue))
1486 return error("expected syntax intrinsic(@llvm.whatever)");
1487
1488 std::string Name = Token.stringValue();
1489 lex();
1490
1491 if (expectAndConsume(MIToken::rparen))
1492 return error("expected ')' to terminate intrinsic name");
1493
1494 // Find out what intrinsic we're dealing with, first try the global namespace
1495 // and then the target's private intrinsics if that fails.
1496 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1497 Intrinsic::ID ID = Function::lookupIntrinsicID(Name);
1498 if (ID == Intrinsic::not_intrinsic && TII)
1499 ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
1500
1501 if (ID == Intrinsic::not_intrinsic)
1502 return error("unknown intrinsic name");
1503 Dest = MachineOperand::CreateIntrinsicID(ID);
1504
1505 return false;
1506}
1507
Tim Northoverde3aea0412016-08-17 20:25:25 +00001508bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
1509 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
1510 bool IsFloat = Token.is(MIToken::kw_floatpred);
1511 lex();
1512
1513 if (expectAndConsume(MIToken::lparen))
1514 return error("expected syntax intpred(whatever) or floatpred(whatever");
1515
1516 if (Token.isNot(MIToken::Identifier))
1517 return error("whatever");
1518
1519 CmpInst::Predicate Pred;
1520 if (IsFloat) {
1521 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1522 .Case("false", CmpInst::FCMP_FALSE)
1523 .Case("oeq", CmpInst::FCMP_OEQ)
1524 .Case("ogt", CmpInst::FCMP_OGT)
1525 .Case("oge", CmpInst::FCMP_OGE)
1526 .Case("olt", CmpInst::FCMP_OLT)
1527 .Case("ole", CmpInst::FCMP_OLE)
1528 .Case("one", CmpInst::FCMP_ONE)
1529 .Case("ord", CmpInst::FCMP_ORD)
1530 .Case("uno", CmpInst::FCMP_UNO)
1531 .Case("ueq", CmpInst::FCMP_UEQ)
1532 .Case("ugt", CmpInst::FCMP_UGT)
1533 .Case("uge", CmpInst::FCMP_UGE)
1534 .Case("ult", CmpInst::FCMP_ULT)
1535 .Case("ule", CmpInst::FCMP_ULE)
1536 .Case("une", CmpInst::FCMP_UNE)
1537 .Case("true", CmpInst::FCMP_TRUE)
1538 .Default(CmpInst::BAD_FCMP_PREDICATE);
1539 if (!CmpInst::isFPPredicate(Pred))
1540 return error("invalid floating-point predicate");
1541 } else {
1542 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1543 .Case("eq", CmpInst::ICMP_EQ)
1544 .Case("ne", CmpInst::ICMP_NE)
1545 .Case("sgt", CmpInst::ICMP_SGT)
1546 .Case("sge", CmpInst::ICMP_SGE)
1547 .Case("slt", CmpInst::ICMP_SLT)
1548 .Case("sle", CmpInst::ICMP_SLE)
1549 .Case("ugt", CmpInst::ICMP_UGT)
1550 .Case("uge", CmpInst::ICMP_UGE)
1551 .Case("ult", CmpInst::ICMP_ULT)
1552 .Case("ule", CmpInst::ICMP_ULE)
1553 .Default(CmpInst::BAD_ICMP_PREDICATE);
1554 if (!CmpInst::isIntPredicate(Pred))
1555 return error("invalid integer predicate");
1556 }
1557
1558 lex();
1559 Dest = MachineOperand::CreatePredicate(Pred);
Tim Northover6cd4b232016-08-23 21:01:26 +00001560 if (expectAndConsume(MIToken::rparen))
Tim Northoverde3aea0412016-08-17 20:25:25 +00001561 return error("predicate should be terminated by ')'.");
1562
1563 return false;
1564}
1565
Alex Lorenzef5c1962015-07-28 23:02:45 +00001566bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1567 assert(Token.is(MIToken::kw_target_index));
1568 lex();
1569 if (expectAndConsume(MIToken::lparen))
1570 return true;
1571 if (Token.isNot(MIToken::Identifier))
1572 return error("expected the name of the target index");
1573 int Index = 0;
1574 if (getTargetIndex(Token.stringValue(), Index))
1575 return error("use of undefined target index '" + Token.stringValue() + "'");
1576 lex();
1577 if (expectAndConsume(MIToken::rparen))
1578 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001579 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001580 if (parseOperandsOffset(Dest))
1581 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001582 return false;
1583}
1584
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001585bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1586 assert(Token.is(MIToken::kw_liveout));
1587 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1588 assert(TRI && "Expected target register info");
1589 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1590 lex();
1591 if (expectAndConsume(MIToken::lparen))
1592 return true;
1593 while (true) {
1594 if (Token.isNot(MIToken::NamedRegister))
1595 return error("expected a named register");
Matthias Braun74ad41c2016-10-11 03:13:01 +00001596 unsigned Reg;
1597 if (parseNamedRegister(Reg))
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001598 return true;
1599 lex();
1600 Mask[Reg / 32] |= 1U << (Reg % 32);
1601 // TODO: Report an error if the same register is used more than once.
1602 if (Token.isNot(MIToken::comma))
1603 break;
1604 lex();
1605 }
1606 if (expectAndConsume(MIToken::rparen))
1607 return true;
1608 Dest = MachineOperand::CreateRegLiveOut(Mask);
1609 return false;
1610}
1611
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001612bool MIParser::parseMachineOperand(MachineOperand &Dest,
1613 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001614 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001615 case MIToken::kw_implicit:
1616 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001617 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001618 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001619 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001620 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001621 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001622 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001623 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001624 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001625 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001626 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001627 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001628 case MIToken::IntegerLiteral:
1629 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001630 case MIToken::IntegerType:
1631 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001632 case MIToken::kw_half:
1633 case MIToken::kw_float:
1634 case MIToken::kw_double:
1635 case MIToken::kw_x86_fp80:
1636 case MIToken::kw_fp128:
1637 case MIToken::kw_ppc_fp128:
1638 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001639 case MIToken::MachineBasicBlock:
1640 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001641 case MIToken::StackObject:
1642 return parseStackObjectOperand(Dest);
1643 case MIToken::FixedStackObject:
1644 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001645 case MIToken::GlobalValue:
1646 case MIToken::NamedGlobalValue:
1647 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001648 case MIToken::ConstantPoolItem:
1649 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001650 case MIToken::JumpTableIndex:
1651 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001652 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001653 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00001654 case MIToken::SubRegisterIndex:
1655 return parseSubRegisterIndexOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001656 case MIToken::exclaim:
1657 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001658 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001659 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001660 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001661 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001662 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001663 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001664 case MIToken::kw_blockaddress:
1665 return parseBlockAddressOperand(Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +00001666 case MIToken::kw_intrinsic:
1667 return parseIntrinsicOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001668 case MIToken::kw_target_index:
1669 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001670 case MIToken::kw_liveout:
1671 return parseLiveoutRegisterMaskOperand(Dest);
Tim Northoverde3aea0412016-08-17 20:25:25 +00001672 case MIToken::kw_floatpred:
1673 case MIToken::kw_intpred:
1674 return parsePredicateOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001675 case MIToken::Error:
1676 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001677 case MIToken::Identifier:
1678 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1679 Dest = MachineOperand::CreateRegMask(RegMask);
1680 lex();
1681 break;
1682 }
Justin Bognerb03fd122016-08-17 05:10:15 +00001683 LLVM_FALLTHROUGH;
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001684 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001685 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001686 return error("expected a machine operand");
1687 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001688 return false;
1689}
1690
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001691bool MIParser::parseMachineOperandAndTargetFlags(
1692 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001693 unsigned TF = 0;
1694 bool HasTargetFlags = false;
1695 if (Token.is(MIToken::kw_target_flags)) {
1696 HasTargetFlags = true;
1697 lex();
1698 if (expectAndConsume(MIToken::lparen))
1699 return true;
1700 if (Token.isNot(MIToken::Identifier))
1701 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001702 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1703 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1704 return error("use of undefined target flag '" + Token.stringValue() +
1705 "'");
1706 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001707 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001708 while (Token.is(MIToken::comma)) {
1709 lex();
1710 if (Token.isNot(MIToken::Identifier))
1711 return error("expected the name of the target flag");
1712 unsigned BitFlag = 0;
1713 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1714 return error("use of undefined target flag '" + Token.stringValue() +
1715 "'");
1716 // TODO: Report an error when using a duplicate bit target flag.
1717 TF |= BitFlag;
1718 lex();
1719 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001720 if (expectAndConsume(MIToken::rparen))
1721 return true;
1722 }
1723 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001724 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001725 return true;
1726 if (!HasTargetFlags)
1727 return false;
1728 if (Dest.isReg())
1729 return error(Loc, "register operands can't have target flags");
1730 Dest.setTargetFlags(TF);
1731 return false;
1732}
1733
Alex Lorenzdc24c172015-08-07 20:21:00 +00001734bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001735 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1736 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001737 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001738 bool IsNegative = Token.is(MIToken::minus);
1739 lex();
1740 if (Token.isNot(MIToken::IntegerLiteral))
1741 return error("expected an integer literal after '" + Sign + "'");
1742 if (Token.integerValue().getMinSignedBits() > 64)
1743 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001744 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001745 if (IsNegative)
1746 Offset = -Offset;
1747 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001748 return false;
1749}
1750
Alex Lorenz620f8912015-08-13 20:33:33 +00001751bool MIParser::parseAlignment(unsigned &Alignment) {
1752 assert(Token.is(MIToken::kw_align));
1753 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001754 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001755 return error("expected an integer literal after 'align'");
1756 if (getUnsigned(Alignment))
1757 return true;
1758 lex();
1759 return false;
1760}
1761
Alex Lorenzdc24c172015-08-07 20:21:00 +00001762bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1763 int64_t Offset = 0;
1764 if (parseOffset(Offset))
1765 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001766 Op.setOffset(Offset);
1767 return false;
1768}
1769
Alex Lorenz36593ac2015-08-19 23:27:07 +00001770bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001771 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001772 case MIToken::NamedIRValue: {
Mehdi Aminia53d49e2016-09-17 06:00:02 +00001773 V = MF.getFunction()->getValueSymbolTable()->lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001774 break;
1775 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001776 case MIToken::IRValue: {
1777 unsigned SlotNumber = 0;
1778 if (getUnsigned(SlotNumber))
1779 return true;
1780 V = getIRValue(SlotNumber);
1781 break;
1782 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001783 case MIToken::NamedGlobalValue:
1784 case MIToken::GlobalValue: {
1785 GlobalValue *GV = nullptr;
1786 if (parseGlobalValue(GV))
1787 return true;
1788 V = GV;
1789 break;
1790 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001791 case MIToken::QuotedIRValue: {
1792 const Constant *C = nullptr;
1793 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1794 return true;
1795 V = C;
1796 break;
1797 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001798 default:
1799 llvm_unreachable("The current token should be an IR block reference");
1800 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001801 if (!V)
1802 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001803 return false;
1804}
1805
1806bool MIParser::getUint64(uint64_t &Result) {
1807 assert(Token.hasIntegerValue());
1808 if (Token.integerValue().getActiveBits() > 64)
1809 return error("expected 64-bit integer (too large)");
1810 Result = Token.integerValue().getZExtValue();
1811 return false;
1812}
1813
Justin Lebar0af80cd2016-07-15 18:26:59 +00001814bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
1815 const auto OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001816 switch (Token.kind()) {
1817 case MIToken::kw_volatile:
1818 Flags |= MachineMemOperand::MOVolatile;
1819 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001820 case MIToken::kw_non_temporal:
1821 Flags |= MachineMemOperand::MONonTemporal;
1822 break;
Justin Lebaradbf09e2016-09-11 01:38:58 +00001823 case MIToken::kw_dereferenceable:
1824 Flags |= MachineMemOperand::MODereferenceable;
1825 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001826 case MIToken::kw_invariant:
1827 Flags |= MachineMemOperand::MOInvariant;
1828 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001829 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001830 default:
1831 llvm_unreachable("The current token should be a memory operand flag");
1832 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001833 if (OldFlags == Flags)
1834 // We know that the same flag is specified more than once when the flags
1835 // weren't modified.
1836 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001837 lex();
1838 return false;
1839}
1840
Alex Lorenz91097a32015-08-12 20:33:26 +00001841bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1842 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001843 case MIToken::kw_stack:
1844 PSV = MF.getPSVManager().getStack();
1845 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001846 case MIToken::kw_got:
1847 PSV = MF.getPSVManager().getGOT();
1848 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001849 case MIToken::kw_jump_table:
1850 PSV = MF.getPSVManager().getJumpTable();
1851 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001852 case MIToken::kw_constant_pool:
1853 PSV = MF.getPSVManager().getConstantPool();
1854 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001855 case MIToken::FixedStackObject: {
1856 int FI;
1857 if (parseFixedStackFrameIndex(FI))
1858 return true;
1859 PSV = MF.getPSVManager().getFixedStack(FI);
1860 // The token was already consumed, so use return here instead of break.
1861 return false;
1862 }
Matthias Braun3ef7df92016-06-08 00:47:07 +00001863 case MIToken::StackObject: {
1864 int FI;
1865 if (parseStackFrameIndex(FI))
1866 return true;
1867 PSV = MF.getPSVManager().getFixedStack(FI);
1868 // The token was already consumed, so use return here instead of break.
1869 return false;
1870 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001871 case MIToken::kw_call_entry: {
1872 lex();
1873 switch (Token.kind()) {
1874 case MIToken::GlobalValue:
1875 case MIToken::NamedGlobalValue: {
1876 GlobalValue *GV = nullptr;
1877 if (parseGlobalValue(GV))
1878 return true;
1879 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1880 break;
1881 }
1882 case MIToken::ExternalSymbol:
1883 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1884 MF.createExternalSymbolName(Token.stringValue()));
1885 break;
1886 default:
1887 return error(
1888 "expected a global value or an external symbol after 'call-entry'");
1889 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001890 break;
1891 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001892 default:
1893 llvm_unreachable("The current token should be pseudo source value");
1894 }
1895 lex();
1896 return false;
1897}
1898
1899bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001900 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001901 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Matthias Braun3ef7df92016-06-08 00:47:07 +00001902 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
1903 Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001904 const PseudoSourceValue *PSV = nullptr;
1905 if (parseMemoryPseudoSourceValue(PSV))
1906 return true;
1907 int64_t Offset = 0;
1908 if (parseOffset(Offset))
1909 return true;
1910 Dest = MachinePointerInfo(PSV, Offset);
1911 return false;
1912 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001913 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1914 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001915 Token.isNot(MIToken::NamedGlobalValue) &&
1916 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001917 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001918 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001919 if (parseIRValue(V))
1920 return true;
1921 if (!V->getType()->isPointerTy())
1922 return error("expected a pointer IR value");
1923 lex();
1924 int64_t Offset = 0;
1925 if (parseOffset(Offset))
1926 return true;
1927 Dest = MachinePointerInfo(V, Offset);
1928 return false;
1929}
1930
Alex Lorenz4af7e612015-08-03 23:08:19 +00001931bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1932 if (expectAndConsume(MIToken::lparen))
1933 return true;
Justin Lebar0af80cd2016-07-15 18:26:59 +00001934 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
Alex Lorenza518b792015-08-04 00:24:45 +00001935 while (Token.isMemoryOperandFlag()) {
1936 if (parseMemoryOperandFlag(Flags))
1937 return true;
1938 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001939 if (Token.isNot(MIToken::Identifier) ||
1940 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1941 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001942 if (Token.stringValue() == "load")
1943 Flags |= MachineMemOperand::MOLoad;
1944 else
1945 Flags |= MachineMemOperand::MOStore;
1946 lex();
1947
1948 if (Token.isNot(MIToken::IntegerLiteral))
1949 return error("expected the size integer literal after memory operation");
1950 uint64_t Size;
1951 if (getUint64(Size))
1952 return true;
1953 lex();
1954
Alex Lorenz91097a32015-08-12 20:33:26 +00001955 MachinePointerInfo Ptr = MachinePointerInfo();
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001956 if (Token.is(MIToken::Identifier)) {
1957 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1958 if (Token.stringValue() != Word)
1959 return error(Twine("expected '") + Word + "'");
1960 lex();
1961
1962 if (parseMachinePointerInfo(Ptr))
1963 return true;
1964 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001965 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001966 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001967 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001968 while (consumeIfPresent(MIToken::comma)) {
1969 switch (Token.kind()) {
1970 case MIToken::kw_align:
1971 if (parseAlignment(BaseAlignment))
1972 return true;
1973 break;
1974 case MIToken::md_tbaa:
1975 lex();
1976 if (parseMDNode(AAInfo.TBAA))
1977 return true;
1978 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001979 case MIToken::md_alias_scope:
1980 lex();
1981 if (parseMDNode(AAInfo.Scope))
1982 return true;
1983 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001984 case MIToken::md_noalias:
1985 lex();
1986 if (parseMDNode(AAInfo.NoAlias))
1987 return true;
1988 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001989 case MIToken::md_range:
1990 lex();
1991 if (parseMDNode(Range))
1992 return true;
1993 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001994 // TODO: Report an error on duplicate metadata nodes.
1995 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001996 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1997 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001998 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001999 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00002000 if (expectAndConsume(MIToken::rparen))
2001 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00002002 Dest =
2003 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00002004 return false;
2005}
2006
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002007void MIParser::initNames2InstrOpCodes() {
2008 if (!Names2InstrOpCodes.empty())
2009 return;
2010 const auto *TII = MF.getSubtarget().getInstrInfo();
2011 assert(TII && "Expected target instruction info");
2012 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
2013 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
2014}
2015
2016bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
2017 initNames2InstrOpCodes();
2018 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
2019 if (InstrInfo == Names2InstrOpCodes.end())
2020 return true;
2021 OpCode = InstrInfo->getValue();
2022 return false;
2023}
2024
Alex Lorenzf3db51de2015-06-23 16:35:26 +00002025void MIParser::initNames2Regs() {
2026 if (!Names2Regs.empty())
2027 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00002028 // The '%noreg' register is the register 0.
2029 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00002030 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2031 assert(TRI && "Expected target register info");
2032 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
2033 bool WasInserted =
2034 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
2035 .second;
2036 (void)WasInserted;
2037 assert(WasInserted && "Expected registers to be unique case-insensitively");
2038 }
2039}
2040
2041bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
2042 initNames2Regs();
2043 auto RegInfo = Names2Regs.find(RegName);
2044 if (RegInfo == Names2Regs.end())
2045 return true;
2046 Reg = RegInfo->getValue();
2047 return false;
2048}
2049
Alex Lorenz8f6f4282015-06-29 16:57:06 +00002050void MIParser::initNames2RegMasks() {
2051 if (!Names2RegMasks.empty())
2052 return;
2053 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2054 assert(TRI && "Expected target register info");
2055 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
2056 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
2057 assert(RegMasks.size() == RegMaskNames.size());
2058 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
2059 Names2RegMasks.insert(
2060 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
2061}
2062
2063const uint32_t *MIParser::getRegMask(StringRef Identifier) {
2064 initNames2RegMasks();
2065 auto RegMaskInfo = Names2RegMasks.find(Identifier);
2066 if (RegMaskInfo == Names2RegMasks.end())
2067 return nullptr;
2068 return RegMaskInfo->getValue();
2069}
2070
Alex Lorenz2eacca82015-07-13 23:24:34 +00002071void MIParser::initNames2SubRegIndices() {
2072 if (!Names2SubRegIndices.empty())
2073 return;
2074 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2075 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
2076 Names2SubRegIndices.insert(
2077 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
2078}
2079
2080unsigned MIParser::getSubRegIndex(StringRef Name) {
2081 initNames2SubRegIndices();
2082 auto SubRegInfo = Names2SubRegIndices.find(Name);
2083 if (SubRegInfo == Names2SubRegIndices.end())
2084 return 0;
2085 return SubRegInfo->getValue();
2086}
2087
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002088static void initSlots2BasicBlocks(
2089 const Function &F,
2090 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2091 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002092 MST.incorporateFunction(F);
2093 for (auto &BB : F) {
2094 if (BB.hasName())
2095 continue;
2096 int Slot = MST.getLocalSlot(&BB);
2097 if (Slot == -1)
2098 continue;
2099 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2100 }
2101}
2102
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002103static const BasicBlock *getIRBlockFromSlot(
2104 unsigned Slot,
2105 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002106 auto BlockInfo = Slots2BasicBlocks.find(Slot);
2107 if (BlockInfo == Slots2BasicBlocks.end())
2108 return nullptr;
2109 return BlockInfo->second;
2110}
2111
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002112const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2113 if (Slots2BasicBlocks.empty())
2114 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
2115 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2116}
2117
2118const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2119 if (&F == MF.getFunction())
2120 return getIRBlock(Slot);
2121 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2122 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2123 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2124}
2125
Alex Lorenzdd13be02015-08-19 23:31:05 +00002126static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2127 DenseMap<unsigned, const Value *> &Slots2Values) {
2128 int Slot = MST.getLocalSlot(V);
2129 if (Slot == -1)
2130 return;
2131 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2132}
2133
2134/// Creates the mapping from slot numbers to function's unnamed IR values.
2135static void initSlots2Values(const Function &F,
2136 DenseMap<unsigned, const Value *> &Slots2Values) {
2137 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2138 MST.incorporateFunction(F);
2139 for (const auto &Arg : F.args())
2140 mapValueToSlot(&Arg, MST, Slots2Values);
2141 for (const auto &BB : F) {
2142 mapValueToSlot(&BB, MST, Slots2Values);
2143 for (const auto &I : BB)
2144 mapValueToSlot(&I, MST, Slots2Values);
2145 }
2146}
2147
2148const Value *MIParser::getIRValue(unsigned Slot) {
2149 if (Slots2Values.empty())
2150 initSlots2Values(*MF.getFunction(), Slots2Values);
2151 auto ValueInfo = Slots2Values.find(Slot);
2152 if (ValueInfo == Slots2Values.end())
2153 return nullptr;
2154 return ValueInfo->second;
2155}
2156
Alex Lorenzef5c1962015-07-28 23:02:45 +00002157void MIParser::initNames2TargetIndices() {
2158 if (!Names2TargetIndices.empty())
2159 return;
2160 const auto *TII = MF.getSubtarget().getInstrInfo();
2161 assert(TII && "Expected target instruction info");
2162 auto Indices = TII->getSerializableTargetIndices();
2163 for (const auto &I : Indices)
2164 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2165}
2166
2167bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2168 initNames2TargetIndices();
2169 auto IndexInfo = Names2TargetIndices.find(Name);
2170 if (IndexInfo == Names2TargetIndices.end())
2171 return true;
2172 Index = IndexInfo->second;
2173 return false;
2174}
2175
Alex Lorenz49873a82015-08-06 00:44:07 +00002176void MIParser::initNames2DirectTargetFlags() {
2177 if (!Names2DirectTargetFlags.empty())
2178 return;
2179 const auto *TII = MF.getSubtarget().getInstrInfo();
2180 assert(TII && "Expected target instruction info");
2181 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2182 for (const auto &I : Flags)
2183 Names2DirectTargetFlags.insert(
2184 std::make_pair(StringRef(I.second), I.first));
2185}
2186
2187bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2188 initNames2DirectTargetFlags();
2189 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2190 if (FlagInfo == Names2DirectTargetFlags.end())
2191 return true;
2192 Flag = FlagInfo->second;
2193 return false;
2194}
2195
Alex Lorenzf3630112015-08-18 22:52:15 +00002196void MIParser::initNames2BitmaskTargetFlags() {
2197 if (!Names2BitmaskTargetFlags.empty())
2198 return;
2199 const auto *TII = MF.getSubtarget().getInstrInfo();
2200 assert(TII && "Expected target instruction info");
2201 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2202 for (const auto &I : Flags)
2203 Names2BitmaskTargetFlags.insert(
2204 std::make_pair(StringRef(I.second), I.first));
2205}
2206
2207bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2208 initNames2BitmaskTargetFlags();
2209 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2210 if (FlagInfo == Names2BitmaskTargetFlags.end())
2211 return true;
2212 Flag = FlagInfo->second;
2213 return false;
2214}
2215
Matthias Braun83947862016-07-13 22:23:23 +00002216bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2217 StringRef Src,
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002218 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002219 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002220}
2221
Matthias Braun74ad41c2016-10-11 03:13:01 +00002222bool llvm::parseMachineInstructions(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002223 StringRef Src, SMDiagnostic &Error) {
2224 return MIParser(PFS, Error, Src).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002225}
Alex Lorenzf09df002015-06-30 18:16:42 +00002226
Matthias Braun74ad41c2016-10-11 03:13:01 +00002227bool llvm::parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002228 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +00002229 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002230 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002231}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002232
Matthias Braun74ad41c2016-10-11 03:13:01 +00002233bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002234 unsigned &Reg, StringRef Src,
Alex Lorenz9fab3702015-07-14 21:24:41 +00002235 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002236 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002237}
Alex Lorenz12045a42015-07-27 17:42:45 +00002238
Matthias Braun74ad41c2016-10-11 03:13:01 +00002239bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState &PFS,
2240 VRegInfo *&Info, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +00002241 SMDiagnostic &Error) {
Matthias Braun74ad41c2016-10-11 03:13:01 +00002242 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
Alex Lorenz12045a42015-07-27 17:42:45 +00002243}
Alex Lorenza314d812015-08-18 22:26:26 +00002244
Matthias Braun74ad41c2016-10-11 03:13:01 +00002245bool llvm::parseStackObjectReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002246 int &FI, StringRef Src,
Alex Lorenza314d812015-08-18 22:26:26 +00002247 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002248 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
Alex Lorenza314d812015-08-18 22:26:26 +00002249}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002250
Matthias Braun74ad41c2016-10-11 03:13:01 +00002251bool llvm::parseMDNode(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002252 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2253 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002254}