blob: 5a8e96df7603abdcb4ffd28475d30a5eae453792 [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"
20#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000021#include "llvm/CodeGen/MachineFrameInfo.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"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000026#include "llvm/IR/Instructions.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000027#include "llvm/IR/Constants.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000028#include "llvm/IR/Module.h"
Alex Lorenz8a1915b2015-07-27 22:42:41 +000029#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000030#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000031#include "llvm/Support/raw_ostream.h"
32#include "llvm/Support/SourceMgr.h"
33#include "llvm/Target/TargetSubtargetInfo.h"
34#include "llvm/Target/TargetInstrInfo.h"
35
36using namespace llvm;
37
38namespace {
39
Alex Lorenz36962cd2015-07-07 02:08:46 +000040/// A wrapper struct around the 'MachineOperand' struct that includes a source
Alex Lorenzfeb6b432015-08-19 19:19:16 +000041/// range and other attributes.
42struct ParsedMachineOperand {
Alex Lorenz36962cd2015-07-07 02:08:46 +000043 MachineOperand Operand;
44 StringRef::iterator Begin;
45 StringRef::iterator End;
Alex Lorenz5ef93b02015-08-19 19:05:34 +000046 Optional<unsigned> TiedDefIdx;
Alex Lorenz36962cd2015-07-07 02:08:46 +000047
Alex Lorenzfeb6b432015-08-19 19:19:16 +000048 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
49 StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
Alex Lorenz5ef93b02015-08-19 19:05:34 +000050 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
51 if (TiedDefIdx)
52 assert(Operand.isReg() && Operand.isUse() &&
53 "Only used register operands can be tied");
54 }
Alex Lorenz36962cd2015-07-07 02:08:46 +000055};
56
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000057class MIParser {
58 SourceMgr &SM;
59 MachineFunction &MF;
60 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000061 StringRef Source, CurrentSource;
62 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000063 const PerFunctionMIParsingState &PFS;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000064 /// Maps from indices to unnamed global values and metadata nodes.
65 const SlotMapping &IRSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000066 /// Maps from instruction names to op codes.
67 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000068 /// Maps from register names to registers.
69 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000070 /// Maps from register mask names to register masks.
71 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000072 /// Maps from subregister names to subregister indices.
73 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000074 /// Maps from slot numbers to function's unnamed basic blocks.
75 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzdd13be02015-08-19 23:31:05 +000076 /// Maps from slot numbers to function's unnamed values.
77 DenseMap<unsigned, const Value *> Slots2Values;
Alex Lorenzef5c1962015-07-28 23:02:45 +000078 /// Maps from target index names to target indices.
79 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +000080 /// Maps from direct target flag names to the direct target flag values.
81 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenzf3630112015-08-18 22:52:15 +000082 /// Maps from direct target flag names to the bitmask target flag values.
83 StringMap<unsigned> Names2BitmaskTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000084
85public:
86 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000087 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000088 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000089
Alex Lorenz91370c52015-06-22 20:37:46 +000090 void lex();
91
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000092 /// Report an error at the current location with the given message.
93 ///
94 /// This function always return true.
95 bool error(const Twine &Msg);
96
Alex Lorenz91370c52015-06-22 20:37:46 +000097 /// Report an error at the given location with the given message.
98 ///
99 /// This function always return true.
100 bool error(StringRef::iterator Loc, const Twine &Msg);
101
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000102 bool
103 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
104 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +0000105 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +0000106 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
107 bool parseStandaloneNamedRegister(unsigned &Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +0000108 bool parseStandaloneVirtualRegister(unsigned &Reg);
Alex Lorenza314d812015-08-18 22:26:26 +0000109 bool parseStandaloneStackObject(int &FI);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000110 bool parseStandaloneMDNode(MDNode *&Node);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000111
112 bool
113 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
114 bool parseBasicBlock(MachineBasicBlock &MBB);
115 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
116 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000117
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000118 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000119 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000120 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000121 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
122 bool parseRegisterOperand(MachineOperand &Dest,
123 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000124 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000125 bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
126 const Constant *&C);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000127 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Alex Lorenz05e38822015-08-05 18:52:21 +0000128 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000129 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000130 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000131 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000132 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000133 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000134 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000135 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000136 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000137 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000138 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000139 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000140 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000141 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000142 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000143 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000144 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000145 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000146 bool parseIRBlock(BasicBlock *&BB, const Function &F);
147 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000148 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000149 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000150 bool parseMachineOperand(MachineOperand &Dest,
151 Optional<unsigned> &TiedDefIdx);
152 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
153 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000154 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000155 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000156 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000157 bool parseIRValue(const Value *&V);
Alex Lorenza518b792015-08-04 00:24:45 +0000158 bool parseMemoryOperandFlag(unsigned &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000159 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
160 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000161 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000162
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000163private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000164 /// Convert the integer literal in the current token into an unsigned integer.
165 ///
166 /// Return true if an error occurred.
167 bool getUnsigned(unsigned &Result);
168
Alex Lorenz4af7e612015-08-03 23:08:19 +0000169 /// Convert the integer literal in the current token into an uint64.
170 ///
171 /// Return true if an error occurred.
172 bool getUint64(uint64_t &Result);
173
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000174 /// If the current token is of the given kind, consume it and return false.
175 /// Otherwise report an error and return true.
176 bool expectAndConsume(MIToken::TokenKind TokenKind);
177
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000178 /// If the current token is of the given kind, consume it and return true.
179 /// Otherwise return false.
180 bool consumeIfPresent(MIToken::TokenKind TokenKind);
181
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000182 void initNames2InstrOpCodes();
183
184 /// Try to convert an instruction name to an opcode. Return true if the
185 /// instruction name is invalid.
186 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000187
Alex Lorenze5a44662015-07-17 00:24:15 +0000188 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000189
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000190 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000191 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000192
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000193 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000194 const MCInstrDesc &MCID);
195
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000196 void initNames2Regs();
197
198 /// Try to convert a register name to a register number. Return true if the
199 /// register name is invalid.
200 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000201
202 void initNames2RegMasks();
203
204 /// Check if the given identifier is a name of a register mask.
205 ///
206 /// Return null if the identifier isn't a register mask.
207 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000208
209 void initNames2SubRegIndices();
210
211 /// Check if the given identifier is a name of a subregister index.
212 ///
213 /// Return 0 if the name isn't a subregister index class.
214 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000215
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000216 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000217 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000218
Alex Lorenzdd13be02015-08-19 23:31:05 +0000219 const Value *getIRValue(unsigned Slot);
220
Alex Lorenzef5c1962015-07-28 23:02:45 +0000221 void initNames2TargetIndices();
222
223 /// Try to convert a name of target index to the corresponding target index.
224 ///
225 /// Return true if the name isn't a name of a target index.
226 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000227
228 void initNames2DirectTargetFlags();
229
230 /// Try to convert a name of a direct target flag to the corresponding
231 /// target flag.
232 ///
233 /// Return true if the name isn't a name of a direct flag.
234 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000235
236 void initNames2BitmaskTargetFlags();
237
238 /// Try to convert a name of a bitmask target flag to the corresponding
239 /// target flag.
240 ///
241 /// Return true if the name isn't a name of a bitmask target flag.
242 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000243};
244
245} // end anonymous namespace
246
247MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000248 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000249 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000250 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz3fb77682015-08-06 23:17:42 +0000251 PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000252
Alex Lorenz91370c52015-06-22 20:37:46 +0000253void MIParser::lex() {
254 CurrentSource = lexMIToken(
255 CurrentSource, Token,
256 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
257}
258
259bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
260
261bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000262 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000263 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
264 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
265 // Create an ordinary diagnostic when the source manager's buffer is the
266 // source string.
267 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
268 return true;
269 }
270 // Create a diagnostic for a YAML string literal.
271 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
272 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
273 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000274 return true;
275}
276
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000277static const char *toString(MIToken::TokenKind TokenKind) {
278 switch (TokenKind) {
279 case MIToken::comma:
280 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000281 case MIToken::equal:
282 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000283 case MIToken::colon:
284 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000285 case MIToken::lparen:
286 return "'('";
287 case MIToken::rparen:
288 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000289 default:
290 return "<unknown token>";
291 }
292}
293
294bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
295 if (Token.isNot(TokenKind))
296 return error(Twine("expected ") + toString(TokenKind));
297 lex();
298 return false;
299}
300
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000301bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
302 if (Token.isNot(TokenKind))
303 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000304 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000305 return true;
306}
Alex Lorenz91370c52015-06-22 20:37:46 +0000307
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000308bool MIParser::parseBasicBlockDefinition(
309 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
310 assert(Token.is(MIToken::MachineBasicBlockLabel));
311 unsigned ID = 0;
312 if (getUnsigned(ID))
313 return true;
314 auto Loc = Token.location();
315 auto Name = Token.stringValue();
316 lex();
317 bool HasAddressTaken = false;
318 bool IsLandingPad = false;
319 unsigned Alignment = 0;
320 BasicBlock *BB = nullptr;
321 if (consumeIfPresent(MIToken::lparen)) {
322 do {
323 // TODO: Report an error when multiple same attributes are specified.
324 switch (Token.kind()) {
325 case MIToken::kw_address_taken:
326 HasAddressTaken = true;
327 lex();
328 break;
329 case MIToken::kw_landing_pad:
330 IsLandingPad = true;
331 lex();
332 break;
333 case MIToken::kw_align:
334 if (parseAlignment(Alignment))
335 return true;
336 break;
337 case MIToken::IRBlock:
338 // TODO: Report an error when both name and ir block are specified.
339 if (parseIRBlock(BB, *MF.getFunction()))
340 return true;
341 lex();
342 break;
343 default:
344 break;
345 }
346 } while (consumeIfPresent(MIToken::comma));
347 if (expectAndConsume(MIToken::rparen))
348 return true;
349 }
350 if (expectAndConsume(MIToken::colon))
351 return true;
352
353 if (!Name.empty()) {
354 BB = dyn_cast_or_null<BasicBlock>(
355 MF.getFunction()->getValueSymbolTable().lookup(Name));
356 if (!BB)
357 return error(Loc, Twine("basic block '") + Name +
358 "' is not defined in the function '" +
359 MF.getName() + "'");
360 }
361 auto *MBB = MF.CreateMachineBasicBlock(BB);
362 MF.insert(MF.end(), MBB);
363 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
364 if (!WasInserted)
365 return error(Loc, Twine("redefinition of machine basic block with id #") +
366 Twine(ID));
367 if (Alignment)
368 MBB->setAlignment(Alignment);
369 if (HasAddressTaken)
370 MBB->setHasAddressTaken();
Reid Kleckner0e288232015-08-27 23:27:47 +0000371 MBB->setIsEHPad(IsLandingPad);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000372 return false;
373}
374
375bool MIParser::parseBasicBlockDefinitions(
376 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
377 lex();
378 // Skip until the first machine basic block.
379 while (Token.is(MIToken::Newline))
380 lex();
381 if (Token.isErrorOrEOF())
382 return Token.isError();
383 if (Token.isNot(MIToken::MachineBasicBlockLabel))
384 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000385 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000386 do {
387 if (parseBasicBlockDefinition(MBBSlots))
388 return true;
389 bool IsAfterNewline = false;
390 // Skip until the next machine basic block.
391 while (true) {
392 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
393 Token.isErrorOrEOF())
394 break;
395 else if (Token.is(MIToken::MachineBasicBlockLabel))
396 return error("basic block definition should be located at the start of "
397 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000398 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000399 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000400 continue;
401 }
402 IsAfterNewline = false;
403 if (Token.is(MIToken::lbrace))
404 ++BraceDepth;
405 if (Token.is(MIToken::rbrace)) {
406 if (!BraceDepth)
407 return error("extraneous closing brace ('}')");
408 --BraceDepth;
409 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000410 lex();
411 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000412 // Verify that we closed all of the '{' at the end of a file or a block.
413 if (!Token.isError() && BraceDepth)
414 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000415 } while (!Token.isErrorOrEOF());
416 return Token.isError();
417}
418
419bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
420 assert(Token.is(MIToken::kw_liveins));
421 lex();
422 if (expectAndConsume(MIToken::colon))
423 return true;
424 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
425 return false;
426 do {
427 if (Token.isNot(MIToken::NamedRegister))
428 return error("expected a named register");
429 unsigned Reg = 0;
430 if (parseRegister(Reg))
431 return true;
432 MBB.addLiveIn(Reg);
433 lex();
434 } while (consumeIfPresent(MIToken::comma));
435 return false;
436}
437
438bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
439 assert(Token.is(MIToken::kw_successors));
440 lex();
441 if (expectAndConsume(MIToken::colon))
442 return true;
443 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
444 return false;
445 do {
446 if (Token.isNot(MIToken::MachineBasicBlock))
447 return error("expected a machine basic block reference");
448 MachineBasicBlock *SuccMBB = nullptr;
449 if (parseMBBReference(SuccMBB))
450 return true;
451 lex();
452 unsigned Weight = 0;
453 if (consumeIfPresent(MIToken::lparen)) {
454 if (Token.isNot(MIToken::IntegerLiteral))
455 return error("expected an integer literal after '('");
456 if (getUnsigned(Weight))
457 return true;
458 lex();
459 if (expectAndConsume(MIToken::rparen))
460 return true;
461 }
462 MBB.addSuccessor(SuccMBB, Weight);
463 } while (consumeIfPresent(MIToken::comma));
464 return false;
465}
466
467bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
468 // Skip the definition.
469 assert(Token.is(MIToken::MachineBasicBlockLabel));
470 lex();
471 if (consumeIfPresent(MIToken::lparen)) {
472 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
473 lex();
474 consumeIfPresent(MIToken::rparen);
475 }
476 consumeIfPresent(MIToken::colon);
477
478 // Parse the liveins and successors.
479 // N.B: Multiple lists of successors and liveins are allowed and they're
480 // merged into one.
481 // Example:
482 // liveins: %edi
483 // liveins: %esi
484 //
485 // is equivalent to
486 // liveins: %edi, %esi
487 while (true) {
488 if (Token.is(MIToken::kw_successors)) {
489 if (parseBasicBlockSuccessors(MBB))
490 return true;
491 } else if (Token.is(MIToken::kw_liveins)) {
492 if (parseBasicBlockLiveins(MBB))
493 return true;
494 } else if (consumeIfPresent(MIToken::Newline)) {
495 continue;
496 } else
497 break;
498 if (!Token.isNewlineOrEOF())
499 return error("expected line break at the end of a list");
500 lex();
501 }
502
503 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000504 bool IsInBundle = false;
505 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000506 while (true) {
507 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
508 return false;
509 else if (consumeIfPresent(MIToken::Newline))
510 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000511 if (consumeIfPresent(MIToken::rbrace)) {
512 // The first parsing pass should verify that all closing '}' have an
513 // opening '{'.
514 assert(IsInBundle);
515 IsInBundle = false;
516 continue;
517 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000518 MachineInstr *MI = nullptr;
519 if (parse(MI))
520 return true;
521 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000522 if (IsInBundle) {
523 PrevMI->setFlag(MachineInstr::BundledSucc);
524 MI->setFlag(MachineInstr::BundledPred);
525 }
526 PrevMI = MI;
527 if (Token.is(MIToken::lbrace)) {
528 if (IsInBundle)
529 return error("nested instruction bundles are not allowed");
530 lex();
531 // This instruction is the start of the bundle.
532 MI->setFlag(MachineInstr::BundledSucc);
533 IsInBundle = true;
534 if (!Token.is(MIToken::Newline))
535 // The next instruction can be on the same line.
536 continue;
537 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000538 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
539 lex();
540 }
541 return false;
542}
543
544bool MIParser::parseBasicBlocks() {
545 lex();
546 // Skip until the first machine basic block.
547 while (Token.is(MIToken::Newline))
548 lex();
549 if (Token.isErrorOrEOF())
550 return Token.isError();
551 // The first parsing pass should have verified that this token is a MBB label
552 // in the 'parseBasicBlockDefinitions' method.
553 assert(Token.is(MIToken::MachineBasicBlockLabel));
554 do {
555 MachineBasicBlock *MBB = nullptr;
556 if (parseMBBReference(MBB))
557 return true;
558 if (parseBasicBlock(*MBB))
559 return true;
560 // The method 'parseBasicBlock' should parse the whole block until the next
561 // block or the end of file.
562 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
563 } while (Token.isNot(MIToken::Eof));
564 return false;
565}
566
567bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000568 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000569 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000570 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000571 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000572 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000573 Optional<unsigned> TiedDefIdx;
574 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000575 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000576 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000577 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000578 if (Token.isNot(MIToken::comma))
579 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000580 lex();
581 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000582 if (!Operands.empty() && expectAndConsume(MIToken::equal))
583 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000584
Alex Lorenze5a44662015-07-17 00:24:15 +0000585 unsigned OpCode, Flags = 0;
586 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000587 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000588
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000589 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000590 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000591 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000592 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000593 Optional<unsigned> TiedDefIdx;
594 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000595 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000596 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000597 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000598 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
599 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000600 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000601 if (Token.isNot(MIToken::comma))
602 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000603 lex();
604 }
605
Alex Lorenz46d760d2015-07-22 21:15:11 +0000606 DebugLoc DebugLocation;
607 if (Token.is(MIToken::kw_debug_location)) {
608 lex();
609 if (Token.isNot(MIToken::exclaim))
610 return error("expected a metadata node after 'debug-location'");
611 MDNode *Node = nullptr;
612 if (parseMDNode(Node))
613 return true;
614 DebugLocation = DebugLoc(Node);
615 }
616
Alex Lorenz4af7e612015-08-03 23:08:19 +0000617 // Parse the machine memory operands.
618 SmallVector<MachineMemOperand *, 2> MemOperands;
619 if (Token.is(MIToken::coloncolon)) {
620 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000621 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000622 MachineMemOperand *MemOp = nullptr;
623 if (parseMachineMemoryOperand(MemOp))
624 return true;
625 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000626 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000627 break;
628 if (Token.isNot(MIToken::comma))
629 return error("expected ',' before the next machine memory operand");
630 lex();
631 }
632 }
633
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000634 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000635 if (!MCID.isVariadic()) {
636 // FIXME: Move the implicit operand verification to the machine verifier.
637 if (verifyImplicitOperands(Operands, MCID))
638 return true;
639 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000640
Alex Lorenzcb268d42015-07-06 23:07:26 +0000641 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000642 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000643 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000644 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000645 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000646 if (assignRegisterTies(*MI, Operands))
647 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000648 if (MemOperands.empty())
649 return false;
650 MachineInstr::mmo_iterator MemRefs =
651 MF.allocateMemRefsArray(MemOperands.size());
652 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
653 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000654 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000655}
656
Alex Lorenz1ea60892015-07-27 20:29:27 +0000657bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000658 lex();
659 if (Token.isNot(MIToken::MachineBasicBlock))
660 return error("expected a machine basic block reference");
661 if (parseMBBReference(MBB))
662 return true;
663 lex();
664 if (Token.isNot(MIToken::Eof))
665 return error(
666 "expected end of string after the machine basic block reference");
667 return false;
668}
669
Alex Lorenz1ea60892015-07-27 20:29:27 +0000670bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000671 lex();
672 if (Token.isNot(MIToken::NamedRegister))
673 return error("expected a named register");
674 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000675 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000676 lex();
677 if (Token.isNot(MIToken::Eof))
678 return error("expected end of string after the register reference");
679 return false;
680}
681
Alex Lorenz12045a42015-07-27 17:42:45 +0000682bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
683 lex();
684 if (Token.isNot(MIToken::VirtualRegister))
685 return error("expected a virtual register");
686 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000687 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000688 lex();
689 if (Token.isNot(MIToken::Eof))
690 return error("expected end of string after the register reference");
691 return false;
692}
693
Alex Lorenza314d812015-08-18 22:26:26 +0000694bool MIParser::parseStandaloneStackObject(int &FI) {
695 lex();
696 if (Token.isNot(MIToken::StackObject))
697 return error("expected a stack object");
698 if (parseStackFrameIndex(FI))
699 return true;
700 if (Token.isNot(MIToken::Eof))
701 return error("expected end of string after the stack object reference");
702 return false;
703}
704
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000705bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
706 lex();
707 if (Token.isNot(MIToken::exclaim))
708 return error("expected a metadata node");
709 if (parseMDNode(Node))
710 return true;
711 if (Token.isNot(MIToken::Eof))
712 return error("expected end of string after the metadata node");
713 return false;
714}
715
Alex Lorenz36962cd2015-07-07 02:08:46 +0000716static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
717 assert(MO.isImplicit());
718 return MO.isDef() ? "implicit-def" : "implicit";
719}
720
721static std::string getRegisterName(const TargetRegisterInfo *TRI,
722 unsigned Reg) {
723 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
724 return StringRef(TRI->getName(Reg)).lower();
725}
726
Alex Lorenz0153e592015-09-10 14:04:34 +0000727/// Return true if the parsed machine operands contain a given machine operand.
728static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
729 ArrayRef<ParsedMachineOperand> Operands) {
730 for (const auto &I : Operands) {
731 if (ImplicitOperand.isIdenticalTo(I.Operand))
732 return true;
733 }
734 return false;
735}
736
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000737bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
738 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000739 if (MCID.isCall())
740 // We can't verify call instructions as they can contain arbitrary implicit
741 // register and register mask operands.
742 return false;
743
744 // Gather all the expected implicit operands.
745 SmallVector<MachineOperand, 4> ImplicitOperands;
746 if (MCID.ImplicitDefs)
747 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
748 ImplicitOperands.push_back(
749 MachineOperand::CreateReg(*ImpDefs, true, true));
750 if (MCID.ImplicitUses)
751 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
752 ImplicitOperands.push_back(
753 MachineOperand::CreateReg(*ImpUses, false, true));
754
755 const auto *TRI = MF.getSubtarget().getRegisterInfo();
756 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000757 for (const auto &I : ImplicitOperands) {
758 if (isImplicitOperandIn(I, Operands))
759 continue;
760 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000761 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000762 printImplicitRegisterFlag(I) + " %" +
763 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000764 }
765 return false;
766}
767
Alex Lorenze5a44662015-07-17 00:24:15 +0000768bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
769 if (Token.is(MIToken::kw_frame_setup)) {
770 Flags |= MachineInstr::FrameSetup;
771 lex();
772 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000773 if (Token.isNot(MIToken::Identifier))
774 return error("expected a machine instruction");
775 StringRef InstrName = Token.stringValue();
776 if (parseInstrName(InstrName, OpCode))
777 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000778 lex();
779 return false;
780}
781
782bool MIParser::parseRegister(unsigned &Reg) {
783 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000784 case MIToken::underscore:
785 Reg = 0;
786 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000787 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000788 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000789 if (getRegisterByName(Name, Reg))
790 return error(Twine("unknown register name '") + Name + "'");
791 break;
792 }
Alex Lorenz53464512015-07-10 22:51:20 +0000793 case MIToken::VirtualRegister: {
794 unsigned ID;
795 if (getUnsigned(ID))
796 return true;
797 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
798 if (RegInfo == PFS.VirtualRegisterSlots.end())
799 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
800 "'");
801 Reg = RegInfo->second;
802 break;
803 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000804 // TODO: Parse other register kinds.
805 default:
806 llvm_unreachable("The current token should be a register");
807 }
808 return false;
809}
810
Alex Lorenzcb268d42015-07-06 23:07:26 +0000811bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000812 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000813 switch (Token.kind()) {
814 case MIToken::kw_implicit:
815 Flags |= RegState::Implicit;
816 break;
817 case MIToken::kw_implicit_define:
818 Flags |= RegState::ImplicitDefine;
819 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000820 case MIToken::kw_def:
821 Flags |= RegState::Define;
822 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000823 case MIToken::kw_dead:
824 Flags |= RegState::Dead;
825 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000826 case MIToken::kw_killed:
827 Flags |= RegState::Kill;
828 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000829 case MIToken::kw_undef:
830 Flags |= RegState::Undef;
831 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000832 case MIToken::kw_internal:
833 Flags |= RegState::InternalRead;
834 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000835 case MIToken::kw_early_clobber:
836 Flags |= RegState::EarlyClobber;
837 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000838 case MIToken::kw_debug_use:
839 Flags |= RegState::Debug;
840 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000841 default:
842 llvm_unreachable("The current token should be a register flag");
843 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000844 if (OldFlags == Flags)
845 // We know that the same flag is specified more than once when the flags
846 // weren't modified.
847 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000848 lex();
849 return false;
850}
851
Alex Lorenz2eacca82015-07-13 23:24:34 +0000852bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
853 assert(Token.is(MIToken::colon));
854 lex();
855 if (Token.isNot(MIToken::Identifier))
856 return error("expected a subregister index after ':'");
857 auto Name = Token.stringValue();
858 SubReg = getSubRegIndex(Name);
859 if (!SubReg)
860 return error(Twine("use of unknown subregister index '") + Name + "'");
861 lex();
862 return false;
863}
864
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000865bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
866 if (!consumeIfPresent(MIToken::kw_tied_def))
867 return error("expected 'tied-def' after '('");
868 if (Token.isNot(MIToken::IntegerLiteral))
869 return error("expected an integer literal after 'tied-def'");
870 if (getUnsigned(TiedDefIdx))
871 return true;
872 lex();
873 if (expectAndConsume(MIToken::rparen))
874 return true;
875 return false;
876}
877
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000878bool MIParser::assignRegisterTies(MachineInstr &MI,
879 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000880 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
881 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
882 if (!Operands[I].TiedDefIdx)
883 continue;
884 // The parser ensures that this operand is a register use, so we just have
885 // to check the tied-def operand.
886 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
887 if (DefIdx >= E)
888 return error(Operands[I].Begin,
889 Twine("use of invalid tied-def operand index '" +
890 Twine(DefIdx) + "'; instruction has only ") +
891 Twine(E) + " operands");
892 const auto &DefOperand = Operands[DefIdx].Operand;
893 if (!DefOperand.isReg() || !DefOperand.isDef())
894 // FIXME: add note with the def operand.
895 return error(Operands[I].Begin,
896 Twine("use of invalid tied-def operand index '") +
897 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
898 " isn't a defined register");
899 // Check that the tied-def operand wasn't tied elsewhere.
900 for (const auto &TiedPair : TiedRegisterPairs) {
901 if (TiedPair.first == DefIdx)
902 return error(Operands[I].Begin,
903 Twine("the tied-def operand #") + Twine(DefIdx) +
904 " is already tied with another register operand");
905 }
906 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
907 }
908 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
909 // indices must be less than tied max.
910 for (const auto &TiedPair : TiedRegisterPairs)
911 MI.tieOperands(TiedPair.first, TiedPair.second);
912 return false;
913}
914
915bool MIParser::parseRegisterOperand(MachineOperand &Dest,
916 Optional<unsigned> &TiedDefIdx,
917 bool IsDef) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000918 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000919 unsigned Flags = IsDef ? RegState::Define : 0;
920 while (Token.isRegisterFlag()) {
921 if (parseRegisterFlag(Flags))
922 return true;
923 }
924 if (!Token.isRegister())
925 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000926 if (parseRegister(Reg))
927 return true;
928 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000929 unsigned SubReg = 0;
930 if (Token.is(MIToken::colon)) {
931 if (parseSubRegisterIndex(SubReg))
932 return true;
933 }
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000934 if ((Flags & RegState::Define) == 0 && consumeIfPresent(MIToken::lparen)) {
935 unsigned Idx;
936 if (parseRegisterTiedDefIndex(Idx))
937 return true;
938 TiedDefIdx = Idx;
939 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000940 Dest = MachineOperand::CreateReg(
941 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000942 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +0000943 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
944 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000945 return false;
946}
947
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000948bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
949 assert(Token.is(MIToken::IntegerLiteral));
950 const APSInt &Int = Token.integerValue();
951 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +0000952 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000953 Dest = MachineOperand::CreateImm(Int.getExtValue());
954 lex();
955 return false;
956}
957
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000958bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
959 const Constant *&C) {
960 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000961 SMDiagnostic Err;
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000962 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
963 &IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000964 if (!C)
965 return error(Loc + Err.getColumnNo(), Err.getMessage());
966 return false;
967}
968
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000969bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
970 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
971 return true;
972 lex();
973 return false;
974}
975
Alex Lorenz05e38822015-08-05 18:52:21 +0000976bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
977 assert(Token.is(MIToken::IntegerType));
978 auto Loc = Token.location();
979 lex();
980 if (Token.isNot(MIToken::IntegerLiteral))
981 return error("expected an integer literal");
982 const Constant *C = nullptr;
983 if (parseIRConstant(Loc, C))
984 return true;
985 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
986 return false;
987}
988
Alex Lorenzad156fb2015-07-31 20:49:21 +0000989bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
990 auto Loc = Token.location();
991 lex();
992 if (Token.isNot(MIToken::FloatingPointLiteral))
993 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000994 const Constant *C = nullptr;
995 if (parseIRConstant(Loc, C))
996 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000997 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
998 return false;
999}
1000
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001001bool MIParser::getUnsigned(unsigned &Result) {
1002 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1003 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1004 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1005 if (Val64 == Limit)
1006 return error("expected 32-bit integer (too large)");
1007 Result = Val64;
1008 return false;
1009}
1010
Alex Lorenzf09df002015-06-30 18:16:42 +00001011bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001012 assert(Token.is(MIToken::MachineBasicBlock) ||
1013 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001014 unsigned Number;
1015 if (getUnsigned(Number))
1016 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001017 auto MBBInfo = PFS.MBBSlots.find(Number);
1018 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001019 return error(Twine("use of undefined machine basic block #") +
1020 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001021 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001022 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1023 return error(Twine("the name of machine basic block #") + Twine(Number) +
1024 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001025 return false;
1026}
1027
1028bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1029 MachineBasicBlock *MBB;
1030 if (parseMBBReference(MBB))
1031 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001032 Dest = MachineOperand::CreateMBB(MBB);
1033 lex();
1034 return false;
1035}
1036
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001037bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001038 assert(Token.is(MIToken::StackObject));
1039 unsigned ID;
1040 if (getUnsigned(ID))
1041 return true;
1042 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1043 if (ObjectInfo == PFS.StackObjectSlots.end())
1044 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1045 "'");
1046 StringRef Name;
1047 if (const auto *Alloca =
1048 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
1049 Name = Alloca->getName();
1050 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1051 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1052 "' isn't '" + Token.stringValue() + "'");
1053 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001054 FI = ObjectInfo->second;
1055 return false;
1056}
1057
1058bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1059 int FI;
1060 if (parseStackFrameIndex(FI))
1061 return true;
1062 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001063 return false;
1064}
1065
Alex Lorenzea882122015-08-12 21:17:02 +00001066bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001067 assert(Token.is(MIToken::FixedStackObject));
1068 unsigned ID;
1069 if (getUnsigned(ID))
1070 return true;
1071 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1072 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1073 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1074 Twine(ID) + "'");
1075 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001076 FI = ObjectInfo->second;
1077 return false;
1078}
1079
1080bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1081 int FI;
1082 if (parseFixedStackFrameIndex(FI))
1083 return true;
1084 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001085 return false;
1086}
1087
Alex Lorenz41df7d32015-07-28 17:09:52 +00001088bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001089 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001090 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001091 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001092 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001093 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001094 return error(Twine("use of undefined global value '") + Token.range() +
1095 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001096 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001097 }
1098 case MIToken::GlobalValue: {
1099 unsigned GVIdx;
1100 if (getUnsigned(GVIdx))
1101 return true;
1102 if (GVIdx >= IRSlots.GlobalValues.size())
1103 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1104 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001105 GV = IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001106 break;
1107 }
1108 default:
1109 llvm_unreachable("The current token should be a global value");
1110 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001111 return false;
1112}
1113
1114bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1115 GlobalValue *GV = nullptr;
1116 if (parseGlobalValue(GV))
1117 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001118 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001119 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001120 if (parseOperandsOffset(Dest))
1121 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001122 return false;
1123}
1124
Alex Lorenzab980492015-07-20 20:51:18 +00001125bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1126 assert(Token.is(MIToken::ConstantPoolItem));
1127 unsigned ID;
1128 if (getUnsigned(ID))
1129 return true;
1130 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1131 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1132 return error("use of undefined constant '%const." + Twine(ID) + "'");
1133 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001134 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001135 if (parseOperandsOffset(Dest))
1136 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001137 return false;
1138}
1139
Alex Lorenz31d70682015-07-15 23:38:35 +00001140bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1141 assert(Token.is(MIToken::JumpTableIndex));
1142 unsigned ID;
1143 if (getUnsigned(ID))
1144 return true;
1145 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1146 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1147 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1148 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001149 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1150 return false;
1151}
1152
Alex Lorenz6ede3742015-07-21 16:59:53 +00001153bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001154 assert(Token.is(MIToken::ExternalSymbol));
1155 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001156 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001157 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001158 if (parseOperandsOffset(Dest))
1159 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001160 return false;
1161}
1162
Alex Lorenz44f29252015-07-22 21:07:04 +00001163bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001164 assert(Token.is(MIToken::exclaim));
1165 auto Loc = Token.location();
1166 lex();
1167 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1168 return error("expected metadata id after '!'");
1169 unsigned ID;
1170 if (getUnsigned(ID))
1171 return true;
1172 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
1173 if (NodeInfo == IRSlots.MetadataNodes.end())
1174 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1175 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001176 Node = NodeInfo->second.get();
1177 return false;
1178}
1179
1180bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1181 MDNode *Node = nullptr;
1182 if (parseMDNode(Node))
1183 return true;
1184 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001185 return false;
1186}
1187
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001188bool MIParser::parseCFIOffset(int &Offset) {
1189 if (Token.isNot(MIToken::IntegerLiteral))
1190 return error("expected a cfi offset");
1191 if (Token.integerValue().getMinSignedBits() > 32)
1192 return error("expected a 32 bit integer (the cfi offset is too large)");
1193 Offset = (int)Token.integerValue().getExtValue();
1194 lex();
1195 return false;
1196}
1197
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001198bool MIParser::parseCFIRegister(unsigned &Reg) {
1199 if (Token.isNot(MIToken::NamedRegister))
1200 return error("expected a cfi register");
1201 unsigned LLVMReg;
1202 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001203 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001204 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1205 assert(TRI && "Expected target register info");
1206 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1207 if (DwarfReg < 0)
1208 return error("invalid DWARF register");
1209 Reg = (unsigned)DwarfReg;
1210 lex();
1211 return false;
1212}
1213
1214bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1215 auto Kind = Token.kind();
1216 lex();
1217 auto &MMI = MF.getMMI();
1218 int Offset;
1219 unsigned Reg;
1220 unsigned CFIIndex;
1221 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001222 case MIToken::kw_cfi_same_value:
1223 if (parseCFIRegister(Reg))
1224 return true;
1225 CFIIndex =
1226 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1227 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001228 case MIToken::kw_cfi_offset:
1229 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1230 parseCFIOffset(Offset))
1231 return true;
1232 CFIIndex =
1233 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1234 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001235 case MIToken::kw_cfi_def_cfa_register:
1236 if (parseCFIRegister(Reg))
1237 return true;
1238 CFIIndex =
1239 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1240 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001241 case MIToken::kw_cfi_def_cfa_offset:
1242 if (parseCFIOffset(Offset))
1243 return true;
1244 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1245 CFIIndex = MMI.addFrameInst(
1246 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1247 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001248 case MIToken::kw_cfi_def_cfa:
1249 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1250 parseCFIOffset(Offset))
1251 return true;
1252 // NB: MCCFIInstruction::createDefCfa negates the offset.
1253 CFIIndex =
1254 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1255 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001256 default:
1257 // TODO: Parse the other CFI operands.
1258 llvm_unreachable("The current token should be a cfi operand");
1259 }
1260 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001261 return false;
1262}
1263
Alex Lorenzdeb53492015-07-28 17:28:03 +00001264bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1265 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001266 case MIToken::NamedIRBlock: {
1267 BB = dyn_cast_or_null<BasicBlock>(
1268 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001269 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001270 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001271 break;
1272 }
1273 case MIToken::IRBlock: {
1274 unsigned SlotNumber = 0;
1275 if (getUnsigned(SlotNumber))
1276 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001277 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001278 if (!BB)
1279 return error(Twine("use of undefined IR block '%ir-block.") +
1280 Twine(SlotNumber) + "'");
1281 break;
1282 }
1283 default:
1284 llvm_unreachable("The current token should be an IR block reference");
1285 }
1286 return false;
1287}
1288
1289bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1290 assert(Token.is(MIToken::kw_blockaddress));
1291 lex();
1292 if (expectAndConsume(MIToken::lparen))
1293 return true;
1294 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001295 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001296 return error("expected a global value");
1297 GlobalValue *GV = nullptr;
1298 if (parseGlobalValue(GV))
1299 return true;
1300 auto *F = dyn_cast<Function>(GV);
1301 if (!F)
1302 return error("expected an IR function reference");
1303 lex();
1304 if (expectAndConsume(MIToken::comma))
1305 return true;
1306 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001307 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001308 return error("expected an IR block reference");
1309 if (parseIRBlock(BB, *F))
1310 return true;
1311 lex();
1312 if (expectAndConsume(MIToken::rparen))
1313 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001314 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001315 if (parseOperandsOffset(Dest))
1316 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001317 return false;
1318}
1319
Alex Lorenzef5c1962015-07-28 23:02:45 +00001320bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1321 assert(Token.is(MIToken::kw_target_index));
1322 lex();
1323 if (expectAndConsume(MIToken::lparen))
1324 return true;
1325 if (Token.isNot(MIToken::Identifier))
1326 return error("expected the name of the target index");
1327 int Index = 0;
1328 if (getTargetIndex(Token.stringValue(), Index))
1329 return error("use of undefined target index '" + Token.stringValue() + "'");
1330 lex();
1331 if (expectAndConsume(MIToken::rparen))
1332 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001333 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001334 if (parseOperandsOffset(Dest))
1335 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001336 return false;
1337}
1338
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001339bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1340 assert(Token.is(MIToken::kw_liveout));
1341 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1342 assert(TRI && "Expected target register info");
1343 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1344 lex();
1345 if (expectAndConsume(MIToken::lparen))
1346 return true;
1347 while (true) {
1348 if (Token.isNot(MIToken::NamedRegister))
1349 return error("expected a named register");
1350 unsigned Reg = 0;
1351 if (parseRegister(Reg))
1352 return true;
1353 lex();
1354 Mask[Reg / 32] |= 1U << (Reg % 32);
1355 // TODO: Report an error if the same register is used more than once.
1356 if (Token.isNot(MIToken::comma))
1357 break;
1358 lex();
1359 }
1360 if (expectAndConsume(MIToken::rparen))
1361 return true;
1362 Dest = MachineOperand::CreateRegLiveOut(Mask);
1363 return false;
1364}
1365
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001366bool MIParser::parseMachineOperand(MachineOperand &Dest,
1367 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001368 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001369 case MIToken::kw_implicit:
1370 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001371 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001372 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001373 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001374 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001375 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001376 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001377 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001378 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001379 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001380 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001381 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001382 case MIToken::IntegerLiteral:
1383 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001384 case MIToken::IntegerType:
1385 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001386 case MIToken::kw_half:
1387 case MIToken::kw_float:
1388 case MIToken::kw_double:
1389 case MIToken::kw_x86_fp80:
1390 case MIToken::kw_fp128:
1391 case MIToken::kw_ppc_fp128:
1392 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001393 case MIToken::MachineBasicBlock:
1394 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001395 case MIToken::StackObject:
1396 return parseStackObjectOperand(Dest);
1397 case MIToken::FixedStackObject:
1398 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001399 case MIToken::GlobalValue:
1400 case MIToken::NamedGlobalValue:
1401 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001402 case MIToken::ConstantPoolItem:
1403 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001404 case MIToken::JumpTableIndex:
1405 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001406 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001407 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001408 case MIToken::exclaim:
1409 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001410 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001411 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001412 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001413 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001414 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001415 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001416 case MIToken::kw_blockaddress:
1417 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001418 case MIToken::kw_target_index:
1419 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001420 case MIToken::kw_liveout:
1421 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001422 case MIToken::Error:
1423 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001424 case MIToken::Identifier:
1425 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1426 Dest = MachineOperand::CreateRegMask(RegMask);
1427 lex();
1428 break;
1429 }
1430 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001431 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001432 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001433 return error("expected a machine operand");
1434 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001435 return false;
1436}
1437
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001438bool MIParser::parseMachineOperandAndTargetFlags(
1439 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001440 unsigned TF = 0;
1441 bool HasTargetFlags = false;
1442 if (Token.is(MIToken::kw_target_flags)) {
1443 HasTargetFlags = true;
1444 lex();
1445 if (expectAndConsume(MIToken::lparen))
1446 return true;
1447 if (Token.isNot(MIToken::Identifier))
1448 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001449 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1450 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1451 return error("use of undefined target flag '" + Token.stringValue() +
1452 "'");
1453 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001454 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001455 while (Token.is(MIToken::comma)) {
1456 lex();
1457 if (Token.isNot(MIToken::Identifier))
1458 return error("expected the name of the target flag");
1459 unsigned BitFlag = 0;
1460 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1461 return error("use of undefined target flag '" + Token.stringValue() +
1462 "'");
1463 // TODO: Report an error when using a duplicate bit target flag.
1464 TF |= BitFlag;
1465 lex();
1466 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001467 if (expectAndConsume(MIToken::rparen))
1468 return true;
1469 }
1470 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001471 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001472 return true;
1473 if (!HasTargetFlags)
1474 return false;
1475 if (Dest.isReg())
1476 return error(Loc, "register operands can't have target flags");
1477 Dest.setTargetFlags(TF);
1478 return false;
1479}
1480
Alex Lorenzdc24c172015-08-07 20:21:00 +00001481bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001482 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1483 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001484 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001485 bool IsNegative = Token.is(MIToken::minus);
1486 lex();
1487 if (Token.isNot(MIToken::IntegerLiteral))
1488 return error("expected an integer literal after '" + Sign + "'");
1489 if (Token.integerValue().getMinSignedBits() > 64)
1490 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001491 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001492 if (IsNegative)
1493 Offset = -Offset;
1494 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001495 return false;
1496}
1497
Alex Lorenz620f8912015-08-13 20:33:33 +00001498bool MIParser::parseAlignment(unsigned &Alignment) {
1499 assert(Token.is(MIToken::kw_align));
1500 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001501 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001502 return error("expected an integer literal after 'align'");
1503 if (getUnsigned(Alignment))
1504 return true;
1505 lex();
1506 return false;
1507}
1508
Alex Lorenzdc24c172015-08-07 20:21:00 +00001509bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1510 int64_t Offset = 0;
1511 if (parseOffset(Offset))
1512 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001513 Op.setOffset(Offset);
1514 return false;
1515}
1516
Alex Lorenz36593ac2015-08-19 23:27:07 +00001517bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001518 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001519 case MIToken::NamedIRValue: {
1520 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001521 break;
1522 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001523 case MIToken::IRValue: {
1524 unsigned SlotNumber = 0;
1525 if (getUnsigned(SlotNumber))
1526 return true;
1527 V = getIRValue(SlotNumber);
1528 break;
1529 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001530 case MIToken::NamedGlobalValue:
1531 case MIToken::GlobalValue: {
1532 GlobalValue *GV = nullptr;
1533 if (parseGlobalValue(GV))
1534 return true;
1535 V = GV;
1536 break;
1537 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001538 case MIToken::QuotedIRValue: {
1539 const Constant *C = nullptr;
1540 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1541 return true;
1542 V = C;
1543 break;
1544 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001545 default:
1546 llvm_unreachable("The current token should be an IR block reference");
1547 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001548 if (!V)
1549 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001550 return false;
1551}
1552
1553bool MIParser::getUint64(uint64_t &Result) {
1554 assert(Token.hasIntegerValue());
1555 if (Token.integerValue().getActiveBits() > 64)
1556 return error("expected 64-bit integer (too large)");
1557 Result = Token.integerValue().getZExtValue();
1558 return false;
1559}
1560
Alex Lorenza518b792015-08-04 00:24:45 +00001561bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
Alex Lorenze86d5152015-08-06 18:26:36 +00001562 const unsigned OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001563 switch (Token.kind()) {
1564 case MIToken::kw_volatile:
1565 Flags |= MachineMemOperand::MOVolatile;
1566 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001567 case MIToken::kw_non_temporal:
1568 Flags |= MachineMemOperand::MONonTemporal;
1569 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001570 case MIToken::kw_invariant:
1571 Flags |= MachineMemOperand::MOInvariant;
1572 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001573 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001574 default:
1575 llvm_unreachable("The current token should be a memory operand flag");
1576 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001577 if (OldFlags == Flags)
1578 // We know that the same flag is specified more than once when the flags
1579 // weren't modified.
1580 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001581 lex();
1582 return false;
1583}
1584
Alex Lorenz91097a32015-08-12 20:33:26 +00001585bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1586 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001587 case MIToken::kw_stack:
1588 PSV = MF.getPSVManager().getStack();
1589 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001590 case MIToken::kw_got:
1591 PSV = MF.getPSVManager().getGOT();
1592 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001593 case MIToken::kw_jump_table:
1594 PSV = MF.getPSVManager().getJumpTable();
1595 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001596 case MIToken::kw_constant_pool:
1597 PSV = MF.getPSVManager().getConstantPool();
1598 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001599 case MIToken::FixedStackObject: {
1600 int FI;
1601 if (parseFixedStackFrameIndex(FI))
1602 return true;
1603 PSV = MF.getPSVManager().getFixedStack(FI);
1604 // The token was already consumed, so use return here instead of break.
1605 return false;
1606 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001607 case MIToken::kw_call_entry: {
1608 lex();
1609 switch (Token.kind()) {
1610 case MIToken::GlobalValue:
1611 case MIToken::NamedGlobalValue: {
1612 GlobalValue *GV = nullptr;
1613 if (parseGlobalValue(GV))
1614 return true;
1615 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1616 break;
1617 }
1618 case MIToken::ExternalSymbol:
1619 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1620 MF.createExternalSymbolName(Token.stringValue()));
1621 break;
1622 default:
1623 return error(
1624 "expected a global value or an external symbol after 'call-entry'");
1625 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001626 break;
1627 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001628 default:
1629 llvm_unreachable("The current token should be pseudo source value");
1630 }
1631 lex();
1632 return false;
1633}
1634
1635bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001636 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001637 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Alex Lorenz0d009642015-08-20 00:12:57 +00001638 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001639 const PseudoSourceValue *PSV = nullptr;
1640 if (parseMemoryPseudoSourceValue(PSV))
1641 return true;
1642 int64_t Offset = 0;
1643 if (parseOffset(Offset))
1644 return true;
1645 Dest = MachinePointerInfo(PSV, Offset);
1646 return false;
1647 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001648 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1649 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001650 Token.isNot(MIToken::NamedGlobalValue) &&
1651 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001652 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001653 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001654 if (parseIRValue(V))
1655 return true;
1656 if (!V->getType()->isPointerTy())
1657 return error("expected a pointer IR value");
1658 lex();
1659 int64_t Offset = 0;
1660 if (parseOffset(Offset))
1661 return true;
1662 Dest = MachinePointerInfo(V, Offset);
1663 return false;
1664}
1665
Alex Lorenz4af7e612015-08-03 23:08:19 +00001666bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1667 if (expectAndConsume(MIToken::lparen))
1668 return true;
Alex Lorenza518b792015-08-04 00:24:45 +00001669 unsigned Flags = 0;
1670 while (Token.isMemoryOperandFlag()) {
1671 if (parseMemoryOperandFlag(Flags))
1672 return true;
1673 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001674 if (Token.isNot(MIToken::Identifier) ||
1675 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1676 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001677 if (Token.stringValue() == "load")
1678 Flags |= MachineMemOperand::MOLoad;
1679 else
1680 Flags |= MachineMemOperand::MOStore;
1681 lex();
1682
1683 if (Token.isNot(MIToken::IntegerLiteral))
1684 return error("expected the size integer literal after memory operation");
1685 uint64_t Size;
1686 if (getUint64(Size))
1687 return true;
1688 lex();
1689
1690 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1691 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1692 return error(Twine("expected '") + Word + "'");
1693 lex();
1694
Alex Lorenz91097a32015-08-12 20:33:26 +00001695 MachinePointerInfo Ptr = MachinePointerInfo();
1696 if (parseMachinePointerInfo(Ptr))
Alex Lorenz83127732015-08-07 20:26:52 +00001697 return true;
Alex Lorenz61420f72015-08-07 20:48:30 +00001698 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001699 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001700 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001701 while (consumeIfPresent(MIToken::comma)) {
1702 switch (Token.kind()) {
1703 case MIToken::kw_align:
1704 if (parseAlignment(BaseAlignment))
1705 return true;
1706 break;
1707 case MIToken::md_tbaa:
1708 lex();
1709 if (parseMDNode(AAInfo.TBAA))
1710 return true;
1711 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001712 case MIToken::md_alias_scope:
1713 lex();
1714 if (parseMDNode(AAInfo.Scope))
1715 return true;
1716 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001717 case MIToken::md_noalias:
1718 lex();
1719 if (parseMDNode(AAInfo.NoAlias))
1720 return true;
1721 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001722 case MIToken::md_range:
1723 lex();
1724 if (parseMDNode(Range))
1725 return true;
1726 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001727 // TODO: Report an error on duplicate metadata nodes.
1728 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001729 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1730 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001731 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001732 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001733 if (expectAndConsume(MIToken::rparen))
1734 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001735 Dest =
1736 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001737 return false;
1738}
1739
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001740void MIParser::initNames2InstrOpCodes() {
1741 if (!Names2InstrOpCodes.empty())
1742 return;
1743 const auto *TII = MF.getSubtarget().getInstrInfo();
1744 assert(TII && "Expected target instruction info");
1745 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1746 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1747}
1748
1749bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1750 initNames2InstrOpCodes();
1751 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1752 if (InstrInfo == Names2InstrOpCodes.end())
1753 return true;
1754 OpCode = InstrInfo->getValue();
1755 return false;
1756}
1757
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001758void MIParser::initNames2Regs() {
1759 if (!Names2Regs.empty())
1760 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001761 // The '%noreg' register is the register 0.
1762 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001763 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1764 assert(TRI && "Expected target register info");
1765 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1766 bool WasInserted =
1767 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1768 .second;
1769 (void)WasInserted;
1770 assert(WasInserted && "Expected registers to be unique case-insensitively");
1771 }
1772}
1773
1774bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1775 initNames2Regs();
1776 auto RegInfo = Names2Regs.find(RegName);
1777 if (RegInfo == Names2Regs.end())
1778 return true;
1779 Reg = RegInfo->getValue();
1780 return false;
1781}
1782
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001783void MIParser::initNames2RegMasks() {
1784 if (!Names2RegMasks.empty())
1785 return;
1786 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1787 assert(TRI && "Expected target register info");
1788 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1789 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1790 assert(RegMasks.size() == RegMaskNames.size());
1791 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1792 Names2RegMasks.insert(
1793 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1794}
1795
1796const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1797 initNames2RegMasks();
1798 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1799 if (RegMaskInfo == Names2RegMasks.end())
1800 return nullptr;
1801 return RegMaskInfo->getValue();
1802}
1803
Alex Lorenz2eacca82015-07-13 23:24:34 +00001804void MIParser::initNames2SubRegIndices() {
1805 if (!Names2SubRegIndices.empty())
1806 return;
1807 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1808 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1809 Names2SubRegIndices.insert(
1810 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1811}
1812
1813unsigned MIParser::getSubRegIndex(StringRef Name) {
1814 initNames2SubRegIndices();
1815 auto SubRegInfo = Names2SubRegIndices.find(Name);
1816 if (SubRegInfo == Names2SubRegIndices.end())
1817 return 0;
1818 return SubRegInfo->getValue();
1819}
1820
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001821static void initSlots2BasicBlocks(
1822 const Function &F,
1823 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1824 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001825 MST.incorporateFunction(F);
1826 for (auto &BB : F) {
1827 if (BB.hasName())
1828 continue;
1829 int Slot = MST.getLocalSlot(&BB);
1830 if (Slot == -1)
1831 continue;
1832 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1833 }
1834}
1835
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001836static const BasicBlock *getIRBlockFromSlot(
1837 unsigned Slot,
1838 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001839 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1840 if (BlockInfo == Slots2BasicBlocks.end())
1841 return nullptr;
1842 return BlockInfo->second;
1843}
1844
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001845const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1846 if (Slots2BasicBlocks.empty())
1847 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1848 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1849}
1850
1851const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1852 if (&F == MF.getFunction())
1853 return getIRBlock(Slot);
1854 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1855 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1856 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1857}
1858
Alex Lorenzdd13be02015-08-19 23:31:05 +00001859static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
1860 DenseMap<unsigned, const Value *> &Slots2Values) {
1861 int Slot = MST.getLocalSlot(V);
1862 if (Slot == -1)
1863 return;
1864 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
1865}
1866
1867/// Creates the mapping from slot numbers to function's unnamed IR values.
1868static void initSlots2Values(const Function &F,
1869 DenseMap<unsigned, const Value *> &Slots2Values) {
1870 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1871 MST.incorporateFunction(F);
1872 for (const auto &Arg : F.args())
1873 mapValueToSlot(&Arg, MST, Slots2Values);
1874 for (const auto &BB : F) {
1875 mapValueToSlot(&BB, MST, Slots2Values);
1876 for (const auto &I : BB)
1877 mapValueToSlot(&I, MST, Slots2Values);
1878 }
1879}
1880
1881const Value *MIParser::getIRValue(unsigned Slot) {
1882 if (Slots2Values.empty())
1883 initSlots2Values(*MF.getFunction(), Slots2Values);
1884 auto ValueInfo = Slots2Values.find(Slot);
1885 if (ValueInfo == Slots2Values.end())
1886 return nullptr;
1887 return ValueInfo->second;
1888}
1889
Alex Lorenzef5c1962015-07-28 23:02:45 +00001890void MIParser::initNames2TargetIndices() {
1891 if (!Names2TargetIndices.empty())
1892 return;
1893 const auto *TII = MF.getSubtarget().getInstrInfo();
1894 assert(TII && "Expected target instruction info");
1895 auto Indices = TII->getSerializableTargetIndices();
1896 for (const auto &I : Indices)
1897 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1898}
1899
1900bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1901 initNames2TargetIndices();
1902 auto IndexInfo = Names2TargetIndices.find(Name);
1903 if (IndexInfo == Names2TargetIndices.end())
1904 return true;
1905 Index = IndexInfo->second;
1906 return false;
1907}
1908
Alex Lorenz49873a82015-08-06 00:44:07 +00001909void MIParser::initNames2DirectTargetFlags() {
1910 if (!Names2DirectTargetFlags.empty())
1911 return;
1912 const auto *TII = MF.getSubtarget().getInstrInfo();
1913 assert(TII && "Expected target instruction info");
1914 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1915 for (const auto &I : Flags)
1916 Names2DirectTargetFlags.insert(
1917 std::make_pair(StringRef(I.second), I.first));
1918}
1919
1920bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1921 initNames2DirectTargetFlags();
1922 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1923 if (FlagInfo == Names2DirectTargetFlags.end())
1924 return true;
1925 Flag = FlagInfo->second;
1926 return false;
1927}
1928
Alex Lorenzf3630112015-08-18 22:52:15 +00001929void MIParser::initNames2BitmaskTargetFlags() {
1930 if (!Names2BitmaskTargetFlags.empty())
1931 return;
1932 const auto *TII = MF.getSubtarget().getInstrInfo();
1933 assert(TII && "Expected target instruction info");
1934 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
1935 for (const auto &I : Flags)
1936 Names2BitmaskTargetFlags.insert(
1937 std::make_pair(StringRef(I.second), I.first));
1938}
1939
1940bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
1941 initNames2BitmaskTargetFlags();
1942 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
1943 if (FlagInfo == Names2BitmaskTargetFlags.end())
1944 return true;
1945 Flag = FlagInfo->second;
1946 return false;
1947}
1948
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001949bool llvm::parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src,
1950 PerFunctionMIParsingState &PFS,
1951 const SlotMapping &IRSlots,
1952 SMDiagnostic &Error) {
1953 SourceMgr SM;
1954 SM.AddNewSourceBuffer(
1955 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1956 SMLoc());
1957 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1958 .parseBasicBlockDefinitions(PFS.MBBSlots);
1959}
1960
1961bool llvm::parseMachineInstructions(MachineFunction &MF, StringRef Src,
1962 const PerFunctionMIParsingState &PFS,
1963 const SlotMapping &IRSlots,
1964 SMDiagnostic &Error) {
1965 SourceMgr SM;
1966 SM.AddNewSourceBuffer(
1967 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1968 SMLoc());
1969 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001970}
Alex Lorenzf09df002015-06-30 18:16:42 +00001971
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001972bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1973 MachineFunction &MF, StringRef Src,
1974 const PerFunctionMIParsingState &PFS,
1975 const SlotMapping &IRSlots, SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001976 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00001977}
Alex Lorenz9fab3702015-07-14 21:24:41 +00001978
1979bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1980 MachineFunction &MF, StringRef Src,
1981 const PerFunctionMIParsingState &PFS,
1982 const SlotMapping &IRSlots,
1983 SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001984 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1985 .parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00001986}
Alex Lorenz12045a42015-07-27 17:42:45 +00001987
1988bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1989 MachineFunction &MF, StringRef Src,
1990 const PerFunctionMIParsingState &PFS,
1991 const SlotMapping &IRSlots,
1992 SMDiagnostic &Error) {
1993 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1994 .parseStandaloneVirtualRegister(Reg);
1995}
Alex Lorenza314d812015-08-18 22:26:26 +00001996
1997bool llvm::parseStackObjectReference(int &FI, SourceMgr &SM,
1998 MachineFunction &MF, StringRef Src,
1999 const PerFunctionMIParsingState &PFS,
2000 const SlotMapping &IRSlots,
2001 SMDiagnostic &Error) {
2002 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2003 .parseStandaloneStackObject(FI);
2004}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002005
2006bool llvm::parseMDNode(MDNode *&Node, SourceMgr &SM, MachineFunction &MF,
2007 StringRef Src, const PerFunctionMIParsingState &PFS,
2008 const SlotMapping &IRSlots, SMDiagnostic &Error) {
2009 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMDNode(Node);
2010}