blob: ed6ccdd668fa569444e800fc47adcd095d72dd33 [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
39namespace {
40
Alex Lorenz36962cd2015-07-07 02:08:46 +000041/// A wrapper struct around the 'MachineOperand' struct that includes a source
Alex Lorenzfeb6b432015-08-19 19:19:16 +000042/// range and other attributes.
43struct ParsedMachineOperand {
Alex Lorenz36962cd2015-07-07 02:08:46 +000044 MachineOperand Operand;
45 StringRef::iterator Begin;
46 StringRef::iterator End;
Alex Lorenz5ef93b02015-08-19 19:05:34 +000047 Optional<unsigned> TiedDefIdx;
Alex Lorenz36962cd2015-07-07 02:08:46 +000048
Alex Lorenzfeb6b432015-08-19 19:19:16 +000049 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
50 StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
Alex Lorenz5ef93b02015-08-19 19:05:34 +000051 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
52 if (TiedDefIdx)
53 assert(Operand.isReg() && Operand.isUse() &&
54 "Only used register operands can be tied");
55 }
Alex Lorenz36962cd2015-07-07 02:08:46 +000056};
57
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000058class MIParser {
59 SourceMgr &SM;
60 MachineFunction &MF;
61 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000062 StringRef Source, CurrentSource;
63 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000064 const PerFunctionMIParsingState &PFS;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000065 /// Maps from indices to unnamed global values and metadata nodes.
66 const SlotMapping &IRSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000067 /// Maps from instruction names to op codes.
68 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000069 /// Maps from register names to registers.
70 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000071 /// Maps from register mask names to register masks.
72 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000073 /// Maps from subregister names to subregister indices.
74 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000075 /// Maps from slot numbers to function's unnamed basic blocks.
76 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzdd13be02015-08-19 23:31:05 +000077 /// Maps from slot numbers to function's unnamed values.
78 DenseMap<unsigned, const Value *> Slots2Values;
Alex Lorenzef5c1962015-07-28 23:02:45 +000079 /// Maps from target index names to target indices.
80 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +000081 /// Maps from direct target flag names to the direct target flag values.
82 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenzf3630112015-08-18 22:52:15 +000083 /// Maps from direct target flag names to the bitmask target flag values.
84 StringMap<unsigned> Names2BitmaskTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000085
86public:
87 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000088 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000089 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000090
Alex Lorenz91370c52015-06-22 20:37:46 +000091 void lex();
92
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000093 /// Report an error at the current location with the given message.
94 ///
95 /// This function always return true.
96 bool error(const Twine &Msg);
97
Alex Lorenz91370c52015-06-22 20:37:46 +000098 /// Report an error at the given location with the given message.
99 ///
100 /// This function always return true.
101 bool error(StringRef::iterator Loc, const Twine &Msg);
102
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000103 bool
104 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
105 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +0000106 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +0000107 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
108 bool parseStandaloneNamedRegister(unsigned &Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +0000109 bool parseStandaloneVirtualRegister(unsigned &Reg);
Alex Lorenza314d812015-08-18 22:26:26 +0000110 bool parseStandaloneStackObject(int &FI);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000111 bool parseStandaloneMDNode(MDNode *&Node);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000112
113 bool
114 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
115 bool parseBasicBlock(MachineBasicBlock &MBB);
116 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
117 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000118
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000119 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000120 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000121 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000122 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000123 bool parseSize(unsigned &Size);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000124 bool parseRegisterOperand(MachineOperand &Dest,
125 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000126 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000127 bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
128 const Constant *&C);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000129 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Alex Lorenz05e38822015-08-05 18:52:21 +0000130 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000131 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000132 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000133 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000134 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000135 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000136 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000137 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000138 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000139 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000140 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000141 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000142 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000143 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000144 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000145 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000146 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000147 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000148 bool parseIRBlock(BasicBlock *&BB, const Function &F);
149 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000150 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000151 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000152 bool parseMachineOperand(MachineOperand &Dest,
153 Optional<unsigned> &TiedDefIdx);
154 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
155 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000156 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000157 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000158 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000159 bool parseIRValue(const Value *&V);
Alex Lorenza518b792015-08-04 00:24:45 +0000160 bool parseMemoryOperandFlag(unsigned &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000161 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
162 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000163 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000164
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000165private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000166 /// Convert the integer literal in the current token into an unsigned integer.
167 ///
168 /// Return true if an error occurred.
169 bool getUnsigned(unsigned &Result);
170
Alex Lorenz4af7e612015-08-03 23:08:19 +0000171 /// Convert the integer literal in the current token into an uint64.
172 ///
173 /// Return true if an error occurred.
174 bool getUint64(uint64_t &Result);
175
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000176 /// If the current token is of the given kind, consume it and return false.
177 /// Otherwise report an error and return true.
178 bool expectAndConsume(MIToken::TokenKind TokenKind);
179
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000180 /// If the current token is of the given kind, consume it and return true.
181 /// Otherwise return false.
182 bool consumeIfPresent(MIToken::TokenKind TokenKind);
183
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000184 void initNames2InstrOpCodes();
185
186 /// Try to convert an instruction name to an opcode. Return true if the
187 /// instruction name is invalid.
188 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000189
Alex Lorenze5a44662015-07-17 00:24:15 +0000190 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000191
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000192 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000193 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000194
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000195 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000196 const MCInstrDesc &MCID);
197
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000198 void initNames2Regs();
199
200 /// Try to convert a register name to a register number. Return true if the
201 /// register name is invalid.
202 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000203
204 void initNames2RegMasks();
205
206 /// Check if the given identifier is a name of a register mask.
207 ///
208 /// Return null if the identifier isn't a register mask.
209 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000210
211 void initNames2SubRegIndices();
212
213 /// Check if the given identifier is a name of a subregister index.
214 ///
215 /// Return 0 if the name isn't a subregister index class.
216 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000217
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000218 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000219 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000220
Alex Lorenzdd13be02015-08-19 23:31:05 +0000221 const Value *getIRValue(unsigned Slot);
222
Alex Lorenzef5c1962015-07-28 23:02:45 +0000223 void initNames2TargetIndices();
224
225 /// Try to convert a name of target index to the corresponding target index.
226 ///
227 /// Return true if the name isn't a name of a target index.
228 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000229
230 void initNames2DirectTargetFlags();
231
232 /// Try to convert a name of a direct target flag to the corresponding
233 /// target flag.
234 ///
235 /// Return true if the name isn't a name of a direct flag.
236 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000237
238 void initNames2BitmaskTargetFlags();
239
240 /// Try to convert a name of a bitmask target flag to the corresponding
241 /// target flag.
242 ///
243 /// Return true if the name isn't a name of a bitmask target flag.
244 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000245};
246
247} // end anonymous namespace
248
249MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000250 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000251 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000252 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz3fb77682015-08-06 23:17:42 +0000253 PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000254
Alex Lorenz91370c52015-06-22 20:37:46 +0000255void MIParser::lex() {
256 CurrentSource = lexMIToken(
257 CurrentSource, Token,
258 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
259}
260
261bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
262
263bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000264 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000265 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
266 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
267 // Create an ordinary diagnostic when the source manager's buffer is the
268 // source string.
269 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
270 return true;
271 }
272 // Create a diagnostic for a YAML string literal.
273 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
274 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
275 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000276 return true;
277}
278
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000279static const char *toString(MIToken::TokenKind TokenKind) {
280 switch (TokenKind) {
281 case MIToken::comma:
282 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000283 case MIToken::equal:
284 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000285 case MIToken::colon:
286 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000287 case MIToken::lparen:
288 return "'('";
289 case MIToken::rparen:
290 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000291 default:
292 return "<unknown token>";
293 }
294}
295
296bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
297 if (Token.isNot(TokenKind))
298 return error(Twine("expected ") + toString(TokenKind));
299 lex();
300 return false;
301}
302
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000303bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
304 if (Token.isNot(TokenKind))
305 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000306 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000307 return true;
308}
Alex Lorenz91370c52015-06-22 20:37:46 +0000309
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000310bool MIParser::parseBasicBlockDefinition(
311 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
312 assert(Token.is(MIToken::MachineBasicBlockLabel));
313 unsigned ID = 0;
314 if (getUnsigned(ID))
315 return true;
316 auto Loc = Token.location();
317 auto Name = Token.stringValue();
318 lex();
319 bool HasAddressTaken = false;
320 bool IsLandingPad = false;
321 unsigned Alignment = 0;
322 BasicBlock *BB = nullptr;
323 if (consumeIfPresent(MIToken::lparen)) {
324 do {
325 // TODO: Report an error when multiple same attributes are specified.
326 switch (Token.kind()) {
327 case MIToken::kw_address_taken:
328 HasAddressTaken = true;
329 lex();
330 break;
331 case MIToken::kw_landing_pad:
332 IsLandingPad = true;
333 lex();
334 break;
335 case MIToken::kw_align:
336 if (parseAlignment(Alignment))
337 return true;
338 break;
339 case MIToken::IRBlock:
340 // TODO: Report an error when both name and ir block are specified.
341 if (parseIRBlock(BB, *MF.getFunction()))
342 return true;
343 lex();
344 break;
345 default:
346 break;
347 }
348 } while (consumeIfPresent(MIToken::comma));
349 if (expectAndConsume(MIToken::rparen))
350 return true;
351 }
352 if (expectAndConsume(MIToken::colon))
353 return true;
354
355 if (!Name.empty()) {
356 BB = dyn_cast_or_null<BasicBlock>(
357 MF.getFunction()->getValueSymbolTable().lookup(Name));
358 if (!BB)
359 return error(Loc, Twine("basic block '") + Name +
360 "' is not defined in the function '" +
361 MF.getName() + "'");
362 }
363 auto *MBB = MF.CreateMachineBasicBlock(BB);
364 MF.insert(MF.end(), MBB);
365 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
366 if (!WasInserted)
367 return error(Loc, Twine("redefinition of machine basic block with id #") +
368 Twine(ID));
369 if (Alignment)
370 MBB->setAlignment(Alignment);
371 if (HasAddressTaken)
372 MBB->setHasAddressTaken();
Reid Kleckner0e288232015-08-27 23:27:47 +0000373 MBB->setIsEHPad(IsLandingPad);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000374 return false;
375}
376
377bool MIParser::parseBasicBlockDefinitions(
378 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
379 lex();
380 // Skip until the first machine basic block.
381 while (Token.is(MIToken::Newline))
382 lex();
383 if (Token.isErrorOrEOF())
384 return Token.isError();
385 if (Token.isNot(MIToken::MachineBasicBlockLabel))
386 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000387 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000388 do {
389 if (parseBasicBlockDefinition(MBBSlots))
390 return true;
391 bool IsAfterNewline = false;
392 // Skip until the next machine basic block.
393 while (true) {
394 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
395 Token.isErrorOrEOF())
396 break;
397 else if (Token.is(MIToken::MachineBasicBlockLabel))
398 return error("basic block definition should be located at the start of "
399 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000400 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000401 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000402 continue;
403 }
404 IsAfterNewline = false;
405 if (Token.is(MIToken::lbrace))
406 ++BraceDepth;
407 if (Token.is(MIToken::rbrace)) {
408 if (!BraceDepth)
409 return error("extraneous closing brace ('}')");
410 --BraceDepth;
411 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000412 lex();
413 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000414 // Verify that we closed all of the '{' at the end of a file or a block.
415 if (!Token.isError() && BraceDepth)
416 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000417 } while (!Token.isErrorOrEOF());
418 return Token.isError();
419}
420
421bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
422 assert(Token.is(MIToken::kw_liveins));
423 lex();
424 if (expectAndConsume(MIToken::colon))
425 return true;
426 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
427 return false;
428 do {
429 if (Token.isNot(MIToken::NamedRegister))
430 return error("expected a named register");
431 unsigned Reg = 0;
432 if (parseRegister(Reg))
433 return true;
434 MBB.addLiveIn(Reg);
435 lex();
436 } while (consumeIfPresent(MIToken::comma));
437 return false;
438}
439
440bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
441 assert(Token.is(MIToken::kw_successors));
442 lex();
443 if (expectAndConsume(MIToken::colon))
444 return true;
445 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
446 return false;
447 do {
448 if (Token.isNot(MIToken::MachineBasicBlock))
449 return error("expected a machine basic block reference");
450 MachineBasicBlock *SuccMBB = nullptr;
451 if (parseMBBReference(SuccMBB))
452 return true;
453 lex();
454 unsigned Weight = 0;
455 if (consumeIfPresent(MIToken::lparen)) {
456 if (Token.isNot(MIToken::IntegerLiteral))
457 return error("expected an integer literal after '('");
458 if (getUnsigned(Weight))
459 return true;
460 lex();
461 if (expectAndConsume(MIToken::rparen))
462 return true;
463 }
Cong Houd97c1002015-12-01 05:29:22 +0000464 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000465 } while (consumeIfPresent(MIToken::comma));
Cong Houd97c1002015-12-01 05:29:22 +0000466 MBB.normalizeSuccProbs();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000467 return false;
468}
469
470bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
471 // Skip the definition.
472 assert(Token.is(MIToken::MachineBasicBlockLabel));
473 lex();
474 if (consumeIfPresent(MIToken::lparen)) {
475 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
476 lex();
477 consumeIfPresent(MIToken::rparen);
478 }
479 consumeIfPresent(MIToken::colon);
480
481 // Parse the liveins and successors.
482 // N.B: Multiple lists of successors and liveins are allowed and they're
483 // merged into one.
484 // Example:
485 // liveins: %edi
486 // liveins: %esi
487 //
488 // is equivalent to
489 // liveins: %edi, %esi
490 while (true) {
491 if (Token.is(MIToken::kw_successors)) {
492 if (parseBasicBlockSuccessors(MBB))
493 return true;
494 } else if (Token.is(MIToken::kw_liveins)) {
495 if (parseBasicBlockLiveins(MBB))
496 return true;
497 } else if (consumeIfPresent(MIToken::Newline)) {
498 continue;
499 } else
500 break;
501 if (!Token.isNewlineOrEOF())
502 return error("expected line break at the end of a list");
503 lex();
504 }
505
506 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000507 bool IsInBundle = false;
508 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000509 while (true) {
510 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
511 return false;
512 else if (consumeIfPresent(MIToken::Newline))
513 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000514 if (consumeIfPresent(MIToken::rbrace)) {
515 // The first parsing pass should verify that all closing '}' have an
516 // opening '{'.
517 assert(IsInBundle);
518 IsInBundle = false;
519 continue;
520 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000521 MachineInstr *MI = nullptr;
522 if (parse(MI))
523 return true;
524 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000525 if (IsInBundle) {
526 PrevMI->setFlag(MachineInstr::BundledSucc);
527 MI->setFlag(MachineInstr::BundledPred);
528 }
529 PrevMI = MI;
530 if (Token.is(MIToken::lbrace)) {
531 if (IsInBundle)
532 return error("nested instruction bundles are not allowed");
533 lex();
534 // This instruction is the start of the bundle.
535 MI->setFlag(MachineInstr::BundledSucc);
536 IsInBundle = true;
537 if (!Token.is(MIToken::Newline))
538 // The next instruction can be on the same line.
539 continue;
540 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000541 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
542 lex();
543 }
544 return false;
545}
546
547bool MIParser::parseBasicBlocks() {
548 lex();
549 // Skip until the first machine basic block.
550 while (Token.is(MIToken::Newline))
551 lex();
552 if (Token.isErrorOrEOF())
553 return Token.isError();
554 // The first parsing pass should have verified that this token is a MBB label
555 // in the 'parseBasicBlockDefinitions' method.
556 assert(Token.is(MIToken::MachineBasicBlockLabel));
557 do {
558 MachineBasicBlock *MBB = nullptr;
559 if (parseMBBReference(MBB))
560 return true;
561 if (parseBasicBlock(*MBB))
562 return true;
563 // The method 'parseBasicBlock' should parse the whole block until the next
564 // block or the end of file.
565 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
566 } while (Token.isNot(MIToken::Eof));
567 return false;
568}
569
570bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000571 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000572 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000573 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000574 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000575 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000576 Optional<unsigned> TiedDefIdx;
577 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000578 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000579 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000580 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000581 if (Token.isNot(MIToken::comma))
582 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000583 lex();
584 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000585 if (!Operands.empty() && expectAndConsume(MIToken::equal))
586 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000587
Alex Lorenze5a44662015-07-17 00:24:15 +0000588 unsigned OpCode, Flags = 0;
589 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000590 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000591
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000592 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000593 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000594 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000595 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000596 Optional<unsigned> TiedDefIdx;
597 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000598 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000599 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000600 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000601 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
602 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000603 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000604 if (Token.isNot(MIToken::comma))
605 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000606 lex();
607 }
608
Alex Lorenz46d760d2015-07-22 21:15:11 +0000609 DebugLoc DebugLocation;
610 if (Token.is(MIToken::kw_debug_location)) {
611 lex();
612 if (Token.isNot(MIToken::exclaim))
613 return error("expected a metadata node after 'debug-location'");
614 MDNode *Node = nullptr;
615 if (parseMDNode(Node))
616 return true;
617 DebugLocation = DebugLoc(Node);
618 }
619
Alex Lorenz4af7e612015-08-03 23:08:19 +0000620 // Parse the machine memory operands.
621 SmallVector<MachineMemOperand *, 2> MemOperands;
622 if (Token.is(MIToken::coloncolon)) {
623 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000624 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000625 MachineMemOperand *MemOp = nullptr;
626 if (parseMachineMemoryOperand(MemOp))
627 return true;
628 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000629 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000630 break;
631 if (Token.isNot(MIToken::comma))
632 return error("expected ',' before the next machine memory operand");
633 lex();
634 }
635 }
636
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000637 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000638 if (!MCID.isVariadic()) {
639 // FIXME: Move the implicit operand verification to the machine verifier.
640 if (verifyImplicitOperands(Operands, MCID))
641 return true;
642 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000643
Alex Lorenzcb268d42015-07-06 23:07:26 +0000644 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000645 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000646 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000647 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000648 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000649 if (assignRegisterTies(*MI, Operands))
650 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000651 if (MemOperands.empty())
652 return false;
653 MachineInstr::mmo_iterator MemRefs =
654 MF.allocateMemRefsArray(MemOperands.size());
655 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
656 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000657 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000658}
659
Alex Lorenz1ea60892015-07-27 20:29:27 +0000660bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000661 lex();
662 if (Token.isNot(MIToken::MachineBasicBlock))
663 return error("expected a machine basic block reference");
664 if (parseMBBReference(MBB))
665 return true;
666 lex();
667 if (Token.isNot(MIToken::Eof))
668 return error(
669 "expected end of string after the machine basic block reference");
670 return false;
671}
672
Alex Lorenz1ea60892015-07-27 20:29:27 +0000673bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000674 lex();
675 if (Token.isNot(MIToken::NamedRegister))
676 return error("expected a named register");
677 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000678 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000679 lex();
680 if (Token.isNot(MIToken::Eof))
681 return error("expected end of string after the register reference");
682 return false;
683}
684
Alex Lorenz12045a42015-07-27 17:42:45 +0000685bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
686 lex();
687 if (Token.isNot(MIToken::VirtualRegister))
688 return error("expected a virtual register");
689 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000690 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000691 lex();
692 if (Token.isNot(MIToken::Eof))
693 return error("expected end of string after the register reference");
694 return false;
695}
696
Alex Lorenza314d812015-08-18 22:26:26 +0000697bool MIParser::parseStandaloneStackObject(int &FI) {
698 lex();
699 if (Token.isNot(MIToken::StackObject))
700 return error("expected a stack object");
701 if (parseStackFrameIndex(FI))
702 return true;
703 if (Token.isNot(MIToken::Eof))
704 return error("expected end of string after the stack object reference");
705 return false;
706}
707
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000708bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
709 lex();
710 if (Token.isNot(MIToken::exclaim))
711 return error("expected a metadata node");
712 if (parseMDNode(Node))
713 return true;
714 if (Token.isNot(MIToken::Eof))
715 return error("expected end of string after the metadata node");
716 return false;
717}
718
Alex Lorenz36962cd2015-07-07 02:08:46 +0000719static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
720 assert(MO.isImplicit());
721 return MO.isDef() ? "implicit-def" : "implicit";
722}
723
724static std::string getRegisterName(const TargetRegisterInfo *TRI,
725 unsigned Reg) {
726 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
727 return StringRef(TRI->getName(Reg)).lower();
728}
729
Alex Lorenz0153e592015-09-10 14:04:34 +0000730/// Return true if the parsed machine operands contain a given machine operand.
731static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
732 ArrayRef<ParsedMachineOperand> Operands) {
733 for (const auto &I : Operands) {
734 if (ImplicitOperand.isIdenticalTo(I.Operand))
735 return true;
736 }
737 return false;
738}
739
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000740bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
741 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000742 if (MCID.isCall())
743 // We can't verify call instructions as they can contain arbitrary implicit
744 // register and register mask operands.
745 return false;
746
747 // Gather all the expected implicit operands.
748 SmallVector<MachineOperand, 4> ImplicitOperands;
749 if (MCID.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000750 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000751 ImplicitOperands.push_back(
752 MachineOperand::CreateReg(*ImpDefs, true, true));
753 if (MCID.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000754 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000755 ImplicitOperands.push_back(
756 MachineOperand::CreateReg(*ImpUses, false, true));
757
758 const auto *TRI = MF.getSubtarget().getRegisterInfo();
759 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000760 for (const auto &I : ImplicitOperands) {
761 if (isImplicitOperandIn(I, Operands))
762 continue;
763 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000764 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000765 printImplicitRegisterFlag(I) + " %" +
766 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000767 }
768 return false;
769}
770
Alex Lorenze5a44662015-07-17 00:24:15 +0000771bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
772 if (Token.is(MIToken::kw_frame_setup)) {
773 Flags |= MachineInstr::FrameSetup;
774 lex();
775 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000776 if (Token.isNot(MIToken::Identifier))
777 return error("expected a machine instruction");
778 StringRef InstrName = Token.stringValue();
779 if (parseInstrName(InstrName, OpCode))
780 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000781 lex();
782 return false;
783}
784
785bool MIParser::parseRegister(unsigned &Reg) {
786 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000787 case MIToken::underscore:
788 Reg = 0;
789 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000790 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000791 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000792 if (getRegisterByName(Name, Reg))
793 return error(Twine("unknown register name '") + Name + "'");
794 break;
795 }
Alex Lorenz53464512015-07-10 22:51:20 +0000796 case MIToken::VirtualRegister: {
797 unsigned ID;
798 if (getUnsigned(ID))
799 return true;
800 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
801 if (RegInfo == PFS.VirtualRegisterSlots.end())
802 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
803 "'");
804 Reg = RegInfo->second;
805 break;
806 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000807 // TODO: Parse other register kinds.
808 default:
809 llvm_unreachable("The current token should be a register");
810 }
811 return false;
812}
813
Alex Lorenzcb268d42015-07-06 23:07:26 +0000814bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000815 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000816 switch (Token.kind()) {
817 case MIToken::kw_implicit:
818 Flags |= RegState::Implicit;
819 break;
820 case MIToken::kw_implicit_define:
821 Flags |= RegState::ImplicitDefine;
822 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000823 case MIToken::kw_def:
824 Flags |= RegState::Define;
825 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000826 case MIToken::kw_dead:
827 Flags |= RegState::Dead;
828 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000829 case MIToken::kw_killed:
830 Flags |= RegState::Kill;
831 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000832 case MIToken::kw_undef:
833 Flags |= RegState::Undef;
834 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000835 case MIToken::kw_internal:
836 Flags |= RegState::InternalRead;
837 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000838 case MIToken::kw_early_clobber:
839 Flags |= RegState::EarlyClobber;
840 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000841 case MIToken::kw_debug_use:
842 Flags |= RegState::Debug;
843 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000844 default:
845 llvm_unreachable("The current token should be a register flag");
846 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000847 if (OldFlags == Flags)
848 // We know that the same flag is specified more than once when the flags
849 // weren't modified.
850 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000851 lex();
852 return false;
853}
854
Alex Lorenz2eacca82015-07-13 23:24:34 +0000855bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
856 assert(Token.is(MIToken::colon));
857 lex();
858 if (Token.isNot(MIToken::Identifier))
859 return error("expected a subregister index after ':'");
860 auto Name = Token.stringValue();
861 SubReg = getSubRegIndex(Name);
862 if (!SubReg)
863 return error(Twine("use of unknown subregister index '") + Name + "'");
864 lex();
865 return false;
866}
867
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000868bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
869 if (!consumeIfPresent(MIToken::kw_tied_def))
870 return error("expected 'tied-def' after '('");
871 if (Token.isNot(MIToken::IntegerLiteral))
872 return error("expected an integer literal after 'tied-def'");
873 if (getUnsigned(TiedDefIdx))
874 return true;
875 lex();
876 if (expectAndConsume(MIToken::rparen))
877 return true;
878 return false;
879}
880
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000881bool MIParser::parseSize(unsigned &Size) {
882 if (Token.isNot(MIToken::IntegerLiteral))
883 return error("expected an integer literal for the size");
884 if (getUnsigned(Size))
885 return true;
886 lex();
887 if (expectAndConsume(MIToken::rparen))
888 return true;
889 return false;
890}
891
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000892bool MIParser::assignRegisterTies(MachineInstr &MI,
893 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000894 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
895 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
896 if (!Operands[I].TiedDefIdx)
897 continue;
898 // The parser ensures that this operand is a register use, so we just have
899 // to check the tied-def operand.
900 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
901 if (DefIdx >= E)
902 return error(Operands[I].Begin,
903 Twine("use of invalid tied-def operand index '" +
904 Twine(DefIdx) + "'; instruction has only ") +
905 Twine(E) + " operands");
906 const auto &DefOperand = Operands[DefIdx].Operand;
907 if (!DefOperand.isReg() || !DefOperand.isDef())
908 // FIXME: add note with the def operand.
909 return error(Operands[I].Begin,
910 Twine("use of invalid tied-def operand index '") +
911 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
912 " isn't a defined register");
913 // Check that the tied-def operand wasn't tied elsewhere.
914 for (const auto &TiedPair : TiedRegisterPairs) {
915 if (TiedPair.first == DefIdx)
916 return error(Operands[I].Begin,
917 Twine("the tied-def operand #") + Twine(DefIdx) +
918 " is already tied with another register operand");
919 }
920 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
921 }
922 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
923 // indices must be less than tied max.
924 for (const auto &TiedPair : TiedRegisterPairs)
925 MI.tieOperands(TiedPair.first, TiedPair.second);
926 return false;
927}
928
929bool MIParser::parseRegisterOperand(MachineOperand &Dest,
930 Optional<unsigned> &TiedDefIdx,
931 bool IsDef) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000932 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000933 unsigned Flags = IsDef ? RegState::Define : 0;
934 while (Token.isRegisterFlag()) {
935 if (parseRegisterFlag(Flags))
936 return true;
937 }
938 if (!Token.isRegister())
939 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000940 if (parseRegister(Reg))
941 return true;
942 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000943 unsigned SubReg = 0;
944 if (Token.is(MIToken::colon)) {
945 if (parseSubRegisterIndex(SubReg))
946 return true;
947 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000948 if ((Flags & RegState::Define) == 0) {
949 if (consumeIfPresent(MIToken::lparen)) {
950 unsigned Idx;
951 if (parseRegisterTiedDefIndex(Idx))
952 return true;
953 TiedDefIdx = Idx;
954 }
955 } else if (consumeIfPresent(MIToken::lparen)) {
956 // Generic virtual registers must have a size.
957 // The "must" part will be verify by the machine verifier,
958 // because at this point we actually do not know if Reg is
959 // a generic virtual register.
960 if (!TargetRegisterInfo::isVirtualRegister(Reg))
961 return error("unexpected size on physical register");
962 unsigned Size;
963 if (parseSize(Size))
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000964 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000965
966 MachineRegisterInfo &MRI = MF.getRegInfo();
967 MRI.setSize(Reg, Size);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000968 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000969 Dest = MachineOperand::CreateReg(
970 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000971 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +0000972 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
973 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000974 return false;
975}
976
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000977bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
978 assert(Token.is(MIToken::IntegerLiteral));
979 const APSInt &Int = Token.integerValue();
980 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +0000981 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000982 Dest = MachineOperand::CreateImm(Int.getExtValue());
983 lex();
984 return false;
985}
986
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000987bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
988 const Constant *&C) {
989 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000990 SMDiagnostic Err;
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000991 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
992 &IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000993 if (!C)
994 return error(Loc + Err.getColumnNo(), Err.getMessage());
995 return false;
996}
997
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000998bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
999 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1000 return true;
1001 lex();
1002 return false;
1003}
1004
Alex Lorenz05e38822015-08-05 18:52:21 +00001005bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1006 assert(Token.is(MIToken::IntegerType));
1007 auto Loc = Token.location();
1008 lex();
1009 if (Token.isNot(MIToken::IntegerLiteral))
1010 return error("expected an integer literal");
1011 const Constant *C = nullptr;
1012 if (parseIRConstant(Loc, C))
1013 return true;
1014 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1015 return false;
1016}
1017
Alex Lorenzad156fb2015-07-31 20:49:21 +00001018bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1019 auto Loc = Token.location();
1020 lex();
1021 if (Token.isNot(MIToken::FloatingPointLiteral))
1022 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001023 const Constant *C = nullptr;
1024 if (parseIRConstant(Loc, C))
1025 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001026 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1027 return false;
1028}
1029
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001030bool MIParser::getUnsigned(unsigned &Result) {
1031 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1032 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1033 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1034 if (Val64 == Limit)
1035 return error("expected 32-bit integer (too large)");
1036 Result = Val64;
1037 return false;
1038}
1039
Alex Lorenzf09df002015-06-30 18:16:42 +00001040bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001041 assert(Token.is(MIToken::MachineBasicBlock) ||
1042 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001043 unsigned Number;
1044 if (getUnsigned(Number))
1045 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001046 auto MBBInfo = PFS.MBBSlots.find(Number);
1047 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001048 return error(Twine("use of undefined machine basic block #") +
1049 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001050 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001051 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1052 return error(Twine("the name of machine basic block #") + Twine(Number) +
1053 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001054 return false;
1055}
1056
1057bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1058 MachineBasicBlock *MBB;
1059 if (parseMBBReference(MBB))
1060 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001061 Dest = MachineOperand::CreateMBB(MBB);
1062 lex();
1063 return false;
1064}
1065
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001066bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001067 assert(Token.is(MIToken::StackObject));
1068 unsigned ID;
1069 if (getUnsigned(ID))
1070 return true;
1071 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1072 if (ObjectInfo == PFS.StackObjectSlots.end())
1073 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1074 "'");
1075 StringRef Name;
1076 if (const auto *Alloca =
1077 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
1078 Name = Alloca->getName();
1079 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1080 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1081 "' isn't '" + Token.stringValue() + "'");
1082 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001083 FI = ObjectInfo->second;
1084 return false;
1085}
1086
1087bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1088 int FI;
1089 if (parseStackFrameIndex(FI))
1090 return true;
1091 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001092 return false;
1093}
1094
Alex Lorenzea882122015-08-12 21:17:02 +00001095bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001096 assert(Token.is(MIToken::FixedStackObject));
1097 unsigned ID;
1098 if (getUnsigned(ID))
1099 return true;
1100 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1101 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1102 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1103 Twine(ID) + "'");
1104 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001105 FI = ObjectInfo->second;
1106 return false;
1107}
1108
1109bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1110 int FI;
1111 if (parseFixedStackFrameIndex(FI))
1112 return true;
1113 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001114 return false;
1115}
1116
Alex Lorenz41df7d32015-07-28 17:09:52 +00001117bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001118 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001119 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001120 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001121 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001122 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001123 return error(Twine("use of undefined global value '") + Token.range() +
1124 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001125 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001126 }
1127 case MIToken::GlobalValue: {
1128 unsigned GVIdx;
1129 if (getUnsigned(GVIdx))
1130 return true;
1131 if (GVIdx >= IRSlots.GlobalValues.size())
1132 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1133 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001134 GV = IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001135 break;
1136 }
1137 default:
1138 llvm_unreachable("The current token should be a global value");
1139 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001140 return false;
1141}
1142
1143bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1144 GlobalValue *GV = nullptr;
1145 if (parseGlobalValue(GV))
1146 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001147 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001148 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001149 if (parseOperandsOffset(Dest))
1150 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001151 return false;
1152}
1153
Alex Lorenzab980492015-07-20 20:51:18 +00001154bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1155 assert(Token.is(MIToken::ConstantPoolItem));
1156 unsigned ID;
1157 if (getUnsigned(ID))
1158 return true;
1159 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1160 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1161 return error("use of undefined constant '%const." + Twine(ID) + "'");
1162 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001163 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001164 if (parseOperandsOffset(Dest))
1165 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001166 return false;
1167}
1168
Alex Lorenz31d70682015-07-15 23:38:35 +00001169bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1170 assert(Token.is(MIToken::JumpTableIndex));
1171 unsigned ID;
1172 if (getUnsigned(ID))
1173 return true;
1174 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1175 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1176 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1177 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001178 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1179 return false;
1180}
1181
Alex Lorenz6ede3742015-07-21 16:59:53 +00001182bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001183 assert(Token.is(MIToken::ExternalSymbol));
1184 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001185 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001186 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001187 if (parseOperandsOffset(Dest))
1188 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001189 return false;
1190}
1191
Alex Lorenz44f29252015-07-22 21:07:04 +00001192bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001193 assert(Token.is(MIToken::exclaim));
1194 auto Loc = Token.location();
1195 lex();
1196 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1197 return error("expected metadata id after '!'");
1198 unsigned ID;
1199 if (getUnsigned(ID))
1200 return true;
1201 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
1202 if (NodeInfo == IRSlots.MetadataNodes.end())
1203 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1204 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001205 Node = NodeInfo->second.get();
1206 return false;
1207}
1208
1209bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1210 MDNode *Node = nullptr;
1211 if (parseMDNode(Node))
1212 return true;
1213 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001214 return false;
1215}
1216
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001217bool MIParser::parseCFIOffset(int &Offset) {
1218 if (Token.isNot(MIToken::IntegerLiteral))
1219 return error("expected a cfi offset");
1220 if (Token.integerValue().getMinSignedBits() > 32)
1221 return error("expected a 32 bit integer (the cfi offset is too large)");
1222 Offset = (int)Token.integerValue().getExtValue();
1223 lex();
1224 return false;
1225}
1226
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001227bool MIParser::parseCFIRegister(unsigned &Reg) {
1228 if (Token.isNot(MIToken::NamedRegister))
1229 return error("expected a cfi register");
1230 unsigned LLVMReg;
1231 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001232 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001233 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1234 assert(TRI && "Expected target register info");
1235 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1236 if (DwarfReg < 0)
1237 return error("invalid DWARF register");
1238 Reg = (unsigned)DwarfReg;
1239 lex();
1240 return false;
1241}
1242
1243bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1244 auto Kind = Token.kind();
1245 lex();
1246 auto &MMI = MF.getMMI();
1247 int Offset;
1248 unsigned Reg;
1249 unsigned CFIIndex;
1250 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001251 case MIToken::kw_cfi_same_value:
1252 if (parseCFIRegister(Reg))
1253 return true;
1254 CFIIndex =
1255 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1256 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001257 case MIToken::kw_cfi_offset:
1258 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1259 parseCFIOffset(Offset))
1260 return true;
1261 CFIIndex =
1262 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1263 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001264 case MIToken::kw_cfi_def_cfa_register:
1265 if (parseCFIRegister(Reg))
1266 return true;
1267 CFIIndex =
1268 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1269 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001270 case MIToken::kw_cfi_def_cfa_offset:
1271 if (parseCFIOffset(Offset))
1272 return true;
1273 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1274 CFIIndex = MMI.addFrameInst(
1275 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1276 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001277 case MIToken::kw_cfi_def_cfa:
1278 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1279 parseCFIOffset(Offset))
1280 return true;
1281 // NB: MCCFIInstruction::createDefCfa negates the offset.
1282 CFIIndex =
1283 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1284 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001285 default:
1286 // TODO: Parse the other CFI operands.
1287 llvm_unreachable("The current token should be a cfi operand");
1288 }
1289 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001290 return false;
1291}
1292
Alex Lorenzdeb53492015-07-28 17:28:03 +00001293bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1294 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001295 case MIToken::NamedIRBlock: {
1296 BB = dyn_cast_or_null<BasicBlock>(
1297 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001298 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001299 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001300 break;
1301 }
1302 case MIToken::IRBlock: {
1303 unsigned SlotNumber = 0;
1304 if (getUnsigned(SlotNumber))
1305 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001306 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001307 if (!BB)
1308 return error(Twine("use of undefined IR block '%ir-block.") +
1309 Twine(SlotNumber) + "'");
1310 break;
1311 }
1312 default:
1313 llvm_unreachable("The current token should be an IR block reference");
1314 }
1315 return false;
1316}
1317
1318bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1319 assert(Token.is(MIToken::kw_blockaddress));
1320 lex();
1321 if (expectAndConsume(MIToken::lparen))
1322 return true;
1323 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001324 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001325 return error("expected a global value");
1326 GlobalValue *GV = nullptr;
1327 if (parseGlobalValue(GV))
1328 return true;
1329 auto *F = dyn_cast<Function>(GV);
1330 if (!F)
1331 return error("expected an IR function reference");
1332 lex();
1333 if (expectAndConsume(MIToken::comma))
1334 return true;
1335 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001336 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001337 return error("expected an IR block reference");
1338 if (parseIRBlock(BB, *F))
1339 return true;
1340 lex();
1341 if (expectAndConsume(MIToken::rparen))
1342 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001343 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001344 if (parseOperandsOffset(Dest))
1345 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001346 return false;
1347}
1348
Alex Lorenzef5c1962015-07-28 23:02:45 +00001349bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1350 assert(Token.is(MIToken::kw_target_index));
1351 lex();
1352 if (expectAndConsume(MIToken::lparen))
1353 return true;
1354 if (Token.isNot(MIToken::Identifier))
1355 return error("expected the name of the target index");
1356 int Index = 0;
1357 if (getTargetIndex(Token.stringValue(), Index))
1358 return error("use of undefined target index '" + Token.stringValue() + "'");
1359 lex();
1360 if (expectAndConsume(MIToken::rparen))
1361 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001362 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001363 if (parseOperandsOffset(Dest))
1364 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001365 return false;
1366}
1367
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001368bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1369 assert(Token.is(MIToken::kw_liveout));
1370 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1371 assert(TRI && "Expected target register info");
1372 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1373 lex();
1374 if (expectAndConsume(MIToken::lparen))
1375 return true;
1376 while (true) {
1377 if (Token.isNot(MIToken::NamedRegister))
1378 return error("expected a named register");
1379 unsigned Reg = 0;
1380 if (parseRegister(Reg))
1381 return true;
1382 lex();
1383 Mask[Reg / 32] |= 1U << (Reg % 32);
1384 // TODO: Report an error if the same register is used more than once.
1385 if (Token.isNot(MIToken::comma))
1386 break;
1387 lex();
1388 }
1389 if (expectAndConsume(MIToken::rparen))
1390 return true;
1391 Dest = MachineOperand::CreateRegLiveOut(Mask);
1392 return false;
1393}
1394
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001395bool MIParser::parseMachineOperand(MachineOperand &Dest,
1396 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001397 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001398 case MIToken::kw_implicit:
1399 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001400 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001401 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001402 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001403 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001404 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001405 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001406 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001407 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001408 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001409 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001410 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001411 case MIToken::IntegerLiteral:
1412 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001413 case MIToken::IntegerType:
1414 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001415 case MIToken::kw_half:
1416 case MIToken::kw_float:
1417 case MIToken::kw_double:
1418 case MIToken::kw_x86_fp80:
1419 case MIToken::kw_fp128:
1420 case MIToken::kw_ppc_fp128:
1421 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001422 case MIToken::MachineBasicBlock:
1423 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001424 case MIToken::StackObject:
1425 return parseStackObjectOperand(Dest);
1426 case MIToken::FixedStackObject:
1427 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001428 case MIToken::GlobalValue:
1429 case MIToken::NamedGlobalValue:
1430 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001431 case MIToken::ConstantPoolItem:
1432 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001433 case MIToken::JumpTableIndex:
1434 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001435 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001436 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001437 case MIToken::exclaim:
1438 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001439 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001440 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001441 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001442 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001443 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001444 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001445 case MIToken::kw_blockaddress:
1446 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001447 case MIToken::kw_target_index:
1448 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001449 case MIToken::kw_liveout:
1450 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001451 case MIToken::Error:
1452 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001453 case MIToken::Identifier:
1454 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1455 Dest = MachineOperand::CreateRegMask(RegMask);
1456 lex();
1457 break;
1458 }
1459 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001460 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001461 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001462 return error("expected a machine operand");
1463 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001464 return false;
1465}
1466
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001467bool MIParser::parseMachineOperandAndTargetFlags(
1468 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001469 unsigned TF = 0;
1470 bool HasTargetFlags = false;
1471 if (Token.is(MIToken::kw_target_flags)) {
1472 HasTargetFlags = true;
1473 lex();
1474 if (expectAndConsume(MIToken::lparen))
1475 return true;
1476 if (Token.isNot(MIToken::Identifier))
1477 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001478 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1479 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1480 return error("use of undefined target flag '" + Token.stringValue() +
1481 "'");
1482 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001483 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001484 while (Token.is(MIToken::comma)) {
1485 lex();
1486 if (Token.isNot(MIToken::Identifier))
1487 return error("expected the name of the target flag");
1488 unsigned BitFlag = 0;
1489 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1490 return error("use of undefined target flag '" + Token.stringValue() +
1491 "'");
1492 // TODO: Report an error when using a duplicate bit target flag.
1493 TF |= BitFlag;
1494 lex();
1495 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001496 if (expectAndConsume(MIToken::rparen))
1497 return true;
1498 }
1499 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001500 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001501 return true;
1502 if (!HasTargetFlags)
1503 return false;
1504 if (Dest.isReg())
1505 return error(Loc, "register operands can't have target flags");
1506 Dest.setTargetFlags(TF);
1507 return false;
1508}
1509
Alex Lorenzdc24c172015-08-07 20:21:00 +00001510bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001511 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1512 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001513 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001514 bool IsNegative = Token.is(MIToken::minus);
1515 lex();
1516 if (Token.isNot(MIToken::IntegerLiteral))
1517 return error("expected an integer literal after '" + Sign + "'");
1518 if (Token.integerValue().getMinSignedBits() > 64)
1519 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001520 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001521 if (IsNegative)
1522 Offset = -Offset;
1523 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001524 return false;
1525}
1526
Alex Lorenz620f8912015-08-13 20:33:33 +00001527bool MIParser::parseAlignment(unsigned &Alignment) {
1528 assert(Token.is(MIToken::kw_align));
1529 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001530 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001531 return error("expected an integer literal after 'align'");
1532 if (getUnsigned(Alignment))
1533 return true;
1534 lex();
1535 return false;
1536}
1537
Alex Lorenzdc24c172015-08-07 20:21:00 +00001538bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1539 int64_t Offset = 0;
1540 if (parseOffset(Offset))
1541 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001542 Op.setOffset(Offset);
1543 return false;
1544}
1545
Alex Lorenz36593ac2015-08-19 23:27:07 +00001546bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001547 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001548 case MIToken::NamedIRValue: {
1549 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001550 break;
1551 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001552 case MIToken::IRValue: {
1553 unsigned SlotNumber = 0;
1554 if (getUnsigned(SlotNumber))
1555 return true;
1556 V = getIRValue(SlotNumber);
1557 break;
1558 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001559 case MIToken::NamedGlobalValue:
1560 case MIToken::GlobalValue: {
1561 GlobalValue *GV = nullptr;
1562 if (parseGlobalValue(GV))
1563 return true;
1564 V = GV;
1565 break;
1566 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001567 case MIToken::QuotedIRValue: {
1568 const Constant *C = nullptr;
1569 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1570 return true;
1571 V = C;
1572 break;
1573 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001574 default:
1575 llvm_unreachable("The current token should be an IR block reference");
1576 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001577 if (!V)
1578 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001579 return false;
1580}
1581
1582bool MIParser::getUint64(uint64_t &Result) {
1583 assert(Token.hasIntegerValue());
1584 if (Token.integerValue().getActiveBits() > 64)
1585 return error("expected 64-bit integer (too large)");
1586 Result = Token.integerValue().getZExtValue();
1587 return false;
1588}
1589
Alex Lorenza518b792015-08-04 00:24:45 +00001590bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
Alex Lorenze86d5152015-08-06 18:26:36 +00001591 const unsigned OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001592 switch (Token.kind()) {
1593 case MIToken::kw_volatile:
1594 Flags |= MachineMemOperand::MOVolatile;
1595 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001596 case MIToken::kw_non_temporal:
1597 Flags |= MachineMemOperand::MONonTemporal;
1598 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001599 case MIToken::kw_invariant:
1600 Flags |= MachineMemOperand::MOInvariant;
1601 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001602 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001603 default:
1604 llvm_unreachable("The current token should be a memory operand flag");
1605 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001606 if (OldFlags == Flags)
1607 // We know that the same flag is specified more than once when the flags
1608 // weren't modified.
1609 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001610 lex();
1611 return false;
1612}
1613
Alex Lorenz91097a32015-08-12 20:33:26 +00001614bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1615 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001616 case MIToken::kw_stack:
1617 PSV = MF.getPSVManager().getStack();
1618 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001619 case MIToken::kw_got:
1620 PSV = MF.getPSVManager().getGOT();
1621 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001622 case MIToken::kw_jump_table:
1623 PSV = MF.getPSVManager().getJumpTable();
1624 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001625 case MIToken::kw_constant_pool:
1626 PSV = MF.getPSVManager().getConstantPool();
1627 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001628 case MIToken::FixedStackObject: {
1629 int FI;
1630 if (parseFixedStackFrameIndex(FI))
1631 return true;
1632 PSV = MF.getPSVManager().getFixedStack(FI);
1633 // The token was already consumed, so use return here instead of break.
1634 return false;
1635 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001636 case MIToken::kw_call_entry: {
1637 lex();
1638 switch (Token.kind()) {
1639 case MIToken::GlobalValue:
1640 case MIToken::NamedGlobalValue: {
1641 GlobalValue *GV = nullptr;
1642 if (parseGlobalValue(GV))
1643 return true;
1644 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1645 break;
1646 }
1647 case MIToken::ExternalSymbol:
1648 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1649 MF.createExternalSymbolName(Token.stringValue()));
1650 break;
1651 default:
1652 return error(
1653 "expected a global value or an external symbol after 'call-entry'");
1654 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001655 break;
1656 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001657 default:
1658 llvm_unreachable("The current token should be pseudo source value");
1659 }
1660 lex();
1661 return false;
1662}
1663
1664bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001665 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001666 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Alex Lorenz0d009642015-08-20 00:12:57 +00001667 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001668 const PseudoSourceValue *PSV = nullptr;
1669 if (parseMemoryPseudoSourceValue(PSV))
1670 return true;
1671 int64_t Offset = 0;
1672 if (parseOffset(Offset))
1673 return true;
1674 Dest = MachinePointerInfo(PSV, Offset);
1675 return false;
1676 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001677 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1678 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001679 Token.isNot(MIToken::NamedGlobalValue) &&
1680 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001681 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001682 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001683 if (parseIRValue(V))
1684 return true;
1685 if (!V->getType()->isPointerTy())
1686 return error("expected a pointer IR value");
1687 lex();
1688 int64_t Offset = 0;
1689 if (parseOffset(Offset))
1690 return true;
1691 Dest = MachinePointerInfo(V, Offset);
1692 return false;
1693}
1694
Alex Lorenz4af7e612015-08-03 23:08:19 +00001695bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1696 if (expectAndConsume(MIToken::lparen))
1697 return true;
Alex Lorenza518b792015-08-04 00:24:45 +00001698 unsigned Flags = 0;
1699 while (Token.isMemoryOperandFlag()) {
1700 if (parseMemoryOperandFlag(Flags))
1701 return true;
1702 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001703 if (Token.isNot(MIToken::Identifier) ||
1704 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1705 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001706 if (Token.stringValue() == "load")
1707 Flags |= MachineMemOperand::MOLoad;
1708 else
1709 Flags |= MachineMemOperand::MOStore;
1710 lex();
1711
1712 if (Token.isNot(MIToken::IntegerLiteral))
1713 return error("expected the size integer literal after memory operation");
1714 uint64_t Size;
1715 if (getUint64(Size))
1716 return true;
1717 lex();
1718
1719 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1720 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1721 return error(Twine("expected '") + Word + "'");
1722 lex();
1723
Alex Lorenz91097a32015-08-12 20:33:26 +00001724 MachinePointerInfo Ptr = MachinePointerInfo();
1725 if (parseMachinePointerInfo(Ptr))
Alex Lorenz83127732015-08-07 20:26:52 +00001726 return true;
Alex Lorenz61420f72015-08-07 20:48:30 +00001727 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001728 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001729 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001730 while (consumeIfPresent(MIToken::comma)) {
1731 switch (Token.kind()) {
1732 case MIToken::kw_align:
1733 if (parseAlignment(BaseAlignment))
1734 return true;
1735 break;
1736 case MIToken::md_tbaa:
1737 lex();
1738 if (parseMDNode(AAInfo.TBAA))
1739 return true;
1740 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001741 case MIToken::md_alias_scope:
1742 lex();
1743 if (parseMDNode(AAInfo.Scope))
1744 return true;
1745 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001746 case MIToken::md_noalias:
1747 lex();
1748 if (parseMDNode(AAInfo.NoAlias))
1749 return true;
1750 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001751 case MIToken::md_range:
1752 lex();
1753 if (parseMDNode(Range))
1754 return true;
1755 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001756 // TODO: Report an error on duplicate metadata nodes.
1757 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001758 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1759 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001760 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001761 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001762 if (expectAndConsume(MIToken::rparen))
1763 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001764 Dest =
1765 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001766 return false;
1767}
1768
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001769void MIParser::initNames2InstrOpCodes() {
1770 if (!Names2InstrOpCodes.empty())
1771 return;
1772 const auto *TII = MF.getSubtarget().getInstrInfo();
1773 assert(TII && "Expected target instruction info");
1774 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1775 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1776}
1777
1778bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1779 initNames2InstrOpCodes();
1780 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1781 if (InstrInfo == Names2InstrOpCodes.end())
1782 return true;
1783 OpCode = InstrInfo->getValue();
1784 return false;
1785}
1786
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001787void MIParser::initNames2Regs() {
1788 if (!Names2Regs.empty())
1789 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001790 // The '%noreg' register is the register 0.
1791 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001792 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1793 assert(TRI && "Expected target register info");
1794 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1795 bool WasInserted =
1796 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1797 .second;
1798 (void)WasInserted;
1799 assert(WasInserted && "Expected registers to be unique case-insensitively");
1800 }
1801}
1802
1803bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1804 initNames2Regs();
1805 auto RegInfo = Names2Regs.find(RegName);
1806 if (RegInfo == Names2Regs.end())
1807 return true;
1808 Reg = RegInfo->getValue();
1809 return false;
1810}
1811
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001812void MIParser::initNames2RegMasks() {
1813 if (!Names2RegMasks.empty())
1814 return;
1815 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1816 assert(TRI && "Expected target register info");
1817 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1818 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1819 assert(RegMasks.size() == RegMaskNames.size());
1820 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1821 Names2RegMasks.insert(
1822 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1823}
1824
1825const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1826 initNames2RegMasks();
1827 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1828 if (RegMaskInfo == Names2RegMasks.end())
1829 return nullptr;
1830 return RegMaskInfo->getValue();
1831}
1832
Alex Lorenz2eacca82015-07-13 23:24:34 +00001833void MIParser::initNames2SubRegIndices() {
1834 if (!Names2SubRegIndices.empty())
1835 return;
1836 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1837 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1838 Names2SubRegIndices.insert(
1839 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1840}
1841
1842unsigned MIParser::getSubRegIndex(StringRef Name) {
1843 initNames2SubRegIndices();
1844 auto SubRegInfo = Names2SubRegIndices.find(Name);
1845 if (SubRegInfo == Names2SubRegIndices.end())
1846 return 0;
1847 return SubRegInfo->getValue();
1848}
1849
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001850static void initSlots2BasicBlocks(
1851 const Function &F,
1852 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1853 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001854 MST.incorporateFunction(F);
1855 for (auto &BB : F) {
1856 if (BB.hasName())
1857 continue;
1858 int Slot = MST.getLocalSlot(&BB);
1859 if (Slot == -1)
1860 continue;
1861 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1862 }
1863}
1864
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001865static const BasicBlock *getIRBlockFromSlot(
1866 unsigned Slot,
1867 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001868 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1869 if (BlockInfo == Slots2BasicBlocks.end())
1870 return nullptr;
1871 return BlockInfo->second;
1872}
1873
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001874const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1875 if (Slots2BasicBlocks.empty())
1876 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1877 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1878}
1879
1880const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1881 if (&F == MF.getFunction())
1882 return getIRBlock(Slot);
1883 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1884 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1885 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1886}
1887
Alex Lorenzdd13be02015-08-19 23:31:05 +00001888static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
1889 DenseMap<unsigned, const Value *> &Slots2Values) {
1890 int Slot = MST.getLocalSlot(V);
1891 if (Slot == -1)
1892 return;
1893 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
1894}
1895
1896/// Creates the mapping from slot numbers to function's unnamed IR values.
1897static void initSlots2Values(const Function &F,
1898 DenseMap<unsigned, const Value *> &Slots2Values) {
1899 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1900 MST.incorporateFunction(F);
1901 for (const auto &Arg : F.args())
1902 mapValueToSlot(&Arg, MST, Slots2Values);
1903 for (const auto &BB : F) {
1904 mapValueToSlot(&BB, MST, Slots2Values);
1905 for (const auto &I : BB)
1906 mapValueToSlot(&I, MST, Slots2Values);
1907 }
1908}
1909
1910const Value *MIParser::getIRValue(unsigned Slot) {
1911 if (Slots2Values.empty())
1912 initSlots2Values(*MF.getFunction(), Slots2Values);
1913 auto ValueInfo = Slots2Values.find(Slot);
1914 if (ValueInfo == Slots2Values.end())
1915 return nullptr;
1916 return ValueInfo->second;
1917}
1918
Alex Lorenzef5c1962015-07-28 23:02:45 +00001919void MIParser::initNames2TargetIndices() {
1920 if (!Names2TargetIndices.empty())
1921 return;
1922 const auto *TII = MF.getSubtarget().getInstrInfo();
1923 assert(TII && "Expected target instruction info");
1924 auto Indices = TII->getSerializableTargetIndices();
1925 for (const auto &I : Indices)
1926 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1927}
1928
1929bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1930 initNames2TargetIndices();
1931 auto IndexInfo = Names2TargetIndices.find(Name);
1932 if (IndexInfo == Names2TargetIndices.end())
1933 return true;
1934 Index = IndexInfo->second;
1935 return false;
1936}
1937
Alex Lorenz49873a82015-08-06 00:44:07 +00001938void MIParser::initNames2DirectTargetFlags() {
1939 if (!Names2DirectTargetFlags.empty())
1940 return;
1941 const auto *TII = MF.getSubtarget().getInstrInfo();
1942 assert(TII && "Expected target instruction info");
1943 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1944 for (const auto &I : Flags)
1945 Names2DirectTargetFlags.insert(
1946 std::make_pair(StringRef(I.second), I.first));
1947}
1948
1949bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1950 initNames2DirectTargetFlags();
1951 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1952 if (FlagInfo == Names2DirectTargetFlags.end())
1953 return true;
1954 Flag = FlagInfo->second;
1955 return false;
1956}
1957
Alex Lorenzf3630112015-08-18 22:52:15 +00001958void MIParser::initNames2BitmaskTargetFlags() {
1959 if (!Names2BitmaskTargetFlags.empty())
1960 return;
1961 const auto *TII = MF.getSubtarget().getInstrInfo();
1962 assert(TII && "Expected target instruction info");
1963 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
1964 for (const auto &I : Flags)
1965 Names2BitmaskTargetFlags.insert(
1966 std::make_pair(StringRef(I.second), I.first));
1967}
1968
1969bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
1970 initNames2BitmaskTargetFlags();
1971 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
1972 if (FlagInfo == Names2BitmaskTargetFlags.end())
1973 return true;
1974 Flag = FlagInfo->second;
1975 return false;
1976}
1977
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001978bool llvm::parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src,
1979 PerFunctionMIParsingState &PFS,
1980 const SlotMapping &IRSlots,
1981 SMDiagnostic &Error) {
1982 SourceMgr SM;
1983 SM.AddNewSourceBuffer(
1984 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1985 SMLoc());
1986 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1987 .parseBasicBlockDefinitions(PFS.MBBSlots);
1988}
1989
1990bool llvm::parseMachineInstructions(MachineFunction &MF, StringRef Src,
1991 const PerFunctionMIParsingState &PFS,
1992 const SlotMapping &IRSlots,
1993 SMDiagnostic &Error) {
1994 SourceMgr SM;
1995 SM.AddNewSourceBuffer(
1996 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1997 SMLoc());
1998 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001999}
Alex Lorenzf09df002015-06-30 18:16:42 +00002000
Alex Lorenz7a503fa2015-07-07 17:46:43 +00002001bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
2002 MachineFunction &MF, StringRef Src,
2003 const PerFunctionMIParsingState &PFS,
2004 const SlotMapping &IRSlots, SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00002005 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002006}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002007
2008bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
2009 MachineFunction &MF, StringRef Src,
2010 const PerFunctionMIParsingState &PFS,
2011 const SlotMapping &IRSlots,
2012 SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00002013 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2014 .parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002015}
Alex Lorenz12045a42015-07-27 17:42:45 +00002016
2017bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
2018 MachineFunction &MF, StringRef Src,
2019 const PerFunctionMIParsingState &PFS,
2020 const SlotMapping &IRSlots,
2021 SMDiagnostic &Error) {
2022 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2023 .parseStandaloneVirtualRegister(Reg);
2024}
Alex Lorenza314d812015-08-18 22:26:26 +00002025
2026bool llvm::parseStackObjectReference(int &FI, SourceMgr &SM,
2027 MachineFunction &MF, StringRef Src,
2028 const PerFunctionMIParsingState &PFS,
2029 const SlotMapping &IRSlots,
2030 SMDiagnostic &Error) {
2031 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2032 .parseStandaloneStackObject(FI);
2033}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002034
2035bool llvm::parseMDNode(MDNode *&Node, SourceMgr &SM, MachineFunction &MF,
2036 StringRef Src, const PerFunctionMIParsingState &PFS,
2037 const SlotMapping &IRSlots, SMDiagnostic &Error) {
2038 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMDNode(Node);
2039}