blob: 3260249d95bd1bd474cb8c3317922183c279d18a [file] [log] [blame]
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001//===- MIParser.cpp - Machine instructions parser implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the parsing of machine instructions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MIParser.h"
Alex Lorenz91370c52015-06-22 20:37:46 +000015#include "MILexer.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000016#include "llvm/ADT/StringMap.h"
Alex Lorenzad156fb2015-07-31 20:49:21 +000017#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000018#include "llvm/AsmParser/SlotMapping.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000019#include "llvm/CodeGen/MachineBasicBlock.h"
20#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000022#include "llvm/CodeGen/MachineInstr.h"
Alex Lorenzcb268d42015-07-06 23:07:26 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000024#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000026#include "llvm/IR/Instructions.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000027#include "llvm/IR/Constants.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000028#include "llvm/IR/Module.h"
Alex Lorenz8a1915b2015-07-27 22:42:41 +000029#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000030#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000031#include "llvm/Support/raw_ostream.h"
32#include "llvm/Support/SourceMgr.h"
33#include "llvm/Target/TargetSubtargetInfo.h"
34#include "llvm/Target/TargetInstrInfo.h"
35
36using namespace llvm;
37
38namespace {
39
Alex Lorenz36962cd2015-07-07 02:08:46 +000040/// A wrapper struct around the 'MachineOperand' struct that includes a source
Alex Lorenzfeb6b432015-08-19 19:19:16 +000041/// range and other attributes.
42struct ParsedMachineOperand {
Alex Lorenz36962cd2015-07-07 02:08:46 +000043 MachineOperand Operand;
44 StringRef::iterator Begin;
45 StringRef::iterator End;
Alex Lorenz5ef93b02015-08-19 19:05:34 +000046 Optional<unsigned> TiedDefIdx;
Alex Lorenz36962cd2015-07-07 02:08:46 +000047
Alex Lorenzfeb6b432015-08-19 19:19:16 +000048 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
49 StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
Alex Lorenz5ef93b02015-08-19 19:05:34 +000050 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
51 if (TiedDefIdx)
52 assert(Operand.isReg() && Operand.isUse() &&
53 "Only used register operands can be tied");
54 }
Alex Lorenz36962cd2015-07-07 02:08:46 +000055};
56
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000057class MIParser {
58 SourceMgr &SM;
59 MachineFunction &MF;
60 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000061 StringRef Source, CurrentSource;
62 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000063 const PerFunctionMIParsingState &PFS;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000064 /// Maps from indices to unnamed global values and metadata nodes.
65 const SlotMapping &IRSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000066 /// Maps from instruction names to op codes.
67 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000068 /// Maps from register names to registers.
69 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000070 /// Maps from register mask names to register masks.
71 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000072 /// Maps from subregister names to subregister indices.
73 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000074 /// Maps from slot numbers to function's unnamed basic blocks.
75 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzdd13be02015-08-19 23:31:05 +000076 /// Maps from slot numbers to function's unnamed values.
77 DenseMap<unsigned, const Value *> Slots2Values;
Alex Lorenzef5c1962015-07-28 23:02:45 +000078 /// Maps from target index names to target indices.
79 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +000080 /// Maps from direct target flag names to the direct target flag values.
81 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenzf3630112015-08-18 22:52:15 +000082 /// Maps from direct target flag names to the bitmask target flag values.
83 StringMap<unsigned> Names2BitmaskTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000084
85public:
86 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000087 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000088 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000089
Alex Lorenz91370c52015-06-22 20:37:46 +000090 void lex();
91
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000092 /// Report an error at the current location with the given message.
93 ///
94 /// This function always return true.
95 bool error(const Twine &Msg);
96
Alex Lorenz91370c52015-06-22 20:37:46 +000097 /// Report an error at the given location with the given message.
98 ///
99 /// This function always return true.
100 bool error(StringRef::iterator Loc, const Twine &Msg);
101
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000102 bool
103 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
104 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +0000105 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +0000106 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
107 bool parseStandaloneNamedRegister(unsigned &Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +0000108 bool parseStandaloneVirtualRegister(unsigned &Reg);
Alex Lorenza314d812015-08-18 22:26:26 +0000109 bool parseStandaloneStackObject(int &FI);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000110 bool parseStandaloneMDNode(MDNode *&Node);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000111
112 bool
113 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
114 bool parseBasicBlock(MachineBasicBlock &MBB);
115 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
116 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000117
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000118 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000119 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000120 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000121 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
122 bool parseRegisterOperand(MachineOperand &Dest,
123 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000124 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000125 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Alex Lorenz05e38822015-08-05 18:52:21 +0000126 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000127 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000128 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000129 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000130 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000131 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000132 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000133 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000134 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000135 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000136 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000137 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000138 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000139 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000140 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000141 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000142 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000143 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000144 bool parseIRBlock(BasicBlock *&BB, const Function &F);
145 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000146 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000147 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000148 bool parseMachineOperand(MachineOperand &Dest,
149 Optional<unsigned> &TiedDefIdx);
150 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
151 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000152 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000153 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000154 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000155 bool parseIRValue(const Value *&V);
Alex Lorenza518b792015-08-04 00:24:45 +0000156 bool parseMemoryOperandFlag(unsigned &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000157 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
158 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000159 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000160
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000161private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000162 /// Convert the integer literal in the current token into an unsigned integer.
163 ///
164 /// Return true if an error occurred.
165 bool getUnsigned(unsigned &Result);
166
Alex Lorenz4af7e612015-08-03 23:08:19 +0000167 /// Convert the integer literal in the current token into an uint64.
168 ///
169 /// Return true if an error occurred.
170 bool getUint64(uint64_t &Result);
171
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000172 /// If the current token is of the given kind, consume it and return false.
173 /// Otherwise report an error and return true.
174 bool expectAndConsume(MIToken::TokenKind TokenKind);
175
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000176 /// If the current token is of the given kind, consume it and return true.
177 /// Otherwise return false.
178 bool consumeIfPresent(MIToken::TokenKind TokenKind);
179
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000180 void initNames2InstrOpCodes();
181
182 /// Try to convert an instruction name to an opcode. Return true if the
183 /// instruction name is invalid.
184 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000185
Alex Lorenze5a44662015-07-17 00:24:15 +0000186 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000187
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000188 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000189 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000190
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000191 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000192 const MCInstrDesc &MCID);
193
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000194 void initNames2Regs();
195
196 /// Try to convert a register name to a register number. Return true if the
197 /// register name is invalid.
198 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000199
200 void initNames2RegMasks();
201
202 /// Check if the given identifier is a name of a register mask.
203 ///
204 /// Return null if the identifier isn't a register mask.
205 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000206
207 void initNames2SubRegIndices();
208
209 /// Check if the given identifier is a name of a subregister index.
210 ///
211 /// Return 0 if the name isn't a subregister index class.
212 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000213
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000214 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000215 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000216
Alex Lorenzdd13be02015-08-19 23:31:05 +0000217 const Value *getIRValue(unsigned Slot);
218
Alex Lorenzef5c1962015-07-28 23:02:45 +0000219 void initNames2TargetIndices();
220
221 /// Try to convert a name of target index to the corresponding target index.
222 ///
223 /// Return true if the name isn't a name of a target index.
224 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000225
226 void initNames2DirectTargetFlags();
227
228 /// Try to convert a name of a direct target flag to the corresponding
229 /// target flag.
230 ///
231 /// Return true if the name isn't a name of a direct flag.
232 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000233
234 void initNames2BitmaskTargetFlags();
235
236 /// Try to convert a name of a bitmask target flag to the corresponding
237 /// target flag.
238 ///
239 /// Return true if the name isn't a name of a bitmask target flag.
240 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000241};
242
243} // end anonymous namespace
244
245MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000246 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000247 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000248 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz3fb77682015-08-06 23:17:42 +0000249 PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000250
Alex Lorenz91370c52015-06-22 20:37:46 +0000251void MIParser::lex() {
252 CurrentSource = lexMIToken(
253 CurrentSource, Token,
254 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
255}
256
257bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
258
259bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000260 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000261 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
262 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
263 // Create an ordinary diagnostic when the source manager's buffer is the
264 // source string.
265 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
266 return true;
267 }
268 // Create a diagnostic for a YAML string literal.
269 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
270 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
271 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000272 return true;
273}
274
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000275static const char *toString(MIToken::TokenKind TokenKind) {
276 switch (TokenKind) {
277 case MIToken::comma:
278 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000279 case MIToken::equal:
280 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000281 case MIToken::colon:
282 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000283 case MIToken::lparen:
284 return "'('";
285 case MIToken::rparen:
286 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000287 default:
288 return "<unknown token>";
289 }
290}
291
292bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
293 if (Token.isNot(TokenKind))
294 return error(Twine("expected ") + toString(TokenKind));
295 lex();
296 return false;
297}
298
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000299bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
300 if (Token.isNot(TokenKind))
301 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000302 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000303 return true;
304}
Alex Lorenz91370c52015-06-22 20:37:46 +0000305
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000306bool MIParser::parseBasicBlockDefinition(
307 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
308 assert(Token.is(MIToken::MachineBasicBlockLabel));
309 unsigned ID = 0;
310 if (getUnsigned(ID))
311 return true;
312 auto Loc = Token.location();
313 auto Name = Token.stringValue();
314 lex();
315 bool HasAddressTaken = false;
316 bool IsLandingPad = false;
317 unsigned Alignment = 0;
318 BasicBlock *BB = nullptr;
319 if (consumeIfPresent(MIToken::lparen)) {
320 do {
321 // TODO: Report an error when multiple same attributes are specified.
322 switch (Token.kind()) {
323 case MIToken::kw_address_taken:
324 HasAddressTaken = true;
325 lex();
326 break;
327 case MIToken::kw_landing_pad:
328 IsLandingPad = true;
329 lex();
330 break;
331 case MIToken::kw_align:
332 if (parseAlignment(Alignment))
333 return true;
334 break;
335 case MIToken::IRBlock:
336 // TODO: Report an error when both name and ir block are specified.
337 if (parseIRBlock(BB, *MF.getFunction()))
338 return true;
339 lex();
340 break;
341 default:
342 break;
343 }
344 } while (consumeIfPresent(MIToken::comma));
345 if (expectAndConsume(MIToken::rparen))
346 return true;
347 }
348 if (expectAndConsume(MIToken::colon))
349 return true;
350
351 if (!Name.empty()) {
352 BB = dyn_cast_or_null<BasicBlock>(
353 MF.getFunction()->getValueSymbolTable().lookup(Name));
354 if (!BB)
355 return error(Loc, Twine("basic block '") + Name +
356 "' is not defined in the function '" +
357 MF.getName() + "'");
358 }
359 auto *MBB = MF.CreateMachineBasicBlock(BB);
360 MF.insert(MF.end(), MBB);
361 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
362 if (!WasInserted)
363 return error(Loc, Twine("redefinition of machine basic block with id #") +
364 Twine(ID));
365 if (Alignment)
366 MBB->setAlignment(Alignment);
367 if (HasAddressTaken)
368 MBB->setHasAddressTaken();
369 MBB->setIsLandingPad(IsLandingPad);
370 return false;
371}
372
373bool MIParser::parseBasicBlockDefinitions(
374 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
375 lex();
376 // Skip until the first machine basic block.
377 while (Token.is(MIToken::Newline))
378 lex();
379 if (Token.isErrorOrEOF())
380 return Token.isError();
381 if (Token.isNot(MIToken::MachineBasicBlockLabel))
382 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000383 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000384 do {
385 if (parseBasicBlockDefinition(MBBSlots))
386 return true;
387 bool IsAfterNewline = false;
388 // Skip until the next machine basic block.
389 while (true) {
390 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
391 Token.isErrorOrEOF())
392 break;
393 else if (Token.is(MIToken::MachineBasicBlockLabel))
394 return error("basic block definition should be located at the start of "
395 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000396 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000397 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000398 continue;
399 }
400 IsAfterNewline = false;
401 if (Token.is(MIToken::lbrace))
402 ++BraceDepth;
403 if (Token.is(MIToken::rbrace)) {
404 if (!BraceDepth)
405 return error("extraneous closing brace ('}')");
406 --BraceDepth;
407 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000408 lex();
409 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000410 // Verify that we closed all of the '{' at the end of a file or a block.
411 if (!Token.isError() && BraceDepth)
412 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000413 } while (!Token.isErrorOrEOF());
414 return Token.isError();
415}
416
417bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
418 assert(Token.is(MIToken::kw_liveins));
419 lex();
420 if (expectAndConsume(MIToken::colon))
421 return true;
422 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
423 return false;
424 do {
425 if (Token.isNot(MIToken::NamedRegister))
426 return error("expected a named register");
427 unsigned Reg = 0;
428 if (parseRegister(Reg))
429 return true;
430 MBB.addLiveIn(Reg);
431 lex();
432 } while (consumeIfPresent(MIToken::comma));
433 return false;
434}
435
436bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
437 assert(Token.is(MIToken::kw_successors));
438 lex();
439 if (expectAndConsume(MIToken::colon))
440 return true;
441 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
442 return false;
443 do {
444 if (Token.isNot(MIToken::MachineBasicBlock))
445 return error("expected a machine basic block reference");
446 MachineBasicBlock *SuccMBB = nullptr;
447 if (parseMBBReference(SuccMBB))
448 return true;
449 lex();
450 unsigned Weight = 0;
451 if (consumeIfPresent(MIToken::lparen)) {
452 if (Token.isNot(MIToken::IntegerLiteral))
453 return error("expected an integer literal after '('");
454 if (getUnsigned(Weight))
455 return true;
456 lex();
457 if (expectAndConsume(MIToken::rparen))
458 return true;
459 }
460 MBB.addSuccessor(SuccMBB, Weight);
461 } while (consumeIfPresent(MIToken::comma));
462 return false;
463}
464
465bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
466 // Skip the definition.
467 assert(Token.is(MIToken::MachineBasicBlockLabel));
468 lex();
469 if (consumeIfPresent(MIToken::lparen)) {
470 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
471 lex();
472 consumeIfPresent(MIToken::rparen);
473 }
474 consumeIfPresent(MIToken::colon);
475
476 // Parse the liveins and successors.
477 // N.B: Multiple lists of successors and liveins are allowed and they're
478 // merged into one.
479 // Example:
480 // liveins: %edi
481 // liveins: %esi
482 //
483 // is equivalent to
484 // liveins: %edi, %esi
485 while (true) {
486 if (Token.is(MIToken::kw_successors)) {
487 if (parseBasicBlockSuccessors(MBB))
488 return true;
489 } else if (Token.is(MIToken::kw_liveins)) {
490 if (parseBasicBlockLiveins(MBB))
491 return true;
492 } else if (consumeIfPresent(MIToken::Newline)) {
493 continue;
494 } else
495 break;
496 if (!Token.isNewlineOrEOF())
497 return error("expected line break at the end of a list");
498 lex();
499 }
500
501 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000502 bool IsInBundle = false;
503 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000504 while (true) {
505 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
506 return false;
507 else if (consumeIfPresent(MIToken::Newline))
508 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000509 if (consumeIfPresent(MIToken::rbrace)) {
510 // The first parsing pass should verify that all closing '}' have an
511 // opening '{'.
512 assert(IsInBundle);
513 IsInBundle = false;
514 continue;
515 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000516 MachineInstr *MI = nullptr;
517 if (parse(MI))
518 return true;
519 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000520 if (IsInBundle) {
521 PrevMI->setFlag(MachineInstr::BundledSucc);
522 MI->setFlag(MachineInstr::BundledPred);
523 }
524 PrevMI = MI;
525 if (Token.is(MIToken::lbrace)) {
526 if (IsInBundle)
527 return error("nested instruction bundles are not allowed");
528 lex();
529 // This instruction is the start of the bundle.
530 MI->setFlag(MachineInstr::BundledSucc);
531 IsInBundle = true;
532 if (!Token.is(MIToken::Newline))
533 // The next instruction can be on the same line.
534 continue;
535 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000536 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
537 lex();
538 }
539 return false;
540}
541
542bool MIParser::parseBasicBlocks() {
543 lex();
544 // Skip until the first machine basic block.
545 while (Token.is(MIToken::Newline))
546 lex();
547 if (Token.isErrorOrEOF())
548 return Token.isError();
549 // The first parsing pass should have verified that this token is a MBB label
550 // in the 'parseBasicBlockDefinitions' method.
551 assert(Token.is(MIToken::MachineBasicBlockLabel));
552 do {
553 MachineBasicBlock *MBB = nullptr;
554 if (parseMBBReference(MBB))
555 return true;
556 if (parseBasicBlock(*MBB))
557 return true;
558 // The method 'parseBasicBlock' should parse the whole block until the next
559 // block or the end of file.
560 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
561 } while (Token.isNot(MIToken::Eof));
562 return false;
563}
564
565bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000566 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000567 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000568 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000569 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000570 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000571 Optional<unsigned> TiedDefIdx;
572 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000573 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000574 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000575 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000576 if (Token.isNot(MIToken::comma))
577 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000578 lex();
579 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000580 if (!Operands.empty() && expectAndConsume(MIToken::equal))
581 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000582
Alex Lorenze5a44662015-07-17 00:24:15 +0000583 unsigned OpCode, Flags = 0;
584 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000585 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000586
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000587 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000588 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000589 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000590 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000591 Optional<unsigned> TiedDefIdx;
592 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000593 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000594 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000595 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000596 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
597 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000598 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000599 if (Token.isNot(MIToken::comma))
600 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000601 lex();
602 }
603
Alex Lorenz46d760d2015-07-22 21:15:11 +0000604 DebugLoc DebugLocation;
605 if (Token.is(MIToken::kw_debug_location)) {
606 lex();
607 if (Token.isNot(MIToken::exclaim))
608 return error("expected a metadata node after 'debug-location'");
609 MDNode *Node = nullptr;
610 if (parseMDNode(Node))
611 return true;
612 DebugLocation = DebugLoc(Node);
613 }
614
Alex Lorenz4af7e612015-08-03 23:08:19 +0000615 // Parse the machine memory operands.
616 SmallVector<MachineMemOperand *, 2> MemOperands;
617 if (Token.is(MIToken::coloncolon)) {
618 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000619 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000620 MachineMemOperand *MemOp = nullptr;
621 if (parseMachineMemoryOperand(MemOp))
622 return true;
623 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000624 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000625 break;
626 if (Token.isNot(MIToken::comma))
627 return error("expected ',' before the next machine memory operand");
628 lex();
629 }
630 }
631
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000632 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000633 if (!MCID.isVariadic()) {
634 // FIXME: Move the implicit operand verification to the machine verifier.
635 if (verifyImplicitOperands(Operands, MCID))
636 return true;
637 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000638
Alex Lorenzcb268d42015-07-06 23:07:26 +0000639 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000640 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000641 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000642 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000643 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000644 if (assignRegisterTies(*MI, Operands))
645 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000646 if (MemOperands.empty())
647 return false;
648 MachineInstr::mmo_iterator MemRefs =
649 MF.allocateMemRefsArray(MemOperands.size());
650 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
651 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000652 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000653}
654
Alex Lorenz1ea60892015-07-27 20:29:27 +0000655bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000656 lex();
657 if (Token.isNot(MIToken::MachineBasicBlock))
658 return error("expected a machine basic block reference");
659 if (parseMBBReference(MBB))
660 return true;
661 lex();
662 if (Token.isNot(MIToken::Eof))
663 return error(
664 "expected end of string after the machine basic block reference");
665 return false;
666}
667
Alex Lorenz1ea60892015-07-27 20:29:27 +0000668bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000669 lex();
670 if (Token.isNot(MIToken::NamedRegister))
671 return error("expected a named register");
672 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000673 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000674 lex();
675 if (Token.isNot(MIToken::Eof))
676 return error("expected end of string after the register reference");
677 return false;
678}
679
Alex Lorenz12045a42015-07-27 17:42:45 +0000680bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
681 lex();
682 if (Token.isNot(MIToken::VirtualRegister))
683 return error("expected a virtual register");
684 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000685 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000686 lex();
687 if (Token.isNot(MIToken::Eof))
688 return error("expected end of string after the register reference");
689 return false;
690}
691
Alex Lorenza314d812015-08-18 22:26:26 +0000692bool MIParser::parseStandaloneStackObject(int &FI) {
693 lex();
694 if (Token.isNot(MIToken::StackObject))
695 return error("expected a stack object");
696 if (parseStackFrameIndex(FI))
697 return true;
698 if (Token.isNot(MIToken::Eof))
699 return error("expected end of string after the stack object reference");
700 return false;
701}
702
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000703bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
704 lex();
705 if (Token.isNot(MIToken::exclaim))
706 return error("expected a metadata node");
707 if (parseMDNode(Node))
708 return true;
709 if (Token.isNot(MIToken::Eof))
710 return error("expected end of string after the metadata node");
711 return false;
712}
713
Alex Lorenz36962cd2015-07-07 02:08:46 +0000714static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
715 assert(MO.isImplicit());
716 return MO.isDef() ? "implicit-def" : "implicit";
717}
718
719static std::string getRegisterName(const TargetRegisterInfo *TRI,
720 unsigned Reg) {
721 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
722 return StringRef(TRI->getName(Reg)).lower();
723}
724
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000725bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
726 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000727 if (MCID.isCall())
728 // We can't verify call instructions as they can contain arbitrary implicit
729 // register and register mask operands.
730 return false;
731
732 // Gather all the expected implicit operands.
733 SmallVector<MachineOperand, 4> ImplicitOperands;
734 if (MCID.ImplicitDefs)
735 for (const uint16_t *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
736 ImplicitOperands.push_back(
737 MachineOperand::CreateReg(*ImpDefs, true, true));
738 if (MCID.ImplicitUses)
739 for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
740 ImplicitOperands.push_back(
741 MachineOperand::CreateReg(*ImpUses, false, true));
742
743 const auto *TRI = MF.getSubtarget().getRegisterInfo();
744 assert(TRI && "Expected target register info");
745 size_t I = ImplicitOperands.size(), J = Operands.size();
746 while (I) {
747 --I;
748 if (J) {
749 --J;
750 const auto &ImplicitOperand = ImplicitOperands[I];
751 const auto &Operand = Operands[J].Operand;
752 if (ImplicitOperand.isIdenticalTo(Operand))
753 continue;
754 if (Operand.isReg() && Operand.isImplicit()) {
Alex Lorenzeb7c9be2015-08-18 17:17:13 +0000755 // Check if this implicit register is a subregister of an explicit
756 // register operand.
757 bool IsImplicitSubRegister = false;
758 for (size_t K = 0, E = Operands.size(); K < E; ++K) {
759 const auto &Op = Operands[K].Operand;
760 if (Op.isReg() && !Op.isImplicit() &&
761 TRI->isSubRegister(Op.getReg(), Operand.getReg())) {
762 IsImplicitSubRegister = true;
763 break;
764 }
765 }
766 if (IsImplicitSubRegister)
767 continue;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000768 return error(Operands[J].Begin,
769 Twine("expected an implicit register operand '") +
770 printImplicitRegisterFlag(ImplicitOperand) + " %" +
771 getRegisterName(TRI, ImplicitOperand.getReg()) + "'");
772 }
773 }
774 // TODO: Fix source location when Operands[J].end is right before '=', i.e:
775 // insead of reporting an error at this location:
776 // %eax = MOV32r0
777 // ^
778 // report the error at the following location:
779 // %eax = MOV32r0
780 // ^
781 return error(J < Operands.size() ? Operands[J].End : Token.location(),
782 Twine("missing implicit register operand '") +
783 printImplicitRegisterFlag(ImplicitOperands[I]) + " %" +
784 getRegisterName(TRI, ImplicitOperands[I].getReg()) + "'");
785 }
786 return false;
787}
788
Alex Lorenze5a44662015-07-17 00:24:15 +0000789bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
790 if (Token.is(MIToken::kw_frame_setup)) {
791 Flags |= MachineInstr::FrameSetup;
792 lex();
793 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000794 if (Token.isNot(MIToken::Identifier))
795 return error("expected a machine instruction");
796 StringRef InstrName = Token.stringValue();
797 if (parseInstrName(InstrName, OpCode))
798 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000799 lex();
800 return false;
801}
802
803bool MIParser::parseRegister(unsigned &Reg) {
804 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000805 case MIToken::underscore:
806 Reg = 0;
807 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000808 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000809 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000810 if (getRegisterByName(Name, Reg))
811 return error(Twine("unknown register name '") + Name + "'");
812 break;
813 }
Alex Lorenz53464512015-07-10 22:51:20 +0000814 case MIToken::VirtualRegister: {
815 unsigned ID;
816 if (getUnsigned(ID))
817 return true;
818 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
819 if (RegInfo == PFS.VirtualRegisterSlots.end())
820 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
821 "'");
822 Reg = RegInfo->second;
823 break;
824 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000825 // TODO: Parse other register kinds.
826 default:
827 llvm_unreachable("The current token should be a register");
828 }
829 return false;
830}
831
Alex Lorenzcb268d42015-07-06 23:07:26 +0000832bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000833 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000834 switch (Token.kind()) {
835 case MIToken::kw_implicit:
836 Flags |= RegState::Implicit;
837 break;
838 case MIToken::kw_implicit_define:
839 Flags |= RegState::ImplicitDefine;
840 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000841 case MIToken::kw_def:
842 Flags |= RegState::Define;
843 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000844 case MIToken::kw_dead:
845 Flags |= RegState::Dead;
846 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000847 case MIToken::kw_killed:
848 Flags |= RegState::Kill;
849 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000850 case MIToken::kw_undef:
851 Flags |= RegState::Undef;
852 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000853 case MIToken::kw_internal:
854 Flags |= RegState::InternalRead;
855 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000856 case MIToken::kw_early_clobber:
857 Flags |= RegState::EarlyClobber;
858 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000859 case MIToken::kw_debug_use:
860 Flags |= RegState::Debug;
861 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000862 default:
863 llvm_unreachable("The current token should be a register flag");
864 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000865 if (OldFlags == Flags)
866 // We know that the same flag is specified more than once when the flags
867 // weren't modified.
868 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000869 lex();
870 return false;
871}
872
Alex Lorenz2eacca82015-07-13 23:24:34 +0000873bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
874 assert(Token.is(MIToken::colon));
875 lex();
876 if (Token.isNot(MIToken::Identifier))
877 return error("expected a subregister index after ':'");
878 auto Name = Token.stringValue();
879 SubReg = getSubRegIndex(Name);
880 if (!SubReg)
881 return error(Twine("use of unknown subregister index '") + Name + "'");
882 lex();
883 return false;
884}
885
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000886bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
887 if (!consumeIfPresent(MIToken::kw_tied_def))
888 return error("expected 'tied-def' after '('");
889 if (Token.isNot(MIToken::IntegerLiteral))
890 return error("expected an integer literal after 'tied-def'");
891 if (getUnsigned(TiedDefIdx))
892 return true;
893 lex();
894 if (expectAndConsume(MIToken::rparen))
895 return true;
896 return false;
897}
898
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000899bool MIParser::assignRegisterTies(MachineInstr &MI,
900 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000901 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
902 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
903 if (!Operands[I].TiedDefIdx)
904 continue;
905 // The parser ensures that this operand is a register use, so we just have
906 // to check the tied-def operand.
907 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
908 if (DefIdx >= E)
909 return error(Operands[I].Begin,
910 Twine("use of invalid tied-def operand index '" +
911 Twine(DefIdx) + "'; instruction has only ") +
912 Twine(E) + " operands");
913 const auto &DefOperand = Operands[DefIdx].Operand;
914 if (!DefOperand.isReg() || !DefOperand.isDef())
915 // FIXME: add note with the def operand.
916 return error(Operands[I].Begin,
917 Twine("use of invalid tied-def operand index '") +
918 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
919 " isn't a defined register");
920 // Check that the tied-def operand wasn't tied elsewhere.
921 for (const auto &TiedPair : TiedRegisterPairs) {
922 if (TiedPair.first == DefIdx)
923 return error(Operands[I].Begin,
924 Twine("the tied-def operand #") + Twine(DefIdx) +
925 " is already tied with another register operand");
926 }
927 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
928 }
929 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
930 // indices must be less than tied max.
931 for (const auto &TiedPair : TiedRegisterPairs)
932 MI.tieOperands(TiedPair.first, TiedPair.second);
933 return false;
934}
935
936bool MIParser::parseRegisterOperand(MachineOperand &Dest,
937 Optional<unsigned> &TiedDefIdx,
938 bool IsDef) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000939 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000940 unsigned Flags = IsDef ? RegState::Define : 0;
941 while (Token.isRegisterFlag()) {
942 if (parseRegisterFlag(Flags))
943 return true;
944 }
945 if (!Token.isRegister())
946 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000947 if (parseRegister(Reg))
948 return true;
949 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000950 unsigned SubReg = 0;
951 if (Token.is(MIToken::colon)) {
952 if (parseSubRegisterIndex(SubReg))
953 return true;
954 }
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000955 if ((Flags & RegState::Define) == 0 && consumeIfPresent(MIToken::lparen)) {
956 unsigned Idx;
957 if (parseRegisterTiedDefIndex(Idx))
958 return true;
959 TiedDefIdx = Idx;
960 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000961 Dest = MachineOperand::CreateReg(
962 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000963 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +0000964 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
965 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000966 return false;
967}
968
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000969bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
970 assert(Token.is(MIToken::IntegerLiteral));
971 const APSInt &Int = Token.integerValue();
972 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +0000973 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000974 Dest = MachineOperand::CreateImm(Int.getExtValue());
975 lex();
976 return false;
977}
978
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000979bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
Alex Lorenz3fb77682015-08-06 23:17:42 +0000980 auto Source = StringRef(Loc, Token.range().end() - Loc).str();
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000981 lex();
982 SMDiagnostic Err;
983 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
984 if (!C)
985 return error(Loc + Err.getColumnNo(), Err.getMessage());
986 return false;
987}
988
Alex Lorenz05e38822015-08-05 18:52:21 +0000989bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
990 assert(Token.is(MIToken::IntegerType));
991 auto Loc = Token.location();
992 lex();
993 if (Token.isNot(MIToken::IntegerLiteral))
994 return error("expected an integer literal");
995 const Constant *C = nullptr;
996 if (parseIRConstant(Loc, C))
997 return true;
998 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
999 return false;
1000}
1001
Alex Lorenzad156fb2015-07-31 20:49:21 +00001002bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1003 auto Loc = Token.location();
1004 lex();
1005 if (Token.isNot(MIToken::FloatingPointLiteral))
1006 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001007 const Constant *C = nullptr;
1008 if (parseIRConstant(Loc, C))
1009 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001010 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1011 return false;
1012}
1013
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001014bool MIParser::getUnsigned(unsigned &Result) {
1015 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1016 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1017 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1018 if (Val64 == Limit)
1019 return error("expected 32-bit integer (too large)");
1020 Result = Val64;
1021 return false;
1022}
1023
Alex Lorenzf09df002015-06-30 18:16:42 +00001024bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001025 assert(Token.is(MIToken::MachineBasicBlock) ||
1026 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001027 unsigned Number;
1028 if (getUnsigned(Number))
1029 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001030 auto MBBInfo = PFS.MBBSlots.find(Number);
1031 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001032 return error(Twine("use of undefined machine basic block #") +
1033 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001034 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001035 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1036 return error(Twine("the name of machine basic block #") + Twine(Number) +
1037 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001038 return false;
1039}
1040
1041bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1042 MachineBasicBlock *MBB;
1043 if (parseMBBReference(MBB))
1044 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001045 Dest = MachineOperand::CreateMBB(MBB);
1046 lex();
1047 return false;
1048}
1049
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001050bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001051 assert(Token.is(MIToken::StackObject));
1052 unsigned ID;
1053 if (getUnsigned(ID))
1054 return true;
1055 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1056 if (ObjectInfo == PFS.StackObjectSlots.end())
1057 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1058 "'");
1059 StringRef Name;
1060 if (const auto *Alloca =
1061 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
1062 Name = Alloca->getName();
1063 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1064 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1065 "' isn't '" + Token.stringValue() + "'");
1066 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001067 FI = ObjectInfo->second;
1068 return false;
1069}
1070
1071bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1072 int FI;
1073 if (parseStackFrameIndex(FI))
1074 return true;
1075 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001076 return false;
1077}
1078
Alex Lorenzea882122015-08-12 21:17:02 +00001079bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001080 assert(Token.is(MIToken::FixedStackObject));
1081 unsigned ID;
1082 if (getUnsigned(ID))
1083 return true;
1084 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1085 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1086 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1087 Twine(ID) + "'");
1088 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001089 FI = ObjectInfo->second;
1090 return false;
1091}
1092
1093bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1094 int FI;
1095 if (parseFixedStackFrameIndex(FI))
1096 return true;
1097 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001098 return false;
1099}
1100
Alex Lorenz41df7d32015-07-28 17:09:52 +00001101bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001102 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001103 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001104 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001105 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001106 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001107 return error(Twine("use of undefined global value '") + Token.range() +
1108 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001109 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001110 }
1111 case MIToken::GlobalValue: {
1112 unsigned GVIdx;
1113 if (getUnsigned(GVIdx))
1114 return true;
1115 if (GVIdx >= IRSlots.GlobalValues.size())
1116 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1117 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001118 GV = IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001119 break;
1120 }
1121 default:
1122 llvm_unreachable("The current token should be a global value");
1123 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001124 return false;
1125}
1126
1127bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1128 GlobalValue *GV = nullptr;
1129 if (parseGlobalValue(GV))
1130 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001131 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001132 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001133 if (parseOperandsOffset(Dest))
1134 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001135 return false;
1136}
1137
Alex Lorenzab980492015-07-20 20:51:18 +00001138bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1139 assert(Token.is(MIToken::ConstantPoolItem));
1140 unsigned ID;
1141 if (getUnsigned(ID))
1142 return true;
1143 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1144 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1145 return error("use of undefined constant '%const." + Twine(ID) + "'");
1146 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001147 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001148 if (parseOperandsOffset(Dest))
1149 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001150 return false;
1151}
1152
Alex Lorenz31d70682015-07-15 23:38:35 +00001153bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1154 assert(Token.is(MIToken::JumpTableIndex));
1155 unsigned ID;
1156 if (getUnsigned(ID))
1157 return true;
1158 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1159 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1160 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1161 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001162 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1163 return false;
1164}
1165
Alex Lorenz6ede3742015-07-21 16:59:53 +00001166bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001167 assert(Token.is(MIToken::ExternalSymbol));
1168 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001169 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001170 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001171 if (parseOperandsOffset(Dest))
1172 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001173 return false;
1174}
1175
Alex Lorenz44f29252015-07-22 21:07:04 +00001176bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001177 assert(Token.is(MIToken::exclaim));
1178 auto Loc = Token.location();
1179 lex();
1180 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1181 return error("expected metadata id after '!'");
1182 unsigned ID;
1183 if (getUnsigned(ID))
1184 return true;
1185 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
1186 if (NodeInfo == IRSlots.MetadataNodes.end())
1187 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1188 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001189 Node = NodeInfo->second.get();
1190 return false;
1191}
1192
1193bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1194 MDNode *Node = nullptr;
1195 if (parseMDNode(Node))
1196 return true;
1197 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001198 return false;
1199}
1200
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001201bool MIParser::parseCFIOffset(int &Offset) {
1202 if (Token.isNot(MIToken::IntegerLiteral))
1203 return error("expected a cfi offset");
1204 if (Token.integerValue().getMinSignedBits() > 32)
1205 return error("expected a 32 bit integer (the cfi offset is too large)");
1206 Offset = (int)Token.integerValue().getExtValue();
1207 lex();
1208 return false;
1209}
1210
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001211bool MIParser::parseCFIRegister(unsigned &Reg) {
1212 if (Token.isNot(MIToken::NamedRegister))
1213 return error("expected a cfi register");
1214 unsigned LLVMReg;
1215 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001216 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001217 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1218 assert(TRI && "Expected target register info");
1219 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1220 if (DwarfReg < 0)
1221 return error("invalid DWARF register");
1222 Reg = (unsigned)DwarfReg;
1223 lex();
1224 return false;
1225}
1226
1227bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1228 auto Kind = Token.kind();
1229 lex();
1230 auto &MMI = MF.getMMI();
1231 int Offset;
1232 unsigned Reg;
1233 unsigned CFIIndex;
1234 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001235 case MIToken::kw_cfi_same_value:
1236 if (parseCFIRegister(Reg))
1237 return true;
1238 CFIIndex =
1239 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1240 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001241 case MIToken::kw_cfi_offset:
1242 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1243 parseCFIOffset(Offset))
1244 return true;
1245 CFIIndex =
1246 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1247 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001248 case MIToken::kw_cfi_def_cfa_register:
1249 if (parseCFIRegister(Reg))
1250 return true;
1251 CFIIndex =
1252 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1253 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001254 case MIToken::kw_cfi_def_cfa_offset:
1255 if (parseCFIOffset(Offset))
1256 return true;
1257 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1258 CFIIndex = MMI.addFrameInst(
1259 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1260 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001261 case MIToken::kw_cfi_def_cfa:
1262 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1263 parseCFIOffset(Offset))
1264 return true;
1265 // NB: MCCFIInstruction::createDefCfa negates the offset.
1266 CFIIndex =
1267 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1268 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001269 default:
1270 // TODO: Parse the other CFI operands.
1271 llvm_unreachable("The current token should be a cfi operand");
1272 }
1273 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001274 return false;
1275}
1276
Alex Lorenzdeb53492015-07-28 17:28:03 +00001277bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1278 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001279 case MIToken::NamedIRBlock: {
1280 BB = dyn_cast_or_null<BasicBlock>(
1281 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001282 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001283 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001284 break;
1285 }
1286 case MIToken::IRBlock: {
1287 unsigned SlotNumber = 0;
1288 if (getUnsigned(SlotNumber))
1289 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001290 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001291 if (!BB)
1292 return error(Twine("use of undefined IR block '%ir-block.") +
1293 Twine(SlotNumber) + "'");
1294 break;
1295 }
1296 default:
1297 llvm_unreachable("The current token should be an IR block reference");
1298 }
1299 return false;
1300}
1301
1302bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1303 assert(Token.is(MIToken::kw_blockaddress));
1304 lex();
1305 if (expectAndConsume(MIToken::lparen))
1306 return true;
1307 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001308 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001309 return error("expected a global value");
1310 GlobalValue *GV = nullptr;
1311 if (parseGlobalValue(GV))
1312 return true;
1313 auto *F = dyn_cast<Function>(GV);
1314 if (!F)
1315 return error("expected an IR function reference");
1316 lex();
1317 if (expectAndConsume(MIToken::comma))
1318 return true;
1319 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001320 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001321 return error("expected an IR block reference");
1322 if (parseIRBlock(BB, *F))
1323 return true;
1324 lex();
1325 if (expectAndConsume(MIToken::rparen))
1326 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001327 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001328 if (parseOperandsOffset(Dest))
1329 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001330 return false;
1331}
1332
Alex Lorenzef5c1962015-07-28 23:02:45 +00001333bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1334 assert(Token.is(MIToken::kw_target_index));
1335 lex();
1336 if (expectAndConsume(MIToken::lparen))
1337 return true;
1338 if (Token.isNot(MIToken::Identifier))
1339 return error("expected the name of the target index");
1340 int Index = 0;
1341 if (getTargetIndex(Token.stringValue(), Index))
1342 return error("use of undefined target index '" + Token.stringValue() + "'");
1343 lex();
1344 if (expectAndConsume(MIToken::rparen))
1345 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001346 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001347 if (parseOperandsOffset(Dest))
1348 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001349 return false;
1350}
1351
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001352bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1353 assert(Token.is(MIToken::kw_liveout));
1354 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1355 assert(TRI && "Expected target register info");
1356 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1357 lex();
1358 if (expectAndConsume(MIToken::lparen))
1359 return true;
1360 while (true) {
1361 if (Token.isNot(MIToken::NamedRegister))
1362 return error("expected a named register");
1363 unsigned Reg = 0;
1364 if (parseRegister(Reg))
1365 return true;
1366 lex();
1367 Mask[Reg / 32] |= 1U << (Reg % 32);
1368 // TODO: Report an error if the same register is used more than once.
1369 if (Token.isNot(MIToken::comma))
1370 break;
1371 lex();
1372 }
1373 if (expectAndConsume(MIToken::rparen))
1374 return true;
1375 Dest = MachineOperand::CreateRegLiveOut(Mask);
1376 return false;
1377}
1378
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001379bool MIParser::parseMachineOperand(MachineOperand &Dest,
1380 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001381 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001382 case MIToken::kw_implicit:
1383 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001384 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001385 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001386 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001387 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001388 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001389 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001390 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001391 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001392 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001393 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001394 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001395 case MIToken::IntegerLiteral:
1396 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001397 case MIToken::IntegerType:
1398 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001399 case MIToken::kw_half:
1400 case MIToken::kw_float:
1401 case MIToken::kw_double:
1402 case MIToken::kw_x86_fp80:
1403 case MIToken::kw_fp128:
1404 case MIToken::kw_ppc_fp128:
1405 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001406 case MIToken::MachineBasicBlock:
1407 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001408 case MIToken::StackObject:
1409 return parseStackObjectOperand(Dest);
1410 case MIToken::FixedStackObject:
1411 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001412 case MIToken::GlobalValue:
1413 case MIToken::NamedGlobalValue:
1414 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001415 case MIToken::ConstantPoolItem:
1416 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001417 case MIToken::JumpTableIndex:
1418 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001419 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001420 return parseExternalSymbolOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001421 case MIToken::exclaim:
1422 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001423 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001424 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001425 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001426 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001427 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001428 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001429 case MIToken::kw_blockaddress:
1430 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001431 case MIToken::kw_target_index:
1432 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001433 case MIToken::kw_liveout:
1434 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001435 case MIToken::Error:
1436 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001437 case MIToken::Identifier:
1438 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1439 Dest = MachineOperand::CreateRegMask(RegMask);
1440 lex();
1441 break;
1442 }
1443 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001444 default:
1445 // TODO: parse the other machine operands.
1446 return error("expected a machine operand");
1447 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001448 return false;
1449}
1450
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001451bool MIParser::parseMachineOperandAndTargetFlags(
1452 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001453 unsigned TF = 0;
1454 bool HasTargetFlags = false;
1455 if (Token.is(MIToken::kw_target_flags)) {
1456 HasTargetFlags = true;
1457 lex();
1458 if (expectAndConsume(MIToken::lparen))
1459 return true;
1460 if (Token.isNot(MIToken::Identifier))
1461 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001462 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1463 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1464 return error("use of undefined target flag '" + Token.stringValue() +
1465 "'");
1466 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001467 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001468 while (Token.is(MIToken::comma)) {
1469 lex();
1470 if (Token.isNot(MIToken::Identifier))
1471 return error("expected the name of the target flag");
1472 unsigned BitFlag = 0;
1473 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1474 return error("use of undefined target flag '" + Token.stringValue() +
1475 "'");
1476 // TODO: Report an error when using a duplicate bit target flag.
1477 TF |= BitFlag;
1478 lex();
1479 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001480 if (expectAndConsume(MIToken::rparen))
1481 return true;
1482 }
1483 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001484 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001485 return true;
1486 if (!HasTargetFlags)
1487 return false;
1488 if (Dest.isReg())
1489 return error(Loc, "register operands can't have target flags");
1490 Dest.setTargetFlags(TF);
1491 return false;
1492}
1493
Alex Lorenzdc24c172015-08-07 20:21:00 +00001494bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001495 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1496 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001497 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001498 bool IsNegative = Token.is(MIToken::minus);
1499 lex();
1500 if (Token.isNot(MIToken::IntegerLiteral))
1501 return error("expected an integer literal after '" + Sign + "'");
1502 if (Token.integerValue().getMinSignedBits() > 64)
1503 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001504 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001505 if (IsNegative)
1506 Offset = -Offset;
1507 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001508 return false;
1509}
1510
Alex Lorenz620f8912015-08-13 20:33:33 +00001511bool MIParser::parseAlignment(unsigned &Alignment) {
1512 assert(Token.is(MIToken::kw_align));
1513 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001514 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001515 return error("expected an integer literal after 'align'");
1516 if (getUnsigned(Alignment))
1517 return true;
1518 lex();
1519 return false;
1520}
1521
Alex Lorenzdc24c172015-08-07 20:21:00 +00001522bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1523 int64_t Offset = 0;
1524 if (parseOffset(Offset))
1525 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001526 Op.setOffset(Offset);
1527 return false;
1528}
1529
Alex Lorenz36593ac2015-08-19 23:27:07 +00001530bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001531 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001532 case MIToken::NamedIRValue: {
1533 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001534 if (!V)
Alex Lorenz2791dcc2015-08-12 21:27:16 +00001535 V = MF.getFunction()->getParent()->getValueSymbolTable().lookup(
1536 Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001537 break;
1538 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001539 case MIToken::IRValue: {
1540 unsigned SlotNumber = 0;
1541 if (getUnsigned(SlotNumber))
1542 return true;
1543 V = getIRValue(SlotNumber);
1544 break;
1545 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001546 default:
1547 llvm_unreachable("The current token should be an IR block reference");
1548 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001549 if (!V)
1550 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001551 return false;
1552}
1553
1554bool MIParser::getUint64(uint64_t &Result) {
1555 assert(Token.hasIntegerValue());
1556 if (Token.integerValue().getActiveBits() > 64)
1557 return error("expected 64-bit integer (too large)");
1558 Result = Token.integerValue().getZExtValue();
1559 return false;
1560}
1561
Alex Lorenza518b792015-08-04 00:24:45 +00001562bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
Alex Lorenze86d5152015-08-06 18:26:36 +00001563 const unsigned OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001564 switch (Token.kind()) {
1565 case MIToken::kw_volatile:
1566 Flags |= MachineMemOperand::MOVolatile;
1567 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001568 case MIToken::kw_non_temporal:
1569 Flags |= MachineMemOperand::MONonTemporal;
1570 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001571 case MIToken::kw_invariant:
1572 Flags |= MachineMemOperand::MOInvariant;
1573 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001574 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001575 default:
1576 llvm_unreachable("The current token should be a memory operand flag");
1577 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001578 if (OldFlags == Flags)
1579 // We know that the same flag is specified more than once when the flags
1580 // weren't modified.
1581 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001582 lex();
1583 return false;
1584}
1585
Alex Lorenz91097a32015-08-12 20:33:26 +00001586bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1587 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001588 case MIToken::kw_stack:
1589 PSV = MF.getPSVManager().getStack();
1590 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001591 case MIToken::kw_got:
1592 PSV = MF.getPSVManager().getGOT();
1593 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001594 case MIToken::kw_jump_table:
1595 PSV = MF.getPSVManager().getJumpTable();
1596 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001597 case MIToken::kw_constant_pool:
1598 PSV = MF.getPSVManager().getConstantPool();
1599 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001600 case MIToken::FixedStackObject: {
1601 int FI;
1602 if (parseFixedStackFrameIndex(FI))
1603 return true;
1604 PSV = MF.getPSVManager().getFixedStack(FI);
1605 // The token was already consumed, so use return here instead of break.
1606 return false;
1607 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001608 case MIToken::kw_call_entry: {
1609 lex();
1610 switch (Token.kind()) {
1611 case MIToken::GlobalValue:
1612 case MIToken::NamedGlobalValue: {
1613 GlobalValue *GV = nullptr;
1614 if (parseGlobalValue(GV))
1615 return true;
1616 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1617 break;
1618 }
1619 case MIToken::ExternalSymbol:
1620 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1621 MF.createExternalSymbolName(Token.stringValue()));
1622 break;
1623 default:
1624 return error(
1625 "expected a global value or an external symbol after 'call-entry'");
1626 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001627 break;
1628 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001629 default:
1630 llvm_unreachable("The current token should be pseudo source value");
1631 }
1632 lex();
1633 return false;
1634}
1635
1636bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001637 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001638 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Alex Lorenz0d009642015-08-20 00:12:57 +00001639 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001640 const PseudoSourceValue *PSV = nullptr;
1641 if (parseMemoryPseudoSourceValue(PSV))
1642 return true;
1643 int64_t Offset = 0;
1644 if (parseOffset(Offset))
1645 return true;
1646 Dest = MachinePointerInfo(PSV, Offset);
1647 return false;
1648 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001649 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001650 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001651 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001652 if (parseIRValue(V))
1653 return true;
1654 if (!V->getType()->isPointerTy())
1655 return error("expected a pointer IR value");
1656 lex();
1657 int64_t Offset = 0;
1658 if (parseOffset(Offset))
1659 return true;
1660 Dest = MachinePointerInfo(V, Offset);
1661 return false;
1662}
1663
Alex Lorenz4af7e612015-08-03 23:08:19 +00001664bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1665 if (expectAndConsume(MIToken::lparen))
1666 return true;
Alex Lorenza518b792015-08-04 00:24:45 +00001667 unsigned Flags = 0;
1668 while (Token.isMemoryOperandFlag()) {
1669 if (parseMemoryOperandFlag(Flags))
1670 return true;
1671 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001672 if (Token.isNot(MIToken::Identifier) ||
1673 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1674 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001675 if (Token.stringValue() == "load")
1676 Flags |= MachineMemOperand::MOLoad;
1677 else
1678 Flags |= MachineMemOperand::MOStore;
1679 lex();
1680
1681 if (Token.isNot(MIToken::IntegerLiteral))
1682 return error("expected the size integer literal after memory operation");
1683 uint64_t Size;
1684 if (getUint64(Size))
1685 return true;
1686 lex();
1687
1688 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1689 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1690 return error(Twine("expected '") + Word + "'");
1691 lex();
1692
Alex Lorenz91097a32015-08-12 20:33:26 +00001693 MachinePointerInfo Ptr = MachinePointerInfo();
1694 if (parseMachinePointerInfo(Ptr))
Alex Lorenz83127732015-08-07 20:26:52 +00001695 return true;
Alex Lorenz61420f72015-08-07 20:48:30 +00001696 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001697 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001698 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001699 while (consumeIfPresent(MIToken::comma)) {
1700 switch (Token.kind()) {
1701 case MIToken::kw_align:
1702 if (parseAlignment(BaseAlignment))
1703 return true;
1704 break;
1705 case MIToken::md_tbaa:
1706 lex();
1707 if (parseMDNode(AAInfo.TBAA))
1708 return true;
1709 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001710 case MIToken::md_alias_scope:
1711 lex();
1712 if (parseMDNode(AAInfo.Scope))
1713 return true;
1714 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001715 case MIToken::md_noalias:
1716 lex();
1717 if (parseMDNode(AAInfo.NoAlias))
1718 return true;
1719 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001720 case MIToken::md_range:
1721 lex();
1722 if (parseMDNode(Range))
1723 return true;
1724 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001725 // TODO: Report an error on duplicate metadata nodes.
1726 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001727 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1728 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001729 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001730 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001731 if (expectAndConsume(MIToken::rparen))
1732 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001733 Dest =
1734 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001735 return false;
1736}
1737
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001738void MIParser::initNames2InstrOpCodes() {
1739 if (!Names2InstrOpCodes.empty())
1740 return;
1741 const auto *TII = MF.getSubtarget().getInstrInfo();
1742 assert(TII && "Expected target instruction info");
1743 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1744 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1745}
1746
1747bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1748 initNames2InstrOpCodes();
1749 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1750 if (InstrInfo == Names2InstrOpCodes.end())
1751 return true;
1752 OpCode = InstrInfo->getValue();
1753 return false;
1754}
1755
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001756void MIParser::initNames2Regs() {
1757 if (!Names2Regs.empty())
1758 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001759 // The '%noreg' register is the register 0.
1760 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001761 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1762 assert(TRI && "Expected target register info");
1763 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1764 bool WasInserted =
1765 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1766 .second;
1767 (void)WasInserted;
1768 assert(WasInserted && "Expected registers to be unique case-insensitively");
1769 }
1770}
1771
1772bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1773 initNames2Regs();
1774 auto RegInfo = Names2Regs.find(RegName);
1775 if (RegInfo == Names2Regs.end())
1776 return true;
1777 Reg = RegInfo->getValue();
1778 return false;
1779}
1780
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001781void MIParser::initNames2RegMasks() {
1782 if (!Names2RegMasks.empty())
1783 return;
1784 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1785 assert(TRI && "Expected target register info");
1786 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1787 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1788 assert(RegMasks.size() == RegMaskNames.size());
1789 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1790 Names2RegMasks.insert(
1791 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1792}
1793
1794const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1795 initNames2RegMasks();
1796 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1797 if (RegMaskInfo == Names2RegMasks.end())
1798 return nullptr;
1799 return RegMaskInfo->getValue();
1800}
1801
Alex Lorenz2eacca82015-07-13 23:24:34 +00001802void MIParser::initNames2SubRegIndices() {
1803 if (!Names2SubRegIndices.empty())
1804 return;
1805 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1806 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1807 Names2SubRegIndices.insert(
1808 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1809}
1810
1811unsigned MIParser::getSubRegIndex(StringRef Name) {
1812 initNames2SubRegIndices();
1813 auto SubRegInfo = Names2SubRegIndices.find(Name);
1814 if (SubRegInfo == Names2SubRegIndices.end())
1815 return 0;
1816 return SubRegInfo->getValue();
1817}
1818
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001819static void initSlots2BasicBlocks(
1820 const Function &F,
1821 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1822 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001823 MST.incorporateFunction(F);
1824 for (auto &BB : F) {
1825 if (BB.hasName())
1826 continue;
1827 int Slot = MST.getLocalSlot(&BB);
1828 if (Slot == -1)
1829 continue;
1830 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1831 }
1832}
1833
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001834static const BasicBlock *getIRBlockFromSlot(
1835 unsigned Slot,
1836 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001837 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1838 if (BlockInfo == Slots2BasicBlocks.end())
1839 return nullptr;
1840 return BlockInfo->second;
1841}
1842
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001843const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1844 if (Slots2BasicBlocks.empty())
1845 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1846 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1847}
1848
1849const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1850 if (&F == MF.getFunction())
1851 return getIRBlock(Slot);
1852 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1853 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1854 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1855}
1856
Alex Lorenzdd13be02015-08-19 23:31:05 +00001857static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
1858 DenseMap<unsigned, const Value *> &Slots2Values) {
1859 int Slot = MST.getLocalSlot(V);
1860 if (Slot == -1)
1861 return;
1862 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
1863}
1864
1865/// Creates the mapping from slot numbers to function's unnamed IR values.
1866static void initSlots2Values(const Function &F,
1867 DenseMap<unsigned, const Value *> &Slots2Values) {
1868 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1869 MST.incorporateFunction(F);
1870 for (const auto &Arg : F.args())
1871 mapValueToSlot(&Arg, MST, Slots2Values);
1872 for (const auto &BB : F) {
1873 mapValueToSlot(&BB, MST, Slots2Values);
1874 for (const auto &I : BB)
1875 mapValueToSlot(&I, MST, Slots2Values);
1876 }
1877}
1878
1879const Value *MIParser::getIRValue(unsigned Slot) {
1880 if (Slots2Values.empty())
1881 initSlots2Values(*MF.getFunction(), Slots2Values);
1882 auto ValueInfo = Slots2Values.find(Slot);
1883 if (ValueInfo == Slots2Values.end())
1884 return nullptr;
1885 return ValueInfo->second;
1886}
1887
Alex Lorenzef5c1962015-07-28 23:02:45 +00001888void MIParser::initNames2TargetIndices() {
1889 if (!Names2TargetIndices.empty())
1890 return;
1891 const auto *TII = MF.getSubtarget().getInstrInfo();
1892 assert(TII && "Expected target instruction info");
1893 auto Indices = TII->getSerializableTargetIndices();
1894 for (const auto &I : Indices)
1895 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1896}
1897
1898bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1899 initNames2TargetIndices();
1900 auto IndexInfo = Names2TargetIndices.find(Name);
1901 if (IndexInfo == Names2TargetIndices.end())
1902 return true;
1903 Index = IndexInfo->second;
1904 return false;
1905}
1906
Alex Lorenz49873a82015-08-06 00:44:07 +00001907void MIParser::initNames2DirectTargetFlags() {
1908 if (!Names2DirectTargetFlags.empty())
1909 return;
1910 const auto *TII = MF.getSubtarget().getInstrInfo();
1911 assert(TII && "Expected target instruction info");
1912 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
1913 for (const auto &I : Flags)
1914 Names2DirectTargetFlags.insert(
1915 std::make_pair(StringRef(I.second), I.first));
1916}
1917
1918bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
1919 initNames2DirectTargetFlags();
1920 auto FlagInfo = Names2DirectTargetFlags.find(Name);
1921 if (FlagInfo == Names2DirectTargetFlags.end())
1922 return true;
1923 Flag = FlagInfo->second;
1924 return false;
1925}
1926
Alex Lorenzf3630112015-08-18 22:52:15 +00001927void MIParser::initNames2BitmaskTargetFlags() {
1928 if (!Names2BitmaskTargetFlags.empty())
1929 return;
1930 const auto *TII = MF.getSubtarget().getInstrInfo();
1931 assert(TII && "Expected target instruction info");
1932 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
1933 for (const auto &I : Flags)
1934 Names2BitmaskTargetFlags.insert(
1935 std::make_pair(StringRef(I.second), I.first));
1936}
1937
1938bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
1939 initNames2BitmaskTargetFlags();
1940 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
1941 if (FlagInfo == Names2BitmaskTargetFlags.end())
1942 return true;
1943 Flag = FlagInfo->second;
1944 return false;
1945}
1946
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001947bool llvm::parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src,
1948 PerFunctionMIParsingState &PFS,
1949 const SlotMapping &IRSlots,
1950 SMDiagnostic &Error) {
1951 SourceMgr SM;
1952 SM.AddNewSourceBuffer(
1953 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1954 SMLoc());
1955 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1956 .parseBasicBlockDefinitions(PFS.MBBSlots);
1957}
1958
1959bool llvm::parseMachineInstructions(MachineFunction &MF, StringRef Src,
1960 const PerFunctionMIParsingState &PFS,
1961 const SlotMapping &IRSlots,
1962 SMDiagnostic &Error) {
1963 SourceMgr SM;
1964 SM.AddNewSourceBuffer(
1965 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
1966 SMLoc());
1967 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001968}
Alex Lorenzf09df002015-06-30 18:16:42 +00001969
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001970bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
1971 MachineFunction &MF, StringRef Src,
1972 const PerFunctionMIParsingState &PFS,
1973 const SlotMapping &IRSlots, SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001974 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00001975}
Alex Lorenz9fab3702015-07-14 21:24:41 +00001976
1977bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
1978 MachineFunction &MF, StringRef Src,
1979 const PerFunctionMIParsingState &PFS,
1980 const SlotMapping &IRSlots,
1981 SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00001982 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1983 .parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00001984}
Alex Lorenz12045a42015-07-27 17:42:45 +00001985
1986bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
1987 MachineFunction &MF, StringRef Src,
1988 const PerFunctionMIParsingState &PFS,
1989 const SlotMapping &IRSlots,
1990 SMDiagnostic &Error) {
1991 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
1992 .parseStandaloneVirtualRegister(Reg);
1993}
Alex Lorenza314d812015-08-18 22:26:26 +00001994
1995bool llvm::parseStackObjectReference(int &FI, SourceMgr &SM,
1996 MachineFunction &MF, StringRef Src,
1997 const PerFunctionMIParsingState &PFS,
1998 const SlotMapping &IRSlots,
1999 SMDiagnostic &Error) {
2000 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2001 .parseStandaloneStackObject(FI);
2002}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002003
2004bool llvm::parseMDNode(MDNode *&Node, SourceMgr &SM, MachineFunction &MF,
2005 StringRef Src, const PerFunctionMIParsingState &PFS,
2006 const SlotMapping &IRSlots, SMDiagnostic &Error) {
2007 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMDNode(Node);
2008}