blob: 3a0e52491ca27a01f60dcfd18e36698267449c25 [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
41/// range.
42struct MachineOperandWithLocation {
43 MachineOperand Operand;
44 StringRef::iterator Begin;
45 StringRef::iterator End;
46
47 MachineOperandWithLocation(const MachineOperand &Operand,
48 StringRef::iterator Begin, StringRef::iterator End)
49 : Operand(Operand), Begin(Begin), End(End) {}
50};
51
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000052class MIParser {
53 SourceMgr &SM;
54 MachineFunction &MF;
55 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000056 StringRef Source, CurrentSource;
57 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000058 const PerFunctionMIParsingState &PFS;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000059 /// Maps from indices to unnamed global values and metadata nodes.
60 const SlotMapping &IRSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000061 /// Maps from instruction names to op codes.
62 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000063 /// Maps from register names to registers.
64 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000065 /// Maps from register mask names to register masks.
66 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000067 /// Maps from subregister names to subregister indices.
68 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000069 /// Maps from slot numbers to function's unnamed basic blocks.
70 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzef5c1962015-07-28 23:02:45 +000071 /// Maps from target index names to target indices.
72 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +000073 /// Maps from direct target flag names to the direct target flag values.
74 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000075
76public:
77 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000078 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000079 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000080
Alex Lorenz91370c52015-06-22 20:37:46 +000081 void lex();
82
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000083 /// Report an error at the current location with the given message.
84 ///
85 /// This function always return true.
86 bool error(const Twine &Msg);
87
Alex Lorenz91370c52015-06-22 20:37:46 +000088 /// Report an error at the given location with the given message.
89 ///
90 /// This function always return true.
91 bool error(StringRef::iterator Loc, const Twine &Msg);
92
Alex Lorenz5022f6b2015-08-13 23:10:16 +000093 bool
94 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
95 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +000096 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +000097 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
98 bool parseStandaloneNamedRegister(unsigned &Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +000099 bool parseStandaloneVirtualRegister(unsigned &Reg);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000100
101 bool
102 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
103 bool parseBasicBlock(MachineBasicBlock &MBB);
104 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
105 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000106
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000107 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000108 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000109 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000110 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000111 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000112 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Alex Lorenz05e38822015-08-05 18:52:21 +0000113 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000114 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000115 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000116 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000117 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000118 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000119 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000120 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000121 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000122 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000123 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000124 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000125 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000126 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000127 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000128 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000129 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000130 bool parseIRBlock(BasicBlock *&BB, const Function &F);
131 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000132 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000133 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000134 bool parseMachineOperand(MachineOperand &Dest);
Alex Lorenz49873a82015-08-06 00:44:07 +0000135 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000136 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000137 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000138 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000139 bool parseIRValue(Value *&V);
Alex Lorenza518b792015-08-04 00:24:45 +0000140 bool parseMemoryOperandFlag(unsigned &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000141 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
142 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000143 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000144
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000145private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000146 /// Convert the integer literal in the current token into an unsigned integer.
147 ///
148 /// Return true if an error occurred.
149 bool getUnsigned(unsigned &Result);
150
Alex Lorenz4af7e612015-08-03 23:08:19 +0000151 /// Convert the integer literal in the current token into an uint64.
152 ///
153 /// Return true if an error occurred.
154 bool getUint64(uint64_t &Result);
155
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000156 /// If the current token is of the given kind, consume it and return false.
157 /// Otherwise report an error and return true.
158 bool expectAndConsume(MIToken::TokenKind TokenKind);
159
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000160 /// If the current token is of the given kind, consume it and return true.
161 /// Otherwise return false.
162 bool consumeIfPresent(MIToken::TokenKind TokenKind);
163
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000164 void initNames2InstrOpCodes();
165
166 /// Try to convert an instruction name to an opcode. Return true if the
167 /// instruction name is invalid.
168 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000169
Alex Lorenze5a44662015-07-17 00:24:15 +0000170 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000171
Alex Lorenz36962cd2015-07-07 02:08:46 +0000172 bool verifyImplicitOperands(ArrayRef<MachineOperandWithLocation> Operands,
173 const MCInstrDesc &MCID);
174
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000175 void initNames2Regs();
176
177 /// Try to convert a register name to a register number. Return true if the
178 /// register name is invalid.
179 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000180
181 void initNames2RegMasks();
182
183 /// Check if the given identifier is a name of a register mask.
184 ///
185 /// Return null if the identifier isn't a register mask.
186 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000187
188 void initNames2SubRegIndices();
189
190 /// Check if the given identifier is a name of a subregister index.
191 ///
192 /// Return 0 if the name isn't a subregister index class.
193 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000194
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000195 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000196 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000197
198 void initNames2TargetIndices();
199
200 /// Try to convert a name of target index to the corresponding target index.
201 ///
202 /// Return true if the name isn't a name of a target index.
203 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000204
205 void initNames2DirectTargetFlags();
206
207 /// Try to convert a name of a direct target flag to the corresponding
208 /// target flag.
209 ///
210 /// Return true if the name isn't a name of a direct flag.
211 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000212};
213
214} // end anonymous namespace
215
216MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000217 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000218 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000219 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz3fb77682015-08-06 23:17:42 +0000220 PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000221
Alex Lorenz91370c52015-06-22 20:37:46 +0000222void MIParser::lex() {
223 CurrentSource = lexMIToken(
224 CurrentSource, Token,
225 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
226}
227
228bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
229
230bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000231 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000232 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
233 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
234 // Create an ordinary diagnostic when the source manager's buffer is the
235 // source string.
236 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
237 return true;
238 }
239 // Create a diagnostic for a YAML string literal.
240 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
241 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
242 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000243 return true;
244}
245
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000246static const char *toString(MIToken::TokenKind TokenKind) {
247 switch (TokenKind) {
248 case MIToken::comma:
249 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000250 case MIToken::equal:
251 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000252 case MIToken::colon:
253 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000254 case MIToken::lparen:
255 return "'('";
256 case MIToken::rparen:
257 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000258 default:
259 return "<unknown token>";
260 }
261}
262
263bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
264 if (Token.isNot(TokenKind))
265 return error(Twine("expected ") + toString(TokenKind));
266 lex();
267 return false;
268}
269
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000270bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
271 if (Token.isNot(TokenKind))
272 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000273 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000274 return true;
275}
Alex Lorenz91370c52015-06-22 20:37:46 +0000276
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000277bool MIParser::parseBasicBlockDefinition(
278 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
279 assert(Token.is(MIToken::MachineBasicBlockLabel));
280 unsigned ID = 0;
281 if (getUnsigned(ID))
282 return true;
283 auto Loc = Token.location();
284 auto Name = Token.stringValue();
285 lex();
286 bool HasAddressTaken = false;
287 bool IsLandingPad = false;
288 unsigned Alignment = 0;
289 BasicBlock *BB = nullptr;
290 if (consumeIfPresent(MIToken::lparen)) {
291 do {
292 // TODO: Report an error when multiple same attributes are specified.
293 switch (Token.kind()) {
294 case MIToken::kw_address_taken:
295 HasAddressTaken = true;
296 lex();
297 break;
298 case MIToken::kw_landing_pad:
299 IsLandingPad = true;
300 lex();
301 break;
302 case MIToken::kw_align:
303 if (parseAlignment(Alignment))
304 return true;
305 break;
306 case MIToken::IRBlock:
307 // TODO: Report an error when both name and ir block are specified.
308 if (parseIRBlock(BB, *MF.getFunction()))
309 return true;
310 lex();
311 break;
312 default:
313 break;
314 }
315 } while (consumeIfPresent(MIToken::comma));
316 if (expectAndConsume(MIToken::rparen))
317 return true;
318 }
319 if (expectAndConsume(MIToken::colon))
320 return true;
321
322 if (!Name.empty()) {
323 BB = dyn_cast_or_null<BasicBlock>(
324 MF.getFunction()->getValueSymbolTable().lookup(Name));
325 if (!BB)
326 return error(Loc, Twine("basic block '") + Name +
327 "' is not defined in the function '" +
328 MF.getName() + "'");
329 }
330 auto *MBB = MF.CreateMachineBasicBlock(BB);
331 MF.insert(MF.end(), MBB);
332 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
333 if (!WasInserted)
334 return error(Loc, Twine("redefinition of machine basic block with id #") +
335 Twine(ID));
336 if (Alignment)
337 MBB->setAlignment(Alignment);
338 if (HasAddressTaken)
339 MBB->setHasAddressTaken();
340 MBB->setIsLandingPad(IsLandingPad);
341 return false;
342}
343
344bool MIParser::parseBasicBlockDefinitions(
345 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
346 lex();
347 // Skip until the first machine basic block.
348 while (Token.is(MIToken::Newline))
349 lex();
350 if (Token.isErrorOrEOF())
351 return Token.isError();
352 if (Token.isNot(MIToken::MachineBasicBlockLabel))
353 return error("expected a basic block definition before instructions");
354 do {
355 if (parseBasicBlockDefinition(MBBSlots))
356 return true;
357 bool IsAfterNewline = false;
358 // Skip until the next machine basic block.
359 while (true) {
360 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
361 Token.isErrorOrEOF())
362 break;
363 else if (Token.is(MIToken::MachineBasicBlockLabel))
364 return error("basic block definition should be located at the start of "
365 "the line");
366 if (Token.is(MIToken::Newline))
367 IsAfterNewline = true;
368 else
369 IsAfterNewline = false;
370 lex();
371 }
372 } while (!Token.isErrorOrEOF());
373 return Token.isError();
374}
375
376bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
377 assert(Token.is(MIToken::kw_liveins));
378 lex();
379 if (expectAndConsume(MIToken::colon))
380 return true;
381 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
382 return false;
383 do {
384 if (Token.isNot(MIToken::NamedRegister))
385 return error("expected a named register");
386 unsigned Reg = 0;
387 if (parseRegister(Reg))
388 return true;
389 MBB.addLiveIn(Reg);
390 lex();
391 } while (consumeIfPresent(MIToken::comma));
392 return false;
393}
394
395bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
396 assert(Token.is(MIToken::kw_successors));
397 lex();
398 if (expectAndConsume(MIToken::colon))
399 return true;
400 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
401 return false;
402 do {
403 if (Token.isNot(MIToken::MachineBasicBlock))
404 return error("expected a machine basic block reference");
405 MachineBasicBlock *SuccMBB = nullptr;
406 if (parseMBBReference(SuccMBB))
407 return true;
408 lex();
409 unsigned Weight = 0;
410 if (consumeIfPresent(MIToken::lparen)) {
411 if (Token.isNot(MIToken::IntegerLiteral))
412 return error("expected an integer literal after '('");
413 if (getUnsigned(Weight))
414 return true;
415 lex();
416 if (expectAndConsume(MIToken::rparen))
417 return true;
418 }
419 MBB.addSuccessor(SuccMBB, Weight);
420 } while (consumeIfPresent(MIToken::comma));
421 return false;
422}
423
424bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
425 // Skip the definition.
426 assert(Token.is(MIToken::MachineBasicBlockLabel));
427 lex();
428 if (consumeIfPresent(MIToken::lparen)) {
429 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
430 lex();
431 consumeIfPresent(MIToken::rparen);
432 }
433 consumeIfPresent(MIToken::colon);
434
435 // Parse the liveins and successors.
436 // N.B: Multiple lists of successors and liveins are allowed and they're
437 // merged into one.
438 // Example:
439 // liveins: %edi
440 // liveins: %esi
441 //
442 // is equivalent to
443 // liveins: %edi, %esi
444 while (true) {
445 if (Token.is(MIToken::kw_successors)) {
446 if (parseBasicBlockSuccessors(MBB))
447 return true;
448 } else if (Token.is(MIToken::kw_liveins)) {
449 if (parseBasicBlockLiveins(MBB))
450 return true;
451 } else if (consumeIfPresent(MIToken::Newline)) {
452 continue;
453 } else
454 break;
455 if (!Token.isNewlineOrEOF())
456 return error("expected line break at the end of a list");
457 lex();
458 }
459
460 // Parse the instructions.
461 while (true) {
462 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
463 return false;
464 else if (consumeIfPresent(MIToken::Newline))
465 continue;
466 MachineInstr *MI = nullptr;
467 if (parse(MI))
468 return true;
469 MBB.insert(MBB.end(), MI);
470 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
471 lex();
472 }
473 return false;
474}
475
476bool MIParser::parseBasicBlocks() {
477 lex();
478 // Skip until the first machine basic block.
479 while (Token.is(MIToken::Newline))
480 lex();
481 if (Token.isErrorOrEOF())
482 return Token.isError();
483 // The first parsing pass should have verified that this token is a MBB label
484 // in the 'parseBasicBlockDefinitions' method.
485 assert(Token.is(MIToken::MachineBasicBlockLabel));
486 do {
487 MachineBasicBlock *MBB = nullptr;
488 if (parseMBBReference(MBB))
489 return true;
490 if (parseBasicBlock(*MBB))
491 return true;
492 // The method 'parseBasicBlock' should parse the whole block until the next
493 // block or the end of file.
494 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
495 } while (Token.isNot(MIToken::Eof));
496 return false;
497}
498
499bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000500 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000501 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000502 SmallVector<MachineOperandWithLocation, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000503 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000504 auto Loc = Token.location();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000505 if (parseRegisterOperand(MO, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000506 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000507 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000508 if (Token.isNot(MIToken::comma))
509 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000510 lex();
511 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000512 if (!Operands.empty() && expectAndConsume(MIToken::equal))
513 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000514
Alex Lorenze5a44662015-07-17 00:24:15 +0000515 unsigned OpCode, Flags = 0;
516 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000517 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000518
Alex Lorenz4af7e612015-08-03 23:08:19 +0000519 // TODO: Parse the bundle instruction flags.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000520
521 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000522 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenz4af7e612015-08-03 23:08:19 +0000523 Token.isNot(MIToken::coloncolon)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000524 auto Loc = Token.location();
Alex Lorenz49873a82015-08-06 00:44:07 +0000525 if (parseMachineOperandAndTargetFlags(MO))
Alex Lorenz3708a642015-06-30 17:47:50 +0000526 return true;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000527 Operands.push_back(MachineOperandWithLocation(MO, Loc, Token.location()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000528 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000529 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000530 if (Token.isNot(MIToken::comma))
531 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000532 lex();
533 }
534
Alex Lorenz46d760d2015-07-22 21:15:11 +0000535 DebugLoc DebugLocation;
536 if (Token.is(MIToken::kw_debug_location)) {
537 lex();
538 if (Token.isNot(MIToken::exclaim))
539 return error("expected a metadata node after 'debug-location'");
540 MDNode *Node = nullptr;
541 if (parseMDNode(Node))
542 return true;
543 DebugLocation = DebugLoc(Node);
544 }
545
Alex Lorenz4af7e612015-08-03 23:08:19 +0000546 // Parse the machine memory operands.
547 SmallVector<MachineMemOperand *, 2> MemOperands;
548 if (Token.is(MIToken::coloncolon)) {
549 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000550 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000551 MachineMemOperand *MemOp = nullptr;
552 if (parseMachineMemoryOperand(MemOp))
553 return true;
554 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000555 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000556 break;
557 if (Token.isNot(MIToken::comma))
558 return error("expected ',' before the next machine memory operand");
559 lex();
560 }
561 }
562
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000563 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000564 if (!MCID.isVariadic()) {
565 // FIXME: Move the implicit operand verification to the machine verifier.
566 if (verifyImplicitOperands(Operands, MCID))
567 return true;
568 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000569
Alex Lorenzcb268d42015-07-06 23:07:26 +0000570 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000571 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000572 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000573 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000574 MI->addOperand(MF, Operand.Operand);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000575 if (MemOperands.empty())
576 return false;
577 MachineInstr::mmo_iterator MemRefs =
578 MF.allocateMemRefsArray(MemOperands.size());
579 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
580 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000581 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000582}
583
Alex Lorenz1ea60892015-07-27 20:29:27 +0000584bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000585 lex();
586 if (Token.isNot(MIToken::MachineBasicBlock))
587 return error("expected a machine basic block reference");
588 if (parseMBBReference(MBB))
589 return true;
590 lex();
591 if (Token.isNot(MIToken::Eof))
592 return error(
593 "expected end of string after the machine basic block reference");
594 return false;
595}
596
Alex Lorenz1ea60892015-07-27 20:29:27 +0000597bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000598 lex();
599 if (Token.isNot(MIToken::NamedRegister))
600 return error("expected a named register");
601 if (parseRegister(Reg))
602 return 0;
603 lex();
604 if (Token.isNot(MIToken::Eof))
605 return error("expected end of string after the register reference");
606 return false;
607}
608
Alex Lorenz12045a42015-07-27 17:42:45 +0000609bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
610 lex();
611 if (Token.isNot(MIToken::VirtualRegister))
612 return error("expected a virtual register");
613 if (parseRegister(Reg))
614 return 0;
615 lex();
616 if (Token.isNot(MIToken::Eof))
617 return error("expected end of string after the register reference");
618 return false;
619}
620
Alex Lorenz36962cd2015-07-07 02:08:46 +0000621static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
622 assert(MO.isImplicit());
623 return MO.isDef() ? "implicit-def" : "implicit";
624}
625
626static std::string getRegisterName(const TargetRegisterInfo *TRI,
627 unsigned Reg) {
628 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
629 return StringRef(TRI->getName(Reg)).lower();
630}
631
632bool MIParser::verifyImplicitOperands(
633 ArrayRef<MachineOperandWithLocation> Operands, const MCInstrDesc &MCID) {
634 if (MCID.isCall())
635 // We can't verify call instructions as they can contain arbitrary implicit
636 // register and register mask operands.
637 return false;
638
639 // Gather all the expected implicit operands.
640 SmallVector<MachineOperand, 4> ImplicitOperands;
641 if (MCID.ImplicitDefs)
642 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
643 ImplicitOperands.push_back(
644 MachineOperand::CreateReg(*ImpDefs, true, true));
645 if (MCID.ImplicitUses)
646 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
647 ImplicitOperands.push_back(
648 MachineOperand::CreateReg(*ImpUses, false, true));
649
650 const auto *TRI = MF.getSubtarget().getRegisterInfo();
651 assert(TRI && "Expected target register info");
652 size_t I = ImplicitOperands.size(), J = Operands.size();
653 while (I) {
654 --I;
655 if (J) {
656 --J;
657 const auto &ImplicitOperand = ImplicitOperands[I];
658 const auto &Operand = Operands[J].Operand;
659 if (ImplicitOperand.isIdenticalTo(Operand))
660 continue;
661 if (Operand.isReg() && Operand.isImplicit()) {
662 return error(Operands[J].Begin,
663 Twine("expected an implicit register operand '") +
664 printImplicitRegisterFlag(ImplicitOperand) + " %" +
665 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
666 }
667 }
668 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
669 // insead of reporting an error at this location:
670 // %eax = MOV32r0
671 // ^
672 // report the error at the following location:
673 // %eax = MOV32r0
674 // ^
675 return error(J < Operands.size() ? Operands[J].End : Token.location(),
676 Twine("missing implicit register operand '") +
677 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
678 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
679 }
680 return false;
681}
682
Alex Lorenze5a44662015-07-17 00:24:15 +0000683bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
684 if (Token.is(MIToken::kw_frame_setup)) {
685 Flags |= MachineInstr::FrameSetup;
686 lex();
687 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000688 if (Token.isNot(MIToken::Identifier))
689 return error("expected a machine instruction");
690 StringRef InstrName = Token.stringValue();
691 if (parseInstrName(InstrName, OpCode))
692 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000693 lex();
694 return false;
695}
696
697bool MIParser::parseRegister(unsigned &Reg) {
698 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000699 case MIToken::underscore:
700 Reg = 0;
701 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000702 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000703 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000704 if (getRegisterByName(Name, Reg))
705 return error(Twine("unknown register name '") + Name + "'");
706 break;
707 }
Alex Lorenz53464512015-07-10 22:51:20 +0000708 case MIToken::VirtualRegister: {
709 unsigned ID;
710 if (getUnsigned(ID))
711 return true;
712 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
713 if (RegInfo == PFS.VirtualRegisterSlots.end())
714 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
715 "'");
716 Reg = RegInfo->second;
717 break;
718 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000719 // TODO: Parse other register kinds.
720 default:
721 llvm_unreachable("The current token should be a register");
722 }
723 return false;
724}
725
Alex Lorenzcb268d42015-07-06 23:07:26 +0000726bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000727 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000728 switch (Token.kind()) {
729 case MIToken::kw_implicit:
730 Flags |= RegState::Implicit;
731 break;
732 case MIToken::kw_implicit_define:
733 Flags |= RegState::ImplicitDefine;
734 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000735 case MIToken::kw_dead:
736 Flags |= RegState::Dead;
737 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000738 case MIToken::kw_killed:
739 Flags |= RegState::Kill;
740 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000741 case MIToken::kw_undef:
742 Flags |= RegState::Undef;
743 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000744 case MIToken::kw_early_clobber:
745 Flags |= RegState::EarlyClobber;
746 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000747 case MIToken::kw_debug_use:
748 Flags |= RegState::Debug;
749 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000750 // TODO: parse the other register flags.
751 default:
752 llvm_unreachable("The current token should be a register flag");
753 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000754 if (OldFlags == Flags)
755 // We know that the same flag is specified more than once when the flags
756 // weren't modified.
757 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000758 lex();
759 return false;
760}
761
Alex Lorenz2eacca82015-07-13 23:24:34 +0000762bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
763 assert(Token.is(MIToken::colon));
764 lex();
765 if (Token.isNot(MIToken::Identifier))
766 return error("expected a subregister index after ':'");
767 auto Name = Token.stringValue();
768 SubReg = getSubRegIndex(Name);
769 if (!SubReg)
770 return error(Twine("use of unknown subregister index '") + Name + "'");
771 lex();
772 return false;
773}
774
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000775bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
776 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000777 unsigned Flags = IsDef ? RegState::Define : 0;
778 while (Token.isRegisterFlag()) {
779 if (parseRegisterFlag(Flags))
780 return true;
781 }
782 if (!Token.isRegister())
783 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000784 if (parseRegister(Reg))
785 return true;
786 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000787 unsigned SubReg = 0;
788 if (Token.is(MIToken::colon)) {
789 if (parseSubRegisterIndex(SubReg))
790 return true;
791 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000792 Dest = MachineOperand::CreateReg(
793 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000794 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000795 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000796 return false;
797}
798
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000799bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
800 assert(Token.is(MIToken::IntegerLiteral));
801 const APSInt &Int = Token.integerValue();
802 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +0000803 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000804 Dest = MachineOperand::CreateImm(Int.getExtValue());
805 lex();
806 return false;
807}
808
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000809bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
Alex Lorenz3fb77682015-08-06 23:17:42 +0000810 auto Source = StringRef(Loc, Token.range().end() - Loc).str();
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000811 lex();
812 SMDiagnostic Err;
813 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
814 if (!C)
815 return error(Loc + Err.getColumnNo(), Err.getMessage());
816 return false;
817}
818
Alex Lorenz05e38822015-08-05 18:52:21 +0000819bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
820 assert(Token.is(MIToken::IntegerType));
821 auto Loc = Token.location();
822 lex();
823 if (Token.isNot(MIToken::IntegerLiteral))
824 return error("expected an integer literal");
825 const Constant *C = nullptr;
826 if (parseIRConstant(Loc, C))
827 return true;
828 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
829 return false;
830}
831
Alex Lorenzad156fb2015-07-31 20:49:21 +0000832bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
833 auto Loc = Token.location();
834 lex();
835 if (Token.isNot(MIToken::FloatingPointLiteral))
836 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000837 const Constant *C = nullptr;
838 if (parseIRConstant(Loc, C))
839 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000840 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
841 return false;
842}
843
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000844bool MIParser::getUnsigned(unsigned &Result) {
845 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
846 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
847 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
848 if (Val64 == Limit)
849 return error("expected 32-bit integer (too large)");
850 Result = Val64;
851 return false;
852}
853
Alex Lorenzf09df002015-06-30 18:16:42 +0000854bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000855 assert(Token.is(MIToken::MachineBasicBlock) ||
856 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000857 unsigned Number;
858 if (getUnsigned(Number))
859 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000860 auto MBBInfo = PFS.MBBSlots.find(Number);
861 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000862 return error(Twine("use of undefined machine basic block #") +
863 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +0000864 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000865 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
866 return error(Twine("the name of machine basic block #") + Twine(Number) +
867 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +0000868 return false;
869}
870
871bool MIParser::parseMBBOperand(MachineOperand &Dest) {
872 MachineBasicBlock *MBB;
873 if (parseMBBReference(MBB))
874 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000875 Dest = MachineOperand::CreateMBB(MBB);
876 lex();
877 return false;
878}
879
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000880bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
881 assert(Token.is(MIToken::StackObject));
882 unsigned ID;
883 if (getUnsigned(ID))
884 return true;
885 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
886 if (ObjectInfo == PFS.StackObjectSlots.end())
887 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
888 "'");
889 StringRef Name;
890 if (const auto *Alloca =
891 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
892 Name = Alloca->getName();
893 if (!Token.stringValue().empty() && Token.stringValue() != Name)
894 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
895 "' isn't '" + Token.stringValue() + "'");
896 lex();
897 Dest = MachineOperand::CreateFI(ObjectInfo->second);
898 return false;
899}
900
Alex Lorenzea882122015-08-12 21:17:02 +0000901bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000902 assert(Token.is(MIToken::FixedStackObject));
903 unsigned ID;
904 if (getUnsigned(ID))
905 return true;
906 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
907 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
908 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
909 Twine(ID) + "'");
910 lex();
Alex Lorenzea882122015-08-12 21:17:02 +0000911 FI = ObjectInfo->second;
912 return false;
913}
914
915bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
916 int FI;
917 if (parseFixedStackFrameIndex(FI))
918 return true;
919 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000920 return false;
921}
922
Alex Lorenz41df7d32015-07-28 17:09:52 +0000923bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000924 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000925 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000926 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +0000927 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +0000928 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +0000929 return error(Twine("use of undefined global value '") + Token.range() +
930 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000931 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000932 }
933 case MIToken::GlobalValue: {
934 unsigned GVIdx;
935 if (getUnsigned(GVIdx))
936 return true;
937 if (GVIdx >= IRSlots.GlobalValues.size())
938 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
939 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +0000940 GV = IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000941 break;
942 }
943 default:
944 llvm_unreachable("The current token should be a global value");
945 }
Alex Lorenz41df7d32015-07-28 17:09:52 +0000946 return false;
947}
948
949bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
950 GlobalValue *GV = nullptr;
951 if (parseGlobalValue(GV))
952 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000953 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000954 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000955 if (parseOperandsOffset(Dest))
956 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000957 return false;
958}
959
Alex Lorenzab980492015-07-20 20:51:18 +0000960bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
961 assert(Token.is(MIToken::ConstantPoolItem));
962 unsigned ID;
963 if (getUnsigned(ID))
964 return true;
965 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
966 if (ConstantInfo == PFS.ConstantPoolSlots.end())
967 return error("use of undefined constant '%const." + Twine(ID) + "'");
968 lex();
Alex Lorenzab980492015-07-20 20:51:18 +0000969 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +0000970 if (parseOperandsOffset(Dest))
971 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000972 return false;
973}
974
Alex Lorenz31d70682015-07-15 23:38:35 +0000975bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
976 assert(Token.is(MIToken::JumpTableIndex));
977 unsigned ID;
978 if (getUnsigned(ID))
979 return true;
980 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
981 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
982 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
983 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000984 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
985 return false;
986}
987
Alex Lorenz6ede3742015-07-21 16:59:53 +0000988bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +0000989 assert(Token.is(MIToken::ExternalSymbol));
990 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000991 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +0000992 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +0000993 if (parseOperandsOffset(Dest))
994 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000995 return false;
996}
997
Alex Lorenz44f29252015-07-22 21:07:04 +0000998bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +0000999 assert(Token.is(MIToken::exclaim));
1000 auto Loc = Token.location();
1001 lex();
1002 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1003 return error("expected metadata id after '!'");
1004 unsigned ID;
1005 if (getUnsigned(ID))
1006 return true;
1007 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
1008 if (NodeInfo == IRSlots.MetadataNodes.end())
1009 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1010 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001011 Node = NodeInfo->second.get();
1012 return false;
1013}
1014
1015bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1016 MDNode *Node = nullptr;
1017 if (parseMDNode(Node))
1018 return true;
1019 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001020 return false;
1021}
1022
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001023bool MIParser::parseCFIOffset(int &Offset) {
1024 if (Token.isNot(MIToken::IntegerLiteral))
1025 return error("expected a cfi offset");
1026 if (Token.integerValue().getMinSignedBits() > 32)
1027 return error("expected a 32 bit integer (the cfi offset is too large)");
1028 Offset = (int)Token.integerValue().getExtValue();
1029 lex();
1030 return false;
1031}
1032
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001033bool MIParser::parseCFIRegister(unsigned &Reg) {
1034 if (Token.isNot(MIToken::NamedRegister))
1035 return error("expected a cfi register");
1036 unsigned LLVMReg;
1037 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001038 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001039 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1040 assert(TRI && "Expected target register info");
1041 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1042 if (DwarfReg < 0)
1043 return error("invalid DWARF register");
1044 Reg = (unsigned)DwarfReg;
1045 lex();
1046 return false;
1047}
1048
1049bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1050 auto Kind = Token.kind();
1051 lex();
1052 auto &MMI = MF.getMMI();
1053 int Offset;
1054 unsigned Reg;
1055 unsigned CFIIndex;
1056 switch (Kind) {
1057 case MIToken::kw_cfi_offset:
1058 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1059 parseCFIOffset(Offset))
1060 return true;
1061 CFIIndex =
1062 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1063 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001064 case MIToken::kw_cfi_def_cfa_register:
1065 if (parseCFIRegister(Reg))
1066 return true;
1067 CFIIndex =
1068 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1069 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001070 case MIToken::kw_cfi_def_cfa_offset:
1071 if (parseCFIOffset(Offset))
1072 return true;
1073 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1074 CFIIndex = MMI.addFrameInst(
1075 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1076 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001077 case MIToken::kw_cfi_def_cfa:
1078 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1079 parseCFIOffset(Offset))
1080 return true;
1081 // NB: MCCFIInstruction::createDefCfa negates the offset.
1082 CFIIndex =
1083 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1084 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001085 default:
1086 // TODO: Parse the other CFI operands.
1087 llvm_unreachable("The current token should be a cfi operand");
1088 }
1089 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001090 return false;
1091}
1092
Alex Lorenzdeb53492015-07-28 17:28:03 +00001093bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1094 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001095 case MIToken::NamedIRBlock: {
1096 BB = dyn_cast_or_null<BasicBlock>(
1097 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001098 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001099 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001100 break;
1101 }
1102 case MIToken::IRBlock: {
1103 unsigned SlotNumber = 0;
1104 if (getUnsigned(SlotNumber))
1105 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001106 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001107 if (!BB)
1108 return error(Twine("use of undefined IR block '%ir-block.") +
1109 Twine(SlotNumber) + "'");
1110 break;
1111 }
1112 default:
1113 llvm_unreachable("The current token should be an IR block reference");
1114 }
1115 return false;
1116}
1117
1118bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1119 assert(Token.is(MIToken::kw_blockaddress));
1120 lex();
1121 if (expectAndConsume(MIToken::lparen))
1122 return true;
1123 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001124 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001125 return error("expected a global value");
1126 GlobalValue *GV = nullptr;
1127 if (parseGlobalValue(GV))
1128 return true;
1129 auto *F = dyn_cast<Function>(GV);
1130 if (!F)
1131 return error("expected an IR function reference");
1132 lex();
1133 if (expectAndConsume(MIToken::comma))
1134 return true;
1135 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001136 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001137 return error("expected an IR block reference");
1138 if (parseIRBlock(BB, *F))
1139 return true;
1140 lex();
1141 if (expectAndConsume(MIToken::rparen))
1142 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001143 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001144 if (parseOperandsOffset(Dest))
1145 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001146 return false;
1147}
1148
Alex Lorenzef5c1962015-07-28 23:02:45 +00001149bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1150 assert(Token.is(MIToken::kw_target_index));
1151 lex();
1152 if (expectAndConsume(MIToken::lparen))
1153 return true;
1154 if (Token.isNot(MIToken::Identifier))
1155 return error("expected the name of the target index");
1156 int Index = 0;
1157 if (getTargetIndex(Token.stringValue(), Index))
1158 return error("use of undefined target index '" + Token.stringValue() + "'");
1159 lex();
1160 if (expectAndConsume(MIToken::rparen))
1161 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001162 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001163 if (parseOperandsOffset(Dest))
1164 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001165 return false;
1166}
1167
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001168bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1169 assert(Token.is(MIToken::kw_liveout));
1170 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1171 assert(TRI && "Expected target register info");
1172 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1173 lex();
1174 if (expectAndConsume(MIToken::lparen))
1175 return true;
1176 while (true) {
1177 if (Token.isNot(MIToken::NamedRegister))
1178 return error("expected a named register");
1179 unsigned Reg = 0;
1180 if (parseRegister(Reg))
1181 return true;
1182 lex();
1183 Mask[Reg / 32] |= 1U << (Reg % 32);
1184 // TODO: Report an error if the same register is used more than once.
1185 if (Token.isNot(MIToken::comma))
1186 break;
1187 lex();
1188 }
1189 if (expectAndConsume(MIToken::rparen))
1190 return true;
1191 Dest = MachineOperand::CreateRegLiveOut(Mask);
1192 return false;
1193}
1194
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001195bool MIParser::parseMachineOperand(MachineOperand &Dest) {
1196 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001197 case MIToken::kw_implicit:
1198 case MIToken::kw_implicit_define:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001199 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001200 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001201 case MIToken::kw_undef:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001202 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001203 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001204 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001205 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001206 case MIToken::VirtualRegister:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001207 return parseRegisterOperand(Dest);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001208 case MIToken::IntegerLiteral:
1209 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001210 case MIToken::IntegerType:
1211 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001212 case MIToken::kw_half:
1213 case MIToken::kw_float:
1214 case MIToken::kw_double:
1215 case MIToken::kw_x86_fp80:
1216 case MIToken::kw_fp128:
1217 case MIToken::kw_ppc_fp128:
1218 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001219 case MIToken::MachineBasicBlock:
1220 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001221 case MIToken::StackObject:
1222 return parseStackObjectOperand(Dest);
1223 case MIToken::FixedStackObject:
1224 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001225 case MIToken::GlobalValue:
1226 case MIToken::NamedGlobalValue:
1227 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001228 case MIToken::ConstantPoolItem:
1229 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001230 case MIToken::JumpTableIndex:
1231 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001232 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001233 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001234 case MIToken::exclaim:
1235 return parseMetadataOperand(Dest);
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001236 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001237 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001238 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001239 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001240 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001241 case MIToken::kw_blockaddress:
1242 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001243 case MIToken::kw_target_index:
1244 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001245 case MIToken::kw_liveout:
1246 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001247 case MIToken::Error:
1248 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001249 case MIToken::Identifier:
1250 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1251 Dest = MachineOperand::CreateRegMask(RegMask);
1252 lex();
1253 break;
1254 }
1255 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001256 default:
1257 // TODO: parse the other machine operands.
1258 return error("expected a machine operand");
1259 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001260 return false;
1261}
1262
Alex Lorenz49873a82015-08-06 00:44:07 +00001263bool MIParser::parseMachineOperandAndTargetFlags(MachineOperand &Dest) {
1264 unsigned TF = 0;
1265 bool HasTargetFlags = false;
1266 if (Token.is(MIToken::kw_target_flags)) {
1267 HasTargetFlags = true;
1268 lex();
1269 if (expectAndConsume(MIToken::lparen))
1270 return true;
1271 if (Token.isNot(MIToken::Identifier))
1272 return error("expected the name of the target flag");
1273 if (getDirectTargetFlag(Token.stringValue(), TF))
1274 return error("use of undefined target flag '" + Token.stringValue() +
1275 "'");
1276 lex();
1277 // TODO: Parse target's bit target flags.
1278 if (expectAndConsume(MIToken::rparen))
1279 return true;
1280 }
1281 auto Loc = Token.location();
1282 if (parseMachineOperand(Dest))
1283 return true;
1284 if (!HasTargetFlags)
1285 return false;
1286 if (Dest.isReg())
1287 return error(Loc, "register operands can't have target flags");
1288 Dest.setTargetFlags(TF);
1289 return false;
1290}
1291
Alex Lorenzdc24c172015-08-07 20:21:00 +00001292bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001293 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1294 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001295 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001296 bool IsNegative = Token.is(MIToken::minus);
1297 lex();
1298 if (Token.isNot(MIToken::IntegerLiteral))
1299 return error("expected an integer literal after '" + Sign + "'");
1300 if (Token.integerValue().getMinSignedBits() > 64)
1301 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001302 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001303 if (IsNegative)
1304 Offset = -Offset;
1305 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001306 return false;
1307}
1308
Alex Lorenz620f8912015-08-13 20:33:33 +00001309bool MIParser::parseAlignment(unsigned &Alignment) {
1310 assert(Token.is(MIToken::kw_align));
1311 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001312 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001313 return error("expected an integer literal after 'align'");
1314 if (getUnsigned(Alignment))
1315 return true;
1316 lex();
1317 return false;
1318}
1319
Alex Lorenzdc24c172015-08-07 20:21:00 +00001320bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1321 int64_t Offset = 0;
1322 if (parseOffset(Offset))
1323 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001324 Op.setOffset(Offset);
1325 return false;
1326}
1327
Alex Lorenz4af7e612015-08-03 23:08:19 +00001328bool MIParser::parseIRValue(Value *&V) {
1329 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001330 case MIToken::NamedIRValue: {
1331 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001332 if (!V)
Alex Lorenz2791dcc2015-08-12 21:27:16 +00001333 V = MF.getFunction()->getParent()->getValueSymbolTable().lookup(
1334 Token.stringValue());
1335 if (!V)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001336 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001337 break;
1338 }
1339 // TODO: Parse unnamed IR value references.
1340 default:
1341 llvm_unreachable("The current token should be an IR block reference");
1342 }
1343 return false;
1344}
1345
1346bool MIParser::getUint64(uint64_t &Result) {
1347 assert(Token.hasIntegerValue());
1348 if (Token.integerValue().getActiveBits() > 64)
1349 return error("expected 64-bit integer (too large)");
1350 Result = Token.integerValue().getZExtValue();
1351 return false;
1352}
1353
Alex Lorenza518b792015-08-04 00:24:45 +00001354bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
Alex Lorenze86d5152015-08-06 18:26:36 +00001355 const unsigned OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001356 switch (Token.kind()) {
1357 case MIToken::kw_volatile:
1358 Flags |= MachineMemOperand::MOVolatile;
1359 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001360 case MIToken::kw_non_temporal:
1361 Flags |= MachineMemOperand::MONonTemporal;
1362 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001363 case MIToken::kw_invariant:
1364 Flags |= MachineMemOperand::MOInvariant;
1365 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001366 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001367 default:
1368 llvm_unreachable("The current token should be a memory operand flag");
1369 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001370 if (OldFlags == Flags)
1371 // We know that the same flag is specified more than once when the flags
1372 // weren't modified.
1373 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001374 lex();
1375 return false;
1376}
1377
Alex Lorenz91097a32015-08-12 20:33:26 +00001378bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1379 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001380 case MIToken::kw_stack:
1381 PSV = MF.getPSVManager().getStack();
1382 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001383 case MIToken::kw_got:
1384 PSV = MF.getPSVManager().getGOT();
1385 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001386 case MIToken::kw_jump_table:
1387 PSV = MF.getPSVManager().getJumpTable();
1388 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001389 case MIToken::kw_constant_pool:
1390 PSV = MF.getPSVManager().getConstantPool();
1391 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001392 case MIToken::FixedStackObject: {
1393 int FI;
1394 if (parseFixedStackFrameIndex(FI))
1395 return true;
1396 PSV = MF.getPSVManager().getFixedStack(FI);
1397 // The token was already consumed, so use return here instead of break.
1398 return false;
1399 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001400 // TODO: Parse the other pseudo source values.
1401 default:
1402 llvm_unreachable("The current token should be pseudo source value");
1403 }
1404 lex();
1405 return false;
1406}
1407
1408bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001409 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001410 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
1411 Token.is(MIToken::FixedStackObject)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001412 const PseudoSourceValue *PSV = nullptr;
1413 if (parseMemoryPseudoSourceValue(PSV))
1414 return true;
1415 int64_t Offset = 0;
1416 if (parseOffset(Offset))
1417 return true;
1418 Dest = MachinePointerInfo(PSV, Offset);
1419 return false;
1420 }
1421 if (Token.isNot(MIToken::NamedIRValue))
1422 return error("expected an IR value reference");
1423 Value *V = nullptr;
1424 if (parseIRValue(V))
1425 return true;
1426 if (!V->getType()->isPointerTy())
1427 return error("expected a pointer IR value");
1428 lex();
1429 int64_t Offset = 0;
1430 if (parseOffset(Offset))
1431 return true;
1432 Dest = MachinePointerInfo(V, Offset);
1433 return false;
1434}
1435
Alex Lorenz4af7e612015-08-03 23:08:19 +00001436bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1437 if (expectAndConsume(MIToken::lparen))
1438 return true;
Alex Lorenza518b792015-08-04 00:24:45 +00001439 unsigned Flags = 0;
1440 while (Token.isMemoryOperandFlag()) {
1441 if (parseMemoryOperandFlag(Flags))
1442 return true;
1443 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001444 if (Token.isNot(MIToken::Identifier) ||
1445 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1446 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001447 if (Token.stringValue() == "load")
1448 Flags |= MachineMemOperand::MOLoad;
1449 else
1450 Flags |= MachineMemOperand::MOStore;
1451 lex();
1452
1453 if (Token.isNot(MIToken::IntegerLiteral))
1454 return error("expected the size integer literal after memory operation");
1455 uint64_t Size;
1456 if (getUint64(Size))
1457 return true;
1458 lex();
1459
1460 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1461 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1462 return error(Twine("expected '") + Word + "'");
1463 lex();
1464
Alex Lorenz91097a32015-08-12 20:33:26 +00001465 MachinePointerInfo Ptr = MachinePointerInfo();
1466 if (parseMachinePointerInfo(Ptr))
Alex Lorenz83127732015-08-07 20:26:52 +00001467 return true;
Alex Lorenz61420f72015-08-07 20:48:30 +00001468 unsigned BaseAlignment = Size;
1469 if (Token.is(MIToken::comma)) {
1470 lex();
1471 if (Token.isNot(MIToken::kw_align))
1472 return error("expected 'align'");
Alex Lorenz620f8912015-08-13 20:33:33 +00001473 if (parseAlignment(BaseAlignment))
Alex Lorenz61420f72015-08-07 20:48:30 +00001474 return true;
Alex Lorenz61420f72015-08-07 20:48:30 +00001475 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001476 // TODO: Parse the attached metadata nodes.
1477 if (expectAndConsume(MIToken::rparen))
1478 return true;
Alex Lorenz91097a32015-08-12 20:33:26 +00001479 Dest = MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001480 return false;
1481}
1482
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001483void MIParser::initNames2InstrOpCodes() {
1484 if (!Names2InstrOpCodes.empty())
1485 return;
1486 const auto *TII = MF.getSubtarget().getInstrInfo();
1487 assert(TII && "Expected target instruction info");
1488 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1489 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1490}
1491
1492bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1493 initNames2InstrOpCodes();
1494 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1495 if (InstrInfo == Names2InstrOpCodes.end())
1496 return true;
1497 OpCode = InstrInfo->getValue();
1498 return false;
1499}
1500
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001501void MIParser::initNames2Regs() {
1502 if (!Names2Regs.empty())
1503 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001504 // The '%noreg' register is the register 0.
1505 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001506 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1507 assert(TRI && "Expected target register info");
1508 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1509 bool WasInserted =
1510 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1511 .second;
1512 (void)WasInserted;
1513 assert(WasInserted && "Expected registers to be unique case-insensitively");
1514 }
1515}
1516
1517bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1518 initNames2Regs();
1519 auto RegInfo = Names2Regs.find(RegName);
1520 if (RegInfo == Names2Regs.end())
1521 return true;
1522 Reg = RegInfo->getValue();
1523 return false;
1524}
1525
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001526void MIParser::initNames2RegMasks() {
1527 if (!Names2RegMasks.empty())
1528 return;
1529 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1530 assert(TRI && "Expected target register info");
1531 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1532 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1533 assert(RegMasks.size() == RegMaskNames.size());
1534 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1535 Names2RegMasks.insert(
1536 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1537}
1538
1539const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1540 initNames2RegMasks();
1541 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1542 if (RegMaskInfo == Names2RegMasks.end())
1543 return nullptr;
1544 return RegMaskInfo->getValue();
1545}
1546
Alex Lorenz2eacca82015-07-13 23:24:34 +00001547void MIParser::initNames2SubRegIndices() {
1548 if (!Names2SubRegIndices.empty())
1549 return;
1550 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1551 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1552 Names2SubRegIndices.insert(
1553 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1554}
1555
1556unsigned MIParser::getSubRegIndex(StringRef Name) {
1557 initNames2SubRegIndices();
1558 auto SubRegInfo = Names2SubRegIndices.find(Name);
1559 if (SubRegInfo == Names2SubRegIndices.end())
1560 return 0;
1561 return SubRegInfo->getValue();
1562}
1563
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001564static void initSlots2BasicBlocks(
1565 const Function &F,
1566 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1567 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001568 MST.incorporateFunction(F);
1569 for (auto &BB : F) {
1570 if (BB.hasName())
1571 continue;
1572 int Slot = MST.getLocalSlot(&BB);
1573 if (Slot == -1)
1574 continue;
1575 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1576 }
1577}
1578
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001579static const BasicBlock *getIRBlockFromSlot(
1580 unsigned Slot,
1581 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001582 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1583 if (BlockInfo == Slots2BasicBlocks.end())
1584 return nullptr;
1585 return BlockInfo->second;
1586}
1587
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001588const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1589 if (Slots2BasicBlocks.empty())
1590 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1591 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1592}
1593
1594const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1595 if (&F == MF.getFunction())
1596 return getIRBlock(Slot);
1597 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1598 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1599 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1600}
1601
Alex Lorenzef5c1962015-07-28 23:02:45 +00001602void MIParser::initNames2TargetIndices() {
1603 if (!Names2TargetIndices.empty())
1604 return;
1605 const auto *TII = MF.getSubtarget().getInstrInfo();
1606 assert(TII && "Expected target instruction info");
1607 auto Indices = TII->getSerializableTargetIndices();
1608 for (const auto &I : Indices)
1609 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1610}
1611
1612bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1613 initNames2TargetIndices();
1614 auto IndexInfo = Names2TargetIndices.find(Name);
1615 if (IndexInfo == Names2TargetIndices.end())
1616 return true;
1617 Index = IndexInfo->second;
1618 return false;
1619}
1620
Alex Lorenz49873a82015-08-06 00:44:07 +00001621void MIParser::initNames2DirectTargetFlags() {
1622 if (!Names2DirectTargetFlags.empty())
1623 return;
1624 const auto *TII = MF.getSubtarget().getInstrInfo();
1625 assert(TII && "Expected target instruction info");
1626 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1627 for (const auto &I : Flags)
1628 Names2DirectTargetFlags.insert(
1629 std::make_pair(StringRef(I.second), I.first));
1630}
1631
1632bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1633 initNames2DirectTargetFlags();
1634 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1635 if (FlagInfo == Names2DirectTargetFlags.end())
1636 return true;
1637 Flag = FlagInfo->second;
1638 return false;
1639}
1640
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001641bool llvm::parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src,
1642 PerFunctionMIParsingState &PFS,
1643 const SlotMapping &IRSlots,
1644 SMDiagnostic &Error) {
1645 SourceMgr SM;
1646 SM.AddNewSourceBuffer(
1647 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1648 SMLoc());
1649 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1650 .parseBasicBlockDefinitions(PFS.MBBSlots);
1651}
1652
1653bool llvm::parseMachineInstructions(MachineFunction &MF, StringRef Src,
1654 const PerFunctionMIParsingState &PFS,
1655 const SlotMapping &IRSlots,
1656 SMDiagnostic &Error) {
1657 SourceMgr SM;
1658 SM.AddNewSourceBuffer(
1659 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1660 SMLoc());
1661 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001662}
Alex Lorenzf09df002015-06-30 18:16:42 +00001663
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001664bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1665 MachineFunction &MF, StringRef Src,
1666 const PerFunctionMIParsingState &PFS,
1667 const SlotMapping &IRSlots, SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001668 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00001669}
Alex Lorenz9fab3702015-07-14 21:24:41 +00001670
1671bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1672 MachineFunction &MF, StringRef Src,
1673 const PerFunctionMIParsingState &PFS,
1674 const SlotMapping &IRSlots,
1675 SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001676 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1677 .parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00001678}
Alex Lorenz12045a42015-07-27 17:42:45 +00001679
1680bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1681 MachineFunction &MF, StringRef Src,
1682 const PerFunctionMIParsingState &PFS,
1683 const SlotMapping &IRSlots,
1684 SMDiagnostic &Error) {
1685 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1686 .parseStandaloneVirtualRegister(Reg);
1687}