blob: a17bc40fe8658134a3f7bed3fd34eb8f72b0a833 [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 Lorenzef5c1962015-07-28 23:02:45 +000076 /// Maps from target index names to target indices.
77 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +000078 /// Maps from direct target flag names to the direct target flag values.
79 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenzf3630112015-08-18 22:52:15 +000080 /// Maps from direct target flag names to the bitmask target flag values.
81 StringMap<unsigned> Names2BitmaskTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000082
83public:
84 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000085 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000086 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000087
Alex Lorenz91370c52015-06-22 20:37:46 +000088 void lex();
89
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000090 /// Report an error at the current location with the given message.
91 ///
92 /// This function always return true.
93 bool error(const Twine &Msg);
94
Alex Lorenz91370c52015-06-22 20:37:46 +000095 /// Report an error at the given location with the given message.
96 ///
97 /// This function always return true.
98 bool error(StringRef::iterator Loc, const Twine &Msg);
99
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000100 bool
101 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
102 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +0000103 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +0000104 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
105 bool parseStandaloneNamedRegister(unsigned &Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +0000106 bool parseStandaloneVirtualRegister(unsigned &Reg);
Alex Lorenza314d812015-08-18 22:26:26 +0000107 bool parseStandaloneStackObject(int &FI);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000108 bool parseStandaloneMDNode(MDNode *&Node);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000109
110 bool
111 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
112 bool parseBasicBlock(MachineBasicBlock &MBB);
113 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
114 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000115
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000116 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000117 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000118 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000119 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
120 bool parseRegisterOperand(MachineOperand &Dest,
121 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000122 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000123 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Alex Lorenz05e38822015-08-05 18:52:21 +0000124 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000125 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000126 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000127 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000128 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000129 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000130 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000131 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000132 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000133 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000134 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000135 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000136 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000137 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000138 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000139 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000140 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000141 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000142 bool parseIRBlock(BasicBlock *&BB, const Function &F);
143 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000144 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000145 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000146 bool parseMachineOperand(MachineOperand &Dest,
147 Optional<unsigned> &TiedDefIdx);
148 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
149 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000150 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000151 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000152 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000153 bool parseIRValue(Value *&V);
Alex Lorenza518b792015-08-04 00:24:45 +0000154 bool parseMemoryOperandFlag(unsigned &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000155 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
156 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000157 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000158
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000159private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000160 /// Convert the integer literal in the current token into an unsigned integer.
161 ///
162 /// Return true if an error occurred.
163 bool getUnsigned(unsigned &Result);
164
Alex Lorenz4af7e612015-08-03 23:08:19 +0000165 /// Convert the integer literal in the current token into an uint64.
166 ///
167 /// Return true if an error occurred.
168 bool getUint64(uint64_t &Result);
169
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000170 /// If the current token is of the given kind, consume it and return false.
171 /// Otherwise report an error and return true.
172 bool expectAndConsume(MIToken::TokenKind TokenKind);
173
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000174 /// If the current token is of the given kind, consume it and return true.
175 /// Otherwise return false.
176 bool consumeIfPresent(MIToken::TokenKind TokenKind);
177
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000178 void initNames2InstrOpCodes();
179
180 /// Try to convert an instruction name to an opcode. Return true if the
181 /// instruction name is invalid.
182 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000183
Alex Lorenze5a44662015-07-17 00:24:15 +0000184 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000185
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000186 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000187 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000188
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000189 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000190 const MCInstrDesc &MCID);
191
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000192 void initNames2Regs();
193
194 /// Try to convert a register name to a register number. Return true if the
195 /// register name is invalid.
196 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000197
198 void initNames2RegMasks();
199
200 /// Check if the given identifier is a name of a register mask.
201 ///
202 /// Return null if the identifier isn't a register mask.
203 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000204
205 void initNames2SubRegIndices();
206
207 /// Check if the given identifier is a name of a subregister index.
208 ///
209 /// Return 0 if the name isn't a subregister index class.
210 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000211
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000212 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000213 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000214
215 void initNames2TargetIndices();
216
217 /// Try to convert a name of target index to the corresponding target index.
218 ///
219 /// Return true if the name isn't a name of a target index.
220 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000221
222 void initNames2DirectTargetFlags();
223
224 /// Try to convert a name of a direct target flag to the corresponding
225 /// target flag.
226 ///
227 /// Return true if the name isn't a name of a direct flag.
228 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000229
230 void initNames2BitmaskTargetFlags();
231
232 /// Try to convert a name of a bitmask target flag to the corresponding
233 /// target flag.
234 ///
235 /// Return true if the name isn't a name of a bitmask target flag.
236 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000237};
238
239} // end anonymous namespace
240
241MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000242 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000243 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000244 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz3fb77682015-08-06 23:17:42 +0000245 PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000246
Alex Lorenz91370c52015-06-22 20:37:46 +0000247void MIParser::lex() {
248 CurrentSource = lexMIToken(
249 CurrentSource, Token,
250 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
251}
252
253bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
254
255bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000256 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000257 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
258 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
259 // Create an ordinary diagnostic when the source manager's buffer is the
260 // source string.
261 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
262 return true;
263 }
264 // Create a diagnostic for a YAML string literal.
265 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
266 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
267 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000268 return true;
269}
270
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000271static const char *toString(MIToken::TokenKind TokenKind) {
272 switch (TokenKind) {
273 case MIToken::comma:
274 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000275 case MIToken::equal:
276 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000277 case MIToken::colon:
278 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000279 case MIToken::lparen:
280 return "'('";
281 case MIToken::rparen:
282 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000283 default:
284 return "<unknown token>";
285 }
286}
287
288bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
289 if (Token.isNot(TokenKind))
290 return error(Twine("expected ") + toString(TokenKind));
291 lex();
292 return false;
293}
294
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000295bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
296 if (Token.isNot(TokenKind))
297 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000298 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000299 return true;
300}
Alex Lorenz91370c52015-06-22 20:37:46 +0000301
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000302bool MIParser::parseBasicBlockDefinition(
303 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
304 assert(Token.is(MIToken::MachineBasicBlockLabel));
305 unsigned ID = 0;
306 if (getUnsigned(ID))
307 return true;
308 auto Loc = Token.location();
309 auto Name = Token.stringValue();
310 lex();
311 bool HasAddressTaken = false;
312 bool IsLandingPad = false;
313 unsigned Alignment = 0;
314 BasicBlock *BB = nullptr;
315 if (consumeIfPresent(MIToken::lparen)) {
316 do {
317 // TODO: Report an error when multiple same attributes are specified.
318 switch (Token.kind()) {
319 case MIToken::kw_address_taken:
320 HasAddressTaken = true;
321 lex();
322 break;
323 case MIToken::kw_landing_pad:
324 IsLandingPad = true;
325 lex();
326 break;
327 case MIToken::kw_align:
328 if (parseAlignment(Alignment))
329 return true;
330 break;
331 case MIToken::IRBlock:
332 // TODO: Report an error when both name and ir block are specified.
333 if (parseIRBlock(BB, *MF.getFunction()))
334 return true;
335 lex();
336 break;
337 default:
338 break;
339 }
340 } while (consumeIfPresent(MIToken::comma));
341 if (expectAndConsume(MIToken::rparen))
342 return true;
343 }
344 if (expectAndConsume(MIToken::colon))
345 return true;
346
347 if (!Name.empty()) {
348 BB = dyn_cast_or_null<BasicBlock>(
349 MF.getFunction()->getValueSymbolTable().lookup(Name));
350 if (!BB)
351 return error(Loc, Twine("basic block '") + Name +
352 "' is not defined in the function '" +
353 MF.getName() + "'");
354 }
355 auto *MBB = MF.CreateMachineBasicBlock(BB);
356 MF.insert(MF.end(), MBB);
357 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
358 if (!WasInserted)
359 return error(Loc, Twine("redefinition of machine basic block with id #") +
360 Twine(ID));
361 if (Alignment)
362 MBB->setAlignment(Alignment);
363 if (HasAddressTaken)
364 MBB->setHasAddressTaken();
365 MBB->setIsLandingPad(IsLandingPad);
366 return false;
367}
368
369bool MIParser::parseBasicBlockDefinitions(
370 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
371 lex();
372 // Skip until the first machine basic block.
373 while (Token.is(MIToken::Newline))
374 lex();
375 if (Token.isErrorOrEOF())
376 return Token.isError();
377 if (Token.isNot(MIToken::MachineBasicBlockLabel))
378 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000379 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000380 do {
381 if (parseBasicBlockDefinition(MBBSlots))
382 return true;
383 bool IsAfterNewline = false;
384 // Skip until the next machine basic block.
385 while (true) {
386 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
387 Token.isErrorOrEOF())
388 break;
389 else if (Token.is(MIToken::MachineBasicBlockLabel))
390 return error("basic block definition should be located at the start of "
391 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000392 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000393 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000394 continue;
395 }
396 IsAfterNewline = false;
397 if (Token.is(MIToken::lbrace))
398 ++BraceDepth;
399 if (Token.is(MIToken::rbrace)) {
400 if (!BraceDepth)
401 return error("extraneous closing brace ('}')");
402 --BraceDepth;
403 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000404 lex();
405 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000406 // Verify that we closed all of the '{' at the end of a file or a block.
407 if (!Token.isError() && BraceDepth)
408 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000409 } while (!Token.isErrorOrEOF());
410 return Token.isError();
411}
412
413bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
414 assert(Token.is(MIToken::kw_liveins));
415 lex();
416 if (expectAndConsume(MIToken::colon))
417 return true;
418 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
419 return false;
420 do {
421 if (Token.isNot(MIToken::NamedRegister))
422 return error("expected a named register");
423 unsigned Reg = 0;
424 if (parseRegister(Reg))
425 return true;
426 MBB.addLiveIn(Reg);
427 lex();
428 } while (consumeIfPresent(MIToken::comma));
429 return false;
430}
431
432bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
433 assert(Token.is(MIToken::kw_successors));
434 lex();
435 if (expectAndConsume(MIToken::colon))
436 return true;
437 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
438 return false;
439 do {
440 if (Token.isNot(MIToken::MachineBasicBlock))
441 return error("expected a machine basic block reference");
442 MachineBasicBlock *SuccMBB = nullptr;
443 if (parseMBBReference(SuccMBB))
444 return true;
445 lex();
446 unsigned Weight = 0;
447 if (consumeIfPresent(MIToken::lparen)) {
448 if (Token.isNot(MIToken::IntegerLiteral))
449 return error("expected an integer literal after '('");
450 if (getUnsigned(Weight))
451 return true;
452 lex();
453 if (expectAndConsume(MIToken::rparen))
454 return true;
455 }
456 MBB.addSuccessor(SuccMBB, Weight);
457 } while (consumeIfPresent(MIToken::comma));
458 return false;
459}
460
461bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
462 // Skip the definition.
463 assert(Token.is(MIToken::MachineBasicBlockLabel));
464 lex();
465 if (consumeIfPresent(MIToken::lparen)) {
466 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
467 lex();
468 consumeIfPresent(MIToken::rparen);
469 }
470 consumeIfPresent(MIToken::colon);
471
472 // Parse the liveins and successors.
473 // N.B: Multiple lists of successors and liveins are allowed and they're
474 // merged into one.
475 // Example:
476 // liveins: %edi
477 // liveins: %esi
478 //
479 // is equivalent to
480 // liveins: %edi, %esi
481 while (true) {
482 if (Token.is(MIToken::kw_successors)) {
483 if (parseBasicBlockSuccessors(MBB))
484 return true;
485 } else if (Token.is(MIToken::kw_liveins)) {
486 if (parseBasicBlockLiveins(MBB))
487 return true;
488 } else if (consumeIfPresent(MIToken::Newline)) {
489 continue;
490 } else
491 break;
492 if (!Token.isNewlineOrEOF())
493 return error("expected line break at the end of a list");
494 lex();
495 }
496
497 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000498 bool IsInBundle = false;
499 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000500 while (true) {
501 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
502 return false;
503 else if (consumeIfPresent(MIToken::Newline))
504 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000505 if (consumeIfPresent(MIToken::rbrace)) {
506 // The first parsing pass should verify that all closing '}' have an
507 // opening '{'.
508 assert(IsInBundle);
509 IsInBundle = false;
510 continue;
511 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000512 MachineInstr *MI = nullptr;
513 if (parse(MI))
514 return true;
515 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000516 if (IsInBundle) {
517 PrevMI->setFlag(MachineInstr::BundledSucc);
518 MI->setFlag(MachineInstr::BundledPred);
519 }
520 PrevMI = MI;
521 if (Token.is(MIToken::lbrace)) {
522 if (IsInBundle)
523 return error("nested instruction bundles are not allowed");
524 lex();
525 // This instruction is the start of the bundle.
526 MI->setFlag(MachineInstr::BundledSucc);
527 IsInBundle = true;
528 if (!Token.is(MIToken::Newline))
529 // The next instruction can be on the same line.
530 continue;
531 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000532 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
533 lex();
534 }
535 return false;
536}
537
538bool MIParser::parseBasicBlocks() {
539 lex();
540 // Skip until the first machine basic block.
541 while (Token.is(MIToken::Newline))
542 lex();
543 if (Token.isErrorOrEOF())
544 return Token.isError();
545 // The first parsing pass should have verified that this token is a MBB label
546 // in the 'parseBasicBlockDefinitions' method.
547 assert(Token.is(MIToken::MachineBasicBlockLabel));
548 do {
549 MachineBasicBlock *MBB = nullptr;
550 if (parseMBBReference(MBB))
551 return true;
552 if (parseBasicBlock(*MBB))
553 return true;
554 // The method 'parseBasicBlock' should parse the whole block until the next
555 // block or the end of file.
556 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
557 } while (Token.isNot(MIToken::Eof));
558 return false;
559}
560
561bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000562 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000563 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000564 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000565 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000566 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000567 Optional<unsigned> TiedDefIdx;
568 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000569 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000570 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000571 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000572 if (Token.isNot(MIToken::comma))
573 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000574 lex();
575 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000576 if (!Operands.empty() && expectAndConsume(MIToken::equal))
577 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000578
Alex Lorenze5a44662015-07-17 00:24:15 +0000579 unsigned OpCode, Flags = 0;
580 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000581 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000582
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000583 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000584 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000585 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000586 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000587 Optional<unsigned> TiedDefIdx;
588 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000589 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000590 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000591 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000592 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
593 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000594 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000595 if (Token.isNot(MIToken::comma))
596 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000597 lex();
598 }
599
Alex Lorenz46d760d2015-07-22 21:15:11 +0000600 DebugLoc DebugLocation;
601 if (Token.is(MIToken::kw_debug_location)) {
602 lex();
603 if (Token.isNot(MIToken::exclaim))
604 return error("expected a metadata node after 'debug-location'");
605 MDNode *Node = nullptr;
606 if (parseMDNode(Node))
607 return true;
608 DebugLocation = DebugLoc(Node);
609 }
610
Alex Lorenz4af7e612015-08-03 23:08:19 +0000611 // Parse the machine memory operands.
612 SmallVector<MachineMemOperand *, 2> MemOperands;
613 if (Token.is(MIToken::coloncolon)) {
614 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000615 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000616 MachineMemOperand *MemOp = nullptr;
617 if (parseMachineMemoryOperand(MemOp))
618 return true;
619 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000620 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000621 break;
622 if (Token.isNot(MIToken::comma))
623 return error("expected ',' before the next machine memory operand");
624 lex();
625 }
626 }
627
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000628 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000629 if (!MCID.isVariadic()) {
630 // FIXME: Move the implicit operand verification to the machine verifier.
631 if (verifyImplicitOperands(Operands, MCID))
632 return true;
633 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000634
Alex Lorenzcb268d42015-07-06 23:07:26 +0000635 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000636 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000637 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000638 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000639 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000640 if (assignRegisterTies(*MI, Operands))
641 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000642 if (MemOperands.empty())
643 return false;
644 MachineInstr::mmo_iterator MemRefs =
645 MF.allocateMemRefsArray(MemOperands.size());
646 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
647 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000648 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000649}
650
Alex Lorenz1ea60892015-07-27 20:29:27 +0000651bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000652 lex();
653 if (Token.isNot(MIToken::MachineBasicBlock))
654 return error("expected a machine basic block reference");
655 if (parseMBBReference(MBB))
656 return true;
657 lex();
658 if (Token.isNot(MIToken::Eof))
659 return error(
660 "expected end of string after the machine basic block reference");
661 return false;
662}
663
Alex Lorenz1ea60892015-07-27 20:29:27 +0000664bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000665 lex();
666 if (Token.isNot(MIToken::NamedRegister))
667 return error("expected a named register");
668 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000669 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000670 lex();
671 if (Token.isNot(MIToken::Eof))
672 return error("expected end of string after the register reference");
673 return false;
674}
675
Alex Lorenz12045a42015-07-27 17:42:45 +0000676bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
677 lex();
678 if (Token.isNot(MIToken::VirtualRegister))
679 return error("expected a virtual register");
680 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000681 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000682 lex();
683 if (Token.isNot(MIToken::Eof))
684 return error("expected end of string after the register reference");
685 return false;
686}
687
Alex Lorenza314d812015-08-18 22:26:26 +0000688bool MIParser::parseStandaloneStackObject(int &FI) {
689 lex();
690 if (Token.isNot(MIToken::StackObject))
691 return error("expected a stack object");
692 if (parseStackFrameIndex(FI))
693 return true;
694 if (Token.isNot(MIToken::Eof))
695 return error("expected end of string after the stack object reference");
696 return false;
697}
698
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000699bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
700 lex();
701 if (Token.isNot(MIToken::exclaim))
702 return error("expected a metadata node");
703 if (parseMDNode(Node))
704 return true;
705 if (Token.isNot(MIToken::Eof))
706 return error("expected end of string after the metadata node");
707 return false;
708}
709
Alex Lorenz36962cd2015-07-07 02:08:46 +0000710static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
711 assert(MO.isImplicit());
712 return MO.isDef() ? "implicit-def" : "implicit";
713}
714
715static std::string getRegisterName(const TargetRegisterInfo *TRI,
716 unsigned Reg) {
717 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
718 return StringRef(TRI->getName(Reg)).lower();
719}
720
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000721bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
722 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000723 if (MCID.isCall())
724 // We can't verify call instructions as they can contain arbitrary implicit
725 // register and register mask operands.
726 return false;
727
728 // Gather all the expected implicit operands.
729 SmallVector<MachineOperand, 4> ImplicitOperands;
730 if (MCID.ImplicitDefs)
731 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
732 ImplicitOperands.push_back(
733 MachineOperand::CreateReg(*ImpDefs, true, true));
734 if (MCID.ImplicitUses)
735 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
736 ImplicitOperands.push_back(
737 MachineOperand::CreateReg(*ImpUses, false, true));
738
739 const auto *TRI = MF.getSubtarget().getRegisterInfo();
740 assert(TRI && "Expected target register info");
741 size_t I = ImplicitOperands.size(), J = Operands.size();
742 while (I) {
743 --I;
744 if (J) {
745 --J;
746 const auto &ImplicitOperand = ImplicitOperands[I];
747 const auto &Operand = Operands[J].Operand;
748 if (ImplicitOperand.isIdenticalTo(Operand))
749 continue;
750 if (Operand.isReg() && Operand.isImplicit()) {
Alex Lorenzeb7c9be2015-08-18 17:17:13 +0000751 // Check if this implicit register is a subregister of an explicit
752 // register operand.
753 bool IsImplicitSubRegister = false;
754 for (size_t K = 0, E = Operands.size(); K < E; ++K) {
755 const auto &Op = Operands[K].Operand;
756 if (Op.isReg() && !Op.isImplicit() &&
757 TRI->isSubRegister(Op.getReg(), Operand.getReg())) {
758 IsImplicitSubRegister = true;
759 break;
760 }
761 }
762 if (IsImplicitSubRegister)
763 continue;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000764 return error(Operands[J].Begin,
765 Twine("expected an implicit register operand '") +
766 printImplicitRegisterFlag(ImplicitOperand) + " %" +
767 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
768 }
769 }
770 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
771 // insead of reporting an error at this location:
772 // %eax = MOV32r0
773 // ^
774 // report the error at the following location:
775 // %eax = MOV32r0
776 // ^
777 return error(J < Operands.size() ? Operands[J].End : Token.location(),
778 Twine("missing implicit register operand '") +
779 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
780 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
781 }
782 return false;
783}
784
Alex Lorenze5a44662015-07-17 00:24:15 +0000785bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
786 if (Token.is(MIToken::kw_frame_setup)) {
787 Flags |= MachineInstr::FrameSetup;
788 lex();
789 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000790 if (Token.isNot(MIToken::Identifier))
791 return error("expected a machine instruction");
792 StringRef InstrName = Token.stringValue();
793 if (parseInstrName(InstrName, OpCode))
794 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000795 lex();
796 return false;
797}
798
799bool MIParser::parseRegister(unsigned &Reg) {
800 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000801 case MIToken::underscore:
802 Reg = 0;
803 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000804 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000805 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000806 if (getRegisterByName(Name, Reg))
807 return error(Twine("unknown register name '") + Name + "'");
808 break;
809 }
Alex Lorenz53464512015-07-10 22:51:20 +0000810 case MIToken::VirtualRegister: {
811 unsigned ID;
812 if (getUnsigned(ID))
813 return true;
814 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
815 if (RegInfo == PFS.VirtualRegisterSlots.end())
816 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
817 "'");
818 Reg = RegInfo->second;
819 break;
820 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000821 // TODO: Parse other register kinds.
822 default:
823 llvm_unreachable("The current token should be a register");
824 }
825 return false;
826}
827
Alex Lorenzcb268d42015-07-06 23:07:26 +0000828bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000829 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000830 switch (Token.kind()) {
831 case MIToken::kw_implicit:
832 Flags |= RegState::Implicit;
833 break;
834 case MIToken::kw_implicit_define:
835 Flags |= RegState::ImplicitDefine;
836 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000837 case MIToken::kw_def:
838 Flags |= RegState::Define;
839 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000840 case MIToken::kw_dead:
841 Flags |= RegState::Dead;
842 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000843 case MIToken::kw_killed:
844 Flags |= RegState::Kill;
845 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000846 case MIToken::kw_undef:
847 Flags |= RegState::Undef;
848 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000849 case MIToken::kw_internal:
850 Flags |= RegState::InternalRead;
851 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000852 case MIToken::kw_early_clobber:
853 Flags |= RegState::EarlyClobber;
854 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000855 case MIToken::kw_debug_use:
856 Flags |= RegState::Debug;
857 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000858 default:
859 llvm_unreachable("The current token should be a register flag");
860 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000861 if (OldFlags == Flags)
862 // We know that the same flag is specified more than once when the flags
863 // weren't modified.
864 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000865 lex();
866 return false;
867}
868
Alex Lorenz2eacca82015-07-13 23:24:34 +0000869bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
870 assert(Token.is(MIToken::colon));
871 lex();
872 if (Token.isNot(MIToken::Identifier))
873 return error("expected a subregister index after ':'");
874 auto Name = Token.stringValue();
875 SubReg = getSubRegIndex(Name);
876 if (!SubReg)
877 return error(Twine("use of unknown subregister index '") + Name + "'");
878 lex();
879 return false;
880}
881
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000882bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
883 if (!consumeIfPresent(MIToken::kw_tied_def))
884 return error("expected 'tied-def' after '('");
885 if (Token.isNot(MIToken::IntegerLiteral))
886 return error("expected an integer literal after 'tied-def'");
887 if (getUnsigned(TiedDefIdx))
888 return true;
889 lex();
890 if (expectAndConsume(MIToken::rparen))
891 return true;
892 return false;
893}
894
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000895bool MIParser::assignRegisterTies(MachineInstr &MI,
896 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000897 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
898 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
899 if (!Operands[I].TiedDefIdx)
900 continue;
901 // The parser ensures that this operand is a register use, so we just have
902 // to check the tied-def operand.
903 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
904 if (DefIdx >= E)
905 return error(Operands[I].Begin,
906 Twine("use of invalid tied-def operand index '" +
907 Twine(DefIdx) + "'; instruction has only ") +
908 Twine(E) + " operands");
909 const auto &DefOperand = Operands[DefIdx].Operand;
910 if (!DefOperand.isReg() || !DefOperand.isDef())
911 // FIXME: add note with the def operand.
912 return error(Operands[I].Begin,
913 Twine("use of invalid tied-def operand index '") +
914 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
915 " isn't a defined register");
916 // Check that the tied-def operand wasn't tied elsewhere.
917 for (const auto &TiedPair : TiedRegisterPairs) {
918 if (TiedPair.first == DefIdx)
919 return error(Operands[I].Begin,
920 Twine("the tied-def operand #") + Twine(DefIdx) +
921 " is already tied with another register operand");
922 }
923 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
924 }
925 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
926 // indices must be less than tied max.
927 for (const auto &TiedPair : TiedRegisterPairs)
928 MI.tieOperands(TiedPair.first, TiedPair.second);
929 return false;
930}
931
932bool MIParser::parseRegisterOperand(MachineOperand &Dest,
933 Optional<unsigned> &TiedDefIdx,
934 bool IsDef) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000935 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000936 unsigned Flags = IsDef ? RegState::Define : 0;
937 while (Token.isRegisterFlag()) {
938 if (parseRegisterFlag(Flags))
939 return true;
940 }
941 if (!Token.isRegister())
942 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000943 if (parseRegister(Reg))
944 return true;
945 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000946 unsigned SubReg = 0;
947 if (Token.is(MIToken::colon)) {
948 if (parseSubRegisterIndex(SubReg))
949 return true;
950 }
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000951 if ((Flags & RegState::Define) == 0 && consumeIfPresent(MIToken::lparen)) {
952 unsigned Idx;
953 if (parseRegisterTiedDefIndex(Idx))
954 return true;
955 TiedDefIdx = Idx;
956 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000957 Dest = MachineOperand::CreateReg(
958 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000959 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +0000960 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
961 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000962 return false;
963}
964
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000965bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
966 assert(Token.is(MIToken::IntegerLiteral));
967 const APSInt &Int = Token.integerValue();
968 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +0000969 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000970 Dest = MachineOperand::CreateImm(Int.getExtValue());
971 lex();
972 return false;
973}
974
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000975bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
Alex Lorenz3fb77682015-08-06 23:17:42 +0000976 auto Source = StringRef(Loc, Token.range().end() - Loc).str();
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000977 lex();
978 SMDiagnostic Err;
979 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
980 if (!C)
981 return error(Loc + Err.getColumnNo(), Err.getMessage());
982 return false;
983}
984
Alex Lorenz05e38822015-08-05 18:52:21 +0000985bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
986 assert(Token.is(MIToken::IntegerType));
987 auto Loc = Token.location();
988 lex();
989 if (Token.isNot(MIToken::IntegerLiteral))
990 return error("expected an integer literal");
991 const Constant *C = nullptr;
992 if (parseIRConstant(Loc, C))
993 return true;
994 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
995 return false;
996}
997
Alex Lorenzad156fb2015-07-31 20:49:21 +0000998bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
999 auto Loc = Token.location();
1000 lex();
1001 if (Token.isNot(MIToken::FloatingPointLiteral))
1002 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001003 const Constant *C = nullptr;
1004 if (parseIRConstant(Loc, C))
1005 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001006 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1007 return false;
1008}
1009
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001010bool MIParser::getUnsigned(unsigned &Result) {
1011 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1012 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1013 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1014 if (Val64 == Limit)
1015 return error("expected 32-bit integer (too large)");
1016 Result = Val64;
1017 return false;
1018}
1019
Alex Lorenzf09df002015-06-30 18:16:42 +00001020bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001021 assert(Token.is(MIToken::MachineBasicBlock) ||
1022 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001023 unsigned Number;
1024 if (getUnsigned(Number))
1025 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001026 auto MBBInfo = PFS.MBBSlots.find(Number);
1027 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001028 return error(Twine("use of undefined machine basic block #") +
1029 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001030 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001031 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1032 return error(Twine("the name of machine basic block #") + Twine(Number) +
1033 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001034 return false;
1035}
1036
1037bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1038 MachineBasicBlock *MBB;
1039 if (parseMBBReference(MBB))
1040 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001041 Dest = MachineOperand::CreateMBB(MBB);
1042 lex();
1043 return false;
1044}
1045
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001046bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001047 assert(Token.is(MIToken::StackObject));
1048 unsigned ID;
1049 if (getUnsigned(ID))
1050 return true;
1051 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1052 if (ObjectInfo == PFS.StackObjectSlots.end())
1053 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1054 "'");
1055 StringRef Name;
1056 if (const auto *Alloca =
1057 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
1058 Name = Alloca->getName();
1059 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1060 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1061 "' isn't '" + Token.stringValue() + "'");
1062 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001063 FI = ObjectInfo->second;
1064 return false;
1065}
1066
1067bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1068 int FI;
1069 if (parseStackFrameIndex(FI))
1070 return true;
1071 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001072 return false;
1073}
1074
Alex Lorenzea882122015-08-12 21:17:02 +00001075bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001076 assert(Token.is(MIToken::FixedStackObject));
1077 unsigned ID;
1078 if (getUnsigned(ID))
1079 return true;
1080 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1081 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1082 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1083 Twine(ID) + "'");
1084 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001085 FI = ObjectInfo->second;
1086 return false;
1087}
1088
1089bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1090 int FI;
1091 if (parseFixedStackFrameIndex(FI))
1092 return true;
1093 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001094 return false;
1095}
1096
Alex Lorenz41df7d32015-07-28 17:09:52 +00001097bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001098 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001099 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001100 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001101 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001102 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001103 return error(Twine("use of undefined global value '") + Token.range() +
1104 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001105 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001106 }
1107 case MIToken::GlobalValue: {
1108 unsigned GVIdx;
1109 if (getUnsigned(GVIdx))
1110 return true;
1111 if (GVIdx >= IRSlots.GlobalValues.size())
1112 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1113 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001114 GV = IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001115 break;
1116 }
1117 default:
1118 llvm_unreachable("The current token should be a global value");
1119 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001120 return false;
1121}
1122
1123bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1124 GlobalValue *GV = nullptr;
1125 if (parseGlobalValue(GV))
1126 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001127 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001128 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001129 if (parseOperandsOffset(Dest))
1130 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001131 return false;
1132}
1133
Alex Lorenzab980492015-07-20 20:51:18 +00001134bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1135 assert(Token.is(MIToken::ConstantPoolItem));
1136 unsigned ID;
1137 if (getUnsigned(ID))
1138 return true;
1139 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1140 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1141 return error("use of undefined constant '%const." + Twine(ID) + "'");
1142 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001143 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001144 if (parseOperandsOffset(Dest))
1145 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001146 return false;
1147}
1148
Alex Lorenz31d70682015-07-15 23:38:35 +00001149bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1150 assert(Token.is(MIToken::JumpTableIndex));
1151 unsigned ID;
1152 if (getUnsigned(ID))
1153 return true;
1154 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1155 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1156 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1157 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001158 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1159 return false;
1160}
1161
Alex Lorenz6ede3742015-07-21 16:59:53 +00001162bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001163 assert(Token.is(MIToken::ExternalSymbol));
1164 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001165 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001166 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001167 if (parseOperandsOffset(Dest))
1168 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001169 return false;
1170}
1171
Alex Lorenz44f29252015-07-22 21:07:04 +00001172bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001173 assert(Token.is(MIToken::exclaim));
1174 auto Loc = Token.location();
1175 lex();
1176 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1177 return error("expected metadata id after '!'");
1178 unsigned ID;
1179 if (getUnsigned(ID))
1180 return true;
1181 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
1182 if (NodeInfo == IRSlots.MetadataNodes.end())
1183 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1184 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001185 Node = NodeInfo->second.get();
1186 return false;
1187}
1188
1189bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1190 MDNode *Node = nullptr;
1191 if (parseMDNode(Node))
1192 return true;
1193 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001194 return false;
1195}
1196
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001197bool MIParser::parseCFIOffset(int &Offset) {
1198 if (Token.isNot(MIToken::IntegerLiteral))
1199 return error("expected a cfi offset");
1200 if (Token.integerValue().getMinSignedBits() > 32)
1201 return error("expected a 32 bit integer (the cfi offset is too large)");
1202 Offset = (int)Token.integerValue().getExtValue();
1203 lex();
1204 return false;
1205}
1206
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001207bool MIParser::parseCFIRegister(unsigned &Reg) {
1208 if (Token.isNot(MIToken::NamedRegister))
1209 return error("expected a cfi register");
1210 unsigned LLVMReg;
1211 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001212 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001213 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1214 assert(TRI && "Expected target register info");
1215 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1216 if (DwarfReg < 0)
1217 return error("invalid DWARF register");
1218 Reg = (unsigned)DwarfReg;
1219 lex();
1220 return false;
1221}
1222
1223bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1224 auto Kind = Token.kind();
1225 lex();
1226 auto &MMI = MF.getMMI();
1227 int Offset;
1228 unsigned Reg;
1229 unsigned CFIIndex;
1230 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001231 case MIToken::kw_cfi_same_value:
1232 if (parseCFIRegister(Reg))
1233 return true;
1234 CFIIndex =
1235 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1236 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001237 case MIToken::kw_cfi_offset:
1238 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1239 parseCFIOffset(Offset))
1240 return true;
1241 CFIIndex =
1242 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1243 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001244 case MIToken::kw_cfi_def_cfa_register:
1245 if (parseCFIRegister(Reg))
1246 return true;
1247 CFIIndex =
1248 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1249 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001250 case MIToken::kw_cfi_def_cfa_offset:
1251 if (parseCFIOffset(Offset))
1252 return true;
1253 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1254 CFIIndex = MMI.addFrameInst(
1255 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1256 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001257 case MIToken::kw_cfi_def_cfa:
1258 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1259 parseCFIOffset(Offset))
1260 return true;
1261 // NB: MCCFIInstruction::createDefCfa negates the offset.
1262 CFIIndex =
1263 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1264 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001265 default:
1266 // TODO: Parse the other CFI operands.
1267 llvm_unreachable("The current token should be a cfi operand");
1268 }
1269 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001270 return false;
1271}
1272
Alex Lorenzdeb53492015-07-28 17:28:03 +00001273bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1274 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001275 case MIToken::NamedIRBlock: {
1276 BB = dyn_cast_or_null<BasicBlock>(
1277 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001278 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001279 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001280 break;
1281 }
1282 case MIToken::IRBlock: {
1283 unsigned SlotNumber = 0;
1284 if (getUnsigned(SlotNumber))
1285 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001286 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001287 if (!BB)
1288 return error(Twine("use of undefined IR block '%ir-block.") +
1289 Twine(SlotNumber) + "'");
1290 break;
1291 }
1292 default:
1293 llvm_unreachable("The current token should be an IR block reference");
1294 }
1295 return false;
1296}
1297
1298bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1299 assert(Token.is(MIToken::kw_blockaddress));
1300 lex();
1301 if (expectAndConsume(MIToken::lparen))
1302 return true;
1303 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001304 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001305 return error("expected a global value");
1306 GlobalValue *GV = nullptr;
1307 if (parseGlobalValue(GV))
1308 return true;
1309 auto *F = dyn_cast<Function>(GV);
1310 if (!F)
1311 return error("expected an IR function reference");
1312 lex();
1313 if (expectAndConsume(MIToken::comma))
1314 return true;
1315 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001316 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001317 return error("expected an IR block reference");
1318 if (parseIRBlock(BB, *F))
1319 return true;
1320 lex();
1321 if (expectAndConsume(MIToken::rparen))
1322 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001323 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001324 if (parseOperandsOffset(Dest))
1325 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001326 return false;
1327}
1328
Alex Lorenzef5c1962015-07-28 23:02:45 +00001329bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1330 assert(Token.is(MIToken::kw_target_index));
1331 lex();
1332 if (expectAndConsume(MIToken::lparen))
1333 return true;
1334 if (Token.isNot(MIToken::Identifier))
1335 return error("expected the name of the target index");
1336 int Index = 0;
1337 if (getTargetIndex(Token.stringValue(), Index))
1338 return error("use of undefined target index '" + Token.stringValue() + "'");
1339 lex();
1340 if (expectAndConsume(MIToken::rparen))
1341 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001342 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001343 if (parseOperandsOffset(Dest))
1344 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001345 return false;
1346}
1347
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001348bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1349 assert(Token.is(MIToken::kw_liveout));
1350 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1351 assert(TRI && "Expected target register info");
1352 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1353 lex();
1354 if (expectAndConsume(MIToken::lparen))
1355 return true;
1356 while (true) {
1357 if (Token.isNot(MIToken::NamedRegister))
1358 return error("expected a named register");
1359 unsigned Reg = 0;
1360 if (parseRegister(Reg))
1361 return true;
1362 lex();
1363 Mask[Reg / 32] |= 1U << (Reg % 32);
1364 // TODO: Report an error if the same register is used more than once.
1365 if (Token.isNot(MIToken::comma))
1366 break;
1367 lex();
1368 }
1369 if (expectAndConsume(MIToken::rparen))
1370 return true;
1371 Dest = MachineOperand::CreateRegLiveOut(Mask);
1372 return false;
1373}
1374
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001375bool MIParser::parseMachineOperand(MachineOperand &Dest,
1376 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001377 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001378 case MIToken::kw_implicit:
1379 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001380 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001381 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001382 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001383 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001384 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001385 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001386 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001387 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001388 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001389 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001390 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001391 case MIToken::IntegerLiteral:
1392 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001393 case MIToken::IntegerType:
1394 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001395 case MIToken::kw_half:
1396 case MIToken::kw_float:
1397 case MIToken::kw_double:
1398 case MIToken::kw_x86_fp80:
1399 case MIToken::kw_fp128:
1400 case MIToken::kw_ppc_fp128:
1401 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001402 case MIToken::MachineBasicBlock:
1403 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001404 case MIToken::StackObject:
1405 return parseStackObjectOperand(Dest);
1406 case MIToken::FixedStackObject:
1407 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001408 case MIToken::GlobalValue:
1409 case MIToken::NamedGlobalValue:
1410 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001411 case MIToken::ConstantPoolItem:
1412 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001413 case MIToken::JumpTableIndex:
1414 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001415 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001416 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001417 case MIToken::exclaim:
1418 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001419 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001420 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001421 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001422 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001423 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001424 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001425 case MIToken::kw_blockaddress:
1426 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001427 case MIToken::kw_target_index:
1428 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001429 case MIToken::kw_liveout:
1430 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001431 case MIToken::Error:
1432 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001433 case MIToken::Identifier:
1434 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1435 Dest = MachineOperand::CreateRegMask(RegMask);
1436 lex();
1437 break;
1438 }
1439 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001440 default:
1441 // TODO: parse the other machine operands.
1442 return error("expected a machine operand");
1443 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001444 return false;
1445}
1446
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001447bool MIParser::parseMachineOperandAndTargetFlags(
1448 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001449 unsigned TF = 0;
1450 bool HasTargetFlags = false;
1451 if (Token.is(MIToken::kw_target_flags)) {
1452 HasTargetFlags = true;
1453 lex();
1454 if (expectAndConsume(MIToken::lparen))
1455 return true;
1456 if (Token.isNot(MIToken::Identifier))
1457 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001458 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1459 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1460 return error("use of undefined target flag '" + Token.stringValue() +
1461 "'");
1462 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001463 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001464 while (Token.is(MIToken::comma)) {
1465 lex();
1466 if (Token.isNot(MIToken::Identifier))
1467 return error("expected the name of the target flag");
1468 unsigned BitFlag = 0;
1469 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1470 return error("use of undefined target flag '" + Token.stringValue() +
1471 "'");
1472 // TODO: Report an error when using a duplicate bit target flag.
1473 TF |= BitFlag;
1474 lex();
1475 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001476 if (expectAndConsume(MIToken::rparen))
1477 return true;
1478 }
1479 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001480 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001481 return true;
1482 if (!HasTargetFlags)
1483 return false;
1484 if (Dest.isReg())
1485 return error(Loc, "register operands can't have target flags");
1486 Dest.setTargetFlags(TF);
1487 return false;
1488}
1489
Alex Lorenzdc24c172015-08-07 20:21:00 +00001490bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001491 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1492 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001493 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001494 bool IsNegative = Token.is(MIToken::minus);
1495 lex();
1496 if (Token.isNot(MIToken::IntegerLiteral))
1497 return error("expected an integer literal after '" + Sign + "'");
1498 if (Token.integerValue().getMinSignedBits() > 64)
1499 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001500 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001501 if (IsNegative)
1502 Offset = -Offset;
1503 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001504 return false;
1505}
1506
Alex Lorenz620f8912015-08-13 20:33:33 +00001507bool MIParser::parseAlignment(unsigned &Alignment) {
1508 assert(Token.is(MIToken::kw_align));
1509 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001510 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001511 return error("expected an integer literal after 'align'");
1512 if (getUnsigned(Alignment))
1513 return true;
1514 lex();
1515 return false;
1516}
1517
Alex Lorenzdc24c172015-08-07 20:21:00 +00001518bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1519 int64_t Offset = 0;
1520 if (parseOffset(Offset))
1521 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001522 Op.setOffset(Offset);
1523 return false;
1524}
1525
Alex Lorenz4af7e612015-08-03 23:08:19 +00001526bool MIParser::parseIRValue(Value *&V) {
1527 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001528 case MIToken::NamedIRValue: {
1529 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001530 if (!V)
Alex Lorenz2791dcc2015-08-12 21:27:16 +00001531 V = MF.getFunction()->getParent()->getValueSymbolTable().lookup(
1532 Token.stringValue());
1533 if (!V)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001534 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001535 break;
1536 }
1537 // TODO: Parse unnamed IR value references.
1538 default:
1539 llvm_unreachable("The current token should be an IR block reference");
1540 }
1541 return false;
1542}
1543
1544bool MIParser::getUint64(uint64_t &Result) {
1545 assert(Token.hasIntegerValue());
1546 if (Token.integerValue().getActiveBits() > 64)
1547 return error("expected 64-bit integer (too large)");
1548 Result = Token.integerValue().getZExtValue();
1549 return false;
1550}
1551
Alex Lorenza518b792015-08-04 00:24:45 +00001552bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
Alex Lorenze86d5152015-08-06 18:26:36 +00001553 const unsigned OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001554 switch (Token.kind()) {
1555 case MIToken::kw_volatile:
1556 Flags |= MachineMemOperand::MOVolatile;
1557 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001558 case MIToken::kw_non_temporal:
1559 Flags |= MachineMemOperand::MONonTemporal;
1560 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001561 case MIToken::kw_invariant:
1562 Flags |= MachineMemOperand::MOInvariant;
1563 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001564 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001565 default:
1566 llvm_unreachable("The current token should be a memory operand flag");
1567 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001568 if (OldFlags == Flags)
1569 // We know that the same flag is specified more than once when the flags
1570 // weren't modified.
1571 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001572 lex();
1573 return false;
1574}
1575
Alex Lorenz91097a32015-08-12 20:33:26 +00001576bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1577 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001578 case MIToken::kw_stack:
1579 PSV = MF.getPSVManager().getStack();
1580 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001581 case MIToken::kw_got:
1582 PSV = MF.getPSVManager().getGOT();
1583 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001584 case MIToken::kw_jump_table:
1585 PSV = MF.getPSVManager().getJumpTable();
1586 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001587 case MIToken::kw_constant_pool:
1588 PSV = MF.getPSVManager().getConstantPool();
1589 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001590 case MIToken::FixedStackObject: {
1591 int FI;
1592 if (parseFixedStackFrameIndex(FI))
1593 return true;
1594 PSV = MF.getPSVManager().getFixedStack(FI);
1595 // The token was already consumed, so use return here instead of break.
1596 return false;
1597 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001598 case MIToken::GlobalValue:
1599 case MIToken::NamedGlobalValue: {
1600 GlobalValue *GV = nullptr;
1601 if (parseGlobalValue(GV))
1602 return true;
1603 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1604 break;
1605 }
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001606 case MIToken::ExternalSymbol:
1607 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1608 MF.createExternalSymbolName(Token.stringValue()));
1609 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001610 default:
1611 llvm_unreachable("The current token should be pseudo source value");
1612 }
1613 lex();
1614 return false;
1615}
1616
1617bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001618 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001619 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Alex Lorenz50b826f2015-08-14 21:08:30 +00001620 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::GlobalValue) ||
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001621 Token.is(MIToken::NamedGlobalValue) ||
1622 Token.is(MIToken::ExternalSymbol)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001623 const PseudoSourceValue *PSV = nullptr;
1624 if (parseMemoryPseudoSourceValue(PSV))
1625 return true;
1626 int64_t Offset = 0;
1627 if (parseOffset(Offset))
1628 return true;
1629 Dest = MachinePointerInfo(PSV, Offset);
1630 return false;
1631 }
1632 if (Token.isNot(MIToken::NamedIRValue))
1633 return error("expected an IR value reference");
1634 Value *V = nullptr;
1635 if (parseIRValue(V))
1636 return true;
1637 if (!V->getType()->isPointerTy())
1638 return error("expected a pointer IR value");
1639 lex();
1640 int64_t Offset = 0;
1641 if (parseOffset(Offset))
1642 return true;
1643 Dest = MachinePointerInfo(V, Offset);
1644 return false;
1645}
1646
Alex Lorenz4af7e612015-08-03 23:08:19 +00001647bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1648 if (expectAndConsume(MIToken::lparen))
1649 return true;
Alex Lorenza518b792015-08-04 00:24:45 +00001650 unsigned Flags = 0;
1651 while (Token.isMemoryOperandFlag()) {
1652 if (parseMemoryOperandFlag(Flags))
1653 return true;
1654 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001655 if (Token.isNot(MIToken::Identifier) ||
1656 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1657 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001658 if (Token.stringValue() == "load")
1659 Flags |= MachineMemOperand::MOLoad;
1660 else
1661 Flags |= MachineMemOperand::MOStore;
1662 lex();
1663
1664 if (Token.isNot(MIToken::IntegerLiteral))
1665 return error("expected the size integer literal after memory operation");
1666 uint64_t Size;
1667 if (getUint64(Size))
1668 return true;
1669 lex();
1670
1671 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1672 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1673 return error(Twine("expected '") + Word + "'");
1674 lex();
1675
Alex Lorenz91097a32015-08-12 20:33:26 +00001676 MachinePointerInfo Ptr = MachinePointerInfo();
1677 if (parseMachinePointerInfo(Ptr))
Alex Lorenz83127732015-08-07 20:26:52 +00001678 return true;
Alex Lorenz61420f72015-08-07 20:48:30 +00001679 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001680 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001681 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001682 while (consumeIfPresent(MIToken::comma)) {
1683 switch (Token.kind()) {
1684 case MIToken::kw_align:
1685 if (parseAlignment(BaseAlignment))
1686 return true;
1687 break;
1688 case MIToken::md_tbaa:
1689 lex();
1690 if (parseMDNode(AAInfo.TBAA))
1691 return true;
1692 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001693 case MIToken::md_alias_scope:
1694 lex();
1695 if (parseMDNode(AAInfo.Scope))
1696 return true;
1697 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001698 case MIToken::md_noalias:
1699 lex();
1700 if (parseMDNode(AAInfo.NoAlias))
1701 return true;
1702 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001703 case MIToken::md_range:
1704 lex();
1705 if (parseMDNode(Range))
1706 return true;
1707 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001708 // TODO: Report an error on duplicate metadata nodes.
1709 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001710 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1711 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001712 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001713 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001714 if (expectAndConsume(MIToken::rparen))
1715 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001716 Dest =
1717 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001718 return false;
1719}
1720
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001721void MIParser::initNames2InstrOpCodes() {
1722 if (!Names2InstrOpCodes.empty())
1723 return;
1724 const auto *TII = MF.getSubtarget().getInstrInfo();
1725 assert(TII && "Expected target instruction info");
1726 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1727 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1728}
1729
1730bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1731 initNames2InstrOpCodes();
1732 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1733 if (InstrInfo == Names2InstrOpCodes.end())
1734 return true;
1735 OpCode = InstrInfo->getValue();
1736 return false;
1737}
1738
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001739void MIParser::initNames2Regs() {
1740 if (!Names2Regs.empty())
1741 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001742 // The '%noreg' register is the register 0.
1743 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001744 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1745 assert(TRI && "Expected target register info");
1746 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1747 bool WasInserted =
1748 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1749 .second;
1750 (void)WasInserted;
1751 assert(WasInserted && "Expected registers to be unique case-insensitively");
1752 }
1753}
1754
1755bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1756 initNames2Regs();
1757 auto RegInfo = Names2Regs.find(RegName);
1758 if (RegInfo == Names2Regs.end())
1759 return true;
1760 Reg = RegInfo->getValue();
1761 return false;
1762}
1763
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001764void MIParser::initNames2RegMasks() {
1765 if (!Names2RegMasks.empty())
1766 return;
1767 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1768 assert(TRI && "Expected target register info");
1769 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1770 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1771 assert(RegMasks.size() == RegMaskNames.size());
1772 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1773 Names2RegMasks.insert(
1774 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1775}
1776
1777const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1778 initNames2RegMasks();
1779 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1780 if (RegMaskInfo == Names2RegMasks.end())
1781 return nullptr;
1782 return RegMaskInfo->getValue();
1783}
1784
Alex Lorenz2eacca82015-07-13 23:24:34 +00001785void MIParser::initNames2SubRegIndices() {
1786 if (!Names2SubRegIndices.empty())
1787 return;
1788 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1789 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1790 Names2SubRegIndices.insert(
1791 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1792}
1793
1794unsigned MIParser::getSubRegIndex(StringRef Name) {
1795 initNames2SubRegIndices();
1796 auto SubRegInfo = Names2SubRegIndices.find(Name);
1797 if (SubRegInfo == Names2SubRegIndices.end())
1798 return 0;
1799 return SubRegInfo->getValue();
1800}
1801
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001802static void initSlots2BasicBlocks(
1803 const Function &F,
1804 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1805 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001806 MST.incorporateFunction(F);
1807 for (auto &BB : F) {
1808 if (BB.hasName())
1809 continue;
1810 int Slot = MST.getLocalSlot(&BB);
1811 if (Slot == -1)
1812 continue;
1813 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1814 }
1815}
1816
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001817static const BasicBlock *getIRBlockFromSlot(
1818 unsigned Slot,
1819 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001820 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1821 if (BlockInfo == Slots2BasicBlocks.end())
1822 return nullptr;
1823 return BlockInfo->second;
1824}
1825
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001826const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1827 if (Slots2BasicBlocks.empty())
1828 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1829 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1830}
1831
1832const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1833 if (&F == MF.getFunction())
1834 return getIRBlock(Slot);
1835 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1836 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1837 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1838}
1839
Alex Lorenzef5c1962015-07-28 23:02:45 +00001840void MIParser::initNames2TargetIndices() {
1841 if (!Names2TargetIndices.empty())
1842 return;
1843 const auto *TII = MF.getSubtarget().getInstrInfo();
1844 assert(TII && "Expected target instruction info");
1845 auto Indices = TII->getSerializableTargetIndices();
1846 for (const auto &I : Indices)
1847 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1848}
1849
1850bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1851 initNames2TargetIndices();
1852 auto IndexInfo = Names2TargetIndices.find(Name);
1853 if (IndexInfo == Names2TargetIndices.end())
1854 return true;
1855 Index = IndexInfo->second;
1856 return false;
1857}
1858
Alex Lorenz49873a82015-08-06 00:44:07 +00001859void MIParser::initNames2DirectTargetFlags() {
1860 if (!Names2DirectTargetFlags.empty())
1861 return;
1862 const auto *TII = MF.getSubtarget().getInstrInfo();
1863 assert(TII && "Expected target instruction info");
1864 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1865 for (const auto &I : Flags)
1866 Names2DirectTargetFlags.insert(
1867 std::make_pair(StringRef(I.second), I.first));
1868}
1869
1870bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1871 initNames2DirectTargetFlags();
1872 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1873 if (FlagInfo == Names2DirectTargetFlags.end())
1874 return true;
1875 Flag = FlagInfo->second;
1876 return false;
1877}
1878
Alex Lorenzf3630112015-08-18 22:52:15 +00001879void MIParser::initNames2BitmaskTargetFlags() {
1880 if (!Names2BitmaskTargetFlags.empty())
1881 return;
1882 const auto *TII = MF.getSubtarget().getInstrInfo();
1883 assert(TII && "Expected target instruction info");
1884 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
1885 for (const auto &I : Flags)
1886 Names2BitmaskTargetFlags.insert(
1887 std::make_pair(StringRef(I.second), I.first));
1888}
1889
1890bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
1891 initNames2BitmaskTargetFlags();
1892 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
1893 if (FlagInfo == Names2BitmaskTargetFlags.end())
1894 return true;
1895 Flag = FlagInfo->second;
1896 return false;
1897}
1898
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001899bool llvm::parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src,
1900 PerFunctionMIParsingState &PFS,
1901 const SlotMapping &IRSlots,
1902 SMDiagnostic &Error) {
1903 SourceMgr SM;
1904 SM.AddNewSourceBuffer(
1905 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1906 SMLoc());
1907 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1908 .parseBasicBlockDefinitions(PFS.MBBSlots);
1909}
1910
1911bool llvm::parseMachineInstructions(MachineFunction &MF, StringRef Src,
1912 const PerFunctionMIParsingState &PFS,
1913 const SlotMapping &IRSlots,
1914 SMDiagnostic &Error) {
1915 SourceMgr SM;
1916 SM.AddNewSourceBuffer(
1917 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1918 SMLoc());
1919 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001920}
Alex Lorenzf09df002015-06-30 18:16:42 +00001921
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001922bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1923 MachineFunction &MF, StringRef Src,
1924 const PerFunctionMIParsingState &PFS,
1925 const SlotMapping &IRSlots, SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001926 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00001927}
Alex Lorenz9fab3702015-07-14 21:24:41 +00001928
1929bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1930 MachineFunction &MF, StringRef Src,
1931 const PerFunctionMIParsingState &PFS,
1932 const SlotMapping &IRSlots,
1933 SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001934 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1935 .parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00001936}
Alex Lorenz12045a42015-07-27 17:42:45 +00001937
1938bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1939 MachineFunction &MF, StringRef Src,
1940 const PerFunctionMIParsingState &PFS,
1941 const SlotMapping &IRSlots,
1942 SMDiagnostic &Error) {
1943 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1944 .parseStandaloneVirtualRegister(Reg);
1945}
Alex Lorenza314d812015-08-18 22:26:26 +00001946
1947bool llvm::parseStackObjectReference(int &FI, SourceMgr &SM,
1948 MachineFunction &MF, StringRef Src,
1949 const PerFunctionMIParsingState &PFS,
1950 const SlotMapping &IRSlots,
1951 SMDiagnostic &Error) {
1952 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1953 .parseStandaloneStackObject(FI);
1954}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00001955
1956bool llvm::parseMDNode(MDNode *&Node, SourceMgr &SM, MachineFunction &MF,
1957 StringRef Src, const PerFunctionMIParsingState &PFS,
1958 const SlotMapping &IRSlots, SMDiagnostic &Error) {
1959 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMDNode(Node);
1960}