blob: 8937ee81c6a39a06abe82f96f6b55df06b46ea1a [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)) {
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000976 MachineRegisterInfo &MRI = MF.getRegInfo();
977
Quentin Colombet2c646962016-06-08 23:27:46 +0000978 // Virtual registers may have a size with GlobalISel.
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000979 if (!TargetRegisterInfo::isVirtualRegister(Reg))
980 return error("unexpected size on physical register");
Ahmed Bougacha5a59b242016-07-19 19:48:36 +0000981 if (MRI.getRegClassOrRegBank(Reg).is<const TargetRegisterClass *>())
982 return error("unexpected size on non-generic virtual register");
983
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000984 unsigned Size;
985 if (parseSize(Size))
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000986 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000987
Quentin Colombet2a831fb2016-03-07 21:48:43 +0000988 MRI.setSize(Reg, Size);
Quentin Colombet2c646962016-06-08 23:27:46 +0000989 } else if (PFS.GenericVRegs.count(Reg)) {
990 // Generic virtual registers must have a size.
991 // If we end up here this means the size hasn't been specified and
992 // this is bad!
993 return error("generic virtual registers must have a size");
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000994 }
Alex Lorenz495ad872015-07-08 21:23:34 +0000995 Dest = MachineOperand::CreateReg(
996 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +0000997 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +0000998 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
999 Flags & RegState::InternalRead);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001000 return false;
1001}
1002
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001003bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1004 assert(Token.is(MIToken::IntegerLiteral));
1005 const APSInt &Int = Token.integerValue();
1006 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +00001007 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001008 Dest = MachineOperand::CreateImm(Int.getExtValue());
1009 lex();
1010 return false;
1011}
1012
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001013bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1014 const Constant *&C) {
1015 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001016 SMDiagnostic Err;
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001017 C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
Matthias Braune35861d2016-07-13 23:27:50 +00001018 &PFS.IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001019 if (!C)
1020 return error(Loc + Err.getColumnNo(), Err.getMessage());
1021 return false;
1022}
1023
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001024bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1025 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1026 return true;
1027 lex();
1028 return false;
1029}
1030
Quentin Colombet85199672016-03-08 00:20:48 +00001031bool MIParser::parseIRType(StringRef::iterator Loc, StringRef StringValue,
Quentin Colombet287c6bb2016-03-08 00:57:31 +00001032 unsigned &Read, Type *&Ty) {
Quentin Colombet85199672016-03-08 00:20:48 +00001033 auto Source = StringValue.str(); // The source has to be null terminated.
1034 SMDiagnostic Err;
Quentin Colombet287c6bb2016-03-08 00:57:31 +00001035 Ty = parseTypeAtBeginning(Source.c_str(), Read, Err,
Matthias Braune35861d2016-07-13 23:27:50 +00001036 *MF.getFunction()->getParent(), &PFS.IRSlots);
Quentin Colombet85199672016-03-08 00:20:48 +00001037 if (!Ty)
1038 return error(Loc + Err.getColumnNo(), Err.getMessage());
1039 return false;
1040}
1041
Quentin Colombet287c6bb2016-03-08 00:57:31 +00001042bool MIParser::parseIRType(StringRef::iterator Loc, Type *&Ty,
1043 bool MustBeSized) {
1044 // At this point we enter in the IR world, i.e., to get the correct type,
1045 // we need to hand off the whole string, not just the current token.
1046 // E.g., <4 x i64> would give '<' as a token and there is not much
1047 // the IR parser can do with that.
1048 unsigned Read = 0;
1049 if (parseIRType(Loc, StringRef(Loc), Read, Ty))
Quentin Colombet85199672016-03-08 00:20:48 +00001050 return true;
Quentin Colombet287c6bb2016-03-08 00:57:31 +00001051 // The type must be sized, otherwise there is not much the backend
1052 // can do with it.
1053 if (MustBeSized && !Ty->isSized())
1054 return error("expected a sized type");
1055 // The next token is Read characters from the Loc.
1056 // However, the current location is not Loc, but Loc + the length of Token.
1057 // Therefore, subtract the length of Token (range().end() - Loc) to the
1058 // number of characters to skip before the next token.
1059 lex(Read - (Token.range().end() - Loc));
Quentin Colombet85199672016-03-08 00:20:48 +00001060 return false;
1061}
1062
Alex Lorenz05e38822015-08-05 18:52:21 +00001063bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1064 assert(Token.is(MIToken::IntegerType));
1065 auto Loc = Token.location();
1066 lex();
1067 if (Token.isNot(MIToken::IntegerLiteral))
1068 return error("expected an integer literal");
1069 const Constant *C = nullptr;
1070 if (parseIRConstant(Loc, C))
1071 return true;
1072 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1073 return false;
1074}
1075
Alex Lorenzad156fb2015-07-31 20:49:21 +00001076bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1077 auto Loc = Token.location();
1078 lex();
1079 if (Token.isNot(MIToken::FloatingPointLiteral))
1080 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001081 const Constant *C = nullptr;
1082 if (parseIRConstant(Loc, C))
1083 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001084 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1085 return false;
1086}
1087
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001088bool MIParser::getUnsigned(unsigned &Result) {
1089 assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1090 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1091 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1092 if (Val64 == Limit)
1093 return error("expected 32-bit integer (too large)");
1094 Result = Val64;
1095 return false;
1096}
1097
Alex Lorenzf09df002015-06-30 18:16:42 +00001098bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001099 assert(Token.is(MIToken::MachineBasicBlock) ||
1100 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001101 unsigned Number;
1102 if (getUnsigned(Number))
1103 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001104 auto MBBInfo = PFS.MBBSlots.find(Number);
1105 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001106 return error(Twine("use of undefined machine basic block #") +
1107 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001108 MBB = MBBInfo->second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001109 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1110 return error(Twine("the name of machine basic block #") + Twine(Number) +
1111 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001112 return false;
1113}
1114
1115bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1116 MachineBasicBlock *MBB;
1117 if (parseMBBReference(MBB))
1118 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001119 Dest = MachineOperand::CreateMBB(MBB);
1120 lex();
1121 return false;
1122}
1123
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001124bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001125 assert(Token.is(MIToken::StackObject));
1126 unsigned ID;
1127 if (getUnsigned(ID))
1128 return true;
1129 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1130 if (ObjectInfo == PFS.StackObjectSlots.end())
1131 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1132 "'");
1133 StringRef Name;
1134 if (const auto *Alloca =
1135 MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
1136 Name = Alloca->getName();
1137 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1138 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1139 "' isn't '" + Token.stringValue() + "'");
1140 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001141 FI = ObjectInfo->second;
1142 return false;
1143}
1144
1145bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1146 int FI;
1147 if (parseStackFrameIndex(FI))
1148 return true;
1149 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001150 return false;
1151}
1152
Alex Lorenzea882122015-08-12 21:17:02 +00001153bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001154 assert(Token.is(MIToken::FixedStackObject));
1155 unsigned ID;
1156 if (getUnsigned(ID))
1157 return true;
1158 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1159 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1160 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1161 Twine(ID) + "'");
1162 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001163 FI = ObjectInfo->second;
1164 return false;
1165}
1166
1167bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1168 int FI;
1169 if (parseFixedStackFrameIndex(FI))
1170 return true;
1171 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001172 return false;
1173}
1174
Alex Lorenz41df7d32015-07-28 17:09:52 +00001175bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001176 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001177 case MIToken::NamedGlobalValue: {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001178 const Module *M = MF.getFunction()->getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001179 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001180 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001181 return error(Twine("use of undefined global value '") + Token.range() +
1182 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001183 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001184 }
1185 case MIToken::GlobalValue: {
1186 unsigned GVIdx;
1187 if (getUnsigned(GVIdx))
1188 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001189 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001190 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1191 "'");
Matthias Braune35861d2016-07-13 23:27:50 +00001192 GV = PFS.IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001193 break;
1194 }
1195 default:
1196 llvm_unreachable("The current token should be a global value");
1197 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001198 return false;
1199}
1200
1201bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1202 GlobalValue *GV = nullptr;
1203 if (parseGlobalValue(GV))
1204 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001205 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001206 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001207 if (parseOperandsOffset(Dest))
1208 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001209 return false;
1210}
1211
Alex Lorenzab980492015-07-20 20:51:18 +00001212bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1213 assert(Token.is(MIToken::ConstantPoolItem));
1214 unsigned ID;
1215 if (getUnsigned(ID))
1216 return true;
1217 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1218 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1219 return error("use of undefined constant '%const." + Twine(ID) + "'");
1220 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001221 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001222 if (parseOperandsOffset(Dest))
1223 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001224 return false;
1225}
1226
Alex Lorenz31d70682015-07-15 23:38:35 +00001227bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1228 assert(Token.is(MIToken::JumpTableIndex));
1229 unsigned ID;
1230 if (getUnsigned(ID))
1231 return true;
1232 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1233 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1234 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1235 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001236 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1237 return false;
1238}
1239
Alex Lorenz6ede3742015-07-21 16:59:53 +00001240bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001241 assert(Token.is(MIToken::ExternalSymbol));
1242 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001243 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001244 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001245 if (parseOperandsOffset(Dest))
1246 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001247 return false;
1248}
1249
Matthias Braunb74eb412016-03-28 18:18:46 +00001250bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1251 assert(Token.is(MIToken::SubRegisterIndex));
1252 StringRef Name = Token.stringValue();
1253 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1254 if (SubRegIndex == 0)
1255 return error(Twine("unknown subregister index '") + Name + "'");
1256 lex();
1257 Dest = MachineOperand::CreateImm(SubRegIndex);
1258 return false;
1259}
1260
Alex Lorenz44f29252015-07-22 21:07:04 +00001261bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001262 assert(Token.is(MIToken::exclaim));
1263 auto Loc = Token.location();
1264 lex();
1265 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1266 return error("expected metadata id after '!'");
1267 unsigned ID;
1268 if (getUnsigned(ID))
1269 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001270 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1271 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
Alex Lorenz35e44462015-07-22 17:58:46 +00001272 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1273 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001274 Node = NodeInfo->second.get();
1275 return false;
1276}
1277
1278bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1279 MDNode *Node = nullptr;
1280 if (parseMDNode(Node))
1281 return true;
1282 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001283 return false;
1284}
1285
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001286bool MIParser::parseCFIOffset(int &Offset) {
1287 if (Token.isNot(MIToken::IntegerLiteral))
1288 return error("expected a cfi offset");
1289 if (Token.integerValue().getMinSignedBits() > 32)
1290 return error("expected a 32 bit integer (the cfi offset is too large)");
1291 Offset = (int)Token.integerValue().getExtValue();
1292 lex();
1293 return false;
1294}
1295
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001296bool MIParser::parseCFIRegister(unsigned &Reg) {
1297 if (Token.isNot(MIToken::NamedRegister))
1298 return error("expected a cfi register");
1299 unsigned LLVMReg;
1300 if (parseRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001301 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001302 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1303 assert(TRI && "Expected target register info");
1304 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1305 if (DwarfReg < 0)
1306 return error("invalid DWARF register");
1307 Reg = (unsigned)DwarfReg;
1308 lex();
1309 return false;
1310}
1311
1312bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1313 auto Kind = Token.kind();
1314 lex();
1315 auto &MMI = MF.getMMI();
1316 int Offset;
1317 unsigned Reg;
1318 unsigned CFIIndex;
1319 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001320 case MIToken::kw_cfi_same_value:
1321 if (parseCFIRegister(Reg))
1322 return true;
1323 CFIIndex =
1324 MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1325 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001326 case MIToken::kw_cfi_offset:
1327 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1328 parseCFIOffset(Offset))
1329 return true;
1330 CFIIndex =
1331 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1332 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001333 case MIToken::kw_cfi_def_cfa_register:
1334 if (parseCFIRegister(Reg))
1335 return true;
1336 CFIIndex =
1337 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1338 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001339 case MIToken::kw_cfi_def_cfa_offset:
1340 if (parseCFIOffset(Offset))
1341 return true;
1342 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1343 CFIIndex = MMI.addFrameInst(
1344 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1345 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001346 case MIToken::kw_cfi_def_cfa:
1347 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1348 parseCFIOffset(Offset))
1349 return true;
1350 // NB: MCCFIInstruction::createDefCfa negates the offset.
1351 CFIIndex =
1352 MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1353 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001354 default:
1355 // TODO: Parse the other CFI operands.
1356 llvm_unreachable("The current token should be a cfi operand");
1357 }
1358 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001359 return false;
1360}
1361
Alex Lorenzdeb53492015-07-28 17:28:03 +00001362bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1363 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001364 case MIToken::NamedIRBlock: {
1365 BB = dyn_cast_or_null<BasicBlock>(
1366 F.getValueSymbolTable().lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001367 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001368 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001369 break;
1370 }
1371 case MIToken::IRBlock: {
1372 unsigned SlotNumber = 0;
1373 if (getUnsigned(SlotNumber))
1374 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001375 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001376 if (!BB)
1377 return error(Twine("use of undefined IR block '%ir-block.") +
1378 Twine(SlotNumber) + "'");
1379 break;
1380 }
1381 default:
1382 llvm_unreachable("The current token should be an IR block reference");
1383 }
1384 return false;
1385}
1386
1387bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1388 assert(Token.is(MIToken::kw_blockaddress));
1389 lex();
1390 if (expectAndConsume(MIToken::lparen))
1391 return true;
1392 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001393 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001394 return error("expected a global value");
1395 GlobalValue *GV = nullptr;
1396 if (parseGlobalValue(GV))
1397 return true;
1398 auto *F = dyn_cast<Function>(GV);
1399 if (!F)
1400 return error("expected an IR function reference");
1401 lex();
1402 if (expectAndConsume(MIToken::comma))
1403 return true;
1404 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001405 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001406 return error("expected an IR block reference");
1407 if (parseIRBlock(BB, *F))
1408 return true;
1409 lex();
1410 if (expectAndConsume(MIToken::rparen))
1411 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001412 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001413 if (parseOperandsOffset(Dest))
1414 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001415 return false;
1416}
1417
Alex Lorenzef5c1962015-07-28 23:02:45 +00001418bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1419 assert(Token.is(MIToken::kw_target_index));
1420 lex();
1421 if (expectAndConsume(MIToken::lparen))
1422 return true;
1423 if (Token.isNot(MIToken::Identifier))
1424 return error("expected the name of the target index");
1425 int Index = 0;
1426 if (getTargetIndex(Token.stringValue(), Index))
1427 return error("use of undefined target index '" + Token.stringValue() + "'");
1428 lex();
1429 if (expectAndConsume(MIToken::rparen))
1430 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001431 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001432 if (parseOperandsOffset(Dest))
1433 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001434 return false;
1435}
1436
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001437bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1438 assert(Token.is(MIToken::kw_liveout));
1439 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1440 assert(TRI && "Expected target register info");
1441 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1442 lex();
1443 if (expectAndConsume(MIToken::lparen))
1444 return true;
1445 while (true) {
1446 if (Token.isNot(MIToken::NamedRegister))
1447 return error("expected a named register");
1448 unsigned Reg = 0;
1449 if (parseRegister(Reg))
1450 return true;
1451 lex();
1452 Mask[Reg / 32] |= 1U << (Reg % 32);
1453 // TODO: Report an error if the same register is used more than once.
1454 if (Token.isNot(MIToken::comma))
1455 break;
1456 lex();
1457 }
1458 if (expectAndConsume(MIToken::rparen))
1459 return true;
1460 Dest = MachineOperand::CreateRegLiveOut(Mask);
1461 return false;
1462}
1463
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001464bool MIParser::parseMachineOperand(MachineOperand &Dest,
1465 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001466 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001467 case MIToken::kw_implicit:
1468 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001469 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001470 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001471 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001472 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001473 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001474 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001475 case MIToken::kw_debug_use:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001476 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001477 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001478 case MIToken::VirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001479 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001480 case MIToken::IntegerLiteral:
1481 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001482 case MIToken::IntegerType:
1483 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001484 case MIToken::kw_half:
1485 case MIToken::kw_float:
1486 case MIToken::kw_double:
1487 case MIToken::kw_x86_fp80:
1488 case MIToken::kw_fp128:
1489 case MIToken::kw_ppc_fp128:
1490 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001491 case MIToken::MachineBasicBlock:
1492 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001493 case MIToken::StackObject:
1494 return parseStackObjectOperand(Dest);
1495 case MIToken::FixedStackObject:
1496 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001497 case MIToken::GlobalValue:
1498 case MIToken::NamedGlobalValue:
1499 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00001500 case MIToken::ConstantPoolItem:
1501 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00001502 case MIToken::JumpTableIndex:
1503 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00001504 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00001505 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00001506 case MIToken::SubRegisterIndex:
1507 return parseSubRegisterIndexOperand(Dest);
Alex Lorenz35e44462015-07-22 17:58:46 +00001508 case MIToken::exclaim:
1509 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00001510 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001511 case MIToken::kw_cfi_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001512 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001513 case MIToken::kw_cfi_def_cfa_offset:
Alex Lorenzb1393232015-07-29 18:57:23 +00001514 case MIToken::kw_cfi_def_cfa:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001515 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00001516 case MIToken::kw_blockaddress:
1517 return parseBlockAddressOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00001518 case MIToken::kw_target_index:
1519 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001520 case MIToken::kw_liveout:
1521 return parseLiveoutRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001522 case MIToken::Error:
1523 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001524 case MIToken::Identifier:
1525 if (const auto *RegMask = getRegMask(Token.stringValue())) {
1526 Dest = MachineOperand::CreateRegMask(RegMask);
1527 lex();
1528 break;
1529 }
1530 // fallthrough
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001531 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001532 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001533 return error("expected a machine operand");
1534 }
Alex Lorenz91370c52015-06-22 20:37:46 +00001535 return false;
1536}
1537
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001538bool MIParser::parseMachineOperandAndTargetFlags(
1539 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00001540 unsigned TF = 0;
1541 bool HasTargetFlags = false;
1542 if (Token.is(MIToken::kw_target_flags)) {
1543 HasTargetFlags = true;
1544 lex();
1545 if (expectAndConsume(MIToken::lparen))
1546 return true;
1547 if (Token.isNot(MIToken::Identifier))
1548 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00001549 if (getDirectTargetFlag(Token.stringValue(), TF)) {
1550 if (getBitmaskTargetFlag(Token.stringValue(), TF))
1551 return error("use of undefined target flag '" + Token.stringValue() +
1552 "'");
1553 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001554 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00001555 while (Token.is(MIToken::comma)) {
1556 lex();
1557 if (Token.isNot(MIToken::Identifier))
1558 return error("expected the name of the target flag");
1559 unsigned BitFlag = 0;
1560 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1561 return error("use of undefined target flag '" + Token.stringValue() +
1562 "'");
1563 // TODO: Report an error when using a duplicate bit target flag.
1564 TF |= BitFlag;
1565 lex();
1566 }
Alex Lorenz49873a82015-08-06 00:44:07 +00001567 if (expectAndConsume(MIToken::rparen))
1568 return true;
1569 }
1570 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001571 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00001572 return true;
1573 if (!HasTargetFlags)
1574 return false;
1575 if (Dest.isReg())
1576 return error(Loc, "register operands can't have target flags");
1577 Dest.setTargetFlags(TF);
1578 return false;
1579}
1580
Alex Lorenzdc24c172015-08-07 20:21:00 +00001581bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00001582 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1583 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00001584 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00001585 bool IsNegative = Token.is(MIToken::minus);
1586 lex();
1587 if (Token.isNot(MIToken::IntegerLiteral))
1588 return error("expected an integer literal after '" + Sign + "'");
1589 if (Token.integerValue().getMinSignedBits() > 64)
1590 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00001591 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00001592 if (IsNegative)
1593 Offset = -Offset;
1594 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00001595 return false;
1596}
1597
Alex Lorenz620f8912015-08-13 20:33:33 +00001598bool MIParser::parseAlignment(unsigned &Alignment) {
1599 assert(Token.is(MIToken::kw_align));
1600 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00001601 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00001602 return error("expected an integer literal after 'align'");
1603 if (getUnsigned(Alignment))
1604 return true;
1605 lex();
1606 return false;
1607}
1608
Alex Lorenzdc24c172015-08-07 20:21:00 +00001609bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1610 int64_t Offset = 0;
1611 if (parseOffset(Offset))
1612 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00001613 Op.setOffset(Offset);
1614 return false;
1615}
1616
Alex Lorenz36593ac2015-08-19 23:27:07 +00001617bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001618 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001619 case MIToken::NamedIRValue: {
1620 V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00001621 break;
1622 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001623 case MIToken::IRValue: {
1624 unsigned SlotNumber = 0;
1625 if (getUnsigned(SlotNumber))
1626 return true;
1627 V = getIRValue(SlotNumber);
1628 break;
1629 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001630 case MIToken::NamedGlobalValue:
1631 case MIToken::GlobalValue: {
1632 GlobalValue *GV = nullptr;
1633 if (parseGlobalValue(GV))
1634 return true;
1635 V = GV;
1636 break;
1637 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001638 case MIToken::QuotedIRValue: {
1639 const Constant *C = nullptr;
1640 if (parseIRConstant(Token.location(), Token.stringValue(), C))
1641 return true;
1642 V = C;
1643 break;
1644 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001645 default:
1646 llvm_unreachable("The current token should be an IR block reference");
1647 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00001648 if (!V)
1649 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001650 return false;
1651}
1652
1653bool MIParser::getUint64(uint64_t &Result) {
1654 assert(Token.hasIntegerValue());
1655 if (Token.integerValue().getActiveBits() > 64)
1656 return error("expected 64-bit integer (too large)");
1657 Result = Token.integerValue().getZExtValue();
1658 return false;
1659}
1660
Justin Lebar0af80cd2016-07-15 18:26:59 +00001661bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
1662 const auto OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00001663 switch (Token.kind()) {
1664 case MIToken::kw_volatile:
1665 Flags |= MachineMemOperand::MOVolatile;
1666 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00001667 case MIToken::kw_non_temporal:
1668 Flags |= MachineMemOperand::MONonTemporal;
1669 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001670 case MIToken::kw_invariant:
1671 Flags |= MachineMemOperand::MOInvariant;
1672 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001673 // TODO: parse the target specific memory operand flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001674 default:
1675 llvm_unreachable("The current token should be a memory operand flag");
1676 }
Alex Lorenze86d5152015-08-06 18:26:36 +00001677 if (OldFlags == Flags)
1678 // We know that the same flag is specified more than once when the flags
1679 // weren't modified.
1680 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00001681 lex();
1682 return false;
1683}
1684
Alex Lorenz91097a32015-08-12 20:33:26 +00001685bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1686 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001687 case MIToken::kw_stack:
1688 PSV = MF.getPSVManager().getStack();
1689 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001690 case MIToken::kw_got:
1691 PSV = MF.getPSVManager().getGOT();
1692 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001693 case MIToken::kw_jump_table:
1694 PSV = MF.getPSVManager().getJumpTable();
1695 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001696 case MIToken::kw_constant_pool:
1697 PSV = MF.getPSVManager().getConstantPool();
1698 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001699 case MIToken::FixedStackObject: {
1700 int FI;
1701 if (parseFixedStackFrameIndex(FI))
1702 return true;
1703 PSV = MF.getPSVManager().getFixedStack(FI);
1704 // The token was already consumed, so use return here instead of break.
1705 return false;
1706 }
Matthias Braun3ef7df92016-06-08 00:47:07 +00001707 case MIToken::StackObject: {
1708 int FI;
1709 if (parseStackFrameIndex(FI))
1710 return true;
1711 PSV = MF.getPSVManager().getFixedStack(FI);
1712 // The token was already consumed, so use return here instead of break.
1713 return false;
1714 }
Alex Lorenz0d009642015-08-20 00:12:57 +00001715 case MIToken::kw_call_entry: {
1716 lex();
1717 switch (Token.kind()) {
1718 case MIToken::GlobalValue:
1719 case MIToken::NamedGlobalValue: {
1720 GlobalValue *GV = nullptr;
1721 if (parseGlobalValue(GV))
1722 return true;
1723 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1724 break;
1725 }
1726 case MIToken::ExternalSymbol:
1727 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1728 MF.createExternalSymbolName(Token.stringValue()));
1729 break;
1730 default:
1731 return error(
1732 "expected a global value or an external symbol after 'call-entry'");
1733 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00001734 break;
1735 }
Alex Lorenz91097a32015-08-12 20:33:26 +00001736 default:
1737 llvm_unreachable("The current token should be pseudo source value");
1738 }
1739 lex();
1740 return false;
1741}
1742
1743bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00001744 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001745 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Matthias Braun3ef7df92016-06-08 00:47:07 +00001746 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
1747 Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00001748 const PseudoSourceValue *PSV = nullptr;
1749 if (parseMemoryPseudoSourceValue(PSV))
1750 return true;
1751 int64_t Offset = 0;
1752 if (parseOffset(Offset))
1753 return true;
1754 Dest = MachinePointerInfo(PSV, Offset);
1755 return false;
1756 }
Alex Lorenz36efd382015-08-20 00:20:03 +00001757 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1758 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00001759 Token.isNot(MIToken::NamedGlobalValue) &&
1760 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00001761 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00001762 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00001763 if (parseIRValue(V))
1764 return true;
1765 if (!V->getType()->isPointerTy())
1766 return error("expected a pointer IR value");
1767 lex();
1768 int64_t Offset = 0;
1769 if (parseOffset(Offset))
1770 return true;
1771 Dest = MachinePointerInfo(V, Offset);
1772 return false;
1773}
1774
Alex Lorenz4af7e612015-08-03 23:08:19 +00001775bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1776 if (expectAndConsume(MIToken::lparen))
1777 return true;
Justin Lebar0af80cd2016-07-15 18:26:59 +00001778 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
Alex Lorenza518b792015-08-04 00:24:45 +00001779 while (Token.isMemoryOperandFlag()) {
1780 if (parseMemoryOperandFlag(Flags))
1781 return true;
1782 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001783 if (Token.isNot(MIToken::Identifier) ||
1784 (Token.stringValue() != "load" && Token.stringValue() != "store"))
1785 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001786 if (Token.stringValue() == "load")
1787 Flags |= MachineMemOperand::MOLoad;
1788 else
1789 Flags |= MachineMemOperand::MOStore;
1790 lex();
1791
1792 if (Token.isNot(MIToken::IntegerLiteral))
1793 return error("expected the size integer literal after memory operation");
1794 uint64_t Size;
1795 if (getUint64(Size))
1796 return true;
1797 lex();
1798
Alex Lorenz91097a32015-08-12 20:33:26 +00001799 MachinePointerInfo Ptr = MachinePointerInfo();
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001800 if (Token.is(MIToken::Identifier)) {
1801 const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1802 if (Token.stringValue() != Word)
1803 return error(Twine("expected '") + Word + "'");
1804 lex();
1805
1806 if (parseMachinePointerInfo(Ptr))
1807 return true;
1808 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001809 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00001810 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00001811 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00001812 while (consumeIfPresent(MIToken::comma)) {
1813 switch (Token.kind()) {
1814 case MIToken::kw_align:
1815 if (parseAlignment(BaseAlignment))
1816 return true;
1817 break;
1818 case MIToken::md_tbaa:
1819 lex();
1820 if (parseMDNode(AAInfo.TBAA))
1821 return true;
1822 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00001823 case MIToken::md_alias_scope:
1824 lex();
1825 if (parseMDNode(AAInfo.Scope))
1826 return true;
1827 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00001828 case MIToken::md_noalias:
1829 lex();
1830 if (parseMDNode(AAInfo.NoAlias))
1831 return true;
1832 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00001833 case MIToken::md_range:
1834 lex();
1835 if (parseMDNode(Range))
1836 return true;
1837 break;
Alex Lorenza617c912015-08-17 22:05:15 +00001838 // TODO: Report an error on duplicate metadata nodes.
1839 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00001840 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1841 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00001842 }
Alex Lorenz61420f72015-08-07 20:48:30 +00001843 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001844 if (expectAndConsume(MIToken::rparen))
1845 return true;
Alex Lorenzeb625682015-08-17 22:09:52 +00001846 Dest =
1847 MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
Alex Lorenz4af7e612015-08-03 23:08:19 +00001848 return false;
1849}
1850
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00001851void MIParser::initNames2InstrOpCodes() {
1852 if (!Names2InstrOpCodes.empty())
1853 return;
1854 const auto *TII = MF.getSubtarget().getInstrInfo();
1855 assert(TII && "Expected target instruction info");
1856 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1857 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1858}
1859
1860bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1861 initNames2InstrOpCodes();
1862 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1863 if (InstrInfo == Names2InstrOpCodes.end())
1864 return true;
1865 OpCode = InstrInfo->getValue();
1866 return false;
1867}
1868
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001869void MIParser::initNames2Regs() {
1870 if (!Names2Regs.empty())
1871 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00001872 // The '%noreg' register is the register 0.
1873 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001874 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1875 assert(TRI && "Expected target register info");
1876 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1877 bool WasInserted =
1878 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1879 .second;
1880 (void)WasInserted;
1881 assert(WasInserted && "Expected registers to be unique case-insensitively");
1882 }
1883}
1884
1885bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1886 initNames2Regs();
1887 auto RegInfo = Names2Regs.find(RegName);
1888 if (RegInfo == Names2Regs.end())
1889 return true;
1890 Reg = RegInfo->getValue();
1891 return false;
1892}
1893
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001894void MIParser::initNames2RegMasks() {
1895 if (!Names2RegMasks.empty())
1896 return;
1897 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1898 assert(TRI && "Expected target register info");
1899 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1900 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1901 assert(RegMasks.size() == RegMaskNames.size());
1902 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1903 Names2RegMasks.insert(
1904 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1905}
1906
1907const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1908 initNames2RegMasks();
1909 auto RegMaskInfo = Names2RegMasks.find(Identifier);
1910 if (RegMaskInfo == Names2RegMasks.end())
1911 return nullptr;
1912 return RegMaskInfo->getValue();
1913}
1914
Alex Lorenz2eacca82015-07-13 23:24:34 +00001915void MIParser::initNames2SubRegIndices() {
1916 if (!Names2SubRegIndices.empty())
1917 return;
1918 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1919 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1920 Names2SubRegIndices.insert(
1921 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1922}
1923
1924unsigned MIParser::getSubRegIndex(StringRef Name) {
1925 initNames2SubRegIndices();
1926 auto SubRegInfo = Names2SubRegIndices.find(Name);
1927 if (SubRegInfo == Names2SubRegIndices.end())
1928 return 0;
1929 return SubRegInfo->getValue();
1930}
1931
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001932static void initSlots2BasicBlocks(
1933 const Function &F,
1934 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1935 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001936 MST.incorporateFunction(F);
1937 for (auto &BB : F) {
1938 if (BB.hasName())
1939 continue;
1940 int Slot = MST.getLocalSlot(&BB);
1941 if (Slot == -1)
1942 continue;
1943 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1944 }
1945}
1946
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001947static const BasicBlock *getIRBlockFromSlot(
1948 unsigned Slot,
1949 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00001950 auto BlockInfo = Slots2BasicBlocks.find(Slot);
1951 if (BlockInfo == Slots2BasicBlocks.end())
1952 return nullptr;
1953 return BlockInfo->second;
1954}
1955
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001956const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1957 if (Slots2BasicBlocks.empty())
1958 initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1959 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1960}
1961
1962const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1963 if (&F == MF.getFunction())
1964 return getIRBlock(Slot);
1965 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1966 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1967 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1968}
1969
Alex Lorenzdd13be02015-08-19 23:31:05 +00001970static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
1971 DenseMap<unsigned, const Value *> &Slots2Values) {
1972 int Slot = MST.getLocalSlot(V);
1973 if (Slot == -1)
1974 return;
1975 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
1976}
1977
1978/// Creates the mapping from slot numbers to function's unnamed IR values.
1979static void initSlots2Values(const Function &F,
1980 DenseMap<unsigned, const Value *> &Slots2Values) {
1981 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1982 MST.incorporateFunction(F);
1983 for (const auto &Arg : F.args())
1984 mapValueToSlot(&Arg, MST, Slots2Values);
1985 for (const auto &BB : F) {
1986 mapValueToSlot(&BB, MST, Slots2Values);
1987 for (const auto &I : BB)
1988 mapValueToSlot(&I, MST, Slots2Values);
1989 }
1990}
1991
1992const Value *MIParser::getIRValue(unsigned Slot) {
1993 if (Slots2Values.empty())
1994 initSlots2Values(*MF.getFunction(), Slots2Values);
1995 auto ValueInfo = Slots2Values.find(Slot);
1996 if (ValueInfo == Slots2Values.end())
1997 return nullptr;
1998 return ValueInfo->second;
1999}
2000
Alex Lorenzef5c1962015-07-28 23:02:45 +00002001void MIParser::initNames2TargetIndices() {
2002 if (!Names2TargetIndices.empty())
2003 return;
2004 const auto *TII = MF.getSubtarget().getInstrInfo();
2005 assert(TII && "Expected target instruction info");
2006 auto Indices = TII->getSerializableTargetIndices();
2007 for (const auto &I : Indices)
2008 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2009}
2010
2011bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2012 initNames2TargetIndices();
2013 auto IndexInfo = Names2TargetIndices.find(Name);
2014 if (IndexInfo == Names2TargetIndices.end())
2015 return true;
2016 Index = IndexInfo->second;
2017 return false;
2018}
2019
Alex Lorenz49873a82015-08-06 00:44:07 +00002020void MIParser::initNames2DirectTargetFlags() {
2021 if (!Names2DirectTargetFlags.empty())
2022 return;
2023 const auto *TII = MF.getSubtarget().getInstrInfo();
2024 assert(TII && "Expected target instruction info");
2025 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2026 for (const auto &I : Flags)
2027 Names2DirectTargetFlags.insert(
2028 std::make_pair(StringRef(I.second), I.first));
2029}
2030
2031bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2032 initNames2DirectTargetFlags();
2033 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2034 if (FlagInfo == Names2DirectTargetFlags.end())
2035 return true;
2036 Flag = FlagInfo->second;
2037 return false;
2038}
2039
Alex Lorenzf3630112015-08-18 22:52:15 +00002040void MIParser::initNames2BitmaskTargetFlags() {
2041 if (!Names2BitmaskTargetFlags.empty())
2042 return;
2043 const auto *TII = MF.getSubtarget().getInstrInfo();
2044 assert(TII && "Expected target instruction info");
2045 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2046 for (const auto &I : Flags)
2047 Names2BitmaskTargetFlags.insert(
2048 std::make_pair(StringRef(I.second), I.first));
2049}
2050
2051bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2052 initNames2BitmaskTargetFlags();
2053 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2054 if (FlagInfo == Names2BitmaskTargetFlags.end())
2055 return true;
2056 Flag = FlagInfo->second;
2057 return false;
2058}
2059
Matthias Braun83947862016-07-13 22:23:23 +00002060bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2061 StringRef Src,
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002062 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002063 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002064}
2065
Matthias Braun83947862016-07-13 22:23:23 +00002066bool llvm::parseMachineInstructions(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002067 StringRef Src, SMDiagnostic &Error) {
2068 return MIParser(PFS, Error, Src).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002069}
Alex Lorenzf09df002015-06-30 18:16:42 +00002070
Matthias Braun83947862016-07-13 22:23:23 +00002071bool llvm::parseMBBReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002072 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +00002073 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002074 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002075}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002076
Matthias Braun83947862016-07-13 22:23:23 +00002077bool llvm::parseNamedRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002078 unsigned &Reg, StringRef Src,
Alex Lorenz9fab3702015-07-14 21:24:41 +00002079 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002080 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002081}
Alex Lorenz12045a42015-07-27 17:42:45 +00002082
Matthias Braun83947862016-07-13 22:23:23 +00002083bool llvm::parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002084 unsigned &Reg, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +00002085 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002086 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Reg);
Alex Lorenz12045a42015-07-27 17:42:45 +00002087}
Alex Lorenza314d812015-08-18 22:26:26 +00002088
Matthias Braun83947862016-07-13 22:23:23 +00002089bool llvm::parseStackObjectReference(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002090 int &FI, StringRef Src,
Alex Lorenza314d812015-08-18 22:26:26 +00002091 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002092 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
Alex Lorenza314d812015-08-18 22:26:26 +00002093}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002094
Matthias Braun83947862016-07-13 22:23:23 +00002095bool llvm::parseMDNode(const PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002096 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2097 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002098}