blob: e4ca9ae02f778ce74ba934c75cbb512a97cd2bf8 [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
Matthias Braune35861d2016-07-13 23:27:50 +000039PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
40 SourceMgr &SM, const SlotMapping &IRSlots)
41 : MF(MF), SM(&SM), IRSlots(IRSlots) {
Matthias Braun83947862016-07-13 22:23:23 +000042}
43
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000044namespace {
45
Alex Lorenz36962cd2015-07-07 02:08:46 +000046/// A wrapper struct around the 'MachineOperand' struct that includes a source
Alex Lorenzfeb6b432015-08-19 19:19:16 +000047/// range and other attributes.
48struct ParsedMachineOperand {
Alex Lorenz36962cd2015-07-07 02:08:46 +000049 MachineOperand Operand;
50 StringRef::iterator Begin;
51 StringRef::iterator End;
Alex Lorenz5ef93b02015-08-19 19:05:34 +000052 Optional<unsigned> TiedDefIdx;
Alex Lorenz36962cd2015-07-07 02:08:46 +000053
Alex Lorenzfeb6b432015-08-19 19:19:16 +000054 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
55 StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
Alex Lorenz5ef93b02015-08-19 19:05:34 +000056 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
57 if (TiedDefIdx)
58 assert(Operand.isReg() && Operand.isUse() &&
59 "Only used register operands can be tied");
60 }
Alex Lorenz36962cd2015-07-07 02:08:46 +000061};
62
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000063class MIParser {
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000064 MachineFunction &MF;
65 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +000066 StringRef Source, CurrentSource;
67 MIToken Token;
Alex Lorenz7a503fa2015-07-07 17:46:43 +000068 const PerFunctionMIParsingState &PFS;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000069 /// Maps from instruction names to op codes.
70 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +000071 /// Maps from register names to registers.
72 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000073 /// Maps from register mask names to register masks.
74 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +000075 /// Maps from subregister names to subregister indices.
76 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +000077 /// Maps from slot numbers to function's unnamed basic blocks.
78 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzdd13be02015-08-19 23:31:05 +000079 /// Maps from slot numbers to function's unnamed values.
80 DenseMap<unsigned, const Value *> Slots2Values;
Alex Lorenzef5c1962015-07-28 23:02:45 +000081 /// Maps from target index names to target indices.
82 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +000083 /// Maps from direct target flag names to the direct target flag values.
84 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenzf3630112015-08-18 22:52:15 +000085 /// Maps from direct target flag names to the bitmask target flag values.
86 StringMap<unsigned> Names2BitmaskTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000087
88public:
Matthias Braune35861d2016-07-13 23:27:50 +000089 MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
90 StringRef Source);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000091
Quentin Colombet287c6bb2016-03-08 00:57:31 +000092 /// \p SkipChar gives the number of characters to skip before looking
93 /// for the next token.
94 void lex(unsigned SkipChar = 0);
Alex Lorenz91370c52015-06-22 20:37:46 +000095
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000096 /// Report an error at the current location with the given message.
97 ///
98 /// This function always return true.
99 bool error(const Twine &Msg);
100
Alex Lorenz91370c52015-06-22 20:37:46 +0000101 /// Report an error at the given location with the given message.
102 ///
103 /// This function always return true.
104 bool error(StringRef::iterator Loc, const Twine &Msg);
105
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000106 bool
107 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
108 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +0000109 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +0000110 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
111 bool parseStandaloneNamedRegister(unsigned &Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +0000112 bool parseStandaloneVirtualRegister(unsigned &Reg);
Alex Lorenza314d812015-08-18 22:26:26 +0000113 bool parseStandaloneStackObject(int &FI);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000114 bool parseStandaloneMDNode(MDNode *&Node);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000115
116 bool
117 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
118 bool parseBasicBlock(MachineBasicBlock &MBB);
119 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
120 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000121
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000122 bool parseRegister(unsigned &Reg);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000123 bool parseRegisterFlag(unsigned &Flags);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000124 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000125 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000126 bool parseSize(unsigned &Size);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000127 bool parseRegisterOperand(MachineOperand &Dest,
128 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000129 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000130 bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
131 const Constant *&C);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000132 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Tim Northover62ae5682016-07-20 19:09:30 +0000133 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty,
134 bool MustBeSized = true);
Alex Lorenz05e38822015-08-05 18:52:21 +0000135 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000136 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000137 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000138 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000139 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000140 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000141 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000142 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000143 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000144 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000145 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +0000146 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000147 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000148 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000149 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000150 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000151 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000152 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000153 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000154 bool parseIRBlock(BasicBlock *&BB, const Function &F);
155 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000156 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000157 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000158 bool parseMachineOperand(MachineOperand &Dest,
159 Optional<unsigned> &TiedDefIdx);
160 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
161 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000162 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000163 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000164 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000165 bool parseIRValue(const Value *&V);
Justin Lebar0af80cd2016-07-15 18:26:59 +0000166 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000167 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
168 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000169 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000170
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000171private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000172 /// Convert the integer literal in the current token into an unsigned integer.
173 ///
174 /// Return true if an error occurred.
175 bool getUnsigned(unsigned &Result);
176
Alex Lorenz4af7e612015-08-03 23:08:19 +0000177 /// Convert the integer literal in the current token into an uint64.
178 ///
179 /// Return true if an error occurred.
180 bool getUint64(uint64_t &Result);
181
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000182 /// If the current token is of the given kind, consume it and return false.
183 /// Otherwise report an error and return true.
184 bool expectAndConsume(MIToken::TokenKind TokenKind);
185
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000186 /// If the current token is of the given kind, consume it and return true.
187 /// Otherwise return false.
188 bool consumeIfPresent(MIToken::TokenKind TokenKind);
189
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000190 void initNames2InstrOpCodes();
191
192 /// Try to convert an instruction name to an opcode. Return true if the
193 /// instruction name is invalid.
194 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000195
Alex Lorenze5a44662015-07-17 00:24:15 +0000196 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000197
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000198 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000199 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000200
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000201 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000202 const MCInstrDesc &MCID);
203
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000204 void initNames2Regs();
205
206 /// Try to convert a register name to a register number. Return true if the
207 /// register name is invalid.
208 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000209
210 void initNames2RegMasks();
211
212 /// Check if the given identifier is a name of a register mask.
213 ///
214 /// Return null if the identifier isn't a register mask.
215 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000216
217 void initNames2SubRegIndices();
218
219 /// Check if the given identifier is a name of a subregister index.
220 ///
221 /// Return 0 if the name isn't a subregister index class.
222 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000223
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000224 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000225 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000226
Alex Lorenzdd13be02015-08-19 23:31:05 +0000227 const Value *getIRValue(unsigned Slot);
228
Alex Lorenzef5c1962015-07-28 23:02:45 +0000229 void initNames2TargetIndices();
230
231 /// Try to convert a name of target index to the corresponding target index.
232 ///
233 /// Return true if the name isn't a name of a target index.
234 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000235
236 void initNames2DirectTargetFlags();
237
238 /// Try to convert a name of a direct target flag to the corresponding
239 /// target flag.
240 ///
241 /// Return true if the name isn't a name of a direct flag.
242 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000243
244 void initNames2BitmaskTargetFlags();
245
246 /// Try to convert a name of a bitmask target flag to the corresponding
247 /// target flag.
248 ///
249 /// Return true if the name isn't a name of a bitmask target flag.
250 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000251};
252
253} // end anonymous namespace
254
Matthias Braune35861d2016-07-13 23:27:50 +0000255MIParser::MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
256 StringRef Source)
257 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
258{}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000259
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000260void MIParser::lex(unsigned SkipChar) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000261 CurrentSource = lexMIToken(
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000262 CurrentSource.data() + SkipChar, Token,
Alex Lorenz91370c52015-06-22 20:37:46 +0000263 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
264}
265
266bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
267
268bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Matthias Braune35861d2016-07-13 23:27:50 +0000269 const SourceMgr &SM = *PFS.SM;
Alex Lorenz91370c52015-06-22 20:37:46 +0000270 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000271 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
272 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
273 // Create an ordinary diagnostic when the source manager's buffer is the
274 // source string.
275 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
276 return true;
277 }
278 // Create a diagnostic for a YAML string literal.
279 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
280 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
281 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000282 return true;
283}
284
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000285static const char *toString(MIToken::TokenKind TokenKind) {
286 switch (TokenKind) {
287 case MIToken::comma:
288 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000289 case MIToken::equal:
290 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000291 case MIToken::colon:
292 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000293 case MIToken::lparen:
294 return "'('";
295 case MIToken::rparen:
296 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000297 default:
298 return "<unknown token>";
299 }
300}
301
302bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
303 if (Token.isNot(TokenKind))
304 return error(Twine("expected ") + toString(TokenKind));
305 lex();
306 return false;
307}
308
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000309bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
310 if (Token.isNot(TokenKind))
311 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000312 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000313 return true;
314}
Alex Lorenz91370c52015-06-22 20:37:46 +0000315
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000316bool MIParser::parseBasicBlockDefinition(
317 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
318 assert(Token.is(MIToken::MachineBasicBlockLabel));
319 unsigned ID = 0;
320 if (getUnsigned(ID))
321 return true;
322 auto Loc = Token.location();
323 auto Name = Token.stringValue();
324 lex();
325 bool HasAddressTaken = false;
326 bool IsLandingPad = false;
327 unsigned Alignment = 0;
328 BasicBlock *BB = nullptr;
329 if (consumeIfPresent(MIToken::lparen)) {
330 do {
331 // TODO: Report an error when multiple same attributes are specified.
332 switch (Token.kind()) {
333 case MIToken::kw_address_taken:
334 HasAddressTaken = true;
335 lex();
336 break;
337 case MIToken::kw_landing_pad:
338 IsLandingPad = true;
339 lex();
340 break;
341 case MIToken::kw_align:
342 if (parseAlignment(Alignment))
343 return true;
344 break;
345 case MIToken::IRBlock:
346 // TODO: Report an error when both name and ir block are specified.
347 if (parseIRBlock(BB, *MF.getFunction()))
348 return true;
349 lex();
350 break;
351 default:
352 break;
353 }
354 } while (consumeIfPresent(MIToken::comma));
355 if (expectAndConsume(MIToken::rparen))
356 return true;
357 }
358 if (expectAndConsume(MIToken::colon))
359 return true;
360
361 if (!Name.empty()) {
362 BB = dyn_cast_or_null<BasicBlock>(
363 MF.getFunction()->getValueSymbolTable().lookup(Name));
364 if (!BB)
365 return error(Loc, Twine("basic block '") + Name +
366 "' is not defined in the function '" +
367 MF.getName() + "'");
368 }
369 auto *MBB = MF.CreateMachineBasicBlock(BB);
370 MF.insert(MF.end(), MBB);
371 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
372 if (!WasInserted)
373 return error(Loc, Twine("redefinition of machine basic block with id #") +
374 Twine(ID));
375 if (Alignment)
376 MBB->setAlignment(Alignment);
377 if (HasAddressTaken)
378 MBB->setHasAddressTaken();
Reid Kleckner0e288232015-08-27 23:27:47 +0000379 MBB->setIsEHPad(IsLandingPad);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000380 return false;
381}
382
383bool MIParser::parseBasicBlockDefinitions(
384 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
385 lex();
386 // Skip until the first machine basic block.
387 while (Token.is(MIToken::Newline))
388 lex();
389 if (Token.isErrorOrEOF())
390 return Token.isError();
391 if (Token.isNot(MIToken::MachineBasicBlockLabel))
392 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000393 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000394 do {
395 if (parseBasicBlockDefinition(MBBSlots))
396 return true;
397 bool IsAfterNewline = false;
398 // Skip until the next machine basic block.
399 while (true) {
400 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
401 Token.isErrorOrEOF())
402 break;
403 else if (Token.is(MIToken::MachineBasicBlockLabel))
404 return error("basic block definition should be located at the start of "
405 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000406 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000407 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000408 continue;
409 }
410 IsAfterNewline = false;
411 if (Token.is(MIToken::lbrace))
412 ++BraceDepth;
413 if (Token.is(MIToken::rbrace)) {
414 if (!BraceDepth)
415 return error("extraneous closing brace ('}')");
416 --BraceDepth;
417 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000418 lex();
419 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000420 // Verify that we closed all of the '{' at the end of a file or a block.
421 if (!Token.isError() && BraceDepth)
422 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000423 } while (!Token.isErrorOrEOF());
424 return Token.isError();
425}
426
427bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
428 assert(Token.is(MIToken::kw_liveins));
429 lex();
430 if (expectAndConsume(MIToken::colon))
431 return true;
432 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
433 return false;
434 do {
435 if (Token.isNot(MIToken::NamedRegister))
436 return error("expected a named register");
437 unsigned Reg = 0;
438 if (parseRegister(Reg))
439 return true;
440 MBB.addLiveIn(Reg);
441 lex();
442 } while (consumeIfPresent(MIToken::comma));
443 return false;
444}
445
446bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
447 assert(Token.is(MIToken::kw_successors));
448 lex();
449 if (expectAndConsume(MIToken::colon))
450 return true;
451 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
452 return false;
453 do {
454 if (Token.isNot(MIToken::MachineBasicBlock))
455 return error("expected a machine basic block reference");
456 MachineBasicBlock *SuccMBB = nullptr;
457 if (parseMBBReference(SuccMBB))
458 return true;
459 lex();
460 unsigned Weight = 0;
461 if (consumeIfPresent(MIToken::lparen)) {
462 if (Token.isNot(MIToken::IntegerLiteral))
463 return error("expected an integer literal after '('");
464 if (getUnsigned(Weight))
465 return true;
466 lex();
467 if (expectAndConsume(MIToken::rparen))
468 return true;
469 }
Cong Houd97c1002015-12-01 05:29:22 +0000470 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000471 } while (consumeIfPresent(MIToken::comma));
Cong Houd97c1002015-12-01 05:29:22 +0000472 MBB.normalizeSuccProbs();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000473 return false;
474}
475
476bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
477 // Skip the definition.
478 assert(Token.is(MIToken::MachineBasicBlockLabel));
479 lex();
480 if (consumeIfPresent(MIToken::lparen)) {
481 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
482 lex();
483 consumeIfPresent(MIToken::rparen);
484 }
485 consumeIfPresent(MIToken::colon);
486
487 // Parse the liveins and successors.
488 // N.B: Multiple lists of successors and liveins are allowed and they're
489 // merged into one.
490 // Example:
491 // liveins: %edi
492 // liveins: %esi
493 //
494 // is equivalent to
495 // liveins: %edi, %esi
496 while (true) {
497 if (Token.is(MIToken::kw_successors)) {
498 if (parseBasicBlockSuccessors(MBB))
499 return true;
500 } else if (Token.is(MIToken::kw_liveins)) {
501 if (parseBasicBlockLiveins(MBB))
502 return true;
503 } else if (consumeIfPresent(MIToken::Newline)) {
504 continue;
505 } else
506 break;
507 if (!Token.isNewlineOrEOF())
508 return error("expected line break at the end of a list");
509 lex();
510 }
511
512 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000513 bool IsInBundle = false;
514 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000515 while (true) {
516 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
517 return false;
518 else if (consumeIfPresent(MIToken::Newline))
519 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000520 if (consumeIfPresent(MIToken::rbrace)) {
521 // The first parsing pass should verify that all closing '}' have an
522 // opening '{'.
523 assert(IsInBundle);
524 IsInBundle = false;
525 continue;
526 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000527 MachineInstr *MI = nullptr;
528 if (parse(MI))
529 return true;
530 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000531 if (IsInBundle) {
532 PrevMI->setFlag(MachineInstr::BundledSucc);
533 MI->setFlag(MachineInstr::BundledPred);
534 }
535 PrevMI = MI;
536 if (Token.is(MIToken::lbrace)) {
537 if (IsInBundle)
538 return error("nested instruction bundles are not allowed");
539 lex();
540 // This instruction is the start of the bundle.
541 MI->setFlag(MachineInstr::BundledSucc);
542 IsInBundle = true;
543 if (!Token.is(MIToken::Newline))
544 // The next instruction can be on the same line.
545 continue;
546 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000547 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
548 lex();
549 }
550 return false;
551}
552
553bool MIParser::parseBasicBlocks() {
554 lex();
555 // Skip until the first machine basic block.
556 while (Token.is(MIToken::Newline))
557 lex();
558 if (Token.isErrorOrEOF())
559 return Token.isError();
560 // The first parsing pass should have verified that this token is a MBB label
561 // in the 'parseBasicBlockDefinitions' method.
562 assert(Token.is(MIToken::MachineBasicBlockLabel));
563 do {
564 MachineBasicBlock *MBB = nullptr;
565 if (parseMBBReference(MBB))
566 return true;
567 if (parseBasicBlock(*MBB))
568 return true;
569 // The method 'parseBasicBlock' should parse the whole block until the next
570 // block or the end of file.
571 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
572 } while (Token.isNot(MIToken::Eof));
573 return false;
574}
575
576bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000577 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000578 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000579 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000580 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000581 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000582 Optional<unsigned> TiedDefIdx;
583 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000584 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000585 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000586 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000587 if (Token.isNot(MIToken::comma))
588 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000589 lex();
590 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000591 if (!Operands.empty() && expectAndConsume(MIToken::equal))
592 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000593
Alex Lorenze5a44662015-07-17 00:24:15 +0000594 unsigned OpCode, Flags = 0;
595 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000596 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000597
Tim Northover62ae5682016-07-20 19:09:30 +0000598 LLT Ty{};
Quentin Colombet85199672016-03-08 00:20:48 +0000599 if (isPreISelGenericOpcode(OpCode)) {
600 // For generic opcode, a type is mandatory.
601 auto Loc = Token.location();
Tim Northover62ae5682016-07-20 19:09:30 +0000602 if (parseLowLevelType(Loc, Ty))
Quentin Colombet85199672016-03-08 00:20:48 +0000603 return true;
Quentin Colombet85199672016-03-08 00:20:48 +0000604 }
605
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000606 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000607 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000608 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000609 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000610 Optional<unsigned> TiedDefIdx;
611 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000612 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000613 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000614 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000615 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
616 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000617 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000618 if (Token.isNot(MIToken::comma))
619 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000620 lex();
621 }
622
Alex Lorenz46d760d2015-07-22 21:15:11 +0000623 DebugLoc DebugLocation;
624 if (Token.is(MIToken::kw_debug_location)) {
625 lex();
626 if (Token.isNot(MIToken::exclaim))
627 return error("expected a metadata node after 'debug-location'");
628 MDNode *Node = nullptr;
629 if (parseMDNode(Node))
630 return true;
631 DebugLocation = DebugLoc(Node);
632 }
633
Alex Lorenz4af7e612015-08-03 23:08:19 +0000634 // Parse the machine memory operands.
635 SmallVector<MachineMemOperand *, 2> MemOperands;
636 if (Token.is(MIToken::coloncolon)) {
637 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000638 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000639 MachineMemOperand *MemOp = nullptr;
640 if (parseMachineMemoryOperand(MemOp))
641 return true;
642 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000643 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000644 break;
645 if (Token.isNot(MIToken::comma))
646 return error("expected ',' before the next machine memory operand");
647 lex();
648 }
649 }
650
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000651 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000652 if (!MCID.isVariadic()) {
653 // FIXME: Move the implicit operand verification to the machine verifier.
654 if (verifyImplicitOperands(Operands, MCID))
655 return true;
656 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000657
Alex Lorenzcb268d42015-07-06 23:07:26 +0000658 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000659 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000660 MI->setFlags(Flags);
Tim Northover62ae5682016-07-20 19:09:30 +0000661 if (Ty.isValid())
Quentin Colombet85199672016-03-08 00:20:48 +0000662 MI->setType(Ty);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000663 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000664 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000665 if (assignRegisterTies(*MI, Operands))
666 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000667 if (MemOperands.empty())
668 return false;
669 MachineInstr::mmo_iterator MemRefs =
670 MF.allocateMemRefsArray(MemOperands.size());
671 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
672 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000673 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000674}
675
Alex Lorenz1ea60892015-07-27 20:29:27 +0000676bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000677 lex();
678 if (Token.isNot(MIToken::MachineBasicBlock))
679 return error("expected a machine basic block reference");
680 if (parseMBBReference(MBB))
681 return true;
682 lex();
683 if (Token.isNot(MIToken::Eof))
684 return error(
685 "expected end of string after the machine basic block reference");
686 return false;
687}
688
Alex Lorenz1ea60892015-07-27 20:29:27 +0000689bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000690 lex();
691 if (Token.isNot(MIToken::NamedRegister))
692 return error("expected a named register");
693 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000694 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000695 lex();
696 if (Token.isNot(MIToken::Eof))
697 return error("expected end of string after the register reference");
698 return false;
699}
700
Alex Lorenz12045a42015-07-27 17:42:45 +0000701bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
702 lex();
703 if (Token.isNot(MIToken::VirtualRegister))
704 return error("expected a virtual register");
705 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000706 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000707 lex();
708 if (Token.isNot(MIToken::Eof))
709 return error("expected end of string after the register reference");
710 return false;
711}
712
Alex Lorenza314d812015-08-18 22:26:26 +0000713bool MIParser::parseStandaloneStackObject(int &FI) {
714 lex();
715 if (Token.isNot(MIToken::StackObject))
716 return error("expected a stack object");
717 if (parseStackFrameIndex(FI))
718 return true;
719 if (Token.isNot(MIToken::Eof))
720 return error("expected end of string after the stack object reference");
721 return false;
722}
723
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000724bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
725 lex();
726 if (Token.isNot(MIToken::exclaim))
727 return error("expected a metadata node");
728 if (parseMDNode(Node))
729 return true;
730 if (Token.isNot(MIToken::Eof))
731 return error("expected end of string after the metadata node");
732 return false;
733}
734
Alex Lorenz36962cd2015-07-07 02:08:46 +0000735static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
736 assert(MO.isImplicit());
737 return MO.isDef() ? "implicit-def" : "implicit";
738}
739
740static std::string getRegisterName(const TargetRegisterInfo *TRI,
741 unsigned Reg) {
742 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
743 return StringRef(TRI->getName(Reg)).lower();
744}
745
Alex Lorenz0153e592015-09-10 14:04:34 +0000746/// Return true if the parsed machine operands contain a given machine operand.
747static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
748 ArrayRef<ParsedMachineOperand> Operands) {
749 for (const auto &I : Operands) {
750 if (ImplicitOperand.isIdenticalTo(I.Operand))
751 return true;
752 }
753 return false;
754}
755
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000756bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
757 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000758 if (MCID.isCall())
759 // We can't verify call instructions as they can contain arbitrary implicit
760 // register and register mask operands.
761 return false;
762
763 // Gather all the expected implicit operands.
764 SmallVector<MachineOperand, 4> ImplicitOperands;
765 if (MCID.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000766 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000767 ImplicitOperands.push_back(
768 MachineOperand::CreateReg(*ImpDefs, true, true));
769 if (MCID.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000770 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000771 ImplicitOperands.push_back(
772 MachineOperand::CreateReg(*ImpUses, false, true));
773
774 const auto *TRI = MF.getSubtarget().getRegisterInfo();
775 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000776 for (const auto &I : ImplicitOperands) {
777 if (isImplicitOperandIn(I, Operands))
778 continue;
779 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000780 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000781 printImplicitRegisterFlag(I) + " %" +
782 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000783 }
784 return false;
785}
786
Alex Lorenze5a44662015-07-17 00:24:15 +0000787bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
788 if (Token.is(MIToken::kw_frame_setup)) {
789 Flags |= MachineInstr::FrameSetup;
790 lex();
791 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000792 if (Token.isNot(MIToken::Identifier))
793 return error("expected a machine instruction");
794 StringRef InstrName = Token.stringValue();
795 if (parseInstrName(InstrName, OpCode))
796 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000797 lex();
798 return false;
799}
800
801bool MIParser::parseRegister(unsigned &Reg) {
802 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000803 case MIToken::underscore:
804 Reg = 0;
805 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000806 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000807 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000808 if (getRegisterByName(Name, Reg))
809 return error(Twine("unknown register name '") + Name + "'");
810 break;
811 }
Alex Lorenz53464512015-07-10 22:51:20 +0000812 case MIToken::VirtualRegister: {
813 unsigned ID;
814 if (getUnsigned(ID))
815 return true;
816 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
817 if (RegInfo == PFS.VirtualRegisterSlots.end())
818 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
819 "'");
820 Reg = RegInfo->second;
821 break;
822 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000823 // TODO: Parse other register kinds.
824 default:
825 llvm_unreachable("The current token should be a register");
826 }
827 return false;
828}
829
Alex Lorenzcb268d42015-07-06 23:07:26 +0000830bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000831 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000832 switch (Token.kind()) {
833 case MIToken::kw_implicit:
834 Flags |= RegState::Implicit;
835 break;
836 case MIToken::kw_implicit_define:
837 Flags |= RegState::ImplicitDefine;
838 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000839 case MIToken::kw_def:
840 Flags |= RegState::Define;
841 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000842 case MIToken::kw_dead:
843 Flags |= RegState::Dead;
844 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000845 case MIToken::kw_killed:
846 Flags |= RegState::Kill;
847 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000848 case MIToken::kw_undef:
849 Flags |= RegState::Undef;
850 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000851 case MIToken::kw_internal:
852 Flags |= RegState::InternalRead;
853 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000854 case MIToken::kw_early_clobber:
855 Flags |= RegState::EarlyClobber;
856 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000857 case MIToken::kw_debug_use:
858 Flags |= RegState::Debug;
859 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000860 default:
861 llvm_unreachable("The current token should be a register flag");
862 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000863 if (OldFlags == Flags)
864 // We know that the same flag is specified more than once when the flags
865 // weren't modified.
866 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000867 lex();
868 return false;
869}
870
Alex Lorenz2eacca82015-07-13 23:24:34 +0000871bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
872 assert(Token.is(MIToken::colon));
873 lex();
874 if (Token.isNot(MIToken::Identifier))
875 return error("expected a subregister index after ':'");
876 auto Name = Token.stringValue();
877 SubReg = getSubRegIndex(Name);
878 if (!SubReg)
879 return error(Twine("use of unknown subregister index '") + Name + "'");
880 lex();
881 return false;
882}
883
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000884bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
885 if (!consumeIfPresent(MIToken::kw_tied_def))
886 return error("expected 'tied-def' after '('");
887 if (Token.isNot(MIToken::IntegerLiteral))
888 return error("expected an integer literal after 'tied-def'");
889 if (getUnsigned(TiedDefIdx))
890 return true;
891 lex();
892 if (expectAndConsume(MIToken::rparen))
893 return true;
894 return false;
895}
896
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000897bool MIParser::parseSize(unsigned &Size) {
898 if (Token.isNot(MIToken::IntegerLiteral))
899 return error("expected an integer literal for the size");
900 if (getUnsigned(Size))
901 return true;
902 lex();
903 if (expectAndConsume(MIToken::rparen))
904 return true;
905 return false;
906}
907
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000908bool MIParser::assignRegisterTies(MachineInstr &MI,
909 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000910 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
911 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
912 if (!Operands[I].TiedDefIdx)
913 continue;
914 // The parser ensures that this operand is a register use, so we just have
915 // to check the tied-def operand.
916 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
917 if (DefIdx >= E)
918 return error(Operands[I].Begin,
919 Twine("use of invalid tied-def operand index '" +
920 Twine(DefIdx) + "'; instruction has only ") +
921 Twine(E) + " operands");
922 const auto &DefOperand = Operands[DefIdx].Operand;
923 if (!DefOperand.isReg() || !DefOperand.isDef())
924 // FIXME: add note with the def operand.
925 return error(Operands[I].Begin,
926 Twine("use of invalid tied-def operand index '") +
927 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
928 " isn't a defined register");
929 // Check that the tied-def operand wasn't tied elsewhere.
930 for (const auto &TiedPair : TiedRegisterPairs) {
931 if (TiedPair.first == DefIdx)
932 return error(Operands[I].Begin,
933 Twine("the tied-def operand #") + Twine(DefIdx) +
934 " is already tied with another register operand");
935 }
936 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
937 }
938 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
939 // indices must be less than tied max.
940 for (const auto &TiedPair : TiedRegisterPairs)
941 MI.tieOperands(TiedPair.first, TiedPair.second);
942 return false;
943}
944
945bool MIParser::parseRegisterOperand(MachineOperand &Dest,
946 Optional<unsigned> &TiedDefIdx,
947 bool IsDef) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000948 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000949 unsigned Flags = IsDef ? RegState::Define : 0;
950 while (Token.isRegisterFlag()) {
951 if (parseRegisterFlag(Flags))
952 return true;
953 }
954 if (!Token.isRegister())
955 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000956 if (parseRegister(Reg))
957 return true;
958 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000959 unsigned SubReg = 0;
960 if (Token.is(MIToken::colon)) {
961 if (parseSubRegisterIndex(SubReg))
962 return true;
Matthias Braun5d00b322016-07-16 01:36:18 +0000963 if (!TargetRegisterInfo::isVirtualRegister(Reg))
964 return error("subregister index expects a virtual register");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000965 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000966 if ((Flags & RegState::Define) == 0) {
967 if (consumeIfPresent(MIToken::lparen)) {
968 unsigned Idx;
969 if (parseRegisterTiedDefIndex(Idx))
970 return true;
971 TiedDefIdx = Idx;
972 }
973 } else if (consumeIfPresent(MIToken::lparen)) {
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000974 MachineRegisterInfo &MRI = MF.getRegInfo();
975
Quentin Colombet2c646962016-06-08 23:27:46 +0000976 // Virtual registers may have a size with GlobalISel.
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000977 if (!TargetRegisterInfo::isVirtualRegister(Reg))
978 return error("unexpected size on physical register");
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000979 if (MRI.getRegClassOrRegBank(Reg).is<const TargetRegisterClass *>())
980 return error("unexpected size on non-generic virtual register");
981
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000982 unsigned Size;
983 if (parseSize(Size))
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000984 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000985
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000986 MRI.setSize(Reg, Size);
Quentin Colombet2c646962016-06-08 23:27:46 +0000987 } else if (PFS.GenericVRegs.count(Reg)) {
988 // Generic virtual registers must have a size.
989 // If we end up here this means the size hasn't been specified and
990 // this is bad!
991 return error("generic virtual registers must have a size");
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000992 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000993 Dest = MachineOperand::CreateReg(
994 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000995 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +0000996 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
997 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000998 return false;
999}
1000
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001001bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1002 assert(Token.is(MIToken::IntegerLiteral));
1003 const APSInt &Int = Token.integerValue();
1004 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +00001005 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001006 Dest = MachineOperand::CreateImm(Int.getExtValue());
1007 lex();
1008 return false;
1009}
1010
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001011bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1012 const Constant *&C) {
1013 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001014 SMDiagnostic Err;
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001015 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
Matthias Braune35861d2016-07-13 23:27:50 +00001016 &PFS.IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001017 if (!C)
1018 return error(Loc + Err.getColumnNo(), Err.getMessage());
1019 return false;
1020}
1021
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001022bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1023 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1024 return true;
1025 lex();
1026 return false;
1027}
1028
Tim Northover62ae5682016-07-20 19:09:30 +00001029bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty,
1030 bool MustBeSized) {
1031 if (Token.is(MIToken::Identifier) && Token.stringValue() == "unsized") {
1032 if (MustBeSized)
1033 return error(Loc, "expected sN or <N x sM> for sized GlobalISel type");
1034 lex();
1035 Ty = LLT::unsized();
1036 return false;
1037 } else if (Token.is(MIToken::ScalarType)) {
1038 Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1039 lex();
1040 return false;
1041 }
Quentin Colombet85199672016-03-08 00:20:48 +00001042
Tim Northover62ae5682016-07-20 19:09:30 +00001043 // Now we're looking for a vector.
1044 if (Token.isNot(MIToken::less))
1045 return error(Loc, "expected unsized, sN or <N x sM> for GlobalISel type");
1046 lex();
1047
1048 if (Token.isNot(MIToken::IntegerLiteral))
1049 return error(Loc, "expected <N x sM> for vctor type");
1050 uint64_t NumElements = Token.integerValue().getZExtValue();
1051 lex();
1052
1053 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1054 return error(Loc, "expected '<N x sM>' for vector type");
1055 lex();
1056
1057 if (Token.isNot(MIToken::ScalarType))
1058 return error(Loc, "expected '<N x sM>' for vector type");
1059 uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1060 lex();
1061
1062 if (Token.isNot(MIToken::greater))
1063 return error(Loc, "expected '<N x sM>' for vector type");
1064 lex();
1065
1066 Ty = LLT::vector(NumElements, ScalarSize);
Quentin Colombet85199672016-03-08 00:20:48 +00001067 return false;
1068}
1069
Alex Lorenz05e38822015-08-05 18:52:21 +00001070bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1071 assert(Token.is(MIToken::IntegerType));
1072 auto Loc = Token.location();
1073 lex();
1074 if (Token.isNot(MIToken::IntegerLiteral))
1075 return error("expected an integer literal");
1076 const Constant *C = nullptr;
1077 if (parseIRConstant(Loc, C))
1078 return true;
1079 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1080 return false;
1081}
1082
Alex Lorenzad156fb2015-07-31 20:49:21 +00001083bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1084 auto Loc = Token.location();
1085 lex();
1086 if (Token.isNot(MIToken::FloatingPointLiteral))
1087 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001088 const Constant *C = nullptr;
1089 if (parseIRConstant(Loc, C))
1090 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001091 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1092 return false;
1093}
1094
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001095bool MIParser::getUnsigned(unsigned &Result) {
1096 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1097 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1098 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1099 if (Val64 == Limit)
1100 return error("expected 32-bit integer (too large)");
1101 Result = Val64;
1102 return false;
1103}
1104
Alex Lorenzf09df002015-06-30 18:16:42 +00001105bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001106 assert(Token.is(MIToken::MachineBasicBlock) ||
1107 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001108 unsigned Number;
1109 if (getUnsigned(Number))
1110 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001111 auto MBBInfo = PFS.MBBSlots.find(Number);
1112 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001113 return error(Twine("use of undefined machine basic block #") +
1114 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001115 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001116 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1117 return error(Twine("the name of machine basic block #") + Twine(Number) +
1118 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001119 return false;
1120}
1121
1122bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1123 MachineBasicBlock *MBB;
1124 if (parseMBBReference(MBB))
1125 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001126 Dest = MachineOperand::CreateMBB(MBB);
1127 lex();
1128 return false;
1129}
1130
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001131bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001132 assert(Token.is(MIToken::StackObject));
1133 unsigned ID;
1134 if (getUnsigned(ID))
1135 return true;
1136 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1137 if (ObjectInfo == PFS.StackObjectSlots.end())
1138 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1139 "'");
1140 StringRef Name;
1141 if (const auto *Alloca =
1142 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
1143 Name = Alloca->getName();
1144 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1145 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1146 "' isn't '" + Token.stringValue() + "'");
1147 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001148 FI = ObjectInfo->second;
1149 return false;
1150}
1151
1152bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1153 int FI;
1154 if (parseStackFrameIndex(FI))
1155 return true;
1156 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001157 return false;
1158}
1159
Alex Lorenzea882122015-08-12 21:17:02 +00001160bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001161 assert(Token.is(MIToken::FixedStackObject));
1162 unsigned ID;
1163 if (getUnsigned(ID))
1164 return true;
1165 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1166 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1167 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1168 Twine(ID) + "'");
1169 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001170 FI = ObjectInfo->second;
1171 return false;
1172}
1173
1174bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1175 int FI;
1176 if (parseFixedStackFrameIndex(FI))
1177 return true;
1178 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001179 return false;
1180}
1181
Alex Lorenz41df7d32015-07-28 17:09:52 +00001182bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001183 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001184 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001185 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001186 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001187 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001188 return error(Twine("use of undefined global value '") + Token.range() +
1189 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001190 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001191 }
1192 case MIToken::GlobalValue: {
1193 unsigned GVIdx;
1194 if (getUnsigned(GVIdx))
1195 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001196 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001197 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1198 "'");
Matthias Braune35861d2016-07-13 23:27:50 +00001199 GV = PFS.IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001200 break;
1201 }
1202 default:
1203 llvm_unreachable("The current token should be a global value");
1204 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001205 return false;
1206}
1207
1208bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1209 GlobalValue *GV = nullptr;
1210 if (parseGlobalValue(GV))
1211 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001212 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001213 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001214 if (parseOperandsOffset(Dest))
1215 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001216 return false;
1217}
1218
Alex Lorenzab980492015-07-20 20:51:18 +00001219bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1220 assert(Token.is(MIToken::ConstantPoolItem));
1221 unsigned ID;
1222 if (getUnsigned(ID))
1223 return true;
1224 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1225 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1226 return error("use of undefined constant '%const." + Twine(ID) + "'");
1227 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001228 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001229 if (parseOperandsOffset(Dest))
1230 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001231 return false;
1232}
1233
Alex Lorenz31d70682015-07-15 23:38:35 +00001234bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1235 assert(Token.is(MIToken::JumpTableIndex));
1236 unsigned ID;
1237 if (getUnsigned(ID))
1238 return true;
1239 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1240 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1241 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1242 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001243 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1244 return false;
1245}
1246
Alex Lorenz6ede3742015-07-21 16:59:53 +00001247bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001248 assert(Token.is(MIToken::ExternalSymbol));
1249 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001250 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001251 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001252 if (parseOperandsOffset(Dest))
1253 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001254 return false;
1255}
1256
Matthias Braunb74eb412016-03-28 18:18:46 +00001257bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1258 assert(Token.is(MIToken::SubRegisterIndex));
1259 StringRef Name = Token.stringValue();
1260 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1261 if (SubRegIndex == 0)
1262 return error(Twine("unknown subregister index '") + Name + "'");
1263 lex();
1264 Dest = MachineOperand::CreateImm(SubRegIndex);
1265 return false;
1266}
1267
Alex Lorenz44f29252015-07-22 21:07:04 +00001268bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001269 assert(Token.is(MIToken::exclaim));
1270 auto Loc = Token.location();
1271 lex();
1272 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1273 return error("expected metadata id after '!'");
1274 unsigned ID;
1275 if (getUnsigned(ID))
1276 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001277 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1278 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
Alex Lorenz35e44462015-07-22 17:58:46 +00001279 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1280 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001281 Node = NodeInfo->second.get();
1282 return false;
1283}
1284
1285bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1286 MDNode *Node = nullptr;
1287 if (parseMDNode(Node))
1288 return true;
1289 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001290 return false;
1291}
1292
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001293bool MIParser::parseCFIOffset(int &Offset) {
1294 if (Token.isNot(MIToken::IntegerLiteral))
1295 return error("expected a cfi offset");
1296 if (Token.integerValue().getMinSignedBits() > 32)
1297 return error("expected a 32 bit integer (the cfi offset is too large)");
1298 Offset = (int)Token.integerValue().getExtValue();
1299 lex();
1300 return false;
1301}
1302
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001303bool MIParser::parseCFIRegister(unsigned &Reg) {
1304 if (Token.isNot(MIToken::NamedRegister))
1305 return error("expected a cfi register");
1306 unsigned LLVMReg;
1307 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001308 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001309 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1310 assert(TRI && "Expected target register info");
1311 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1312 if (DwarfReg < 0)
1313 return error("invalid DWARF register");
1314 Reg = (unsigned)DwarfReg;
1315 lex();
1316 return false;
1317}
1318
1319bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1320 auto Kind = Token.kind();
1321 lex();
1322 auto &MMI = MF.getMMI();
1323 int Offset;
1324 unsigned Reg;
1325 unsigned CFIIndex;
1326 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001327 case MIToken::kw_cfi_same_value:
1328 if (parseCFIRegister(Reg))
1329 return true;
1330 CFIIndex =
1331 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1332 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001333 case MIToken::kw_cfi_offset:
1334 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1335 parseCFIOffset(Offset))
1336 return true;
1337 CFIIndex =
1338 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1339 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001340 case MIToken::kw_cfi_def_cfa_register:
1341 if (parseCFIRegister(Reg))
1342 return true;
1343 CFIIndex =
1344 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1345 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001346 case MIToken::kw_cfi_def_cfa_offset:
1347 if (parseCFIOffset(Offset))
1348 return true;
1349 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1350 CFIIndex = MMI.addFrameInst(
1351 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1352 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001353 case MIToken::kw_cfi_def_cfa:
1354 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1355 parseCFIOffset(Offset))
1356 return true;
1357 // NB: MCCFIInstruction::createDefCfa negates the offset.
1358 CFIIndex =
1359 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1360 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001361 default:
1362 // TODO: Parse the other CFI operands.
1363 llvm_unreachable("The current token should be a cfi operand");
1364 }
1365 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001366 return false;
1367}
1368
Alex Lorenzdeb53492015-07-28 17:28:03 +00001369bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1370 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001371 case MIToken::NamedIRBlock: {
1372 BB = dyn_cast_or_null<BasicBlock>(
1373 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001374 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001375 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001376 break;
1377 }
1378 case MIToken::IRBlock: {
1379 unsigned SlotNumber = 0;
1380 if (getUnsigned(SlotNumber))
1381 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001382 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001383 if (!BB)
1384 return error(Twine("use of undefined IR block '%ir-block.") +
1385 Twine(SlotNumber) + "'");
1386 break;
1387 }
1388 default:
1389 llvm_unreachable("The current token should be an IR block reference");
1390 }
1391 return false;
1392}
1393
1394bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1395 assert(Token.is(MIToken::kw_blockaddress));
1396 lex();
1397 if (expectAndConsume(MIToken::lparen))
1398 return true;
1399 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001400 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001401 return error("expected a global value");
1402 GlobalValue *GV = nullptr;
1403 if (parseGlobalValue(GV))
1404 return true;
1405 auto *F = dyn_cast<Function>(GV);
1406 if (!F)
1407 return error("expected an IR function reference");
1408 lex();
1409 if (expectAndConsume(MIToken::comma))
1410 return true;
1411 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001412 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001413 return error("expected an IR block reference");
1414 if (parseIRBlock(BB, *F))
1415 return true;
1416 lex();
1417 if (expectAndConsume(MIToken::rparen))
1418 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001419 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001420 if (parseOperandsOffset(Dest))
1421 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001422 return false;
1423}
1424
Alex Lorenzef5c1962015-07-28 23:02:45 +00001425bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1426 assert(Token.is(MIToken::kw_target_index));
1427 lex();
1428 if (expectAndConsume(MIToken::lparen))
1429 return true;
1430 if (Token.isNot(MIToken::Identifier))
1431 return error("expected the name of the target index");
1432 int Index = 0;
1433 if (getTargetIndex(Token.stringValue(), Index))
1434 return error("use of undefined target index '" + Token.stringValue() + "'");
1435 lex();
1436 if (expectAndConsume(MIToken::rparen))
1437 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001438 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001439 if (parseOperandsOffset(Dest))
1440 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001441 return false;
1442}
1443
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001444bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1445 assert(Token.is(MIToken::kw_liveout));
1446 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1447 assert(TRI && "Expected target register info");
1448 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1449 lex();
1450 if (expectAndConsume(MIToken::lparen))
1451 return true;
1452 while (true) {
1453 if (Token.isNot(MIToken::NamedRegister))
1454 return error("expected a named register");
1455 unsigned Reg = 0;
1456 if (parseRegister(Reg))
1457 return true;
1458 lex();
1459 Mask[Reg / 32] |= 1U << (Reg % 32);
1460 // TODO: Report an error if the same register is used more than once.
1461 if (Token.isNot(MIToken::comma))
1462 break;
1463 lex();
1464 }
1465 if (expectAndConsume(MIToken::rparen))
1466 return true;
1467 Dest = MachineOperand::CreateRegLiveOut(Mask);
1468 return false;
1469}
1470
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001471bool MIParser::parseMachineOperand(MachineOperand &Dest,
1472 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001473 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001474 case MIToken::kw_implicit:
1475 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001476 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001477 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001478 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001479 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001480 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001481 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001482 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001483 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001484 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001485 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001486 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001487 case MIToken::IntegerLiteral:
1488 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001489 case MIToken::IntegerType:
1490 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001491 case MIToken::kw_half:
1492 case MIToken::kw_float:
1493 case MIToken::kw_double:
1494 case MIToken::kw_x86_fp80:
1495 case MIToken::kw_fp128:
1496 case MIToken::kw_ppc_fp128:
1497 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001498 case MIToken::MachineBasicBlock:
1499 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001500 case MIToken::StackObject:
1501 return parseStackObjectOperand(Dest);
1502 case MIToken::FixedStackObject:
1503 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001504 case MIToken::GlobalValue:
1505 case MIToken::NamedGlobalValue:
1506 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001507 case MIToken::ConstantPoolItem:
1508 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001509 case MIToken::JumpTableIndex:
1510 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001511 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001512 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00001513 case MIToken::SubRegisterIndex:
1514 return parseSubRegisterIndexOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001515 case MIToken::exclaim:
1516 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001517 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001518 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001519 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001520 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001521 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001522 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001523 case MIToken::kw_blockaddress:
1524 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001525 case MIToken::kw_target_index:
1526 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001527 case MIToken::kw_liveout:
1528 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001529 case MIToken::Error:
1530 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001531 case MIToken::Identifier:
1532 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1533 Dest = MachineOperand::CreateRegMask(RegMask);
1534 lex();
1535 break;
1536 }
1537 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001538 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001539 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001540 return error("expected a machine operand");
1541 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001542 return false;
1543}
1544
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001545bool MIParser::parseMachineOperandAndTargetFlags(
1546 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001547 unsigned TF = 0;
1548 bool HasTargetFlags = false;
1549 if (Token.is(MIToken::kw_target_flags)) {
1550 HasTargetFlags = true;
1551 lex();
1552 if (expectAndConsume(MIToken::lparen))
1553 return true;
1554 if (Token.isNot(MIToken::Identifier))
1555 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001556 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1557 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1558 return error("use of undefined target flag '" + Token.stringValue() +
1559 "'");
1560 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001561 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001562 while (Token.is(MIToken::comma)) {
1563 lex();
1564 if (Token.isNot(MIToken::Identifier))
1565 return error("expected the name of the target flag");
1566 unsigned BitFlag = 0;
1567 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1568 return error("use of undefined target flag '" + Token.stringValue() +
1569 "'");
1570 // TODO: Report an error when using a duplicate bit target flag.
1571 TF |= BitFlag;
1572 lex();
1573 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001574 if (expectAndConsume(MIToken::rparen))
1575 return true;
1576 }
1577 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001578 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001579 return true;
1580 if (!HasTargetFlags)
1581 return false;
1582 if (Dest.isReg())
1583 return error(Loc, "register operands can't have target flags");
1584 Dest.setTargetFlags(TF);
1585 return false;
1586}
1587
Alex Lorenzdc24c172015-08-07 20:21:00 +00001588bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001589 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1590 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001591 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001592 bool IsNegative = Token.is(MIToken::minus);
1593 lex();
1594 if (Token.isNot(MIToken::IntegerLiteral))
1595 return error("expected an integer literal after '" + Sign + "'");
1596 if (Token.integerValue().getMinSignedBits() > 64)
1597 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001598 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001599 if (IsNegative)
1600 Offset = -Offset;
1601 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001602 return false;
1603}
1604
Alex Lorenz620f8912015-08-13 20:33:33 +00001605bool MIParser::parseAlignment(unsigned &Alignment) {
1606 assert(Token.is(MIToken::kw_align));
1607 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001608 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001609 return error("expected an integer literal after 'align'");
1610 if (getUnsigned(Alignment))
1611 return true;
1612 lex();
1613 return false;
1614}
1615
Alex Lorenzdc24c172015-08-07 20:21:00 +00001616bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1617 int64_t Offset = 0;
1618 if (parseOffset(Offset))
1619 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001620 Op.setOffset(Offset);
1621 return false;
1622}
1623
Alex Lorenz36593ac2015-08-19 23:27:07 +00001624bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001625 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001626 case MIToken::NamedIRValue: {
1627 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001628 break;
1629 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001630 case MIToken::IRValue: {
1631 unsigned SlotNumber = 0;
1632 if (getUnsigned(SlotNumber))
1633 return true;
1634 V = getIRValue(SlotNumber);
1635 break;
1636 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001637 case MIToken::NamedGlobalValue:
1638 case MIToken::GlobalValue: {
1639 GlobalValue *GV = nullptr;
1640 if (parseGlobalValue(GV))
1641 return true;
1642 V = GV;
1643 break;
1644 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001645 case MIToken::QuotedIRValue: {
1646 const Constant *C = nullptr;
1647 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1648 return true;
1649 V = C;
1650 break;
1651 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001652 default:
1653 llvm_unreachable("The current token should be an IR block reference");
1654 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001655 if (!V)
1656 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001657 return false;
1658}
1659
1660bool MIParser::getUint64(uint64_t &Result) {
1661 assert(Token.hasIntegerValue());
1662 if (Token.integerValue().getActiveBits() > 64)
1663 return error("expected 64-bit integer (too large)");
1664 Result = Token.integerValue().getZExtValue();
1665 return false;
1666}
1667
Justin Lebar0af80cd2016-07-15 18:26:59 +00001668bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
1669 const auto OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001670 switch (Token.kind()) {
1671 case MIToken::kw_volatile:
1672 Flags |= MachineMemOperand::MOVolatile;
1673 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001674 case MIToken::kw_non_temporal:
1675 Flags |= MachineMemOperand::MONonTemporal;
1676 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001677 case MIToken::kw_invariant:
1678 Flags |= MachineMemOperand::MOInvariant;
1679 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001680 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001681 default:
1682 llvm_unreachable("The current token should be a memory operand flag");
1683 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001684 if (OldFlags == Flags)
1685 // We know that the same flag is specified more than once when the flags
1686 // weren't modified.
1687 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001688 lex();
1689 return false;
1690}
1691
Alex Lorenz91097a32015-08-12 20:33:26 +00001692bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1693 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001694 case MIToken::kw_stack:
1695 PSV = MF.getPSVManager().getStack();
1696 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001697 case MIToken::kw_got:
1698 PSV = MF.getPSVManager().getGOT();
1699 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001700 case MIToken::kw_jump_table:
1701 PSV = MF.getPSVManager().getJumpTable();
1702 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001703 case MIToken::kw_constant_pool:
1704 PSV = MF.getPSVManager().getConstantPool();
1705 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001706 case MIToken::FixedStackObject: {
1707 int FI;
1708 if (parseFixedStackFrameIndex(FI))
1709 return true;
1710 PSV = MF.getPSVManager().getFixedStack(FI);
1711 // The token was already consumed, so use return here instead of break.
1712 return false;
1713 }
Matthias Braun3ef7df92016-06-08 00:47:07 +00001714 case MIToken::StackObject: {
1715 int FI;
1716 if (parseStackFrameIndex(FI))
1717 return true;
1718 PSV = MF.getPSVManager().getFixedStack(FI);
1719 // The token was already consumed, so use return here instead of break.
1720 return false;
1721 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001722 case MIToken::kw_call_entry: {
1723 lex();
1724 switch (Token.kind()) {
1725 case MIToken::GlobalValue:
1726 case MIToken::NamedGlobalValue: {
1727 GlobalValue *GV = nullptr;
1728 if (parseGlobalValue(GV))
1729 return true;
1730 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1731 break;
1732 }
1733 case MIToken::ExternalSymbol:
1734 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1735 MF.createExternalSymbolName(Token.stringValue()));
1736 break;
1737 default:
1738 return error(
1739 "expected a global value or an external symbol after 'call-entry'");
1740 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001741 break;
1742 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001743 default:
1744 llvm_unreachable("The current token should be pseudo source value");
1745 }
1746 lex();
1747 return false;
1748}
1749
1750bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001751 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001752 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Matthias Braun3ef7df92016-06-08 00:47:07 +00001753 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
1754 Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001755 const PseudoSourceValue *PSV = nullptr;
1756 if (parseMemoryPseudoSourceValue(PSV))
1757 return true;
1758 int64_t Offset = 0;
1759 if (parseOffset(Offset))
1760 return true;
1761 Dest = MachinePointerInfo(PSV, Offset);
1762 return false;
1763 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001764 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1765 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001766 Token.isNot(MIToken::NamedGlobalValue) &&
1767 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001768 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001769 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001770 if (parseIRValue(V))
1771 return true;
1772 if (!V->getType()->isPointerTy())
1773 return error("expected a pointer IR value");
1774 lex();
1775 int64_t Offset = 0;
1776 if (parseOffset(Offset))
1777 return true;
1778 Dest = MachinePointerInfo(V, Offset);
1779 return false;
1780}
1781
Alex Lorenz4af7e612015-08-03 23:08:19 +00001782bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1783 if (expectAndConsume(MIToken::lparen))
1784 return true;
Justin Lebar0af80cd2016-07-15 18:26:59 +00001785 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
Alex Lorenza518b792015-08-04 00:24:45 +00001786 while (Token.isMemoryOperandFlag()) {
1787 if (parseMemoryOperandFlag(Flags))
1788 return true;
1789 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001790 if (Token.isNot(MIToken::Identifier) ||
1791 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1792 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001793 if (Token.stringValue() == "load")
1794 Flags |= MachineMemOperand::MOLoad;
1795 else
1796 Flags |= MachineMemOperand::MOStore;
1797 lex();
1798
1799 if (Token.isNot(MIToken::IntegerLiteral))
1800 return error("expected the size integer literal after memory operation");
1801 uint64_t Size;
1802 if (getUint64(Size))
1803 return true;
1804 lex();
1805
Alex Lorenz91097a32015-08-12 20:33:26 +00001806 MachinePointerInfo Ptr = MachinePointerInfo();
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001807 if (Token.is(MIToken::Identifier)) {
1808 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1809 if (Token.stringValue() != Word)
1810 return error(Twine("expected '") + Word + "'");
1811 lex();
1812
1813 if (parseMachinePointerInfo(Ptr))
1814 return true;
1815 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001816 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001817 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001818 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001819 while (consumeIfPresent(MIToken::comma)) {
1820 switch (Token.kind()) {
1821 case MIToken::kw_align:
1822 if (parseAlignment(BaseAlignment))
1823 return true;
1824 break;
1825 case MIToken::md_tbaa:
1826 lex();
1827 if (parseMDNode(AAInfo.TBAA))
1828 return true;
1829 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001830 case MIToken::md_alias_scope:
1831 lex();
1832 if (parseMDNode(AAInfo.Scope))
1833 return true;
1834 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001835 case MIToken::md_noalias:
1836 lex();
1837 if (parseMDNode(AAInfo.NoAlias))
1838 return true;
1839 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001840 case MIToken::md_range:
1841 lex();
1842 if (parseMDNode(Range))
1843 return true;
1844 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001845 // TODO: Report an error on duplicate metadata nodes.
1846 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001847 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1848 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001849 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001850 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001851 if (expectAndConsume(MIToken::rparen))
1852 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001853 Dest =
1854 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001855 return false;
1856}
1857
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001858void MIParser::initNames2InstrOpCodes() {
1859 if (!Names2InstrOpCodes.empty())
1860 return;
1861 const auto *TII = MF.getSubtarget().getInstrInfo();
1862 assert(TII && "Expected target instruction info");
1863 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1864 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1865}
1866
1867bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1868 initNames2InstrOpCodes();
1869 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1870 if (InstrInfo == Names2InstrOpCodes.end())
1871 return true;
1872 OpCode = InstrInfo->getValue();
1873 return false;
1874}
1875
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001876void MIParser::initNames2Regs() {
1877 if (!Names2Regs.empty())
1878 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001879 // The '%noreg' register is the register 0.
1880 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001881 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1882 assert(TRI && "Expected target register info");
1883 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1884 bool WasInserted =
1885 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1886 .second;
1887 (void)WasInserted;
1888 assert(WasInserted && "Expected registers to be unique case-insensitively");
1889 }
1890}
1891
1892bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1893 initNames2Regs();
1894 auto RegInfo = Names2Regs.find(RegName);
1895 if (RegInfo == Names2Regs.end())
1896 return true;
1897 Reg = RegInfo->getValue();
1898 return false;
1899}
1900
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001901void MIParser::initNames2RegMasks() {
1902 if (!Names2RegMasks.empty())
1903 return;
1904 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1905 assert(TRI && "Expected target register info");
1906 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1907 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1908 assert(RegMasks.size() == RegMaskNames.size());
1909 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1910 Names2RegMasks.insert(
1911 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1912}
1913
1914const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1915 initNames2RegMasks();
1916 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1917 if (RegMaskInfo == Names2RegMasks.end())
1918 return nullptr;
1919 return RegMaskInfo->getValue();
1920}
1921
Alex Lorenz2eacca82015-07-13 23:24:34 +00001922void MIParser::initNames2SubRegIndices() {
1923 if (!Names2SubRegIndices.empty())
1924 return;
1925 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1926 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1927 Names2SubRegIndices.insert(
1928 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1929}
1930
1931unsigned MIParser::getSubRegIndex(StringRef Name) {
1932 initNames2SubRegIndices();
1933 auto SubRegInfo = Names2SubRegIndices.find(Name);
1934 if (SubRegInfo == Names2SubRegIndices.end())
1935 return 0;
1936 return SubRegInfo->getValue();
1937}
1938
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001939static void initSlots2BasicBlocks(
1940 const Function &F,
1941 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1942 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001943 MST.incorporateFunction(F);
1944 for (auto &BB : F) {
1945 if (BB.hasName())
1946 continue;
1947 int Slot = MST.getLocalSlot(&BB);
1948 if (Slot == -1)
1949 continue;
1950 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1951 }
1952}
1953
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001954static const BasicBlock *getIRBlockFromSlot(
1955 unsigned Slot,
1956 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001957 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1958 if (BlockInfo == Slots2BasicBlocks.end())
1959 return nullptr;
1960 return BlockInfo->second;
1961}
1962
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001963const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1964 if (Slots2BasicBlocks.empty())
1965 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1966 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1967}
1968
1969const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1970 if (&F == MF.getFunction())
1971 return getIRBlock(Slot);
1972 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1973 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1974 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1975}
1976
Alex Lorenzdd13be02015-08-19 23:31:05 +00001977static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
1978 DenseMap<unsigned, const Value *> &Slots2Values) {
1979 int Slot = MST.getLocalSlot(V);
1980 if (Slot == -1)
1981 return;
1982 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
1983}
1984
1985/// Creates the mapping from slot numbers to function's unnamed IR values.
1986static void initSlots2Values(const Function &F,
1987 DenseMap<unsigned, const Value *> &Slots2Values) {
1988 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1989 MST.incorporateFunction(F);
1990 for (const auto &Arg : F.args())
1991 mapValueToSlot(&Arg, MST, Slots2Values);
1992 for (const auto &BB : F) {
1993 mapValueToSlot(&BB, MST, Slots2Values);
1994 for (const auto &I : BB)
1995 mapValueToSlot(&I, MST, Slots2Values);
1996 }
1997}
1998
1999const Value *MIParser::getIRValue(unsigned Slot) {
2000 if (Slots2Values.empty())
2001 initSlots2Values(*MF.getFunction(), Slots2Values);
2002 auto ValueInfo = Slots2Values.find(Slot);
2003 if (ValueInfo == Slots2Values.end())
2004 return nullptr;
2005 return ValueInfo->second;
2006}
2007
Alex Lorenzef5c1962015-07-28 23:02:45 +00002008void MIParser::initNames2TargetIndices() {
2009 if (!Names2TargetIndices.empty())
2010 return;
2011 const auto *TII = MF.getSubtarget().getInstrInfo();
2012 assert(TII && "Expected target instruction info");
2013 auto Indices = TII->getSerializableTargetIndices();
2014 for (const auto &I : Indices)
2015 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2016}
2017
2018bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2019 initNames2TargetIndices();
2020 auto IndexInfo = Names2TargetIndices.find(Name);
2021 if (IndexInfo == Names2TargetIndices.end())
2022 return true;
2023 Index = IndexInfo->second;
2024 return false;
2025}
2026
Alex Lorenz49873a82015-08-06 00:44:07 +00002027void MIParser::initNames2DirectTargetFlags() {
2028 if (!Names2DirectTargetFlags.empty())
2029 return;
2030 const auto *TII = MF.getSubtarget().getInstrInfo();
2031 assert(TII && "Expected target instruction info");
2032 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2033 for (const auto &I : Flags)
2034 Names2DirectTargetFlags.insert(
2035 std::make_pair(StringRef(I.second), I.first));
2036}
2037
2038bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2039 initNames2DirectTargetFlags();
2040 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2041 if (FlagInfo == Names2DirectTargetFlags.end())
2042 return true;
2043 Flag = FlagInfo->second;
2044 return false;
2045}
2046
Alex Lorenzf3630112015-08-18 22:52:15 +00002047void MIParser::initNames2BitmaskTargetFlags() {
2048 if (!Names2BitmaskTargetFlags.empty())
2049 return;
2050 const auto *TII = MF.getSubtarget().getInstrInfo();
2051 assert(TII && "Expected target instruction info");
2052 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2053 for (const auto &I : Flags)
2054 Names2BitmaskTargetFlags.insert(
2055 std::make_pair(StringRef(I.second), I.first));
2056}
2057
2058bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2059 initNames2BitmaskTargetFlags();
2060 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2061 if (FlagInfo == Names2BitmaskTargetFlags.end())
2062 return true;
2063 Flag = FlagInfo->second;
2064 return false;
2065}
2066
Matthias Braun83947862016-07-13 22:23:23 +00002067bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2068 StringRef Src,
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002069 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002070 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002071}
2072
Matthias Braun83947862016-07-13 22:23:23 +00002073bool llvm::parseMachineInstructions(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002074 StringRef Src, SMDiagnostic &Error) {
2075 return MIParser(PFS, Error, Src).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002076}
Alex Lorenzf09df002015-06-30 18:16:42 +00002077
Matthias Braun83947862016-07-13 22:23:23 +00002078bool llvm::parseMBBReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002079 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +00002080 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002081 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002082}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002083
Matthias Braun83947862016-07-13 22:23:23 +00002084bool llvm::parseNamedRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002085 unsigned &Reg, StringRef Src,
Alex Lorenz9fab3702015-07-14 21:24:41 +00002086 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002087 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002088}
Alex Lorenz12045a42015-07-27 17:42:45 +00002089
Matthias Braun83947862016-07-13 22:23:23 +00002090bool llvm::parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002091 unsigned &Reg, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +00002092 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002093 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +00002094}
Alex Lorenza314d812015-08-18 22:26:26 +00002095
Matthias Braun83947862016-07-13 22:23:23 +00002096bool llvm::parseStackObjectReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002097 int &FI, StringRef Src,
Alex Lorenza314d812015-08-18 22:26:26 +00002098 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002099 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
Alex Lorenza314d812015-08-18 22:26:26 +00002100}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002101
Matthias Braun83947862016-07-13 22:23:23 +00002102bool llvm::parseMDNode(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002103 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2104 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002105}