blob: b1292bb48b09ed484ecbdc06d4d5c4ec4f81bde2 [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);
Tim Northover62ae5682016-07-20 19:09:30 +0000133 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty,
134 bool MustBeSized = true);
Alex Lorenz05e38822015-08-05 18:52:21 +0000135 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000136 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000137 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000138 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000139 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000140 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000141 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000142 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000143 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000144 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000145 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +0000146 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000147 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000148 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000149 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000150 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000151 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000152 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000153 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000154 bool parseIRBlock(BasicBlock *&BB, const Function &F);
155 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000156 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000157 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000158 bool parseMachineOperand(MachineOperand &Dest,
159 Optional<unsigned> &TiedDefIdx);
160 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
161 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000162 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000163 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000164 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000165 bool parseIRValue(const Value *&V);
Justin Lebar0af80cd2016-07-15 18:26:59 +0000166 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000167 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
168 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000169 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000170
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000171private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000172 /// Convert the integer literal in the current token into an unsigned integer.
173 ///
174 /// Return true if an error occurred.
175 bool getUnsigned(unsigned &Result);
176
Alex Lorenz4af7e612015-08-03 23:08:19 +0000177 /// Convert the integer literal in the current token into an uint64.
178 ///
179 /// Return true if an error occurred.
180 bool getUint64(uint64_t &Result);
181
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000182 /// If the current token is of the given kind, consume it and return false.
183 /// Otherwise report an error and return true.
184 bool expectAndConsume(MIToken::TokenKind TokenKind);
185
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000186 /// If the current token is of the given kind, consume it and return true.
187 /// Otherwise return false.
188 bool consumeIfPresent(MIToken::TokenKind TokenKind);
189
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000190 void initNames2InstrOpCodes();
191
192 /// Try to convert an instruction name to an opcode. Return true if the
193 /// instruction name is invalid.
194 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000195
Alex Lorenze5a44662015-07-17 00:24:15 +0000196 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000197
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000198 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000199 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000200
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000201 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000202 const MCInstrDesc &MCID);
203
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000204 void initNames2Regs();
205
206 /// Try to convert a register name to a register number. Return true if the
207 /// register name is invalid.
208 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000209
210 void initNames2RegMasks();
211
212 /// Check if the given identifier is a name of a register mask.
213 ///
214 /// Return null if the identifier isn't a register mask.
215 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000216
217 void initNames2SubRegIndices();
218
219 /// Check if the given identifier is a name of a subregister index.
220 ///
221 /// Return 0 if the name isn't a subregister index class.
222 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000223
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000224 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000225 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000226
Alex Lorenzdd13be02015-08-19 23:31:05 +0000227 const Value *getIRValue(unsigned Slot);
228
Alex Lorenzef5c1962015-07-28 23:02:45 +0000229 void initNames2TargetIndices();
230
231 /// Try to convert a name of target index to the corresponding target index.
232 ///
233 /// Return true if the name isn't a name of a target index.
234 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000235
236 void initNames2DirectTargetFlags();
237
238 /// Try to convert a name of a direct target flag to the corresponding
239 /// target flag.
240 ///
241 /// Return true if the name isn't a name of a direct flag.
242 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000243
244 void initNames2BitmaskTargetFlags();
245
246 /// Try to convert a name of a bitmask target flag to the corresponding
247 /// target flag.
248 ///
249 /// Return true if the name isn't a name of a bitmask target flag.
250 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000251};
252
253} // end anonymous namespace
254
Matthias Braune35861d2016-07-13 23:27:50 +0000255MIParser::MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
256 StringRef Source)
257 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
258{}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000259
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000260void MIParser::lex(unsigned SkipChar) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000261 CurrentSource = lexMIToken(
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000262 CurrentSource.data() + SkipChar, Token,
Alex Lorenz91370c52015-06-22 20:37:46 +0000263 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
264}
265
266bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
267
268bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Matthias Braune35861d2016-07-13 23:27:50 +0000269 const SourceMgr &SM = *PFS.SM;
Alex Lorenz91370c52015-06-22 20:37:46 +0000270 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000271 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
272 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
273 // Create an ordinary diagnostic when the source manager's buffer is the
274 // source string.
275 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
276 return true;
277 }
278 // Create a diagnostic for a YAML string literal.
279 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
280 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
281 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000282 return true;
283}
284
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000285static const char *toString(MIToken::TokenKind TokenKind) {
286 switch (TokenKind) {
287 case MIToken::comma:
288 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000289 case MIToken::equal:
290 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000291 case MIToken::colon:
292 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000293 case MIToken::lparen:
294 return "'('";
295 case MIToken::rparen:
296 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000297 default:
298 return "<unknown token>";
299 }
300}
301
302bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
303 if (Token.isNot(TokenKind))
304 return error(Twine("expected ") + toString(TokenKind));
305 lex();
306 return false;
307}
308
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000309bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
310 if (Token.isNot(TokenKind))
311 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000312 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000313 return true;
314}
Alex Lorenz91370c52015-06-22 20:37:46 +0000315
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000316bool MIParser::parseBasicBlockDefinition(
317 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
318 assert(Token.is(MIToken::MachineBasicBlockLabel));
319 unsigned ID = 0;
320 if (getUnsigned(ID))
321 return true;
322 auto Loc = Token.location();
323 auto Name = Token.stringValue();
324 lex();
325 bool HasAddressTaken = false;
326 bool IsLandingPad = false;
327 unsigned Alignment = 0;
328 BasicBlock *BB = nullptr;
329 if (consumeIfPresent(MIToken::lparen)) {
330 do {
331 // TODO: Report an error when multiple same attributes are specified.
332 switch (Token.kind()) {
333 case MIToken::kw_address_taken:
334 HasAddressTaken = true;
335 lex();
336 break;
337 case MIToken::kw_landing_pad:
338 IsLandingPad = true;
339 lex();
340 break;
341 case MIToken::kw_align:
342 if (parseAlignment(Alignment))
343 return true;
344 break;
345 case MIToken::IRBlock:
346 // TODO: Report an error when both name and ir block are specified.
347 if (parseIRBlock(BB, *MF.getFunction()))
348 return true;
349 lex();
350 break;
351 default:
352 break;
353 }
354 } while (consumeIfPresent(MIToken::comma));
355 if (expectAndConsume(MIToken::rparen))
356 return true;
357 }
358 if (expectAndConsume(MIToken::colon))
359 return true;
360
361 if (!Name.empty()) {
362 BB = dyn_cast_or_null<BasicBlock>(
363 MF.getFunction()->getValueSymbolTable().lookup(Name));
364 if (!BB)
365 return error(Loc, Twine("basic block '") + Name +
366 "' is not defined in the function '" +
367 MF.getName() + "'");
368 }
369 auto *MBB = MF.CreateMachineBasicBlock(BB);
370 MF.insert(MF.end(), MBB);
371 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
372 if (!WasInserted)
373 return error(Loc, Twine("redefinition of machine basic block with id #") +
374 Twine(ID));
375 if (Alignment)
376 MBB->setAlignment(Alignment);
377 if (HasAddressTaken)
378 MBB->setHasAddressTaken();
Reid Kleckner0e288232015-08-27 23:27:47 +0000379 MBB->setIsEHPad(IsLandingPad);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000380 return false;
381}
382
383bool MIParser::parseBasicBlockDefinitions(
384 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
385 lex();
386 // Skip until the first machine basic block.
387 while (Token.is(MIToken::Newline))
388 lex();
389 if (Token.isErrorOrEOF())
390 return Token.isError();
391 if (Token.isNot(MIToken::MachineBasicBlockLabel))
392 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000393 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000394 do {
395 if (parseBasicBlockDefinition(MBBSlots))
396 return true;
397 bool IsAfterNewline = false;
398 // Skip until the next machine basic block.
399 while (true) {
400 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
401 Token.isErrorOrEOF())
402 break;
403 else if (Token.is(MIToken::MachineBasicBlockLabel))
404 return error("basic block definition should be located at the start of "
405 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000406 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000407 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000408 continue;
409 }
410 IsAfterNewline = false;
411 if (Token.is(MIToken::lbrace))
412 ++BraceDepth;
413 if (Token.is(MIToken::rbrace)) {
414 if (!BraceDepth)
415 return error("extraneous closing brace ('}')");
416 --BraceDepth;
417 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000418 lex();
419 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000420 // Verify that we closed all of the '{' at the end of a file or a block.
421 if (!Token.isError() && BraceDepth)
422 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000423 } while (!Token.isErrorOrEOF());
424 return Token.isError();
425}
426
427bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
428 assert(Token.is(MIToken::kw_liveins));
429 lex();
430 if (expectAndConsume(MIToken::colon))
431 return true;
432 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
433 return false;
434 do {
435 if (Token.isNot(MIToken::NamedRegister))
436 return error("expected a named register");
437 unsigned Reg = 0;
438 if (parseRegister(Reg))
439 return true;
440 MBB.addLiveIn(Reg);
441 lex();
442 } while (consumeIfPresent(MIToken::comma));
443 return false;
444}
445
446bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
447 assert(Token.is(MIToken::kw_successors));
448 lex();
449 if (expectAndConsume(MIToken::colon))
450 return true;
451 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
452 return false;
453 do {
454 if (Token.isNot(MIToken::MachineBasicBlock))
455 return error("expected a machine basic block reference");
456 MachineBasicBlock *SuccMBB = nullptr;
457 if (parseMBBReference(SuccMBB))
458 return true;
459 lex();
460 unsigned Weight = 0;
461 if (consumeIfPresent(MIToken::lparen)) {
462 if (Token.isNot(MIToken::IntegerLiteral))
463 return error("expected an integer literal after '('");
464 if (getUnsigned(Weight))
465 return true;
466 lex();
467 if (expectAndConsume(MIToken::rparen))
468 return true;
469 }
Cong Houd97c1002015-12-01 05:29:22 +0000470 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000471 } while (consumeIfPresent(MIToken::comma));
Cong Houd97c1002015-12-01 05:29:22 +0000472 MBB.normalizeSuccProbs();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000473 return false;
474}
475
476bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
477 // Skip the definition.
478 assert(Token.is(MIToken::MachineBasicBlockLabel));
479 lex();
480 if (consumeIfPresent(MIToken::lparen)) {
481 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
482 lex();
483 consumeIfPresent(MIToken::rparen);
484 }
485 consumeIfPresent(MIToken::colon);
486
487 // Parse the liveins and successors.
488 // N.B: Multiple lists of successors and liveins are allowed and they're
489 // merged into one.
490 // Example:
491 // liveins: %edi
492 // liveins: %esi
493 //
494 // is equivalent to
495 // liveins: %edi, %esi
496 while (true) {
497 if (Token.is(MIToken::kw_successors)) {
498 if (parseBasicBlockSuccessors(MBB))
499 return true;
500 } else if (Token.is(MIToken::kw_liveins)) {
501 if (parseBasicBlockLiveins(MBB))
502 return true;
503 } else if (consumeIfPresent(MIToken::Newline)) {
504 continue;
505 } else
506 break;
507 if (!Token.isNewlineOrEOF())
508 return error("expected line break at the end of a list");
509 lex();
510 }
511
512 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000513 bool IsInBundle = false;
514 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000515 while (true) {
516 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
517 return false;
518 else if (consumeIfPresent(MIToken::Newline))
519 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000520 if (consumeIfPresent(MIToken::rbrace)) {
521 // The first parsing pass should verify that all closing '}' have an
522 // opening '{'.
523 assert(IsInBundle);
524 IsInBundle = false;
525 continue;
526 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000527 MachineInstr *MI = nullptr;
528 if (parse(MI))
529 return true;
530 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000531 if (IsInBundle) {
532 PrevMI->setFlag(MachineInstr::BundledSucc);
533 MI->setFlag(MachineInstr::BundledPred);
534 }
535 PrevMI = MI;
536 if (Token.is(MIToken::lbrace)) {
537 if (IsInBundle)
538 return error("nested instruction bundles are not allowed");
539 lex();
540 // This instruction is the start of the bundle.
541 MI->setFlag(MachineInstr::BundledSucc);
542 IsInBundle = true;
543 if (!Token.is(MIToken::Newline))
544 // The next instruction can be on the same line.
545 continue;
546 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000547 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
548 lex();
549 }
550 return false;
551}
552
553bool MIParser::parseBasicBlocks() {
554 lex();
555 // Skip until the first machine basic block.
556 while (Token.is(MIToken::Newline))
557 lex();
558 if (Token.isErrorOrEOF())
559 return Token.isError();
560 // The first parsing pass should have verified that this token is a MBB label
561 // in the 'parseBasicBlockDefinitions' method.
562 assert(Token.is(MIToken::MachineBasicBlockLabel));
563 do {
564 MachineBasicBlock *MBB = nullptr;
565 if (parseMBBReference(MBB))
566 return true;
567 if (parseBasicBlock(*MBB))
568 return true;
569 // The method 'parseBasicBlock' should parse the whole block until the next
570 // block or the end of file.
571 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
572 } while (Token.isNot(MIToken::Eof));
573 return false;
574}
575
576bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000577 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000578 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000579 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000580 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000581 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000582 Optional<unsigned> TiedDefIdx;
583 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000584 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000585 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000586 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000587 if (Token.isNot(MIToken::comma))
588 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000589 lex();
590 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000591 if (!Operands.empty() && expectAndConsume(MIToken::equal))
592 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000593
Alex Lorenze5a44662015-07-17 00:24:15 +0000594 unsigned OpCode, Flags = 0;
595 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000596 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000597
Tim Northover98a56eb2016-07-22 22:13:36 +0000598 SmallVector<LLT, 1> Tys;
Quentin Colombet85199672016-03-08 00:20:48 +0000599 if (isPreISelGenericOpcode(OpCode)) {
Tim Northover98a56eb2016-07-22 22:13:36 +0000600 // For generic opcode, at least one type is mandatory.
601 expectAndConsume(MIToken::lbrace);
602 do {
603 auto Loc = Token.location();
604 Tys.resize(Tys.size() + 1);
605 if (parseLowLevelType(Loc, Tys[Tys.size() - 1]))
606 return true;
607 } while (consumeIfPresent(MIToken::comma));
608 expectAndConsume(MIToken::rbrace);
Quentin Colombet85199672016-03-08 00:20:48 +0000609 }
610
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000611 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000612 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000613 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000614 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000615 Optional<unsigned> TiedDefIdx;
616 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000617 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000618 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000619 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000620 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
621 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000622 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000623 if (Token.isNot(MIToken::comma))
624 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000625 lex();
626 }
627
Alex Lorenz46d760d2015-07-22 21:15:11 +0000628 DebugLoc DebugLocation;
629 if (Token.is(MIToken::kw_debug_location)) {
630 lex();
631 if (Token.isNot(MIToken::exclaim))
632 return error("expected a metadata node after 'debug-location'");
633 MDNode *Node = nullptr;
634 if (parseMDNode(Node))
635 return true;
636 DebugLocation = DebugLoc(Node);
637 }
638
Alex Lorenz4af7e612015-08-03 23:08:19 +0000639 // Parse the machine memory operands.
640 SmallVector<MachineMemOperand *, 2> MemOperands;
641 if (Token.is(MIToken::coloncolon)) {
642 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000643 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000644 MachineMemOperand *MemOp = nullptr;
645 if (parseMachineMemoryOperand(MemOp))
646 return true;
647 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000648 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000649 break;
650 if (Token.isNot(MIToken::comma))
651 return error("expected ',' before the next machine memory operand");
652 lex();
653 }
654 }
655
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000656 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000657 if (!MCID.isVariadic()) {
658 // FIXME: Move the implicit operand verification to the machine verifier.
659 if (verifyImplicitOperands(Operands, MCID))
660 return true;
661 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000662
Alex Lorenzcb268d42015-07-06 23:07:26 +0000663 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000664 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000665 MI->setFlags(Flags);
Tim Northover98a56eb2016-07-22 22:13:36 +0000666 if (Tys.size() > 0) {
667 for (unsigned i = 0; i < Tys.size(); ++i)
668 MI->setType(Tys[i], i);
669 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000670 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000671 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000672 if (assignRegisterTies(*MI, Operands))
673 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000674 if (MemOperands.empty())
675 return false;
676 MachineInstr::mmo_iterator MemRefs =
677 MF.allocateMemRefsArray(MemOperands.size());
678 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
679 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000680 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000681}
682
Alex Lorenz1ea60892015-07-27 20:29:27 +0000683bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000684 lex();
685 if (Token.isNot(MIToken::MachineBasicBlock))
686 return error("expected a machine basic block reference");
687 if (parseMBBReference(MBB))
688 return true;
689 lex();
690 if (Token.isNot(MIToken::Eof))
691 return error(
692 "expected end of string after the machine basic block reference");
693 return false;
694}
695
Alex Lorenz1ea60892015-07-27 20:29:27 +0000696bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000697 lex();
698 if (Token.isNot(MIToken::NamedRegister))
699 return error("expected a named register");
700 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000701 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000702 lex();
703 if (Token.isNot(MIToken::Eof))
704 return error("expected end of string after the register reference");
705 return false;
706}
707
Alex Lorenz12045a42015-07-27 17:42:45 +0000708bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
709 lex();
710 if (Token.isNot(MIToken::VirtualRegister))
711 return error("expected a virtual register");
712 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000713 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000714 lex();
715 if (Token.isNot(MIToken::Eof))
716 return error("expected end of string after the register reference");
717 return false;
718}
719
Alex Lorenza314d812015-08-18 22:26:26 +0000720bool MIParser::parseStandaloneStackObject(int &FI) {
721 lex();
722 if (Token.isNot(MIToken::StackObject))
723 return error("expected a stack object");
724 if (parseStackFrameIndex(FI))
725 return true;
726 if (Token.isNot(MIToken::Eof))
727 return error("expected end of string after the stack object reference");
728 return false;
729}
730
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000731bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
732 lex();
733 if (Token.isNot(MIToken::exclaim))
734 return error("expected a metadata node");
735 if (parseMDNode(Node))
736 return true;
737 if (Token.isNot(MIToken::Eof))
738 return error("expected end of string after the metadata node");
739 return false;
740}
741
Alex Lorenz36962cd2015-07-07 02:08:46 +0000742static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
743 assert(MO.isImplicit());
744 return MO.isDef() ? "implicit-def" : "implicit";
745}
746
747static std::string getRegisterName(const TargetRegisterInfo *TRI,
748 unsigned Reg) {
749 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
750 return StringRef(TRI->getName(Reg)).lower();
751}
752
Alex Lorenz0153e592015-09-10 14:04:34 +0000753/// Return true if the parsed machine operands contain a given machine operand.
754static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
755 ArrayRef<ParsedMachineOperand> Operands) {
756 for (const auto &I : Operands) {
757 if (ImplicitOperand.isIdenticalTo(I.Operand))
758 return true;
759 }
760 return false;
761}
762
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000763bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
764 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000765 if (MCID.isCall())
766 // We can't verify call instructions as they can contain arbitrary implicit
767 // register and register mask operands.
768 return false;
769
770 // Gather all the expected implicit operands.
771 SmallVector<MachineOperand, 4> ImplicitOperands;
772 if (MCID.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000773 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000774 ImplicitOperands.push_back(
775 MachineOperand::CreateReg(*ImpDefs, true, true));
776 if (MCID.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000777 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000778 ImplicitOperands.push_back(
779 MachineOperand::CreateReg(*ImpUses, false, true));
780
781 const auto *TRI = MF.getSubtarget().getRegisterInfo();
782 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000783 for (const auto &I : ImplicitOperands) {
784 if (isImplicitOperandIn(I, Operands))
785 continue;
786 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000787 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000788 printImplicitRegisterFlag(I) + " %" +
789 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000790 }
791 return false;
792}
793
Alex Lorenze5a44662015-07-17 00:24:15 +0000794bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
795 if (Token.is(MIToken::kw_frame_setup)) {
796 Flags |= MachineInstr::FrameSetup;
797 lex();
798 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000799 if (Token.isNot(MIToken::Identifier))
800 return error("expected a machine instruction");
801 StringRef InstrName = Token.stringValue();
802 if (parseInstrName(InstrName, OpCode))
803 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000804 lex();
805 return false;
806}
807
808bool MIParser::parseRegister(unsigned &Reg) {
809 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000810 case MIToken::underscore:
811 Reg = 0;
812 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000813 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000814 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000815 if (getRegisterByName(Name, Reg))
816 return error(Twine("unknown register name '") + Name + "'");
817 break;
818 }
Alex Lorenz53464512015-07-10 22:51:20 +0000819 case MIToken::VirtualRegister: {
820 unsigned ID;
821 if (getUnsigned(ID))
822 return true;
823 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
824 if (RegInfo == PFS.VirtualRegisterSlots.end())
825 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
826 "'");
827 Reg = RegInfo->second;
828 break;
829 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000830 // TODO: Parse other register kinds.
831 default:
832 llvm_unreachable("The current token should be a register");
833 }
834 return false;
835}
836
Alex Lorenzcb268d42015-07-06 23:07:26 +0000837bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000838 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000839 switch (Token.kind()) {
840 case MIToken::kw_implicit:
841 Flags |= RegState::Implicit;
842 break;
843 case MIToken::kw_implicit_define:
844 Flags |= RegState::ImplicitDefine;
845 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000846 case MIToken::kw_def:
847 Flags |= RegState::Define;
848 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000849 case MIToken::kw_dead:
850 Flags |= RegState::Dead;
851 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000852 case MIToken::kw_killed:
853 Flags |= RegState::Kill;
854 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000855 case MIToken::kw_undef:
856 Flags |= RegState::Undef;
857 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000858 case MIToken::kw_internal:
859 Flags |= RegState::InternalRead;
860 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000861 case MIToken::kw_early_clobber:
862 Flags |= RegState::EarlyClobber;
863 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000864 case MIToken::kw_debug_use:
865 Flags |= RegState::Debug;
866 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000867 default:
868 llvm_unreachable("The current token should be a register flag");
869 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000870 if (OldFlags == Flags)
871 // We know that the same flag is specified more than once when the flags
872 // weren't modified.
873 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000874 lex();
875 return false;
876}
877
Alex Lorenz2eacca82015-07-13 23:24:34 +0000878bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
879 assert(Token.is(MIToken::colon));
880 lex();
881 if (Token.isNot(MIToken::Identifier))
882 return error("expected a subregister index after ':'");
883 auto Name = Token.stringValue();
884 SubReg = getSubRegIndex(Name);
885 if (!SubReg)
886 return error(Twine("use of unknown subregister index '") + Name + "'");
887 lex();
888 return false;
889}
890
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000891bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
892 if (!consumeIfPresent(MIToken::kw_tied_def))
893 return error("expected 'tied-def' after '('");
894 if (Token.isNot(MIToken::IntegerLiteral))
895 return error("expected an integer literal after 'tied-def'");
896 if (getUnsigned(TiedDefIdx))
897 return true;
898 lex();
899 if (expectAndConsume(MIToken::rparen))
900 return true;
901 return false;
902}
903
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000904bool MIParser::parseSize(unsigned &Size) {
905 if (Token.isNot(MIToken::IntegerLiteral))
906 return error("expected an integer literal for the size");
907 if (getUnsigned(Size))
908 return true;
909 lex();
910 if (expectAndConsume(MIToken::rparen))
911 return true;
912 return false;
913}
914
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000915bool MIParser::assignRegisterTies(MachineInstr &MI,
916 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000917 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
918 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
919 if (!Operands[I].TiedDefIdx)
920 continue;
921 // The parser ensures that this operand is a register use, so we just have
922 // to check the tied-def operand.
923 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
924 if (DefIdx >= E)
925 return error(Operands[I].Begin,
926 Twine("use of invalid tied-def operand index '" +
927 Twine(DefIdx) + "'; instruction has only ") +
928 Twine(E) + " operands");
929 const auto &DefOperand = Operands[DefIdx].Operand;
930 if (!DefOperand.isReg() || !DefOperand.isDef())
931 // FIXME: add note with the def operand.
932 return error(Operands[I].Begin,
933 Twine("use of invalid tied-def operand index '") +
934 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
935 " isn't a defined register");
936 // Check that the tied-def operand wasn't tied elsewhere.
937 for (const auto &TiedPair : TiedRegisterPairs) {
938 if (TiedPair.first == DefIdx)
939 return error(Operands[I].Begin,
940 Twine("the tied-def operand #") + Twine(DefIdx) +
941 " is already tied with another register operand");
942 }
943 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
944 }
945 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
946 // indices must be less than tied max.
947 for (const auto &TiedPair : TiedRegisterPairs)
948 MI.tieOperands(TiedPair.first, TiedPair.second);
949 return false;
950}
951
952bool MIParser::parseRegisterOperand(MachineOperand &Dest,
953 Optional<unsigned> &TiedDefIdx,
954 bool IsDef) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000955 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000956 unsigned Flags = IsDef ? RegState::Define : 0;
957 while (Token.isRegisterFlag()) {
958 if (parseRegisterFlag(Flags))
959 return true;
960 }
961 if (!Token.isRegister())
962 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000963 if (parseRegister(Reg))
964 return true;
965 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000966 unsigned SubReg = 0;
967 if (Token.is(MIToken::colon)) {
968 if (parseSubRegisterIndex(SubReg))
969 return true;
Matthias Braun5d00b322016-07-16 01:36:18 +0000970 if (!TargetRegisterInfo::isVirtualRegister(Reg))
971 return error("subregister index expects a virtual register");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000972 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000973 if ((Flags & RegState::Define) == 0) {
974 if (consumeIfPresent(MIToken::lparen)) {
975 unsigned Idx;
976 if (parseRegisterTiedDefIndex(Idx))
977 return true;
978 TiedDefIdx = Idx;
979 }
980 } else if (consumeIfPresent(MIToken::lparen)) {
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000981 MachineRegisterInfo &MRI = MF.getRegInfo();
982
Quentin Colombet2c646962016-06-08 23:27:46 +0000983 // Virtual registers may have a size with GlobalISel.
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000984 if (!TargetRegisterInfo::isVirtualRegister(Reg))
985 return error("unexpected size on physical register");
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000986 if (MRI.getRegClassOrRegBank(Reg).is<const TargetRegisterClass *>())
987 return error("unexpected size on non-generic virtual register");
988
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000989 unsigned Size;
990 if (parseSize(Size))
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000991 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000992
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000993 MRI.setSize(Reg, Size);
Quentin Colombet2c646962016-06-08 23:27:46 +0000994 } else if (PFS.GenericVRegs.count(Reg)) {
995 // Generic virtual registers must have a size.
996 // If we end up here this means the size hasn't been specified and
997 // this is bad!
998 return error("generic virtual registers must have a size");
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000999 }
Alex Lorenz495ad872015-07-08 21:23:34 +00001000 Dest = MachineOperand::CreateReg(
1001 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +00001002 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +00001003 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1004 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001005 return false;
1006}
1007
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001008bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1009 assert(Token.is(MIToken::IntegerLiteral));
1010 const APSInt &Int = Token.integerValue();
1011 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +00001012 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001013 Dest = MachineOperand::CreateImm(Int.getExtValue());
1014 lex();
1015 return false;
1016}
1017
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001018bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1019 const Constant *&C) {
1020 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001021 SMDiagnostic Err;
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001022 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
Matthias Braune35861d2016-07-13 23:27:50 +00001023 &PFS.IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001024 if (!C)
1025 return error(Loc + Err.getColumnNo(), Err.getMessage());
1026 return false;
1027}
1028
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001029bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1030 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1031 return true;
1032 lex();
1033 return false;
1034}
1035
Tim Northover62ae5682016-07-20 19:09:30 +00001036bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty,
1037 bool MustBeSized) {
1038 if (Token.is(MIToken::Identifier) && Token.stringValue() == "unsized") {
1039 if (MustBeSized)
Tim Northoverbd505462016-07-22 16:59:52 +00001040 return error(Loc, "expected pN, sN or <N x sM> for sized GlobalISel type");
Tim Northover62ae5682016-07-20 19:09:30 +00001041 lex();
1042 Ty = LLT::unsized();
1043 return false;
1044 } else if (Token.is(MIToken::ScalarType)) {
1045 Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1046 lex();
1047 return false;
Tim Northoverbd505462016-07-22 16:59:52 +00001048 } else if (Token.is(MIToken::PointerType)) {
1049 Ty = LLT::pointer(APSInt(Token.range().drop_front()).getZExtValue());
1050 lex();
1051 return false;
Tim Northover62ae5682016-07-20 19:09:30 +00001052 }
Quentin Colombet85199672016-03-08 00:20:48 +00001053
Tim Northover62ae5682016-07-20 19:09:30 +00001054 // Now we're looking for a vector.
1055 if (Token.isNot(MIToken::less))
Tim Northoverbd505462016-07-22 16:59:52 +00001056 return error(Loc,
1057 "expected unsized, pN, sN or <N x sM> for GlobalISel type");
1058
Tim Northover62ae5682016-07-20 19:09:30 +00001059 lex();
1060
1061 if (Token.isNot(MIToken::IntegerLiteral))
1062 return error(Loc, "expected <N x sM> for vctor type");
1063 uint64_t NumElements = Token.integerValue().getZExtValue();
1064 lex();
1065
1066 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1067 return error(Loc, "expected '<N x sM>' for vector type");
1068 lex();
1069
1070 if (Token.isNot(MIToken::ScalarType))
1071 return error(Loc, "expected '<N x sM>' for vector type");
1072 uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1073 lex();
1074
1075 if (Token.isNot(MIToken::greater))
1076 return error(Loc, "expected '<N x sM>' for vector type");
1077 lex();
1078
1079 Ty = LLT::vector(NumElements, ScalarSize);
Quentin Colombet85199672016-03-08 00:20:48 +00001080 return false;
1081}
1082
Alex Lorenz05e38822015-08-05 18:52:21 +00001083bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1084 assert(Token.is(MIToken::IntegerType));
1085 auto Loc = Token.location();
1086 lex();
1087 if (Token.isNot(MIToken::IntegerLiteral))
1088 return error("expected an integer literal");
1089 const Constant *C = nullptr;
1090 if (parseIRConstant(Loc, C))
1091 return true;
1092 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1093 return false;
1094}
1095
Alex Lorenzad156fb2015-07-31 20:49:21 +00001096bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1097 auto Loc = Token.location();
1098 lex();
1099 if (Token.isNot(MIToken::FloatingPointLiteral))
1100 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001101 const Constant *C = nullptr;
1102 if (parseIRConstant(Loc, C))
1103 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001104 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1105 return false;
1106}
1107
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001108bool MIParser::getUnsigned(unsigned &Result) {
1109 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1110 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1111 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1112 if (Val64 == Limit)
1113 return error("expected 32-bit integer (too large)");
1114 Result = Val64;
1115 return false;
1116}
1117
Alex Lorenzf09df002015-06-30 18:16:42 +00001118bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001119 assert(Token.is(MIToken::MachineBasicBlock) ||
1120 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001121 unsigned Number;
1122 if (getUnsigned(Number))
1123 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001124 auto MBBInfo = PFS.MBBSlots.find(Number);
1125 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001126 return error(Twine("use of undefined machine basic block #") +
1127 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001128 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001129 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1130 return error(Twine("the name of machine basic block #") + Twine(Number) +
1131 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001132 return false;
1133}
1134
1135bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1136 MachineBasicBlock *MBB;
1137 if (parseMBBReference(MBB))
1138 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001139 Dest = MachineOperand::CreateMBB(MBB);
1140 lex();
1141 return false;
1142}
1143
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001144bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001145 assert(Token.is(MIToken::StackObject));
1146 unsigned ID;
1147 if (getUnsigned(ID))
1148 return true;
1149 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1150 if (ObjectInfo == PFS.StackObjectSlots.end())
1151 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1152 "'");
1153 StringRef Name;
1154 if (const auto *Alloca =
1155 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
1156 Name = Alloca->getName();
1157 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1158 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1159 "' isn't '" + Token.stringValue() + "'");
1160 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001161 FI = ObjectInfo->second;
1162 return false;
1163}
1164
1165bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1166 int FI;
1167 if (parseStackFrameIndex(FI))
1168 return true;
1169 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001170 return false;
1171}
1172
Alex Lorenzea882122015-08-12 21:17:02 +00001173bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001174 assert(Token.is(MIToken::FixedStackObject));
1175 unsigned ID;
1176 if (getUnsigned(ID))
1177 return true;
1178 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1179 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1180 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1181 Twine(ID) + "'");
1182 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001183 FI = ObjectInfo->second;
1184 return false;
1185}
1186
1187bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1188 int FI;
1189 if (parseFixedStackFrameIndex(FI))
1190 return true;
1191 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001192 return false;
1193}
1194
Alex Lorenz41df7d32015-07-28 17:09:52 +00001195bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001196 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001197 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001198 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001199 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001200 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001201 return error(Twine("use of undefined global value '") + Token.range() +
1202 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001203 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001204 }
1205 case MIToken::GlobalValue: {
1206 unsigned GVIdx;
1207 if (getUnsigned(GVIdx))
1208 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001209 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001210 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1211 "'");
Matthias Braune35861d2016-07-13 23:27:50 +00001212 GV = PFS.IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001213 break;
1214 }
1215 default:
1216 llvm_unreachable("The current token should be a global value");
1217 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001218 return false;
1219}
1220
1221bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1222 GlobalValue *GV = nullptr;
1223 if (parseGlobalValue(GV))
1224 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001225 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001226 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001227 if (parseOperandsOffset(Dest))
1228 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001229 return false;
1230}
1231
Alex Lorenzab980492015-07-20 20:51:18 +00001232bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1233 assert(Token.is(MIToken::ConstantPoolItem));
1234 unsigned ID;
1235 if (getUnsigned(ID))
1236 return true;
1237 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1238 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1239 return error("use of undefined constant '%const." + Twine(ID) + "'");
1240 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001241 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001242 if (parseOperandsOffset(Dest))
1243 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001244 return false;
1245}
1246
Alex Lorenz31d70682015-07-15 23:38:35 +00001247bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1248 assert(Token.is(MIToken::JumpTableIndex));
1249 unsigned ID;
1250 if (getUnsigned(ID))
1251 return true;
1252 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1253 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1254 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1255 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001256 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1257 return false;
1258}
1259
Alex Lorenz6ede3742015-07-21 16:59:53 +00001260bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001261 assert(Token.is(MIToken::ExternalSymbol));
1262 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001263 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001264 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001265 if (parseOperandsOffset(Dest))
1266 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001267 return false;
1268}
1269
Matthias Braunb74eb412016-03-28 18:18:46 +00001270bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1271 assert(Token.is(MIToken::SubRegisterIndex));
1272 StringRef Name = Token.stringValue();
1273 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1274 if (SubRegIndex == 0)
1275 return error(Twine("unknown subregister index '") + Name + "'");
1276 lex();
1277 Dest = MachineOperand::CreateImm(SubRegIndex);
1278 return false;
1279}
1280
Alex Lorenz44f29252015-07-22 21:07:04 +00001281bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001282 assert(Token.is(MIToken::exclaim));
1283 auto Loc = Token.location();
1284 lex();
1285 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1286 return error("expected metadata id after '!'");
1287 unsigned ID;
1288 if (getUnsigned(ID))
1289 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001290 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1291 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
Alex Lorenz35e44462015-07-22 17:58:46 +00001292 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1293 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001294 Node = NodeInfo->second.get();
1295 return false;
1296}
1297
1298bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1299 MDNode *Node = nullptr;
1300 if (parseMDNode(Node))
1301 return true;
1302 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001303 return false;
1304}
1305
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001306bool MIParser::parseCFIOffset(int &Offset) {
1307 if (Token.isNot(MIToken::IntegerLiteral))
1308 return error("expected a cfi offset");
1309 if (Token.integerValue().getMinSignedBits() > 32)
1310 return error("expected a 32 bit integer (the cfi offset is too large)");
1311 Offset = (int)Token.integerValue().getExtValue();
1312 lex();
1313 return false;
1314}
1315
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001316bool MIParser::parseCFIRegister(unsigned &Reg) {
1317 if (Token.isNot(MIToken::NamedRegister))
1318 return error("expected a cfi register");
1319 unsigned LLVMReg;
1320 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001321 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001322 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1323 assert(TRI && "Expected target register info");
1324 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1325 if (DwarfReg < 0)
1326 return error("invalid DWARF register");
1327 Reg = (unsigned)DwarfReg;
1328 lex();
1329 return false;
1330}
1331
1332bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1333 auto Kind = Token.kind();
1334 lex();
1335 auto &MMI = MF.getMMI();
1336 int Offset;
1337 unsigned Reg;
1338 unsigned CFIIndex;
1339 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001340 case MIToken::kw_cfi_same_value:
1341 if (parseCFIRegister(Reg))
1342 return true;
1343 CFIIndex =
1344 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1345 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001346 case MIToken::kw_cfi_offset:
1347 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1348 parseCFIOffset(Offset))
1349 return true;
1350 CFIIndex =
1351 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1352 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001353 case MIToken::kw_cfi_def_cfa_register:
1354 if (parseCFIRegister(Reg))
1355 return true;
1356 CFIIndex =
1357 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1358 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001359 case MIToken::kw_cfi_def_cfa_offset:
1360 if (parseCFIOffset(Offset))
1361 return true;
1362 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1363 CFIIndex = MMI.addFrameInst(
1364 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1365 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001366 case MIToken::kw_cfi_def_cfa:
1367 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1368 parseCFIOffset(Offset))
1369 return true;
1370 // NB: MCCFIInstruction::createDefCfa negates the offset.
1371 CFIIndex =
1372 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1373 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001374 default:
1375 // TODO: Parse the other CFI operands.
1376 llvm_unreachable("The current token should be a cfi operand");
1377 }
1378 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001379 return false;
1380}
1381
Alex Lorenzdeb53492015-07-28 17:28:03 +00001382bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1383 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001384 case MIToken::NamedIRBlock: {
1385 BB = dyn_cast_or_null<BasicBlock>(
1386 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001387 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001388 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001389 break;
1390 }
1391 case MIToken::IRBlock: {
1392 unsigned SlotNumber = 0;
1393 if (getUnsigned(SlotNumber))
1394 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001395 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001396 if (!BB)
1397 return error(Twine("use of undefined IR block '%ir-block.") +
1398 Twine(SlotNumber) + "'");
1399 break;
1400 }
1401 default:
1402 llvm_unreachable("The current token should be an IR block reference");
1403 }
1404 return false;
1405}
1406
1407bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1408 assert(Token.is(MIToken::kw_blockaddress));
1409 lex();
1410 if (expectAndConsume(MIToken::lparen))
1411 return true;
1412 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001413 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001414 return error("expected a global value");
1415 GlobalValue *GV = nullptr;
1416 if (parseGlobalValue(GV))
1417 return true;
1418 auto *F = dyn_cast<Function>(GV);
1419 if (!F)
1420 return error("expected an IR function reference");
1421 lex();
1422 if (expectAndConsume(MIToken::comma))
1423 return true;
1424 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001425 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001426 return error("expected an IR block reference");
1427 if (parseIRBlock(BB, *F))
1428 return true;
1429 lex();
1430 if (expectAndConsume(MIToken::rparen))
1431 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001432 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001433 if (parseOperandsOffset(Dest))
1434 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001435 return false;
1436}
1437
Alex Lorenzef5c1962015-07-28 23:02:45 +00001438bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1439 assert(Token.is(MIToken::kw_target_index));
1440 lex();
1441 if (expectAndConsume(MIToken::lparen))
1442 return true;
1443 if (Token.isNot(MIToken::Identifier))
1444 return error("expected the name of the target index");
1445 int Index = 0;
1446 if (getTargetIndex(Token.stringValue(), Index))
1447 return error("use of undefined target index '" + Token.stringValue() + "'");
1448 lex();
1449 if (expectAndConsume(MIToken::rparen))
1450 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001451 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001452 if (parseOperandsOffset(Dest))
1453 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001454 return false;
1455}
1456
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001457bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1458 assert(Token.is(MIToken::kw_liveout));
1459 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1460 assert(TRI && "Expected target register info");
1461 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1462 lex();
1463 if (expectAndConsume(MIToken::lparen))
1464 return true;
1465 while (true) {
1466 if (Token.isNot(MIToken::NamedRegister))
1467 return error("expected a named register");
1468 unsigned Reg = 0;
1469 if (parseRegister(Reg))
1470 return true;
1471 lex();
1472 Mask[Reg / 32] |= 1U << (Reg % 32);
1473 // TODO: Report an error if the same register is used more than once.
1474 if (Token.isNot(MIToken::comma))
1475 break;
1476 lex();
1477 }
1478 if (expectAndConsume(MIToken::rparen))
1479 return true;
1480 Dest = MachineOperand::CreateRegLiveOut(Mask);
1481 return false;
1482}
1483
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001484bool MIParser::parseMachineOperand(MachineOperand &Dest,
1485 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001486 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001487 case MIToken::kw_implicit:
1488 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001489 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001490 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001491 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001492 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001493 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001494 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001495 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001496 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001497 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001498 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001499 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001500 case MIToken::IntegerLiteral:
1501 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001502 case MIToken::IntegerType:
1503 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001504 case MIToken::kw_half:
1505 case MIToken::kw_float:
1506 case MIToken::kw_double:
1507 case MIToken::kw_x86_fp80:
1508 case MIToken::kw_fp128:
1509 case MIToken::kw_ppc_fp128:
1510 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001511 case MIToken::MachineBasicBlock:
1512 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001513 case MIToken::StackObject:
1514 return parseStackObjectOperand(Dest);
1515 case MIToken::FixedStackObject:
1516 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001517 case MIToken::GlobalValue:
1518 case MIToken::NamedGlobalValue:
1519 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001520 case MIToken::ConstantPoolItem:
1521 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001522 case MIToken::JumpTableIndex:
1523 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001524 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001525 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00001526 case MIToken::SubRegisterIndex:
1527 return parseSubRegisterIndexOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001528 case MIToken::exclaim:
1529 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001530 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001531 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001532 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001533 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001534 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001535 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001536 case MIToken::kw_blockaddress:
1537 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001538 case MIToken::kw_target_index:
1539 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001540 case MIToken::kw_liveout:
1541 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001542 case MIToken::Error:
1543 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001544 case MIToken::Identifier:
1545 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1546 Dest = MachineOperand::CreateRegMask(RegMask);
1547 lex();
1548 break;
1549 }
1550 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001551 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001552 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001553 return error("expected a machine operand");
1554 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001555 return false;
1556}
1557
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001558bool MIParser::parseMachineOperandAndTargetFlags(
1559 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001560 unsigned TF = 0;
1561 bool HasTargetFlags = false;
1562 if (Token.is(MIToken::kw_target_flags)) {
1563 HasTargetFlags = true;
1564 lex();
1565 if (expectAndConsume(MIToken::lparen))
1566 return true;
1567 if (Token.isNot(MIToken::Identifier))
1568 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001569 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1570 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1571 return error("use of undefined target flag '" + Token.stringValue() +
1572 "'");
1573 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001574 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001575 while (Token.is(MIToken::comma)) {
1576 lex();
1577 if (Token.isNot(MIToken::Identifier))
1578 return error("expected the name of the target flag");
1579 unsigned BitFlag = 0;
1580 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1581 return error("use of undefined target flag '" + Token.stringValue() +
1582 "'");
1583 // TODO: Report an error when using a duplicate bit target flag.
1584 TF |= BitFlag;
1585 lex();
1586 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001587 if (expectAndConsume(MIToken::rparen))
1588 return true;
1589 }
1590 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001591 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001592 return true;
1593 if (!HasTargetFlags)
1594 return false;
1595 if (Dest.isReg())
1596 return error(Loc, "register operands can't have target flags");
1597 Dest.setTargetFlags(TF);
1598 return false;
1599}
1600
Alex Lorenzdc24c172015-08-07 20:21:00 +00001601bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001602 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1603 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001604 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001605 bool IsNegative = Token.is(MIToken::minus);
1606 lex();
1607 if (Token.isNot(MIToken::IntegerLiteral))
1608 return error("expected an integer literal after '" + Sign + "'");
1609 if (Token.integerValue().getMinSignedBits() > 64)
1610 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001611 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001612 if (IsNegative)
1613 Offset = -Offset;
1614 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001615 return false;
1616}
1617
Alex Lorenz620f8912015-08-13 20:33:33 +00001618bool MIParser::parseAlignment(unsigned &Alignment) {
1619 assert(Token.is(MIToken::kw_align));
1620 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001621 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001622 return error("expected an integer literal after 'align'");
1623 if (getUnsigned(Alignment))
1624 return true;
1625 lex();
1626 return false;
1627}
1628
Alex Lorenzdc24c172015-08-07 20:21:00 +00001629bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1630 int64_t Offset = 0;
1631 if (parseOffset(Offset))
1632 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001633 Op.setOffset(Offset);
1634 return false;
1635}
1636
Alex Lorenz36593ac2015-08-19 23:27:07 +00001637bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001638 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001639 case MIToken::NamedIRValue: {
1640 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001641 break;
1642 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001643 case MIToken::IRValue: {
1644 unsigned SlotNumber = 0;
1645 if (getUnsigned(SlotNumber))
1646 return true;
1647 V = getIRValue(SlotNumber);
1648 break;
1649 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001650 case MIToken::NamedGlobalValue:
1651 case MIToken::GlobalValue: {
1652 GlobalValue *GV = nullptr;
1653 if (parseGlobalValue(GV))
1654 return true;
1655 V = GV;
1656 break;
1657 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001658 case MIToken::QuotedIRValue: {
1659 const Constant *C = nullptr;
1660 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1661 return true;
1662 V = C;
1663 break;
1664 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001665 default:
1666 llvm_unreachable("The current token should be an IR block reference");
1667 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001668 if (!V)
1669 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001670 return false;
1671}
1672
1673bool MIParser::getUint64(uint64_t &Result) {
1674 assert(Token.hasIntegerValue());
1675 if (Token.integerValue().getActiveBits() > 64)
1676 return error("expected 64-bit integer (too large)");
1677 Result = Token.integerValue().getZExtValue();
1678 return false;
1679}
1680
Justin Lebar0af80cd2016-07-15 18:26:59 +00001681bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
1682 const auto OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001683 switch (Token.kind()) {
1684 case MIToken::kw_volatile:
1685 Flags |= MachineMemOperand::MOVolatile;
1686 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001687 case MIToken::kw_non_temporal:
1688 Flags |= MachineMemOperand::MONonTemporal;
1689 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001690 case MIToken::kw_invariant:
1691 Flags |= MachineMemOperand::MOInvariant;
1692 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001693 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001694 default:
1695 llvm_unreachable("The current token should be a memory operand flag");
1696 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001697 if (OldFlags == Flags)
1698 // We know that the same flag is specified more than once when the flags
1699 // weren't modified.
1700 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001701 lex();
1702 return false;
1703}
1704
Alex Lorenz91097a32015-08-12 20:33:26 +00001705bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1706 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001707 case MIToken::kw_stack:
1708 PSV = MF.getPSVManager().getStack();
1709 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001710 case MIToken::kw_got:
1711 PSV = MF.getPSVManager().getGOT();
1712 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001713 case MIToken::kw_jump_table:
1714 PSV = MF.getPSVManager().getJumpTable();
1715 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001716 case MIToken::kw_constant_pool:
1717 PSV = MF.getPSVManager().getConstantPool();
1718 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001719 case MIToken::FixedStackObject: {
1720 int FI;
1721 if (parseFixedStackFrameIndex(FI))
1722 return true;
1723 PSV = MF.getPSVManager().getFixedStack(FI);
1724 // The token was already consumed, so use return here instead of break.
1725 return false;
1726 }
Matthias Braun3ef7df92016-06-08 00:47:07 +00001727 case MIToken::StackObject: {
1728 int FI;
1729 if (parseStackFrameIndex(FI))
1730 return true;
1731 PSV = MF.getPSVManager().getFixedStack(FI);
1732 // The token was already consumed, so use return here instead of break.
1733 return false;
1734 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001735 case MIToken::kw_call_entry: {
1736 lex();
1737 switch (Token.kind()) {
1738 case MIToken::GlobalValue:
1739 case MIToken::NamedGlobalValue: {
1740 GlobalValue *GV = nullptr;
1741 if (parseGlobalValue(GV))
1742 return true;
1743 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1744 break;
1745 }
1746 case MIToken::ExternalSymbol:
1747 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1748 MF.createExternalSymbolName(Token.stringValue()));
1749 break;
1750 default:
1751 return error(
1752 "expected a global value or an external symbol after 'call-entry'");
1753 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001754 break;
1755 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001756 default:
1757 llvm_unreachable("The current token should be pseudo source value");
1758 }
1759 lex();
1760 return false;
1761}
1762
1763bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001764 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001765 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Matthias Braun3ef7df92016-06-08 00:47:07 +00001766 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
1767 Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001768 const PseudoSourceValue *PSV = nullptr;
1769 if (parseMemoryPseudoSourceValue(PSV))
1770 return true;
1771 int64_t Offset = 0;
1772 if (parseOffset(Offset))
1773 return true;
1774 Dest = MachinePointerInfo(PSV, Offset);
1775 return false;
1776 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001777 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1778 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001779 Token.isNot(MIToken::NamedGlobalValue) &&
1780 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001781 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001782 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001783 if (parseIRValue(V))
1784 return true;
1785 if (!V->getType()->isPointerTy())
1786 return error("expected a pointer IR value");
1787 lex();
1788 int64_t Offset = 0;
1789 if (parseOffset(Offset))
1790 return true;
1791 Dest = MachinePointerInfo(V, Offset);
1792 return false;
1793}
1794
Alex Lorenz4af7e612015-08-03 23:08:19 +00001795bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1796 if (expectAndConsume(MIToken::lparen))
1797 return true;
Justin Lebar0af80cd2016-07-15 18:26:59 +00001798 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
Alex Lorenza518b792015-08-04 00:24:45 +00001799 while (Token.isMemoryOperandFlag()) {
1800 if (parseMemoryOperandFlag(Flags))
1801 return true;
1802 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001803 if (Token.isNot(MIToken::Identifier) ||
1804 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1805 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001806 if (Token.stringValue() == "load")
1807 Flags |= MachineMemOperand::MOLoad;
1808 else
1809 Flags |= MachineMemOperand::MOStore;
1810 lex();
1811
1812 if (Token.isNot(MIToken::IntegerLiteral))
1813 return error("expected the size integer literal after memory operation");
1814 uint64_t Size;
1815 if (getUint64(Size))
1816 return true;
1817 lex();
1818
Alex Lorenz91097a32015-08-12 20:33:26 +00001819 MachinePointerInfo Ptr = MachinePointerInfo();
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001820 if (Token.is(MIToken::Identifier)) {
1821 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1822 if (Token.stringValue() != Word)
1823 return error(Twine("expected '") + Word + "'");
1824 lex();
1825
1826 if (parseMachinePointerInfo(Ptr))
1827 return true;
1828 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001829 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001830 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001831 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001832 while (consumeIfPresent(MIToken::comma)) {
1833 switch (Token.kind()) {
1834 case MIToken::kw_align:
1835 if (parseAlignment(BaseAlignment))
1836 return true;
1837 break;
1838 case MIToken::md_tbaa:
1839 lex();
1840 if (parseMDNode(AAInfo.TBAA))
1841 return true;
1842 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001843 case MIToken::md_alias_scope:
1844 lex();
1845 if (parseMDNode(AAInfo.Scope))
1846 return true;
1847 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001848 case MIToken::md_noalias:
1849 lex();
1850 if (parseMDNode(AAInfo.NoAlias))
1851 return true;
1852 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001853 case MIToken::md_range:
1854 lex();
1855 if (parseMDNode(Range))
1856 return true;
1857 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001858 // TODO: Report an error on duplicate metadata nodes.
1859 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001860 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1861 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001862 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001863 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001864 if (expectAndConsume(MIToken::rparen))
1865 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001866 Dest =
1867 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001868 return false;
1869}
1870
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001871void MIParser::initNames2InstrOpCodes() {
1872 if (!Names2InstrOpCodes.empty())
1873 return;
1874 const auto *TII = MF.getSubtarget().getInstrInfo();
1875 assert(TII && "Expected target instruction info");
1876 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1877 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1878}
1879
1880bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1881 initNames2InstrOpCodes();
1882 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1883 if (InstrInfo == Names2InstrOpCodes.end())
1884 return true;
1885 OpCode = InstrInfo->getValue();
1886 return false;
1887}
1888
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001889void MIParser::initNames2Regs() {
1890 if (!Names2Regs.empty())
1891 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001892 // The '%noreg' register is the register 0.
1893 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001894 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1895 assert(TRI && "Expected target register info");
1896 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1897 bool WasInserted =
1898 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1899 .second;
1900 (void)WasInserted;
1901 assert(WasInserted && "Expected registers to be unique case-insensitively");
1902 }
1903}
1904
1905bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1906 initNames2Regs();
1907 auto RegInfo = Names2Regs.find(RegName);
1908 if (RegInfo == Names2Regs.end())
1909 return true;
1910 Reg = RegInfo->getValue();
1911 return false;
1912}
1913
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001914void MIParser::initNames2RegMasks() {
1915 if (!Names2RegMasks.empty())
1916 return;
1917 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1918 assert(TRI && "Expected target register info");
1919 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1920 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1921 assert(RegMasks.size() == RegMaskNames.size());
1922 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1923 Names2RegMasks.insert(
1924 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1925}
1926
1927const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1928 initNames2RegMasks();
1929 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1930 if (RegMaskInfo == Names2RegMasks.end())
1931 return nullptr;
1932 return RegMaskInfo->getValue();
1933}
1934
Alex Lorenz2eacca82015-07-13 23:24:34 +00001935void MIParser::initNames2SubRegIndices() {
1936 if (!Names2SubRegIndices.empty())
1937 return;
1938 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1939 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1940 Names2SubRegIndices.insert(
1941 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1942}
1943
1944unsigned MIParser::getSubRegIndex(StringRef Name) {
1945 initNames2SubRegIndices();
1946 auto SubRegInfo = Names2SubRegIndices.find(Name);
1947 if (SubRegInfo == Names2SubRegIndices.end())
1948 return 0;
1949 return SubRegInfo->getValue();
1950}
1951
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001952static void initSlots2BasicBlocks(
1953 const Function &F,
1954 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1955 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001956 MST.incorporateFunction(F);
1957 for (auto &BB : F) {
1958 if (BB.hasName())
1959 continue;
1960 int Slot = MST.getLocalSlot(&BB);
1961 if (Slot == -1)
1962 continue;
1963 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1964 }
1965}
1966
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001967static const BasicBlock *getIRBlockFromSlot(
1968 unsigned Slot,
1969 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001970 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1971 if (BlockInfo == Slots2BasicBlocks.end())
1972 return nullptr;
1973 return BlockInfo->second;
1974}
1975
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001976const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1977 if (Slots2BasicBlocks.empty())
1978 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1979 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1980}
1981
1982const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1983 if (&F == MF.getFunction())
1984 return getIRBlock(Slot);
1985 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1986 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1987 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1988}
1989
Alex Lorenzdd13be02015-08-19 23:31:05 +00001990static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
1991 DenseMap<unsigned, const Value *> &Slots2Values) {
1992 int Slot = MST.getLocalSlot(V);
1993 if (Slot == -1)
1994 return;
1995 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
1996}
1997
1998/// Creates the mapping from slot numbers to function's unnamed IR values.
1999static void initSlots2Values(const Function &F,
2000 DenseMap<unsigned, const Value *> &Slots2Values) {
2001 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2002 MST.incorporateFunction(F);
2003 for (const auto &Arg : F.args())
2004 mapValueToSlot(&Arg, MST, Slots2Values);
2005 for (const auto &BB : F) {
2006 mapValueToSlot(&BB, MST, Slots2Values);
2007 for (const auto &I : BB)
2008 mapValueToSlot(&I, MST, Slots2Values);
2009 }
2010}
2011
2012const Value *MIParser::getIRValue(unsigned Slot) {
2013 if (Slots2Values.empty())
2014 initSlots2Values(*MF.getFunction(), Slots2Values);
2015 auto ValueInfo = Slots2Values.find(Slot);
2016 if (ValueInfo == Slots2Values.end())
2017 return nullptr;
2018 return ValueInfo->second;
2019}
2020
Alex Lorenzef5c1962015-07-28 23:02:45 +00002021void MIParser::initNames2TargetIndices() {
2022 if (!Names2TargetIndices.empty())
2023 return;
2024 const auto *TII = MF.getSubtarget().getInstrInfo();
2025 assert(TII && "Expected target instruction info");
2026 auto Indices = TII->getSerializableTargetIndices();
2027 for (const auto &I : Indices)
2028 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2029}
2030
2031bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2032 initNames2TargetIndices();
2033 auto IndexInfo = Names2TargetIndices.find(Name);
2034 if (IndexInfo == Names2TargetIndices.end())
2035 return true;
2036 Index = IndexInfo->second;
2037 return false;
2038}
2039
Alex Lorenz49873a82015-08-06 00:44:07 +00002040void MIParser::initNames2DirectTargetFlags() {
2041 if (!Names2DirectTargetFlags.empty())
2042 return;
2043 const auto *TII = MF.getSubtarget().getInstrInfo();
2044 assert(TII && "Expected target instruction info");
2045 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2046 for (const auto &I : Flags)
2047 Names2DirectTargetFlags.insert(
2048 std::make_pair(StringRef(I.second), I.first));
2049}
2050
2051bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2052 initNames2DirectTargetFlags();
2053 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2054 if (FlagInfo == Names2DirectTargetFlags.end())
2055 return true;
2056 Flag = FlagInfo->second;
2057 return false;
2058}
2059
Alex Lorenzf3630112015-08-18 22:52:15 +00002060void MIParser::initNames2BitmaskTargetFlags() {
2061 if (!Names2BitmaskTargetFlags.empty())
2062 return;
2063 const auto *TII = MF.getSubtarget().getInstrInfo();
2064 assert(TII && "Expected target instruction info");
2065 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2066 for (const auto &I : Flags)
2067 Names2BitmaskTargetFlags.insert(
2068 std::make_pair(StringRef(I.second), I.first));
2069}
2070
2071bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2072 initNames2BitmaskTargetFlags();
2073 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2074 if (FlagInfo == Names2BitmaskTargetFlags.end())
2075 return true;
2076 Flag = FlagInfo->second;
2077 return false;
2078}
2079
Matthias Braun83947862016-07-13 22:23:23 +00002080bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2081 StringRef Src,
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002082 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002083 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002084}
2085
Matthias Braun83947862016-07-13 22:23:23 +00002086bool llvm::parseMachineInstructions(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002087 StringRef Src, SMDiagnostic &Error) {
2088 return MIParser(PFS, Error, Src).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002089}
Alex Lorenzf09df002015-06-30 18:16:42 +00002090
Matthias Braun83947862016-07-13 22:23:23 +00002091bool llvm::parseMBBReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002092 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +00002093 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002094 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002095}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002096
Matthias Braun83947862016-07-13 22:23:23 +00002097bool llvm::parseNamedRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002098 unsigned &Reg, StringRef Src,
Alex Lorenz9fab3702015-07-14 21:24:41 +00002099 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002100 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002101}
Alex Lorenz12045a42015-07-27 17:42:45 +00002102
Matthias Braun83947862016-07-13 22:23:23 +00002103bool llvm::parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002104 unsigned &Reg, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +00002105 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002106 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +00002107}
Alex Lorenza314d812015-08-18 22:26:26 +00002108
Matthias Braun83947862016-07-13 22:23:23 +00002109bool llvm::parseStackObjectReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002110 int &FI, StringRef Src,
Alex Lorenza314d812015-08-18 22:26:26 +00002111 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002112 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
Alex Lorenza314d812015-08-18 22:26:26 +00002113}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002114
Matthias Braun83947862016-07-13 22:23:23 +00002115bool llvm::parseMDNode(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002116 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2117 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002118}