blob: 2675254e4b9870e327504248e4b4fe50198f2347 [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"
Alex Lorenzad156fb2015-07-31 20:49:21 +000017#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000018#include "llvm/AsmParser/SlotMapping.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000019#include "llvm/CodeGen/MachineBasicBlock.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000021#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000022#include "llvm/CodeGen/MachineInstr.h"
Alex Lorenzcb268d42015-07-06 23:07:26 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000024#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000027#include "llvm/IR/Constants.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000028#include "llvm/IR/Instructions.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000029#include "llvm/IR/Module.h"
Alex Lorenz8a1915b2015-07-27 22:42:41 +000030#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000031#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000032#include "llvm/Support/SourceMgr.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000033#include "llvm/Support/raw_ostream.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000034#include "llvm/Target/TargetInstrInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000035#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000036
37using namespace llvm;
38
Matthias Braune35861d2016-07-13 23:27:50 +000039PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
40 SourceMgr &SM, const SlotMapping &IRSlots)
41 : MF(MF), SM(&SM), IRSlots(IRSlots) {
Matthias Braun83947862016-07-13 22:23:23 +000042}
43
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000044namespace {
45
Alex Lorenz36962cd2015-07-07 02:08:46 +000046/// A wrapper struct around the 'MachineOperand' struct that includes a source
Alex Lorenzfeb6b432015-08-19 19:19:16 +000047/// range and other attributes.
48struct ParsedMachineOperand {
Alex Lorenz36962cd2015-07-07 02:08:46 +000049 MachineOperand Operand;
50 StringRef::iterator Begin;
51 StringRef::iterator End;
Alex Lorenz5ef93b02015-08-19 19:05:34 +000052 Optional<unsigned> TiedDefIdx;
Alex Lorenz36962cd2015-07-07 02:08:46 +000053
Alex Lorenzfeb6b432015-08-19 19:19:16 +000054 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
55 StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
Alex Lorenz5ef93b02015-08-19 19:05:34 +000056 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
57 if (TiedDefIdx)
58 assert(Operand.isReg() && Operand.isUse() &&
59 "Only used register operands can be tied");
60 }
Alex Lorenz36962cd2015-07-07 02:08:46 +000061};
62
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000063class MIParser {
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000064 MachineFunction &MF;
65 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000066 StringRef Source, CurrentSource;
67 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000068 const PerFunctionMIParsingState &PFS;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000069 /// Maps from instruction names to op codes.
70 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000071 /// Maps from register names to registers.
72 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000073 /// Maps from register mask names to register masks.
74 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000075 /// Maps from subregister names to subregister indices.
76 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000077 /// Maps from slot numbers to function's unnamed basic blocks.
78 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzdd13be02015-08-19 23:31:05 +000079 /// Maps from slot numbers to function's unnamed values.
80 DenseMap<unsigned, const Value *> Slots2Values;
Alex Lorenzef5c1962015-07-28 23:02:45 +000081 /// Maps from target index names to target indices.
82 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +000083 /// Maps from direct target flag names to the direct target flag values.
84 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenzf3630112015-08-18 22:52:15 +000085 /// Maps from direct target flag names to the bitmask target flag values.
86 StringMap<unsigned> Names2BitmaskTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000087
88public:
Matthias Braune35861d2016-07-13 23:27:50 +000089 MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
90 StringRef Source);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000091
Quentin Colombet287c6bb2016-03-08 00:57:31 +000092 /// \p SkipChar gives the number of characters to skip before looking
93 /// for the next token.
94 void lex(unsigned SkipChar = 0);
Alex Lorenz91370c52015-06-22 20:37:46 +000095
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000096 /// Report an error at the current location with the given message.
97 ///
98 /// This function always return true.
99 bool error(const Twine &Msg);
100
Alex Lorenz91370c52015-06-22 20:37:46 +0000101 /// Report an error at the given location with the given message.
102 ///
103 /// This function always return true.
104 bool error(StringRef::iterator Loc, const Twine &Msg);
105
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000106 bool
107 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
108 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +0000109 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +0000110 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
111 bool parseStandaloneNamedRegister(unsigned &Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +0000112 bool parseStandaloneVirtualRegister(unsigned &Reg);
Alex Lorenza314d812015-08-18 22:26:26 +0000113 bool parseStandaloneStackObject(int &FI);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000114 bool parseStandaloneMDNode(MDNode *&Node);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000115
116 bool
117 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
118 bool parseBasicBlock(MachineBasicBlock &MBB);
119 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
120 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000121
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000122 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000123 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000124 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000125 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000126 bool parseSize(unsigned &Size);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000127 bool parseRegisterOperand(MachineOperand &Dest,
128 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000129 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000130 bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
131 const Constant *&C);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000132 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Ahmed Bougachad760de02016-07-28 17:15:12 +0000133 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
Alex Lorenz05e38822015-08-05 18:52:21 +0000134 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000135 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000136 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000137 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000138 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000139 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000140 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000141 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000142 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000143 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000144 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +0000145 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000146 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000147 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000148 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000149 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000150 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000151 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000152 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000153 bool parseIRBlock(BasicBlock *&BB, const Function &F);
154 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000155 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000156 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000157 bool parseMachineOperand(MachineOperand &Dest,
158 Optional<unsigned> &TiedDefIdx);
159 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
160 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000161 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000162 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000163 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000164 bool parseIRValue(const Value *&V);
Justin Lebar0af80cd2016-07-15 18:26:59 +0000165 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000166 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
167 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000168 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000169
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000170private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000171 /// Convert the integer literal in the current token into an unsigned integer.
172 ///
173 /// Return true if an error occurred.
174 bool getUnsigned(unsigned &Result);
175
Alex Lorenz4af7e612015-08-03 23:08:19 +0000176 /// Convert the integer literal in the current token into an uint64.
177 ///
178 /// Return true if an error occurred.
179 bool getUint64(uint64_t &Result);
180
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000181 /// If the current token is of the given kind, consume it and return false.
182 /// Otherwise report an error and return true.
183 bool expectAndConsume(MIToken::TokenKind TokenKind);
184
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000185 /// If the current token is of the given kind, consume it and return true.
186 /// Otherwise return false.
187 bool consumeIfPresent(MIToken::TokenKind TokenKind);
188
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000189 void initNames2InstrOpCodes();
190
191 /// Try to convert an instruction name to an opcode. Return true if the
192 /// instruction name is invalid.
193 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000194
Alex Lorenze5a44662015-07-17 00:24:15 +0000195 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000196
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000197 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000198 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000199
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000200 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000201 const MCInstrDesc &MCID);
202
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000203 void initNames2Regs();
204
205 /// Try to convert a register name to a register number. Return true if the
206 /// register name is invalid.
207 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000208
209 void initNames2RegMasks();
210
211 /// Check if the given identifier is a name of a register mask.
212 ///
213 /// Return null if the identifier isn't a register mask.
214 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000215
216 void initNames2SubRegIndices();
217
218 /// Check if the given identifier is a name of a subregister index.
219 ///
220 /// Return 0 if the name isn't a subregister index class.
221 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000222
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000223 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000224 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000225
Alex Lorenzdd13be02015-08-19 23:31:05 +0000226 const Value *getIRValue(unsigned Slot);
227
Alex Lorenzef5c1962015-07-28 23:02:45 +0000228 void initNames2TargetIndices();
229
230 /// Try to convert a name of target index to the corresponding target index.
231 ///
232 /// Return true if the name isn't a name of a target index.
233 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000234
235 void initNames2DirectTargetFlags();
236
237 /// Try to convert a name of a direct target flag to the corresponding
238 /// target flag.
239 ///
240 /// Return true if the name isn't a name of a direct flag.
241 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000242
243 void initNames2BitmaskTargetFlags();
244
245 /// Try to convert a name of a bitmask target flag to the corresponding
246 /// target flag.
247 ///
248 /// Return true if the name isn't a name of a bitmask target flag.
249 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000250};
251
252} // end anonymous namespace
253
Matthias Braune35861d2016-07-13 23:27:50 +0000254MIParser::MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
255 StringRef Source)
256 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
257{}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000258
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000259void MIParser::lex(unsigned SkipChar) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000260 CurrentSource = lexMIToken(
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000261 CurrentSource.data() + SkipChar, Token,
Alex Lorenz91370c52015-06-22 20:37:46 +0000262 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
263}
264
265bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
266
267bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Matthias Braune35861d2016-07-13 23:27:50 +0000268 const SourceMgr &SM = *PFS.SM;
Alex Lorenz91370c52015-06-22 20:37:46 +0000269 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000270 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
271 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
272 // Create an ordinary diagnostic when the source manager's buffer is the
273 // source string.
274 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
275 return true;
276 }
277 // Create a diagnostic for a YAML string literal.
278 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
279 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
280 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000281 return true;
282}
283
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000284static const char *toString(MIToken::TokenKind TokenKind) {
285 switch (TokenKind) {
286 case MIToken::comma:
287 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000288 case MIToken::equal:
289 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000290 case MIToken::colon:
291 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000292 case MIToken::lparen:
293 return "'('";
294 case MIToken::rparen:
295 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000296 default:
297 return "<unknown token>";
298 }
299}
300
301bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
302 if (Token.isNot(TokenKind))
303 return error(Twine("expected ") + toString(TokenKind));
304 lex();
305 return false;
306}
307
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000308bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
309 if (Token.isNot(TokenKind))
310 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000311 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000312 return true;
313}
Alex Lorenz91370c52015-06-22 20:37:46 +0000314
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000315bool MIParser::parseBasicBlockDefinition(
316 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
317 assert(Token.is(MIToken::MachineBasicBlockLabel));
318 unsigned ID = 0;
319 if (getUnsigned(ID))
320 return true;
321 auto Loc = Token.location();
322 auto Name = Token.stringValue();
323 lex();
324 bool HasAddressTaken = false;
325 bool IsLandingPad = false;
326 unsigned Alignment = 0;
327 BasicBlock *BB = nullptr;
328 if (consumeIfPresent(MIToken::lparen)) {
329 do {
330 // TODO: Report an error when multiple same attributes are specified.
331 switch (Token.kind()) {
332 case MIToken::kw_address_taken:
333 HasAddressTaken = true;
334 lex();
335 break;
336 case MIToken::kw_landing_pad:
337 IsLandingPad = true;
338 lex();
339 break;
340 case MIToken::kw_align:
341 if (parseAlignment(Alignment))
342 return true;
343 break;
344 case MIToken::IRBlock:
345 // TODO: Report an error when both name and ir block are specified.
346 if (parseIRBlock(BB, *MF.getFunction()))
347 return true;
348 lex();
349 break;
350 default:
351 break;
352 }
353 } while (consumeIfPresent(MIToken::comma));
354 if (expectAndConsume(MIToken::rparen))
355 return true;
356 }
357 if (expectAndConsume(MIToken::colon))
358 return true;
359
360 if (!Name.empty()) {
361 BB = dyn_cast_or_null<BasicBlock>(
362 MF.getFunction()->getValueSymbolTable().lookup(Name));
363 if (!BB)
364 return error(Loc, Twine("basic block '") + Name +
365 "' is not defined in the function '" +
366 MF.getName() + "'");
367 }
368 auto *MBB = MF.CreateMachineBasicBlock(BB);
369 MF.insert(MF.end(), MBB);
370 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
371 if (!WasInserted)
372 return error(Loc, Twine("redefinition of machine basic block with id #") +
373 Twine(ID));
374 if (Alignment)
375 MBB->setAlignment(Alignment);
376 if (HasAddressTaken)
377 MBB->setHasAddressTaken();
Reid Kleckner0e288232015-08-27 23:27:47 +0000378 MBB->setIsEHPad(IsLandingPad);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000379 return false;
380}
381
382bool MIParser::parseBasicBlockDefinitions(
383 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
384 lex();
385 // Skip until the first machine basic block.
386 while (Token.is(MIToken::Newline))
387 lex();
388 if (Token.isErrorOrEOF())
389 return Token.isError();
390 if (Token.isNot(MIToken::MachineBasicBlockLabel))
391 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000392 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000393 do {
394 if (parseBasicBlockDefinition(MBBSlots))
395 return true;
396 bool IsAfterNewline = false;
397 // Skip until the next machine basic block.
398 while (true) {
399 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
400 Token.isErrorOrEOF())
401 break;
402 else if (Token.is(MIToken::MachineBasicBlockLabel))
403 return error("basic block definition should be located at the start of "
404 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000405 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000406 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000407 continue;
408 }
409 IsAfterNewline = false;
410 if (Token.is(MIToken::lbrace))
411 ++BraceDepth;
412 if (Token.is(MIToken::rbrace)) {
413 if (!BraceDepth)
414 return error("extraneous closing brace ('}')");
415 --BraceDepth;
416 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000417 lex();
418 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000419 // Verify that we closed all of the '{' at the end of a file or a block.
420 if (!Token.isError() && BraceDepth)
421 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000422 } while (!Token.isErrorOrEOF());
423 return Token.isError();
424}
425
426bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
427 assert(Token.is(MIToken::kw_liveins));
428 lex();
429 if (expectAndConsume(MIToken::colon))
430 return true;
431 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
432 return false;
433 do {
434 if (Token.isNot(MIToken::NamedRegister))
435 return error("expected a named register");
436 unsigned Reg = 0;
437 if (parseRegister(Reg))
438 return true;
439 MBB.addLiveIn(Reg);
440 lex();
441 } while (consumeIfPresent(MIToken::comma));
442 return false;
443}
444
445bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
446 assert(Token.is(MIToken::kw_successors));
447 lex();
448 if (expectAndConsume(MIToken::colon))
449 return true;
450 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
451 return false;
452 do {
453 if (Token.isNot(MIToken::MachineBasicBlock))
454 return error("expected a machine basic block reference");
455 MachineBasicBlock *SuccMBB = nullptr;
456 if (parseMBBReference(SuccMBB))
457 return true;
458 lex();
459 unsigned Weight = 0;
460 if (consumeIfPresent(MIToken::lparen)) {
461 if (Token.isNot(MIToken::IntegerLiteral))
462 return error("expected an integer literal after '('");
463 if (getUnsigned(Weight))
464 return true;
465 lex();
466 if (expectAndConsume(MIToken::rparen))
467 return true;
468 }
Cong Houd97c1002015-12-01 05:29:22 +0000469 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000470 } while (consumeIfPresent(MIToken::comma));
Cong Houd97c1002015-12-01 05:29:22 +0000471 MBB.normalizeSuccProbs();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000472 return false;
473}
474
475bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
476 // Skip the definition.
477 assert(Token.is(MIToken::MachineBasicBlockLabel));
478 lex();
479 if (consumeIfPresent(MIToken::lparen)) {
480 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
481 lex();
482 consumeIfPresent(MIToken::rparen);
483 }
484 consumeIfPresent(MIToken::colon);
485
486 // Parse the liveins and successors.
487 // N.B: Multiple lists of successors and liveins are allowed and they're
488 // merged into one.
489 // Example:
490 // liveins: %edi
491 // liveins: %esi
492 //
493 // is equivalent to
494 // liveins: %edi, %esi
495 while (true) {
496 if (Token.is(MIToken::kw_successors)) {
497 if (parseBasicBlockSuccessors(MBB))
498 return true;
499 } else if (Token.is(MIToken::kw_liveins)) {
500 if (parseBasicBlockLiveins(MBB))
501 return true;
502 } else if (consumeIfPresent(MIToken::Newline)) {
503 continue;
504 } else
505 break;
506 if (!Token.isNewlineOrEOF())
507 return error("expected line break at the end of a list");
508 lex();
509 }
510
511 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000512 bool IsInBundle = false;
513 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000514 while (true) {
515 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
516 return false;
517 else if (consumeIfPresent(MIToken::Newline))
518 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000519 if (consumeIfPresent(MIToken::rbrace)) {
520 // The first parsing pass should verify that all closing '}' have an
521 // opening '{'.
522 assert(IsInBundle);
523 IsInBundle = false;
524 continue;
525 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000526 MachineInstr *MI = nullptr;
527 if (parse(MI))
528 return true;
529 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000530 if (IsInBundle) {
531 PrevMI->setFlag(MachineInstr::BundledSucc);
532 MI->setFlag(MachineInstr::BundledPred);
533 }
534 PrevMI = MI;
535 if (Token.is(MIToken::lbrace)) {
536 if (IsInBundle)
537 return error("nested instruction bundles are not allowed");
538 lex();
539 // This instruction is the start of the bundle.
540 MI->setFlag(MachineInstr::BundledSucc);
541 IsInBundle = true;
542 if (!Token.is(MIToken::Newline))
543 // The next instruction can be on the same line.
544 continue;
545 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000546 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
547 lex();
548 }
549 return false;
550}
551
552bool MIParser::parseBasicBlocks() {
553 lex();
554 // Skip until the first machine basic block.
555 while (Token.is(MIToken::Newline))
556 lex();
557 if (Token.isErrorOrEOF())
558 return Token.isError();
559 // The first parsing pass should have verified that this token is a MBB label
560 // in the 'parseBasicBlockDefinitions' method.
561 assert(Token.is(MIToken::MachineBasicBlockLabel));
562 do {
563 MachineBasicBlock *MBB = nullptr;
564 if (parseMBBReference(MBB))
565 return true;
566 if (parseBasicBlock(*MBB))
567 return true;
568 // The method 'parseBasicBlock' should parse the whole block until the next
569 // block or the end of file.
570 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
571 } while (Token.isNot(MIToken::Eof));
572 return false;
573}
574
575bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000576 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000577 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000578 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000579 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000580 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000581 Optional<unsigned> TiedDefIdx;
582 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000583 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000584 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000585 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000586 if (Token.isNot(MIToken::comma))
587 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000588 lex();
589 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000590 if (!Operands.empty() && expectAndConsume(MIToken::equal))
591 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000592
Alex Lorenze5a44662015-07-17 00:24:15 +0000593 unsigned OpCode, Flags = 0;
594 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000595 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000596
Tim Northover98a56eb2016-07-22 22:13:36 +0000597 SmallVector<LLT, 1> Tys;
Quentin Colombet85199672016-03-08 00:20:48 +0000598 if (isPreISelGenericOpcode(OpCode)) {
Tim Northover98a56eb2016-07-22 22:13:36 +0000599 // For generic opcode, at least one type is mandatory.
Tim Northover26e40bd2016-07-26 17:28:01 +0000600 auto Loc = Token.location();
601 bool ManyTypes = Token.is(MIToken::lbrace);
602 if (ManyTypes)
603 lex();
604
605 // Now actually parse the type(s).
Tim Northover98a56eb2016-07-22 22:13:36 +0000606 do {
Tim Northover98a56eb2016-07-22 22:13:36 +0000607 Tys.resize(Tys.size() + 1);
608 if (parseLowLevelType(Loc, Tys[Tys.size() - 1]))
609 return true;
Tim Northover26e40bd2016-07-26 17:28:01 +0000610 } while (ManyTypes && consumeIfPresent(MIToken::comma));
611
612 if (ManyTypes)
613 expectAndConsume(MIToken::rbrace);
Quentin Colombet85199672016-03-08 00:20:48 +0000614 }
615
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000616 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000617 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000618 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000619 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000620 Optional<unsigned> TiedDefIdx;
621 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000622 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000623 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000624 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000625 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
626 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000627 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000628 if (Token.isNot(MIToken::comma))
629 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000630 lex();
631 }
632
Alex Lorenz46d760d2015-07-22 21:15:11 +0000633 DebugLoc DebugLocation;
634 if (Token.is(MIToken::kw_debug_location)) {
635 lex();
636 if (Token.isNot(MIToken::exclaim))
637 return error("expected a metadata node after 'debug-location'");
638 MDNode *Node = nullptr;
639 if (parseMDNode(Node))
640 return true;
641 DebugLocation = DebugLoc(Node);
642 }
643
Alex Lorenz4af7e612015-08-03 23:08:19 +0000644 // Parse the machine memory operands.
645 SmallVector<MachineMemOperand *, 2> MemOperands;
646 if (Token.is(MIToken::coloncolon)) {
647 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000648 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000649 MachineMemOperand *MemOp = nullptr;
650 if (parseMachineMemoryOperand(MemOp))
651 return true;
652 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000653 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000654 break;
655 if (Token.isNot(MIToken::comma))
656 return error("expected ',' before the next machine memory operand");
657 lex();
658 }
659 }
660
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000661 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000662 if (!MCID.isVariadic()) {
663 // FIXME: Move the implicit operand verification to the machine verifier.
664 if (verifyImplicitOperands(Operands, MCID))
665 return true;
666 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000667
Alex Lorenzcb268d42015-07-06 23:07:26 +0000668 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000669 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000670 MI->setFlags(Flags);
Tim Northover98a56eb2016-07-22 22:13:36 +0000671 if (Tys.size() > 0) {
672 for (unsigned i = 0; i < Tys.size(); ++i)
673 MI->setType(Tys[i], i);
674 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000675 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000676 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000677 if (assignRegisterTies(*MI, Operands))
678 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000679 if (MemOperands.empty())
680 return false;
681 MachineInstr::mmo_iterator MemRefs =
682 MF.allocateMemRefsArray(MemOperands.size());
683 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
684 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000685 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000686}
687
Alex Lorenz1ea60892015-07-27 20:29:27 +0000688bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000689 lex();
690 if (Token.isNot(MIToken::MachineBasicBlock))
691 return error("expected a machine basic block reference");
692 if (parseMBBReference(MBB))
693 return true;
694 lex();
695 if (Token.isNot(MIToken::Eof))
696 return error(
697 "expected end of string after the machine basic block reference");
698 return false;
699}
700
Alex Lorenz1ea60892015-07-27 20:29:27 +0000701bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000702 lex();
703 if (Token.isNot(MIToken::NamedRegister))
704 return error("expected a named register");
705 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000706 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000707 lex();
708 if (Token.isNot(MIToken::Eof))
709 return error("expected end of string after the register reference");
710 return false;
711}
712
Alex Lorenz12045a42015-07-27 17:42:45 +0000713bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
714 lex();
715 if (Token.isNot(MIToken::VirtualRegister))
716 return error("expected a virtual register");
717 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000718 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000719 lex();
720 if (Token.isNot(MIToken::Eof))
721 return error("expected end of string after the register reference");
722 return false;
723}
724
Alex Lorenza314d812015-08-18 22:26:26 +0000725bool MIParser::parseStandaloneStackObject(int &FI) {
726 lex();
727 if (Token.isNot(MIToken::StackObject))
728 return error("expected a stack object");
729 if (parseStackFrameIndex(FI))
730 return true;
731 if (Token.isNot(MIToken::Eof))
732 return error("expected end of string after the stack object reference");
733 return false;
734}
735
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000736bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
737 lex();
738 if (Token.isNot(MIToken::exclaim))
739 return error("expected a metadata node");
740 if (parseMDNode(Node))
741 return true;
742 if (Token.isNot(MIToken::Eof))
743 return error("expected end of string after the metadata node");
744 return false;
745}
746
Alex Lorenz36962cd2015-07-07 02:08:46 +0000747static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
748 assert(MO.isImplicit());
749 return MO.isDef() ? "implicit-def" : "implicit";
750}
751
752static std::string getRegisterName(const TargetRegisterInfo *TRI,
753 unsigned Reg) {
754 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
755 return StringRef(TRI->getName(Reg)).lower();
756}
757
Alex Lorenz0153e592015-09-10 14:04:34 +0000758/// Return true if the parsed machine operands contain a given machine operand.
759static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
760 ArrayRef<ParsedMachineOperand> Operands) {
761 for (const auto &I : Operands) {
762 if (ImplicitOperand.isIdenticalTo(I.Operand))
763 return true;
764 }
765 return false;
766}
767
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000768bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
769 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000770 if (MCID.isCall())
771 // We can't verify call instructions as they can contain arbitrary implicit
772 // register and register mask operands.
773 return false;
774
775 // Gather all the expected implicit operands.
776 SmallVector<MachineOperand, 4> ImplicitOperands;
777 if (MCID.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000778 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000779 ImplicitOperands.push_back(
780 MachineOperand::CreateReg(*ImpDefs, true, true));
781 if (MCID.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000782 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000783 ImplicitOperands.push_back(
784 MachineOperand::CreateReg(*ImpUses, false, true));
785
786 const auto *TRI = MF.getSubtarget().getRegisterInfo();
787 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000788 for (const auto &I : ImplicitOperands) {
789 if (isImplicitOperandIn(I, Operands))
790 continue;
791 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000792 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000793 printImplicitRegisterFlag(I) + " %" +
794 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000795 }
796 return false;
797}
798
Alex Lorenze5a44662015-07-17 00:24:15 +0000799bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
800 if (Token.is(MIToken::kw_frame_setup)) {
801 Flags |= MachineInstr::FrameSetup;
802 lex();
803 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000804 if (Token.isNot(MIToken::Identifier))
805 return error("expected a machine instruction");
806 StringRef InstrName = Token.stringValue();
807 if (parseInstrName(InstrName, OpCode))
808 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000809 lex();
810 return false;
811}
812
813bool MIParser::parseRegister(unsigned &Reg) {
814 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000815 case MIToken::underscore:
816 Reg = 0;
817 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000818 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000819 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000820 if (getRegisterByName(Name, Reg))
821 return error(Twine("unknown register name '") + Name + "'");
822 break;
823 }
Alex Lorenz53464512015-07-10 22:51:20 +0000824 case MIToken::VirtualRegister: {
825 unsigned ID;
826 if (getUnsigned(ID))
827 return true;
828 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
829 if (RegInfo == PFS.VirtualRegisterSlots.end())
830 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
831 "'");
832 Reg = RegInfo->second;
833 break;
834 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000835 // TODO: Parse other register kinds.
836 default:
837 llvm_unreachable("The current token should be a register");
838 }
839 return false;
840}
841
Alex Lorenzcb268d42015-07-06 23:07:26 +0000842bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000843 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000844 switch (Token.kind()) {
845 case MIToken::kw_implicit:
846 Flags |= RegState::Implicit;
847 break;
848 case MIToken::kw_implicit_define:
849 Flags |= RegState::ImplicitDefine;
850 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000851 case MIToken::kw_def:
852 Flags |= RegState::Define;
853 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000854 case MIToken::kw_dead:
855 Flags |= RegState::Dead;
856 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000857 case MIToken::kw_killed:
858 Flags |= RegState::Kill;
859 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000860 case MIToken::kw_undef:
861 Flags |= RegState::Undef;
862 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000863 case MIToken::kw_internal:
864 Flags |= RegState::InternalRead;
865 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000866 case MIToken::kw_early_clobber:
867 Flags |= RegState::EarlyClobber;
868 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000869 case MIToken::kw_debug_use:
870 Flags |= RegState::Debug;
871 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000872 default:
873 llvm_unreachable("The current token should be a register flag");
874 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000875 if (OldFlags == Flags)
876 // We know that the same flag is specified more than once when the flags
877 // weren't modified.
878 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000879 lex();
880 return false;
881}
882
Alex Lorenz2eacca82015-07-13 23:24:34 +0000883bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
Matthias Braun333e4682016-07-26 21:49:34 +0000884 assert(Token.is(MIToken::dot));
Alex Lorenz2eacca82015-07-13 23:24:34 +0000885 lex();
886 if (Token.isNot(MIToken::Identifier))
Matthias Braun333e4682016-07-26 21:49:34 +0000887 return error("expected a subregister index after '.'");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000888 auto Name = Token.stringValue();
889 SubReg = getSubRegIndex(Name);
890 if (!SubReg)
891 return error(Twine("use of unknown subregister index '") + Name + "'");
892 lex();
893 return false;
894}
895
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000896bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
897 if (!consumeIfPresent(MIToken::kw_tied_def))
898 return error("expected 'tied-def' after '('");
899 if (Token.isNot(MIToken::IntegerLiteral))
900 return error("expected an integer literal after 'tied-def'");
901 if (getUnsigned(TiedDefIdx))
902 return true;
903 lex();
904 if (expectAndConsume(MIToken::rparen))
905 return true;
906 return false;
907}
908
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000909bool MIParser::parseSize(unsigned &Size) {
910 if (Token.isNot(MIToken::IntegerLiteral))
911 return error("expected an integer literal for the size");
912 if (getUnsigned(Size))
913 return true;
914 lex();
915 if (expectAndConsume(MIToken::rparen))
916 return true;
917 return false;
918}
919
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000920bool MIParser::assignRegisterTies(MachineInstr &MI,
921 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000922 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
923 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
924 if (!Operands[I].TiedDefIdx)
925 continue;
926 // The parser ensures that this operand is a register use, so we just have
927 // to check the tied-def operand.
928 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
929 if (DefIdx >= E)
930 return error(Operands[I].Begin,
931 Twine("use of invalid tied-def operand index '" +
932 Twine(DefIdx) + "'; instruction has only ") +
933 Twine(E) + " operands");
934 const auto &DefOperand = Operands[DefIdx].Operand;
935 if (!DefOperand.isReg() || !DefOperand.isDef())
936 // FIXME: add note with the def operand.
937 return error(Operands[I].Begin,
938 Twine("use of invalid tied-def operand index '") +
939 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
940 " isn't a defined register");
941 // Check that the tied-def operand wasn't tied elsewhere.
942 for (const auto &TiedPair : TiedRegisterPairs) {
943 if (TiedPair.first == DefIdx)
944 return error(Operands[I].Begin,
945 Twine("the tied-def operand #") + Twine(DefIdx) +
946 " is already tied with another register operand");
947 }
948 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
949 }
950 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
951 // indices must be less than tied max.
952 for (const auto &TiedPair : TiedRegisterPairs)
953 MI.tieOperands(TiedPair.first, TiedPair.second);
954 return false;
955}
956
957bool MIParser::parseRegisterOperand(MachineOperand &Dest,
958 Optional<unsigned> &TiedDefIdx,
959 bool IsDef) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000960 unsigned Reg;
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");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000968 if (parseRegister(Reg))
969 return true;
970 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000971 unsigned SubReg = 0;
Matthias Braun333e4682016-07-26 21:49:34 +0000972 if (Token.is(MIToken::dot)) {
Alex Lorenz2eacca82015-07-13 23:24:34 +0000973 if (parseSubRegisterIndex(SubReg))
974 return true;
Matthias Braun5d00b322016-07-16 01:36:18 +0000975 if (!TargetRegisterInfo::isVirtualRegister(Reg))
976 return error("subregister index expects a virtual register");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000977 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000978 if ((Flags & RegState::Define) == 0) {
979 if (consumeIfPresent(MIToken::lparen)) {
980 unsigned Idx;
981 if (parseRegisterTiedDefIndex(Idx))
982 return true;
983 TiedDefIdx = Idx;
984 }
985 } else if (consumeIfPresent(MIToken::lparen)) {
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000986 MachineRegisterInfo &MRI = MF.getRegInfo();
987
Quentin Colombet2c646962016-06-08 23:27:46 +0000988 // Virtual registers may have a size with GlobalISel.
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000989 if (!TargetRegisterInfo::isVirtualRegister(Reg))
990 return error("unexpected size on physical register");
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000991 if (MRI.getRegClassOrRegBank(Reg).is<const TargetRegisterClass *>())
992 return error("unexpected size on non-generic virtual register");
993
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000994 unsigned Size;
995 if (parseSize(Size))
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000996 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000997
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000998 MRI.setSize(Reg, Size);
Quentin Colombet2c646962016-06-08 23:27:46 +0000999 } else if (PFS.GenericVRegs.count(Reg)) {
1000 // Generic virtual registers must have a size.
1001 // If we end up here this means the size hasn't been specified and
1002 // this is bad!
1003 return error("generic virtual registers must have a size");
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001004 }
Alex Lorenz495ad872015-07-08 21:23:34 +00001005 Dest = MachineOperand::CreateReg(
1006 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +00001007 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +00001008 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1009 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001010 return false;
1011}
1012
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001013bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1014 assert(Token.is(MIToken::IntegerLiteral));
1015 const APSInt &Int = Token.integerValue();
1016 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +00001017 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001018 Dest = MachineOperand::CreateImm(Int.getExtValue());
1019 lex();
1020 return false;
1021}
1022
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001023bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1024 const Constant *&C) {
1025 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001026 SMDiagnostic Err;
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001027 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
Matthias Braune35861d2016-07-13 23:27:50 +00001028 &PFS.IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001029 if (!C)
1030 return error(Loc + Err.getColumnNo(), Err.getMessage());
1031 return false;
1032}
1033
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001034bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1035 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1036 return true;
1037 lex();
1038 return false;
1039}
1040
Ahmed Bougachad760de02016-07-28 17:15:12 +00001041bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
Tim Northover62ae5682016-07-20 19:09:30 +00001042 if (Token.is(MIToken::Identifier) && Token.stringValue() == "unsized") {
Tim Northover62ae5682016-07-20 19:09:30 +00001043 lex();
1044 Ty = LLT::unsized();
1045 return false;
1046 } else if (Token.is(MIToken::ScalarType)) {
1047 Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1048 lex();
1049 return false;
Tim Northoverbd505462016-07-22 16:59:52 +00001050 } else if (Token.is(MIToken::PointerType)) {
1051 Ty = LLT::pointer(APSInt(Token.range().drop_front()).getZExtValue());
1052 lex();
1053 return false;
Tim Northover62ae5682016-07-20 19:09:30 +00001054 }
Quentin Colombet85199672016-03-08 00:20:48 +00001055
Tim Northover62ae5682016-07-20 19:09:30 +00001056 // Now we're looking for a vector.
1057 if (Token.isNot(MIToken::less))
Tim Northoverbd505462016-07-22 16:59:52 +00001058 return error(Loc,
1059 "expected unsized, pN, sN or <N x sM> for GlobalISel type");
1060
Tim Northover62ae5682016-07-20 19:09:30 +00001061 lex();
1062
1063 if (Token.isNot(MIToken::IntegerLiteral))
1064 return error(Loc, "expected <N x sM> for vctor type");
1065 uint64_t NumElements = Token.integerValue().getZExtValue();
1066 lex();
1067
1068 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1069 return error(Loc, "expected '<N x sM>' for vector type");
1070 lex();
1071
1072 if (Token.isNot(MIToken::ScalarType))
1073 return error(Loc, "expected '<N x sM>' for vector type");
1074 uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1075 lex();
1076
1077 if (Token.isNot(MIToken::greater))
1078 return error(Loc, "expected '<N x sM>' for vector type");
1079 lex();
1080
1081 Ty = LLT::vector(NumElements, ScalarSize);
Quentin Colombet85199672016-03-08 00:20:48 +00001082 return false;
1083}
1084
Alex Lorenz05e38822015-08-05 18:52:21 +00001085bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1086 assert(Token.is(MIToken::IntegerType));
1087 auto Loc = Token.location();
1088 lex();
1089 if (Token.isNot(MIToken::IntegerLiteral))
1090 return error("expected an integer literal");
1091 const Constant *C = nullptr;
1092 if (parseIRConstant(Loc, C))
1093 return true;
1094 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1095 return false;
1096}
1097
Alex Lorenzad156fb2015-07-31 20:49:21 +00001098bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1099 auto Loc = Token.location();
1100 lex();
1101 if (Token.isNot(MIToken::FloatingPointLiteral))
1102 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001103 const Constant *C = nullptr;
1104 if (parseIRConstant(Loc, C))
1105 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001106 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1107 return false;
1108}
1109
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001110bool MIParser::getUnsigned(unsigned &Result) {
1111 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1112 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1113 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1114 if (Val64 == Limit)
1115 return error("expected 32-bit integer (too large)");
1116 Result = Val64;
1117 return false;
1118}
1119
Alex Lorenzf09df002015-06-30 18:16:42 +00001120bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001121 assert(Token.is(MIToken::MachineBasicBlock) ||
1122 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001123 unsigned Number;
1124 if (getUnsigned(Number))
1125 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001126 auto MBBInfo = PFS.MBBSlots.find(Number);
1127 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001128 return error(Twine("use of undefined machine basic block #") +
1129 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001130 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001131 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1132 return error(Twine("the name of machine basic block #") + Twine(Number) +
1133 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001134 return false;
1135}
1136
1137bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1138 MachineBasicBlock *MBB;
1139 if (parseMBBReference(MBB))
1140 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001141 Dest = MachineOperand::CreateMBB(MBB);
1142 lex();
1143 return false;
1144}
1145
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001146bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001147 assert(Token.is(MIToken::StackObject));
1148 unsigned ID;
1149 if (getUnsigned(ID))
1150 return true;
1151 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1152 if (ObjectInfo == PFS.StackObjectSlots.end())
1153 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1154 "'");
1155 StringRef Name;
1156 if (const auto *Alloca =
Matthias Braun941a7052016-07-28 18:40:00 +00001157 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001158 Name = Alloca->getName();
1159 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1160 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1161 "' isn't '" + Token.stringValue() + "'");
1162 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001163 FI = ObjectInfo->second;
1164 return false;
1165}
1166
1167bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1168 int FI;
1169 if (parseStackFrameIndex(FI))
1170 return true;
1171 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001172 return false;
1173}
1174
Alex Lorenzea882122015-08-12 21:17:02 +00001175bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001176 assert(Token.is(MIToken::FixedStackObject));
1177 unsigned ID;
1178 if (getUnsigned(ID))
1179 return true;
1180 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1181 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1182 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1183 Twine(ID) + "'");
1184 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001185 FI = ObjectInfo->second;
1186 return false;
1187}
1188
1189bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1190 int FI;
1191 if (parseFixedStackFrameIndex(FI))
1192 return true;
1193 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001194 return false;
1195}
1196
Alex Lorenz41df7d32015-07-28 17:09:52 +00001197bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001198 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001199 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001200 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001201 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001202 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001203 return error(Twine("use of undefined global value '") + Token.range() +
1204 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001205 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001206 }
1207 case MIToken::GlobalValue: {
1208 unsigned GVIdx;
1209 if (getUnsigned(GVIdx))
1210 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001211 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001212 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1213 "'");
Matthias Braune35861d2016-07-13 23:27:50 +00001214 GV = PFS.IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001215 break;
1216 }
1217 default:
1218 llvm_unreachable("The current token should be a global value");
1219 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001220 return false;
1221}
1222
1223bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1224 GlobalValue *GV = nullptr;
1225 if (parseGlobalValue(GV))
1226 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001227 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001228 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001229 if (parseOperandsOffset(Dest))
1230 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001231 return false;
1232}
1233
Alex Lorenzab980492015-07-20 20:51:18 +00001234bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1235 assert(Token.is(MIToken::ConstantPoolItem));
1236 unsigned ID;
1237 if (getUnsigned(ID))
1238 return true;
1239 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1240 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1241 return error("use of undefined constant '%const." + Twine(ID) + "'");
1242 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001243 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001244 if (parseOperandsOffset(Dest))
1245 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001246 return false;
1247}
1248
Alex Lorenz31d70682015-07-15 23:38:35 +00001249bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1250 assert(Token.is(MIToken::JumpTableIndex));
1251 unsigned ID;
1252 if (getUnsigned(ID))
1253 return true;
1254 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1255 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1256 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1257 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001258 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1259 return false;
1260}
1261
Alex Lorenz6ede3742015-07-21 16:59:53 +00001262bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001263 assert(Token.is(MIToken::ExternalSymbol));
1264 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001265 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001266 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001267 if (parseOperandsOffset(Dest))
1268 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001269 return false;
1270}
1271
Matthias Braunb74eb412016-03-28 18:18:46 +00001272bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1273 assert(Token.is(MIToken::SubRegisterIndex));
1274 StringRef Name = Token.stringValue();
1275 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1276 if (SubRegIndex == 0)
1277 return error(Twine("unknown subregister index '") + Name + "'");
1278 lex();
1279 Dest = MachineOperand::CreateImm(SubRegIndex);
1280 return false;
1281}
1282
Alex Lorenz44f29252015-07-22 21:07:04 +00001283bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001284 assert(Token.is(MIToken::exclaim));
1285 auto Loc = Token.location();
1286 lex();
1287 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1288 return error("expected metadata id after '!'");
1289 unsigned ID;
1290 if (getUnsigned(ID))
1291 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001292 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1293 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
Alex Lorenz35e44462015-07-22 17:58:46 +00001294 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1295 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001296 Node = NodeInfo->second.get();
1297 return false;
1298}
1299
1300bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1301 MDNode *Node = nullptr;
1302 if (parseMDNode(Node))
1303 return true;
1304 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001305 return false;
1306}
1307
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001308bool MIParser::parseCFIOffset(int &Offset) {
1309 if (Token.isNot(MIToken::IntegerLiteral))
1310 return error("expected a cfi offset");
1311 if (Token.integerValue().getMinSignedBits() > 32)
1312 return error("expected a 32 bit integer (the cfi offset is too large)");
1313 Offset = (int)Token.integerValue().getExtValue();
1314 lex();
1315 return false;
1316}
1317
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001318bool MIParser::parseCFIRegister(unsigned &Reg) {
1319 if (Token.isNot(MIToken::NamedRegister))
1320 return error("expected a cfi register");
1321 unsigned LLVMReg;
1322 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001323 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001324 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1325 assert(TRI && "Expected target register info");
1326 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1327 if (DwarfReg < 0)
1328 return error("invalid DWARF register");
1329 Reg = (unsigned)DwarfReg;
1330 lex();
1331 return false;
1332}
1333
1334bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1335 auto Kind = Token.kind();
1336 lex();
1337 auto &MMI = MF.getMMI();
1338 int Offset;
1339 unsigned Reg;
1340 unsigned CFIIndex;
1341 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001342 case MIToken::kw_cfi_same_value:
1343 if (parseCFIRegister(Reg))
1344 return true;
1345 CFIIndex =
1346 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1347 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001348 case MIToken::kw_cfi_offset:
1349 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1350 parseCFIOffset(Offset))
1351 return true;
1352 CFIIndex =
1353 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1354 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001355 case MIToken::kw_cfi_def_cfa_register:
1356 if (parseCFIRegister(Reg))
1357 return true;
1358 CFIIndex =
1359 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1360 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001361 case MIToken::kw_cfi_def_cfa_offset:
1362 if (parseCFIOffset(Offset))
1363 return true;
1364 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1365 CFIIndex = MMI.addFrameInst(
1366 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1367 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001368 case MIToken::kw_cfi_def_cfa:
1369 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1370 parseCFIOffset(Offset))
1371 return true;
1372 // NB: MCCFIInstruction::createDefCfa negates the offset.
1373 CFIIndex =
1374 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1375 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001376 default:
1377 // TODO: Parse the other CFI operands.
1378 llvm_unreachable("The current token should be a cfi operand");
1379 }
1380 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001381 return false;
1382}
1383
Alex Lorenzdeb53492015-07-28 17:28:03 +00001384bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1385 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001386 case MIToken::NamedIRBlock: {
1387 BB = dyn_cast_or_null<BasicBlock>(
1388 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001389 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001390 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001391 break;
1392 }
1393 case MIToken::IRBlock: {
1394 unsigned SlotNumber = 0;
1395 if (getUnsigned(SlotNumber))
1396 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001397 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001398 if (!BB)
1399 return error(Twine("use of undefined IR block '%ir-block.") +
1400 Twine(SlotNumber) + "'");
1401 break;
1402 }
1403 default:
1404 llvm_unreachable("The current token should be an IR block reference");
1405 }
1406 return false;
1407}
1408
1409bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1410 assert(Token.is(MIToken::kw_blockaddress));
1411 lex();
1412 if (expectAndConsume(MIToken::lparen))
1413 return true;
1414 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001415 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001416 return error("expected a global value");
1417 GlobalValue *GV = nullptr;
1418 if (parseGlobalValue(GV))
1419 return true;
1420 auto *F = dyn_cast<Function>(GV);
1421 if (!F)
1422 return error("expected an IR function reference");
1423 lex();
1424 if (expectAndConsume(MIToken::comma))
1425 return true;
1426 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001427 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001428 return error("expected an IR block reference");
1429 if (parseIRBlock(BB, *F))
1430 return true;
1431 lex();
1432 if (expectAndConsume(MIToken::rparen))
1433 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001434 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001435 if (parseOperandsOffset(Dest))
1436 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001437 return false;
1438}
1439
Alex Lorenzef5c1962015-07-28 23:02:45 +00001440bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1441 assert(Token.is(MIToken::kw_target_index));
1442 lex();
1443 if (expectAndConsume(MIToken::lparen))
1444 return true;
1445 if (Token.isNot(MIToken::Identifier))
1446 return error("expected the name of the target index");
1447 int Index = 0;
1448 if (getTargetIndex(Token.stringValue(), Index))
1449 return error("use of undefined target index '" + Token.stringValue() + "'");
1450 lex();
1451 if (expectAndConsume(MIToken::rparen))
1452 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001453 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001454 if (parseOperandsOffset(Dest))
1455 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001456 return false;
1457}
1458
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001459bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1460 assert(Token.is(MIToken::kw_liveout));
1461 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1462 assert(TRI && "Expected target register info");
1463 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1464 lex();
1465 if (expectAndConsume(MIToken::lparen))
1466 return true;
1467 while (true) {
1468 if (Token.isNot(MIToken::NamedRegister))
1469 return error("expected a named register");
1470 unsigned Reg = 0;
1471 if (parseRegister(Reg))
1472 return true;
1473 lex();
1474 Mask[Reg / 32] |= 1U << (Reg % 32);
1475 // TODO: Report an error if the same register is used more than once.
1476 if (Token.isNot(MIToken::comma))
1477 break;
1478 lex();
1479 }
1480 if (expectAndConsume(MIToken::rparen))
1481 return true;
1482 Dest = MachineOperand::CreateRegLiveOut(Mask);
1483 return false;
1484}
1485
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001486bool MIParser::parseMachineOperand(MachineOperand &Dest,
1487 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001488 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001489 case MIToken::kw_implicit:
1490 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001491 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001492 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001493 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001494 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001495 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001496 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001497 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001498 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001499 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001500 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001501 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001502 case MIToken::IntegerLiteral:
1503 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001504 case MIToken::IntegerType:
1505 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001506 case MIToken::kw_half:
1507 case MIToken::kw_float:
1508 case MIToken::kw_double:
1509 case MIToken::kw_x86_fp80:
1510 case MIToken::kw_fp128:
1511 case MIToken::kw_ppc_fp128:
1512 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001513 case MIToken::MachineBasicBlock:
1514 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001515 case MIToken::StackObject:
1516 return parseStackObjectOperand(Dest);
1517 case MIToken::FixedStackObject:
1518 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001519 case MIToken::GlobalValue:
1520 case MIToken::NamedGlobalValue:
1521 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001522 case MIToken::ConstantPoolItem:
1523 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001524 case MIToken::JumpTableIndex:
1525 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001526 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001527 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00001528 case MIToken::SubRegisterIndex:
1529 return parseSubRegisterIndexOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001530 case MIToken::exclaim:
1531 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001532 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001533 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001534 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001535 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001536 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001537 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001538 case MIToken::kw_blockaddress:
1539 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001540 case MIToken::kw_target_index:
1541 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001542 case MIToken::kw_liveout:
1543 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001544 case MIToken::Error:
1545 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001546 case MIToken::Identifier:
1547 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1548 Dest = MachineOperand::CreateRegMask(RegMask);
1549 lex();
1550 break;
1551 }
1552 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001553 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001554 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001555 return error("expected a machine operand");
1556 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001557 return false;
1558}
1559
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001560bool MIParser::parseMachineOperandAndTargetFlags(
1561 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001562 unsigned TF = 0;
1563 bool HasTargetFlags = false;
1564 if (Token.is(MIToken::kw_target_flags)) {
1565 HasTargetFlags = true;
1566 lex();
1567 if (expectAndConsume(MIToken::lparen))
1568 return true;
1569 if (Token.isNot(MIToken::Identifier))
1570 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001571 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1572 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1573 return error("use of undefined target flag '" + Token.stringValue() +
1574 "'");
1575 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001576 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001577 while (Token.is(MIToken::comma)) {
1578 lex();
1579 if (Token.isNot(MIToken::Identifier))
1580 return error("expected the name of the target flag");
1581 unsigned BitFlag = 0;
1582 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1583 return error("use of undefined target flag '" + Token.stringValue() +
1584 "'");
1585 // TODO: Report an error when using a duplicate bit target flag.
1586 TF |= BitFlag;
1587 lex();
1588 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001589 if (expectAndConsume(MIToken::rparen))
1590 return true;
1591 }
1592 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001593 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001594 return true;
1595 if (!HasTargetFlags)
1596 return false;
1597 if (Dest.isReg())
1598 return error(Loc, "register operands can't have target flags");
1599 Dest.setTargetFlags(TF);
1600 return false;
1601}
1602
Alex Lorenzdc24c172015-08-07 20:21:00 +00001603bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001604 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1605 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001606 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001607 bool IsNegative = Token.is(MIToken::minus);
1608 lex();
1609 if (Token.isNot(MIToken::IntegerLiteral))
1610 return error("expected an integer literal after '" + Sign + "'");
1611 if (Token.integerValue().getMinSignedBits() > 64)
1612 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001613 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001614 if (IsNegative)
1615 Offset = -Offset;
1616 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001617 return false;
1618}
1619
Alex Lorenz620f8912015-08-13 20:33:33 +00001620bool MIParser::parseAlignment(unsigned &Alignment) {
1621 assert(Token.is(MIToken::kw_align));
1622 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001623 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001624 return error("expected an integer literal after 'align'");
1625 if (getUnsigned(Alignment))
1626 return true;
1627 lex();
1628 return false;
1629}
1630
Alex Lorenzdc24c172015-08-07 20:21:00 +00001631bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1632 int64_t Offset = 0;
1633 if (parseOffset(Offset))
1634 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001635 Op.setOffset(Offset);
1636 return false;
1637}
1638
Alex Lorenz36593ac2015-08-19 23:27:07 +00001639bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001640 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001641 case MIToken::NamedIRValue: {
1642 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001643 break;
1644 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001645 case MIToken::IRValue: {
1646 unsigned SlotNumber = 0;
1647 if (getUnsigned(SlotNumber))
1648 return true;
1649 V = getIRValue(SlotNumber);
1650 break;
1651 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001652 case MIToken::NamedGlobalValue:
1653 case MIToken::GlobalValue: {
1654 GlobalValue *GV = nullptr;
1655 if (parseGlobalValue(GV))
1656 return true;
1657 V = GV;
1658 break;
1659 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001660 case MIToken::QuotedIRValue: {
1661 const Constant *C = nullptr;
1662 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1663 return true;
1664 V = C;
1665 break;
1666 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001667 default:
1668 llvm_unreachable("The current token should be an IR block reference");
1669 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001670 if (!V)
1671 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001672 return false;
1673}
1674
1675bool MIParser::getUint64(uint64_t &Result) {
1676 assert(Token.hasIntegerValue());
1677 if (Token.integerValue().getActiveBits() > 64)
1678 return error("expected 64-bit integer (too large)");
1679 Result = Token.integerValue().getZExtValue();
1680 return false;
1681}
1682
Justin Lebar0af80cd2016-07-15 18:26:59 +00001683bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
1684 const auto OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001685 switch (Token.kind()) {
1686 case MIToken::kw_volatile:
1687 Flags |= MachineMemOperand::MOVolatile;
1688 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001689 case MIToken::kw_non_temporal:
1690 Flags |= MachineMemOperand::MONonTemporal;
1691 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001692 case MIToken::kw_invariant:
1693 Flags |= MachineMemOperand::MOInvariant;
1694 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001695 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001696 default:
1697 llvm_unreachable("The current token should be a memory operand flag");
1698 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001699 if (OldFlags == Flags)
1700 // We know that the same flag is specified more than once when the flags
1701 // weren't modified.
1702 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001703 lex();
1704 return false;
1705}
1706
Alex Lorenz91097a32015-08-12 20:33:26 +00001707bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1708 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001709 case MIToken::kw_stack:
1710 PSV = MF.getPSVManager().getStack();
1711 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001712 case MIToken::kw_got:
1713 PSV = MF.getPSVManager().getGOT();
1714 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001715 case MIToken::kw_jump_table:
1716 PSV = MF.getPSVManager().getJumpTable();
1717 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001718 case MIToken::kw_constant_pool:
1719 PSV = MF.getPSVManager().getConstantPool();
1720 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001721 case MIToken::FixedStackObject: {
1722 int FI;
1723 if (parseFixedStackFrameIndex(FI))
1724 return true;
1725 PSV = MF.getPSVManager().getFixedStack(FI);
1726 // The token was already consumed, so use return here instead of break.
1727 return false;
1728 }
Matthias Braun3ef7df92016-06-08 00:47:07 +00001729 case MIToken::StackObject: {
1730 int FI;
1731 if (parseStackFrameIndex(FI))
1732 return true;
1733 PSV = MF.getPSVManager().getFixedStack(FI);
1734 // The token was already consumed, so use return here instead of break.
1735 return false;
1736 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001737 case MIToken::kw_call_entry: {
1738 lex();
1739 switch (Token.kind()) {
1740 case MIToken::GlobalValue:
1741 case MIToken::NamedGlobalValue: {
1742 GlobalValue *GV = nullptr;
1743 if (parseGlobalValue(GV))
1744 return true;
1745 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1746 break;
1747 }
1748 case MIToken::ExternalSymbol:
1749 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1750 MF.createExternalSymbolName(Token.stringValue()));
1751 break;
1752 default:
1753 return error(
1754 "expected a global value or an external symbol after 'call-entry'");
1755 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001756 break;
1757 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001758 default:
1759 llvm_unreachable("The current token should be pseudo source value");
1760 }
1761 lex();
1762 return false;
1763}
1764
1765bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001766 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001767 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Matthias Braun3ef7df92016-06-08 00:47:07 +00001768 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
1769 Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001770 const PseudoSourceValue *PSV = nullptr;
1771 if (parseMemoryPseudoSourceValue(PSV))
1772 return true;
1773 int64_t Offset = 0;
1774 if (parseOffset(Offset))
1775 return true;
1776 Dest = MachinePointerInfo(PSV, Offset);
1777 return false;
1778 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001779 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1780 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001781 Token.isNot(MIToken::NamedGlobalValue) &&
1782 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001783 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001784 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001785 if (parseIRValue(V))
1786 return true;
1787 if (!V->getType()->isPointerTy())
1788 return error("expected a pointer IR value");
1789 lex();
1790 int64_t Offset = 0;
1791 if (parseOffset(Offset))
1792 return true;
1793 Dest = MachinePointerInfo(V, Offset);
1794 return false;
1795}
1796
Alex Lorenz4af7e612015-08-03 23:08:19 +00001797bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1798 if (expectAndConsume(MIToken::lparen))
1799 return true;
Justin Lebar0af80cd2016-07-15 18:26:59 +00001800 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
Alex Lorenza518b792015-08-04 00:24:45 +00001801 while (Token.isMemoryOperandFlag()) {
1802 if (parseMemoryOperandFlag(Flags))
1803 return true;
1804 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001805 if (Token.isNot(MIToken::Identifier) ||
1806 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1807 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001808 if (Token.stringValue() == "load")
1809 Flags |= MachineMemOperand::MOLoad;
1810 else
1811 Flags |= MachineMemOperand::MOStore;
1812 lex();
1813
1814 if (Token.isNot(MIToken::IntegerLiteral))
1815 return error("expected the size integer literal after memory operation");
1816 uint64_t Size;
1817 if (getUint64(Size))
1818 return true;
1819 lex();
1820
Alex Lorenz91097a32015-08-12 20:33:26 +00001821 MachinePointerInfo Ptr = MachinePointerInfo();
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001822 if (Token.is(MIToken::Identifier)) {
1823 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1824 if (Token.stringValue() != Word)
1825 return error(Twine("expected '") + Word + "'");
1826 lex();
1827
1828 if (parseMachinePointerInfo(Ptr))
1829 return true;
1830 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001831 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001832 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001833 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001834 while (consumeIfPresent(MIToken::comma)) {
1835 switch (Token.kind()) {
1836 case MIToken::kw_align:
1837 if (parseAlignment(BaseAlignment))
1838 return true;
1839 break;
1840 case MIToken::md_tbaa:
1841 lex();
1842 if (parseMDNode(AAInfo.TBAA))
1843 return true;
1844 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001845 case MIToken::md_alias_scope:
1846 lex();
1847 if (parseMDNode(AAInfo.Scope))
1848 return true;
1849 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001850 case MIToken::md_noalias:
1851 lex();
1852 if (parseMDNode(AAInfo.NoAlias))
1853 return true;
1854 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001855 case MIToken::md_range:
1856 lex();
1857 if (parseMDNode(Range))
1858 return true;
1859 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001860 // TODO: Report an error on duplicate metadata nodes.
1861 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001862 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1863 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001864 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001865 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001866 if (expectAndConsume(MIToken::rparen))
1867 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001868 Dest =
1869 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001870 return false;
1871}
1872
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001873void MIParser::initNames2InstrOpCodes() {
1874 if (!Names2InstrOpCodes.empty())
1875 return;
1876 const auto *TII = MF.getSubtarget().getInstrInfo();
1877 assert(TII && "Expected target instruction info");
1878 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1879 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1880}
1881
1882bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1883 initNames2InstrOpCodes();
1884 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1885 if (InstrInfo == Names2InstrOpCodes.end())
1886 return true;
1887 OpCode = InstrInfo->getValue();
1888 return false;
1889}
1890
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001891void MIParser::initNames2Regs() {
1892 if (!Names2Regs.empty())
1893 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001894 // The '%noreg' register is the register 0.
1895 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001896 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1897 assert(TRI && "Expected target register info");
1898 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1899 bool WasInserted =
1900 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1901 .second;
1902 (void)WasInserted;
1903 assert(WasInserted && "Expected registers to be unique case-insensitively");
1904 }
1905}
1906
1907bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1908 initNames2Regs();
1909 auto RegInfo = Names2Regs.find(RegName);
1910 if (RegInfo == Names2Regs.end())
1911 return true;
1912 Reg = RegInfo->getValue();
1913 return false;
1914}
1915
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001916void MIParser::initNames2RegMasks() {
1917 if (!Names2RegMasks.empty())
1918 return;
1919 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1920 assert(TRI && "Expected target register info");
1921 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1922 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1923 assert(RegMasks.size() == RegMaskNames.size());
1924 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1925 Names2RegMasks.insert(
1926 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1927}
1928
1929const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1930 initNames2RegMasks();
1931 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1932 if (RegMaskInfo == Names2RegMasks.end())
1933 return nullptr;
1934 return RegMaskInfo->getValue();
1935}
1936
Alex Lorenz2eacca82015-07-13 23:24:34 +00001937void MIParser::initNames2SubRegIndices() {
1938 if (!Names2SubRegIndices.empty())
1939 return;
1940 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1941 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1942 Names2SubRegIndices.insert(
1943 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1944}
1945
1946unsigned MIParser::getSubRegIndex(StringRef Name) {
1947 initNames2SubRegIndices();
1948 auto SubRegInfo = Names2SubRegIndices.find(Name);
1949 if (SubRegInfo == Names2SubRegIndices.end())
1950 return 0;
1951 return SubRegInfo->getValue();
1952}
1953
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001954static void initSlots2BasicBlocks(
1955 const Function &F,
1956 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1957 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001958 MST.incorporateFunction(F);
1959 for (auto &BB : F) {
1960 if (BB.hasName())
1961 continue;
1962 int Slot = MST.getLocalSlot(&BB);
1963 if (Slot == -1)
1964 continue;
1965 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1966 }
1967}
1968
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001969static const BasicBlock *getIRBlockFromSlot(
1970 unsigned Slot,
1971 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001972 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1973 if (BlockInfo == Slots2BasicBlocks.end())
1974 return nullptr;
1975 return BlockInfo->second;
1976}
1977
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001978const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1979 if (Slots2BasicBlocks.empty())
1980 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1981 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1982}
1983
1984const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1985 if (&F == MF.getFunction())
1986 return getIRBlock(Slot);
1987 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1988 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1989 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1990}
1991
Alex Lorenzdd13be02015-08-19 23:31:05 +00001992static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
1993 DenseMap<unsigned, const Value *> &Slots2Values) {
1994 int Slot = MST.getLocalSlot(V);
1995 if (Slot == -1)
1996 return;
1997 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
1998}
1999
2000/// Creates the mapping from slot numbers to function's unnamed IR values.
2001static void initSlots2Values(const Function &F,
2002 DenseMap<unsigned, const Value *> &Slots2Values) {
2003 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2004 MST.incorporateFunction(F);
2005 for (const auto &Arg : F.args())
2006 mapValueToSlot(&Arg, MST, Slots2Values);
2007 for (const auto &BB : F) {
2008 mapValueToSlot(&BB, MST, Slots2Values);
2009 for (const auto &I : BB)
2010 mapValueToSlot(&I, MST, Slots2Values);
2011 }
2012}
2013
2014const Value *MIParser::getIRValue(unsigned Slot) {
2015 if (Slots2Values.empty())
2016 initSlots2Values(*MF.getFunction(), Slots2Values);
2017 auto ValueInfo = Slots2Values.find(Slot);
2018 if (ValueInfo == Slots2Values.end())
2019 return nullptr;
2020 return ValueInfo->second;
2021}
2022
Alex Lorenzef5c1962015-07-28 23:02:45 +00002023void MIParser::initNames2TargetIndices() {
2024 if (!Names2TargetIndices.empty())
2025 return;
2026 const auto *TII = MF.getSubtarget().getInstrInfo();
2027 assert(TII && "Expected target instruction info");
2028 auto Indices = TII->getSerializableTargetIndices();
2029 for (const auto &I : Indices)
2030 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2031}
2032
2033bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2034 initNames2TargetIndices();
2035 auto IndexInfo = Names2TargetIndices.find(Name);
2036 if (IndexInfo == Names2TargetIndices.end())
2037 return true;
2038 Index = IndexInfo->second;
2039 return false;
2040}
2041
Alex Lorenz49873a82015-08-06 00:44:07 +00002042void MIParser::initNames2DirectTargetFlags() {
2043 if (!Names2DirectTargetFlags.empty())
2044 return;
2045 const auto *TII = MF.getSubtarget().getInstrInfo();
2046 assert(TII && "Expected target instruction info");
2047 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2048 for (const auto &I : Flags)
2049 Names2DirectTargetFlags.insert(
2050 std::make_pair(StringRef(I.second), I.first));
2051}
2052
2053bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2054 initNames2DirectTargetFlags();
2055 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2056 if (FlagInfo == Names2DirectTargetFlags.end())
2057 return true;
2058 Flag = FlagInfo->second;
2059 return false;
2060}
2061
Alex Lorenzf3630112015-08-18 22:52:15 +00002062void MIParser::initNames2BitmaskTargetFlags() {
2063 if (!Names2BitmaskTargetFlags.empty())
2064 return;
2065 const auto *TII = MF.getSubtarget().getInstrInfo();
2066 assert(TII && "Expected target instruction info");
2067 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2068 for (const auto &I : Flags)
2069 Names2BitmaskTargetFlags.insert(
2070 std::make_pair(StringRef(I.second), I.first));
2071}
2072
2073bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2074 initNames2BitmaskTargetFlags();
2075 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2076 if (FlagInfo == Names2BitmaskTargetFlags.end())
2077 return true;
2078 Flag = FlagInfo->second;
2079 return false;
2080}
2081
Matthias Braun83947862016-07-13 22:23:23 +00002082bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2083 StringRef Src,
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002084 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002085 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002086}
2087
Matthias Braun83947862016-07-13 22:23:23 +00002088bool llvm::parseMachineInstructions(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002089 StringRef Src, SMDiagnostic &Error) {
2090 return MIParser(PFS, Error, Src).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002091}
Alex Lorenzf09df002015-06-30 18:16:42 +00002092
Matthias Braun83947862016-07-13 22:23:23 +00002093bool llvm::parseMBBReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002094 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +00002095 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002096 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002097}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002098
Matthias Braun83947862016-07-13 22:23:23 +00002099bool llvm::parseNamedRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002100 unsigned &Reg, StringRef Src,
Alex Lorenz9fab3702015-07-14 21:24:41 +00002101 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002102 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002103}
Alex Lorenz12045a42015-07-27 17:42:45 +00002104
Matthias Braun83947862016-07-13 22:23:23 +00002105bool llvm::parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002106 unsigned &Reg, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +00002107 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002108 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +00002109}
Alex Lorenza314d812015-08-18 22:26:26 +00002110
Matthias Braun83947862016-07-13 22:23:23 +00002111bool llvm::parseStackObjectReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002112 int &FI, StringRef Src,
Alex Lorenza314d812015-08-18 22:26:26 +00002113 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002114 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
Alex Lorenza314d812015-08-18 22:26:26 +00002115}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002116
Matthias Braun83947862016-07-13 22:23:23 +00002117bool llvm::parseMDNode(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002118 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2119 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002120}