blob: b3fd16f15889589c435c1c68c2bf8571260eb533 [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);
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000133 bool parseIRType(StringRef::iterator Loc, StringRef Source, unsigned &Read,
134 Type *&Ty);
135 // \p MustBeSized defines whether or not \p Ty must be sized.
136 bool parseIRType(StringRef::iterator Loc, Type *&Ty, bool MustBeSized = true);
Alex Lorenz05e38822015-08-05 18:52:21 +0000137 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000138 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000139 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000140 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000141 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000142 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000143 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000144 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000145 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000146 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000147 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +0000148 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000149 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000150 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000151 bool parseMDNode(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000152 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000153 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000154 bool parseCFIRegister(unsigned &Reg);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000155 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000156 bool parseIRBlock(BasicBlock *&BB, const Function &F);
157 bool parseBlockAddressOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000158 bool parseTargetIndexOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000159 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000160 bool parseMachineOperand(MachineOperand &Dest,
161 Optional<unsigned> &TiedDefIdx);
162 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
163 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000164 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000165 bool parseAlignment(unsigned &Alignment);
Alex Lorenz5672a892015-08-05 22:26:15 +0000166 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000167 bool parseIRValue(const Value *&V);
Justin Lebar0af80cd2016-07-15 18:26:59 +0000168 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000169 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
170 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000171 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000172
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000173private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000174 /// Convert the integer literal in the current token into an unsigned integer.
175 ///
176 /// Return true if an error occurred.
177 bool getUnsigned(unsigned &Result);
178
Alex Lorenz4af7e612015-08-03 23:08:19 +0000179 /// Convert the integer literal in the current token into an uint64.
180 ///
181 /// Return true if an error occurred.
182 bool getUint64(uint64_t &Result);
183
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000184 /// If the current token is of the given kind, consume it and return false.
185 /// Otherwise report an error and return true.
186 bool expectAndConsume(MIToken::TokenKind TokenKind);
187
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000188 /// If the current token is of the given kind, consume it and return true.
189 /// Otherwise return false.
190 bool consumeIfPresent(MIToken::TokenKind TokenKind);
191
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000192 void initNames2InstrOpCodes();
193
194 /// Try to convert an instruction name to an opcode. Return true if the
195 /// instruction name is invalid.
196 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000197
Alex Lorenze5a44662015-07-17 00:24:15 +0000198 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000199
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000200 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000201 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000202
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000203 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000204 const MCInstrDesc &MCID);
205
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000206 void initNames2Regs();
207
208 /// Try to convert a register name to a register number. Return true if the
209 /// register name is invalid.
210 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000211
212 void initNames2RegMasks();
213
214 /// Check if the given identifier is a name of a register mask.
215 ///
216 /// Return null if the identifier isn't a register mask.
217 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000218
219 void initNames2SubRegIndices();
220
221 /// Check if the given identifier is a name of a subregister index.
222 ///
223 /// Return 0 if the name isn't a subregister index class.
224 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000225
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000226 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000227 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000228
Alex Lorenzdd13be02015-08-19 23:31:05 +0000229 const Value *getIRValue(unsigned Slot);
230
Alex Lorenzef5c1962015-07-28 23:02:45 +0000231 void initNames2TargetIndices();
232
233 /// Try to convert a name of target index to the corresponding target index.
234 ///
235 /// Return true if the name isn't a name of a target index.
236 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000237
238 void initNames2DirectTargetFlags();
239
240 /// Try to convert a name of a direct target flag to the corresponding
241 /// target flag.
242 ///
243 /// Return true if the name isn't a name of a direct flag.
244 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000245
246 void initNames2BitmaskTargetFlags();
247
248 /// Try to convert a name of a bitmask target flag to the corresponding
249 /// target flag.
250 ///
251 /// Return true if the name isn't a name of a bitmask target flag.
252 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000253};
254
255} // end anonymous namespace
256
Matthias Braune35861d2016-07-13 23:27:50 +0000257MIParser::MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
258 StringRef Source)
259 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
260{}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000261
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000262void MIParser::lex(unsigned SkipChar) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000263 CurrentSource = lexMIToken(
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000264 CurrentSource.data() + SkipChar, Token,
Alex Lorenz91370c52015-06-22 20:37:46 +0000265 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
266}
267
268bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
269
270bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Matthias Braune35861d2016-07-13 23:27:50 +0000271 const SourceMgr &SM = *PFS.SM;
Alex Lorenz91370c52015-06-22 20:37:46 +0000272 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000273 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
274 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
275 // Create an ordinary diagnostic when the source manager's buffer is the
276 // source string.
277 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
278 return true;
279 }
280 // Create a diagnostic for a YAML string literal.
281 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
282 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
283 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000284 return true;
285}
286
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000287static const char *toString(MIToken::TokenKind TokenKind) {
288 switch (TokenKind) {
289 case MIToken::comma:
290 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000291 case MIToken::equal:
292 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000293 case MIToken::colon:
294 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000295 case MIToken::lparen:
296 return "'('";
297 case MIToken::rparen:
298 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000299 default:
300 return "<unknown token>";
301 }
302}
303
304bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
305 if (Token.isNot(TokenKind))
306 return error(Twine("expected ") + toString(TokenKind));
307 lex();
308 return false;
309}
310
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000311bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
312 if (Token.isNot(TokenKind))
313 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000314 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000315 return true;
316}
Alex Lorenz91370c52015-06-22 20:37:46 +0000317
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000318bool MIParser::parseBasicBlockDefinition(
319 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
320 assert(Token.is(MIToken::MachineBasicBlockLabel));
321 unsigned ID = 0;
322 if (getUnsigned(ID))
323 return true;
324 auto Loc = Token.location();
325 auto Name = Token.stringValue();
326 lex();
327 bool HasAddressTaken = false;
328 bool IsLandingPad = false;
329 unsigned Alignment = 0;
330 BasicBlock *BB = nullptr;
331 if (consumeIfPresent(MIToken::lparen)) {
332 do {
333 // TODO: Report an error when multiple same attributes are specified.
334 switch (Token.kind()) {
335 case MIToken::kw_address_taken:
336 HasAddressTaken = true;
337 lex();
338 break;
339 case MIToken::kw_landing_pad:
340 IsLandingPad = true;
341 lex();
342 break;
343 case MIToken::kw_align:
344 if (parseAlignment(Alignment))
345 return true;
346 break;
347 case MIToken::IRBlock:
348 // TODO: Report an error when both name and ir block are specified.
349 if (parseIRBlock(BB, *MF.getFunction()))
350 return true;
351 lex();
352 break;
353 default:
354 break;
355 }
356 } while (consumeIfPresent(MIToken::comma));
357 if (expectAndConsume(MIToken::rparen))
358 return true;
359 }
360 if (expectAndConsume(MIToken::colon))
361 return true;
362
363 if (!Name.empty()) {
364 BB = dyn_cast_or_null<BasicBlock>(
365 MF.getFunction()->getValueSymbolTable().lookup(Name));
366 if (!BB)
367 return error(Loc, Twine("basic block '") + Name +
368 "' is not defined in the function '" +
369 MF.getName() + "'");
370 }
371 auto *MBB = MF.CreateMachineBasicBlock(BB);
372 MF.insert(MF.end(), MBB);
373 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
374 if (!WasInserted)
375 return error(Loc, Twine("redefinition of machine basic block with id #") +
376 Twine(ID));
377 if (Alignment)
378 MBB->setAlignment(Alignment);
379 if (HasAddressTaken)
380 MBB->setHasAddressTaken();
Reid Kleckner0e288232015-08-27 23:27:47 +0000381 MBB->setIsEHPad(IsLandingPad);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000382 return false;
383}
384
385bool MIParser::parseBasicBlockDefinitions(
386 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
387 lex();
388 // Skip until the first machine basic block.
389 while (Token.is(MIToken::Newline))
390 lex();
391 if (Token.isErrorOrEOF())
392 return Token.isError();
393 if (Token.isNot(MIToken::MachineBasicBlockLabel))
394 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000395 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000396 do {
397 if (parseBasicBlockDefinition(MBBSlots))
398 return true;
399 bool IsAfterNewline = false;
400 // Skip until the next machine basic block.
401 while (true) {
402 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
403 Token.isErrorOrEOF())
404 break;
405 else if (Token.is(MIToken::MachineBasicBlockLabel))
406 return error("basic block definition should be located at the start of "
407 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000408 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000409 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000410 continue;
411 }
412 IsAfterNewline = false;
413 if (Token.is(MIToken::lbrace))
414 ++BraceDepth;
415 if (Token.is(MIToken::rbrace)) {
416 if (!BraceDepth)
417 return error("extraneous closing brace ('}')");
418 --BraceDepth;
419 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000420 lex();
421 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000422 // Verify that we closed all of the '{' at the end of a file or a block.
423 if (!Token.isError() && BraceDepth)
424 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000425 } while (!Token.isErrorOrEOF());
426 return Token.isError();
427}
428
429bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
430 assert(Token.is(MIToken::kw_liveins));
431 lex();
432 if (expectAndConsume(MIToken::colon))
433 return true;
434 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
435 return false;
436 do {
437 if (Token.isNot(MIToken::NamedRegister))
438 return error("expected a named register");
439 unsigned Reg = 0;
440 if (parseRegister(Reg))
441 return true;
442 MBB.addLiveIn(Reg);
443 lex();
444 } while (consumeIfPresent(MIToken::comma));
445 return false;
446}
447
448bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
449 assert(Token.is(MIToken::kw_successors));
450 lex();
451 if (expectAndConsume(MIToken::colon))
452 return true;
453 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
454 return false;
455 do {
456 if (Token.isNot(MIToken::MachineBasicBlock))
457 return error("expected a machine basic block reference");
458 MachineBasicBlock *SuccMBB = nullptr;
459 if (parseMBBReference(SuccMBB))
460 return true;
461 lex();
462 unsigned Weight = 0;
463 if (consumeIfPresent(MIToken::lparen)) {
464 if (Token.isNot(MIToken::IntegerLiteral))
465 return error("expected an integer literal after '('");
466 if (getUnsigned(Weight))
467 return true;
468 lex();
469 if (expectAndConsume(MIToken::rparen))
470 return true;
471 }
Cong Houd97c1002015-12-01 05:29:22 +0000472 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000473 } while (consumeIfPresent(MIToken::comma));
Cong Houd97c1002015-12-01 05:29:22 +0000474 MBB.normalizeSuccProbs();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000475 return false;
476}
477
478bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
479 // Skip the definition.
480 assert(Token.is(MIToken::MachineBasicBlockLabel));
481 lex();
482 if (consumeIfPresent(MIToken::lparen)) {
483 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
484 lex();
485 consumeIfPresent(MIToken::rparen);
486 }
487 consumeIfPresent(MIToken::colon);
488
489 // Parse the liveins and successors.
490 // N.B: Multiple lists of successors and liveins are allowed and they're
491 // merged into one.
492 // Example:
493 // liveins: %edi
494 // liveins: %esi
495 //
496 // is equivalent to
497 // liveins: %edi, %esi
498 while (true) {
499 if (Token.is(MIToken::kw_successors)) {
500 if (parseBasicBlockSuccessors(MBB))
501 return true;
502 } else if (Token.is(MIToken::kw_liveins)) {
503 if (parseBasicBlockLiveins(MBB))
504 return true;
505 } else if (consumeIfPresent(MIToken::Newline)) {
506 continue;
507 } else
508 break;
509 if (!Token.isNewlineOrEOF())
510 return error("expected line break at the end of a list");
511 lex();
512 }
513
514 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000515 bool IsInBundle = false;
516 MachineInstr *PrevMI = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000517 while (true) {
518 if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
519 return false;
520 else if (consumeIfPresent(MIToken::Newline))
521 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000522 if (consumeIfPresent(MIToken::rbrace)) {
523 // The first parsing pass should verify that all closing '}' have an
524 // opening '{'.
525 assert(IsInBundle);
526 IsInBundle = false;
527 continue;
528 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000529 MachineInstr *MI = nullptr;
530 if (parse(MI))
531 return true;
532 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000533 if (IsInBundle) {
534 PrevMI->setFlag(MachineInstr::BundledSucc);
535 MI->setFlag(MachineInstr::BundledPred);
536 }
537 PrevMI = MI;
538 if (Token.is(MIToken::lbrace)) {
539 if (IsInBundle)
540 return error("nested instruction bundles are not allowed");
541 lex();
542 // This instruction is the start of the bundle.
543 MI->setFlag(MachineInstr::BundledSucc);
544 IsInBundle = true;
545 if (!Token.is(MIToken::Newline))
546 // The next instruction can be on the same line.
547 continue;
548 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000549 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
550 lex();
551 }
552 return false;
553}
554
555bool MIParser::parseBasicBlocks() {
556 lex();
557 // Skip until the first machine basic block.
558 while (Token.is(MIToken::Newline))
559 lex();
560 if (Token.isErrorOrEOF())
561 return Token.isError();
562 // The first parsing pass should have verified that this token is a MBB label
563 // in the 'parseBasicBlockDefinitions' method.
564 assert(Token.is(MIToken::MachineBasicBlockLabel));
565 do {
566 MachineBasicBlock *MBB = nullptr;
567 if (parseMBBReference(MBB))
568 return true;
569 if (parseBasicBlock(*MBB))
570 return true;
571 // The method 'parseBasicBlock' should parse the whole block until the next
572 // block or the end of file.
573 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
574 } while (Token.isNot(MIToken::Eof));
575 return false;
576}
577
578bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000579 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000580 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000581 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000582 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000583 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000584 Optional<unsigned> TiedDefIdx;
585 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000586 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000587 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000588 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000589 if (Token.isNot(MIToken::comma))
590 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000591 lex();
592 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000593 if (!Operands.empty() && expectAndConsume(MIToken::equal))
594 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000595
Alex Lorenze5a44662015-07-17 00:24:15 +0000596 unsigned OpCode, Flags = 0;
597 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000598 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000599
Quentin Colombet85199672016-03-08 00:20:48 +0000600 Type *Ty = nullptr;
601 if (isPreISelGenericOpcode(OpCode)) {
602 // For generic opcode, a type is mandatory.
603 auto Loc = Token.location();
604 if (parseIRType(Loc, Ty))
605 return true;
Quentin Colombet85199672016-03-08 00:20:48 +0000606 }
607
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000608 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000609 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000610 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000611 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000612 Optional<unsigned> TiedDefIdx;
613 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000614 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000615 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000616 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000617 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
618 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000619 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000620 if (Token.isNot(MIToken::comma))
621 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000622 lex();
623 }
624
Alex Lorenz46d760d2015-07-22 21:15:11 +0000625 DebugLoc DebugLocation;
626 if (Token.is(MIToken::kw_debug_location)) {
627 lex();
628 if (Token.isNot(MIToken::exclaim))
629 return error("expected a metadata node after 'debug-location'");
630 MDNode *Node = nullptr;
631 if (parseMDNode(Node))
632 return true;
633 DebugLocation = DebugLoc(Node);
634 }
635
Alex Lorenz4af7e612015-08-03 23:08:19 +0000636 // Parse the machine memory operands.
637 SmallVector<MachineMemOperand *, 2> MemOperands;
638 if (Token.is(MIToken::coloncolon)) {
639 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000640 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000641 MachineMemOperand *MemOp = nullptr;
642 if (parseMachineMemoryOperand(MemOp))
643 return true;
644 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000645 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000646 break;
647 if (Token.isNot(MIToken::comma))
648 return error("expected ',' before the next machine memory operand");
649 lex();
650 }
651 }
652
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000653 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000654 if (!MCID.isVariadic()) {
655 // FIXME: Move the implicit operand verification to the machine verifier.
656 if (verifyImplicitOperands(Operands, MCID))
657 return true;
658 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000659
Alex Lorenzcb268d42015-07-06 23:07:26 +0000660 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000661 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000662 MI->setFlags(Flags);
Quentin Colombet85199672016-03-08 00:20:48 +0000663 if (Ty)
664 MI->setType(Ty);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000665 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000666 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000667 if (assignRegisterTies(*MI, Operands))
668 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000669 if (MemOperands.empty())
670 return false;
671 MachineInstr::mmo_iterator MemRefs =
672 MF.allocateMemRefsArray(MemOperands.size());
673 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
674 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000675 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000676}
677
Alex Lorenz1ea60892015-07-27 20:29:27 +0000678bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000679 lex();
680 if (Token.isNot(MIToken::MachineBasicBlock))
681 return error("expected a machine basic block reference");
682 if (parseMBBReference(MBB))
683 return true;
684 lex();
685 if (Token.isNot(MIToken::Eof))
686 return error(
687 "expected end of string after the machine basic block reference");
688 return false;
689}
690
Alex Lorenz1ea60892015-07-27 20:29:27 +0000691bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000692 lex();
693 if (Token.isNot(MIToken::NamedRegister))
694 return error("expected a named register");
695 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000696 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000697 lex();
698 if (Token.isNot(MIToken::Eof))
699 return error("expected end of string after the register reference");
700 return false;
701}
702
Alex Lorenz12045a42015-07-27 17:42:45 +0000703bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
704 lex();
705 if (Token.isNot(MIToken::VirtualRegister))
706 return error("expected a virtual register");
707 if (parseRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000708 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000709 lex();
710 if (Token.isNot(MIToken::Eof))
711 return error("expected end of string after the register reference");
712 return false;
713}
714
Alex Lorenza314d812015-08-18 22:26:26 +0000715bool MIParser::parseStandaloneStackObject(int &FI) {
716 lex();
717 if (Token.isNot(MIToken::StackObject))
718 return error("expected a stack object");
719 if (parseStackFrameIndex(FI))
720 return true;
721 if (Token.isNot(MIToken::Eof))
722 return error("expected end of string after the stack object reference");
723 return false;
724}
725
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000726bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
727 lex();
728 if (Token.isNot(MIToken::exclaim))
729 return error("expected a metadata node");
730 if (parseMDNode(Node))
731 return true;
732 if (Token.isNot(MIToken::Eof))
733 return error("expected end of string after the metadata node");
734 return false;
735}
736
Alex Lorenz36962cd2015-07-07 02:08:46 +0000737static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
738 assert(MO.isImplicit());
739 return MO.isDef() ? "implicit-def" : "implicit";
740}
741
742static std::string getRegisterName(const TargetRegisterInfo *TRI,
743 unsigned Reg) {
744 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
745 return StringRef(TRI->getName(Reg)).lower();
746}
747
Alex Lorenz0153e592015-09-10 14:04:34 +0000748/// Return true if the parsed machine operands contain a given machine operand.
749static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
750 ArrayRef<ParsedMachineOperand> Operands) {
751 for (const auto &I : Operands) {
752 if (ImplicitOperand.isIdenticalTo(I.Operand))
753 return true;
754 }
755 return false;
756}
757
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000758bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
759 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000760 if (MCID.isCall())
761 // We can't verify call instructions as they can contain arbitrary implicit
762 // register and register mask operands.
763 return false;
764
765 // Gather all the expected implicit operands.
766 SmallVector<MachineOperand, 4> ImplicitOperands;
767 if (MCID.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000768 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000769 ImplicitOperands.push_back(
770 MachineOperand::CreateReg(*ImpDefs, true, true));
771 if (MCID.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000772 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000773 ImplicitOperands.push_back(
774 MachineOperand::CreateReg(*ImpUses, false, true));
775
776 const auto *TRI = MF.getSubtarget().getRegisterInfo();
777 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000778 for (const auto &I : ImplicitOperands) {
779 if (isImplicitOperandIn(I, Operands))
780 continue;
781 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000782 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000783 printImplicitRegisterFlag(I) + " %" +
784 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000785 }
786 return false;
787}
788
Alex Lorenze5a44662015-07-17 00:24:15 +0000789bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
790 if (Token.is(MIToken::kw_frame_setup)) {
791 Flags |= MachineInstr::FrameSetup;
792 lex();
793 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000794 if (Token.isNot(MIToken::Identifier))
795 return error("expected a machine instruction");
796 StringRef InstrName = Token.stringValue();
797 if (parseInstrName(InstrName, OpCode))
798 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000799 lex();
800 return false;
801}
802
803bool MIParser::parseRegister(unsigned &Reg) {
804 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000805 case MIToken::underscore:
806 Reg = 0;
807 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000808 case MIToken::NamedRegister: {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000809 StringRef Name = Token.stringValue();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000810 if (getRegisterByName(Name, Reg))
811 return error(Twine("unknown register name '") + Name + "'");
812 break;
813 }
Alex Lorenz53464512015-07-10 22:51:20 +0000814 case MIToken::VirtualRegister: {
815 unsigned ID;
816 if (getUnsigned(ID))
817 return true;
818 const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
819 if (RegInfo == PFS.VirtualRegisterSlots.end())
820 return error(Twine("use of undefined virtual register '%") + Twine(ID) +
821 "'");
822 Reg = RegInfo->second;
823 break;
824 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000825 // TODO: Parse other register kinds.
826 default:
827 llvm_unreachable("The current token should be a register");
828 }
829 return false;
830}
831
Alex Lorenzcb268d42015-07-06 23:07:26 +0000832bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000833 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000834 switch (Token.kind()) {
835 case MIToken::kw_implicit:
836 Flags |= RegState::Implicit;
837 break;
838 case MIToken::kw_implicit_define:
839 Flags |= RegState::ImplicitDefine;
840 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000841 case MIToken::kw_def:
842 Flags |= RegState::Define;
843 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000844 case MIToken::kw_dead:
845 Flags |= RegState::Dead;
846 break;
Alex Lorenz495ad872015-07-08 21:23:34 +0000847 case MIToken::kw_killed:
848 Flags |= RegState::Kill;
849 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +0000850 case MIToken::kw_undef:
851 Flags |= RegState::Undef;
852 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +0000853 case MIToken::kw_internal:
854 Flags |= RegState::InternalRead;
855 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000856 case MIToken::kw_early_clobber:
857 Flags |= RegState::EarlyClobber;
858 break;
Alex Lorenz90752582015-08-05 17:41:17 +0000859 case MIToken::kw_debug_use:
860 Flags |= RegState::Debug;
861 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000862 default:
863 llvm_unreachable("The current token should be a register flag");
864 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +0000865 if (OldFlags == Flags)
866 // We know that the same flag is specified more than once when the flags
867 // weren't modified.
868 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +0000869 lex();
870 return false;
871}
872
Alex Lorenz2eacca82015-07-13 23:24:34 +0000873bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
874 assert(Token.is(MIToken::colon));
875 lex();
876 if (Token.isNot(MIToken::Identifier))
877 return error("expected a subregister index after ':'");
878 auto Name = Token.stringValue();
879 SubReg = getSubRegIndex(Name);
880 if (!SubReg)
881 return error(Twine("use of unknown subregister index '") + Name + "'");
882 lex();
883 return false;
884}
885
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000886bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
887 if (!consumeIfPresent(MIToken::kw_tied_def))
888 return error("expected 'tied-def' after '('");
889 if (Token.isNot(MIToken::IntegerLiteral))
890 return error("expected an integer literal after 'tied-def'");
891 if (getUnsigned(TiedDefIdx))
892 return true;
893 lex();
894 if (expectAndConsume(MIToken::rparen))
895 return true;
896 return false;
897}
898
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000899bool MIParser::parseSize(unsigned &Size) {
900 if (Token.isNot(MIToken::IntegerLiteral))
901 return error("expected an integer literal for the size");
902 if (getUnsigned(Size))
903 return true;
904 lex();
905 if (expectAndConsume(MIToken::rparen))
906 return true;
907 return false;
908}
909
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000910bool MIParser::assignRegisterTies(MachineInstr &MI,
911 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000912 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
913 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
914 if (!Operands[I].TiedDefIdx)
915 continue;
916 // The parser ensures that this operand is a register use, so we just have
917 // to check the tied-def operand.
918 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
919 if (DefIdx >= E)
920 return error(Operands[I].Begin,
921 Twine("use of invalid tied-def operand index '" +
922 Twine(DefIdx) + "'; instruction has only ") +
923 Twine(E) + " operands");
924 const auto &DefOperand = Operands[DefIdx].Operand;
925 if (!DefOperand.isReg() || !DefOperand.isDef())
926 // FIXME: add note with the def operand.
927 return error(Operands[I].Begin,
928 Twine("use of invalid tied-def operand index '") +
929 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
930 " isn't a defined register");
931 // Check that the tied-def operand wasn't tied elsewhere.
932 for (const auto &TiedPair : TiedRegisterPairs) {
933 if (TiedPair.first == DefIdx)
934 return error(Operands[I].Begin,
935 Twine("the tied-def operand #") + Twine(DefIdx) +
936 " is already tied with another register operand");
937 }
938 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
939 }
940 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
941 // indices must be less than tied max.
942 for (const auto &TiedPair : TiedRegisterPairs)
943 MI.tieOperands(TiedPair.first, TiedPair.second);
944 return false;
945}
946
947bool MIParser::parseRegisterOperand(MachineOperand &Dest,
948 Optional<unsigned> &TiedDefIdx,
949 bool IsDef) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000950 unsigned Reg;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000951 unsigned Flags = IsDef ? RegState::Define : 0;
952 while (Token.isRegisterFlag()) {
953 if (parseRegisterFlag(Flags))
954 return true;
955 }
956 if (!Token.isRegister())
957 return error("expected a register after register flags");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000958 if (parseRegister(Reg))
959 return true;
960 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +0000961 unsigned SubReg = 0;
962 if (Token.is(MIToken::colon)) {
963 if (parseSubRegisterIndex(SubReg))
964 return true;
Matthias Braun5d00b322016-07-16 01:36:18 +0000965 if (!TargetRegisterInfo::isVirtualRegister(Reg))
966 return error("subregister index expects a virtual register");
Alex Lorenz2eacca82015-07-13 23:24:34 +0000967 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000968 if ((Flags & RegState::Define) == 0) {
969 if (consumeIfPresent(MIToken::lparen)) {
970 unsigned Idx;
971 if (parseRegisterTiedDefIndex(Idx))
972 return true;
973 TiedDefIdx = Idx;
974 }
975 } else if (consumeIfPresent(MIToken::lparen)) {
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");
979 unsigned Size;
980 if (parseSize(Size))
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000981 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000982
983 MachineRegisterInfo &MRI = MF.getRegInfo();
984 MRI.setSize(Reg, Size);
Quentin Colombet2c646962016-06-08 23:27:46 +0000985 } else if (PFS.GenericVRegs.count(Reg)) {
986 // Generic virtual registers must have a size.
987 // If we end up here this means the size hasn't been specified and
988 // this is bad!
989 return error("generic virtual registers must have a size");
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000990 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000991 Dest = MachineOperand::CreateReg(
992 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000993 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +0000994 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
995 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000996 return false;
997}
998
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000999bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1000 assert(Token.is(MIToken::IntegerLiteral));
1001 const APSInt &Int = Token.integerValue();
1002 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +00001003 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001004 Dest = MachineOperand::CreateImm(Int.getExtValue());
1005 lex();
1006 return false;
1007}
1008
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001009bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1010 const Constant *&C) {
1011 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001012 SMDiagnostic Err;
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001013 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
Matthias Braune35861d2016-07-13 23:27:50 +00001014 &PFS.IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001015 if (!C)
1016 return error(Loc + Err.getColumnNo(), Err.getMessage());
1017 return false;
1018}
1019
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001020bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1021 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1022 return true;
1023 lex();
1024 return false;
1025}
1026
Quentin Colombet85199672016-03-08 00:20:48 +00001027bool MIParser::parseIRType(StringRef::iterator Loc, StringRef StringValue,
Quentin Colombet287c6bb2016-03-08 00:57:31 +00001028 unsigned &Read, Type *&Ty) {
Quentin Colombet85199672016-03-08 00:20:48 +00001029 auto Source = StringValue.str(); // The source has to be null terminated.
1030 SMDiagnostic Err;
Quentin Colombet287c6bb2016-03-08 00:57:31 +00001031 Ty = parseTypeAtBeginning(Source.c_str(), Read, Err,
Matthias Braune35861d2016-07-13 23:27:50 +00001032 *MF.getFunction()->getParent(), &PFS.IRSlots);
Quentin Colombet85199672016-03-08 00:20:48 +00001033 if (!Ty)
1034 return error(Loc + Err.getColumnNo(), Err.getMessage());
1035 return false;
1036}
1037
Quentin Colombet287c6bb2016-03-08 00:57:31 +00001038bool MIParser::parseIRType(StringRef::iterator Loc, Type *&Ty,
1039 bool MustBeSized) {
1040 // At this point we enter in the IR world, i.e., to get the correct type,
1041 // we need to hand off the whole string, not just the current token.
1042 // E.g., <4 x i64> would give '<' as a token and there is not much
1043 // the IR parser can do with that.
1044 unsigned Read = 0;
1045 if (parseIRType(Loc, StringRef(Loc), Read, Ty))
Quentin Colombet85199672016-03-08 00:20:48 +00001046 return true;
Quentin Colombet287c6bb2016-03-08 00:57:31 +00001047 // The type must be sized, otherwise there is not much the backend
1048 // can do with it.
1049 if (MustBeSized && !Ty->isSized())
1050 return error("expected a sized type");
1051 // The next token is Read characters from the Loc.
1052 // However, the current location is not Loc, but Loc + the length of Token.
1053 // Therefore, subtract the length of Token (range().end() - Loc) to the
1054 // number of characters to skip before the next token.
1055 lex(Read - (Token.range().end() - Loc));
Quentin Colombet85199672016-03-08 00:20:48 +00001056 return false;
1057}
1058
Alex Lorenz05e38822015-08-05 18:52:21 +00001059bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1060 assert(Token.is(MIToken::IntegerType));
1061 auto Loc = Token.location();
1062 lex();
1063 if (Token.isNot(MIToken::IntegerLiteral))
1064 return error("expected an integer literal");
1065 const Constant *C = nullptr;
1066 if (parseIRConstant(Loc, C))
1067 return true;
1068 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1069 return false;
1070}
1071
Alex Lorenzad156fb2015-07-31 20:49:21 +00001072bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1073 auto Loc = Token.location();
1074 lex();
1075 if (Token.isNot(MIToken::FloatingPointLiteral))
1076 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001077 const Constant *C = nullptr;
1078 if (parseIRConstant(Loc, C))
1079 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001080 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1081 return false;
1082}
1083
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001084bool MIParser::getUnsigned(unsigned &Result) {
1085 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1086 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1087 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1088 if (Val64 == Limit)
1089 return error("expected 32-bit integer (too large)");
1090 Result = Val64;
1091 return false;
1092}
1093
Alex Lorenzf09df002015-06-30 18:16:42 +00001094bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001095 assert(Token.is(MIToken::MachineBasicBlock) ||
1096 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001097 unsigned Number;
1098 if (getUnsigned(Number))
1099 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001100 auto MBBInfo = PFS.MBBSlots.find(Number);
1101 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001102 return error(Twine("use of undefined machine basic block #") +
1103 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001104 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001105 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1106 return error(Twine("the name of machine basic block #") + Twine(Number) +
1107 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001108 return false;
1109}
1110
1111bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1112 MachineBasicBlock *MBB;
1113 if (parseMBBReference(MBB))
1114 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001115 Dest = MachineOperand::CreateMBB(MBB);
1116 lex();
1117 return false;
1118}
1119
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001120bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001121 assert(Token.is(MIToken::StackObject));
1122 unsigned ID;
1123 if (getUnsigned(ID))
1124 return true;
1125 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1126 if (ObjectInfo == PFS.StackObjectSlots.end())
1127 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1128 "'");
1129 StringRef Name;
1130 if (const auto *Alloca =
1131 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
1132 Name = Alloca->getName();
1133 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1134 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1135 "' isn't '" + Token.stringValue() + "'");
1136 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001137 FI = ObjectInfo->second;
1138 return false;
1139}
1140
1141bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1142 int FI;
1143 if (parseStackFrameIndex(FI))
1144 return true;
1145 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001146 return false;
1147}
1148
Alex Lorenzea882122015-08-12 21:17:02 +00001149bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001150 assert(Token.is(MIToken::FixedStackObject));
1151 unsigned ID;
1152 if (getUnsigned(ID))
1153 return true;
1154 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1155 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1156 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1157 Twine(ID) + "'");
1158 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001159 FI = ObjectInfo->second;
1160 return false;
1161}
1162
1163bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1164 int FI;
1165 if (parseFixedStackFrameIndex(FI))
1166 return true;
1167 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001168 return false;
1169}
1170
Alex Lorenz41df7d32015-07-28 17:09:52 +00001171bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001172 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001173 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001174 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001175 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001176 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001177 return error(Twine("use of undefined global value '") + Token.range() +
1178 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001179 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001180 }
1181 case MIToken::GlobalValue: {
1182 unsigned GVIdx;
1183 if (getUnsigned(GVIdx))
1184 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001185 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001186 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1187 "'");
Matthias Braune35861d2016-07-13 23:27:50 +00001188 GV = PFS.IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001189 break;
1190 }
1191 default:
1192 llvm_unreachable("The current token should be a global value");
1193 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001194 return false;
1195}
1196
1197bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1198 GlobalValue *GV = nullptr;
1199 if (parseGlobalValue(GV))
1200 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001201 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001202 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001203 if (parseOperandsOffset(Dest))
1204 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001205 return false;
1206}
1207
Alex Lorenzab980492015-07-20 20:51:18 +00001208bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1209 assert(Token.is(MIToken::ConstantPoolItem));
1210 unsigned ID;
1211 if (getUnsigned(ID))
1212 return true;
1213 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1214 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1215 return error("use of undefined constant '%const." + Twine(ID) + "'");
1216 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001217 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001218 if (parseOperandsOffset(Dest))
1219 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001220 return false;
1221}
1222
Alex Lorenz31d70682015-07-15 23:38:35 +00001223bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1224 assert(Token.is(MIToken::JumpTableIndex));
1225 unsigned ID;
1226 if (getUnsigned(ID))
1227 return true;
1228 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1229 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1230 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1231 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001232 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1233 return false;
1234}
1235
Alex Lorenz6ede3742015-07-21 16:59:53 +00001236bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001237 assert(Token.is(MIToken::ExternalSymbol));
1238 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001239 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001240 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001241 if (parseOperandsOffset(Dest))
1242 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001243 return false;
1244}
1245
Matthias Braunb74eb412016-03-28 18:18:46 +00001246bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1247 assert(Token.is(MIToken::SubRegisterIndex));
1248 StringRef Name = Token.stringValue();
1249 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1250 if (SubRegIndex == 0)
1251 return error(Twine("unknown subregister index '") + Name + "'");
1252 lex();
1253 Dest = MachineOperand::CreateImm(SubRegIndex);
1254 return false;
1255}
1256
Alex Lorenz44f29252015-07-22 21:07:04 +00001257bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001258 assert(Token.is(MIToken::exclaim));
1259 auto Loc = Token.location();
1260 lex();
1261 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1262 return error("expected metadata id after '!'");
1263 unsigned ID;
1264 if (getUnsigned(ID))
1265 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001266 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1267 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
Alex Lorenz35e44462015-07-22 17:58:46 +00001268 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1269 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001270 Node = NodeInfo->second.get();
1271 return false;
1272}
1273
1274bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1275 MDNode *Node = nullptr;
1276 if (parseMDNode(Node))
1277 return true;
1278 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001279 return false;
1280}
1281
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001282bool MIParser::parseCFIOffset(int &Offset) {
1283 if (Token.isNot(MIToken::IntegerLiteral))
1284 return error("expected a cfi offset");
1285 if (Token.integerValue().getMinSignedBits() > 32)
1286 return error("expected a 32 bit integer (the cfi offset is too large)");
1287 Offset = (int)Token.integerValue().getExtValue();
1288 lex();
1289 return false;
1290}
1291
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001292bool MIParser::parseCFIRegister(unsigned &Reg) {
1293 if (Token.isNot(MIToken::NamedRegister))
1294 return error("expected a cfi register");
1295 unsigned LLVMReg;
1296 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001297 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001298 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1299 assert(TRI && "Expected target register info");
1300 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1301 if (DwarfReg < 0)
1302 return error("invalid DWARF register");
1303 Reg = (unsigned)DwarfReg;
1304 lex();
1305 return false;
1306}
1307
1308bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1309 auto Kind = Token.kind();
1310 lex();
1311 auto &MMI = MF.getMMI();
1312 int Offset;
1313 unsigned Reg;
1314 unsigned CFIIndex;
1315 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001316 case MIToken::kw_cfi_same_value:
1317 if (parseCFIRegister(Reg))
1318 return true;
1319 CFIIndex =
1320 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1321 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001322 case MIToken::kw_cfi_offset:
1323 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1324 parseCFIOffset(Offset))
1325 return true;
1326 CFIIndex =
1327 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1328 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001329 case MIToken::kw_cfi_def_cfa_register:
1330 if (parseCFIRegister(Reg))
1331 return true;
1332 CFIIndex =
1333 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1334 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001335 case MIToken::kw_cfi_def_cfa_offset:
1336 if (parseCFIOffset(Offset))
1337 return true;
1338 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1339 CFIIndex = MMI.addFrameInst(
1340 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1341 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001342 case MIToken::kw_cfi_def_cfa:
1343 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1344 parseCFIOffset(Offset))
1345 return true;
1346 // NB: MCCFIInstruction::createDefCfa negates the offset.
1347 CFIIndex =
1348 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1349 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001350 default:
1351 // TODO: Parse the other CFI operands.
1352 llvm_unreachable("The current token should be a cfi operand");
1353 }
1354 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001355 return false;
1356}
1357
Alex Lorenzdeb53492015-07-28 17:28:03 +00001358bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1359 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001360 case MIToken::NamedIRBlock: {
1361 BB = dyn_cast_or_null<BasicBlock>(
1362 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001363 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001364 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001365 break;
1366 }
1367 case MIToken::IRBlock: {
1368 unsigned SlotNumber = 0;
1369 if (getUnsigned(SlotNumber))
1370 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001371 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001372 if (!BB)
1373 return error(Twine("use of undefined IR block '%ir-block.") +
1374 Twine(SlotNumber) + "'");
1375 break;
1376 }
1377 default:
1378 llvm_unreachable("The current token should be an IR block reference");
1379 }
1380 return false;
1381}
1382
1383bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1384 assert(Token.is(MIToken::kw_blockaddress));
1385 lex();
1386 if (expectAndConsume(MIToken::lparen))
1387 return true;
1388 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001389 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001390 return error("expected a global value");
1391 GlobalValue *GV = nullptr;
1392 if (parseGlobalValue(GV))
1393 return true;
1394 auto *F = dyn_cast<Function>(GV);
1395 if (!F)
1396 return error("expected an IR function reference");
1397 lex();
1398 if (expectAndConsume(MIToken::comma))
1399 return true;
1400 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001401 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001402 return error("expected an IR block reference");
1403 if (parseIRBlock(BB, *F))
1404 return true;
1405 lex();
1406 if (expectAndConsume(MIToken::rparen))
1407 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001408 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001409 if (parseOperandsOffset(Dest))
1410 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001411 return false;
1412}
1413
Alex Lorenzef5c1962015-07-28 23:02:45 +00001414bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1415 assert(Token.is(MIToken::kw_target_index));
1416 lex();
1417 if (expectAndConsume(MIToken::lparen))
1418 return true;
1419 if (Token.isNot(MIToken::Identifier))
1420 return error("expected the name of the target index");
1421 int Index = 0;
1422 if (getTargetIndex(Token.stringValue(), Index))
1423 return error("use of undefined target index '" + Token.stringValue() + "'");
1424 lex();
1425 if (expectAndConsume(MIToken::rparen))
1426 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001427 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001428 if (parseOperandsOffset(Dest))
1429 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001430 return false;
1431}
1432
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001433bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1434 assert(Token.is(MIToken::kw_liveout));
1435 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1436 assert(TRI && "Expected target register info");
1437 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1438 lex();
1439 if (expectAndConsume(MIToken::lparen))
1440 return true;
1441 while (true) {
1442 if (Token.isNot(MIToken::NamedRegister))
1443 return error("expected a named register");
1444 unsigned Reg = 0;
1445 if (parseRegister(Reg))
1446 return true;
1447 lex();
1448 Mask[Reg / 32] |= 1U << (Reg % 32);
1449 // TODO: Report an error if the same register is used more than once.
1450 if (Token.isNot(MIToken::comma))
1451 break;
1452 lex();
1453 }
1454 if (expectAndConsume(MIToken::rparen))
1455 return true;
1456 Dest = MachineOperand::CreateRegLiveOut(Mask);
1457 return false;
1458}
1459
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001460bool MIParser::parseMachineOperand(MachineOperand &Dest,
1461 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001462 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001463 case MIToken::kw_implicit:
1464 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001465 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001466 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001467 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001468 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001469 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001470 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001471 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001472 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001473 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001474 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001475 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001476 case MIToken::IntegerLiteral:
1477 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001478 case MIToken::IntegerType:
1479 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001480 case MIToken::kw_half:
1481 case MIToken::kw_float:
1482 case MIToken::kw_double:
1483 case MIToken::kw_x86_fp80:
1484 case MIToken::kw_fp128:
1485 case MIToken::kw_ppc_fp128:
1486 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001487 case MIToken::MachineBasicBlock:
1488 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001489 case MIToken::StackObject:
1490 return parseStackObjectOperand(Dest);
1491 case MIToken::FixedStackObject:
1492 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001493 case MIToken::GlobalValue:
1494 case MIToken::NamedGlobalValue:
1495 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001496 case MIToken::ConstantPoolItem:
1497 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001498 case MIToken::JumpTableIndex:
1499 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001500 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001501 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00001502 case MIToken::SubRegisterIndex:
1503 return parseSubRegisterIndexOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001504 case MIToken::exclaim:
1505 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001506 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001507 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001508 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001509 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001510 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001511 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001512 case MIToken::kw_blockaddress:
1513 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001514 case MIToken::kw_target_index:
1515 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001516 case MIToken::kw_liveout:
1517 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001518 case MIToken::Error:
1519 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001520 case MIToken::Identifier:
1521 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1522 Dest = MachineOperand::CreateRegMask(RegMask);
1523 lex();
1524 break;
1525 }
1526 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001527 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001528 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001529 return error("expected a machine operand");
1530 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001531 return false;
1532}
1533
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001534bool MIParser::parseMachineOperandAndTargetFlags(
1535 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001536 unsigned TF = 0;
1537 bool HasTargetFlags = false;
1538 if (Token.is(MIToken::kw_target_flags)) {
1539 HasTargetFlags = true;
1540 lex();
1541 if (expectAndConsume(MIToken::lparen))
1542 return true;
1543 if (Token.isNot(MIToken::Identifier))
1544 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001545 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1546 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1547 return error("use of undefined target flag '" + Token.stringValue() +
1548 "'");
1549 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001550 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001551 while (Token.is(MIToken::comma)) {
1552 lex();
1553 if (Token.isNot(MIToken::Identifier))
1554 return error("expected the name of the target flag");
1555 unsigned BitFlag = 0;
1556 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1557 return error("use of undefined target flag '" + Token.stringValue() +
1558 "'");
1559 // TODO: Report an error when using a duplicate bit target flag.
1560 TF |= BitFlag;
1561 lex();
1562 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001563 if (expectAndConsume(MIToken::rparen))
1564 return true;
1565 }
1566 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001567 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001568 return true;
1569 if (!HasTargetFlags)
1570 return false;
1571 if (Dest.isReg())
1572 return error(Loc, "register operands can't have target flags");
1573 Dest.setTargetFlags(TF);
1574 return false;
1575}
1576
Alex Lorenzdc24c172015-08-07 20:21:00 +00001577bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001578 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1579 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001580 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001581 bool IsNegative = Token.is(MIToken::minus);
1582 lex();
1583 if (Token.isNot(MIToken::IntegerLiteral))
1584 return error("expected an integer literal after '" + Sign + "'");
1585 if (Token.integerValue().getMinSignedBits() > 64)
1586 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001587 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001588 if (IsNegative)
1589 Offset = -Offset;
1590 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001591 return false;
1592}
1593
Alex Lorenz620f8912015-08-13 20:33:33 +00001594bool MIParser::parseAlignment(unsigned &Alignment) {
1595 assert(Token.is(MIToken::kw_align));
1596 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001597 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001598 return error("expected an integer literal after 'align'");
1599 if (getUnsigned(Alignment))
1600 return true;
1601 lex();
1602 return false;
1603}
1604
Alex Lorenzdc24c172015-08-07 20:21:00 +00001605bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1606 int64_t Offset = 0;
1607 if (parseOffset(Offset))
1608 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001609 Op.setOffset(Offset);
1610 return false;
1611}
1612
Alex Lorenz36593ac2015-08-19 23:27:07 +00001613bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001614 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001615 case MIToken::NamedIRValue: {
1616 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001617 break;
1618 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001619 case MIToken::IRValue: {
1620 unsigned SlotNumber = 0;
1621 if (getUnsigned(SlotNumber))
1622 return true;
1623 V = getIRValue(SlotNumber);
1624 break;
1625 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001626 case MIToken::NamedGlobalValue:
1627 case MIToken::GlobalValue: {
1628 GlobalValue *GV = nullptr;
1629 if (parseGlobalValue(GV))
1630 return true;
1631 V = GV;
1632 break;
1633 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001634 case MIToken::QuotedIRValue: {
1635 const Constant *C = nullptr;
1636 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1637 return true;
1638 V = C;
1639 break;
1640 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001641 default:
1642 llvm_unreachable("The current token should be an IR block reference");
1643 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001644 if (!V)
1645 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001646 return false;
1647}
1648
1649bool MIParser::getUint64(uint64_t &Result) {
1650 assert(Token.hasIntegerValue());
1651 if (Token.integerValue().getActiveBits() > 64)
1652 return error("expected 64-bit integer (too large)");
1653 Result = Token.integerValue().getZExtValue();
1654 return false;
1655}
1656
Justin Lebar0af80cd2016-07-15 18:26:59 +00001657bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
1658 const auto OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001659 switch (Token.kind()) {
1660 case MIToken::kw_volatile:
1661 Flags |= MachineMemOperand::MOVolatile;
1662 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001663 case MIToken::kw_non_temporal:
1664 Flags |= MachineMemOperand::MONonTemporal;
1665 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001666 case MIToken::kw_invariant:
1667 Flags |= MachineMemOperand::MOInvariant;
1668 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001669 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001670 default:
1671 llvm_unreachable("The current token should be a memory operand flag");
1672 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001673 if (OldFlags == Flags)
1674 // We know that the same flag is specified more than once when the flags
1675 // weren't modified.
1676 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001677 lex();
1678 return false;
1679}
1680
Alex Lorenz91097a32015-08-12 20:33:26 +00001681bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1682 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001683 case MIToken::kw_stack:
1684 PSV = MF.getPSVManager().getStack();
1685 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001686 case MIToken::kw_got:
1687 PSV = MF.getPSVManager().getGOT();
1688 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001689 case MIToken::kw_jump_table:
1690 PSV = MF.getPSVManager().getJumpTable();
1691 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001692 case MIToken::kw_constant_pool:
1693 PSV = MF.getPSVManager().getConstantPool();
1694 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001695 case MIToken::FixedStackObject: {
1696 int FI;
1697 if (parseFixedStackFrameIndex(FI))
1698 return true;
1699 PSV = MF.getPSVManager().getFixedStack(FI);
1700 // The token was already consumed, so use return here instead of break.
1701 return false;
1702 }
Matthias Braun3ef7df92016-06-08 00:47:07 +00001703 case MIToken::StackObject: {
1704 int FI;
1705 if (parseStackFrameIndex(FI))
1706 return true;
1707 PSV = MF.getPSVManager().getFixedStack(FI);
1708 // The token was already consumed, so use return here instead of break.
1709 return false;
1710 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001711 case MIToken::kw_call_entry: {
1712 lex();
1713 switch (Token.kind()) {
1714 case MIToken::GlobalValue:
1715 case MIToken::NamedGlobalValue: {
1716 GlobalValue *GV = nullptr;
1717 if (parseGlobalValue(GV))
1718 return true;
1719 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1720 break;
1721 }
1722 case MIToken::ExternalSymbol:
1723 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1724 MF.createExternalSymbolName(Token.stringValue()));
1725 break;
1726 default:
1727 return error(
1728 "expected a global value or an external symbol after 'call-entry'");
1729 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001730 break;
1731 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001732 default:
1733 llvm_unreachable("The current token should be pseudo source value");
1734 }
1735 lex();
1736 return false;
1737}
1738
1739bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001740 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001741 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Matthias Braun3ef7df92016-06-08 00:47:07 +00001742 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
1743 Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001744 const PseudoSourceValue *PSV = nullptr;
1745 if (parseMemoryPseudoSourceValue(PSV))
1746 return true;
1747 int64_t Offset = 0;
1748 if (parseOffset(Offset))
1749 return true;
1750 Dest = MachinePointerInfo(PSV, Offset);
1751 return false;
1752 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001753 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1754 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001755 Token.isNot(MIToken::NamedGlobalValue) &&
1756 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001757 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001758 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001759 if (parseIRValue(V))
1760 return true;
1761 if (!V->getType()->isPointerTy())
1762 return error("expected a pointer IR value");
1763 lex();
1764 int64_t Offset = 0;
1765 if (parseOffset(Offset))
1766 return true;
1767 Dest = MachinePointerInfo(V, Offset);
1768 return false;
1769}
1770
Alex Lorenz4af7e612015-08-03 23:08:19 +00001771bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1772 if (expectAndConsume(MIToken::lparen))
1773 return true;
Justin Lebar0af80cd2016-07-15 18:26:59 +00001774 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
Alex Lorenza518b792015-08-04 00:24:45 +00001775 while (Token.isMemoryOperandFlag()) {
1776 if (parseMemoryOperandFlag(Flags))
1777 return true;
1778 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001779 if (Token.isNot(MIToken::Identifier) ||
1780 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1781 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001782 if (Token.stringValue() == "load")
1783 Flags |= MachineMemOperand::MOLoad;
1784 else
1785 Flags |= MachineMemOperand::MOStore;
1786 lex();
1787
1788 if (Token.isNot(MIToken::IntegerLiteral))
1789 return error("expected the size integer literal after memory operation");
1790 uint64_t Size;
1791 if (getUint64(Size))
1792 return true;
1793 lex();
1794
Alex Lorenz91097a32015-08-12 20:33:26 +00001795 MachinePointerInfo Ptr = MachinePointerInfo();
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001796 if (Token.is(MIToken::Identifier)) {
1797 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1798 if (Token.stringValue() != Word)
1799 return error(Twine("expected '") + Word + "'");
1800 lex();
1801
1802 if (parseMachinePointerInfo(Ptr))
1803 return true;
1804 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001805 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001806 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001807 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001808 while (consumeIfPresent(MIToken::comma)) {
1809 switch (Token.kind()) {
1810 case MIToken::kw_align:
1811 if (parseAlignment(BaseAlignment))
1812 return true;
1813 break;
1814 case MIToken::md_tbaa:
1815 lex();
1816 if (parseMDNode(AAInfo.TBAA))
1817 return true;
1818 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001819 case MIToken::md_alias_scope:
1820 lex();
1821 if (parseMDNode(AAInfo.Scope))
1822 return true;
1823 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001824 case MIToken::md_noalias:
1825 lex();
1826 if (parseMDNode(AAInfo.NoAlias))
1827 return true;
1828 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001829 case MIToken::md_range:
1830 lex();
1831 if (parseMDNode(Range))
1832 return true;
1833 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001834 // TODO: Report an error on duplicate metadata nodes.
1835 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001836 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1837 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001838 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001839 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001840 if (expectAndConsume(MIToken::rparen))
1841 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001842 Dest =
1843 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001844 return false;
1845}
1846
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001847void MIParser::initNames2InstrOpCodes() {
1848 if (!Names2InstrOpCodes.empty())
1849 return;
1850 const auto *TII = MF.getSubtarget().getInstrInfo();
1851 assert(TII && "Expected target instruction info");
1852 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1853 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1854}
1855
1856bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1857 initNames2InstrOpCodes();
1858 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1859 if (InstrInfo == Names2InstrOpCodes.end())
1860 return true;
1861 OpCode = InstrInfo->getValue();
1862 return false;
1863}
1864
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001865void MIParser::initNames2Regs() {
1866 if (!Names2Regs.empty())
1867 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001868 // The '%noreg' register is the register 0.
1869 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001870 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1871 assert(TRI && "Expected target register info");
1872 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1873 bool WasInserted =
1874 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1875 .second;
1876 (void)WasInserted;
1877 assert(WasInserted && "Expected registers to be unique case-insensitively");
1878 }
1879}
1880
1881bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1882 initNames2Regs();
1883 auto RegInfo = Names2Regs.find(RegName);
1884 if (RegInfo == Names2Regs.end())
1885 return true;
1886 Reg = RegInfo->getValue();
1887 return false;
1888}
1889
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001890void MIParser::initNames2RegMasks() {
1891 if (!Names2RegMasks.empty())
1892 return;
1893 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1894 assert(TRI && "Expected target register info");
1895 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1896 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1897 assert(RegMasks.size() == RegMaskNames.size());
1898 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1899 Names2RegMasks.insert(
1900 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1901}
1902
1903const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1904 initNames2RegMasks();
1905 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1906 if (RegMaskInfo == Names2RegMasks.end())
1907 return nullptr;
1908 return RegMaskInfo->getValue();
1909}
1910
Alex Lorenz2eacca82015-07-13 23:24:34 +00001911void MIParser::initNames2SubRegIndices() {
1912 if (!Names2SubRegIndices.empty())
1913 return;
1914 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1915 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1916 Names2SubRegIndices.insert(
1917 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1918}
1919
1920unsigned MIParser::getSubRegIndex(StringRef Name) {
1921 initNames2SubRegIndices();
1922 auto SubRegInfo = Names2SubRegIndices.find(Name);
1923 if (SubRegInfo == Names2SubRegIndices.end())
1924 return 0;
1925 return SubRegInfo->getValue();
1926}
1927
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001928static void initSlots2BasicBlocks(
1929 const Function &F,
1930 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1931 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001932 MST.incorporateFunction(F);
1933 for (auto &BB : F) {
1934 if (BB.hasName())
1935 continue;
1936 int Slot = MST.getLocalSlot(&BB);
1937 if (Slot == -1)
1938 continue;
1939 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1940 }
1941}
1942
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001943static const BasicBlock *getIRBlockFromSlot(
1944 unsigned Slot,
1945 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001946 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1947 if (BlockInfo == Slots2BasicBlocks.end())
1948 return nullptr;
1949 return BlockInfo->second;
1950}
1951
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001952const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1953 if (Slots2BasicBlocks.empty())
1954 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1955 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1956}
1957
1958const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1959 if (&F == MF.getFunction())
1960 return getIRBlock(Slot);
1961 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1962 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1963 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1964}
1965
Alex Lorenzdd13be02015-08-19 23:31:05 +00001966static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
1967 DenseMap<unsigned, const Value *> &Slots2Values) {
1968 int Slot = MST.getLocalSlot(V);
1969 if (Slot == -1)
1970 return;
1971 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
1972}
1973
1974/// Creates the mapping from slot numbers to function's unnamed IR values.
1975static void initSlots2Values(const Function &F,
1976 DenseMap<unsigned, const Value *> &Slots2Values) {
1977 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1978 MST.incorporateFunction(F);
1979 for (const auto &Arg : F.args())
1980 mapValueToSlot(&Arg, MST, Slots2Values);
1981 for (const auto &BB : F) {
1982 mapValueToSlot(&BB, MST, Slots2Values);
1983 for (const auto &I : BB)
1984 mapValueToSlot(&I, MST, Slots2Values);
1985 }
1986}
1987
1988const Value *MIParser::getIRValue(unsigned Slot) {
1989 if (Slots2Values.empty())
1990 initSlots2Values(*MF.getFunction(), Slots2Values);
1991 auto ValueInfo = Slots2Values.find(Slot);
1992 if (ValueInfo == Slots2Values.end())
1993 return nullptr;
1994 return ValueInfo->second;
1995}
1996
Alex Lorenzef5c1962015-07-28 23:02:45 +00001997void MIParser::initNames2TargetIndices() {
1998 if (!Names2TargetIndices.empty())
1999 return;
2000 const auto *TII = MF.getSubtarget().getInstrInfo();
2001 assert(TII && "Expected target instruction info");
2002 auto Indices = TII->getSerializableTargetIndices();
2003 for (const auto &I : Indices)
2004 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2005}
2006
2007bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2008 initNames2TargetIndices();
2009 auto IndexInfo = Names2TargetIndices.find(Name);
2010 if (IndexInfo == Names2TargetIndices.end())
2011 return true;
2012 Index = IndexInfo->second;
2013 return false;
2014}
2015
Alex Lorenz49873a82015-08-06 00:44:07 +00002016void MIParser::initNames2DirectTargetFlags() {
2017 if (!Names2DirectTargetFlags.empty())
2018 return;
2019 const auto *TII = MF.getSubtarget().getInstrInfo();
2020 assert(TII && "Expected target instruction info");
2021 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2022 for (const auto &I : Flags)
2023 Names2DirectTargetFlags.insert(
2024 std::make_pair(StringRef(I.second), I.first));
2025}
2026
2027bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2028 initNames2DirectTargetFlags();
2029 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2030 if (FlagInfo == Names2DirectTargetFlags.end())
2031 return true;
2032 Flag = FlagInfo->second;
2033 return false;
2034}
2035
Alex Lorenzf3630112015-08-18 22:52:15 +00002036void MIParser::initNames2BitmaskTargetFlags() {
2037 if (!Names2BitmaskTargetFlags.empty())
2038 return;
2039 const auto *TII = MF.getSubtarget().getInstrInfo();
2040 assert(TII && "Expected target instruction info");
2041 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2042 for (const auto &I : Flags)
2043 Names2BitmaskTargetFlags.insert(
2044 std::make_pair(StringRef(I.second), I.first));
2045}
2046
2047bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2048 initNames2BitmaskTargetFlags();
2049 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2050 if (FlagInfo == Names2BitmaskTargetFlags.end())
2051 return true;
2052 Flag = FlagInfo->second;
2053 return false;
2054}
2055
Matthias Braun83947862016-07-13 22:23:23 +00002056bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2057 StringRef Src,
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002058 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002059 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002060}
2061
Matthias Braun83947862016-07-13 22:23:23 +00002062bool llvm::parseMachineInstructions(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002063 StringRef Src, SMDiagnostic &Error) {
2064 return MIParser(PFS, Error, Src).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002065}
Alex Lorenzf09df002015-06-30 18:16:42 +00002066
Matthias Braun83947862016-07-13 22:23:23 +00002067bool llvm::parseMBBReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002068 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +00002069 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002070 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002071}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002072
Matthias Braun83947862016-07-13 22:23:23 +00002073bool llvm::parseNamedRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002074 unsigned &Reg, StringRef Src,
Alex Lorenz9fab3702015-07-14 21:24:41 +00002075 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002076 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002077}
Alex Lorenz12045a42015-07-27 17:42:45 +00002078
Matthias Braun83947862016-07-13 22:23:23 +00002079bool llvm::parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002080 unsigned &Reg, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +00002081 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002082 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +00002083}
Alex Lorenza314d812015-08-18 22:26:26 +00002084
Matthias Braun83947862016-07-13 22:23:23 +00002085bool llvm::parseStackObjectReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002086 int &FI, StringRef Src,
Alex Lorenza314d812015-08-18 22:26:26 +00002087 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002088 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
Alex Lorenza314d812015-08-18 22:26:26 +00002089}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002090
Matthias Braun83947862016-07-13 22:23:23 +00002091bool llvm::parseMDNode(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002092 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2093 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002094}