blob: 8c7345111c58e0ac47ee03bb3a9cc5596c30db6e [file] [log] [blame]
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001//===- MIParser.cpp - Machine instructions parser implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the parsing of machine instructions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MIParser.h"
Alex Lorenz91370c52015-06-22 20:37:46 +000015#include "MILexer.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000016#include "llvm/ADT/StringMap.h"
Alex Lorenzad156fb2015-07-31 20:49:21 +000017#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000018#include "llvm/AsmParser/SlotMapping.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000019#include "llvm/CodeGen/MachineBasicBlock.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000021#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000022#include "llvm/CodeGen/MachineInstr.h"
Alex Lorenzcb268d42015-07-06 23:07:26 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000024#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000027#include "llvm/IR/Constants.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000028#include "llvm/IR/Instructions.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000029#include "llvm/IR/Module.h"
Alex Lorenz8a1915b2015-07-27 22:42:41 +000030#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000031#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000032#include "llvm/Support/SourceMgr.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000033#include "llvm/Support/raw_ostream.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000034#include "llvm/Target/TargetInstrInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000035#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000036
37using namespace llvm;
38
39namespace {
40
Alex Lorenz36962cd2015-07-07 02:08:46 +000041/// A wrapper struct around the 'MachineOperand' struct that includes a source
Alex Lorenzfeb6b432015-08-19 19:19:16 +000042/// range and other attributes.
43struct ParsedMachineOperand {
Alex Lorenz36962cd2015-07-07 02:08:46 +000044 MachineOperand Operand;
45 StringRef::iterator Begin;
46 StringRef::iterator End;
Alex Lorenz5ef93b02015-08-19 19:05:34 +000047 Optional<unsigned> TiedDefIdx;
Alex Lorenz36962cd2015-07-07 02:08:46 +000048
Alex Lorenzfeb6b432015-08-19 19:19:16 +000049 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
50 StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
Alex Lorenz5ef93b02015-08-19 19:05:34 +000051 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
52 if (TiedDefIdx)
53 assert(Operand.isReg() && Operand.isUse() &&
54 "Only used register operands can be tied");
55 }
Alex Lorenz36962cd2015-07-07 02:08:46 +000056};
57
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000058class MIParser {
59 SourceMgr &SM;
60 MachineFunction &MF;
61 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000062 StringRef Source, CurrentSource;
63 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000064 const PerFunctionMIParsingState &PFS;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000065 /// Maps from indices to unnamed global values and metadata nodes.
66 const SlotMapping &IRSlots;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000067 /// Maps from instruction names to op codes.
68 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000069 /// Maps from register names to registers.
70 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000071 /// Maps from register mask names to register masks.
72 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000073 /// Maps from subregister names to subregister indices.
74 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000075 /// Maps from slot numbers to function's unnamed basic blocks.
76 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzdd13be02015-08-19 23:31:05 +000077 /// Maps from slot numbers to function's unnamed values.
78 DenseMap<unsigned, const Value *> Slots2Values;
Alex Lorenzef5c1962015-07-28 23:02:45 +000079 /// Maps from target index names to target indices.
80 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +000081 /// Maps from direct target flag names to the direct target flag values.
82 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenzf3630112015-08-18 22:52:15 +000083 /// Maps from direct target flag names to the bitmask target flag values.
84 StringMap<unsigned> Names2BitmaskTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000085
86public:
87 MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +000088 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000089 const SlotMapping &IRSlots);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000090
Quentin Colombet287c6bb2016-03-08 00:57:31 +000091 /// \p SkipChar gives the number of characters to skip before looking
92 /// for the next token.
93 void lex(unsigned SkipChar = 0);
Alex Lorenz91370c52015-06-22 20:37:46 +000094
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000095 /// Report an error at the current location with the given message.
96 ///
97 /// This function always return true.
98 bool error(const Twine &Msg);
99
Alex Lorenz91370c52015-06-22 20:37:46 +0000100 /// Report an error at the given location with the given message.
101 ///
102 /// This function always return true.
103 bool error(StringRef::iterator Loc, const Twine &Msg);
104
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000105 bool
106 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
107 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +0000108 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +0000109 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
110 bool parseStandaloneNamedRegister(unsigned &Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +0000111 bool parseStandaloneVirtualRegister(unsigned &Reg);
Alex Lorenza314d812015-08-18 22:26:26 +0000112 bool parseStandaloneStackObject(int &FI);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000113 bool parseStandaloneMDNode(MDNode *&Node);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000114
115 bool
116 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
117 bool parseBasicBlock(MachineBasicBlock &MBB);
118 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
119 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000120
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000121 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000122 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000123 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000124 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000125 bool parseSize(unsigned &Size);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000126 bool parseRegisterOperand(MachineOperand &Dest,
127 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000128 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000129 bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
130 const Constant *&C);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000131 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000132 bool parseIRType(StringRef::iterator Loc, StringRef Source, unsigned &Read,
133 Type *&Ty);
134 // \p MustBeSized defines whether or not \p Ty must be sized.
135 bool parseIRType(StringRef::iterator Loc, Type *&Ty, bool MustBeSized = true);
Alex Lorenz05e38822015-08-05 18:52:21 +0000136 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000137 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000138 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000139 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000140 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000141 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000142 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000143 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000144 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000145 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000146 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +0000147 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000148 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000149 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000150 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000151 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000152 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000153 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000154 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000155 bool parseIRBlock(BasicBlock *&BB, const Function &F);
156 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000157 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000158 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000159 bool parseMachineOperand(MachineOperand &Dest,
160 Optional<unsigned> &TiedDefIdx);
161 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
162 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000163 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000164 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000165 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000166 bool parseIRValue(const Value *&V);
Alex Lorenza518b792015-08-04 00:24:45 +0000167 bool parseMemoryOperandFlag(unsigned &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000168 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
169 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000170 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000171
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000172private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000173 /// Convert the integer literal in the current token into an unsigned integer.
174 ///
175 /// Return true if an error occurred.
176 bool getUnsigned(unsigned &Result);
177
Alex Lorenz4af7e612015-08-03 23:08:19 +0000178 /// Convert the integer literal in the current token into an uint64.
179 ///
180 /// Return true if an error occurred.
181 bool getUint64(uint64_t &Result);
182
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000183 /// If the current token is of the given kind, consume it and return false.
184 /// Otherwise report an error and return true.
185 bool expectAndConsume(MIToken::TokenKind TokenKind);
186
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000187 /// If the current token is of the given kind, consume it and return true.
188 /// Otherwise return false.
189 bool consumeIfPresent(MIToken::TokenKind TokenKind);
190
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000191 void initNames2InstrOpCodes();
192
193 /// Try to convert an instruction name to an opcode. Return true if the
194 /// instruction name is invalid.
195 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000196
Alex Lorenze5a44662015-07-17 00:24:15 +0000197 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000198
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000199 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000200 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000201
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000202 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000203 const MCInstrDesc &MCID);
204
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000205 void initNames2Regs();
206
207 /// Try to convert a register name to a register number. Return true if the
208 /// register name is invalid.
209 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000210
211 void initNames2RegMasks();
212
213 /// Check if the given identifier is a name of a register mask.
214 ///
215 /// Return null if the identifier isn't a register mask.
216 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000217
218 void initNames2SubRegIndices();
219
220 /// Check if the given identifier is a name of a subregister index.
221 ///
222 /// Return 0 if the name isn't a subregister index class.
223 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000224
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000225 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000226 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000227
Alex Lorenzdd13be02015-08-19 23:31:05 +0000228 const Value *getIRValue(unsigned Slot);
229
Alex Lorenzef5c1962015-07-28 23:02:45 +0000230 void initNames2TargetIndices();
231
232 /// Try to convert a name of target index to the corresponding target index.
233 ///
234 /// Return true if the name isn't a name of a target index.
235 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000236
237 void initNames2DirectTargetFlags();
238
239 /// Try to convert a name of a direct target flag to the corresponding
240 /// target flag.
241 ///
242 /// Return true if the name isn't a name of a direct flag.
243 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000244
245 void initNames2BitmaskTargetFlags();
246
247 /// Try to convert a name of a bitmask target flag to the corresponding
248 /// target flag.
249 ///
250 /// Return true if the name isn't a name of a bitmask target flag.
251 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000252};
253
254} // end anonymous namespace
255
256MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000257 StringRef Source, const PerFunctionMIParsingState &PFS,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000258 const SlotMapping &IRSlots)
Alex Lorenz91370c52015-06-22 20:37:46 +0000259 : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
Alex Lorenz3fb77682015-08-06 23:17:42 +0000260 PFS(PFS), IRSlots(IRSlots) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000261
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000262void MIParser::lex(unsigned SkipChar) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000263 CurrentSource = lexMIToken(
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000264 CurrentSource.data() + SkipChar, Token,
Alex Lorenz91370c52015-06-22 20:37:46 +0000265 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
266}
267
268bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
269
270bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000271 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000272 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
273 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
274 // Create an ordinary diagnostic when the source manager's buffer is the
275 // source string.
276 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
277 return true;
278 }
279 // Create a diagnostic for a YAML string literal.
280 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
281 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
282 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000283 return true;
284}
285
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000286static const char *toString(MIToken::TokenKind TokenKind) {
287 switch (TokenKind) {
288 case MIToken::comma:
289 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000290 case MIToken::equal:
291 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000292 case MIToken::colon:
293 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000294 case MIToken::lparen:
295 return "'('";
296 case MIToken::rparen:
297 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000298 default:
299 return "<unknown token>";
300 }
301}
302
303bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
304 if (Token.isNot(TokenKind))
305 return error(Twine("expected ") + toString(TokenKind));
306 lex();
307 return false;
308}
309
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000310bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
311 if (Token.isNot(TokenKind))
312 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000313 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000314 return true;
315}
Alex Lorenz91370c52015-06-22 20:37:46 +0000316
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000317bool MIParser::parseBasicBlockDefinition(
318 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
319 assert(Token.is(MIToken::MachineBasicBlockLabel));
320 unsigned ID = 0;
321 if (getUnsigned(ID))
322 return true;
323 auto Loc = Token.location();
324 auto Name = Token.stringValue();
325 lex();
326 bool HasAddressTaken = false;
327 bool IsLandingPad = false;
328 unsigned Alignment = 0;
329 BasicBlock *BB = nullptr;
330 if (consumeIfPresent(MIToken::lparen)) {
331 do {
332 // TODO: Report an error when multiple same attributes are specified.
333 switch (Token.kind()) {
334 case MIToken::kw_address_taken:
335 HasAddressTaken = true;
336 lex();
337 break;
338 case MIToken::kw_landing_pad:
339 IsLandingPad = true;
340 lex();
341 break;
342 case MIToken::kw_align:
343 if (parseAlignment(Alignment))
344 return true;
345 break;
346 case MIToken::IRBlock:
347 // TODO: Report an error when both name and ir block are specified.
348 if (parseIRBlock(BB, *MF.getFunction()))
349 return true;
350 lex();
351 break;
352 default:
353 break;
354 }
355 } while (consumeIfPresent(MIToken::comma));
356 if (expectAndConsume(MIToken::rparen))
357 return true;
358 }
359 if (expectAndConsume(MIToken::colon))
360 return true;
361
362 if (!Name.empty()) {
363 BB = dyn_cast_or_null<BasicBlock>(
364 MF.getFunction()->getValueSymbolTable().lookup(Name));
365 if (!BB)
366 return error(Loc, Twine("basic block '") + Name +
367 "' is not defined in the function '" +
368 MF.getName() + "'");
369 }
370 auto *MBB = MF.CreateMachineBasicBlock(BB);
371 MF.insert(MF.end(), MBB);
372 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
373 if (!WasInserted)
374 return error(Loc, Twine("redefinition of machine basic block with id #") +
375 Twine(ID));
376 if (Alignment)
377 MBB->setAlignment(Alignment);
378 if (HasAddressTaken)
379 MBB->setHasAddressTaken();
Reid Kleckner0e288232015-08-27 23:27:47 +0000380 MBB->setIsEHPad(IsLandingPad);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000381 return false;
382}
383
384bool MIParser::parseBasicBlockDefinitions(
385 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
386 lex();
387 // Skip until the first machine basic block.
388 while (Token.is(MIToken::Newline))
389 lex();
390 if (Token.isErrorOrEOF())
391 return Token.isError();
392 if (Token.isNot(MIToken::MachineBasicBlockLabel))
393 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000394 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000395 do {
396 if (parseBasicBlockDefinition(MBBSlots))
397 return true;
398 bool IsAfterNewline = false;
399 // Skip until the next machine basic block.
400 while (true) {
401 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
402 Token.isErrorOrEOF())
403 break;
404 else if (Token.is(MIToken::MachineBasicBlockLabel))
405 return error("basic block definition should be located at the start of "
406 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000407 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000408 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000409 continue;
410 }
411 IsAfterNewline = false;
412 if (Token.is(MIToken::lbrace))
413 ++BraceDepth;
414 if (Token.is(MIToken::rbrace)) {
415 if (!BraceDepth)
416 return error("extraneous closing brace ('}')");
417 --BraceDepth;
418 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000419 lex();
420 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000421 // Verify that we closed all of the '{' at the end of a file or a block.
422 if (!Token.isError() && BraceDepth)
423 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000424 } while (!Token.isErrorOrEOF());
425 return Token.isError();
426}
427
428bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
429 assert(Token.is(MIToken::kw_liveins));
430 lex();
431 if (expectAndConsume(MIToken::colon))
432 return true;
433 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
434 return false;
435 do {
436 if (Token.isNot(MIToken::NamedRegister))
437 return error("expected a named register");
438 unsigned Reg = 0;
439 if (parseRegister(Reg))
440 return true;
441 MBB.addLiveIn(Reg);
442 lex();
443 } while (consumeIfPresent(MIToken::comma));
444 return false;
445}
446
447bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
448 assert(Token.is(MIToken::kw_successors));
449 lex();
450 if (expectAndConsume(MIToken::colon))
451 return true;
452 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
453 return false;
454 do {
455 if (Token.isNot(MIToken::MachineBasicBlock))
456 return error("expected a machine basic block reference");
457 MachineBasicBlock *SuccMBB = nullptr;
458 if (parseMBBReference(SuccMBB))
459 return true;
460 lex();
461 unsigned Weight = 0;
462 if (consumeIfPresent(MIToken::lparen)) {
463 if (Token.isNot(MIToken::IntegerLiteral))
464 return error("expected an integer literal after '('");
465 if (getUnsigned(Weight))
466 return true;
467 lex();
468 if (expectAndConsume(MIToken::rparen))
469 return true;
470 }
Cong Houd97c1002015-12-01 05:29:22 +0000471 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000472 } while (consumeIfPresent(MIToken::comma));
Cong Houd97c1002015-12-01 05:29:22 +0000473 MBB.normalizeSuccProbs();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000474 return false;
475}
476
477bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
478 // Skip the definition.
479 assert(Token.is(MIToken::MachineBasicBlockLabel));
480 lex();
481 if (consumeIfPresent(MIToken::lparen)) {
482 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
483 lex();
484 consumeIfPresent(MIToken::rparen);
485 }
486 consumeIfPresent(MIToken::colon);
487
488 // Parse the liveins and successors.
489 // N.B: Multiple lists of successors and liveins are allowed and they're
490 // merged into one.
491 // Example:
492 // liveins: %edi
493 // liveins: %esi
494 //
495 // is equivalent to
496 // liveins: %edi, %esi
497 while (true) {
498 if (Token.is(MIToken::kw_successors)) {
499 if (parseBasicBlockSuccessors(MBB))
500 return true;
501 } else if (Token.is(MIToken::kw_liveins)) {
502 if (parseBasicBlockLiveins(MBB))
503 return true;
504 } else if (consumeIfPresent(MIToken::Newline)) {
505 continue;
506 } else
507 break;
508 if (!Token.isNewlineOrEOF())
509 return error("expected line break at the end of a list");
510 lex();
511 }
512
513 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000514 bool IsInBundle = false;
515 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000516 while (true) {
517 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
518 return false;
519 else if (consumeIfPresent(MIToken::Newline))
520 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000521 if (consumeIfPresent(MIToken::rbrace)) {
522 // The first parsing pass should verify that all closing '}' have an
523 // opening '{'.
524 assert(IsInBundle);
525 IsInBundle = false;
526 continue;
527 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000528 MachineInstr *MI = nullptr;
529 if (parse(MI))
530 return true;
531 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000532 if (IsInBundle) {
533 PrevMI->setFlag(MachineInstr::BundledSucc);
534 MI->setFlag(MachineInstr::BundledPred);
535 }
536 PrevMI = MI;
537 if (Token.is(MIToken::lbrace)) {
538 if (IsInBundle)
539 return error("nested instruction bundles are not allowed");
540 lex();
541 // This instruction is the start of the bundle.
542 MI->setFlag(MachineInstr::BundledSucc);
543 IsInBundle = true;
544 if (!Token.is(MIToken::Newline))
545 // The next instruction can be on the same line.
546 continue;
547 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000548 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
549 lex();
550 }
551 return false;
552}
553
554bool MIParser::parseBasicBlocks() {
555 lex();
556 // Skip until the first machine basic block.
557 while (Token.is(MIToken::Newline))
558 lex();
559 if (Token.isErrorOrEOF())
560 return Token.isError();
561 // The first parsing pass should have verified that this token is a MBB label
562 // in the 'parseBasicBlockDefinitions' method.
563 assert(Token.is(MIToken::MachineBasicBlockLabel));
564 do {
565 MachineBasicBlock *MBB = nullptr;
566 if (parseMBBReference(MBB))
567 return true;
568 if (parseBasicBlock(*MBB))
569 return true;
570 // The method 'parseBasicBlock' should parse the whole block until the next
571 // block or the end of file.
572 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
573 } while (Token.isNot(MIToken::Eof));
574 return false;
575}
576
577bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000578 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000579 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000580 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000581 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000582 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000583 Optional<unsigned> TiedDefIdx;
584 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000585 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000586 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000587 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000588 if (Token.isNot(MIToken::comma))
589 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000590 lex();
591 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000592 if (!Operands.empty() && expectAndConsume(MIToken::equal))
593 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000594
Alex Lorenze5a44662015-07-17 00:24:15 +0000595 unsigned OpCode, Flags = 0;
596 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000597 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000598
Quentin Colombet85199672016-03-08 00:20:48 +0000599 Type *Ty = nullptr;
600 if (isPreISelGenericOpcode(OpCode)) {
601 // For generic opcode, a type is mandatory.
602 auto Loc = Token.location();
603 if (parseIRType(Loc, Ty))
604 return true;
Quentin Colombet85199672016-03-08 00:20:48 +0000605 }
606
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000607 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000608 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000609 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000610 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000611 Optional<unsigned> TiedDefIdx;
612 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000613 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000614 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000615 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000616 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
617 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000618 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000619 if (Token.isNot(MIToken::comma))
620 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000621 lex();
622 }
623
Alex Lorenz46d760d2015-07-22 21:15:11 +0000624 DebugLoc DebugLocation;
625 if (Token.is(MIToken::kw_debug_location)) {
626 lex();
627 if (Token.isNot(MIToken::exclaim))
628 return error("expected a metadata node after 'debug-location'");
629 MDNode *Node = nullptr;
630 if (parseMDNode(Node))
631 return true;
632 DebugLocation = DebugLoc(Node);
633 }
634
Alex Lorenz4af7e612015-08-03 23:08:19 +0000635 // Parse the machine memory operands.
636 SmallVector<MachineMemOperand *, 2> MemOperands;
637 if (Token.is(MIToken::coloncolon)) {
638 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000639 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000640 MachineMemOperand *MemOp = nullptr;
641 if (parseMachineMemoryOperand(MemOp))
642 return true;
643 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000644 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000645 break;
646 if (Token.isNot(MIToken::comma))
647 return error("expected ',' before the next machine memory operand");
648 lex();
649 }
650 }
651
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000652 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000653 if (!MCID.isVariadic()) {
654 // FIXME: Move the implicit operand verification to the machine verifier.
655 if (verifyImplicitOperands(Operands, MCID))
656 return true;
657 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000658
Alex Lorenzcb268d42015-07-06 23:07:26 +0000659 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000660 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000661 MI->setFlags(Flags);
Quentin Colombet85199672016-03-08 00:20:48 +0000662 if (Ty)
663 MI->setType(Ty);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000664 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000665 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000666 if (assignRegisterTies(*MI, Operands))
667 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000668 if (MemOperands.empty())
669 return false;
670 MachineInstr::mmo_iterator MemRefs =
671 MF.allocateMemRefsArray(MemOperands.size());
672 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
673 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000674 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000675}
676
Alex Lorenz1ea60892015-07-27 20:29:27 +0000677bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000678 lex();
679 if (Token.isNot(MIToken::MachineBasicBlock))
680 return error("expected a machine basic block reference");
681 if (parseMBBReference(MBB))
682 return true;
683 lex();
684 if (Token.isNot(MIToken::Eof))
685 return error(
686 "expected end of string after the machine basic block reference");
687 return false;
688}
689
Alex Lorenz1ea60892015-07-27 20:29:27 +0000690bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000691 lex();
692 if (Token.isNot(MIToken::NamedRegister))
693 return error("expected a named register");
694 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000695 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000696 lex();
697 if (Token.isNot(MIToken::Eof))
698 return error("expected end of string after the register reference");
699 return false;
700}
701
Alex Lorenz12045a42015-07-27 17:42:45 +0000702bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
703 lex();
704 if (Token.isNot(MIToken::VirtualRegister))
705 return error("expected a virtual register");
706 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000707 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000708 lex();
709 if (Token.isNot(MIToken::Eof))
710 return error("expected end of string after the register reference");
711 return false;
712}
713
Alex Lorenza314d812015-08-18 22:26:26 +0000714bool MIParser::parseStandaloneStackObject(int &FI) {
715 lex();
716 if (Token.isNot(MIToken::StackObject))
717 return error("expected a stack object");
718 if (parseStackFrameIndex(FI))
719 return true;
720 if (Token.isNot(MIToken::Eof))
721 return error("expected end of string after the stack object reference");
722 return false;
723}
724
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000725bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
726 lex();
727 if (Token.isNot(MIToken::exclaim))
728 return error("expected a metadata node");
729 if (parseMDNode(Node))
730 return true;
731 if (Token.isNot(MIToken::Eof))
732 return error("expected end of string after the metadata node");
733 return false;
734}
735
Alex Lorenz36962cd2015-07-07 02:08:46 +0000736static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
737 assert(MO.isImplicit());
738 return MO.isDef() ? "implicit-def" : "implicit";
739}
740
741static std::string getRegisterName(const TargetRegisterInfo *TRI,
742 unsigned Reg) {
743 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
744 return StringRef(TRI->getName(Reg)).lower();
745}
746
Alex Lorenz0153e592015-09-10 14:04:34 +0000747/// Return true if the parsed machine operands contain a given machine operand.
748static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
749 ArrayRef<ParsedMachineOperand> Operands) {
750 for (const auto &I : Operands) {
751 if (ImplicitOperand.isIdenticalTo(I.Operand))
752 return true;
753 }
754 return false;
755}
756
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000757bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
758 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000759 if (MCID.isCall())
760 // We can't verify call instructions as they can contain arbitrary implicit
761 // register and register mask operands.
762 return false;
763
764 // Gather all the expected implicit operands.
765 SmallVector<MachineOperand, 4> ImplicitOperands;
766 if (MCID.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000767 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000768 ImplicitOperands.push_back(
769 MachineOperand::CreateReg(*ImpDefs, true, true));
770 if (MCID.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000771 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000772 ImplicitOperands.push_back(
773 MachineOperand::CreateReg(*ImpUses, false, true));
774
775 const auto *TRI = MF.getSubtarget().getRegisterInfo();
776 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000777 for (const auto &I : ImplicitOperands) {
778 if (isImplicitOperandIn(I, Operands))
779 continue;
780 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000781 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000782 printImplicitRegisterFlag(I) + " %" +
783 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000784 }
785 return false;
786}
787
Alex Lorenze5a44662015-07-17 00:24:15 +0000788bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
789 if (Token.is(MIToken::kw_frame_setup)) {
790 Flags |= MachineInstr::FrameSetup;
791 lex();
792 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000793 if (Token.isNot(MIToken::Identifier))
794 return error("expected a machine instruction");
795 StringRef InstrName = Token.stringValue();
796 if (parseInstrName(InstrName, OpCode))
797 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000798 lex();
799 return false;
800}
801
802bool MIParser::parseRegister(unsigned &Reg) {
803 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000804 case MIToken::underscore:
805 Reg = 0;
806 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000807 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000808 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000809 if (getRegisterByName(Name, Reg))
810 return error(Twine("unknown register name '") + Name + "'");
811 break;
812 }
Alex Lorenz53464512015-07-10 22:51:20 +0000813 case MIToken::VirtualRegister: {
814 unsigned ID;
815 if (getUnsigned(ID))
816 return true;
817 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
818 if (RegInfo == PFS.VirtualRegisterSlots.end())
819 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
820 "'");
821 Reg = RegInfo->second;
822 break;
823 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000824 // TODO: Parse other register kinds.
825 default:
826 llvm_unreachable("The current token should be a register");
827 }
828 return false;
829}
830
Alex Lorenzcb268d42015-07-06 23:07:26 +0000831bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000832 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000833 switch (Token.kind()) {
834 case MIToken::kw_implicit:
835 Flags |= RegState::Implicit;
836 break;
837 case MIToken::kw_implicit_define:
838 Flags |= RegState::ImplicitDefine;
839 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000840 case MIToken::kw_def:
841 Flags |= RegState::Define;
842 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000843 case MIToken::kw_dead:
844 Flags |= RegState::Dead;
845 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000846 case MIToken::kw_killed:
847 Flags |= RegState::Kill;
848 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000849 case MIToken::kw_undef:
850 Flags |= RegState::Undef;
851 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000852 case MIToken::kw_internal:
853 Flags |= RegState::InternalRead;
854 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000855 case MIToken::kw_early_clobber:
856 Flags |= RegState::EarlyClobber;
857 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000858 case MIToken::kw_debug_use:
859 Flags |= RegState::Debug;
860 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000861 default:
862 llvm_unreachable("The current token should be a register flag");
863 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000864 if (OldFlags == Flags)
865 // We know that the same flag is specified more than once when the flags
866 // weren't modified.
867 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000868 lex();
869 return false;
870}
871
Alex Lorenz2eacca82015-07-13 23:24:34 +0000872bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
873 assert(Token.is(MIToken::colon));
874 lex();
875 if (Token.isNot(MIToken::Identifier))
876 return error("expected a subregister index after ':'");
877 auto Name = Token.stringValue();
878 SubReg = getSubRegIndex(Name);
879 if (!SubReg)
880 return error(Twine("use of unknown subregister index '") + Name + "'");
881 lex();
882 return false;
883}
884
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000885bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
886 if (!consumeIfPresent(MIToken::kw_tied_def))
887 return error("expected 'tied-def' after '('");
888 if (Token.isNot(MIToken::IntegerLiteral))
889 return error("expected an integer literal after 'tied-def'");
890 if (getUnsigned(TiedDefIdx))
891 return true;
892 lex();
893 if (expectAndConsume(MIToken::rparen))
894 return true;
895 return false;
896}
897
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000898bool MIParser::parseSize(unsigned &Size) {
899 if (Token.isNot(MIToken::IntegerLiteral))
900 return error("expected an integer literal for the size");
901 if (getUnsigned(Size))
902 return true;
903 lex();
904 if (expectAndConsume(MIToken::rparen))
905 return true;
906 return false;
907}
908
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000909bool MIParser::assignRegisterTies(MachineInstr &MI,
910 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000911 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
912 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
913 if (!Operands[I].TiedDefIdx)
914 continue;
915 // The parser ensures that this operand is a register use, so we just have
916 // to check the tied-def operand.
917 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
918 if (DefIdx >= E)
919 return error(Operands[I].Begin,
920 Twine("use of invalid tied-def operand index '" +
921 Twine(DefIdx) + "'; instruction has only ") +
922 Twine(E) + " operands");
923 const auto &DefOperand = Operands[DefIdx].Operand;
924 if (!DefOperand.isReg() || !DefOperand.isDef())
925 // FIXME: add note with the def operand.
926 return error(Operands[I].Begin,
927 Twine("use of invalid tied-def operand index '") +
928 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
929 " isn't a defined register");
930 // Check that the tied-def operand wasn't tied elsewhere.
931 for (const auto &TiedPair : TiedRegisterPairs) {
932 if (TiedPair.first == DefIdx)
933 return error(Operands[I].Begin,
934 Twine("the tied-def operand #") + Twine(DefIdx) +
935 " is already tied with another register operand");
936 }
937 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
938 }
939 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
940 // indices must be less than tied max.
941 for (const auto &TiedPair : TiedRegisterPairs)
942 MI.tieOperands(TiedPair.first, TiedPair.second);
943 return false;
944}
945
946bool MIParser::parseRegisterOperand(MachineOperand &Dest,
947 Optional<unsigned> &TiedDefIdx,
948 bool IsDef) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000949 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000950 unsigned Flags = IsDef ? RegState::Define : 0;
951 while (Token.isRegisterFlag()) {
952 if (parseRegisterFlag(Flags))
953 return true;
954 }
955 if (!Token.isRegister())
956 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000957 if (parseRegister(Reg))
958 return true;
959 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000960 unsigned SubReg = 0;
961 if (Token.is(MIToken::colon)) {
962 if (parseSubRegisterIndex(SubReg))
963 return true;
964 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000965 if ((Flags & RegState::Define) == 0) {
966 if (consumeIfPresent(MIToken::lparen)) {
967 unsigned Idx;
968 if (parseRegisterTiedDefIndex(Idx))
969 return true;
970 TiedDefIdx = Idx;
971 }
972 } else if (consumeIfPresent(MIToken::lparen)) {
973 // Generic virtual registers must have a size.
974 // The "must" part will be verify by the machine verifier,
975 // because at this point we actually do not know if Reg is
976 // a generic virtual register.
977 if (!TargetRegisterInfo::isVirtualRegister(Reg))
978 return error("unexpected size on physical register");
979 unsigned Size;
980 if (parseSize(Size))
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000981 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000982
983 MachineRegisterInfo &MRI = MF.getRegInfo();
984 MRI.setSize(Reg, Size);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000985 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000986 Dest = MachineOperand::CreateReg(
987 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000988 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +0000989 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
990 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000991 return false;
992}
993
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000994bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
995 assert(Token.is(MIToken::IntegerLiteral));
996 const APSInt &Int = Token.integerValue();
997 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +0000998 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000999 Dest = MachineOperand::CreateImm(Int.getExtValue());
1000 lex();
1001 return false;
1002}
1003
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001004bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1005 const Constant *&C) {
1006 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001007 SMDiagnostic Err;
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001008 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
1009 &IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001010 if (!C)
1011 return error(Loc + Err.getColumnNo(), Err.getMessage());
1012 return false;
1013}
1014
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001015bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1016 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1017 return true;
1018 lex();
1019 return false;
1020}
1021
Quentin Colombet85199672016-03-08 00:20:48 +00001022bool MIParser::parseIRType(StringRef::iterator Loc, StringRef StringValue,
Quentin Colombet287c6bb2016-03-08 00:57:31 +00001023 unsigned &Read, Type *&Ty) {
Quentin Colombet85199672016-03-08 00:20:48 +00001024 auto Source = StringValue.str(); // The source has to be null terminated.
1025 SMDiagnostic Err;
Quentin Colombet287c6bb2016-03-08 00:57:31 +00001026 Ty = parseTypeAtBeginning(Source.c_str(), Read, Err,
1027 *MF.getFunction()->getParent(), &IRSlots);
Quentin Colombet85199672016-03-08 00:20:48 +00001028 if (!Ty)
1029 return error(Loc + Err.getColumnNo(), Err.getMessage());
1030 return false;
1031}
1032
Quentin Colombet287c6bb2016-03-08 00:57:31 +00001033bool MIParser::parseIRType(StringRef::iterator Loc, Type *&Ty,
1034 bool MustBeSized) {
1035 // At this point we enter in the IR world, i.e., to get the correct type,
1036 // we need to hand off the whole string, not just the current token.
1037 // E.g., <4 x i64> would give '<' as a token and there is not much
1038 // the IR parser can do with that.
1039 unsigned Read = 0;
1040 if (parseIRType(Loc, StringRef(Loc), Read, Ty))
Quentin Colombet85199672016-03-08 00:20:48 +00001041 return true;
Quentin Colombet287c6bb2016-03-08 00:57:31 +00001042 // The type must be sized, otherwise there is not much the backend
1043 // can do with it.
1044 if (MustBeSized && !Ty->isSized())
1045 return error("expected a sized type");
1046 // The next token is Read characters from the Loc.
1047 // However, the current location is not Loc, but Loc + the length of Token.
1048 // Therefore, subtract the length of Token (range().end() - Loc) to the
1049 // number of characters to skip before the next token.
1050 lex(Read - (Token.range().end() - Loc));
Quentin Colombet85199672016-03-08 00:20:48 +00001051 return false;
1052}
1053
Alex Lorenz05e38822015-08-05 18:52:21 +00001054bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1055 assert(Token.is(MIToken::IntegerType));
1056 auto Loc = Token.location();
1057 lex();
1058 if (Token.isNot(MIToken::IntegerLiteral))
1059 return error("expected an integer literal");
1060 const Constant *C = nullptr;
1061 if (parseIRConstant(Loc, C))
1062 return true;
1063 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1064 return false;
1065}
1066
Alex Lorenzad156fb2015-07-31 20:49:21 +00001067bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1068 auto Loc = Token.location();
1069 lex();
1070 if (Token.isNot(MIToken::FloatingPointLiteral))
1071 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001072 const Constant *C = nullptr;
1073 if (parseIRConstant(Loc, C))
1074 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001075 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1076 return false;
1077}
1078
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001079bool MIParser::getUnsigned(unsigned &Result) {
1080 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1081 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1082 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1083 if (Val64 == Limit)
1084 return error("expected 32-bit integer (too large)");
1085 Result = Val64;
1086 return false;
1087}
1088
Alex Lorenzf09df002015-06-30 18:16:42 +00001089bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001090 assert(Token.is(MIToken::MachineBasicBlock) ||
1091 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001092 unsigned Number;
1093 if (getUnsigned(Number))
1094 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001095 auto MBBInfo = PFS.MBBSlots.find(Number);
1096 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001097 return error(Twine("use of undefined machine basic block #") +
1098 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001099 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001100 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1101 return error(Twine("the name of machine basic block #") + Twine(Number) +
1102 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001103 return false;
1104}
1105
1106bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1107 MachineBasicBlock *MBB;
1108 if (parseMBBReference(MBB))
1109 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001110 Dest = MachineOperand::CreateMBB(MBB);
1111 lex();
1112 return false;
1113}
1114
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001115bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001116 assert(Token.is(MIToken::StackObject));
1117 unsigned ID;
1118 if (getUnsigned(ID))
1119 return true;
1120 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1121 if (ObjectInfo == PFS.StackObjectSlots.end())
1122 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1123 "'");
1124 StringRef Name;
1125 if (const auto *Alloca =
1126 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
1127 Name = Alloca->getName();
1128 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1129 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1130 "' isn't '" + Token.stringValue() + "'");
1131 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001132 FI = ObjectInfo->second;
1133 return false;
1134}
1135
1136bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1137 int FI;
1138 if (parseStackFrameIndex(FI))
1139 return true;
1140 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001141 return false;
1142}
1143
Alex Lorenzea882122015-08-12 21:17:02 +00001144bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001145 assert(Token.is(MIToken::FixedStackObject));
1146 unsigned ID;
1147 if (getUnsigned(ID))
1148 return true;
1149 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1150 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1151 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1152 Twine(ID) + "'");
1153 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001154 FI = ObjectInfo->second;
1155 return false;
1156}
1157
1158bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1159 int FI;
1160 if (parseFixedStackFrameIndex(FI))
1161 return true;
1162 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001163 return false;
1164}
1165
Alex Lorenz41df7d32015-07-28 17:09:52 +00001166bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001167 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001168 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001169 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001170 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001171 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001172 return error(Twine("use of undefined global value '") + Token.range() +
1173 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001174 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001175 }
1176 case MIToken::GlobalValue: {
1177 unsigned GVIdx;
1178 if (getUnsigned(GVIdx))
1179 return true;
1180 if (GVIdx >= IRSlots.GlobalValues.size())
1181 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1182 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001183 GV = IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001184 break;
1185 }
1186 default:
1187 llvm_unreachable("The current token should be a global value");
1188 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001189 return false;
1190}
1191
1192bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1193 GlobalValue *GV = nullptr;
1194 if (parseGlobalValue(GV))
1195 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001196 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001197 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001198 if (parseOperandsOffset(Dest))
1199 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001200 return false;
1201}
1202
Alex Lorenzab980492015-07-20 20:51:18 +00001203bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1204 assert(Token.is(MIToken::ConstantPoolItem));
1205 unsigned ID;
1206 if (getUnsigned(ID))
1207 return true;
1208 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1209 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1210 return error("use of undefined constant '%const." + Twine(ID) + "'");
1211 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001212 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001213 if (parseOperandsOffset(Dest))
1214 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001215 return false;
1216}
1217
Alex Lorenz31d70682015-07-15 23:38:35 +00001218bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1219 assert(Token.is(MIToken::JumpTableIndex));
1220 unsigned ID;
1221 if (getUnsigned(ID))
1222 return true;
1223 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1224 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1225 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1226 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001227 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1228 return false;
1229}
1230
Alex Lorenz6ede3742015-07-21 16:59:53 +00001231bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001232 assert(Token.is(MIToken::ExternalSymbol));
1233 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001234 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001235 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001236 if (parseOperandsOffset(Dest))
1237 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001238 return false;
1239}
1240
Matthias Braunb74eb412016-03-28 18:18:46 +00001241bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1242 assert(Token.is(MIToken::SubRegisterIndex));
1243 StringRef Name = Token.stringValue();
1244 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1245 if (SubRegIndex == 0)
1246 return error(Twine("unknown subregister index '") + Name + "'");
1247 lex();
1248 Dest = MachineOperand::CreateImm(SubRegIndex);
1249 return false;
1250}
1251
Alex Lorenz44f29252015-07-22 21:07:04 +00001252bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001253 assert(Token.is(MIToken::exclaim));
1254 auto Loc = Token.location();
1255 lex();
1256 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1257 return error("expected metadata id after '!'");
1258 unsigned ID;
1259 if (getUnsigned(ID))
1260 return true;
1261 auto NodeInfo = IRSlots.MetadataNodes.find(ID);
1262 if (NodeInfo == IRSlots.MetadataNodes.end())
1263 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1264 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001265 Node = NodeInfo->second.get();
1266 return false;
1267}
1268
1269bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1270 MDNode *Node = nullptr;
1271 if (parseMDNode(Node))
1272 return true;
1273 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001274 return false;
1275}
1276
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001277bool MIParser::parseCFIOffset(int &Offset) {
1278 if (Token.isNot(MIToken::IntegerLiteral))
1279 return error("expected a cfi offset");
1280 if (Token.integerValue().getMinSignedBits() > 32)
1281 return error("expected a 32 bit integer (the cfi offset is too large)");
1282 Offset = (int)Token.integerValue().getExtValue();
1283 lex();
1284 return false;
1285}
1286
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001287bool MIParser::parseCFIRegister(unsigned &Reg) {
1288 if (Token.isNot(MIToken::NamedRegister))
1289 return error("expected a cfi register");
1290 unsigned LLVMReg;
1291 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001292 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001293 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1294 assert(TRI && "Expected target register info");
1295 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1296 if (DwarfReg < 0)
1297 return error("invalid DWARF register");
1298 Reg = (unsigned)DwarfReg;
1299 lex();
1300 return false;
1301}
1302
1303bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1304 auto Kind = Token.kind();
1305 lex();
1306 auto &MMI = MF.getMMI();
1307 int Offset;
1308 unsigned Reg;
1309 unsigned CFIIndex;
1310 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001311 case MIToken::kw_cfi_same_value:
1312 if (parseCFIRegister(Reg))
1313 return true;
1314 CFIIndex =
1315 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1316 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001317 case MIToken::kw_cfi_offset:
1318 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1319 parseCFIOffset(Offset))
1320 return true;
1321 CFIIndex =
1322 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1323 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001324 case MIToken::kw_cfi_def_cfa_register:
1325 if (parseCFIRegister(Reg))
1326 return true;
1327 CFIIndex =
1328 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1329 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001330 case MIToken::kw_cfi_def_cfa_offset:
1331 if (parseCFIOffset(Offset))
1332 return true;
1333 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1334 CFIIndex = MMI.addFrameInst(
1335 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1336 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001337 case MIToken::kw_cfi_def_cfa:
1338 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1339 parseCFIOffset(Offset))
1340 return true;
1341 // NB: MCCFIInstruction::createDefCfa negates the offset.
1342 CFIIndex =
1343 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1344 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001345 default:
1346 // TODO: Parse the other CFI operands.
1347 llvm_unreachable("The current token should be a cfi operand");
1348 }
1349 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001350 return false;
1351}
1352
Alex Lorenzdeb53492015-07-28 17:28:03 +00001353bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1354 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001355 case MIToken::NamedIRBlock: {
1356 BB = dyn_cast_or_null<BasicBlock>(
1357 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001358 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001359 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001360 break;
1361 }
1362 case MIToken::IRBlock: {
1363 unsigned SlotNumber = 0;
1364 if (getUnsigned(SlotNumber))
1365 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001366 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001367 if (!BB)
1368 return error(Twine("use of undefined IR block '%ir-block.") +
1369 Twine(SlotNumber) + "'");
1370 break;
1371 }
1372 default:
1373 llvm_unreachable("The current token should be an IR block reference");
1374 }
1375 return false;
1376}
1377
1378bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1379 assert(Token.is(MIToken::kw_blockaddress));
1380 lex();
1381 if (expectAndConsume(MIToken::lparen))
1382 return true;
1383 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001384 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001385 return error("expected a global value");
1386 GlobalValue *GV = nullptr;
1387 if (parseGlobalValue(GV))
1388 return true;
1389 auto *F = dyn_cast<Function>(GV);
1390 if (!F)
1391 return error("expected an IR function reference");
1392 lex();
1393 if (expectAndConsume(MIToken::comma))
1394 return true;
1395 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001396 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001397 return error("expected an IR block reference");
1398 if (parseIRBlock(BB, *F))
1399 return true;
1400 lex();
1401 if (expectAndConsume(MIToken::rparen))
1402 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001403 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001404 if (parseOperandsOffset(Dest))
1405 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001406 return false;
1407}
1408
Alex Lorenzef5c1962015-07-28 23:02:45 +00001409bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1410 assert(Token.is(MIToken::kw_target_index));
1411 lex();
1412 if (expectAndConsume(MIToken::lparen))
1413 return true;
1414 if (Token.isNot(MIToken::Identifier))
1415 return error("expected the name of the target index");
1416 int Index = 0;
1417 if (getTargetIndex(Token.stringValue(), Index))
1418 return error("use of undefined target index '" + Token.stringValue() + "'");
1419 lex();
1420 if (expectAndConsume(MIToken::rparen))
1421 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001422 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001423 if (parseOperandsOffset(Dest))
1424 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001425 return false;
1426}
1427
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001428bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1429 assert(Token.is(MIToken::kw_liveout));
1430 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1431 assert(TRI && "Expected target register info");
1432 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1433 lex();
1434 if (expectAndConsume(MIToken::lparen))
1435 return true;
1436 while (true) {
1437 if (Token.isNot(MIToken::NamedRegister))
1438 return error("expected a named register");
1439 unsigned Reg = 0;
1440 if (parseRegister(Reg))
1441 return true;
1442 lex();
1443 Mask[Reg / 32] |= 1U << (Reg % 32);
1444 // TODO: Report an error if the same register is used more than once.
1445 if (Token.isNot(MIToken::comma))
1446 break;
1447 lex();
1448 }
1449 if (expectAndConsume(MIToken::rparen))
1450 return true;
1451 Dest = MachineOperand::CreateRegLiveOut(Mask);
1452 return false;
1453}
1454
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001455bool MIParser::parseMachineOperand(MachineOperand &Dest,
1456 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001457 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001458 case MIToken::kw_implicit:
1459 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001460 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001461 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001462 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001463 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001464 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001465 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001466 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001467 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001468 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001469 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001470 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001471 case MIToken::IntegerLiteral:
1472 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001473 case MIToken::IntegerType:
1474 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001475 case MIToken::kw_half:
1476 case MIToken::kw_float:
1477 case MIToken::kw_double:
1478 case MIToken::kw_x86_fp80:
1479 case MIToken::kw_fp128:
1480 case MIToken::kw_ppc_fp128:
1481 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001482 case MIToken::MachineBasicBlock:
1483 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001484 case MIToken::StackObject:
1485 return parseStackObjectOperand(Dest);
1486 case MIToken::FixedStackObject:
1487 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001488 case MIToken::GlobalValue:
1489 case MIToken::NamedGlobalValue:
1490 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001491 case MIToken::ConstantPoolItem:
1492 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001493 case MIToken::JumpTableIndex:
1494 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001495 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001496 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00001497 case MIToken::SubRegisterIndex:
1498 return parseSubRegisterIndexOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001499 case MIToken::exclaim:
1500 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001501 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001502 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001503 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001504 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001505 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001506 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001507 case MIToken::kw_blockaddress:
1508 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001509 case MIToken::kw_target_index:
1510 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001511 case MIToken::kw_liveout:
1512 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001513 case MIToken::Error:
1514 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001515 case MIToken::Identifier:
1516 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1517 Dest = MachineOperand::CreateRegMask(RegMask);
1518 lex();
1519 break;
1520 }
1521 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001522 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001523 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001524 return error("expected a machine operand");
1525 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001526 return false;
1527}
1528
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001529bool MIParser::parseMachineOperandAndTargetFlags(
1530 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001531 unsigned TF = 0;
1532 bool HasTargetFlags = false;
1533 if (Token.is(MIToken::kw_target_flags)) {
1534 HasTargetFlags = true;
1535 lex();
1536 if (expectAndConsume(MIToken::lparen))
1537 return true;
1538 if (Token.isNot(MIToken::Identifier))
1539 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001540 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1541 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1542 return error("use of undefined target flag '" + Token.stringValue() +
1543 "'");
1544 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001545 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001546 while (Token.is(MIToken::comma)) {
1547 lex();
1548 if (Token.isNot(MIToken::Identifier))
1549 return error("expected the name of the target flag");
1550 unsigned BitFlag = 0;
1551 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1552 return error("use of undefined target flag '" + Token.stringValue() +
1553 "'");
1554 // TODO: Report an error when using a duplicate bit target flag.
1555 TF |= BitFlag;
1556 lex();
1557 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001558 if (expectAndConsume(MIToken::rparen))
1559 return true;
1560 }
1561 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001562 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001563 return true;
1564 if (!HasTargetFlags)
1565 return false;
1566 if (Dest.isReg())
1567 return error(Loc, "register operands can't have target flags");
1568 Dest.setTargetFlags(TF);
1569 return false;
1570}
1571
Alex Lorenzdc24c172015-08-07 20:21:00 +00001572bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001573 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1574 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001575 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001576 bool IsNegative = Token.is(MIToken::minus);
1577 lex();
1578 if (Token.isNot(MIToken::IntegerLiteral))
1579 return error("expected an integer literal after '" + Sign + "'");
1580 if (Token.integerValue().getMinSignedBits() > 64)
1581 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001582 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001583 if (IsNegative)
1584 Offset = -Offset;
1585 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001586 return false;
1587}
1588
Alex Lorenz620f8912015-08-13 20:33:33 +00001589bool MIParser::parseAlignment(unsigned &Alignment) {
1590 assert(Token.is(MIToken::kw_align));
1591 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001592 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001593 return error("expected an integer literal after 'align'");
1594 if (getUnsigned(Alignment))
1595 return true;
1596 lex();
1597 return false;
1598}
1599
Alex Lorenzdc24c172015-08-07 20:21:00 +00001600bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1601 int64_t Offset = 0;
1602 if (parseOffset(Offset))
1603 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001604 Op.setOffset(Offset);
1605 return false;
1606}
1607
Alex Lorenz36593ac2015-08-19 23:27:07 +00001608bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001609 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001610 case MIToken::NamedIRValue: {
1611 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001612 break;
1613 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001614 case MIToken::IRValue: {
1615 unsigned SlotNumber = 0;
1616 if (getUnsigned(SlotNumber))
1617 return true;
1618 V = getIRValue(SlotNumber);
1619 break;
1620 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001621 case MIToken::NamedGlobalValue:
1622 case MIToken::GlobalValue: {
1623 GlobalValue *GV = nullptr;
1624 if (parseGlobalValue(GV))
1625 return true;
1626 V = GV;
1627 break;
1628 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001629 case MIToken::QuotedIRValue: {
1630 const Constant *C = nullptr;
1631 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1632 return true;
1633 V = C;
1634 break;
1635 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001636 default:
1637 llvm_unreachable("The current token should be an IR block reference");
1638 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001639 if (!V)
1640 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001641 return false;
1642}
1643
1644bool MIParser::getUint64(uint64_t &Result) {
1645 assert(Token.hasIntegerValue());
1646 if (Token.integerValue().getActiveBits() > 64)
1647 return error("expected 64-bit integer (too large)");
1648 Result = Token.integerValue().getZExtValue();
1649 return false;
1650}
1651
Alex Lorenza518b792015-08-04 00:24:45 +00001652bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
Alex Lorenze86d5152015-08-06 18:26:36 +00001653 const unsigned OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001654 switch (Token.kind()) {
1655 case MIToken::kw_volatile:
1656 Flags |= MachineMemOperand::MOVolatile;
1657 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001658 case MIToken::kw_non_temporal:
1659 Flags |= MachineMemOperand::MONonTemporal;
1660 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001661 case MIToken::kw_invariant:
1662 Flags |= MachineMemOperand::MOInvariant;
1663 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001664 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001665 default:
1666 llvm_unreachable("The current token should be a memory operand flag");
1667 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001668 if (OldFlags == Flags)
1669 // We know that the same flag is specified more than once when the flags
1670 // weren't modified.
1671 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001672 lex();
1673 return false;
1674}
1675
Alex Lorenz91097a32015-08-12 20:33:26 +00001676bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1677 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001678 case MIToken::kw_stack:
1679 PSV = MF.getPSVManager().getStack();
1680 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001681 case MIToken::kw_got:
1682 PSV = MF.getPSVManager().getGOT();
1683 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001684 case MIToken::kw_jump_table:
1685 PSV = MF.getPSVManager().getJumpTable();
1686 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001687 case MIToken::kw_constant_pool:
1688 PSV = MF.getPSVManager().getConstantPool();
1689 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001690 case MIToken::FixedStackObject: {
1691 int FI;
1692 if (parseFixedStackFrameIndex(FI))
1693 return true;
1694 PSV = MF.getPSVManager().getFixedStack(FI);
1695 // The token was already consumed, so use return here instead of break.
1696 return false;
1697 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001698 case MIToken::kw_call_entry: {
1699 lex();
1700 switch (Token.kind()) {
1701 case MIToken::GlobalValue:
1702 case MIToken::NamedGlobalValue: {
1703 GlobalValue *GV = nullptr;
1704 if (parseGlobalValue(GV))
1705 return true;
1706 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1707 break;
1708 }
1709 case MIToken::ExternalSymbol:
1710 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1711 MF.createExternalSymbolName(Token.stringValue()));
1712 break;
1713 default:
1714 return error(
1715 "expected a global value or an external symbol after 'call-entry'");
1716 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001717 break;
1718 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001719 default:
1720 llvm_unreachable("The current token should be pseudo source value");
1721 }
1722 lex();
1723 return false;
1724}
1725
1726bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001727 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001728 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Alex Lorenz0d009642015-08-20 00:12:57 +00001729 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001730 const PseudoSourceValue *PSV = nullptr;
1731 if (parseMemoryPseudoSourceValue(PSV))
1732 return true;
1733 int64_t Offset = 0;
1734 if (parseOffset(Offset))
1735 return true;
1736 Dest = MachinePointerInfo(PSV, Offset);
1737 return false;
1738 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001739 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1740 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001741 Token.isNot(MIToken::NamedGlobalValue) &&
1742 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001743 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001744 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001745 if (parseIRValue(V))
1746 return true;
1747 if (!V->getType()->isPointerTy())
1748 return error("expected a pointer IR value");
1749 lex();
1750 int64_t Offset = 0;
1751 if (parseOffset(Offset))
1752 return true;
1753 Dest = MachinePointerInfo(V, Offset);
1754 return false;
1755}
1756
Alex Lorenz4af7e612015-08-03 23:08:19 +00001757bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1758 if (expectAndConsume(MIToken::lparen))
1759 return true;
Alex Lorenza518b792015-08-04 00:24:45 +00001760 unsigned Flags = 0;
1761 while (Token.isMemoryOperandFlag()) {
1762 if (parseMemoryOperandFlag(Flags))
1763 return true;
1764 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001765 if (Token.isNot(MIToken::Identifier) ||
1766 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1767 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001768 if (Token.stringValue() == "load")
1769 Flags |= MachineMemOperand::MOLoad;
1770 else
1771 Flags |= MachineMemOperand::MOStore;
1772 lex();
1773
1774 if (Token.isNot(MIToken::IntegerLiteral))
1775 return error("expected the size integer literal after memory operation");
1776 uint64_t Size;
1777 if (getUint64(Size))
1778 return true;
1779 lex();
1780
1781 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1782 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != Word)
1783 return error(Twine("expected '") + Word + "'");
1784 lex();
1785
Alex Lorenz91097a32015-08-12 20:33:26 +00001786 MachinePointerInfo Ptr = MachinePointerInfo();
1787 if (parseMachinePointerInfo(Ptr))
Alex Lorenz83127732015-08-07 20:26:52 +00001788 return true;
Alex Lorenz61420f72015-08-07 20:48:30 +00001789 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001790 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001791 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001792 while (consumeIfPresent(MIToken::comma)) {
1793 switch (Token.kind()) {
1794 case MIToken::kw_align:
1795 if (parseAlignment(BaseAlignment))
1796 return true;
1797 break;
1798 case MIToken::md_tbaa:
1799 lex();
1800 if (parseMDNode(AAInfo.TBAA))
1801 return true;
1802 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001803 case MIToken::md_alias_scope:
1804 lex();
1805 if (parseMDNode(AAInfo.Scope))
1806 return true;
1807 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001808 case MIToken::md_noalias:
1809 lex();
1810 if (parseMDNode(AAInfo.NoAlias))
1811 return true;
1812 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001813 case MIToken::md_range:
1814 lex();
1815 if (parseMDNode(Range))
1816 return true;
1817 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001818 // TODO: Report an error on duplicate metadata nodes.
1819 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001820 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1821 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001822 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001823 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001824 if (expectAndConsume(MIToken::rparen))
1825 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001826 Dest =
1827 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001828 return false;
1829}
1830
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001831void MIParser::initNames2InstrOpCodes() {
1832 if (!Names2InstrOpCodes.empty())
1833 return;
1834 const auto *TII = MF.getSubtarget().getInstrInfo();
1835 assert(TII && "Expected target instruction info");
1836 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1837 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1838}
1839
1840bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1841 initNames2InstrOpCodes();
1842 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1843 if (InstrInfo == Names2InstrOpCodes.end())
1844 return true;
1845 OpCode = InstrInfo->getValue();
1846 return false;
1847}
1848
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001849void MIParser::initNames2Regs() {
1850 if (!Names2Regs.empty())
1851 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001852 // The '%noreg' register is the register 0.
1853 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001854 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1855 assert(TRI && "Expected target register info");
1856 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1857 bool WasInserted =
1858 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1859 .second;
1860 (void)WasInserted;
1861 assert(WasInserted && "Expected registers to be unique case-insensitively");
1862 }
1863}
1864
1865bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1866 initNames2Regs();
1867 auto RegInfo = Names2Regs.find(RegName);
1868 if (RegInfo == Names2Regs.end())
1869 return true;
1870 Reg = RegInfo->getValue();
1871 return false;
1872}
1873
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001874void MIParser::initNames2RegMasks() {
1875 if (!Names2RegMasks.empty())
1876 return;
1877 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1878 assert(TRI && "Expected target register info");
1879 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1880 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1881 assert(RegMasks.size() == RegMaskNames.size());
1882 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1883 Names2RegMasks.insert(
1884 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1885}
1886
1887const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1888 initNames2RegMasks();
1889 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1890 if (RegMaskInfo == Names2RegMasks.end())
1891 return nullptr;
1892 return RegMaskInfo->getValue();
1893}
1894
Alex Lorenz2eacca82015-07-13 23:24:34 +00001895void MIParser::initNames2SubRegIndices() {
1896 if (!Names2SubRegIndices.empty())
1897 return;
1898 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1899 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1900 Names2SubRegIndices.insert(
1901 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1902}
1903
1904unsigned MIParser::getSubRegIndex(StringRef Name) {
1905 initNames2SubRegIndices();
1906 auto SubRegInfo = Names2SubRegIndices.find(Name);
1907 if (SubRegInfo == Names2SubRegIndices.end())
1908 return 0;
1909 return SubRegInfo->getValue();
1910}
1911
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001912static void initSlots2BasicBlocks(
1913 const Function &F,
1914 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1915 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001916 MST.incorporateFunction(F);
1917 for (auto &BB : F) {
1918 if (BB.hasName())
1919 continue;
1920 int Slot = MST.getLocalSlot(&BB);
1921 if (Slot == -1)
1922 continue;
1923 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1924 }
1925}
1926
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001927static const BasicBlock *getIRBlockFromSlot(
1928 unsigned Slot,
1929 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001930 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1931 if (BlockInfo == Slots2BasicBlocks.end())
1932 return nullptr;
1933 return BlockInfo->second;
1934}
1935
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001936const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1937 if (Slots2BasicBlocks.empty())
1938 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1939 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1940}
1941
1942const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1943 if (&F == MF.getFunction())
1944 return getIRBlock(Slot);
1945 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1946 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1947 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1948}
1949
Alex Lorenzdd13be02015-08-19 23:31:05 +00001950static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
1951 DenseMap<unsigned, const Value *> &Slots2Values) {
1952 int Slot = MST.getLocalSlot(V);
1953 if (Slot == -1)
1954 return;
1955 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
1956}
1957
1958/// Creates the mapping from slot numbers to function's unnamed IR values.
1959static void initSlots2Values(const Function &F,
1960 DenseMap<unsigned, const Value *> &Slots2Values) {
1961 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1962 MST.incorporateFunction(F);
1963 for (const auto &Arg : F.args())
1964 mapValueToSlot(&Arg, MST, Slots2Values);
1965 for (const auto &BB : F) {
1966 mapValueToSlot(&BB, MST, Slots2Values);
1967 for (const auto &I : BB)
1968 mapValueToSlot(&I, MST, Slots2Values);
1969 }
1970}
1971
1972const Value *MIParser::getIRValue(unsigned Slot) {
1973 if (Slots2Values.empty())
1974 initSlots2Values(*MF.getFunction(), Slots2Values);
1975 auto ValueInfo = Slots2Values.find(Slot);
1976 if (ValueInfo == Slots2Values.end())
1977 return nullptr;
1978 return ValueInfo->second;
1979}
1980
Alex Lorenzef5c1962015-07-28 23:02:45 +00001981void MIParser::initNames2TargetIndices() {
1982 if (!Names2TargetIndices.empty())
1983 return;
1984 const auto *TII = MF.getSubtarget().getInstrInfo();
1985 assert(TII && "Expected target instruction info");
1986 auto Indices = TII->getSerializableTargetIndices();
1987 for (const auto &I : Indices)
1988 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
1989}
1990
1991bool MIParser::getTargetIndex(StringRef Name, int &Index) {
1992 initNames2TargetIndices();
1993 auto IndexInfo = Names2TargetIndices.find(Name);
1994 if (IndexInfo == Names2TargetIndices.end())
1995 return true;
1996 Index = IndexInfo->second;
1997 return false;
1998}
1999
Alex Lorenz49873a82015-08-06 00:44:07 +00002000void MIParser::initNames2DirectTargetFlags() {
2001 if (!Names2DirectTargetFlags.empty())
2002 return;
2003 const auto *TII = MF.getSubtarget().getInstrInfo();
2004 assert(TII && "Expected target instruction info");
2005 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2006 for (const auto &I : Flags)
2007 Names2DirectTargetFlags.insert(
2008 std::make_pair(StringRef(I.second), I.first));
2009}
2010
2011bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2012 initNames2DirectTargetFlags();
2013 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2014 if (FlagInfo == Names2DirectTargetFlags.end())
2015 return true;
2016 Flag = FlagInfo->second;
2017 return false;
2018}
2019
Alex Lorenzf3630112015-08-18 22:52:15 +00002020void MIParser::initNames2BitmaskTargetFlags() {
2021 if (!Names2BitmaskTargetFlags.empty())
2022 return;
2023 const auto *TII = MF.getSubtarget().getInstrInfo();
2024 assert(TII && "Expected target instruction info");
2025 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2026 for (const auto &I : Flags)
2027 Names2BitmaskTargetFlags.insert(
2028 std::make_pair(StringRef(I.second), I.first));
2029}
2030
2031bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2032 initNames2BitmaskTargetFlags();
2033 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2034 if (FlagInfo == Names2BitmaskTargetFlags.end())
2035 return true;
2036 Flag = FlagInfo->second;
2037 return false;
2038}
2039
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002040bool llvm::parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src,
2041 PerFunctionMIParsingState &PFS,
2042 const SlotMapping &IRSlots,
2043 SMDiagnostic &Error) {
2044 SourceMgr SM;
2045 SM.AddNewSourceBuffer(
2046 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
2047 SMLoc());
2048 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2049 .parseBasicBlockDefinitions(PFS.MBBSlots);
2050}
2051
2052bool llvm::parseMachineInstructions(MachineFunction &MF, StringRef Src,
2053 const PerFunctionMIParsingState &PFS,
2054 const SlotMapping &IRSlots,
2055 SMDiagnostic &Error) {
2056 SourceMgr SM;
2057 SM.AddNewSourceBuffer(
2058 MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
2059 SMLoc());
2060 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002061}
Alex Lorenzf09df002015-06-30 18:16:42 +00002062
Alex Lorenz7a503fa2015-07-07 17:46:43 +00002063bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
2064 MachineFunction &MF, StringRef Src,
2065 const PerFunctionMIParsingState &PFS,
2066 const SlotMapping &IRSlots, SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00002067 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002068}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002069
2070bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
2071 MachineFunction &MF, StringRef Src,
2072 const PerFunctionMIParsingState &PFS,
2073 const SlotMapping &IRSlots,
2074 SMDiagnostic &Error) {
Alex Lorenz1ea60892015-07-27 20:29:27 +00002075 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2076 .parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002077}
Alex Lorenz12045a42015-07-27 17:42:45 +00002078
2079bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
2080 MachineFunction &MF, StringRef Src,
2081 const PerFunctionMIParsingState &PFS,
2082 const SlotMapping &IRSlots,
2083 SMDiagnostic &Error) {
2084 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2085 .parseStandaloneVirtualRegister(Reg);
2086}
Alex Lorenza314d812015-08-18 22:26:26 +00002087
2088bool llvm::parseStackObjectReference(int &FI, SourceMgr &SM,
2089 MachineFunction &MF, StringRef Src,
2090 const PerFunctionMIParsingState &PFS,
2091 const SlotMapping &IRSlots,
2092 SMDiagnostic &Error) {
2093 return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2094 .parseStandaloneStackObject(FI);
2095}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002096
2097bool llvm::parseMDNode(MDNode *&Node, SourceMgr &SM, MachineFunction &MF,
2098 StringRef Src, const PerFunctionMIParsingState &PFS,
2099 const SlotMapping &IRSlots, SMDiagnostic &Error) {
2100 return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMDNode(Node);
2101}